Skip to content

Commit

Permalink
Merge branch 'master' into vc/vtune
Browse files Browse the repository at this point in the history
  • Loading branch information
vchuravy authored Nov 18, 2022
2 parents db9e45c + d12ac36 commit df6c18a
Show file tree
Hide file tree
Showing 35 changed files with 398 additions and 210 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ and then use the command prompt to change into the resulting julia directory. By
Julia. However, most users should use the [most recent stable version](https://github.com/JuliaLang/julia/releases)
of Julia. You can get this version by running:

git checkout v1.8.2
git checkout v1.8.3

To build the `julia` executable, run `make` from within the julia directory.

Expand Down
2 changes: 1 addition & 1 deletion base/Enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function Base.show(io::IO, x::Enum)
sym = _symbol(x)
if !(get(io, :compact, false)::Bool)
from = get(io, :module, Base.active_module())
def = typeof(x).name.module
def = parentmodule(typeof(x))
if from === nothing || !Base.isvisible(sym, def, from)
show(io, def)
print(io, ".")
Expand Down
195 changes: 127 additions & 68 deletions base/compiler/abstractinterpretation.jl

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions base/compiler/abstractlattice.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ widenlattice(L::InterConditionalsLattice) = L.parent
is_valid_lattice_norec(lattice::InterConditionalsLattice, @nospecialize(elem)) = isa(elem, InterConditional)

const AnyConditionalsLattice{L} = Union{ConditionalsLattice{L}, InterConditionalsLattice{L}}
const BaseInferenceLattice = typeof(ConditionalsLattice(PartialsLattice(ConstsLattice())))
const IPOResultLattice = typeof(InterConditionalsLattice(PartialsLattice(ConstsLattice())))

const SimpleInferenceLattice = typeof(PartialsLattice(ConstsLattice()))
const BaseInferenceLattice = typeof(ConditionalsLattice(SimpleInferenceLattice.instance))
const IPOResultLattice = typeof(InterConditionalsLattice(SimpleInferenceLattice.instance))

"""
struct InferenceLattice{L}
Expand All @@ -74,7 +76,7 @@ The lattice used by the optimizer. Extends
struct OptimizerLattice{L} <: AbstractLattice
parent::L
end
OptimizerLattice() = OptimizerLattice(BaseInferenceLattice.instance)
OptimizerLattice() = OptimizerLattice(SimpleInferenceLattice.instance)
widenlattice(L::OptimizerLattice) = L.parent
is_valid_lattice_norec(lattice::OptimizerLattice, @nospecialize(elem)) = isa(elem, MaybeUndef)

Expand Down
6 changes: 3 additions & 3 deletions base/compiler/inferencestate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -448,14 +448,14 @@ end

update_valid_age!(edge::InferenceState, sv::InferenceState) = update_valid_age!(sv, edge.valid_worlds)

function record_ssa_assign!(ssa_id::Int, @nospecialize(new), frame::InferenceState)
function record_ssa_assign!(𝕃ᵢ::AbstractLattice, ssa_id::Int, @nospecialize(new), frame::InferenceState)
ssavaluetypes = frame.ssavaluetypes
old = ssavaluetypes[ssa_id]
if old === NOT_FOUND || !(new old)
if old === NOT_FOUND || !(𝕃ᵢ, new, old)
# typically, we expect that old ⊑ new (that output information only
# gets less precise with worse input information), but to actually
# guarantee convergence we need to use tmerge here to ensure that is true
ssavaluetypes[ssa_id] = old === NOT_FOUND ? new : tmerge(old, new)
ssavaluetypes[ssa_id] = old === NOT_FOUND ? new : tmerge(𝕃ᵢ, old, new)
W = frame.ip
for r in frame.ssavalue_uses[ssa_id]
if was_reached(frame, r)
Expand Down
19 changes: 19 additions & 0 deletions base/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,25 @@ function ir_to_codeinf!(opt::OptimizationState)
return src
end

# widen all Const elements in type annotations
function widen_all_consts!(src::CodeInfo)
ssavaluetypes = src.ssavaluetypes::Vector{Any}
for i = 1:length(ssavaluetypes)
ssavaluetypes[i] = widenconst(ssavaluetypes[i])
end

for i = 1:length(src.code)
x = src.code[i]
if isa(x, PiNode)
src.code[i] = PiNode(x.val, widenconst(x.typ))
end
end

src.rettype = widenconst(src.rettype)

return src
end

#########
# logic #
#########
Expand Down
2 changes: 1 addition & 1 deletion base/compiler/ssair/irinterp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function codeinst_to_ir(interp::AbstractInterpreter, code::CodeInstance)
if isa(src, Vector{UInt8})
src = ccall(:jl_uncompress_ir, Any, (Any, Ptr{Cvoid}, Any), mi.def, C_NULL, src)::CodeInfo
else
isa(src, CodeInfo) || return src
isa(src, CodeInfo) || return nothing
end
return inflate_ir(src, mi)
end
Expand Down
27 changes: 7 additions & 20 deletions base/compiler/typeinfer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -590,25 +590,6 @@ function store_backedges(frame::MethodInstance, edges::Vector{Any})
end
end

# widen all Const elements in type annotations
function widen_all_consts!(src::CodeInfo)
ssavaluetypes = src.ssavaluetypes::Vector{Any}
for i = 1:length(ssavaluetypes)
ssavaluetypes[i] = widenconst(ssavaluetypes[i])
end

for i = 1:length(src.code)
x = src.code[i]
if isa(x, PiNode)
src.code[i] = PiNode(x.val, widenconst(x.typ))
end
end

src.rettype = widenconst(src.rettype)

return src
end

function record_slot_assign!(sv::InferenceState)
# look at all assignments to slots
# and union the set of types stored there
Expand Down Expand Up @@ -706,8 +687,14 @@ function find_dominating_assignment(id::Int, idx::Int, sv::InferenceState)
return nothing
end

# annotate types of all symbols in AST
# annotate types of all symbols in AST, preparing for optimization
function type_annotate!(interp::AbstractInterpreter, sv::InferenceState, run_optimizer::Bool)
# widen `Conditional`s from `slottypes`
slottypes = sv.slottypes
for i = 1:length(slottypes)
slottypes[i] = widenconditional(slottypes[i])
end

# compute the required type for each slot
# to hold all of the items assigned into it
record_slot_assign!(sv)
Expand Down
2 changes: 1 addition & 1 deletion base/compiler/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ infer_compilation_signature(::NativeInterpreter) = true

typeinf_lattice(::AbstractInterpreter) = InferenceLattice(BaseInferenceLattice.instance)
ipo_lattice(::AbstractInterpreter) = InferenceLattice(IPOResultLattice.instance)
optimizer_lattice(::AbstractInterpreter) = OptimizerLattice(BaseInferenceLattice.instance)
optimizer_lattice(::AbstractInterpreter) = OptimizerLattice(SimpleInferenceLattice.instance)

abstract type CallInfo end

Expand Down
9 changes: 4 additions & 5 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,7 @@ function showerror(io::IO, ex::MethodError)
elseif isempty(methods(f)) && !isa(f, Function) && !isa(f, Type)
print(io, "objects of type ", ft, " are not callable")
else
if ft <: Function && isempty(ft.parameters) &&
isdefined(ft.name.module, name) &&
ft == typeof(getfield(ft.name.module, name))
if ft <: Function && isempty(ft.parameters) && _isself(ft)
f_is_function = true
end
print(io, "no method matching ")
Expand Down Expand Up @@ -541,7 +539,7 @@ function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=()
end
println(iob)

m = parentmodule_before_main(method.module)
m = parentmodule_before_main(method)
modulecolor = get!(() -> popfirst!(STACKTRACE_MODULECOLORS), STACKTRACE_FIXEDCOLORS, m)
print_module_path_file(iob, m, string(file), line; modulecolor, digit_align_width = 3)

Expand Down Expand Up @@ -696,14 +694,15 @@ function print_stackframe(io, i, frame::StackFrame, n::Int, ndigits_max, modulec
end

# Gets the topmost parent module that isn't Main
function parentmodule_before_main(m)
function parentmodule_before_main(m::Module)
while parentmodule(m) !== m
pm = parentmodule(m)
pm == Main && break
m = pm
end
m
end
parentmodule_before_main(x) = parentmodule_before_main(parentmodule(x))

# Print a stack frame where the module color is set manually with `modulecolor`.
function print_stackframe(io, i, frame::StackFrame, n::Int, ndigits_max, modulecolor)
Expand Down
9 changes: 6 additions & 3 deletions base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -504,13 +504,16 @@ trailing_ones(x::Integer) = trailing_zeros(~x)
>>>(x::BitInteger, y::BitUnsigned) = lshr_int(x, y)
# signed shift counts can shift in either direction
# note: this early during bootstrap, `>=` is not yet available
# note: we only define Int shift counts here; the generic case is handled later
>>(x::BitInteger, y::Int) =
ifelse(0 <= y, x >> unsigned(y), x << unsigned(-y))
<<(x::BitInteger, y::Int) =
ifelse(0 <= y, x << unsigned(y), x >> unsigned(-y))
>>>(x::BitInteger, y::Int) =
ifelse(0 <= y, x >>> unsigned(y), x << unsigned(-y))
>>(x::BitInteger, y::BitSigned) =
ifelse(0 <= y, x >> unsigned(y), x << unsigned(-y))
<<(x::BitInteger, y::BitSigned) =
ifelse(0 <= y, x << unsigned(y), x >> unsigned(-y))
>>>(x::BitInteger, y::BitSigned) =
ifelse(0 <= y, x >>> unsigned(y), x << unsigned(-y))

for to in BitInteger_types, from in (BitInteger_types..., Bool)
if !(to === from)
Expand Down
4 changes: 2 additions & 2 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ julia> gcdx(240, 46)
Bézout coefficients that are computed by the extended Euclidean algorithm.
(Ref: D. Knuth, TAoCP, 2/e, p. 325, Algorithm X.)
For signed integers, these coefficients `u` and `v` are minimal in
the sense that ``|u| < |y/d|`` and ``|v| < |x/d|``. Furthermore,
the sense that ``|u| < |b/d|`` and ``|v| < |a/d|``. Furthermore,
the signs of `u` and `v` are chosen so that `d` is positive.
For unsigned integers, the coefficients `u` and `v` might be near
their `typemax`, and the identity then holds only via the unsigned
Expand All @@ -188,7 +188,7 @@ Base.@assume_effects :terminates_locally function gcdx(a::Integer, b::Integer)
# a0, b0 = a, b
s0, s1 = oneunit(T), zero(T)
t0, t1 = s1, s0
# The loop invariant is: s0*a0 + t0*b0 == a
# The loop invariant is: s0*a0 + t0*b0 == a && s1*a0 + t1*b0 == b
x = a % T
y = b % T
while y != 0
Expand Down
19 changes: 9 additions & 10 deletions base/methodshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,12 @@ function show_method(io::IO, m::Method; modulecolor = :light_black, digit_align_
show_method_params(io, tv)
end

# module & file, re-using function from errorshow.jl
if get(io, :compact, false)::Bool # single-line mode
print_module_path_file(io, m.module, string(file), line; modulecolor, digit_align_width)
else
if !(get(io, :compact, false)::Bool) # single-line mode
println(io)
print_module_path_file(io, m.module, string(file), line; modulecolor, digit_align_width=digit_align_width+4)
digit_align_width += 4
end
# module & file, re-using function from errorshow.jl
print_module_path_file(io, parentmodule(m), string(file), line; modulecolor, digit_align_width)
end

function show_method_list_header(io::IO, ms::MethodList, namefmt::Function)
Expand Down Expand Up @@ -312,10 +311,10 @@ function show_method_table(io::IO, ms::MethodList, max::Int=-1, header::Bool=tru

print(io, " ", lpad("[$n]", digit_align_width + 2), " ")

modulecolor = if meth.module == modul
modulecolor = if parentmodule(meth) == modul
nothing
else
m = parentmodule_before_main(meth.module)
m = parentmodule_before_main(meth)
get!(() -> popfirst!(STACKTRACE_MODULECOLORS), STACKTRACE_FIXEDCOLORS, m)
end
show_method(io, meth; modulecolor)
Expand Down Expand Up @@ -358,7 +357,7 @@ end
fileurl(file) = let f = find_source_file(file); f === nothing ? "" : "file://"*f; end

function url(m::Method)
M = m.module
M = parentmodule(m)
(m.file === :null || m.file === :string) && return ""
file = string(m.file)
line = m.line
Expand Down Expand Up @@ -402,7 +401,7 @@ function show(io::IO, ::MIME"text/html", m::Method)
sig = unwrap_unionall(m.sig)
if sig === Tuple
# Builtin
print(io, m.name, "(...) in ", m.module)
print(io, m.name, "(...) in ", parentmodule(m))
return
end
print(io, decls[1][2], "(")
Expand All @@ -426,7 +425,7 @@ function show(io::IO, ::MIME"text/html", m::Method)
show_method_params(io, tv)
print(io,"</i>")
end
print(io, " in ", m.module)
print(io, " in ", parentmodule(m))
if line > 0
file, line = updated_methodloc(m)
u = url(m)
Expand Down
17 changes: 9 additions & 8 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -641,9 +641,10 @@ See also [`>>`](@ref), [`>>>`](@ref), [`exp2`](@ref), [`ldexp`](@ref).
"""
function <<(x::Integer, c::Integer)
@inline
typemin(Int) <= c <= typemax(Int) && return x << (c % Int)
(x >= 0 || c >= 0) && return zero(x) << 0 # for type stability
oftype(x, -1)
0 <= c <= typemax(UInt) && return x << (c % UInt)
-c <= typemax(UInt) && return x >> (-c % UInt)
(x >= 0 || c >= 0) && return zero(x) << UInt(0) # for type stability
return oftype(x, -1) << UInt(0)
end
function <<(x::Integer, c::Unsigned)
@inline
Expand All @@ -652,7 +653,6 @@ function <<(x::Integer, c::Unsigned)
end
c <= typemax(UInt) ? x << (c % UInt) : zero(x) << UInt(0)
end
<<(x::Integer, c::Int) = c >= 0 ? x << unsigned(c) : x >> unsigned(-c)

"""
>>(x, n)
Expand Down Expand Up @@ -689,11 +689,11 @@ function >>(x::Integer, c::Integer)
if c isa UInt
throw(MethodError(>>, (x, c)))
end
typemin(Int) <= c <= typemax(Int) && return x >> (c % Int)
0 <= c <= typemax(UInt) && return x >> (c % UInt)
-c <= typemax(UInt) && return x << (-c % UInt)
(x >= 0 || c < 0) && return zero(x) >> 0
oftype(x, -1)
end
>>(x::Integer, c::Int) = c >= 0 ? x >> unsigned(c) : x << unsigned(-c)

"""
>>>(x, n)
Expand Down Expand Up @@ -724,7 +724,9 @@ See also [`>>`](@ref), [`<<`](@ref).
"""
function >>>(x::Integer, c::Integer)
@inline
typemin(Int) <= c <= typemax(Int) ? x >>> (c % Int) : zero(x) >>> 0
0 <= c <= typemax(UInt) && return x >>> (c % UInt)
-c <= typemax(UInt) && return x << (-c % UInt)
zero(x) >>> 0
end
function >>>(x::Integer, c::Unsigned)
@inline
Expand All @@ -733,7 +735,6 @@ function >>>(x::Integer, c::Unsigned)
end
c <= typemax(UInt) ? x >>> (c % UInt) : zero(x) >>> 0
end
>>>(x::Integer, c::Int) = c >= 0 ? x >>> unsigned(c) : x << unsigned(-c)

# operator alias

Expand Down
21 changes: 16 additions & 5 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ function methods(@nospecialize(f), @nospecialize(t),
ms = Method[]
for m in _methods(f, t, -1, world)::Vector
m = m::Core.MethodMatch
(mod === nothing || m.method.module mod) && push!(ms, m.method)
(mod === nothing || parentmodule(m.method) mod) && push!(ms, m.method)
end
MethodList(ms, typeof(f).name.mt)
end
Expand Down Expand Up @@ -1561,16 +1561,27 @@ parentmodule(f::Function) = parentmodule(typeof(f))
"""
parentmodule(f::Function, types) -> Module
Determine the module containing a given definition of a generic function.
Determine the module containing the first method of a generic function `f` matching
the specified `types`.
"""
function parentmodule(@nospecialize(f), @nospecialize(types))
m = methods(f, types)
if isempty(m)
error("no matching methods")
end
return first(m).module
return parentmodule(first(m))
end

"""
parentmodule(m::Method) -> Module
Return the module in which the given method `m` is defined.
!!! compat "Julia 1.9"
Passing a `Method` as an argument requires Julia 1.9 or later.
"""
parentmodule(m::Method) = m.module

"""
hasmethod(f, t::Type{<:Tuple}[, kwnames]; world=get_world_counter()) -> Bool
Expand Down Expand Up @@ -1632,7 +1643,7 @@ as written, called after all missing keyword-arguments have been assigned defaul
`basemethod` is the method you obtain via [`which`](@ref) or [`methods`](@ref).
"""
function bodyfunction(basemethod::Method)
fmod = basemethod.module
fmod = parentmodule(basemethod)
# The lowered code for `basemethod` should look like
# %1 = mkw(kwvalues..., #self#, args...)
# return %1
Expand All @@ -1652,7 +1663,7 @@ function bodyfunction(basemethod::Method)
fsym = callexpr.args[3]
end
if isa(fsym, Symbol)
return getfield(basemethod.module, fsym)::Function
return getfield(fmod, fsym)::Function
elseif isa(fsym, GlobalRef)
return getfield(fsym.mod, fsym.name)::Function
elseif isa(fsym, Core.SSAValue)
Expand Down
Loading

0 comments on commit df6c18a

Please sign in to comment.