[llvm] [OpenMP][OMPIRBuilder] Fix lastiter assertion in target workshare loop (PR #214996)

via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 8 10:34:14 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-openmp

Author: RohithPariki

<details>
<summary>Changes</summary>

Fixes #<!-- -->213905.

### Description
When translating an `omp.wsloop` with a `linear` clause for a target device, `applyWorkshareLoopTarget` failed to set the `lastiter` value in `CanonicalLoopInfo`. This resulted in an assertion failure when later attempting to finalize the linear variables.

This patch fixes the issue by allocating a `p.lastiter` variable and setting it to `1` (true). Since target workshare loops execute synchronously on the device and complete entirely before returning, the thread executing after the loop is effectively the one that executed the last iteration, and therefore should be the one to apply the linear variable updates.

**Note to reviewers:**
* AI was used to help identify the root cause and generate this patch. 


---
Full diff: https://github.com/llvm/llvm-project/pull/214996.diff


1 Files Affected:

- (modified) llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp (+9) 


``````````diff
diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index 5a363d0ac3dbd..173cbddf2a4d6 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -6577,6 +6577,15 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::applyWorkshareLoopTarget(
   }
   Value *Ident = getOrCreateIdent(SrcLocStr, SrcLocStrSize, Flag);
 
+  // Allocate p.lastiter and set it to 1 (true). The target workshare loop
+  // executes synchronously on the device and completely finishes before
+  // returning, so the thread executing after it is effectively the one that
+  // executed the last iteration, and needs to do the linear variable updates.
+  Builder.restoreIP(AllocaIP);
+  Value *PLastIter = Builder.CreateAlloca(Builder.getInt32Ty(), nullptr, "p.lastiter");
+  Builder.CreateStore(Builder.getInt32(1), PLastIter);
+  CLI->setLastIter(PLastIter);
+
   auto OI = std::make_unique<OutlineInfo>();
   OI->OuterAllocBB = CLI->getPreheader();
   Function *OuterFn = CLI->getPreheader()->getParent();

``````````

</details>


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


More information about the llvm-commits mailing list