Skip to content

Commit bb3b743

Browse files
committed
remove weird special inline treatment, simplify inline cost computation
1 parent 5d660ef commit bb3b743

File tree

3 files changed

+17
-32
lines changed

3 files changed

+17
-32
lines changed

base/compiler/optimize.jl

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -174,25 +174,6 @@ const TOP_TUPLE = GlobalRef(Core, :tuple)
174174

175175
_topmod(sv::OptimizationState) = _topmod(sv.mod)
176176

177-
function isinlineable(m::Method, me::OptimizationState, params::OptimizationParams, union_penalties::Bool, bonus::Int=0)
178-
# compute the cost (size) of inlining this code
179-
cost_threshold = params.inline_cost_threshold
180-
if m.module === _topmod(m.module)
181-
# a few functions get special treatment
182-
name = m.name
183-
sig = m.sig
184-
if ((name === :+ || name === :* || name === :min || name === :max) &&
185-
isa(sig,DataType) &&
186-
sig === Tuple{sig.parameters[1],Any,Any,Any,Vararg{Any}})
187-
return true
188-
elseif (name === :iterate || name === :unsafe_convert ||
189-
name === :cconvert)
190-
cost_threshold *= 4
191-
end
192-
end
193-
return inline_worthy(me.ir::IRCode, params, union_penalties, cost_threshold + bonus)
194-
end
195-
196177
is_stmt_inline(stmt_flag::UInt8) = stmt_flag & IR_FLAG_INLINE 0
197178
is_stmt_noinline(stmt_flag::UInt8) = stmt_flag & IR_FLAG_NOINLINE 0
198179
is_stmt_throw_block(stmt_flag::UInt8) = stmt_flag & IR_FLAG_THROW_BLOCK 0
@@ -292,19 +273,27 @@ function finish(interp::AbstractInterpreter, opt::OptimizationState, params::Opt
292273
if src.inlineable && isdispatchtuple(specTypes)
293274
# obey @inline declaration if a dispatch barrier would not help
294275
else
295-
bonus = 0
276+
# compute the cost (size) of inlining this code
277+
cost_threshold = default = params.inline_cost_threshold
296278
if result Tuple && !isconcretetype(widenconst(result))
297-
bonus = params.inline_tupleret_bonus
279+
cost_threshold += params.inline_tupleret_bonus
298280
end
281+
# if the method is declared as `@inline`, increase the cost threshold 20x
299282
if src.inlineable
300-
# For functions declared @inline, increase the cost threshold 20x
301-
bonus += params.inline_cost_threshold*19
283+
cost_threshold += 19*default
302284
end
303-
src.inlineable = isinlineable(def, opt, params, union_penalties, bonus)
285+
# a few functions get special treatment
286+
if def.module === _topmod(def.module)
287+
name = def.name
288+
if name === :iterate || name === :unsafe_convert || name === :cconvert
289+
cost_threshold += 4*default
290+
end
291+
end
292+
src.inlineable = inline_worthy(ir, params, union_penalties, cost_threshold)
304293
end
305294
end
306295

307-
nothing
296+
return nothing
308297
end
309298

310299
# run the optimization work

base/operators.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ for op in (:+, :*, :&, :|, :xor, :min, :max, :kron)
636636
# note: these definitions must not cause a dispatch loop when +(a,b) is
637637
# not defined, and must only try to call 2-argument definitions, so
638638
# that defining +(a,b) is sufficient for full functionality.
639-
($op)(a, b, c, xs...) = afoldl($op, ($op)(($op)(a,b),c), xs...)
639+
($op)(a, b, c, xs...) = (@inline; afoldl($op, ($op)(($op)(a,b),c), xs...))
640640
# a further concern is that it's easy for a type like (Int,Int...)
641641
# to match many definitions, so we need to keep the number of
642642
# definitions down to avoid losing type information.

doc/src/devdocs/inference.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,8 @@ ci = (@code_typed convert(Int, UInt(1)))[1]
5353

5454
Much of the hardest work for inlining runs in `ssa_inlining_pass!`.
5555
However, if your question is "why didn't my function inline?"
56-
then you will most likely be interested in `isinlineable` and its
57-
primary callee, `inline_worthy`.
58-
`isinlineable` handles a number of special cases
59-
(e.g., critical functions like `iterate`).
60-
The main decision-making happens in `inline_worthy`,
61-
which returns `true` if the function should be inlined.
56+
then you will most likely be interested in `inline_worthy`,
57+
which makes a decision to inline the function call or not.
6258

6359
`inline_worthy` implements a cost-model, where "cheap" functions get
6460
inlined; more specifically, we inline functions if their anticipated

0 commit comments

Comments
 (0)