Skip to content

Commit 99b7937

Browse files
authored
Add a cleanup flag to disable global opt. (#349)
1 parent c687cae commit 99b7937

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

src/driver.jl

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The following keyword arguments are supported:
2323
- `libraries`: link the GPU runtime and `libdevice` libraries (if required)
2424
- `deferred_codegen`: resolve deferred compiler invocations (if required)
2525
- `optimize`: optimize the code (default: true)
26+
- `cleanup`: run cleanup passes on the code (default: true)
2627
- `strip`: strip non-functional metadata and debug information (default: false)
2728
- `validate`: validate the generated IR before emitting machine code (default: true)
2829
- `only_entry`: only keep the entry function, remove all others (default: false).
@@ -32,14 +33,15 @@ Other keyword arguments can be found in the documentation of [`cufunction`](@ref
3233
"""
3334
function compile(target::Symbol, @nospecialize(job::CompilerJob);
3435
libraries::Bool=true, deferred_codegen::Bool=true,
35-
optimize::Bool=true, strip::Bool=false, validate::Bool=true,
36-
only_entry::Bool=false, ctx::Union{JuliaContextType,Nothing}=nothing)
36+
optimize::Bool=true, cleanup::Bool=true, strip::Bool=false,
37+
validate::Bool=true, only_entry::Bool=false,
38+
ctx::Union{JuliaContextType,Nothing}=nothing)
3739
if compile_hook[] !== nothing
3840
compile_hook[](job)
3941
end
4042

4143
return codegen(target, job;
42-
libraries, deferred_codegen, optimize, strip, validate, only_entry, ctx)
44+
libraries, deferred_codegen, optimize, cleanup, strip, validate, only_entry, ctx)
4345
end
4446

4547
# transitionary feature to deal versions of Julia that rely on a global context
@@ -83,8 +85,8 @@ unwrap_context(ctx::Context) = ctx
8385

8486
function codegen(output::Symbol, @nospecialize(job::CompilerJob);
8587
libraries::Bool=true, deferred_codegen::Bool=true, optimize::Bool=true,
86-
strip::Bool=false, validate::Bool=true, only_entry::Bool=false,
87-
parent_job::Union{Nothing, CompilerJob}=nothing,
88+
cleanup::Bool=true, strip::Bool=false, validate::Bool=true,
89+
only_entry::Bool=false, parent_job::Union{Nothing, CompilerJob}=nothing,
8890
ctx::Union{JuliaContextType,Nothing}=nothing)
8991
## Julia IR
9092

@@ -110,7 +112,7 @@ function codegen(output::Symbol, @nospecialize(job::CompilerJob);
110112
Use a JuliaContext instead.""")
111113
end
112114

113-
ir, ir_meta = emit_llvm(job, mi; libraries, deferred_codegen, optimize, only_entry, ctx)
115+
ir, ir_meta = emit_llvm(job, mi; libraries, deferred_codegen, optimize, cleanup, only_entry, ctx)
114116

115117
if output == :llvm
116118
if strip
@@ -199,7 +201,7 @@ const __llvm_initialized = Ref(false)
199201

200202
@locked function emit_llvm(@nospecialize(job::CompilerJob), @nospecialize(method_instance);
201203
libraries::Bool=true, deferred_codegen::Bool=true, optimize::Bool=true,
202-
only_entry::Bool=false, ctx::JuliaContextType)
204+
cleanup::Bool=true, only_entry::Bool=false, ctx::JuliaContextType)
203205
if !__llvm_initialized[]
204206
InitializeAllTargets()
205207
InitializeAllTargetInfos()
@@ -319,7 +321,7 @@ const __llvm_initialized = Ref(false)
319321
@compiler_assert isempty(uses(dyn_marker)) job
320322
unsafe_delete!(ir, dyn_marker)
321323
end
322-
324+
323325
@timeit_debug to "IR post-processing" begin
324326
# mark the kernel entry-point functions (optimization may need it)
325327
if job.source.kernel
@@ -332,7 +334,7 @@ const __llvm_initialized = Ref(false)
332334
if optimize
333335
@timeit_debug to "optimization" begin
334336
optimize!(job, ir)
335-
337+
336338
# deferred codegen has some special optimization requirements,
337339
# which also need to happen _after_ regular optimization.
338340
# XXX: make these part of the optimizer pipeline?
@@ -357,22 +359,24 @@ const __llvm_initialized = Ref(false)
357359
entry = functions(ir)[entry_fn]
358360
end
359361

360-
@timeit_debug to "clean-up" begin
361-
# we can only clean-up now, as optimization may lower or introduce calls to
362-
# functions from the GPU runtime (e.g. julia.gc_alloc_obj -> gpu_gc_pool_alloc)
363-
@dispose pm=ModulePassManager() begin
364-
# eliminate all unused internal functions
365-
global_optimizer!(pm)
366-
global_dce!(pm)
367-
strip_dead_prototypes!(pm)
362+
if cleanup
363+
@timeit_debug to "clean-up" begin
364+
# we can only clean-up now, as optimization may lower or introduce calls to
365+
# functions from the GPU runtime (e.g. julia.gc_alloc_obj -> gpu_gc_pool_alloc)
366+
@dispose pm=ModulePassManager() begin
367+
# eliminate all unused internal functions
368+
global_optimizer!(pm)
369+
global_dce!(pm)
370+
strip_dead_prototypes!(pm)
368371

369-
# merge constants (such as exception messages)
370-
constant_merge!(pm)
372+
# merge constants (such as exception messages)
373+
constant_merge!(pm)
371374

372-
run!(pm, ir)
375+
run!(pm, ir)
376+
end
373377
end
374378
end
375-
379+
376380
# finish the module
377381
#
378382
# we want to finish the module after optimization, so we cannot do so during

src/precompile.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ function _precompile_()
5959
#@assert precompile(Tuple{typeof(GPUCompiler.split_kwargs),Tuple{},Vector{Symbol},Vararg{Vector{Symbol}, N} where N})
6060
let fbody = try __lookup_kwbody__(which(GPUCompiler.compile, (Symbol,GPUCompiler.CompilerJob,))) catch missing end
6161
if !ismissing(fbody)
62-
@assert precompile(fbody, (Bool,Bool,Bool,Bool,Bool,Bool,JuliaContextType,typeof(GPUCompiler.compile),Symbol,GPUCompiler.CompilerJob,))
63-
@assert precompile(fbody, (Bool,Bool,Bool,Bool,Bool,Bool,Nothing,typeof(GPUCompiler.compile),Symbol,GPUCompiler.CompilerJob,))
62+
@assert precompile(fbody, (Bool,Bool,Bool,Bool,Bool,Bool,Bool,JuliaContextType,typeof(GPUCompiler.compile),Symbol,GPUCompiler.CompilerJob,))
63+
@assert precompile(fbody, (Bool,Bool,Bool,Bool,Bool,Bool,Bool,Nothing,typeof(GPUCompiler.compile),Symbol,GPUCompiler.CompilerJob,))
6464
end
6565
end
6666
end

0 commit comments

Comments
 (0)