Repository navigation
perf(mask): SVE2 BDEP and a branch-free portable deposit for intersect_by_rank - #10168
Conversation
af09f7b to
c915625
Compare
…t_by_rank Signed-off-by: mightsleep <acannix@proton.me>
c915625 to
95dcb4a
Compare
|
can you split this into its own sve mod in a new file |
Merging this PR will regress 2 benchmarks
|
Signed-off-by: mightsleep <acannix@proton.me>
|
yes hello |
| let self_count = self_chunk.count_ones() as usize; | ||
| let rank_bits = reader.read(self_count); | ||
| push_result_chunk::<D>(&mut result, self_chunk, self_count, rank_bits); | ||
| let trivial = D::SKIP_TRIVIAL_RUNS && runs.next_is_trivial(); |
There was a problem hiding this comment.
Not sure how much benchmarking you have done but looking at instruction count it seems that this parameter is more overhead than it saves
There was a problem hiding this comment.
Weird.. with the portable kernel forced on Zen 5: on nightly 1.101 the lookahead buys ~6% on runs and costs ~9% on random. On 1.98 it mostly just costs, runs within 0-3%, random -9 to -10%. Removed. Interesting enough that I'd like to look at it properly, just not in this PR.
| 0..=2 => deposit_two_lowest(source, mask), | ||
| 62.. => deposit_two_holes(source, mask), | ||
| _ => deposit_bytewise(source, mask), | ||
| } |
There was a problem hiding this comment.
This function grew and lost it's inlining. I think we can fix the calling loop to have hot and cold branches? Did you check whether inlining of this function matters?
There was a problem hiding this comment.
My bad, and it goes further than this PR. In the bench profile Portable::deposit_bits was a call per word. The BMI2 kernel has the same problem, and that one is already on develop: pdep_bmi2 is #[target_feature(enable = "bmi2")] and the generic loop isn't, so LLVM never inlines it. On develop (3750d50, 1.98) the loop makes an indirect call per word to a five-instruction push rbp; pdep; pop rbp; ret. The CpuKernel docs cite this code as an example to follow...
Fixed with the pattern from bit/pack.rs and the bool filter: a #[target_feature] wrapper around an #[inline(always)] kernel body. Applied to develop alone (+63/-7, BMI2 only) it gives 1.22-1.31x on the chunk paths, 1.52x on sparse/sparse rank indices and 1.0-1.1x on mask-driven, with no regressions (Zen 5, 3 interleaved rounds). In this PR the same treatment covers SVE2, and the portable deposit inlines too (1.13-1.18x).
I can send the BMI2 part as a separate small PR so it lands on its own and this one rebases on it, or keep everything here. Your call.
Signed-off-by: mightsleep <acannix@proton.me>
…hreshold in benches Assert in debug builds that mask_count equals mask.count_ones(), because the portable deposits pick a closed form from it. Inline bdep_sve2 like pdep_bmi2. Add 5 % and 10 % self densities to random_rotating, so the PORTABLE_LOOP_MAX_DENSITY crossover can be measured, and update the bench doc. Signed-off-by: Robert Kruszewski <robert@spiraldb.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Measured on Graviton3 (Neoverse V1) and Graviton2 (Neoverse N1) with random masks: the network beats the loop from about 3.5 % self density on V1, but not below 8 % on N1. 1/16 is close to where the two balance, so keep it. Signed-off-by: Robert Kruszewski <robert@spiraldb.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
robert3005
left a comment
There was a problem hiding this comment.
thanks for the contribution, I have fixed up minor nits
…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>
Summary
Follow-up to #10138. Without BMI2,
intersect_by_rankdeposits the rank bits with a loop over the set bits of eachselfchunk. That covers every aarch64 build, plus x86 without BMI2. On masks that don't repeat, the loop mispredicts about once per chunk and runs 3 to 8 times slower than PDEP.This PR uses SVE2 BDEP where the CPU has it and a branch-free deposit elsewhere:
uN::extract_bitsscheme from core, with three stages instead of six.self, where a chunk has about four set bits. The first version used the network everywhere and lost to the old loop on every sparse bench, so the loop is back for sparseself, chosen once per call, and now stops when the source bits run out.The BMI2 path is unchanged.
Median µs before / after on Neoverse N2, and the speedup of the portable path on Apple M1:
density_matrixdensedensity_matrixself sparseintersect_by_rankrunsvery_sparse_mask_cachedrandom_rotatingis the bench from #10138, with extra densities added for this measurement. The runners are GitHub'subuntu-24.04-arm(Cobalt 100) andmacos-15(a virtualized M1), with two rounds per run. For the portable column on N2 the portable path is forced. The M1 numbers are ranges over several runs, because a virtual M1 has opinions. I also tried HD 7-5 through PMULL, on the theory that the Apple cores love it. The M1 ran it at the speed of the network, so it isn't in this PR.Where it's slower:
(i * 7 + 13) % 1024, repeat every 16 chunks, and the predictor learns the old loop on them. With a sparse mask over a denseself, the portable path stays on the network and runs 0.67x to 0.9x. With random masks at the same densities it's 2.9x to 5.4x faster, so I didn't also pick the kernel by mask density.selfat 5 %: about 0.9x, just above the crossover. The threshold is set for the M1, since N2 takes BDEP anyway.CodSpeed's neon leg runs on Graviton3, which has SVE but not SVE2, so it will only show the portable path.
Written with the help of AI. I reviewed the code and ran the measurements.