Skip to content

optimizer: don't insert :throw_undef_if_not for defined slots #55600

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

Merged
merged 1 commit into from
Aug 29, 2024
Merged
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
9 changes: 6 additions & 3 deletions base/compiler/ssair/slot2ssa.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ function fixup_slot!(ir::IRCode, ci::CodeInfo, idx::Int, slot::Int, @nospecializ
insert_node!(ir, idx, NewInstruction(
Expr(:throw_undef_if_not, ci.slotnames[slot], false), Any))
return UNDEF_TOKEN
elseif has_flag(ir.stmts[idx], IR_FLAG_NOTHROW)
# if the `isdefined`-ness of this slot is guaranteed by abstract interpretation,
# there is no need to form a `:throw_undef_if_not`
elseif def_ssa !== true
insert_node!(ir, idx, NewInstruction(
Expr(:throw_undef_if_not, ci.slotnames[slot], def_ssa), Any))
Expand Down Expand Up @@ -153,12 +156,12 @@ end

function fixup_uses!(ir::IRCode, ci::CodeInfo, code::Vector{Any}, uses::Vector{Int}, slot::Int, @nospecialize(ssa))
for use in uses
code[use] = fixemup!(x::SlotNumber->slot_id(x)==slot, stmt::SlotNumber->(ssa, true), ir, ci, use, code[use])
code[use] = fixemup!(x::SlotNumber->slot_id(x)==slot, ::SlotNumber->Pair{Any,Any}(ssa, true), ir, ci, use, code[use])
end
end

function rename_uses!(ir::IRCode, ci::CodeInfo, idx::Int, @nospecialize(stmt), renames::Vector{Pair{Any, Any}})
return fixemup!(stmt::SlotNumber->true, stmt::SlotNumber->renames[slot_id(stmt)], ir, ci, idx, stmt)
return fixemup!(::SlotNumber->true, x::SlotNumber->renames[slot_id(x)], ir, ci, idx, stmt)
end

# maybe use expr_type?
Expand Down Expand Up @@ -656,7 +659,7 @@ function construct_ssa!(ci::CodeInfo, ir::IRCode, sv::OptimizationState,
visited = BitSet()
new_nodes = ir.new_nodes
@timeit "SSA Rename" while !isempty(worklist)
(item::Int, pred, incoming_vals) = pop!(worklist)
(item, pred, incoming_vals) = pop!(worklist)
if sv.bb_vartables[item] === nothing
continue
end
Expand Down
8 changes: 6 additions & 2 deletions test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6051,7 +6051,7 @@ end |> Core.Compiler.is_nothrow
end |> Core.Compiler.is_nothrow

# refine `undef` information from `@isdefined` check
@test Base.infer_effects((Bool,Int)) do c, x
function isdefined_nothrow(c, x)
local val
if c
val = x
Expand All @@ -6060,7 +6060,11 @@ end |> Core.Compiler.is_nothrow
return val
end
return zero(Int)
end |> Core.Compiler.is_nothrow
end
@test Core.Compiler.is_nothrow(Base.infer_effects(isdefined_nothrow, (Bool,Int)))
@test !any(first(only(code_typed(isdefined_nothrow, (Bool,Int)))).code) do @nospecialize x
Meta.isexpr(x, :throw_undef_if_not)
end

# End to end test case for the partially initialized struct with `PartialStruct`
@noinline broadcast_noescape1(a) = (broadcast(identity, a); nothing)
Expand Down