[clang] [CIR] Upstream initial for-loop support (PR #132266)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 20 11:55:05 PDT 2025
================
@@ -280,3 +313,77 @@ mlir::LogicalResult CIRGenFunction::emitReturnStmt(const ReturnStmt &s) {
return mlir::success();
}
+
+mlir::LogicalResult CIRGenFunction::emitForStmt(const ForStmt &s) {
+ cir::ForOp forOp;
+
+ // TODO: pass in an array of attributes.
+ auto forStmtBuilder = [&]() -> mlir::LogicalResult {
+ mlir::LogicalResult loopRes = mlir::success();
+ // Evaluate the first part before the loop.
+ if (s.getInit())
+ if (emitStmt(s.getInit(), /*useCurrentScope=*/true).failed())
+ return mlir::failure();
+ assert(!cir::MissingFeatures::loopInfoStack());
+ // In the classic codegen, if there are any cleanups between here and the
+ // loop-exit scope, a block is created to stage the loop exit. We probably
+ // already do the right thing because of ScopeOp, but we need more testing
+ // to be sure we handle all cases.
+ assert(!cir::MissingFeatures::requiresCleanups());
+
+ forOp = builder.createFor(
+ getLoc(s.getSourceRange()),
+ /*condBuilder=*/
+ [&](mlir::OpBuilder &b, mlir::Location loc) {
+ assert(!cir::MissingFeatures::createProfileWeightsForLoop());
+ assert(!cir::MissingFeatures::emitCondLikelihoodViaExpectIntrinsic());
+ mlir::Value condVal;
+ if (s.getCond()) {
+ // If the for statement has a condition scope,
+ // emit the local variable declaration.
+ if (s.getConditionVariable())
+ emitDecl(*s.getConditionVariable());
+ // C99 6.8.5p2/p4: The first substatement is executed if the
+ // expression compares unequal to 0. The condition must be a
+ // scalar type.
+ condVal = evaluateExprAsBool(s.getCond());
----------------
andykaylor wrote:
Even if we have a boolean expression, the expression must be emitted, and that happens here.
https://github.com/llvm/llvm-project/pull/132266
More information about the cfe-commits
mailing list