5
5
# ####################
6
6
7
7
mutable struct OptimizationState
8
+ params:: OptimizationParams
8
9
linfo:: MethodInstance
9
10
calledges:: Vector{Any}
10
11
src:: CodeInfo
11
12
mod:: Module
12
13
nargs:: Int
14
+ world:: UInt
13
15
min_valid:: UInt
14
16
max_valid:: UInt
15
- params:: Params
16
17
sptypes:: Vector{Any} # static parameters
17
18
slottypes:: Vector{Any}
18
19
const_api:: Bool
19
20
# cached results of calling `_methods_by_ftype` from inference, including
20
21
# `min_valid` and `max_valid`
21
22
matching_methods_cache:: IdDict{Any, Tuple{Any, UInt, UInt}}
22
- function OptimizationState (frame:: InferenceState )
23
+ # TODO : This will be eliminated once optimization no longer needs to do method lookups
24
+ interp:: AbstractInterpreter
25
+ function OptimizationState (frame:: InferenceState , params:: OptimizationParams , interp:: AbstractInterpreter )
23
26
s_edges = frame. stmt_edges[1 ]
24
27
if s_edges === nothing
25
28
s_edges = []
26
29
frame. stmt_edges[1 ] = s_edges
27
30
end
28
31
src = frame. src
29
- return new (frame. linfo,
32
+ return new (params, frame. linfo,
30
33
s_edges:: Vector{Any} ,
31
34
src, frame. mod, frame. nargs,
32
- frame. min_valid, frame. max_valid,
33
- frame. params, frame . sptypes, frame. slottypes, false ,
34
- frame. matching_methods_cache)
35
+ frame. world, frame . min_valid, frame. max_valid,
36
+ frame. sptypes, frame. slottypes, false ,
37
+ frame. matching_methods_cache, interp )
35
38
end
36
- function OptimizationState (linfo:: MethodInstance , src:: CodeInfo ,
37
- params:: Params )
39
+ function OptimizationState (linfo:: MethodInstance , src:: CodeInfo , params:: OptimizationParams , interp:: AbstractInterpreter )
38
40
# prepare src for running optimization passes
39
41
# if it isn't already
40
42
nssavalues = src. ssavaluetypes
@@ -57,19 +59,19 @@ mutable struct OptimizationState
57
59
inmodule = linfo. def:: Module
58
60
nargs = 0
59
61
end
60
- return new (linfo,
62
+ return new (params, linfo,
61
63
s_edges:: Vector{Any} ,
62
64
src, inmodule, nargs,
63
- UInt (1 ), get_world_counter (),
64
- params, sptypes_from_meth_instance (linfo), slottypes, false ,
65
- IdDict {Any, Tuple{Any, UInt, UInt}} ())
65
+ get_world_counter (), UInt (1 ), get_world_counter (),
66
+ sptypes_from_meth_instance (linfo), slottypes, false ,
67
+ IdDict {Any, Tuple{Any, UInt, UInt}} (), interp )
66
68
end
67
69
end
68
70
69
- function OptimizationState (linfo:: MethodInstance , params:: Params )
71
+ function OptimizationState (linfo:: MethodInstance , params:: OptimizationParams , interp :: AbstractInterpreter )
70
72
src = retrieve_code_info (linfo)
71
73
src === nothing && return nothing
72
- return OptimizationState (linfo, src, params)
74
+ return OptimizationState (linfo, src, params, interp )
73
75
end
74
76
75
77
@@ -109,7 +111,7 @@ _topmod(sv::OptimizationState) = _topmod(sv.mod)
109
111
function update_valid_age! (min_valid:: UInt , max_valid:: UInt , sv:: OptimizationState )
110
112
sv. min_valid = max (sv. min_valid, min_valid)
111
113
sv. max_valid = min (sv. max_valid, max_valid)
112
- @assert (sv. min_valid <= sv. params . world <= sv. max_valid,
114
+ @assert (sv. min_valid <= sv. world <= sv. max_valid,
113
115
" invalid age range update" )
114
116
nothing
115
117
end
@@ -127,10 +129,10 @@ function add_backedge!(li::CodeInstance, caller::OptimizationState)
127
129
nothing
128
130
end
129
131
130
- function isinlineable (m:: Method , me:: OptimizationState , bonus:: Int = 0 )
132
+ function isinlineable (m:: Method , me:: OptimizationState , params :: OptimizationParams , bonus:: Int = 0 )
131
133
# compute the cost (size) of inlining this code
132
134
inlineable = false
133
- cost_threshold = me . params. inline_cost_threshold
135
+ cost_threshold = params. inline_cost_threshold
134
136
if m. module === _topmod (m. module)
135
137
# a few functions get special treatment
136
138
name = m. name
@@ -145,7 +147,7 @@ function isinlineable(m::Method, me::OptimizationState, bonus::Int=0)
145
147
end
146
148
end
147
149
if ! inlineable
148
- inlineable = inline_worthy (me. src. code, me. src, me. sptypes, me. slottypes, me . params, cost_threshold + bonus)
150
+ inlineable = inline_worthy (me. src. code, me. src, me. sptypes, me. slottypes, params, cost_threshold + bonus)
149
151
end
150
152
return inlineable
151
153
end
@@ -168,7 +170,7 @@ function stmt_affects_purity(@nospecialize(stmt), ir)
168
170
end
169
171
170
172
# run the optimization work
171
- function optimize (opt:: OptimizationState , @nospecialize (result))
173
+ function optimize (opt:: OptimizationState , params :: OptimizationParams , @nospecialize (result))
172
174
def = opt. linfo. def
173
175
nargs = Int (opt. nargs) - 1
174
176
@timeit " optimizer" ir = run_passes (opt. src, nargs, opt)
@@ -247,13 +249,13 @@ function optimize(opt::OptimizationState, @nospecialize(result))
247
249
else
248
250
bonus = 0
249
251
if result ⊑ Tuple && ! isbitstype (widenconst (result))
250
- bonus = opt . params. inline_tupleret_bonus
252
+ bonus = params. inline_tupleret_bonus
251
253
end
252
254
if opt. src. inlineable
253
255
# For functions declared @inline, increase the cost threshold 20x
254
- bonus += opt . params. inline_cost_threshold* 19
256
+ bonus += params. inline_cost_threshold* 19
255
257
end
256
- opt. src. inlineable = isinlineable (def, opt, bonus)
258
+ opt. src. inlineable = isinlineable (def, opt, params, bonus)
257
259
end
258
260
end
259
261
nothing
@@ -282,7 +284,7 @@ plus_saturate(x::Int, y::Int) = max(x, y, x+y)
282
284
# known return type
283
285
isknowntype (@nospecialize T) = (T === Union{}) || isa (T, Const) || isconcretetype (widenconst (T))
284
286
285
- function statement_cost (ex:: Expr , line:: Int , src:: CodeInfo , sptypes:: Vector{Any} , slottypes:: Vector{Any} , params:: Params )
287
+ function statement_cost (ex:: Expr , line:: Int , src:: CodeInfo , sptypes:: Vector{Any} , slottypes:: Vector{Any} , params:: OptimizationParams )
286
288
head = ex. head
287
289
if is_meta_expr_head (head)
288
290
return 0
@@ -372,7 +374,7 @@ function statement_cost(ex::Expr, line::Int, src::CodeInfo, sptypes::Vector{Any}
372
374
end
373
375
374
376
function inline_worthy (body:: Array{Any,1} , src:: CodeInfo , sptypes:: Vector{Any} , slottypes:: Vector{Any} ,
375
- params:: Params , cost_threshold:: Integer = params. inline_cost_threshold)
377
+ params:: OptimizationParams , cost_threshold:: Integer = params. inline_cost_threshold)
376
378
bodycost:: Int = 0
377
379
for line = 1 : length (body)
378
380
stmt = body[line]
0 commit comments