Skip to content

GCN: fold null checks of stack objects - #1000

Merged
maleadt merged 2 commits into
mainfrom
tb/gcn-alloca-null
Oct 8, 2026
Merged

maleadt merged 2 commits into
mainfrom
tb/gcn-alloca-null

Conversation

@maleadt

@maleadt maleadt commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Julia 1.10 represents phis of immutables kept in memory as phis of pointers, with null for undefined incoming values, and guards their loads with ptr == null ? undef : load(ptr). LLVM folds those checks once the phi reduces to an alloca, but not in address spaces where null is a valid address, like AMDGPU's private one. The check then survives, and on AMDGPU it holds for the stack object at private address 0 (s_cmp_eq_u32 0, 0 in the ISA), so the value reads as undef.

This broke AcceleratedKernels' reductions with 16-byte accumulators on Julia 1.10 (ComplexF64 / Complex{Int64} sums, and reductions with operators without a neutral element), which return garbage in AMDGPU.jl's GPUArrays 12 port (JuliaGPU/AMDGPU.jl#1145). Julia 1.11+ doesn't emit these checks.

This PR folds the comparisons in the GCN target, before optimization and again after. Folding them also lets SROA promote the stack slot, so the affected kernels no longer use scratch memory (24 bytes before, 0 after). The new test checks the IR of a kernel shaped like AK's reduction. On Julia 1.10 it has two such checks without the fold. With the fold, AMDGPU's failing GPUArrays testsets pass on Julia 1.10 (gfx1036).

Bumps the version to 2.13.1.

The second commit fixes the GCN atomic validation test, broken on main since #983: llvm_datalayout now returns a string for the external back-end, and the test tried to @dispose it.

Julia 1.10 represents phis of immutables kept in memory as phis of pointers,
with null for undefined incoming values, and guards their loads with a null
check. LLVM folds these checks once the phi reduces to an alloca, except in
address spaces where null is a valid address, like AMDGPU's private one. The
check then survives, and holds for the stack object at private address 0, so
the value reads as undef. This broke AcceleratedKernels' reductions with
16-byte accumulators on Julia 1.10.

Fold the checks before and after optimization. That also lets SROA promote the
stack slot, so these kernels no longer use scratch memory.
Since #983, the GCN target's llvm_datalayout returns a String with the
external back-end, which the atomic validation test passed to @dispose.
@maleadt
maleadt merged commit 6783e66 into main Oct 8, 2026
35 of 36 checks passed
@maleadt
maleadt deleted the tb/gcn-alloca-null branch October 8, 2026 08:03
@codecov

codecov Bot commented Oct 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.30435% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.46%. Comparing base (dc597a2) to head (2da78b0).
⚠️ Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
src/gcn.jl 91.30% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1000      +/-   ##
==========================================
+ Coverage   81.74%   89.46%   +7.71%     
==========================================
  Files          31       31              
  Lines        6854     6883      +29     
==========================================
+ Hits         5603     6158     +555     
+ Misses       1251      725     -526     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant