Skip to content

Commit c4effda

Browse files
Kenoaviatesk
andauthored
Eager finalizer insertion (#45272)
* Eager finalizer insertion This is a variant of the eager-finalization idea (e.g. as seen in #44056), but with a focus on the mechanism of finalizer insertion, since I need a similar pass downstream. Integration of EscapeAnalysis is left to #44056. My motivation for this change is somewhat different. In particular, I want to be able to insert finalize call such that I can subsequently SROA the mutable object. This requires a couple design points that are more stringent than the pass from #44056, so I decided to prototype them as an independent PR. The primary things I need here that are not seen in #44056 are: - The ability to forgo finalizer registration with the runtime entirely (requires additional legality analyis) - The ability to inline the registered finalizer at the deallocation point (to enable subsequent SROA) To this end, adding a finalizer is promoted to a builtin that is recognized by inference and inlining (such that inference can produce an inferred version of the finalizer for inlining). The current status is that this fixes the minimal example I wanted to have work, but does not yet extend to the motivating case I had. Nevertheless, I felt that this was a good checkpoint to synchronize with other efforts along these lines. Currently working demo: ``` julia> const total_deallocations = Ref{Int}(0) Base.RefValue{Int64}(0) julia> mutable struct DoAlloc function DoAlloc() this = new() Core._add_finalizer(this, function(this) global total_deallocations[] += 1 end) return this end end julia> function foo() for i = 1:1000 DoAlloc() end end foo (generic function with 1 method) julia> @code_llvm foo() ; @ REPL[3]:1 within `foo` define void @julia_foo_111() #0 { top: %.promoted = load i64, i64* inttoptr (i64 140370001753968 to i64*), align 16 ; @ REPL[3]:2 within `foo` %0 = add i64 %.promoted, 1000 ; @ REPL[3] within `foo` store i64 %0, i64* inttoptr (i64 140370001753968 to i64*), align 16 ; @ REPL[3]:4 within `foo` ret void } ``` * rm redundant copy Co-authored-by: Shuhei Kadowaki <[email protected]>
1 parent 9086fd0 commit c4effda

File tree

9 files changed

+382
-79
lines changed

9 files changed

+382
-79
lines changed

base/compiler/abstractinterpretation.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,15 @@ function invoke_rewrite(xs::Vector{Any})
16191619
return newxs
16201620
end
16211621

1622+
function abstract_finalizer(interp::AbstractInterpreter, argtypes::Vector{Any}, sv::InferenceState)
1623+
if length(argtypes) == 3
1624+
finalizer_argvec = Any[argtypes[2], argtypes[3]]
1625+
call = abstract_call(interp, ArgInfo(nothing, finalizer_argvec), sv, 1)
1626+
return CallMeta(Nothing, Effects(), FinalizerInfo(call.info, call.effects))
1627+
end
1628+
return CallMeta(Nothing, Effects(), false)
1629+
end
1630+
16221631
# call where the function is known exactly
16231632
function abstract_call_known(interp::AbstractInterpreter, @nospecialize(f),
16241633
arginfo::ArgInfo, sv::InferenceState,
@@ -1633,6 +1642,8 @@ function abstract_call_known(interp::AbstractInterpreter, @nospecialize(f),
16331642
return abstract_invoke(interp, arginfo, sv)
16341643
elseif f === modifyfield!
16351644
return abstract_modifyfield!(interp, argtypes, sv)
1645+
elseif f === Core.finalizer
1646+
return abstract_finalizer(interp, argtypes, sv)
16361647
end
16371648
rt = abstract_call_builtin(interp, f, arginfo, sv, max_methods)
16381649
return CallMeta(rt, builtin_effects(f, argtypes, rt), false)

base/compiler/optimize.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ const IR_FLAG_THROW_BLOCK = 0x01 << 3
2727
# This statement may be removed if its result is unused. In particular it must
2828
# thus be both pure and effect free.
2929
const IR_FLAG_EFFECT_FREE = 0x01 << 4
30+
# This statement was proven not to throw
31+
const IR_FLAG_NOTHROW = 0x01 << 5
32+
3033

3134
const TOP_TUPLE = GlobalRef(Core, :tuple)
3235

@@ -567,7 +570,7 @@ function run_passes(
567570
@pass "Inlining" ir = ssa_inlining_pass!(ir, ir.linetable, sv.inlining, ci.propagate_inbounds)
568571
# @timeit "verify 2" verify_ir(ir)
569572
@pass "compact 2" ir = compact!(ir)
570-
@pass "SROA" ir = sroa_pass!(ir)
573+
@pass "SROA" ir = sroa_pass!(ir, sv.inlining)
571574
@pass "ADCE" ir = adce_pass!(ir)
572575
@pass "type lift" ir = type_lift_pass!(ir)
573576
@pass "compact 3" ir = compact!(ir)

base/compiler/ssair/inlining.jl

Lines changed: 98 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -308,21 +308,17 @@ function finish_cfg_inline!(state::CFGInliningState)
308308
end
309309
end
310310

311-
function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector{Any},
312-
linetable::Vector{LineInfoNode}, item::InliningTodo,
313-
boundscheck::Symbol, todo_bbs::Vector{Tuple{Int, Int}})
314-
# Ok, do the inlining here
315-
spec = item.spec::ResolvedInliningSpec
316-
sparam_vals = item.mi.sparam_vals
317-
def = item.mi.def::Method
311+
function ir_inline_linetable!(linetable::Vector{LineInfoNode}, inlinee_ir::IRCode,
312+
inlinee::Method,
313+
inlined_at::Int32)
314+
coverage = coverage_enabled(inlinee.module)
318315
linetable_offset::Int32 = length(linetable)
319316
# Append the linetable of the inlined function to our line table
320-
inlined_at = compact.result[idx][:line]
321317
topline::Int32 = linetable_offset + Int32(1)
322-
coverage = coverage_enabled(def.module)
323318
coverage_by_path = JLOptions().code_coverage == 3
324-
push!(linetable, LineInfoNode(def.module, def.name, def.file, def.line, inlined_at))
325-
oldlinetable = spec.ir.linetable
319+
push!(linetable, LineInfoNode(inlinee.module, inlinee.name, inlinee.file, inlinee.line, inlined_at))
320+
oldlinetable = inlinee_ir.linetable
321+
extra_coverage_line = 0
326322
for oldline in 1:length(oldlinetable)
327323
entry = oldlinetable[oldline]
328324
if !coverage && coverage_by_path && is_file_tracked(entry.file)
@@ -341,8 +337,25 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
341337
end
342338
push!(linetable, newentry)
343339
end
344-
if coverage && spec.ir.stmts[1][:line] + linetable_offset != topline
345-
insert_node_here!(compact, NewInstruction(Expr(:code_coverage_effect), Nothing, topline))
340+
if coverage && inlinee_ir.stmts[1][:line] + linetable_offset != topline
341+
extra_coverage_line = topline
342+
end
343+
return linetable_offset, extra_coverage_line
344+
end
345+
346+
function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector{Any},
347+
linetable::Vector{LineInfoNode}, item::InliningTodo,
348+
boundscheck::Symbol, todo_bbs::Vector{Tuple{Int, Int}})
349+
# Ok, do the inlining here
350+
spec = item.spec::ResolvedInliningSpec
351+
sparam_vals = item.mi.sparam_vals
352+
def = item.mi.def::Method
353+
inlined_at = compact.result[idx][:line]
354+
linetable_offset::Int32 = length(linetable)
355+
topline::Int32 = linetable_offset + Int32(1)
356+
linetable_offset, extra_coverage_line = ir_inline_linetable!(linetable, item.spec.ir, def, inlined_at)
357+
if extra_coverage_line != 0
358+
insert_node_here!(compact, NewInstruction(Expr(:code_coverage_effect), Nothing, extra_coverage_line))
346359
end
347360
if def.isva
348361
nargs_def = Int(def.nargs::Int32)
@@ -839,7 +852,7 @@ function resolve_todo(todo::InliningTodo, state::InliningState, flag::UInt8)
839852
src === nothing && return compileable_specialization(et, match, effects)
840853

841854
et !== nothing && push!(et, mi)
842-
return InliningTodo(mi, src, effects)
855+
return InliningTodo(mi, retrieve_ir_for_inlining(mi, src), effects)
843856
end
844857

845858
function resolve_todo((; fully_covered, atype, cases, #=bbs=#)::UnionSplit, state::InliningState, flag::UInt8)
@@ -861,7 +874,8 @@ function validate_sparams(sparams::SimpleVector)
861874
end
862875

863876
function analyze_method!(match::MethodMatch, argtypes::Vector{Any},
864-
flag::UInt8, state::InliningState)
877+
flag::UInt8, state::InliningState,
878+
do_resolve::Bool = true)
865879
method = match.method
866880
spec_types = match.spec_types
867881

@@ -895,26 +909,20 @@ function analyze_method!(match::MethodMatch, argtypes::Vector{Any},
895909
todo = InliningTodo(mi, match, argtypes)
896910
# If we don't have caches here, delay resolving this MethodInstance
897911
# until the batch inlining step (or an external post-processing pass)
898-
state.mi_cache === nothing && return todo
912+
do_resolve && state.mi_cache === nothing && return todo
899913
return resolve_todo(todo, state, flag)
900914
end
901915

902916
function InliningTodo(mi::MethodInstance, ir::IRCode, effects::Effects)
903-
ir = copy(ir)
904917
return InliningTodo(mi, ResolvedInliningSpec(ir, effects))
905918
end
906919

907-
function InliningTodo(mi::MethodInstance, src::Union{CodeInfo, Vector{UInt8}}, effects::Effects)
908-
if !isa(src, CodeInfo)
909-
src = ccall(:jl_uncompress_ir, Any, (Any, Ptr{Cvoid}, Any), mi.def, C_NULL, src::Vector{UInt8})::CodeInfo
910-
else
911-
src = copy(src)
912-
end
913-
@timeit "inline IR inflation" begin
914-
ir = inflate_ir!(src, mi)::IRCode
915-
return InliningTodo(mi, ResolvedInliningSpec(ir, effects))
916-
end
920+
function retrieve_ir_for_inlining(mi::MethodInstance, src::Array{UInt8, 1})
921+
src = ccall(:jl_uncompress_ir, Any, (Any, Ptr{Cvoid}, Any), mi.def, C_NULL, src::Vector{UInt8})::CodeInfo
922+
return inflate_ir!(src, mi)
917923
end
924+
retrieve_ir_for_inlining(mi::MethodInstance, src::CodeInfo) = inflate_ir(src, mi)::IRCode
925+
retrieve_ir_for_inlining(mi::MethodInstance, ir::IRCode) = copy(ir)
918926

919927
function handle_single_case!(
920928
ir::IRCode, idx::Int, stmt::Expr,
@@ -1196,7 +1204,7 @@ function process_simple!(ir::IRCode, idx::Int, state::InliningState, todo::Vecto
11961204
end
11971205
end
11981206

1199-
if sig.f !== Core.invoke && is_builtin(sig)
1207+
if sig.f !== Core.invoke && sig.f !== Core.finalizer && is_builtin(sig)
12001208
# No inlining for builtins (other invoke/apply/typeassert)
12011209
return nothing
12021210
end
@@ -1213,9 +1221,10 @@ function process_simple!(ir::IRCode, idx::Int, state::InliningState, todo::Vecto
12131221
end
12141222

12151223
# TODO inline non-`isdispatchtuple`, union-split callsites?
1216-
function analyze_single_call!(
1217-
ir::IRCode, idx::Int, stmt::Expr, infos::Vector{MethodMatchInfo}, flag::UInt8,
1218-
sig::Signature, state::InliningState, todo::Vector{Pair{Int, Any}})
1224+
function compute_inlining_cases(
1225+
infos::Vector{MethodMatchInfo}, flag::UInt8,
1226+
sig::Signature, state::InliningState,
1227+
do_resolve::Bool = true)
12191228
argtypes = sig.argtypes
12201229
cases = InliningCase[]
12211230
local any_fully_covered = false
@@ -1232,7 +1241,7 @@ function analyze_single_call!(
12321241
continue
12331242
end
12341243
for match in meth
1235-
handled_all_cases &= handle_match!(match, argtypes, flag, state, cases, true)
1244+
handled_all_cases &= handle_match!(match, argtypes, flag, state, cases, true, do_resolve)
12361245
any_fully_covered |= match.fully_covers
12371246
end
12381247
end
@@ -1242,8 +1251,18 @@ function analyze_single_call!(
12421251
filter!(case::InliningCase->isdispatchtuple(case.sig), cases)
12431252
end
12441253

1245-
handle_cases!(ir, idx, stmt, argtypes_to_type(argtypes), cases,
1246-
handled_all_cases & any_fully_covered, todo, state.params)
1254+
return cases, handled_all_cases & any_fully_covered
1255+
end
1256+
1257+
function analyze_single_call!(
1258+
ir::IRCode, idx::Int, stmt::Expr, infos::Vector{MethodMatchInfo}, flag::UInt8,
1259+
sig::Signature, state::InliningState, todo::Vector{Pair{Int, Any}})
1260+
1261+
r = compute_inlining_cases(infos, flag, sig, state)
1262+
r === nothing && return nothing
1263+
cases, all_covered = r
1264+
handle_cases!(ir, idx, stmt, argtypes_to_type(sig.argtypes), cases,
1265+
all_covered, todo, state.params)
12471266
end
12481267

12491268
# similar to `analyze_single_call!`, but with constant results
@@ -1295,14 +1314,15 @@ end
12951314

12961315
function handle_match!(
12971316
match::MethodMatch, argtypes::Vector{Any}, flag::UInt8, state::InliningState,
1298-
cases::Vector{InliningCase}, allow_abstract::Bool = false)
1317+
cases::Vector{InliningCase}, allow_abstract::Bool = false,
1318+
do_resolve::Bool = true)
12991319
spec_types = match.spec_types
13001320
allow_abstract || isdispatchtuple(spec_types) || return false
13011321
# we may see duplicated dispatch signatures here when a signature gets widened
13021322
# during abstract interpretation: for the purpose of inlining, we can just skip
13031323
# processing this dispatch candidate
13041324
_any(case->case.sig === spec_types, cases) && return true
1305-
item = analyze_method!(match, argtypes, flag, state)
1325+
item = analyze_method!(match, argtypes, flag, state, do_resolve)
13061326
item === nothing && return false
13071327
push!(cases, InliningCase(spec_types, item))
13081328
return true
@@ -1417,6 +1437,48 @@ function assemble_inline_todo!(ir::IRCode, state::InliningState)
14171437
continue
14181438
end
14191439

1440+
# Handle finalizer
1441+
if sig.f === Core.finalizer
1442+
if isa(info, FinalizerInfo)
1443+
# Only inline finalizers that are known nothrow and notls.
1444+
# This avoids having to set up state for finalizer isolation
1445+
(is_nothrow(info.effects) && is_notaskstate(info.effects)) || continue
1446+
1447+
info = info.info
1448+
if isa(info, MethodMatchInfo)
1449+
infos = MethodMatchInfo[info]
1450+
elseif isa(info, UnionSplitInfo)
1451+
infos = info.matches
1452+
else
1453+
continue
1454+
end
1455+
1456+
ft = argextype(stmt.args[2], ir)
1457+
has_free_typevars(ft) && return nothing
1458+
f = singleton_type(ft)
1459+
argtypes = Vector{Any}(undef, 2)
1460+
argtypes[1] = ft
1461+
argtypes[2] = argextype(stmt.args[3], ir)
1462+
sig = Signature(f, ft, argtypes)
1463+
1464+
cases, all_covered = compute_inlining_cases(infos, UInt8(0), sig, state, false)
1465+
length(cases) == 0 && continue
1466+
if all_covered && length(cases) == 1
1467+
if isa(cases[1], InliningCase)
1468+
case1 = cases[1].item
1469+
if isa(case1, InliningTodo)
1470+
push!(stmt.args, true)
1471+
push!(stmt.args, case1.mi)
1472+
elseif isa(case1, InvokeCase)
1473+
push!(stmt.args, false)
1474+
push!(stmt.args, case1.invoke)
1475+
end
1476+
end
1477+
end
1478+
continue
1479+
end
1480+
end
1481+
14201482
# if inference arrived here with constant-prop'ed result(s),
14211483
# we can perform a specialized analysis for just this case
14221484
if isa(info, ConstCallInfo)

base/compiler/ssair/ir.jl

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -163,36 +163,6 @@ const AnySSAValue = Union{SSAValue, OldSSAValue, NewSSAValue}
163163

164164

165165
# SSA-indexed nodes
166-
167-
struct NewInstruction
168-
stmt::Any
169-
type::Any
170-
info::Any
171-
# If nothing, copy the line from previous statement
172-
# in the insertion location
173-
line::Union{Int32, Nothing}
174-
flag::UInt8
175-
176-
## Insertion options
177-
178-
# The IR_FLAG_EFFECT_FREE flag has already been computed (or forced).
179-
# Don't bother redoing so on insertion.
180-
effect_free_computed::Bool
181-
NewInstruction(@nospecialize(stmt), @nospecialize(type), @nospecialize(info),
182-
line::Union{Int32, Nothing}, flag::UInt8, effect_free_computed::Bool) =
183-
new(stmt, type, info, line, flag, effect_free_computed)
184-
end
185-
NewInstruction(@nospecialize(stmt), @nospecialize(type)) =
186-
NewInstruction(stmt, type, nothing)
187-
NewInstruction(@nospecialize(stmt), @nospecialize(type), line::Union{Nothing, Int32}) =
188-
NewInstruction(stmt, type, nothing, line, IR_FLAG_NULL, false)
189-
190-
effect_free(inst::NewInstruction) =
191-
NewInstruction(inst.stmt, inst.type, inst.info, inst.line, inst.flag | IR_FLAG_EFFECT_FREE, true)
192-
non_effect_free(inst::NewInstruction) =
193-
NewInstruction(inst.stmt, inst.type, inst.info, inst.line, inst.flag & ~IR_FLAG_EFFECT_FREE, true)
194-
195-
196166
struct InstructionStream
197167
inst::Vector{Any}
198168
type::Vector{Any}
@@ -292,6 +262,36 @@ function add!(new::NewNodeStream, pos::Int, attach_after::Bool)
292262
end
293263
copy(nns::NewNodeStream) = NewNodeStream(copy(nns.stmts), copy(nns.info))
294264

265+
struct NewInstruction
266+
stmt::Any
267+
type::Any
268+
info::Any
269+
# If nothing, copy the line from previous statement
270+
# in the insertion location
271+
line::Union{Int32, Nothing}
272+
flag::UInt8
273+
274+
## Insertion options
275+
276+
# The IR_FLAG_EFFECT_FREE flag has already been computed (or forced).
277+
# Don't bother redoing so on insertion.
278+
effect_free_computed::Bool
279+
NewInstruction(@nospecialize(stmt), @nospecialize(type), @nospecialize(info),
280+
line::Union{Int32, Nothing}, flag::UInt8, effect_free_computed::Bool) =
281+
new(stmt, type, info, line, flag, effect_free_computed)
282+
end
283+
NewInstruction(@nospecialize(stmt), @nospecialize(type)) =
284+
NewInstruction(stmt, type, nothing)
285+
NewInstruction(@nospecialize(stmt), @nospecialize(type), line::Union{Nothing, Int32}) =
286+
NewInstruction(stmt, type, nothing, line, IR_FLAG_NULL, false)
287+
NewInstruction(@nospecialize(stmt), meta::Instruction; line::Union{Int32, Nothing}=nothing) =
288+
NewInstruction(stmt, meta[:type], meta[:info], line === nothing ? meta[:line] : line, meta[:flag], true)
289+
290+
effect_free(inst::NewInstruction) =
291+
NewInstruction(inst.stmt, inst.type, inst.info, inst.line, inst.flag | IR_FLAG_EFFECT_FREE, true)
292+
non_effect_free(inst::NewInstruction) =
293+
NewInstruction(inst.stmt, inst.type, inst.info, inst.line, inst.flag & ~IR_FLAG_EFFECT_FREE, true)
294+
295295
struct IRCode
296296
stmts::InstructionStream
297297
argtypes::Vector{Any}

0 commit comments

Comments
 (0)