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

Adapt to LLVM.jl 8 #591

Merged
merged 1 commit into from
Jun 27, 2024
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ InteractiveUtils = "1"
Libdl = "1"
Logging = "1"
UUIDs = "1"
LLVM = "7.1"
LLVM = "8"
Scratch = "1"
TimerOutputs = "0.5"
julia = "1.8"
32 changes: 15 additions & 17 deletions src/driver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,8 @@ const __llvm_initialized = Ref(false)
end
end
if use_newpm && LLVM.version() >= v"17"
@dispose pb=PassBuilder() mpm=NewPMModulePassManager(pb) begin
add!(mpm, InternalizePass(InternalizePassOptions(; preserved_gvs)))
run!(mpm, ir)
end
run!(InternalizePass(InternalizePassOptions(; preserved_gvs)), ir,
llvm_machine(job.config.target))
else
@dispose pm=ModulePassManager() begin
internalize!(pm, preserved_gvs)
Expand All @@ -363,17 +361,17 @@ const __llvm_initialized = Ref(false)
# XXX: make these part of the optimizer pipeline?
if has_deferred_jobs
if use_newpm
@dispose pb=PassBuilder() mpm=NewPMModulePassManager(pb) begin
add!(mpm, NewPMFunctionPassManager) do fpm
@dispose pb=NewPMPassBuilder() begin
add!(pb, NewPMFunctionPassManager()) do fpm
add!(fpm, InstCombinePass())
end
add!(mpm, AlwaysInlinerPass())
add!(mpm, NewPMFunctionPassManager) do fpm
add!(pb, AlwaysInlinerPass())
add!(pb, NewPMFunctionPassManager()) do fpm
add!(fpm, SROAPass())
add!(fpm, GVNPass())
end
add!(mpm, MergeFunctionsPass())
run!(mpm, ir)
add!(pb, MergeFunctionsPass())
run!(pb, ir, llvm_machine(job.config.target))
end
else
@dispose pm=ModulePassManager() begin
Expand Down Expand Up @@ -402,13 +400,13 @@ const __llvm_initialized = Ref(false)
if cleanup
@timeit_debug to "clean-up" begin
if use_newpm
@dispose pb=PassBuilder() mpm=NewPMModulePassManager(pb) begin
add!(mpm, RecomputeGlobalsAAPass())
add!(mpm, GlobalOptPass())
add!(mpm, GlobalDCEPass())
add!(mpm, StripDeadPrototypesPass())
add!(mpm, ConstantMergePass())
run!(mpm, ir)
@dispose pb=NewPMPassBuilder() begin
add!(pb, RecomputeGlobalsAAPass())
add!(pb, GlobalOptPass())
add!(pb, GlobalDCEPass())
add!(pb, StripDeadPrototypesPass())
add!(pb, ConstantMergePass())
run!(pb, ir, llvm_machine(job.config.target))
end
else
# we can only clean-up now, as optimization may lower or introduce calls to
Expand Down
26 changes: 22 additions & 4 deletions src/irgen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ function irgen(@nospecialize(job::CompilerJob))
push!(preserved_gvs, LLVM.name(gvar))
end
if use_newpm && LLVM.version() >= v"17"
@dispose pb=PassBuilder() mpm=NewPMModulePassManager(pb) begin
add!(mpm, InternalizePass(InternalizePassOptions(; preserved_gvs)))
add!(mpm, AlwaysInlinerPass())
run!(mpm, mod)
@dispose pb=NewPMPassBuilder() begin
add!(pb, InternalizePass(InternalizePassOptions(; preserved_gvs)))
add!(pb, AlwaysInlinerPass())
run!(pb, mod, llvm_machine(job.config.target))
end
else
@dispose pm=ModulePassManager() begin
Expand Down Expand Up @@ -718,6 +718,12 @@ function add_kernel_state!(mod::LLVM.Module)

return true
end
if LLVM.has_newpm()
AddKernelStatePass() = NewPMModulePass("AddKernelStatePass", add_kernel_state!)
else
add_kernel_state!(pm::PassManager) =
add!(pm, ModulePass("AddKernelStatePass", add_kernel_state!))
end

# lower calls to the state getter intrinsic. this is a two-step process, so that the state
# argument can be added before optimization, and that optimization can introduce new uses
Expand Down Expand Up @@ -769,6 +775,12 @@ function lower_kernel_state!(fun::LLVM.Function)

return changed
end
if LLVM.has_newpm()
LowerKernelStatePass() = NewPMFunctionPass("LowerKernelStatePass", lower_kernel_state!)
else
lower_kernel_state!(pm::PassManager) =
add!(pm, FunctionPass("LowerKernelStatePass", lower_kernel_state!))
end

function cleanup_kernel_state!(mod::LLVM.Module)
job = current_job::CompilerJob
Expand All @@ -786,6 +798,12 @@ function cleanup_kernel_state!(mod::LLVM.Module)

return changed
end
if LLVM.has_newpm()
CleanupKernelStatePass() = NewPMModulePass("CleanupKernelStatePass", cleanup_kernel_state!)
else
cleanup_kernel_state!(pm::PassManager) =
add!(pm, ModulePass("CleanupKernelStatePass", cleanup_kernel_state!))
end

function kernel_state_intr(mod::LLVM.Module, T_state)
state_intr = if haskey(functions(mod), "julia.gpu.state_getter")
Expand Down
27 changes: 18 additions & 9 deletions src/mcgen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ function prepare_execution!(@nospecialize(job::CompilerJob), mod::LLVM.Module)
current_job = job

if use_newpm
@dispose pb=PassBuilder() mpm=NewPMModulePassManager(pb) begin
add!(mpm, RecomputeGlobalsAAPass())
add!(mpm, GlobalOptPass())
resolve_cpu_references!(mod)
add!(legacy2newpm(resolve_cpu_references!), mpm)
add!(mpm, GlobalDCEPass())
add!(mpm, StripDeadPrototypesPass())
run!(mpm, mod)
@dispose pb=NewPMPassBuilder() begin
register!(pb, ResolveCPUReferencesPass())

add!(pb, RecomputeGlobalsAAPass())
add!(pb, GlobalOptPass())
add!(pb, ResolveCPUReferencesPass())
add!(pb, GlobalDCEPass())
add!(pb, StripDeadPrototypesPass())

run!(pb, mod, llvm_machine(job.config.target))
end
else
@dispose pm=ModulePassManager() begin
global_optimizer!(pm)

add!(pm, ModulePass("ResolveCPUReferences", resolve_cpu_references!))
resolve_cpu_references!(pm)

global_dce!(pm)
strip_dead_prototypes!(pm)
Expand Down Expand Up @@ -76,6 +78,13 @@ function resolve_cpu_references!(mod::LLVM.Module)

return changed
end
if LLVM.has_newpm()
ResolveCPUReferencesPass() =
NewPMModulePass("ResolveCPUReferences", resolve_cpu_references!)
else
resolve_cpu_references!(pm::PassManager) =
add!(pm, ModulePass("ResolveCPUReferences", resolve_cpu_references!))
end


function mcgen(@nospecialize(job::CompilerJob), mod::LLVM.Module, format=LLVM.API.LLVMAssemblyFile)
Expand Down
38 changes: 18 additions & 20 deletions src/metal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ function finish_module!(@nospecialize(job::CompilerJob{MetalCompilerTarget}), mo
# we emit properties (of the air and metal version) as private global constants,
# so run the optimizer so that they are inlined before the rest of the optimizer runs.
if use_newpm
@dispose pb=PassBuilder() mpm=NewPMModulePassManager(pb) begin
add!(mpm, RecomputeGlobalsAAPass())
add!(mpm, GlobalOptPass())
run!(mpm, mod)
@dispose pb=NewPMPassBuilder() begin
add!(pb, RecomputeGlobalsAAPass())
add!(pb, GlobalOptPass())
run!(pb, mod)
end
else
@dispose pm=ModulePassManager() begin
Expand Down Expand Up @@ -122,13 +122,13 @@ function hide_noreturn!(mod::LLVM.Module)
any_noreturn || return false

if use_newpm
@dispose pb=PassBuilder() mpm=NewPMModulePassManager(pb) begin
add!(mpm, AlwaysInlinerPass())
add!(mpm, NewPMFunctionPassManager) do fpm
@dispose pb=NewPMPassBuilder() begin
add!(pb, AlwaysInlinerPass())
add!(pb, NewPMFunctionPassManager()) do fpm
add!(fpm, SimplifyCFGPass())
add!(fpm, InstCombinePass())
end
run!(mpm, mod)
run!(pb, mod)
end
else
@dispose pm=ModulePassManager() begin
Expand Down Expand Up @@ -165,13 +165,13 @@ function finish_ir!(@nospecialize(job::CompilerJob{MetalCompilerTarget}), mod::L
if changed
# lowering may have introduced additional functions marked `alwaysinline`
if use_newpm
@dispose pb=PassBuilder() mpm=NewPMModulePassManager(pb) begin
add!(mpm, AlwaysInlinerPass())
add!(mpm, NewPMFunctionPassManager) do fpm
@dispose pb=NewPMPassBuilder() begin
add!(pb, AlwaysInlinerPass())
add!(pb, NewPMFunctionPassManager()) do fpm
add!(fpm, SimplifyCFGPass())
add!(fpm, InstCombinePass())
end
run!(mpm, mod)
run!(pb, mod)
end
else
@dispose pm=ModulePassManager() begin
Expand Down Expand Up @@ -306,15 +306,13 @@ function add_address_spaces!(@nospecialize(job::CompilerJob), mod::LLVM.Module,

# clean-up after this pass (which runs after optimization)
if use_newpm
@dispose pb=PassBuilder() mpm=NewPMModulePassManager(pb) begin
add!(mpm, NewPMFunctionPassManager) do fpm
add!(fpm, SimplifyCFGPass())
add!(fpm, SROAPass())
add!(fpm, EarlyCSEPass())
add!(fpm, InstCombinePass())
end
@dispose pb=NewPMPassBuilder() begin
add!(pb, SimplifyCFGPass())
add!(pb, SROAPass())
add!(pb, EarlyCSEPass())
add!(pb, InstCombinePass())

run!(mpm, mod)
run!(pb, mod)
end
else
@dispose pm=ModulePassManager() begin
Expand Down
Loading
Loading