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 Dialect &Type::getDialect() const { 20 return impl->getAbstractType().getDialect(); 21 } 22 23 MLIRContext *Type::getContext() const { return getDialect().getContext(); } 24 25 bool Type::isBF16() const { return isa<BFloat16Type>(); } 26 bool Type::isF16() const { return isa<Float16Type>(); } 27 bool Type::isF32() const { return isa<Float32Type>(); } 28 bool Type::isF64() const { return isa<Float64Type>(); } 29 bool Type::isF80() const { return isa<Float80Type>(); } 30 bool Type::isF128() const { return isa<Float128Type>(); } 31 32 bool Type::isIndex() const { return isa<IndexType>(); } 33 34 /// Return true if this is an integer type with the specified width. 35 bool Type::isInteger(unsigned width) const { 36 if (auto intTy = dyn_cast<IntegerType>()) 37 return intTy.getWidth() == width; 38 return false; 39 } 40 41 bool Type::isSignlessInteger() const { 42 if (auto intTy = dyn_cast<IntegerType>()) 43 return intTy.isSignless(); 44 return false; 45 } 46 47 bool Type::isSignlessInteger(unsigned width) const { 48 if (auto intTy = dyn_cast<IntegerType>()) 49 return intTy.isSignless() && intTy.getWidth() == width; 50 return false; 51 } 52 53 bool Type::isSignedInteger() const { 54 if (auto intTy = dyn_cast<IntegerType>()) 55 return intTy.isSigned(); 56 return false; 57 } 58 59 bool Type::isSignedInteger(unsigned width) const { 60 if (auto intTy = dyn_cast<IntegerType>()) 61 return intTy.isSigned() && intTy.getWidth() == width; 62 return false; 63 } 64 65 bool Type::isUnsignedInteger() const { 66 if (auto intTy = dyn_cast<IntegerType>()) 67 return intTy.isUnsigned(); 68 return false; 69 } 70 71 bool Type::isUnsignedInteger(unsigned width) const { 72 if (auto intTy = dyn_cast<IntegerType>()) 73 return intTy.isUnsigned() && intTy.getWidth() == width; 74 return false; 75 } 76 77 bool Type::isSignlessIntOrIndex() const { 78 return isSignlessInteger() || isa<IndexType>(); 79 } 80 81 bool Type::isSignlessIntOrIndexOrFloat() const { 82 return isSignlessInteger() || isa<IndexType, FloatType>(); 83 } 84 85 bool Type::isSignlessIntOrFloat() const { 86 return isSignlessInteger() || isa<FloatType>(); 87 } 88 89 bool Type::isIntOrIndex() const { return isa<IntegerType>() || isIndex(); } 90 91 bool Type::isIntOrFloat() const { return isa<IntegerType, FloatType>(); } 92 93 bool Type::isIntOrIndexOrFloat() const { return isIntOrFloat() || isIndex(); } 94 95 unsigned Type::getIntOrFloatBitWidth() const { 96 assert(isIntOrFloat() && "only integers and floats have a bitwidth"); 97 if (auto intType = dyn_cast<IntegerType>()) 98 return intType.getWidth(); 99 return cast<FloatType>().getWidth(); 100 } 101