[llvm] [UnrollAnalyzerTest] Remove dependency to pass managers (PR #78473)

Björn Pettersson via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 18 04:39:47 PST 2024


https://github.com/bjope updated https://github.com/llvm/llvm-project/pull/78473

>From 61557719fa0ff66f5f0512be1e1edf934f9fc6dd Mon Sep 17 00:00:00 2001
From: Bjorn Pettersson <bjorn.a.pettersson at ericsson.com>
Date: Wed, 17 Jan 2024 17:50:26 +0100
Subject: [PATCH 1/2] [UnrollAnalyzerTest] Remove dependency to Pass Managers

Remove use of LegacyPassManager in the UnrollAnalyzerTest unit test.

Given that the goal isn't to test pass manager interfaces, and
since the LoopUnrollAnalyzer isn't even implemented as a pass, we do
not really need the complexity of using a pass manager. Instead we
just make sure that we run LoopUnrollAnalyzer and other needed
analyses standalone (without any pass manager). This was inspired
by the LoopInfoTest unit test.
---
 .../unittests/Analysis/UnrollAnalyzerTest.cpp | 101 +++++++-----------
 1 file changed, 36 insertions(+), 65 deletions(-)

diff --git a/llvm/unittests/Analysis/UnrollAnalyzerTest.cpp b/llvm/unittests/Analysis/UnrollAnalyzerTest.cpp
index 2f9135d4fb242d..f6d1c13b3c30c4 100644
--- a/llvm/unittests/Analysis/UnrollAnalyzerTest.cpp
+++ b/llvm/unittests/Analysis/UnrollAnalyzerTest.cpp
@@ -6,61 +6,52 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/Analysis/AssumptionCache.h"
 #include "llvm/Analysis/LoopInfo.h"
 #include "llvm/Analysis/LoopUnrollAnalyzer.h"
+#include "llvm/Analysis/ScalarEvolution.h"
+#include "llvm/Analysis/TargetLibraryInfo.h"
 #include "llvm/AsmParser/Parser.h"
 #include "llvm/IR/Dominators.h"
-#include "llvm/IR/LegacyPassManager.h"
-#include "llvm/InitializePasses.h"
 #include "llvm/Support/SourceMgr.h"
 #include "gtest/gtest.h"
 
 using namespace llvm;
 namespace llvm {
-void initializeUnrollAnalyzerTestPass(PassRegistry &);
 
 static SmallVector<DenseMap<Value *, Value *>, 16> SimplifiedValuesVector;
 static unsigned TripCount = 0;
 
-namespace {
-struct UnrollAnalyzerTest : public FunctionPass {
-  static char ID;
-  bool runOnFunction(Function &F) override {
-    LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
-    ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
+/// Build loop info and scalar evolution for the function and run the analysis.
+static void runUnrollAnalyzer(Module &M, StringRef FuncName) {
+  auto *F = M.getFunction(FuncName);
+  ASSERT_NE(F, nullptr) << "Could not find " << FuncName;
 
-    Function::iterator FI = F.begin();
-    FI++; // First basic block is entry - skip it.
-    BasicBlock *Header = &*FI++;
-    Loop *L = LI->getLoopFor(Header);
-    BasicBlock *Exiting = L->getExitingBlock();
+  TargetLibraryInfoImpl TLII;
+  TargetLibraryInfo TLI(TLII);
+  AssumptionCache AC(*F);
+  DominatorTree DT(*F);
+  LoopInfo LI(DT);
+  ScalarEvolution SE(*F, TLI, AC, DT, LI);
 
-    SimplifiedValuesVector.clear();
-    TripCount = SE->getSmallConstantTripCount(L, Exiting);
-    for (unsigned Iteration = 0; Iteration < TripCount; Iteration++) {
-      DenseMap<Value *, Value *> SimplifiedValues;
-      UnrolledInstAnalyzer Analyzer(Iteration, SimplifiedValues, *SE, L);
-      for (auto *BB : L->getBlocks())
-        for (Instruction &I : *BB)
-          Analyzer.visit(I);
-      SimplifiedValuesVector.push_back(SimplifiedValues);
-    }
-    return false;
-  }
-  void getAnalysisUsage(AnalysisUsage &AU) const override {
-    AU.addRequired<DominatorTreeWrapperPass>();
-    AU.addRequired<LoopInfoWrapperPass>();
-    AU.addRequired<ScalarEvolutionWrapperPass>();
-    AU.setPreservesAll();
-  }
-  UnrollAnalyzerTest() : FunctionPass(ID) {
-    initializeUnrollAnalyzerTestPass(*PassRegistry::getPassRegistry());
+  Function::iterator FI = F->begin();
+  FI++; // First basic block is entry - skip it.
+  BasicBlock *Header = &*FI++;
+  Loop *L = LI.getLoopFor(Header);
+  BasicBlock *Exiting = L->getExitingBlock();
+
+  SimplifiedValuesVector.clear();
+  TripCount = SE.getSmallConstantTripCount(L, Exiting);
+  for (unsigned Iteration = 0; Iteration < TripCount; Iteration++) {
+    DenseMap<Value *, Value *> SimplifiedValues;
+    UnrolledInstAnalyzer Analyzer(Iteration, SimplifiedValues, SE, L);
+    for (auto *BB : L->getBlocks())
+      for (Instruction &I : *BB)
+        Analyzer.visit(I);
+    SimplifiedValuesVector.push_back(SimplifiedValues);
   }
-};
 }
 
-char UnrollAnalyzerTest::ID = 0;
-
 std::unique_ptr<Module> makeLLVMModule(LLVMContext &Context,
                                        const char *ModuleStr) {
   SMDiagnostic Err;
@@ -85,12 +76,9 @@ TEST(UnrollAnalyzerTest, BasicSimplifications) {
       "  %x.lcssa = phi i64 [ %x2, %loop ]\n"
       "  ret i64 %x.lcssa\n"
       "}\n";
-  UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
   LLVMContext Context;
   std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
-  legacy::PassManager Passes;
-  Passes.add(P);
-  Passes.run(*M);
+  runUnrollAnalyzer(*M, "propagate_loop_phis");
 
   // Perform checks
   Module::iterator MI = M->begin();
@@ -148,12 +136,9 @@ TEST(UnrollAnalyzerTest, OuterLoopSimplification) {
       "  ret void\n"
       "}\n";
 
-  UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
   LLVMContext Context;
   std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
-  legacy::PassManager Passes;
-  Passes.add(P);
-  Passes.run(*M);
+  runUnrollAnalyzer(*M, "foo");
 
   Module::iterator MI = M->begin();
   Function *F = &*MI++;
@@ -177,6 +162,7 @@ TEST(UnrollAnalyzerTest, OuterLoopSimplification) {
   auto I2 = SimplifiedValuesVector[0].find(Y2);
   EXPECT_TRUE(I2 == SimplifiedValuesVector[0].end());
 }
+
 TEST(UnrollAnalyzerTest, CmpSimplifications) {
   const char *ModuleStr =
       "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
@@ -193,12 +179,9 @@ TEST(UnrollAnalyzerTest, CmpSimplifications) {
       "for.end:\n"
       "  ret void\n"
       "}\n";
-  UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
   LLVMContext Context;
   std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
-  legacy::PassManager Passes;
-  Passes.add(P);
-  Passes.run(*M);
+  runUnrollAnalyzer(*M, "branch_iv_trunc");
 
   // Perform checks
   Module::iterator MI = M->begin();
@@ -221,6 +204,7 @@ TEST(UnrollAnalyzerTest, CmpSimplifications) {
   EXPECT_TRUE(I2 != SimplifiedValuesVector[5].end());
   EXPECT_EQ(cast<ConstantInt>((*I2).second)->getZExtValue(), 1U);
 }
+
 TEST(UnrollAnalyzerTest, PtrCmpSimplifications) {
   const char *ModuleStr =
       "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
@@ -240,12 +224,9 @@ TEST(UnrollAnalyzerTest, PtrCmpSimplifications) {
       "loop.exit:\n"
       "  ret void\n"
       "}\n";
-  UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
   LLVMContext Context;
   std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
-  legacy::PassManager Passes;
-  Passes.add(P);
-  Passes.run(*M);
+  runUnrollAnalyzer(*M, "ptr_cmp");
 
   // Perform checks
   Module::iterator MI = M->begin();
@@ -263,6 +244,7 @@ TEST(UnrollAnalyzerTest, PtrCmpSimplifications) {
   EXPECT_TRUE(I1 != SimplifiedValuesVector[5].end());
   EXPECT_EQ(cast<ConstantInt>((*I1).second)->getZExtValue(), 0U);
 }
+
 TEST(UnrollAnalyzerTest, CastSimplifications) {
   const char *ModuleStr =
       "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
@@ -286,12 +268,9 @@ TEST(UnrollAnalyzerTest, CastSimplifications) {
       "  ret void\n"
       "}\n";
 
-  UnrollAnalyzerTest *P = new UnrollAnalyzerTest();
   LLVMContext Context;
   std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
-  legacy::PassManager Passes;
-  Passes.add(P);
-  Passes.run(*M);
+  runUnrollAnalyzer(*M, "const_load_cast");
 
   // Perform checks
   Module::iterator MI = M->begin();
@@ -321,11 +300,3 @@ TEST(UnrollAnalyzerTest, CastSimplifications) {
 }
 
 } // end namespace llvm
-
-INITIALIZE_PASS_BEGIN(UnrollAnalyzerTest, "unrollanalyzertestpass",
-                      "unrollanalyzertestpass", false, false)
-INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
-INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
-INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
-INITIALIZE_PASS_END(UnrollAnalyzerTest, "unrollanalyzertestpass",
-                    "unrollanalyzertestpass", false, false)

>From 662a955f04d5212a4c89835a02f8bac83323d694 Mon Sep 17 00:00:00 2001
From: Bjorn Pettersson <bjorn.a.pettersson at ericsson.com>
Date: Thu, 18 Jan 2024 13:39:18 +0100
Subject: [PATCH 2/2] Fixup review comments:

- Refactor away static global variables.
- Drop llvm namespace.
---
 .../unittests/Analysis/UnrollAnalyzerTest.cpp | 28 +++++++++++--------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/llvm/unittests/Analysis/UnrollAnalyzerTest.cpp b/llvm/unittests/Analysis/UnrollAnalyzerTest.cpp
index f6d1c13b3c30c4..721d67f22f2f27 100644
--- a/llvm/unittests/Analysis/UnrollAnalyzerTest.cpp
+++ b/llvm/unittests/Analysis/UnrollAnalyzerTest.cpp
@@ -17,13 +17,13 @@
 #include "gtest/gtest.h"
 
 using namespace llvm;
-namespace llvm {
 
-static SmallVector<DenseMap<Value *, Value *>, 16> SimplifiedValuesVector;
-static unsigned TripCount = 0;
+typedef SmallVector<DenseMap<Value *, Value *>, 16> SimplifiedValuesVectorTy;
 
 /// Build loop info and scalar evolution for the function and run the analysis.
-static void runUnrollAnalyzer(Module &M, StringRef FuncName) {
+static void
+runUnrollAnalyzer(Module &M, StringRef FuncName,
+                  SimplifiedValuesVectorTy &SimplifiedValuesVector) {
   auto *F = M.getFunction(FuncName);
   ASSERT_NE(F, nullptr) << "Could not find " << FuncName;
 
@@ -41,7 +41,7 @@ static void runUnrollAnalyzer(Module &M, StringRef FuncName) {
   BasicBlock *Exiting = L->getExitingBlock();
 
   SimplifiedValuesVector.clear();
-  TripCount = SE.getSmallConstantTripCount(L, Exiting);
+  unsigned TripCount = SE.getSmallConstantTripCount(L, Exiting);
   for (unsigned Iteration = 0; Iteration < TripCount; Iteration++) {
     DenseMap<Value *, Value *> SimplifiedValues;
     UnrolledInstAnalyzer Analyzer(Iteration, SimplifiedValues, SE, L);
@@ -78,7 +78,9 @@ TEST(UnrollAnalyzerTest, BasicSimplifications) {
       "}\n";
   LLVMContext Context;
   std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
-  runUnrollAnalyzer(*M, "propagate_loop_phis");
+  SimplifiedValuesVectorTy SimplifiedValuesVector;
+  runUnrollAnalyzer(*M, "propagate_loop_phis", SimplifiedValuesVector);
+  unsigned TripCount = SimplifiedValuesVector.size();
 
   // Perform checks
   Module::iterator MI = M->begin();
@@ -138,7 +140,8 @@ TEST(UnrollAnalyzerTest, OuterLoopSimplification) {
 
   LLVMContext Context;
   std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
-  runUnrollAnalyzer(*M, "foo");
+  SimplifiedValuesVectorTy SimplifiedValuesVector;
+  runUnrollAnalyzer(*M, "foo", SimplifiedValuesVector);
 
   Module::iterator MI = M->begin();
   Function *F = &*MI++;
@@ -181,7 +184,8 @@ TEST(UnrollAnalyzerTest, CmpSimplifications) {
       "}\n";
   LLVMContext Context;
   std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
-  runUnrollAnalyzer(*M, "branch_iv_trunc");
+  SimplifiedValuesVectorTy SimplifiedValuesVector;
+  runUnrollAnalyzer(*M, "branch_iv_trunc", SimplifiedValuesVector);
 
   // Perform checks
   Module::iterator MI = M->begin();
@@ -226,7 +230,8 @@ TEST(UnrollAnalyzerTest, PtrCmpSimplifications) {
       "}\n";
   LLVMContext Context;
   std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
-  runUnrollAnalyzer(*M, "ptr_cmp");
+  SimplifiedValuesVectorTy SimplifiedValuesVector;
+  runUnrollAnalyzer(*M, "ptr_cmp", SimplifiedValuesVector);
 
   // Perform checks
   Module::iterator MI = M->begin();
@@ -270,7 +275,8 @@ TEST(UnrollAnalyzerTest, CastSimplifications) {
 
   LLVMContext Context;
   std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
-  runUnrollAnalyzer(*M, "const_load_cast");
+  SimplifiedValuesVectorTy SimplifiedValuesVector;
+  runUnrollAnalyzer(*M, "const_load_cast", SimplifiedValuesVector);
 
   // Perform checks
   Module::iterator MI = M->begin();
@@ -298,5 +304,3 @@ TEST(UnrollAnalyzerTest, CastSimplifications) {
   EXPECT_TRUE(I3 != SimplifiedValuesVector[5].end());
   EXPECT_EQ(cast<ConstantInt>((*I3).second)->getZExtValue(), 3U);
 }
-
-} // end namespace llvm



More information about the llvm-commits mailing list