================ @@ -316,6 +316,106 @@ void CIRGenFunction::emitIgnoredExpr(const Expr *e) { emitLValue(e); } +/// Emit an `if` on a boolean condition, filling `then` and `else` into +/// appropriated regions. +mlir::LogicalResult CIRGenFunction::emitIfOnBoolExpr(const Expr *cond, + const Stmt *thenS, + const Stmt *elseS) { + // Attempt to be more accurate as possible with IfOp location, generate + // one fused location that has either 2 or 4 total locations, depending + // on else's availability. + auto getStmtLoc = [this](const Stmt &s) { + return mlir::FusedLoc::get(&getMLIRContext(), + {getLoc(s.getSourceRange().getBegin()), + getLoc(s.getSourceRange().getEnd())}); + }; + mlir::Location thenLoc = getStmtLoc(*thenS); + std::optional<mlir::Location> elseLoc; + if (elseS) + elseLoc = getStmtLoc(*elseS); + + mlir::LogicalResult resThen = mlir::success(), resElse = mlir::success(); + emitIfOnBoolExpr( + cond, /*thenBuilder=*/ + [&](mlir::OpBuilder &, mlir::Location) { + LexicalScope lexScope{*this, thenLoc, builder.getInsertionBlock()}; + resThen = emitStmt(thenS, /*useCurrentScope=*/true); + }, + thenLoc, + /*elseBuilder=*/ + [&](mlir::OpBuilder &, mlir::Location) { + assert(elseLoc && "Invalid location for elseS."); + LexicalScope lexScope{*this, *elseLoc, builder.getInsertionBlock()}; + resElse = emitStmt(elseS, /*useCurrentScope=*/true); + }, + elseLoc); + + return mlir::LogicalResult::success(resThen.succeeded() && + resElse.succeeded()); +} + +/// Emit an `if` on a boolean condition, filling `then` and `else` into +/// appropriated regions. +cir::IfOp CIRGenFunction::emitIfOnBoolExpr( + const clang::Expr *cond, BuilderCallbackRef thenBuilder, + mlir::Location thenLoc, BuilderCallbackRef elseBuilder, + std::optional<mlir::Location> elseLoc) { + + SmallVector<mlir::Location, 2> ifLocs{thenLoc}; + if (elseLoc) + ifLocs.push_back(*elseLoc); + mlir::Location loc = mlir::FusedLoc::get(&getMLIRContext(), ifLocs); + + // Emit the code with the fully general case. + mlir::Value condV = emitOpOnBoolExpr(loc, cond); + return builder.create<cir::IfOp>(loc, condV, elseLoc.has_value(), + /*thenBuilder=*/thenBuilder, + /*elseBuilder=*/elseBuilder); +} + +/// TODO(cir): PGO data +/// TODO(cir): see EmitBranchOnBoolExpr for extra ideas). +mlir::Value CIRGenFunction::emitOpOnBoolExpr(mlir::Location loc, + const Expr *cond) { + // TODO(CIR): scoped ApplyDebugLocation DL(*this, Cond); ---------------- andykaylor wrote:
```suggestion assert(!cir::MissingFeatures::generateDebugInfo()); ``` https://github.com/llvm/llvm-project/pull/134333 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits