Skip to content

GH-55708: [C++] Resolve ALP multipliers once per vector for aarch64 - #55709

Closed
mapleFU wants to merge 1 commit into
apache:mainfrom
mapleFU:alp-auto-vec
Closed

mapleFU wants to merge 1 commit into
apache:mainfrom
mapleFU:alp-auto-vec

Conversation

@mapleFU

@mapleFU mapleFU commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Rationale for this change

Part of #55448.

In AlpCompression<T>::Decompress(), AlpInlines<T>::DecodeValue() previously took AlpExponentAndFactor (a 2-byte struct) by value and looked up the power-of-ten tables inside the loop.

On AArch64 (both macOS Apple Clang and upstream Linux Clang 19–21), AAPCS64 lowers the 2-byte by-value struct parameter into an i64 with undef upper 48 bits. After inlining, LLVM leaves a loop-carried phi i64 and fails to hoist the negated-exponent table load (kTable[-exponent + 10]), causing LoopVectorize to bail out with cannot identify array bounds on all three Decompress instantiations (float -> float, float -> double, double -> double).

Godbolt repro (upstream Clang 20.1, aarch64 vs x86-64): https://godbolt.org/z/1ov94ccYM

What changes are included in this PR?

  • Add AlpInlines<T>::Multipliers with GetEncodeMultipliers() / GetDecodeMultipliers() and pass the pre-resolved multipliers into EncodeValue() / DecodeValue().
  • Resolve the multipliers once before the vector loops in Decompress(), EncodeVector(), and EstimateCompressedSizeBits().

Benchmark on Apple M5 Max (N = 65536, Release):

Case Before After Speedup
DecodeFloat (constant 64.0) 7.70 GiB/s 33.72 GiB/s 4.38x
DecodeDouble (constant 64.0) 14.53 GiB/s 38.60 GiB/s 2.66x
DecodeFloat (2-decimal prices, bit_width > 0) 6.71 GiB/s 21.94 GiB/s 3.27x
DecodeDouble (2-decimal prices, bit_width > 0) 13.32 GiB/s 33.68 GiB/s 2.53x
DecodeFloat (2-decimal + 1% exceptions) 6.56 GiB/s 20.52 GiB/s 3.13x
DecodeDouble (2-decimal + 1% exceptions) 13.21 GiB/s 31.03 GiB/s 2.35x

Are these changes tested?

Covered by existing ALP and Parquet encoding tests (arrow-encoding-utility-test --gtest_filter='*Alp*' and parquet-encoding-test --gtest_filter='*Alp*:*ALP*').

Are there any user-facing changes?

No.

Was AI used for this PR?

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human

…ector

Pre-resolve the power-of-ten scaling multipliers once before the vector loops in Decompress(), EncodeVector(), and EstimateCompressedSizeBits() instead of passing AlpExponentAndFactor by value into EncodeValue()/DecodeValue() on every element.

On AArch64 (both macOS Apple Clang and upstream Linux Clang 19-21), passing the 2-byte AlpExponentAndFactor struct by value into DecodeValue() inside the loop leaves a loop-carried undef-padded i64 PHI after AAPCS64 ABI lowering, preventing LICM from hoisting the power-of-ten table load and blocking auto-vectorization on all three Decompress instantiations (float->float, float->double, double->double).
@mapleFU
mapleFU marked this pull request as ready for review November 21, 2026 14:08
Copilot AI balanced review requested due to automatic review settings November 21, 2026 14:08

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.

🟢 Approval recommended

There are no active review issues for the ALP multiplier-hoisting change.

No open issues 🎉

What changed in this PR

This PR optimizes C++ ALP compression and decompression by resolving decimal multipliers outside per-value loops, targeting Clang auto-vectorization on AArch64 without changing numerical behavior.

Changes:

  • Adds helpers to resolve type-specific encode and decode multipliers.
  • Reuses resolved multipliers in size estimation, encoding, and decompression while preserving multiplication order and source-type precision.
File Description
cpp/​src/​arrow/​util/​alp/​alp_compression.cc Moves power-of-ten table lookups outside ALP processing loops to enable vectorization.

Review effort: Balanced

@mapleFU mapleFU changed the title GH-55708: [C++] Resolve ALP multipliers once per vector GH-55708: [C++] Resolve ALP multipliers once per vector for aarch64 Nov 21, 2026
@mapleFU

mapleFU commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

@wgtmac @pitrou @HuaHuaY Would you mind take a quick look?

Constants::GetFactor(exponent_and_factor.factor);
// The two decimal multipliers applied by EncodeValue() or DecodeValue(),
// resolved once per vector from the power-of-ten tables.
struct Multipliers {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've done a similar thing in #55449 :)

@pitrou

pitrou commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Interestingly, it doesn't seem to make a difference on my x86 machine (gcc 15.3.0, AMD Zen 2).

BM_AlpEncodingFloat/1024        56186 ns        56177 ns         4947 bytes_per_second=69.5351Mi/s items_per_second=18.2282M/s
BM_AlpEncodingFloat/4096        65447 ns        65392 ns         4273 bytes_per_second=238.942Mi/s items_per_second=62.6372M/s
BM_AlpEncodingFloat/32768      202354 ns       202352 ns         1383 bytes_per_second=617.737Mi/s items_per_second=161.936M/s
BM_AlpEncodingFloat/65536      405044 ns       404879 ns          693 bytes_per_second=617.468Mi/s items_per_second=161.866M/s

BM_AlpDecodingFloat/1024          822 ns          822 ns       341024 bytes_per_second=4.63955Gi/s items_per_second=1.24542G/s
BM_AlpDecodingFloat/4096         2767 ns         2766 ns       101357 bytes_per_second=5.516Gi/s items_per_second=1.48069G/s
BM_AlpDecodingFloat/32768       20651 ns        20652 ns        13349 bytes_per_second=5.91096Gi/s items_per_second=1.58671G/s
BM_AlpDecodingFloat/65536       41336 ns        41325 ns         6771 bytes_per_second=5.90777Gi/s items_per_second=1.58585G/s

BM_AlpEncodingDouble/1024      157289 ns       157272 ns         1782 bytes_per_second=49.6749Mi/s items_per_second=6.51099M/s
BM_AlpEncodingDouble/4096      170475 ns       170312 ns         1670 bytes_per_second=183.487Mi/s items_per_second=24.05M/s
BM_AlpEncodingDouble/32768     416532 ns       416441 ns          675 bytes_per_second=600.325Mi/s items_per_second=78.6858M/s
BM_AlpEncodingDouble/65536     834922 ns       834900 ns          333 bytes_per_second=598.874Mi/s items_per_second=78.4956M/s

BM_AlpDecodingDouble/1024         911 ns          911 ns       302764 bytes_per_second=8.376Gi/s items_per_second=1.12421G/s
BM_AlpDecodingDouble/4096        2958 ns         2958 ns        94071 bytes_per_second=10.3178Gi/s items_per_second=1.38483G/s
BM_AlpDecodingDouble/32768      21992 ns        21976 ns        12456 bytes_per_second=11.1094Gi/s items_per_second=1.49108G/s
BM_AlpDecodingDouble/65536      44145 ns        44132 ns         6353 bytes_per_second=11.0641Gi/s items_per_second=1.485G/s

@mapleFU

mapleFU commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

This only works on AArch.

gcc has another problem about SafeCopy, which will making it failed to vectorize, I'll submit an issue later.

@mapleFU

mapleFU commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

Maybe we can waiting for #55449 , and I can close this firstly

@mapleFU mapleFU closed this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants