[llvm] [SPIRV] Let atomic store store pointers (PR #201251)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 2 19:48:47 PDT 2026


https://github.com/fineg74 created https://github.com/llvm/llvm-project/pull/201251

SPIRV atomic store permits only integer or floats as per SPIRV spec. When compiling libc there several places in the code where pointers are atomically stored causing compilation to break. It can be fixed by using casting in the libc code but in order to keep the libc code clean it is preferrable to do it in SPIRV backend. This change will cast pointer parameters to integers of appropriate size and generate atomic store instruction that uses integers per SPIRV spec.

>From 7b3468282ea87580ddca788ca77f87ba3708cd08 Mon Sep 17 00:00:00 2001
From: "Fine, Gregory" <gregory.fine at intel.com>
Date: Tue, 2 Jun 2026 18:51:17 -0700
Subject: [PATCH] Let atomic store store pointers

---
 .../Target/SPIRV/SPIRVInstructionSelector.cpp | 51 ++++++++++++++++---
 llvm/lib/Target/SPIRV/SPIRVTypeInst.cpp       |  4 ++
 llvm/lib/Target/SPIRV/SPIRVTypeInst.h         |  2 +
 3 files changed, 50 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp b/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
index c575932cd8709..480f83ea0e610 100644
--- a/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
@@ -2051,7 +2051,7 @@ bool SPIRVInstructionSelector::selectAtomicStore(MachineInstr &I) const {
 
   SPIRVTypeInst PtrType = GR.getSPIRVTypeForVReg(Ptr);
   SPIRVTypeInst PointeeType = GR.getPointeeType(PtrType);
-  if (!PointeeType.isTypeIntOrFloat())
+  if (!PointeeType.isTypeIntOrFloat() && !PointeeType.isTypePtr())
     return diagnoseUnsupported(I,
                                "Lowering to SPIR-V of atomic store is only "
                                "allowed for integer or floating point types");
@@ -2073,12 +2073,49 @@ bool SPIRVInstructionSelector::selectAtomicStore(MachineInstr &I) const {
   Register MemSemReg = buildI32Constant(MemSem | StorageClass, I);
 
   MachineIRBuilder MIRBuilder(I);
-  auto AtomicStore = MIRBuilder.buildInstr(SPIRV::OpAtomicStore)
-                         .addUse(Ptr)
-                         .addUse(ScopeReg)
-                         .addUse(MemSemReg)
-                         .addUse(StoreVal);
-  AtomicStore.constrainAllUses(TII, TRI, RBI);
+
+  if (PointeeType.isTypePtr()) {
+    auto PtrSize = GR.getPointerSize();
+    SPIRVTypeInst SpirvType = GR.getOrCreateSPIRVIntegerType(PtrSize, I, TII);
+
+    Register NewVRegVal =
+        MRI->createGenericVirtualRegister(LLT::scalar(PtrSize));
+    MRI->setRegClass(NewVRegVal, MRI->getRegClassOrNull(StoreVal));
+    GR.assignSPIRVTypeToVReg(SpirvType, NewVRegVal, MIRBuilder.getMF());
+    MIRBuilder.buildInstr(SPIRV::OpConvertPtrToU)
+        .addDef(NewVRegVal)
+        .addUse(GR.getSPIRVTypeID(SpirvType)) // Result type
+        .addUse(StoreVal)                     // Pointer operand
+        .constrainAllUses(TII, TRI, RBI);
+
+    Register NewVRegPtr =
+        MRI->createGenericVirtualRegister(LLT::scalar(PtrSize));
+    MRI->setRegClass(NewVRegPtr, MRI->getRegClassOrNull(Ptr));
+    SPIRVTypeInst PtrType = GR.getOrCreateSPIRVPointerType(
+        SpirvType, MIRBuilder,
+        addressSpaceToStorageClass(MemOp.getAddrSpace(), STI));
+    GR.assignSPIRVTypeToVReg(PtrType, NewVRegPtr, MIRBuilder.getMF());
+
+    MIRBuilder.buildInstr(SPIRV::OpBitcast)
+        .addDef(NewVRegPtr)
+        .addUse(GR.getSPIRVTypeID(PtrType))
+        .addUse(Ptr)
+        .constrainAllUses(TII, TRI, RBI);
+
+    MIRBuilder.buildInstr(SPIRV::OpAtomicStore)
+        .addUse(NewVRegPtr)
+        .addUse(ScopeReg)
+        .addUse(MemSemReg)
+        .addUse(NewVRegVal)
+        .constrainAllUses(TII, TRI, RBI);
+  } else {
+    auto AtomicStore = MIRBuilder.buildInstr(SPIRV::OpAtomicStore)
+                           .addUse(Ptr)
+                           .addUse(ScopeReg)
+                           .addUse(MemSemReg)
+                           .addUse(StoreVal);
+    AtomicStore.constrainAllUses(TII, TRI, RBI);
+  }
   return true;
 }
 
diff --git a/llvm/lib/Target/SPIRV/SPIRVTypeInst.cpp b/llvm/lib/Target/SPIRV/SPIRVTypeInst.cpp
index 7a7d62f9297f5..58857b6a3eac2 100644
--- a/llvm/lib/Target/SPIRV/SPIRVTypeInst.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVTypeInst.cpp
@@ -38,4 +38,8 @@ bool SPIRVTypeInst::isTypeIntN(unsigned N) const {
 bool SPIRVTypeInst::isAnyTypeFloat() const {
   return MI->getOpcode() == SPIRV::OpTypeFloat;
 }
+
+bool SPIRVTypeInst::isTypePtr() const {
+  return MI->getOpcode() == SPIRV::OpTypePointer;
+}
 } // namespace llvm
diff --git a/llvm/lib/Target/SPIRV/SPIRVTypeInst.h b/llvm/lib/Target/SPIRV/SPIRVTypeInst.h
index 39648ef622d3e..0d371f441cf9e 100644
--- a/llvm/lib/Target/SPIRV/SPIRVTypeInst.h
+++ b/llvm/lib/Target/SPIRV/SPIRVTypeInst.h
@@ -55,6 +55,8 @@ class SPIRVTypeInst {
   bool isAnyTypeFloat() const;
   // Returns true if this is an OpTypeInt or OpTypeFloat instruction.
   bool isTypeIntOrFloat() const { return isTypeIntN() || isAnyTypeFloat(); }
+  // Returns true if this is an OpTypePointer instruction.
+  bool isTypePtr() const;
 
   friend struct DenseMapInfo<SPIRVTypeInst>;
 };



More information about the llvm-commits mailing list