Skip to content

Commit b98e2f3

Browse files
committed
Split optimization pipeline into multiple sections
1 parent e0c0d29 commit b98e2f3

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

src/compiler/optimize.jl

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ function optimize!(mod::LLVM.Module, tm::LLVM.TargetMachine)
2929
@dispose pb = NewPMPassBuilder() begin
3030
registerEnzymeAndPassPipeline!(pb)
3131
register!(pb, Addr13NoAliasPass())
32-
register!(pb, RewriteGenericMemoryPass())
3332
add!(pb, NewPMAAManager()) do aam
3433
add!(aam, ScopedNoAliasAA())
3534
add!(aam, TypeBasedAA())
@@ -51,13 +50,38 @@ function optimize!(mod::LLVM.Module, tm::LLVM.TargetMachine)
5150
add!(mpm, AlwaysInlinerPass())
5251
add!(mpm, NewPMFunctionPassManager()) do fpm
5352
add!(fpm, AllocOptPass())
54-
end
53+
end
54+
end
55+
run!(pb, mod, tm)
56+
end
5557

58+
# Globalopt is separated as it can delete functions, which invalidates the Julia hardcoded pointers to
59+
# known functions
60+
@dispose pb = NewPMPassBuilder() begin
61+
add!(pb, NewPMAAManager()) do aam
62+
add!(aam, ScopedNoAliasAA())
63+
add!(aam, TypeBasedAA())
64+
add!(aam, BasicAA())
65+
end
66+
add!(pb, NewPMModulePassManager()) do mpm
5667
add!(mpm, GlobalOptPass())
5768
add!(mpm, NewPMFunctionPassManager()) do fpm
5869
add!(fpm, GVNPass())
5970
end
71+
end
72+
run!(pb, mod, tm)
73+
end
6074

75+
# Note: Enzyme uses to run this part twice
76+
@dispose pb = NewPMPassBuilder() begin
77+
registerEnzymeAndPassPipeline!(pb)
78+
register!(pb, RewriteGenericMemoryPass())
79+
add!(pb, NewPMAAManager()) do aam
80+
add!(aam, ScopedNoAliasAA())
81+
add!(aam, TypeBasedAA())
82+
add!(aam, BasicAA())
83+
end
84+
add!(pb, NewPMModulePassManager()) do mpm
6185
add!(mpm, RewriteGenericMemoryPass())
6286

6387
add!(mpm, NewPMFunctionPassManager()) do fpm
@@ -74,6 +98,11 @@ function optimize!(mod::LLVM.Module, tm::LLVM.TargetMachine)
7498
add!(fpm, ReassociatePass())
7599
add!(fpm, EarlyCSEPass())
76100
add!(fpm, AllocOptPass())
101+
102+
add!(fpm, InstCombinePass())
103+
add!(fpm, JLInstSimplifyPass())
104+
add!(fpm, JumpThreadingPass())
105+
77106
add!(fpm, NewPMLoopPassManager(use_memory_ssa=true)) do lpm
78107
add!(lpm, LoopIdiomRecognizePass())
79108
add!(lpm, LoopRotatePass())
@@ -89,7 +118,7 @@ function optimize!(mod::LLVM.Module, tm::LLVM.TargetMachine)
89118
add!(lpm, IndVarSimplifyPass())
90119
add!(lpm, LoopDeletionPass())
91120
end
92-
add!(fpm, LoopUnrollPass(opt_level=2))
121+
add!(fpm, LoopUnrollPass(opt_level=2)) # what opt level?
93122
add!(fpm, AllocOptPass())
94123
add!(fpm, SROAPass())
95124
add!(fpm, GVNPass())
@@ -125,20 +154,29 @@ function optimize!(mod::LLVM.Module, tm::LLVM.TargetMachine)
125154
add!(fpm, InstCombinePass())
126155
add!(fpm, JLInstSimplifyPass())
127156
end
157+
end
158+
run!(pb, mod, tm)
159+
end
128160

161+
# Globalopt is separated as it can delete functions, which invalidates the Julia hardcoded pointers to
162+
# known functions
163+
@dispose pb = NewPMPassBuilder() begin
164+
add!(pb, NewPMAAManager()) do aam
165+
add!(aam, ScopedNoAliasAA())
166+
add!(aam, TypeBasedAA())
167+
add!(aam, BasicAA())
168+
end
169+
add!(pb, NewPMModulePassManager()) do mpm
129170
add!(mpm, GlobalOptPass())
130171
add!(mpm, NewPMFunctionPassManager()) do fpm
131172
add!(fpm, GVNPass())
132173
end
133174
end
134-
135175
run!(pb, mod, tm)
136-
137-
# TODO: Turn into passes?
138-
removeDeadArgs!(mod, tm)
139-
detect_writeonly!(mod)
140-
nodecayed_phis!(mod)
141176
end
177+
removeDeadArgs!(mod, tm)
178+
detect_writeonly!(mod)
179+
nodecayed_phis!(mod)
142180
end
143181

144182
function addOptimizationPasses!(mpm::LLVM.NewPMPassManager)

0 commit comments

Comments
 (0)