[clang] [Clang][OpenMP] Fix crash on a range-based for loop counted by `ordered(n)` but not collapsed (PR #240126)

Alexey Bataev via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 8 06:51:18 PST 2026


alexey-bataev wrote:

```
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -175,36 +175,18 @@
     CodeGenFunction::OMPMapVars PreCondVars;
     SmallVector<const Decl *, 4> DoacrossOnlyDecls;
     if (auto *LD = dyn_cast<OMPLoopDirective>(&S)) {
-      // The pre-inits also use the bounds of the loops that an 'ordered(n)'
-      // clause counts beyond the collapsed ones.
-      unsigned NumLoops = LD->getLoopsNumber();
-      for (const auto *C : LD->getClausesOfKind<OMPOrderedClause>())
-        if (C->getNumForLoops() && C->getLoopNumIterations().size() > NumLoops)
-          NumLoops = C->getLoopNumIterations().size();
       // Emit init, __range, __begin and __end variables for C++ range loops.
-      // A loop that is not collapsed is emitted as a regular loop in the body
-      // and declares its own, so its copies only live until the pre-inits.
       (void)OMPLoopBasedDirective::doForAllLoops(
           LD->getInnermostCapturedStmt()->getCapturedStmt(),
-          /*TryImperfectlyNestedLoops=*/true, NumLoops,
-          [&CGF, LD, &DoacrossOnlyDecls](unsigned Cnt, const Stmt *CurStmt) {
-            const auto *CXXFor = dyn_cast<CXXForRangeStmt>(CurStmt);
-            if (!CXXFor)
-              return false;
-            const Stmt *Init = CXXFor->getInit();
-            if (Init)
-              CGF.EmitStmt(Init);
-            CGF.EmitStmt(CXXFor->getRangeStmt());
-            CGF.EmitStmt(CXXFor->getBeginStmt());
-            CGF.EmitStmt(CXXFor->getEndStmt());
-            if (Cnt < LD->getLoopsNumber())
-              return false;
-            for (const Stmt *Helper :
-                 {Init, static_cast<const Stmt *>(CXXFor->getRangeStmt()),
-                  static_cast<const Stmt *>(CXXFor->getBeginStmt()),
-                  static_cast<const Stmt *>(CXXFor->getEndStmt())})
-              if (const auto *DS = dyn_cast_or_null<DeclStmt>(Helper))
-                llvm::append_range(DoacrossOnlyDecls, DS->decls());
+          /*TryImperfectlyNestedLoops=*/true, LD->getLoopsNumber(),
+          [&CGF](unsigned Cnt, const Stmt *CurStmt) {
+            if (const auto *CXXFor = dyn_cast<CXXForRangeStmt>(CurStmt)) {
+              if (const Stmt *Init = CXXFor->getInit())
+                CGF.EmitStmt(Init);
+              CGF.EmitStmt(CXXFor->getRangeStmt());
+              CGF.EmitStmt(CXXFor->getBeginStmt());
+              CGF.EmitStmt(CXXFor->getEndStmt());
+            }
             return false;
           });
       llvm::DenseSet<const VarDecl *> EmittedAsPrivate;
@@ -233,6 +215,30 @@
         }
       }
       (void)PreCondVars.apply(CGF);
+      // The pre-inits also use the bounds of the loops that an 'ordered(n)'
+      // clause counts beyond the collapsed ones. Such a loop is emitted as a
+      // regular loop in the body and declares its own helpers, so these copies
+      // only live until the pre-inits.
+      if (const auto *C = LD->getSingleClause<OMPOrderedClause>())
+        (void)OMPLoopBasedDirective::doForAllLoops(
+            LD->getInnermostCapturedStmt()->getCapturedStmt(),
+            /*TryImperfectlyNestedLoops=*/true,
+            C->getLoopNumIterations().size(),
+            [&CGF, LD, &DoacrossOnlyDecls](unsigned Cnt, const Stmt *CurStmt) {
+              const auto *CXXFor = dyn_cast<CXXForRangeStmt>(CurStmt);
+              if (!CXXFor || Cnt < LD->getLoopsNumber())
+                return false;
+              for (const Stmt *Helper : std::initializer_list<const Stmt *>{
+                       CXXFor->getInit(), CXXFor->getRangeStmt(),
+                       CXXFor->getBeginStmt(), CXXFor->getEndStmt()}) {
+                if (!Helper)
+                  continue;
+                CGF.EmitStmt(Helper);
+                if (const auto *DS = dyn_cast<DeclStmt>(Helper))
+                  llvm::append_range(DoacrossOnlyDecls, DS->decls());
+              }
+              return false;
+            });
       PreInits = LD->getPreInits();
     } else if (const auto *Tile = dyn_cast<OMPTileDirective>(&S)) {
       PreInits = Tile->getPreInits();

```

https://github.com/llvm/llvm-project/pull/240126


More information about the cfe-commits mailing list