[clang] [openmp] [OpenMP] Assign lastprivate loop counter init on empty worksharing loops (PR #243086)

Amit Tiwari via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 25 03:03:37 PST 2026


================
@@ -4075,14 +4063,25 @@ bool CodeGenFunction::EmitOMPWorksharingLoop(
     // whole loop.
     bool CondConstant;
     llvm::BasicBlock *ContBlock = nullptr;
+    const Expr *EmptyLoopInit = getEmptyLoopLastprivateInit(S);
     if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) {
-      if (!CondConstant)
+      if (!CondConstant) {
+        if (EmptyLoopInit)
+          EmitIgnoredExpr(EmptyLoopInit);
         return false;
+      }
     } else {
       llvm::BasicBlock *ThenBlock = createBasicBlock("omp.precond.then");
       ContBlock = createBasicBlock("omp.precond.end");
-      emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock,
+      llvm::BasicBlock *EmptyBlock =
----------------
loopacino wrote:

`ContBlock` is reached even when the loop has non-zero iterations so that would make the betterX2 proposal invalid. Also, even if we perform a store conditionally, it will nullify our benefit of eliminating one basic block.  betterX1 approach looks fair but includes 2 big calls to `emitPreCond`. IIUC, your first suggestion (renaming it with `FalseBlock`) + keeping only one call (shown below) should do. 

```
BasicBlock *FalseBlock =
    EmptyLoopInit ? createBasicBlock("omp.precond.empty") : ContBlock;

emitPreCond(..., ThenBlock, FalseBlock, ...);

if (FalseBlock != ContBlock) {
  EmitBlock(FalseBlock);
  EmitIgnoredExpr(EmptyLoopInit); // i = 0
  EmitBranch(ContBlock);
}
```

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


More information about the cfe-commits mailing list