Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/mcgen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@ function (self::ResolveCPUReferences)(mod::LLVM.Module)
for f in functions(mod)
fn = LLVM.name(f)
if isdeclaration(f) && !LLVM.isintrinsic(f) && startswith(fn, "jl_")
# eagerly resolve the address of the binding
address = ccall(:jl_cglobal, Any, (Any, Any), fn, UInt)
dereferenced = unsafe_load(address)
dereferenced = LLVM.ConstantInt(dereferenced)
# lazily resolve the address of the binding; some symbols only exist
# within the JIT (e.g. `jl_get_pgcstack_resolved`) and cannot be looked up,
# but such symbols are only ever called, not loaded from.
dereferenced = nothing
function resolve_binding()
if dereferenced === nothing
address = ccall(:jl_cglobal, Any, (Any, Any), fn, UInt)
dereferenced = LLVM.ConstantInt(unsafe_load(address))
end
dereferenced
end

function replace_bindings!(value)
changed = false
Expand All @@ -49,7 +56,7 @@ function (self::ResolveCPUReferences)(mod::LLVM.Module)
changed |= replace_bindings!(val)
elseif isa(val, LLVM.LoadInst)
# resolve
replace_uses!(val, dereferenced)
replace_uses!(val, resolve_binding())
erase!(val)
# FIXME: iterator invalidation?
changed = true
Expand Down
48 changes: 38 additions & 10 deletions test/native.jl
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,11 @@ end
end

@testset "unbound typevars" begin
mod = @eval module $(gensym())
invalid_kernel() where {unbound} = return
# suppress the warning Julia emits when defining a method with an unbound typevar
mod = redirect_stderr(devnull) do
@eval module $(gensym())
invalid_kernel() where {unbound} = return
end
end
@test_throws KernelError Native.code_llvm(devnull, mod.invalid_kernel, Tuple{})
end
Expand Down Expand Up @@ -456,20 +459,27 @@ end
Native.code_llvm(mod.g, Tuple{Int64}; dump_module=true, kernel=true)
end

@test @filecheck(begin
@check_not "@{{(julia|j)_expensive_[0-9]+}}"
Native.code_llvm(mod.g, Tuple{Int64}; dump_module=true, kernel=true, always_inline=true)
end) broken=broken
# suppress FileCheck diagnostics when the failure is known and expected
quiet(f) = broken ? redirect_stderr(f, devnull) : f()

@test quiet() do
@filecheck begin
@check_not "@{{(julia|j)_expensive_[0-9]+}}"
Native.code_llvm(mod.g, Tuple{Int64}; dump_module=true, kernel=true, always_inline=true)
end
end broken=broken

@test @filecheck begin
@check "@{{(julia|j)_expensive_[0-9]+}}"
Native.code_llvm(mod.h, Tuple{Int64}; dump_module=true, kernel=true)
end

@test @filecheck(begin
@check_not "@{{(julia|j)_expensive_[0-9]+}}"
Native.code_llvm(mod.h, Tuple{Int64}; dump_module=true, kernel=true, always_inline=true)
end) broken=broken
@test quiet() do
@filecheck begin
@check_not "@{{(julia|j)_expensive_[0-9]+}}"
Native.code_llvm(mod.h, Tuple{Int64}; dump_module=true, kernel=true, always_inline=true)
end
end broken=broken
end

@testset "function attributes" begin
Expand All @@ -495,6 +505,24 @@ end
end
end

@testset "CPU reference resolution" begin
# JIT-private symbols like `jl_get_pgcstack_resolved` (JuliaLang/julia#61527) cannot
# be looked up using `jl_cglobal`, so we should only resolve bindings that are
# actually loaded from, leaving called functions alone.
job, _ = Native.create_job(identity, (Nothing,))
JuliaContext() do ctx
mod = parse(LLVM.Module, """
declare void @jl_get_pgcstack_resolved()

define void @entry() {
call void @jl_get_pgcstack_resolved()
ret void
}""")
GPUCompiler.prepare_execution!(job, mod)
@test haskey(functions(mod), "jl_get_pgcstack_resolved")
end
end

end

############################################################################################
Expand Down