Skip to content

Commit e57a7e4

Browse files
authored
Forward aliasing check for ReshapedArrays to the parent (#54168)
1 parent aad7245 commit e57a7e4

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

base/reshapedarray.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ elsize(::Type{<:ReshapedArray{<:Any,<:Any,P}}) where {P} = elsize(P)
225225

226226
unaliascopy(A::ReshapedArray) = typeof(A)(unaliascopy(A.parent), A.dims, A.mi)
227227
dataids(A::ReshapedArray) = dataids(A.parent)
228+
# forward the aliasing check the parent in case there are specializations
229+
mightalias(A::ReshapedArray, B::ReshapedArray) = mightalias(parent(A), parent(B))
230+
# special handling for reshaped SubArrays that dispatches to the subarray aliasing check
231+
mightalias(A::ReshapedArray, B::SubArray) = mightalias(parent(A), B)
232+
mightalias(A::SubArray, B::ReshapedArray) = mightalias(A, parent(B))
228233

229234
@inline ind2sub_rs(ax, ::Tuple{}, i::Int) = (i,)
230235
@inline ind2sub_rs(ax, strds, i) = _ind2sub_rs(ax, strds, i - 1)

test/subarray.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,3 +1120,19 @@ end
11201120
end
11211121
end
11221122
end
1123+
1124+
@testset "aliasing check with reshaped subarrays" begin
1125+
C = rand(2,1)
1126+
V1 = @view C[1, :]
1127+
V2 = @view C[2, :]
1128+
1129+
@test !Base.mightalias(V1, V2)
1130+
@test !Base.mightalias(V1, permutedims(V2))
1131+
@test !Base.mightalias(permutedims(V1), V2)
1132+
@test !Base.mightalias(permutedims(V1), permutedims(V2))
1133+
1134+
@test Base.mightalias(V1, V1)
1135+
@test Base.mightalias(V1, permutedims(V1))
1136+
@test Base.mightalias(permutedims(V1), V1)
1137+
@test Base.mightalias(permutedims(V1), permutedims(V1))
1138+
end

0 commit comments

Comments
 (0)