[llvm] r295072 - Add new pass LazyMachineBlockFrequencyInfo

Adam Nemet via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 14 09:21:09 PST 2017


Author: anemet
Date: Tue Feb 14 11:21:09 2017
New Revision: 295072

URL: http://llvm.org/viewvc/llvm-project?rev=295072&view=rev
Log:
Add new pass LazyMachineBlockFrequencyInfo

And use it in MachineOptimizationRemarkEmitter.  A test will follow on top of
Justin's changes to enable MachineORE in AsmPrinter.

The approach is similar to the IR-level pass.  It's a bit simpler because BPI
is immutable at the Machine level so we don't need to make that lazy.

Because of this, a new function mapping is introduced (BPIPassTrait::getBPI).
This function extracts BPI from the pass.  In case of the lazy pass, this is
when the calculation of the BFI occurs.  For Machine-level, this is the
identity function.

Differential Revision: https://reviews.llvm.org/D29836

Added:
    llvm/trunk/include/llvm/CodeGen/LazyMachineBlockFrequencyInfo.h
    llvm/trunk/lib/CodeGen/LazyMachineBlockFrequencyInfo.cpp
Modified:
    llvm/trunk/include/llvm/Analysis/LazyBlockFrequencyInfo.h
    llvm/trunk/include/llvm/Analysis/LazyBranchProbabilityInfo.h
    llvm/trunk/include/llvm/CodeGen/MachineBlockFrequencyInfo.h
    llvm/trunk/include/llvm/InitializePasses.h
    llvm/trunk/lib/CodeGen/CMakeLists.txt
    llvm/trunk/lib/CodeGen/MachineBlockFrequencyInfo.cpp
    llvm/trunk/lib/CodeGen/MachineOptimizationRemarkEmitter.cpp

Modified: llvm/trunk/include/llvm/Analysis/LazyBlockFrequencyInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/LazyBlockFrequencyInfo.h?rev=295072&r1=295071&r2=295072&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/LazyBlockFrequencyInfo.h (original)
+++ llvm/trunk/include/llvm/Analysis/LazyBlockFrequencyInfo.h Tue Feb 14 11:21:09 2017
@@ -50,7 +50,8 @@ public:
   BlockFrequencyInfoT &getCalculated() {
     if (!Calculated) {
       assert(F && BPIPass && LI && "call setAnalysis");
-      BFI.calculate(*F, BPIPass->getBPI(), *LI);
+      BFI.calculate(
+          *F, BPIPassTrait<BranchProbabilityInfoPassT>::getBPI(BPIPass), *LI);
       Calculated = true;
     }
     return BFI;

Modified: llvm/trunk/include/llvm/Analysis/LazyBranchProbabilityInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/LazyBranchProbabilityInfo.h?rev=295072&r1=295071&r2=295072&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/LazyBranchProbabilityInfo.h (original)
+++ llvm/trunk/include/llvm/Analysis/LazyBranchProbabilityInfo.h Tue Feb 14 11:21:09 2017
@@ -105,5 +105,17 @@ public:
 
 /// \brief Helper for client passes to initialize dependent passes for LBPI.
 void initializeLazyBPIPassPass(PassRegistry &Registry);
+
+/// \brief Simple trait class that provides a mapping between BPI passes and the
+/// corresponding BPInfo.
+template <typename PassT> struct BPIPassTrait {
+  static PassT &getBPI(PassT *P) { return *P; }
+};
+
+template <> struct BPIPassTrait<LazyBranchProbabilityInfoPass> {
+  static BranchProbabilityInfo &getBPI(LazyBranchProbabilityInfoPass *P) {
+    return P->getBPI();
+  }
+};
 }
 #endif

Added: llvm/trunk/include/llvm/CodeGen/LazyMachineBlockFrequencyInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/LazyMachineBlockFrequencyInfo.h?rev=295072&view=auto
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/LazyMachineBlockFrequencyInfo.h (added)
+++ llvm/trunk/include/llvm/CodeGen/LazyMachineBlockFrequencyInfo.h Tue Feb 14 11:21:09 2017
@@ -0,0 +1,83 @@
+///===- LazyMachineBlockFrequencyInfo.h - Lazy Block Frequency -*- C++ -*--===//
+///
+///                     The LLVM Compiler Infrastructure
+///
+/// This file is distributed under the University of Illinois Open Source
+/// License. See LICENSE.TXT for details.
+///
+///===---------------------------------------------------------------------===//
+/// \file
+/// This is an alternative analysis pass to MachineBlockFrequencyInfo.  The
+/// difference is that with this pass the block frequencies are not computed
+/// when the analysis pass is executed but rather when the BFI result is
+/// explicitly requested by the analysis client.
+///
+///===---------------------------------------------------------------------===//
+
+#ifndef LLVM_ANALYSIS_LAZYMACHINEBLOCKFREQUENCYINFO_H
+#define LLVM_ANALYSIS_LAZYMACHINEBLOCKFREQUENCYINFO_H
+
+#include "llvm/Analysis/LazyBlockFrequencyInfo.h"
+#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
+#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
+#include "llvm/CodeGen/MachineLoopInfo.h"
+
+namespace llvm {
+/// \brief This is an alternative analysis pass to MachineBlockFrequencyInfo.
+/// The difference is that with this pass, the block frequencies are not
+/// computed when the analysis pass is executed but rather when the BFI result
+/// is explicitly requested by the analysis client.
+///
+/// There are some additional requirements for any client pass that wants to use
+/// the analysis:
+///
+/// 1. The pass needs to initialize dependent passes with:
+///
+///   INITIALIZE_PASS_DEPENDENCY(LazyMachineBFIPass)
+///
+/// 2. Similarly, getAnalysisUsage should call:
+///
+///   LazyMachineBlockFrequencyInfoPass::getLazyMachineBFIAnalysisUsage(AU)
+///
+/// 3. The computed MachineBFI should be requested with
+///    getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() before
+///    MachineLoopInfo could be invalidated for example by changing the CFG.
+///
+/// Note that it is expected that we wouldn't need this functionality for the
+/// new PM since with the new PM, analyses are executed on demand.
+
+class LazyMachineBlockFrequencyInfoPass : public MachineFunctionPass {
+private:
+  /// \brief Machine BPI is an immutable pass, no need to use it lazily.
+  LazyBlockFrequencyInfo<MachineFunction, MachineBranchProbabilityInfo,
+                         MachineLoopInfo, MachineBlockFrequencyInfo>
+      LMBFI;
+
+public:
+  static char ID;
+
+  LazyMachineBlockFrequencyInfoPass();
+
+  /// \brief Compute and return the block frequencies.
+  MachineBlockFrequencyInfo &getBFI() { return LMBFI.getCalculated(); }
+
+  /// \brief Compute and return the block frequencies.
+  const MachineBlockFrequencyInfo &getBFI() const {
+    return LMBFI.getCalculated();
+  }
+
+  void getAnalysisUsage(AnalysisUsage &AU) const override;
+
+  /// Helper for client passes to set up the analysis usage on behalf of this
+  /// pass.
+  static void getLazyMachineBFIAnalysisUsage(AnalysisUsage &AU);
+
+  bool runOnMachineFunction(MachineFunction &F) override;
+  void releaseMemory() override;
+  void print(raw_ostream &OS, const Module *M) const override;
+};
+
+/// \brief Helper for client passes to initialize dependent passes for LMBFI.
+void initializeLazyMachineBFIPassPass(PassRegistry &Registry);
+}
+#endif

Modified: llvm/trunk/include/llvm/CodeGen/MachineBlockFrequencyInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/MachineBlockFrequencyInfo.h?rev=295072&r1=295071&r2=295072&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/MachineBlockFrequencyInfo.h (original)
+++ llvm/trunk/include/llvm/CodeGen/MachineBlockFrequencyInfo.h Tue Feb 14 11:21:09 2017
@@ -23,6 +23,7 @@ namespace llvm {
 
 class MachineBasicBlock;
 class MachineBranchProbabilityInfo;
+class MachineLoopInfo;
 template <class BlockT> class BlockFrequencyInfoImpl;
 
 /// MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation
@@ -42,6 +43,11 @@ public:
 
   bool runOnMachineFunction(MachineFunction &F) override;
 
+  /// calculate - compute block frequency info for the given function.
+  void calculate(const MachineFunction &F,
+                 const MachineBranchProbabilityInfo &MBPI,
+                 const MachineLoopInfo &MLI);
+
   void releaseMemory() override;
 
   /// getblockFreq - Return block frequency. Return 0 if we don't have the

Modified: llvm/trunk/include/llvm/InitializePasses.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/InitializePasses.h?rev=295072&r1=295071&r2=295072&view=diff
==============================================================================
--- llvm/trunk/include/llvm/InitializePasses.h (original)
+++ llvm/trunk/include/llvm/InitializePasses.h Tue Feb 14 11:21:09 2017
@@ -175,6 +175,7 @@ void initializeLCSSAVerificationPassPass
 void initializeLegacyLICMPassPass(PassRegistry&);
 void initializeLegacyLoopSinkPassPass(PassRegistry&);
 void initializeLazyBranchProbabilityInfoPassPass(PassRegistry&);
+void initializeLazyMachineBlockFrequencyInfoPassPass(PassRegistry&);
 void initializeLazyBlockFrequencyInfoPassPass(PassRegistry&);
 void initializeLazyValueInfoWrapperPassPass(PassRegistry&);
 void initializeLegalizerPass(PassRegistry&);

Modified: llvm/trunk/lib/CodeGen/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/CMakeLists.txt?rev=295072&r1=295071&r2=295072&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/CMakeLists.txt (original)
+++ llvm/trunk/lib/CodeGen/CMakeLists.txt Tue Feb 14 11:21:09 2017
@@ -37,6 +37,7 @@ add_llvm_library(LLVMCodeGen
   InterleavedAccessPass.cpp
   IntrinsicLowering.cpp
   LatencyPriorityQueue.cpp
+  LazyMachineBlockFrequencyInfo.cpp
   LexicalScopes.cpp
   LiveDebugValues.cpp
   LiveDebugVariables.cpp

Added: llvm/trunk/lib/CodeGen/LazyMachineBlockFrequencyInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/LazyMachineBlockFrequencyInfo.cpp?rev=295072&view=auto
==============================================================================
--- llvm/trunk/lib/CodeGen/LazyMachineBlockFrequencyInfo.cpp (added)
+++ llvm/trunk/lib/CodeGen/LazyMachineBlockFrequencyInfo.cpp Tue Feb 14 11:21:09 2017
@@ -0,0 +1,74 @@
+///===- LazyMachineBlockFrequencyInfo.cpp - Lazy Machine Block Frequency --===//
+///
+///                     The LLVM Compiler Infrastructure
+///
+/// This file is distributed under the University of Illinois Open Source
+/// License. See LICENSE.TXT for details.
+///
+///===---------------------------------------------------------------------===//
+/// \file
+/// This is an alternative analysis pass to MachineBlockFrequencyInfo.  The
+/// difference is that with this pass the block frequencies are not computed
+/// when the analysis pass is executed but rather when the BFI result is
+/// explicitly requested by the analysis client.
+///
+///===---------------------------------------------------------------------===//
+
+#include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
+
+using namespace llvm;
+
+#define DEBUG_TYPE "lazy-machine-block-freq"
+
+INITIALIZE_PASS_BEGIN(LazyMachineBlockFrequencyInfoPass, DEBUG_TYPE,
+                      "Lazy Machine Block Frequency Analysis", true, true)
+INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
+INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
+INITIALIZE_PASS_END(LazyMachineBlockFrequencyInfoPass, DEBUG_TYPE,
+                    "Lazy Machine Block Frequency Analysis", true, true)
+
+char LazyMachineBlockFrequencyInfoPass::ID = 0;
+
+LazyMachineBlockFrequencyInfoPass::LazyMachineBlockFrequencyInfoPass()
+    : MachineFunctionPass(ID) {
+  initializeLazyMachineBlockFrequencyInfoPassPass(
+      *PassRegistry::getPassRegistry());
+}
+
+void LazyMachineBlockFrequencyInfoPass::print(raw_ostream &OS,
+                                              const Module *M) const {
+  LMBFI.getCalculated().print(OS, M);
+}
+
+void LazyMachineBlockFrequencyInfoPass::getAnalysisUsage(
+    AnalysisUsage &AU) const {
+  AU.addRequired<MachineBranchProbabilityInfo>();
+  AU.addRequired<MachineLoopInfo>();
+  AU.setPreservesAll();
+  MachineFunctionPass::getAnalysisUsage(AU);
+}
+
+void LazyMachineBlockFrequencyInfoPass::releaseMemory() {
+  LMBFI.releaseMemory();
+}
+
+bool LazyMachineBlockFrequencyInfoPass::runOnMachineFunction(
+    MachineFunction &MF) {
+  auto &BPIPass = getAnalysis<MachineBranchProbabilityInfo>();
+  auto &LI = getAnalysis<MachineLoopInfo>();
+  LMBFI.setAnalysis(&MF, &BPIPass, &LI);
+  return false;
+}
+
+void LazyMachineBlockFrequencyInfoPass::getLazyMachineBFIAnalysisUsage(
+    AnalysisUsage &AU) {
+  AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
+  AU.addRequired<MachineBranchProbabilityInfo>();
+  AU.addRequired<MachineLoopInfo>();
+}
+
+void llvm::initializeLazyMachineBFIPassPass(PassRegistry &Registry) {
+  INITIALIZE_PASS_DEPENDENCY(LazyMachineBlockFrequencyInfoPass);
+  INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo);
+  INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo);
+}

Modified: llvm/trunk/lib/CodeGen/MachineBlockFrequencyInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineBlockFrequencyInfo.cpp?rev=295072&r1=295071&r2=295072&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MachineBlockFrequencyInfo.cpp (original)
+++ llvm/trunk/lib/CodeGen/MachineBlockFrequencyInfo.cpp Tue Feb 14 11:21:09 2017
@@ -172,10 +172,9 @@ void MachineBlockFrequencyInfo::getAnaly
   MachineFunctionPass::getAnalysisUsage(AU);
 }
 
-bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
-  MachineBranchProbabilityInfo &MBPI =
-      getAnalysis<MachineBranchProbabilityInfo>();
-  MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
+void MachineBlockFrequencyInfo::calculate(
+    const MachineFunction &F, const MachineBranchProbabilityInfo &MBPI,
+    const MachineLoopInfo &MLI) {
   if (!MBFI)
     MBFI.reset(new ImplType);
   MBFI->calculate(F, MBPI, MLI);
@@ -184,6 +183,13 @@ bool MachineBlockFrequencyInfo::runOnMac
        F.getName().equals(ViewBlockFreqFuncName))) {
     view();
   }
+}
+
+bool MachineBlockFrequencyInfo::runOnMachineFunction(MachineFunction &F) {
+  MachineBranchProbabilityInfo &MBPI =
+      getAnalysis<MachineBranchProbabilityInfo>();
+  MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
+  calculate(F, MBPI, MLI);
   return false;
 }
 

Modified: llvm/trunk/lib/CodeGen/MachineOptimizationRemarkEmitter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineOptimizationRemarkEmitter.cpp?rev=295072&r1=295071&r2=295072&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MachineOptimizationRemarkEmitter.cpp (original)
+++ llvm/trunk/lib/CodeGen/MachineOptimizationRemarkEmitter.cpp Tue Feb 14 11:21:09 2017
@@ -14,7 +14,7 @@
 ///===---------------------------------------------------------------------===//
 
 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
-#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
+#include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
 #include "llvm/IR/DebugInfo.h"
 #include "llvm/IR/DiagnosticInfo.h"
 #include "llvm/IR/LLVMContext.h"
@@ -64,7 +64,7 @@ bool MachineOptimizationRemarkEmitterPas
   MachineBlockFrequencyInfo *MBFI;
 
   if (MF.getFunction()->getContext().getDiagnosticHotnessRequested())
-    MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
+    MBFI = &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI();
   else
     MBFI = nullptr;
 
@@ -74,7 +74,7 @@ bool MachineOptimizationRemarkEmitterPas
 
 void MachineOptimizationRemarkEmitterPass::getAnalysisUsage(
     AnalysisUsage &AU) const {
-  AU.addRequired<MachineBlockFrequencyInfo>();
+  LazyMachineBlockFrequencyInfoPass::getLazyMachineBFIAnalysisUsage(AU);
   AU.setPreservesAll();
   MachineFunctionPass::getAnalysisUsage(AU);
 }
@@ -85,6 +85,6 @@ static const char ore_name[] = "Machine
 
 INITIALIZE_PASS_BEGIN(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name,
                       false, true)
-INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfo)
+INITIALIZE_PASS_DEPENDENCY(LazyMachineBFIPass)
 INITIALIZE_PASS_END(MachineOptimizationRemarkEmitterPass, ORE_NAME, ore_name,
                     false, true)




More information about the llvm-commits mailing list