Skip to content

Commit 9cc21ea

Browse files
authored
optimizer: handle EnterNode with catch_dest == 0 (JuliaLang#56686)
In some parts of the optimizer code, such as `cfg_simplify!` and irinterp, it is assumed that `EnterNode` always has `catch_dest ≠ 0`, but this assumption is incorrect. This commit fixes those cases.
1 parent 6fb8b01 commit 9cc21ea

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

src/ssair/irinterp.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,10 @@ function process_terminator!(@nospecialize(stmt), bb::Int, bb_ip::BitSetBoundedM
249249
return backedge
250250
elseif isa(stmt, EnterNode)
251251
dest = stmt.catch_dest
252-
@assert dest > bb
253-
push!(bb_ip, dest)
252+
if dest 0
253+
@assert dest > bb
254+
push!(bb_ip, dest)
255+
end
254256
push!(bb_ip, bb+1)
255257
return false
256258
else

src/ssair/passes.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2393,8 +2393,10 @@ function cfg_simplify!(ir::IRCode)
23932393
end
23942394
elseif isa(terminator, EnterNode)
23952395
catchbb = terminator.catch_dest
2396-
if bb_rename_succ[catchbb] == 0
2397-
push!(worklist, catchbb)
2396+
if catchbb 0
2397+
if bb_rename_succ[catchbb] == 0
2398+
push!(worklist, catchbb)
2399+
end
23982400
end
23992401
elseif isa(terminator, GotoNode) || isa(terminator, ReturnNode)
24002402
# No implicit fall through. Schedule from work list.

test/irpasses.jl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1816,7 +1816,34 @@ function f53521()
18161816
end
18171817
end
18181818
end
1819-
@test code_typed(f53521)[1][2] === Nothing
1819+
let (ir,rt) = only(Base.code_ircode(f53521, ()))
1820+
@test rt == Nothing
1821+
Compiler.verify_ir(ir)
1822+
Compiler.cfg_simplify!(ir)
1823+
Compiler.verify_ir(ir)
1824+
end
1825+
1826+
Base.@assume_effects :foldable Base.@constprop :aggressive function f53521(x::Int, ::Int)
1827+
VALUE = ScopedValue(x)
1828+
@with VALUE => 2 begin
1829+
for i = 1
1830+
@with VALUE => 3 begin
1831+
local v
1832+
try
1833+
v = sin(VALUE[])
1834+
catch
1835+
v = nothing
1836+
end
1837+
return v
1838+
end
1839+
end
1840+
end
1841+
end
1842+
let (ir,rt) = only(Base.code_ircode((Int,)) do y
1843+
f53521(1, y)
1844+
end)
1845+
@test rt == Union{Nothing,Float64}
1846+
end
18201847

18211848
# Test that adce_pass! sets Refined on PhiNode values
18221849
let code = Any[

0 commit comments

Comments
 (0)