Skip to content

fix(cuda-device): handle partial warps in block collectives - #1311

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
YunusMutlu:fix/block-collectives-partial-warp
Sep 23, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
YunusMutlu:fix/block-collectives-partial-warp

Conversation

@YunusMutlu

@YunusMutlu YunusMutlu commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

What this fixes

Block reductions and scans now handle a partial final warp without reading nonexistent lanes or unused scratch slots.

48-thread block: full warp + 16 live lanes -> block sum 48
scratch capacity 32, live warp count 2     -> only two totals are read

What we fixed

  • Kept the full-warp shuffle path and used a live contiguous-prefix mask for the tail. Single-warp blocks avoid a second full-warp collective.
  • Checked ceil(block_threads / 32) <= NUM_WARPS before scratch access. Extra capacity is allowed.
  • Changed the scratch accesses to raw element reads/writes, avoiding an overlapping mutable borrow of the complete allocation in every thread.
  • Made block_reduce and block_scan unsafe and updated all repository callers. An arbitrary raw pointer cannot prove that shared scratch is valid or exclusively assigned to the collective.
  • Added 77 launch shapes, including every size 1..65, full-warp controls, 1024 threads, multidimensional blocks, two CTAs, and scratch reuse.

Call contract

Every thread in the block must call with the same live shared allocation and reach every collective/barrier. Other operations must not access that scratch while the collective runs. Place a block barrier before reusing it.

Verification

Checked at cba13824 against main 66ff93c1:

  • Device tests/doctests, strict device/example Clippy, formatting, and warnings-denied device rustdoc pass.
  • The 77-shape regression and all existing cooperative-groups checks pass on RTX 5090. Memcheck and synccheck report zero errors; racecheck reports zero hazards.
  • An undersized-scratch probe traps; PTX retains runtime masks for partial-warp shuffles and the capacity guard before scratch access.
  • Independent review covered linear ranks, publication barriers, single-warp paths, and raw-pointer ownership. All seven hosted workflows pass on this exact head.

Closes #1310. The unsafe call requirement is a source compatibility change.

@YunusMutlu
YunusMutlu marked this pull request as draft September 20, 2026 17:42
@YunusMutlu
YunusMutlu force-pushed the fix/block-collectives-partial-warp branch from 712fdeb to c8fd64d Compare September 20, 2026 17:50
@YunusMutlu
YunusMutlu marked this pull request as ready for review September 20, 2026 17:51
@YunusMutlu
YunusMutlu force-pushed the fix/block-collectives-partial-warp branch from c8fd64d to fb158b1 Compare September 20, 2026 18:20
@nihalpasham
nihalpasham force-pushed the fix/block-collectives-partial-warp branch from fb158b1 to cba1382 Compare September 23, 2026 06:14
@nihalpasham nihalpasham added breaking-change Changes a public API, import path, or dependency line that downstream users must update bug Something isn't working device-apis User-facing device-side and kernel-authoring APIs (cuda-device, cuda-macros) safety Memory safety, soundness, or undefined behavior labels Sep 23, 2026

@nihalpasham nihalpasham left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept the partial-warp algorithm and added the scratch ownership follow-up:

  • Raw element accesses avoid overlapping mutable borrows of the shared allocation.
  • Both block collective APIs now require unsafe, with allocation, participation, and reuse requirements documented; all callers are updated.
  • The regressions cover 77 launch shapes, including 3D blocks, two CTAs, maximum capacity, and scratch reuse. Undersized scratch traps.

The GPU cases, memcheck, racecheck, and synccheck pass on RTX 5090. Local device checks pass; all seven hosted workflows pass on this exact head.

This closes #1310 and changes the call sites to require unsafe.

@nihalpasham
nihalpasham merged commit 66fdc19 into NVIDIA:main Sep 23, 2026
55 checks passed
pifu-zh added a commit to pifu-zh/cuda-oxide that referenced this pull request Oct 6, 2026
…4 restored)

Rebase onto upstream ec4aa47 (74 commits) changed the exported .ll
shapes; four adaptations, each semantics-preserving:

- Step 4: tid.y/tid.z -> llvm.amdgcn.workitem.id.y/z (upstream NVIDIA#1311
  computes in-block rank via 3-D coords; on 1-D launches both sides
  return 0). Declares added to the mapped-strip list.
- Step 10: accept dynamic membermask ((1<<live)-1 from the partial-warp
  collectives) and clamp=0 for .up (PTX clamp is the min-source-lane
  bound for .up; ds_bpermute's data movement is mask-independent and
  the convergent marker carries the all-lanes-execute requirement).
- Step 12d: parse asm operands with top-level comma splitting - NVIDIA#1327
  packed-as3 emits constant-expression pointers
  (ptr addrspacecast (ptr addrspace(3) @sym to ptr)) that the old
  [^,)] captures could not span.
- Step 12e: panic trap is now inline PTX asm "trap;" (NVIDIA#1309) which the
  AMDGPU backend rejects; map to llvm.trap() (lowers to s_trap).

Batch tool: select artifacts by exact name - the old "*.opt.ll" glob
also matched the integrated pipeline's leftover *.amdgcn.preopt.opt.ll
(sorted first), so the Python dimension double-rewrote the wrong file;
the recorded 9/17 was measurement poisoning as much as shape drift.

Verified: batch 14/14 llc-pass; tests/test_gfx1030.py 14 passed.
pifu-zh added a commit to pifu-zh/cuda-oxide that referenced this pull request Oct 6, 2026
…tions

Mirror of the Python-pipeline adaptation (35bed77) on the integrated
path, for the two shapes the Rust prep pass also meets:

- Step 10 envelope: dynamic membermask accepted (upstream NVIDIA#1311
  partial-warp collectives emit (1<<live)-1 as SSA; ds_bpermute's data
  movement is mask-independent, convergent survives), and .up clamp=0
  accepted (PTX clamp bounds the lowest source lane for .up; the other
  modes keep width-1=31). Literal non-(-1) masks still fall through for
  llc to reject explicitly.
- Step 12e: panic trap is inline PTX asm post-NVIDIA#1309 and the AMDGPU
  backend rejects the mnemonic; map the exact shape to llvm.trap()
  (s_trap on gfx1030).

12d's store/load operand parsing already handled NVIDIA#1327 constant-
expression pointers via split_top_level_commas (no change needed).

Verified: dynamic-mask up@0 + trap-asm unit tests; lib 1252 green.
pifu-zh added a commit to pifu-zh/cuda-oxide that referenced this pull request Oct 6, 2026
… launches)

Upstream NVIDIA#1311's examples launch multi-dimensional blocks (warp_reduce
runs 77 shapes including (3,5,3)/(8,3,2)/(7,3,3)/(8,8,8) and expands
the in-block rank from the 3-D coordinates). Folding ntid.{y,z} to
constant 1 - the Phase-1 1-D-launch decision - is a wrong-semantics bug
there: block_scan_partial_warp_distinct died with an HSA hardware
exception on the GPU.

- prep pass (amdgcn.rs) + Python rewriter: ntid.{y,z} -> kernarg
  identity-ors like ntid.x; nctaid.{y,z} keep the 1-D-grid folding.
- signatures: every amdgpu_kernel unconditionally gains
  (i32 %ntid_x, %ntid_y, %ntid_z). The launchers always push
  blockDim.0/.1/.2 on the gfx path, so a reference-only rule would
  misalign the whole kernarg tail for subset readers (e.g. .y without
  .x); two dead i32 kernargs are the price of a deterministic ABI.
  Non-kernel defines keep the conditional rule (alwaysinline
  leftovers).
- host: the three macro templates (async builder, generated async
  launchers, generated sync launchers) push all three dims behind the
  same amdgcn_ntid_append_active() gate.
- fixture hosts in tests/test_gfx1030.py updated to the 9- and
  5-slot kparams.

Verified on gfx1030 GPU: warp_reduce e2e (all warp tests + 77 launch
shapes incl. every 3-D one), barrier e2e (3 kernels incl. noComplete
split), redux_sum e2e (warp sums = 496). Python batch 14/14;
codegen lib 1262 green; test_gfx1030.py 14 passed (incl. the two GPU
e2e fixtures).
pifu-zh added a commit to pifu-zh/cuda-oxide that referenced this pull request Oct 24, 2026
… launches)

Upstream NVIDIA#1311's examples launch multi-dimensional blocks (warp_reduce
runs 77 shapes including (3,5,3)/(8,3,2)/(7,3,3)/(8,8,8) and expands
the in-block rank from the 3-D coordinates). Folding ntid.{y,z} to
constant 1 - the Phase-1 1-D-launch decision - is a wrong-semantics bug
there: block_scan_partial_warp_distinct died with an HSA hardware
exception on the GPU.

- prep pass (amdgcn.rs) + Python rewriter: ntid.{y,z} -> kernarg
  identity-ors like ntid.x; nctaid.{y,z} keep the 1-D-grid folding.
- signatures: every amdgpu_kernel unconditionally gains
  (i32 %ntid_x, %ntid_y, %ntid_z). The launchers always push
  blockDim.0/.1/.2 on the gfx path, so a reference-only rule would
  misalign the whole kernarg tail for subset readers (e.g. .y without
  .x); two dead i32 kernargs are the price of a deterministic ABI.
  Non-kernel defines keep the conditional rule (alwaysinline
  leftovers).
- host: the three macro templates (async builder, generated async
  launchers, generated sync launchers) push all three dims behind the
  same amdgcn_ntid_append_active() gate.
- fixture hosts in tests/test_gfx1030.py updated to the 9- and
  5-slot kparams.

Verified on gfx1030 GPU: warp_reduce e2e (all warp tests + 77 launch
shapes incl. every 3-D one), barrier e2e (3 kernels incl. noComplete
split), redux_sum e2e (warp sums = 496). Python batch 14/14;
codegen lib 1262 green; test_gfx1030.py 14 passed (incl. the two GPU
e2e fixtures).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change Changes a public API, import path, or dependency line that downstream users must update bug Something isn't working device-apis User-facing device-side and kernel-authoring APIs (cuda-device, cuda-macros) safety Memory safety, soundness, or undefined behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cooperative_groups: block_reduce/block_scan mishandle partial final warps

2 participants