Repository navigation
Support 32-bit indices for 64-bit integer gathers - #1457
Conversation
| SUBCASE("masked") { Test.test_masked(); } | ||
| } | ||
|
|
||
| #if XSIMD_WITH_AVX2 |
There was a problem hiding this comment.
Such API should be genral across all arch maybe with a bit_cast/compress fallback. Then each arch can optimize this with specific intrinsics.
There was a problem hiding this comment.
Updated in e939403. The public overload now checks for one signed 32-bit index per 64-bit result lane. The common kernel accepts independent index and result architectures and reuses the existing per-lane gather. AVX2 still selects its intrinsic for the Velox index type.
The emulated<256> result / emulated<128> index regression failed to compile on the previous head and now passes all four lane checks, including the negative offset. The two AVX2 index types also pass, and the runtime-offset probe still emits vpgatherdq. I also cross-compiled the public call for a 256-bit SVE result with 128-bit NEON indices (compile-only).
DiamonDinoia
left a comment
There was a problem hiding this comment.
I will have to review the templating properly and possible pitfalls where the wrong overload is picked.
Some suggestions.
1.I would use if constexpr in the current gather kernels instead of sfinae
2. Optimized kernels per arch do not seem difficult to implement so we should do so to prevent providing a suboptimal api that catch the users by surprise
3. You could run locally using qemu if you compiled for different arch than your own. I will still double check locally and wait for ci to pass after that.
4. Please squash your commits. I think one commit is enough for this functionality.
Thanks for the contribution and addressing some initial comments. I would greatly appreciate if you could avoid using LLM in your replies as those response I prefer interacting with humans :)
I do not mind wrong grammar and typos slipping though. If it is a translation aid due to language barrier then I have no problem with it.
| CHECK_EQ(result.get(i), base[offsets[i]]); | ||
| } | ||
|
|
||
| #if XSIMD_WITH_AVX2 |
There was a problem hiding this comment.
If all architecture support this write the tests so that they don't require this macro.
There was a problem hiding this comment.
In 59dec01, I removed the AVX2 guard from the tests. They now run whenever the required batch types are available. The four-lane case also passes with AVX2 disabled. I moved the dispatch logic into the existing gather kernels and retested the existing calls. I’ve squashed the PR into a single commit.
e939403 to
59dec01
Compare
DiamonDinoia
left a comment
There was a problem hiding this comment.
I will have to think a bit more deeply about it. I am not sure the way is implemented is the simplest.
Definitely better this way. Though for the tests I would like to see the comination of T = [u]int64, V = [u]int32. As I am not sure that the current implementation does not break if there is no arch half the size of A. I am thinking of for example sse2 gather with 2 int 64 has not corresponding int32 batch.
| template <typename T, typename A, typename V, typename IndexArch> | ||
| XSIMD_INLINE batch<T, A> | ||
| gather(batch<T, A> const&, T const* src, batch<V, A> const& index, | ||
| gather(batch<T, A> const&, T const* src, batch<V, IndexArch> const& index, |
There was a problem hiding this comment.
So you are changing the Arch because now using a type that is half the size of T now you can use a different architecture for this.
There was a problem hiding this comment.
Yes, that’s why I used a separate IndexArch. Velox needs four 32-bit indices to gather four 64-bit values. The indices fit in an SSE2 register and the result fits in an AVX2 register.
In 4546e8c, I tested all four signed/unsigned combinations where both batch types are available. I also tried your SSE2-only example. There’s no native batch of two 32-bit indices there, so I tested the existing gather with two 64-bit indices. It returned the expected values for both int64_t and uint64_t.
59dec01 to
4546e8c
Compare
For cases like these we can upcast to the bigger type and do a normal gather. Or we can do a scalar emulation. We shall check the performance of the two approach and pick the fastest each time. Othrwise the user might have code with default arch that does not compile depending on the compile flags. |
| } | ||
|
|
||
| template <class T, class V, size_t N> | ||
| static void check_sized_integer_gather() |
There was a problem hiding this comment.
I think think this is the way. We just need to do gather with T and T/2 if sizeof(t)==8. I do not want the std::is_void_v because gather with int32 might not compile with some archs but compile with other and code that ships with defaul arch compiled with something ike march=native might not compile on another CPU.
There was a problem hiding this comment.
Gahter<[u]int64,[u]int32> and Gahter<[u]int64,[u]int64> must compile on all archs
There was a problem hiding this comment.
a105381 removes the is_void_v check. The same test now passes with 32-bit and 64-bit indices on both SSE2 and AVX2, including the signed and unsigned cases. In your SSE2 example, the two 64-bit results use the first two 32-bit indices.
4546e8c to
a105381
Compare
|
|
|
I do not know how to explain the changes I would like in code. But, I still think the code is a bit too complicated compared to what I would like, especially since I want humans to maintain this. I propose those changes: diff --git a/include/xsimd/arch/common/xsimd_common_memory.hpp b/include/xsimd/arch/common/xsimd_common_memory.hpp
index 81eb999a..3f62fbc9 100644
--- a/include/xsimd/arch/common/xsimd_common_memory.hpp
+++ b/include/xsimd/arch/common/xsimd_common_memory.hpp
@@ -156,24 +156,12 @@ namespace xsimd
}
} // namespace detail
- template <typename T, typename A, typename V, typename IndexArch>
+ template <typename T, typename A, typename V>
XSIMD_INLINE batch<T, A>
- gather(batch<T, A> const&, T const* src, batch<V, IndexArch> const& index,
+ gather(batch<T, A> const&, T const* src, batch<V, A> const& index,
kernel::requires_arch<common>) noexcept
{
- static_assert(detail::compatible_gather_sizes_v<T, A, V, IndexArch>, "Incompatible gather index size");
-
- if constexpr (std::is_same_v<A, IndexArch>)
- {
- return detail::gather<batch<T, A>::size - 1, T, A>(src, index, {});
- }
- else
- {
- // Both signed and unsigned 32-bit offsets are representable in int64_t.
- V offsets[batch<T, A>::size];
- index.store_unaligned(offsets);
- return batch<T, A>::gather(src, batch<int64_t, A>::load_unaligned(offsets));
- }
+ return detail::gather<batch<T, A>::size - 1, T, A>(src, index, {});
}
// Gather with runtime indexes and mismatched strides.
diff --git a/include/xsimd/arch/xsimd_avx2.hpp b/include/xsimd/arch/xsimd_avx2.hpp
index c09f3406..a86b021b 100644
--- a/include/xsimd/arch/xsimd_avx2.hpp
+++ b/include/xsimd/arch/xsimd_avx2.hpp
@@ -572,15 +572,14 @@ namespace xsimd
return _mm256_i32gather_epi32(reinterpret_cast<const int*>(src), index, sizeof(T));
}
- template <class T, class A, class U, class IndexArch, detail::enable_sized_integral_t<T, 8> = 0, detail::enable_integral_t<U> = 0>
- XSIMD_INLINE batch<T, A> gather(batch<T, A> const&, T const* src, batch<U, IndexArch> const& index,
+ template <class T, class A, class U, detail::enable_sized_integral_t<T, 8> = 0>
+ XSIMD_INLINE batch<T, A> gather(batch<T, A> const&, T const* src, batch<U, A> const& index,
kernel::requires_arch<avx2>) noexcept
{
- static_assert(detail::compatible_gather_sizes_v<T, A, U, IndexArch>, "Incompatible gather index size");
if constexpr (sizeof(U) == 4)
{
- __m128i narrow;
- std::memcpy(&narrow, &index.data, sizeof(narrow));
+ // vpgatherdq reads 4 int32 offsets: the low half of the index register
+ const __m128i narrow = _mm256_castsi256_si128(index);
if constexpr (std::is_signed_v<U>)
return _mm256_i32gather_epi64(reinterpret_cast<const long long int*>(src), narrow, sizeof(T));
else
diff --git a/include/xsimd/arch/xsimd_avx512f.hpp b/include/xsimd/arch/xsimd_avx512f.hpp
index ebee382b..4b025cb2 100644
--- a/include/xsimd/arch/xsimd_avx512f.hpp
+++ b/include/xsimd/arch/xsimd_avx512f.hpp
@@ -1190,15 +1190,14 @@ namespace xsimd
return _mm512_i32gather_epi32(index, static_cast<const void*>(src), sizeof(T));
}
- template <class T, class A, class U, class IndexArch, detail::enable_sized_integral_t<T, 8> = 0, detail::enable_integral_t<U> = 0>
- XSIMD_INLINE batch<T, A> gather(batch<T, A> const&, T const* src, batch<U, IndexArch> const& index,
+ template <class T, class A, class U, detail::enable_sized_integral_t<T, 8> = 0>
+ XSIMD_INLINE batch<T, A> gather(batch<T, A> const&, T const* src, batch<U, A> const& index,
kernel::requires_arch<avx512f>) noexcept
{
- static_assert(detail::compatible_gather_sizes_v<T, A, U, IndexArch>, "Incompatible gather index size");
if constexpr (sizeof(U) == 4)
{
- __m256i narrow;
- std::memcpy(&narrow, &index.data, sizeof(narrow));
+ // vpgatherdq reads 8 int32 offsets: the low half of the index register
+ const __m256i narrow = _mm512_castsi512_si256(index);
if constexpr (std::is_signed_v<U>)
return _mm512_i32gather_epi64(narrow, static_cast<const void*>(src), sizeof(T));
else
diff --git a/include/xsimd/arch/xsimd_rvv.hpp b/include/xsimd/arch/xsimd_rvv.hpp
index 977d1afc..e2ed58cf 100644
--- a/include/xsimd/arch/xsimd_rvv.hpp
+++ b/include/xsimd/arch/xsimd_rvv.hpp
@@ -553,46 +553,29 @@ namespace xsimd
}
// gather
- template <class A, class T, class U, class IndexArch, std::enable_if_t<sizeof(T) == 4 || sizeof(T) == 8, int> = 0>
- XSIMD_INLINE batch<T, A> gather(batch<T, A> const&, T const* src, batch<U, IndexArch> const& index, kernel::requires_arch<rvv>) noexcept
+ template <class A, class T, class U, std::enable_if_t<sizeof(T) == 4 || sizeof(T) == 8, int> = 0>
+ XSIMD_INLINE batch<T, A> gather(batch<T, A> const&, T const* src, batch<U, A> const& index, kernel::requires_arch<rvv>) noexcept
{
- constexpr auto lanes = batch<T, A>::size;
- static_assert(lanes == batch<U, IndexArch>::size, "Index and destination sizes must match");
- if constexpr (std::is_same_v<A, IndexArch>)
+ if constexpr (sizeof(T) == 8 && sizeof(U) == 4)
+ {
+ // 64-bit lanes with 32-bit offsets: widen the low size offsets
+ using indices = batch<widen_t<U>, A>;
+ const auto wide = [&]()
+ {
+ if constexpr (std::is_signed_v<U>)
+ return __riscv_vsext_vf2_i64m1(__riscv_vlmul_trunc_i32mf2(index.data), indices::size);
+ else
+ return __riscv_vzext_vf2_u64m1(__riscv_vlmul_trunc_u32mf2(index.data), indices::size);
+ }();
+ return batch<T, A>::gather(src, indices(typename indices::register_type(wide)));
+ }
+ else
{
const auto uindex = detail_rvv::rvv_to_unsigned_batch(index);
auto const* base = reinterpret_cast<map_to_sized_type_t<T> const*>(src);
const auto bi = detail_rvv::rvvmul_splat(uindex, sizeof(T));
return detail_rvv::rvvloxei(base, bi);
}
- else
- {
- using indices = batch<widen_t<U>, A>;
- map_to_sized_type_t<U> offsets[lanes];
- index.store_unaligned(offsets);
- const auto wide = [&]()
- {
- if constexpr (std::is_signed_v<U>)
- return __riscv_vsext_vf2_i64m1(__riscv_vle32_v_i32mf2(offsets, lanes), lanes);
- else
- return __riscv_vzext_vf2_u64m1(__riscv_vle32_v_u32mf2(offsets, lanes), lanes);
- }();
- return batch<T, A>::gather(src, indices(typename indices::register_type(wide)));
- }
- }
-
- template <class A, class T, class U, detail::enable_sized_integral_t<T, 8> = 0, detail::enable_sized_integral_t<U, 4> = 0>
- XSIMD_INLINE batch<T, A> gather(batch<T, A> const&, T const* src, batch<U, A> const& index, kernel::requires_arch<rvv>) noexcept
- {
- using indices = batch<widen_t<U>, A>;
- const auto wide = [&]()
- {
- if constexpr (std::is_signed_v<U>)
- return __riscv_vsext_vf2_i64m1(__riscv_vlmul_trunc_i32mf2(index.data), indices::size);
- else
- return __riscv_vzext_vf2_u64m1(__riscv_vlmul_trunc_u32mf2(index.data), indices::size);
- }();
- return batch<T, A>::gather(src, indices(typename indices::register_type(wide)));
}
/**************
diff --git a/include/xsimd/arch/xsimd_sve.hpp b/include/xsimd/arch/xsimd_sve.hpp
index 5095debc..09ebf134 100644
--- a/include/xsimd/arch/xsimd_sve.hpp
+++ b/include/xsimd/arch/xsimd_sve.hpp
@@ -205,7 +205,7 @@ namespace xsimd
// gather
template <class A, class T, class U,
- std::enable_if_t<(sizeof(T) == 4 || sizeof(T) == 8) && detail::compatible_gather_sizes_v<T, A, U, A>, int> = 0>
+ std::enable_if_t<(sizeof(T) == 4 || sizeof(T) == 8), int> = 0>
XSIMD_INLINE batch<T, A> gather(batch<T, A> const&, T const* src, batch<U, A> const& index, kernel::requires_arch<sve>) noexcept
{
const auto* base = reinterpret_cast<map_to_sized_type_t<T> const*>(src);
diff --git a/include/xsimd/types/xsimd_batch.hpp b/include/xsimd/types/xsimd_batch.hpp
index a8ba8040..17c25454 100644
--- a/include/xsimd/types/xsimd_batch.hpp
+++ b/include/xsimd/types/xsimd_batch.hpp
@@ -26,13 +26,6 @@ namespace xsimd
{
namespace detail
{
- template <class T, class A, class V, class IndexArch>
- using enable_narrow_gather_t = std::enable_if_t<
- IndexArch::supported() && std::is_integral_v<T> && sizeof(T) == 8
- && std::is_integral_v<V> && sizeof(V) == 4
- && batch<T, A>::size == batch<V, IndexArch>::size,
- int>;
-
// Does batch<T, A> have a real predicated masked load/store, vs. the
// emulated scalar `requires_arch<common>` fallback? Compile-time signal to
// pick a masked single-loop over a vector+trailer loop. Size-keyed.
@@ -303,8 +296,6 @@ namespace xsimd
template <class U, class V>
[[nodiscard]] static XSIMD_INLINE batch gather(U const* src, batch<V, arch_type> const& index) noexcept;
- template <class IndexArch, class V, detail::enable_narrow_gather_t<T, A, V, IndexArch> = 0>
- [[nodiscard]] static XSIMD_INLINE batch gather(T const* src, batch<V, IndexArch> const& index) noexcept;
template <class U, class V>
XSIMD_INLINE void scatter(U* dst, batch<V, arch_type> const& index) const noexcept;
@@ -928,14 +919,8 @@ namespace xsimd
{
detail::static_check_supported_config<T, A>();
static_assert(std::is_convertible_v<T, U>, "Can't convert from src to this batch's type!");
- return kernel::gather(batch {}, src, index, A {});
- }
-
- /** Gather 64-bit integers using one 32-bit integer element offset per result lane. */
- template <class T, class A>
- template <class IndexArch, class V, detail::enable_narrow_gather_t<T, A, V, IndexArch>>
- XSIMD_INLINE batch<T, A> batch<T, A>::gather(T const* src, batch<V, IndexArch> const& index) noexcept
- {
+ static_assert(batch<V, A>::size == batch<T, A>::size || (sizeof(T) == 8 && sizeof(V) == 4),
+ "gather rule: the index batch needs one offset per result lane, or 32-bit offsets for 64-bit elements (the low size offsets are used)");
return kernel::gather(batch {}, src, index, A {});
}
diff --git a/include/xsimd/types/xsimd_utils.hpp b/include/xsimd/types/xsimd_utils.hpp
index ed3b38f5..f339c5de 100644
--- a/include/xsimd/types/xsimd_utils.hpp
+++ b/include/xsimd/types/xsimd_utils.hpp
@@ -243,11 +243,6 @@ namespace xsimd
* Matching & mismatching sizes *
********************************/
- template <class T, class A, class V, class IndexArch>
- inline constexpr bool compatible_gather_sizes_v = batch<T, A>::size == batch<V, IndexArch>::size
- || (std::is_same_v<A, IndexArch> && std::is_integral_v<T> && sizeof(T) == 8
- && std::is_integral_v<V> && sizeof(V) == 4);
-
template <class T, class U, class B = int>
using sizes_match_t = std::enable_if_t<sizeof(T) == sizeof(U), B>;
diff --git a/test/test_load_store.cpp b/test/test_load_store.cpp
index d032e4e6..2ad9b6f9 100644
--- a/test/test_load_store.cpp
+++ b/test/test_load_store.cpp
@@ -15,6 +15,7 @@
#include "test_utils.hpp"
#include <algorithm>
+#include <numeric>
#include <random>
#include <type_traits>
@@ -178,6 +179,13 @@ struct load_store_test
#if !XSIMD_WITH_NEON || XSIMD_WITH_NEON64
test_gather_impl(d_vec, "gather double");
#endif
+ if constexpr (sizeof(value_type) == 8 && std::is_integral_v<value_type>)
+ {
+ test_gather_indexed<xsimd::batch<int64_t, typename B::arch_type>>("int64_t");
+ test_gather_indexed<xsimd::batch<uint64_t, typename B::arch_type>>("uint64_t");
+ test_gather_indexed<xsimd::batch<int32_t, typename B::arch_type>>("int32_t");
+ test_gather_indexed<xsimd::batch<uint32_t, typename B::arch_type>>("uint32_t");
+ }
}
void test_scatter()
@@ -402,6 +410,22 @@ private:
CHECK_BATCH_EQ(b, expected);
}
+ template <class I>
+ void test_gather_indexed(const std::string& index_name)
+ {
+ using index_value_type = typename I::value_type;
+ array_type data;
+ std::iota(data.begin(), data.end(), value_type(0));
+ std::reverse_copy(data.cbegin(), data.cend(), expected.begin());
+ std::array<index_value_type, I::size> offsets { }; // unused 32-bit lanes stay 0: catch a gather that reads the wrong index lanes
+ const index_value_type start = std::is_signed_v<index_value_type> ? index_value_type(0) : index_value_type(size - 1);
+ const value_type* base = std::is_signed_v<index_value_type> ? data.data() + size - 1 : data.data(); // negative offsets: catch a zero-extended index
+ for (std::size_t i = 0; i < size; ++i)
+ offsets[i] = start - static_cast<index_value_type>(i); // offsets count down: a plain load cannot match
+ INFO("gather 64-bit integers with ", index_name, " index");
+ CHECK_BATCH_EQ(batch_type::gather(base, I::load_unaligned(offsets.data())), expected);
+ }
+
template <class V>
void test_scatter_impl(const V& v, const std::string& name)
{
@@ -461,46 +485,6 @@ TEST_CASE_TEMPLATE("[load store]", B, BATCH_TYPES)
SUBCASE("masked") { Test.test_masked(); }
}
-template <class Values, class Indices>
-static void check_integer_gather()
-{
- using T = typename Values::value_type;
- using V = typename Indices::value_type;
- static_assert(Indices::size >= Values::size);
- const T data[] = { T(0x100000001), T(0), T(-0x200000004), T(0x300000006) };
- const V active_offsets[] = { 2, 0, 2, 1 };
- V offsets[Indices::size];
- for (std::size_t i = 0; i < Indices::size; ++i)
- {
- // Extra index lanes point at a different value from every active lane.
- offsets[i] = i < Values::size ? active_offsets[i % 4] : V(3);
- }
- auto index = Indices::load_unaligned(offsets);
- auto base = data;
- if constexpr (std::is_signed_v<V>)
- {
- base += 2;
- index -= Indices(2);
- }
- const auto result = Values::gather(base, index);
- for (std::size_t i = 0; i < Values::size; ++i)
- CHECK_EQ(result.get(i), data[offsets[i]]);
-}
-
-TEST_CASE_TEMPLATE("Gather 64-bit integers", T, int64_t, uint64_t)
-{
- using B = xsimd::batch<T>;
- check_integer_gather<B, xsimd::batch<int32_t>>();
- check_integer_gather<B, xsimd::batch<uint32_t>>();
- check_integer_gather<B, xsimd::batch<int64_t>>();
- check_integer_gather<B, xsimd::batch<uint64_t>>();
-#if XSIMD_WITH_EMULATED
- using values = xsimd::batch<T, xsimd::emulated<128>>;
- check_integer_gather<values, xsimd::batch<int32_t, xsimd::emulated<64>>>();
- check_integer_gather<values, xsimd::batch<uint32_t, xsimd::emulated<64>>>();
-#endif
-}
-
template <class B>
struct masked_load_store_test
{ |
a105381 to
16e5ec8
Compare
|
@DiamonDinoia, I used your same-arch gather and load/store test changes in 16e5ec8. I kept one integer-only overload because Velox passes four 32-bit offsets in an SSE register directly to the AVX2 gather. That call fails to compile with just the same-arch overload. |
DiamonDinoia
left a comment
There was a problem hiding this comment.
Hi,
- Remove the second
gatheroverload inxsimd_batch.hpp, the one that takesbatch<V, IndexArch>. I still do not understand where you need this. - Remove
detail::enable_avx2_half_gather_t. I do not see where it is used. - Remove the avx2 kernel overload that widens SSE indices with
_mm256_castsi128_si256and forwards to the same-arch kernel. - In
test_load_store.cpp, remove the AVX2-only branch with the twoxsimd::sse2index cases. Also remove the arch template parameter oftest_gather_indexed, so the helper usestypename B::arch_type.
|
Why Velox can't do: // offsets is an int32_t const*
auto idx = xsimd::batch<int64_t>::load_unaligned(offsets);
auto r = xsimd::batch<int64_t>::gather(base, idx);After the changes I suggested. |
16e5ec8 to
eaf147a
Compare
|
@DiamonDinoia, I tried your |
DiamonDinoia
left a comment
There was a problem hiding this comment.
I like how this looks now both code and tests I understand.
Sorry for the back and forth but I do not merge code I do not understand.
If CI passes this is mergeable. Otherwise, could you fix the issues?
|
Thanks for the review, @DiamonDinoia. I checked the Windows build on |
Summary
Gathering 64-bit integers with a same-architecture batch of 32-bit offsets fails on the base revision because the index batch has twice as many lanes as the result. This change lets the existing
batch::gatheruse the firstresult_type::sizeoffsets in lane order. The source and result must have the same 64-bit integer type. The remaining offsets cause no source reads. Loading the index batch itself still requires a full batch of readable offsets. Refs #751.On AVX2, signed 32-bit offsets use
vpgatherdq. Unsigned offsets are zero-extended beforevpgatherqqso offsets with bit 31 set stay positive. The common fallback loads one element per result lane. SSE2 fallback measurementsFor callers starting with an
int32_t const*, the existing widening load also works:auto idx = xsimd::batch<int64_t>::load_unaligned(offsets);followed byxsimd::batch<int64_t>::gather(base, idx). I checked this on the base revision and with this patch. Passing an SSE index batch directly to an AVX2 result remains outside the scope of this PR. Velox exampleTests
From the repository root, set
DOCTEST_INCLUDEto the directory containingdoctest/doctest.hand runAI assistance
The contributor selected the use case, reviewed the scope with the maintainer and requested the compatibility checks. OpenAI Codex revised the code and tests, ran the checks and benchmarks, and drafted this description.