<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">This revision broke the build here:<div class=""><a href="http://lab.llvm.org:8080/green/job/clang-stage1-cmake-RA-incremental_build/12982/console" class="">http://lab.llvm.org:8080/green/job/clang-stage1-cmake-RA-incremental_build/12982/console</a></div><div class=""><br class=""></div><div class="">BTW, it looks like the Phab review was never approved.</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Aug 5, 2015, at 3:01 PM, Cong Hou <<a href="mailto:congh@google.com" class="">congh@google.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Author: conghou<br class="">Date: Wed Aug 5 17:01:20 2015<br class="">New Revision: 244154<br class=""><br class="">URL: <a href="http://llvm.org/viewvc/llvm-project?rev=244154&view=rev" class="">http://llvm.org/viewvc/llvm-project?rev=244154&view=rev</a><br class="">Log:<br class="">Record whether the weights on out-edges from a MBB are normalized.<br class=""><br class="">1. Create a utility function normalizeEdgeWeights() in MachineBranchProbabilityInfo that normalizes a list of edge weights so that the sum of then can fit in uint32_t.<br class="">2. Provide an interface in MachineBasicBlock to normalize its successors' weights.<br class="">3. Add a flag in MachineBasicBlock that tracks whether its successors' weights are normalized.<br class="">4. Provide an overload of getSumForBlock that accepts a non-const pointer to a MBB so that it can force normalizing this MBB's successors' weights.<br class="">5. Update several uses of getSumForBlock() by eliminating the once needed weight scale.<br class=""><br class="">Differential Revision: <a href="http://reviews.llvm.org/D11442" class="">http://reviews.llvm.org/D11442</a><br class=""><br class=""><br class="">Modified:<br class=""> llvm/trunk/include/llvm/CodeGen/MachineBasicBlock.h<br class=""> llvm/trunk/include/llvm/CodeGen/MachineBranchProbabilityInfo.h<br class=""> llvm/trunk/lib/CodeGen/IfConversion.cpp<br class=""> llvm/trunk/lib/CodeGen/MachineBasicBlock.cpp<br class=""> llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp<br class=""> llvm/trunk/lib/CodeGen/MachineBranchProbabilityInfo.cpp<br class=""><br class="">Modified: llvm/trunk/include/llvm/CodeGen/MachineBasicBlock.h<br class="">URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/MachineBasicBlock.h?rev=244154&r1=244153&r2=244154&view=diff" class="">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/MachineBasicBlock.h?rev=244154&r1=244153&r2=244154&view=diff</a><br class="">==============================================================================<br class="">--- llvm/trunk/include/llvm/CodeGen/MachineBasicBlock.h (original)<br class="">+++ llvm/trunk/include/llvm/CodeGen/MachineBasicBlock.h Wed Aug 5 17:01:20 2015<br class="">@@ -65,6 +65,10 @@ class MachineBasicBlock : public ilist_n<br class=""> Instructions Insts;<br class=""> const BasicBlock *BB;<br class=""> int Number;<br class="">+<br class="">+ /// A flag tracking whether the weights of all successors are normalized.<br class="">+ bool AreSuccWeightsNormalized;<br class="">+<br class=""> MachineFunction *xParent;<br class=""><br class=""> /// Keep track of the predecessor / successor basicblocks.<br class="">@@ -129,6 +133,9 @@ public:<br class=""> const MachineFunction *getParent() const { return xParent; }<br class=""> MachineFunction *getParent() { return xParent; }<br class=""><br class="">+ /// Return whether all weights of successors are normalized.<br class="">+ bool areSuccWeightsNormalized() const { return AreSuccWeightsNormalized; }<br class="">+<br class=""> /// MachineBasicBlock iterator that automatically skips over MIs that are<br class=""> /// inside bundles (i.e. walk top level MIs only).<br class=""> template<typename Ty, typename IterTy><br class="">@@ -384,6 +391,12 @@ public:<br class=""> /// Set successor weight of a given iterator.<br class=""> void setSuccWeight(succ_iterator I, uint32_t weight);<br class=""><br class="">+ /// Normalize all succesor weights so that the sum of them does not exceed<br class="">+ /// UINT32_MAX. Return true if the weights are modified and false otherwise.<br class="">+ /// Note that weights that are modified after calling this function are not<br class="">+ /// guaranteed to be normalized.<br class="">+ bool normalizeSuccWeights();<br class="">+<br class=""> /// Remove successor from the successors list of this MachineBasicBlock. The<br class=""> /// Predecessors list of succ is automatically updated.<br class=""> void removeSuccessor(MachineBasicBlock *succ);<br class=""><br class="">Modified: llvm/trunk/include/llvm/CodeGen/MachineBranchProbabilityInfo.h<br class="">URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/MachineBranchProbabilityInfo.h?rev=244154&r1=244153&r2=244154&view=diff" class="">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/MachineBranchProbabilityInfo.h?rev=244154&r1=244153&r2=244154&view=diff</a><br class="">==============================================================================<br class="">--- llvm/trunk/include/llvm/CodeGen/MachineBranchProbabilityInfo.h (original)<br class="">+++ llvm/trunk/include/llvm/CodeGen/MachineBranchProbabilityInfo.h Wed Aug 5 17:01:20 2015<br class="">@@ -59,6 +59,10 @@ public:<br class=""> // adjustment. Any edge weights used with the sum should be divided by Scale.<br class=""> uint32_t getSumForBlock(const MachineBasicBlock *MBB, uint32_t &Scale) const;<br class=""><br class="">+ // Get sum of the block successors' weights, and force normalizing the<br class="">+ // successors' weights of MBB so that their sum fit within 32-bits.<br class="">+ uint32_t getSumForBlock(MachineBasicBlock *MBB) const;<br class="">+<br class=""> // A 'Hot' edge is an edge which probability is >= 80%.<br class=""> bool isEdgeHot(const MachineBasicBlock *Src,<br class=""> const MachineBasicBlock *Dst) const;<br class="">@@ -82,8 +86,34 @@ public:<br class=""> raw_ostream &printEdgeProbability(raw_ostream &OS,<br class=""> const MachineBasicBlock *Src,<br class=""> const MachineBasicBlock *Dst) const;<br class="">+<br class="">+ // Normalize a list of weights by scaling them down so that the sum of them<br class="">+ // doesn't exceed UINT32_MAX. Return the scale.<br class="">+ template <class WeightList><br class="">+ static uint32_t normalizeEdgeWeights(WeightList &Weights);<br class=""> };<br class=""><br class="">+template <class WeightList><br class="">+uint32_t<br class="">+MachineBranchProbabilityInfo::normalizeEdgeWeights(WeightList &Weights) {<br class="">+ assert(Weights.size() < UINT32_MAX && "Too many weights in the list!");<br class="">+ // First we compute the sum with 64-bits of precision.<br class="">+ uint64_t Sum = std::accumulate(Weights.begin(), Weights.end(), uint64_t(0));<br class="">+<br class="">+ // If the computed sum fits in 32-bits, we're done.<br class="">+ if (Sum <= UINT32_MAX)<br class="">+ return 1;<br class="">+<br class="">+ // Otherwise, compute the scale necessary to cause the weights to fit, and<br class="">+ // re-sum with that scale applied.<br class="">+ assert((Sum / UINT32_MAX) < UINT32_MAX &&<br class="">+ "The sum of weights exceeds UINT32_MAX^2!");<br class="">+ uint32_t Scale = (Sum / UINT32_MAX) + 1;<br class="">+ for (auto &W : Weights)<br class="">+ W /= Scale;<br class="">+ return Scale;<br class="">+}<br class="">+<br class=""> }<br class=""><br class=""><br class=""><br class="">Modified: llvm/trunk/lib/CodeGen/IfConversion.cpp<br class="">URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/IfConversion.cpp?rev=244154&r1=244153&r2=244154&view=diff" class="">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/IfConversion.cpp?rev=244154&r1=244153&r2=244154&view=diff</a><br class="">==============================================================================<br class="">--- llvm/trunk/lib/CodeGen/IfConversion.cpp (original)<br class="">+++ llvm/trunk/lib/CodeGen/IfConversion.cpp Wed Aug 5 17:01:20 2015<br class="">@@ -1232,15 +1232,17 @@ bool IfConverter::IfConvertTriangle(BBIn<br class=""><br class=""> bool HasEarlyExit = CvtBBI->FalseBB != nullptr;<br class=""> uint64_t CvtNext = 0, CvtFalse = 0, BBNext = 0, BBCvt = 0, SumWeight = 0;<br class="">- uint32_t WeightScale = 0;<br class=""><br class=""> if (HasEarlyExit) {<br class=""> // Get weights before modifying CvtBBI->BB and BBI.BB.<br class="">+ // Explictly normalize the weights of all edges from CvtBBI->BB so that we<br class="">+ // are aware that the edge weights obtained below are normalized.<br class="">+ CvtBBI->BB->normalizeSuccWeights();<br class=""> CvtNext = MBPI->getEdgeWeight(CvtBBI->BB, NextBBI->BB);<br class=""> CvtFalse = MBPI->getEdgeWeight(CvtBBI->BB, CvtBBI->FalseBB);<br class=""> BBNext = MBPI->getEdgeWeight(BBI.BB, NextBBI->BB);<br class=""> BBCvt = MBPI->getEdgeWeight(BBI.BB, CvtBBI->BB);<br class="">- SumWeight = MBPI->getSumForBlock(CvtBBI->BB, WeightScale);<br class="">+ SumWeight = MBPI->getSumForBlock(CvtBBI->BB);<br class=""> }<br class=""><br class=""> if (CvtBBI->BB->pred_size() > 1) {<br class="">@@ -1277,8 +1279,8 @@ bool IfConverter::IfConvertTriangle(BBIn<br class=""> // New_Weight(BBI.BB, CvtBBI->FalseBB) =<br class=""> // Weight(BBI.BB, CvtBBI->BB) * Weight(CvtBBI->BB, CvtBBI->FalseBB)<br class=""><br class="">- uint64_t NewNext = BBNext * SumWeight + (BBCvt * CvtNext) / WeightScale;<br class="">- uint64_t NewFalse = (BBCvt * CvtFalse) / WeightScale;<br class="">+ uint64_t NewNext = BBNext * SumWeight + BBCvt * CvtNext;<br class="">+ uint64_t NewFalse = BBCvt * CvtFalse;<br class=""> // We need to scale down all weights of BBI.BB to fit uint32_t.<br class=""> // Here BBI.BB is connected to CvtBBI->FalseBB and will fall through to<br class=""> // the next block.<br class=""><br class="">Modified: llvm/trunk/lib/CodeGen/MachineBasicBlock.cpp<br class="">URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineBasicBlock.cpp?rev=244154&r1=244153&r2=244154&view=diff" class="">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineBasicBlock.cpp?rev=244154&r1=244153&r2=244154&view=diff</a><br class="">==============================================================================<br class="">--- llvm/trunk/lib/CodeGen/MachineBasicBlock.cpp (original)<br class="">+++ llvm/trunk/lib/CodeGen/MachineBasicBlock.cpp Wed Aug 5 17:01:20 2015<br class="">@@ -16,6 +16,7 @@<br class=""> #include "llvm/ADT/SmallString.h"<br class=""> #include "llvm/CodeGen/LiveIntervalAnalysis.h"<br class=""> #include "llvm/CodeGen/LiveVariables.h"<br class="">+#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"<br class=""> #include "llvm/CodeGen/MachineDominators.h"<br class=""> #include "llvm/CodeGen/MachineFunction.h"<br class=""> #include "llvm/CodeGen/MachineInstrBuilder.h"<br class="">@@ -39,8 +40,9 @@ using namespace llvm;<br class=""> #define DEBUG_TYPE "codegen"<br class=""><br class=""> MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb)<br class="">- : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false),<br class="">- AddressTaken(false), CachedMCSymbol(nullptr) {<br class="">+ : BB(bb), Number(-1), AreSuccWeightsNormalized(false), xParent(&mf),<br class="">+ Alignment(0), IsLandingPad(false), AddressTaken(false),<br class="">+ CachedMCSymbol(nullptr) {<br class=""> Insts.Parent = this;<br class=""> }<br class=""><br class="">@@ -481,8 +483,10 @@ void MachineBasicBlock::addSuccessor(Mac<br class=""> if (weight != 0 && Weights.empty())<br class=""> Weights.resize(Successors.size());<br class=""><br class="">- if (weight != 0 || !Weights.empty())<br class="">+ if (weight != 0 || !Weights.empty()) {<br class=""> Weights.push_back(weight);<br class="">+ AreSuccWeightsNormalized = false;<br class="">+ }<br class=""><br class=""> Successors.push_back(succ);<br class=""> succ->addPredecessor(this);<br class="">@@ -1096,7 +1100,25 @@ uint32_t MachineBasicBlock::getSuccWeigh<br class=""> void MachineBasicBlock::setSuccWeight(succ_iterator I, uint32_t weight) {<br class=""> if (Weights.empty())<br class=""> return;<br class="">- *getWeightIterator(I) = weight;<br class="">+ auto WeightIter = getWeightIterator(I);<br class="">+ uint32_t OldWeight = *WeightIter;<br class="">+ *WeightIter = weight;<br class="">+ if (weight > OldWeight)<br class="">+ AreSuccWeightsNormalized = false;<br class="">+}<br class="">+<br class="">+/// Normalize all succesor weights so that the sum of them does not exceed<br class="">+/// UINT32_MAX. Return true if the weights are modified and false otherwise.<br class="">+/// Note that weights that are modified after calling this function are not<br class="">+/// guaranteed to be normalized.<br class="">+bool MachineBasicBlock::normalizeSuccWeights() {<br class="">+ if (!AreSuccWeightsNormalized) {<br class="">+ uint32_t Scale =<br class="">+ MachineBranchProbabilityInfo::normalizeEdgeWeights(Weights);<br class="">+ AreSuccWeightsNormalized = true;<br class="">+ return Scale != 1;<br class="">+ }<br class="">+ return false;<br class=""> }<br class=""><br class=""> /// getWeightIterator - Return wight iterator corresonding to the I successor<br class=""><br class="">Modified: llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp<br class="">URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp?rev=244154&r1=244153&r2=244154&view=diff" class="">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp?rev=244154&r1=244153&r2=244154&view=diff</a><br class="">==============================================================================<br class="">--- llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp (original)<br class="">+++ llvm/trunk/lib/CodeGen/MachineBlockPlacement.cpp Wed Aug 5 17:01:20 2015<br class="">@@ -361,8 +361,7 @@ MachineBlockPlacement::selectBestSuccess<br class=""> // improve the MBPI interface to efficiently support query patterns such as<br class=""> // this.<br class=""> uint32_t BestWeight = 0;<br class="">- uint32_t WeightScale = 0;<br class="">- uint32_t SumWeight = MBPI->getSumForBlock(BB, WeightScale);<br class="">+ uint32_t SumWeight = MBPI->getSumForBlock(BB);<br class=""> DEBUG(dbgs() << "Attempting merge from: " << getBlockName(BB) << "\n");<br class=""> for (MachineBasicBlock *Succ : BB->successors()) {<br class=""> if (BlockFilter && !BlockFilter->count(Succ))<br class="">@@ -378,7 +377,7 @@ MachineBlockPlacement::selectBestSuccess<br class=""> }<br class=""><br class=""> uint32_t SuccWeight = MBPI->getEdgeWeight(BB, Succ);<br class="">- BranchProbability SuccProb(SuccWeight / WeightScale, SumWeight);<br class="">+ BranchProbability SuccProb(SuccWeight, SumWeight);<br class=""><br class=""> // If we outline optional branches, look whether Succ is unavoidable, i.e.<br class=""> // dominates all terminators of the MachineFunction. If it does, other<br class="">@@ -675,8 +674,7 @@ MachineBlockPlacement::findBestLoopExit(<br class=""> // FIXME: Due to the performance of the probability and weight routines in<br class=""> // the MBPI analysis, we use the internal weights and manually compute the<br class=""> // probabilities to avoid quadratic behavior.<br class="">- uint32_t WeightScale = 0;<br class="">- uint32_t SumWeight = MBPI->getSumForBlock(MBB, WeightScale);<br class="">+ uint32_t SumWeight = MBPI->getSumForBlock(MBB);<br class=""> for (MachineBasicBlock *Succ : MBB->successors()) {<br class=""> if (Succ->isLandingPad())<br class=""> continue;<br class="">@@ -705,7 +703,7 @@ MachineBlockPlacement::findBestLoopExit(<br class=""> BlocksExitingToOuterLoop.insert(MBB);<br class=""> }<br class=""><br class="">- BranchProbability SuccProb(SuccWeight / WeightScale, SumWeight);<br class="">+ BranchProbability SuccProb(SuccWeight, SumWeight);<br class=""> BlockFrequency ExitEdgeFreq = MBFI->getBlockFreq(MBB) * SuccProb;<br class=""> DEBUG(dbgs() << " exiting: " << getBlockName(MBB) << " -> "<br class=""> << getBlockName(Succ) << " [L:" << SuccLoopDepth << "] (";<br class=""><br class="">Modified: llvm/trunk/lib/CodeGen/MachineBranchProbabilityInfo.cpp<br class="">URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineBranchProbabilityInfo.cpp?rev=244154&r1=244153&r2=244154&view=diff" class="">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineBranchProbabilityInfo.cpp?rev=244154&r1=244153&r2=244154&view=diff</a><br class="">==============================================================================<br class="">--- llvm/trunk/lib/CodeGen/MachineBranchProbabilityInfo.cpp (original)<br class="">+++ llvm/trunk/lib/CodeGen/MachineBranchProbabilityInfo.cpp Wed Aug 5 17:01:20 2015<br class="">@@ -28,36 +28,35 @@ char MachineBranchProbabilityInfo::ID =<br class=""><br class=""> void MachineBranchProbabilityInfo::anchor() { }<br class=""><br class="">-uint32_t MachineBranchProbabilityInfo::<br class="">-getSumForBlock(const MachineBasicBlock *MBB, uint32_t &Scale) const {<br class="">- // First we compute the sum with 64-bits of precision, ensuring that cannot<br class="">- // overflow by bounding the number of weights considered. Hopefully no one<br class="">- // actually needs 2^32 successors.<br class="">- assert(MBB->succ_size() < UINT32_MAX);<br class="">- uint64_t Sum = 0;<br class="">- Scale = 1;<br class="">+uint32_t<br class="">+MachineBranchProbabilityInfo::getSumForBlock(MachineBasicBlock *MBB) const {<br class="">+ // Normalize the weights of MBB's all successors so that the sum is guaranteed<br class="">+ // to be no greater than UINT32_MAX.<br class="">+ MBB->normalizeSuccWeights();<br class="">+<br class="">+ SmallVector<uint32_t, 8> Weights;<br class=""> for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),<br class="">- E = MBB->succ_end(); I != E; ++I) {<br class="">- uint32_t Weight = getEdgeWeight(MBB, I);<br class="">- Sum += Weight;<br class="">- }<br class="">-<br class="">- // If the computed sum fits in 32-bits, we're done.<br class="">- if (Sum <= UINT32_MAX)<br class="">- return Sum;<br class="">-<br class="">- // Otherwise, compute the scale necessary to cause the weights to fit, and<br class="">- // re-sum with that scale applied.<br class="">- assert((Sum / UINT32_MAX) < UINT32_MAX);<br class="">- Scale = (Sum / UINT32_MAX) + 1;<br class="">- Sum = 0;<br class="">+ E = MBB->succ_end();<br class="">+ I != E; ++I)<br class="">+ Weights.push_back(getEdgeWeight(MBB, I));<br class="">+<br class="">+ return std::accumulate(Weights.begin(), Weights.end(), 0u);<br class="">+}<br class="">+<br class="">+uint32_t<br class="">+MachineBranchProbabilityInfo::getSumForBlock(const MachineBasicBlock *MBB,<br class="">+ uint32_t &Scale) const {<br class="">+ SmallVector<uint32_t, 8> Weights;<br class=""> for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),<br class="">- E = MBB->succ_end(); I != E; ++I) {<br class="">- uint32_t Weight = getEdgeWeight(MBB, I);<br class="">- Sum += Weight / Scale;<br class="">- }<br class="">- assert(Sum <= UINT32_MAX);<br class="">- return Sum;<br class="">+ E = MBB->succ_end();<br class="">+ I != E; ++I)<br class="">+ Weights.push_back(getEdgeWeight(MBB, I));<br class="">+<br class="">+ if (MBB->areSuccWeightsNormalized())<br class="">+ Scale = 1;<br class="">+ else<br class="">+ Scale = MachineBranchProbabilityInfo::normalizeEdgeWeights(Weights);<br class="">+ return std::accumulate(Weights.begin(), Weights.end(), 0u);<br class=""> }<br class=""><br class=""> uint32_t MachineBranchProbabilityInfo::<br class=""><br class=""><br class="">_______________________________________________<br class="">llvm-commits mailing list<br class=""><a href="mailto:llvm-commits@lists.llvm.org" class="">llvm-commits@lists.llvm.org</a><br class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits<br class=""></div></div></blockquote></div><br class=""></div></body></html>