[llvm] [LLVM] Refine MemoryEffect handling for target-specific intrinsics (PR #155590)

Paul Walker via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 20 06:46:08 PST 2025


================
@@ -277,6 +277,30 @@ static bool areLoadsReorderable(const LoadInst *Use,
   return !(SeqCstUse || MayClobberIsAcquire);
 }
 
+bool writeToSameTargetMemLoc(const CallBase *CallFirst,
+                             const CallBase *CallSecond) {
+  MemoryEffects ME1 = CallFirst->getMemoryEffects();
+  MemoryEffects ME2 = CallSecond->getMemoryEffects();
+
+  auto writes = [](ModRefInfo m) {
+    return m != ModRefInfo::NoModRef && m != ModRefInfo::Ref;
+  };
+
+  for (unsigned ILoc = static_cast<unsigned>(IRMemLocation::FirstTarget);
+       ILoc <= static_cast<unsigned>(IRMemLocation::Last); ++ILoc) {
+    const auto Loc = static_cast<IRMemLocation>(ILoc);
+
+    if (!writes(ME1.getModRef(Loc)))
+      continue;
+    if (!writes(ME2.getModRef(Loc)))
+      continue;
+
+    // Both have write capability to the same location.
+    return true;
+  }
----------------
paulwalker-arm wrote:

You shouldn't need an iterative approach here given MemoryEffects works like a bitfield. If you move the `onlyAccessesTargetMemory` calls into this function you'll then be able to safely write something like:
```
!(CU & CB & MemoryEffects::writeOnly()).onlyReadsMemory()
```
Doing this means the same code can later support inaccessible memory as well, once we're able/want to remove/simplify the onlyAccessesTargetMemory guards.

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


More information about the llvm-commits mailing list