Skip to content

Commit

Permalink
some compiler-related improvements and fixes (#43502)
Browse files Browse the repository at this point in the history
* improve comipler type stabilities
* fix `code_typed_opaque_closure`
  • Loading branch information
aviatesk authored Dec 23, 2021
1 parent 6c78ef0 commit 5bd3a6d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
12 changes: 6 additions & 6 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function abstract_call_gf_by_type(interp::AbstractInterpreter, @nospecialize(f),

if f !== nothing && napplicable == 1 && is_method_pure(applicable[1]::MethodMatch)
val = pure_eval_call(f, argtypes)
if val !== false
if val !== nothing
# TODO: add some sort of edge(s)
return CallMeta(val, MethodResultPure(info))
end
Expand Down Expand Up @@ -1143,16 +1143,16 @@ function pure_eval_call(@nospecialize(f), argtypes::Vector{Any})
for i = 2:length(argtypes)
a = widenconditional(argtypes[i])
if !(isa(a, Const) || isconstType(a))
return false
return nothing
end
end

args = Any[ (a = widenconditional(argtypes[i]); isa(a, Const) ? a.val : a.parameters[1]) for i in 2:length(argtypes) ]
args = Any[ (a = widenconditional(argtypes[i]);
isa(a, Const) ? a.val : (a::DataType).parameters[1]) for i in 2:length(argtypes) ]
try
value = Core._apply_pure(f, args)
return Const(value)
catch
return false
return nothing
end
end

Expand Down Expand Up @@ -1481,7 +1481,7 @@ function abstract_call_known(interp::AbstractInterpreter, @nospecialize(f),
max_methods = 1
elseif la == 3 && istopfunction(f, :typejoin)
val = pure_eval_call(f, argtypes)
return CallMeta(val === false ? Type : val, MethodResultPure())
return CallMeta(val === nothing ? Type : val, MethodResultPure())
end
atype = argtypes_to_type(argtypes)
return abstract_call_gf_by_type(interp, f, arginfo, atype, sv, max_methods)
Expand Down
8 changes: 4 additions & 4 deletions base/compiler/ssair/slot2ssa.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function make_ssa!(ci::CodeInfo, code::Vector{Any}, idx, slot, @nospecialize(typ
stmt = code[idx]
@assert isexpr(stmt, :(=))
code[idx] = stmt.args[2]
ci.ssavaluetypes[idx] = typ
(ci.ssavaluetypes::Vector{Any})[idx] = typ
idx
end

Expand Down Expand Up @@ -209,16 +209,16 @@ end
function typ_for_val(@nospecialize(x), ci::CodeInfo, sptypes::Vector{Any}, idx::Int, slottypes::Vector{Any})
if isa(x, Expr)
if x.head === :static_parameter
return sptypes[x.args[1]]
return sptypes[x.args[1]::Int]
elseif x.head === :boundscheck
return Bool
elseif x.head === :copyast
return typ_for_val(x.args[1], ci, sptypes, idx, slottypes)
end
return ci.ssavaluetypes[idx]
return (ci.ssavaluetypes::Vector{Any})[idx]
end
isa(x, GlobalRef) && return abstract_eval_global(x.mod, x.name)
isa(x, SSAValue) && return ci.ssavaluetypes[x.id]
isa(x, SSAValue) && return (ci.ssavaluetypes::Vector{Any})[x.id]
isa(x, Argument) && return slottypes[x.n]
isa(x, NewSSAValue) && return DelayedTyp(x)
isa(x, QuoteNode) && return Const(x.value)
Expand Down
18 changes: 10 additions & 8 deletions base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,8 @@ function typevar_tfunc(@nospecialize(n), @nospecialize(lb_arg), @nospecialize(ub
ub = Any
ub_certain = lb_certain = true
if isa(n, Const)
isa(n.val, Symbol) || return Union{}
nval = n.val
isa(nval, Symbol) || return Union{}
if isa(lb_arg, Const)
lb = lb_arg.val
elseif isType(lb_arg)
Expand All @@ -437,7 +438,7 @@ function typevar_tfunc(@nospecialize(n), @nospecialize(lb_arg), @nospecialize(ub
else
return TypeVar
end
tv = TypeVar(n.val, lb, ub)
tv = TypeVar(nval, lb, ub)
return PartialTypeVar(tv, lb_certain, ub_certain)
end
return TypeVar
Expand Down Expand Up @@ -1273,11 +1274,10 @@ function apply_type_tfunc(@nospecialize(headtypetype), @nospecialize args...)
return Union{}
end
uw = unwrap_unionall(headtype)
isnamedtuple = isa(uw, DataType) && uw.name === _NAMEDTUPLE_NAME
uncertain = false
canconst = true
tparams = Any[]
outervars = Any[]
outervars = TypeVar[]
varnamectr = 1
ua = headtype
for i = 1:largs
Expand Down Expand Up @@ -1324,7 +1324,7 @@ function apply_type_tfunc(@nospecialize(headtypetype), @nospecialize args...)
# end
else
# Is this the second parameter to a NamedTuple?
if isnamedtuple && isa(ua, UnionAll) && uw.parameters[2] === ua.var
if isa(uw, DataType) && uw.name === _NAMEDTUPLE_NAME && isa(ua, UnionAll) && uw.parameters[2] === ua.var
# If the names are known, keep the upper bound, but otherwise widen to Tuple.
# This is a widening heuristic to avoid keeping type information
# that's unlikely to be useful.
Expand Down Expand Up @@ -1510,9 +1510,11 @@ function _builtin_nothrow(@nospecialize(f), argtypes::Array{Any,1}, @nospecializ
# Additionally check element type compatibility
a = widenconst(argtypes[2])
# Check that we can determine the element type
(isa(a, DataType) && isa(a.parameters[1], Type)) || return false
isa(a, DataType) || return false
ap1 = a.parameters[1]
isa(ap1, Type) || return false
# Check that the element type is compatible with the element we're assigning
(argtypes[3] a.parameters[1]) || return false
argtypes[3] ap1 || return false
return true
elseif f === arrayref || f === const_arrayref
return array_builtin_common_nothrow(argtypes, 3)
Expand Down Expand Up @@ -1568,7 +1570,7 @@ function builtin_tfunction(interp::AbstractInterpreter, @nospecialize(f), argtyp
end
if isa(f, IntrinsicFunction)
if is_pure_intrinsic_infer(f) && _all(@nospecialize(a) -> isa(a, Const), argtypes)
argvals = anymap(a::Const -> a.val, argtypes)
argvals = anymap(@nospecialize(a) -> (a::Const).val, argtypes)
try
return Const(f(argvals...))
catch
Expand Down
9 changes: 5 additions & 4 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1192,7 +1192,7 @@ function code_typed(@nospecialize(f), @nospecialize(types=default_tt(f));
throw(ArgumentError("argument is not a generic function"))
end
if isa(f, Core.OpaqueClosure)
return code_typed_opaque_closure(f, types; optimize, debuginfo, interp)
return code_typed_opaque_closure(f; optimize, debuginfo, interp)
end
ft = Core.Typeof(f)
if isa(types, Type)
Expand Down Expand Up @@ -1258,10 +1258,11 @@ function code_typed_opaque_closure(@nospecialize(closure::Core.OpaqueClosure);
debuginfo::Symbol=:default,
interp = Core.Compiler.NativeInterpreter(closure.world))
ccall(:jl_is_in_pure_context, Bool, ()) && error("code reflection cannot be used from generated functions")
if isa(closure.source, Method)
code = _uncompressed_ir(closure.source, closure.source.source)
m = closure.source
if isa(m, Method)
code = _uncompressed_ir(m, m.source)
debuginfo === :none && remove_linenums!(code)
return Any[Pair{CodeInfo,Any}(code, code.rettype)]
return Any[(code => code.rettype)]
else
error("encountered invalid Core.OpaqueClosure object")
end
Expand Down
4 changes: 4 additions & 0 deletions test/opaque_closure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,7 @@ end
const GLOBAL_OPAQUE_CLOSURE = @opaque () -> 123
call_global_opaque_closure() = GLOBAL_OPAQUE_CLOSURE()
@test call_global_opaque_closure() == 123

let oc = @opaque a->sin(a)
@test length(code_typed(oc, (Int,))) == 1
end

2 comments on commit 5bd3a6d

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your package evaluation job has completed - possible new issues were detected. A full report can be found here.

Please sign in to comment.