Skip to content

Commit

Permalink
Merge pull request #238 from maleadt/tb/runtime_ccall
Browse files Browse the repository at this point in the history
Remove @runtime_ccall
  • Loading branch information
maleadt authored Jun 30, 2021
2 parents ca82a4f + 04fb1cf commit e36e7cf
Show file tree
Hide file tree
Showing 9 changed files with 5,302 additions and 5,349 deletions.
4,215 changes: 2,108 additions & 2,107 deletions lib/11/libLLVM_h.jl

Large diffs are not rendered by default.

6,271 changes: 3,136 additions & 3,135 deletions lib/12/libLLVM_h.jl

Large diffs are not rendered by default.

75 changes: 43 additions & 32 deletions lib/libLLVM_extra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,43 @@
# initialization functions

function LLVMInitializeAllTargetInfos()
@runtime_ccall (:LLVMExtraInitializeAllTargetInfos, libLLVMExtra) Cvoid ()
ccall(:LLVMExtraInitializeAllTargetInfos,Cvoid,())
end

function LLVMInitializeAllTargets()
@runtime_ccall (:LLVMExtraInitializeAllTargets, libLLVMExtra) Cvoid ()
ccall(:LLVMExtraInitializeAllTargets,Cvoid,())
end

function LLVMInitializeAllTargetMCs()
@runtime_ccall (:LLVMExtraInitializeAllTargetMCs, libLLVMExtra) Cvoid ()
ccall(:LLVMExtraInitializeAllTargetMCs,Cvoid,())
end

function LLVMInitializeAllAsmPrinters()
@runtime_ccall (:LLVMExtraInitializeAllAsmPrinters, libLLVMExtra) Cvoid ()
ccall(:LLVMExtraInitializeAllAsmPrinters,Cvoid,())
end

function LLVMInitializeAllAsmParsers()
@runtime_ccall (:LLVMExtraInitializeAllAsmParsers, libLLVMExtra) Cvoid ()
ccall(:LLVMExtraInitializeAllAsmParsers,Cvoid,())
end

function LLVMInitializeAllDisassemblers()
@runtime_ccall (:LLVMExtraInitializeAllDisassemblers, libLLVMExtra) Cvoid ()
ccall(:LLVMExtraInitializeAllDisassemblers,Cvoid,())
end

function LLVMInitializeNativeTarget()
@runtime_ccall (:LLVMExtraInitializeNativeTarget, libLLVMExtra) LLVMBool ()
ccall(:LLVMExtraInitializeNativeTarget,LLVMBool,())
end

function LLVMInitializeNativeAsmPrinter()
@runtime_ccall (:LLVMExtraInitializeNativeAsmPrinter, libLLVMExtra) LLVMBool ()
ccall(:LLVMExtraInitializeNativeAsmPrinter,LLVMBool,())
end

function LLVMInitializeNativeAsmParser()
@runtime_ccall (:LLVMExtraInitializeNativeAsmParser, libLLVMExtra) LLVMBool ()
ccall(:LLVMExtraInitializeNativeAsmParser,LLVMBool,())
end

function LLVMInitializeNativeDisassembler()
@runtime_ccall (:LLVMExtraInitializeNativeDisassembler, libLLVMExtra) LLVMBool ()
ccall(:LLVMExtraInitializeNativeDisassembler,LLVMBool,())
end

# infrastructure for writing LLVM passes in Julia
Expand All @@ -50,94 +50,105 @@ end
const LLVMPassRef = Ptr{LLVMOpaquePass}

function LLVMAddPass(PM, P)
@runtime_ccall (:LLVMExtraAddPass, libLLVMExtra) Cvoid (LLVMPassManagerRef, LLVMPassRef) PM P
ccall(:LLVMExtraAddPass,Cvoid,
(LLVMPassManagerRef, LLVMPassRef),
PM, P)
end

function LLVMCreateModulePass(Name, Callback)
@runtime_ccall (:LLVMExtraCreateModulePass, libLLVMExtra) LLVMPassRef (Cstring, Any) Name Callback
ccall(:LLVMExtraCreateModulePass,LLVMPassRef,
(Cstring, Any),
Name, Callback)
end

function LLVMCreateFunctionPass(Name, Callback)
@runtime_ccall (:LLVMExtraCreateFunctionPass, libLLVMExtra) LLVMPassRef (Cstring, Any) Name Callback
ccall(:LLVMExtraCreateFunctionPass,LLVMPassRef,
(Cstring, Any),
Name, Callback)
end

function LLVMCreateBasicBlockPass(Name, Callback)
@runtime_ccall (:LLVMExtraCreateBasicBlockPass, libLLVMExtra) LLVMPassRef (Cstring, Any) Name Callback
ccall(:LLVMExtraCreateBasicBlockPass,LLVMPassRef,
(Cstring, Any),
Name, Callback)
end

function LLVMCreateModulePass2(Name, Callback, Data)
@runtime_ccall (:LLVMExtraCreateModulePass2, libLLVMExtra) LLVMPassRef (Cstring, Ptr{Cvoid}, Ptr{Cvoid}) Name Callback Data
ccall(:LLVMExtraCreateModulePass2,LLVMPassRef,
(Cstring, Ptr{Cvoid}, Ptr{Cvoid}),
Name, Callback, Data)
end

function LLVMCreateFunctionPass2(Name, Callback, Data)
@runtime_ccall (:LLVMExtraCreateFunctionPass2, libLLVMExtra) LLVMPassRef (Cstring, Ptr{Cvoid}, Ptr{Cvoid}) Name Callback Data
ccall(:LLVMExtraCreateFunctionPass2,LLVMPassRef,
(Cstring, Ptr{Cvoid}, Ptr{Cvoid}),
Name, Callback, Data)
end


# various missing functions

function LLVMAddInternalizePassWithExportList(PM, ExportList, Length)
@runtime_ccall (:LLVMExtraAddInternalizePassWithExportList, libLLVMExtra) Cvoid (LLVMPassManagerRef,Ptr{Cstring},Csize_t) PM ExportList Length
ccall(:LLVMExtraAddInternalizePassWithExportList,Cvoid,(LLVMPassManagerRef,Ptr{Cstring},Csize_t), PM, ExportList, Length)
end

function LLVMAddTargetLibraryInfoByTriple(Triple, PM)
@runtime_ccall (:LLVMExtraAddTargetLibraryInfoByTiple, libLLVMExtra) Cvoid (Cstring, LLVMPassManagerRef) Triple PM
ccall(:LLVMExtraAddTargetLibraryInfoByTiple,Cvoid,(Cstring, LLVMPassManagerRef), Triple, PM)
end

function LLVMAddNVVMReflectPass(PM, smversion)
@runtime_ccall (:LLVMExtraAddNVVMReflectFunctionPass, libLLVMExtra) Cvoid (LLVMPassManagerRef, Cuint) PM smversion
ccall(:LLVMExtraAddNVVMReflectFunctionPass,Cvoid,(LLVMPassManagerRef, Cuint), PM, smversion)
end

function LLVMAddBarrierNoopPass(PM)
@runtime_ccall (:LLVMExtraAddBarrierNoopPass, libLLVMExtra) Cvoid (LLVMPassManagerRef,) PM
ccall(:LLVMExtraAddBarrierNoopPass,Cvoid,(LLVMPassManagerRef,), PM)
end

function LLVMAddDivRemPairsPass(PM)
@runtime_ccall (:LLVMExtraAddDivRemPairsPass, libLLVMExtra) Cvoid (LLVMPassManagerRef,) PM
ccall(:LLVMExtraAddDivRemPairsPass,Cvoid,(LLVMPassManagerRef,), PM)
end

function LLVMAddLoopDistributePass(PM)
@runtime_ccall (:LLVMExtraAddLoopDistributePass, libLLVMExtra) Cvoid (LLVMPassManagerRef,) PM
ccall(:LLVMExtraAddLoopDistributePass,Cvoid,(LLVMPassManagerRef,), PM)
end

function LLVMAddLoopFusePass(PM)
@runtime_ccall (:LLVMExtraAddLoopFusePass, libLLVMExtra) Cvoid (LLVMPassManagerRef,) PM
ccall(:LLVMExtraAddLoopFusePass,Cvoid,(LLVMPassManagerRef,), PM)
end

function LLVMAddLoopLoadEliminationPass(PM)
@runtime_ccall (:LLVMExtraLoopLoadEliminationPass, libLLVMExtra) Cvoid (LLVMPassManagerRef,) PM
ccall(:LLVMExtraLoopLoadEliminationPass,Cvoid,(LLVMPassManagerRef,), PM)
end

function LLVMAddLoadStoreVectorizerPass(PM)
@runtime_ccall (:LLVMExtraAddLoadStoreVectorizerPass, libLLVMExtra) Cvoid (LLVMPassManagerRef,) PM
ccall(:LLVMExtraAddLoadStoreVectorizerPass,Cvoid,(LLVMPassManagerRef,), PM)
end

if LLVM.version() < v"12"
function LLVMAddInstSimplifyPass(PM)
@runtime_ccall (:LLVMExtraAddInstructionSimplifyPass, libLLVMExtra) Cvoid (LLVMPassManagerRef,) PM
ccall(:LLVMExtraAddInstructionSimplifyPass,Cvoid,(LLVMPassManagerRef,), PM)
end
end

function LLVMGetValueContext(V)
@runtime_ccall (:LLVMExtraGetValueContext, libLLVMExtra) LLVMContextRef (LLVMValueRef,) V
ccall(:LLVMExtraGetValueContext,LLVMContextRef,(LLVMValueRef,),V)
end

function LLVMGetSourceLocation(V, index, Name, Filename, Line, Column)
@runtime_ccall (:LLVMExtraGetSourceLocation, libLLVMExtra) Cint (LLVMValueRef,Cint,Ptr{Cstring},Ptr{Cstring},Ptr{Cuint},Ptr{Cuint}) V index Name Filename Line Column
ccall(:LLVMExtraGetSourceLocation,Cint,(LLVMValueRef,Cint,Ptr{Cstring},Ptr{Cstring},Ptr{Cuint},Ptr{Cuint}), V, index, Name, Filename, Line, Column)
end

if VERSION >= v"1.5" && !(v"1.6-" <= VERSION < v"1.6.0-DEV.90")
function LLVMExtraAppendToUsed(Mod, Values, Count)
@runtime_ccall (:LLVMExtraAppendToUsed, libLLVMExtra) Cvoid (LLVMModuleRef,Ptr{LLVMValueRef},Csize_t) Mod Values Count
ccall(:LLVMExtraAppendToUsed,Cvoid,(LLVMModuleRef,Ptr{LLVMValueRef},Csize_t), Mod, Values, Count)
end

function LLVMExtraAppendToCompilerUsed(Mod, Values, Count)
@runtime_ccall (:LLVMExtraAppendToCompilerUsed, libLLVMExtra) Cvoid (LLVMModuleRef,Ptr{LLVMValueRef},Csize_t) Mod Values Count
ccall(:LLVMExtraAppendToCompilerUsed,Cvoid,(LLVMModuleRef,Ptr{LLVMValueRef},Csize_t), Mod, Values, Count)
end

function LLVMExtraAddGenericAnalysisPasses(PM)
ccall(:LLVMExtraAddGenericAnalysisPasses, Cvoid, (LLVMPassManagerRef,), PM)
end
end

@cenum(LLVMDebugEmissionKind,
LLVMDebugEmissionKindNoDebug = 0,
Expand Down
18 changes: 12 additions & 6 deletions res/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ uuid = "fa961155-64e5-5f13-b03f-caf6b980ea82"
version = "0.4.1"

[[Clang]]
deps = ["CEnum", "Clang_jll", "TOML"]
path = "/Users/gnimuc/.julia/dev/Clang"
deps = ["CEnum", "Clang_jll", "Downloads", "Pkg", "TOML"]
git-tree-sha1 = "c142f1cf596f41fe023d7f4aabea2065347dc10c"
repo-rev = "master"
repo-url = "https://github.com/JuliaInterop/Clang.jl.git"
uuid = "40e3b903-d033-50b4-a0cc-940c62c95e31"
version = "0.13.0"
version = "0.14.0"

[[Clang_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg", "libLLVM_jll"]
git-tree-sha1 = "df205bee9db582fecbf5393a86f93426b79c0e93"
git-tree-sha1 = "a5923c06de3178dd755f4b9411ea8922a7ae6fb8"
uuid = "0ee61d77-7f21-5576-8119-9fcc46b10100"
version = "11.0.1+2"
version = "11.0.1+3"

[[Dates]]
deps = ["Printf"]
Expand Down Expand Up @@ -86,7 +88,7 @@ uuid = "14a3606d-f60d-562e-9121-12d972cd8159"
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"

[[Pkg]]
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs"]
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"]
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

[[Printf]]
Expand Down Expand Up @@ -136,3 +138,7 @@ uuid = "8f36deef-c2a5-5394-99ed-8e07531fb29a"
[[nghttp2_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"

[[p7zip_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "3f19e933-33d8-53b3-aaab-bd5110c3b7a0"
16 changes: 0 additions & 16 deletions res/wrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,6 @@ cd(@__DIR__)

@add_def off_t

# replace `ccall` with `@runtime_ccall`
function rewriter!(ctx)
for node in get_nodes(ctx.dag)
Generators.is_function(node) || continue
if !Generators.is_variadic_function(node)
expr = node.exprs[1]
call_expr = expr.args[2].args[1]
call_expr.head = :macrocall
call_expr.args[1] = Symbol("@runtime_ccall")
insert!(call_expr.args, 2, nothing)
end
end
end

function main()
options = load_options(joinpath(@__DIR__, "wrap.toml"))

Expand All @@ -42,8 +28,6 @@ function main()

build!(ctx, BUILDSTAGE_NO_PRINTING)

rewriter!(ctx)

build!(ctx, BUILDSTAGE_PRINTING_ONLY)
end

Expand Down
2 changes: 1 addition & 1 deletion src/LLVM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const libllvm = Ref{String}()
module API
using CEnum
using ..LLVM
using ..LLVM: libllvm, @runtime_ccall
using ..LLVM: libllvm

llvm_version = if version() < v"12"
"11"
Expand Down
2 changes: 1 addition & 1 deletion src/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version() = Base.libllvm_version
function runtime_version()
# FIXME: add a proper C API to LLVM
version_print = unsafe_string(
@runtime_ccall((:_ZN4llvm16LTOCodeGenerator16getVersionStringEv, libllvm[]), Cstring, ()))
ccall((:_ZN4llvm16LTOCodeGenerator16getVersionStringEv, libllvm[]), Cstring, ()))
m = match(r"LLVM version (?<version>.+)", version_print)
m === nothing && error("Unrecognized version string: '$version_print'")
if endswith(m[:version], "jl")
Expand Down
2 changes: 1 addition & 1 deletion src/init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ for backend in libllvm_backends,
@eval begin
export $jl_fname
$jl_fname() =
$supported ? @runtime_ccall(($(QuoteNode(api_fname)),libllvm[]), Cvoid, ()) :
$supported ? ccall(($(QuoteNode(api_fname)),libllvm[]), Cvoid, ()) :
error($"The $backend back-end is not part of your LLVM library.")

end
Expand Down
50 changes: 0 additions & 50 deletions src/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,53 +65,3 @@ end
@inline function refcheck(::Type, ref::Ptr)
ref==C_NULL && throw(UndefRefError())
end


## runtime ccall wrapper

"""
@runtime_ccall((function_name, library), returntype, (argtype1, ...), argvalue1, ...)
Extension of `ccall` that performs the lookup of `function_name` in `library` at run time.
This is useful in the case that `library` might not be available, in which case a function
that performs a `ccall` to that library would fail to compile.
After a slower first call to load the library and look up the function, no additional
overhead is expected compared to regular `ccall`.
"""
macro runtime_ccall(target, args...)
if VERSION >= v"1.6.0-DEV.819"
quote
ccall($(esc(target)), $(map(esc, args)...))
end
else
# decode ccall function/library target
Meta.isexpr(target, :tuple) || error("Expected (function_name, library) tuple")
function_name, library = target.args

# global const ref to hold the function pointer
@gensym fptr_cache
@eval __module__ begin
# uses atomics (release store, acquire load) for thread safety.
# see https://github.com/JuliaGPU/CUDAapi.jl/issues/106 for details
const $fptr_cache = Threads.Atomic{UInt}(0)
end

quote
# use a closure to hold the lookup and avoid code bloat in the caller
@noinline function cache_fptr!()
library = Libdl.dlopen($(esc(library)))
$(esc(fptr_cache))[] = Libdl.dlsym(library, $(esc(function_name)))

$(esc(fptr_cache))[]
end

fptr = $(esc(fptr_cache))[]
if fptr == 0 # folded into the null check performed by ccall
fptr = cache_fptr!()
end

ccall(reinterpret(Ptr{Cvoid}, fptr), $(map(esc, args)...))
end
end
end

0 comments on commit e36e7cf

Please sign in to comment.