xref: /llvm-project-15.0.7/mlir/lib/IR/Types.cpp (revision 36b538f5)
1 //===- Types.cpp - MLIR Type Classes --------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "mlir/IR/BuiltinTypes.h"
10 #include "mlir/IR/Dialect.h"
11 
12 using namespace mlir;
13 using namespace mlir::detail;
14 
15 //===----------------------------------------------------------------------===//
16 // Type
17 //===----------------------------------------------------------------------===//
18 
getContext() const19 MLIRContext *Type::getContext() const { return getDialect().getContext(); }
20 
isBF16() const21 bool Type::isBF16() const { return isa<BFloat16Type>(); }
isF16() const22 bool Type::isF16() const { return isa<Float16Type>(); }
isF32() const23 bool Type::isF32() const { return isa<Float32Type>(); }
isF64() const24 bool Type::isF64() const { return isa<Float64Type>(); }
isF80() const25 bool Type::isF80() const { return isa<Float80Type>(); }
isF128() const26 bool Type::isF128() const { return isa<Float128Type>(); }
27 
isIndex() const28 bool Type::isIndex() const { return isa<IndexType>(); }
29 
30 /// Return true if this is an integer type with the specified width.
isInteger(unsigned width) const31 bool Type::isInteger(unsigned width) const {
32   if (auto intTy = dyn_cast<IntegerType>())
33     return intTy.getWidth() == width;
34   return false;
35 }
36 
isSignlessInteger() const37 bool Type::isSignlessInteger() const {
38   if (auto intTy = dyn_cast<IntegerType>())
39     return intTy.isSignless();
40   return false;
41 }
42 
isSignlessInteger(unsigned width) const43 bool Type::isSignlessInteger(unsigned width) const {
44   if (auto intTy = dyn_cast<IntegerType>())
45     return intTy.isSignless() && intTy.getWidth() == width;
46   return false;
47 }
48 
isSignedInteger() const49 bool Type::isSignedInteger() const {
50   if (auto intTy = dyn_cast<IntegerType>())
51     return intTy.isSigned();
52   return false;
53 }
54 
isSignedInteger(unsigned width) const55 bool Type::isSignedInteger(unsigned width) const {
56   if (auto intTy = dyn_cast<IntegerType>())
57     return intTy.isSigned() && intTy.getWidth() == width;
58   return false;
59 }
60 
isUnsignedInteger() const61 bool Type::isUnsignedInteger() const {
62   if (auto intTy = dyn_cast<IntegerType>())
63     return intTy.isUnsigned();
64   return false;
65 }
66 
isUnsignedInteger(unsigned width) const67 bool Type::isUnsignedInteger(unsigned width) const {
68   if (auto intTy = dyn_cast<IntegerType>())
69     return intTy.isUnsigned() && intTy.getWidth() == width;
70   return false;
71 }
72 
isSignlessIntOrIndex() const73 bool Type::isSignlessIntOrIndex() const {
74   return isSignlessInteger() || isa<IndexType>();
75 }
76 
isSignlessIntOrIndexOrFloat() const77 bool Type::isSignlessIntOrIndexOrFloat() const {
78   return isSignlessInteger() || isa<IndexType, FloatType>();
79 }
80 
isSignlessIntOrFloat() const81 bool Type::isSignlessIntOrFloat() const {
82   return isSignlessInteger() || isa<FloatType>();
83 }
84 
isIntOrIndex() const85 bool Type::isIntOrIndex() const { return isa<IntegerType>() || isIndex(); }
86 
isIntOrFloat() const87 bool Type::isIntOrFloat() const { return isa<IntegerType, FloatType>(); }
88 
isIntOrIndexOrFloat() const89 bool Type::isIntOrIndexOrFloat() const { return isIntOrFloat() || isIndex(); }
90 
getIntOrFloatBitWidth() const91 unsigned Type::getIntOrFloatBitWidth() const {
92   assert(isIntOrFloat() && "only integers and floats have a bitwidth");
93   if (auto intType = dyn_cast<IntegerType>())
94     return intType.getWidth();
95   return cast<FloatType>().getWidth();
96 }
97