Skip to content

Commit

Permalink
Fix condition for unreachable code in IRCode conversion
Browse files Browse the repository at this point in the history
This is a partial back-port of JuliaLang#50924, where we discovered that the
optimizer would ignore:
  1. must-throw `%XX = SlotNumber(_)` statements
  2. must-throw `goto #bb if not %x` statements
when re-building the CFG during IRCode conversion.

This is mostly harmless, except that in the case of (1) we can accidentally
fall through the statically deleted (`Const()`-wrapped) code from inference
and observe a control-flow edge that never should have existed, such as an
edge into a catch block. Such an edge is invalid semantically and breaks our
SSA conversion.

This one-line change fixes (1) but not (2), which is enough for IR validity.
We should follow-up with a tweak to `compute_basic_blocks` to enforce this
requirement.
  • Loading branch information
topolarity committed Feb 29, 2024
1 parent 7790d6f commit c8d1ad8
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion base/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ function convert_to_ircode(ci::CodeInfo, sv::OptimizationState)
idx += 1
prevloc = codeloc
end
if code[idx] isa Expr && ssavaluetypes[idx] === Union{}
if ssavaluetypes[idx] === Union{} && !isnothing(code[idx]) && !(code[idx] isa Core.Const)
if !(idx < length(code) && isa(code[idx + 1], ReturnNode) && !isdefined((code[idx + 1]::ReturnNode), :val))
# insert unreachable in the same basic block after the current instruction (splitting it)
insert!(code, idx + 1, ReturnNode())
Expand Down

0 comments on commit c8d1ad8

Please sign in to comment.