Skip to content

Commit 5bd3a6d

Browse files
authored
some compiler-related improvements and fixes (#43502)
* improve comipler type stabilities * fix `code_typed_opaque_closure`
1 parent 6c78ef0 commit 5bd3a6d

File tree

5 files changed

+29
-22
lines changed

5 files changed

+29
-22
lines changed

base/compiler/abstractinterpretation.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function abstract_call_gf_by_type(interp::AbstractInterpreter, @nospecialize(f),
5151

5252
if f !== nothing && napplicable == 1 && is_method_pure(applicable[1]::MethodMatch)
5353
val = pure_eval_call(f, argtypes)
54-
if val !== false
54+
if val !== nothing
5555
# TODO: add some sort of edge(s)
5656
return CallMeta(val, MethodResultPure(info))
5757
end
@@ -1143,16 +1143,16 @@ function pure_eval_call(@nospecialize(f), argtypes::Vector{Any})
11431143
for i = 2:length(argtypes)
11441144
a = widenconditional(argtypes[i])
11451145
if !(isa(a, Const) || isconstType(a))
1146-
return false
1146+
return nothing
11471147
end
11481148
end
1149-
1150-
args = Any[ (a = widenconditional(argtypes[i]); isa(a, Const) ? a.val : a.parameters[1]) for i in 2:length(argtypes) ]
1149+
args = Any[ (a = widenconditional(argtypes[i]);
1150+
isa(a, Const) ? a.val : (a::DataType).parameters[1]) for i in 2:length(argtypes) ]
11511151
try
11521152
value = Core._apply_pure(f, args)
11531153
return Const(value)
11541154
catch
1155-
return false
1155+
return nothing
11561156
end
11571157
end
11581158

@@ -1481,7 +1481,7 @@ function abstract_call_known(interp::AbstractInterpreter, @nospecialize(f),
14811481
max_methods = 1
14821482
elseif la == 3 && istopfunction(f, :typejoin)
14831483
val = pure_eval_call(f, argtypes)
1484-
return CallMeta(val === false ? Type : val, MethodResultPure())
1484+
return CallMeta(val === nothing ? Type : val, MethodResultPure())
14851485
end
14861486
atype = argtypes_to_type(argtypes)
14871487
return abstract_call_gf_by_type(interp, f, arginfo, atype, sv, max_methods)

base/compiler/ssair/slot2ssa.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function make_ssa!(ci::CodeInfo, code::Vector{Any}, idx, slot, @nospecialize(typ
6767
stmt = code[idx]
6868
@assert isexpr(stmt, :(=))
6969
code[idx] = stmt.args[2]
70-
ci.ssavaluetypes[idx] = typ
70+
(ci.ssavaluetypes::Vector{Any})[idx] = typ
7171
idx
7272
end
7373

@@ -209,16 +209,16 @@ end
209209
function typ_for_val(@nospecialize(x), ci::CodeInfo, sptypes::Vector{Any}, idx::Int, slottypes::Vector{Any})
210210
if isa(x, Expr)
211211
if x.head === :static_parameter
212-
return sptypes[x.args[1]]
212+
return sptypes[x.args[1]::Int]
213213
elseif x.head === :boundscheck
214214
return Bool
215215
elseif x.head === :copyast
216216
return typ_for_val(x.args[1], ci, sptypes, idx, slottypes)
217217
end
218-
return ci.ssavaluetypes[idx]
218+
return (ci.ssavaluetypes::Vector{Any})[idx]
219219
end
220220
isa(x, GlobalRef) && return abstract_eval_global(x.mod, x.name)
221-
isa(x, SSAValue) && return ci.ssavaluetypes[x.id]
221+
isa(x, SSAValue) && return (ci.ssavaluetypes::Vector{Any})[x.id]
222222
isa(x, Argument) && return slottypes[x.n]
223223
isa(x, NewSSAValue) && return DelayedTyp(x)
224224
isa(x, QuoteNode) && return Const(x.value)

base/compiler/tfuncs.jl

+10-8
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,8 @@ function typevar_tfunc(@nospecialize(n), @nospecialize(lb_arg), @nospecialize(ub
420420
ub = Any
421421
ub_certain = lb_certain = true
422422
if isa(n, Const)
423-
isa(n.val, Symbol) || return Union{}
423+
nval = n.val
424+
isa(nval, Symbol) || return Union{}
424425
if isa(lb_arg, Const)
425426
lb = lb_arg.val
426427
elseif isType(lb_arg)
@@ -437,7 +438,7 @@ function typevar_tfunc(@nospecialize(n), @nospecialize(lb_arg), @nospecialize(ub
437438
else
438439
return TypeVar
439440
end
440-
tv = TypeVar(n.val, lb, ub)
441+
tv = TypeVar(nval, lb, ub)
441442
return PartialTypeVar(tv, lb_certain, ub_certain)
442443
end
443444
return TypeVar
@@ -1273,11 +1274,10 @@ function apply_type_tfunc(@nospecialize(headtypetype), @nospecialize args...)
12731274
return Union{}
12741275
end
12751276
uw = unwrap_unionall(headtype)
1276-
isnamedtuple = isa(uw, DataType) && uw.name === _NAMEDTUPLE_NAME
12771277
uncertain = false
12781278
canconst = true
12791279
tparams = Any[]
1280-
outervars = Any[]
1280+
outervars = TypeVar[]
12811281
varnamectr = 1
12821282
ua = headtype
12831283
for i = 1:largs
@@ -1324,7 +1324,7 @@ function apply_type_tfunc(@nospecialize(headtypetype), @nospecialize args...)
13241324
# end
13251325
else
13261326
# Is this the second parameter to a NamedTuple?
1327-
if isnamedtuple && isa(ua, UnionAll) && uw.parameters[2] === ua.var
1327+
if isa(uw, DataType) && uw.name === _NAMEDTUPLE_NAME && isa(ua, UnionAll) && uw.parameters[2] === ua.var
13281328
# If the names are known, keep the upper bound, but otherwise widen to Tuple.
13291329
# This is a widening heuristic to avoid keeping type information
13301330
# that's unlikely to be useful.
@@ -1510,9 +1510,11 @@ function _builtin_nothrow(@nospecialize(f), argtypes::Array{Any,1}, @nospecializ
15101510
# Additionally check element type compatibility
15111511
a = widenconst(argtypes[2])
15121512
# Check that we can determine the element type
1513-
(isa(a, DataType) && isa(a.parameters[1], Type)) || return false
1513+
isa(a, DataType) || return false
1514+
ap1 = a.parameters[1]
1515+
isa(ap1, Type) || return false
15141516
# Check that the element type is compatible with the element we're assigning
1515-
(argtypes[3] a.parameters[1]) || return false
1517+
argtypes[3] ap1 || return false
15161518
return true
15171519
elseif f === arrayref || f === const_arrayref
15181520
return array_builtin_common_nothrow(argtypes, 3)
@@ -1568,7 +1570,7 @@ function builtin_tfunction(interp::AbstractInterpreter, @nospecialize(f), argtyp
15681570
end
15691571
if isa(f, IntrinsicFunction)
15701572
if is_pure_intrinsic_infer(f) && _all(@nospecialize(a) -> isa(a, Const), argtypes)
1571-
argvals = anymap(a::Const -> a.val, argtypes)
1573+
argvals = anymap(@nospecialize(a) -> (a::Const).val, argtypes)
15721574
try
15731575
return Const(f(argvals...))
15741576
catch

base/reflection.jl

+5-4
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ function code_typed(@nospecialize(f), @nospecialize(types=default_tt(f));
11921192
throw(ArgumentError("argument is not a generic function"))
11931193
end
11941194
if isa(f, Core.OpaqueClosure)
1195-
return code_typed_opaque_closure(f, types; optimize, debuginfo, interp)
1195+
return code_typed_opaque_closure(f; optimize, debuginfo, interp)
11961196
end
11971197
ft = Core.Typeof(f)
11981198
if isa(types, Type)
@@ -1258,10 +1258,11 @@ function code_typed_opaque_closure(@nospecialize(closure::Core.OpaqueClosure);
12581258
debuginfo::Symbol=:default,
12591259
interp = Core.Compiler.NativeInterpreter(closure.world))
12601260
ccall(:jl_is_in_pure_context, Bool, ()) && error("code reflection cannot be used from generated functions")
1261-
if isa(closure.source, Method)
1262-
code = _uncompressed_ir(closure.source, closure.source.source)
1261+
m = closure.source
1262+
if isa(m, Method)
1263+
code = _uncompressed_ir(m, m.source)
12631264
debuginfo === :none && remove_linenums!(code)
1264-
return Any[Pair{CodeInfo,Any}(code, code.rettype)]
1265+
return Any[(code => code.rettype)]
12651266
else
12661267
error("encountered invalid Core.OpaqueClosure object")
12671268
end

test/opaque_closure.jl

+4
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,7 @@ end
211211
const GLOBAL_OPAQUE_CLOSURE = @opaque () -> 123
212212
call_global_opaque_closure() = GLOBAL_OPAQUE_CLOSURE()
213213
@test call_global_opaque_closure() == 123
214+
215+
let oc = @opaque a->sin(a)
216+
@test length(code_typed(oc, (Int,))) == 1
217+
end

0 commit comments

Comments
 (0)