Skip to content

Commit

Permalink
Add imaging_mode support (static compilation)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsamaroo committed Jun 9, 2021
1 parent 2c4a304 commit 9fbe9cc
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,6 @@ function llvm_debug_info(@nospecialize(job::CompilerJob))
LLVM.API.LLVMDebugEmissionKindFullDebug
end
end

# whether we should compile in imaging mode
extern_policy(::CompilerJob) = false
11 changes: 9 additions & 2 deletions src/native.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export NativeCompilerTarget
Base.@kwdef struct NativeCompilerTarget <: AbstractCompilerTarget
cpu::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUName())
features::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUFeatures())
always_inline::Bool=false # will mark the job function as always inline
always_inline::Bool=false # will mark the job function as always inline
reloc::LLVM.API.LLVMRelocMode=LLVM.API.LLVMRelocDefault
extern::Bool
end

llvm_triple(::NativeCompilerTarget) = Sys.MACHINE
Expand All @@ -17,7 +19,9 @@ function llvm_machine(target::NativeCompilerTarget)

t = Target(triple=triple)

tm = TargetMachine(t, triple, target.cpu, target.features)
optlevel = LLVM.API.LLVMCodeGenLevelDefault
reloc = target.reloc
tm = TargetMachine(t, triple, target.cpu, target.features, optlevel, reloc)
asm_verbosity!(tm, true)

return tm
Expand All @@ -30,6 +34,9 @@ function process_entry!(job::CompilerJob{NativeCompilerTarget}, mod::LLVM.Module
invoke(process_entry!, Tuple{CompilerJob, LLVM.Module, LLVM.Function}, job, mod, entry)
end

GPUCompiler.extern_policy(job::CompilerJob{NativeCompilerTarget,P} where P) =
job.target.extern

## job

runtime_slug(job::CompilerJob{NativeCompilerTarget}) = "native_$(job.target.cpu)-$(hash(job.target.features))"
64 changes: 64 additions & 0 deletions test/native.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,70 @@ end
@test call_delayed(complex, 1.0, 2.0) == 1.0+2.0im
end

using Libdl

function generate_shlib_fptr(f, tt, name=GPUCompiler.safe_name(repr(f)))
mktemp() do path, io
source = FunctionSpec(f, Base.to_tuple_type(tt), false, name)
target = NativeCompilerTarget(;reloc=LLVM.API.LLVMRelocPIC, extern=true)
params = TestCompilerParams()
job = CompilerJob(target, source, params)
obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false)
write(io, obj)
flush(io)
# FIXME: Be more portable
run(`ld -shared -o $path.$dlext $path`)
ptr = dlopen("$path.$dlext", Libdl.RTLD_LOCAL)
fptr = dlsym(ptr, "julia_$name")
@assert fptr != C_NULL
atexit(()->rm("$path.$dlext"))
fptr
end
end

@static if VERSION >= v"1.7.0-DEV.600"
@testset "shared library emission" begin
f1(x) = x+1
@test ccall(generate_shlib_fptr(f1, (Int,)), Int, (Int,), 1) == 2
f2(x,y) = x+y
@test ccall(generate_shlib_fptr(f2, (Int,Int)), Int, (Int,Int), 1, 2) == 3
f3(str) = str*"!"
@test_skip ccall(generate_shlib_fptr(f3, (String,)), String, (String,), "Hello") == "Hello!"
function f4()
# Something reasonably complicated
if isdir(homedir())
true
else
false
end
end
@test ccall(generate_shlib_fptr(f4, ()), Bool, ())
f5() = :asymbol
@test_skip ccall(generate_shlib_fptr(f5, ()), Symbol, ()) == :asymbol
f6(x) = x == :asymbol ? true : false
@test_skip ccall(generate_shlib_fptr(f6, (Symbol,)), Bool, (Symbol,), :asymbol)
@test_skip !ccall(generate_shlib_fptr(f6, (Symbol,)), Bool, (Symbol,), :bsymbol)
#= FIXME
function f7(A, sym)
if sym != :asymbol
A[] = true
else
A[] = false
end
return nothing
end
A = Ref(false)
ccall(generate_shlib_fptr(f7, (Base.RefValue{Bool}, Symbol)), Nothing, (Base.RefValue{Bool}, Symbol), A, :asymbol); @test A[]
ccall(generate_shlib_fptr(f7, (Base.RefValue{Bool}, Symbol)), Nothing, (Base.RefValue{Bool}, Symbol), A, :bsymbol); @test !A[]
=#
y = [42.0]
function cf1(x)
x + y[1]
end
@test ccall(generate_shlib_fptr(cf1, (Float64,)), Float64, (Any, Float64,), cf1, 1.0) == 43.0
end
end

end

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

0 comments on commit 9fbe9cc

Please sign in to comment.