@@ -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) {
23532352mlir::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
0 commit comments