Repository navigation
Load relocation table entries as words - #975
Merged
Merged
Conversation
The `:table` lowering replaces each relocation slot with the address of its entry in the relocation table, but kept the type the slot was loaded as. Slots holding host identities (a Symbol's address, a cglobal's type tag) are mostly loaded as pointers, so their words ended up being loaded from device memory as pointers into the default address space. On Metal that is thread memory, and with shader validation enabled such a loaded value does not keep its bits: comparing a Symbol argument against a relocated Symbol failed (Metal.jl's relocations test under `MTL_SHADER_VALIDATION=1`). The offset-based lowering this replaced loaded integer words, and did not have that problem. The table holds words, which is what the loader delivers, so load every slot entry as a word and convert it back with `inttoptr` where it was loaded as a pointer. Comparisons then fold into integer ones on LLVM 20+. The access properties and metadata carry over, except for metadata describing the loaded pointer. Collection and the `:patch`/`:bake` lowerings are unchanged, and the address PHIs and selects are still moved into the table's address space. Also correct the comment that called AS 0 generic on Metal: it is thread memory in AIR, and only modeled as the flat space for address-space inference.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #975 +/- ##
=======================================
Coverage 87.72% 87.73%
=======================================
Files 30 30
Lines 6078 6097 +19
=======================================
+ Hits 5332 5349 +17
- Misses 746 748 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Since #960, Metal kernels that compare a Julia object against a relocated one give the wrong answer when Metal's shader validation is enabled. Metal.jl's relocations test shows it (JuliaGPU/Metal.jl#997, the
:mag: API validationjob):With
MTL_SHADER_VALIDATION=1this returns-1for both:fooand:bar; without validation it works. The bisect points at 9d808bc ("Lower relocation slots by substituting table addresses"): GPUCompiler 2.9 passes, 2.10 and 2.11 fail. This isn't related to LLVM.jl 10.Why
With the
:tablelowering, which Metal uses, relocations are words that Metal.jl's loader writes into a device buffer. #960 replaced each relocation slot by the address of its table entry, but the load from that address kept the type Julia gave it. For a Symbol, that's a pointer. So the kernel ended up reading a host address out of device memory as a pointer:On Metal, the default address space is thread memory, and shader validation instruments loads of thread pointers. A host address read that way doesn't keep its bits. Comparing the values as integers afterwards doesn't help: it's the pointer-typed load itself. Before #960, the lowering loaded an
i64, which is what the table actually holds:What this changes
The
:tablelowering now loads every table entry as a word, and converts it back withinttoptrwhere the code used it as a pointer. That puts the table's contract (it holds the wordsresolved_relocation_tablereturns) where the representation changes, instead of special-casing Metal. Identity comparisons then become integer comparisons. The rest of #960 stays::patchand:bakelowerings, and cglobal collectionLoads keep their alignment, volatility, atomic ordering and metadata, except for metadata that only makes sense for pointers (
!nonnull,!dereferenceable, ...). The comment insrc/metal.jlthat called address space 0 generic now says what it is in AIR: thread memory, modeled as the flat space only for address-space inference.We also considered fixing this in the Metal back-end, by rewriting address-space-0 loads late, or in Metal.jl. Neither knows which pointers are really host identities, so they would have to guess.
Testing
load ptr, ptr addrspace(1), i.e. the bug. It now requires word loads, and a new Symbol test checks for word loads and integer comparisons (the latter on LLVM 20+, which foldsicmpof twointtoptrs).:bake,:patchand:table.This also bumps the version to 2.11.1, which Metal.jl#997 needs to go green.