Skip to content

Commit c92ec6d

Browse files
Use NewPM wherever possible (#482)
Co-authored-by: Tim Besard <[email protected]>
1 parent 06e4288 commit c92ec6d

File tree

12 files changed

+593
-224
lines changed

12 files changed

+593
-224
lines changed

Manifest.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
julia_version = "1.8.5"
44
manifest_format = "2.0"
5-
project_hash = "27e7a553e90a68d1f177019877f2e4b06a23c81a"
5+
project_hash = "7fba634d9208dc361f8caa397a5e8d2621e56bcf"
66

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

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

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

6161
[[deps.LazyArtifacts]]
6262
deps = ["Artifacts", "Pkg"]

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
1515

1616
[compat]
1717
ExprTools = "0.1"
18-
LLVM = "6"
18+
LLVM = "6.2"
1919
Scratch = "1"
2020
TimerOutputs = "0.5"
2121
julia = "1.8"

src/GPUCompiler.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ using Scratch: @get_scratch!
1414
const CC = Core.Compiler
1515
using Core: MethodInstance, CodeInstance, CodeInfo
1616

17+
const use_newpm = LLVM.has_newpm()
18+
1719
include("utils.jl")
1820

1921
# compiler interface and implementations

src/driver.jl

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ const __llvm_initialized = Ref(false)
313313
# global variables. this makes sure that the optimizer can, e.g.,
314314
# rewrite function signatures.
315315
if toplevel
316+
# TODO: there's no good API to use internalize with the new pass manager yet
316317
@dispose pm=ModulePassManager() begin
317318
exports = collect(values(jobs))
318319
for gvar in globals(ir)
@@ -340,20 +341,37 @@ const __llvm_initialized = Ref(false)
340341
# deferred codegen has some special optimization requirements,
341342
# which also need to happen _after_ regular optimization.
342343
# XXX: make these part of the optimizer pipeline?
343-
has_deferred_jobs && @dispose pm=ModulePassManager() begin
344-
# inline and optimize the call to e deferred code. in particular we want
345-
# to remove unnecessary alloca's created by pass-by-ref semantics.
346-
instruction_combining!(pm)
347-
always_inliner!(pm)
348-
scalar_repl_aggregates_ssa!(pm)
349-
promote_memory_to_register!(pm)
350-
gvn!(pm)
351-
352-
# merge duplicate functions, since each compilation invocation emits everything
353-
# XXX: ideally we want to avoid emitting these in the first place
354-
merge_functions!(pm)
355-
356-
run!(pm, ir)
344+
if has_deferred_jobs
345+
if use_newpm
346+
@dispose pb=PassBuilder() mpm=NewPMModulePassManager(pb) begin
347+
add!(mpm, NewPMFunctionPassManager) do fpm
348+
add!(fpm, InstCombinePass())
349+
end
350+
add!(mpm, AlwaysInlinerPass())
351+
add!(mpm, NewPMFunctionPassManager) do fpm
352+
add!(fpm, SROAPass())
353+
add!(fpm, GVNPass())
354+
end
355+
add!(mpm, MergeFunctionsPass())
356+
run!(mpm, ir)
357+
end
358+
else
359+
@dispose pm=ModulePassManager() begin
360+
# inline and optimize the call to e deferred code. in particular we want
361+
# to remove unnecessary alloca's created by pass-by-ref semantics.
362+
instruction_combining!(pm)
363+
always_inliner!(pm)
364+
scalar_repl_aggregates_ssa!(pm)
365+
promote_memory_to_register!(pm)
366+
gvn!(pm)
367+
368+
# merge duplicate functions, since each compilation invocation emits everything
369+
# XXX: ideally we want to avoid emitting these in the first place
370+
merge_functions!(pm)
371+
372+
run!(pm, ir)
373+
end
374+
end
357375
end
358376
end
359377

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

364382
if cleanup
365383
@timeit_debug to "clean-up" begin
366-
# we can only clean-up now, as optimization may lower or introduce calls to
367-
# functions from the GPU runtime (e.g. julia.gc_alloc_obj -> gpu_gc_pool_alloc)
368-
@dispose pm=ModulePassManager() begin
369-
# eliminate all unused internal functions
370-
global_optimizer!(pm)
371-
global_dce!(pm)
372-
strip_dead_prototypes!(pm)
373-
374-
# merge constants (such as exception messages)
375-
constant_merge!(pm)
376-
377-
run!(pm, ir)
384+
if use_newpm
385+
@dispose pb=PassBuilder() mpm=NewPMModulePassManager(pb) begin
386+
add!(mpm, RecomputeGlobalsAAPass())
387+
add!(mpm, GlobalOptPass())
388+
add!(mpm, GlobalDCEPass())
389+
add!(mpm, StripDeadPrototypesPass())
390+
add!(mpm, ConstantMergePass())
391+
run!(mpm, ir)
392+
end
393+
else
394+
# we can only clean-up now, as optimization may lower or introduce calls to
395+
# functions from the GPU runtime (e.g. julia.gc_alloc_obj -> gpu_gc_pool_alloc)
396+
@dispose pm=ModulePassManager() begin
397+
# eliminate all unused internal functions
398+
global_optimizer!(pm)
399+
global_dce!(pm)
400+
strip_dead_prototypes!(pm)
401+
402+
# merge constants (such as exception messages)
403+
constant_merge!(pm)
404+
405+
run!(pm, ir)
406+
end
378407
end
379408
end
380409
end

src/gcn.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ isintrinsic(::CompilerJob{GCNCompilerTarget}, fn::String) = in(fn, gcn_intrinsic
3737

3838
function finish_module!(@nospecialize(job::CompilerJob{GCNCompilerTarget}),
3939
mod::LLVM.Module, entry::LLVM.Function)
40-
@dispose pm=ModulePassManager() begin
41-
add!(pm, ModulePass("LowerThrowExtra", lower_throw_extra!))
42-
run!(pm, mod)
43-
end
40+
lower_throw_extra!(mod)
4441

4542
if job.config.kernel
4643
# calling convention

src/irgen.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ function irgen(@nospecialize(job::CompilerJob))
9696
end
9797
end
9898

99+
# TODO: there's no good API to use internalize with the new pass manager yet
99100
@dispose pm=ModulePassManager() begin
100101
global current_job
101102
current_job = job

src/mcgen.jl

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,30 @@
33
# final preparations for the module to be compiled to machine code
44
# these passes should not be run when e.g. compiling to write to disk.
55
function prepare_execution!(@nospecialize(job::CompilerJob), mod::LLVM.Module)
6-
@dispose pm=ModulePassManager() begin
7-
global current_job
8-
current_job = job
6+
global current_job
7+
current_job = job
98

10-
global_optimizer!(pm)
9+
if use_newpm
10+
@dispose pb=PassBuilder() mpm=NewPMModulePassManager(pb) begin
11+
add!(mpm, RecomputeGlobalsAAPass())
12+
add!(mpm, GlobalOptPass())
13+
resolve_cpu_references!(mod)
14+
add!(legacy2newpm(resolve_cpu_references!), mpm)
15+
add!(mpm, GlobalDCEPass())
16+
add!(mpm, StripDeadPrototypesPass())
17+
run!(mpm, mod)
18+
end
19+
else
20+
@dispose pm=ModulePassManager() begin
21+
global_optimizer!(pm)
1122

12-
add!(pm, ModulePass("ResolveCPUReferences", resolve_cpu_references!))
23+
add!(pm, ModulePass("ResolveCPUReferences", resolve_cpu_references!))
1324

14-
global_dce!(pm)
15-
strip_dead_prototypes!(pm)
25+
global_dce!(pm)
26+
strip_dead_prototypes!(pm)
1627

17-
run!(pm, mod)
28+
run!(pm, mod)
29+
end
1830
end
1931

2032
return

src/metal.jl

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,19 @@ function finish_module!(@nospecialize(job::CompilerJob{MetalCompilerTarget}), mo
8787
# add metadata to AIR intrinsics LLVM doesn't know about
8888
annotate_air_intrinsics!(job, mod)
8989

90-
@dispose pm=ModulePassManager() begin
91-
# we emit properties (of the device and ptx isa) as private global constants,
92-
# so run the optimizer so that they are inlined before the rest of the optimizer runs.
93-
global_optimizer!(pm)
90+
# we emit properties (of the air and metal version) as private global constants,
91+
# so run the optimizer so that they are inlined before the rest of the optimizer runs.
92+
if use_newpm
93+
@dispose pb=PassBuilder() mpm=NewPMModulePassManager(pb) begin
94+
add!(mpm, RecomputeGlobalsAAPass())
95+
add!(mpm, GlobalOptPass())
96+
run!(mpm, mod)
97+
end
98+
else
99+
@dispose pm=ModulePassManager() begin
100+
global_optimizer!(pm)
101+
run!(pm, mod)
102+
end
94103
end
95104

96105
return functions(mod)[entry_fn]
@@ -121,11 +130,22 @@ function finish_ir!(@nospecialize(job::CompilerJob{MetalCompilerTarget}), mod::L
121130
end
122131
if changed
123132
# lowering may have introduced additional functions marked `alwaysinline`
124-
@dispose pm=ModulePassManager() begin
125-
always_inliner!(pm)
126-
cfgsimplification!(pm)
127-
instruction_combining!(pm)
128-
run!(pm, mod)
133+
if use_newpm
134+
@dispose pb=PassBuilder() mpm=NewPMModulePassManager(pb) begin
135+
add!(mpm, AlwaysInlinerPass())
136+
add!(mpm, NewPMFunctionPassManager) do fpm
137+
add!(fpm, SimplifyCFGPass())
138+
add!(fpm, InstCombinePass())
139+
end
140+
run!(mpm, mod)
141+
end
142+
else
143+
@dispose pm=ModulePassManager() begin
144+
always_inliner!(pm)
145+
cfgsimplification!(pm)
146+
instruction_combining!(pm)
147+
run!(pm, mod)
148+
end
129149
end
130150
end
131151

@@ -158,11 +178,22 @@ end
158178
end
159179

160180
if any_noreturn
161-
@dispose pm=ModulePassManager() begin
162-
always_inliner!(pm)
163-
cfgsimplification!(pm)
164-
instruction_combining!(pm)
165-
run!(pm, mod)
181+
if use_newpm
182+
@dispose pb=PassBuilder() mpm=NewPMModulePassManager(pb) begin
183+
add!(mpm, AlwaysInlinerPass())
184+
add!(mpm, NewPMFunctionPassManager) do fpm
185+
add!(fpm, SimplifyCFGPass())
186+
add!(fpm, InstCombinePass())
187+
end
188+
run!(mpm, mod)
189+
end
190+
else
191+
@dispose pm=ModulePassManager() begin
192+
always_inliner!(pm)
193+
cfgsimplification!(pm)
194+
instruction_combining!(pm)
195+
run!(pm, mod)
196+
end
166197
end
167198
end
168199
end
@@ -294,13 +325,26 @@ function add_address_spaces!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
294325
LLVM.name!(new_f, fn)
295326

296327
# clean-up after this pass (which runs after optimization)
297-
@dispose pm=ModulePassManager() begin
298-
cfgsimplification!(pm)
299-
scalar_repl_aggregates!(pm)
300-
early_cse!(pm)
301-
instruction_combining!(pm)
328+
if use_newpm
329+
@dispose pb=PassBuilder() mpm=NewPMModulePassManager(pb) begin
330+
add!(mpm, NewPMFunctionPassManager) do fpm
331+
add!(fpm, SimplifyCFGPass())
332+
add!(fpm, SROAPass())
333+
add!(fpm, EarlyCSEPass())
334+
add!(fpm, InstCombinePass())
335+
end
302336

303-
run!(pm, mod)
337+
run!(mpm, mod)
338+
end
339+
else
340+
@dispose pm=ModulePassManager() begin
341+
cfgsimplification!(pm)
342+
scalar_repl_aggregates!(pm)
343+
early_cse!(pm)
344+
instruction_combining!(pm)
345+
346+
run!(pm, mod)
347+
end
304348
end
305349

306350
return new_f

0 commit comments

Comments
 (0)