MLIR  17.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 // Type
17 //===----------------------------------------------------------------------===//
18 
19 MLIRContext *Type::getContext() const { return getDialect().getContext(); }
20 
21 bool Type::isFloat8E5M2() const { return isa<Float8E5M2Type>(); }
22 bool Type::isFloat8E4M3FN() const { return isa<Float8E4M3FNType>(); }
23 bool Type::isBF16() const { return isa<BFloat16Type>(); }
24 bool Type::isF16() const { return isa<Float16Type>(); }
25 bool Type::isF32() const { return isa<Float32Type>(); }
26 bool Type::isF64() const { return isa<Float64Type>(); }
27 bool Type::isF80() const { return isa<Float80Type>(); }
28 bool Type::isF128() const { return isa<Float128Type>(); }
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 
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 
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 
76  return isSignlessInteger() || isa<IndexType>();
77 }
78 
80  return isSignlessInteger() || isa<IndexType, FloatType>();
81 }
82 
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 }
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:56
bool isF64() const
Definition: Types.cpp:26
bool isInteger(unsigned width) const
Return true if this is an integer type with the specified width.
Definition: Types.cpp:33
bool isSignlessIntOrIndex() const
Return true if this is a signless integer or index type.
Definition: Types.cpp:75
MLIRContext * getContext() const
Return the MLIRContext in which this type was uniqued.
Definition: Types.cpp:19
bool isSignedInteger() const
Return true if this is a signed integer type (with the specified width).
Definition: Types.cpp:51
bool isFloat8E4M3FN() const
Definition: Types.cpp:22
bool isSignlessInteger() const
Return true if this is a signless integer type (with the specified width).
Definition: Types.cpp:39
bool isIndex() const
Definition: Types.cpp:30
bool isIntOrIndexOrFloat() const
Return true if this is an integer (of any signedness), index, or float type.
Definition: Types.cpp:91
bool isF32() const
Definition: Types.cpp:25
bool isUnsignedInteger() const
Return true if this is an unsigned integer type (with the specified width).
Definition: Types.cpp:63
bool isIntOrIndex() const
Return true if this is an integer (of any signedness) or an index type.
Definition: Types.cpp:87
bool isIntOrFloat() const
Return true if this is an integer (of any signedness) or a float type.
Definition: Types.cpp:89
bool isFloat8E5M2() const
Definition: Types.cpp:21
bool isF128() const
Definition: Types.cpp:28
bool isF16() const
Definition: Types.cpp:24
bool isF80() const
Definition: Types.cpp:27
bool isSignlessIntOrFloat() const
Return true of this is a signless integer or a float type.
Definition: Types.cpp:83
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition: Types.cpp:93
bool isBF16() const
Definition: Types.cpp:23
bool isSignlessIntOrIndexOrFloat() const
Return true if this is a signless integer, index, or float type.
Definition: Types.cpp:79
Detect if any of the given parameter types has a sub-element handler.
Include the generated interface declarations.