Repository navigation
codegen: cast globals to the generic address space where used as pointers - #63715
Merged
Merged
Conversation
…ters Globals can live in a nonzero address space on targets such as AMDGPU and SPIR-V. Cast their addresses at generic pointer uses in runtime symbol lookup, union typeof, and `@cfunction` codegen so the emitted IR verifies. Keep direct loads and stores in the globals' address space. Assisted-by: Codex (GPT-6)
vchuravy
approved these changes
Oct 10, 2026
15 of 29 tasks
KristofferC
pushed a commit
that referenced
this pull request
Oct 20, 2026
…ters (#63715) Globals can live in a nonzero address space on targets such as AMDGPU and SPIR-V. Cast their addresses at generic pointer uses in runtime symbol lookup, union typeof, and `@cfunction` codegen so the emitted IR verifies. Keep direct loads and stores in the globals' address space. Assisted-by: Codex (GPT-6) (cherry picked from commit 73bc166)
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.
LLVM creates globals in the datalayout's globals address space. On AMDGPU and SPIR-V this is address space 1, but several codegen paths use their addresses where a generic
ptris required. Runtime symbol lookup passes the library-handle global tojl_load_and_lookupwithout a cast; uniontypeofbitcasts type slots across address spaces or selects a small-tag global against a generic null; and@cfunctionuses globals as runtime call arguments and elements of a generic-pointer initializer. The resulting IR fails verification withInvalid bitcastorCall parameter type does not match function signature!.This causes the verifier abort described in JuliaGPU/AMDGPU.jl#1149, for which JuliaGPU/GPUCompiler.jl#1003 adds a runtime-lookup workaround. The union
typeofpaths also affect ordinary device code, such as selecting betweenInt32andFloat32values and comparing the selected value's type.Add
generic_global_ptr, usingConstantExpr::getPointerBitCastOrAddrSpaceCast, at uses that require a generic pointer. It also replaces the existing casts for string constants and small-type slots. Casting at each use preserves direct loads and stores in the global's address space, which permits better AMDGPU code than casting every global at creation. Globals already in address space 0 are unchanged, andemission_context.cfuncscontinues to retain the rawGlobalVariable.This follows Clang's treatment of globals whose storage address space differs from the pointer address space expected by the language:
CodeGenModule::GetOrCreateLLVMGlobalreturns an address-space cast in that case. Julia applies the cast at the uses that require a generic pointer, rather than returning a cast when the global is created, so direct memory accesses retain the storage address space.Validated with GPUCompiler main (
6783e66) and LLVM.jl 10.1.0, using separate depots for the baseline nightly (1.14.0-DEV.3501,f5fdd1cdc80) and the assertions-enabled fixed build (1.14.0-DEV.3587, based on3d57df8728). These are different Julia revisions.check.jlverifies raw output fromGPUCompiler.compile_method_instanceon GCN and SPIR-V (backend=:llvmfor SPIR-V). The fixed results are identical with and withoutCI=true:tag: uniontypeofcomparisontag2: three-way uniontypeofsmalltag: unionisaalloc_string: runtimeccalllookupcfun: closure@cfunctioncfun_static: static@cfunctionCI=truetypeofkernelalloc_stringInvalidIRErrorInvalidIRError, without a verifier abortThe remaining kernel failure is a GPUCompiler follow-up.
Assisted by: Sol 6.1