julia
1d8f7e07 - inference: implement type-based alias analysis to refine constrained field (#41199)

Commit
3 years ago
inference: implement type-based alias analysis to refine constrained field (#41199) This commit tries to propagate constraints imposed on object fields, e.g.: ```julia struct SomeX{T} x::Union{Nothing,T} end mutable struct MutableSomeX{T} const x::Union{Nothing,T} end let # o1::SomeX{T}, o2::MutableSomeX{T} if !isnothing(o1.x) # now inference knows `o1.x::T` here ... if !isnothing(o2.x) # now inference knows `o2.x::T` here ... end end end ``` The idea is that we can make `isa` and `===` propagate constraint imposed on an object field if the _identity_ of that object. We can have such a lattice element that wraps return type of abstract `getfield` call together with the object _identity_, and then we can form a conditional constraint that propagates the refinement information imposed on the object field when we see `isa`/`===` applied the return value of the preceding `getfield` call. So this PR defines the new lattice element called `MustAlias` (and also `InterMustAlias`, which just works in a similar way to `InterConditional`), which may be formed upon `getfield` inference to hold the retrieved type of the field and track the _identity_ of the object (in inference, "object identity" can be represented as a `SlotNumber`). This PR also implements the new logic in `abstract_call_builtin` so that `isa` and `===` can form a conditional constraint (i.e. `Conditional`) from `MustAlias`-argument that may later refine the wrapped object to `PartialStruct` that holds the refined field type information. One important note here is, `MustAlias` expects the invariant that the field of wrapped slot object never changes. The biggest limitation with this invariant is that it can't propagate constraints imposed on mutable fields, because inference currently doesn't have a precise (per-object) knowledge of memory effect.
Author
Parents
Loading