Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inference: Fix correctness and ensure termination in the presence of PhiNodes #53876

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2731,7 +2731,8 @@ function abstract_eval_phi(interp::AbstractInterpreter, phi::PhiNode, vtypes::Un
val = phi.values[i]
# N.B.: Phi arguments are restricted to not have effects, so we can drop
# them here safely.
rt = tmerge(typeinf_lattice(interp), rt, abstract_eval_special_value(interp, val, vtypes, sv).rt)
thisval = abstract_eval_special_value(interp, val, vtypes, sv).rt
rt = tmerge(typeinf_lattice(interp), rt, thisval)
end
return rt
end
Expand All @@ -2745,7 +2746,14 @@ function abstract_eval_statement(interp::AbstractInterpreter, @nospecialize(e),
if !isa(e, Expr)
if isa(e, PhiNode)
add_curr_ssaflag!(sv, IR_FLAGS_REMOVABLE)
return RTEffects(abstract_eval_phi(interp, e, vtypes, sv), Union{}, EFFECTS_TOTAL)
# Implement convergence for PhiNodes. In particular, PhiNodes need to tmerge over
# the incoming values from all iterations, but `abstract_eval_phi` will only tmerge
# over the first and last iterations. By tmerging in the current old_rt, we ensure that
# we will not lose an intermediate value.
rt = abstract_eval_phi(interp, e, vtypes, sv)
old_rt = sv.ssavaluetypes[sv.currpc]
rt = old_rt === NOT_FOUND ? rt : tmerge(typeinf_lattice(interp), old_rt, rt)
return RTEffects(rt, Union{}, EFFECTS_TOTAL)
end
(; rt, exct, effects) = abstract_eval_special_value(interp, e, vtypes, sv)
else
Expand Down
5 changes: 1 addition & 4 deletions base/compiler/inferencestate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -691,10 +691,7 @@ function record_ssa_assign!(𝕃ᵢ::AbstractLattice, ssa_id::Int, @nospecialize
for r in frame.ssavalue_uses[ssa_id]
if was_reached(frame, r)
usebb = block_for_inst(frame.cfg, r)
# We're guaranteed to visit the statement if it's in the current
# basic block, since SSA values can only ever appear after their
# def.
if usebb != frame.currbb
if usebb != frame.currbb || r < ssa_id
push!(W, usebb)
end
end
Expand Down
28 changes: 28 additions & 0 deletions test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5645,3 +5645,31 @@ let t = ntuple(i -> i % 8 == 1 ? Int64 : Float64, 4000)
@test only(Base.return_types(Base.promote_typeof, t)) == Type{Float64}
@test only(Base.return_types(vcat, t)) == Vector{Float64}
end

# Infinite loop in inference on SSA assignment
const stop_infinite_loop::Base.Threads.Atomic{Bool} = Base.Threads.Atomic{Bool}(false)
function gen_infinite_loop_ssa_generator(world::UInt, source, _)
ci = make_codeinfo(Any[
# Block 1
(),
# Block 2
PhiNode(Int32[1, 5], Any[SSAValue(1), SSAValue(3)]),
Expr(:call, tuple, SSAValue(2)),
Expr(:call, getindex, GlobalRef(@__MODULE__, :stop_infinite_loop)),
GotoIfNot(SSAValue(4), 2),
# Block 3
ReturnNode(SSAValue(2))
]; slottypes=Any[Any])
ci.slotnames = Symbol[:var"#self#"]
ci
end

@eval function gen_infinite_loop_ssa()
$(Expr(:meta, :generated, gen_infinite_loop_ssa_generator))
$(Expr(:meta, :generated_only))
#= no body =#
end

# We want to make sure that both this returns `Tuple` and that
# it doesn't infinite loop inside inference.
@test Core.Compiler.return_type(gen_infinite_loop_ssa, Tuple{}) === Tuple
Loading