Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UnrollAnalyzerTest] Remove dependency to pass managers #78473

Merged
merged 2 commits into from
Jan 22, 2024

Conversation

bjope
Copy link
Collaborator

@bjope bjope commented Jan 17, 2024

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.

@llvmbot
Copy link
Collaborator

llvmbot commented Jan 17, 2024

@llvm/pr-subscribers-llvm-analysis

Author: Björn Pettersson (bjope)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/78473.diff

1 Files Affected:

  • (modified) llvm/unittests/Analysis/UnrollAnalyzerTest.cpp (+37-65)
diff --git a/llvm/unittests/Analysis/UnrollAnalyzerTest.cpp b/llvm/unittests/Analysis/UnrollAnalyzerTest.cpp
index 2f9135d4fb242d..4791bfc164beee 100644
--- a/llvm/unittests/Analysis/UnrollAnalyzerTest.cpp
+++ b/llvm/unittests/Analysis/UnrollAnalyzerTest.cpp
@@ -6,61 +6,53 @@
 //
 //===----------------------------------------------------------------------===//
 
+#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 +77,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 +137,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 +163,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 +180,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 +205,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 +225,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 +245,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 +269,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 +301,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)

Copy link

github-actions bot commented Jan 17, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

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.
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while you are at it, could you also add SimplifiedValuesVector as an argument to the function instead of using a static global?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fhahn: Certainly! I was a bit annoyed over those as well (having to understand if the subtests could run in parallel etc).

I've added a fixup commit that should be squashed to fix this.

#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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this still needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the llvm namespace scoping in latest version of this pull request.

Thanks for reviewing this!

- Refactor away static global variables.
- Drop llvm namespace.
@bjope bjope merged commit 490a09a into llvm:main Jan 22, 2024
4 checks passed
@bjope bjope deleted the unrollanalyzer branch January 22, 2024 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants