You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
}
```
0 commit comments