Skip to content

[AMDGPU] Use native atomics for reductions instead of runtime reduce_* helpers - #843

Open
paveltc wants to merge 1 commit into
Genesis-Embodied-AI:mainfrom
paveltc:perf/amdgpu-direct-atomic-reductions
Open

[AMDGPU] Use native atomics for reductions instead of runtime reduce_* helpers#843
paveltc wants to merge 1 commit into
Genesis-Embodied-AI:mainfrom
paveltc:perf/amdgpu-direct-atomic-reductions

Conversation

@paveltc

@paveltc paveltc commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Summary

On AMDGPU, optimized_reduction() lowered every reduction (+=, atomic_min, atomic_max, ...) to a call into a runtime reduce_* helper (reduce_add_i32, reduce_add_f32, reduce_min_f32, ...). Those helpers take addrspace(0) pointers, so a global SNode destination was routed through a flat pointer for the call, and the f32-add helper path did not lower to the hardware float atomic.

This PR removes the AMDGPU optimized_reduction() override. Reduction atomics now fall through the standard atomic visitor to real_type_atomic() / integral_type_atomic(), which already emit native LLVM atomics (AtomicRMW / FAdd / FMin / FMax) at "agent" syncscope via kernel_atomic_syncscope(). On gfx942 these lower to hardware global_atomic_* instructions and preserve the destination address space.

This is a simplification / dependency removal — it deletes AMDGPU-specific reduction codegen in favor of the shared LLVM atomic path already used by the CPU and CUDA backends. This also aligns with the project's broader move toward native float atomics (cf. the separate Metal-backend migration in #788), and is a cleaner base for upcoming AMDGPU address-space work.

Changes

  • quadrants/codegen/amdgpu/codegen_amdgpu.cpp: remove optimized_reduction() override (+9/-27), replaced by an explanatory comment. Single-file change.

Validation (gfx942 / MI300X)

  • IR: f32 add now emits native atomicrmw fadd ... syncscope("agent"); i32 add/min/max likewise; no @reduce_* helper call.
  • Correctness: i32 add/min/max/and/or/xor and f32 add/min/max match NumPy across 1M+ elements.
  • Performance: perf-neutral vs. the helper path (f32 add ~0.72 ms, i32 add ~0.73 ms / 16M elems, matching baseline). Emitting the same reduction as a default-syncscope atomicrmw fadd regresses ~1000x due to software CAS lowering; this path correctly uses "agent" scope and avoids that.

Made with Cursor

…elpers

optimized_reduction() on AMDGPU lowered every reduction to a call into a runtime
reduce_* helper (reduce_add_i32, reduce_add_f32, reduce_min_f32, ...). Those
helpers take addrspace(0) pointers, so a global SNode destination had to be
routed through a flat pointer for the call, and the f32-add helper path did not
lower to the hardware float atomic.

Remove the AMDGPU optimized_reduction override. Reduction atomics now fall
through to real_type_atomic() / integral_type_atomic(), which emit native LLVM
atomics (AtomicRMW / FAdd / FMin / FMax) at "agent" syncscope via
kernel_atomic_syncscope(). On gfx942 these lower to hardware global_atomic_*
instructions and preserve the destination address space.

Verified on gfx942 (MI300X): reduction correctness for i32 add/min/max/and/or/xor
and f32 add/min/max matches NumPy. A contended f32 sum reduction, which regressed
~1000x when emitted as a default-syncscope atomicrmw fadd (software CAS lowering),
runs at the native agent-scoped hardware-atomic speed via this path.

Co-authored-by: Cursor <cursoragent@cursor.com>
@paveltc
paveltc force-pushed the perf/amdgpu-direct-atomic-reductions branch from ca76613 to 1c22013 Compare August 14, 2026 21:12
@paveltc

paveltc commented Aug 17, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 1c22013771

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants