Skip to content

simd: coordinate folds back on the read-in-place loop (removes the random-data regression merged in #426) - #427

Merged
AdaWorldAPI merged 2 commits into
masterfrom
ccr-1fd55114-ndx7e4
Nov 14, 2026
Merged

AdaWorldAPI merged 2 commits into
masterfrom
ccr-1fd55114-ndx7e4

Conversation

@AdaWorldAPI

@AdaWorldAPI AdaWorldAPI commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Why this PR exists

#426 was merged at commit b28eb33, which carries the lane-candidate kernel for masked_argmin_i32 / masked_argmax_i32 / masked_argminmax_i32. That kernel is about 20% slower than the one before it on random data (fused min+max about 50% slower). The fix was pushed to the branch after the merge and never reached master. This PR is that fix. The description of #426 was edited after the merge and describes this kernel, not the one that was merged.

I32x16::mask_blend itself (all seven backends) is already on master and is unchanged here.

What changes

src/simd_masking_ops.rs only. The loop from before #426 is restored: one scalar candidate per side, one packed compare per 16-lane group against the group where it lies in the caller's slice. An improving group is no longer read lane by lane: the lanes that are not better are overwritten with the worst value (I32x16::mask_blend), one reduce_min / reduce_max gives the group's best value, and one compare gives its lowest lane. The lane candidates, the i32 group-ordinal lane and the 2^30-row segmenting are removed. Results and the lowest-position tie rule are unchanged.

What the disassembly showed

llvm-objdump on non-inlined probes of the three kernels:

  • The kernel on master copies each 16-lane group by value before the compare. The group goes through the stack (vmovdqa64 [rsp+..]), the candidate is compared from memory, and the bookkeeping for four groups per mask word is vectorized into stack stores and reloads.
  • The loop restored here compares in place: vpcmpgtd k0, zmm1, zmmword ptr [rax].

So the regression came from the loop shape, not from the select.

Measured

Unit: microseconds (µs) for one fold over 65,536 rows. The second table gives the same measurements in nanoseconds (ns) per row.

Intel Xeon 2.10 GHz, AVX-512, release, target-cpu=native, binaries interleaved, minimum over rounds, 0 allocations. "before #426" and "this PR" were measured in the same interleaved session; "master" in an earlier interleaved session against the same "before #426" binary.

µs per fold of 65,536 rows:

data mask fold before #426 (µs) master (µs) this PR (µs)
random dense argmin 10.7 12.8 7.4
random dense min+max 12.5 18.7 12.1
random half argmin 10.6 13.6 7.7
random half min+max 12.3 18.9 11.7
descending dense argmin 109.1 24.6 36.0
descending dense min+max 110.4 18.0 36.1
ascending dense argmin 10.4 11.9 7.4
ascending dense min+max 118.1 23.3 36.4
random 1 in 64 argmin 2.7 2.5 3.2
random 1 in 64 min+max 3.3 3.7 3.7

ns per row (the same runs):

data mask fold before #426 (ns/row) master (ns/row) this PR (ns/row)
random dense argmin 0.163 0.195 0.113
random dense min+max 0.190 0.286 0.184
random half argmin 0.161 0.207 0.118
random half min+max 0.187 0.289 0.178
descending dense argmin 1.664 0.375 0.550
descending dense min+max 1.685 0.274 0.551
ascending dense argmin 0.158 0.181 0.113
ascending dense min+max 1.802 0.355 0.555
random 1 in 64 argmin 0.041 0.038 0.049
random 1 in 64 min+max 0.051 0.056 0.057

Tests

  • tests/masked_argminmax_i32.rs (17) and tests/i32x16_mask_blend.rs (10): green, unchanged.
  • Parity arms green on this kernel: native (AVX-512), v3 (AVX2), avx-qemu, neon-qemu, wasm, nightly.
  • Disable runs, anchor asserted, exit 101 each: lowest-lane pick, filler value, blend operands, group base, AVX-512 blend operand order (5 of 5).
  • cargo clippy --lib --tests -- -D warnings, cargo fmt --check and the facet-stride guard are clean.

Scope note

These are i32 value lanes. They are kept as a fallback for lanes that exist only as i32; no further tuning of this path is planned.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XAqKMNPpoyKDKuAgEnu6HU

…esolved by blend + reduce

Replaces the lane-candidate kernel of the previous commit. Disassembly
showed its regression on random data came from copying each 16-lane
group by value (the group went through the stack and the candidate was
compared from memory), not from the select. This kernel keeps the
merged loop, which compares against the group where it lies, and only
changes how an improving group is resolved: non-improving lanes are
overwritten with the worst value (I32x16::mask_blend), one horizontal
reduction gives the best value, one compare gives its lowest lane.

AVX-512, release, 65,536 rows, ns per element, old -> new:
random dense argmin 0.163 -> 0.113, fused 0.190 -> 0.184;
descending dense argmin 1.664 -> 0.550, fused 1.685 -> 0.551;
random 1-in-64 argmin 0.041 -> 0.049.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XAqKMNPpoyKDKuAgEnu6HU
@coderabbitai

coderabbitai Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a21ec2ee-4d3c-4004-9403-e591e7c9b5fe
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@AdaWorldAPI
AdaWorldAPI marked this pull request as ready for review November 14, 2026 21:29
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-14T21:31:03.049904Z e0262d6 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@AdaWorldAPI
AdaWorldAPI merged commit dc71c76 into master Nov 14, 2026
31 checks passed
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