Skip to content

Commit 3e4d5e6

Browse files
committed
[CIR][CIRGen][NFC] Cleanup buildIfOnBoolExpr
1 parent d92b583 commit 3e4d5e6

File tree

3 files changed

+42
-33
lines changed

3 files changed

+42
-33
lines changed

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,8 +2112,7 @@ CIRGenFunction::buildConditionalBlocks(const AbstractConditionalOperator *E,
21122112
auto *trueExpr = E->getTrueExpr();
21132113
auto *falseExpr = E->getFalseExpr();
21142114

2115-
mlir::Value condV =
2116-
CGF.buildOpOnBoolExpr(E->getCond(), loc, trueExpr, falseExpr);
2115+
mlir::Value condV = CGF.buildOpOnBoolExpr(loc, E->getCond());
21172116
SmallVector<mlir::OpBuilder::InsertPoint, 2> insertPoints{};
21182117
mlir::Type yieldTy{};
21192118

@@ -2353,54 +2352,64 @@ bool CIRGenFunction::LValueIsSuitableForInlineAtomic(LValue LV) {
23532352
mlir::LogicalResult CIRGenFunction::buildIfOnBoolExpr(const Expr *cond,
23542353
const Stmt *thenS,
23552354
const Stmt *elseS) {
2355+
// Attempt to be more accurate as possible with IfOp location, generate
2356+
// one fused location that has either 2 or 4 total locations, depending
2357+
// on else's availability.
23562358
auto getStmtLoc = [this](const Stmt &s) {
23572359
return mlir::FusedLoc::get(builder.getContext(),
23582360
{getLoc(s.getSourceRange().getBegin()),
23592361
getLoc(s.getSourceRange().getEnd())});
23602362
};
2361-
23622363
auto thenLoc = getStmtLoc(*thenS);
23632364
std::optional<mlir::Location> elseLoc;
2364-
SmallVector<mlir::Location, 2> ifLocs{thenLoc};
2365-
2366-
if (elseS) {
2365+
if (elseS)
23672366
elseLoc = getStmtLoc(*elseS);
2368-
ifLocs.push_back(*elseLoc);
2369-
}
23702367

2371-
// Attempt to be more accurate as possible with IfOp location, generate
2372-
// one fused location that has either 2 or 4 total locations, depending
2373-
// on else's availability.
2374-
auto loc = mlir::FusedLoc::get(builder.getContext(), ifLocs);
2375-
2376-
// Emit the code with the fully general case.
2377-
mlir::Value condV = buildOpOnBoolExpr(cond, loc, thenS, elseS);
23782368
mlir::LogicalResult resThen = mlir::success(), resElse = mlir::success();
2379-
2380-
builder.create<mlir::cir::IfOp>(
2381-
loc, condV, elseS,
2382-
/*thenBuilder=*/
2369+
buildIfOnBoolExpr(
2370+
cond, /*thenBuilder=*/
23832371
[&](mlir::OpBuilder &, mlir::Location) {
23842372
LexicalScope lexScope{*this, thenLoc, builder.getInsertionBlock()};
23852373
resThen = buildStmt(thenS, /*useCurrentScope=*/true);
23862374
},
2375+
thenLoc,
23872376
/*elseBuilder=*/
23882377
[&](mlir::OpBuilder &, mlir::Location) {
23892378
assert(elseLoc && "Invalid location for elseS.");
23902379
LexicalScope lexScope{*this, *elseLoc, builder.getInsertionBlock()};
23912380
resElse = buildStmt(elseS, /*useCurrentScope=*/true);
2392-
});
2381+
},
2382+
elseLoc);
23932383

23942384
return mlir::LogicalResult::success(resThen.succeeded() &&
23952385
resElse.succeeded());
23962386
}
23972387

2388+
/// Emit an `if` on a boolean condition, filling `then` and `else` into
2389+
/// appropriated regions.
2390+
mlir::cir::IfOp CIRGenFunction::buildIfOnBoolExpr(
2391+
const clang::Expr *cond,
2392+
llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)> thenBuilder,
2393+
mlir::Location thenLoc,
2394+
llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)> elseBuilder,
2395+
std::optional<mlir::Location> elseLoc) {
2396+
2397+
SmallVector<mlir::Location, 2> ifLocs{thenLoc};
2398+
if (elseLoc)
2399+
ifLocs.push_back(*elseLoc);
2400+
auto loc = mlir::FusedLoc::get(builder.getContext(), ifLocs);
2401+
2402+
// Emit the code with the fully general case.
2403+
mlir::Value condV = buildOpOnBoolExpr(loc, cond);
2404+
return builder.create<mlir::cir::IfOp>(loc, condV, elseLoc.has_value(),
2405+
/*thenBuilder=*/thenBuilder,
2406+
/*elseBuilder=*/elseBuilder);
2407+
}
2408+
23982409
/// TODO(cir): PGO data
23992410
/// TODO(cir): see EmitBranchOnBoolExpr for extra ideas).
2400-
mlir::Value CIRGenFunction::buildOpOnBoolExpr(const Expr *cond,
2401-
mlir::Location loc,
2402-
const Stmt *thenS,
2403-
const Stmt *elseS) {
2411+
mlir::Value CIRGenFunction::buildOpOnBoolExpr(mlir::Location loc,
2412+
const Expr *cond) {
24042413
// TODO(CIR): scoped ApplyDebugLocation DL(*this, Cond);
24052414
// TODO(CIR): __builtin_unpredictable and profile counts?
24062415
cond = cond->IgnoreParens();
@@ -2414,17 +2423,13 @@ mlir::Value CIRGenFunction::buildOpOnBoolExpr(const Expr *cond,
24142423
// This should be done in CIR prior to LLVM lowering, if we do now
24152424
// we can make CIR based diagnostics misleading.
24162425
// cir.ternary(!x, t, f) -> cir.ternary(x, f, t)
2417-
// if (CondUOp->getOpcode() == UO_LNot) {
2418-
// buildOpOnBoolExpr(CondUOp->getSubExpr(), loc, elseS, thenS);
2419-
// }
24202426
assert(!UnimplementedFeature::shouldReverseUnaryCondOnBoolExpr());
24212427
}
24222428

24232429
if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(cond)) {
24242430
auto *trueExpr = CondOp->getTrueExpr();
24252431
auto *falseExpr = CondOp->getFalseExpr();
2426-
mlir::Value condV =
2427-
buildOpOnBoolExpr(CondOp->getCond(), loc, trueExpr, falseExpr);
2432+
mlir::Value condV = buildOpOnBoolExpr(loc, CondOp->getCond());
24282433

24292434
auto ternaryOpRes =
24302435
builder

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2139,7 +2139,7 @@ mlir::Value ScalarExprEmitter::VisitAbstractConditionalOperator(
21392139
.getResult();
21402140
}
21412141

2142-
mlir::Value condV = CGF.buildOpOnBoolExpr(condExpr, loc, lhsExpr, rhsExpr);
2142+
mlir::Value condV = CGF.buildOpOnBoolExpr(loc, condExpr);
21432143
CIRGenFunction::ConditionalEvaluation eval(CGF);
21442144
SmallVector<mlir::OpBuilder::InsertPoint, 2> insertPoints{};
21452145
mlir::Type yieldTy{};

clang/lib/CIR/CodeGen/CIRGenFunction.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,13 +1031,17 @@ class CIRGenFunction : public CIRGenTypeCache {
10311031
mlir::LogicalResult buildIfOnBoolExpr(const clang::Expr *cond,
10321032
const clang::Stmt *thenS,
10331033
const clang::Stmt *elseS);
1034+
mlir::cir::IfOp buildIfOnBoolExpr(
1035+
const clang::Expr *cond,
1036+
llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)> thenBuilder,
1037+
mlir::Location thenLoc,
1038+
llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)> elseBuilder,
1039+
std::optional<mlir::Location> elseLoc = {});
10341040
mlir::Value buildTernaryOnBoolExpr(const clang::Expr *cond,
10351041
mlir::Location loc,
10361042
const clang::Stmt *thenS,
10371043
const clang::Stmt *elseS);
1038-
mlir::Value buildOpOnBoolExpr(const clang::Expr *cond, mlir::Location loc,
1039-
const clang::Stmt *thenS,
1040-
const clang::Stmt *elseS);
1044+
mlir::Value buildOpOnBoolExpr(mlir::Location loc, const clang::Expr *cond);
10411045

10421046
class ConstantEmission {
10431047
// Cannot use mlir::TypedAttr directly here because of bit availability.

0 commit comments

Comments
 (0)