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

Improve handling of aliasing of AbstractSlices #49182

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion NEWS.md
Expand Up @@ -6,7 +6,7 @@ New language features

Language changes
----------------

* `AbstractSlice` exposes `dataids` of its parent array to properly identify aliasing in broadcasting ([#49182]).

Compiler/Runtime improvements
-----------------------------
Expand Down
4 changes: 4 additions & 0 deletions base/slicearray.jl
Expand Up @@ -44,6 +44,10 @@ function Slices(A::P, slicemap::SM, ax::AX) where {P,SM,AX}
Slices{P,SM,AX,S,N}(A, slicemap, ax)
end

dataids(C::AbstractSlices) = dataids(parent(C))
unaliascopy(A::Slices{P,SM,AX,S,N}) where {P,SM,AX,S,N} =
bkamins marked this conversation as resolved.
Show resolved Hide resolved
Slices{P,SM,AX,S,N}(unaliascopy(A.parent), A.slicemap, A.axes)

_slice_check_dims(N) = nothing
function _slice_check_dims(N, dim, dims...)
1 <= dim <= N || throw(DimensionMismatch("Invalid dimension $dim"))
Expand Down
2 changes: 2 additions & 0 deletions doc/src/manual/interfaces.md
Expand Up @@ -442,6 +442,8 @@ V = view(A, [1,2,4], :) # is not strided, as the spacing between rows is not f
| `Base.BroadcastStyle(::Style1, ::Style2) = Style12()` | Precedence rules for mixing styles |
| `Base.axes(x)` | Declaration of the indices of `x`, as per [`axes(x)`](@ref). |
| `Base.broadcastable(x)` | Convert `x` to an object that has `axes` and supports indexing |
| `Base.dataids(x::AbstractArray)` | Return a tuple of `UInt`s that represent the mutable data segments of an array `x`. |
| `Base.unaliascopy(x::AbstractArray)` | Make a preventative copy of an array `x` in an operation where `x` `Base.mightalias` against another array. |
| **Bypassing default machinery** | |
| `Base.copy(bc::Broadcasted{DestStyle})` | Custom implementation of `broadcast` |
| `Base.copyto!(dest, bc::Broadcasted{DestStyle})` | Custom implementation of `broadcast!`, specializing on `DestStyle` |
Expand Down
9 changes: 9 additions & 0 deletions test/abstractarray.jl
Expand Up @@ -1823,6 +1823,15 @@ end
@test_broken IRUtils.fully_eliminated(_has_offset_axes, Base.typesof(a, a, b, b))
end

@testset "aliasing of slices" begin
x = [1 2; 3 4]
@test Base.mightalias(x, eachrow(x))
@test Base.mightalias(eachrow(x), x)
@test Base.mightalias(eachcol(x), eachrow(x))
bkamins marked this conversation as resolved.
Show resolved Hide resolved
x .+= sum.(eachrow(x))
@test x == [4 5; 10 11]
end

# type stable [x;;] (https://github.com/JuliaLang/julia/issues/45952)
f45952(x) = [x;;]
@inferred f45952(1.0)