Skip to content

Commit eacbcbe

Browse files
authored
[CIR] Upstream type bool (llvm#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.
1 parent 4357a66 commit eacbcbe

File tree

10 files changed

+122
-2
lines changed

10 files changed

+122
-2
lines changed

clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#define LLVM_CLANG_CIR_DIALECT_BUILDER_CIRBASEBUILDER_H
1111

1212
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
13+
#include "clang/CIR/Dialect/IR/CIRDialect.h"
14+
#include "clang/CIR/Dialect/IR/CIRTypes.h"
1315

1416
#include "mlir/IR/Builders.h"
1517
#include "mlir/IR/BuiltinTypes.h"
@@ -23,6 +25,14 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
2325
CIRBaseBuilderTy(mlir::MLIRContext &mlirContext)
2426
: mlir::OpBuilder(&mlirContext) {}
2527

28+
cir::ConstantOp getBool(bool state, mlir::Location loc) {
29+
return create<cir::ConstantOp>(loc, getBoolTy(), getCIRBoolAttr(state));
30+
}
31+
cir::ConstantOp getFalse(mlir::Location loc) { return getBool(false, loc); }
32+
cir::ConstantOp getTrue(mlir::Location loc) { return getBool(true, loc); }
33+
34+
cir::BoolType getBoolTy() { return cir::BoolType::get(getContext()); }
35+
2636
cir::PointerType getPointerTo(mlir::Type ty) {
2737
return cir::PointerType::get(getContext(), ty);
2838
}
@@ -31,6 +41,10 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
3141
return getPointerTo(cir::VoidType::get(getContext()));
3242
}
3343

44+
cir::BoolAttr getCIRBoolAttr(bool state) {
45+
return cir::BoolAttr::get(getContext(), getBoolTy(), state);
46+
}
47+
3448
mlir::TypedAttr getConstPtrAttr(mlir::Type type, int64_t value) {
3549
auto valueAttr = mlir::IntegerAttr::get(
3650
mlir::IntegerType::get(type.getContext(), 64), value);

clang/include/clang/CIR/Dialect/IR/CIRAttrs.td

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@ class CIRUnitAttr<string name, string attrMnemonic, list<Trait> traits = []>
3535
let isOptional = 1;
3636
}
3737

38+
//===----------------------------------------------------------------------===//
39+
// BoolAttr
40+
//===----------------------------------------------------------------------===//
41+
42+
def CIR_BoolAttr : CIR_Attr<"Bool", "bool", [TypedAttrInterface]> {
43+
let summary = "Represent true/false for !cir.bool types";
44+
let description = [{
45+
The BoolAttr represents a 'true' or 'false' value.
46+
}];
47+
48+
let parameters = (ins AttributeSelfTypeParameter<
49+
"", "cir::BoolType">:$type,
50+
"bool":$value);
51+
52+
let assemblyFormat = [{
53+
`<` $value `>`
54+
}];
55+
}
56+
3857
//===----------------------------------------------------------------------===//
3958
// IntegerAttr
4059
//===----------------------------------------------------------------------===//

clang/include/clang/CIR/Dialect/IR/CIRTypes.td

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,20 @@ def CIR_PointerType : CIR_Type<"Pointer", "ptr",
266266
}];
267267
}
268268

269+
//===----------------------------------------------------------------------===//
270+
// BoolType
271+
//===----------------------------------------------------------------------===//
272+
273+
def CIR_BoolType :
274+
CIR_Type<"Bool", "bool",
275+
[DeclareTypeInterfaceMethods<DataLayoutTypeInterface>]> {
276+
277+
let summary = "CIR bool type";
278+
let description = [{
279+
`cir.bool` represents C++ bool type.
280+
}];
281+
}
282+
269283
//===----------------------------------------------------------------------===//
270284
// FuncType
271285
//===----------------------------------------------------------------------===//
@@ -355,7 +369,8 @@ def VoidPtr : Type<
355369
//===----------------------------------------------------------------------===//
356370

357371
def CIR_AnyType : AnyTypeOf<[
358-
CIR_VoidType, CIR_IntType, CIR_AnyFloat, CIR_PointerType, CIR_FuncType
372+
CIR_VoidType, CIR_BoolType, CIR_IntType, CIR_AnyFloat, CIR_PointerType,
373+
CIR_FuncType
359374
]>;
360375

361376
#endif // MLIR_CIR_DIALECT_CIR_TYPES

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
5858
cgf.getLoc(e->getExprLoc()), type,
5959
builder.getAttr<cir::IntAttr>(type, e->getValue()));
6060
}
61+
62+
mlir::Value VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *e) {
63+
mlir::Type type = cgf.convertType(e->getType());
64+
return builder.create<cir::ConstantOp>(
65+
cgf.getLoc(e->getExprLoc()), type,
66+
builder.getCIRBoolAttr(e->getValue()));
67+
}
6168
};
6269
} // namespace
6370

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *vd,
141141
if (APValue *value = initDecl->evaluateValue()) {
142142
switch (value->getKind()) {
143143
case APValue::Int: {
144-
initializer = builder.getAttr<cir::IntAttr>(type, value->getInt());
144+
if (mlir::isa<cir::BoolType>(type))
145+
initializer =
146+
builder.getCIRBoolAttr(value->getInt().getZExtValue());
147+
else
148+
initializer = builder.getAttr<cir::IntAttr>(type, value->getInt());
145149
break;
146150
}
147151
case APValue::Float: {

clang/lib/CIR/CodeGen/CIRGenTypes.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
108108
resultType = cgm.VoidTy;
109109
break;
110110

111+
// bool
112+
case BuiltinType::Bool:
113+
resultType = cir::BoolType::get(&getMLIRContext());
114+
break;
115+
111116
// Signed integral types.
112117
case BuiltinType::Char_S:
113118
case BuiltinType::Int:

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ using namespace cir;
2525
//===----------------------------------------------------------------------===//
2626
// CIR Dialect
2727
//===----------------------------------------------------------------------===//
28+
namespace {
29+
struct CIROpAsmDialectInterface : public OpAsmDialectInterface {
30+
using OpAsmDialectInterface::OpAsmDialectInterface;
31+
32+
AliasResult getAlias(Type type, raw_ostream &os) const final {
33+
return AliasResult::NoAlias;
34+
}
35+
36+
AliasResult getAlias(Attribute attr, raw_ostream &os) const final {
37+
if (auto boolAttr = mlir::dyn_cast<cir::BoolAttr>(attr)) {
38+
os << (boolAttr.getValue() ? "true" : "false");
39+
return AliasResult::FinalAlias;
40+
}
41+
return AliasResult::NoAlias;
42+
}
43+
};
44+
} // namespace
2845

2946
void cir::CIRDialect::initialize() {
3047
registerTypes();
@@ -33,6 +50,7 @@ void cir::CIRDialect::initialize() {
3350
#define GET_OP_LIST
3451
#include "clang/CIR/Dialect/IR/CIROps.cpp.inc"
3552
>();
53+
addInterfaces<CIROpAsmDialectInterface>();
3654
}
3755

3856
//===----------------------------------------------------------------------===//
@@ -112,6 +130,13 @@ static LogicalResult checkConstantTypes(mlir::Operation *op, mlir::Type opType,
112130
return success();
113131
}
114132

133+
if (mlir::isa<cir::BoolAttr>(attrType)) {
134+
if (!mlir::isa<cir::BoolType>(opType))
135+
return op->emitOpError("result type (")
136+
<< opType << ") must be '!cir.bool' for '" << attrType << "'";
137+
return success();
138+
}
139+
115140
if (mlir::isa<cir::IntAttr, cir::FPAttr>(attrType)) {
116141
auto at = cast<TypedAttr>(attrType);
117142
if (at.getType() != opType) {

clang/lib/CIR/Dialect/IR/CIRTypes.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,28 @@ llvm::ArrayRef<mlir::Type> FuncType::getReturnTypes() const {
381381

382382
bool FuncType::isVoid() const { return mlir::isa<VoidType>(getReturnType()); }
383383

384+
//===----------------------------------------------------------------------===//
385+
// BoolType
386+
//===----------------------------------------------------------------------===//
387+
388+
llvm::TypeSize
389+
BoolType::getTypeSizeInBits(const ::mlir::DataLayout &dataLayout,
390+
::mlir::DataLayoutEntryListRef params) const {
391+
return llvm::TypeSize::getFixed(8);
392+
}
393+
394+
uint64_t
395+
BoolType::getABIAlignment(const ::mlir::DataLayout &dataLayout,
396+
::mlir::DataLayoutEntryListRef params) const {
397+
return 1;
398+
}
399+
400+
uint64_t
401+
BoolType::getPreferredAlignment(const ::mlir::DataLayout &dataLayout,
402+
::mlir::DataLayoutEntryListRef params) const {
403+
return 1;
404+
}
405+
384406
//===----------------------------------------------------------------------===//
385407
// PointerType Definitions
386408
//===----------------------------------------------------------------------===//

clang/test/CIR/func-simple.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,9 @@ unsigned long long ullfunc() { return 42ull; }
5151
// CHECK: %0 = cir.const #cir.int<42> : !cir.int<u, 64>
5252
// CHECK: cir.return %0 : !cir.int<u, 64>
5353
// CHECK: }
54+
55+
bool boolfunc() { return true; }
56+
// CHECK: cir.func @boolfunc() -> !cir.bool {
57+
// CHECK: %0 = cir.const #true
58+
// CHECK: cir.return %0 : !cir.bool
59+
// CHECK: }

clang/test/CIR/global-var-simple.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ _BitInt(20) sb20;
5858
unsigned _BitInt(48) ub48;
5959
// CHECK: cir.global @ub48 : !cir.int<u, 48>
6060

61+
bool boolfalse = false;
62+
// CHECK: cir.global @boolfalse = #false
63+
6164
_Float16 f16;
6265
// CHECK: cir.global @f16 : !cir.f16
6366

0 commit comments

Comments
 (0)