[PATCH] D107513: [MemCpyOpt/MemorySSA] Do not run the pass for prohibitively large number of memory accesses.

Alina Sbirlea via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 4 16:45:22 PDT 2021


asbirlea created this revision.
asbirlea added reviewers: aeubanks, nikic, george.burgess.iv.
Herald added a subscriber: hiraditya.
asbirlea requested review of this revision.
Herald added a project: LLVM.

Provide a knob in MemorySSA for passes to query if they're dealing with a pathological testcase: very large number of memory accesses.
While MemorySSA queries are bounded, updates to the analysis are not - they are necessary for correctness. Inserting a new memory access will trigger the renaming of all accesses affected, so an update is liniar in the number of accesses.
The utility introduced here uses the counter used to assign unique IDs in MemorySSA, which is an approximation of how many accesses have been created, not necesarily how many accesses are currently in the function.
The current knob is set to 100000, so an approximation should be enough.

Usecase: triggered in MemCpyOpt on a function with a single BB and 100k+ memoryaccesses.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D107513

Files:
  llvm/include/llvm/Analysis/MemorySSA.h
  llvm/lib/Analysis/MemorySSA.cpp
  llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp


Index: llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
+++ llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
@@ -1760,6 +1760,10 @@
   if (!TLI->has(LibFunc_memset) || !TLI->has(LibFunc_memcpy))
     return false;
 
+  // If the number of memory accesses is prohibitively large, skip the pass.
+  if (MSSA && MSSA->prohibitivelyLargeNumberOfAccesses())
+    return false;
+
   while (true) {
     if (!iterateOnFunction(F))
       break;
Index: llvm/lib/Analysis/MemorySSA.cpp
===================================================================
--- llvm/lib/Analysis/MemorySSA.cpp
+++ llvm/lib/Analysis/MemorySSA.cpp
@@ -95,6 +95,12 @@
     "enable-mssa-loop-dependency", cl::Hidden, cl::init(true),
     cl::desc("Enable MemorySSA dependency for loop pass manager"));
 
+static cl::opt<unsigned> ProhibitivelyLargeAccessNo(
+    "memssa-prohibitively-large-access-no", cl::Hidden, cl::init(100000),
+    cl::desc("The number of accesses seen in a function by MemorySSA, past"
+             "which optimizations should consider updates to MemorySSA to be"
+             "prohibitively expensive (default = 100000)"));
+
 static cl::opt<bool, true>
     VerifyMemorySSAX("verify-memoryssa", cl::location(VerifyMemorySSA),
                      cl::Hidden, cl::desc("Enable verification of MemorySSA."));
@@ -1864,6 +1870,10 @@
 LLVM_DUMP_METHOD void MemorySSA::dump() const { print(dbgs()); }
 #endif
 
+bool MemorySSA::prohibitivelyLargeNumberOfAccesses() const {
+  return NextID > ProhibitivelyLargeAccessNo;
+}
+
 void MemorySSA::verifyMemorySSA() const {
   verifyOrderingDominationAndDefUses(F);
   verifyDominationNumbers(F);
Index: llvm/include/llvm/Analysis/MemorySSA.h
===================================================================
--- llvm/include/llvm/Analysis/MemorySSA.h
+++ llvm/include/llvm/Analysis/MemorySSA.h
@@ -735,6 +735,15 @@
   void dump() const;
   void print(raw_ostream &) const;
 
+  // Return true if the *approximate* number of accesses seen by MemorySSA for
+  // the current function is prohibitively large.
+  //
+  // This information can be used to limit optimizations that need to update
+  // MemorySSA for IRs with pathological patterns.
+  // FIXME: A better informative number would be the number of Defs, but this
+  // is currently only targetting extreme cases. Revisit if the usecases change.
+  bool prohibitivelyLargeNumberOfAccesses() const;
+
   /// Return true if \p MA represents the live on entry value
   ///
   /// Loads and stores from pointer arguments and other global values may be


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D107513.364290.patch
Type: text/x-patch
Size: 2658 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210804/0f261131/attachment.bin>


More information about the llvm-commits mailing list