[llvm] 7b08d9d - Reland [MemorySSA] Add pass to print results of MemorySSA walker

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 1 18:59:37 PDT 2021


Author: Arthur Eubanks
Date: 2021-09-01T18:58:57-07:00
New Revision: 7b08d9da5581865a80a0edc46c666cf262ee62d1

URL: https://github.com/llvm/llvm-project/commit/7b08d9da5581865a80a0edc46c666cf262ee62d1
DIFF: https://github.com/llvm/llvm-project/commit/7b08d9da5581865a80a0edc46c666cf262ee62d1.diff

LOG: Reland [MemorySSA] Add pass to print results of MemorySSA walker

Reviewed By: asbirlea

Differential Revision: https://reviews.llvm.org/D109028

Added: 
    llvm/test/Analysis/MemorySSA/print-walker.ll

Modified: 
    llvm/include/llvm/Analysis/MemorySSA.h
    llvm/lib/Analysis/MemorySSA.cpp
    llvm/lib/Passes/PassRegistry.def

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/MemorySSA.h b/llvm/include/llvm/Analysis/MemorySSA.h
index 024d2c4b003c9..a05cefacceec4 100644
--- a/llvm/include/llvm/Analysis/MemorySSA.h
+++ b/llvm/include/llvm/Analysis/MemorySSA.h
@@ -792,8 +792,7 @@ class MemorySSA {
   enum InsertionPlace { Beginning, End, BeforeTerminator };
 
 protected:
-  // Used by Memory SSA annotater, dumpers, and wrapper pass
-  friend class MemorySSAAnnotatedWriter;
+  // Used by Memory SSA dumpers and wrapper pass
   friend class MemorySSAPrinterLegacyPass;
   friend class MemorySSAUpdater;
 
@@ -953,6 +952,17 @@ class MemorySSAPrinterPass : public PassInfoMixin<MemorySSAPrinterPass> {
   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
 };
 
+/// Printer pass for \c MemorySSA via the walker.
+class MemorySSAWalkerPrinterPass
+    : public PassInfoMixin<MemorySSAWalkerPrinterPass> {
+  raw_ostream &OS;
+
+public:
+  explicit MemorySSAWalkerPrinterPass(raw_ostream &OS) : OS(OS) {}
+
+  PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
+};
+
 /// Verifier pass for \c MemorySSA.
 struct MemorySSAVerifierPass : PassInfoMixin<MemorySSAVerifierPass> {
   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);

diff  --git a/llvm/lib/Analysis/MemorySSA.cpp b/llvm/lib/Analysis/MemorySSA.cpp
index dc830ca7a812a..06976c444336b 100644
--- a/llvm/lib/Analysis/MemorySSA.cpp
+++ b/llvm/lib/Analysis/MemorySSA.cpp
@@ -95,13 +95,11 @@ static cl::opt<bool, true>
     VerifyMemorySSAX("verify-memoryssa", cl::location(VerifyMemorySSA),
                      cl::Hidden, cl::desc("Enable verification of MemorySSA."));
 
-namespace llvm {
+namespace {
 
 /// An assembly annotator class to print Memory SSA information in
 /// comments.
 class MemorySSAAnnotatedWriter : public AssemblyAnnotationWriter {
-  friend class MemorySSA;
-
   const MemorySSA *MSSA;
 
 public:
@@ -120,7 +118,29 @@ class MemorySSAAnnotatedWriter : public AssemblyAnnotationWriter {
   }
 };
 
-} // end namespace llvm
+/// An assembly annotator class to print Memory SSA information in
+/// comments.
+class MemorySSAWalkerAnnotatedWriter : public AssemblyAnnotationWriter {
+  MemorySSA *MSSA;
+  MemorySSAWalker *Walker;
+
+public:
+  MemorySSAWalkerAnnotatedWriter(MemorySSA *M)
+      : MSSA(M), Walker(M->getWalker()) {}
+
+  void emitInstructionAnnot(const Instruction *I,
+                            formatted_raw_ostream &OS) override {
+    if (MemoryAccess *MA = MSSA->getMemoryAccess(I)) {
+      MemoryAccess *Clobber = Walker->getClobberingMemoryAccess(MA);
+      OS << "; " << *MA;
+      if (Clobber)
+        OS << " - clobbered by " << *Clobber;
+      OS << "\n";
+    }
+  }
+};
+
+} // namespace
 
 namespace {
 
@@ -2351,6 +2371,16 @@ PreservedAnalyses MemorySSAPrinterPass::run(Function &F,
   return PreservedAnalyses::all();
 }
 
+PreservedAnalyses MemorySSAWalkerPrinterPass::run(Function &F,
+                                                  FunctionAnalysisManager &AM) {
+  auto &MSSA = AM.getResult<MemorySSAAnalysis>(F).getMSSA();
+  OS << "MemorySSA (walker) for function: " << F.getName() << "\n";
+  MemorySSAWalkerAnnotatedWriter Writer(&MSSA);
+  F.print(OS, &Writer);
+
+  return PreservedAnalyses::all();
+}
+
 PreservedAnalyses MemorySSAVerifierPass::run(Function &F,
                                              FunctionAnalysisManager &AM) {
   AM.getResult<MemorySSAAnalysis>(F).getMSSA().verifyMemorySSA();

diff  --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index fef3aba69e25f..29ab4bbcbdbb7 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -297,6 +297,7 @@ FUNCTION_PASS("print<inliner-size-estimator>",
   InlineSizeEstimatorAnalysisPrinterPass(dbgs()))
 FUNCTION_PASS("print<loops>", LoopPrinterPass(dbgs()))
 FUNCTION_PASS("print<memoryssa>", MemorySSAPrinterPass(dbgs()))
+FUNCTION_PASS("print<memoryssa-walker>", MemorySSAWalkerPrinterPass(dbgs()))
 FUNCTION_PASS("print<phi-values>", PhiValuesPrinterPass(dbgs()))
 FUNCTION_PASS("print<regions>", RegionInfoPrinterPass(dbgs()))
 FUNCTION_PASS("print<scalar-evolution>", ScalarEvolutionPrinterPass(dbgs()))

diff  --git a/llvm/test/Analysis/MemorySSA/print-walker.ll b/llvm/test/Analysis/MemorySSA/print-walker.ll
new file mode 100644
index 0000000000000..5c44b5b2c758e
--- /dev/null
+++ b/llvm/test/Analysis/MemorySSA/print-walker.ll
@@ -0,0 +1,35 @@
+; RUN: opt -passes='print<memoryssa-walker>' -disable-output < %s 2>&1 | FileCheck %s
+
+; CHECK: define void @test
+; CHECK: 1 = MemoryDef(liveOnEntry)->liveOnEntry - clobbered by 0 = MemoryDef(liveOnEntry)
+; CHECK: store i8 42, i8* %a1
+; CHECK: 2 = MemoryDef(1)->liveOnEntry - clobbered by 0 = MemoryDef(liveOnEntry)
+; CHECK: store i8 42, i8* %a2
+; CHECK: MemoryUse(1) MustAlias - clobbered by 1 = MemoryDef(liveOnEntry)->liveOnEntry
+; CHECK: %l1 = load i8, i8* %a1
+; CHECK: MemoryUse(2) MustAlias - clobbered by 2 = MemoryDef(1)->liveOnEntry
+; CHECK: %l2 = load i8, i8* %a2
+; CHECK: 3 = MemoryDef(2)->liveOnEntry - clobbered by 0 = MemoryDef(liveOnEntry)
+; CHECK: store i8 42, i8* %p
+; CHECK: 4 = MemoryDef(3)->3 MustAlias - clobbered by 3 = MemoryDef(2)->liveOnEntry
+; CHECK: store i8 42, i8* %p
+; CHECK: MemoryUse(4) MustAlias - clobbered by 4 = MemoryDef(3)->3 MustAlias
+; CHECK: %p1 = load i8, i8* %p
+; CHECK: MemoryUse(4) MustAlias - clobbered by 4 = MemoryDef(3)->3 MustAlias
+; CHECK: %p2 = load i8, i8* %p
+
+define void @test(i8* %p) {
+  %a1 = alloca i8
+  %a2 = alloca i8
+  store i8 42, i8* %a1
+  store i8 42, i8* %a2
+  %l1 =  load i8, i8* %a1
+  %l2 =  load i8, i8* %a2
+
+  store i8 42, i8* %p
+  store i8 42, i8* %p
+  %p1 =  load i8, i8* %p
+  %p2 =  load i8, i8* %p
+
+  ret void
+}


        


More information about the llvm-commits mailing list