From e0074a285f1748feb3f996453280f3cc5f24d1c7 Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Thu, 12 May 2022 12:31:35 +0900 Subject: [PATCH] optimize more --- base/compiler/abstractinterpretation.jl | 59 +++++++++++++------------ base/compiler/inferencestate.jl | 9 ++-- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/base/compiler/abstractinterpretation.jl b/base/compiler/abstractinterpretation.jl index 15fd43fe1d210..4326b709141b0 100644 --- a/base/compiler/abstractinterpretation.jl +++ b/base/compiler/abstractinterpretation.jl @@ -2253,7 +2253,8 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState) frame.currbb = _bits_findnext(W.bits, 1)::Int # next basic block end - stoverwrite!(frame.pc_vartable, frame.bb_vartables[frame.currbb]) + states = frame.bb_vartables + currstate = copy(states[frame.currbb]) while frame.currbb <= nbbs delete!(W, frame.currbb) frame.currpc = first(bbs[frame.currbb].stmts) @@ -2274,7 +2275,7 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState) @goto branch elseif isa(stmt, GotoIfNot) condx = stmt.cond - condt = abstract_eval_value(interp, condx, frame.pc_vartable, frame) + condt = abstract_eval_value(interp, condx, currstate, frame) if condt === Bottom empty!(frame.pclimitations) @goto find_next_bb @@ -2313,31 +2314,31 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState) # We continue with the true branch, but process the false # branch here. if isa(condt, Conditional) - else_changes = conditional_changes(frame.pc_vartable, condt.elsetype, condt.var) - if else_changes !== nothing - false_vartable = stoverwrite1!(copy(frame.pc_vartable), else_changes) + else_change = conditional_change(currstate, condt.elsetype, condt.var) + if else_change !== nothing + false_vartable = stoverwrite1!(copy(currstate), else_change) else - false_vartable = frame.pc_vartable + false_vartable = currstate end if falsebb in analyzed_bbs - newstate = stupdate!(frame.bb_vartables[falsebb], false_vartable) + newstate = stupdate!(states[falsebb], false_vartable) else - newstate = stoverwrite!(frame.bb_vartables[falsebb], false_vartable) + newstate = stoverwrite!(states[falsebb], false_vartable) push!(analyzed_bbs, falsebb) end - then_changes = conditional_changes(frame.pc_vartable, condt.vtype, condt.var) - if then_changes !== nothing - stoverwrite1!(frame.pc_vartable, then_changes) + then_change = conditional_change(currstate, condt.vtype, condt.var) + if then_change !== nothing + stoverwrite1!(currstate, then_change) end else if falsebb in analyzed_bbs - newstate = stupdate!(frame.bb_vartables[falsebb], frame.pc_vartable) + newstate = stupdate!(states[falsebb], currstate) else - newstate = stoverwrite!(frame.bb_vartables[falsebb], frame.pc_vartable) + newstate = stoverwrite!(states[falsebb], currstate) push!(analyzed_bbs, falsebb) end end - if newstate !== nothing || !was_reached(frame, first(bbs[falsebb].stmts)) + if newstate !== nothing handle_control_backedge!(frame, frame.currpc, stmt.dest) push!(W, falsebb) end @@ -2346,8 +2347,8 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState) end elseif isa(stmt, ReturnNode) bestguess = frame.bestguess - rt = abstract_eval_value(interp, stmt.val, frame.pc_vartable, frame) - rt = widenreturn(rt, bestguess, nargs, slottypes, frame.pc_vartable) + rt = abstract_eval_value(interp, stmt.val, currstate, frame) + rt = widenreturn(rt, bestguess, nargs, slottypes, currstate) # narrow representation of bestguess slightly to prepare for tmerge with rt if rt isa InterConditional && bestguess isa Const let slot_id = rt.slot @@ -2386,9 +2387,9 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState) l = stmt.args[1]::Int catchbb = block_for_inst(frame.cfg, l) if catchbb in analyzed_bbs - newstate = stupdate!(frame.bb_vartables[catchbb], frame.pc_vartable) + newstate = stupdate!(states[catchbb], currstate) else - newstate = stoverwrite!(frame.bb_vartables[catchbb], frame.pc_vartable) + newstate = stoverwrite!(states[catchbb], currstate) push!(analyzed_bbs, catchbb) end if newstate !== nothing @@ -2401,12 +2402,12 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState) end # Process non control-flow statements (; changes, type) = abstract_eval_basic_statement(interp, - stmt, frame.pc_vartable, frame) + stmt, currstate, frame) if type === Union{} @goto find_next_bb end if changes !== nothing - stoverwrite1!(frame.pc_vartable, changes) + stoverwrite1!(currstate, changes) let cur_hand = frame.handler_at[frame.currpc], l, enter while cur_hand != 0 enter = frame.src.code[cur_hand]::Expr @@ -2415,8 +2416,8 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState) # propagate new type info to exception handler # the handling for Expr(:enter) propagates all changes from before the try/catch # so this only needs to propagate any changes - if stupdate1!(frame.bb_vartables[exceptbb], changes) || !was_reached(frame, first(bbs[exceptbb].stmts)) - push!(frame.ip, exceptbb) + if stupdate1!(states[exceptbb], changes) + push!(W, exceptbb) end cur_hand = frame.handler_at[cur_hand] end @@ -2441,12 +2442,12 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState) # Case 2: Directly branch to a different BB begin @label branch if nextbb in analyzed_bbs - newstate = stupdate!(frame.bb_vartables[nextbb], frame.pc_vartable) + newstate = stupdate!(states[nextbb], currstate) else - newstate = stoverwrite!(frame.bb_vartables[nextbb], frame.pc_vartable) + newstate = stoverwrite!(states[nextbb], currstate) push!(analyzed_bbs, nextbb) end - if newstate !== nothing || !was_reached(frame, first(bbs[nextbb].stmts)) + if newstate !== nothing push!(W, nextbb) end end @@ -2458,7 +2459,7 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState) frame.currbb > nbbs && break frame.currpc = first(bbs[frame.currbb].stmts) - stoverwrite!(frame.pc_vartable, frame.bb_vartables[frame.currbb]) + stoverwrite!(currstate, states[frame.currbb]) end end # while frame.currbb <= nbbs @@ -2466,14 +2467,14 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState) nothing end -function conditional_changes(changes::VarTable, @nospecialize(typ), var::SlotNumber) - oldtyp = changes[slot_id(var)].typ +function conditional_change(state::VarTable, @nospecialize(typ), var::SlotNumber) + oldtyp = state[slot_id(var)].typ # approximate test for `typ ∩ oldtyp` being better than `oldtyp` # since we probably formed these types with `typesubstract`, the comparison is likely simple if ignorelimited(typ) ⊑ ignorelimited(oldtyp) # typ is better unlimited, but we may still need to compute the tmeet with the limit "causes" since we ignored those in the comparison oldtyp isa LimitedAccuracy && (typ = tmerge(typ, LimitedAccuracy(Bottom, oldtyp.causes))) - return StateUpdate(var, VarState(typ, false), changes, true) + return StateUpdate(var, VarState(typ, false), state, true) end return nothing end diff --git a/base/compiler/inferencestate.jl b/base/compiler/inferencestate.jl index b713e86f543ac..1ce3ff10f2a30 100644 --- a/base/compiler/inferencestate.jl +++ b/base/compiler/inferencestate.jl @@ -99,7 +99,6 @@ mutable struct InferenceState ssavalue_uses::Vector{BitSet} # ssavalue sparsity and restart info # TODO: Could keep this sparsely by doing structural liveness analysis ahead of time. bb_vartables::Vector{VarTable} - pc_vartable::VarTable stmt_edges::Vector{Union{Nothing, Vector{Any}}} stmt_info::Vector{Any} @@ -152,17 +151,17 @@ mutable struct InferenceState nslots = length(src.slotflags) slottypes = Vector{Any}(undef, nslots) - pc_vartable = VarTable(undef, nslots) + bb_vartable1 = VarTable(undef, nslots) bb_vartable_proto = VarTable(undef, nslots) argtypes = result.argtypes nargtypes = length(argtypes) for i in 1:nslots argtyp = (i > nargtypes) ? Bottom : argtypes[i] - pc_vartable[i] = VarState(argtyp, i > nargtypes) + bb_vartable1[i] = VarState(argtyp, i > nargtypes) bb_vartable_proto[i] = VarState(Bottom, i > nargtypes) slottypes[i] = argtyp end - bb_vartables = VarTable[i == 1 ? copy(pc_vartable) : copy(bb_vartable_proto) + bb_vartables = VarTable[i == 1 ? bb_vartable1 : copy(bb_vartable_proto) for i = 1:length(cfg.blocks)] pclimitations = IdSet{InferenceState}() @@ -193,7 +192,7 @@ mutable struct InferenceState frame = new( linfo, world, mod, sptypes, slottypes, src, cfg, - currbb, currpc, ip, was_reached, handler_at, ssavalue_uses, bb_vartables, pc_vartable, stmt_edges, stmt_info, + currbb, currpc, ip, was_reached, 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,