Skip to content

Commit 078ca7c

Browse files
committed
TODO
Signed-off-by: Connor Tsui <[email protected]>
1 parent e184b25 commit 078ca7c

4 files changed

Lines changed: 131 additions & 31 deletions

File tree

‎vortex-array/src/arrays/primitive/compute/take/avx2.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
//! Only enabled for x86_64 hosts and it is gated at runtime behind feature detection to
77
//! ensure AVX2 instructions are available.
88
9+
#![allow(
10+
unused,
11+
reason = "Compiler may see things in this module as unused based on enabled features"
12+
)]
13+
914
use vortex_compute::take::slice::avx2;
1015
use vortex_dtype::NativePType;
1116
use vortex_dtype::UnsignedPType;
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
//! Assembly inspection stubs for cargo-show-asm.
5+
//!
6+
//! These functions are `#[inline(never)]` wrappers around the take implementations
7+
//! to allow inspecting the generated assembly with cargo-show-asm.
8+
//!
9+
//! # Usage
10+
//!
11+
//! ```bash
12+
//! # Scalar implementations
13+
//! cargo asm -p vortex-compute take_scalar_u32_u32 --rust
14+
//! cargo asm -p vortex-compute take_scalar_u64_u32 --rust
15+
//!
16+
//! # AVX2 implementations
17+
//! cargo asm -p vortex-compute take_avx2_u32_u32 --rust
18+
//! cargo asm -p vortex-compute take_avx2_u64_u32 --rust
19+
//!
20+
//! # Portable SIMD implementations (requires nightly)
21+
//! RUSTFLAGS='--cfg vortex_nightly' cargo +nightly asm -p vortex-compute take_portable_simd_u32_u32 --rust
22+
//! RUSTFLAGS='--cfg vortex_nightly' cargo +nightly asm -p vortex-compute take_portable_simd_u64_u32 --rust
23+
//! ```
24+
25+
#![allow(unused, reason = "These stubs are for assembly inspection only")]
26+
27+
use vortex_buffer::Buffer;
28+
29+
// ============ SCALAR STUBS ============
30+
31+
/// Scalar take: u32 values, u32 indices.
32+
#[inline(never)]
33+
pub fn take_scalar_u32_u32(buffer: &[u32], indices: &[u32]) -> Buffer<u32> {
34+
super::take_scalar(buffer, indices)
35+
}
36+
37+
/// Scalar take: u64 values, u32 indices.
38+
#[inline(never)]
39+
pub fn take_scalar_u64_u32(buffer: &[u64], indices: &[u32]) -> Buffer<u64> {
40+
super::take_scalar(buffer, indices)
41+
}
42+
43+
// ============ PORTABLE SIMD STUBS ============
44+
45+
/// Portable SIMD assembly stubs.
46+
#[cfg(vortex_nightly)]
47+
pub mod portable {
48+
use vortex_buffer::Buffer;
49+
50+
/// Portable SIMD take: u32 values, u32 indices.
51+
#[inline(never)]
52+
pub fn take_portable_simd_u32_u32(buffer: &[u32], indices: &[u32]) -> Buffer<u32> {
53+
super::super::portable::take_portable_simd::<u32, u32, 16>(buffer, indices)
54+
}
55+
56+
/// Portable SIMD take: u64 values, u32 indices.
57+
#[inline(never)]
58+
pub fn take_portable_simd_u64_u32(buffer: &[u64], indices: &[u32]) -> Buffer<u64> {
59+
super::super::portable::take_portable_simd::<u64, u32, 8>(buffer, indices)
60+
}
61+
}
62+
63+
// ============ AVX2 STUBS ============
64+
65+
/// AVX2 assembly stubs.
66+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
67+
pub mod avx2 {
68+
use vortex_buffer::Buffer;
69+
70+
/// AVX2 take: u32 values, u32 indices.
71+
///
72+
/// # Safety
73+
///
74+
/// Caller must ensure AVX2 is available.
75+
#[inline(never)]
76+
#[target_feature(enable = "avx2")]
77+
pub unsafe fn take_avx2_u32_u32(buffer: &[u32], indices: &[u32]) -> Buffer<u32> {
78+
unsafe { super::super::avx2::take_avx2(buffer, indices) }
79+
}
80+
81+
/// AVX2 take: u64 values, u32 indices.
82+
///
83+
/// # Safety
84+
///
85+
/// Caller must ensure AVX2 is available.
86+
#[inline(never)]
87+
#[target_feature(enable = "avx2")]
88+
pub unsafe fn take_avx2_u64_u32(buffer: &[u64], indices: &[u32]) -> Buffer<u64> {
89+
unsafe { super::super::avx2::take_avx2(buffer, indices) }
90+
}
91+
}

‎vortex-compute/src/take/slice/mod.rs‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use vortex_dtype::UnsignedPType;
1414

1515
use crate::take::Take;
1616

17+
pub mod asm_stubs;
1718
pub mod avx2;
1819
pub mod portable;
1920

@@ -22,19 +23,22 @@ impl<T: Copy, I: UnsignedPType> Take<[I]> for &[T] {
2223
type Output = Buffer<T>;
2324

2425
fn take(self, indices: &[I]) -> Buffer<T> {
25-
#[cfg(vortex_nightly)]
26-
{
27-
return portable::take_portable(self, indices);
28-
}
29-
26+
// Prefer hand-tuned AVX2 on x86 - it's faster than portable SIMD because it uses
27+
// native 32-bit index gathers (`vpgatherdd`) instead of extending to 64-bit.
3028
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
3129
{
3230
if is_x86_feature_detected!("avx2") {
33-
// SAFETY: We just checked that the AVX2 feature in enabled.
31+
// SAFETY: We just checked that the AVX2 feature is enabled.
3432
return unsafe { avx2::take_avx2(self, indices) };
3533
}
3634
}
3735

36+
// Fall back to portable SIMD on non-x86 platforms (ARM, RISC-V) or x86 without AVX2.
37+
#[cfg(vortex_nightly)]
38+
{
39+
return portable::take_portable(self, indices);
40+
}
41+
3842
#[allow(unreachable_code, reason = "`vortex_nightly` path returns early")]
3943
take_scalar(self, indices)
4044
}

‎vortex-compute/src/take/slice/portable.rs‎

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ use vortex_buffer::BufferMut;
1818
use vortex_dtype::UnsignedPType;
1919
use vortex_dtype::match_each_unsigned_integer_ptype;
2020

21-
/// SIMD types larger than the SIMD register size are beneficial for
22-
/// performance as this leads to better instruction level parallelism.
23-
pub const SIMD_WIDTH: usize = 64;
21+
/// SIMD lane count. Benchmarking shows 16 is optimal on AVX-512 systems:
22+
/// - 8 lanes: ~33 µs for 100k elements
23+
/// - 16 lanes: ~31 µs (best)
24+
/// - 32 lanes: ~34 µs
25+
/// - 64 lanes: ~41 µs (causes stack spills)
26+
pub const SIMD_WIDTH: usize = 16;
2427

2528
/// Takes the specified indices into a new [`Buffer`] using portable SIMD.
2629
///
@@ -87,7 +90,7 @@ fn take_with_indices<T: Copy + Default + simd::SimdElement, I: UnsignedPType>(
8790
/// # Panics
8891
///
8992
/// Panics if any index is out of bounds for `values`.
90-
#[multiversion(targets("x86_64+avx2", "x86_64+avx", "aarch64+neon"))]
93+
#[multiversion(targets("x86_64+avx512f+avx512vl", "x86_64+avx2", "x86_64+avx", "aarch64+neon"))]
9194
pub fn take_portable_simd<T, I, const LANE_COUNT: usize>(values: &[T], indices: &[I]) -> Buffer<T>
9295
where
9396
T: Copy + Default + simd::SimdElement,
@@ -177,10 +180,10 @@ mod tests {
177180
#[test]
178181
#[should_panic(expected = "index out of bounds")]
179182
fn test_take_out_of_bounds() {
180-
let indices = vec![2_000_000u32; 64];
183+
let indices = vec![2_000_000u32; 8];
181184
let values = vec![1i32];
182185

183-
drop(take_portable_simd::<i32, u32, 64>(&values, &indices));
186+
drop(take_portable_simd::<i32, u32, 8>(&values, &indices));
184187
}
185188

186189
/// Tests SIMD gather with a mix of sequential, strided, and repeated indices. This exercises
@@ -206,7 +209,8 @@ mod tests {
206209
// Reverse: 255, 254, ..., 216.
207210
indices.extend((216u32..256).rev());
208211

209-
let result = take_portable_simd::<i64, u32, 64>(&values, &indices);
212+
// Use 4 lanes for i64 (256-bit / 64-bit = 4).
213+
let result = take_portable_simd::<i64, u32, 4>(&values, &indices);
210214
let result_slice = result.as_slice();
211215

212216
// Verify sequential portion.
@@ -244,25 +248,25 @@ mod tests {
244248
fn test_take_with_remainder() {
245249
let values: Vec<u16> = (0..1000).collect();
246250

247-
// Use 64 + 37 = 101 indices to test both the SIMD loop (64 elements) and the scalar
248-
// remainder (37 elements).
249-
let indices: Vec<u8> = (0u8..101).collect();
251+
// Use 8 + 5 = 13 indices to test both the SIMD loop (8 elements) and the scalar
252+
// remainder (5 elements). Using 8 lanes for u16 values.
253+
let indices: Vec<u8> = (0u8..13).collect();
250254

251-
let result = take_portable_simd::<u16, u8, 64>(&values, &indices);
255+
let result = take_portable_simd::<u16, u8, 8>(&values, &indices);
252256
let result_slice = result.as_slice();
253257

254-
assert_eq!(result_slice.len(), 101);
258+
assert_eq!(result_slice.len(), 13);
255259

256260
// Verify all elements.
257-
for i in 0..101 {
261+
for i in 0..13 {
258262
assert_eq!(result_slice[i], i as u16, "mismatch at index {i}");
259263
}
260264

261265
// Also test with exactly 1 remainder element.
262-
let indices_one_remainder: Vec<u8> = (0u8..65).collect();
263-
let result_one = take_portable_simd::<u16, u8, 64>(&values, &indices_one_remainder);
264-
assert_eq!(result_one.as_slice().len(), 65);
265-
assert_eq!(result_one.as_slice()[64], 64);
266+
let indices_one_remainder: Vec<u8> = (0u8..9).collect();
267+
let result_one = take_portable_simd::<u16, u8, 8>(&values, &indices_one_remainder);
268+
assert_eq!(result_one.as_slice().len(), 9);
269+
assert_eq!(result_one.as_slice()[8], 8);
266270
}
267271

268272
/// Tests gather with large 64-bit values and various index types to ensure no truncation
@@ -283,17 +287,13 @@ mod tests {
283287
];
284288

285289
// Indices that access each value multiple times in different orders.
290+
// Pad to 8 to ensure we hit the SIMD path (4 lanes for i64).
286291
let indices: Vec<u16> = vec![
287-
0, 8, 1, 7, 2, 6, 3, 5, 4, // Forward-backward interleaved.
288-
8, 8, 8, 0, 0, 0, // Repeated extremes.
289-
4, 4, 4, 4, 4, 4, 4, 4, // Repeated zero.
290-
0, 1, 2, 3, 4, 5, 6, 7, 8, // Sequential.
291-
8, 7, 6, 5, 4, 3, 2, 1, 0, // Reverse.
292-
// Pad to 64 to ensure we hit the SIMD path.
293-
0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 1, 2, 3,
292+
0, 8, 1, 7, 2, 6, 3, 5, // 8 indices - exercises SIMD path
294293
];
295294

296-
let result = take_portable_simd::<i64, u16, 64>(&values, &indices);
295+
// Use 4 lanes for i64 (256-bit / 64-bit = 4).
296+
let result = take_portable_simd::<i64, u16, 4>(&values, &indices);
297297
let result_slice = result.as_slice();
298298

299299
// Verify each result matches the expected value.

0 commit comments

Comments
 (0)