MLIR  20.0.0git
Types.cpp
Go to the documentation of this file.
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 
20  Type type, function_ref<void(Attribute)> walkAttrsFn,
21  function_ref<void(Type)> walkTypesFn) const {
22  walkImmediateSubElementsFn(type, walkAttrsFn, walkTypesFn);
23 }
24 
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::isFloat4E2M1FN() const { return llvm::isa<Float4E2M1FNType>(*this); }
38 bool Type::isFloat6E2M3FN() const { return llvm::isa<Float6E2M3FNType>(*this); }
39 bool Type::isFloat6E3M2FN() const { return llvm::isa<Float6E3M2FNType>(*this); }
40 bool Type::isFloat8E5M2() const { return llvm::isa<Float8E5M2Type>(*this); }
41 bool Type::isFloat8E4M3() const { return llvm::isa<Float8E4M3Type>(*this); }
42 bool Type::isFloat8E4M3FN() const { return llvm::isa<Float8E4M3FNType>(*this); }
43 bool Type::isFloat8E5M2FNUZ() const {
44  return llvm::isa<Float8E5M2FNUZType>(*this);
45 }
46 bool Type::isFloat8E4M3FNUZ() const {
47  return llvm::isa<Float8E4M3FNUZType>(*this);
48 }
50  return llvm::isa<Float8E4M3B11FNUZType>(*this);
51 }
52 bool Type::isFloat8E8M0FNU() const {
53  return llvm::isa<Float8E8M0FNUType>(*this);
54 }
55 bool Type::isFloat8E3M4() const { return llvm::isa<Float8E3M4Type>(*this); }
56 bool Type::isBF16() const { return llvm::isa<BFloat16Type>(*this); }
57 bool Type::isF16() const { return llvm::isa<Float16Type>(*this); }
58 bool Type::isTF32() const { return llvm::isa<FloatTF32Type>(*this); }
59 bool Type::isF32() const { return llvm::isa<Float32Type>(*this); }
60 bool Type::isF64() const { return llvm::isa<Float64Type>(*this); }
61 bool Type::isF80() const { return llvm::isa<Float80Type>(*this); }
62 bool Type::isF128() const { return llvm::isa<Float128Type>(*this); }
63 
64 bool Type::isIndex() const { return llvm::isa<IndexType>(*this); }
65 
66 bool Type::isInteger() const { return llvm::isa<IntegerType>(*this); }
67 
68 /// Return true if this is an integer type with the specified width.
69 bool Type::isInteger(unsigned width) const {
70  if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
71  return intTy.getWidth() == width;
72  return false;
73 }
74 
76  if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
77  return intTy.isSignless();
78  return false;
79 }
80 
81 bool Type::isSignlessInteger(unsigned width) const {
82  if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
83  return intTy.isSignless() && intTy.getWidth() == width;
84  return false;
85 }
86 
87 bool Type::isSignedInteger() const {
88  if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
89  return intTy.isSigned();
90  return false;
91 }
92 
93 bool Type::isSignedInteger(unsigned width) const {
94  if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
95  return intTy.isSigned() && intTy.getWidth() == width;
96  return false;
97 }
98 
100  if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
101  return intTy.isUnsigned();
102  return false;
103 }
104 
105 bool Type::isUnsignedInteger(unsigned width) const {
106  if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
107  return intTy.isUnsigned() && intTy.getWidth() == width;
108  return false;
109 }
110 
112  return isSignlessInteger() || llvm::isa<IndexType>(*this);
113 }
114 
116  return isSignlessInteger() || llvm::isa<IndexType, FloatType>(*this);
117 }
118 
120  return isSignlessInteger() || llvm::isa<FloatType>(*this);
121 }
122 
123 bool Type::isIntOrIndex() const {
124  return llvm::isa<IntegerType>(*this) || isIndex();
125 }
126 
127 bool Type::isIntOrFloat() const {
128  return llvm::isa<IntegerType, FloatType>(*this);
129 }
130 
131 bool Type::isIntOrIndexOrFloat() const { return isIntOrFloat() || isIndex(); }
132 
133 unsigned Type::getIntOrFloatBitWidth() const {
134  assert(isIntOrFloat() && "only integers and floats have a bitwidth");
135  if (auto intType = llvm::dyn_cast<IntegerType>(*this))
136  return intType.getWidth();
137  return llvm::cast<FloatType>(*this).getWidth();
138 }
Type replaceImmediateSubElements(Type type, ArrayRef< Attribute > replAttrs, ArrayRef< Type > replTypes) const
Replace the immediate sub-elements of the given type.
Definition: Types.cpp:25
void walkImmediateSubElements(Type type, function_ref< void(Attribute)> walkAttrsFn, function_ref< void(Type)> walkTypesFn) const
Walk the immediate sub-elements of the given type.
Definition: Types.cpp:19
Attributes are known-constant values of operations.
Definition: Attributes.h:25
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
bool isF64() const
Definition: Types.cpp:60
bool isTF32() const
Definition: Types.cpp:58
bool isSignlessIntOrIndex() const
Return true if this is a signless integer or index type.
Definition: Types.cpp:111
MLIRContext * getContext() const
Return the MLIRContext in which this type was uniqued.
Definition: Types.cpp:35
bool isSignedInteger() const
Return true if this is a signed integer type (with the specified width).
Definition: Types.cpp:87
bool isFloat8E4M3FN() const
Definition: Types.cpp:42
bool isSignlessInteger() const
Return true if this is a signless integer type (with the specified width).
Definition: Types.cpp:75
bool isIndex() const
Definition: Types.cpp:64
bool isFloat8E3M4() const
Definition: Types.cpp:55
bool isIntOrIndexOrFloat() const
Return true if this is an integer (of any signedness), index, or float type.
Definition: Types.cpp:131
bool isF32() const
Definition: Types.cpp:59
bool isFloat8E4M3FNUZ() const
Definition: Types.cpp:46
bool isUnsignedInteger() const
Return true if this is an unsigned integer type (with the specified width).
Definition: Types.cpp:99
bool isIntOrIndex() const
Return true if this is an integer (of any signedness) or an index type.
Definition: Types.cpp:123
bool isFloat8E4M3B11FNUZ() const
Definition: Types.cpp:49
bool isFloat6E3M2FN() const
Definition: Types.cpp:39
bool isInteger() const
Return true if this is an integer type (with the specified width).
Definition: Types.cpp:66
bool isIntOrFloat() const
Return true if this is an integer (of any signedness) or a float type.
Definition: Types.cpp:127
bool isFloat8E5M2() const
Definition: Types.cpp:40
bool isFloat8E8M0FNU() const
Definition: Types.cpp:52
bool isFloat4E2M1FN() const
Definition: Types.cpp:37
bool isF128() const
Definition: Types.cpp:62
bool isF16() const
Definition: Types.cpp:57
bool isF80() const
Definition: Types.cpp:61
bool isFloat6E2M3FN() const
Definition: Types.cpp:38
bool isSignlessIntOrFloat() const
Return true of this is a signless integer or a float type.
Definition: Types.cpp:119
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition: Types.cpp:133
bool isBF16() const
Definition: Types.cpp:56
bool isFloat8E4M3() const
Definition: Types.cpp:41
bool isFloat8E5M2FNUZ() const
Definition: Types.cpp:43
bool isSignlessIntOrIndexOrFloat() const
Return true if this is a signless integer, index, or float type.
Definition: Types.cpp:115
AttrTypeReplacer.
Include the generated interface declarations.