Skip to content

perf: enable POPCNT in the BMI2, AVX2 and AVX-512 kernels - #10456

Merged
robert3005 merged 1 commit into
vortex-data:developfrom
mightsleep:popcnt-kernels
Oct 12, 2026
Merged

robert3005 merged 1 commit into
vortex-data:developfrom
mightsleep:popcnt-kernels

Conversation

@mightsleep

Copy link
Copy Markdown
Contributor

Summary

#[target_feature(enable = "bmi2")], "avx2" and "avx512f,..." do not imply POPCNT, so count_ones() inside these kernels compiles to a SWAR sequence (with imul for narrow masks) in any build without target-cpu. The bench runners use target-cpu=native, so they won't show a difference.

Changes

Add popcnt to the target features and to the runtime detection of the intersect_by_rank BMI2 kernels and the x86 simd_compress kernels. No other #[target_feature] function in the tree counts bits without it (the fearless_simd levels already enable POPCNT).

Default build, Zen 5, divan medians:

bench before after
intersect_by_rank 100k random 3.17 µs 1.93 µs 1.64x
random_rotating 0.5/0.5 9.71 µs 5.67 µs 1.71x
very_sparse_mask_cached 10%/1% 1.91 µs 0.81 µs 2.36x
filter_fixed_width random_i16 0.8 1.60 µs 1.06 µs 1.51x
filter_fixed_width random_i32 0.5 1.49 µs 1.03 µs 1.45x
filter_fixed_width random_i64 0.5 1.13 µs 0.99 µs 1.14x
filter_fixed_width random_i128 0.5 (no kernel) 1.45 µs 1.42 µs 1.02x

The filter numbers are the AVX-512 kernels; the AVX2 ones should show up in the CodSpeed simulation shard, where AFAIK valgrind has no AVX-512.

Signed-off-by: mightsleep <acannix@proton.me>

@robert3005 robert3005 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.

Thanks!

@robert3005 robert3005 added the changelog/performance A performance improvement label Oct 12, 2026
@robert3005
robert3005 enabled auto-merge (squash) October 12, 2026 20:35
@codspeed

codspeed Bot commented Oct 12, 2026

Copy link
Copy Markdown

Merging this PR will regress 3 benchmarks

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

⚠️ 10 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 29 improved benchmarks
❌ 3 regressed benchmarks
✅ 2181 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ WallTime filter_avx2[80] 2.5 µs 2.9 µs -13.72%
❌ WallTime filter_avx2[64] 2.4 µs 2.7 µs -12.25%
❌ WallTime threshold_neon[8] 2.9 µs 3.3 µs -11.83%
⚡ WallTime random_rotating_avx512[(0.05, 0.5, "self_5pct_mask_50pct")] 17.9 µs 9.2 µs +94.28%
⚡ WallTime random_rotating_avx512[(0.1, 0.5, "self_10pct_mask_50pct")] 19.8 µs 10.3 µs +91.75%
⚡ WallTime random_rotating_avx2[(0.02, 0.5, "self_2pct_mask_50pct")] 16.3 µs 8.6 µs +89.11%
⚡ WallTime random_rotating_avx512[(0.02, 0.5, "self_2pct_mask_50pct")] 16.1 µs 8.5 µs +89%
⚡ WallTime random_rotating_avx2[(0.05, 0.5, "self_5pct_mask_50pct")] 17.9 µs 9.5 µs +88.52%
⚡ WallTime random_rotating_avx2[(0.1, 0.5, "self_10pct_mask_50pct")] 19.4 µs 10.3 µs +88.08%
⚡ WallTime random_rotating_avx2[(0.5, 0.5, "self_50pct_mask_50pct")] 19.7 µs 10.8 µs +82.69%
⚡ WallTime random_rotating_avx512[(0.5, 0.5, "self_50pct_mask_50pct")] 19.7 µs 10.8 µs +81.76%
⚡ WallTime random_rotating_avx512[(0.9, 0.9, "self_90pct_mask_90pct")] 22.2 µs 13.8 µs +60.3%
⚡ WallTime random_rotating_avx2[(0.9, 0.9, "self_90pct_mask_90pct")] 22.2 µs 14.1 µs +57.91%
⚡ Simulation rank_indices[(0.05, 0.05, "self_sparse_rank_sparse")] 60.1 µs 48.5 µs +23.89%
⚡ WallTime threshold_avx512[8] 2.1 µs 1.8 µs +16.83%
⚡ WallTime random_rotating_avx512[(0.5, 0.02, "self_50pct_mask_2pct")] 58.3 µs 50.7 µs +14.88%
⚡ Simulation rank_indices[(0.5, 0.05, "self_dense_rank_sparse")] 92.4 µs 80.8 µs +14.39%
⚡ Simulation density_matrix[(0.05, 0.5, "self_sparse_mask_dense")] 62.8 µs 54.9 µs +14.33%
⚡ Simulation density_matrix[(0.05, 0.05, "self_sparse_mask_sparse")] 63.1 µs 55.2 µs +14.24%
⚡ Simulation rank_indices[(0.05, 0.5, "self_sparse_rank_dense")] 62.9 µs 55.1 µs +14.22%
... ... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing mightsleep:popcnt-kernels (3be756d) with develop (d42e7c9)

Open in CodSpeed

@robert3005
robert3005 merged commit 1a9ed68 into vortex-data:develop Oct 12, 2026
146 of 149 checks passed
robert3005 pushed a commit that referenced this pull request Oct 14, 2026
…10480)

## Summary

Follow-up to #10168 and #10456. `RankBitReader` pulled the rank bits
through a two-word window over `BitChunkIterator` and refilled it behind
a branch on every 64 bits consumed. With random masks that branch
mispredicts often.

The kernel already counts the set bits of each `self` chunk, so the rank
offset of the next chunk is known without any reader state. `RankBits`
reads 16 bytes at that offset and shifts by `offset % 8`, which covers
any 64-bit read. There is one bounds check per read and no iterator, so
`RankBitReader` and `low_bits` are gone. This applies to every deposit
(BMI2, SVE2 BDEP, portable), not just one path.

Reads near the end of the buffer come from a copy of its last 16 bytes
followed by zeros, made once per call. With a sparse `self` the last 128
rank bits cover many chunks: at 1/64 density that is about 120 of them.
A first version copied the tail on each such read in a `#[cold]`
function, and the call made the whole loop spill to the stack, so sparse
masks ran 5 % slower than before. `cold_path()` keeps the branch:
without it LLVM picks the address with three `cmov`s, which puts the
comparisons in front of every load and costs dense masks 8 %.

## Numbers

Zen 5, default build, `perf stat` over the kernel only: each bench runs
with `--sample-count 100` at `--sample-size 200` and `400`, and the
difference between the two runs is the cost of 100 x 200 calls with the
setup subtracted out. Wall-clock medians on this machine moved by up to
1.4x between processes of the same binary, so I'm not quoting them.

| | before | after | |
|---|---|---|---|
| `random_rotating`, cycles | 4894 M | 4256 M | 1.15x |
| `random_rotating`, instructions | 17.9 G | 15.8 G | -12 % |
| `sparse`, cycles | 3184 M | 3110 M | 1.02x |

`random_rotating` and `sparse` are all their cases together, weighted by
time.

## Testing

`kernels_match_reference` now also slices the rank buffer at a bit
offset (`past_a_word`, 67) so that a read starting past the first word
is covered. Dropping `start` from `RankBits::read` fails four tests,
ignoring `tail_start` fails five.

Signed-off-by: mightsleep <acannix@proton.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/performance A performance improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants