Skip to content

RFC: stop wrapping unreachable statements in Const #58581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Compiler/src/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1245,8 +1245,7 @@ function convert_to_ircode(ci::CodeInfo, sv::OptimizationState)
empty!(sv.cfg.blocks[block].succs)

if !(idx < length(code) && isa(code[idx + 1], ReturnNode) && !isdefined((code[idx + 1]::ReturnNode), :val))
# Any statements from here to the end of the block have been wrapped in Core.Const(...)
# by type inference (effectively deleting them). Only task left is to replace the block
# Any statements from here to the end of the block are unreachable. Replace the block
# terminator with an explicit `unreachable` marker.

if block_end > idx
Expand Down
4 changes: 2 additions & 2 deletions Compiler/src/ssair/slot2ssa.jl
Original file line number Diff line number Diff line change
Expand Up @@ -638,10 +638,10 @@ function construct_ssa!(ci::CodeInfo, ir::IRCode, sv::OptimizationState,
end
phiblocks = iterated_dominance_frontier(cfg, live, domtree)
for block in phiblocks
varstate = sv.bb_vartables[block]
varstate === nothing && continue
push!(phi_slots[block], idx)
node = PhiNode()
varstate = sv.bb_vartables[block]
@assert varstate !== nothing
vt = varstate[idx]
ssaval = NewSSAValue(insert_node!(ir,
first_insert_for_bb(code, cfg, block), NewInstruction(node, vt.typ)).id - length(ir.stmts))
Expand Down
3 changes: 1 addition & 2 deletions Compiler/src/typeinfer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -870,9 +870,8 @@ function type_annotate!(interp::AbstractInterpreter, sv::InferenceState)
if is_meta_expr(expr) # keep any lexically scoped expressions
ssavaluetypes[i] = Any # 3
else
ssavaluetypes[i] = Bottom # 3
# annotate that this statement actually is dead
stmts[i] = Const(expr)
ssavaluetypes[i] = Bottom # 3
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions Compiler/test/ssair.jl
Original file line number Diff line number Diff line change
Expand Up @@ -721,18 +721,18 @@ end
return x
end
let
# At least some statements should have been found to be statically unreachable and wrapped in Const(...)::Union{}
# At least some statements should have been found to be statically unreachable
unopt = code_typed1(f_with_maybe_nonbool_cond, (Int, Bool); optimize=false)
@test any(j -> isa(unopt.code[j], Core.Const) && unopt.ssavaluetypes[j] == Union{}, 1:length(unopt.code))
@test any(j -> unopt.ssavaluetypes[j] === Union{}, 1:length(unopt.code))

# Any GotoIfNot destinations after IRCode conversion should not be statically unreachable
ircode = first(only(Base.code_ircode(f_with_maybe_nonbool_cond, (Int, Bool); optimize_until="CC: CONVERT")))
for i = 1:length(ircode.stmts)
expr = ircode.stmts[i][:stmt]
if isa(expr, GotoIfNot)
# If this statement is Core.Const(...)::Union{}, that means this code was not reached
@test !(isa(ircode.stmts[i+1][:stmt], Core.Const) && (unopt.ssavaluetypes[i+1] === Union{}))
@test !(isa(ircode.stmts[expr.dest][:stmt], Core.Const) && (unopt.ssavaluetypes[expr.dest] === Union{}))
# GotoIfNot branch destinations should not be unreachable
@test !(unopt.ssavaluetypes[i+1] === Union{})
@test !(unopt.ssavaluetypes[expr.dest] === Union{})
end
end
end
Expand Down