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

ir: Don't accidentally consider pending statements from previous block #52863

Merged
merged 1 commit into from
Jan 12, 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
24 changes: 19 additions & 5 deletions base/compiler/ssair/ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1340,8 +1340,8 @@ function kill_edge!(compact::IncrementalCompact, active_bb::Int, from::Int, to::
else
stmts = compact.ir.cfg.blocks[to].stmts
for stmt in CompactPeekIterator(compact, first(stmts), last(stmts))
stmt === nothing && continue
isa(stmt, PhiNode) || break
is_valid_phiblock_stmt(stmt) || break
isa(stmt, PhiNode) || continue
i = findfirst(x::Int32->x==from, stmt.edges)
if i !== nothing
deleteat!(stmt.edges, i)
Expand Down Expand Up @@ -1684,13 +1684,27 @@ struct CompactPeekIterator
compact::IncrementalCompact
start_idx::Int
end_idx::Int
include_stmts_before_start::Bool
end
CompactPeekIterator(compact::IncrementalCompact, start_idx::Int, end_idx::Int) =
CompactPeekIterator(compact, start_idx, end_idx, false)


function CompactPeekIterator(compact::IncrementalCompact, start_idx::Int)
return CompactPeekIterator(compact, start_idx, 0)
end

entry_at_idx(entry::NewNodeInfo, idx::Int) = entry.attach_after ? entry.pos == idx - 1 : entry.pos == idx
function entry_at_idx(entry::NewNodeInfo, idx::Int, start_idx::Int, include_stmts_before_start::Bool)
if entry.attach_after
if !include_stmts_before_start
entry.pos >= start_idx || return false
end
return entry.pos == idx - 1
else
return entry.pos == idx
end
end

function iterate(it::CompactPeekIterator, (idx, aidx, bidx)::NTuple{3, Int}=(it.start_idx, it.compact.new_nodes_idx, 1))
if it.end_idx > 0 && idx > it.end_idx
return nothing
Expand All @@ -1702,15 +1716,15 @@ function iterate(it::CompactPeekIterator, (idx, aidx, bidx)::NTuple{3, Int}=(it.
if compact.new_nodes_idx <= length(compact.perm)
new_nodes = compact.ir.new_nodes
for eidx in aidx:length(compact.perm)
if entry_at_idx(new_nodes.info[compact.perm[eidx]], idx)
if entry_at_idx(new_nodes.info[compact.perm[eidx]], idx, it.start_idx, it.include_stmts_before_start)
entry = new_nodes.stmts[compact.perm[eidx]]
return (entry[:stmt], (idx, eidx+1, bidx))
end
end
end
if !isempty(compact.pending_perm)
for eidx in bidx:length(compact.pending_perm)
if entry_at_idx(compact.pending_nodes.info[compact.pending_perm[eidx]], idx)
if entry_at_idx(compact.pending_nodes.info[compact.pending_perm[eidx]], idx, it.start_idx, it.include_stmts_before_start)
entry = compact.pending_nodes.stmts[compact.pending_perm[eidx]]
return (entry[:stmt], (idx, aidx, eidx+1))
end
Expand Down
24 changes: 24 additions & 0 deletions test/compiler/irpasses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1740,3 +1740,27 @@ end
return 0
end
@test code_typed(f52703)[1][2] === Int

# Issue #52858 - compaction gets confused by pending node
let code = Any[
# Block 1
GotoIfNot(true, 6),
# Block 2
Expr(:call, println, 1),
Expr(:call, Base.inferencebarrier, true),
GotoIfNot(SSAValue(3), 6),
# Block 3
nothing,
# Block 4
PhiNode(Int32[1, 4, 5], Any[1, 2, 3]),
ReturnNode(SSAValue(6))
]
ir = make_ircode(code)
Core.Compiler.insert_node!(ir, SSAValue(5),
Core.Compiler.NewInstruction(
Expr(:call, println, 2), Nothing, Int32(1)),
#= attach_after = =# true)
ir = Core.Compiler.compact!(ir, true)
@test Core.Compiler.verify_ir(ir) === nothing
@test count(x->isa(x, GotoIfNot), ir.stmts.stmt) == 1
end