← Back to Pull requests

fix(rccl): replace atomicAdd(ptr, 0) reads with scoped atomic loads #13864

Open
alex-breslow-amd opened this pull request with 16 lines diff across 2 files
View on GitHub
alex-breslow-amd alex-breslow-amd commented on Oct 20, 2026 22:22 UTC
Author

Motivation

Several RCCL device primitives read shared values with atomicAdd(ptr, 0). That makes every poll an atomic read-modify-write, which is more expensive than needed, especially in spin loops polling memory another GPU or the host may be writing.

Alex: Previously the compiler optimized this into a relaxed system scope atomic load. However, no longer. Let's just ask for what we want. :-) Credit goes to Larry Meadows for root causing this and Marc Charest for opening the ticket and doing the initial work to determine it was a compiler bump.

Technical Details

Replaces each of these reads with __scoped_atomic_load_n(..., __ATOMIC_RELAXED, __MEMORY_SCOPE_SYSTEM) on a global (address_space(1)) pointer, following the existing pattern in op128.h and prims_ll.h:

  • prims_ll.h: the sendConnHeadCache reload in waitSend.

  • prims_simple.h: the four ptrExchange slot reads in setDataPtrs (the recvProvider and sendProvider waits, and the sendAcceptor and recvAcceptor polls).
  • Ordering stays relaxed, as with atomicAdd. System scope makes sure writes from peer GPUs and the host are observed.

    Issue Tracking


    JIRA ID: ROCM-32706

    Test Plan

  • Compiled a standalone kernel using the same casts with hipcc --offload-arch=gfx942 and checked the generated ISA.
  • Test Result

  • Each load compiles to a plain global_load_dwordx2 ... sc0 sc1 (system-scope load) instead of an atomic RMW.
  • Submission Checklist

  • Look over the contributing guidelines at https://github.com/ROCm/rocm-systems/blob/develop/CONTRIBUTING.md.
  • Diff: 16 lines (+11 / -5) across 2 files Reviewer: nileshnegi Review duration: 36217m
    All Vouch review confidence checks passed
    Residual risk 0.03 = Change Risk (0.21) × (1 − Review Confidence (0.86)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.21 · 16 lines diff across 2 files
    0.21
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.82 · 5 substantive comment(s)
    0.82
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    86%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.170
    Real PR Conversation Timeline
    11 items
    therock-pr-bot[bot] therock-pr-bot[bot] commented on Oct 20, 2026 22:23 UTC
    Bot

    <!-- therock-pr-bot-policy-check -->

    ✅ All Policy Checks Passed

    | Check | Status | Details |
    |---|:---:|---|
    | 📝 PR Description | ✅ Pass | — |
    | ⛔ Forbidden Files | ✅ Pass | — |
    | 🧪 Unit Test | ⚠️ Warning | Error: Source/code files changed without an accompanying unit test.<br>Expected: add at least one test file named like test_<name>.py / test_<name>.cpp (or <name>_test.*).<br>Current: code file(s) changed: projects/rccl/src/device/prims_ll.h, projects/rccl/src/device/prims_simple.h; no test file found |
    | 🚫 Draft PR | 🔜 To Be Enabled | — |
    | 🚩 Feature Flag | 🔜 To Be Enabled | — |
    | 📊 Code Coverage | 🔜 To Be Enabled | — |

    🎉 All policy checks passed!

    📖 Need help? See the Policy FAQ for details on every check and how to fix failures.

    🙋 Wish to Override Policy?

    therock-pr-bot[bot] therock-pr-bot[bot] commented on Oct 20, 2026 22:23 UTC
    Bot

    <!-- therock-pr-bot-fix-policies -->
    🚫 Please fix the failed policies before requesting reviews.

    The following policy checks failed:

  • ❌ PR Description
  • The Not ready to Review label has been added to this PR.
    Once all policies pass, the label will be removed automatically.

    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Oct 20, 2026 22:24 UTC
    Reviewer

    <!-- ccr-overview-v2 -->

    🔵 Needs a closer look

    Cross-GPU and host-memory polling changes warrant runtime validation beyond the reported compilation and ISA check.

    No open issues 🎉

    <details>
    <summary><strong>What changed in this PR</strong></summary>

    Replaces five RCCL polling reads with system-scoped atomic loads to avoid unnecessary read-modify-write operations.

    Changes:

  • Updates the LL send-head cache reload.

  • Updates four pointer-exchange polls while preserving relaxed ordering.
  • | File | Description |
    | ---- | ----------- |
    | projects/​rccl/​src/​device/​prims_simple.h | Uses scoped loads for four pointer-exchange polls. |
    | projects/​rccl/​src/​device/​prims_ll.h | Uses a scoped load for the send-head cache reload. |
    </details>

    <sub>Review effort: Balanced</sub>

    nileshnegi nileshnegi approved these changes on Oct 20, 2026 22:56 UTC
    Approved Reviewer

    Approved these changes.

    weilewei weilewei approved these changes on Oct 20, 2026 23:00 UTC
    Approved Reviewer

    Approved these changes.

    amd-argus[bot] amd-argus[bot] commented on Oct 20, 2026 23:10 UTC
    Reviewer

    <!-- argus:v1 pr=13864 sha=821e24e243b7 mode=review -->

    ARGUS

    Findings

    <details>
    <summary>1 more finding(s), not posted inline</summary>

  • projects/rccl/src/device/prims_simple.h:936 (Low) Five new inline __scoped_atomic_load_n casts duplicate the existing ld_relaxed_sys_global() helper (op128.h:438)
  • </details>

    Bottom line: Looks approval ready.

    <sub>Comment <code>@argus review</code> to re-run, or <code>@argus re-review</code> after pushing.</sub>

    omor1 omor1 commented on code on Oct 20, 2026 23:33 UTC
    Code Review
    projects/rccl/src/device/prims_ll.h:1
    @@ -144,7 +144,8 @@ class Primitives<T, RedOp, Fan, Direct, ProtoLL, P2p, isNetOffload, Metadata, Pi
           int spins = 0;
           while (sendConnHeadCache + NCCL_STEPS < sendConnHead + 1) {
             __builtin_amdgcn_s_sleep(1);
    -        sendConnHeadCache = atomicAdd((unsigned long long*)sendConnHeadPtr, 0);
    +        sendConnHeadCache = __scoped_atomic_load_n((__attribute__((address_space(1))) uint64_t*)sendConnHeadPtr,
    +                                                   __ATOMIC_RELAXED, __MEMORY_SCOPE_SYSTEM);

    atomicAdd seems to be device-scope. Why does the load need to be system scope?

    alex-breslow-amd alex-breslow-amd commented on code on Oct 21, 2026 02:06 UTC
    Code Review
    projects/rccl/src/device/prims_ll.h:1
    @@ -144,7 +144,8 @@ class Primitives<T, RedOp, Fan, Direct, ProtoLL, P2p, isNetOffload, Metadata, Pi
           int spins = 0;
           while (sendConnHeadCache + NCCL_STEPS < sendConnHead + 1) {
             __builtin_amdgcn_s_sleep(1);
    -        sendConnHeadCache = atomicAdd((unsigned long long*)sendConnHeadPtr, 0);
    +        sendConnHeadCache = __scoped_atomic_load_n((__attribute__((address_space(1))) uint64_t*)sendConnHeadPtr,
    +                                                   __ATOMIC_RELAXED, __MEMORY_SCOPE_SYSTEM);

    Yeah, it should be device scope generally. I had a similar thought given the memory is local to the device. It turns out it doesn't make much of performance difference, so I left it as system scope. It's more idiot-proof if we start polling on CPU memory or remote GPU memory.

    doru1004 doru1004 commented on code on Oct 21, 2026 21:52 UTC
    Code Review
    projects/rccl/src/device/prims_simple.h:1
    @@ -931,7 +931,9 @@ __device__ void setDataPtrs(void const* inputBuf, void* outputBuf, uint64_t redO
           if (slot) {
             T* exchgPtr;
             directBuff = (T*)outputBuf;
    -        while ((void*)atomicAdd((unsigned long long*)slot, 0) != nullptr && !checkAbort(flags, Aborted, spins));
    +        while (__scoped_atomic_load_n((__attribute__((address_space(1))) uint64_t*)slot, __ATOMIC_RELAXED,

    Why is there an address space 1 here?

    alex-breslow-amd alex-breslow-amd commented on code on Oct 22, 2026 14:56 UTC
    Code Review
    projects/rccl/src/device/prims_simple.h:1
    @@ -931,7 +931,9 @@ __device__ void setDataPtrs(void const* inputBuf, void* outputBuf, uint64_t redO
           if (slot) {
             T* exchgPtr;
             directBuff = (T*)outputBuf;
    -        while ((void*)atomicAdd((unsigned long long*)slot, 0) != nullptr && !checkAbort(flags, Aborted, spins));
    +        while (__scoped_atomic_load_n((__attribute__((address_space(1))) uint64_t*)slot, __ATOMIC_RELAXED,

    So that the compiler emits a global_load instruction instead of a flat_load instruction. This can have a perf impact. Otherwise, the compiler can lose track of whether this access is to LDS or global memory and have to emit a conservative flat VMEM instruction.

    Reviewers
    Assignees
    alex-breslow-amd alex-breslow-amd
    Labels
    risk: low confidence: 86.4% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 21.4% (LOW) · Higher = larger blast radius
    21.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 86.4% · Lower = rushed or shallow review
    86.4%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 2.9% · Safe to merge
    2.9%