Skip to content

done_mode=spin (experimental): the rescue path's flag write can be lost while the spin polls #39

Open

Description

@phantoslayer21

With the experimental options ready_wait=memory and done_mode=spin, the game's queue runs a one-thread pass that
polls the done flag with an atomic (InterlockedAdd(flag, 0), PASS_SPIN in clean/runtime/interop/convert.cpp)
until the network writes it. When the network of a frame cannot be enqueued, rescue_flag
(clean/runtime/interop/backend.cpp) is meant to release that spin: a separate list on the backend's own compute queue
waits for the previous network frame, then writes the flag with WriteBufferImmediate. The spin was recorded into the
game's list before that, so the write can arrive while the spin is polling.

Measured on Windows with a test harness (no HIP): a flag written by a shader store on another queue while the spin
polls is seen in 5 of 5 runs; the same flag written with WriteBufferImmediate on another queue is not seen in any of
5 runs, the spin times out, and the flag still reads its old value afterwards: the write is lost, not late. Written
after the spin has finished, the same WriteBufferImmediate lands.

Why: while the spin polls, the flag buffer is in a write state on the game's queue (its atomic is a read-modify-write,
UNORDERED_ACCESS), and the rescue list writes the same 32 bits from a second queue (COPY_DEST, by implicit
promotion from COMMON, which is valid for buffers). Buffers have simultaneous-access semantics, which allow "multiple
readers and one writer, as long as the writer doesn't concurrently modify the texels that other readers are accessing"
(D3D12_RESOURCE_FLAGS, D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS). Two queues writing the same 32 bits at once
breaks the first half of that rule. The second half means that no cross-queue write to the flag while the spin reads
it is covered by the API, whoever writes it: the spin design as a whole relies on how the hardware behaves, which is
why it is experimental. On this hardware a shader write is seen and a command-processor write is lost.

The cost today: one spin timeout (bounded by spin_polls) and a stale flag for that frame. The normal writer
(hipStreamWriteValue32 on the HIP stream) also writes while the spin polls; whether it is seen could not be measured
here.

To do: write the rescue flag with the existing flag pass (a compute dispatch on the rescue queue: the shader-write case
that worked) instead of WriteBufferImmediate, and test that a rescued frame no longer costs a timeout. Measure the
HIP writer's case on hardware. Until this is fixed, the option's documentation states that spin mode relies on
behaviour the API does not guarantee.

Related: #7

Activity

  1. added this to the Later milestone on Nov 13, 2026
  2. added
    bug
    needs-hardware-test
    on Nov 13, 2026
  3. added
    area: runtime
    on Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: runtimebugneeds-hardware-test

    Type

    No type

    Projects

    • Status
      Todo

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions