Skip to content

Commit 796d182

Browse files
committed
Limit tfunc access to InferenceParams, no need for entire state.
1 parent 8b09da8 commit 796d182

File tree

3 files changed

+59
-57
lines changed

3 files changed

+59
-57
lines changed

base/inference.jl

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ tupletype_tail(t::ANY, n) = Tuple{t.parameters[n:end]...}
237237

238238
#### type-functions for builtins / intrinsics ####
239239

240-
cmp_tfunc = (sv,x,y)->Bool
240+
cmp_tfunc = (params,x,y)->Bool
241241

242242
isType(t::ANY) = isa(t,DataType) && is((t::DataType).name,Type.name)
243243

@@ -253,8 +253,8 @@ function add_tfunc(f::Function, minarg::Int, maxarg::Int, tfunc::ANY)
253253
push!(t_ffunc_key, f)
254254
push!(t_ffunc_val, (minarg, maxarg, tfunc))
255255
end
256-
add_tfunc(throw, 1, 1, (sv,x)->Bottom)
257-
add_tfunc(box, 2, 2, (sv,t,v)->(isType(t) ? t.parameters[1] : Any))
256+
add_tfunc(throw, 1, 1, (params,x)->Bottom)
257+
add_tfunc(box, 2, 2, (params,t,v)->(isType(t) ? t.parameters[1] : Any))
258258
add_tfunc(eq_int, 2, 2, cmp_tfunc)
259259
add_tfunc(ne_int, 2, 2, cmp_tfunc)
260260
add_tfunc(slt_int, 2, 2, cmp_tfunc)
@@ -268,7 +268,7 @@ add_tfunc(le_float, 2, 2, cmp_tfunc)
268268
add_tfunc(fpiseq, 2, 2, cmp_tfunc)
269269
add_tfunc(fpislt, 2, 2, cmp_tfunc)
270270
add_tfunc(Core.Intrinsics.ccall, 3, IInf,
271-
function(sv, fptr, rt, at, a...)
271+
function(params, fptr, rt, at, a...)
272272
if !isType(rt)
273273
return Any
274274
end
@@ -283,12 +283,12 @@ add_tfunc(Core.Intrinsics.ccall, 3, IInf,
283283
return t
284284
end)
285285
add_tfunc(Core.Intrinsics.llvmcall, 3, IInf,
286-
(sv, fptr, rt, at, a...)->(isType(rt) ? rt.parameters[1] : Any))
286+
(params, fptr, rt, at, a...)->(isType(rt) ? rt.parameters[1] : Any))
287287
add_tfunc(Core.Intrinsics.cglobal, 1, 2,
288-
(sv, fptr, t...)->(isempty(t) ? Ptr{Void} :
288+
(params, fptr, t...)->(isempty(t) ? Ptr{Void} :
289289
isType(t[1]) ? Ptr{t[1].parameters[1]} : Ptr))
290290
add_tfunc(Core.Intrinsics.select_value, 3, 3,
291-
function (sv, cnd, x, y)
291+
function (params, cnd, x, y)
292292
if isa(cnd, Const)
293293
if cnd.val === true
294294
return x
@@ -302,7 +302,7 @@ add_tfunc(Core.Intrinsics.select_value, 3, 3,
302302
tmerge(x, y)
303303
end)
304304
add_tfunc(is, 2, 2,
305-
function (sv, x::ANY, y::ANY)
305+
function (params, x::ANY, y::ANY)
306306
if isa(x,Const) && isa(y,Const)
307307
return Const(x.val===y.val)
308308
elseif isType(x) && isType(y) && isleaftype(x) && isleaftype(y)
@@ -316,23 +316,23 @@ add_tfunc(is, 2, 2,
316316
return Bool
317317
end
318318
end)
319-
add_tfunc(isdefined, 1, IInf, (sv, args...)->Bool)
320-
add_tfunc(Core.sizeof, 1, 1, (sv,x)->Int)
321-
add_tfunc(nfields, 1, 1, (sv,x)->(isa(x,Const) ? Const(nfields(x.val)) :
319+
add_tfunc(isdefined, 1, IInf, (params, args...)->Bool)
320+
add_tfunc(Core.sizeof, 1, 1, (params,x)->Int)
321+
add_tfunc(nfields, 1, 1, (params,x)->(isa(x,Const) ? Const(nfields(x.val)) :
322322
isType(x) && isleaftype(x.parameters[1]) ? Const(nfields(x.parameters[1])) :
323323
Int))
324-
add_tfunc(Core._expr, 1, IInf, (sv, args...)->Expr)
325-
add_tfunc(applicable, 1, IInf, (sv, f, args...)->Bool)
326-
add_tfunc(Core.Intrinsics.arraylen, 1, 1, (sv,x)->Int)
327-
add_tfunc(arraysize, 2, 2, (sv,a,d)->Int)
324+
add_tfunc(Core._expr, 1, IInf, (params, args...)->Expr)
325+
add_tfunc(applicable, 1, IInf, (params, f, args...)->Bool)
326+
add_tfunc(Core.Intrinsics.arraylen, 1, 1, (params,x)->Int)
327+
add_tfunc(arraysize, 2, 2, (params,a,d)->Int)
328328
add_tfunc(pointerref, 3, 3,
329-
function (sv,a,i,align)
329+
function (params,a,i,align)
330330
a = widenconst(a)
331331
isa(a,DataType) && a<:Ptr && isa(a.parameters[1],Union{Type,TypeVar}) ? a.parameters[1] : Any
332332
end)
333-
add_tfunc(pointerset, 4, 4, (sv,a,v,i,align)->a)
333+
add_tfunc(pointerset, 4, 4, (params,a,v,i,align)->a)
334334

335-
function typeof_tfunc(sv, t::ANY)
335+
function typeof_tfunc(params, t::ANY)
336336
if isa(t,Const)
337337
return Type{typeof(t.val)}
338338
elseif isType(t)
@@ -351,7 +351,7 @@ function typeof_tfunc(sv, t::ANY)
351351
Type{TypeVar(:_,t)}
352352
end
353353
elseif isa(t,Union)
354-
Union{map(t -> typeof_tfunc(sv,t), t.types)...}
354+
Union{map(t -> typeof_tfunc(params,t), t.types)...}
355355
elseif isa(t,TypeVar) && !(Any <: t.ub)
356356
Type{t}
357357
else
@@ -360,7 +360,7 @@ function typeof_tfunc(sv, t::ANY)
360360
end
361361
add_tfunc(typeof, 1, 1, typeof_tfunc)
362362
add_tfunc(typeassert, 2, 2,
363-
function (sv, v, t)
363+
function (params, v, t)
364364
if isType(t)
365365
if isa(v,Const)
366366
if isleaftype(t) && !isa(v.val, t.parameters[1])
@@ -373,7 +373,7 @@ add_tfunc(typeassert, 2, 2,
373373
return v
374374
end)
375375
add_tfunc(isa, 2, 2,
376-
function (sv, v, t)
376+
function (params, v, t)
377377
if isType(t) && isleaftype(t)
378378
if v t.parameters[1]
379379
return Const(true)
@@ -384,7 +384,7 @@ add_tfunc(isa, 2, 2,
384384
return Bool
385385
end)
386386
add_tfunc(issubtype, 2, 2,
387-
function (sv, a, b)
387+
function (params, a, b)
388388
if isType(a) && isType(b) && isleaftype(a) && isleaftype(b)
389389
return Const(issubtype(a.parameters[1], b.parameters[1]))
390390
end
@@ -401,26 +401,26 @@ function type_depth(t::ANY)
401401
return 0
402402
end
403403

404-
function limit_type_depth(t::ANY, d::Int, cov::Bool, vars, sv::InferenceState)
404+
function limit_type_depth(t::ANY, d::Int, cov::Bool, vars, params::InferenceParams)
405405
if isa(t,TypeVar) || isa(t,TypeConstructor)
406406
return t
407407
end
408-
inexact = !cov && d > sv.params.MAX_TYPE_DEPTH
408+
inexact = !cov && d > params.MAX_TYPE_DEPTH
409409
if isa(t,Union)
410410
t === Bottom && return t
411-
if d > sv.params.MAX_TYPE_DEPTH
411+
if d > params.MAX_TYPE_DEPTH
412412
R = Any
413413
else
414-
R = Union{map(x->limit_type_depth(x, d+1, cov, vars, sv), t.types)...}
414+
R = Union{map(x->limit_type_depth(x, d+1, cov, vars, params), t.types)...}
415415
end
416416
elseif isa(t,DataType)
417417
P = t.parameters
418418
isempty(P) && return t
419-
if d > sv.params.MAX_TYPE_DEPTH
419+
if d > params.MAX_TYPE_DEPTH
420420
R = t.name.primary
421421
else
422422
stillcov = cov && (t.name === Tuple.name)
423-
Q = map(x->limit_type_depth(x, d+1, stillcov, vars, sv), P)
423+
Q = map(x->limit_type_depth(x, d+1, stillcov, vars, params), P)
424424
if !cov && _any(p->contains_is(vars,p), Q)
425425
R = t.name.primary
426426
inexact = true
@@ -439,7 +439,7 @@ function limit_type_depth(t::ANY, d::Int, cov::Bool, vars, sv::InferenceState)
439439
end
440440

441441
# returns (type, isexact)
442-
function getfield_tfunc(sv::InferenceState, s0::ANY, name)
442+
function getfield_tfunc(params::InferenceParams, s0::ANY, name)
443443
if isa(s0, TypeVar)
444444
s0 = s0.ub
445445
end
@@ -459,7 +459,7 @@ function getfield_tfunc(sv::InferenceState, s0::ANY, name)
459459
s = typeof(s.val)
460460
end
461461
if isa(s,Union)
462-
return reduce(tmerge, Bottom, map(t->getfield_tfunc(sv, t, name)[1], s.types)), false
462+
return reduce(tmerge, Bottom, map(t->getfield_tfunc(params, t, name)[1], s.types)), false
463463
end
464464
if isa(s,DataType)
465465
if s.abstract
@@ -501,7 +501,7 @@ function getfield_tfunc(sv::InferenceState, s0::ANY, name)
501501
# in the current type system
502502
typ = limit_type_depth(R, 0, true,
503503
filter!(x->isa(x,TypeVar), Any[s.parameters...]),
504-
sv)
504+
params)
505505
return typ, isleaftype(s) && isa(R, Type) && typeof(R) === typeof(typ) && typeseq(R, typ)
506506
end
507507
end
@@ -533,20 +533,20 @@ function getfield_tfunc(sv::InferenceState, s0::ANY, name)
533533
else
534534
typ = limit_type_depth(R, 0, true,
535535
filter!(x->isa(x,TypeVar), Any[s.parameters...]),
536-
sv)
536+
params)
537537
return typ, alleq && isleaftype(s) && typeof(R) === typeof(typ) && typeseq(R, typ)
538538
end
539539
end
540540
end
541-
add_tfunc(getfield, 2, 2, (sv,s,name)->getfield_tfunc(sv,s,name)[1])
542-
add_tfunc(setfield!, 3, 3, (sv, o, f, v)->v)
543-
function fieldtype_tfunc(sv, s::ANY, name)
541+
add_tfunc(getfield, 2, 2, (params,s,name)->getfield_tfunc(params,s,name)[1])
542+
add_tfunc(setfield!, 3, 3, (params, o, f, v)->v)
543+
function fieldtype_tfunc(params, s::ANY, name)
544544
if isType(s)
545545
s = s.parameters[1]
546546
else
547547
return Type
548548
end
549-
t, exact = getfield_tfunc(sv, s, name)
549+
t, exact = getfield_tfunc(params, s, name)
550550
if is(t,Bottom)
551551
return t
552552
end
@@ -567,7 +567,7 @@ end
567567
has_typevars(t::ANY, all=false) = ccall(:jl_has_typevars_, Cint, (Any,Cint), t, all)!=0
568568

569569
# TODO: handle e.g. apply_type(T, R::Union{Type{Int32},Type{Float64}})
570-
function apply_type_tfunc(sv::InferenceState, args...)
570+
function apply_type_tfunc(params::InferenceParams, args...)
571571
if !isType(args[1])
572572
return Any
573573
end
@@ -627,7 +627,7 @@ function apply_type_tfunc(sv::InferenceState, args...)
627627
uncertain = true
628628
end
629629
!uncertain && return Type{appl}
630-
if type_too_complex(appl, 0, sv)
630+
if type_too_complex(appl, 0, params)
631631
return Type{TypeVar(:_,headtype)}
632632
end
633633
!(isa(appl,TypeVar) || isvarargtype(appl)) ? Type{TypeVar(:_,appl)} : Type{appl}
@@ -645,7 +645,7 @@ function invoke_tfunc(f::ANY, types::ANY, argtype::ANY, sv::InferenceState)
645645
if !isleaftype(Type{types})
646646
return Any
647647
end
648-
argtype = typeintersect(types,limit_tuple_type(argtype, sv))
648+
argtype = typeintersect(types,limit_tuple_type(argtype, sv.params))
649649
if is(argtype,Bottom)
650650
return Bottom
651651
end
@@ -736,7 +736,7 @@ function builtin_tfunction(f::ANY, argtypes::Array{Any,1}, sv::InferenceState)
736736
# wrong # of args
737737
return Bottom
738738
end
739-
return tf[3](sv, argtypes...)
739+
return tf[3](sv.params, argtypes...)
740740
end
741741

742742
limit_tuple_depth(params::InferenceParams, t::ANY) = limit_tuple_depth_(params,t,0)
@@ -760,9 +760,9 @@ function limit_tuple_depth_(params::InferenceParams, t::ANY, d::Int)
760760
Tuple{p...}
761761
end
762762

763-
limit_tuple_type = (t::ANY, sv::InferenceState) -> limit_tuple_type_n(t, sv.params.MAX_TUPLETYPE_LEN, sv)
763+
limit_tuple_type = (t::ANY, params::InferenceParams) -> limit_tuple_type_n(t, params.MAX_TUPLETYPE_LEN)
764764

765-
function limit_tuple_type_n(t::ANY, lim::Int, sv::InferenceState)
765+
function limit_tuple_type_n(t::ANY, lim::Int)
766766
p = t.parameters
767767
n = length(p)
768768
if n > lim
@@ -784,7 +784,7 @@ function abstract_call_gf_by_type(f::ANY, argtype::ANY, sv::InferenceState)
784784
# It is important for N to be >= the number of methods in the error()
785785
# function, so we can still know that error() is always Bottom.
786786
# here I picked 4.
787-
argtype = limit_tuple_type(argtype, sv)
787+
argtype = limit_tuple_type(argtype, sv.params)
788788
argtypes = argtype.parameters
789789
applicable = _methods_by_ftype(argtype, 4)
790790
rettype = Bottom
@@ -830,7 +830,7 @@ function abstract_call_gf_by_type(f::ANY, argtype::ANY, sv::InferenceState)
830830
if td > type_depth(infstate.linfo.specTypes)
831831
# impose limit if we recur and the argument types grow beyond MAX_TYPE_DEPTH
832832
if td > sv.params.MAX_TYPE_DEPTH
833-
sig = limit_type_depth(sig, 0, true, [], sv)
833+
sig = limit_type_depth(sig, 0, true, [], sv.params)
834834
break
835835
else
836836
p1, p2 = sig.parameters, infstate.linfo.specTypes.parameters
@@ -846,7 +846,7 @@ function abstract_call_gf_by_type(f::ANY, argtype::ANY, sv::InferenceState)
846846
newsig[i] = p1[i].name.primary
847847
limitdepth = true
848848
else
849-
newsig[i] = limit_type_depth(p1[i], 1, true, [], sv)
849+
newsig[i] = limit_type_depth(p1[i], 1, true, [], sv.params)
850850
end
851851
end
852852
if limitdepth
@@ -862,7 +862,7 @@ function abstract_call_gf_by_type(f::ANY, argtype::ANY, sv::InferenceState)
862862
# # limit argument type size growth
863863
# tdepth = type_depth(sig)
864864
# if tdepth > sv.params.MAX_TYPE_DEPTH
865-
# sig = limit_type_depth(sig, 0, true, [], sv)
865+
# sig = limit_type_depth(sig, 0, true, [], sv.params)
866866
# end
867867

868868
# limit length based on size of definition signature.
@@ -882,7 +882,7 @@ function abstract_call_gf_by_type(f::ANY, argtype::ANY, sv::InferenceState)
882882
end
883883
end
884884
if !allsame
885-
sig = limit_tuple_type_n(sig, lsig+1, sv)
885+
sig = limit_tuple_type_n(sig, lsig+1)
886886
end
887887
end
888888
end
@@ -1068,12 +1068,12 @@ function abstract_call(f::ANY, fargs, argtypes::Vector{Any}, vtypes::VarTable, s
10681068
# allow tuple indexing functions to take advantage of constant
10691069
# index arguments.
10701070
if istopfunction(tm, f, :getindex)
1071-
return getfield_tfunc(sv, argtypes[2], argtypes[3])[1]
1071+
return getfield_tfunc(sv.params, argtypes[2], argtypes[3])[1]
10721072
elseif istopfunction(tm, f, :next)
1073-
t1 = getfield_tfunc(sv, argtypes[2], argtypes[3])[1]
1073+
t1 = getfield_tfunc(sv.params, argtypes[2], argtypes[3])[1]
10741074
return t1===Bottom ? Bottom : Tuple{t1, Int}
10751075
elseif istopfunction(tm, f, :indexed_next)
1076-
t1 = getfield_tfunc(sv, argtypes[2], argtypes[3])[1]
1076+
t1 = getfield_tfunc(sv.params, argtypes[2], argtypes[3])[1]
10771077
return t1===Bottom ? Bottom : Tuple{t1, Int}
10781078
end
10791079
end
@@ -1268,21 +1268,21 @@ function abstract_interpret(e::ANY, vtypes::VarTable, sv::InferenceState)
12681268
return vtypes
12691269
end
12701270

1271-
function type_too_complex(t::ANY, d, sv::InferenceState)
1272-
if d > sv.params.MAX_TYPE_DEPTH
1271+
function type_too_complex(t::ANY, d, params::InferenceParams)
1272+
if d > params.MAX_TYPE_DEPTH
12731273
return true
12741274
end
12751275
if isa(t,Union)
12761276
p = t.types
12771277
elseif isa(t,DataType)
12781278
p = t.parameters
12791279
elseif isa(t,TypeVar)
1280-
return type_too_complex(t.lb, d+1, sv) || type_too_complex(t.ub, d+1, sv)
1280+
return type_too_complex(t.lb, d+1, params) || type_too_complex(t.ub, d+1, params)
12811281
else
12821282
return false
12831283
end
12841284
for x in (p::SimpleVector)
1285-
if type_too_complex(x, d+1, sv)
1285+
if type_too_complex(x, d+1, params)
12861286
return true
12871287
end
12881288
end
@@ -1340,7 +1340,7 @@ function tmerge(typea::ANY, typeb::ANY)
13401340
end
13411341
end
13421342
u = Union{typea, typeb}
1343-
if length(u.types) > global_sv.params.MAX_TYPEUNION_LEN || type_too_complex(u, 0, global_sv)
1343+
if length(u.types) > global_sv.params.MAX_TYPEUNION_LEN || type_too_complex(u, 0, global_sv.params)
13441344
# don't let type unions get too big
13451345
# TODO: something smarter, like a common supertype
13461346
return Any
@@ -2535,7 +2535,7 @@ function inlineable(f::ANY, ft::ANY, e::Expr, atypes::Vector{Any}, sv::Inference
25352535
end
25362536

25372537
if length(atype_unlimited.parameters) - 1 > sv.params.MAX_TUPLETYPE_LEN
2538-
atype = limit_tuple_type(atype_unlimited, sv)
2538+
atype = limit_tuple_type(atype_unlimited, sv.params)
25392539
else
25402540
atype = atype_unlimited
25412541
end

base/reflection.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,8 @@ function code_typed(f::ANY, types::ANY=Tuple; optimize=true)
568568
if optimize
569569
(li, ty, inf) = Core.Inference.typeinf(linfo, x[1], x[2], true)
570570
else
571-
(li, ty, inf) = Core.Inference.typeinf_uncached(linfo, x[1], x[2], optimize=false)
571+
(li, ty, inf) = Core.Inference.typeinf_uncached(linfo, x[1], x[2],
572+
Core.Inference.InferenceParams(optimize=false))
572573
end
573574
inf || error("inference not successful") # Inference disabled
574575
push!(asts, li)

test/core.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,6 +1758,7 @@ macro m6031(x); x; end
17581758

17591759
# issue #6050
17601760
@test Core.Inference.getfield_tfunc(
1761+
Core.Inference.InferenceParams(),
17611762
Dict{Int64,Tuple{UnitRange{Int64},UnitRange{Int64}}},
17621763
Core.Inference.Const(:vals)) == (Array{Tuple{UnitRange{Int64},UnitRange{Int64}},1},true)
17631764

0 commit comments

Comments
 (0)