Skip to content

Commit 6c34681

Browse files
authored
[CIR][NFC] Simplify BoolAttr builders (#1572)
1 parent 390cf25 commit 6c34681

File tree

7 files changed

+32
-37
lines changed

7 files changed

+32
-37
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
4141

4242
public:
4343
CIRBaseBuilderTy(mlir::MLIRContext &C) : mlir::OpBuilder(&C) {}
44+
CIRBaseBuilderTy(mlir::OpBuilder &B) : mlir::OpBuilder(B) {}
4445

4546
mlir::Value getConstAPSInt(mlir::Location loc, const llvm::APSInt &val) {
4647
auto ty =
@@ -125,9 +126,13 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
125126
}
126127

127128
cir::BoolAttr getCIRBoolAttr(bool state) {
128-
return cir::BoolAttr::get(getContext(), getBoolTy(), state);
129+
return cir::BoolAttr::get(getContext(), state);
129130
}
130131

132+
cir::BoolAttr getTrueAttr() { return getCIRBoolAttr(true); }
133+
134+
cir::BoolAttr getFalseAttr() { return getCIRBoolAttr(false); }
135+
131136
mlir::TypedAttr getZeroAttr(mlir::Type t) {
132137
return cir::ZeroAttr::get(getContext(), t);
133138
}
@@ -148,7 +153,7 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
148153
if (auto methodTy = mlir::dyn_cast<cir::MethodType>(ty))
149154
return getNullMethodAttr(methodTy);
150155
if (mlir::isa<cir::BoolType>(ty)) {
151-
return getCIRBoolAttr(false);
156+
return getFalseAttr();
152157
}
153158
llvm_unreachable("Zero initializer for given type is NYI");
154159
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ def CIR_BoolAttr : CIR_Attr<"Bool", "bool", [TypedAttrInterface]> {
209209
"", "cir::BoolType">:$type,
210210
"bool":$value);
211211

212+
let builders = [
213+
AttrBuilder<(ins "bool":$value), [{
214+
return $_get($_ctxt, cir::BoolType::get($_ctxt), value);
215+
}]>,
216+
];
217+
212218
let assemblyFormat = [{
213219
`<` $value `>`
214220
}];

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,12 @@ def ConstantOp : CIR_Op<"const",
407407
// The constant operation returns a single value of CIR_AnyType.
408408
let results = (outs CIR_AnyType:$res);
409409

410+
let builders = [
411+
OpBuilder<(ins "cir::BoolAttr":$value), [{
412+
build($_builder, $_state, value.getType(), value);
413+
}]>
414+
];
415+
410416
let assemblyFormat = "attr-dict $value";
411417

412418
let hasVerifier = 1;

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,7 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
199199
llvm_unreachable("NYI");
200200
}
201201
mlir::Value VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
202-
mlir::Type Ty = CGF.convertType(E->getType());
203-
return Builder.create<cir::ConstantOp>(
204-
CGF.getLoc(E->getExprLoc()), Ty, Builder.getCIRBoolAttr(E->getValue()));
202+
return Builder.getBool(E->getValue(), CGF.getLoc(E->getExprLoc()));
205203
}
206204

207205
mlir::Value VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
@@ -419,9 +417,7 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
419417
// An interesting aspect of this is that increment is always true.
420418
// Decrement does not have this property.
421419
if (isInc && type->isBooleanType()) {
422-
value = Builder.create<cir::ConstantOp>(CGF.getLoc(E->getExprLoc()),
423-
CGF.convertType(type),
424-
Builder.getCIRBoolAttr(true));
420+
value = Builder.getTrue(CGF.getLoc(E->getExprLoc()));
425421
} else if (type->isIntegerType()) {
426422
QualType promotedType;
427423
bool canPerformLossyDemotionCheck = false;
@@ -2669,19 +2665,15 @@ mlir::Value ScalarExprEmitter::VisitBinLAnd(const clang::BinaryOperator *E) {
26692665
CIRGenFunction::LexicalScope lexScope{CGF, Loc,
26702666
B.getInsertionBlock()};
26712667
CGF.currLexScope->setAsTernary();
2672-
auto res = B.create<cir::ConstantOp>(
2673-
Loc, Builder.getBoolTy(),
2674-
Builder.getAttr<cir::BoolAttr>(Builder.getBoolTy(), true));
2668+
auto res = B.create<cir::ConstantOp>(Loc, Builder.getTrueAttr());
26752669
B.create<cir::YieldOp>(Loc, res.getRes());
26762670
},
26772671
/*falseBuilder*/
26782672
[&](mlir::OpBuilder &b, mlir::Location Loc) {
26792673
CIRGenFunction::LexicalScope lexScope{CGF, Loc,
26802674
b.getInsertionBlock()};
26812675
CGF.currLexScope->setAsTernary();
2682-
auto res = b.create<cir::ConstantOp>(
2683-
Loc, Builder.getBoolTy(),
2684-
Builder.getAttr<cir::BoolAttr>(Builder.getBoolTy(), false));
2676+
auto res = b.create<cir::ConstantOp>(Loc, Builder.getFalseAttr());
26852677
b.create<cir::YieldOp>(Loc, res.getRes());
26862678
});
26872679
B.create<cir::YieldOp>(Loc, res.getResult());
@@ -2690,9 +2682,7 @@ mlir::Value ScalarExprEmitter::VisitBinLAnd(const clang::BinaryOperator *E) {
26902682
[&](mlir::OpBuilder &B, mlir::Location Loc) {
26912683
CIRGenFunction::LexicalScope lexScope{CGF, Loc, B.getInsertionBlock()};
26922684
CGF.currLexScope->setAsTernary();
2693-
auto res = B.create<cir::ConstantOp>(
2694-
Loc, Builder.getBoolTy(),
2695-
Builder.getAttr<cir::BoolAttr>(Builder.getBoolTy(), false));
2685+
auto res = B.create<cir::ConstantOp>(Loc, Builder.getFalseAttr());
26962686
B.create<cir::YieldOp>(Loc, res.getRes());
26972687
});
26982688
return Builder.createZExtOrBitCast(ResOp.getLoc(), ResOp.getResult(), ResTy);
@@ -2738,9 +2728,7 @@ mlir::Value ScalarExprEmitter::VisitBinLOr(const clang::BinaryOperator *E) {
27382728
[&](mlir::OpBuilder &B, mlir::Location Loc) {
27392729
CIRGenFunction::LexicalScope lexScope{CGF, Loc, B.getInsertionBlock()};
27402730
CGF.currLexScope->setAsTernary();
2741-
auto res = B.create<cir::ConstantOp>(
2742-
Loc, Builder.getBoolTy(),
2743-
Builder.getAttr<cir::BoolAttr>(Builder.getBoolTy(), true));
2731+
auto res = B.create<cir::ConstantOp>(Loc, Builder.getTrueAttr());
27442732
B.create<cir::YieldOp>(Loc, res.getRes());
27452733
},
27462734
/*falseBuilder*/
@@ -2763,9 +2751,7 @@ mlir::Value ScalarExprEmitter::VisitBinLOr(const clang::BinaryOperator *E) {
27632751
CIRGenFunction::LexicalScope lexScope{CGF, Loc,
27642752
B.getInsertionBlock()};
27652753
CGF.currLexScope->setAsTernary();
2766-
auto res = B.create<cir::ConstantOp>(
2767-
Loc, Builder.getBoolTy(),
2768-
Builder.getAttr<cir::BoolAttr>(Builder.getBoolTy(), true));
2754+
auto res = B.create<cir::ConstantOp>(Loc, Builder.getTrueAttr());
27692755
B.create<cir::YieldOp>(Loc, res.getRes());
27702756
},
27712757
/*falseBuilder*/
@@ -2782,9 +2768,7 @@ mlir::Value ScalarExprEmitter::VisitBinLOr(const clang::BinaryOperator *E) {
27822768
CIRGenFunction::LexicalScope lexScope{CGF, Loc,
27832769
B.getInsertionBlock()};
27842770
CGF.currLexScope->setAsTernary();
2785-
auto res = b.create<cir::ConstantOp>(
2786-
Loc, Builder.getBoolTy(),
2787-
Builder.getAttr<cir::BoolAttr>(Builder.getBoolTy(), false));
2771+
auto res = b.create<cir::ConstantOp>(Loc, Builder.getFalseAttr());
27882772
b.create<cir::YieldOp>(Loc, res.getRes());
27892773
});
27902774
B.create<cir::YieldOp>(Loc, res.getResult());

clang/lib/CIR/CodeGen/CIRGenStmt.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -947,9 +947,7 @@ mlir::LogicalResult CIRGenFunction::emitForStmt(const ForStmt &S) {
947947
// scalar type.
948948
condVal = evaluateExprAsBool(S.getCond());
949949
} else {
950-
auto boolTy = cir::BoolType::get(b.getContext());
951-
condVal = b.create<cir::ConstantOp>(
952-
loc, boolTy, cir::BoolAttr::get(b.getContext(), boolTy, true));
950+
condVal = b.create<cir::ConstantOp>(loc, builder.getTrueAttr());
953951
}
954952
builder.createCondition(condVal);
955953
},

clang/lib/CIR/Dialect/Transforms/TargetLowering/ItaniumCXXABI.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,9 @@ mlir::Value ItaniumCXXABI::lowerMethodCmp(cir::CmpOp op, mlir::Value loweredLhs,
564564
// - cir.select if %a then true else %b => %a || %b
565565
// TODO: Do we need to invent dedicated "cir.logical_or" and "cir.logical_and"
566566
// operations for this?
567-
auto boolTy = cir::BoolType::get(op.getContext());
568-
mlir::Value trueValue = builder.create<cir::ConstantOp>(
569-
op.getLoc(), boolTy, cir::BoolAttr::get(op.getContext(), boolTy, true));
570-
mlir::Value falseValue = builder.create<cir::ConstantOp>(
571-
op.getLoc(), boolTy, cir::BoolAttr::get(op.getContext(), boolTy, false));
567+
CIRBaseBuilderTy cirBuilder(builder);
568+
mlir::Value trueValue = cirBuilder.getTrue(op.getLoc());
569+
mlir::Value falseValue = cirBuilder.getFalse(op.getLoc());
572570
auto create_and = [&](mlir::Value lhs, mlir::Value rhs) {
573571
return builder.create<cir::SelectOp>(op.getLoc(), lhs, rhs, falseValue);
574572
};

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,9 +1760,7 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
17601760
// during a pass as long as they don't live past the end of the pass.
17611761
attr = op.getValue();
17621762
} else if (mlir::isa<cir::BoolType>(op.getType())) {
1763-
int value = (op.getValue() ==
1764-
cir::BoolAttr::get(getContext(),
1765-
cir::BoolType::get(getContext()), true));
1763+
int value = (op.getValue() == cir::BoolAttr::get(getContext(), true));
17661764
attr = rewriter.getIntegerAttr(typeConverter->convertType(op.getType()),
17671765
value);
17681766
} else if (mlir::isa<cir::IntType>(op.getType())) {

0 commit comments

Comments
 (0)