Skip to content
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

llvmcall(Function*) does not survive precompilation #30434

Closed
maleadt opened this issue Dec 18, 2018 · 3 comments
Closed

llvmcall(Function*) does not survive precompilation #30434

maleadt opened this issue Dec 18, 2018 · 3 comments
Labels
bug Indicates an unexpected problem or unintended behavior

Comments

@maleadt
Copy link
Member

maleadt commented Dec 18, 2018

Bug.jl:

module Bug

# from test/llvmcall.jl
function julia_to_llvm(@nospecialize x)
    isboxed = Ref{UInt8}()
    ccall(:julia_type_to_llvm,Ptr{Cvoid},(Any,Ref{UInt8}),x,isboxed)
end
const AnyTy = julia_to_llvm(Any)

const libllvmcalltest = joinpath(Sys.BINDIR, Base.LIBDIR, "libllvmcalltest")
const the_f = ccall((:MakeIdentityFunction, libllvmcalltest), Ptr{Cvoid}, (Ptr{Cvoid},), AnyTy)

@eval identity(x) = Base.llvmcall($(the_f), Any, Tuple{Any}, x)

end

main.jl:

using InteractiveUtils
using Bug

code_warntype(Bug.identity, Tuple{Any})
Bug.identity(nothing)

Crashes depending on precompilation:

$ JULIA_DEBUG=all JULIA_LOAD_PATH="@::." jj --compiled-modules=no main.jl
Body::Any
1 ─ %1 = Base.llvmcall::Core.Compiler.Const(llvmcall, false)
│   %2 = (%1)(Ptr{Nothing} @0x000056204b539648, Bug.Any, Tuple{Any}, x)::Any
└──      return %2
$ JULIA_DEBUG=all JULIA_LOAD_PATH="@::." jj test.jl
┌ Debug: Precompiling Bug [top-level]
└ @ Base loading.jl:1186
Body::Any
1 ─ %1 = Base.llvmcall::Core.Compiler.Const(llvmcall, false)
│   %2 = (%1)(Ptr{Nothing} @0x0000000000000000, Bug.Any, Tuple{Any}, x)::Any
└──      return %2
julia: /home/tbesard/Julia/julia-dev/deps/srccache/llvm-6.0.1/include/llvm/Support/Casting.h:106: static bool llvm::isa_impl_cl<llvm::GlobalVariable, const llvm::GlobalValue *>::doit(const From *) [To = llvm::GlobalVariable, From = const llvm::GlobalValue *]: Assertion `Val && "isa<> used on a null pointer"' failed.

A NULL function handle obviously crashes immediately, so the exact backtrace isn't interesting. Reproduces on both 1.0 and master. Looks like #25041 but the handle really is a literal here.
Ref JuliaGPU/CUDAnative.jl#303

@maleadt maleadt added the bug Indicates an unexpected problem or unintended behavior label Dec 18, 2018
@vtjnash
Copy link
Member

vtjnash commented Dec 18, 2018

Yes, this is what https://github.com/vtjnash/julep/wiki/0002:-Enhanced-llvmcall is intended to fix (note that it's the first item on the list of primary issues with the current design), and also might help #25984 get moving again

@maleadt
Copy link
Member Author

maleadt commented Dec 19, 2018

Uh, of course, not sure what I was thinking. Would it make sense to prevent precompilation of methods with llvmcall, in the meantime? No since the literal Function* is a precompiled global const.

@maleadt
Copy link
Member Author

maleadt commented Dec 20, 2018

My original MWE was oversimplified. I'm actually doing something like:

module Bug

function julia_to_llvm(@nospecialize x)
    isboxed = Ref{UInt8}()
    ccall(:julia_type_to_llvm,Ptr{Cvoid},(Any,Ref{UInt8}),x,isboxed)
end

@generated function indirect_llvmcall()
    AnyTy = julia_to_llvm(Any)
    the_f = ccall((:MakeIdentityFunction, "usr/lib/libllvmcalltest"), Ptr{Cvoid},
                  (Ptr{Cvoid},), AnyTy)
    quote
        Base.@_inline_meta
        Base.llvmcall($the_f, Any, Tuple{Any}, nothing)
    end
end
test() = indirect_llvmcall()

end

Which works, because the Function* is looked up as part of the @generated function, so it isn't precompiled. However, during some unrelated other change I introduced a global call to return_types, which now causes the function containing the llvmcall to be precompiled:

module Bug
...
Base.return_types(test, Tuple{})
...
end

So I can work around this limitation by not calling return_types.

@maleadt maleadt closed this as completed Dec 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants