Skip to content

Commit

Permalink
audit argument types in compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Jul 29, 2018
1 parent 692bd91 commit aa70c12
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 30 deletions.
2 changes: 1 addition & 1 deletion base/compiler/inferencestate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ function add_mt_backedge!(mt::Core.MethodTable, @nospecialize(typ), caller::Infe
nothing
end

function is_specializable_vararg_slot(@nospecialize(arg), nargs, vargs)
function is_specializable_vararg_slot(@nospecialize(arg), nargs::Int, vargs::Vector{Any})
return (isa(arg, Slot) && slot_id(arg) == nargs && !isempty(vargs))
end

Expand Down
4 changes: 2 additions & 2 deletions base/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ end
# These affect control flow within the function (so may not be removed
# if there is no usage within the function), but don't affect the purity
# of the function as a whole.
function stmt_affects_purity(stmt)
function stmt_affects_purity(@nospecialize stmt)
if isa(stmt, GotoIfNot) || isa(stmt, GotoNode) || isa(stmt, ReturnNode)
return false
end
Expand Down Expand Up @@ -270,7 +270,7 @@ end
## Computing the cost of a function body

# saturating sum (inputs are nonnegative), prevents overflow with typemax(Int) below
plus_saturate(x, y) = max(x, y, x+y)
plus_saturate(x::Int, y::Int) = max(x, y, x+y)

# known return type
isknowntype(@nospecialize T) = (T == Union{}) || isconcretetype(T)
Expand Down
16 changes: 8 additions & 8 deletions base/compiler/ssair/passes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function lift_defuse(cfg::CFG, ssa::SSADefUse)
SSADefUse(bb_uses, bb_defs, Int[])
end

function find_curblock(domtree::DomTree, allblocks, curblock::Int)
function find_curblock(domtree::DomTree, allblocks::Vector{Int}, curblock::Int)
# TODO: This can be much faster by looking at current level and only
# searching for those blocks in a sorted order
while !(curblock in allblocks)
Expand All @@ -69,7 +69,7 @@ function val_for_def_expr(ir::IRCode, def::Int, fidx::Int)
end
end

function compute_value_for_block(ir::IRCode, domtree::DomTree, allblocks, du, phinodes, fidx, curblock)
function compute_value_for_block(ir::IRCode, domtree::DomTree, allblocks::Vector{Int}, du::SSADefUse, phinodes::IdDict{Int, SSAValue}, fidx::Int, curblock::Int)
curblock = find_curblock(domtree, allblocks, curblock)
def = 0
for stmt in du.defs
Expand All @@ -80,7 +80,7 @@ function compute_value_for_block(ir::IRCode, domtree::DomTree, allblocks, du, ph
def == 0 ? phinodes[curblock] : val_for_def_expr(ir, def, fidx)
end

function compute_value_for_use(ir::IRCode, domtree::DomTree, allblocks, du, phinodes, fidx, use_idx)
function compute_value_for_use(ir::IRCode, domtree::DomTree, allblocks::Vector{Int}, du::SSADefUse, phinodes::IdDict{Int, SSAValue}, fidx::Int, use_idx::Int)
# Find the first dominating def
curblock = stmtblock = block_for_inst(ir.cfg, use_idx)
curblock = find_curblock(domtree, allblocks, curblock)
Expand Down Expand Up @@ -502,7 +502,7 @@ function perform_lifting!(compact::IncrementalCompact,
end

assertion_counter = 0
function getfield_elim_pass!(ir::IRCode, domtree)
function getfield_elim_pass!(ir::IRCode, domtree::DomTree)
compact = IncrementalCompact(ir)
insertions = Vector{Any}()
defuses = IdDict{Int, Tuple{IdSet{Int}, SSADefUse}}()
Expand Down Expand Up @@ -563,7 +563,7 @@ function getfield_elim_pass!(ir::IRCode, domtree)
for (pidx, preserved_arg) in enumerate(old_preserves)
isa(preserved_arg, SSAValue) || continue
let intermediaries = IdSet()
callback = function(@nospecialize(pi), @nospecialize(ssa))
callback = function(@nospecialize(pi), ssa::AnySSAValue)
push!(intermediaries, ssa.id)
return false
end
Expand Down Expand Up @@ -617,7 +617,7 @@ function getfield_elim_pass!(ir::IRCode, domtree)
if struct_typ.mutable
isa(def, SSAValue) || continue
let intermediaries = IdSet()
callback = function(@nospecialize(pi), @nospecialize(ssa))
callback = function(@nospecialize(pi), ssa::AnySSAValue)
push!(intermediaries, ssa.id)
return false
end
Expand Down Expand Up @@ -747,7 +747,7 @@ function getfield_elim_pass!(ir::IRCode, domtree)
if !isempty(du.uses)
push!(du.defs, idx)
ldu = compute_live_ins(ir.cfg, du)
phiblocks = []
phiblocks = Int[]
if !isempty(ldu.live_in_bbs)
phiblocks = idf(ir.cfg, ldu, domtree)
end
Expand Down Expand Up @@ -828,7 +828,7 @@ function mark_phi_cycles(compact, safe_phis, phi)
end
end

function adce_pass!(ir)
function adce_pass!(ir::IRCode)
phi_uses = fill(0, length(ir.stmts) + length(ir.new_nodes))
all_phis = Int[]
compact = IncrementalCompact(ir)
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 @@ -44,7 +44,7 @@ function lift_defuse(cfg::CFG, defuse)
end

@inline slot_id(s) = isa(s, SlotNumber) ? (s::SlotNumber).id : (s::TypedSlot).id
function scan_slot_def_use(nargs, ci::CodeInfo, code)
function scan_slot_def_use(nargs::Int, ci::CodeInfo, code::Vector{Any})
nslots = length(ci.slotnames)
result = SlotInfo[SlotInfo() for i = 1:nslots]
# Set defs for arguments
Expand Down
29 changes: 22 additions & 7 deletions base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ add_tfunc(<:, 2, 2,
return Bool
end, 0)

function const_datatype_getfield_tfunc(@nospecialize(sv), @nospecialize(fld))
function const_datatype_getfield_tfunc(@nospecialize(sv), fld::Int)
if (fld == DATATYPE_NAME_FIELDINDEX ||
fld == DATATYPE_PARAMETERS_FIELDINDEX ||
fld == DATATYPE_TYPES_FIELDINDEX ||
Expand Down Expand Up @@ -604,9 +604,14 @@ function getfield_tfunc(@nospecialize(s00), @nospecialize(name))
return Const(sv.body)
end
elseif isa(sv, DataType)
t = const_datatype_getfield_tfunc(sv, isa(nv, Symbol) ?
fieldindex(DataType, nv, false) : nv)
t !== nothing && return t
idx = nv
if isa(idx, Symbol)
idx = fieldindex(DataType, idx, false)
end
if isa(idx, Int)
t = const_datatype_getfield_tfunc(sv, idx)
t === nothing || return t
end
elseif isa(sv, Core.TypeName)
fld = isa(nv, Symbol) ? fieldindex(Core.TypeName, nv, false) : nv
if (fld == TYPENAME_NAME_FIELDINDEX ||
Expand Down Expand Up @@ -657,7 +662,12 @@ function getfield_tfunc(@nospecialize(s00), @nospecialize(name))
return rewrap_unionall(unwrapva(s.types[1]), s00)
end
# union together types of all fields
return tmerge_all(map(@nospecialize(t) -> rewrap_unionall(unwrapva(t), s00), s.types))
t = Bottom
for _ft in s.types
t = tmerge(t, rewrap_unionall(unwrapva(_ft), s00))
t === Any && break
end
return t
end
fld = name.val
if isa(fld,Symbol)
Expand Down Expand Up @@ -767,7 +777,12 @@ function fieldtype_tfunc(@nospecialize(s0), @nospecialize(name))
if !(Int <: name || Symbol <: name)
return Bottom
end
return tmerge_all(Any[ fieldtype_tfunc(s0, Const(i)) for i = 1:length(ftypes) ])
t = Bottom
for i in 1:length(ftypes)
t = tmerge(t, fieldtype_tfunc(s0, Const(i)))
t === Any && break
end
return t
end

fld = name.val
Expand Down Expand Up @@ -1037,7 +1052,7 @@ function _builtin_nothrow(@nospecialize(f), argtypes::Array{Any,1}, @nospecializ
return false
end

function builtin_nothrow(@nospecialize(f), argtypes::Array{Any, 1}, rt)
function builtin_nothrow(@nospecialize(f), argtypes::Array{Any, 1}, @nospecialize(rt))
rt === Bottom && return false
contains_is(_PURE_BUILTINS, f) && return true
return _builtin_nothrow(f, argtypes, rt)
Expand Down
2 changes: 1 addition & 1 deletion base/compiler/typelattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function maybe_extract_const_bool(c::Conditional)
(c.elsetype === Bottom && !(c.vtype === Bottom)) && return true
nothing
end
maybe_extract_const_bool(c) = nothing
maybe_extract_const_bool(@nospecialize c) = nothing

function (@nospecialize(a), @nospecialize(b))
if isa(a, MaybeUndef) && !isa(b, MaybeUndef)
Expand Down
8 changes: 0 additions & 8 deletions base/compiler/typelimits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,6 @@ function tmerge(@nospecialize(typea), @nospecialize(typeb))
return Any
end

function tmerge_all(itr)
t = Bottom
for x in itr
t = tmerge(t, x)
end
return t
end

# the inverse of switchtupleunion, with limits on max element union size
function tuplemerge(a::DataType, b::DataType)
@assert a.name === b.name === Tuple.name "assertion failure"
Expand Down
4 changes: 2 additions & 2 deletions base/compiler/typeutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ end
_typename(union::UnionAll) = _typename(union.body)
_typename(a::DataType) = Const(a.name)

function tuple_tail_elem(@nospecialize(init), ct)
function tuple_tail_elem(@nospecialize(init), ct::Vector{Any})
# FIXME: this is broken: it violates subtyping relations and creates invalid types with free typevars
tmerge_maybe_vararg(@nospecialize(a), @nospecialize(b)) = tmerge(a, tvar_extent(unwrapva(b)))
t = init
Expand All @@ -98,7 +98,7 @@ function tuple_tail_elem(@nospecialize(init), ct)
return Vararg{widenconst(t)}
end

function countunionsplit(atypes)
function countunionsplit(atypes::Union{SimpleVector,Vector{Any}})
nu = 1
for ti in atypes
if isa(ti, Union)
Expand Down

0 comments on commit aa70c12

Please sign in to comment.