Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Use SumsOf8 for AVX2 u8 lane sums - #3242

Merged
copybara-service[bot] merged 1 commit into
google:masterfrom
Magnushst:codex/avx2-u8-lane-sum
Aug 5, 2026
Merged

Use SumsOf8 for AVX2 u8 lane sums#3242
copybara-service[bot] merged 1 commit into
google:masterfrom
Magnushst:codex/avx2-u8-lane-sum

Conversation

@Magnushst

Copy link
Copy Markdown
Contributor

Problem

Full-width AVX2 uint8_t SumOfLanes and ReduceSum use the generic shuffle-and-widen reduction despite AVX2 already having a native SumsOf8 implementation.

Root cause

The generic reduction eligibility condition excludes the existing u8x8 and u8x16 specialisations, but not u8x32. There is therefore no overload that routes a 256-bit byte reduction through SumsOf8.

Change

Exclude u8x32 from the generic reduction and add a 256-bit overload that:

  1. uses SumsOf8 to form four exact uint64 partial sums;
  2. reduces those partial sums using the existing uint64 implementation; and
  3. broadcasts the low byte.

The low byte preserves the existing modulo-256 uint8 reduction semantics.

Correctness

The existing parameterised reduction tests cover SumOfLanes, ReduceSum, full and partial vectors, and unsigned wraparound.

  • GCC 14.2: 40/40 focused cases passed
  • Clang 19.1 with warnings-as-errors: 40/40 focused cases passed
  • Clang ASan+UBSan: 40/40 focused cases passed
  • MSVC 19.41 core Release test: passed
  • Full GCC suite: 2,360 executed tests passed, 18 expected skips, zero failures

Performance

Intel Core i9-13905H, WSL2 Debian, static AVX2, -O3 -march=haswell, pinned to logical CPU 4. Results are medians of 31 interleaved samples with 16,777,216 reductions per sample. Brackets show IQR.

Case Compiler Baseline Patched Change
1 vector/call GCC 14.2 1.104 [1.079, 1.124] ns 0.652 [0.644, 0.671] ns -40.9%
8 vectors/call GCC 14.2 1.095 [1.075, 1.124] ns 0.609 [0.599, 0.638] ns -44.4%
1 vector/call Clang 19.1 1.050 [1.016, 1.068] ns 0.561 [0.558, 0.570] ns -46.6%
8 vectors/call Clang 19.1 0.983 [0.977, 1.003] ns 0.568 [0.559, 0.578] ns -42.2%

The GCC SIMD reduction sequence decreases from 14 vector operations to 7.
Clang shows a comparable reduction.

Regressions

The u8x16 control code generation is unchanged with both GCC and Clang.
The isolated micro-object text size also decreases. No tested target regressed.

Scope

Performance was measured only on AVX2 x86-64. No AVX-512, Arm, RISC-V or Power performance is claimed.

@google-cla

google-cla Bot commented Aug 1, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@Magnushst
Magnushst force-pushed the codex/avx2-u8-lane-sum branch from f83aa55 to f5ce2cd Compare August 1, 2026 19:37
@Magnushst

Copy link
Copy Markdown
Contributor Author

The GCC-16 AArch64 job was cancelled (timed-out?) during the build without a compiler or test failure. The same PR commit passes AArch64 Clang-22 and the other foreign-architecture jobs. Could someone with repository permissions please re-run this job?

@jan-wassenberg jan-wassenberg left a comment

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.

Nice optimization, thanks :)

Comment thread hwy/ops/x86_256-inl.h
@Magnushst Magnushst closed this Aug 3, 2026
@Magnushst
Magnushst deleted the codex/avx2-u8-lane-sum branch August 3, 2026 17:38
@Magnushst
Magnushst restored the codex/avx2-u8-lane-sum branch August 3, 2026 17:39
@Magnushst Magnushst reopened this Aug 3, 2026
@copybara-service
copybara-service Bot merged commit 10da432 into google:master Aug 5, 2026
108 of 129 checks passed
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.

2 participants