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 
getDialect() const19 Dialect &Type::getDialect() const {
20   return impl->getAbstractType().getDialect();
21 }
22 
getContext() const23 MLIRContext *Type::getContext() const { return getDialect().getContext(); }
24 
isBF16() const25 bool Type::isBF16() const { return isa<BFloat16Type>(); }
isF16() const26 bool Type::isF16() const { return isa<Float16Type>(); }
isF32() const27 bool Type::isF32() const { return isa<Float32Type>(); }
isF64() const28 bool Type::isF64() const { return isa<Float64Type>(); }
29 
isIndex() const30 bool Type::isIndex() const { return isa<IndexType>(); }
31 
32 /// Return true if this is an integer type with the specified width.
isInteger(unsigned width) const33 bool Type::isInteger(unsigned width) const {
34   if (auto intTy = dyn_cast<IntegerType>())
35     return intTy.getWidth() == width;
36   return false;
37 }
38 
isSignlessInteger() const39 bool Type::isSignlessInteger() const {
40   if (auto intTy = dyn_cast<IntegerType>())
41     return intTy.isSignless();
42   return false;
43 }
44 
isSignlessInteger(unsigned width) const45 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 
isSignedInteger() const51 bool Type::isSignedInteger() const {
52   if (auto intTy = dyn_cast<IntegerType>())
53     return intTy.isSigned();
54   return false;
55 }
56 
isSignedInteger(unsigned width) const57 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 
isUnsignedInteger() const63 bool Type::isUnsignedInteger() const {
64   if (auto intTy = dyn_cast<IntegerType>())
65     return intTy.isUnsigned();
66   return false;
67 }
68 
isUnsignedInteger(unsigned width) const69 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 
isSignlessIntOrIndex() const75 bool Type::isSignlessIntOrIndex() const {
76   return isSignlessInteger() || isa<IndexType>();
77 }
78 
isSignlessIntOrIndexOrFloat() const79 bool Type::isSignlessIntOrIndexOrFloat() const {
80   return isSignlessInteger() || isa<IndexType, FloatType>();
81 }
82 
isSignlessIntOrFloat() const83 bool Type::isSignlessIntOrFloat() const {
84   return isSignlessInteger() || isa<FloatType>();
85 }
86 
isIntOrIndex() const87 bool Type::isIntOrIndex() const { return isa<IntegerType>() || isIndex(); }
88 
isIntOrFloat() const89 bool Type::isIntOrFloat() const { return isa<IntegerType, FloatType>(); }
90 
isIntOrIndexOrFloat() const91 bool Type::isIntOrIndexOrFloat() const { return isIntOrFloat() || isIndex(); }
92 
getIntOrFloatBitWidth() const93 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