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 getContext() const19MLIRContext *Type::getContext() const { return getDialect().getContext(); } 20 isBF16() const21bool Type::isBF16() const { return isa<BFloat16Type>(); } isF16() const22bool Type::isF16() const { return isa<Float16Type>(); } isF32() const23bool Type::isF32() const { return isa<Float32Type>(); } isF64() const24bool Type::isF64() const { return isa<Float64Type>(); } isF80() const25bool Type::isF80() const { return isa<Float80Type>(); } isF128() const26bool Type::isF128() const { return isa<Float128Type>(); } 27 isIndex() const28bool Type::isIndex() const { return isa<IndexType>(); } 29 30 /// Return true if this is an integer type with the specified width. isInteger(unsigned width) const31bool Type::isInteger(unsigned width) const { 32 if (auto intTy = dyn_cast<IntegerType>()) 33 return intTy.getWidth() == width; 34 return false; 35 } 36 isSignlessInteger() const37bool Type::isSignlessInteger() const { 38 if (auto intTy = dyn_cast<IntegerType>()) 39 return intTy.isSignless(); 40 return false; 41 } 42 isSignlessInteger(unsigned width) const43bool Type::isSignlessInteger(unsigned width) const { 44 if (auto intTy = dyn_cast<IntegerType>()) 45 return intTy.isSignless() && intTy.getWidth() == width; 46 return false; 47 } 48 isSignedInteger() const49bool Type::isSignedInteger() const { 50 if (auto intTy = dyn_cast<IntegerType>()) 51 return intTy.isSigned(); 52 return false; 53 } 54 isSignedInteger(unsigned width) const55bool Type::isSignedInteger(unsigned width) const { 56 if (auto intTy = dyn_cast<IntegerType>()) 57 return intTy.isSigned() && intTy.getWidth() == width; 58 return false; 59 } 60 isUnsignedInteger() const61bool Type::isUnsignedInteger() const { 62 if (auto intTy = dyn_cast<IntegerType>()) 63 return intTy.isUnsigned(); 64 return false; 65 } 66 isUnsignedInteger(unsigned width) const67bool Type::isUnsignedInteger(unsigned width) const { 68 if (auto intTy = dyn_cast<IntegerType>()) 69 return intTy.isUnsigned() && intTy.getWidth() == width; 70 return false; 71 } 72 isSignlessIntOrIndex() const73bool Type::isSignlessIntOrIndex() const { 74 return isSignlessInteger() || isa<IndexType>(); 75 } 76 isSignlessIntOrIndexOrFloat() const77bool Type::isSignlessIntOrIndexOrFloat() const { 78 return isSignlessInteger() || isa<IndexType, FloatType>(); 79 } 80 isSignlessIntOrFloat() const81bool Type::isSignlessIntOrFloat() const { 82 return isSignlessInteger() || isa<FloatType>(); 83 } 84 isIntOrIndex() const85bool Type::isIntOrIndex() const { return isa<IntegerType>() || isIndex(); } 86 isIntOrFloat() const87bool Type::isIntOrFloat() const { return isa<IntegerType, FloatType>(); } 88 isIntOrIndexOrFloat() const89bool Type::isIntOrIndexOrFloat() const { return isIntOrFloat() || isIndex(); } 90 getIntOrFloatBitWidth() const91unsigned Type::getIntOrFloatBitWidth() const { 92 assert(isIntOrFloat() && "only integers and floats have a bitwidth"); 93 if (auto intType = dyn_cast<IntegerType>()) 94 return intType.getWidth(); 95 return cast<FloatType>().getWidth(); 96 } 97