Skip to content

Commit 5aa92a2

Browse files
authored
Merge pull request #188 from maleadt/tb/simplify_init
Simplify library selection
2 parents 68c4086 + 206e23a commit 5aa92a2

File tree

13 files changed

+48
-9586
lines changed

13 files changed

+48
-9586
lines changed

lib/6.0/libLLVM_common.jl

Lines changed: 0 additions & 578 deletions
This file was deleted.

lib/6.0/libLLVM_h.jl

Lines changed: 0 additions & 3722 deletions
This file was deleted.

lib/8.0/libLLVM_common.jl

Lines changed: 0 additions & 694 deletions
This file was deleted.

lib/8.0/libLLVM_h.jl

Lines changed: 0 additions & 4490 deletions
This file was deleted.
File renamed without changes.

lib/libLLVM_extra.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function LLVMAddNVVMReflectPass(PM::LLVMPassManagerRef, smversion)
9292
end
9393
else
9494

95-
if libllvm_version < v"8.0"
95+
if version() < v"8.0"
9696
function LLVMAddNVVMReflectPass(PM::LLVMPassManagerRef, smversion)
9797
@apicall(:LLVMExtraAddNVVMReflectPass,Cvoid,(LLVMPassManagerRef,), PM)
9898
end
@@ -180,7 +180,7 @@ function LLVMExtraAddGenericAnalysisPasses(PM)
180180
end
181181
end
182182

183-
if libllvm_version >= v"8.0"
183+
if version() >= v"8.0"
184184
@cenum(LLVMDebugEmissionKind,
185185
LLVMDebugEmissionKindNoDebug = 0,
186186
LLVMDebugEmissionKindFullDebug = 1,
File renamed without changes.

src/LLVM.jl

Lines changed: 29 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -7,79 +7,29 @@ using Libdl
77

88
## discovery
99

10-
using Libdl
11-
12-
VERSION >= v"0.7.0-DEV.2576" || error("This version of LLVM.jl requires Julia 0.7")
13-
14-
let
15-
# find LLVM library
16-
17-
libllvm_paths = filter(Libdl.dllist()) do lib
18-
occursin(r"LLVM\b", basename(lib))
19-
end
20-
if isempty(libllvm_paths)
21-
error("""
22-
Cannot find the LLVM library loaded by Julia.
23-
Please use a version of Julia that has been built with USE_LLVM_SHLIB=1 (like the official binaries).
24-
If you are, please file an issue and attach the output of `Libdl.dllist()`.""")
25-
end
26-
if length(libllvm_paths) > 1
27-
error("""
28-
Multiple LLVM libraries loaded by Julia.
29-
Please file an issue and attach the output of `Libdl.dllist()`.""")
30-
end
31-
global const libllvm = first(libllvm_paths)
32-
Base.include_dependency(libllvm)
33-
34-
global const libllvm_version = Base.libllvm_version::VersionNumber
35-
36-
# figure out the supported targets by looking at initialization routines
37-
lib = Libdl.dlopen(libllvm)
38-
llvm_targets = [:AArch64, :AMDGPU, :ARC, :ARM, :AVR, :BPF, :Hexagon, :Lanai, :MSP430,
39-
:Mips, :NVPTX, :PowerPC, :RISCV, :Sparc, :SystemZ, :WebAssembly, :X86,
40-
:XCore]
41-
global const libllvm_targets = filter(llvm_targets) do target
42-
sym = Libdl.dlsym_e(lib, Symbol("LLVMInitialize$(target)Target"))
43-
sym !== nothing
44-
end
45-
# TODO: figure out the name of the native target
46-
47-
@debug "Found LLVM v$libllvm_version at $libllvm with support for $(join(libllvm_targets, ", "))"
48-
49-
50-
# find appropriate LLVM.jl wrapper
51-
52-
vercmp_match(a,b) = a.major==b.major && a.minor==b.minor
53-
vercmp_compat(a,b) = a.major>b.major || (a.major==b.major && a.minor>=b.minor)
54-
55-
llvmjl_wrappers_path = joinpath(@__DIR__, "..", "lib")
56-
57-
llvmjl_wrappers = filter(path->isdir(joinpath(llvmjl_wrappers_path, path)),
58-
readdir(llvmjl_wrappers_path))
59-
@assert !isempty(llvmjl_wrappers)
60-
61-
matching_wrappers = filter(wrapper->vercmp_match(libllvm_version,
62-
VersionNumber(wrapper)),
63-
llvmjl_wrappers)
64-
global const llvmjl_wrapper = if !isempty(matching_wrappers)
65-
@assert length(matching_wrappers) == 1
66-
matching_wrappers[1]
67-
else
68-
compatible_wrappers = filter(wrapper->vercmp_compat(libllvm_version,
69-
VersionNumber(wrapper)),
70-
llvmjl_wrappers)
71-
isempty(compatible_wrappers) && error("Could not find any compatible wrapper for LLVM $(libllvm_version)")
72-
last(compatible_wrappers)
10+
export version
11+
12+
# make sure we precompile again when LLVM changes (some definitions are version-dependent)
13+
global const libllvm = Sys.iswindows() ? :LLVM : :libLLVM
14+
Base.include_dependency(Libdl.dlpath(libllvm))
15+
16+
const libllvm_version = Ref{VersionNumber}()
17+
function version()
18+
if !isassigned(libllvm_version)
19+
# FIXME: add a proper C API to LLVM
20+
version_print = unsafe_string(
21+
ccall((:_ZN4llvm16LTOCodeGenerator16getVersionStringEv, libllvm), Cstring, ()))
22+
m = match(r"LLVM version (?<version>.+)", version_print)
23+
m === nothing && error("Unrecognized version string: '$version_print'")
24+
libllvm_version[] = if endswith(m[:version], "jl")
25+
# strip the "jl" SONAME suffix (JuliaLang/julia#33058)
26+
# (LLVM does never report a prerelease version anyway)
27+
VersionNumber(m[:version][1:end-2])
28+
else
29+
VersionNumber(m[:version])
30+
end
7331
end
74-
75-
@debug "Using LLVM.jl wrapper for LLVM v$llvmjl_wrapper"
76-
77-
78-
# backwards-compatible flags
79-
80-
global const libllvm_system = false
81-
82-
global const configured = true
32+
return libllvm_version[]
8333
end
8434

8535

@@ -92,12 +42,12 @@ include("base.jl")
9242
module API
9343
using CEnum
9444
using ..LLVM
95-
using ..LLVM: @apicall, libllvm_version
45+
using ..LLVM: @apicall
9646
const off_t = Csize_t
97-
libdir = joinpath(@__DIR__, "..", "lib", LLVM.llvmjl_wrapper)
47+
libdir = joinpath(@__DIR__, "..", "lib")
9848
include(joinpath(libdir, "libLLVM_common.jl"))
9949
include(joinpath(libdir, "libLLVM_h.jl"))
100-
include(joinpath(libdir, "..", "libLLVM_extra.jl"))
50+
include(joinpath(libdir, "libLLVM_extra.jl"))
10151
end
10252

10353
# LLVM API wrappers
@@ -130,19 +80,10 @@ include("deprecated.jl")
13080
## initialization
13181

13282
function __init__()
133-
libllvm_paths = filter(Libdl.dllist()) do lib
134-
occursin(r"LLVM\b", basename(lib))
135-
end
136-
if length(libllvm_paths) > 1
137-
# NOTE: this still allows switching to a non-USE_LLVM_SHLIB version, but
138-
# there's no way to detect that since the new libLLVM is loaded before this...
139-
cachefile = if VERSION >= v"1.3-"
140-
Base.compilecache_path(Base.PkgId(LLVM))
141-
else
142-
abspath(DEPOT_PATH[1], Base.cache_file_entry(Base.PkgId(LLVM)))
143-
end
144-
rm(cachefile)
145-
error("Your set-up changed, and LLVM.jl needs to be reconfigured. Please load the package again.")
83+
libllvm_version = version()
84+
@debug "Using LLVM $libllvm_version at $(Libdl.dlpath(libllvm))"
85+
if libllvm_version != Base.libllvm_version
86+
@warn "Using a different version of LLVM ($libllvm_version) than the one shipped with Julia ($(Base.libllvm_version)); this is unsupported"
14687
end
14788

14889
_install_handlers()

src/core/module.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ end
194194
## module flag iteration
195195
# TODO: doesn't actually iterate, since we can't list the available keys
196196

197-
if libllvm_version >= v"8.0"
197+
if version() >= v"8.0"
198198

199199
export flags
200200

src/debuginfo.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ DEBUG_METADATA_VERSION() = API.LLVMDebugMetadataVersion()
44

55
strip_debuginfo!(mod::Module) = API.LLVMStripModuleDebugInfo(ref(mod))
66

7-
if libllvm_version >= v"8.0"
7+
if version() >= v"8.0"
88
set_subprogram!(func::Function, sp::Metadata) = LLVM.API.LLVMSetSubprogram(ref(func), ref(sp))
99
get_subprogram(func::Function) = Metadata(LLVM.API.LLVMGetSubprogram(ref(func)))
1010
end

0 commit comments

Comments
 (0)