[PATCH] D28535: Scale frequencies of a set of blocks

Easwaran Raman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 10 14:20:01 PST 2017


eraman created this revision.
eraman added reviewers: davidxl, chandlerc.
eraman added a subscriber: llvm-commits.

Scaling is done with respect to a reference block whose new frequency is specified.


https://reviews.llvm.org/D28535

Files:
  include/llvm/Analysis/BlockFrequencyInfo.h
  lib/Analysis/BlockFrequencyInfo.cpp
  unittests/Analysis/BlockFrequencyInfoTest.cpp


Index: unittests/Analysis/BlockFrequencyInfoTest.cpp
===================================================================
--- unittests/Analysis/BlockFrequencyInfoTest.cpp
+++ unittests/Analysis/BlockFrequencyInfoTest.cpp
@@ -80,6 +80,14 @@
   EXPECT_EQ(BFI.getBlockProfileCount(BB3).getValue(), UINT64_C(100));
   EXPECT_EQ(BFI.getBlockProfileCount(BB1).getValue(), 100 * BB1Freq / BB0Freq);
   EXPECT_EQ(BFI.getBlockProfileCount(BB2).getValue(), 100 * BB2Freq / BB0Freq);
+
+  // Scale the frequencies of BB0, BB1 and BB2 by a factor of two.
+  SmallPtrSet<BasicBlock *, 4> BlocksToScale({BB1, BB2});
+  BFI.setBlockFreqAndScale(&BB0, BB0Freq * 2, BlocksToScale);
+  EXPECT_EQ(BFI.getBlockFreq(&BB0).getFrequency(), 2 * BB0Freq);
+  EXPECT_EQ(BFI.getBlockFreq(BB1).getFrequency(), 2 * BB1Freq);
+  EXPECT_EQ(BFI.getBlockFreq(BB2).getFrequency(), 2 * BB2Freq);
+  EXPECT_EQ(BFI.getBlockFreq(BB3).getFrequency(), BB3Freq);
 }
 
 } // end anonymous namespace
Index: lib/Analysis/BlockFrequencyInfo.cpp
===================================================================
--- lib/Analysis/BlockFrequencyInfo.cpp
+++ lib/Analysis/BlockFrequencyInfo.cpp
@@ -171,6 +171,26 @@
   BFI->setBlockFreq(BB, Freq);
 }
 
+void BlockFrequencyInfo::setBlockFreqAndScale(
+    const BasicBlock *ReferenceBB, uint64_t Freq,
+    SmallPtrSetImpl<BasicBlock *> &BlocksToScale) {
+  assert(BFI && "Expected analysis to be available");
+  // Use 128 bits APInt to avoid overflow.
+  APInt NewFreq(128, Freq);
+  APInt OldFreq(128, BFI->getBlockFreq(ReferenceBB).getFrequency());
+  for (auto *BB : BlocksToScale) {
+
+    APInt BBFreq(128, BFI->getBlockFreq(BB).getFrequency());
+
+    // Multiply first by NewFreq and then divide by OldFreq
+    // to minimize loss of precision.
+    BBFreq *= NewFreq;
+    BBFreq = BBFreq.udiv(OldFreq);
+    BFI->setBlockFreq(BB, BBFreq.getLimitedValue());
+  }
+  BFI->setBlockFreq(ReferenceBB, Freq);
+}
+
 /// Pop up a ghostview window with the current block frequency propagation
 /// rendered using dot.
 void BlockFrequencyInfo::view() const {
Index: include/llvm/Analysis/BlockFrequencyInfo.h
===================================================================
--- include/llvm/Analysis/BlockFrequencyInfo.h
+++ include/llvm/Analysis/BlockFrequencyInfo.h
@@ -69,6 +69,12 @@
   // Set the frequency of the given basic block.
   void setBlockFreq(const BasicBlock *BB, uint64_t Freq);
 
+  /// Set the frequency of \p ReferenceBB to \p Freq and scale the frequencies
+  /// of the blocks in \p BlocksToScale such that their frequencies relative
+  /// to \p ReferenceBB remain unchanged.
+  void setBlockFreqAndScale(const BasicBlock *ReferenceBB, uint64_t Freq,
+                            SmallPtrSetImpl<BasicBlock *> &BlocksToScale);
+
   /// calculate - compute block frequency info for the given function.
   void calculate(const Function &F, const BranchProbabilityInfo &BPI,
                  const LoopInfo &LI);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28535.83871.patch
Type: text/x-patch
Size: 2943 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170110/42014b73/attachment.bin>


More information about the llvm-commits mailing list