[OpenMP][DeviceRTL] Fix wrong iteration stride in DistributeFor SPMD no-loop fast path#2602
Open
sbryngelson wants to merge 1 commit into
Open
Conversation
This was referenced May 19, 2026
…st path In the SPMD no-loop fast path (one_iteration_per_thread=1, triggered by -fopenmp-target-fast with both oversubscription flags), the NumThreads argument to DistributeFor derives from omp_get_num_threads() which is evaluated before the parallel region is active. At that point icv::Level=0 and the parallel team state has not been initialized, so omp_get_num_threads() returns 1 instead of the actual hardware block size. NormalizedLoopNestNoChunk uses NumThreads as the per-block stride: IV = BId * NumThreads + TId // = BId * 1 + TId (wrong) KernelIteration = NumBlocks * NumThreads // = NumBlocks * 1 (wrong) With blocksize=32 and K=ceil(N/32) blocks this leaves iterations K+31 .. N-1 permanently unexecuted (output stays at zero). Fix: when OneIterationPerThread=1, override NumThreads with mapping::getMaxTeamThreads() which reads directly from hardware registers and is always valid at kernel entry. This also corrects the BlockChunk default and the ASSERT that follow. Reproducer (fails without fix, passes with fix on gfx90a / MI250X): integer, parameter :: N = 64 integer :: out(N, 2), i, nerr !$omp target teams distribute parallel do map(from:out) do i = 1, N; out(i,:) = [i, i*2]; end do !$omp end target teams distribute parallel do ! Fails: 31 of 64 cells are zero (never written) Verified on amdflang 23.1.0 / 23.2.0 / 23.2.1, gfx90a (MI250X). Fixes: ROCm#2601
b8906a1 to
d0d7275
Compare
Author
|
CI failure (Attempt #2) was unrelated to this PR. Both Linux and Windows failed in the The fix was to rebase this branch onto the current |
Author
|
CI failed on what appears to be a flaky test (many runs fail it spuriously). I can look into it further if appropriate. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2601.
Problem
When compiled with
-fopenmp-target-fast(which implies both-fopenmp-assume-threads-oversubscriptionand-fopenmp-assume-teams-oversubscription),target teams distribute parallel doloops that use array expressions (constructors[a,b,c]orwhole-array slice ops
A(i,:)) silently produce wrong output: adeterministic suffix of iterations never executes, leaving those output
elements at zero.
Trigger: all three must be present:
-fopenmp-assume-threads-oversubscriptionAND-fopenmp-assume-teams-oversubscription-fopenmp-target-fastmalloc)-O0through-O3without-fopenmp-target-fastall pass. Scalarassignments are immune.
Quantitative signature (verified on amdflang 23.1.0–23.2.1, gfx90a):
e.g. N=64 → 31 wrong, N=128 → 93 wrong. Always the same (last) iterations,
always zero, perfectly deterministic.
Root cause
The five-step chain is detailed in issue #2601. The short version:
Both oversubscription flags →
canPromoteToNoLoop→no_loopkernel mode →one_iteration_per_thread=1emitted as a literal constant in the call to__kmpc_distribute_for_static_loop_4u.Array-expression kernels set
MayUseNestedParallelism=1inKernelEnvironmentTy(device stdlib is linked in). This prevents LTO from replacingomp_get_num_threads()with a hardware register read.Instead, LTO hoists
omp_get_num_threads()to kernel entry — before__kmpc_parallel_spmdhas seticv::Level=1. AtLevel=0,omp_get_num_threads()returns 1.DistributeForreceivesNumThreads=1.NormalizedLoopNestNoChunkcomputes:With K=⌈N/32⌉ blocks of 32 threads: coverage is flat_ids
0..(K+30)only.Flat_ids
K+31..N-1are never assigned → those iterations never execute.Scalar kernels are immune because
MayUseNestedParallelism=0allows LTO toreplace
omp_get_num_threads()withmapping::getMaxTeamThreads()(ahardware register read that is correct at any point in kernel execution).
Fix
In
DistributeFor, whenOneIterationPerThread=1, overrideNumThreadswith
mapping::getMaxTeamThreads()before it is used forBlockChunkdefaulting and stride computation. This hardware register is always valid
at kernel entry and reflects the actual number of threads per block.
The fix is placed before
BlockChunk = NumThreadsso the chunk size isalso set correctly.
Minimal Fortran reproducer
Compile with:
amdflang -fopenmp --offload-arch=gfx90a -fopenmp-target-fast \ -fopenmp-assume-threads-oversubscription \ -fopenmp-assume-teams-oversubscription \ -o t t.f90 && ./tA full test suite with additional cases (whole-array ops, varying N, scalar
immunity verification) is at https://github.com/sbryngelson/amd-flang-omp-bugs.
Testing
FAIL: 31 of 64without fix,PASSwith fix-O0through-O3paths unaffected (OneIterationPerThread=0)ASSERT(NumBlocks * NumThreads >= NumIters)now checks against the correct blocksize