Skip to content

Handle loads from merged relocation slot addresses - #960

Open
maleadt wants to merge 3 commits into
mainfrom
tb/relocation-phi
Open

maleadt wants to merge 3 commits into
mainfrom
tb/relocation-phi

Conversation

@maleadt

@maleadt maleadt commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

LLVM can merge singleton-address loads into a load from a phi or select of relocation slot addresses. Metal's table lowering only handled direct loads, so kernels comparing a boxed value against several singletons failed with “Relocation slot … still has uses after redirection”.

Replace each slot address with its indexed address in the relocation table, using replace_global_with_local!, and let LLVM's address-space inference propagate the table's address space through the existing PHIs, selects and casts. This also handles loop-carried addresses without rebuilding them as an integer-offset graph. Expand constant users before choosing entry insertion points, retain the word-load restrictions, and adjust alignment and load names for packed, reproducible tables.

Redirect cglobal addresses such as jl_nothing to relocation slots in the same way, preserving the original loads. Keeping their pointer load types matters on Metal: mixing preserved pointer loads with cglobal loads rebuilt as integers followed by inttoptr gave incorrect singleton comparisons on the M1. Unsupported cglobal loads remain diagnosed.

Validation:

  • Native and Metal code-generation suites pass on Julia 1.10.12, 1.12.7 and 1.13.0 (one expected broken native test per version; table tests require Julia 1.12+).
  • Native execution covers merged addresses, loop-carried PHIs/selects, instruction and constant-expression casts, and jl_nothing; tests also cover alignment, preserved cglobal load types, reproducibility, and rejected non-word loads, writes and unrelated addresses.
  • On an Apple M1 with macOS 27, Julia 1.12.7 and Metal.jl 1.12.0-DEV, the original singleton kernel, a nothing variant and a loop variant return the expected results for inputs 1–7 repeated over 896 elements, across three launches each. Validated from a fresh process using the final source.

Fixes #959

LLVM can merge the loads from several relocation slots into a single
load from a phi or select of the slots' addresses, e.g. when a boxed
value is compared against the singletons it may be. The :table lowering
only redirected direct loads of a slot, so such a slot kept a use and
compilation failed with "still has uses after redirection". Rebuild
those phis and selects over the slots' table offsets instead, and load
the word at the resulting offset. Merging a slot with any other address,
or using a merged address for anything but loading its word, still
errors.

Libjulia globals can be merged the same way, e.g. jl_nothing with the
slots of other singletons. Only their direct loads were relocated,
leaving the phi referencing the global itself. As such a phi is only
ever loaded from, give it the address of the global's slot, which holds
the same word, and report merged loads that cannot be redirected as
unresolved.

Fixes #959.
@codecov

codecov Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 81.01%. Comparing base (2f56a83) to head (9d808bc).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
src/relocation.jl 99.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #960      +/-   ##
==========================================
- Coverage   86.59%   81.01%   -5.59%     
==========================================
  Files          29       29              
  Lines        5760     6277     +517     
==========================================
+ Hits         4988     5085      +97     
- Misses        772     1192     +420     

☔ 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.

Redirect both direct and merged cglobal addresses to their word slots. This keeps pointer loads intact instead of rebuilding them as integer loads followed by inttoptr, which can miscompile mixed singleton comparisons on Metal.
Use replace_global_with_local! and LLVM address-space inference instead of rebuilding pointer PHIs and selects as an offset graph. Keep the word-load checks, cap alignment at the table word size, and discard session-specific load names. Expand constant users before choosing insertion points so later substitutions cannot introduce uses before their definitions.
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.

Metal relocation fails when boxed values are compared with singletons

1 participant