Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImmutableArrays #42465

Closed
wants to merge 58 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
fcbafaa
Implement ImmutableArray
Keno Apr 5, 2019
233bb0c
Implement maybecopy in BoundsError, start optimization, refactor memo…
Sep 29, 2021
424232a
Begin EscapeAnalysis.jl port
Nov 9, 2021
e031da8
Update memory_opt! for usage with EscapeState, begin port of EA.jl in…
Dec 21, 2021
c2ca5c5
Merge branch 'master' into HEAD
aviatesk Dec 21, 2021
49f9337
fixup definitions of array primitives
aviatesk Dec 21, 2021
b02e7c0
fixup `@noinline` annotations within `BoundsError`
aviatesk Dec 21, 2021
2a52e3d
disable EA global caching to succeed in sysimg building
aviatesk Dec 21, 2021
bd0bdb7
define EA in a separate module
aviatesk Dec 21, 2021
51120cf
Merge branch 'master' into kf/immutablearray
aviatesk Dec 23, 2021
4b33fc7
add simple `all`/`any` definitions to make `::BitSet ⊆ ::BitSet` work…
aviatesk Dec 23, 2021
cb4b1b8
comment dead code for now
aviatesk Dec 23, 2021
f59d58b
Merge branch 'master' into kf/immutablearray
aviatesk Dec 25, 2021
bca3078
update to latest EA
aviatesk Dec 25, 2021
2c466e1
update to latest EA
aviatesk Dec 26, 2021
235ab84
Restrict arrayfreeze/thaw arg # in base/compiler/tfuncs.jl
ianatol Dec 28, 2021
0e5833b
Restrict nargs of arrayfreeze/thaw to 1 in builtins.c
Dec 28, 2021
4a57838
Merge branch 'master' into kf/immutablearray
aviatesk Jan 4, 2022
de437d4
update EA and get array supports
aviatesk Jan 4, 2022
a34166c
fixup `memory_opt!`
aviatesk Jan 4, 2022
1b1babf
update EA and handle conflicting field information correctly
aviatesk Jan 4, 2022
eee43bd
minor optimization for `memory_opt!`
aviatesk Jan 4, 2022
9b8233a
define tfuncs for `ImmutableArray` primitives
aviatesk Jan 4, 2022
6fad97d
Correct maybecopy and begin implementing its optimization. Also some …
Jan 8, 2022
e1a1dc5
Merge branch 'master' into kf/immutablearray
aviatesk Jan 8, 2022
dd77189
update to latest EA
aviatesk Jan 8, 2022
39c925b
fixup `memory_opt!`
aviatesk Jan 8, 2022
fbc2c20
update new array tfuncs to accout for `ImmutableArray`
aviatesk Jan 9, 2022
5d93701
simplify `memory_opt!`
aviatesk Jan 9, 2022
19d5d20
Merge branch 'master' into kf/immutablearray
aviatesk Jan 9, 2022
b16190e
Merge branch 'master' into kf/immutablearray
aviatesk Jan 10, 2022
697b6f1
simplify
aviatesk Jan 10, 2022
0403a04
Merge branch 'master' into kf/immutablearray
aviatesk Jan 10, 2022
8507643
improve test implementations
aviatesk Jan 10, 2022
77eec88
Fixup unescaped1_1 test
Jan 10, 2022
c958ac9
Cleanup memory_opt, add some tests
Jan 11, 2022
c718fb9
avoid unintended name leak, better naming
aviatesk Jan 11, 2022
c4c7acf
Add some basic tests and move non-compiler tests
Jan 12, 2022
2438fd4
Merge branch 'master' into kf/immutablearray
aviatesk Jan 12, 2022
0d7d81a
add test cases https://github.com/aviatesk/EscapeAnalysis.jl/pull/72 …
aviatesk Jan 12, 2022
765f59c
Merge branch 'master' into kf/immutablearray
aviatesk Jan 13, 2022
150c490
update EA
aviatesk Jan 13, 2022
eaf5005
no broken tests now
aviatesk Jan 13, 2022
1dcfa1b
Merge branch 'master' into kf/immutablearray
aviatesk Jan 14, 2022
eb519c8
Merge branch 'master' into kf/immutablearray
aviatesk Jan 15, 2022
8110fad
update EA
aviatesk Jan 15, 2022
7b3c099
export `ImmutableArray`, define `ImmutableVector` alias
aviatesk Jan 15, 2022
d494cf2
use `IndexLinear` for immutable array
aviatesk Jan 15, 2022
de410f1
enable `test/immutablearray` testset
aviatesk Jan 15, 2022
d3c21bd
Merge branch 'master' into kf/immutablearray
aviatesk Jan 17, 2022
283c36b
fix tests
aviatesk Jan 18, 2022
eb221f6
Merge branch 'master' into kf/immutablearray
aviatesk Jan 18, 2022
10c161f
Merge branch 'master' into kf/immutablearray
aviatesk Jan 19, 2022
2560b73
update EA
aviatesk Jan 20, 2022
fe3717a
Merge branch 'master' into kf/immutablearray
aviatesk Jan 21, 2022
bbe8b3d
Merge branch 'master' into kf/immutablearray
aviatesk Jan 25, 2022
bca548f
Remove maybecopy code
Jan 27, 2022
df8bccc
Add tests
Jan 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,7 @@ function copy(a::AbstractArray)
@_propagate_inbounds_meta
copymutable(a)
end
copy(a::Core.ImmutableArray) = a

function copyto!(B::AbstractVecOrMat{R}, ir_dest::AbstractRange{Int}, jr_dest::AbstractRange{Int},
A::AbstractVecOrMat{S}, ir_src::AbstractRange{Int}, jr_src::AbstractRange{Int}) where {R,S}
Expand Down
55 changes: 48 additions & 7 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,42 @@ Union type of [`DenseVector{T}`](@ref) and [`DenseMatrix{T}`](@ref).
"""
const DenseVecOrMat{T} = Union{DenseVector{T}, DenseMatrix{T}}

"""
ImmutableArray{T,N} <: AbstractArray{T,N}

Dynamically allocated, immutable array.

"""
const ImmutableArray = Core.ImmutableArray

"""
ImmutableVector{T} <: AbstractVector{T}

Dynamically allocated, immutable vector.
"""
const ImmutableVector{T} = ImmutableArray{T,1}

"""
IMArray{T,N}

Union type of [`Array{T,N}`](@ref) and [`ImmutableArray{T,N}`](@ref)
"""
const IMArray{T,N} = Union{Array{T, N}, ImmutableArray{T,N}}

"""
IMVector{T}

One-dimensional [`ImmutableArray`](@ref) or [`Array`](@ref) with elements of type `T`. Alias for `IMArray{T, 1}`.
"""
const IMVector{T} = IMArray{T, 1}

"""
IMMatrix{T}

Two-dimensional [`ImmutableArray`](@ref) or [`Array`](@ref) with elements of type `T`. Alias for `IMArray{T,2}`.
"""
const IMMatrix{T} = IMArray{T, 2}

## Basic functions ##

import Core: arraysize, arrayset, arrayref, const_arrayref
Expand Down Expand Up @@ -147,12 +183,13 @@ function vect(X...)
return copyto!(Vector{T}(undef, length(X)), X)
end

size(a::Array, d::Integer) = arraysize(a, convert(Int, d))
size(a::Vector) = (arraysize(a,1),)
size(a::Matrix) = (arraysize(a,1), arraysize(a,2))
size(a::Array{<:Any,N}) where {N} = (@inline; ntuple(M -> size(a, M), Val(N))::Dims)
# Size functions for arrays, both mutable and immutable
size(a::IMArray, d::Integer) = arraysize(a, convert(Int, d))
size(a::IMVector) = (arraysize(a,1),)
size(a::IMMatrix) = (arraysize(a,1), arraysize(a,2))
size(a::IMArray{<:Any,N}) where {N} = (@inline; ntuple(M -> size(a, M), Val(N))::Dims)

asize_from(a::Array, n) = n > ndims(a) ? () : (arraysize(a,n), asize_from(a, n+1)...)
asize_from(a::IMArray, n) = n > ndims(a) ? () : (arraysize(a,n), asize_from(a, n+1)...)

allocatedinline(T::Type) = (@_pure_meta; ccall(:jl_stored_inline, Cint, (Any,), T) != Cint(0))

Expand Down Expand Up @@ -216,7 +253,7 @@ length(a::Array) = arraylen(a)
elsize(::Type{<:Array{T}}) where {T} = aligned_sizeof(T)
sizeof(a::Array) = Core.sizeof(a)

function isassigned(a::Array, i::Int...)
function isassigned(a::IMArray, i::Int...)
@inline
ii = (_sub2ind(size(a), i...) % UInt) - 1
@boundscheck ii < length(a) % UInt || return false
Expand Down Expand Up @@ -611,8 +648,8 @@ oneunit(x::AbstractMatrix{T}) where {T} = _one(oneunit(T), x)

## Conversions ##

convert(::Type{T}, a::AbstractArray) where {T<:Array} = a isa T ? a : T(a)
convert(::Type{Union{}}, a::AbstractArray) = throw(MethodError(convert, (Union{}, a)))
convert(T::Type{<:IMArray}, a::AbstractArray) = a isa T ? a : T(a)

promote_rule(a::Type{Array{T,n}}, b::Type{Array{S,n}}) where {T,n,S} = el_same(promote_type(T,S), a, b)

Expand All @@ -622,6 +659,7 @@ if nameof(@__MODULE__) === :Base # avoid method overwrite
# constructors should make copies
Array{T,N}(x::AbstractArray{S,N}) where {T,N,S} = copyto_axcheck!(Array{T,N}(undef, size(x)), x)
AbstractArray{T,N}(A::AbstractArray{S,N}) where {T,N,S} = copyto_axcheck!(similar(A,T), A)
ImmutableArray{T,N}(Ar::AbstractArray{S,N}) where {T,N,S} = Core.arrayfreeze(copyto_axcheck!(Array{T,N}(undef, size(Ar)), Ar))
end

## copying iterators to containers
Expand Down Expand Up @@ -921,6 +959,9 @@ function getindex end
@eval getindex(A::Array, i1::Int) = arrayref($(Expr(:boundscheck)), A, i1)
@eval getindex(A::Array, i1::Int, i2::Int, I::Int...) = (@inline; arrayref($(Expr(:boundscheck)), A, i1, i2, I...))

@eval getindex(A::ImmutableArray, i1::Int) = arrayref($(Expr(:boundscheck)), A, i1)
@eval getindex(A::ImmutableArray, i1::Int, i2::Int, I::Int...) = (@inline; arrayref($(Expr(:boundscheck)), A, i1, i2, I...))

# Faster contiguous indexing using copyto! for AbstractUnitRange and Colon
function getindex(A::Array, I::AbstractUnitRange{<:Integer})
@inline
Expand Down
13 changes: 13 additions & 0 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1345,4 +1345,17 @@ function Base.show(io::IO, op::BroadcastFunction)
end
Base.show(io::IO, ::MIME"text/plain", op::BroadcastFunction) = show(io, op)

struct IMArrayStyle <: Broadcast.AbstractArrayStyle{Any} end
BroadcastStyle(::Type{<:Core.ImmutableArray}) = IMArrayStyle()

#similar has to return mutable array
function Base.similar(bc::Broadcasted{IMArrayStyle}, ::Type{ElType}) where ElType
similar(Array{ElType}, axes(bc))
end

@inline function copy(bc::Broadcasted{IMArrayStyle})
ElType = combine_eltypes(bc.f, bc.args)
return Core.ImmutableArray(copyto!(similar(bc, ElType), bc))
end

end # module
Loading