xref: /llvm-project-15.0.7/mlir/lib/IR/Types.cpp (revision e60f2cbd)
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 
19 Dialect &Type::getDialect() const {
20   return impl->getAbstractType().getDialect();
21 }
22 
23 MLIRContext *Type::getContext() const { return getDialect().getContext(); }
24 
25 bool Type::isBF16() const { return isa<BFloat16Type>(); }
26 bool Type::isF16() const { return isa<Float16Type>(); }
27 bool Type::isF32() const { return isa<Float32Type>(); }
28 bool Type::isF64() const { return isa<Float64Type>(); }
29 
30 bool Type::isIndex() const { return isa<IndexType>(); }
31 
32 /// Return true if this is an integer type with the specified width.
33 bool Type::isInteger(unsigned width) const {
34   if (auto intTy = dyn_cast<IntegerType>())
35     return intTy.getWidth() == width;
36   return false;
37 }
38 
39 bool Type::isSignlessInteger() const {
40   if (auto intTy = dyn_cast<IntegerType>())
41     return intTy.isSignless();
42   return false;
43 }
44 
45 bool Type::isSignlessInteger(unsigned width) const {
46   if (auto intTy = dyn_cast<IntegerType>())
47     return intTy.isSignless() && intTy.getWidth() == width;
48   return false;
49 }
50 
51 bool Type::isSignedInteger() const {
52   if (auto intTy = dyn_cast<IntegerType>())
53     return intTy.isSigned();
54   return false;
55 }
56 
57 bool Type::isSignedInteger(unsigned width) const {
58   if (auto intTy = dyn_cast<IntegerType>())
59     return intTy.isSigned() && intTy.getWidth() == width;
60   return false;
61 }
62 
63 bool Type::isUnsignedInteger() const {
64   if (auto intTy = dyn_cast<IntegerType>())
65     return intTy.isUnsigned();
66   return false;
67 }
68 
69 bool Type::isUnsignedInteger(unsigned width) const {
70   if (auto intTy = dyn_cast<IntegerType>())
71     return intTy.isUnsigned() && intTy.getWidth() == width;
72   return false;
73 }
74 
75 bool Type::isSignlessIntOrIndex() const {
76   return isSignlessInteger() || isa<IndexType>();
77 }
78 
79 bool Type::isSignlessIntOrIndexOrFloat() const {
80   return isSignlessInteger() || isa<IndexType, FloatType>();
81 }
82 
83 bool Type::isSignlessIntOrFloat() const {
84   return isSignlessInteger() || isa<FloatType>();
85 }
86 
87 bool Type::isIntOrIndex() const { return isa<IntegerType>() || isIndex(); }
88 
89 bool Type::isIntOrFloat() const { return isa<IntegerType, FloatType>(); }
90 
91 bool Type::isIntOrIndexOrFloat() const { return isIntOrFloat() || isIndex(); }
92 
93 unsigned Type::getIntOrFloatBitWidth() const {
94   assert(isIntOrFloat() && "only integers and floats have a bitwidth");
95   if (auto intType = dyn_cast<IntegerType>())
96     return intType.getWidth();
97   return cast<FloatType>().getWidth();
98 }
99