Skip to content

Expose native_code's jl_sysimg_gvars. #58423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2025
Merged

Expose native_code's jl_sysimg_gvars. #58423

merged 1 commit into from
May 21, 2025

Conversation

maleadt
Copy link
Member

@maleadt maleadt commented May 15, 2025

#57010 overhauled how global variables are initialized in code, breaking GPUCompiler.jl. After talking to @vtjnash, we are apparently supposed to use jl_get_llvm_gvs now to get the Julia memory location of a global variable (for a binding or constant value), however, the elements in there don't exactly correspond to the global variables in the module (not all module globals are "managed" by Julia, and the order is different).

To make GPUCompiler.jl work again, here I propose renaming jl_get_llvm_gvs to jl_get_llvm_gv_inits, and making jl_get_llvm_gvs instead return the list of the managed global variables, making it possible to perform the global variable initialization that was done by Julia before using something like:

if VERSION >= v"1.13.0-DEV.533"
    num_gvars = Ref{Csize_t}(0)
    @ccall jl_get_llvm_gvs(native_code::Ptr{Cvoid}, num_gvars::Ptr{Csize_t},
                           C_NULL::Ptr{Cvoid})::Nothing
    gvs = Vector{Ptr{LLVM.API.LLVMOpaqueValue}}(undef, num_gvars[])
    @ccall jl_get_llvm_gvs(native_code::Ptr{Cvoid}, num_gvars::Ptr{Csize_t},
                           gvs::Ptr{LLVM.API.LLVMOpaqueValue})::Nothing
    inits = Vector{Ptr{Cvoid}}(undef, num_gvars[])
    @ccall jl_get_llvm_gv_inits(native_code::Ptr{Cvoid}, num_gvars::Ptr{Csize_t},
                                inits::Ptr{Cvoid})::Nothing

    for (gv_ref, init) in zip(gvs, inits)
        gv = GlobalVariable(gv_ref)
        ptr = const_inttoptr(ConstantInt(Int64(init)), value_type(gv))
        initializer!(gv, ptr)
        obj = Base.unsafe_pointer_to_objref(init)
        @safe_info "Resolved $(name(gv)) to $obj"
    end
end

@maleadt maleadt requested review from vtjnash and gbaraldi May 15, 2025 16:00
@maleadt maleadt added compiler:codegen Generation of LLVM IR and native code gpu Affects running Julia on a GPU labels May 15, 2025
@gbaraldi
Copy link
Member

gbaraldi commented May 15, 2025

Locations seems a bit odd, I was thinking something like values? Or inits? Just to make it clear it's actually a list of jl_value_t*s. But naming aside LGTM

@maleadt
Copy link
Member Author

maleadt commented May 15, 2025

Yeah "locations" is a bit odd, but "global value values" seemed redundant while "inits" is specific to my use case. But I don't care about the name, so let's see what @vtjnash suggests.

@vchuravy
Copy link
Member

I think inits is the correct wording, these are the initializer values of the global values.

@maleadt maleadt force-pushed the tb/native_code_gvars branch from 96c06a1 to c963dcb Compare May 15, 2025 16:39
@maleadt
Copy link
Member Author

maleadt commented May 15, 2025

OK, renamed to inits.

@maleadt
Copy link
Member Author

maleadt commented May 20, 2025

@vtjnash Bump.

@gbaraldi gbaraldi merged commit 4b90899 into master May 21, 2025
8 checks passed
@gbaraldi gbaraldi deleted the tb/native_code_gvars branch May 21, 2025 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:codegen Generation of LLVM IR and native code gpu Affects running Julia on a GPU
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants