[PATCH] D84357: [llvm][NFC] Add comments and common-case API to MachineBlockFrequencyInfo

Mircea Trofin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 22 13:00:45 PDT 2020


mtrofin created this revision.
mtrofin added reviewers: wmi, yamauchi.
Herald added subscribers: llvm-commits, hiraditya, qcolombet, MatzeB.
Herald added a project: LLVM.

Clarify the relation between a block's BlockFrequency and the
getEntryFreq() API, and added an API for the relatively common case of
finding a block's frequency relative to the entrypoint.

Added / moved some comments to header.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D84357

Files:
  llvm/include/llvm/CodeGen/MachineBlockFrequencyInfo.h
  llvm/lib/CodeGen/LiveIntervals.cpp
  llvm/lib/CodeGen/MachineBlockFrequencyInfo.cpp
  llvm/lib/CodeGen/RegAllocPBQP.cpp


Index: llvm/lib/CodeGen/RegAllocPBQP.cpp
===================================================================
--- llvm/lib/CodeGen/RegAllocPBQP.cpp
+++ llvm/lib/CodeGen/RegAllocPBQP.cpp
@@ -451,8 +451,7 @@
         unsigned DstReg = CP.getDstReg();
         unsigned SrcReg = CP.getSrcReg();
 
-        const float Scale = 1.0f / MBFI.getEntryFreq();
-        PBQP::PBQPNum CBenefit = MBFI.getBlockFreq(&MBB).getFrequency() * Scale;
+        PBQP::PBQPNum CBenefit = MBFI.getBlockFreqRelativeToEntrypoint(&MBB);
 
         if (CP.isPhys()) {
           if (!MF.getRegInfo().isAllocatable(DstReg))
Index: llvm/lib/CodeGen/MachineBlockFrequencyInfo.cpp
===================================================================
--- llvm/lib/CodeGen/MachineBlockFrequencyInfo.cpp
+++ llvm/lib/CodeGen/MachineBlockFrequencyInfo.cpp
@@ -241,8 +241,8 @@
   return MBFI ? MBFI->getProfileCountFromFreq(F, Freq) : None;
 }
 
-bool
-MachineBlockFrequencyInfo::isIrrLoopHeader(const MachineBasicBlock *MBB) {
+bool MachineBlockFrequencyInfo::isIrrLoopHeader(
+    const MachineBasicBlock *MBB) const {
   assert(MBFI && "Expected analysis to be available");
   return MBFI->isIrrLoopHeader(MBB);
 }
Index: llvm/lib/CodeGen/LiveIntervals.cpp
===================================================================
--- llvm/lib/CodeGen/LiveIntervals.cpp
+++ llvm/lib/CodeGen/LiveIntervals.cpp
@@ -868,9 +868,7 @@
 float LiveIntervals::getSpillWeight(bool isDef, bool isUse,
                                     const MachineBlockFrequencyInfo *MBFI,
                                     const MachineBasicBlock *MBB) {
-  BlockFrequency Freq = MBFI->getBlockFreq(MBB);
-  const float Scale = 1.0f / MBFI->getEntryFreq();
-  return (isDef + isUse) * (Freq.getFrequency() * Scale);
+  return (isDef + isUse) * MBFI->getBlockFreqRelativeToEntrypoint(MBB);
 }
 
 LiveRange::Segment
Index: llvm/include/llvm/CodeGen/MachineBlockFrequencyInfo.h
===================================================================
--- llvm/include/llvm/CodeGen/MachineBlockFrequencyInfo.h
+++ llvm/include/llvm/CodeGen/MachineBlockFrequencyInfo.h
@@ -58,18 +58,29 @@
   /// information. Please note that initial frequency is equal to 1024. It means
   /// that we should not rely on the value itself, but only on the comparison to
   /// the other block frequencies. We do this to avoid using of floating points.
-  ///
+  /// For example, to get the frequency of a block relative to the entrypoint,
+  /// divide the integral value returned by this function (the
+  /// BlockFrequency::getFrequency() value) by getEntryFreq().
   BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
 
+  /// Compute the frequency of the block, relative to the entrypoint.
+  /// This API assumes getEntryFreq() is non-zero.
+  double getBlockFreqRelativeToEntrypoint(const MachineBasicBlock *MBB) const {
+    return getBlockFreq(MBB).getFrequency() * 1.0 / getEntryFreq();
+  }
+
   Optional<uint64_t> getBlockProfileCount(const MachineBasicBlock *MBB) const;
   Optional<uint64_t> getProfileCountFromFreq(uint64_t Freq) const;
 
-  bool isIrrLoopHeader(const MachineBasicBlock *MBB);
+  bool isIrrLoopHeader(const MachineBasicBlock *MBB) const;
 
   void setBlockFreq(const MachineBasicBlock *MBB, uint64_t Freq);
 
   const MachineFunction *getFunction() const;
   const MachineBranchProbabilityInfo *getMBPI() const;
+
+  /// Pop up a ghostview window with the current block frequency propagation
+  /// rendered using dot.
   void view(const Twine &Name, bool isSimple = true) const;
 
   // Print the block frequency Freq to OS using the current functions entry
@@ -81,6 +92,8 @@
   raw_ostream &printBlockFreq(raw_ostream &OS,
                               const MachineBasicBlock *MBB) const;
 
+  /// Divide a block's BlockFrequency::getFrequency() value by this value to
+  /// obtain the entrypoint-relative frequency of said block.
   uint64_t getEntryFreq() const;
 };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84357.279923.patch
Type: text/x-patch
Size: 3934 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200722/b1a49ac5/attachment.bin>


More information about the llvm-commits mailing list