Skip to content

SDMA: cache the HW read index and let commands straddle the ring end - #660

Open
dsidler wants to merge 1 commit into
dev/dasidler/sdma-packet-vector-storefrom
dev/dasidler/sdma-acquire-straddle
Open

dsidler wants to merge 1 commit into
dev/dasidler/sdma-packet-vector-storefrom
dev/dasidler/sdma-acquire-straddle

Conversation

@dsidler

@dsidler dsidler commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #659 (base dev/dasidler/sdma-packet-vector-store). Review only the top commit.

Summary

Two changes to reserving space in a device-initiated SDMA queue (iris/device/sdma_utils.py).

Cached HW read index. Every reservation read the queue's hardware read pointer, an uncached round trip of ~1.8 µs, to check that the ring has room. Like rocm-xio's SdmaQueueHandle::CanWriteUpto, can_write_up_to now keeps the last observed read index and reads the hardware pointer only when the cached value says the ring is full. The hardware read index only grows. A stale cached value, or one written by another workgroup, is therefore a lower bound and never reports space that does not exist.

The cache lives in a new last column of the copy-engine context tensor and starts at 0. The row layout now has one source of truth:

  • queue_ctx_size(): used by Iris to allocate the tensor
  • queue_ctx(copy_engine_ctx, to_rank): device-side row lookup
  • cached_read_index_slot(row): pointer to the cached index

All row arithmetic in iris/mem/triton/ops.py and copy_linear goes through these.

Commands straddle the ring end. When a command did not fit before the end of the ring, acquire_fadd padded the rest of the ring with NOPs, submitted the padding, and reserved again. The copy engine fetches the ring modulo its size; the amdgpu kernel driver never pads at the wrap either. So every packet writer now wraps each dword (_ring_dword), and a command may cross the end of the ring. As a result:

  • acquire_fadd / acquire return just the start index.
  • They no longer take the queue, write, doorbell and committed pointers.
  • All call sites drop the place_nop_packet(..., offset) / + offset bookkeeping.
  • place_nop_packet stays available and also wraps per dword.

The modulo indexing keeps one store instruction per packet (checked in the AMDGCN).

Measured on MI300X (8 GPUs, timestamped copy of the submit path):

  • reservation: ~2.3 µs → ~0.85 µs
  • whole submission (reserve + packet + submit): ~4.3 µs → ~2.9 µs

Tests

  • test_sdma_packets.py, fake queue:
    • test_command_straddles_ring_end: an atomic (8 dwords) and a 2-D put (20 dwords) placed 1, 2 or 7 dwords before the ring end are split across end and start with the exact dwords, no NOP, and all pointers advanced by exactly the command.
    • test_reservation_refreshes_cached_read_index: the cache is refreshed only when the ring looks full.
  • test_copy_engine_ops.py::test_copy_engine_ring_wrap, hardware: 8 workgroups wrap a device queue three times with 28-byte linear copies. 8 MiB mod 28 ≠ 0, so at most one of the three wraps can land on a command boundary. Every copied element is checked.
  • Mutation checks:
    • no per-dword wrap → the 6 straddle tests fail
    • no cache write-back → the refresh test fails
  • Full set, same results as unchanged main in this environment:
    • 2 ranks: test_sdma_packets.py 25, test_copy_engine_ops.py 25, test_copy_engine_tile_ops.py 13, test_all_gather_sdma.py 26, test_message_passing.py 36, test_matmul_reduce_scatter.py 21, test_optional_sdma.py 4 passed
    • 8 ranks: test_all_gather_sdma.py 25 passed / 1 skipped, test_matmul_reduce_scatter.py 21 passed
    • Failing identically on unchanged main here: test_plan.py gluon (2) and test_device_placement.py::test_ranks_placed_off_rank_mod_gpus (8).

🤖 Generated with Claude Code

Two changes to reserving space in a device-initiated SDMA queue.

Cached read index: every reservation read the queue's HW read pointer, an
uncached round trip of ~1.8 us, to check that the ring has room. Like
rocm-xio's SdmaQueueHandle::CanWriteUpto, keep the last observed read index
and read the HW pointer only when the cached value says the ring is full. The
HW read index only grows, so a stale value (or one written by another
workgroup) is a lower bound and never reports space that does not exist. The
cache lives in a new last column of the copy-engine context tensor
(sdma_utils.queue_ctx_size(), queue_ctx(), cached_read_index_slot()); it
starts at 0. Reservation drops from ~2.3 to ~0.85 us.

Straddle the ring end: when a command did not fit before the end of the
ring, acquire_fadd padded the rest of the ring with NOPs, submitted the
padding and reserved again. The copy engine fetches the ring modulo its size
(the amdgpu driver never pads at the wrap either), so instead every packet
writer now wraps each dword and a command may cross the end. acquire_fadd
and acquire return just the start index and no longer need the queue,
write, doorbell and committed pointers.

Tests: fake-queue tests for commands that straddle the end (atomic, 2-D put)
and for the cached read index (refreshed only when the ring looks full); a
hardware test that wraps a device queue three times with 28-byte copies,
which cannot all land on the ring boundary, and checks every copy.

Co-Authored-By: Claude <[email protected]>
@github-actions github-actions Bot added in-progress We are working on it iris Iris project issue labels Nov 25, 2026
@dsidler
dsidler added this pull request to stack #664 November 25, 2026 19:06

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in-progress We are working on it iris Iris project issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant