MLIR 22.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
10#include "mlir/IR/Dialect.h"
11
12using namespace mlir;
13using 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
36
37bool Type::isBF16() const { return llvm::isa<BFloat16Type>(*this); }
38bool Type::isF16() const { return llvm::isa<Float16Type>(*this); }
39bool Type::isTF32() const { return llvm::isa<FloatTF32Type>(*this); }
40bool Type::isF32() const { return llvm::isa<Float32Type>(*this); }
41bool Type::isF64() const { return llvm::isa<Float64Type>(*this); }
42bool Type::isF80() const { return llvm::isa<Float80Type>(*this); }
43bool Type::isF128() const { return llvm::isa<Float128Type>(*this); }
44
45bool Type::isFloat() const { return llvm::isa<FloatType>(*this); }
46
47/// Return true if this is a float type with the specified width.
48bool Type::isFloat(unsigned width) const {
49 if (auto fltTy = llvm::dyn_cast<FloatType>(*this))
50 return fltTy.getWidth() == width;
51 return false;
52}
53
54bool Type::isIndex() const { return llvm::isa<IndexType>(*this); }
55
56bool Type::isInteger() const { return llvm::isa<IntegerType>(*this); }
57
58bool Type::isInteger(unsigned width) const {
59 if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
60 return intTy.getWidth() == width;
61 return false;
62}
63
65 if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
66 return intTy.isSignless();
67 return false;
68}
69
70bool Type::isSignlessInteger(unsigned width) const {
71 if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
72 return intTy.isSignless() && intTy.getWidth() == width;
73 return false;
74}
75
77 if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
78 return intTy.isSigned();
79 return false;
80}
81
82bool Type::isSignedInteger(unsigned width) const {
83 if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
84 return intTy.isSigned() && intTy.getWidth() == width;
85 return false;
86}
87
89 if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
90 return intTy.isUnsigned();
91 return false;
92}
93
94bool Type::isUnsignedInteger(unsigned width) const {
95 if (auto intTy = llvm::dyn_cast<IntegerType>(*this))
96 return intTy.isUnsigned() && intTy.getWidth() == width;
97 return false;
98}
99
101 return isSignlessInteger() || llvm::isa<IndexType>(*this);
102}
103
105 return isSignlessInteger() || llvm::isa<IndexType, FloatType>(*this);
106}
107
109 return isSignlessInteger() || llvm::isa<FloatType>(*this);
110}
111
112bool Type::isIntOrIndex() const {
113 return llvm::isa<IntegerType>(*this) || isIndex();
114}
115
116bool Type::isIntOrFloat() const {
117 return llvm::isa<IntegerType, FloatType>(*this);
118}
119
120bool Type::isIntOrIndexOrFloat() const { return isIntOrFloat() || isIndex(); }
121
123 assert(isIntOrFloat() && "only integers and floats have a bitwidth");
124 if (auto intType = llvm::dyn_cast<IntegerType>(*this))
125 return intType.getWidth();
126 return llvm::cast<FloatType>(*this).getWidth();
127}
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 * getContext() const
Definition Dialect.h:52
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
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:41
bool isTF32() const
Definition Types.cpp:39
bool isFloat() const
Return true if this is an float type (with the specified width).
Definition Types.cpp:45
Dialect & getDialect() const
Get the dialect this type is registered to.
Definition Types.h:107
bool isSignlessIntOrIndex() const
Return true if this is a signless integer or index type.
Definition Types.cpp:100
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:76
bool isSignlessInteger() const
Return true if this is a signless integer type (with the specified width).
Definition Types.cpp:64
bool isIndex() const
Definition Types.cpp:54
bool isIntOrIndexOrFloat() const
Return true if this is an integer (of any signedness), index, or float type.
Definition Types.cpp:120
bool isF32() const
Definition Types.cpp:40
bool isUnsignedInteger() const
Return true if this is an unsigned integer type (with the specified width).
Definition Types.cpp:88
bool isIntOrIndex() const
Return true if this is an integer (of any signedness) or an index type.
Definition Types.cpp:112
bool isInteger() const
Return true if this is an integer type (with the specified width).
Definition Types.cpp:56
bool isIntOrFloat() const
Return true if this is an integer (of any signedness) or a float type.
Definition Types.cpp:116
bool isF128() const
Definition Types.cpp:43
bool isF16() const
Definition Types.cpp:38
bool isF80() const
Definition Types.cpp:42
bool isSignlessIntOrFloat() const
Return true of this is a signless integer or a float type.
Definition Types.cpp:108
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition Types.cpp:122
bool isBF16() const
Definition Types.cpp:37
bool isSignlessIntOrIndexOrFloat() const
Return true if this is a signless integer, index, or float type.
Definition Types.cpp:104
AttrTypeReplacer.
Include the generated interface declarations.
llvm::function_ref< Fn > function_ref
Definition LLVM.h:152