Skip to content

Commit

Permalink
remove maybe_widenconditional
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed Dec 17, 2021
1 parent 0b60f1a commit dae5ad3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 31 deletions.
11 changes: 5 additions & 6 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,6 @@ function const_prop_argument_heuristic(_::AbstractInterpreter, (; fargs, argtype
if isConditional(a) && fargs !== nothing
is_const_prop_profitable_conditional(conditional(a), fargs, sv) && return true
else
a = widenconditional(a)
has_nontrivial_const_info(a) && is_const_prop_profitable_arg(a) && return true
end
end
Expand Down Expand Up @@ -1133,13 +1132,14 @@ is_method_pure(match::MethodMatch) = is_method_pure(match.method, match.spec_typ

function pure_eval_call(@nospecialize(f), argtypes::Argtypes)
for i = 2:length(argtypes)
a = widenconditional(argtypes[i])
a = argtypes[i]
if !(isConst(a) || isconstType(widenconst(a)))
return nothing
end
end

args = Any[ (a = widenconditional(argtypes[i]); isConst(a) ? constant(a) : (widenconst(a)::Type).parameters[1]) for i in 2:length(argtypes) ]
args = Any[ (a = argtypes[i]; isConst(a) ? constant(a) : (widenconst(a)::Type).parameters[1])
for i in 2:length(argtypes) ]
try
value = Core._apply_pure(f, args)
return Const(value)
Expand Down Expand Up @@ -1175,12 +1175,11 @@ function abstract_call_builtin(
typ = argtypes[2]
if isConditional(typ)
cnd = conditional(typ)
newtyp = widenconditional(typ)
tx = argtypes[3]
ty = argtypes[4]
if isConst(newtyp)
if isConst(typ)
# if `cnd` is constant, we should just respect its constantness to keep inference accuracy
return constant(newtyp)::Bool ? tx : ty
return constant(typ)::Bool ? tx : ty
else
# try to simulate this as a real conditional (`cnd ? x : y`), so that the penalty for using `ifelse` instead isn't too high
a = ssa_def_slot(fargs[3], sv)
Expand Down
42 changes: 18 additions & 24 deletions base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,24 +236,22 @@ end
add_tfunc(Core.ifelse, 3, 3, ifelse_tfunc, 1)

function egal_tfunc(@nospecialize(x), @nospecialize(y))
xx = widenconditional(x)
yy = widenconditional(y)
if isConditional(x) && isConst(yy)
if isConditional(x) && isConst(y)
cnd = conditional(x)
constant(yy) === false && return Conditional(cnd.slot_id, cnd.elsetype, cnd.vtype)
constant(yy) === true && return x
constant(y) === false && return Conditional(cnd.slot_id, cnd.elsetype, cnd.vtype)
constant(y) === true && return x
return Const(false)
elseif isConditional(y) && isConst(xx)
elseif isConditional(y) && isConst(x)
cnd = conditional(y)
constant(xx) === false && return Conditional(cnd.slot_id, cnd.elsetype, cnd.vtype)
constant(xx) === true && return y
constant(x) === false && return Conditional(cnd.slot_id, cnd.elsetype, cnd.vtype)
constant(x) === true && return y
return Const(false)
elseif isConst(xx) && isConst(yy)
return Const(constant(xx) === constant(yy))
elseif !hasintersect(widenconst(xx), widenconst(yy))
elseif isConst(x) && isConst(y)
return Const(constant(x) === constant(y))
elseif !hasintersect(widenconst(x), widenconst(y))
return Const(false)
elseif (isConst(xx) && y === typeof(constant(xx)) && isdefined(y, :instance)) ||
(isConst(yy) && x === typeof(constant(yy)) && isdefined(x, :instance))
elseif (isConst(x) && y === typeof(constant(x)) && isdefined(y, :instance)) ||
(isConst(y) && x === typeof(constant(y)) && isdefined(x, :instance))
return Const(true)
end
return Bool
Expand Down Expand Up @@ -1231,7 +1229,7 @@ function apply_type_nothrow(argtypes::Array{Any, 1}, @nospecialize(rt))
u = headtype
for i = 2:length(argtypes)
isa(u, UnionAll) || return false
ai = widenconditional(argtypes[i])
ai = argtypes[i]
if ai TypeVar || ai === DataType
# We don't know anything about the bounds of this typevar, but as
# long as the UnionAll is not constrained, that's ok.
Expand Down Expand Up @@ -1343,7 +1341,7 @@ function apply_type_tfunc(@nospecialize(headtypetype), @nospecialize args...)
varnamectr = 1
ua = headtype
for i = 1:largs
ai = widenconditional(args[i])
ai = args[i]
if isType(ai)
aip1 = ai.parameters[1]
canconst &= !has_free_typevars(aip1)
Expand Down Expand Up @@ -1434,17 +1432,10 @@ function apply_type_tfunc(@nospecialize(headtypetype), @nospecialize args...)
end
add_tfunc(apply_type, 1, INT_INF, apply_type_tfunc, 10)

function has_struct_const_info(@nospecialize x)
isPartialTypeVar(x) && return true
isConditional(x) && return true
return has_nontrivial_const_info(x)
end

# convert the dispatch tuple type argtype to the real (concrete) type of
# the tuple of those values
tuple_tfunc(argtypes::Argtypes) = tuple_tfunc(Any[a for a in argtypes])
function tuple_tfunc(argtypes::Vector{Any})
argtypes = anymap(widenconditional, argtypes)
all_are_const = true
for i in 1:length(argtypes)
if !isConst(argtypes[i])
Expand All @@ -1459,9 +1450,12 @@ function tuple_tfunc(argtypes::Vector{Any})
anyinfo = false
for i in 1:length(argtypes)
x = argtypes[i]
if has_struct_const_info(x)
if has_nontrivial_const_info(x) || isPartialTypeVar(x)
anyinfo = true
else
# `PartialStruct` doesn't wrap `Conditional`, widen here
# TODO (lattice-overhaul) just `widenconditional` unconditionally (don't work on native types here)
isConditional(x) && (x = widenconditional(x))
if !isVararg(x)
x = widenconst(x)
end
Expand Down Expand Up @@ -1790,7 +1784,7 @@ function return_type_tfunc(interp::AbstractInterpreter, argtypes::Argtypes, sv::
end
call = abstract_call(interp, ArgInfo(nothing, argtypes), sv, -1)
info = verbose_stmt_info(interp) ? ReturnTypeCallInfo(call.info) : false
rt = widenconditional(call.rt) # TODO (lattice overhaul) remove this sort of "maybe" `widenconditional` calls
rt = call.rt
if isConst(rt)
# output was computed to be constant
return CallMeta(Const(typeof(constant(rt))), info)
Expand Down
1 change: 0 additions & 1 deletion base/compiler/typelattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ isAnyConditional(typ::LatticeElement) = hasConditional(typ)
# all usages of this function can be simply replaced with `x.conditional`
@inline conditional((; conditional)::LatticeElement) = (@assert !conditional.inter; conditional)
@inline interconditional((; conditional)::LatticeElement) = (@assert conditional.inter; conditional)
widenconditional(@nospecialize typ) = typ
widenconditional(typ::LatticeElement) = isAnyConditional(typ) ? _widenconditional(typ) : typ
_widenconditional(typ::LatticeElement) = LatticeElement(typ; conditional = __NULL_CONDITIONAL__)

Expand Down

0 comments on commit dae5ad3

Please sign in to comment.