|
307 | 307 | end |
308 | 308 | end |
309 | 309 |
|
| 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 | + |
310 | 374 | end |
311 | 375 |
|
312 | 376 | ############################################################################################ |
|
0 commit comments