Skip to content

Commit 0f56da8

Browse files
authored
inlining: NFC simplifications on the inlining algorithm (#50593)
- pack return value of `ir_prepare_inlining!` into a struct and pass it around - improved handling of `linetable` argument
1 parent c5e4621 commit 0f56da8

File tree

3 files changed

+61
-62
lines changed

3 files changed

+61
-62
lines changed

base/compiler/ssair/inlining.jl

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,7 @@ inline_node_is_duplicate(topline::LineInfoNode, line::LineInfoNode) =
322322
topline.line === line.line
323323

324324
function ir_inline_linetable!(linetable::Vector{LineInfoNode}, inlinee_ir::IRCode,
325-
inlinee::MethodInstance,
326-
inlined_at::Int32)
325+
inlinee::MethodInstance, inlined_at::Int32)
327326
inlinee_def = inlinee.def::Method
328327
coverage = coverage_enabled(inlinee_def.module)
329328
linetable_offset::Int32 = length(linetable)
@@ -358,18 +357,18 @@ function ir_inline_linetable!(linetable::Vector{LineInfoNode}, inlinee_ir::IRCod
358357
end
359358

360359
function ir_prepare_inlining!(insert_node!::Inserter, inline_target::Union{IRCode, IncrementalCompact},
361-
linetable::Vector{LineInfoNode}, ir′::IRCode, sparam_vals::SimpleVector,
362-
mi::MethodInstance, inlined_at::Int32, argexprs::Vector{Any})
360+
ir::IRCode, mi::MethodInstance, inlined_at::Int32, argexprs::Vector{Any})
363361
def = mi.def::Method
362+
linetable = inline_target isa IRCode ? inline_target.linetable : inline_target.ir.linetable
364363
topline::Int32 = length(linetable) + Int32(1)
365-
linetable_offset, extra_coverage_line = ir_inline_linetable!(linetable, ir, mi, inlined_at)
364+
linetable_offset, extra_coverage_line = ir_inline_linetable!(linetable, ir, mi, inlined_at)
366365
if extra_coverage_line != 0
367366
insert_node!(NewInstruction(Expr(:code_coverage_effect), Nothing, extra_coverage_line))
368367
end
369-
sp_ssa = nothing
370-
if !validate_sparams(sparam_vals)
368+
spvals_ssa = nothing
369+
if !validate_sparams(mi.sparam_vals)
371370
# N.B. This works on the caller-side argexprs, (i.e. before the va fixup below)
372-
sp_ssa = insert_node!(
371+
spvals_ssa = insert_node!(
373372
effect_free_and_nothrow(NewInstruction(Expr(:call, Core._compute_sparams, def, argexprs...), SimpleVector, topline)))
374373
end
375374
if def.isva
@@ -382,20 +381,17 @@ function ir_prepare_inlining!(insert_node!::Inserter, inline_target::Union{IRCod
382381
# Replace the first argument by a load of the capture environment
383382
argexprs[1] = insert_node!(
384383
NewInstruction(Expr(:call, GlobalRef(Core, :getfield), argexprs[1], QuoteNode(:captures)),
385-
ir.argtypes[1], topline))
384+
ir.argtypes[1], topline))
386385
end
387-
return (Pair{Union{Nothing, SSAValue}, Vector{Any}}(sp_ssa, argexprs), linetable_offset)
386+
return SSASubstitute(mi, argexprs, spvals_ssa, linetable_offset)
388387
end
389388

390389
function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector{Any},
391-
linetable::Vector{LineInfoNode}, item::InliningTodo,
392-
boundscheck::Symbol, todo_bbs::Vector{Tuple{Int, Int}})
390+
item::InliningTodo, boundscheck::Symbol, todo_bbs::Vector{Tuple{Int, Int}})
393391
# Ok, do the inlining here
394-
sparam_vals = item.mi.sparam_vals
395392
inlined_at = compact.result[idx][:line]
396393

397-
((sp_ssa, argexprs), linetable_offset) = ir_prepare_inlining!(InsertHere(compact),
398-
compact, linetable, item.ir, sparam_vals, item.mi, inlined_at, argexprs)
394+
ssa_substitute = ir_prepare_inlining!(InsertHere(compact), compact, item.ir, item.mi, inlined_at, argexprs)
399395

400396
if boundscheck === :default || boundscheck === :propagate
401397
if (compact.result[idx][:flag] & IR_FLAG_INBOUNDS) != 0
@@ -405,8 +401,6 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
405401
# If the iterator already moved on to the next basic block,
406402
# temporarily re-open in again.
407403
local return_value
408-
def = item.mi.def::Method
409-
sig = def.sig
410404
# Special case inlining that maintains the current basic block if there's only one BB in the target
411405
new_new_offset = length(compact.new_new_nodes)
412406
late_fixup_offset = length(compact.late_fixup)
@@ -418,7 +412,9 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
418412
# face of rename_arguments! mutating in place - should figure out
419413
# something better eventually.
420414
inline_compact[idx′] = nothing
421-
stmt′ = ssa_substitute!(InsertBefore(inline_compact, SSAValue(idx′)), inline_compact[SSAValue(idx′)], stmt′, argexprs, sig, sparam_vals, sp_ssa, linetable_offset, boundscheck)
415+
insert_node! = InsertBefore(inline_compact, SSAValue(idx′))
416+
stmt′ = ssa_substitute!(insert_node!, inline_compact[SSAValue(idx′)], stmt′,
417+
ssa_substitute, boundscheck)
422418
if isa(stmt′, ReturnNode)
423419
val = stmt′.val
424420
return_value = SSAValue(idx′)
@@ -445,7 +441,9 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
445441
inline_compact = IncrementalCompact(compact, item.ir, compact.result_idx)
446442
for ((_, idx′), stmt′) in inline_compact
447443
inline_compact[idx′] = nothing
448-
stmt′ = ssa_substitute!(InsertBefore(inline_compact, SSAValue(idx′)), inline_compact[SSAValue(idx′)], stmt′, argexprs, sig, sparam_vals, sp_ssa, linetable_offset, boundscheck)
444+
insert_node! = InsertBefore(inline_compact, SSAValue(idx′))
445+
stmt′ = ssa_substitute!(insert_node!, inline_compact[SSAValue(idx′)], stmt′,
446+
ssa_substitute, boundscheck)
449447
if isa(stmt′, ReturnNode)
450448
if isdefined(stmt′, :val)
451449
val = stmt′.val
@@ -554,11 +552,10 @@ excluding cases where `case.sig::UnionAll`.
554552
In short, here we can process the dispatch candidates in order, assuming we haven't changed
555553
their order somehow somewhere up to this point.
556554
"""
557-
function ir_inline_unionsplit!(compact::IncrementalCompact, idx::Int,
558-
argexprs::Vector{Any}, linetable::Vector{LineInfoNode},
559-
(; fully_covered, atype, cases, bbs)::UnionSplit,
560-
boundscheck::Symbol, todo_bbs::Vector{Tuple{Int, Int}},
561-
params::OptimizationParams)
555+
function ir_inline_unionsplit!(compact::IncrementalCompact, idx::Int, argexprs::Vector{Any},
556+
union_split::UnionSplit, boundscheck::Symbol,
557+
todo_bbs::Vector{Tuple{Int,Int}}, params::OptimizationParams)
558+
(; fully_covered, atype, cases, bbs) = union_split
562559
stmt, typ, line = compact.result[idx][:inst], compact.result[idx][:type], compact.result[idx][:line]
563560
join_bb = bbs[end]
564561
pn = PhiNode()
@@ -606,7 +603,7 @@ function ir_inline_unionsplit!(compact::IncrementalCompact, idx::Int,
606603
end
607604
end
608605
if isa(case, InliningTodo)
609-
val = ir_inline_item!(compact, idx, argexprs′, linetable, case, boundscheck, todo_bbs)
606+
val = ir_inline_item!(compact, idx, argexprs′, case, boundscheck, todo_bbs)
610607
elseif isa(case, InvokeCase)
611608
inst = Expr(:invoke, case.invoke, argexprs′...)
612609
flag = flags_for_effects(case.effects)
@@ -698,9 +695,9 @@ function batch_inline!(ir::IRCode, todo::Vector{Pair{Int,Any}}, propagate_inboun
698695
end
699696
end
700697
if isa(item, InliningTodo)
701-
compact.ssa_rename[old_idx] = ir_inline_item!(compact, idx, argexprs, ir.linetable, item, boundscheck, state.todo_bbs)
698+
compact.ssa_rename[old_idx] = ir_inline_item!(compact, idx, argexprs, item, boundscheck, state.todo_bbs)
702699
elseif isa(item, UnionSplit)
703-
compact.ssa_rename[old_idx] = ir_inline_unionsplit!(compact, idx, argexprs, ir.linetable, item, boundscheck, state.todo_bbs, params)
700+
compact.ssa_rename[old_idx] = ir_inline_unionsplit!(compact, idx, argexprs, item, boundscheck, state.todo_bbs, params)
704701
end
705702
compact[idx] = nothing
706703
refinish && finish_current_bb!(compact, 0)
@@ -1795,15 +1792,18 @@ function late_inline_special_case!(
17951792
return nothing
17961793
end
17971794

1798-
function ssa_substitute!(insert_node!::Inserter,
1799-
subst_inst::Instruction, @nospecialize(val), arg_replacements::Vector{Any},
1800-
@nospecialize(spsig), spvals::SimpleVector,
1801-
spvals_ssa::Union{Nothing, SSAValue},
1802-
linetable_offset::Int32, boundscheck::Symbol)
1795+
struct SSASubstitute
1796+
mi::MethodInstance
1797+
arg_replacements::Vector{Any}
1798+
spvals_ssa::Union{Nothing,SSAValue}
1799+
linetable_offset::Int32
1800+
end
1801+
1802+
function ssa_substitute!(insert_node!::Inserter, subst_inst::Instruction, @nospecialize(val),
1803+
ssa_substitute::SSASubstitute, boundscheck::Symbol)
18031804
subst_inst[:flag] &= ~IR_FLAG_INBOUNDS
1804-
subst_inst[:line] += linetable_offset
1805-
return ssa_substitute_op!(insert_node!, subst_inst,
1806-
val, arg_replacements, spsig, spvals, spvals_ssa, boundscheck)
1805+
subst_inst[:line] += ssa_substitute.linetable_offset
1806+
return ssa_substitute_op!(insert_node!, subst_inst, val, ssa_substitute, boundscheck)
18071807
end
18081808

18091809
function insert_spval!(insert_node!::Inserter, spvals_ssa::SSAValue, spidx::Int, do_isdefined::Bool)
@@ -1819,26 +1819,24 @@ function insert_spval!(insert_node!::Inserter, spvals_ssa::SSAValue, spidx::Int,
18191819
return (ret, tcheck_not)
18201820
end
18211821

1822-
function ssa_substitute_op!(insert_node!::Inserter, subst_inst::Instruction,
1823-
@nospecialize(val), arg_replacements::Vector{Any},
1824-
@nospecialize(spsig), spvals::SimpleVector,
1825-
spvals_ssa::Union{Nothing, SSAValue},
1826-
boundscheck::Symbol)
1822+
function ssa_substitute_op!(insert_node!::Inserter, subst_inst::Instruction, @nospecialize(val),
1823+
ssa_substitute::SSASubstitute, boundscheck::Symbol)
18271824
if isa(val, Argument)
1828-
return arg_replacements[val.n]
1825+
return ssa_substitute.arg_replacements[val.n]
18291826
end
18301827
if isa(val, Expr)
18311828
e = val::Expr
18321829
head = e.head
1830+
sparam_vals = ssa_substitute.mi.sparam_vals
18331831
if head === :static_parameter
18341832
spidx = e.args[1]::Int
1835-
val = spvals[spidx]
1833+
val = sparam_vals[spidx]
18361834
if !isa(val, TypeVar) && val !== Vararg
18371835
return quoted(val)
18381836
else
18391837
flag = subst_inst[:flag]
18401838
maybe_undef = (flag & IR_FLAG_NOTHROW) == 0 && isa(val, TypeVar)
1841-
(ret, tcheck_not) = insert_spval!(insert_node!, spvals_ssa::SSAValue, spidx, maybe_undef)
1839+
(ret, tcheck_not) = insert_spval!(insert_node!, ssa_substitute.spvals_ssa::SSAValue, spidx, maybe_undef)
18421840
if maybe_undef
18431841
insert_node!(
18441842
NewInstruction(Expr(:throw_undef_if_not, val.name, tcheck_not), Nothing))
@@ -1847,27 +1845,29 @@ function ssa_substitute_op!(insert_node!::Inserter, subst_inst::Instruction,
18471845
end
18481846
elseif head === :isdefined && isa(e.args[1], Expr) && e.args[1].head === :static_parameter
18491847
spidx = (e.args[1]::Expr).args[1]::Int
1850-
val = spvals[spidx]
1848+
val = sparam_vals[spidx]
18511849
if !isa(val, TypeVar)
18521850
return true
18531851
else
1854-
(_, tcheck_not) = insert_spval!(insert_node!, spvals_ssa::SSAValue, spidx, true)
1852+
(_, tcheck_not) = insert_spval!(insert_node!, ssa_substitute.spvals_ssa::SSAValue, spidx, true)
18551853
return tcheck_not
18561854
end
1857-
elseif head === :cfunction && spvals_ssa === nothing
1858-
@assert !isa(spsig, UnionAll) || !isempty(spvals)
1859-
e.args[3] = ccall(:jl_instantiate_type_in_env, Any, (Any, Any, Ptr{Any}), e.args[3], spsig, spvals)
1855+
elseif head === :cfunction && ssa_substitute.spvals_ssa === nothing
1856+
msig = (ssa_substitute.mi.def::Method).sig
1857+
@assert !isa(msig, UnionAll) || !isempty(sparam_vals)
1858+
e.args[3] = ccall(:jl_instantiate_type_in_env, Any, (Any, Any, Ptr{Any}), e.args[3], msig, sparam_vals)
18601859
e.args[4] = svec(Any[
1861-
ccall(:jl_instantiate_type_in_env, Any, (Any, Any, Ptr{Any}), argt, spsig, spvals)
1860+
ccall(:jl_instantiate_type_in_env, Any, (Any, Any, Ptr{Any}), argt, msig, sparam_vals)
18621861
for argt in e.args[4]::SimpleVector ]...)
1863-
elseif head === :foreigncall && spvals_ssa === nothing
1864-
@assert !isa(spsig, UnionAll) || !isempty(spvals)
1862+
elseif head === :foreigncall && ssa_substitute.spvals_ssa === nothing
1863+
msig = (ssa_substitute.mi.def::Method).sig
1864+
@assert !isa(msig, UnionAll) || !isempty(sparam_vals)
18651865
for i = 1:length(e.args)
18661866
if i == 2
1867-
e.args[2] = ccall(:jl_instantiate_type_in_env, Any, (Any, Any, Ptr{Any}), e.args[2], spsig, spvals)
1867+
e.args[2] = ccall(:jl_instantiate_type_in_env, Any, (Any, Any, Ptr{Any}), e.args[2], msig, sparam_vals)
18681868
elseif i == 3
18691869
e.args[3] = svec(Any[
1870-
ccall(:jl_instantiate_type_in_env, Any, (Any, Any, Ptr{Any}), argt, spsig, spvals)
1870+
ccall(:jl_instantiate_type_in_env, Any, (Any, Any, Ptr{Any}), argt, msig, sparam_vals)
18711871
for argt in e.args[3]::SimpleVector ]...)
18721872
end
18731873
end
@@ -1884,7 +1884,7 @@ function ssa_substitute_op!(insert_node!::Inserter, subst_inst::Instruction,
18841884
isa(val, Union{SSAValue, NewSSAValue}) && return val # avoid infinite loop
18851885
urs = userefs(val)
18861886
for op in urs
1887-
op[] = ssa_substitute_op!(insert_node!, subst_inst, op[], arg_replacements, spsig, spvals, spvals_ssa, boundscheck)
1887+
op[] = ssa_substitute_op!(insert_node!, subst_inst, op[], ssa_substitute, boundscheck)
18881888
end
18891889
return urs[]
18901890
end

base/compiler/ssair/ir.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -765,16 +765,16 @@ function dominates_ssa(compact::IncrementalCompact, domtree::DomTree, x::AnySSAV
765765
else
766766
y′ = y
767767
end
768-
if x′.id == y′.id && (xinfo !== nothing || yinfo !== nothing)
768+
if x′.id == y′.id
769769
if xinfo !== nothing && yinfo !== nothing
770770
if xinfo.attach_after == yinfo.attach_after
771771
return x.id < y.id
772772
end
773773
return yinfo.attach_after
774774
elseif xinfo !== nothing
775775
return !xinfo.attach_after
776-
else
777-
return (yinfo::NewNodeInfo).attach_after
776+
elseif yinfo !== nothing
777+
return yinfo.attach_after
778778
end
779779
end
780780
return x′.id < y′.id

base/compiler/ssair/passes.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,11 +1248,9 @@ function try_inline_finalizer!(ir::IRCode, argexprs::Vector{Any}, idx::Int,
12481248

12491249
# TODO: Should there be a special line number node for inlined finalizers?
12501250
inlined_at = ir[SSAValue(idx)][:line]
1251-
((sp_ssa, argexprs), linetable_offset) = ir_prepare_inlining!(InsertBefore(ir, SSAValue(idx)), ir,
1252-
ir.linetable, src, mi.sparam_vals, mi, inlined_at, argexprs)
1251+
ssa_substitute = ir_prepare_inlining!(InsertBefore(ir, SSAValue(idx)), ir, src, mi, inlined_at, argexprs)
12531252

12541253
# TODO: Use the actual inliner here rather than open coding this special purpose inliner.
1255-
spvals = mi.sparam_vals
12561254
ssa_rename = Vector{Any}(undef, length(src.stmts))
12571255
for idx′ = 1:length(src.stmts)
12581256
inst = src[SSAValue(idx′)]
@@ -1261,9 +1259,10 @@ function try_inline_finalizer!(ir::IRCode, argexprs::Vector{Any}, idx::Int,
12611259
stmt′ = ssamap(stmt′) do ssa::SSAValue
12621260
ssa_rename[ssa.id]
12631261
end
1264-
stmt′ = ssa_substitute_op!(InsertBefore(ir, SSAValue(idx)), inst, stmt′, argexprs, mi.specTypes, mi.sparam_vals, sp_ssa, :default)
1262+
stmt′ = ssa_substitute_op!(InsertBefore(ir, SSAValue(idx)), inst, stmt′,
1263+
ssa_substitute, :default)
12651264
ssa_rename[idx′] = insert_node!(ir, idx,
1266-
NewInstruction(inst; stmt=stmt′, line=inst[:line]+linetable_offset),
1265+
NewInstruction(inst; stmt=stmt′, line=inst[:line]+ssa_substitute.linetable_offset),
12671266
attach_after)
12681267
end
12691268

0 commit comments

Comments
 (0)