|
1 | 1 | # LLVM IR optimization
|
2 | 2 |
|
| 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 | + |
3 | 11 | function optimize!(@nospecialize(job::CompilerJob), mod::LLVM.Module)
|
| 12 | + triple = llvm_triple(job.target) |
4 | 13 | tm = llvm_machine(job.target)
|
5 | 14 |
|
6 | 15 | function initialize!(pm)
|
7 |
| - add_library_info!(pm, triple(mod)) |
| 16 | + add_library_info!(pm, triple) |
8 | 17 | add_transform_info!(pm, tm)
|
9 | 18 | end
|
10 | 19 |
|
11 | 20 | global current_job
|
12 | 21 | current_job = job
|
13 | 22 |
|
| 23 | + params = optimization_params(job) |
| 24 | + |
14 | 25 | # Julia-specific optimizations
|
15 | 26 | #
|
16 | 27 | # NOTE: we need to use multiple distinct pass managers to force pass ordering;
|
17 | 28 | # intrinsics should never get lowered before Julia has optimized them.
|
18 | 29 |
|
19 | 30 | ModulePassManager() do pm
|
20 | 31 | 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 |
24 | 42 | run!(pm, mod)
|
25 | 43 | end
|
26 | 44 |
|
27 |
| - ModulePassManager() do pm |
| 45 | + params.intrinsics && ModulePassManager() do pm |
28 | 46 | initialize!(pm)
|
29 | 47 |
|
30 | 48 | # lower intrinsics
|
@@ -55,7 +73,7 @@ function optimize!(@nospecialize(job::CompilerJob), mod::LLVM.Module)
|
55 | 73 | # part of the LateLowerGCFrame pass) aren't collected properly.
|
56 | 74 | #
|
57 | 75 | # 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 |
59 | 77 | initialize!(pm)
|
60 | 78 |
|
61 | 79 | dead_arg_elimination!(pm) # parent doesn't use return value --> ret void
|
|
0 commit comments