Skip to content

Commit

Permalink
improve some type information to reduce backedges; fixes #29166
Browse files Browse the repository at this point in the history
reuse `Base._array_for` in lowering instead of manually inlining it
  • Loading branch information
JeffBezanson committed Feb 7, 2019
1 parent 8dee5bf commit ed6e3fc
Show file tree
Hide file tree
Showing 20 changed files with 58 additions and 57 deletions.
8 changes: 4 additions & 4 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ function copyto!(dest::AbstractArray, src)
y = iterate(destiter)
for x in src
y === nothing &&
throw(ArgumentError(string("destination has fewer elements than required")))
throw(ArgumentError("destination has fewer elements than required"))
dest[y[1]] = x
y = iterate(destiter, y[2])
end
Expand Down Expand Up @@ -700,7 +700,7 @@ function copyto!(dest::AbstractArray, dstart::Integer, src, sstart::Integer)
end
if y === nothing
throw(ArgumentError(string("source has fewer elements than required, ",
"expected at least ",sstart,", got ",sstart-1)))
"expected at least ",sstart,", got ",sstart-1)))
end
i = Int(dstart)
while y != nothing
Expand Down Expand Up @@ -1370,13 +1370,13 @@ _cs(d, a, b) = (a == b ? a : throw(DimensionMismatch(
"mismatch in dimension $d (expected $a got $b)")))

function dims2cat(::Val{n}) where {n}
n <= 0 && throw(ArgumentError("cat dimension must be a positive integer, but got $n"))
n <= 0 && throw(DomainError(n, "cat dimension must be a positive integer"))
ntuple(i -> (i == n), Val(n))
end

function dims2cat(dims)
if any(dims .<= 0)
throw(ArgumentError("All cat dimensions must be positive integers, but got $dims"))
throw(DomainError(dims, "All cat dimensions must be positive integers"))
end
ntuple(in(dims), maximum(dims))
end
Expand Down
2 changes: 1 addition & 1 deletion base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const ComplexF16 = Complex{Float16}
Complex{T}(x::Real) where {T<:Real} = Complex{T}(x,0)
Complex{T}(z::Complex) where {T<:Real} = Complex{T}(real(z),imag(z))
(::Type{T})(z::Complex) where {T<:Real} =
isreal(z) ? T(real(z))::T : throw(InexactError(Symbol(string(T)), T, z))
isreal(z) ? T(real(z))::T : throw(InexactError(nameof(T), T, z))

Complex(z::Complex) = z

Expand Down
4 changes: 2 additions & 2 deletions base/fastmath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ function make_fastmath(expr::Expr)
elseif isa(var, Expr) && var.head === :ref
# array reference
arr = var.args[1]
inds = tuple(var.args[2:end]...)
inds = var.args[2:end]
arrvar = gensym()
indvars = tuple([gensym() for i in inds]...)
indvars = Any[gensym() for i in inds]
expr = quote
$(Expr(:(=), arrvar, arr))
$(Expr(:(=), Expr(:tuple, indvars...), Expr(:tuple, inds...)))
Expand Down
6 changes: 3 additions & 3 deletions base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ function (::Type{T})(x::BigInt) where T<:Base.BitUnsigned
if sizeof(T) < sizeof(Limb)
convert(T, convert(Limb,x))
else
0 <= x.size <= cld(sizeof(T),sizeof(Limb)) || throw(InexactError(Symbol(string(T)), T, x))
0 <= x.size <= cld(sizeof(T),sizeof(Limb)) || throw(InexactError(nameof(T), T, x))
x % T
end
end
Expand All @@ -332,9 +332,9 @@ function (::Type{T})(x::BigInt) where T<:Base.BitSigned
SLimb = typeof(Signed(one(Limb)))
convert(T, convert(SLimb, x))
else
0 <= n <= cld(sizeof(T),sizeof(Limb)) || throw(InexactError(Symbol(string(T)), T, x))
0 <= n <= cld(sizeof(T),sizeof(Limb)) || throw(InexactError(nameof(T), T, x))
y = x % T
ispos(x) (y > 0) && throw(InexactError(Symbol(string(T)), T, x)) # catch overflow
ispos(x) (y > 0) && throw(InexactError(nameof(T), T, x)) # catch overflow
y
end
end
Expand Down
23 changes: 13 additions & 10 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,12 @@ const powers_of_ten = [
0x000000e8d4a51000, 0x000009184e72a000, 0x00005af3107a4000, 0x00038d7ea4c68000,
0x002386f26fc10000, 0x016345785d8a0000, 0x0de0b6b3a7640000, 0x8ac7230489e80000,
]
function ndigits0z(x::Base.BitUnsigned64)
function bit_ndigits0z(x::Base.BitUnsigned64)
lz = (sizeof(x)<<3)-leading_zeros(x)
nd = (1233*lz)>>12+1
nd -= x < powers_of_ten[nd]
end
function ndigits0z(x::UInt128)
function bit_ndigits0z(x::UInt128)
n = 0
while x > 0x8ac7230489e80000
x = div(x,0x8ac7230489e80000)
Expand All @@ -423,16 +423,20 @@ function ndigits0z(x::UInt128)
return n + ndigits0z(UInt64(x))
end

ndigits0z(x::BitSigned) = ndigits0z(unsigned(abs(x)))

ndigits0z(x::BitSigned) = bit_ndigits0z(unsigned(abs(x)))
ndigits0z(x::BitUnsigned) = bit_ndigits0z(x)
ndigits0z(x::Integer) = ndigits0zpb(x, 10)

## ndigits with specified base ##

# The suffix "nb" stands for "negative base"
function ndigits0znb(x::Integer, b::Integer)
# precondition: b < -1 && !(typeof(x) <: Unsigned)
d = 0
if x isa Unsigned
d += (x != 0)::Bool
x = -signed(fld(x, -b))
end
# precondition: b < -1 && !(typeof(x) <: Unsigned)
while x != 0
x = cld(x,b)
d += 1
Expand All @@ -441,7 +445,6 @@ function ndigits0znb(x::Integer, b::Integer)
end

# do first division before conversion with signed here, which can otherwise overflow
ndigits0znb(x::Unsigned, b::Integer) = ndigits0znb(-signed(fld(x, -b)), b) + (x != 0)
ndigits0znb(x::Bool, b::Integer) = x % Int

# The suffix "pb" stands for "positive base"
Expand All @@ -451,11 +454,11 @@ function ndigits0zpb(x::Integer, b::Integer)
b = Int(b)
x = abs(x)
if x isa Base.BitInteger
x = unsigned(x)
x = unsigned(x)::Unsigned
b == 2 && return sizeof(x)<<3 - leading_zeros(x)
b == 8 && return (sizeof(x)<<3 - leading_zeros(x) + 2) ÷ 3
b == 16 && return sizeof(x)<<1 - leading_zeros(x)>>2
b == 10 && return ndigits0z(x)
b == 10 && return bit_ndigits0z(x)
end

d = 0
Expand Down Expand Up @@ -595,7 +598,7 @@ const base62digits = ['0':'9';'A':'Z';'a':'z']

function _base(b::Integer, x::Integer, pad::Integer, neg::Bool)
(x >= 0) | (b < 0) || throw(DomainError(x, "For negative `x`, `b` must be negative."))
2 <= abs(b) <= 62 || throw(ArgumentError("base must satisfy 2 ≤ abs(base) ≤ 62, got $b"))
2 <= abs(b) <= 62 || throw(DomainError(b, "base must satisfy 2 ≤ abs(base) ≤ 62"))
digits = abs(b) <= 36 ? base36digits : base62digits
i = neg + ndigits(x, base=b, pad=pad)
a = StringVector(i)
Expand Down Expand Up @@ -745,7 +748,7 @@ julia> digits!([2,2,2,2,2,2], 10, base = 2)
```
"""
function digits!(a::AbstractVector{T}, n::Integer; base::Integer = 10) where T<:Integer
2 <= abs(base) || throw(ArgumentError("base must be ≥ 2 or ≤ -2, got $base"))
2 <= abs(base) || throw(DomainError(base, "base must be ≥ 2 or ≤ -2"))
hastypemax(T) && abs(base) - 1 > typemax(T) &&
throw(ArgumentError("type $T too small for base $base"))
isempty(a) && return a
Expand Down
2 changes: 1 addition & 1 deletion base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ function readline(filename::AbstractString; keep::Bool=false)
end
end

function readline(s::IO=stdin; keep::Bool=false)
function readline(s::IO=stdin; keep::Bool=false)::String
line = readuntil(s, 0x0a, keep=true)
i = length(line)
if keep || i == 0 || line[i] != 0x0a
Expand Down
2 changes: 1 addition & 1 deletion base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ function BigInt(x::BigFloat)
end

function (::Type{T})(x::BigFloat) where T<:Integer
isinteger(x) || throw(InexactError(Symbol(string(T)), T, x))
isinteger(x) || throw(InexactError(nameof(T), T, x))
trunc(T,x)
end

Expand Down
2 changes: 1 addition & 1 deletion base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Rational(x::Rational) = x
Bool(x::Rational) = x==0 ? false : x==1 ? true :
throw(InexactError(:Bool, Bool, x)) # to resolve ambiguity
(::Type{T})(x::Rational) where {T<:Integer} = (isinteger(x) ? convert(T, x.num) :
throw(InexactError(Symbol(string(T)), T, x)))
throw(InexactError(nameof(T), T, x)))

AbstractFloat(x::Rational) = float(x.num)/float(x.den)
function (::Type{T})(x::Rational{S}) where T<:AbstractFloat where S
Expand Down
4 changes: 2 additions & 2 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ julia> nameof(Foo.S{T} where T)
```
"""
nameof(t::DataType) = t.name.name
nameof(t::UnionAll) = nameof(unwrap_unionall(t))
nameof(t::UnionAll) = nameof(unwrap_unionall(t))::Symbol

"""
parentmodule(t::DataType) -> Module
Expand Down Expand Up @@ -1043,7 +1043,7 @@ end
Get the name of a generic `Function` as a symbol, or `:anonymous`.
"""
nameof(f::Function) = typeof(f).name.mt.name
nameof(f::Function) = (typeof(f).name.mt::Core.MethodTable).name

functionloc(m::Core.MethodInstance) = functionloc(m.def)

Expand Down
19 changes: 11 additions & 8 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,6 @@ end

show(io::IO, ::Core.TypeofBottom) = print(io, "Union{}")

function show(io::IO, x::Union)
print(io, "Union")
show_delim_array(io, uniontypes(x), '{', ',', '}', false)
end

function print_without_params(@nospecialize(x))
if isa(x,UnionAll)
b = unwrap_unionall(x)
Expand All @@ -424,7 +419,17 @@ function io_has_tvar_name(io::IOContext, name::Symbol, @nospecialize(x))
end
io_has_tvar_name(io::IO, name::Symbol, @nospecialize(x)) = false

function show(io::IO, x::UnionAll)
function show(io::IO, @nospecialize(x::Type))
if x isa DataType
show_datatype(io, x)
return
elseif x isa Union
print(io, "Union")
show_delim_array(io, uniontypes(x), '{', ',', '}', false)
return
end
x::UnionAll

if print_without_params(x)
return show(io, unwrap_unionall(x).name)
end
Expand All @@ -447,8 +452,6 @@ function show(io::IO, x::UnionAll)
show(io, x.var)
end

show(io::IO, x::DataType) = show_datatype(io, x)

# Check whether 'sym' (defined in module 'parent') is visible from module 'from'
# If an object with this name exists in 'from', we need to check that it's the same binding
# and that it's not deprecated.
Expand Down
7 changes: 3 additions & 4 deletions base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ prevind(s::AbstractString, i::Integer) = prevind(s, Int(i))
prevind(s::AbstractString, i::Int) = prevind(s, i, 1)

function prevind(s::AbstractString, i::Int, n::Int)
n < 0 && throw(ArgumentError("n cannot be negative: $n"))
n < 0 && throw(DomainError(n, "n cannot be negative"))
z = ncodeunits(s) + 1
@boundscheck 0 < i z || throw(BoundsError(s, i))
n == 0 && return thisind(s, i) == i ? i : string_index_err(s, i)
Expand Down Expand Up @@ -516,7 +516,7 @@ nextind(s::AbstractString, i::Integer) = nextind(s, Int(i))
nextind(s::AbstractString, i::Int) = nextind(s, i, 1)

function nextind(s::AbstractString, i::Int, n::Int)
n < 0 && throw(ArgumentError("n cannot be negative: $n"))
n < 0 && throw(DomainError(n, "n cannot be negative"))
z = ncodeunits(s)
@boundscheck 0 i z || throw(BoundsError(s, i))
n == 0 && return thisind(s, i) == i ? i : string_index_err(s, i)
Expand Down Expand Up @@ -571,8 +571,7 @@ function map(f, s::AbstractString)
for c in s
c′ = f(c)
isa(c′, AbstractChar) || throw(ArgumentError(
"map(f, s::AbstractString) requires f to return AbstractChar; " *
"try map(f, collect(s)) or a comprehension instead"))
"map(f, s::AbstractString) requires f to return AbstractChar; try map(f, collect(s)) or a comprehension instead"))
write(out, c′::AbstractChar)
end
String(take!(out))
Expand Down
4 changes: 2 additions & 2 deletions base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function print_to_string(xs...)
if isempty(xs)
return ""
end
siz = 0
siz::Int = 0
for x in xs
siz += tostr_sizehint(x)
end
Expand All @@ -135,7 +135,7 @@ function string_with_env(env, xs...)
if isempty(xs)
return ""
end
siz = 0
siz::Int = 0
for x in xs
siz += tostr_sizehint(x)
end
Expand Down
2 changes: 1 addition & 1 deletion base/strings/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ julia> repeat('A', 3)
repeat(c::AbstractChar, r::Integer) = repeat(Char(c), r) # fallback
function repeat(c::Char, r::Integer)
r == 0 && return ""
r < 0 && throw(ArgumentError("can't repeat a character $r times"))
r < 0 && throw(DomainError(r, "repeat count cannot be negative"))
u = bswap(reinterpret(UInt32, c))
n = 4 - (leading_zeros(u | 0xff) >> 3)
s = _string_n(n*r)
Expand Down
2 changes: 1 addition & 1 deletion base/strings/substring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function string(a::Union{Char, String, SubString{String}}...)
end

function repeat(s::Union{String, SubString{String}}, r::Integer)
r < 0 && throw(ArgumentError("can't repeat a string $r times"))
r < 0 && throw(DomainError(r, "repeat count cannot be negative"))
r == 1 && return String(s)
n = sizeof(s)
out = _string_n(n*r)
Expand Down
2 changes: 1 addition & 1 deletion base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ function ascii(s::String)
end
return s
end
@noinline __throw_invalid_ascii(s, i) = throw(ArgumentError("invalid ASCII at index $i in $(repr(s))"))
@noinline __throw_invalid_ascii(s::String, i::Int) = throw(ArgumentError("invalid ASCII at index $i in $(repr(s))"))

"""
ascii(s::AbstractString)
Expand Down
6 changes: 1 addition & 5 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2319,11 +2319,7 @@
(= ,szunk (call (core isa) ,isz (top SizeUnknown)))
(if ,szunk
(= ,result (call (curly (core Array) ,ty 1) (core undef) 0))
(if (call (core isa) ,isz (top HasShape))
(= ,result (call (top similar) (curly (core Array) ,ty) (call (top axes) ,overall-itr)))
(= ,result (call (curly (core Array) ,ty 1) (core undef) (call (core Int)
(|::| (call (top length) ,overall-itr)
(core Integer)))))))
(= ,result (call (top _array_for) ,ty ,overall-itr ,isz)))
(= ,idx (call (top first) (call (top LinearIndices) ,result)))
,(construct-loops (reverse itrs) (reverse iv))
,result)))))
Expand Down
6 changes: 3 additions & 3 deletions stdlib/Distributed/src/cluster.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mutable struct WorkerConfig
# Common fields relevant to all cluster managers
io::Union{IO, Nothing}
host::Union{AbstractString, Nothing}
port::Union{Integer, Nothing}
port::Union{Int, Nothing}

# Used when launching additional workers at a host
count::Union{Int, Symbol, Nothing}
Expand All @@ -21,13 +21,13 @@ mutable struct WorkerConfig
tunnel::Union{Bool, Nothing}
bind_addr::Union{AbstractString, Nothing}
sshflags::Union{Cmd, Nothing}
max_parallel::Union{Integer, Nothing}
max_parallel::Union{Int, Nothing}

# Used by Local/SSH managers
connect_at::Any

process::Union{Process, Nothing}
ospid::Union{Integer, Nothing}
ospid::Union{Int, Nothing}

# Private dictionary used to store temporary information by Local/SSH managers.
environ::Union{Dict, Nothing}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Distributed/src/managers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ end

# LocalManager
struct LocalManager <: ClusterManager
np::Integer
np::Int
restrict::Bool # Restrict binding to 127.0.0.1 only
end

Expand Down
2 changes: 1 addition & 1 deletion stdlib/LibGit2/src/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ end

function credentials_callback(libgit2credptr::Ptr{Ptr{Cvoid}}, url_ptr::Cstring,
username_ptr::Cstring, allowed_types::Cuint,
payloads::Dict)
payloads::Dict{Symbol, Any})
p = payloads[:credentials]
return credentials_callback(libgit2credptr, url_ptr, username_ptr, allowed_types, p)
end
Expand Down
Loading

0 comments on commit ed6e3fc

Please sign in to comment.