diff options
author | David Olsen <dolsen@nvidia.com> | 2025-02-25 11:40:15 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-02-25 11:40:15 -0800 |
commit | eacbcbe47744a496ad1651ebd65914f9e6a66f85 (patch) | |
tree | 57000c54c9562bcf04263b8f85e859e625131cbf /clang/lib | |
parent | 4357a6603f2c21f343d500778f71494e865262ac (diff) | |
download | llvm-eacbcbe47744a496ad1651ebd65914f9e6a66f85.zip llvm-eacbcbe47744a496ad1651ebd65914f9e6a66f85.tar.gz llvm-eacbcbe47744a496ad1651ebd65914f9e6a66f85.tar.bz2 |
[CIR] Upstream type `bool` (#128601)
Support the type `bool` and the literals `true` and `false`. Add the
type `cir::BoolType` and the attribute `cir::BoolAttr` to ClangIR. Add
code in all the necessary places in ClangIR CodeGen to handle and to
recognize the type and the attribute.
Add test cases to existing tests func-simple.cpp and
global-var-simple.cpp.
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 7 | ||||
-rw-r--r-- | clang/lib/CIR/CodeGen/CIRGenModule.cpp | 6 | ||||
-rw-r--r-- | clang/lib/CIR/CodeGen/CIRGenTypes.cpp | 5 | ||||
-rw-r--r-- | clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 25 | ||||
-rw-r--r-- | clang/lib/CIR/Dialect/IR/CIRTypes.cpp | 22 |
5 files changed, 64 insertions, 1 deletions
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp index b802705..24a9591 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp @@ -58,6 +58,13 @@ public: cgf.getLoc(e->getExprLoc()), type, builder.getAttr<cir::IntAttr>(type, e->getValue())); } + + mlir::Value VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *e) { + mlir::Type type = cgf.convertType(e->getType()); + return builder.create<cir::ConstantOp>( + cgf.getLoc(e->getExprLoc()), type, + builder.getCIRBoolAttr(e->getValue())); + } }; } // namespace diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index c1d3265..d8acc99 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -141,7 +141,11 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *vd, if (APValue *value = initDecl->evaluateValue()) { switch (value->getKind()) { case APValue::Int: { - initializer = builder.getAttr<cir::IntAttr>(type, value->getInt()); + if (mlir::isa<cir::BoolType>(type)) + initializer = + builder.getCIRBoolAttr(value->getInt().getZExtValue()); + else + initializer = builder.getAttr<cir::IntAttr>(type, value->getInt()); break; } case APValue::Float: { diff --git a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp index 551b43e..16aec10 100644 --- a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp @@ -108,6 +108,11 @@ mlir::Type CIRGenTypes::convertType(QualType type) { resultType = cgm.VoidTy; break; + // bool + case BuiltinType::Bool: + resultType = cir::BoolType::get(&getMLIRContext()); + break; + // Signed integral types. case BuiltinType::Char_S: case BuiltinType::Int: diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp index 10ad7fb..bfc74d4 100644 --- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp @@ -25,6 +25,23 @@ using namespace cir; //===----------------------------------------------------------------------===// // CIR Dialect //===----------------------------------------------------------------------===// +namespace { +struct CIROpAsmDialectInterface : public OpAsmDialectInterface { + using OpAsmDialectInterface::OpAsmDialectInterface; + + AliasResult getAlias(Type type, raw_ostream &os) const final { + return AliasResult::NoAlias; + } + + AliasResult getAlias(Attribute attr, raw_ostream &os) const final { + if (auto boolAttr = mlir::dyn_cast<cir::BoolAttr>(attr)) { + os << (boolAttr.getValue() ? "true" : "false"); + return AliasResult::FinalAlias; + } + return AliasResult::NoAlias; + } +}; +} // namespace void cir::CIRDialect::initialize() { registerTypes(); @@ -33,6 +50,7 @@ void cir::CIRDialect::initialize() { #define GET_OP_LIST #include "clang/CIR/Dialect/IR/CIROps.cpp.inc" >(); + addInterfaces<CIROpAsmDialectInterface>(); } //===----------------------------------------------------------------------===// @@ -112,6 +130,13 @@ static LogicalResult checkConstantTypes(mlir::Operation *op, mlir::Type opType, return success(); } + if (mlir::isa<cir::BoolAttr>(attrType)) { + if (!mlir::isa<cir::BoolType>(opType)) + return op->emitOpError("result type (") + << opType << ") must be '!cir.bool' for '" << attrType << "'"; + return success(); + } + if (mlir::isa<cir::IntAttr, cir::FPAttr>(attrType)) { auto at = cast<TypedAttr>(attrType); if (at.getType() != opType) { diff --git a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp index 48be11b..f334925 100644 --- a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp @@ -382,6 +382,28 @@ llvm::ArrayRef<mlir::Type> FuncType::getReturnTypes() const { bool FuncType::isVoid() const { return mlir::isa<VoidType>(getReturnType()); } //===----------------------------------------------------------------------===// +// BoolType +//===----------------------------------------------------------------------===// + +llvm::TypeSize +BoolType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout, + ::mlir::DataLayoutEntryListRef params) const { + return llvm::TypeSize::getFixed(8); +} + +uint64_t +BoolType::getABIAlignment(const ::mlir::DataLayout &dataLayout, + ::mlir::DataLayoutEntryListRef params) const { + return 1; +} + +uint64_t +BoolType::getPreferredAlignment(const ::mlir::DataLayout &dataLayout, + ::mlir::DataLayoutEntryListRef params) const { + return 1; +} + +//===----------------------------------------------------------------------===// // PointerType Definitions //===----------------------------------------------------------------------===// |