[llvm-branch-commits] [llvm] [AMDGPU] Extract SIAtomicAddrSpace and fence MMRA parsing from SIMemoryLegalizer (PR #231854)

Fabian Ritter via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Oct 28 01:21:07 PDT 2026


================
@@ -478,4 +481,70 @@ bool isClobberedInFunction(const LoadInst *Load, MemorySSA *MSSA,
   return false;
 }
 
+static constexpr SIAtomicAddrSpace LLVMToSIAtomicAddrSpaceMapping[] = {
+    SIAtomicAddrSpace::FLAT,    //< AMDGPUAS::FLAT_ADDRESS
+    SIAtomicAddrSpace::GLOBAL,  //< AMDGPUAS::GLOBAL_ADDRESS
+    SIAtomicAddrSpace::GDS,     //< AMDGPUAS::REGION_ADDRESS
+    SIAtomicAddrSpace::LDS,     //< AMDGPUAS::LOCAL_ADDRESS
+    SIAtomicAddrSpace::GLOBAL,  //< AMDGPUAS::CONSTANT_ADDRESS
+    SIAtomicAddrSpace::SCRATCH, //< AMDGPUAS::PRIVATE_ADDRESS
+    SIAtomicAddrSpace::GLOBAL,  //< AMDGPUAS::CONSTANT_ADDRESS_32BIT
+    SIAtomicAddrSpace::GLOBAL,  //< AMDGPUAS::BUFFER_FAT_POINTER
+    SIAtomicAddrSpace::GLOBAL,  //< AMDGPUAS::BUFFER_RESOURCE
+    SIAtomicAddrSpace::GLOBAL,  //< AMDGPUAS::BUFFER_STRIDED_POINTER
+};
+
+SIAtomicAddrSpace toSIAtomicAddrSpace(unsigned LLVMAddrSpace) {
+  constexpr unsigned SizeOfLLVMToSIAtomicAddrSpaceMapping =
+      sizeof(LLVMToSIAtomicAddrSpaceMapping) /
+      sizeof(LLVMToSIAtomicAddrSpaceMapping[0]);
+  if (LLVMAddrSpace > AMDGPUAS::MAX_AMDGPU_ADDRESS)
+    return SIAtomicAddrSpace::GLOBAL;
+  if (LLVMAddrSpace < SizeOfLLVMToSIAtomicAddrSpaceMapping)
+    return LLVMToSIAtomicAddrSpaceMapping[LLVMAddrSpace];
+  return SIAtomicAddrSpace::OTHER;
+}
+
+static const StringMap<SIAtomicAddrSpace> ASNames = {{
+    {"global", SIAtomicAddrSpace::GLOBAL},
+    {"local", SIAtomicAddrSpace::LDS},
+}};
+
+std::optional<SIAtomicAddrSpace>
+getSynchronizeAddrSpace(const MMRAMetadata &MMRA, const Function *DiagFn,
+                        const DebugLoc &DL) {
+  static constexpr StringLiteral FenceASPrefix = "amdgpu-synchronize-as";
+
+  if (!MMRA)
+    return std::nullopt;
+
+  SIAtomicAddrSpace Result = SIAtomicAddrSpace::NONE;
+  for (const auto &[Prefix, Suffix] : MMRA) {
+    if (Prefix != FenceASPrefix)
+      continue;
+
+    if (auto It = ASNames.find(Suffix); It != ASNames.end()) {
+      Result |= It->second;
+    } else if (DiagFn) {
+      SmallString<128> Str;
+      raw_svector_ostream OS(Str);
+      OS << "unknown address space '" << Suffix << "'; expected one of ";
+      ListSeparator LS;
+      for (const auto &[Name, Val] : ASNames)
+        OS << LS << '\'' << Name << '\'';
+      DiagFn->getContext().diagnose(
+          DiagnosticInfoUnsupported(*DiagFn, Str, DL, DS_Warning));
+    }
+  }
+
+  if (Result == SIAtomicAddrSpace::NONE)
+    return std::nullopt;
----------------
ritter-x2a wrote:

We currently define all synchronization to only affect at most `SIAtomicAddrSpace::ATOMIC`, as can be seen from the returns in [SIMemOpAccess::toSIAtomicScope](https://github.com/llvm/llvm-project/blob/9f0c2f993640bcafb3e2722f1fe68d564b537276/llvm/lib/Target/AMDGPU/SIMemoryLegalizer.cpp#L789-L826), where the second tuple value is always `ATOMIC` (for non-one-as syncscopes) or `ATOMIC` intersected with something (for one-as syncscopes).
We'd [issue diagnostics](https://github.com/llvm/llvm-project/blob/14a8bae5a7664333dcb7618a1e3e30b9e07e4ce3/llvm/lib/Target/AMDGPU/SIMemoryLegalizer.cpp#L918-L923) if something including `SIAtomicAddrSpace::OTHER` was involved.
So, with the way this is set up and used right now, I'd argue that `ATOMIC` is the right choice here.

But yes, that's odd. Acquire/release without restrictions should affect all memory accesses. One specific point that I see where this gives us (at least theoretical) trouble is the BARRIER address space, 15, which falls under `OTHER`. The "fence-release; s_barrier; fence-acquire" pattern relies on the fences pairing with operations in the BARRIER AS (that's the reason why one-as fences don't work for this pattern, they also restrict what operations the fences pair with).
I'd really like to not put these refactors (including the renaming of `SIAtomicAddrSpace` to `AMDGPU::MemoryApertureSet`) on the critical path for landing the correctness fix in #219991, though.

https://github.com/llvm/llvm-project/pull/231854


More information about the llvm-branch-commits mailing list