Skip to content

feat: add 64-bit warp shuffle (u64/f64, idx/xor/up/down) - #278

Merged
nihalpasham merged 1 commit into
NVIDIA:mainfrom
goog00:feat/shuffle-64bit
Jun 24, 2026
Merged

nihalpasham merged 1 commit into
NVIDIA:mainfrom
goog00:feat/shuffle-64bit

Conversation

@goog00

@goog00 goog00 commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds 64-bit warp shuffle (u64/f64, all four modes: idx/xor/up/down), completing the shuffle family alongside the existing 32-bit u32/f32 ops. PTX shfl.sync is 32-bit-only (no .b64 form, no i64 LLVM intrinsic), so each 64-bit shuffle lowers to one convergent inline-PTX block that splits the value into halves, shuffles each, and repacks.

Changes

  • Device API (cuda-device/src/warp.rs): shuffle_{,xor_,down_,up_}u64{,_sync} core + 8 f64 wrappers. Scope mirrors the 32-bit API (u32+f32, no i32); f64 are zero-cost to_bits/from_bits wrappers over u64, and i64 data uses as u64.
  • Dialect (dialect-nvvm): 4 ShflSync{Idx,Bfly,Down,Up}I64Op + registration.
  • Importer (mir-importer): emit_warp_shuffle_i64 + 4 dispatch arms.
  • Lowering (mir-lower): convert_shuffle_i64 emits mov.b64 {lo,hi} → two shfl.sync.<mode>.b32 → mov.b64, with the shfl clamp baked per mode (31 for idx/bfly/down, 0 for up — matching the 32-bit intrinsic path). Convergence comes from the inline-asm Convergent attr, so no state.rs change.

Testing

  • cargo oxide run shuffle_64 passes (RTX 5090, sm_120) — idx broadcast (hi/lo reassembly), f64 butterfly sum (528), down/up neighbor exchange (edge keeps own), masked half-warp broadcast (subset membermask, no cross-talk)
  • cargo test --workspace passes — incl. new dialect construct/verify (4 modes) and lowering inline-PTX pin (template/clamp/=l,l,r,r/convergent for all 4 modes)
  • New example added (shuffle_64)
--- Test 1: shuffle_u64 (idx broadcast of lane 5) ---
out[0]   = 0x00000005abcd0005 (expected 0x00000005abcd0005)  ✓
--- Test 2: shuffle_xor_f64 (butterfly sum) ---
sum[0]   = 528 (expected 528)                                 ✓
--- Test 3: shuffle_down_u64 / shuffle_up_u64 (delta 1) ---
down[31] = 0xdead00000000001f (edge keeps own)                ✓
--- Test 4: shuffle_u64_sync (two independent 16-lane halves) ---
half[0]  = 0xf00d000000000000  half[16] = 0xf00d000000000010  ✓

Checklist

  • All commits signed off (git commit -s)
  • No files under cuda-bindings/ modified by an external contributor (see CONTRIBUTING.md)
  • SPDX headers on new source files

@nihalpasham

Copy link
Copy Markdown
Collaborator

@goog00 - could you resubmit this with conflicts resolved.

shfl.sync is 32-bit-only (no .b64 form, no i64 LLVM intrinsic), so each
64-bit shuffle lowers to one convergent inline-PTX block that splits the
value with mov.b64 {lo,hi}, runs two shfl.sync.<mode>.b32, and repacks —
clamp baked per mode (31 for idx/bfly/down, 0 for up) to match the 32-bit
intrinsic path. f64 forms are zero-cost device wrappers bitcasting through
u64, so only 4 i64 dialect ops are needed. Convergence comes from the inline
asm Convergent attr (no state.rs change). Adds dialect construct/verify and
lowering inline-asm tests plus a shuffle_64 example.

Signed-off-by: sunteng <[email protected]>
@goog00
goog00 force-pushed the feat/shuffle-64bit branch from e9e9ac5 to 105ac71 Compare June 24, 2026 07:14
@goog00

goog00 commented Jun 24, 2026

Copy link
Copy Markdown
Contributor Author

Resolved

@nihalpasham nihalpasham added intrinsics Device intrinsics and libdevice math mappings codegen Device code-generation pipeline (Rust MIR to IR to PTX) IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) enhancement New feature or request labels Jun 24, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks, this LGTM 👍🏾

Reviewer note: confirmed the PTX built locally; Doesn't close an open issue (shfl.sync is sm_30, so unrelated to the Ampere tracking issue #274).

@nihalpasham
nihalpasham merged commit baa1773 into NVIDIA:main Jun 24, 2026
24 checks passed
nihalpasham added a commit that referenced this pull request Oct 29, 2026
…ns and scans (#1576)

Closes #1497.

## The problem

`warp_reduce`, `warp_scan`, `block_reduce`, and `block_scan` accept only
`u32`, `i32`, and `f32`. A `u64` call such as `warp_reduce::<u64, Sum,
32>(tile, value)` fails with `E0277` for both `u64: WarpShuffle` and
`Sum: ReduceOp<u64>`. A downstream crate can't add the missing
`WarpShuffle` impl for a primitive type, so the only workaround is a
newtype with its own shuffles and combiners.

`WarpShuffle` is generic over `WarpCollective`, and that trait had no
64-bit shuffle. So the low-level `warp::shuffle_*_u64_sync` functions
from #278 were not reachable with a group's mask and tile-boundary
rules.

## The fix

All library changes are in `cuda-device/src/cooperative_groups.rs`:

1. `WarpCollective` gains `shfl_u64`, `shfl_xor_u64`, `shfl_down_u64`,
and `shfl_up_u64`, matching the existing `_f32` set. `WarpTile<N>`,
`CoalescedThreads`, and the private partial-warp prefix group implement
them with the same masks and source-lane rules as their 32-bit shuffles,
on top of `warp::shuffle_*_u64_sync`. Each one is a single inline-PTX
block with two `shfl.sync.*.b32`, one per half. `CoalescedThreads` gets
a private `rank_to_lane` helper, shared by the `u32`, `f32`, and `u64`
index shuffles, in place of three copies of the same loop.
2. `WarpShuffle` is implemented for `u64` and for `i64`. `i64` casts
through `u64`, the same way `i32` casts through `u32`.
3. `ReduceOp<u64>` is implemented for `Sum` (wrapping), `Min`, `Max`,
`BitAnd`, `BitOr`, and `BitXor`. `ReduceOp<i64>` is implemented for
`Sum`, `Min`, and `Max`.

I added `i64` because it falls out of the same pattern: one
`WarpShuffle` impl that casts through `u64`, and the three combiners
`i32` already has. Bitwise ops stay unsigned-only, as they are for 32
bits. `f64` would work the same way, but I left it out of this PR.

The new trait methods have default bodies that move the two halves
through the existing `u32` methods, so a `WarpCollective` impl outside
this crate still compiles and gets working `u64` shuffles. The built-in
groups override all four with the single-block form.

`redux.sync` has no 64-bit form, so the 64-bit types always use the
shuffle path. The new impls sit after the `f32` ones and use the default
for the `HAS_FULL_WARP_FORM` item that #1305 adds, so they don't overlap
its hunks. The book's supported-features table now lists the new types.

## Testing

On an RTX 5090 (sm_120), CUDA 13.2, driver 595.99:

- New example `coop_groups_u64` compares every result with a CPU
reference:
- `WarpTile<N>` for N = 1, 2, 4, 8, 16, and 32, in blocks of 128
threads, so each launch has several warps and, below 32 lanes, several
tiles per warp. Each tile size runs the 18 reductions and inclusive
scans (six `u64` ops and three `i64` ops) and the four `u64` group
shuffles. The shuffle offsets include ones that reach past the tile,
where the caller must get its own value back. 28,672 results per tile
size.
- `block_reduce` and `block_scan` at 14 block sizes: 1, 7, 31, 32, 33,
45, 64, 100, 255, 256, 257, 640, 1000, and 1024 threads, three blocks
each. Nine of the sizes leave a partial final warp. 202,230 results in
total.
- `CoalescedThreads` `u64` shuffles on eight groups, from the full warp
to a single lane, including sparse ones such as `0x8010_0089`.
- The same `u64` shuffles on 8- and 32-lane groups through a wrapper
that implements only the required `WarpCollective` methods, as a group
from another crate would. This exercises the default bodies.
  - A `total(&WarpTile<32>, u64)` function written as in the issue.
- The inputs include values whose low words alone would give the wrong
`Min` and `Max`, low words whose `Sum` carries into the high word,
values just below `u64::MAX` whose `Sum` wraps, the `u64` and `i64`
extremes, and all-`u64::MAX` and all-negative blocks.
- Without this change the example does not compile. I also ran it
against 18 single-line mutations of the new code. It caught 17: high
word dropped from the `u64` or `i64` shuffle; `Min` comparing low words
only; `Sum` without the carry between halves; 32-bit `BitAnd` and `Min`
identities; `0` as the `i64` `Max` identity; each `WarpTile` shuffle
ignoring the tile boundary or base lane; `CoalescedThreads` treating a
rank as a lane or skipping the member check; and, in the default bodies,
a high half taken from the low half, shuffled the wrong way or read from
the wrong rank, or reassembled with its halves swapped. The one it
missed makes the partial-warp prefix use a full 32-lane mask. That is
undefined behavior, but the 5090 returns correct results, and memcheck
and synccheck report nothing. The existing `u32` path has the same gap,
and `partial_warp_reduce` documents the same behavior.
- `compute-sanitizer` 2026.1.1: memcheck 0 errors and synccheck 0 errors
on the final code. Racecheck reported 0 hazards on the code before
review, whose block kernels are unchanged.
- PTX: each `u64` exchange in `warp_total` is one `{ mov.b64 {lo, hi};
shfl.sync.bfly.b32 lo; shfl.sync.bfly.b32 hi; mov.b64 }` block followed
by `add.s64`. That is five blocks and ten `shfl.sync.bfly.b32` for a
full warp, with no local memory. Before I added the kernels for the
default bodies, which use two separate `u32` shuffles by design, all 638
shuffle sites in the example module were that same two-shuffle block
(308 `up`, 291 `bfly`, 29 `down`, 10 `idx`), and the module used no
local memory.
- `just check` passes, along with the new example's `cargo clippy
--all-targets -D warnings`, `cargo fmt --check`, and
`scripts/sync-example-locks.sh --check`. Before review,
`scripts/smoketest.sh` passed 241/241 in both GPU and `--compile-only`
modes, and 235/241 in `--full-debug`, `coop_groups_u64` included. The
six full-debug failures fail the same way on unmodified `main`:
constant_memory_simple, iket_trace, packed_aggregate_abi,
partial_warp_reduce, warp_sums, and wgmma_mma_bf16. After the review
changes (default bodies and docs), the 14 cooperative-groups, warp,
reduce, scan and shuffle examples pass in GPU and `--compile-only`
modes.

---------

Signed-off-by: Praveen Perera <[email protected]>
Signed-off-by: nihalpasham <[email protected]>
Co-authored-by: nihalpasham <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codegen Device code-generation pipeline (Rust MIR to IR to PTX) enhancement New feature or request intrinsics Device intrinsics and libdevice math mappings IR-lowering Lowering between dialects (dialect-mir to LLVM dialect)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants