@@ -1594,11 +1594,11 @@ function try_resolve_finalizer!(ir::IRCode, alloc_idx::Int, finalizer_idx::Int,
15941594 inlining:: InliningState , lazydomtree:: LazyDomtree ,
15951595 lazypostdomtree:: LazyPostDomtree , @nospecialize (info:: CallInfo ))
15961596 # For now, require that:
1597- # 1. The allocation dominates the finalizer registration
1597+ # 1. The allocation dominates the finalizer registration.
15981598 # 2. The insertion block for the finalizer is the post-dominator of all
15991599 # uses (including the finalizer registration).
16001600 # 3. The path from the finalizer registration to the finalizer inlining
1601- # location is nothrow
1601+ # location is nothrow, otherwise we need to insert `Core._cancel_finalizer` too.
16021602 #
16031603 # TODO : We could relax the check 2, by inlining the finalizer multiple times.
16041604
@@ -1677,8 +1677,8 @@ function try_resolve_finalizer!(ir::IRCode, alloc_idx::Int, finalizer_idx::Int,
16771677 attach_after = insert_idx != = nothing
16781678 flag = info isa FinalizerInfo ? flags_for_effects (info. effects) : IR_FLAG_NULL
16791679 alloc_obj = finalizer_stmt. args[3 ]
1680- cancel_registration = current_task_ssa != = nothing
1681- if cancel_registration
1680+ cancellation_required = current_task_ssa != = nothing
1681+ if cancellation_required
16821682 lookup_idx_ssa = SSAValue (finalizer_idx)
16831683 finalize_call = Expr (:call , GlobalRef (Core, :_cancel_finalizer ), alloc_obj, current_task_ssa, lookup_idx_ssa)
16841684 newinst = add_flag (NewInstruction (finalize_call, Nothing), flag)
@@ -1702,7 +1702,7 @@ function try_resolve_finalizer!(ir::IRCode, alloc_idx::Int, finalizer_idx::Int,
17021702 newinst = add_flag (NewInstruction (Expr (:call , argexprs... ), Nothing), flag)
17031703 insert_node! (ir, loc, newinst, attach_after)
17041704 end
1705- if ! cancel_registration
1705+ if ! cancellation_required
17061706 # Erase the call to `finalizer`
17071707 ir[SSAValue (finalizer_idx)][:stmt ] = nothing
17081708 end
@@ -1721,10 +1721,7 @@ function sroa_mutables!(ir::IRCode, defuses::IdDict{Int,Tuple{SPCSet,SSADefUse}}
17211721 finalizer_useidx = useidx
17221722 end
17231723 end
1724- if finalizer_useidx === nothing || inlining === nothing
1725- return true
1726- end
1727- return finalizer_useidx
1724+ return something (finalizer_useidx, true )
17281725 end
17291726 for (defidx, (intermediaries, defuse)) in defuses
17301727 # Find the type for this allocation
@@ -1749,28 +1746,28 @@ function sroa_mutables!(ir::IRCode, defuses::IdDict{Int,Tuple{SPCSet,SSADefUse}}
17491746 all_eliminated = all_forwarded = true
17501747 if nleaves ≠ nuses_total
17511748 finalizer_useidx = find_finalizer_useidx (defuse)
1752- if finalizer_useidx isa Int
1749+ if finalizer_useidx isa Int && inlining != = nothing
17531750 nargs = length (ir. argtypes) # COMBAK this might need to be `Int(opt.src.nargs)`
17541751 estate = EscapeAnalysis. analyze_escapes (ir, nargs, 𝕃ₒ, get_escape_cache (inlining. interp))
17551752 einfo = estate[SSAValue (defidx)]
1756- if EscapeAnalysis. has_no_escape (einfo)
1753+ if EscapeAnalysis. has_no_escape (EscapeAnalysis . ignore_thrownescapes ( einfo) )
17571754 already = BitSet (use. idx for use in defuse. uses)
17581755 for idx = einfo. Liveness
17591756 if idx ∉ already
17601757 push! (defuse. uses, SSAUse (:EALiveness , idx))
17611758 end
17621759 end
17631760 finalizer_idx = defuse. uses[finalizer_useidx]. idx
1764- try_resolve_finalizer! (ir, defidx, finalizer_idx, defuse, inlining:: InliningState ,
1761+ try_resolve_finalizer! (ir, defidx, finalizer_idx, defuse, inlining,
17651762 lazydomtree, lazypostdomtree, ir[SSAValue (finalizer_idx)][:info ])
17661763 end
17671764 end
17681765 continue
17691766 else
17701767 finalizer_useidx = find_finalizer_useidx (defuse)
1771- if finalizer_useidx isa Int
1768+ if finalizer_useidx isa Int && inlining != = nothing
17721769 finalizer_idx = defuse. uses[finalizer_useidx]. idx
1773- try_resolve_finalizer! (ir, defidx, finalizer_idx, defuse, inlining:: InliningState ,
1770+ try_resolve_finalizer! (ir, defidx, finalizer_idx, defuse, inlining,
17741771 lazydomtree, lazypostdomtree, ir[SSAValue (finalizer_idx)][:info ])
17751772 deleteat! (defuse. uses, finalizer_useidx)
17761773 all_eliminated = all_forwarded = false # can't eliminate `setfield!` calls safely
0 commit comments