xref: /llvm-project-15.0.7/mlir/lib/IR/Types.cpp (revision 36b538f5)
149795d16SChris Lattner //===- Types.cpp - MLIR Type Classes --------------------------------------===//
249795d16SChris Lattner //
330857107SMehdi Amini // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
456222a06SMehdi Amini // See https://llvm.org/LICENSE.txt for license information.
556222a06SMehdi Amini // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
649795d16SChris Lattner //
756222a06SMehdi Amini //===----------------------------------------------------------------------===//
849795d16SChris Lattner 
909f7a55fSRiver Riddle #include "mlir/IR/BuiltinTypes.h"
108abc06f3SRiver Riddle #include "mlir/IR/Dialect.h"
114c465a18SRiver Riddle 
1249795d16SChris Lattner using namespace mlir;
134c465a18SRiver Riddle using namespace mlir::detail;
144c465a18SRiver Riddle 
15dbf8538bSRiver Riddle //===----------------------------------------------------------------------===//
16dbf8538bSRiver Riddle // Type
17dbf8538bSRiver Riddle //===----------------------------------------------------------------------===//
18dbf8538bSRiver Riddle 
getContext() const19c73edeecSMahesh Ravishankar MLIRContext *Type::getContext() const { return getDialect().getContext(); }
2049795d16SChris Lattner 
isBF16() const2109f7a55fSRiver Riddle bool Type::isBF16() const { return isa<BFloat16Type>(); }
isF16() const2209f7a55fSRiver Riddle bool Type::isF16() const { return isa<Float16Type>(); }
isF32() const2309f7a55fSRiver Riddle bool Type::isF32() const { return isa<Float32Type>(); }
isF64() const2409f7a55fSRiver Riddle bool Type::isF64() const { return isa<Float64Type>(); }
isF80() const25*cf0173deSValentin Clement bool Type::isF80() const { return isa<Float80Type>(); }
isF128() const26*cf0173deSValentin Clement bool Type::isF128() const { return isa<Float128Type>(); }
27df9bd857SAlex Zinenko 
isIndex() const2809f7a55fSRiver Riddle bool Type::isIndex() const { return isa<IndexType>(); }
2909f7a55fSRiver Riddle 
3009f7a55fSRiver Riddle /// Return true if this is an integer type with the specified width.
isInteger(unsigned width) const3109f7a55fSRiver Riddle bool Type::isInteger(unsigned width) const {
3209f7a55fSRiver Riddle   if (auto intTy = dyn_cast<IntegerType>())
3309f7a55fSRiver Riddle     return intTy.getWidth() == width;
3409f7a55fSRiver Riddle   return false;
35d2cd083fSRiver Riddle }
36d2cd083fSRiver Riddle 
isSignlessInteger() const3709f7a55fSRiver Riddle bool Type::isSignlessInteger() const {
3809f7a55fSRiver Riddle   if (auto intTy = dyn_cast<IntegerType>())
3909f7a55fSRiver Riddle     return intTy.isSignless();
4009f7a55fSRiver Riddle   return false;
414c465a18SRiver Riddle }
424c465a18SRiver Riddle 
isSignlessInteger(unsigned width) const4309f7a55fSRiver Riddle bool Type::isSignlessInteger(unsigned width) const {
4409f7a55fSRiver Riddle   if (auto intTy = dyn_cast<IntegerType>())
4509f7a55fSRiver Riddle     return intTy.isSignless() && intTy.getWidth() == width;
4609f7a55fSRiver Riddle   return false;
474c465a18SRiver Riddle }
484c465a18SRiver Riddle 
isSignedInteger() const4909f7a55fSRiver Riddle bool Type::isSignedInteger() const {
5009f7a55fSRiver Riddle   if (auto intTy = dyn_cast<IntegerType>())
5109f7a55fSRiver Riddle     return intTy.isSigned();
5209f7a55fSRiver Riddle   return false;
532e36e0daSmikeurbach }
542e36e0daSmikeurbach 
isSignedInteger(unsigned width) const5509f7a55fSRiver Riddle bool Type::isSignedInteger(unsigned width) const {
5609f7a55fSRiver Riddle   if (auto intTy = dyn_cast<IntegerType>())
5709f7a55fSRiver Riddle     return intTy.isSigned() && intTy.getWidth() == width;
5809f7a55fSRiver Riddle   return false;
592e36e0daSmikeurbach }
602e36e0daSmikeurbach 
isUnsignedInteger() const6109f7a55fSRiver Riddle bool Type::isUnsignedInteger() const {
6209f7a55fSRiver Riddle   if (auto intTy = dyn_cast<IntegerType>())
6309f7a55fSRiver Riddle     return intTy.isUnsigned();
6409f7a55fSRiver Riddle   return false;
652e36e0daSmikeurbach }
662e36e0daSmikeurbach 
isUnsignedInteger(unsigned width) const6709f7a55fSRiver Riddle bool Type::isUnsignedInteger(unsigned width) const {
6809f7a55fSRiver Riddle   if (auto intTy = dyn_cast<IntegerType>())
6909f7a55fSRiver Riddle     return intTy.isUnsigned() && intTy.getWidth() == width;
7009f7a55fSRiver Riddle   return false;
712e36e0daSmikeurbach }
722e36e0daSmikeurbach 
isSignlessIntOrIndex() const7309f7a55fSRiver Riddle bool Type::isSignlessIntOrIndex() const {
7409f7a55fSRiver Riddle   return isSignlessInteger() || isa<IndexType>();
753b2c5600SRiver Riddle }
763b2c5600SRiver Riddle 
isSignlessIntOrIndexOrFloat() const7709f7a55fSRiver Riddle bool Type::isSignlessIntOrIndexOrFloat() const {
7809f7a55fSRiver Riddle   return isSignlessInteger() || isa<IndexType, FloatType>();
793b3e11daSRiver Riddle }
803b3e11daSRiver Riddle 
isSignlessIntOrFloat() const8109f7a55fSRiver Riddle bool Type::isSignlessIntOrFloat() const {
8209f7a55fSRiver Riddle   return isSignlessInteger() || isa<FloatType>();
833b2c5600SRiver Riddle }
843b2c5600SRiver Riddle 
isIntOrIndex() const8509f7a55fSRiver Riddle bool Type::isIntOrIndex() const { return isa<IntegerType>() || isIndex(); }
863b2c5600SRiver Riddle 
isIntOrFloat() const8709f7a55fSRiver Riddle bool Type::isIntOrFloat() const { return isa<IntegerType, FloatType>(); }
8809f7a55fSRiver Riddle 
isIntOrIndexOrFloat() const8909f7a55fSRiver Riddle bool Type::isIntOrIndexOrFloat() const { return isIntOrFloat() || isIndex(); }
9009f7a55fSRiver Riddle 
getIntOrFloatBitWidth() const9109f7a55fSRiver Riddle unsigned Type::getIntOrFloatBitWidth() const {
9209f7a55fSRiver Riddle   assert(isIntOrFloat() && "only integers and floats have a bitwidth");
9309f7a55fSRiver Riddle   if (auto intType = dyn_cast<IntegerType>())
9409f7a55fSRiver Riddle     return intType.getWidth();
9509f7a55fSRiver Riddle   return cast<FloatType>().getWidth();
963b3e11daSRiver Riddle }
97