Skip to content

Commit c49715a

Browse files
authored
inference: improve typing of stmt_edges (#54729)
1 parent 75951ec commit c49715a

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

base/compiler/inferencestate.jl

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ mutable struct InferenceState
247247
# TODO: Could keep this sparsely by doing structural liveness analysis ahead of time.
248248
bb_vartables::Vector{Union{Nothing,VarTable}} # nothing if not analyzed yet
249249
ssavaluetypes::Vector{Any}
250-
stmt_edges::Vector{Union{Nothing,Vector{Any}}}
250+
stmt_edges::Vector{Vector{Any}}
251251
stmt_info::Vector{CallInfo}
252252

253253
#= intermediate states for interprocedural abstract interpretation =#
@@ -298,7 +298,7 @@ mutable struct InferenceState
298298
nssavalues = src.ssavaluetypes::Int
299299
ssavalue_uses = find_ssavalue_uses(code, nssavalues)
300300
nstmts = length(code)
301-
stmt_edges = Union{Nothing, Vector{Any}}[ nothing for i = 1:nstmts ]
301+
stmt_edges = Vector{Vector{Any}}(undef, nstmts)
302302
stmt_info = CallInfo[ NoCallInfo() for i = 1:nstmts ]
303303

304304
nslots = length(src.slotflags)
@@ -805,26 +805,27 @@ function record_ssa_assign!(𝕃ᵢ::AbstractLattice, ssa_id::Int, @nospecialize
805805
return nothing
806806
end
807807

808-
function add_cycle_backedge!(caller::InferenceState, frame::InferenceState, currpc::Int)
808+
function add_cycle_backedge!(caller::InferenceState, frame::InferenceState)
809809
update_valid_age!(caller, frame.valid_worlds)
810-
backedge = (caller, currpc)
810+
backedge = (caller, caller.currpc)
811811
contains_is(frame.cycle_backedges, backedge) || push!(frame.cycle_backedges, backedge)
812812
add_backedge!(caller, frame.linfo)
813813
return frame
814814
end
815815

816816
function get_stmt_edges!(caller::InferenceState, currpc::Int=caller.currpc)
817817
stmt_edges = caller.stmt_edges
818-
edges = stmt_edges[currpc]
819-
if edges === nothing
820-
edges = stmt_edges[currpc] = []
818+
if !isassigned(stmt_edges, currpc)
819+
return stmt_edges[currpc] = Any[]
820+
else
821+
return stmt_edges[currpc]
821822
end
822-
return edges
823823
end
824824

825825
function empty_backedges!(frame::InferenceState, currpc::Int=frame.currpc)
826-
edges = frame.stmt_edges[currpc]
827-
edges === nothing || empty!(edges)
826+
if isassigned(frame.stmt_edges, currpc)
827+
empty!(frame.stmt_edges[currpc])
828+
end
828829
return nothing
829830
end
830831

base/compiler/optimize.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ struct InliningState{Interp<:AbstractInterpreter}
131131
interp::Interp
132132
end
133133
function InliningState(sv::InferenceState, interp::AbstractInterpreter)
134-
edges = sv.stmt_edges[1]::Vector{Any}
134+
edges = sv.stmt_edges[1]
135135
return InliningState(edges, sv.world, interp)
136136
end
137137
function InliningState(interp::AbstractInterpreter)

base/compiler/typeinfer.jl

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -563,13 +563,10 @@ end
563563
# update the MethodInstance
564564
function finish(me::InferenceState, interp::AbstractInterpreter)
565565
# prepare to run optimization passes on fulltree
566-
s_edges = me.stmt_edges[1]
567-
if s_edges === nothing
568-
s_edges = me.stmt_edges[1] = []
569-
end
570-
for edges in me.stmt_edges
571-
edges === nothing && continue
572-
edges === s_edges && continue
566+
s_edges = get_stmt_edges!(me, 1)
567+
for i = 2:length(me.stmt_edges)
568+
isassigned(me.stmt_edges, i) || continue
569+
edges = me.stmt_edges[i]
573570
append!(s_edges, edges)
574571
empty!(edges)
575572
end
@@ -776,7 +773,7 @@ function merge_call_chain!(interp::AbstractInterpreter, parent::InferenceState,
776773
# and merge all of the callers into ancestor.callers_in_cycle
777774
# and ensure that walking the parent list will get the same result (DAG) from everywhere
778775
while true
779-
add_cycle_backedge!(parent, child, parent.currpc)
776+
add_cycle_backedge!(parent, child)
780777
union_caller_cycle!(ancestor, child)
781778
child = parent
782779
child === ancestor && break

0 commit comments

Comments
 (0)