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
12using namespace mlir;
13using namespace mlir::detail;
14
15//===----------------------------------------------------------------------===//
16// AbstractType
17//===----------------------------------------------------------------------===//
18
19void AbstractType::walkImmediateSubElements(
20 Type type, function_ref<void(Attribute)> walkAttrsFn,
21 function_ref<void(Type)> walkTypesFn) const {
22 walkImmediateSubElementsFn(type, walkAttrsFn, walkTypesFn);
23}
24
25Type AbstractType::replaceImmediateSubElements(Type type,
26 ArrayRef<Attribute> replAttrs,
27 ArrayRef<Type> replTypes) const {
28 return replaceImmediateSubElementsFn(type, replAttrs, replTypes);
29}
30
31//===----------------------------------------------------------------------===//
32// Type
33//===----------------------------------------------------------------------===//
34
35MLIRContext *Type::getContext() const { return getDialect().getContext(); }
36
37bool Type::isBF16() const { return llvm::isa<BFloat16Type>(Val: *this); }
38bool Type::isF16() const { return llvm::isa<Float16Type>(Val: *this); }
39bool Type::isTF32() const { return llvm::isa<FloatTF32Type>(Val: *this); }
40bool Type::isF32() const { return llvm::isa<Float32Type>(Val: *this); }
41bool Type::isF64() const { return llvm::isa<Float64Type>(Val: *this); }
42bool Type::isF80() const { return llvm::isa<Float80Type>(Val: *this); }
43bool Type::isF128() const { return llvm::isa<Float128Type>(Val: *this); }
44bool Type::isF8E4M3FN() const { return llvm::isa<Float8E4M3FNType>(Val: *this); }
45bool Type::isF8E5M2() const { return llvm::isa<Float8E5M2Type>(Val: *this); }
46
47bool Type::isFloat() const { return llvm::isa<FloatType>(Val: *this); }
48
49/// Return true if this is a float type with the specified width.
50bool Type::isFloat(unsigned width) const {
51 if (auto fltTy = llvm::dyn_cast<FloatType>(Val: *this))
52 return fltTy.getWidth() == width;
53 return false;
54}
55
56bool Type::isIndex() const { return llvm::isa<IndexType>(Val: *this); }
57
58bool Type::isInteger() const { return llvm::isa<IntegerType>(Val: *this); }
59
60bool Type::isInteger(unsigned width) const {
61 if (auto intTy = llvm::dyn_cast<IntegerType>(Val: *this))
62 return intTy.getWidth() == width;
63 return false;
64}
65
66bool Type::isSignlessInteger() const {
67 if (auto intTy = llvm::dyn_cast<IntegerType>(Val: *this))
68 return intTy.isSignless();
69 return false;
70}
71
72bool Type::isSignlessInteger(unsigned width) const {
73 if (auto intTy = llvm::dyn_cast<IntegerType>(Val: *this))
74 return intTy.isSignless() && intTy.getWidth() == width;
75 return false;
76}
77
78bool Type::isSignedInteger() const {
79 if (auto intTy = llvm::dyn_cast<IntegerType>(Val: *this))
80 return intTy.isSigned();
81 return false;
82}
83
84bool Type::isSignedInteger(unsigned width) const {
85 if (auto intTy = llvm::dyn_cast<IntegerType>(Val: *this))
86 return intTy.isSigned() && intTy.getWidth() == width;
87 return false;
88}
89
90bool Type::isUnsignedInteger() const {
91 if (auto intTy = llvm::dyn_cast<IntegerType>(Val: *this))
92 return intTy.isUnsigned();
93 return false;
94}
95
96bool Type::isUnsignedInteger(unsigned width) const {
97 if (auto intTy = llvm::dyn_cast<IntegerType>(Val: *this))
98 return intTy.isUnsigned() && intTy.getWidth() == width;
99 return false;
100}
101
102bool Type::isSignlessIntOrIndex() const {
103 return isSignlessInteger() || llvm::isa<IndexType>(Val: *this);
104}
105
106bool Type::isSignlessIntOrIndexOrFloat() const {
107 return isSignlessInteger() || llvm::isa<IndexType, FloatType>(Val: *this);
108}
109
110bool Type::isSignlessIntOrFloat() const {
111 return isSignlessInteger() || llvm::isa<FloatType>(Val: *this);
112}
113
114bool Type::isIntOrIndex() const {
115 return llvm::isa<IntegerType>(Val: *this) || isIndex();
116}
117
118bool Type::isIntOrFloat() const {
119 return llvm::isa<IntegerType, FloatType>(Val: *this);
120}
121
122bool Type::isIntOrIndexOrFloat() const { return isIntOrFloat() || isIndex(); }
123
124unsigned Type::getIntOrFloatBitWidth() const {
125 assert(isIntOrFloat() && "only integers and floats have a bitwidth");
126 if (auto intType = llvm::dyn_cast<IntegerType>(Val: *this))
127 return intType.getWidth();
128 return llvm::cast<FloatType>(Val: *this).getWidth();
129}
130