xref: /llvm-project/mlir/lib/IR/Types.cpp (revision 7a77f14c0abfbecbfb800ea8d974e66d81ee516a)
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 // AbstractType
17 //===----------------------------------------------------------------------===//
18 
19 void AbstractType::walkImmediateSubElements(
20     Type type, function_ref<void(Attribute)> walkAttrsFn,
21     function_ref<void(Type)> walkTypesFn) const {
22   walkImmediateSubElementsFn(type, walkAttrsFn, walkTypesFn);
23 }
24 
25 Type AbstractType::replaceImmediateSubElements(Type type,
26                                                ArrayRef<Attribute> replAttrs,
27                                                ArrayRef<Type> replTypes) const {
28   return replaceImmediateSubElementsFn(type, replAttrs, replTypes);
29 }
30 
31 //===----------------------------------------------------------------------===//
32 // Type
33 //===----------------------------------------------------------------------===//
34 
35 MLIRContext *Type::getContext() const { return getDialect().getContext(); }
36 
37 bool Type::isBF16() const { return llvm::isa<BFloat16Type>(*this); }
38 bool Type::isF16() const { return llvm::isa<Float16Type>(*this); }
39 bool Type::isTF32() const { return llvm::isa<FloatTF32Type>(*this); }
40 bool Type::isF32() const { return llvm::isa<Float32Type>(*this); }
41 bool Type::isF64() const { return llvm::isa<Float64Type>(*this); }
42 bool Type::isF80() const { return llvm::isa<Float80Type>(*this); }
43 bool Type::isF128() const { return llvm::isa<Float128Type>(*this); }
44 
45 bool Type::isIndex() const { return llvm::isa<IndexType>(*this); }
46 
47 bool Type::isInteger() const { return llvm::isa<IntegerType>(*this); }
48 
49 /// Return true if this is an integer type with the specified width.
50 bool Type::isInteger(unsigned width) const {
51   if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
52     return intTy.getWidth() == width;
53   return false;
54 }
55 
56 bool Type::isSignlessInteger() const {
57   if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
58     return intTy.isSignless();
59   return false;
60 }
61 
62 bool Type::isSignlessInteger(unsigned width) const {
63   if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
64     return intTy.isSignless() && intTy.getWidth() == width;
65   return false;
66 }
67 
68 bool Type::isSignedInteger() const {
69   if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
70     return intTy.isSigned();
71   return false;
72 }
73 
74 bool Type::isSignedInteger(unsigned width) const {
75   if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
76     return intTy.isSigned() && intTy.getWidth() == width;
77   return false;
78 }
79 
80 bool Type::isUnsignedInteger() const {
81   if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
82     return intTy.isUnsigned();
83   return false;
84 }
85 
86 bool Type::isUnsignedInteger(unsigned width) const {
87   if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
88     return intTy.isUnsigned() && intTy.getWidth() == width;
89   return false;
90 }
91 
92 bool Type::isSignlessIntOrIndex() const {
93   return isSignlessInteger() || llvm::isa<IndexType>(*this);
94 }
95 
96 bool Type::isSignlessIntOrIndexOrFloat() const {
97   return isSignlessInteger() || llvm::isa<IndexType, FloatType>(*this);
98 }
99 
100 bool Type::isSignlessIntOrFloat() const {
101   return isSignlessInteger() || llvm::isa<FloatType>(*this);
102 }
103 
104 bool Type::isIntOrIndex() const {
105   return llvm::isa<IntegerType>(*this) || isIndex();
106 }
107 
108 bool Type::isIntOrFloat() const {
109   return llvm::isa<IntegerType, FloatType>(*this);
110 }
111 
112 bool Type::isIntOrIndexOrFloat() const { return isIntOrFloat() || isIndex(); }
113 
114 unsigned Type::getIntOrFloatBitWidth() const {
115   assert(isIntOrFloat() && "only integers and floats have a bitwidth");
116   if (auto intType = llvm::dyn_cast<IntegerType>(*this))
117     return intType.getWidth();
118   return llvm::cast<FloatType>(*this).getWidth();
119 }
120