[llvm] [SimplifyCFG]divergent undef predecessor removal (PR #204287)
Fujun Han via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 22 17:55:01 PDT 2026
Peter9606 wrote:
> I don't understand why passing poison to a noundef argument is special. Under our model of "convergent", it is legal to eliminate dead edges. And the transform in question is just eliminating a dead edge. How we prove the edge is dead should be irrelevant.
>
> (Are you sure your original code doesn't have undefined behavior?)
Thanks for the question — it helped me think more carefully about how this
looks from the IR rules side.
Here is the original CUDA pattern that exposed the miscompile:
```cuda
__global__ void k_buggy(uint32_t* counter, uint32_t* out) {
int lane_id = threadIdx.x % 32;
int row_ix = threadIdx.x / 32;
if (row_ix >= 2) return;
uint32_t val;
if (lane_id == 0) val = atomicAdd(counter, 1);
val = __shfl_sync(0xFFFFFFFF, val, 0, 32);
out[threadIdx.x] = val;
}
```
I agree this source is not ideal: on non-lane-0 threads val is not assigned before __shfl_sync, and there is no warp barrier between the divergent if and the shuffle. Under strict C++/CUDA rules that can reasonably be called undefined behavior.
I still think that does not justify the optimization we saw. After InstCombine the IR has a merge PHI [atomic result, if.then], [poison, if.false] feeding a noundef argument of a convergent call. The poison on the if.false incoming is the compiler's way of saying "not defined on this arm", not that the predecessor is unreachable for the whole warp. Other lanes still take that arm concurrently with lane 0; folding the branch and hoisting the atomic makes every lane execute it and changes observable behavior (the counter).
The patch simply skips scalar dead-edge elimination in that convergent case; divergent-poison-phi.ll checks that the convergent callee keeps the branch/PHI/atomic, and that the non-convergent case still folds as before.
Happy to adjust the approach if you think there is a cleaner way to draw the line between "legitimately dead edge" and "poison placeholder at a convergent join". I may be missing a simpler framing here, so I'd appreciate any further thoughts.
https://github.com/llvm/llvm-project/pull/204287
More information about the llvm-commits
mailing list