Skip to content

simd: fuse wrapping i32 add into masked sum - #319

Merged
AdaWorldAPI merged 2 commits into
masterfrom
gpt/masked-sum-wrapping-add-i32
Sep 22, 2026
Merged

AdaWorldAPI merged 2 commits into
masterfrom
gpt/masked-sum-wrapping-add-i32

Conversation

@AdaWorldAPI

@AdaWorldAPI AdaWorldAPI commented Sep 22, 2026 •

Copy link
Copy Markdown
Owner

Adds one semantics-preserving reduction primitive for expression folding: masked_sum_wrapping_add_i32(a, b, mask). For each selected row it performs i32::wrapping_add first, then widens the wrapped row value to i64 and accumulates. This is deliberately not masked_sum_i32(a)+masked_sum_i32(b), which changes semantics on row overflow.

Why this belongs in ndarray: lance-graph's mask-risc law routes all row/mask arithmetic through ndarray::simd; the new kernel erases a derived i32 lane without materialising it and without creating a consumer-side hand-rolled loop. Like masked_sum_i32, the implementation walks selected bits and is intentionally scalar because exact widening semantics matter more than an I32x16::reduce_sum that accumulates in i32.

Coverage: facade re-export plus the existing cross-realization masking parity group, with the reference explicitly computing vals[i].wrapping_add(rhs[i]) before widening. Public docs include the overflow boundary example i32::MAX + 1 -> i32::MIN.

Consumer follow-up: lance-graph mask-risc terminal/lowering for fused SUM(a+b) after this lands.

Summary by CodeRabbit

  • New Features

    • Added a masked integer reduction that combines paired 32-bit values with wrapping arithmetic and returns a 64-bit total.
    • The operation is now available through the public SIMD interface.
    • Mask selections beyond the available input rows are ignored.
  • Bug Fixes

    • Added validation to ensure masked reduction results match the selected-element reference calculation, including wrapping overflow behavior.

@coderabbitai

coderabbitai Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Essentials

Run ID: ed339002-cb9b-4753-b1ac-a3abffe5d704

📥 Commits

Reviewing files that changed from the base of the PR and between ecb2f2c and 932729e.

📒 Files selected for processing (3)
  • crates/simd-masking-parity/src/lib.rs
  • src/simd.rs
  • src/simd_masking_ops.rs

Included review availability: 1 review is currently available. Your included PR review attempts over the past 7 days set your current allowance at 2 reviews per hour.


📝 Walkthrough

Walkthrough

The change adds masked_sum_wrapping_add_i32, exports it through simd, and validates its masked pairwise wrapping behavior in the SIMD parity checks.

Changes

Masked wrapping reduction

Layer / File(s) Summary
Implement and export the reduction
src/simd_masking_ops.rs, src/simd.rs
Adds masked_sum_wrapping_add_i32. It validates input lengths and mask size, performs wrapping i32 pairwise addition, widens results to i64, and exposes the function through the public SIMD facade.
Validate masked pairwise sums
crates/simd-masking-parity/src/lib.rs
Extends check_masked_reductions with a second operand vector and compares each masked reduction with a wrapping reference accumulation.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Feature

Suggested reviewers: claude

Merge Risk: ⚪ Minimal · up to 93272

The new masked wrapping reduction follows the repository’s slice-level API contract and preserves the documented wrapping and masking behavior. No actionable merge-blocking risk remains.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: adding a fused wrapping i32 addition to the masked sum operation.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR

A rabbit checks each masked sum,
With wrapping hops where values run.
Two vectors meet, then widen wide,
While parity guards stand by their side.
The SIMD trail now passes bright.

Comment @coderabbitai help to get the list of available commands.

Copy link
Copy Markdown
Owner Author

@coderabbitai review

Please attack the semantic boundary rather than style:

  1. masked_sum_wrapping_add_i32 must compute i32::wrapping_add per selected row before widening to i64; any implementation equivalent to independent widened sums is wrong on overflow.
  2. The new function must preserve masked_sum_i32's mask-tail and sparse-set-bit behavior, with no read past either input lane.
  3. The parity reference must be independent enough to catch an accidental widen-before-add implementation, not merely repeat the function under test.

This is intentionally scalar inside the masking facade: the existing masked_sum_i32 rationale against I32x16::reduce_sum applies here too.

@coderabbitai

coderabbitai Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor
🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo '=== Changed files ==='
git diff --name-status HEAD^ HEAD

echo '=== Pull request diff ==='
git diff --unified=80 HEAD^ HEAD

echo '=== Candidate masking symbols ==='
rg -n -C 8 --glob '*.rs' 'masked_sum_(wrapping_add_i32|i32)\s*\(' . || true

Length of output: 42363


@AdaWorldAPI Reviewed the requested semantic boundary.

  • masked_sum_wrapping_add_i32 performs a[i].wrapping_add(b[i]) before the i64 cast. It does not implement independent widened sums.
  • It retains masked_sum_i32 behavior for required mask coverage, sparse set-bit iteration, and partial final-word masking. The clamp prevents a dirty tail bit from indexing past either input slice.
  • The parity reference is separate from the kernel. It folds selected rows with vals[i].wrapping_add(rhs[i]) as i64.
  • The parity corpus includes overflow-capable i32::MIN and i32::MAX rows. It also covers lengths 0, 1, 63, 64, 65, and 130 with empty, full, random, and sparse masks.

I found no defect in these semantic boundaries.

✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@cursor

cursor Bot commented Sep 22, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: serverGenReqId_dd39f13c-1849-4e7f-beb0-5f18e35964e8)

@AdaWorldAPI
AdaWorldAPI marked this pull request as ready for review September 22, 2026 19:30
@AdaWorldAPI
AdaWorldAPI merged commit c21b674 into master Sep 22, 2026
27 checks passed
@cursor

cursor Bot commented Sep 22, 2026

Copy link
Copy Markdown

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: serverGenReqId_6766b071-6bed-42fb-821b-084c349aba74)

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.

1 participant