Skip to content

[AMD] Swap floating-point values as integers in buffer atomic xchg - #11904

Merged
antiagainst merged 4 commits into
triton-lang:mainfrom
LiRunGuo:amd-fp64-buffer-xchg
Oct 13, 2026
Merged

antiagainst merged 4 commits into
triton-lang:mainfrom
LiRunGuo:amd-fp64-buffer-xchg

Conversation

@LiRunGuo

Copy link
Copy Markdown
Contributor

Background

tl.atomic_xchg on an fp64 tensor aborts the process on gfx942, gfx950 and gfx1250:

LLVM ERROR: Cannot select: t90: f64,ch = AMDGPUISD::BUFFER_ATOMIC_SWAP<(volatile dereferenceable load store (s64) ...
@triton.jit
def kernel(X, Y):
    offs = tl.arange(0, 256)
    old = tl.atomic_xchg(Y + offs, tl.load(X + offs))   # X, Y: *fp64
    tl.store(X + offs, old)

ConvertToBufferOps turns the atomic into amdg.buffer_atomic_rmw exch, which lowers to llvm.amdgcn.raw.ptr.buffer.atomic.swap on the value type, and the AMDGPU backend has no f64 buffer swap. fp32 swaps, i64 swaps and fp64 add/max compile fine (#11019 fixed the swap intrinsic name, not the type).

Summary

A swap only moves bits, so BufferEmitter::emitAtomicRMW now emits the XCHG intrinsic on the same-width integer type and bitcasts around it (fp32 swaps become i32 swaps, which is equivalent).

The new Python test also showed that the interpreter rejects floating-point atomic_xchg (ValueError: Unsupported data type) even though the GPU path accepts it; float and double are added to the interpreter's XCHG types.

Tests

  • test/Conversion/amd/buffer_load_store.mlir: f64 and f32 exch lower to an integer buffer.atomic.swap (the f64 case fails before, passes after). All AMD lit tests pass (test/Conversion/amd, test/TritonGPU/amd: 124).
  • test_core.py::test_tensor_atomic_xchg: int32/int64/fp32/fp64 tensor exchange, with and without using the returned values. Passes on H200 and in the interpreter (the float cases failed in the interpreter before).
  • Compile-only (to hsaco) matrix of atomic_xchg/atomic_add/atomic_max x fp64/fp32/i64/i32 x acq_rel/relaxed on gfx942/gfx950/gfx1250: 6 fp64 xchg aborts before, all 72 compile after.
  • I don't have AMD hardware, so the new Python test has not been run on an AMD GPU; CI should cover it.

New contributor declaration

  • I am not making a trivial change, such as fixing a typo in a comment.

  • I have written a PR description following these
    rules.

  • I have run pre-commit run --from-ref origin/main --to-ref HEAD.

  • Select one of the following.

    • I have added tests.
      • /test for lit tests
      • /unittest for C++ tests
      • /python/test for end-to-end tests
    • This PR does not need a test because FILL THIS IN.
  • Select one of the following.

    • I have not added any lit tests.
    • The lit tests I have added follow these best practices,
      including the "tests should be minimal" section. (Usually running Python code
      and using the instructions it generates is not minimal.)

tl.atomic_xchg on an fp64 tensor aborts compilation on gfx942, gfx950 and
gfx1250 with "LLVM ERROR: Cannot select: ... AMDGPUISD::BUFFER_ATOMIC_SWAP":
ConvertToBufferOps turns the atomic into a buffer atomic, and the AMDGPU
backend has no f64 buffer swap. A swap only moves bits, so emit the swap
intrinsic on the same-width integer type and bitcast around it.

The new test_tensor_atomic_xchg also exposed that the interpreter rejects
floating-point atomic_xchg; allow float and double there too.
The XCHG path converted each float element to an integer but kept the
vector, so a vector<2xf16>/vector<2xbf16> exchange became a <2 x i16>
buffer swap, which the AMDGPU backend cannot select either ("Cannot
select: v2i16 ... BUFFER_ATOMIC_SWAP"). The backend only has i32, i64
and f32 buffer swap patterns, so exchange a scalar integer of the whole
value width instead.
Small integer values cannot tell a bit-preserving swap from a
value-converting one, so exchange random bit patterns plus -0.0,
infinities, NaN payloads and a denormal, and compare bits. On targets
with buffer atomics, also check that the kernel uses buffer_atomic_swap,
so the test keeps covering the f64 buffer swap fix.
@antiagainst
antiagainst merged commit 872c90a into triton-lang:main Oct 13, 2026
11 checks passed
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