Skip to content

Commit a4b3126

Browse files
committed
some inlining cost model updates
- treat arrayset like arrayref - higher cost for passing unknown things to builtins - add small cost for forward branches - add small cost for sizeof, nfields, isa, subtype
1 parent b81ce9e commit a4b3126

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

base/compiler/optimize.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,17 +315,24 @@ function statement_cost(ex::Expr, line::Int, src::CodeInfo, sptypes::Vector{Any}
315315
# tuple iteration/destructuring makes that impossible
316316
# return plus_saturate(argcost, isknowntype(extyp) ? 1 : params.inline_nonleaf_penalty)
317317
return 0
318-
elseif (f === Main.Core.arrayref || f === Main.Core.const_arrayref) && length(ex.args) >= 3
318+
elseif (f === Main.Core.arrayref || f === Main.Core.const_arrayref || f === Main.Core.arrayset) && length(ex.args) >= 3
319319
atyp = argextype(ex.args[3], src, sptypes, slottypes)
320320
return isknowntype(atyp) ? 4 : params.inline_nonleaf_penalty
321321
end
322322
fidx = find_tfunc(f)
323323
if fidx === nothing
324-
# unknown/unhandled builtin or anonymous function
324+
# unknown/unhandled builtin
325325
# Use the generic cost of a direct function call
326326
return 20
327327
end
328-
return T_FFUNC_COST[fidx]
328+
cost = T_FFUNC_COST[fidx]
329+
for i = 2:length(ex.args)
330+
argtyp = argextype(ex.args[i], src, sptypes, slottypes)
331+
if !(isknowntype(argtyp) || isType(argtyp))
332+
return max(cost, 10)
333+
end
334+
end
335+
return cost
329336
end
330337
return params.inline_nonleaf_penalty
331338
elseif head === :foreigncall || head === :invoke
@@ -366,7 +373,7 @@ function statement_cost(ex::Expr, line::Int, src::CodeInfo, sptypes::Vector{Any}
366373
# loops are generally always expensive
367374
# but assume that forward jumps are already counted for from
368375
# summing the cost of the not-taken branch
369-
return target < line ? 40 : 0
376+
return target < line ? 40 : 1
370377
end
371378
return 0
372379
end

base/compiler/tfuncs.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ function sizeof_tfunc(@nospecialize(x),)
342342
isprimitivetype(x) && return _const_sizeof(x)
343343
return Int
344344
end
345-
add_tfunc(Core.sizeof, 1, 1, sizeof_tfunc, 0)
345+
add_tfunc(Core.sizeof, 1, 1, sizeof_tfunc, 1)
346346
function nfields_tfunc(@nospecialize(x))
347347
isa(x, Const) && return Const(nfields(x.val))
348348
isa(x, Conditional) && return Const(0)
@@ -354,7 +354,7 @@ function nfields_tfunc(@nospecialize(x))
354354
end
355355
return Int
356356
end
357-
add_tfunc(nfields, 1, 1, nfields_tfunc, 0)
357+
add_tfunc(nfields, 1, 1, nfields_tfunc, 1)
358358
add_tfunc(Core._expr, 1, INT_INF, (@nospecialize args...)->Expr, 100)
359359
function typevar_tfunc(@nospecialize(n), @nospecialize(lb_arg), @nospecialize(ub_arg))
360360
lb = Union{}
@@ -537,7 +537,7 @@ function isa_tfunc(@nospecialize(v), @nospecialize(tt))
537537
# TODO: handle non-leaftype(t) by testing against lower and upper bounds
538538
return Bool
539539
end
540-
add_tfunc(isa, 2, 2, isa_tfunc, 0)
540+
add_tfunc(isa, 2, 2, isa_tfunc, 1)
541541

542542
function subtype_tfunc(@nospecialize(a), @nospecialize(b))
543543
a, isexact_a = instanceof_tfunc(a)
@@ -555,7 +555,7 @@ function subtype_tfunc(@nospecialize(a), @nospecialize(b))
555555
end
556556
return Bool
557557
end
558-
add_tfunc(<:, 2, 2, subtype_tfunc, 0)
558+
add_tfunc(<:, 2, 2, subtype_tfunc, 1)
559559

560560
is_dt_const_field(fld::Int) = (
561561
fld == DATATYPE_NAME_FIELDINDEX ||

0 commit comments

Comments
 (0)