Skip to content

Commit b2524b5

Browse files
committed
Add imaging_mode support (static compilation)
1 parent cd8e62e commit b2524b5

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

src/interface.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,6 @@ link_libraries!(::CompilerJob, mod::LLVM.Module, undefined_fns::Vector{String})
158158

159159
# whether pointer is a valid call target
160160
valid_function_pointer(::CompilerJob, ptr::Ptr{Cvoid}) = false
161+
162+
# whether we should compile in imaging mode
163+
extern_policy(::CompilerJob) = false

src/jlgen.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ function compile_method_instance(@nospecialize(job::CompilerJob), method_instanc
213213
# generate IR
214214
native_code = ccall(:jl_create_native, Ptr{Cvoid},
215215
(Vector{MethodInstance}, Base.CodegenParams, Cint),
216-
[method_instance], params, #=extern policy=# 1)
216+
[method_instance], params, extern_policy(job) ? 2 : 1)
217217
@assert native_code != C_NULL
218218
llvm_mod_ref = ccall(:jl_get_llvm_module, LLVM.API.LLVMModuleRef,
219219
(Ptr{Cvoid},), native_code)

src/native.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export NativeCompilerTarget
77
Base.@kwdef struct NativeCompilerTarget <: AbstractCompilerTarget
88
cpu::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUName())
99
features::String=(LLVM.version() < v"8") ? "" : unsafe_string(LLVM.API.LLVMGetHostCPUFeatures())
10+
reloc::LLVM.API.LLVMRelocMode=LLVM.API.LLVMRelocDefault
11+
extern::Bool
1012
end
1113

1214
llvm_triple(::NativeCompilerTarget) = Sys.MACHINE
@@ -16,12 +18,17 @@ function llvm_machine(target::NativeCompilerTarget)
1618

1719
t = Target(triple=triple)
1820

19-
tm = TargetMachine(t, triple, target.cpu, target.features)
21+
optlevel = LLVM.API.LLVMCodeGenLevelDefault
22+
reloc = target.reloc
23+
tm = TargetMachine(t, triple, target.cpu, target.features, optlevel, reloc)
2024
asm_verbosity!(tm, true)
2125

2226
return tm
2327
end
2428

29+
GPUCompiler.extern_policy(job::CompilerJob{NativeCompilerTarget,P} where P) =
30+
job.target.extern
31+
2532

2633
## job
2734

test/native.jl

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,70 @@ end
307307
end
308308
end
309309

310+
using Libdl
311+
312+
function generate_shlib_fptr(f, tt, name=GPUCompiler.safe_name(repr(f)))
313+
mktemp() do path, io
314+
source = FunctionSpec(f, Base.to_tuple_type(tt), false, name)
315+
target = NativeCompilerTarget(;reloc=LLVM.API.LLVMRelocPIC, extern=true)
316+
params = TestCompilerParams()
317+
job = CompilerJob(target, source, params)
318+
obj, _ = GPUCompiler.codegen(:obj, job; strip=true, only_entry=false, validate=false)
319+
write(io, obj)
320+
flush(io)
321+
# FIXME: Be more portable
322+
run(`ld -shared -o $path.$dlext $path`)
323+
ptr = dlopen("$path.$dlext", Libdl.RTLD_LOCAL)
324+
fptr = dlsym(ptr, "julia_$name")
325+
@assert fptr != C_NULL
326+
atexit(()->rm("$path.$dlext"))
327+
fptr
328+
end
329+
end
330+
331+
@static if VERSION >= v"1.7.0-DEV.600"
332+
@testset "shared library emission" begin
333+
f1(x) = x+1
334+
@test ccall(generate_shlib_fptr(f1, (Int,)), Int, (Int,), 1) == 2
335+
f2(x,y) = x+y
336+
@test ccall(generate_shlib_fptr(f2, (Int,Int)), Int, (Int,Int), 1, 2) == 3
337+
f3(str) = str*"!"
338+
@test_skip ccall(generate_shlib_fptr(f3, (String,)), String, (String,), "Hello") == "Hello!"
339+
function f4()
340+
# Something reasonably complicated
341+
if isdir(homedir())
342+
true
343+
else
344+
false
345+
end
346+
end
347+
@test ccall(generate_shlib_fptr(f4, ()), Bool, ())
348+
f5() = :asymbol
349+
@test_skip ccall(generate_shlib_fptr(f5, ()), Symbol, ()) == :asymbol
350+
f6(x) = x == :asymbol ? true : false
351+
@test_skip ccall(generate_shlib_fptr(f6, (Symbol,)), Bool, (Symbol,), :asymbol)
352+
@test_skip !ccall(generate_shlib_fptr(f6, (Symbol,)), Bool, (Symbol,), :bsymbol)
353+
#= FIXME
354+
function f7(A, sym)
355+
if sym != :asymbol
356+
A[] = true
357+
else
358+
A[] = false
359+
end
360+
return nothing
361+
end
362+
A = Ref(false)
363+
ccall(generate_shlib_fptr(f7, (Base.RefValue{Bool}, Symbol)), Nothing, (Base.RefValue{Bool}, Symbol), A, :asymbol); @test A[]
364+
ccall(generate_shlib_fptr(f7, (Base.RefValue{Bool}, Symbol)), Nothing, (Base.RefValue{Bool}, Symbol), A, :bsymbol); @test !A[]
365+
=#
366+
y = [42.0]
367+
function cf1(x)
368+
x + y[1]
369+
end
370+
@test ccall(generate_shlib_fptr(cf1, (Float64,)), Float64, (Any, Float64,), cf1, 1.0) == 43.0
371+
end
372+
end
373+
310374
end
311375

312376
############################################################################################

0 commit comments

Comments
 (0)