Skip to content

Commit 7c3bd1f

Browse files
authored
Merge branch 'master' into vc/fp16
2 parents 9dc6746 + 3c04919 commit 7c3bd1f

File tree

30 files changed

+237
-161
lines changed

30 files changed

+237
-161
lines changed

base/Base.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ include("operators.jl")
124124
include("pointer.jl")
125125
include("refvalue.jl")
126126
include("refpointer.jl")
127+
128+
# The REPL stdlib hooks into Base using this Ref
129+
const REPL_MODULE_REF = Ref{Module}()
130+
127131
include("checked.jl")
128132
using .Checked
129133

base/array.jl

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,11 @@ function reverseind(a::AbstractVector, i::Integer)
18521852
first(li) + last(li) - i
18531853
end
18541854

1855+
# This implementation of `midpoint` is performance-optimized but safe
1856+
# only if `lo <= hi`.
1857+
midpoint(lo::T, hi::T) where T<:Integer = lo + ((hi - lo) >>> 0x01)
1858+
midpoint(lo::Integer, hi::Integer) = midpoint(promote(lo, hi)...)
1859+
18551860
"""
18561861
reverse!(v [, start=firstindex(v) [, stop=lastindex(v) ]]) -> v
18571862
@@ -1880,17 +1885,18 @@ julia> A
18801885
"""
18811886
function reverse!(v::AbstractVector, start::Integer, stop::Integer=lastindex(v))
18821887
s, n = Int(start), Int(stop)
1883-
liv = LinearIndices(v)
1884-
if n <= s # empty case; ok
1885-
elseif !(first(liv) s last(liv))
1886-
throw(BoundsError(v, s))
1887-
elseif !(first(liv) n last(liv))
1888-
throw(BoundsError(v, n))
1889-
end
1890-
r = n
1891-
@inbounds for i in s:div(s+n-1, 2)
1892-
v[i], v[r] = v[r], v[i]
1893-
r -= 1
1888+
if n > s # non-empty and non-trivial
1889+
liv = LinearIndices(v)
1890+
if !(first(liv) s last(liv))
1891+
throw(BoundsError(v, s))
1892+
elseif !(first(liv) n last(liv))
1893+
throw(BoundsError(v, n))
1894+
end
1895+
r = n
1896+
@inbounds for i in s:midpoint(s, n-1)
1897+
v[i], v[r] = v[r], v[i]
1898+
r -= 1
1899+
end
18941900
end
18951901
return v
18961902
end

base/boot.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ eval(Core, quote
435435
# NOTE the main constructor is defined within `Core.Compiler`
436436
_PartialStruct(typ::DataType, fields::Array{Any, 1}) = $(Expr(:new, :PartialStruct, :typ, :fields))
437437
PartialOpaque(@nospecialize(typ), @nospecialize(env), parent::MethodInstance, source::Method) = $(Expr(:new, :PartialOpaque, :typ, :env, :parent, :source))
438+
InterConditional(slot::Int, @nospecialize(thentype), @nospecialize(elsetype)) = $(Expr(:new, :InterConditional, :slot, :thentype, :elsetype))
438439
MethodMatch(@nospecialize(spec_types), sparams::SimpleVector, method::Method, fully_covers::Bool) = $(Expr(:new, :MethodMatch, :spec_types, :sparams, :method, :fully_covers))
439440
end)
440441

base/client.jl

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -370,9 +370,6 @@ function __atreplinit(repl)
370370
end
371371
_atreplinit(repl) = invokelatest(__atreplinit, repl)
372372

373-
# The REPL stdlib hooks into Base using this Ref
374-
const REPL_MODULE_REF = Ref{Module}()
375-
376373
function load_InteractiveUtils(mod::Module=Main)
377374
# load interactive-only libraries
378375
if !isdefined(mod, :InteractiveUtils)
@@ -390,10 +387,10 @@ function load_InteractiveUtils(mod::Module=Main)
390387
return getfield(mod, :InteractiveUtils)
391388
end
392389

390+
global active_repl
391+
393392
# run the requested sort of evaluation loop on stdio
394393
function run_main_repl(interactive::Bool, quiet::Bool, banner::Bool, history_file::Bool, color_set::Bool)
395-
global active_repl
396-
397394
load_InteractiveUtils()
398395

399396
if interactive && isassigned(REPL_MODULE_REF)
@@ -402,17 +399,18 @@ function run_main_repl(interactive::Bool, quiet::Bool, banner::Bool, history_fil
402399
term = REPL.Terminals.TTYTerminal(term_env, stdin, stdout, stderr)
403400
banner && Base.banner(term)
404401
if term.term_type == "dumb"
405-
active_repl = REPL.BasicREPL(term)
402+
repl = REPL.BasicREPL(term)
406403
quiet || @warn "Terminal not fully functional"
407404
else
408-
active_repl = REPL.LineEditREPL(term, get(stdout, :color, false), true)
409-
active_repl.history_file = history_file
405+
repl = REPL.LineEditREPL(term, get(stdout, :color, false), true)
406+
repl.history_file = history_file
410407
end
408+
global active_repl = repl
411409
# Make sure any displays pushed in .julia/config/startup.jl ends up above the
412410
# REPLDisplay
413-
pushdisplay(REPL.REPLDisplay(active_repl))
414-
_atreplinit(active_repl)
415-
REPL.run_repl(active_repl, backend->(global active_repl_backend = backend))
411+
pushdisplay(REPL.REPLDisplay(repl))
412+
_atreplinit(repl)
413+
REPL.run_repl(repl, backend->(global active_repl_backend = backend))
416414
end
417415
else
418416
# otherwise provide a simple fallback

base/compiler/compiler.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ include("generator.jl")
3838
include("reflection.jl")
3939
include("options.jl")
4040

41+
ntuple(f, ::Val{0}) = ()
42+
ntuple(f, ::Val{1}) = (@inline; (f(1),))
43+
ntuple(f, ::Val{2}) = (@inline; (f(1), f(2)))
44+
ntuple(f, ::Val{3}) = (@inline; (f(1), f(2), f(3)))
45+
ntuple(f, ::Val{n}) where {n} = ntuple(f, n::Int)
46+
ntuple(f, n) = (Any[f(i) for i = 1:n]...,)
47+
4148
# core operations & types
4249
function return_type end # promotion.jl expects this to exist
4350
is_return_type(@Core.nospecialize(f)) = f === return_type
@@ -92,13 +99,6 @@ using .Iterators: zip, enumerate
9299
using .Iterators: Flatten, Filter, product # for generators
93100
include("namedtuple.jl")
94101

95-
ntuple(f, ::Val{0}) = ()
96-
ntuple(f, ::Val{1}) = (@inline; (f(1),))
97-
ntuple(f, ::Val{2}) = (@inline; (f(1), f(2)))
98-
ntuple(f, ::Val{3}) = (@inline; (f(1), f(2), f(3)))
99-
ntuple(f, ::Val{n}) where {n} = ntuple(f, n::Int)
100-
ntuple(f, n) = (Any[f(i) for i = 1:n]...,)
101-
102102
# core docsystem
103103
include("docs/core.jl")
104104
import Core.Compiler.CoreDocs

base/compiler/typelattice.jl

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# structs/constants #
55
#####################
66

7-
# N.B.: Const/PartialStruct are defined in Core, to allow them to be used
7+
# N.B.: Const/PartialStruct/InterConditional are defined in Core, to allow them to be used
88
# inside the global code cache.
99
#
1010
# # The type of a value might be constant
@@ -65,16 +65,15 @@ This is separate from `Conditional` to catch logic errors: the lattice element n
6565
while processing a call, then `Conditional` everywhere else. Thus `InterConditional` does not appear in
6666
`CompilerTypes`—these type's usages are disjoint—though we define the lattice for `InterConditional`.
6767
"""
68-
struct InterConditional
69-
slot::Int
70-
thentype
71-
elsetype
72-
function InterConditional(slot::Int, @nospecialize(thentype), @nospecialize(elsetype))
73-
assert_nested_slotwrapper(thentype)
74-
assert_nested_slotwrapper(elsetype)
75-
return new(slot, thentype, elsetype)
76-
end
77-
end
68+
:(InterConditional)
69+
import Core: InterConditional
70+
# struct InterConditional
71+
# slot::Int
72+
# thentype
73+
# elsetype
74+
# InterConditional(slot::Int, @nospecialize(thentype), @nospecialize(elsetype)) =
75+
# new(slot, thentype, elsetype)
76+
# end
7877
InterConditional(var::SlotNumber, @nospecialize(thentype), @nospecialize(elsetype)) =
7978
InterConditional(slot_id(var), thentype, elsetype)
8079

base/methodshow.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ function url(m::Method)
374374
return LibGit2.with(LibGit2.GitRepoExt(d)) do repo
375375
LibGit2.with(LibGit2.GitConfig(repo)) do cfg
376376
u = LibGit2.get(cfg, "remote.origin.url", "")
377-
u = match(LibGit2.GITHUB_REGEX,u).captures[1]
377+
u = (match(LibGit2.GITHUB_REGEX,u)::AbstractMatch).captures[1]
378378
commit = string(LibGit2.head_oid(repo))
379379
root = LibGit2.path(repo)
380380
if startswith(file, root) || startswith(realpath(file), root)

base/path.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ elseif Sys.iswindows()
3535
const path_ext_splitter = r"^((?:.*[/\\])?(?:\.|[^/\\\.])[^/\\]*?)(\.[^/\\\.]*|)$"
3636

3737
function splitdrive(path::String)
38-
m = match(r"^([^\\]+:|\\\\[^\\]+\\[^\\]+|\\\\\?\\UNC\\[^\\]+\\[^\\]+|\\\\\?\\[^\\]+:|)(.*)$"s, path)
38+
m = match(r"^([^\\]+:|\\\\[^\\]+\\[^\\]+|\\\\\?\\UNC\\[^\\]+\\[^\\]+|\\\\\?\\[^\\]+:|)(.*)$"s, path)::AbstractMatch
3939
String(something(m.captures[1])), String(something(m.captures[2]))
4040
end
4141
else

base/show.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,11 @@ function _show_default(io::IO, @nospecialize(x))
482482
print(io,')')
483483
end
484484

485-
active_module()::Module = isdefined(Base, :active_repl) && isdefined(Base.active_repl, :mistate) && Base.active_repl.mistate !== nothing ?
486-
Base.active_repl.mistate.active_module :
487-
Main
485+
function active_module()
486+
isassigned(REPL_MODULE_REF) || return Main
487+
REPL = REPL_MODULE_REF[]
488+
return REPL.active_module()::Module
489+
end
488490

489491
# Check if a particular symbol is exported from a standard library module
490492
function is_exported_from_stdlib(name::Symbol, mod::Module)

base/sort.jl

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ using .Base: copymutable, LinearIndices, length, (:), iterate, OneTo,
1111
AbstractMatrix, AbstractUnitRange, isless, identity, eltype, >, <, <=, >=, |, +, -, *, !,
1212
extrema, sub_with_overflow, add_with_overflow, oneunit, div, getindex, setindex!,
1313
length, resize!, fill, Missing, require_one_based_indexing, keytype, UnitRange,
14-
min, max, reinterpret, signed, unsigned, Signed, Unsigned, typemin, xor, Type, BitSigned, Val
14+
min, max, reinterpret, signed, unsigned, Signed, Unsigned, typemin, xor, Type, BitSigned, Val,
15+
midpoint
1516

1617
using .Base: >>>, !==
1718

@@ -165,11 +166,6 @@ same thing as `partialsort!` but leaving `v` unmodified.
165166
partialsort(v::AbstractVector, k::Union{Integer,OrdinalRange}; kws...) =
166167
partialsort!(copymutable(v), k; kws...)
167168

168-
# This implementation of `midpoint` is performance-optimized but safe
169-
# only if `lo <= hi`.
170-
midpoint(lo::T, hi::T) where T<:Integer = lo + ((hi - lo) >>> 0x01)
171-
midpoint(lo::Integer, hi::Integer) = midpoint(promote(lo, hi)...)
172-
173169
# reference on sorted binary search:
174170
# http://www.tbray.org/ongoing/When/200x/2003/03/22/Binary
175171

@@ -735,7 +731,7 @@ end
735731

736732
# For AbstractVector{Bool}, counting sort is always best.
737733
# This is an implementation of counting sort specialized for Bools.
738-
# Accepts unused workspace to avoid method ambiguity.
734+
# Accepts unused buffer to avoid method ambiguity.
739735
function sort!(v::AbstractVector{B}, lo::Integer, hi::Integer, a::AdaptiveSort, o::Ordering,
740736
t::Union{AbstractVector{B}, Nothing}=nothing) where {B <: Bool}
741737
first = lt(o, false, true) ? false : lt(o, true, false) ? true : return v
@@ -856,15 +852,15 @@ function sort!(v::AbstractVector{T}, lo::Integer, hi::Integer, a::AdaptiveSort,
856852
u[i] -= u_min
857853
end
858854

859-
if t !== nothing && checkbounds(Bool, t, lo:hi) # Fully preallocated and aligned workspace
855+
if t !== nothing && checkbounds(Bool, t, lo:hi) # Fully preallocated and aligned buffer
860856
u2 = radix_sort!(u, lo, hi, bits, reinterpret(U, t))
861857
uint_unmap!(v, u2, lo, hi, o, u_min)
862-
elseif t !== nothing && (applicable(resize!, t) || length(t) >= hi-lo+1) # Viable workspace
858+
elseif t !== nothing && (applicable(resize!, t) || length(t) >= hi-lo+1) # Viable buffer
863859
length(t) >= hi-lo+1 || resize!(t, hi-lo+1)
864860
t1 = axes(t, 1) isa OneTo ? t : view(t, firstindex(t):lastindex(t))
865861
u2 = radix_sort!(view(u, lo:hi), 1, hi-lo+1, bits, reinterpret(U, t1))
866862
uint_unmap!(view(v, lo:hi), u2, 1, hi-lo+1, o, u_min)
867-
else # No viable workspace
863+
else # No viable buffer
868864
u2 = radix_sort!(u, lo, hi, bits, similar(u))
869865
uint_unmap!(v, u2, lo, hi, o, u_min)
870866
end
@@ -933,8 +929,8 @@ function sort!(v::AbstractVector{T};
933929
by=identity,
934930
rev::Union{Bool,Nothing}=nothing,
935931
order::Ordering=Forward,
936-
workspace::Union{AbstractVector{T}, Nothing}=nothing) where T
937-
sort!(v, alg, ord(lt,by,rev,order), workspace)
932+
buffer::Union{AbstractVector{T}, Nothing}=nothing) where T
933+
sort!(v, alg, ord(lt,by,rev,order), buffer)
938934
end
939935

940936
# sort! for vectors of few unique integers
@@ -1073,7 +1069,7 @@ function partialsortperm!(ix::AbstractVector{<:Integer}, v::AbstractVector,
10731069
order::Ordering=Forward,
10741070
initialized::Bool=false)
10751071
if axes(ix,1) != axes(v,1)
1076-
throw(ArgumentError("The index vector is used as a workspace and must have the " *
1072+
throw(ArgumentError("The index vector is used as a buffer and must have the " *
10771073
"same length/indices as the source vector, $(axes(ix,1)) != $(axes(v,1))"))
10781074
end
10791075
if !initialized
@@ -1140,7 +1136,7 @@ function sortperm(A::AbstractArray;
11401136
by=identity,
11411137
rev::Union{Bool,Nothing}=nothing,
11421138
order::Ordering=Forward,
1143-
workspace::Union{AbstractVector{<:Integer}, Nothing}=nothing,
1139+
buffer::Union{AbstractVector{<:Integer}, Nothing}=nothing,
11441140
dims...) #to optionally specify dims argument
11451141
ordr = ord(lt,by,rev,order)
11461142
if ordr === Forward && isa(A,Vector) && eltype(A)<:Integer
@@ -1155,7 +1151,7 @@ function sortperm(A::AbstractArray;
11551151
end
11561152
end
11571153
ix = copymutable(LinearIndices(A))
1158-
sort!(ix; alg, order = Perm(ordr, vec(A)), workspace, dims...)
1154+
sort!(ix; alg, order = Perm(ordr, vec(A)), buffer, dims...)
11591155
end
11601156

11611157

@@ -1201,15 +1197,15 @@ function sortperm!(ix::AbstractArray{T}, A::AbstractArray;
12011197
rev::Union{Bool,Nothing}=nothing,
12021198
order::Ordering=Forward,
12031199
initialized::Bool=false,
1204-
workspace::Union{AbstractVector{T}, Nothing}=nothing,
1200+
buffer::Union{AbstractVector{T}, Nothing}=nothing,
12051201
dims...) where T <: Integer #to optionally specify dims argument
12061202
(typeof(A) <: AbstractVector) == (:dims in keys(dims)) && throw(ArgumentError("Dims argument incorrect for type $(typeof(A))"))
12071203
axes(ix) == axes(A) || throw(ArgumentError("index array must have the same size/axes as the source array, $(axes(ix)) != $(axes(A))"))
12081204

12091205
if !initialized
12101206
ix .= LinearIndices(A)
12111207
end
1212-
sort!(ix; alg, order = Perm(ord(lt, by, rev, order), vec(A)), workspace, dims...)
1208+
sort!(ix; alg, order = Perm(ord(lt, by, rev, order), vec(A)), buffer, dims...)
12131209
end
12141210

12151211
# sortperm for vectors of few unique integers
@@ -1274,19 +1270,19 @@ function sort(A::AbstractArray{T};
12741270
by=identity,
12751271
rev::Union{Bool,Nothing}=nothing,
12761272
order::Ordering=Forward,
1277-
workspace::Union{AbstractVector{T}, Nothing}=similar(A, size(A, dims))) where T
1273+
buffer::Union{AbstractVector{T}, Nothing}=similar(A, size(A, dims))) where T
12781274
dim = dims
12791275
order = ord(lt,by,rev,order)
12801276
n = length(axes(A, dim))
12811277
if dim != 1
12821278
pdims = (dim, setdiff(1:ndims(A), dim)...) # put the selected dimension first
12831279
Ap = permutedims(A, pdims)
12841280
Av = vec(Ap)
1285-
sort_chunks!(Av, n, alg, order, workspace)
1281+
sort_chunks!(Av, n, alg, order, buffer)
12861282
permutedims(Ap, invperm(pdims))
12871283
else
12881284
Av = A[:]
1289-
sort_chunks!(Av, n, alg, order, workspace)
1285+
sort_chunks!(Av, n, alg, order, buffer)
12901286
reshape(Av, axes(A))
12911287
end
12921288
end
@@ -1335,21 +1331,21 @@ function sort!(A::AbstractArray{T};
13351331
by=identity,
13361332
rev::Union{Bool,Nothing}=nothing,
13371333
order::Ordering=Forward,
1338-
workspace::Union{AbstractVector{T}, Nothing}=similar(A, size(A, dims))) where T
1339-
_sort!(A, Val(dims), alg, ord(lt, by, rev, order), workspace)
1334+
buffer::Union{AbstractVector{T}, Nothing}=similar(A, size(A, dims))) where T
1335+
_sort!(A, Val(dims), alg, ord(lt, by, rev, order), buffer)
13401336
end
13411337
function _sort!(A::AbstractArray{T}, ::Val{K},
13421338
alg::Algorithm,
13431339
order::Ordering,
1344-
workspace::Union{AbstractVector{T}, Nothing}) where {K,T}
1340+
buffer::Union{AbstractVector{T}, Nothing}) where {K,T}
13451341
nd = ndims(A)
13461342

13471343
1 <= K <= nd || throw(ArgumentError("dimension out of range"))
13481344

13491345
remdims = ntuple(i -> i == K ? 1 : axes(A, i), nd)
13501346
for idx in CartesianIndices(remdims)
13511347
Av = view(A, ntuple(i -> i == K ? Colon() : idx[i], nd)...)
1352-
sort!(Av, alg, order, workspace)
1348+
sort!(Av, alg, order, buffer)
13531349
end
13541350
A
13551351
end

0 commit comments

Comments
 (0)