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

Upgrade wrapper generator #226

Merged
merged 6 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
741 changes: 0 additions & 741 deletions lib/libLLVM_common.jl

This file was deleted.

2 changes: 0 additions & 2 deletions lib/libLLVM_extra.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Julia wrapper for source: julia/src/llvm-api.cpp


# initialization functions

function LLVMInitializeAllTargetInfos()
Expand Down Expand Up @@ -43,7 +42,6 @@ function LLVMInitializeNativeDisassembler()
ccall(:LLVMExtraInitializeNativeDisassembler,LLVMBool,())
end


# infrastructure for writing LLVM passes in Julia

mutable struct LLVMOpaquePass
Expand Down
6,083 changes: 3,480 additions & 2,603 deletions lib/libLLVM_h.jl

Large diffs are not rendered by default.

116 changes: 54 additions & 62 deletions res/Manifest.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# This file is machine-generated - editing it directly is not advised

[[ArgTools]]
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"

[[Artifacts]]
deps = ["Pkg"]
git-tree-sha1 = "c30985d8821e0cd73870b17b0ed0ce6dc44cb744"
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
version = "1.3.0"

[[Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand All @@ -15,92 +15,86 @@ uuid = "fa961155-64e5-5f13-b03f-caf6b980ea82"
version = "0.4.1"

[[Clang]]
deps = ["CEnum", "Clang_jll", "DataStructures", "Libdl"]
git-tree-sha1 = "5237d0c6c52179b958df0aeef171658a791456ab"
deps = ["CEnum", "Clang_jll", "TOML"]
path = "/Users/gnimuc/.julia/dev/Clang"
uuid = "40e3b903-d033-50b4-a0cc-940c62c95e31"
version = "0.12.1"
version = "0.13.0"

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

[[Compat]]
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
git-tree-sha1 = "a706ff10f1cd8dab94f59fd09c0e657db8e77ff0"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.23.0"

[[DataStructures]]
deps = ["Compat", "InteractiveUtils", "OrderedCollections"]
git-tree-sha1 = "fb0aa371da91c1ff9dc7fbed6122d3e411420b9c"
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.18.8"
version = "11.0.1+2"

[[Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"

[[DelimitedFiles]]
deps = ["Mmap"]
uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"

[[Distributed]]
deps = ["Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"
[[Downloads]]
deps = ["ArgTools", "LibCURL", "NetworkOptions"]
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"

[[InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[JLLWrappers]]
git-tree-sha1 = "c70593677bbf2c3ccab4f7500d0f4dacfff7b75c"
git-tree-sha1 = "a431f5f2ca3f4feef3bd7a5e94b8b8d4f2f647a0"
uuid = "692b3bcd-3c85-4b1f-b108-f13ce0eb3210"
version = "1.1.3"
version = "1.2.0"

[[LLVM_full_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
git-tree-sha1 = "465041a64bcea5a504733323f9bf1b4c6220883d"
git-tree-sha1 = "3a744e1960881da01d8f2ebc6117619ee56ca503"
uuid = "a3ccf953-465e-511d-b87f-60a6490c289d"
version = "11.0.0+6"
version = "11.0.0+10"

[[LibCURL]]
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21"

[[LibCURL_jll]]
deps = ["Artifacts", "LibSSH2_jll", "Libdl", "MbedTLS_jll", "Zlib_jll", "nghttp2_jll"]
uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0"

[[LibGit2]]
deps = ["Printf"]
deps = ["Base64", "NetworkOptions", "Printf", "SHA"]
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"

[[LibSSH2_jll]]
deps = ["Artifacts", "Libdl", "MbedTLS_jll"]
uuid = "29816b5a-b9ab-546f-933c-edad1886dfa8"

[[Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"

[[LinearAlgebra]]
deps = ["Libdl"]
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[[Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

[[Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[Mmap]]
uuid = "a63ad114-7e13-5084-954f-fe012c677804"
[[MbedTLS_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"

[[OrderedCollections]]
git-tree-sha1 = "cf59cfed2e2c12e8a2ff0a4f1e9b2cd8650da6db"
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
version = "1.3.2"
[[MozillaCACerts_jll]]
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"

[[NetworkOptions]]
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"

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

[[Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[[REPL]]
deps = ["InteractiveUtils", "Markdown", "Sockets"]
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"

[[Random]]
Expand All @@ -113,24 +107,16 @@ uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
[[Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

[[SharedArrays]]
deps = ["Distributed", "Mmap", "Random", "Serialization"]
uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383"

[[Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"

[[SparseArrays]]
deps = ["LinearAlgebra", "Random"]
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
[[TOML]]
deps = ["Dates"]
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"

[[Statistics]]
deps = ["LinearAlgebra", "SparseArrays"]
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[[Test]]
deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
[[Tar]]
deps = ["ArgTools", "SHA"]
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"

[[UUIDs]]
deps = ["Random", "SHA"]
Expand All @@ -139,8 +125,14 @@ uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
[[Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"

[[Zlib_jll]]
deps = ["Libdl"]
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"

[[libLLVM_jll]]
deps = ["Libdl", "Pkg"]
git-tree-sha1 = "8bbcb2ac071438cd061ac210f6ba6754f45b8672"
deps = ["Artifacts", "Libdl"]
uuid = "8f36deef-c2a5-5394-99ed-8e07531fb29a"
version = "9.0.1+5"

[[nghttp2_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"
40 changes: 40 additions & 0 deletions res/missing_defs.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
@static if VERSION >= v"1.6.0"
@static if Sys.islinux() && Sys.ARCH === :aarch64 && !occursin("musl", Base.BUILD_TRIPLET)
const __off_t = Clong
const off_t = __off_t
elseif Sys.islinux() && Sys.ARCH === :aarch64 && occursin("musl", Base.BUILD_TRIPLET)
const off_t = Clong
elseif Sys.islinux() && startswith(string(Sys.ARCH), "arm") && !occursin("musl", Base.BUILD_TRIPLET)
const __off_t = Clong
const off_t = __off_t
elseif Sys.islinux() && startswith(string(Sys.ARCH), "arm") && occursin("musl", Base.BUILD_TRIPLET)
const off_t = Clonglong
elseif Sys.islinux() && Sys.ARCH === :i686 && !occursin("musl", Base.BUILD_TRIPLET)
const __off_t = Clong
const off_t = __off_t
elseif Sys.islinux() && Sys.ARCH === :i686 && occursin("musl", Base.BUILD_TRIPLET)
const off_t = Clonglong
elseif Sys.iswindows() && Sys.ARCH === :i686
const off32_t = Clong
const off_t = off32_t
elseif Sys.islinux() && Sys.ARCH === :powerpc64le
const __off_t = Clong
const off_t = __off_t
elseif Sys.isapple() && Sys.ARCH === :x86_64
const __darwin_off_t = Int64
const off_t = __darwin_off_t
elseif Sys.islinux() && Sys.ARCH === :x86_64 && !occursin("musl", Base.BUILD_TRIPLET)
const __off_t = Clong
const off_t = __off_t
elseif Sys.islinux() && Sys.ARCH === :x86_64 && occursin("musl", Base.BUILD_TRIPLET)
const off_t = Clong
elseif Sys.isbsd() && !Sys.isapple()
const __off_t = Int64
const off_t = __off_t
elseif Sys.iswindows() && Sys.ARCH === :x86_64
const off32_t = Clong
const off_t = off32_t
end
else
const off_t = Csize_t
end
74 changes: 30 additions & 44 deletions res/wrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,49 @@

using LLVM_full_jll

using Clang
using Clang.Generators

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"))

function wrap()
includedir = LLVM_full_jll.llvm_config() do config
readchomp(`$config --includedir`)
end
cppflags = LLVM_full_jll.llvm_config() do config
split(readchomp(`$config --cppflags`))
end

# Set-up arguments to clang
clang_includes = map(x->x[3:end], filter( x->startswith(x,"-I"), cppflags))
clang_extraargs = filter(x->!startswith(x,"-I"), cppflags)
args = get_default_args("x86_64-linux-gnu")
push!(args, "-I$includedir")
append!(args, cppflags)

# FIXME: Clang.jl doesn't properly detect system headers
push!(clang_extraargs, "-I/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include")
header_files = detect_headers(joinpath(includedir, "llvm-c"), args)

# Recursively discover LLVM C API headers (files ending in .h)
header_dirs = String[joinpath(includedir, "llvm-c")]
header_files = String[]
while !isempty(header_dirs)
parent = pop!(header_dirs)
children = readdir(parent)
for child in children
path = joinpath(parent, child)
if isdir(path)
push!(header_dirs, path)
elseif isfile(path) && endswith(path, ".h")
push!(header_files, path)
end
end
end
ctx = create_context(header_files, args, options)

context = init(;
headers = header_files,
output_file = "libLLVM_h.jl",
common_file = "libLLVM_common.jl",
clang_includes = convert(Vector{String}, clang_includes),
clang_args = convert(Vector{String}, clang_extraargs),
header_library = x->"libllvm",
header_wrapped = (top,cursor)->occursin("include/llvm", cursor)
)
build!(ctx, BUILDSTAGE_NO_PRINTING)

run(context)
end
rewriter!(ctx)

function main()
cd(joinpath(dirname(@__DIR__), "lib")) do
wrap()
end
build!(ctx, BUILDSTAGE_PRINTING_ONLY)
end

isinteractive() || main()

# Manual clean-up:
# - remove build-host details (LLVM_DEFAULT_TARGET_TRIPLE etc) in libLLVM_common.jl
# - remove "# Skipping ..." comments by Clang.jl
# - replace `const (LLVMOpaque.*) = Cvoid` with `struct $1 end`
# - use `gawk -i inplace '/^[[:blank:]]*$/ { print; next; }; {cur = seen[$0]; if(!seen[$0]++ || (/^end$/ && !prev) || /^.*Clang.*$/) print $0; prev=cur}' libLLVM_h.jl` to remove duplicates
# - use `cat -s` to remove duplicate empty lines
35 changes: 35 additions & 0 deletions res/wrap.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[general]
library_name = "libllvm[]"
output_file_path = "../lib/libLLVM_h.jl"
prologue_file_path = "./missing_defs.jl"
epilogue_file_path = ""
use_julia_native_enum_type = false
use_deterministic_symbol = true
printer_blacklist = [
"LLVM_C_STRICT_PROTOTYPES_END",
"LLVM_NATIVE_ARCH",
"LLVMInitializeAllTargetInfos",
"LLVMInitializeAllTargets",
"LLVMInitializeAllTargetMCs",
"LLVMInitializeAllAsmPrinters",
"LLVMInitializeAllAsmParsers",
"LLVMInitializeAllDisassemblers",
"LLVMInitializeNativeTarget",
"LLVMInitializeNativeAsmPrinter",
"LLVMInitializeNativeAsmParser",
"LLVMInitializeNativeDisassembler",
"LLVM_C_EXTERN_C_BEGIN",
"LLVM_C_EXTERN_C_END",
"HUGE_VALF",
]

[codegen]
use_julia_bool = true
always_NUL_terminated_string = true
is_function_strictly_typed = false
opaque_func_arg_as_PtrCvoid = false
opaque_as_mutable_struct = true

[codegen.macro]
macro_mode = "basic"
add_comment_for_skipped_macro = false
2 changes: 0 additions & 2 deletions src/LLVM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ module API
using CEnum
using ..LLVM
using ..LLVM: libllvm, @runtime_ccall
const off_t = Csize_t
libdir = joinpath(@__DIR__, "..", "lib")
include(joinpath(libdir, "libLLVM_common.jl"))
include(joinpath(libdir, "libLLVM_h.jl"))
include(joinpath(libdir, "libLLVM_extra.jl"))
end
Expand Down
2 changes: 1 addition & 1 deletion src/core/function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct FunctionAttrSet
idx::API.LLVMAttributeIndex
end

function_attributes(f::Function) = FunctionAttrSet(f, API.LLVMAttributeFunctionIndex)
function_attributes(f::Function) = FunctionAttrSet(f, reinterpret(API.LLVMAttributeIndex, API.LLVMAttributeFunctionIndex))
parameter_attributes(f::Function, idx::Integer) = FunctionAttrSet(f, API.LLVMAttributeIndex(idx))
return_attributes(f::Function) = FunctionAttrSet(f, API.LLVMAttributeReturnIndex)

Expand Down