[llvm] 72e7e58 - [MemorySSA] Always perform MemoryUses liveOnEntry optimization on MSSA construction

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 6 14:09:55 PDT 2023


Author: Arthur Eubanks
Date: 2023-07-06T14:09:47-07:00
New Revision: 72e7e5851f4aa1d7aef62d85338493ac34df5837

URL: https://github.com/llvm/llvm-project/commit/72e7e5851f4aa1d7aef62d85338493ac34df5837
DIFF: https://github.com/llvm/llvm-project/commit/72e7e5851f4aa1d7aef62d85338493ac34df5837.diff

LOG: [MemorySSA] Always perform MemoryUses liveOnEntry optimization on MSSA construction

Fixes invariant memory regressions in future DSE patches.

Also add a flag to print<memoryssa> to not ensure optimized uses to test this.

Noticeable compile time regression [1], but a future DSE change that depends on this more than makes up for it.

[1] https://llvm-compile-time-tracker.com/compare.php?from=9d5466849a770eeab222d5a5890376d3596e8ad6&to=95682dbe11d76a3342870437377216e96b167504&stat=instructions:u

Reviewed By: asbirlea

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

Added: 
    llvm/test/Analysis/MemorySSA/tbaa.ll

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

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/MemorySSA.h b/llvm/include/llvm/Analysis/MemorySSA.h
index d71b2ac6c487f9..94d7f1a78b8470 100644
--- a/llvm/include/llvm/Analysis/MemorySSA.h
+++ b/llvm/include/llvm/Analysis/MemorySSA.h
@@ -946,9 +946,11 @@ class MemorySSAAnalysis : public AnalysisInfoMixin<MemorySSAAnalysis> {
 /// Printer pass for \c MemorySSA.
 class MemorySSAPrinterPass : public PassInfoMixin<MemorySSAPrinterPass> {
   raw_ostream &OS;
+  bool EnsureOptimizedUses;
 
 public:
-  explicit MemorySSAPrinterPass(raw_ostream &OS) : OS(OS) {}
+  explicit MemorySSAPrinterPass(raw_ostream &OS, bool EnsureOptimizedUses)
+      : OS(OS), EnsureOptimizedUses(EnsureOptimizedUses) {}
 
   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
 };

diff  --git a/llvm/lib/Analysis/MemorySSA.cpp b/llvm/lib/Analysis/MemorySSA.cpp
index 6496bbc3b9f7ab..d1665802826683 100644
--- a/llvm/lib/Analysis/MemorySSA.cpp
+++ b/llvm/lib/Analysis/MemorySSA.cpp
@@ -364,7 +364,8 @@ struct UpwardsMemoryQuery {
 
 } // end anonymous namespace
 
-static bool isUseTriviallyOptimizableToLiveOnEntry(BatchAAResults &AA,
+template <typename AliasAnalysisType>
+static bool isUseTriviallyOptimizableToLiveOnEntry(AliasAnalysisType &AA,
                                                    const Instruction *I) {
   // If the memory can't be changed, then loads of the memory can't be
   // clobbered.
@@ -1361,11 +1362,6 @@ void MemorySSA::OptimizeUses::optimizeUsesInBlock(
     if (MU->isOptimized())
       continue;
 
-    if (isUseTriviallyOptimizableToLiveOnEntry(*AA, MU->getMemoryInst())) {
-      MU->setDefiningAccess(MSSA->getLiveOnEntryDef(), true);
-      continue;
-    }
-
     MemoryLocOrCall UseMLOC(MU);
     auto &LocInfo = LocStackInfo[UseMLOC];
     // If the pop epoch changed, it means we've removed stuff from top of
@@ -1781,10 +1777,15 @@ MemoryUseOrDef *MemorySSA::createNewAccess(Instruction *I,
     return nullptr;
 
   MemoryUseOrDef *MUD;
-  if (Def)
+  if (Def) {
     MUD = new MemoryDef(I->getContext(), nullptr, I, I->getParent(), NextID++);
-  else
+  } else {
     MUD = new MemoryUse(I->getContext(), nullptr, I, I->getParent());
+    if (isUseTriviallyOptimizableToLiveOnEntry(*AAP, I)) {
+      MemoryAccess *LiveOnEntry = getLiveOnEntryDef();
+      MUD->setOptimized(LiveOnEntry);
+    }
+  }
   ValueToMemoryAccess[I] = MUD;
   return MUD;
 }
@@ -2318,7 +2319,8 @@ bool MemorySSAAnalysis::Result::invalidate(
 PreservedAnalyses MemorySSAPrinterPass::run(Function &F,
                                             FunctionAnalysisManager &AM) {
   auto &MSSA = AM.getResult<MemorySSAAnalysis>(F).getMSSA();
-  MSSA.ensureOptimizedUses();
+  if (EnsureOptimizedUses)
+    MSSA.ensureOptimizedUses();
   if (DotCFGMSSA != "") {
     DOTFuncMSSAInfo CFGInfo(F, MSSA);
     WriteGraph(&CFGInfo, "", false, "MSSA", DotCFGMSSA);

diff  --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index d86ec218cd013a..aea38733872c54 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -1066,6 +1066,11 @@ parseFunctionSimplificationPipelineOptions(StringRef Params) {
   return *L;
 }
 
+Expected<bool> parseMemorySSAPrinterPassOptions(StringRef Params) {
+  return parseSinglePassOption(Params, "no-ensure-optimized-uses",
+                               "MemorySSAPrinterPass");
+}
+
 } // namespace
 
 /// Tests whether a pass name starts with a valid prefix for a default pipeline

diff  --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 800feb2e5f1162..5b0e3b8a496ccb 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -379,7 +379,6 @@ FUNCTION_PASS("print<inline-cost>", InlineCostAnnotationPrinterPass(dbgs()))
 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()))
@@ -557,6 +556,13 @@ FUNCTION_PASS_WITH_PARAMS("function-simplification",
                            },
                           parseFunctionSimplificationPipelineOptions,
                           "O1;O2;O3;Os;Oz")
+FUNCTION_PASS_WITH_PARAMS("print<memoryssa>",
+                          "MemorySSAPrinterPass",
+                           [](bool NoEnsureOptimizedUses) {
+                             return MemorySSAPrinterPass(dbgs(), !NoEnsureOptimizedUses);
+                           },
+                          parseMemorySSAPrinterPassOptions,
+                          "no-ensure-optimized-uses")
 #undef FUNCTION_PASS_WITH_PARAMS
 
 #ifndef LOOPNEST_PASS

diff  --git a/llvm/test/Analysis/MemorySSA/tbaa.ll b/llvm/test/Analysis/MemorySSA/tbaa.ll
new file mode 100644
index 00000000000000..e12e4a1930f310
--- /dev/null
+++ b/llvm/test/Analysis/MemorySSA/tbaa.ll
@@ -0,0 +1,13 @@
+; RUN: opt -aa-pipeline=basic-aa,tbaa -passes='print<memoryssa><no-ensure-optimized-uses>' -disable-output < %s 2>&1 | FileCheck %s
+
+define i8 @test1_yes(ptr %a, ptr %b) {
+; CHECK: 1 = MemoryDef(liveOnEntry)
+  store i8 0, ptr %a, align 1
+; CHECK: MemoryUse(liveOnEntry)
+  %y = load i8, ptr %b, align 1, !tbaa !0
+  ret i8 %y
+}
+
+!0 = !{!1, !1, i64 0, i1 true}
+!1 = !{!"qux", !2}
+!2 = !{}


        


More information about the llvm-commits mailing list