Skip to content

Commit

Permalink
replace use of () as a sentinel in inference with nothing (JuliaL…
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson authored Jan 31, 2019
1 parent 82d7bd3 commit f916dd8
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 35 deletions.
31 changes: 15 additions & 16 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ function abstract_iteration(@nospecialize(itertype), vtypes::VarTable, sv::Infer
return Any[Vararg{Any}]
end
iteratef = getfield(Main.Base, :iterate)
stateordonet = abstract_call(iteratef, (), Any[Const(iteratef), itertype], vtypes, sv)
stateordonet = abstract_call(iteratef, nothing, Any[Const(iteratef), itertype], vtypes, sv)
# Return Bottom if this is not an iterator.
# WARNING: Changes to the iteration protocol must be reflected here,
# this is not just an optimization.
Expand All @@ -465,7 +465,7 @@ function abstract_iteration(@nospecialize(itertype), vtypes::VarTable, sv::Infer
valtype = stateordonet.parameters[1]
statetype = stateordonet.parameters[2]
push!(ret, valtype)
stateordonet = abstract_call(iteratef, (), Any[Const(iteratef), itertype, statetype], vtypes, sv)
stateordonet = abstract_call(iteratef, nothing, Any[Const(iteratef), itertype, statetype], vtypes, sv)
stateordonet = widenconst(stateordonet)
end
if stateordonet === Nothing
Expand All @@ -482,7 +482,7 @@ function abstract_iteration(@nospecialize(itertype), vtypes::VarTable, sv::Infer
end
valtype = tmerge(valtype, nounion.parameters[1])
statetype = tmerge(statetype, nounion.parameters[2])
stateordonet = abstract_call(iteratef, (), Any[Const(iteratef), itertype, statetype], vtypes, sv)
stateordonet = abstract_call(iteratef, nothing, Any[Const(iteratef), itertype, statetype], vtypes, sv)
stateordonet = widenconst(stateordonet)
end
push!(ret, Vararg{valtype})
Expand Down Expand Up @@ -524,9 +524,9 @@ function abstract_apply(@nospecialize(aft), aargtypes::Vector{Any}, vtypes::VarT
end
for ct in ctypes
if isa(aft, Const)
rt = abstract_call(aft.val, (), ct, vtypes, sv, max_methods)
rt = abstract_call(aft.val, nothing, ct, vtypes, sv, max_methods)
elseif isconstType(aft)
rt = abstract_call(aft.parameters[1], (), ct, vtypes, sv, max_methods)
rt = abstract_call(aft.parameters[1], nothing, ct, vtypes, sv, max_methods)
else
astype = argtypes_to_type(ct)
rt = abstract_call_gf_by_type(nothing, ct, astype, sv, max_methods)
Expand Down Expand Up @@ -570,7 +570,7 @@ function pure_eval_call(@nospecialize(f), argtypes::Vector{Any}, @nospecialize(a
end
end

function abstract_call(@nospecialize(f), fargs::Union{Tuple{},Vector{Any}}, argtypes::Vector{Any}, vtypes::VarTable, sv::InferenceState, max_methods = sv.params.MAX_METHODS)
function abstract_call(@nospecialize(f), fargs::Union{Nothing,Vector{Any}}, argtypes::Vector{Any}, vtypes::VarTable, sv::InferenceState, max_methods = sv.params.MAX_METHODS)
if f === _apply
return abstract_apply(argtypes[2], argtypes[3:end], vtypes, sv, max_methods)
end
Expand Down Expand Up @@ -728,7 +728,7 @@ function abstract_call(@nospecialize(f), fargs::Union{Tuple{},Vector{Any}}, argt
return Any
elseif is_return_type(f)
rt_rt = return_type_tfunc(argtypes, vtypes, sv)
if rt_rt !== NOT_FOUND
if rt_rt !== nothing
return rt_rt
end
elseif length(argtypes) == 2 && istopfunction(f, :!)
Expand All @@ -753,7 +753,7 @@ function abstract_call(@nospecialize(f), fargs::Union{Tuple{},Vector{Any}}, argt
if length(fargs) == 3
fargs = Any[<:, fargs[3], fargs[2]]
else
fargs = ()
fargs = nothing
end
argtypes = Any[typeof(<:), argtypes[3], argtypes[2]]
rty = abstract_call(<:, fargs, argtypes, vtypes, sv)
Expand Down Expand Up @@ -785,7 +785,7 @@ function abstract_call(@nospecialize(f), fargs::Union{Tuple{},Vector{Any}}, argt
end

# wrapper around `abstract_call` for first computing if `f` is available
function abstract_eval_call(fargs::Union{Tuple{},Vector{Any}}, argtypes::Vector{Any}, vtypes::VarTable, sv::InferenceState)
function abstract_eval_call(fargs::Union{Nothing,Vector{Any}}, argtypes::Vector{Any}, vtypes::VarTable, sv::InferenceState)
#print("call ", e.args[1], argtypes, "\n\n")
for x in argtypes
x === Bottom && return Bottom
Expand Down Expand Up @@ -859,7 +859,7 @@ function abstract_eval_cfunction(e::Expr, vtypes::VarTable, sv::InferenceState)
# this may be the wrong world for the call,
# but some of the result is likely to be valid anyways
# and that may help generate better codegen
abstract_eval_call((), at, vtypes, sv)
abstract_eval_call(nothing, at, vtypes, sv)
nothing
end

Expand Down Expand Up @@ -1035,7 +1035,7 @@ function typeinf_local(frame::InferenceState)
delete!(W, pc)
frame.currpc = pc
frame.cur_hand = frame.handler_at[pc]
frame.stmt_edges[pc] === () || empty!(frame.stmt_edges[pc])
frame.stmt_edges[pc] === nothing || empty!(frame.stmt_edges[pc])
stmt = frame.src.code[pc]
changes = s[pc]::VarTable
t = nothing
Expand Down Expand Up @@ -1105,9 +1105,8 @@ function typeinf_local(frame::InferenceState)
end
elseif hd === :enter
l = stmt.args[1]::Int
frame.cur_hand = (l, frame.cur_hand)
frame.cur_hand = Pair{Any,Any}(l, frame.cur_hand)
# propagate type info to exception handler
l = frame.cur_hand[1]
old = s[l]
new = s[pc]::Array{Any,1}
newstate_catch = stupdate!(old, new)
Expand All @@ -1122,7 +1121,7 @@ function typeinf_local(frame::InferenceState)
frame.handler_at[l] = frame.cur_hand
elseif hd === :leave
for i = 1:((stmt.args[1])::Int)
frame.cur_hand = frame.cur_hand[2]
frame.cur_hand = (frame.cur_hand::Pair{Any,Any}).second
end
else
if hd === :(=)
Expand All @@ -1148,11 +1147,11 @@ function typeinf_local(frame::InferenceState)
frame.src.ssavaluetypes[pc] = t
end
end
if frame.cur_hand !== () && isa(changes, StateUpdate)
if frame.cur_hand !== nothing && isa(changes, StateUpdate)
# 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
l = frame.cur_hand[1]
l = frame.cur_hand.first::Int
if stupdate1!(s[l]::VarTable, changes::StateUpdate) !== false
if l < frame.pc´´
frame.pc´´ = l
Expand Down
16 changes: 8 additions & 8 deletions base/compiler/inferencestate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mutable struct InferenceState
pc´´::LineNum
nstmts::Int
# current exception handler info
cur_hand #::Tuple{LineNum, Tuple{LineNum, ...}}
cur_hand #::Union{Nothing, Pair{LineNum, prev_handler}}
handler_at::Vector{Any}
n_handlers::Int
# ssavalue sparsity and restart info
Expand Down Expand Up @@ -54,8 +54,8 @@ mutable struct InferenceState
src.ssavaluetypes = Any[ NOT_FOUND for i = 1:nssavalues ]

n = length(code)
s_edges = Any[ () for i = 1:n ]
s_types = Any[ () for i = 1:n ]
s_edges = Any[ nothing for i = 1:n ]
s_types = Any[ nothing for i = 1:n ]

# initial types
nslots = length(src.slotnames)
Expand All @@ -73,8 +73,8 @@ mutable struct InferenceState
ssavalue_uses = find_ssavalue_uses(code, nssavalues)

# exception handlers
cur_hand = ()
handler_at = Any[ () for i=1:n ]
cur_hand = nothing
handler_at = Any[ nothing for i=1:n ]
n_handlers = 0

W = BitSet()
Expand Down Expand Up @@ -186,7 +186,7 @@ function record_ssa_assign(ssa_id::Int, @nospecialize(new), frame::InferenceStat
W = frame.ip
s = frame.stmt_types
for r in frame.ssavalue_uses[ssa_id]
if s[r] !== () # s[r] === () => unreached statement
if s[r] !== nothing # s[r] === nothing => unreached statement
if r < frame.pc´´
frame.pc´´ = r
end
Expand All @@ -207,7 +207,7 @@ end
# temporarily accumulate our edges to later add as backedges in the callee
function add_backedge!(li::MethodInstance, caller::InferenceState)
isa(caller.linfo.def, Method) || return # don't add backedges to toplevel exprs
if caller.stmt_edges[caller.currpc] === ()
if caller.stmt_edges[caller.currpc] === nothing
caller.stmt_edges[caller.currpc] = []
end
push!(caller.stmt_edges[caller.currpc], li)
Expand All @@ -218,7 +218,7 @@ end
# used to temporarily accumulate our no method errors to later add as backedges in the callee method table
function add_mt_backedge!(mt::Core.MethodTable, @nospecialize(typ), caller::InferenceState)
isa(caller.linfo.def, Method) || return # don't add backedges to toplevel exprs
if caller.stmt_edges[caller.currpc] === ()
if caller.stmt_edges[caller.currpc] === nothing
caller.stmt_edges[caller.currpc] = []
end
push!(caller.stmt_edges[caller.currpc], mt)
Expand Down
2 changes: 1 addition & 1 deletion base/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mutable struct OptimizationState
const_api::Bool
function OptimizationState(frame::InferenceState)
s_edges = frame.stmt_edges[1]
if s_edges === ()
if s_edges === nothing
s_edges = []
frame.stmt_edges[1] = s_edges
end
Expand Down
1 change: 0 additions & 1 deletion base/compiler/ssair/ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,6 @@ function fixup_node(compact::IncrementalCompact, @nospecialize(stmt))
return compact.ssa_rename[stmt.id]
else
urs = userefs(stmt)
urs === () && return stmt
for ur in urs
val = ur[]
if isa(val, NewSSAValue)
Expand Down
2 changes: 1 addition & 1 deletion base/compiler/ssair/slot2ssa.jl
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ function construct_ssa!(ci::CodeInfo, code::Vector{Any}, ir::IRCode, domtree::Do
visited = BitSet()
type_refine_phi = BitSet()
@timeit "SSA Rename" while !isempty(worklist)
(item, pred, incoming_vals) = pop!(worklist)
(item::Int, pred, incoming_vals) = pop!(worklist)
# Rename existing phi nodes first, because their uses occur on the edge
# TODO: This isn't necessary if inlining stops replacing arguments by slots.
for idx in cfg.blocks[item].stmts
Expand Down
6 changes: 3 additions & 3 deletions base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1330,9 +1330,9 @@ function return_type_tfunc(argtypes::Vector{Any}, vtypes::VarTable, sv::Inferenc
end
astype = argtypes_to_type(argtypes_vec)
if isa(aft, Const)
rt = abstract_call(aft.val, (), argtypes_vec, vtypes, sv, -1)
rt = abstract_call(aft.val, nothing, argtypes_vec, vtypes, sv, -1)
elseif isconstType(aft)
rt = abstract_call(aft.parameters[1], (), argtypes_vec, vtypes, sv, -1)
rt = abstract_call(aft.parameters[1], nothing, argtypes_vec, vtypes, sv, -1)
else
rt = abstract_call_gf_by_type(nothing, argtypes_vec, astype, sv, -1)
end
Expand All @@ -1356,7 +1356,7 @@ function return_type_tfunc(argtypes::Vector{Any}, vtypes::VarTable, sv::Inferenc
end
end
end
return NOT_FOUND
return nothing
end

# N.B.: typename maps type equivalence classes to a single value
Expand Down
5 changes: 3 additions & 2 deletions base/compiler/typeinfer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ function store_backedges(frame::InferenceState)
if !toplevel && (frame.cached || frame.parent !== nothing)
caller = frame.result.linfo
for edges in frame.stmt_edges
edges === nothing && continue
i = 1
while i <= length(edges)
to = edges[i]
Expand Down Expand Up @@ -314,7 +315,7 @@ function type_annotate!(sv::InferenceState)
src = sv.src
states = sv.stmt_types
nargs = sv.nargs
nslots = length(states[1])
nslots = length(states[1]::Array{Any,1})
undefs = fill(false, nslots)
body = src.code::Array{Any,1}
nexpr = length(body)
Expand All @@ -339,7 +340,7 @@ function type_annotate!(sv::InferenceState)
st_i = states[i]
expr = body[i]
if isa(st_i, VarTable)
# st_i === () => unreached statement (see issue #7836)
# st_i === nothing => unreached statement (see issue #7836)
if isa(expr, Expr)
annotate_slot_load!(expr, st_i, sv, undefs)
elseif isa(expr, Slot)
Expand Down
6 changes: 3 additions & 3 deletions base/compiler/typelattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ function widenconditional(typ::Conditional)
end
end

function stupdate!(state::Tuple{}, changes::StateUpdate)
function stupdate!(state::Nothing, changes::StateUpdate)
newst = copy(changes.state)
if isa(changes.var, Slot)
changeid = slot_id(changes.var::Slot)
Expand Down Expand Up @@ -288,9 +288,9 @@ function stupdate!(state::VarTable, changes::VarTable)
return newstate
end

stupdate!(state::Tuple{}, changes::VarTable) = copy(changes)
stupdate!(state::Nothing, changes::VarTable) = copy(changes)

stupdate!(state::Tuple{}, changes::Tuple{}) = false
stupdate!(state::Nothing, changes::Nothing) = false

function stupdate1!(state::VarTable, change::StateUpdate)
if !isa(change.var, Slot)
Expand Down

0 comments on commit f916dd8

Please sign in to comment.