Skip to content

Commit 235ec99

Browse files
committed
Introduce parameters to control optimization.
1 parent 5cce9e0 commit 235ec99

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

src/interface.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,6 @@ function process_entry!(@nospecialize(job::CompilerJob), mod::LLVM.Module,
189189
return entry
190190
end
191191

192-
# post-Julia optimization processing of the module
193-
optimize_module!(@nospecialize(job::CompilerJob), mod::LLVM.Module) = return
194-
195192
# final processing of the IR module, right before validation and machine-code generation
196193
finish_module!(@nospecialize(job::CompilerJob), mod::LLVM.Module) = return
197194

@@ -219,3 +216,7 @@ function llvm_debug_info(@nospecialize(job::CompilerJob))
219216
LLVM.API.LLVMDebugEmissionKindFullDebug
220217
end
221218
end
219+
220+
# optimization
221+
optimization_params(@nospecialize(job::CompilerJob)) = GPUOptimizationParams()
222+
optimize_module!(@nospecialize(job::CompilerJob), mod::LLVM.Module) = return

src/optim.jl

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,48 @@
11
# LLVM IR optimization
22

3+
Base.@kwdef struct GPUOptimizationParams
4+
julia::Bool = true
5+
intrinsics::Bool = true
6+
ipo::Bool = true
7+
8+
optlevel::Int = Base.JLOptions().opt_level
9+
end
10+
311
function optimize!(@nospecialize(job::CompilerJob), mod::LLVM.Module)
12+
triple = llvm_triple(job.target)
413
tm = llvm_machine(job.target)
514

615
function initialize!(pm)
7-
add_library_info!(pm, triple(mod))
16+
add_library_info!(pm, triple)
817
add_transform_info!(pm, tm)
918
end
1019

1120
global current_job
1221
current_job = job
1322

23+
params = optimization_params(job)
24+
1425
# Julia-specific optimizations
1526
#
1627
# NOTE: we need to use multiple distinct pass managers to force pass ordering;
1728
# intrinsics should never get lowered before Julia has optimized them.
1829

1930
ModulePassManager() do pm
2031
initialize!(pm)
21-
ccall(:jl_add_optimization_passes, Cvoid,
22-
(LLVM.API.LLVMPassManagerRef, Cint, Cint),
23-
pm, Base.JLOptions().opt_level, #=lower_intrinsics=# 0)
32+
if params.julia
33+
ccall(:jl_add_optimization_passes, Cvoid,
34+
(LLVM.API.LLVMPassManagerRef, Cint, Cint),
35+
pm, params.optlevel, #=lower_intrinsics=# 0)
36+
end
37+
if params.optlevel < 2
38+
# Julia doesn't run the alloc optimizer on lower optimization levels,
39+
# but the pass is crucial to remove possibly unsupported malloc calls.
40+
alloc_opt!(pm)
41+
end
2442
run!(pm, mod)
2543
end
2644

27-
ModulePassManager() do pm
45+
params.intrinsics && ModulePassManager() do pm
2846
initialize!(pm)
2947

3048
# lower intrinsics
@@ -55,7 +73,7 @@ function optimize!(@nospecialize(job::CompilerJob), mod::LLVM.Module)
5573
# part of the LateLowerGCFrame pass) aren't collected properly.
5674
#
5775
# these might not always be safe, as Julia's IR metadata isn't designed for IPO.
58-
ModulePassManager() do pm
76+
params.ipo && ModulePassManager() do pm
5977
initialize!(pm)
6078

6179
dead_arg_elimination!(pm) # parent doesn't use return value --> ret void

src/spirv.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ function finish_module!(job::CompilerJob{SPIRVCompilerTarget}, mod::LLVM.Module)
6262
end
6363
end
6464

65+
# the LLVM to SPIRV translator does not support optimized LLVM IR
66+
# (KhronosGroup/SPIRV-LLVM-Translator#203). however, not optimizing at all
67+
# doesn't work either, as we then don't even run the alloc optimizer (resulting
68+
# in many calls to gpu_alloc that spirv-opt cannot remove) or even 'invalid IR'
69+
# like casts to addrspace-less pointers (which aren't allowed in SPIR-V).
70+
optimization_params(@nospecialize(job::CompilerJob{SPIRVCompilerTarget})) =
71+
GPUOptimizationParams(; optlevel=1)
72+
6573
@unlocked function mcgen(job::CompilerJob{SPIRVCompilerTarget}, mod::LLVM.Module,
6674
format=LLVM.API.LLVMAssemblyFile)
6775
# The SPIRV Tools don't handle Julia's debug info, rejecting DW_LANG_Julia...

0 commit comments

Comments
 (0)