[llvm] r244239 - Revert r244154 which causes some build failure. See https://llvm.org/bugs/show_bug.cgi?id=24377.

Cong Hou via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 6 11:17:30 PDT 2015


Author: conghou
Date: Thu Aug  6 13:17:29 2015
New Revision: 244239

URL: http://llvm.org/viewvc/llvm-project?rev=244239&view=rev
Log:
Revert r244154 which causes some build failure. See https://llvm.org/bugs/show_bug.cgi?id=24377.


Modified:
    llvm/trunk/include/llvm/CodeGen/MachineBasicBlock.h
    llvm/trunk/include/llvm/CodeGen/MachineBranchProbabilityInfo.h
    llvm/trunk/lib/CodeGen/IfConversion.cpp
    llvm/trunk/lib/CodeGen/MachineBasicBlock.cpp
    llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp
    llvm/trunk/lib/CodeGen/MachineBranchProbabilityInfo.cpp

Modified: llvm/trunk/include/llvm/CodeGen/MachineBasicBlock.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/MachineBasicBlock.h?rev=244239&r1=244238&r2=244239&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/MachineBasicBlock.h (original)
+++ llvm/trunk/include/llvm/CodeGen/MachineBasicBlock.h Thu Aug  6 13:17:29 2015
@@ -65,10 +65,6 @@ class MachineBasicBlock : public ilist_n
   Instructions Insts;
   const BasicBlock *BB;
   int Number;
-
-  /// A flag tracking whether the weights of all successors are normalized.
-  bool AreSuccWeightsNormalized;
-
   MachineFunction *xParent;
 
   /// Keep track of the predecessor / successor basic blocks.
@@ -133,9 +129,6 @@ public:
   const MachineFunction *getParent() const { return xParent; }
   MachineFunction *getParent() { return xParent; }
 
-  /// Return whether all weights of successors are normalized.
-  bool areSuccWeightsNormalized() const { return AreSuccWeightsNormalized; }
-
   /// MachineBasicBlock iterator that automatically skips over MIs that are
   /// inside bundles (i.e. walk top level MIs only).
   template<typename Ty, typename IterTy>
@@ -391,12 +384,6 @@ public:
   /// Set successor weight of a given iterator.
   void setSuccWeight(succ_iterator I, uint32_t weight);
 
-  /// Normalize all succesor weights so that the sum of them does not exceed
-  /// UINT32_MAX. Return true if the weights are modified and false otherwise.
-  /// Note that weights that are modified after calling this function are not
-  /// guaranteed to be normalized.
-  bool normalizeSuccWeights();
-
   /// Remove successor from the successors list of this MachineBasicBlock. The
   /// Predecessors list of succ is automatically updated.
   void removeSuccessor(MachineBasicBlock *succ);

Modified: llvm/trunk/include/llvm/CodeGen/MachineBranchProbabilityInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/MachineBranchProbabilityInfo.h?rev=244239&r1=244238&r2=244239&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/MachineBranchProbabilityInfo.h (original)
+++ llvm/trunk/include/llvm/CodeGen/MachineBranchProbabilityInfo.h Thu Aug  6 13:17:29 2015
@@ -60,10 +60,6 @@ public:
   // adjustment. Any edge weights used with the sum should be divided by Scale.
   uint32_t getSumForBlock(const MachineBasicBlock *MBB, uint32_t &Scale) const;
 
-  // Get sum of the block successors' weights, and force normalizing the
-  // successors' weights of MBB so that their sum fit within 32-bits.
-  uint32_t getSumForBlock(MachineBasicBlock *MBB) const;
-
   // A 'Hot' edge is an edge which probability is >= 80%.
   bool isEdgeHot(const MachineBasicBlock *Src,
                  const MachineBasicBlock *Dst) const;
@@ -87,34 +83,8 @@ public:
   raw_ostream &printEdgeProbability(raw_ostream &OS,
                                     const MachineBasicBlock *Src,
                                     const MachineBasicBlock *Dst) const;
-
-  // Normalize a list of weights by scaling them down so that the sum of them
-  // doesn't exceed UINT32_MAX. Return the scale.
-  template <class WeightList>
-  static uint32_t normalizeEdgeWeights(WeightList &Weights);
 };
 
-template <class WeightList>
-uint32_t
-MachineBranchProbabilityInfo::normalizeEdgeWeights(WeightList &Weights) {
-  assert(Weights.size() < UINT32_MAX && "Too many weights in the list!");
-  // First we compute the sum with 64-bits of precision.
-  uint64_t Sum = std::accumulate(Weights.begin(), Weights.end(), uint64_t(0));
-
-  // If the computed sum fits in 32-bits, we're done.
-  if (Sum <= UINT32_MAX)
-    return 1;
-
-  // Otherwise, compute the scale necessary to cause the weights to fit, and
-  // re-sum with that scale applied.
-  assert((Sum / UINT32_MAX) < UINT32_MAX &&
-         "The sum of weights exceeds UINT32_MAX^2!");
-  uint32_t Scale = (Sum / UINT32_MAX) + 1;
-  for (auto &W : Weights)
-    W /= Scale;
-  return Scale;
-}
-
 }
 
 

Modified: llvm/trunk/lib/CodeGen/IfConversion.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/IfConversion.cpp?rev=244239&r1=244238&r2=244239&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/IfConversion.cpp (original)
+++ llvm/trunk/lib/CodeGen/IfConversion.cpp Thu Aug  6 13:17:29 2015
@@ -1232,17 +1232,15 @@ bool IfConverter::IfConvertTriangle(BBIn
 
   bool HasEarlyExit = CvtBBI->FalseBB != nullptr;
   uint64_t CvtNext = 0, CvtFalse = 0, BBNext = 0, BBCvt = 0, SumWeight = 0;
+  uint32_t WeightScale = 0;
 
   if (HasEarlyExit) {
     // Get weights before modifying CvtBBI->BB and BBI.BB.
-    // Explictly normalize the weights of all edges from CvtBBI->BB so that we
-    // are aware that the edge weights obtained below are normalized.
-    CvtBBI->BB->normalizeSuccWeights();
     CvtNext = MBPI->getEdgeWeight(CvtBBI->BB, NextBBI->BB);
     CvtFalse = MBPI->getEdgeWeight(CvtBBI->BB, CvtBBI->FalseBB);
     BBNext = MBPI->getEdgeWeight(BBI.BB, NextBBI->BB);
     BBCvt = MBPI->getEdgeWeight(BBI.BB, CvtBBI->BB);
-    SumWeight = MBPI->getSumForBlock(CvtBBI->BB);
+    SumWeight = MBPI->getSumForBlock(CvtBBI->BB, WeightScale);
   }
 
   if (CvtBBI->BB->pred_size() > 1) {
@@ -1279,8 +1277,8 @@ bool IfConverter::IfConvertTriangle(BBIn
     // New_Weight(BBI.BB, CvtBBI->FalseBB) =
     //   Weight(BBI.BB, CvtBBI->BB) * Weight(CvtBBI->BB, CvtBBI->FalseBB)
 
-    uint64_t NewNext = BBNext * SumWeight + BBCvt * CvtNext;
-    uint64_t NewFalse = BBCvt * CvtFalse;
+    uint64_t NewNext = BBNext * SumWeight + (BBCvt * CvtNext) / WeightScale;
+    uint64_t NewFalse = (BBCvt * CvtFalse) / WeightScale;
     // We need to scale down all weights of BBI.BB to fit uint32_t.
     // Here BBI.BB is connected to CvtBBI->FalseBB and will fall through to
     // the next block.

Modified: llvm/trunk/lib/CodeGen/MachineBasicBlock.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineBasicBlock.cpp?rev=244239&r1=244238&r2=244239&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MachineBasicBlock.cpp (original)
+++ llvm/trunk/lib/CodeGen/MachineBasicBlock.cpp Thu Aug  6 13:17:29 2015
@@ -16,7 +16,6 @@
 #include "llvm/ADT/SmallString.h"
 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
 #include "llvm/CodeGen/LiveVariables.h"
-#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
 #include "llvm/CodeGen/MachineDominators.h"
 #include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/MachineInstrBuilder.h"
@@ -40,9 +39,8 @@ using namespace llvm;
 #define DEBUG_TYPE "codegen"
 
 MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb)
-    : BB(bb), Number(-1), AreSuccWeightsNormalized(false), xParent(&mf),
-      Alignment(0), IsLandingPad(false), AddressTaken(false),
-      CachedMCSymbol(nullptr) {
+  : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false),
+    AddressTaken(false), CachedMCSymbol(nullptr) {
   Insts.Parent = this;
 }
 
@@ -483,10 +481,8 @@ void MachineBasicBlock::addSuccessor(Mac
   if (weight != 0 && Weights.empty())
     Weights.resize(Successors.size());
 
-  if (weight != 0 || !Weights.empty()) {
+  if (weight != 0 || !Weights.empty())
     Weights.push_back(weight);
-    AreSuccWeightsNormalized = false;
-  }
 
    Successors.push_back(succ);
    succ->addPredecessor(this);
@@ -1100,25 +1096,7 @@ uint32_t MachineBasicBlock::getSuccWeigh
 void MachineBasicBlock::setSuccWeight(succ_iterator I, uint32_t weight) {
   if (Weights.empty())
     return;
-  auto WeightIter = getWeightIterator(I);
-  uint32_t OldWeight = *WeightIter;
-  *WeightIter = weight;
-  if (weight > OldWeight)
-    AreSuccWeightsNormalized = false;
-}
-
-/// Normalize all succesor weights so that the sum of them does not exceed
-/// UINT32_MAX. Return true if the weights are modified and false otherwise.
-/// Note that weights that are modified after calling this function are not
-/// guaranteed to be normalized.
-bool MachineBasicBlock::normalizeSuccWeights() {
-  if (!AreSuccWeightsNormalized) {
-    uint32_t Scale =
-        MachineBranchProbabilityInfo::normalizeEdgeWeights(Weights);
-    AreSuccWeightsNormalized = true;
-    return Scale != 1;
-  }
-  return false;
+  *getWeightIterator(I) = weight;
 }
 
 /// getWeightIterator - Return wight iterator corresonding to the I successor

Modified: llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp?rev=244239&r1=244238&r2=244239&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp (original)
+++ llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp Thu Aug  6 13:17:29 2015
@@ -361,7 +361,8 @@ MachineBlockPlacement::selectBestSuccess
   // improve the MBPI interface to efficiently support query patterns such as
   // this.
   uint32_t BestWeight = 0;
-  uint32_t SumWeight = MBPI->getSumForBlock(BB);
+  uint32_t WeightScale = 0;
+  uint32_t SumWeight = MBPI->getSumForBlock(BB, WeightScale);
   DEBUG(dbgs() << "Attempting merge from: " << getBlockName(BB) << "\n");
   for (MachineBasicBlock *Succ : BB->successors()) {
     if (BlockFilter && !BlockFilter->count(Succ))
@@ -377,7 +378,7 @@ MachineBlockPlacement::selectBestSuccess
     }
 
     uint32_t SuccWeight = MBPI->getEdgeWeight(BB, Succ);
-    BranchProbability SuccProb(SuccWeight, SumWeight);
+    BranchProbability SuccProb(SuccWeight / WeightScale, SumWeight);
 
     // If we outline optional branches, look whether Succ is unavoidable, i.e.
     // dominates all terminators of the MachineFunction. If it does, other
@@ -674,7 +675,8 @@ MachineBlockPlacement::findBestLoopExit(
     // FIXME: Due to the performance of the probability and weight routines in
     // the MBPI analysis, we use the internal weights and manually compute the
     // probabilities to avoid quadratic behavior.
-    uint32_t SumWeight = MBPI->getSumForBlock(MBB);
+    uint32_t WeightScale = 0;
+    uint32_t SumWeight = MBPI->getSumForBlock(MBB, WeightScale);
     for (MachineBasicBlock *Succ : MBB->successors()) {
       if (Succ->isLandingPad())
         continue;
@@ -703,7 +705,7 @@ MachineBlockPlacement::findBestLoopExit(
           BlocksExitingToOuterLoop.insert(MBB);
       }
 
-      BranchProbability SuccProb(SuccWeight, SumWeight);
+      BranchProbability SuccProb(SuccWeight / WeightScale, SumWeight);
       BlockFrequency ExitEdgeFreq = MBFI->getBlockFreq(MBB) * SuccProb;
       DEBUG(dbgs() << "    exiting: " << getBlockName(MBB) << " -> "
                    << getBlockName(Succ) << " [L:" << SuccLoopDepth << "] (";

Modified: llvm/trunk/lib/CodeGen/MachineBranchProbabilityInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineBranchProbabilityInfo.cpp?rev=244239&r1=244238&r2=244239&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MachineBranchProbabilityInfo.cpp (original)
+++ llvm/trunk/lib/CodeGen/MachineBranchProbabilityInfo.cpp Thu Aug  6 13:17:29 2015
@@ -28,35 +28,36 @@ char MachineBranchProbabilityInfo::ID =
 
 void MachineBranchProbabilityInfo::anchor() { }
 
-uint32_t
-MachineBranchProbabilityInfo::getSumForBlock(MachineBasicBlock *MBB) const {
-  // Normalize the weights of MBB's all successors so that the sum is guaranteed
-  // to be no greater than UINT32_MAX.
-  MBB->normalizeSuccWeights();
-
-  SmallVector<uint32_t, 8> Weights;
+uint32_t MachineBranchProbabilityInfo::
+getSumForBlock(const MachineBasicBlock *MBB, uint32_t &Scale) const {
+  // First we compute the sum with 64-bits of precision, ensuring that cannot
+  // overflow by bounding the number of weights considered. Hopefully no one
+  // actually needs 2^32 successors.
+  assert(MBB->succ_size() < UINT32_MAX);
+  uint64_t Sum = 0;
+  Scale = 1;
   for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
-                                              E = MBB->succ_end();
-       I != E; ++I)
-    Weights.push_back(getEdgeWeight(MBB, I));
-
-  return std::accumulate(Weights.begin(), Weights.end(), 0u);
-}
-
-uint32_t
-MachineBranchProbabilityInfo::getSumForBlock(const MachineBasicBlock *MBB,
-                                             uint32_t &Scale) const {
-  SmallVector<uint32_t, 8> Weights;
+       E = MBB->succ_end(); I != E; ++I) {
+    uint32_t Weight = getEdgeWeight(MBB, I);
+    Sum += Weight;
+  }
+
+  // If the computed sum fits in 32-bits, we're done.
+  if (Sum <= UINT32_MAX)
+    return Sum;
+
+  // Otherwise, compute the scale necessary to cause the weights to fit, and
+  // re-sum with that scale applied.
+  assert((Sum / UINT32_MAX) < UINT32_MAX);
+  Scale = (Sum / UINT32_MAX) + 1;
+  Sum = 0;
   for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
-                                              E = MBB->succ_end();
-       I != E; ++I)
-    Weights.push_back(getEdgeWeight(MBB, I));
-
-  if (MBB->areSuccWeightsNormalized())
-    Scale = 1;
-  else
-    Scale = MachineBranchProbabilityInfo::normalizeEdgeWeights(Weights);
-  return std::accumulate(Weights.begin(), Weights.end(), 0u);
+       E = MBB->succ_end(); I != E; ++I) {
+    uint32_t Weight = getEdgeWeight(MBB, I);
+    Sum += Weight / Scale;
+  }
+  assert(Sum <= UINT32_MAX);
+  return Sum;
 }
 
 uint32_t MachineBranchProbabilityInfo::




More information about the llvm-commits mailing list