xref: /llvm-project-15.0.7/mlir/lib/IR/Types.cpp (revision bbef51eb)
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 MLIRContext *Type::getContext() const { return getDialect().getContext(); }
20 
21 bool Type::isBF16() const { return isa<BFloat16Type>(); }
22 bool Type::isF16() const { return isa<Float16Type>(); }
23 bool Type::isF32() const { return isa<Float32Type>(); }
24 bool Type::isF64() const { return isa<Float64Type>(); }
25 bool Type::isF80() const { return isa<Float80Type>(); }
26 bool Type::isF128() const { return isa<Float128Type>(); }
27 
28 bool Type::isIndex() const { return isa<IndexType>(); }
29 
30 /// Return true if this is an integer type with the specified width.
31 bool Type::isInteger(unsigned width) const {
32   if (auto intTy = dyn_cast<IntegerType>())
33     return intTy.getWidth() == width;
34   return false;
35 }
36 
37 bool Type::isSignlessInteger() const {
38   if (auto intTy = dyn_cast<IntegerType>())
39     return intTy.isSignless();
40   return false;
41 }
42 
43 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 
49 bool Type::isSignedInteger() const {
50   if (auto intTy = dyn_cast<IntegerType>())
51     return intTy.isSigned();
52   return false;
53 }
54 
55 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 
61 bool Type::isUnsignedInteger() const {
62   if (auto intTy = dyn_cast<IntegerType>())
63     return intTy.isUnsigned();
64   return false;
65 }
66 
67 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 
73 bool Type::isSignlessIntOrIndex() const {
74   return isSignlessInteger() || isa<IndexType>();
75 }
76 
77 bool Type::isSignlessIntOrIndexOrFloat() const {
78   return isSignlessInteger() || isa<IndexType, FloatType>();
79 }
80 
81 bool Type::isSignlessIntOrFloat() const {
82   return isSignlessInteger() || isa<FloatType>();
83 }
84 
85 bool Type::isIntOrIndex() const { return isa<IntegerType>() || isIndex(); }
86 
87 bool Type::isIntOrFloat() const { return isa<IntegerType, FloatType>(); }
88 
89 bool Type::isIntOrIndexOrFloat() const { return isIntOrFloat() || isIndex(); }
90 
91 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