@@ -2113,8 +2113,7 @@ CIRGenFunction::buildConditionalBlocks(const AbstractConditionalOperator *E,
21132113 auto *trueExpr = E->getTrueExpr ();
21142114 auto *falseExpr = E->getFalseExpr ();
21152115
2116- mlir::Value condV =
2117- CGF.buildOpOnBoolExpr (E->getCond (), loc, trueExpr, falseExpr);
2116+ mlir::Value condV = CGF.buildOpOnBoolExpr (loc, E->getCond ());
21182117 SmallVector<mlir::OpBuilder::InsertPoint, 2 > insertPoints{};
21192118 mlir::Type yieldTy{};
21202119
@@ -2354,54 +2353,64 @@ bool CIRGenFunction::LValueIsSuitableForInlineAtomic(LValue LV) {
23542353mlir::LogicalResult CIRGenFunction::buildIfOnBoolExpr (const Expr *cond,
23552354 const Stmt *thenS,
23562355 const Stmt *elseS) {
2356+ // Attempt to be more accurate as possible with IfOp location, generate
2357+ // one fused location that has either 2 or 4 total locations, depending
2358+ // on else's availability.
23572359 auto getStmtLoc = [this ](const Stmt &s) {
23582360 return mlir::FusedLoc::get (builder.getContext (),
23592361 {getLoc (s.getSourceRange ().getBegin ()),
23602362 getLoc (s.getSourceRange ().getEnd ())});
23612363 };
2362-
23632364 auto thenLoc = getStmtLoc (*thenS);
23642365 std::optional<mlir::Location> elseLoc;
2365- SmallVector<mlir::Location, 2 > ifLocs{thenLoc};
2366-
2367- if (elseS) {
2366+ if (elseS)
23682367 elseLoc = getStmtLoc (*elseS);
2369- ifLocs.push_back (*elseLoc);
2370- }
23712368
2372- // Attempt to be more accurate as possible with IfOp location, generate
2373- // one fused location that has either 2 or 4 total locations, depending
2374- // on else's availability.
2375- auto loc = mlir::FusedLoc::get (builder.getContext (), ifLocs);
2376-
2377- // Emit the code with the fully general case.
2378- mlir::Value condV = buildOpOnBoolExpr (cond, loc, thenS, elseS);
23792369 mlir::LogicalResult resThen = mlir::success (), resElse = mlir::success ();
2380-
2381- builder.create <mlir::cir::IfOp>(
2382- loc, condV, elseS,
2383- /* thenBuilder=*/
2370+ buildIfOnBoolExpr (
2371+ cond, /* thenBuilder=*/
23842372 [&](mlir::OpBuilder &, mlir::Location) {
23852373 LexicalScope lexScope{*this , thenLoc, builder.getInsertionBlock ()};
23862374 resThen = buildStmt (thenS, /* useCurrentScope=*/ true );
23872375 },
2376+ thenLoc,
23882377 /* elseBuilder=*/
23892378 [&](mlir::OpBuilder &, mlir::Location) {
23902379 assert (elseLoc && " Invalid location for elseS." );
23912380 LexicalScope lexScope{*this , *elseLoc, builder.getInsertionBlock ()};
23922381 resElse = buildStmt (elseS, /* useCurrentScope=*/ true );
2393- });
2382+ },
2383+ elseLoc);
23942384
23952385 return mlir::LogicalResult::success (resThen.succeeded () &&
23962386 resElse.succeeded ());
23972387}
23982388
2389+ // / Emit an `if` on a boolean condition, filling `then` and `else` into
2390+ // / appropriated regions.
2391+ mlir::cir::IfOp CIRGenFunction::buildIfOnBoolExpr (
2392+ const clang::Expr *cond,
2393+ llvm::function_ref<void (mlir::OpBuilder &, mlir::Location)> thenBuilder,
2394+ mlir::Location thenLoc,
2395+ llvm::function_ref<void(mlir::OpBuilder &, mlir::Location)> elseBuilder,
2396+ std::optional<mlir::Location> elseLoc) {
2397+
2398+ SmallVector<mlir::Location, 2 > ifLocs{thenLoc};
2399+ if (elseLoc)
2400+ ifLocs.push_back (*elseLoc);
2401+ auto loc = mlir::FusedLoc::get (builder.getContext (), ifLocs);
2402+
2403+ // Emit the code with the fully general case.
2404+ mlir::Value condV = buildOpOnBoolExpr (loc, cond);
2405+ return builder.create <mlir::cir::IfOp>(loc, condV, elseLoc.has_value (),
2406+ /* thenBuilder=*/ thenBuilder,
2407+ /* elseBuilder=*/ elseBuilder);
2408+ }
2409+
23992410// / TODO(cir): PGO data
24002411// / TODO(cir): see EmitBranchOnBoolExpr for extra ideas).
2401- mlir::Value CIRGenFunction::buildOpOnBoolExpr (const Expr *cond,
2402- mlir::Location loc,
2403- const Stmt *thenS,
2404- const Stmt *elseS) {
2412+ mlir::Value CIRGenFunction::buildOpOnBoolExpr (mlir::Location loc,
2413+ const Expr *cond) {
24052414 // TODO(CIR): scoped ApplyDebugLocation DL(*this, Cond);
24062415 // TODO(CIR): __builtin_unpredictable and profile counts?
24072416 cond = cond->IgnoreParens ();
@@ -2415,17 +2424,13 @@ mlir::Value CIRGenFunction::buildOpOnBoolExpr(const Expr *cond,
24152424 // This should be done in CIR prior to LLVM lowering, if we do now
24162425 // we can make CIR based diagnostics misleading.
24172426 // cir.ternary(!x, t, f) -> cir.ternary(x, f, t)
2418- // if (CondUOp->getOpcode() == UO_LNot) {
2419- // buildOpOnBoolExpr(CondUOp->getSubExpr(), loc, elseS, thenS);
2420- // }
24212427 assert (!UnimplementedFeature::shouldReverseUnaryCondOnBoolExpr ());
24222428 }
24232429
24242430 if (const ConditionalOperator *CondOp = dyn_cast<ConditionalOperator>(cond)) {
24252431 auto *trueExpr = CondOp->getTrueExpr ();
24262432 auto *falseExpr = CondOp->getFalseExpr ();
2427- mlir::Value condV =
2428- buildOpOnBoolExpr (CondOp->getCond (), loc, trueExpr, falseExpr);
2433+ mlir::Value condV = buildOpOnBoolExpr (loc, CondOp->getCond ());
24292434
24302435 auto ternaryOpRes =
24312436 builder
0 commit comments