[llvm] [SPIRV] Handle atomic cmpxchg users in SPIRVLegalizePointerCast (PR #202098)
Julian Klappenbach via llvm-commits
llvm-commits at lists.llvm.org
Sat Jun 6 17:47:05 PDT 2026
https://github.com/jklappenbach created https://github.com/llvm/llvm-project/pull/202098
`SPIRVLegalizePointerCast` rewrites the users of an `llvm.spv.ptrcast`, but it
only handles `load`, `store`, `spv.gep`, `spv.store` and `spv.assign_ptr_type`
users. Every other user falls through to
```cpp
llvm_unreachable("Unsupported ptrcast user. Please fix.");
```
which is a crash in a release (NDEBUG) build.
A `cmpxchg` whose pointer flows through a ptrcast — e.g. an `i32` atomic accessed
through a differently-typed global or descriptor-bound buffer — is lowered by
`spirv-emit-intrinsics` to an `llvm.spv.cmpxchg` that consumes the cast, and then
trips this unhandled case. HLSL's `InterlockedCompareExchange` lowers its pointer
differently, so the in-tree frontends never exercised it (the crash was surfaced
by an out-of-tree frontend emitting `Buffer<int>.atomicCompareExchange`).
The cmpxchg consumes the pointer directly and its value type already matches the
pointee, so the ptrcast is spurious. This handles the `spv.cmpxchg` user the same
way as `spv.gep`: replace the casted pointer with the original operand and drop
the cast.
A regression case is added to `CodeGen/SPIRV/passes/SPIRVLegalizePointerCast.ll`
(the new function crashes without the fix and passes with it — verified that the
`spv.ptrcast` is removed and the `spv.cmpxchg` consumes the original pointer).
🤖 Generated with [Claude Code](https://claude.com/claude-code)
>From 74b63b8c60c87fb21829473cb10969723fb689b6 Mon Sep 17 00:00:00 2001
From: Julian Klappenbach <julian at twilight.digital>
Date: Sat, 6 Jun 2026 20:36:16 -0400
Subject: [PATCH] [SPIRV] Handle atomic cmpxchg users in
SPIRVLegalizePointerCast
SPIRVLegalizePointerCast rewrites the users of an llvm.spv.ptrcast, but it
only handles load, store, spv.gep, spv.store and spv.assign_ptr_type users.
Every other user reaches
llvm_unreachable("Unsupported ptrcast user. Please fix.");
which is a segfault in a release (NDEBUG) build.
A cmpxchg whose pointer flows through a ptrcast -- for example an i32 atomic
accessed through a differently-typed global or descriptor-bound buffer -- is
lowered by spirv-emit-intrinsics to an llvm.spv.cmpxchg that consumes the
cast, and then trips this unhandled case. HLSL's InterlockedCompareExchange
lowers its pointer differently, so it was never exercised.
The cmpxchg consumes the pointer directly and its value type already matches
the pointee, so the ptrcast is spurious. Handle the spv.cmpxchg user the same
way as spv.gep: replace the casted pointer with the original operand and drop
the cast.
---
.../Target/SPIRV/SPIRVLegalizePointerCast.cpp | 11 +++++++++++
.../SPIRV/passes/SPIRVLegalizePointerCast.ll | 18 ++++++++++++++++++
2 files changed, 29 insertions(+)
diff --git a/llvm/lib/Target/SPIRV/SPIRVLegalizePointerCast.cpp b/llvm/lib/Target/SPIRV/SPIRVLegalizePointerCast.cpp
index cb03dcae21ede..0d1b4f38e4356 100644
--- a/llvm/lib/Target/SPIRV/SPIRVLegalizePointerCast.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVLegalizePointerCast.cpp
@@ -544,6 +544,17 @@ class SPIRVLegalizePointerCastImpl {
continue;
}
+ // An atomic compare-exchange consumes the pointer directly (its value
+ // type already matches the pointee). The ptrcast inserted ahead of it is
+ // spurious — bypass it like spv_gep, so the cmpxchg uses the original
+ // pointer. Without this the loop falls through to llvm_unreachable below
+ // (a segfault in release) for any cmpxchg through a casted pointer.
+ if (Intrin->getIntrinsicID() == Intrinsic::spv_cmpxchg) {
+ GR->replaceAllUsesWith(CastedOperand, OriginalOperand,
+ /* DeleteOld= */ false);
+ continue;
+ }
+
if (Intrin->getIntrinsicID() == Intrinsic::spv_store) {
Align Alignment;
if (ConstantInt *C = dyn_cast<ConstantInt>(Intrin->getOperand(3)))
diff --git a/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizePointerCast.ll b/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizePointerCast.ll
index 7d2fb71275290..508803ee4d183 100644
--- a/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizePointerCast.ll
+++ b/llvm/test/CodeGen/SPIRV/passes/SPIRVLegalizePointerCast.ll
@@ -6,6 +6,7 @@
@M = internal addrspace(10) global [4 x <2 x float>] zeroinitializer, align 4
@OUT = internal addrspace(10) global float zeroinitializer, align 4
+ at OUTI = internal addrspace(10) global i32 zeroinitializer, align 4
; Loading a <5 x float> through a [4 x <2 x float>] forces emit-intrinsics to
; insert spv.ptrcast; legalize-pointer-cast lowers it to typed <2 x float>
@@ -25,4 +26,21 @@ entry:
ret void
}
+; A cmpxchg whose pointer flows through an emit-intrinsics-inserted spv.ptrcast
+; (an i32 atomic accessed through the differently-typed @M) must be handled by
+; legalize-pointer-cast: the cmpxchg consumes the original pointer directly, so
+; the spurious ptrcast is dropped. Previously this fell through to
+; llvm_unreachable("Unsupported ptrcast user"), a release-mode crash.
+
+define spir_func void @cmpxchg_through_ptrcast() #0 {
+; CHECK-LABEL: define spir_func void @cmpxchg_through_ptrcast(
+; CHECK-NOT: call {{.*}}@llvm.spv.ptrcast
+; CHECK: call i32 {{.*}}@llvm.spv.cmpxchg.p10(ptr addrspace(10) @M,
+entry:
+ %pair = cmpxchg ptr addrspace(10) @M, i32 0, i32 1 monotonic monotonic
+ %old = extractvalue { i32, i1 } %pair, 0
+ store i32 %old, ptr addrspace(10) @OUTI, align 4
+ ret void
+}
+
attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
More information about the llvm-commits
mailing list