Commit b682f49
authored
Inference: propagate struct initialization info on
When a variable has a field set with `setfield!(var, field, value)`,
inference now assumes that this specific field is defined and may for
example constant-propagate `isdefined(var, field)` as `true`.
`PartialStruct`, the lattice element used to encode this information,
still has a few limitations in terms of what it may represent (it cannot
represent mutable structs with non-contiguously defined fields yet),
further work on extending it would increase the impact of this change.
Consider the following function:
```julia
julia> function f()
a = A(1)
setfield!(a, :y, 2)
invokelatest(identity, a)
isdefined(a, :y) && return 1.0
a
end
f (generic function with 1 method)
```
Here is before on `master`:
```julia
julia> @code_typed f()
CodeInfo(
1 ─ %1 = %new(Main.A, 1)::A
│ builtin Main.setfield!(%1, :y, 2)::Int64
│ dynamic builtin (Core._call_latest)(identity, %1)::Any
│ %4 = builtin Main.isdefined(%1, :y)::Bool
└── goto mmtk#3 if not %4
2 ─ return 1.0
3 ─ return %1
) => Union{Float64, A}
```
And after this PR:
```julia
julia> @code_typed f()
CodeInfo(
1 ─ %1 = %new(Main.A, 1)::A
│ builtin Main.setfield!(%1, :y, 2)::Int64
│ dynamic builtin (Core._call_latest)(identity, %1)::Any
└── return 1.0
) => Float64
```
---------
Co-authored-by: Cédric Belmant <[email protected]>setfield! (JuliaLang#57222)1 parent 4ebb50b commit b682f49
2 files changed
+27
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2665 | 2665 | | |
2666 | 2666 | | |
2667 | 2667 | | |
2668 | | - | |
2669 | | - | |
2670 | | - | |
| 2668 | + | |
| 2669 | + | |
| 2670 | + | |
| 2671 | + | |
| 2672 | + | |
| 2673 | + | |
| 2674 | + | |
| 2675 | + | |
| 2676 | + | |
| 2677 | + | |
| 2678 | + | |
| 2679 | + | |
2671 | 2680 | | |
2672 | 2681 | | |
2673 | | - | |
| 2682 | + | |
| 2683 | + | |
| 2684 | + | |
| 2685 | + | |
2674 | 2686 | | |
2675 | 2687 | | |
2676 | 2688 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2042 | 2042 | | |
2043 | 2043 | | |
2044 | 2044 | | |
| 2045 | + | |
| 2046 | + | |
| 2047 | + | |
| 2048 | + | |
| 2049 | + | |
| 2050 | + | |
| 2051 | + | |
| 2052 | + | |
| 2053 | + | |
| 2054 | + | |
| 2055 | + | |
0 commit comments