Skip to content

Commit c2336d2

Browse files
committed
Perform module clean-up before optimization.
That way we need to optimize less, and the optimizer can rewrite internalized functions.
1 parent fe5cf6e commit c2336d2

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

src/driver.jl

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -244,17 +244,7 @@ const __llvm_initialized = Ref(false)
244244
@timeit_debug to "IR post-processing" begin
245245
entry = finish_module!(job, ir, entry)
246246

247-
if optimize
248-
@timeit_debug to "optimization" optimize!(job, ir)
249-
250-
# optimization may have replaced functions, so look the entry point up again
251-
entry = functions(ir)[entry_fn]
252-
end
253-
254-
if ccall(:jl_is_debugbuild, Cint, ()) == 1
255-
@timeit_debug to "verification" verify(ir)
256-
end
257-
247+
# some early clean-up to reduce the amount of code to optimize
258248
@timeit_debug to "clean-up" begin
259249
# replace non-entry function definitions with a declaration
260250
if only_entry
@@ -266,13 +256,13 @@ const __llvm_initialized = Ref(false)
266256
end
267257
end
268258

269-
# remove everything except for the entry and any exported global variables
270-
exports = String[entry_fn]
271-
for gvar in globals(ir)
272-
push!(exports, LLVM.name(gvar))
273-
end
274-
275259
ModulePassManager() do pm
260+
# mark everything internal except for the entry and exported global variables.
261+
# this makes sure that the optimizer can, e.g., touch function signatures.
262+
exports = String[entry_fn]
263+
for gvar in globals(ir)
264+
push!(exports, LLVM.name(gvar))
265+
end
276266
internalize!(pm, exports)
277267

278268
# eliminate all unused internal functions
@@ -284,8 +274,9 @@ const __llvm_initialized = Ref(false)
284274
constant_merge!(pm)
285275

286276
if do_deferred_codegen
287-
# inline and optimize the call to the deferred code. in particular we want to
288-
# remove unnecessary alloca's that are created by pass-by-ref semantics.
277+
# inline and optimize the call to e deferred code. in particular we want
278+
# to remove unnecessary alloca's created by pass-by-ref semantics.
279+
# TODO: is this still necesary, now that we optimize after codegen?
289280
instruction_combining!(pm)
290281
always_inliner!(pm)
291282
scalar_repl_aggregates_ssa!(pm)
@@ -300,6 +291,17 @@ const __llvm_initialized = Ref(false)
300291
run!(pm, ir)
301292
end
302293
end
294+
295+
if optimize
296+
@timeit_debug to "optimization" optimize!(job, ir)
297+
298+
# optimization may have replaced functions, so look the entry point up again
299+
entry = functions(ir)[entry_fn]
300+
end
301+
302+
if ccall(:jl_is_debugbuild, Cint, ()) == 1
303+
@timeit_debug to "verification" verify(ir)
304+
end
303305
end
304306

305307
return ir, (; entry, compiled)

0 commit comments

Comments
 (0)