Repository navigation
Metal: load thread pointers from other address spaces as words - #1012
Merged
Merged
Conversation
The relocation lowering loads table entries as words and converts them back with `inttoptr`. Move that into a helper so that the Metal back-end can use it for other pointer loads, and bitcast the address under typed pointers, where the load type has to match the pointee type.
Julia object references are AS 0 pointers, which AIR considers thread memory. The AGX back-end keeps only the low 32 bits of such a pointer when it is loaded from device memory, so a `Symbol` stored in an array no longer compared equal to itself (JuliaGPU/Metal.jl#1017). Pointers produced by `inttoptr` keep all their bits, so load these pointers as words, at the end of the AIR lowering where InstCombine cannot fold them back.
Byref kernel arguments arrive in device memory, and were copied to a stack slot for the body, which expects AS 0 pointers. Optimization normally removes that copy, but not when the argument is indexed dynamically, and the AGX back-end miscompiles some kernels where it survives: a closure capturing arrays and a dynamically indexed `SVector` wrote nothing for most threads (JuliaGPU/Metal.jl#1018). Pass arguments that are only loaded from as casts of the device pointer instead, which address-space inference turns into device-memory loads. Arguments with other uses, e.g. passed to a call that also receives stack memory, are still copied.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1012 +/- ##
==========================================
+ Coverage 89.51% 89.54% +0.02%
==========================================
Files 31 31
Lines 6906 6935 +29
==========================================
+ Hits 6182 6210 +28
- Misses 724 725 +1 ☔ 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.
On Metal, a
Symbolread from an array compares wrong (JuliaGPU/Metal.jl#1017):Julia object references end up as
ptrin address space 0, which AIR treats as thread memory. When such a pointer is loaded from another address space (load ptr, ptr addrspace(1)), Apple's back-end keeps only its low 32 bits: a stored0x1050e7450reads back as0x050e7450. Loading the same bytes asi64and converting withinttoptrkeeps all 64 bits, through stores,select,icmp, and round-trips through thread memory. I checked this with hand-written IR kernels on an M1 with macOS 27; the issue was reported on an M2.#975 already loads relocation-table entries this way. This PR moves that code into a helper and adds a pass at the end of the AIR lowering. The pass rewrites every load of an AS 0 pointer from a non-AS 0 address into a word load plus
inttoptr. It has to run last, because InstCombine would fold it back into a pointer load. Alignment, volatility, atomic ordering and the metadata that applies to integers carry over.Mapping object references to the device address space instead doesn't work: boxes demoted to the stack and
sretslots are real thread pointers that get merged with these references, and AIR can't cast between the two spaces. Loads of aggregates or vectors that contain such pointers, and pointer atomics, are not rewritten.Tested on Julia 1.10, 1.11 and 1.12 with the GPUCompiler Metal tests; the new IR test fails without the pass. The Metal.jl regression tests are on the
tb/thread-pointersbranch of Metal.jl.