Skip to content

Commit df09f67

Browse files
authored
irverify: Enforce invariant that PhiNodes are at the beginning of a BB (#50158)
We have an invariant that all PhiNodes are at the beginning of a BasicBlock (only possible interrupted by a `nothing`) and we rely on this in various places for correctness. However, we did not actually verify this invariant.
1 parent 8a1b642 commit df09f67

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

base/compiler/ssair/verify.jl

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,20 +187,30 @@ function verify_ir(ir::IRCode, print::Bool=true,
187187
end
188188
end
189189
end
190+
lastbb = 0
191+
is_phinode_block = false
190192
for (bb, idx) in bbidxiter(ir)
193+
if bb != lastbb
194+
is_phinode_block = true
195+
lastbb = bb
196+
end
191197
# We allow invalid IR in dead code to avoid passes having to detect when
192198
# they're generating dead code.
193199
bb_unreachable(domtree, bb) && continue
194200
stmt = ir.stmts[idx][:inst]
195201
stmt === nothing && continue
196202
if isa(stmt, PhiNode)
203+
if !is_phinode_block
204+
@verify_error "φ node $idx is not at the beginning of the basic block $bb"
205+
error("")
206+
end
197207
@assert length(stmt.edges) == length(stmt.values)
198208
for i = 1:length(stmt.edges)
199209
edge = stmt.edges[i]
200210
for j = (i+1):length(stmt.edges)
201211
edge′ = stmt.edges[j]
202212
if edge == edge′
203-
# TODO: Move `unique` to Core.Compiler. For now we assume the predecessor list is
213+
# TODO: Move `unique` to Core.Compiler. For now we assume the predecessor list is always unique.
204214
@verify_error "Edge list φ node $idx in bb $bb not unique (double edge?)"
205215
error("")
206216
end
@@ -233,7 +243,14 @@ function verify_ir(ir::IRCode, print::Bool=true,
233243
end
234244
check_op(ir, domtree, val, Int(edge), last(ir.cfg.blocks[stmt.edges[i]].stmts)+1, idx, print, false, i, allow_frontend_forms)
235245
end
236-
elseif isa(stmt, PhiCNode)
246+
continue
247+
elseif stmt === nothing
248+
# Nothing to do
249+
continue
250+
end
251+
252+
is_phinode_block = false
253+
if isa(stmt, PhiCNode)
237254
for i = 1:length(stmt.values)
238255
val = stmt.values[i]
239256
if !isa(val, SSAValue)

0 commit comments

Comments
 (0)