Skip to content

feat: add cp.async 4-byte and 8-byte copy intrinsics (sm_80+) - #280

Merged
nihalpasham merged 3 commits into
NVIDIA:mainfrom
honeyspoon:upstream-cp-async-small
Jun 28, 2026
Merged

nihalpasham merged 3 commits into
NVIDIA:mainfrom
honeyspoon:upstream-cp-async-small

Conversation

@honeyspoon

@honeyspoon honeyspoon commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Add cp.async.ca variants for 4-byte and 8-byte asynchronous global-to-shared memory copies, extending the existing 16-byte cp.async.ca/cg support.

Summary

The existing cp.async support covers 16-byte cache-all (ca) and cache-global (cg) copies. This PR adds the smaller 4-byte and 8-byte cp.async.ca variants, which are needed when copying sub-cacheline data (e.g., packed scalars, individual matrix fragments) in asynchronous pipelines.

These are lowered to inline PTX assembly with AsmKind::SideEffect (memory write to shared memory, non-convergent). A new inline_asm_sideeffect() helper is added to common.rs alongside the existing inline_asm_convergent(), providing the correct non-convergent + side-effect combination for memory operations that are not warp-synchronous.

Device API PTX Instruction Copy size
cp_async_ca_4(dst, src) cp.async.ca.shared.global [%0], [%1], 4; 4 bytes
cp_async_ca_8(dst, src) cp.async.ca.shared.global [%0], [%1], 8; 8 bytes

Part of #274

Changes

  • cuda-device: 2 new pub unsafe fn stubs in async_copy.rs (raw pointer operations require unsafe).
  • dialect-nvvm: 2 new NVVM ops (CpAsyncCa4Op, CpAsyncCa8Op) with 2 operands (dst shared ptr, src global ptr) and 0 results.
  • mir-importer: 2 emit_cp_async_ca_* translation functions with dispatch arms.
  • mir-lower: 2 convert_cp_async_ca_* lowering functions using new inline_asm_sideeffect() helper; 2 MirToLlvmConversion impls.
  • mir-lower/common.rs: New inline_asm_sideeffect() helper for non-convergent side-effect inline asm (distinct from the existing inline_asm_convergent() which is for warp-synchronous ops).
  • tests: 2 lowering tests verifying the PTX output.
  • example: cp_async_small end-to-end GPU example testing 4-byte and 8-byte async copies via shared memory.

Testing

  • cargo test -p mir-lower --test lowering_test passes
  • cargo fmt --check and cargo clippy clean
  • E2E example included (cp_async_small)

Checklist

  • All commits signed off (git commit -s)
  • No files under cuda-bindings/ modified
  • SPDX headers on new source files

@honeyspoon
honeyspoon force-pushed the upstream-cp-async-small branch from 11f7ac2 to ae2100f Compare June 24, 2026 13:25
@honeyspoon
honeyspoon force-pushed the upstream-cp-async-small branch from ae2100f to 40d8ff0 Compare June 26, 2026 17:41
honeyspoon added a commit to honeyspoon/cuda-oxide that referenced this pull request Jun 26, 2026
Add three `cp.async.ca` zero-fill variants that accept a `src_size`
parameter for hardware-assisted zero-fill of boundary tiles. When
`src_size < cp_size`, the remaining bytes are zero-filled by hardware.

| Device API               | PTX                                                    |
|--------------------------|--------------------------------------------------------|
| `cp_async_ca_zfill_4`    | `cp.async.ca.shared.global [smem], [gmem], 4, src_sz;` |
| `cp_async_ca_zfill_8`    | `cp.async.ca.shared.global [smem], [gmem], 8, src_sz;` |
| `cp_async_ca_zfill_16`   | `cp.async.ca.shared.global [smem], [gmem],16, src_sz;` |

Changes:
- cuda-device: 3 new `pub unsafe fn` stubs in `async_copy.rs`
- dialect-nvvm: 3 new ops (CpAsyncCaZfill{4,8,16}Op) with 3 operands
- mir-importer: generalized `emit_cp_async<T: Op>` shared emitter,
  3 new zero-fill dispatch arms
- mir-lower: `convert_cp_async_zfill_impl` shared lowering with
  `inline_asm_sideeffect()`, 3 MirToLlvmConversion impls
- tests: 3 new lowering tests
- example: `cp_async_zfill` e2e GPU test (full copy, partial copy,
  zero copy)

Part of NVIDIA#274. Depends on NVIDIA#280.

Signed-off-by: abder <[email protected]>
honeyspoon and others added 2 commits June 28, 2026 08:26
Add cp.async.ca variants for 4-byte and 8-byte asynchronous
global-to-shared memory copies, extending the existing 16-byte
cp.async.ca/cg support. Lowered to inline PTX with AsmKind::SideEffect
(memory write, non-convergent).

Part of NVIDIA#274

Signed-off-by: abder <[email protected]>
@nihalpasham
nihalpasham force-pushed the upstream-cp-async-small branch from 40d8ff0 to 24e53b0 Compare June 28, 2026 03:32
nihalpasham pushed a commit to honeyspoon/cuda-oxide that referenced this pull request Jun 28, 2026
Add three `cp.async.ca` zero-fill variants that accept a `src_size`
parameter for hardware-assisted zero-fill of boundary tiles. When
`src_size < cp_size`, the remaining bytes are zero-filled by hardware.

| Device API               | PTX                                                    |
|--------------------------|--------------------------------------------------------|
| `cp_async_ca_zfill_4`    | `cp.async.ca.shared.global [smem], [gmem], 4, src_sz;` |
| `cp_async_ca_zfill_8`    | `cp.async.ca.shared.global [smem], [gmem], 8, src_sz;` |
| `cp_async_ca_zfill_16`   | `cp.async.ca.shared.global [smem], [gmem],16, src_sz;` |

Changes:
- cuda-device: 3 new `pub unsafe fn` stubs in `async_copy.rs`
- dialect-nvvm: 3 new ops (CpAsyncCaZfill{4,8,16}Op) with 3 operands
- mir-importer: generalized `emit_cp_async<T: Op>` shared emitter,
  3 new zero-fill dispatch arms
- mir-lower: `convert_cp_async_zfill_impl` shared lowering with
  `inline_asm_sideeffect()`, 3 MirToLlvmConversion impls
- tests: 3 new lowering tests
- example: `cp_async_zfill` e2e GPU test (full copy, partial copy,
  zero copy)

Part of NVIDIA#274. Depends on NVIDIA#280.

Signed-off-by: abder <[email protected]>
@nihalpasham nihalpasham added enhancement New feature or request safety Memory safety, soundness, or undefined behavior 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) cuda-feature A CUDA hardware/toolkit feature (TMA, tcgen05, WGMMA) tests Adds or improves test coverage examples Example kernels and host harnesses under crates/rustc-codegen-cuda/examples labels Jun 28, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thank you for adding the missing 4-byte and 8-byte async-copy forms. They are useful building blocks for kernels that cannot naturally move a full 16 bytes per lane.

I added a few safety and integration details on the branch:

  • added the sm_80 target floor and explicit shared/global address conversions;
  • fixed the example's Rust 2024 static access and 8-byte shared alignment;
  • completed the unsafe contract, compiler-ordered waits, exact lowering checks, and standalone lockfile.

Both copy widths now compile to the expected sm_80 PTX and are accepted by ptxas.

@nihalpasham
nihalpasham merged commit c5fcc9b into NVIDIA:main Jun 28, 2026
26 checks passed
nihalpasham pushed a commit to honeyspoon/cuda-oxide that referenced this pull request Jun 28, 2026
Add three `cp.async.ca` zero-fill variants that accept a `src_size`
parameter for hardware-assisted zero-fill of boundary tiles. When
`src_size < cp_size`, the remaining bytes are zero-filled by hardware.

| Device API               | PTX                                                    |
|--------------------------|--------------------------------------------------------|
| `cp_async_ca_zfill_4`    | `cp.async.ca.shared.global [smem], [gmem], 4, src_sz;` |
| `cp_async_ca_zfill_8`    | `cp.async.ca.shared.global [smem], [gmem], 8, src_sz;` |
| `cp_async_ca_zfill_16`   | `cp.async.ca.shared.global [smem], [gmem],16, src_sz;` |

Changes:
- cuda-device: 3 new `pub unsafe fn` stubs in `async_copy.rs`
- dialect-nvvm: 3 new ops (CpAsyncCaZfill{4,8,16}Op) with 3 operands
- mir-importer: generalized `emit_cp_async<T: Op>` shared emitter,
  3 new zero-fill dispatch arms
- mir-lower: `convert_cp_async_zfill_impl` shared lowering with
  `inline_asm_sideeffect()`, 3 MirToLlvmConversion impls
- tests: 3 new lowering tests
- example: `cp_async_zfill` e2e GPU test (full copy, partial copy,
  zero copy)

Part of NVIDIA#274. Depends on NVIDIA#280.

Signed-off-by: abder <[email protected]>
nihalpasham added a commit that referenced this pull request Jun 28, 2026
* feat: add cp.async zero-fill variants (src_size parameter, sm_80+)

Add three `cp.async.ca` zero-fill variants that accept a `src_size`
parameter for hardware-assisted zero-fill of boundary tiles. When
`src_size < cp_size`, the remaining bytes are zero-filled by hardware.

| Device API               | PTX                                                    |
|--------------------------|--------------------------------------------------------|
| `cp_async_ca_zfill_4`    | `cp.async.ca.shared.global [smem], [gmem], 4, src_sz;` |
| `cp_async_ca_zfill_8`    | `cp.async.ca.shared.global [smem], [gmem], 8, src_sz;` |
| `cp_async_ca_zfill_16`   | `cp.async.ca.shared.global [smem], [gmem],16, src_sz;` |

Changes:
- cuda-device: 3 new `pub unsafe fn` stubs in `async_copy.rs`
- dialect-nvvm: 3 new ops (CpAsyncCaZfill{4,8,16}Op) with 3 operands
- mir-importer: generalized `emit_cp_async<T: Op>` shared emitter,
  3 new zero-fill dispatch arms
- mir-lower: `convert_cp_async_zfill_impl` shared lowering with
  `inline_asm_sideeffect()`, 3 MirToLlvmConversion impls
- tests: 3 new lowering tests
- example: `cp_async_zfill` e2e GPU test (full copy, partial copy,
  zero copy)

Part of #274. Depends on #280.

Signed-off-by: abder <[email protected]>

* fix: harden cp.async zero-fill integration

Signed-off-by: nihalp <[email protected]>

* style: format cp.async zero-fill example

Signed-off-by: nihalp <[email protected]>

---------

Signed-off-by: abder <[email protected]>
Signed-off-by: nihalp <[email protected]>
Co-authored-by: nihalp <[email protected]>
@honeyspoon
honeyspoon deleted the upstream-cp-async-small branch June 30, 2026 01:47
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) cuda-feature A CUDA hardware/toolkit feature (TMA, tcgen05, WGMMA) enhancement New feature or request examples Example kernels and host harnesses under crates/rustc-codegen-cuda/examples intrinsics Device intrinsics and libdevice math mappings IR-lowering Lowering between dialects (dialect-mir to LLVM dialect) safety Memory safety, soundness, or undefined behavior tests Adds or improves test coverage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants