================ @@ -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 ---------------- andykaylor wrote:
Instead of this comment, add an assert in the function body. `assert(!cir::MissingFeatures::pgoUse());` We use this idiom to mark places where code will need to be added later when some feature is implemented. The "TODO" comment here probably predates the introduction of the MissingFeatures idiom. We're trying to be more consistent as we upstream things. 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