@@ -282,6 +282,14 @@ const __llvm_initialized = Ref(false)
282282 erase! (call)
283283 end
284284 end
285+
286+ # minimal optimization to convert the inttoptr/call into a direct call
287+ @dispose pb= NewPMPassBuilder () begin
288+ add! (pb, NewPMFunctionPassManager ()) do fpm
289+ add! (fpm, InstCombinePass ())
290+ end
291+ run! (pb, ir, llvm_machine (job. config. target))
292+ end
285293 end
286294
287295 # all deferred compilations should have been resolved
@@ -312,10 +320,15 @@ const __llvm_initialized = Ref(false)
312320 end
313321
314322 @tracepoint " IR post-processing" begin
315- # mark everything internal except for entrypoints and any exported
316- # global variables. this makes sure that the optimizer can, e.g.,
317- # rewrite function signatures.
323+ # mark the kernel entry-point functions (optimization may need it)
324+ if job. config. kernel
325+ mark_kernel! (entry)
326+ end
327+
318328 if job. config. toplevel
329+ # mark everything internal except for entrypoints and any exported
330+ # global variables. this makes sure that the optimizer can, e.g.,
331+ # rewrite function signatures.
319332 preserved_gvs = collect (values (jobs))
320333 for gvar in globals (ir)
321334 if linkage (gvar) == LLVM. API. LLVMExternalLinkage
@@ -331,64 +344,55 @@ const __llvm_initialized = Ref(false)
331344 run! (pm, ir)
332345 end
333346 end
334- end
335-
336- # mark the kernel entry-point functions (optimization may need it)
337- if job. config. kernel
338- push! (metadata (ir)[" julia.kernel" ], MDNode ([entry]))
339-
340- # IDEA: save all jobs, not only kernels, and save other attributes
341- # so that we can reconstruct the CompileJob instead of setting it globally
342- end
343347
344- if job. config. toplevel && job. config. optimize
345- @tracepoint " optimization" begin
346- optimize! (job, ir; job. config. opt_level)
348+ finish_linked_module! (job, ir)
349+
350+ if job. config. optimize
351+ @tracepoint " optimization" begin
352+ optimize! (job, ir; job. config. opt_level)
353+
354+ # deferred codegen has some special optimization requirements,
355+ # which also need to happen _after_ regular optimization.
356+ # XXX : make these part of the optimizer pipeline?
357+ if has_deferred_jobs
358+ @dispose pb= NewPMPassBuilder () begin
359+ add! (pb, NewPMFunctionPassManager ()) do fpm
360+ add! (fpm, InstCombinePass ())
361+ end
362+ add! (pb, AlwaysInlinerPass ())
363+ add! (pb, NewPMFunctionPassManager ()) do fpm
364+ add! (fpm, SROAPass ())
365+ add! (fpm, GVNPass ())
366+ end
367+ add! (pb, MergeFunctionsPass ())
368+ run! (pb, ir, llvm_machine (job. config. target))
369+ end
370+ end
371+ end
372+ end
347373
348- # deferred codegen has some special optimization requirements,
349- # which also need to happen _after_ regular optimization.
350- # XXX : make these part of the optimizer pipeline?
351- if has_deferred_jobs
374+ if job. config. cleanup
375+ @tracepoint " clean-up" begin
352376 @dispose pb= NewPMPassBuilder () begin
353- add! (pb, NewPMFunctionPassManager ()) do fpm
354- add! (fpm, InstCombinePass ())
355- end
356- add! (pb, AlwaysInlinerPass ())
357- add! (pb, NewPMFunctionPassManager ()) do fpm
358- add! (fpm, SROAPass ())
359- add! (fpm, GVNPass ())
360- end
361- add! (pb, MergeFunctionsPass ())
377+ add! (pb, RecomputeGlobalsAAPass ())
378+ add! (pb, GlobalOptPass ())
379+ add! (pb, GlobalDCEPass ())
380+ add! (pb, StripDeadPrototypesPass ())
381+ add! (pb, ConstantMergePass ())
362382 run! (pb, ir, llvm_machine (job. config. target))
363383 end
364384 end
365385 end
366386
367387 # optimization may have replaced functions, so look the entry point up again
368388 entry = functions (ir)[entry_fn]
369- end
370389
371- if job. config. toplevel && job. config. cleanup
372- @tracepoint " clean-up" begin
373- @dispose pb= NewPMPassBuilder () begin
374- add! (pb, RecomputeGlobalsAAPass ())
375- add! (pb, GlobalOptPass ())
376- add! (pb, GlobalDCEPass ())
377- add! (pb, StripDeadPrototypesPass ())
378- add! (pb, ConstantMergePass ())
379- run! (pb, ir, llvm_machine (job. config. target))
380- end
381- end
382- end
383-
384- # finish the module
385- #
386- # we want to finish the module after optimization, so we cannot do so
387- # during deferred code generation. instead, process the deferred jobs
388- # here.
389- if job. config. toplevel
390+ # finish the module
391+ #
392+ # we want to finish the module after optimization, so we cannot do so
393+ # during deferred code generation. instead, process the deferred jobs
394+ # here.
390395 entry = finish_ir! (job, ir, entry)
391-
392396 for (job′, fn′) in jobs
393397 job′ == job && continue
394398 finish_ir! (job′, ir, functions (ir)[fn′])
@@ -409,7 +413,7 @@ const __llvm_initialized = Ref(false)
409413 end
410414
411415 if job. config. toplevel && job. config. validate
412- @tracepoint " Validation " begin
416+ @tracepoint " validation " begin
413417 check_ir (job, ir)
414418 end
415419 end
0 commit comments