Skip to content

perf(cpp): runtime AVX2 dispatch for the ALP kernels - #1116

Open
ColinLeeo wants to merge 9 commits into
apache:developfrom
ColinLeeo:colin/alp-avx2-runtime-dispatch-2added
Open

ColinLeeo wants to merge 9 commits into
apache:developfrom
ColinLeeo:colin/alp-avx2-runtime-dispatch-2added

Conversation

@ColinLeeo

@ColinLeeo ColinLeeo commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The ALP SIMD kernels are written with SIMDe's AVX2 API, but TsFile builds for the portable x86-64 baseline, so SIMDe compiles them into its portable fallback. This mirrors the RLE packer's existing runtime dispatch (cpp/src/encoding/int64_packer.h):

  • cpp/src/encoding/alp/alp_avx2.h adds native AVX2 copies of EncodeValues and DecodeValues (float, double) plus the Unpack kernels (uint32_t, uint64_t), compiled with TSFILE_TARGET_AVX2 (__attribute__((target("avx2")))).
  • AlpSimdKernel / AlpSimdUnpackKernel pick the AVX2 copy once per batch call through common::cpu_has_avx2(). ALP_AVX2_DISPATCH gates the ALP kernels to x86-64.
  • No -mavx2 / -march=native is added; the binary's ISA floor is unchanged. Non-x86-64 targets (including 32-bit x86), MSVC and CPUs without AVX2 keep the SIMDe kernels. Both paths encode identical bytes and decode bit-identical values.
  • The SIMDe kernels are renamed to AlpSimdeKernel / AlpSimdeUnpackKernel and stay untouched as the fallback.

Double kernels process four lanes. AVX2 has no packed int64 <-> double conversion (AVX-512DQ), so the per-lane conversion uses the same scalar instruction as the reference, preserving bit patterns.

Benchmarks

cpp/bench_mark/alp_avx2_benchmark.cc, default build (no -mavx2), i7-11700, GCC 9.4, ALP_BLOCK_SIZE (1024) values per call, one warmup plus a calibrated batch per row. Every row verifies that the AVX2 output matches SIMDe.

Kernel Data SIMDe (M values/s) AVX2 (M values/s) Speedup
FLOAT EncodeValues decimal 330.2 2377.8 7.20x
random 284.7 850.2 2.99x
constant 456.0 2459.8 5.39x
FLOAT DecodeValues decimal 998.5 1288.3 1.29x
random 796.1 1131.3 1.42x
constant 1061.4 1704.7 1.61x
DOUBLE EncodeValues decimal 437.5 663.0 1.52x
random 381.1 562.3 1.48x
constant 293.2 426.3 1.45x
DOUBLE DecodeValues decimal 903.1 841.3 0.93x
random 794.8 804.6 1.01x
constant 809.8 829.5 1.02x
Unpack uint32 (widths 1..32) 159-166 528-643 3.2-4.0x
Unpack uint64 (widths 1..57) 163-167 610-622 3.7-3.8x

Double decode stays on the same order of magnitude because it is conversion-bound: it cannot use a packed int64 -> double instruction on AVX2 (a second run measured 0.97x/1.05x/1.04x, i.e. within run-to-run noise). An all-vector 32-bit split conversion was tried and reverted: it did not help decode and dropped double encode from ~1.5x to ~1.26x.

Tests

  • New AlpAvx2Test differential tests call the AVX2 and SIMDe kernels directly on the same inputs and compare encoded arrays, bitmaps, exception bit patterns and decoded values: all factor/exponent pairs, decimal/high-entropy streams, NaN/±Inf/-0.0/subnormals/max, counts 0..1024 around the 8-lane (float) and 4-lane (double) boundaries, every safe bit width for both unpack widths, and malformed exception statuses. A dedicated case pins the int64 -> double conversion at INT64_MIN/MAX and powers of two against both SIMDe and the scalar reference. Decode tests always pass the exception bitmap when one exists, so a wrongly set bit with exception_count == 0 fails. They skip on CPUs without AVX2; the existing AlpSimdeTest cases also run through the dispatcher.
  • Full ./mvnw clean verify -P with-java,with-python -Denable.lzma2=ON on the merged tree: BUILD SUCCESS (Java 1119 tests, C++ CTest 1624/1624, Python 610 passed/16 skipped, checkstyle/spotless/RAT clean).
  • CMake ASan+UBSan build (-DENABLE_ASAN=ON, RelWithDebInfo) full C++ suite on the merged tree: 1619/1619, no sanitizer reports.
  • Default Release full C++ suite on x86-64 with AVX2: 1619/1619.
  • objdump confirms the AVX2 kernels are separate target-attributed symbols using vpgatherdq/vpsrlvq/vpermps; they are not inlined into baseline callers.

Docs

cpp/docs/alp-codec-design.md previously said there was no runtime dispatch; the SIMD Plan section now describes the AVX2 copies, the dispatcher, the x86-64 restriction and the fallback. cpp/bench_mark/README.md documents the new benchmark.

The ALP SIMD kernels are written with SIMDe's AVX2 API, but TsFile builds
for the portable x86-64 baseline, so SIMDe compiles them into its portable
fallback. Mirror the RLE packer's dispatch: add native AVX2 copies of
EncodeValues, DecodeValues and the bit-unpack kernels in alp_avx2.h,
compiled with __attribute__((target("avx2"))), and select them at runtime
through common::cpu_has_avx2(). The binary's ISA floor is unchanged;
non-x86 targets, MSVC and CPUs without AVX2 keep the SIMDe path, and both
paths encode identical bytes and decode bit-identical values.

Measured on an i7-11700 with the default build (no -mavx2), ALP block size
(1024 values) per call: float encode 3.3-6.2x, float decode 1.4-1.6x,
double encode 1.2-1.6x, double decode 1.03-1.10x, uint32 unpack 2.9-3.5x,
uint64 unpack 2.7-3.4x.

Add differential AVX2-vs-SIMDe tests over all factor/exponent pairs,
special values, counts around the vector-width boundaries and every safe
bit width, plus malformed-exception status checks. Update the ALP design
doc and add cpp/bench_mark/alp_avx2_benchmark.cc.
@ColinLeeo
ColinLeeo requested a balanced review from Copilot November 16, 2026 03:39
Copilot stopped reviewing on behalf of ColinLeeo due to an error November 16, 2026 03:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Copilot was unable to run its full agentic suite in this review.

Copilot review overview

3 open issues
What changed in this PR

Adds runtime AVX2 dispatch for ALP SIMD kernels (keeping a portable SIMDe fallback), plus differential tests and a micro-benchmark to validate/measure the native AVX2 path.

Changes:

  • Introduces native AVX2 implementations (alp_avx2.h) and runtime dispatch wrappers in alp_simde.h.
  • Adds AVX2-vs-SIMDe differential tests (encode/decode + unpack) gated on AVX2 dispatch support.
  • Adds an ALP SIMD micro-benchmark target and documents how to run it.
File Description
cpp/​test/​encoding/​alp_simde_test.cc Adds differential tests comparing AVX2 kernels vs SIMDe fallback.
cpp/​src/​encoding/​alp/​alp_simde.h Adds runtime dispatch wrappers and separates “SIMDe fallback” kernel naming.
cpp/​src/​encoding/​alp/​alp_avx2.h New native AVX2 kernels compiled with target attributes and used via dispatch.
cpp/​docs/​alp-codec-design.md Updates design doc to describe AVX2 copies, runtime dispatch, and new tests/bench.
cpp/​bench_mark/​alp_avx2_benchmark.cc New benchmark executable to compare SIMDe vs AVX2 performance per entry point.
cpp/​bench_mark/​README.md Documents how to build/run the new AVX2 benchmark.
cpp/​CMakeLists.txt Adds alp_avx2_benchmark target under BUILD_BENCHMARK when ENABLE_SIMD is on.

Review effort: Lite

Comment thread cpp/src/encoding/alp/alp_avx2.h Resolved
Comment thread cpp/test/encoding/alp_simde_test.cc Resolved
Comment thread cpp/test/encoding/alp_simde_test.cc Resolved
Document the padded-body contract the gather-based unpack kernels rely on,
compare the AVX2 unpack output against the packed input instead of only
against SIMDe, and use nullptr in the new code.
…time-dispatch-2added

# Conflicts:
#	cpp/CMakeLists.txt
@ColinLeeo
ColinLeeo requested a balanced review from Copilot November 16, 2026 04:02
…time-dispatch-2added

# Conflicts:
#	cpp/bench_mark/README.md

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread cpp/src/encoding/alp/alp_avx2.h Resolved
Comment thread cpp/test/encoding/alp_simde_test.cc Resolved
Comment thread cpp/bench_mark/alp_avx2_benchmark.cc Resolved
Comment thread cpp/bench_mark/alp_avx2_benchmark.cc Resolved
Comment thread cpp/src/encoding/alp/alp_avx2.h Resolved
Restrict ALP AVX2 dispatch to x86-64 so 32-bit x86 cannot reach an
undefined double-to-int64 cast, always pass the exception bitmap to the
differential decode test so a wrongly set bit with exception_count == 0
is caught, use nullptr in the benchmark, and use an epi64 all-ones mask
in the double kernel.
The second review round restricted the ALP AVX2 kernels to x86-64; update
the remaining comments, benchmark output and docs that still referred to
x86 generally.
The AVX2 kernel converts int64 to double with the scalar cvtsi2sd
instruction while SIMDe uses split arithmetic; add a regression test that
decodes INT64_MIN/MAX and powers of two through both kernels and the
scalar reference. Also finish the x86-64 wording in the benchmark build
comment and design doc.
@ColinLeeo
ColinLeeo requested a balanced review from Copilot November 16, 2026 05:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread cpp/bench_mark/alp_avx2_benchmark.cc Resolved
Comment thread cpp/src/encoding/alp/alp_avx2.h Resolved
Comment thread cpp/test/encoding/alp_simde_test.cc Resolved
Guard the benchmark speedup column against a zero SIMDe rate, copy the
converted int64 lanes with std::memcpy instead of aliasing a __m256i
store, and make the test encode helpers return a status so the caller can
assert fatally instead of continuing after a failed encode.
@ColinLeeo
ColinLeeo requested a balanced review from Copilot November 16, 2026 07:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread cpp/src/encoding/alp/alp_avx2.h Resolved
Comment thread cpp/src/encoding/alp/alp_avx2.h Resolved
Comment thread cpp/src/encoding/alp/alp_avx2.h Resolved
The scalar tails of the SIMDe and AVX2 encode kernels left encoded[i]
untouched on the exception path while the vector body and the scalar
reference write 0. Zero them for deterministic output, and assert it in
the differential test by pre-filling the output buffers with a sentinel.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants