[PATCH] D137553: [MemorySSA] Delete dead MemoryUseOrDef for CallInst when clone loop basicblock

luxufan via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 7 07:23:24 PST 2022


StephenFan created this revision.
StephenFan added reviewers: nikic, asbirlea.
Herald added subscribers: george.burgess.iv, hiraditya.
Herald added a project: All.
StephenFan requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

If globals-aa is enabled, because of the deletion of memory instructions, there
may be call instruction that is not in ModOrRefSet but is a MemoryUseOrDef.
This causes the crash in the process of clone uses and defs.

Fixes https://github.com/llvm/llvm-project/issues/58719


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D137553

Files:
  llvm/lib/Analysis/MemorySSAUpdater.cpp
  llvm/test/Transforms/SimpleLoopUnswitch/pr58719.ll


Index: llvm/test/Transforms/SimpleLoopUnswitch/pr58719.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/SimpleLoopUnswitch/pr58719.ll
@@ -0,0 +1,19 @@
+; RUN: opt -passes="require<globals-aa>,cgscc(instcombine),recompute-globalsaa,function(loop-mssa(simple-loop-unswitch<nontrivial>),print<memoryssa>)" -disable-output < %s 2>&1 | FileCheck %s
+define void @f() {
+entry:
+  %0 = load i16, ptr null, align 1
+  ret void
+}
+
+define void @g(i1 %tobool.not) {
+entry:
+  br label %for.cond
+
+for.cond:                                         ; preds = %if.then, %for.cond, %entry
+  br i1 %tobool.not, label %if.then, label %for.cond
+
+if.then:                                          ; preds = %for.cond
+; CHECK-NOT: MemoryUse(liveOnEntry)
+  call void @f()
+  br label %for.cond
+}
Index: llvm/lib/Analysis/MemorySSAUpdater.cpp
===================================================================
--- llvm/lib/Analysis/MemorySSAUpdater.cpp
+++ llvm/lib/Analysis/MemorySSAUpdater.cpp
@@ -727,6 +727,33 @@
     }
   };
 
+  auto RemoveDeadMemoryAccess = [&](BasicBlock *BB) {
+    // If globals-aa is enabled, because of the deletion of memory instructions,
+    // there may be call instruction that is not in ModOrRefSet but is a
+    // MemoryUseOrDef. This causes the crash in the process of clone uses and
+    // defs.
+    BasicBlock *NewBlock = cast_or_null<BasicBlock>(VMap.lookup(BB));
+    if (!NewBlock)
+      return;
+    const MemorySSA::AccessList *BA = MSSA->getBlockAccesses(BB);
+    if (!BA)
+      return;
+    SmallVector<Instruction *, 2> ToRemove;
+    for (const MemoryAccess &MA : *BA) {
+      if (const MemoryUseOrDef *MUD = dyn_cast<MemoryUseOrDef>(&MA)) {
+        Instruction *Inst = MUD->getMemoryInst();
+        if (auto *Call = dyn_cast<CallInst>(Inst)) {
+          ModRefInfo ModRef = MSSA->AA->getModRefInfo(Call, None);
+          if (!isModOrRefSet(ModRef))
+            ToRemove.push_back(Inst);
+        }
+      }
+    }
+    for (auto *Inst : ToRemove) {
+      removeMemoryAccess(Inst);
+    }
+  };
+
   auto ProcessBlock = [&](BasicBlock *BB) {
     BasicBlock *NewBlock = cast_or_null<BasicBlock>(VMap.lookup(BB));
     if (!NewBlock)
@@ -744,6 +771,9 @@
     cloneUsesAndDefs(BB, NewBlock, VMap, MPhiMap);
   };
 
+  for (auto *BB : llvm::concat<BasicBlock *const>(LoopBlocks, ExitBlocks))
+    RemoveDeadMemoryAccess(BB);
+
   for (auto *BB : llvm::concat<BasicBlock *const>(LoopBlocks, ExitBlocks))
     ProcessBlock(BB);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137553.473661.patch
Type: text/x-patch
Size: 2538 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221107/e056d2ee/attachment.bin>


More information about the llvm-commits mailing list