Skip to content

Commit 70b2fe8

Browse files
committed
Put MAX_TYPE_DEPTH in InferenceParams.
1 parent 91d150e commit 70b2fe8

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

base/inference.jl

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import Core: _apply, svec, apply_type, Builtin, IntrinsicFunction
44

55
#### parameters limiting potentially-infinite types ####
66
const MAX_TYPEUNION_LEN = 3
7-
const MAX_TYPE_DEPTH = 7
87

98
const UNION_SPLIT_MISMATCH_ERROR = false
109

1110
immutable InferenceParams
11+
MAX_TYPE_DEPTH
1212
MAX_TUPLETYPE_LEN
1313
MAX_TUPLE_SPLAT
1414
MAX_UNION_SPLITTING
1515
MAX_TUPLE_DEPTH
1616
end
17-
const DEFAULT_PARAMS = InferenceParams(15,16, 4, 4)
17+
const DEFAULT_PARAMS = InferenceParams(7, 15, 16, 4, 4)
1818

1919
# alloc_elim_pass! relies on `Slot_AssignedOnce | Slot_UsedUndef` being
2020
# SSA. This should be true now but can break if we start to track conditional
@@ -395,26 +395,26 @@ function type_depth(t::ANY)
395395
return 0
396396
end
397397

398-
function limit_type_depth(t::ANY, d::Int, cov::Bool, vars)
398+
function limit_type_depth(t::ANY, d::Int, cov::Bool, vars, sv::InferenceState)
399399
if isa(t,TypeVar) || isa(t,TypeConstructor)
400400
return t
401401
end
402-
inexact = !cov && d > MAX_TYPE_DEPTH
402+
inexact = !cov && d > sv.params.MAX_TYPE_DEPTH
403403
if isa(t,Union)
404404
t === Bottom && return t
405-
if d > MAX_TYPE_DEPTH
405+
if d > sv.params.MAX_TYPE_DEPTH
406406
R = Any
407407
else
408-
R = Union{map(x->limit_type_depth(x, d+1, cov, vars), t.types)...}
408+
R = Union{map(x->limit_type_depth(x, d+1, cov, vars, sv), t.types)...}
409409
end
410410
elseif isa(t,DataType)
411411
P = t.parameters
412412
isempty(P) && return t
413-
if d > MAX_TYPE_DEPTH
413+
if d > sv.params.MAX_TYPE_DEPTH
414414
R = t.name.primary
415415
else
416416
stillcov = cov && (t.name === Tuple.name)
417-
Q = map(x->limit_type_depth(x, d+1, stillcov, vars), P)
417+
Q = map(x->limit_type_depth(x, d+1, stillcov, vars, sv), P)
418418
if !cov && _any(p->contains_is(vars,p), Q)
419419
R = t.name.primary
420420
inexact = true
@@ -494,7 +494,8 @@ function getfield_tfunc(sv::InferenceState, s0::ANY, name)
494494
# since the UnionAll type bound is otherwise incorrect
495495
# in the current type system
496496
typ = limit_type_depth(R, 0, true,
497-
filter!(x->isa(x,TypeVar), Any[s.parameters...]))
497+
filter!(x->isa(x,TypeVar), Any[s.parameters...]),
498+
sv)
498499
return typ, isleaftype(s) && isa(R, Type) && typeof(R) === typeof(typ) && typeseq(R, typ)
499500
end
500501
end
@@ -525,7 +526,8 @@ function getfield_tfunc(sv::InferenceState, s0::ANY, name)
525526
return R, alleq
526527
else
527528
typ = limit_type_depth(R, 0, true,
528-
filter!(x->isa(x,TypeVar), Any[s.parameters...]))
529+
filter!(x->isa(x,TypeVar), Any[s.parameters...]),
530+
sv)
529531
return typ, alleq && isleaftype(s) && typeof(R) === typeof(typ) && typeseq(R, typ)
530532
end
531533
end
@@ -559,7 +561,7 @@ end
559561
has_typevars(t::ANY, all=false) = ccall(:jl_has_typevars_, Cint, (Any,Cint), t, all)!=0
560562

561563
# TODO: handle e.g. apply_type(T, R::Union{Type{Int32},Type{Float64}})
562-
function apply_type_tfunc(sv, args...)
564+
function apply_type_tfunc(sv::InferenceState, args...)
563565
if !isType(args[1])
564566
return Any
565567
end
@@ -619,7 +621,7 @@ function apply_type_tfunc(sv, args...)
619621
uncertain = true
620622
end
621623
!uncertain && return Type{appl}
622-
if type_too_complex(appl,0)
624+
if type_too_complex(appl, 0, sv)
623625
return Type{TypeVar(:_,headtype)}
624626
end
625627
!(isa(appl,TypeVar) || isvarargtype(appl)) ? Type{TypeVar(:_,appl)} : Type{appl}
@@ -821,8 +823,8 @@ function abstract_call_gf_by_type(f::ANY, argtype::ANY, sv::InferenceState)
821823
end
822824
if td > type_depth(infstate.linfo.specTypes)
823825
# impose limit if we recur and the argument types grow beyond MAX_TYPE_DEPTH
824-
if td > MAX_TYPE_DEPTH
825-
sig = limit_type_depth(sig, 0, true, [])
826+
if td > sv.params.MAX_TYPE_DEPTH
827+
sig = limit_type_depth(sig, 0, true, [], sv)
826828
break
827829
else
828830
p1, p2 = sig.parameters, infstate.linfo.specTypes.parameters
@@ -838,7 +840,7 @@ function abstract_call_gf_by_type(f::ANY, argtype::ANY, sv::InferenceState)
838840
newsig[i] = p1[i].name.primary
839841
limitdepth = true
840842
else
841-
newsig[i] = limit_type_depth(p1[i], 1, true, [])
843+
newsig[i] = limit_type_depth(p1[i], 1, true, [], sv)
842844
end
843845
end
844846
if limitdepth
@@ -853,8 +855,8 @@ function abstract_call_gf_by_type(f::ANY, argtype::ANY, sv::InferenceState)
853855

854856
# # limit argument type size growth
855857
# tdepth = type_depth(sig)
856-
# if tdepth > MAX_TYPE_DEPTH
857-
# sig = limit_type_depth(sig, 0, true, [])
858+
# if tdepth > sv.params.MAX_TYPE_DEPTH
859+
# sig = limit_type_depth(sig, 0, true, [], sv)
858860
# end
859861

860862
# limit length based on size of definition signature.
@@ -1260,21 +1262,21 @@ function abstract_interpret(e::ANY, vtypes::VarTable, sv::InferenceState)
12601262
return vtypes
12611263
end
12621264

1263-
function type_too_complex(t::ANY, d)
1264-
if d > MAX_TYPE_DEPTH
1265+
function type_too_complex(t::ANY, d, sv::InferenceState)
1266+
if d > sv.params.MAX_TYPE_DEPTH
12651267
return true
12661268
end
12671269
if isa(t,Union)
12681270
p = t.types
12691271
elseif isa(t,DataType)
12701272
p = t.parameters
12711273
elseif isa(t,TypeVar)
1272-
return type_too_complex(t.lb,d+1) || type_too_complex(t.ub,d+1)
1274+
return type_too_complex(t.lb, d+1, sv) || type_too_complex(t.ub, d+1, sv)
12731275
else
12741276
return false
12751277
end
12761278
for x in (p::SimpleVector)
1277-
if type_too_complex(x, d+1)
1279+
if type_too_complex(x, d+1, sv)
12781280
return true
12791281
end
12801282
end
@@ -1332,7 +1334,7 @@ function tmerge(typea::ANY, typeb::ANY)
13321334
end
13331335
end
13341336
u = Union{typea, typeb}
1335-
if length(u.types) > MAX_TYPEUNION_LEN || type_too_complex(u, 0)
1337+
if length(u.types) > MAX_TYPEUNION_LEN || type_too_complex(u, 0, global_sv) # FIXME: use of global_sv
13361338
# don't let type unions get too big
13371339
# TODO: something smarter, like a common supertype
13381340
return Any

demo.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ end
4848
hooks = Core.Inference.InferenceHooks(call_hook)
4949

5050
# raise limits on inference parameters, performing a more exhaustive search
51-
params = Core.Inference.InferenceParams(15, 16, 4, 4)
51+
params = Core.Inference.InferenceParams(7, 15, 16, 4, 4)
5252

5353
(linfo, rettyp, inferred) =
5454
Core.Inference.typeinf_uncached(m, sig, spvals, optimize=true,

0 commit comments

Comments
 (0)