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

via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 10 23:52:18 PDT 2026


================
@@ -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);
----------------
Ritanya-B-Bharadwaj wrote:

Just want to make sure I understand the execution model here. Since the linear finalization runs in the outer kernel (the exit blocks aren't outlined), wouldn't every device thread take the write-back branch once `lastiter` is forced to 1, rather than just the last-iteration thread like on the CPU path? If multiple threads participate, could that race on the linear variable? Or is a single thread guaranteed to run the loop here?

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


More information about the llvm-commits mailing list