[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 18:51:42 PST 2022


StephenFan updated this revision to Diff 473856.
StephenFan added a comment.

1. Replace NULL as a actual address.
2. Add `function(loop-mssa(loop-simplifycfg))` in passes. Only if MemorySSA is constructed before recomputing globalsaa, the crash happens.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137553/new/

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,20 @@
+; RUN: opt -passes="require<globals-aa>,cgscc(instcombine),function(loop-mssa(loop-simplifycfg)),recompute-globalsaa,function(loop-mssa(simple-loop-unswitch<nontrivial>),print<memoryssa>)" -disable-output < %s 2>&1 | FileCheck %s
+
+define void @f(ptr %p) {
+entry:
+  %0 = load i16, ptr %p, 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 cloning 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.473856.patch
Type: text/x-patch
Size: 2584 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221108/4a684e02/attachment.bin>


More information about the llvm-commits mailing list