Skip to content

Commit ff295ca

Browse files
pablosanjosebarucden
authored andcommitted
Ensure elision of require_one_based_indexing with high-dim array views (#53091)
Closes #49332 --------- Co-authored-by: Denis Barucic <barucic.d@gmail.com> (cherry picked from commit 9edf1dd)
1 parent 1dfdf66 commit ff295ca

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

base/abstractarray.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,20 @@ end
103103
has_offset_axes(A, B, ...)
104104
105105
Return `true` if the indices of `A` start with something other than 1 along any axis.
106-
If multiple arguments are passed, equivalent to `has_offset_axes(A) | has_offset_axes(B) | ...`.
106+
If multiple arguments are passed, equivalent to `has_offset_axes(A) || has_offset_axes(B) || ...`.
107107
108108
See also [`require_one_based_indexing`](@ref).
109109
"""
110+
has_offset_axes() = false
110111
has_offset_axes(A) = _any_tuple(x->Int(first(x))::Int != 1, false, axes(A)...)
111112
has_offset_axes(A::AbstractVector) = Int(firstindex(A))::Int != 1 # improve performance of a common case (ranges)
112-
# Use `_any_tuple` to avoid unneeded invoke.
113-
# note: this could call `any` directly if the compiler can infer it
114-
has_offset_axes(As...) = _any_tuple(has_offset_axes, false, As...)
115113
has_offset_axes(::Colon) = false
116114
has_offset_axes(::Array) = false
115+
# note: this could call `any` directly if the compiler can infer it. We don't use _any_tuple
116+
# here because it stops full elision in some cases (#49332) and we don't need handling of
117+
# `missing` (has_offset_axes(A) always returns a Bool)
118+
has_offset_axes(A, As...) = has_offset_axes(A) || has_offset_axes(As...)
119+
117120

118121
"""
119122
require_one_based_indexing(A::AbstractArray)

test/abstractarray.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,13 +1825,17 @@ end
18251825

18261826
@testset "type-based offset axes check" begin
18271827
a = randn(ComplexF64, 10)
1828+
b = randn(ComplexF64, 4, 4, 4, 4)
18281829
ta = reinterpret(Float64, a)
18291830
tb = reinterpret(Float64, view(a, 1:2:10))
18301831
tc = reinterpret(Float64, reshape(view(a, 1:3:10), 2, 2, 1))
1832+
td = view(b, :, :, 1, 1)
18311833
# Issue #44040
18321834
@test IRUtils.fully_eliminated(Base.require_one_based_indexing, Base.typesof(ta, tc))
18331835
@test IRUtils.fully_eliminated(Base.require_one_based_indexing, Base.typesof(tc, tc))
18341836
@test IRUtils.fully_eliminated(Base.require_one_based_indexing, Base.typesof(ta, tc, tb))
1837+
# Issue #49332
1838+
@test IRUtils.fully_eliminated(Base.require_one_based_indexing, Base.typesof(td, td, td))
18351839
# Ranges && CartesianIndices
18361840
@test IRUtils.fully_eliminated(Base.require_one_based_indexing, Base.typesof(1:10, Base.OneTo(10), 1.0:2.0, LinRange(1.0, 2.0, 2), 1:2:10, CartesianIndices((1:2:10, 1:2:10))))
18371841
# Remind us to call `any` in `Base.has_offset_axes` once our compiler is ready.

0 commit comments

Comments
 (0)