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

Port CodeGenPrepare to new pass manager #68530

Closed

Conversation

Krishna-13-cyber
Copy link
Contributor

No description provided.

@github-actions
Copy link

github-actions bot commented Oct 8, 2023

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff 32f719776552542691f14e786400fc222b7416f1 990cd21f5f268ca45e95a07c703e2658c70aa02f -- llvm/include/llvm/CodeGen/CodeGenPrepare.h llvm/include/llvm/InitializePasses.h llvm/lib/CodeGen/CodeGen.cpp llvm/lib/CodeGen/CodeGenPrepare.cpp llvm/lib/Passes/PassBuilder.cpp llvm/tools/opt/opt.cpp
View the diff from clang-format here.
diff --git a/llvm/include/llvm/CodeGen/CodeGenPrepare.h b/llvm/include/llvm/CodeGen/CodeGenPrepare.h
index 66a76257423a..fd2a09ff739f 100644
--- a/llvm/include/llvm/CodeGen/CodeGenPrepare.h
+++ b/llvm/include/llvm/CodeGen/CodeGenPrepare.h
@@ -1,4 +1,5 @@
-//===- CodeGenPrepare.h ------------------------------------------*- C++ -*-===//
+//===- CodeGenPrepare.h ------------------------------------------*- C++
+//-*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -26,7 +27,7 @@ private:
   const TargetMachine *TM;
 
 public:
-  CodeGenPreparePass(const TargetMachine *TM): TM(TM) { }
+  CodeGenPreparePass(const TargetMachine *TM) : TM(TM) {}
   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
 };
 
diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h
index 2b368b3f2552..f48e6c975f7d 100644
--- a/llvm/include/llvm/InitializePasses.h
+++ b/llvm/include/llvm/InitializePasses.h
@@ -81,7 +81,7 @@ void initializeCallGraphPrinterLegacyPassPass(PassRegistry&);
 void initializeCallGraphViewerPass(PassRegistry&);
 void initializeCallGraphWrapperPassPass(PassRegistry&);
 void initializeCheckDebugMachineModulePass(PassRegistry &);
-void initializeCodeGenPrepareLegacyPass(PassRegistry&);
+void initializeCodeGenPrepareLegacyPass(PassRegistry &);
 void initializeComplexDeinterleavingLegacyPassPass(PassRegistry&);
 void initializeConstantHoistingLegacyPassPass(PassRegistry&);
 void initializeCostModelAnalysisPass(PassRegistry&);
diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 63afcf74ad2d..03fae7e90ee3 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -12,6 +12,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/CodeGen/CodeGenPrepare.h"
 #include "llvm/ADT/APInt.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/DenseMap.h"
@@ -34,7 +35,6 @@
 #include "llvm/CodeGen/BasicBlockSectionsProfileReader.h"
 #include "llvm/CodeGen/ISDOpcodes.h"
 #include "llvm/CodeGen/MachineValueType.h"
-#include "llvm/CodeGen/CodeGenPrepare.h"
 #include "llvm/CodeGen/SelectionDAGNodes.h"
 #include "llvm/CodeGen/TargetLowering.h"
 #include "llvm/CodeGen/TargetPassConfig.h"
@@ -273,7 +273,7 @@ static cl::opt<unsigned>
 static cl::opt<bool>
     DisableDeletePHIs("disable-cgp-delete-phis", cl::Hidden, cl::init(false),
                       cl::desc("Disable elimination of dead PHI nodes."));
-namespace llvm{
+namespace llvm {
 
 enum ExtType {
   ZeroExtension, // Zero extension has been seen.
@@ -302,7 +302,7 @@ using ValueToSExts = MapVector<Value *, SExts>;
 class TypePromotionTransaction;
 
 class CodeGenPrepare {
-  public:
+public:
   const TargetMachine *TM = nullptr;
   const TargetSubtargetInfo *SubtargetInfo = nullptr;
   const TargetLowering *TLI = nullptr;
@@ -365,7 +365,7 @@ class CodeGenPrepare {
   /// lazily and update it when required.
   std::unique_ptr<DominatorTree> DT;
 
-  public:
+public:
   /// If encounter huge function, we need to limit the build time.
   bool IsHugeFunc = false;
 
@@ -441,10 +441,9 @@ class CodeGenPrepare {
     BPI.reset();
     BFI.reset();
   }
-  bool run(Function &F, const TargetMachine *TM,
-                              const TargetTransformInfo &TTI,
-                              LoopInfo &LI, ProfileSummaryInfo &PSI, 
-                              const TargetLibraryInfo &TLInfo);
+  bool run(Function &F, const TargetMachine *TM, const TargetTransformInfo &TTI,
+           LoopInfo &LI, ProfileSummaryInfo &PSI,
+           const TargetLibraryInfo &TLInfo);
 
 private:
   template <typename F>
@@ -473,39 +472,39 @@ private:
   }
 };
 
- class CodeGenPrepareLegacy : public FunctionPass {
- public:
-    static char ID; // Pass identification, replacement for typeid
-    bool runOnFunction(Function &F) override;
-    const TargetMachine *TM = nullptr;
-    const TargetSubtargetInfo *SubtargetInfo = nullptr;
-    const TargetLowering *TLI = nullptr;
-    const TargetRegisterInfo *TRI = nullptr;
-    const TargetTransformInfo *TTI = nullptr;
-    const BasicBlockSectionsProfileReader *BBSectionsProfileReader = nullptr;
-    const TargetLibraryInfo *TLInfo = nullptr;
-    LoopInfo *LI = nullptr;
-    std::unique_ptr<BlockFrequencyInfo> BFI;
-    std::unique_ptr<BranchProbabilityInfo> BPI;
-    ProfileSummaryInfo *PSI = nullptr;
-    StringRef getPassName() const { return "CodeGen Prepare"; }
-
-    void getAnalysisUsage(AnalysisUsage &AU) const {
-      // FIXME: When we can selectively preserve passes, preserve the domtree.
-      AU.addRequired<ProfileSummaryInfoWrapperPass>();
-      AU.addRequired<TargetLibraryInfoWrapperPass>();
-      AU.addRequired<TargetPassConfig>();
-      AU.addRequired<TargetTransformInfoWrapperPass>();
-      AU.addRequired<LoopInfoWrapperPass>();
-      AU.addUsedIfAvailable<BasicBlockSectionsProfileReader>();
-    }
-
-    CodeGenPrepareLegacy() : FunctionPass(ID) {
-      initializeCodeGenPrepareLegacyPass(*PassRegistry::getPassRegistry());
-    }
+class CodeGenPrepareLegacy : public FunctionPass {
+public:
+  static char ID; // Pass identification, replacement for typeid
+  bool runOnFunction(Function &F) override;
+  const TargetMachine *TM = nullptr;
+  const TargetSubtargetInfo *SubtargetInfo = nullptr;
+  const TargetLowering *TLI = nullptr;
+  const TargetRegisterInfo *TRI = nullptr;
+  const TargetTransformInfo *TTI = nullptr;
+  const BasicBlockSectionsProfileReader *BBSectionsProfileReader = nullptr;
+  const TargetLibraryInfo *TLInfo = nullptr;
+  LoopInfo *LI = nullptr;
+  std::unique_ptr<BlockFrequencyInfo> BFI;
+  std::unique_ptr<BranchProbabilityInfo> BPI;
+  ProfileSummaryInfo *PSI = nullptr;
+  StringRef getPassName() const { return "CodeGen Prepare"; }
+
+  void getAnalysisUsage(AnalysisUsage &AU) const {
+    // FIXME: When we can selectively preserve passes, preserve the domtree.
+    AU.addRequired<ProfileSummaryInfoWrapperPass>();
+    AU.addRequired<TargetLibraryInfoWrapperPass>();
+    AU.addRequired<TargetPassConfig>();
+    AU.addRequired<TargetTransformInfoWrapperPass>();
+    AU.addRequired<LoopInfoWrapperPass>();
+    AU.addUsedIfAvailable<BasicBlockSectionsProfileReader>();
+  }
+
+  CodeGenPrepareLegacy() : FunctionPass(ID) {
+    initializeCodeGenPrepareLegacyPass(*PassRegistry::getPassRegistry());
+  }
 };
 
-} // end anonymous namespace
+} // namespace llvm
 
 char CodeGenPrepareLegacy::ID = 0;
 
@@ -517,11 +516,11 @@ INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
 INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
-INITIALIZE_PASS_END(CodeGenPrepareLegacy, DEBUG_TYPE, "Optimize for code generation",
-                    false, false)
+INITIALIZE_PASS_END(CodeGenPrepareLegacy, DEBUG_TYPE,
+                    "Optimize for code generation", false, false)
 
 FunctionPass *llvm::createCodeGenPreparePass() {
- return new CodeGenPrepareLegacy();
+  return new CodeGenPrepareLegacy();
 }
 
 bool CodeGenPrepareLegacy::runOnFunction(Function &F) {
@@ -543,27 +542,29 @@ bool CodeGenPrepareLegacy::runOnFunction(Function &F) {
   return CGP.run(F, TM, TTI, LI, PSI, TLInfo);
 }
 
-PreservedAnalyses CodeGenPreparePass::run(Function &F, FunctionAnalysisManager &AM) {
- auto &TTI = AM.getResult<TargetIRAnalysis>(F);
- auto &LI = AM.getResult<LoopAnalysis>(F);
- auto &PSI = AM.getResult<ProfileSummaryAnalysis>(F);
- auto &TLInfo = AM.getResult<TargetLibraryAnalysis>(F);
-// auto &BasicBlockSectionsProfileReader = AM.getResult<BasicBlockSectionsProfileReader>(F);
-
- CodeGenPrepare CGP;
- bool Changed = CGP.run(F, TM, TTI, LI, PSI, TLInfo);
- if (!Changed)
-   return PreservedAnalyses::all();
- PreservedAnalyses PA;
- PA.preserveSet<CFGAnalyses>();
- PA.preserve<LoopAnalysis>();
- return PA;
+PreservedAnalyses CodeGenPreparePass::run(Function &F,
+                                          FunctionAnalysisManager &AM) {
+  auto &TTI = AM.getResult<TargetIRAnalysis>(F);
+  auto &LI = AM.getResult<LoopAnalysis>(F);
+  auto &PSI = AM.getResult<ProfileSummaryAnalysis>(F);
+  auto &TLInfo = AM.getResult<TargetLibraryAnalysis>(F);
+  // auto &BasicBlockSectionsProfileReader =
+  // AM.getResult<BasicBlockSectionsProfileReader>(F);
+
+  CodeGenPrepare CGP;
+  bool Changed = CGP.run(F, TM, TTI, LI, PSI, TLInfo);
+  if (!Changed)
+    return PreservedAnalyses::all();
+  PreservedAnalyses PA;
+  PA.preserveSet<CFGAnalyses>();
+  PA.preserve<LoopAnalysis>();
+  return PA;
 }
 
 bool CodeGenPrepare::run(Function &F, const TargetMachine *TM,
-                            const TargetTransformInfo &TTI,
-                            LoopInfo &LI, ProfileSummaryInfo &PSI,
-                            const TargetLibraryInfo &TLInfo) {
+                         const TargetTransformInfo &TTI, LoopInfo &LI,
+                         ProfileSummaryInfo &PSI,
+                         const TargetLibraryInfo &TLInfo) {
   if (DisableBranchOpts)
     return false;
 
@@ -589,8 +590,7 @@ bool CodeGenPrepare::run(Function &F, const TargetMachine *TM,
   if (BBSectionsGuidedSectionPrefix && BBSectionsProfileReader &&
       BBSectionsProfileReader->isFunctionHot(F.getName())) {
     F.setSectionPrefix("hot");
-  } 
-  else if (ProfileGuidedSectionPrefix) {
+  } else if (ProfileGuidedSectionPrefix) {
     // The hot attribute overwrites profile count based hotness while profile
     // counts based hotness overwrite the cold attribute.
     // This is a conservative behabvior.
@@ -2837,9 +2837,9 @@ LLVM_DUMP_METHOD void ExtAddrMode::dump() const {
 }
 #endif
 
-} // end anonymous namespace
+} // namespace llvm
 
-namespace llvm{
+namespace llvm {
 
 /// This class provides transaction based operation on the IR.
 /// Every change made through this class is recorded in the internal state and
@@ -3265,7 +3265,7 @@ private:
   SetOfInstrs &RemovedInsts;
 };
 
-} // end anonymous namespace
+} // namespace llvm
 
 void TypePromotionTransaction::setOperand(Instruction *Inst, unsigned Idx,
                                           Value *NewVal) {
@@ -4092,7 +4092,7 @@ private:
     }
   }
 };
-} // end anonymous namespace
+} // namespace llvm
 
 /// Try adding ScaleReg*Scale to the current addressing mode.
 /// Return true and update AddrMode if this addr mode is legal for the target,
@@ -4268,7 +4268,7 @@ static bool isPromotedInstructionLegal(const TargetLowering &TLI,
       ISDOpcode, TLI.getValueType(DL, PromotedInst->getType()));
 }
 
-namespace llvm{
+namespace llvm {
 
 /// Hepler class to perform type promotion.
 class TypePromotionHelper {
@@ -4400,7 +4400,7 @@ public:
                           const InstrToOrigTy &PromotedInsts);
 };
 
-} // end anonymous namespace
+} // namespace llvm
 
 bool TypePromotionHelper::canGetThrough(const Instruction *Inst,
                                         Type *ConsideredExtType,
@@ -7428,7 +7428,7 @@ bool CodeGenPrepare::optimizeSwitchInst(SwitchInst *SI) {
   return Changed;
 }
 
-namespace llvm{
+namespace llvm {
 
 /// Helper class to promote a scalar operation to a vector one.
 /// This class is used to move downward extractelement transition.
@@ -7707,7 +7707,7 @@ public:
   }
 };
 
-} // end anonymous namespace
+} // namespace llvm
 
 void VectorPromoteHelper::promoteImpl(Instruction *ToBePromoted) {
   // At this point, we know that all the operands of ToBePromoted but Def
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 101ddee2b4ae..006fdd1ae9ac 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -234,8 +234,8 @@
 #include "llvm/Transforms/Utils/CanonicalizeAliases.h"
 #include "llvm/Transforms/Utils/CanonicalizeFreezeInLoops.h"
 #include "llvm/Transforms/Utils/CountVisits.h"
-#include "llvm/Transforms/Utils/Debugify.h"
 #include "llvm/Transforms/Utils/DXILUpgrade.h"
+#include "llvm/Transforms/Utils/Debugify.h"
 #include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
 #include "llvm/Transforms/Utils/FixIrreducible.h"
 #include "llvm/Transforms/Utils/HelloWorld.h"

// auto &BBSectionsProfileReader =
// getAnalysisIfAvailable<BasicBlockSectionsProfileReader>();

CodeGenPrepare CGP;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd expect this to be constructed with the TargetMachine, not have a TM argument

auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
auto &PSI = getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
auto &TLInfo = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
// auto &BBSectionsProfileReader =
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't comment this out?

if (skipFunction(F))
return false;
auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you still need this if the TM is passed to the constructor function?

auto &LI = AM.getResult<LoopAnalysis>(F);
auto &PSI = AM.getResult<ProfileSummaryAnalysis>(F);
auto &TLInfo = AM.getResult<TargetLibraryAnalysis>(F);
// auto &BasicBlockSectionsProfileReader = AM.getResult<BasicBlockSectionsProfileReader>(F);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is another analysis pass that needs to be ported as a dependency


DL = &F.getParent()->getDataLayout();

bool EverMadeChange = false;

TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
// TM = &getAnalysisUsage<TargetPassConfig>().getTM<TargetMachine>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Drop this, it comes from the pass constructor

BBSectionsProfileReader =
getAnalysisIfAvailable<BasicBlockSectionsProfileReader>();
// TLInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
// TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
Copy link
Contributor

Choose a reason for hiding this comment

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

Get TTI with &AM.getResult(F)

PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
BBSectionsProfileReader =
getAnalysisIfAvailable<BasicBlockSectionsProfileReader>();
// TLInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
Copy link
Contributor

Choose a reason for hiding this comment

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

AM.getResult(F)

getAnalysisIfAvailable<BasicBlockSectionsProfileReader>();
// TLInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
// TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
// LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Copy link
Contributor

Choose a reason for hiding this comment

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

AM.getResult(F)

// LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
BPI.reset(new BranchProbabilityInfo(F, LI));
BFI.reset(new BlockFrequencyInfo(F, *BPI, LI));
// PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at ConstantHoisting as an example, I think you need something like:

  auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
  auto *PSI = MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent());

BFI.reset(new BlockFrequencyInfo(F, *BPI, LI));
// PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
// BBSectionsProfileReader =
// getAnalysisIfAvailable<BasicBlockSectionsProfileReader>();
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs to be ported for access through the analysis manager

@Krishna-13-cyber
Copy link
Contributor Author

Hi @nickleus27 ,
Thanks for carrying the work as I couldn't continue due to hardware constraints on my side.

Closing this PR as done in #75380

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.

3 participants