Skip to content

Commit

Permalink
Use NewPM wherever possible (#482)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Besard <tim.besard@gmail.com>
  • Loading branch information
pchintalapudi and maleadt authored Sep 6, 2023
1 parent 06e4288 commit c92ec6d
Show file tree
Hide file tree
Showing 12 changed files with 593 additions and 224 deletions.
10 changes: 5 additions & 5 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

julia_version = "1.8.5"
manifest_format = "2.0"
project_hash = "27e7a553e90a68d1f177019877f2e4b06a23c81a"
project_hash = "7fba634d9208dc361f8caa397a5e8d2621e56bcf"

[[deps.ArgTools]]
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"
Expand Down Expand Up @@ -48,15 +48,15 @@ version = "1.5.0"

[[deps.LLVM]]
deps = ["CEnum", "LLVMExtra_jll", "Libdl", "Printf", "Unicode"]
git-tree-sha1 = "8695a49bfe05a2dc0feeefd06b4ca6361a018729"
git-tree-sha1 = "f7e39b1ecd9531475bbf3b25363027ba14c3e563"
uuid = "929cbde3-209d-540e-8aea-75f648917ca0"
version = "6.1.0"
version = "6.2.0"

[[deps.LLVMExtra_jll]]
deps = ["Artifacts", "JLLWrappers", "LazyArtifacts", "Libdl", "TOML"]
git-tree-sha1 = "c35203c1e1002747da220ffc3c0762ce7754b08c"
git-tree-sha1 = "7ca6850ae880cc99b59b88517545f91a52020afa"
uuid = "dad2f222-ce93-54a1-a47d-0025e8a3acab"
version = "0.0.23+0"
version = "0.0.25+0"

[[deps.LazyArtifacts]]
deps = ["Artifacts", "Pkg"]
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[compat]
ExprTools = "0.1"
LLVM = "6"
LLVM = "6.2"
Scratch = "1"
TimerOutputs = "0.5"
julia = "1.8"
2 changes: 2 additions & 0 deletions src/GPUCompiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ using Scratch: @get_scratch!
const CC = Core.Compiler
using Core: MethodInstance, CodeInstance, CodeInfo

const use_newpm = LLVM.has_newpm()

include("utils.jl")

# compiler interface and implementations
Expand Down
81 changes: 55 additions & 26 deletions src/driver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ const __llvm_initialized = Ref(false)
# global variables. this makes sure that the optimizer can, e.g.,
# rewrite function signatures.
if toplevel
# TODO: there's no good API to use internalize with the new pass manager yet
@dispose pm=ModulePassManager() begin
exports = collect(values(jobs))
for gvar in globals(ir)
Expand Down Expand Up @@ -340,20 +341,37 @@ const __llvm_initialized = Ref(false)
# deferred codegen has some special optimization requirements,
# which also need to happen _after_ regular optimization.
# XXX: make these part of the optimizer pipeline?
has_deferred_jobs && @dispose pm=ModulePassManager() begin
# inline and optimize the call to e deferred code. in particular we want
# to remove unnecessary alloca's created by pass-by-ref semantics.
instruction_combining!(pm)
always_inliner!(pm)
scalar_repl_aggregates_ssa!(pm)
promote_memory_to_register!(pm)
gvn!(pm)

# merge duplicate functions, since each compilation invocation emits everything
# XXX: ideally we want to avoid emitting these in the first place
merge_functions!(pm)

run!(pm, ir)
if has_deferred_jobs
if use_newpm
@dispose pb=PassBuilder() mpm=NewPMModulePassManager(pb) begin
add!(mpm, NewPMFunctionPassManager) do fpm
add!(fpm, InstCombinePass())
end
add!(mpm, AlwaysInlinerPass())
add!(mpm, NewPMFunctionPassManager) do fpm
add!(fpm, SROAPass())
add!(fpm, GVNPass())
end
add!(mpm, MergeFunctionsPass())
run!(mpm, ir)
end
else
@dispose pm=ModulePassManager() begin
# inline and optimize the call to e deferred code. in particular we want
# to remove unnecessary alloca's created by pass-by-ref semantics.
instruction_combining!(pm)
always_inliner!(pm)
scalar_repl_aggregates_ssa!(pm)
promote_memory_to_register!(pm)
gvn!(pm)

# merge duplicate functions, since each compilation invocation emits everything
# XXX: ideally we want to avoid emitting these in the first place
merge_functions!(pm)

run!(pm, ir)
end
end
end
end

Expand All @@ -363,18 +381,29 @@ const __llvm_initialized = Ref(false)

if cleanup
@timeit_debug to "clean-up" begin
# we can only clean-up now, as optimization may lower or introduce calls to
# functions from the GPU runtime (e.g. julia.gc_alloc_obj -> gpu_gc_pool_alloc)
@dispose pm=ModulePassManager() begin
# eliminate all unused internal functions
global_optimizer!(pm)
global_dce!(pm)
strip_dead_prototypes!(pm)

# merge constants (such as exception messages)
constant_merge!(pm)

run!(pm, ir)
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)
end
else
# we can only clean-up now, as optimization may lower or introduce calls to
# functions from the GPU runtime (e.g. julia.gc_alloc_obj -> gpu_gc_pool_alloc)
@dispose pm=ModulePassManager() begin
# eliminate all unused internal functions
global_optimizer!(pm)
global_dce!(pm)
strip_dead_prototypes!(pm)

# merge constants (such as exception messages)
constant_merge!(pm)

run!(pm, ir)
end
end
end
end
Expand Down
5 changes: 1 addition & 4 deletions src/gcn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ isintrinsic(::CompilerJob{GCNCompilerTarget}, fn::String) = in(fn, gcn_intrinsic

function finish_module!(@nospecialize(job::CompilerJob{GCNCompilerTarget}),
mod::LLVM.Module, entry::LLVM.Function)
@dispose pm=ModulePassManager() begin
add!(pm, ModulePass("LowerThrowExtra", lower_throw_extra!))
run!(pm, mod)
end
lower_throw_extra!(mod)

if job.config.kernel
# calling convention
Expand Down
1 change: 1 addition & 0 deletions src/irgen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ function irgen(@nospecialize(job::CompilerJob))
end
end

# TODO: there's no good API to use internalize with the new pass manager yet
@dispose pm=ModulePassManager() begin
global current_job
current_job = job
Expand Down
28 changes: 20 additions & 8 deletions src/mcgen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,30 @@
# final preparations for the module to be compiled to machine code
# these passes should not be run when e.g. compiling to write to disk.
function prepare_execution!(@nospecialize(job::CompilerJob), mod::LLVM.Module)
@dispose pm=ModulePassManager() begin
global current_job
current_job = job
global current_job
current_job = job

global_optimizer!(pm)
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)
end
else
@dispose pm=ModulePassManager() begin
global_optimizer!(pm)

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

global_dce!(pm)
strip_dead_prototypes!(pm)
global_dce!(pm)
strip_dead_prototypes!(pm)

run!(pm, mod)
run!(pm, mod)
end
end

return
Expand Down
84 changes: 64 additions & 20 deletions src/metal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,19 @@ function finish_module!(@nospecialize(job::CompilerJob{MetalCompilerTarget}), mo
# add metadata to AIR intrinsics LLVM doesn't know about
annotate_air_intrinsics!(job, mod)

@dispose pm=ModulePassManager() begin
# we emit properties (of the device and ptx isa) as private global constants,
# so run the optimizer so that they are inlined before the rest of the optimizer runs.
global_optimizer!(pm)
# 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)
end
else
@dispose pm=ModulePassManager() begin
global_optimizer!(pm)
run!(pm, mod)
end
end

return functions(mod)[entry_fn]
Expand Down Expand Up @@ -121,11 +130,22 @@ function finish_ir!(@nospecialize(job::CompilerJob{MetalCompilerTarget}), mod::L
end
if changed
# lowering may have introduced additional functions marked `alwaysinline`
@dispose pm=ModulePassManager() begin
always_inliner!(pm)
cfgsimplification!(pm)
instruction_combining!(pm)
run!(pm, mod)
if use_newpm
@dispose pb=PassBuilder() mpm=NewPMModulePassManager(pb) begin
add!(mpm, AlwaysInlinerPass())
add!(mpm, NewPMFunctionPassManager) do fpm
add!(fpm, SimplifyCFGPass())
add!(fpm, InstCombinePass())
end
run!(mpm, mod)
end
else
@dispose pm=ModulePassManager() begin
always_inliner!(pm)
cfgsimplification!(pm)
instruction_combining!(pm)
run!(pm, mod)
end
end
end

Expand Down Expand Up @@ -158,11 +178,22 @@ end
end

if any_noreturn
@dispose pm=ModulePassManager() begin
always_inliner!(pm)
cfgsimplification!(pm)
instruction_combining!(pm)
run!(pm, mod)
if use_newpm
@dispose pb=PassBuilder() mpm=NewPMModulePassManager(pb) begin
add!(mpm, AlwaysInlinerPass())
add!(mpm, NewPMFunctionPassManager) do fpm
add!(fpm, SimplifyCFGPass())
add!(fpm, InstCombinePass())
end
run!(mpm, mod)
end
else
@dispose pm=ModulePassManager() begin
always_inliner!(pm)
cfgsimplification!(pm)
instruction_combining!(pm)
run!(pm, mod)
end
end
end
end
Expand Down Expand Up @@ -294,13 +325,26 @@ function add_address_spaces!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
LLVM.name!(new_f, fn)

# clean-up after this pass (which runs after optimization)
@dispose pm=ModulePassManager() begin
cfgsimplification!(pm)
scalar_repl_aggregates!(pm)
early_cse!(pm)
instruction_combining!(pm)
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

run!(pm, mod)
run!(mpm, mod)
end
else
@dispose pm=ModulePassManager() begin
cfgsimplification!(pm)
scalar_repl_aggregates!(pm)
early_cse!(pm)
instruction_combining!(pm)

run!(pm, mod)
end
end

return new_f
Expand Down
Loading

0 comments on commit c92ec6d

Please sign in to comment.