-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This redoes `reinterpret` in julia rather than punning the memory of the actual array. The motivation for this is to avoid the API limitations of the current reinterpret implementation (Array only, preventing strong TBAA, alignment problems). The surface API essentially unchanged, though the shape argument to reinterpret is removed, since those concepts are now orthogonal. The return type from `reinterpret` is now `ReinterpretArray`, which implements the AbstractArray interface and does the reinterpreting lazily on demand. The compiler is able to fold away the abstraction and generate very tight IR: ``` julia> ar = reinterpret(Complex{Int64}, rand(Int64, 1000)); julia> typeof(ar) Base.ReinterpretArray{Complex{Int64},Int64,1,Array{Int64,1}} julia> f(ar) = @inbounds return ar[1] f (generic function with 1 method) julia> @code_llvm f(ar) ; Function f ; Location: REPL[2] define void @julia_f_63575({ i64, i64 } addrspace(11)* noalias nocapture sret, %jl_value_t addrspace(10)* dereferenceable(8)) #0 { top: ; Location: REPL[2]:1 ; Function getindex; { ; Location: reinterpretarray.jl:31 %2 = addrspacecast %jl_value_t addrspace(10)* %1 to %jl_value_t addrspace(11)* %3 = bitcast %jl_value_t addrspace(11)* %2 to %jl_value_t addrspace(10)* addrspace(11)* %4 = load %jl_value_t addrspace(10)*, %jl_value_t addrspace(10)* addrspace(11)* %3, align 8 %5 = addrspacecast %jl_value_t addrspace(10)* %4 to %jl_value_t addrspace(11)* %6 = bitcast %jl_value_t addrspace(11)* %5 to i64* addrspace(11)* %7 = load i64*, i64* addrspace(11)* %6, align 8 %8 = load i64, i64* %7, align 8 %9 = getelementptr i64, i64* %7, i64 1 %10 = load i64, i64* %9, align 8 %.sroa.0.0..sroa_idx = getelementptr inbounds { i64, i64 }, { i64, i64 } addrspace(11)* %0, i64 0, i32 0 store i64 %8, i64 addrspace(11)* %.sroa.0.0..sroa_idx, align 8 %.sroa.3.0..sroa_idx13 = getelementptr inbounds { i64, i64 }, { i64, i64 } addrspace(11)* %0, i64 0, i32 1 store i64 %10, i64 addrspace(11)* %.sroa.3.0..sroa_idx13, align 8 ;} ret void } julia> g(a) = @inbounds return reinterpret(Complex{Int64}, a)[1] g (generic function with 1 method) julia> @code_llvm g(randn(1000)) ; Function g ; Location: REPL[4] define void @julia_g_63642({ i64, i64 } addrspace(11)* noalias nocapture sret, %jl_value_t addrspace(10)* dereferenceable(40)) #0 { top: ; Location: REPL[4]:1 ; Function getindex; { ; Location: reinterpretarray.jl:31 %2 = addrspacecast %jl_value_t addrspace(10)* %1 to %jl_value_t addrspace(11)* %3 = bitcast %jl_value_t addrspace(11)* %2 to double* addrspace(11)* %4 = load double*, double* addrspace(11)* %3, align 8 %5 = bitcast double* %4 to i64* %6 = load i64, i64* %5, align 8 %7 = getelementptr double, double* %4, i64 1 %8 = bitcast double* %7 to i64* %9 = load i64, i64* %8, align 8 %.sroa.0.0..sroa_idx = getelementptr inbounds { i64, i64 }, { i64, i64 } addrspace(11)* %0, i64 0, i32 0 store i64 %6, i64 addrspace(11)* %.sroa.0.0..sroa_idx, align 8 %.sroa.3.0..sroa_idx13 = getelementptr inbounds { i64, i64 }, { i64, i64 } addrspace(11)* %0, i64 0, i32 1 store i64 %9, i64 addrspace(11)* %.sroa.3.0..sroa_idx13, align 8 ;} ret void } ``` In addition, the new `reinterpret` implementation is able to handle any AbstractArray (whether useful or not is a separate decision): ``` invoke(reinterpret, Tuple{Type{Complex{Float64}}, AbstractArray}, Complex{Float64}, speye(10)) 5×10 Base.ReinterpretArray{Complex{Float64},Float64,2,SparseMatrixCSC{Float64,Int64}}: 1.0+0.0im 0.0+1.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 1.0+0.0im 0.0+1.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 1.0+0.0im 0.0+1.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 1.0+0.0im 0.0+1.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 0.0+0.0im 1.0+0.0im 0.0+1.0im ``` The remaining todo is to audit the uses of reinterpret in base. I've fixed up the uses themselves, but there's code deeper in the array code that needs to be broadened to allow ReinterpretArray. Fixes #22849 Fixes #19238
- Loading branch information
Showing
23 changed files
with
214 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
""" | ||
Gives a reinterpreted view (of element type T) of the underlying array (of element type S). | ||
If the size of `T` differs from the size of `S`, the array will be compressed/expanded in | ||
the first dimension. | ||
""" | ||
struct ReinterpretArray{T,N,S,A<:AbstractArray{S, N}} <: AbstractArray{T, N} | ||
parent::A | ||
function reinterpret(::Type{T}, a::A) where {T,N,S,A<:AbstractArray{S, N}} | ||
function throwbits(::Type{S}, ::Type{T}, ::Type{U}) where {S,T,U} | ||
@_noinline_meta | ||
throw(ArgumentError("cannot reinterpret `$(S)` `$(T)`, type `$(U)` is not a bits type")) | ||
end | ||
function throwsize0(::Type{S}, ::Type{T}) | ||
@_noinline_meta | ||
throw(ArgumentError("cannot reinterpret a zero-dimensional `$(S)` array to `$(T)` which is of a different size")) | ||
end | ||
function thrownonint(::Type{S}, ::Type{T}, dim) | ||
@_noinline_meta | ||
throw(ArgumentError(""" | ||
cannot reinterpret an `$(S)` array to `$(T)` whose first dimension has size `$(dim)`. | ||
The resulting array would have non-integral first dimension. | ||
""")) | ||
end | ||
isbits(T) || throwbits(S, T, T) | ||
isbits(S) || throwbits(S, T, S) | ||
(N != 0 || sizeof(T) == sizeof(S)) || throwsize0(S, T) | ||
if N != 0 && sizeof(S) != sizeof(T) | ||
dim = size(a)[1] | ||
rem(dim*sizeof(S),sizeof(T)) == 0 || thrownonint(S, T, dim) | ||
end | ||
new{T, N, S, A}(a) | ||
end | ||
end | ||
|
||
parent(a::ReinterpretArray) = a.parent | ||
|
||
eltype(a::ReinterpretArray{T}) where {T} = T | ||
function size(a::ReinterpretArray{T,N,S} where {N}) where {T,S} | ||
psize = size(a.parent) | ||
size1 = div(psize[1]*sizeof(S), sizeof(T)) | ||
tuple(size1, tail(psize)...) | ||
end | ||
|
||
unsafe_convert(::Type{Ptr{T}}, a::ReinterpretArray{T,N,S} where N) where {T,S} = Ptr{T}(unsafe_convert(Ptr{S},a.parent)) | ||
|
||
@inline @propagate_inbounds getindex(a::ReinterpretArray{T,0}) where {T} = reinterpret(T, a.parent[]) | ||
@inline @propagate_inbounds getindex(a::ReinterpretArray) = a[1] | ||
|
||
@inline @propagate_inbounds function getindex(a::ReinterpretArray{T,N,S}, inds::Vararg{Int, N}) where {T,N,S} | ||
if sizeof(T) == sizeof(S) | ||
return reinterpret(T, a.parent[inds...]) | ||
else | ||
ind_start, sidx = divrem((inds[1]-1)*sizeof(T), sizeof(S)) | ||
t = Ref{T}() | ||
s = Ref{S}() | ||
@gc_preserve t s begin | ||
tptr = Ptr{UInt8}(unsafe_convert(Ref{T}, t)) | ||
sptr = Ptr{UInt8}(unsafe_convert(Ref{S}, s)) | ||
i = 1 | ||
nbytes_copied = 0 | ||
# This is a bit complicated to deal with partial elements | ||
# at both the start and the end. LLVM will fold as appropriate, | ||
# once it knows the data layout | ||
while nbytes_copied < sizeof(T) | ||
s[] = a.parent[ind_start + i, tail(inds)...] | ||
while nbytes_copied < sizeof(T) && sidx < sizeof(S) | ||
unsafe_store!(tptr, unsafe_load(sptr, sidx + 1), nbytes_copied + 1) | ||
sidx += 1 | ||
nbytes_copied += 1 | ||
end | ||
sidx = 0 | ||
i += 1 | ||
end | ||
end | ||
return t[] | ||
end | ||
end | ||
|
||
@inline @propagate_inbounds setindex!(a::ReinterpretArray{T,0,S} where T, v) where {S} = (a.parent[] = reinterpret(S, v)) | ||
@inline @propagate_inbounds setindex!(a::ReinterpretArray, v) = (a[1] = v) | ||
|
||
@inline @propagate_inbounds function setindex!(a::ReinterpretArray{T,N,S}, v, inds::Vararg{Int, N}) where {T,N,S} | ||
v = convert(T, v)::T | ||
if sizeof(T) == sizeof(S) | ||
return setindex!(a.parent, reinterpret(S, v), inds...) | ||
else | ||
ind_start, sidx = divrem((inds[1]-1)*sizeof(T), sizeof(S)) | ||
t = Ref{T}(v) | ||
s = Ref{S}() | ||
@gc_preserve t s begin | ||
tptr = Ptr{UInt8}(unsafe_convert(Ref{T}, t)) | ||
sptr = Ptr{UInt8}(unsafe_convert(Ref{S}, s)) | ||
nbytes_copied = 0 | ||
i = 1 | ||
@inline function copy_element() | ||
while nbytes_copied < sizeof(T) && sidx < sizeof(S) | ||
unsafe_store!(sptr, unsafe_load(tptr, nbytes_copied + 1), sidx + 1) | ||
sidx += 1 | ||
nbytes_copied += 1 | ||
end | ||
end | ||
# Deal with any partial elements at the start. We'll have to copy in the | ||
# element from the original array and overwrite the relevant parts | ||
if sidx != 0 | ||
s[] = a.parent[ind_start + i, tail(inds)...] | ||
copy_element() | ||
a.parent[ind_start + i, tail(inds)...] = s[] | ||
i += 1 | ||
sidx = 0 | ||
end | ||
# Deal with the main body of elements | ||
while nbytes_copied < sizeof(T) && (sizeof(T) - nbytes_copied) > sizeof(S) | ||
copy_element() | ||
a.parent[ind_start + i, tail(inds)...] = s[] | ||
i += 1 | ||
sidx = 0 | ||
end | ||
# Deal with trailing partial elements | ||
if nbytes_copied < sizeof(T) | ||
s[] = a.parent[ind_start + i, tail(inds)...] | ||
copy_element() | ||
a.parent[ind_start + i, tail(inds)...] = s[] | ||
end | ||
end | ||
end | ||
return a | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.