@@ -64,6 +64,7 @@ function optimize!(mod::LLVM.Module, tm::LLVM.TargetMachine)
6464 add! (aam, BasicAA ())
6565 end
6666 add! (pb, NewPMModulePassManager ()) do mpm
67+ add! (mpm, CPUFeaturesPass ()) # why is this duplicated?
6768 add! (mpm, GlobalOptPass ())
6869 add! (mpm, NewPMFunctionPassManager ()) do fpm
6970 add! (fpm, GVNPass ())
@@ -72,7 +73,7 @@ function optimize!(mod::LLVM.Module, tm::LLVM.TargetMachine)
7273 run! (pb, mod, tm)
7374 end
7475
75- # Note: Enzyme uses to run this part twice
76+ function middle_optimize! (second_stage = false )
7677 @dispose pb = NewPMPassBuilder () begin
7778 registerEnzymeAndPassPipeline! (pb)
7879 register! (pb, RewriteGenericMemoryPass ())
@@ -83,6 +84,7 @@ function optimize!(mod::LLVM.Module, tm::LLVM.TargetMachine)
8384 end
8485 add! (pb, NewPMModulePassManager ()) do mpm
8586 add! (mpm, RewriteGenericMemoryPass ())
87+ add! (mpm, CPUFeaturesPass ()) # why is this duplicated?
8688
8789 add! (mpm, NewPMFunctionPassManager ()) do fpm
8890 add! (fpm, InstCombinePass ())
@@ -99,10 +101,6 @@ function optimize!(mod::LLVM.Module, tm::LLVM.TargetMachine)
99101 add! (fpm, EarlyCSEPass ())
100102 add! (fpm, AllocOptPass ())
101103
102- add! (fpm, InstCombinePass ())
103- add! (fpm, JLInstSimplifyPass ())
104- add! (fpm, JumpThreadingPass ())
105-
106104 add! (fpm, NewPMLoopPassManager (use_memory_ssa= true )) do lpm
107105 add! (lpm, LoopIdiomRecognizePass ())
108106 add! (lpm, LoopRotatePass ())
@@ -143,6 +141,7 @@ function optimize!(mod::LLVM.Module, tm::LLVM.TargetMachine)
143141 end
144142 add! (fpm, JumpThreadingPass ())
145143 add! (fpm, CorrelatedValuePropagationPass ())
144+ if second_stage
146145
147146 add! (fpm, ADCEPass ())
148147 add! (fpm, InstCombinePass ())
@@ -153,10 +152,15 @@ function optimize!(mod::LLVM.Module, tm::LLVM.TargetMachine)
153152 add! (fpm, SimplifyCFGPass ())
154153 add! (fpm, InstCombinePass ())
155154 add! (fpm, JLInstSimplifyPass ())
155+ end # second_stage
156156 end
157157 end
158158 run! (pb, mod, tm)
159159 end
160+ end # middle_optimize!
161+
162+ middle_optimize! ()
163+ middle_optimize! (true )
160164
161165 # Globalopt is separated as it can delete functions, which invalidates the Julia hardcoded pointers to
162166 # known functions
0 commit comments