@@ -2112,8 +2112,7 @@ CIRGenFunction::buildConditionalBlocks(const AbstractConditionalOperator *E,
2112
2112
auto *trueExpr = E->getTrueExpr ();
2113
2113
auto *falseExpr = E->getFalseExpr ();
2114
2114
2115
- mlir::Value condV =
2116
- CGF.buildOpOnBoolExpr (E->getCond (), loc, trueExpr, falseExpr);
2115
+ mlir::Value condV = CGF.buildOpOnBoolExpr (loc, E->getCond ());
2117
2116
SmallVector<mlir::OpBuilder::InsertPoint, 2 > insertPoints{};
2118
2117
mlir::Type yieldTy{};
2119
2118
@@ -2353,54 +2352,64 @@ bool CIRGenFunction::LValueIsSuitableForInlineAtomic(LValue LV) {
2353
2352
mlir::LogicalResult CIRGenFunction::buildIfOnBoolExpr (const Expr *cond,
2354
2353
const Stmt *thenS,
2355
2354
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.
2356
2358
auto getStmtLoc = [this ](const Stmt &s) {
2357
2359
return mlir::FusedLoc::get (builder.getContext (),
2358
2360
{getLoc (s.getSourceRange ().getBegin ()),
2359
2361
getLoc (s.getSourceRange ().getEnd ())});
2360
2362
};
2361
-
2362
2363
auto thenLoc = getStmtLoc (*thenS);
2363
2364
std::optional<mlir::Location> elseLoc;
2364
- SmallVector<mlir::Location, 2 > ifLocs{thenLoc};
2365
-
2366
- if (elseS) {
2365
+ if (elseS)
2367
2366
elseLoc = getStmtLoc (*elseS);
2368
- ifLocs.push_back (*elseLoc);
2369
- }
2370
2367
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);
2378
2368
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=*/
2383
2371
[&](mlir::OpBuilder &, mlir::Location) {
2384
2372
LexicalScope lexScope{*this , thenLoc, builder.getInsertionBlock ()};
2385
2373
resThen = buildStmt (thenS, /* useCurrentScope=*/ true );
2386
2374
},
2375
+ thenLoc,
2387
2376
/* elseBuilder=*/
2388
2377
[&](mlir::OpBuilder &, mlir::Location) {
2389
2378
assert (elseLoc && " Invalid location for elseS." );
2390
2379
LexicalScope lexScope{*this , *elseLoc, builder.getInsertionBlock ()};
2391
2380
resElse = buildStmt (elseS, /* useCurrentScope=*/ true );
2392
- });
2381
+ },
2382
+ elseLoc);
2393
2383
2394
2384
return mlir::LogicalResult::success (resThen.succeeded () &&
2395
2385
resElse.succeeded ());
2396
2386
}
2397
2387
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
+
2398
2409
// / TODO(cir): PGO data
2399
2410
// / 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) {
2404
2413
// TODO(CIR): scoped ApplyDebugLocation DL(*this, Cond);
2405
2414
// TODO(CIR): __builtin_unpredictable and profile counts?
2406
2415
cond = cond->IgnoreParens ();
@@ -2414,17 +2423,13 @@ mlir::Value CIRGenFunction::buildOpOnBoolExpr(const Expr *cond,
2414
2423
// This should be done in CIR prior to LLVM lowering, if we do now
2415
2424
// we can make CIR based diagnostics misleading.
2416
2425
// cir.ternary(!x, t, f) -> cir.ternary(x, f, t)
2417
- // if (CondUOp->getOpcode() == UO_LNot) {
2418
- // buildOpOnBoolExpr(CondUOp->getSubExpr(), loc, elseS, thenS);
2419
- // }
2420
2426
assert (!UnimplementedFeature::shouldReverseUnaryCondOnBoolExpr ());
2421
2427
}
2422
2428
2423
2429
if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(cond)) {
2424
2430
auto *trueExpr = CondOp->getTrueExpr ();
2425
2431
auto *falseExpr = CondOp->getFalseExpr ();
2426
- mlir::Value condV =
2427
- buildOpOnBoolExpr (CondOp->getCond (), loc, trueExpr, falseExpr);
2432
+ mlir::Value condV = buildOpOnBoolExpr (loc, CondOp->getCond ());
2428
2433
2429
2434
auto ternaryOpRes =
2430
2435
builder
0 commit comments