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

Support views on empty UnsafeArrays #22

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/CompatHelper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
run: which julia
continue-on-error: true
- name: Install Julia, but only if it is not already available in the PATH
uses: julia-actions/setup-julia@v1
uses: julia-actions/setup-julia@v2
with:
version: '1'
arch: ${{ runner.arch }}
Expand Down
10 changes: 6 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,21 @@ jobs:
os: windows-latest
arch: x64
steps:
- uses: actions/checkout@v3
- uses: julia-actions/setup-julia@v1
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v2
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: julia-actions/cache@v1
- uses: julia-actions/cache@v2
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
with:
coverage: ${{ matrix.version == '1' && matrix.os == 'ubuntu-latest' && matrix.arch == 'x64' }}
- uses: julia-actions/julia-processcoverage@v1
if: matrix.version == '1' && matrix.os == 'ubuntu-latest' && matrix.arch == 'x64'
- uses: codecov/codecov-action@v3
- uses: codecov/codecov-action@v4
if: matrix.version == '1' && matrix.os == 'ubuntu-latest' && matrix.arch == 'x64'
with:
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
file: lcov.info
10 changes: 9 additions & 1 deletion src/unsafe_array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,20 @@ Base.@propagate_inbounds _unsafe_view_impl(IFwd::NTuple{M,Base.ViewIndex}, A::Un
_unsafe_view_impl((IFwd..., i), A, I...)

@inline function _unsafe_view_impl(IFwd::NTuple{M,Base.Slice}, A::UnsafeArray{T,N}, i::DenseIdx, I::Integer...) where {T,M,N}
ax_A = axes(A)
@assert IFwd == ntuple(i -> axes(A)[i], Val{M}())
I_all = (IFwd..., i, I...)
@boundscheck checkbounds(A, I_all...)
startidxs = map(first, (IFwd..., i, I...))
firstidxs = map(first, ax_A)
sub_s = _sub_size(I_all...)
p = pointer(A, LinearIndices(size(A))[startidxs...])
A_isempty = length(A) == 0
view_isempty = prod(sub_s) == 0
# getindex on LinearIndices fails with startidxs if A is empty, so use pseudo-size in that case:
sz = size(A)
pseudo_sz = ntuple(_ -> 1, Val(N))
mod_size, mod_startidxs = ifelse(A_isempty && view_isempty, (pseudo_sz, firstidxs), (sz, startidxs))
p = pointer(A, LinearIndices(mod_size)[mod_startidxs...])
UnsafeArray(p, sub_s)
end

Expand Down
10 changes: 10 additions & 0 deletions src/uview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,23 @@ function Base.mightalias(A::UnsafeArray, B::AbstractArray)
if typeof(B) <: UnsafeArray
Base.mightalias(A, B)
else
# Don't know if A and B might alias:
false
end
end
end

Base.mightalias(A::AbstractArray, B::UnsafeArray) = Base.mightalias(B, A)

# Need to specialize mightalias SubArrays of UnsafeArrays since the
# default mightalias implementation returns false for all isbits types:

Base.mightalias(A::SubArray{T,N,<:UnsafeArray}, B::AbstractArray) where {T,N} =
Base.mightalias(parent(A), B)

Base.mightalias(A::SubArray{T,N,<:UnsafeArray}, B::SubArray) where {T,N} =
Base.mightalias(parent(A), B)

Base.mightalias(A::SubArray{T1,N1,<:UnsafeArray}, B::SubArray{T2,N2,<:UnsafeArray}) where {T1,N1,T2,N2} =
Base.mightalias(parent(A), parent(B))

Expand All @@ -173,5 +180,8 @@ Base.mightalias(A::SubArray{T,N,<:UnsafeArray}, B::UnsafeArray) where {T,N} =
Base.mightalias(A::AbstractArray, B::SubArray{T,N,<:UnsafeArray}) where {T,N} =
Base.mightalias(B, A)

Base.mightalias(A::SubArray, B::SubArray{T,N,<:UnsafeArray}) where {T,N} =
Base.mightalias(B, A)

Base.mightalias(A::UnsafeArray, B::SubArray{T,N,<:UnsafeArray}) where {T,N} =
Base.mightalias(B, A)
12 changes: 12 additions & 0 deletions test/unsafe_array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ using Random
end


function test_empty_A_UA(test_code::Function, ::Type{T}, Val_N::Val{N}) where {T, N}
A = rand(T, 0, fill(4, N-1)...)
UA = UnsafeArray(pointer(A), size(A))
UnsafeArrays.@gc_preserve A test_code(A, UA)
end


@testset "ctors" begin
test_A(Float64, Val(0)) do A
ptr = pointer(A)
Expand Down Expand Up @@ -146,6 +153,11 @@ using Random
@test isbits(view(UA, :, :, :)) == true
@test isbits(view(UA, :, 3, :)) == true
end

test_empty_A_UA(Float32, Val(2)) do A, UA
@test @inferred(view(UA, :, 2:3)) isa UnsafeArray
@test size(@inferred(view(UA, :, 2:3))) == (0, 2)
end
end


Expand Down
Loading