Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Changed

* Cartesian `getindex` and `setindex!` with one `Int` index per dimension still compute the linear index in Horner form, but make every index zero-based first, `1 + (i1 - 1) + n1 * ((i2 - 1) + n2 * (...))`, instead of subtracting one from the partial sums. The number of integer operations is the same, but with Julia v1.12 and newer (LLVM 18 and newer) the loop vectorizer did not vectorize short loops over an index of a `FixedSizeArray` with the previous form.

## Version [v1.5.2](https://github.com/JuliaArrays/FixedSizeArrays.jl/releases/tag/v1.5.2) - 2026-11-08

### Fixed
Expand Down
22 changes: 14 additions & 8 deletions src/FixedSizeArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -352,17 +352,23 @@ function Base.isassigned(a::FixedSizeArrayDims, i::Int)
end

# Cartesian indexing: the column-major linear index of the (in-bounds) indices `inds` for
# the dimensions `dims` in Horner form, e.g. `i + n1 * ((j - 1) + n2 * (k - 1))` for the
# indices `(i, j, k)`, where the factors are constants as long as the dimensions are
# static. This needs fewer integer operations than a sum of strided terms
# the dimensions `dims` in Horner form, e.g. `1 + (i - 1) + n1 * ((j - 1) + n2 * (k - 1))`
# for the indices `(i, j, k)`, where the factors are constants as long as the dimensions
# are static. This needs fewer integer operations than a sum of strided terms
# (`1 + (i - 1) + n1 * (j - 1) + (n1 * n2) * (k - 1)`): in the flux differencing kernels
# of Trixi.jl, the volume integral is 2.5-3.5% faster.
function static_size_linear_index(dims::Tuple{Vararg{Any, N}}, inds::NTuple{N, Int}) where {N}
horner_index(map(dim_value, dims), inds)
end
horner_index(::Tuple{Any}, inds::Tuple{Int}) = inds[1]
function horner_index(ds::Tuple, inds::Tuple)
inds[1] + ds[1] * (horner_index(Base.tail(ds), Base.tail(inds)) - 1)
horner_offset(map(dim_value, dims), inds) + 1
end
# The zero-based offset `(i - 1) + n1 * ((j - 1) + n2 * (k - 1))`. Each index is made
# zero-based before it is used, instead of subtracting one from the partial sums
# (`i + n1 * ((j + n2 * (k - 1)) - 1)`, which has the same number of operations). The
# association matters for LLVM's loop vectorizer since Julia v1.12 (LLVM 18): with the
# latter form it does not vectorize short loops over an index, e.g. over the nodes of an
# element, while it does with the former.
horner_offset(::Tuple{Any}, inds::Tuple{Int}) = inds[1] - 1
function horner_offset(ds::Tuple, inds::Tuple)
(inds[1] - 1) + ds[1] * horner_offset(Base.tail(ds), Base.tail(inds))
end

# The linear index of the (in-bounds) indices `inds`: with the dimensions if there is one
Expand Down
26 changes: 26 additions & 0 deletions test/filecheck.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ fc_getindex_inbounds(A, i, j, k) = @inbounds A[i, j, k]
fc_node_sum(u, i, j, k) = @inbounds u[1, i, j, k] + u[2, i, j, k] + u[3, i, j, k] + u[4, i, j, k] + u[5, i, j, k]
const fc_node_val_dims_array = FixedSizeArrayDims{Float64, 4, Tuple{Val{5}, Int, Int, Int}, FixedSizeArrays.default_underlying_storage_type{Float64}}
fc_setindex_inbounds!(A, i, j, k, x) = (@inbounds A[i, j, k] = x; nothing)
# Short loops over the nodes of an element (one variable, or all of them), as in Trixi.jl.
fc_nodes_loop!(u, v, j, k) = (for i in axes(u, 2); @inbounds u[v, i, j, k] += 1.0; end; nothing)
fc_node_vars_loop!(u, j, k) = (for i in axes(u, 2), v in axes(u, 1); @inbounds u[v, i, j, k] += 1.0; end; nothing)

# Since Julia 1.13, if the Cartesian linear index makes every index zero-based first
# (`(i - 1) + n1 * ((j - 1) + n2 * (k - 1))`), LLVM can prove that the index does not wrap
# in loops over one of the indices. The loop vectorizer then needs no runtime check of
# that (`vector.scevcheck`) and no scalar fallback loop. Before 1.13 the IR is the same
# with both forms of the index.
const fc_run_horner_offset_checks = fc_run_bounds_check_elision_checks && VERSION >= v"1.13"

# In-place broadcasting.
fc_broadcast!(A) = (A .= A .+ A; nothing)
Expand Down Expand Up @@ -568,6 +578,22 @@ build_is_production_build &&
end
end

fc_run_horner_offset_checks &&
@testset "loops over the nodes of an element vectorize without a runtime check of the index" begin
@testset "$(f) $(A)" for (f, A, args) in (
(fc_nodes_loop!, fc_node_val_dims_array, (Int, Int, Int)),
(fc_node_vars_loop!, FixedSizeArrayDefault{Float64, 4}, (Int, Int)),
)
@test @filecheck begin
@check "define"
@check_not "vector.scevcheck"
@check fc_vector_body
@check_not "vector.scevcheck"
fc_llvm(f, (A, args...))
end
end
end

fc_run_bounds_check_elision_checks &&
@testset "in-place broadcasting: no allocation, no bounds check, vectorized" begin
@testset "$(A)" for A in (FixedSizeVectorDefault{Float64}, fc_static_dims_arrays...)
Expand Down
Loading