Skip to content

Commit fcd62da

Browse files
authored
cfg_simplify: Avoid ambiguous magic number (#52111)
This code was using the sentinel value -1 as a special marker in addition to the negative BB indices. That ambiguity was causing `cfg_simplify!` to fail on functions that merge any BB into the first basic block. This change is to use `typemin(Int)` as a marker instead. Fixes #52058
1 parent 16e61e2 commit fcd62da

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

base/compiler/ssair/passes.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,7 +2029,7 @@ function cfg_simplify!(ir::IRCode)
20292029
i = popfirst!(worklist)
20302030
# Drop blocks that will be merged away
20312031
if merge_into[i] != 0
2032-
bb_rename_succ[i] = -1
2032+
bb_rename_succ[i] = typemin(Int)
20332033
end
20342034
# Mark dropped blocks for fixup
20352035
if !isempty(searchsorted(dropped_bbs, i))
@@ -2049,7 +2049,7 @@ function cfg_simplify!(ir::IRCode)
20492049
# we have to schedule that block next
20502050
while merged_succ[curr] != 0
20512051
if bb_rename_succ[curr] == 0
2052-
bb_rename_succ[curr] = -1
2052+
bb_rename_succ[curr] = typemin(Int)
20532053
end
20542054
curr = merged_succ[curr]
20552055
end
@@ -2089,9 +2089,9 @@ function cfg_simplify!(ir::IRCode)
20892089
resolved_all = true
20902090
for bb in dropped_bbs
20912091
obb = bb_rename_succ[bb]
2092-
if obb < -1
2092+
if obb < 0 && obb != typemin(Int)
20932093
nsucc = bb_rename_succ[-obb]
2094-
if nsucc == -1
2094+
if nsucc == typemin(Int)
20952095
nsucc = -merge_into[-obb]
20962096
end
20972097
bb_rename_succ[bb] = nsucc
@@ -2106,6 +2106,8 @@ function cfg_simplify!(ir::IRCode)
21062106
if bb_rename_succ[i] == 0
21072107
bb_rename_succ[i] = -1
21082108
bb_rename_pred[i] = -2
2109+
elseif bb_rename_succ[i] == typemin(Int)
2110+
bb_rename_succ[i] = -1
21092111
end
21102112
end
21112113

test/compiler/irpasses.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,4 +1569,19 @@ let m = Meta.@lower 1 + 1
15691569
Core.Compiler.verify_ir(ir)
15701570
end
15711571

1572+
function f_with_merge_to_entry_block()
1573+
while true
1574+
i = @noinline rand(Int)
1575+
if @noinline isodd(i)
1576+
return i
1577+
end
1578+
end
1579+
end
1580+
1581+
let (ir, _) = only(Base.code_ircode(f_with_merge_to_entry_block))
1582+
Core.Compiler.verify_ir(ir)
1583+
ir = Core.Compiler.cfg_simplify!(ir)
1584+
Core.Compiler.verify_ir(ir)
1585+
end
1586+
15721587
# JET.test_opt(Core.Compiler.cfg_simplify!, (Core.Compiler.IRCode,))

0 commit comments

Comments
 (0)