Skip to content

Conversation

topolarity
Copy link
Member

@topolarity topolarity commented Aug 27, 2024

This PR implements the "Path-Convergence Criterion" for SSA / ϕ-nodes as part of type-inference.

The VarState.ssadef field corresponds to the "reaching definition" of the slot in SSA form, which allows us to conveniently reason about the identity of a slot across multiple program points. If the reaching def is equal at two program points, then the slot contents are guaranteed to be egal (i.e. x₀ === x₁)

Tasks:

  • Split StateRefinement change to separate PR
  • Split VarTable plumbing to separate PR
  • Remove old Conditional invalidation logic
  • Add tests

Fixes #55548 (alternative to #55551), by having Conditional remember the reaching definition that it narrows.

This change effectively computes the SSA / ϕ-nodes for program slots as
part of type-inference, using the "path-convergence criterion" for SSA.

This allows us to conveniently reason about slot identity (in typical
SSA fashion) without having to quadratically expand all of our SSA type
state over the CFG.
Another change will probably be needed to make sure that `MustAlias`
itself is invalidated by `.defssa`, but this is enough to make sure that
any Conditional derived from MustAlias works correctly.
Copy link
Member

@aviatesk aviatesk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks much better than my PR in general.
Still I would like to add the test case from it, i.e.

# JuliaLang/julia#55548: invalidate stale slot wrapper types in `ssavaluetypes`
_issue55548_proj1(a, b) = a
function issue55548(a)
    a = Base.inferencebarrier(a)::Union{Int64,Float64}
    if _issue55548_proj1(isa(a, Int64), (a = Base.inferencebarrier(1.0)::Union{Int64,Float64}; true))
        return a
    end
    return 2
end
@test Float64 <: Base.infer_return_type(issue55548, (Int,))

@aviatesk
Copy link
Member

@nanosoldier runbenchmarks("inference", vs=":master")

@topolarity
Copy link
Member Author

topolarity commented Aug 29, 2024

Thanks for the test case and review @aviatesk

fwiw, there's also a precision regression that I encountered w/ this PR:

function foo()
    value = @noinline rand((true, false, 1))
    is_bool = value isa Bool
    if inferencebarrier(@noinline rand(Bool))
        value = 1.0
        is_bool = false
    end
    # at this merge point, only the "false half" of the Conditional should be invalidated
    return is_bool ? value : false
end
@assert only(Base.return_types(foo, ())) === Bool # fails w/ PR

but I think this can be mostly fixed by widening the Conditional at the merge point and giving it a new .ssadef to match

@nanosoldier
Copy link
Collaborator

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here.

@vtjnash
Copy link
Member

vtjnash commented Nov 18, 2024

bump? are we able to land this and backport to v1.11/v1.10?

@vtjnash vtjnash added compiler:inference Type inference bugfix This change fixes an existing bug labels Nov 18, 2024
@serenity4 serenity4 force-pushed the ct/fix-55548 branch 2 times, most recently from df333d8 to e614050 Compare August 6, 2025 04:47
@serenity4
Copy link
Member

As discussed with @topolarity, I will take over this PR and finish it so we can have a fix for the underlying issue.

It seems like most of the core functionality works well at the moment, so I believe it's mostly a matter of finishing up the design and related refactors, and address performance regressions.

@serenity4
Copy link
Member

@nanosoldier runbenchmarks("inference", vs=":master")

@nanosoldier
Copy link
Collaborator

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here.

@serenity4
Copy link
Member

@nanosoldier runbenchmarks("inference", vs=":master")

@nanosoldier
Copy link
Collaborator

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here.

@serenity4
Copy link
Member

@nanosoldier runbenchmarks("inference", vs=":master")

@nanosoldier
Copy link
Collaborator

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix This change fixes an existing bug compiler:inference Type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Conditional lattice element is semantically broken in the presence of SSAValues
5 participants