[PATCH] D116612: [LoopFlatten] Move it from LPM2 to LPM1

Sjoerd Meijer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 4 11:29:56 PST 2022


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

In D110057 <https://reviews.llvm.org/D110057> we moved LoopFlatten to a LoopPassManager. This caused a regression for our 64-bit targets (the 32-bit were unaffected), the pass is no longer triggering for a motivating example. Long story short, the IR is just very different than expected; we try to match loop statements and particular uses of induction variables. The easiest is to just move LoopFlatten to a place in the pipeline where the IR is as expected, which is just before IndVarSimplify. This means we move it from LPM2 to LPM1, so that it actually runs just a bit earlier from where it was running before. IndVarSimplify is responsible for significant rewrites that are difficult to "look through" in LoopFlatten.

One thing I had to do, and am unsure about, is the necessary change to add MemorySSAAnalysis as preserved. Without this, I am running in this assertion failure:

  fatal error: error in backend: Loop pass manager using MemorySSA contains a pass that does not preserve MemorySSA

It looks correct to me to add MemorySSA as preserved, but am still investigating this, and thought about sharing this in the mean time.


https://reviews.llvm.org/D116612

Files:
  llvm/lib/Passes/PassBuilderPipelines.cpp
  llvm/lib/Transforms/Scalar/LoopFlatten.cpp


Index: llvm/lib/Transforms/Scalar/LoopFlatten.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/LoopFlatten.cpp
+++ llvm/lib/Transforms/Scalar/LoopFlatten.cpp
@@ -31,6 +31,7 @@
 #include "llvm/ADT/Statistic.h"
 #include "llvm/Analysis/AssumptionCache.h"
 #include "llvm/Analysis/LoopInfo.h"
+#include "llvm/Analysis/MemorySSA.h"
 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
 #include "llvm/Analysis/ScalarEvolution.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
@@ -822,7 +823,9 @@
   if (!Changed)
     return PreservedAnalyses::all();
 
-  return getLoopPassPreservedAnalyses();
+  auto PA = getLoopPassPreservedAnalyses();
+  PA.preserve<MemorySSAAnalysis>();
+  return PA;
 }
 
 namespace {
Index: llvm/lib/Passes/PassBuilderPipelines.cpp
===================================================================
--- llvm/lib/Passes/PassBuilderPipelines.cpp
+++ llvm/lib/Passes/PassBuilderPipelines.cpp
@@ -300,6 +300,8 @@
   // TODO: Investigate promotion cap for O1.
   LPM1.addPass(LICMPass(PTO.LicmMssaOptCap, PTO.LicmMssaNoAccForPromotionCap));
   LPM1.addPass(SimpleLoopUnswitchPass());
+  if (EnableLoopFlatten)
+    LPM1.addPass(LoopFlattenPass());
 
   LPM2.addPass(LoopIdiomRecognizePass());
   LPM2.addPass(IndVarSimplifyPass());
@@ -311,8 +313,6 @@
 
   if (EnableLoopInterchange)
     LPM2.addPass(LoopInterchangePass());
-  if (EnableLoopFlatten)
-    LPM2.addPass(LoopFlattenPass());
 
   // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
   // because it changes IR to makes profile annotation in back compile
@@ -475,6 +475,9 @@
   LPM1.addPass(
       SimpleLoopUnswitchPass(/* NonTrivial */ Level == OptimizationLevel::O3 &&
                              EnableO3NonTrivialUnswitching));
+  if (EnableLoopFlatten)
+    LPM1.addPass(LoopFlattenPass());
+
   LPM2.addPass(LoopIdiomRecognizePass());
   LPM2.addPass(IndVarSimplifyPass());
 
@@ -485,8 +488,6 @@
 
   if (EnableLoopInterchange)
     LPM2.addPass(LoopInterchangePass());
-  if (EnableLoopFlatten)
-    LPM2.addPass(LoopFlattenPass());
 
   // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
   // because it changes IR to makes profile annotation in back compile
@@ -1628,10 +1629,10 @@
     MainFPM.addPass(ConstraintEliminationPass());
 
   LoopPassManager LPM;
-  LPM.addPass(IndVarSimplifyPass());
-  LPM.addPass(LoopDeletionPass());
   if (EnableLoopFlatten && Level.getSpeedupLevel() > 1)
     LPM.addPass(LoopFlattenPass());
+  LPM.addPass(IndVarSimplifyPass());
+  LPM.addPass(LoopDeletionPass());
   // FIXME: Add loop interchange.
 
   // Unroll small loops and perform peeling.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116612.397354.patch
Type: text/x-patch
Size: 2670 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220104/a257d2ae/attachment.bin>


More information about the llvm-commits mailing list