Skip to content

Commit 88ebed3

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 - remove `nkw` meta after it's consumed
1 parent 47087cb commit 88ebed3

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

base/compiler/optimize.jl

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,17 +315,30 @@ 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
321+
elseif (f === isa || f === typeassert) && isconstType(widenconst(argextype(ex.args[3], src, sptypes, slottypes)))
322+
return 1
323+
elseif f === typeof
324+
return 1
321325
end
322326
fidx = find_tfunc(f)
323327
if fidx === nothing
324-
# unknown/unhandled builtin or anonymous function
328+
# unknown/unhandled builtin
325329
# Use the generic cost of a direct function call
326330
return 20
327331
end
328-
return T_FFUNC_COST[fidx]
332+
cost = T_FFUNC_COST[fidx]
333+
if cost < 20
334+
for i = 2:length(ex.args)
335+
argtyp = argextype(ex.args[i], src, sptypes, slottypes)
336+
if !(isknowntype(argtyp) || isType(argtyp))
337+
return 20
338+
end
339+
end
340+
end
341+
return cost
329342
end
330343
return params.inline_nonleaf_penalty
331344
elseif head === :foreigncall || head === :invoke
@@ -366,7 +379,7 @@ function statement_cost(ex::Expr, line::Int, src::CodeInfo, sptypes::Vector{Any}
366379
# loops are generally always expensive
367380
# but assume that forward jumps are already counted for from
368381
# summing the cost of the not-taken branch
369-
return target < line ? 40 : 0
382+
return target < line ? 40 : 1
370383
end
371384
return 0
372385
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 ||

base/iddict.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function get(d::IdDict{K,V}, @nospecialize(key), @nospecialize(default)) where {
8686
val === default ? default : val::V
8787
end
8888
function getindex(d::IdDict{K,V}, @nospecialize(key)) where {K, V}
89-
val = get(d, key, secret_table_token)
89+
val = ccall(:jl_eqtable_get, Any, (Any, Any, Any), d.ht, key, secret_table_token)
9090
val === secret_table_token && throw(KeyError(key))
9191
return val::V
9292
end
@@ -137,15 +137,15 @@ copy(d::IdDict) = typeof(d)(d)
137137
get!(d::IdDict{K,V}, @nospecialize(key), @nospecialize(default)) where {K, V} = (d[key] = get(d, key, default))::V
138138

139139
function get(default::Callable, d::IdDict{K,V}, @nospecialize(key)) where {K, V}
140-
val = get(d, key, secret_table_token)
140+
val = ccall(:jl_eqtable_get, Any, (Any, Any, Any), d.ht, key, secret_table_token)
141141
if val === secret_table_token
142142
val = default()
143143
end
144144
return val
145145
end
146146

147147
function get!(default::Callable, d::IdDict{K,V}, @nospecialize(key)) where {K, V}
148-
val = get(d, key, secret_table_token)
148+
val = ccall(:jl_eqtable_get, Any, (Any, Any, Any), d.ht, key, secret_table_token)
149149
if val === secret_table_token
150150
val = default()
151151
setindex!(d, val, key)

doc/src/devdocs/inference.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ cst = map(cost, ci.code)
124124
1
125125
1
126126
1
127-
0
127+
1
128128
0
129129
0
130130
@@ -133,7 +133,7 @@ cst = map(cost, ci.code)
133133
0
134134
0
135135
1
136-
0
136+
1
137137
0
138138
0
139139
0

0 commit comments

Comments
 (0)