Skip to content

Commit 4cdbb38

Browse files
authored
LLVM 17 enablement. (#404)
1 parent 07ae1c8 commit 4cdbb38

File tree

7 files changed

+70
-32
lines changed

7 files changed

+70
-32
lines changed

examples/Kaleidoscope/run.jl

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,30 @@ function generate_IR(str)
1616
end
1717

1818
function optimize!(mod::LLVM.Module)
19-
LLVM.@dispose pass_manager=LLVM.ModulePassManager() begin
20-
LLVM.instruction_combining!(pass_manager)
21-
LLVM.reassociate!(pass_manager)
22-
LLVM.gvn!(pass_manager)
23-
LLVM.cfgsimplification!(pass_manager)
24-
LLVM.promote_memory_to_register!(pass_manager)
25-
LLVM.run!(pass_manager, mod)
19+
if LLVM.has_newpm()
20+
host_triple = Sys.MACHINE # LLVM.triple() might be wrong (see LLVM.jl#108)
21+
host_t = LLVM.Target(triple=host_triple)
22+
LLVM.@dispose tm=LLVM.TargetMachine(host_t, host_triple) pb=LLVM.PassBuilder(tm) begin
23+
LLVM.NewPMModulePassManager(pb) do mpm
24+
LLVM.add!(mpm, LLVM.NewPMFunctionPassManager) do fpm
25+
LLVM.add!(fpm, LLVM.InstCombinePass())
26+
LLVM.add!(fpm, LLVM.ReassociatePass())
27+
LLVM.add!(fpm, LLVM.GVNPass())
28+
LLVM.add!(fpm, LLVM.SimplifyCFGPass())
29+
LLVM.add!(fpm, LLVM.PromotePass())
30+
end
31+
LLVM.run!(mpm, mod, tm)
32+
end
33+
end
34+
else
35+
LLVM.@dispose pass_manager=LLVM.ModulePassManager() begin
36+
LLVM.instruction_combining!(pass_manager)
37+
LLVM.reassociate!(pass_manager)
38+
LLVM.gvn!(pass_manager)
39+
LLVM.cfgsimplification!(pass_manager)
40+
LLVM.promote_memory_to_register!(pass_manager)
41+
LLVM.run!(pass_manager, mod)
42+
end
2643
end
2744
return mod
2845
end

src/LLVM.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ let
4040
if version().major < 13
4141
error("LLVM.jl only supports LLVM 13 and later.")
4242
end
43-
dir = if version().major > 16
44-
@warn "LLVM.jl has not been tested with LLVM versions newer than 16."
45-
joinpath(@__DIR__, "..", "lib", "16")
43+
dir = if version().major > 17
44+
@warn "LLVM.jl has not been tested with LLVM versions newer than 17."
45+
joinpath(@__DIR__, "..", "lib", "17")
4646
else
4747
joinpath(@__DIR__, "..", "lib", string(version().major))
4848
end
@@ -55,12 +55,15 @@ include(joinpath(@__DIR__, "..", "lib", "libLLVM_julia.jl"))
5555

5656
end # module API
5757

58+
has_oldpm() = LLVM.version() < v"17"
5859
has_newpm() = LLVM.version() >= v"15"
5960
has_julia_ojit() = VERSION >= v"1.10.0-DEV.1395"
6061

6162
# LLVM API wrappers
6263
include("support.jl")
63-
include("passregistry.jl")
64+
if LLVM.version() < v"17"
65+
include("passregistry.jl")
66+
end
6467
include("init.jl")
6568
include("core.jl")
6669
include("linker.jl")
@@ -76,7 +79,9 @@ include("targetmachine.jl")
7679
include("datalayout.jl")
7780
include("ir.jl")
7881
include("bitcode.jl")
79-
include("transform.jl")
82+
if has_oldpm()
83+
include("transform.jl")
84+
end
8085
include("debuginfo.jl")
8186
include("dibuilder.jl")
8287
include("jitevents.jl")

src/core/value/constant.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ export ConstantExpr,
355355
const_sext, const_zext, const_fptrunc, const_fpext, const_uitofp, const_sitofp,
356356
const_fptoui, const_fptosi, const_ptrtoint, const_inttoptr, const_bitcast,
357357
const_addrspacecast, const_zextorbitcast, const_sextorbitcast, const_truncorbitcast,
358-
const_pointercast, const_intcast, const_fpcast, const_select, const_shufflevector
358+
const_pointercast, const_intcast, const_fpcast, const_shufflevector
359359

360360
@checked struct ConstantExpr <: Constant
361361
ref::API.LLVMValueRef
@@ -492,9 +492,6 @@ const_intcast(val::Constant, ToType::LLVMType, isSigned::Bool) =
492492
const_fpcast(val::Constant, ToType::LLVMType) =
493493
Value(API.LLVMConstFPCast(val, ToType))
494494

495-
const_select(cond::Constant, if_true::Value, if_false::Value) =
496-
Value(API.LLVMConstSelect(cond, if_true, if_false))
497-
498495
const_extractelement(vector::Constant, index::Constant) =
499496
Value(API.LLVMConstExtractElement(vector ,index))
500497

@@ -544,6 +541,15 @@ const_fmul(lhs::Constant, rhs::Constant) =
544541

545542
end
546543

544+
if version() < v"17"
545+
546+
export const_select
547+
548+
const_select(cond::Constant, if_true::Value, if_false::Value) =
549+
Value(API.LLVMConstSelect(cond, if_true, if_false))
550+
551+
end
552+
547553
# TODO: alignof, sizeof, block_address
548554

549555

src/init.jl

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@ export ismultithreaded
44

55
ismultithreaded() = API.LLVMIsMultithreaded() |> Bool
66

7-
const subsystems = [:Core, :TransformUtils, :ScalarOpts, :Vectorization, :InstCombine,
8-
:IPO, :Analysis, :IPA, :CodeGen, :Target]
9-
if LLVM.version() < v"16"
10-
append!(subsystems, [:ObjCARCOpts, :Instrumentation])
11-
end
12-
for subsystem in subsystems
13-
jl_fname = Symbol(:Initialize, subsystem)
14-
api_fname = Symbol(:LLVM, jl_fname)
15-
@eval begin
16-
export $jl_fname
17-
$jl_fname(R::PassRegistry) = API.$api_fname(R)
7+
if LLVM.version() < v"17"
8+
const subsystems = [:Core, :TransformUtils, :ScalarOpts, :Vectorization, :InstCombine,
9+
:IPO, :Analysis, :IPA, :CodeGen, :Target]
10+
if LLVM.version() < v"16"
11+
append!(subsystems, [:ObjCARCOpts, :Instrumentation])
12+
end
13+
for subsystem in subsystems
14+
jl_fname = Symbol(:Initialize, subsystem)
15+
api_fname = Symbol(:LLVM, jl_fname)
16+
@eval begin
17+
export $jl_fname
18+
$jl_fname(R::PassRegistry) = API.$api_fname(R)
19+
end
1820
end
1921
end
2022

src/newpm/passes.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,9 @@ is_function_pass(::Type{InvalidateAllAnalysesPass}) = true
379379
@function_pass "print<cost-model>" CostModelPrinterPass
380380
@function_pass "print<cycles>" CycleInfoPrinterPass
381381
@function_pass "print<da>" DependenceAnalysisPrinterPass
382+
if LLVM.version() < v"17"
382383
@function_pass "print<divergence>" DivergenceAnalysisPrinterPass
384+
end
383385
@function_pass "print<domtree>" DominatorTreePrinterPass
384386
@function_pass "print<postdomtree>" PostDominatorTreePrinterPass
385387
@function_pass "print<delinearization>" DelinearizationPrinterPass

test/essential_tests.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@testitem "essentials" begin
22

3+
if LLVM.version() < v"17"
34
@testset "pass registry" begin
45
passreg = GlobalPassRegistry()
56

@@ -20,11 +21,12 @@
2021
InitializeIPA(passreg)
2122
InitializeCodeGen(passreg)
2223
InitializeTarget(passreg)
23-
24-
InitializeNativeTarget()
25-
InitializeAllTargetInfos()
26-
InitializeAllTargetMCs()
27-
InitializeNativeAsmPrinter()
2824
end
25+
end
26+
27+
InitializeNativeTarget()
28+
InitializeAllTargetInfos()
29+
InitializeAllTargetMCs()
30+
InitializeNativeAsmPrinter()
2931

3032
end

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,9 @@ runtests(LLVM; worker_init_expr, nworkers=min(Sys.CPU_THREADS,4), nworker_thread
4444
LLVM.has_newpm() || return false
4545
end
4646

47+
if ti.name == "transform"
48+
LLVM.has_oldpm() || return false
49+
end
50+
4751
true
4852
end

0 commit comments

Comments
 (0)