Skip to content

Commit

Permalink
optimize a bit further
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed May 17, 2022
1 parent b637a34 commit d6a4417
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
7 changes: 4 additions & 3 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2057,7 +2057,7 @@ function abstract_eval_statement(interp::AbstractInterpreter, @nospecialize(e),
t = Const(t.instance)
end
if !isempty(sv.pclimitations)
if t isa Const || t === Union{}
if t isa Const || t === Bottom
empty!(sv.pclimitations)
else
t = LimitedAccuracy(t, sv.pclimitations)
Expand Down Expand Up @@ -2276,7 +2276,6 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState)

for frame.currpc in frame.currpc:bbend
stmt = frame.src.code[frame.currpc]
push!(frame.was_reached, frame.currpc)
# If we're at the end of the basic block ...
if frame.currpc == bbend
# Handle control flow
Expand All @@ -2291,6 +2290,7 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState)
condx = stmt.cond
condt = abstract_eval_value(interp, condx, currstate, frame)
if condt === Bottom
ssavaluetypes[frame.currpc] = Bottom
empty!(frame.pclimitations)
@goto find_next_bb
end
Expand Down Expand Up @@ -2402,7 +2402,8 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState)
# Process non control-flow statements
(; changes, type) = abstract_eval_basic_statement(interp,
stmt, currstate, frame)
if type === Union{}
if type === Bottom
ssavaluetypes[frame.currpc] = Bottom
@goto find_next_bb
end
if changes !== nothing
Expand Down
6 changes: 2 additions & 4 deletions base/compiler/inferencestate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ mutable struct InferenceState
currbb::Int
currpc::Int
ip::BitSet#=TODO BoundedMinPrioritySet=# # current active instruction pointers
was_reached::BitSet
handler_at::Vector{Int} # current exception handler info
ssavalue_uses::Vector{BitSet} # ssavalue sparsity and restart info
# TODO: Could keep this sparsely by doing structural liveness analysis ahead of time.
Expand Down Expand Up @@ -141,7 +140,6 @@ mutable struct InferenceState

currbb = currpc = 1
ip = BitSet(1) # TODO BitSetBoundedMinPrioritySet(1)
was_reached = BitSet()
handler_at = compute_trycatch(code, BitSet())
nssavalues = src.ssavaluetypes::Int
ssavalue_uses = find_ssavalue_uses(code, nssavalues)
Expand Down Expand Up @@ -192,7 +190,7 @@ mutable struct InferenceState

frame = new(
linfo, world, mod, sptypes, slottypes, src, cfg,
currbb, currpc, ip, was_reached, handler_at, ssavalue_uses, bb_vartables, stmt_edges, stmt_info,
currbb, currpc, ip, handler_at, ssavalue_uses, bb_vartables, stmt_edges, stmt_info,
pclimitations, limitations, cycle_backedges, callers_in_cycle, dont_work_on_me, parent, inferred,
result, valid_worlds, bestguess, ipo_effects,
params, restrict_abstract_call_sites, cached,
Expand Down Expand Up @@ -234,7 +232,7 @@ function any_inbounds(code::Vector{Any})
return false
end

was_reached((; was_reached)::InferenceState, pc::Int) = pc in was_reached
was_reached(sv::InferenceState, pc::Int) = sv.src.ssavaluetypes[pc] !== NOT_FOUND

function compute_trycatch(code::Vector{Any}, ip::BitSet)
# The goal initially is to record the frame like this for the state at exit:
Expand Down
30 changes: 15 additions & 15 deletions base/compiler/typeinfer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -565,15 +565,6 @@ function widen_all_consts!(src::CodeInfo)
return src
end

function widen_ssavaluetypes!(sv::InferenceState)
ssavaluetypes = sv.src.ssavaluetypes::Vector{Any}
for j = 1:length(ssavaluetypes)
t = ssavaluetypes[j]
ssavaluetypes[j] = t === NOT_FOUND ? Bottom : widenconditional(t)
end
return nothing
end

function record_slot_assign!(sv::InferenceState)
# look at all assignments to slots
# and union the set of types stored there
Expand All @@ -587,7 +578,9 @@ function record_slot_assign!(sv::InferenceState)
if was_reached(sv, i) && isexpr(expr, :(=))
lhs = expr.args[1]
if isa(lhs, SlotNumber)
vt = widenconst(ssavaluetypes[i])
typ = ssavaluetypes[i]
@assert typ !== NOT_FOUND "active slot in unreached region"
vt = widenconst(typ)
if vt !== Bottom
id = slot_id(lhs)
otherTy = slottypes[id]
Expand Down Expand Up @@ -627,6 +620,7 @@ function annotate_slot_load!(undefs::Vector{Bool}, idx::Int, sv::InferenceState,
typ = widenconditional(ignorelimited(vt.typ))
else
typ = sv.src.ssavaluetypes[pc]
@assert typ !== NOT_FOUND "active slot in unreached region"
end
# add type annotations where needed
if !(sv.slottypes[id] typ)
Expand Down Expand Up @@ -670,10 +664,16 @@ function find_dominating_assignment(id::Int, idx::Int, sv::InferenceState)
return nothing
end

function widen_ssavaluetypes!(ssavaluetypes::Vector{Any})
for j = 1:length(ssavaluetypes)
t = ssavaluetypes[j]
ssavaluetypes[j] = t === NOT_FOUND ? Bottom : widenconditional(t)
end
return ssavaluetypes
end

# annotate types of all symbols in AST
function type_annotate!(sv::InferenceState, run_optimizer::Bool)
widen_ssavaluetypes!(sv)

# compute the required type for each slot
# to hold all of the items assigned into it
record_slot_assign!(sv)
Expand Down Expand Up @@ -722,8 +722,8 @@ function type_annotate!(sv::InferenceState, run_optimizer::Bool)
# introduce temporary TypedSlot for the later optimization passes
# and also mark used-undef slots
body[i] = annotate_slot_load!(undefs, oldidx, sv, expr)
else # unreached statement (see issue #7836)
if isa(expr, Expr) && is_meta_expr_head(expr.head)
else # unreached statement (see issue #7836)
if is_meta_expr(expr)
# keep any lexically scoped expressions
elseif run_optimizer
deleteat!(body, i)
Expand Down Expand Up @@ -752,7 +752,7 @@ function type_annotate!(sv::InferenceState, run_optimizer::Bool)
end

src.code = body
src.ssavaluetypes = ssavaluetypes
src.ssavaluetypes = widen_ssavaluetypes!(ssavaluetypes)

nothing
end
Expand Down

0 comments on commit d6a4417

Please sign in to comment.