[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:33:15 PDT 2026
https://github.com/RohithPariki created https://github.com/llvm/llvm-project/pull/214996
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.
>From c282049a8f9d2b63a3aecef9262e1ddf6087095b Mon Sep 17 00:00:00 2001
From: Rohith Pariki <rohithpariki at gmail.com>
Date: Sat, 8 Aug 2026 18:36:57 +0530
Subject: [PATCH] [OpenMP][OMPIRBuilder] Fix lastiter assertion in target
workshare loop
When translating an omp.wsloop with a linear clause for a target device, applyWorkshareLoopTarget failed to set the lastiter value in CanonicalLoopInfo, resulting in an assertion failure when finalizing linear variables. This patch allocates and sets lastiter to true, as target workshare loops execute synchronously on the device and complete entirely before returning.
---
llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp | 9 +++++++++
1 file changed, 9 insertions(+)
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();
More information about the llvm-commits
mailing list