Skip to content

Commit f9d3e16

Browse files
committed
Fix SROA miscompile in large functions
During the development of #46775, we saw a miscompile in the compiler, specifically, we saw SROA failing to provide a proper PHI nest. The root cause of this turned out to be an incorrect dominance query. In particular, during incremental compaction, the non-compacted portion of the IncrementalCompact cfg is allocated, but not valid, so we must not query it for basic block information. Unfortunately, I don't really have a good test case for this. This code has grown mostly organically for a few years, and I think it's probably overdue for an overhaul.
1 parent e758982 commit f9d3e16

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

base/compiler/ssair/ir.jl

+11-3
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,18 @@ function cfg_delete_edge!(cfg::CFG, from::Int, to::Int)
2828
nothing
2929
end
3030

31+
function bb_ordering()
32+
lt=(<=)
33+
by=x->first(x.stmts)
34+
ord(lt, by, nothing, Forward)
35+
end
36+
3137
function block_for_inst(index::Vector{Int}, inst::Int)
3238
return searchsortedfirst(index, inst, lt=(<=))
3339
end
3440

3541
function block_for_inst(index::Vector{BasicBlock}, inst::Int)
36-
return searchsortedfirst(index, BasicBlock(StmtRange(inst, inst)), by=x->first(x.stmts), lt=(<=))-1
42+
return searchsortedfirst(index, BasicBlock(StmtRange(inst, inst)), bb_ordering())-1
3743
end
3844

3945
block_for_inst(cfg::CFG, inst::Int) = block_for_inst(cfg.index, inst)
@@ -672,7 +678,8 @@ end
672678
function block_for_inst(compact::IncrementalCompact, idx::SSAValue)
673679
id = idx.id
674680
if id < compact.result_idx # if ssa within result
675-
return block_for_inst(compact.result_bbs, id)
681+
return searchsortedfirst(compact.result_bbs, BasicBlock(StmtRange(id, id)),
682+
1, compact.active_result_bb, bb_ordering())-1
676683
else
677684
return block_for_inst(compact.ir.cfg, id)
678685
end
@@ -681,7 +688,8 @@ end
681688
function block_for_inst(compact::IncrementalCompact, idx::OldSSAValue)
682689
id = idx.id
683690
if id < compact.idx # if ssa within result
684-
return block_for_inst(compact.result_bbs, compact.ssa_rename[id])
691+
id = compact.ssa_rename[id]
692+
return block_for_inst(compact, SSAValue(id))
685693
else
686694
return block_for_inst(compact.ir.cfg, id)
687695
end

0 commit comments

Comments
 (0)