[llvm] r256263 - [BPI] Replace weights by probabilities in BPI.
Eric Christopher via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 22 15:28:55 PST 2015
Thanks for the quick response!
-eric
On Tue, Dec 22, 2015 at 3:27 PM Cong Hou <congh at google.com> wrote:
> Thanks! After reviewing my patch, I think I have found the places where
> divide-by-zero may happen. I am working on the fix now. I will test it on
> your testcase.
>
>
> thanks,
> Cong
>
> On Tue, Dec 22, 2015 at 3:23 PM, Eric Christopher <echristo at gmail.com>
> wrote:
>
>> Working on a public testcase :)
>>
>> I've sent you some other links though.
>>
>> -eric
>>
>> On Tue, Dec 22, 2015 at 3:21 PM Cong Hou <congh at google.com> wrote:
>>
>>> Thanks for letting me know this. Could you please send me some links
>>> showing such errors?
>>>
>>>
>>>
>>> thanks,
>>> Cong
>>>
>>> On Tue, Dec 22, 2015 at 3:05 PM, Eric Christopher <echristo at gmail.com>
>>> wrote:
>>>
>>>> Hi Cong,
>>>>
>>>> Just to let you know I'm seeing some (likely) divide by zeros here. I'm
>>>> looking into it and will either fix, or revert and get a testcase for you.
>>>>
>>>> -eric
>>>>
>>>> On Tue, Dec 22, 2015 at 10:59 AM Cong Hou via llvm-commits <
>>>> llvm-commits at lists.llvm.org> wrote:
>>>>
>>>>> Author: conghou
>>>>> Date: Tue Dec 22 12:56:14 2015
>>>>> New Revision: 256263
>>>>>
>>>>> URL: http://llvm.org/viewvc/llvm-project?rev=256263&view=rev
>>>>> Log:
>>>>> [BPI] Replace weights by probabilities in BPI.
>>>>>
>>>>> This patch removes all weight-related interfaces from BPI and replace
>>>>> them by probability versions. With this patch, we won't use edge weight
>>>>> anymore in either IR or MC passes. Edge probabilitiy is a better
>>>>> representation in terms of CFG update and validation.
>>>>>
>>>>>
>>>>> Differential revision: http://reviews.llvm.org/D15519
>>>>>
>>>>>
>>>>>
>>>>> Modified:
>>>>> llvm/trunk/include/llvm/Analysis/BranchProbabilityInfo.h
>>>>> llvm/trunk/include/llvm/Support/BranchProbability.h
>>>>> llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp
>>>>> llvm/trunk/lib/CodeGen/StackProtector.cpp
>>>>> llvm/trunk/lib/Target/PowerPC/PPCISelDAGToDAG.cpp
>>>>> llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp
>>>>> llvm/trunk/test/Analysis/BranchProbabilityInfo/noreturn.ll
>>>>> llvm/trunk/test/Transforms/JumpThreading/update-edge-weight.ll
>>>>>
>>>>> Modified: llvm/trunk/include/llvm/Analysis/BranchProbabilityInfo.h
>>>>> URL:
>>>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/BranchProbabilityInfo.h?rev=256263&r1=256262&r2=256263&view=diff
>>>>>
>>>>> ==============================================================================
>>>>> --- llvm/trunk/include/llvm/Analysis/BranchProbabilityInfo.h (original)
>>>>> +++ llvm/trunk/include/llvm/Analysis/BranchProbabilityInfo.h Tue Dec
>>>>> 22 12:56:14 2015
>>>>> @@ -84,36 +84,14 @@ public:
>>>>> raw_ostream &printEdgeProbability(raw_ostream &OS, const BasicBlock
>>>>> *Src,
>>>>> const BasicBlock *Dst) const;
>>>>>
>>>>> - /// \brief Get the raw edge weight calculated for the edge.
>>>>> + /// \brief Set the raw edge probability for the given edge.
>>>>> ///
>>>>> - /// This returns the raw edge weight. It is guaranteed to fall
>>>>> between 1 and
>>>>> - /// UINT32_MAX. Note that the raw edge weight is not meaningful in
>>>>> isolation.
>>>>> - /// This interface should be very carefully, and primarily by
>>>>> routines that
>>>>> - /// are updating the analysis by later calling setEdgeWeight.
>>>>> - uint32_t getEdgeWeight(const BasicBlock *Src,
>>>>> - unsigned IndexInSuccessors) const;
>>>>> -
>>>>> - /// \brief Get the raw edge weight calculated for the block pair.
>>>>> - ///
>>>>> - /// This returns the sum of all raw edge weights from Src to Dst.
>>>>> - /// It is guaranteed to fall between 1 and UINT32_MAX.
>>>>> - uint32_t getEdgeWeight(const BasicBlock *Src, const BasicBlock
>>>>> *Dst) const;
>>>>> -
>>>>> - uint32_t getEdgeWeight(const BasicBlock *Src,
>>>>> - succ_const_iterator Dst) const;
>>>>> -
>>>>> - /// \brief Set the raw edge weight for a given edge.
>>>>> - ///
>>>>> - /// This allows a pass to explicitly set the edge weight for an
>>>>> edge. It can
>>>>> - /// be used when updating the CFG to update and preserve the branch
>>>>> + /// This allows a pass to explicitly set the edge probability for
>>>>> an edge. It
>>>>> + /// can be used when updating the CFG to update and preserve the
>>>>> branch
>>>>> /// probability information. Read the implementation of how these
>>>>> edge
>>>>> - /// weights are calculated carefully before using!
>>>>> - void setEdgeWeight(const BasicBlock *Src, unsigned
>>>>> IndexInSuccessors,
>>>>> - uint32_t Weight);
>>>>> -
>>>>> - static uint32_t getBranchWeightStackProtector(bool IsLikely) {
>>>>> - return IsLikely ? (1u << 20) - 1 : 1;
>>>>> - }
>>>>> + /// probabilities are calculated carefully before using!
>>>>> + void setEdgeProbability(const BasicBlock *Src, unsigned
>>>>> IndexInSuccessors,
>>>>> + BranchProbability Prob);
>>>>>
>>>>> static BranchProbability getBranchProbStackProtector(bool IsLikely)
>>>>> {
>>>>> static const BranchProbability LikelyProb((1u << 20) - 1, 1u <<
>>>>> 20);
>>>>> @@ -135,7 +113,7 @@ private:
>>>>> // weight to just "inherit" the non-zero weight of an adjacent
>>>>> successor.
>>>>> static const uint32_t DEFAULT_WEIGHT = 16;
>>>>>
>>>>> - DenseMap<Edge, uint32_t> Weights;
>>>>> + DenseMap<Edge, BranchProbability> Probs;
>>>>>
>>>>> /// \brief Track the last function we run over for printing.
>>>>> Function *LastF;
>>>>> @@ -146,9 +124,6 @@ private:
>>>>> /// \brief Track the set of blocks that always lead to a cold call.
>>>>> SmallPtrSet<BasicBlock *, 16> PostDominatedByColdCall;
>>>>>
>>>>> - /// \brief Get sum of the block successors' weights.
>>>>> - uint32_t getSumForBlock(const BasicBlock *BB) const;
>>>>> -
>>>>> bool calcUnreachableHeuristics(BasicBlock *BB);
>>>>> bool calcMetadataWeights(BasicBlock *BB);
>>>>> bool calcColdCallHeuristics(BasicBlock *BB);
>>>>>
>>>>> Modified: llvm/trunk/include/llvm/Support/BranchProbability.h
>>>>> URL:
>>>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/BranchProbability.h?rev=256263&r1=256262&r2=256263&view=diff
>>>>>
>>>>> ==============================================================================
>>>>> --- llvm/trunk/include/llvm/Support/BranchProbability.h (original)
>>>>> +++ llvm/trunk/include/llvm/Support/BranchProbability.h Tue Dec 22
>>>>> 12:56:14 2015
>>>>> @@ -63,11 +63,6 @@ public:
>>>>> static void normalizeProbabilities(ProbabilityIter Begin,
>>>>> ProbabilityIter End);
>>>>>
>>>>> - // Normalize a list of weights by scaling them down so that the sum
>>>>> of them
>>>>> - // doesn't exceed UINT32_MAX.
>>>>> - template <class WeightListIter>
>>>>> - static void normalizeEdgeWeights(WeightListIter Begin,
>>>>> WeightListIter End);
>>>>> -
>>>>> uint32_t getNumerator() const { return N; }
>>>>> static uint32_t getDenominator() { return D; }
>>>>>
>>>>> @@ -219,49 +214,6 @@ void BranchProbability::normalizeProbabi
>>>>> I->N = (I->N * uint64_t(D) + Sum / 2) / Sum;
>>>>> }
>>>>>
>>>>> -template <class WeightListIter>
>>>>> -void BranchProbability::normalizeEdgeWeights(WeightListIter Begin,
>>>>> - WeightListIter End) {
>>>>> - // First we compute the sum with 64-bits of precision.
>>>>> - uint64_t Sum = std::accumulate(Begin, End, uint64_t(0));
>>>>> -
>>>>> - if (Sum > UINT32_MAX) {
>>>>> - // 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 I = Begin; I != End; ++I)
>>>>> - *I /= Scale;
>>>>> - Sum = std::accumulate(Begin, End, uint64_t(0));
>>>>> - }
>>>>> -
>>>>> - // Eliminate zero weights.
>>>>> - auto ZeroWeightNum = std::count(Begin, End, 0u);
>>>>> - if (ZeroWeightNum > 0) {
>>>>> - // If all weights are zeros, replace them by 1.
>>>>> - if (Sum == 0)
>>>>> - std::fill(Begin, End, 1u);
>>>>> - else {
>>>>> - // We are converting zeros into ones, and here we need to make
>>>>> sure that
>>>>> - // after this the sum won't exceed UINT32_MAX.
>>>>> - if (Sum + ZeroWeightNum > UINT32_MAX) {
>>>>> - for (auto I = Begin; I != End; ++I)
>>>>> - *I /= 2;
>>>>> - ZeroWeightNum = std::count(Begin, End, 0u);
>>>>> - Sum = std::accumulate(Begin, End, uint64_t(0));
>>>>> - }
>>>>> - // Scale up non-zero weights and turn zero weights into ones.
>>>>> - uint64_t ScalingFactor = (UINT32_MAX - ZeroWeightNum) / Sum;
>>>>> - assert(ScalingFactor >= 1);
>>>>> - if (ScalingFactor > 1)
>>>>> - for (auto I = Begin; I != End; ++I)
>>>>> - *I *= ScalingFactor;
>>>>> - std::replace(Begin, End, 0u, 1u);
>>>>> - }
>>>>> - }
>>>>> -}
>>>>> -
>>>>> }
>>>>>
>>>>> #endif
>>>>>
>>>>> Modified: llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp
>>>>> URL:
>>>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp?rev=256263&r1=256262&r2=256263&view=diff
>>>>>
>>>>> ==============================================================================
>>>>> --- llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp (original)
>>>>> +++ llvm/trunk/lib/Analysis/BranchProbabilityInfo.cpp Tue Dec 22
>>>>> 12:56:14 2015
>>>>> @@ -108,13 +108,6 @@ static const uint32_t IH_TAKEN_WEIGHT =
>>>>> /// instruction. This is essentially never taken.
>>>>> static const uint32_t IH_NONTAKEN_WEIGHT = 1;
>>>>>
>>>>> -// Standard weight value. Used when none of the heuristics set weight
>>>>> for
>>>>> -// the edge.
>>>>> -static const uint32_t NORMAL_WEIGHT = 16;
>>>>> -
>>>>> -// Minimum weight of an edge. Please note, that weight is NEVER 0.
>>>>> -static const uint32_t MIN_WEIGHT = 1;
>>>>> -
>>>>> /// \brief Calculate edge weights for successors lead to unreachable.
>>>>> ///
>>>>> /// Predict that a successor which leads necessarily to an
>>>>> @@ -157,22 +150,24 @@ bool BranchProbabilityInfo::calcUnreacha
>>>>> return false;
>>>>> }
>>>>>
>>>>> - uint32_t UnreachableWeight =
>>>>> - std::max(UR_TAKEN_WEIGHT / (unsigned)UnreachableEdges.size(),
>>>>> MIN_WEIGHT);
>>>>> - for (SmallVectorImpl<unsigned>::iterator I =
>>>>> UnreachableEdges.begin(),
>>>>> - E = UnreachableEdges.end();
>>>>> - I != E; ++I)
>>>>> - setEdgeWeight(BB, *I, UnreachableWeight);
>>>>> -
>>>>> - if (ReachableEdges.empty())
>>>>> + if (ReachableEdges.empty()) {
>>>>> + BranchProbability Prob(1, UnreachableEdges.size());
>>>>> + for (unsigned SuccIdx : UnreachableEdges)
>>>>> + setEdgeProbability(BB, SuccIdx, Prob);
>>>>> return true;
>>>>> - uint32_t ReachableWeight =
>>>>> - std::max(UR_NONTAKEN_WEIGHT / (unsigned)ReachableEdges.size(),
>>>>> - NORMAL_WEIGHT);
>>>>> - for (SmallVectorImpl<unsigned>::iterator I = ReachableEdges.begin(),
>>>>> - E = ReachableEdges.end();
>>>>> - I != E; ++I)
>>>>> - setEdgeWeight(BB, *I, ReachableWeight);
>>>>> + }
>>>>> +
>>>>> + BranchProbability UnreachableProb(UR_TAKEN_WEIGHT,
>>>>> + (UR_TAKEN_WEIGHT +
>>>>> UR_NONTAKEN_WEIGHT) *
>>>>> + UnreachableEdges.size());
>>>>> + BranchProbability ReachableProb(UR_NONTAKEN_WEIGHT,
>>>>> + (UR_TAKEN_WEIGHT +
>>>>> UR_NONTAKEN_WEIGHT) *
>>>>> + ReachableEdges.size());
>>>>> +
>>>>> + for (unsigned SuccIdx : UnreachableEdges)
>>>>> + setEdgeProbability(BB, SuccIdx, UnreachableProb);
>>>>> + for (unsigned SuccIdx : ReachableEdges)
>>>>> + setEdgeProbability(BB, SuccIdx, ReachableProb);
>>>>>
>>>>> return true;
>>>>> }
>>>>> @@ -223,10 +218,12 @@ bool BranchProbabilityInfo::calcMetadata
>>>>>
>>>>> WeightSum = 0;
>>>>> for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
>>>>> - uint32_t W = Weights[i] / ScalingFactor;
>>>>> - WeightSum += W;
>>>>> - setEdgeWeight(BB, i, W);
>>>>> + Weights[i] /= ScalingFactor;
>>>>> + WeightSum += Weights[i];
>>>>> }
>>>>> + for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
>>>>> + setEdgeProbability(BB, i, {Weights[i],
>>>>> static_cast<uint32_t>(WeightSum)});
>>>>> +
>>>>> assert(WeightSum <= UINT32_MAX &&
>>>>> "Expected weights to scale down to 32 bits");
>>>>>
>>>>> @@ -275,21 +272,24 @@ bool BranchProbabilityInfo::calcColdCall
>>>>> if (TI->getNumSuccessors() == 1 || ColdEdges.empty())
>>>>> return false;
>>>>>
>>>>> - uint32_t ColdWeight =
>>>>> - std::max(CC_TAKEN_WEIGHT / (unsigned) ColdEdges.size(),
>>>>> MIN_WEIGHT);
>>>>> - for (SmallVectorImpl<unsigned>::iterator I = ColdEdges.begin(),
>>>>> - E = ColdEdges.end();
>>>>> - I != E; ++I)
>>>>> - setEdgeWeight(BB, *I, ColdWeight);
>>>>> -
>>>>> - if (NormalEdges.empty())
>>>>> + if (NormalEdges.empty()) {
>>>>> + BranchProbability Prob(1, ColdEdges.size());
>>>>> + for (unsigned SuccIdx : ColdEdges)
>>>>> + setEdgeProbability(BB, SuccIdx, Prob);
>>>>> return true;
>>>>> - uint32_t NormalWeight = std::max(
>>>>> - CC_NONTAKEN_WEIGHT / (unsigned) NormalEdges.size(),
>>>>> NORMAL_WEIGHT);
>>>>> - for (SmallVectorImpl<unsigned>::iterator I = NormalEdges.begin(),
>>>>> - E = NormalEdges.end();
>>>>> - I != E; ++I)
>>>>> - setEdgeWeight(BB, *I, NormalWeight);
>>>>> + }
>>>>> +
>>>>> + BranchProbability ColdProb(CC_TAKEN_WEIGHT,
>>>>> + (CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT) *
>>>>> + ColdEdges.size());
>>>>> + BranchProbability NormalProb(CC_NONTAKEN_WEIGHT,
>>>>> + (CC_TAKEN_WEIGHT + CC_NONTAKEN_WEIGHT)
>>>>> *
>>>>> + NormalEdges.size());
>>>>> +
>>>>> + for (unsigned SuccIdx : ColdEdges)
>>>>> + setEdgeProbability(BB, SuccIdx, ColdProb);
>>>>> + for (unsigned SuccIdx : NormalEdges)
>>>>> + setEdgeProbability(BB, SuccIdx, NormalProb);
>>>>>
>>>>> return true;
>>>>> }
>>>>> @@ -322,8 +322,10 @@ bool BranchProbabilityInfo::calcPointerH
>>>>> if (!isProb)
>>>>> std::swap(TakenIdx, NonTakenIdx);
>>>>>
>>>>> - setEdgeWeight(BB, TakenIdx, PH_TAKEN_WEIGHT);
>>>>> - setEdgeWeight(BB, NonTakenIdx, PH_NONTAKEN_WEIGHT);
>>>>> + BranchProbability TakenProb(PH_TAKEN_WEIGHT,
>>>>> + PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT);
>>>>> + setEdgeProbability(BB, TakenIdx, TakenProb);
>>>>> + setEdgeProbability(BB, NonTakenIdx, TakenProb.getCompl());
>>>>> return true;
>>>>> }
>>>>>
>>>>> @@ -351,37 +353,35 @@ bool BranchProbabilityInfo::calcLoopBran
>>>>> if (BackEdges.empty() && ExitingEdges.empty())
>>>>> return false;
>>>>>
>>>>> + // Collect the sum of probabilities of
>>>>> back-edges/in-edges/exiting-edges, and
>>>>> + // normalize them so that they sum up to one.
>>>>> + SmallVector<BranchProbability, 4> Probs(3,
>>>>> BranchProbability::getZero());
>>>>> + unsigned Denom = (BackEdges.empty() ? 0 : LBH_TAKEN_WEIGHT) +
>>>>> + (InEdges.empty() ? 0 : LBH_TAKEN_WEIGHT) +
>>>>> + (ExitingEdges.empty() ? 0 : LBH_NONTAKEN_WEIGHT);
>>>>> + if (!BackEdges.empty())
>>>>> + Probs[0] = BranchProbability(LBH_TAKEN_WEIGHT, Denom);
>>>>> + if (!InEdges.empty())
>>>>> + Probs[1] = BranchProbability(LBH_TAKEN_WEIGHT, Denom);
>>>>> + if (!ExitingEdges.empty())
>>>>> + Probs[2] = BranchProbability(LBH_NONTAKEN_WEIGHT, Denom);
>>>>> +
>>>>> if (uint32_t numBackEdges = BackEdges.size()) {
>>>>> - uint32_t backWeight = LBH_TAKEN_WEIGHT / numBackEdges;
>>>>> - if (backWeight < NORMAL_WEIGHT)
>>>>> - backWeight = NORMAL_WEIGHT;
>>>>> -
>>>>> - for (SmallVectorImpl<unsigned>::iterator EI = BackEdges.begin(),
>>>>> - EE = BackEdges.end(); EI != EE; ++EI) {
>>>>> - setEdgeWeight(BB, *EI, backWeight);
>>>>> - }
>>>>> + auto Prob = Probs[0] / numBackEdges;
>>>>> + for (unsigned SuccIdx : BackEdges)
>>>>> + setEdgeProbability(BB, SuccIdx, Prob);
>>>>> }
>>>>>
>>>>> if (uint32_t numInEdges = InEdges.size()) {
>>>>> - uint32_t inWeight = LBH_TAKEN_WEIGHT / numInEdges;
>>>>> - if (inWeight < NORMAL_WEIGHT)
>>>>> - inWeight = NORMAL_WEIGHT;
>>>>> -
>>>>> - for (SmallVectorImpl<unsigned>::iterator EI = InEdges.begin(),
>>>>> - EE = InEdges.end(); EI != EE; ++EI) {
>>>>> - setEdgeWeight(BB, *EI, inWeight);
>>>>> - }
>>>>> + auto Prob = Probs[1] / numInEdges;
>>>>> + for (unsigned SuccIdx : InEdges)
>>>>> + setEdgeProbability(BB, SuccIdx, Prob);
>>>>> }
>>>>>
>>>>> if (uint32_t numExitingEdges = ExitingEdges.size()) {
>>>>> - uint32_t exitWeight = LBH_NONTAKEN_WEIGHT / numExitingEdges;
>>>>> - if (exitWeight < MIN_WEIGHT)
>>>>> - exitWeight = MIN_WEIGHT;
>>>>> -
>>>>> - for (SmallVectorImpl<unsigned>::iterator EI =
>>>>> ExitingEdges.begin(),
>>>>> - EE = ExitingEdges.end(); EI != EE; ++EI) {
>>>>> - setEdgeWeight(BB, *EI, exitWeight);
>>>>> - }
>>>>> + auto Prob = Probs[2] / numExitingEdges;
>>>>> + for (unsigned SuccIdx : ExitingEdges)
>>>>> + setEdgeProbability(BB, SuccIdx, Prob);
>>>>> }
>>>>>
>>>>> return true;
>>>>> @@ -463,9 +463,10 @@ bool BranchProbabilityInfo::calcZeroHeur
>>>>> if (!isProb)
>>>>> std::swap(TakenIdx, NonTakenIdx);
>>>>>
>>>>> - setEdgeWeight(BB, TakenIdx, ZH_TAKEN_WEIGHT);
>>>>> - setEdgeWeight(BB, NonTakenIdx, ZH_NONTAKEN_WEIGHT);
>>>>> -
>>>>> + BranchProbability TakenProb(ZH_TAKEN_WEIGHT,
>>>>> + ZH_TAKEN_WEIGHT + ZH_NONTAKEN_WEIGHT);
>>>>> + setEdgeProbability(BB, TakenIdx, TakenProb);
>>>>> + setEdgeProbability(BB, NonTakenIdx, TakenProb.getCompl());
>>>>> return true;
>>>>> }
>>>>>
>>>>> @@ -499,9 +500,10 @@ bool BranchProbabilityInfo::calcFloating
>>>>> if (!isProb)
>>>>> std::swap(TakenIdx, NonTakenIdx);
>>>>>
>>>>> - setEdgeWeight(BB, TakenIdx, FPH_TAKEN_WEIGHT);
>>>>> - setEdgeWeight(BB, NonTakenIdx, FPH_NONTAKEN_WEIGHT);
>>>>> -
>>>>> + BranchProbability TakenProb(FPH_TAKEN_WEIGHT,
>>>>> + FPH_TAKEN_WEIGHT + FPH_NONTAKEN_WEIGHT);
>>>>> + setEdgeProbability(BB, TakenIdx, TakenProb);
>>>>> + setEdgeProbability(BB, NonTakenIdx, TakenProb.getCompl());
>>>>> return true;
>>>>> }
>>>>>
>>>>> @@ -510,13 +512,15 @@ bool BranchProbabilityInfo::calcInvokeHe
>>>>> if (!II)
>>>>> return false;
>>>>>
>>>>> - setEdgeWeight(BB, 0/*Index for Normal*/, IH_TAKEN_WEIGHT);
>>>>> - setEdgeWeight(BB, 1/*Index for Unwind*/, IH_NONTAKEN_WEIGHT);
>>>>> + BranchProbability TakenProb(IH_TAKEN_WEIGHT,
>>>>> + IH_TAKEN_WEIGHT + IH_NONTAKEN_WEIGHT);
>>>>> + setEdgeProbability(BB, 0 /*Index for Normal*/, TakenProb);
>>>>> + setEdgeProbability(BB, 1 /*Index for Unwind*/,
>>>>> TakenProb.getCompl());
>>>>> return true;
>>>>> }
>>>>>
>>>>> void BranchProbabilityInfo::releaseMemory() {
>>>>> - Weights.clear();
>>>>> + Probs.clear();
>>>>> }
>>>>>
>>>>> void BranchProbabilityInfo::print(raw_ostream &OS) const {
>>>>> @@ -532,20 +536,6 @@ void BranchProbabilityInfo::print(raw_os
>>>>> }
>>>>> }
>>>>>
>>>>> -uint32_t BranchProbabilityInfo::getSumForBlock(const BasicBlock *BB)
>>>>> const {
>>>>> - uint32_t Sum = 0;
>>>>> -
>>>>> - for (succ_const_iterator I = succ_begin(BB), E = succ_end(BB); I !=
>>>>> E; ++I) {
>>>>> - uint32_t Weight = getEdgeWeight(BB, I.getSuccessorIndex());
>>>>> - uint32_t PrevSum = Sum;
>>>>> -
>>>>> - Sum += Weight;
>>>>> - assert(Sum >= PrevSum); (void) PrevSum;
>>>>> - }
>>>>> -
>>>>> - return Sum;
>>>>> -}
>>>>> -
>>>>> bool BranchProbabilityInfo::
>>>>> isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const {
>>>>> // Hot probability is at least 4/5 = 80%
>>>>> @@ -554,113 +544,74 @@ isEdgeHot(const BasicBlock *Src, const B
>>>>> }
>>>>>
>>>>> BasicBlock *BranchProbabilityInfo::getHotSucc(BasicBlock *BB) const {
>>>>> - uint32_t Sum = 0;
>>>>> - uint32_t MaxWeight = 0;
>>>>> + auto MaxProb = BranchProbability::getZero();
>>>>> BasicBlock *MaxSucc = nullptr;
>>>>>
>>>>> for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E;
>>>>> ++I) {
>>>>> BasicBlock *Succ = *I;
>>>>> - uint32_t Weight = getEdgeWeight(BB, Succ);
>>>>> - uint32_t PrevSum = Sum;
>>>>> -
>>>>> - Sum += Weight;
>>>>> - assert(Sum > PrevSum); (void) PrevSum;
>>>>> -
>>>>> - if (Weight > MaxWeight) {
>>>>> - MaxWeight = Weight;
>>>>> + auto Prob = getEdgeProbability(BB, Succ);
>>>>> + if (Prob > MaxProb) {
>>>>> + MaxProb = Prob;
>>>>> MaxSucc = Succ;
>>>>> }
>>>>> }
>>>>>
>>>>> // Hot probability is at least 4/5 = 80%
>>>>> - if (BranchProbability(MaxWeight, Sum) > BranchProbability(4, 5))
>>>>> + if (MaxProb > BranchProbability(4, 5))
>>>>> return MaxSucc;
>>>>>
>>>>> return nullptr;
>>>>> }
>>>>>
>>>>> -/// Get the raw edge weight for the edge. If can't find it, return
>>>>> -/// DEFAULT_WEIGHT value. Here an edge is specified using PredBlock
>>>>> and an index
>>>>> -/// to the successors.
>>>>> -uint32_t BranchProbabilityInfo::
>>>>> -getEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors)
>>>>> const {
>>>>> - DenseMap<Edge, uint32_t>::const_iterator I =
>>>>> - Weights.find(std::make_pair(Src, IndexInSuccessors));
>>>>> +/// Get the raw edge probability for the edge. If can't find it,
>>>>> return a
>>>>> +/// default probability 1/N where N is the number of successors. Here
>>>>> an edge is
>>>>> +/// specified using PredBlock and an
>>>>> +/// index to the successors.
>>>>> +BranchProbability
>>>>> +BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,
>>>>> + unsigned IndexInSuccessors)
>>>>> const {
>>>>> + auto I = Probs.find(std::make_pair(Src, IndexInSuccessors));
>>>>>
>>>>> - if (I != Weights.end())
>>>>> + if (I != Probs.end())
>>>>> return I->second;
>>>>>
>>>>> - return DEFAULT_WEIGHT;
>>>>> + return {1,
>>>>> + static_cast<uint32_t>(std::distance(succ_begin(Src),
>>>>> succ_end(Src)))};
>>>>> }
>>>>>
>>>>> -uint32_t BranchProbabilityInfo::getEdgeWeight(const BasicBlock *Src,
>>>>> - succ_const_iterator
>>>>> Dst) const {
>>>>> - return getEdgeWeight(Src, Dst.getSuccessorIndex());
>>>>> +BranchProbability
>>>>> +BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,
>>>>> + succ_const_iterator Dst)
>>>>> const {
>>>>> + return getEdgeProbability(Src, Dst.getSuccessorIndex());
>>>>> }
>>>>>
>>>>> -/// Get the raw edge weight calculated for the block pair. This
>>>>> returns the sum
>>>>> -/// of all raw edge weights from Src to Dst.
>>>>> -uint32_t BranchProbabilityInfo::
>>>>> -getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const {
>>>>> - uint32_t Weight = 0;
>>>>> - bool FoundWeight = false;
>>>>> - DenseMap<Edge, uint32_t>::const_iterator MapI;
>>>>> +/// Get the raw edge probability calculated for the block pair. This
>>>>> returns the
>>>>> +/// sum of all raw edge probabilities from Src to Dst.
>>>>> +BranchProbability
>>>>> +BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,
>>>>> + const BasicBlock *Dst)
>>>>> const {
>>>>> + auto Prob = BranchProbability::getZero();
>>>>> + bool FoundProb = false;
>>>>> for (succ_const_iterator I = succ_begin(Src), E = succ_end(Src); I
>>>>> != E; ++I)
>>>>> if (*I == Dst) {
>>>>> - MapI = Weights.find(std::make_pair(Src, I.getSuccessorIndex()));
>>>>> - if (MapI != Weights.end()) {
>>>>> - FoundWeight = true;
>>>>> - Weight += MapI->second;
>>>>> + auto MapI = Probs.find(std::make_pair(Src,
>>>>> I.getSuccessorIndex()));
>>>>> + if (MapI != Probs.end()) {
>>>>> + FoundProb = true;
>>>>> + Prob += MapI->second;
>>>>> }
>>>>> }
>>>>> - return (!FoundWeight) ? DEFAULT_WEIGHT : Weight;
>>>>> -}
>>>>> -
>>>>> -/// Set the edge weight for a given edge specified by PredBlock and
>>>>> an index
>>>>> -/// to the successors.
>>>>> -void BranchProbabilityInfo::
>>>>> -setEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors,
>>>>> - uint32_t Weight) {
>>>>> - Weights[std::make_pair(Src, IndexInSuccessors)] = Weight;
>>>>> - DEBUG(dbgs() << "set edge " << Src->getName() << " -> "
>>>>> - << IndexInSuccessors << " successor weight to "
>>>>> - << Weight << "\n");
>>>>> + uint32_t succ_num = std::distance(succ_begin(Src), succ_end(Src));
>>>>> + return FoundProb ? Prob : BranchProbability(1, succ_num);
>>>>> }
>>>>>
>>>>> -/// Get an edge's probability, relative to other out-edges from Src.
>>>>> -BranchProbability BranchProbabilityInfo::
>>>>> -getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors)
>>>>> const {
>>>>> - uint32_t N = getEdgeWeight(Src, IndexInSuccessors);
>>>>> - uint32_t D = getSumForBlock(Src);
>>>>> -
>>>>> - // It is possible that the edge weight on the only successor edge
>>>>> of Src is
>>>>> - // zero, in which case we return 100%.
>>>>> - if (N == 0 && D == 0)
>>>>> - return BranchProbability::getOne();
>>>>> -
>>>>> - return BranchProbability(N, D);
>>>>> -}
>>>>> -
>>>>> -/// Get the probability of going from Src to Dst. It returns the sum
>>>>> of all
>>>>> -/// probabilities for edges from Src to Dst.
>>>>> -BranchProbability BranchProbabilityInfo::
>>>>> -getEdgeProbability(const BasicBlock *Src, const BasicBlock *Dst)
>>>>> const {
>>>>> -
>>>>> - uint32_t N = getEdgeWeight(Src, Dst);
>>>>> - uint32_t D = getSumForBlock(Src);
>>>>> -
>>>>> - // It is possible that the edge weight on the only successor edge
>>>>> of Src is
>>>>> - // zero, in which case we return 100%.
>>>>> - if (N == 0 && D == 0)
>>>>> - return BranchProbability::getOne();
>>>>> -
>>>>> - return BranchProbability(N, D);
>>>>> -}
>>>>> -
>>>>> -BranchProbability
>>>>> -BranchProbabilityInfo::getEdgeProbability(const BasicBlock *Src,
>>>>> - succ_const_iterator Dst)
>>>>> const {
>>>>> - return getEdgeProbability(Src, Dst.getSuccessorIndex());
>>>>> +/// Set the edge probability for a given edge specified by PredBlock
>>>>> and an
>>>>> +/// index to the successors.
>>>>> +void BranchProbabilityInfo::setEdgeProbability(const BasicBlock *Src,
>>>>> + unsigned
>>>>> IndexInSuccessors,
>>>>> + BranchProbability
>>>>> Prob) {
>>>>> + Probs[std::make_pair(Src, IndexInSuccessors)] = Prob;
>>>>> + DEBUG(dbgs() << "set edge " << Src->getName() << " -> " <<
>>>>> IndexInSuccessors
>>>>> + << " successor probability to " << Prob << "\n");
>>>>> }
>>>>>
>>>>> raw_ostream &
>>>>>
>>>>> Modified: llvm/trunk/lib/CodeGen/StackProtector.cpp
>>>>> URL:
>>>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/StackProtector.cpp?rev=256263&r1=256262&r2=256263&view=diff
>>>>>
>>>>> ==============================================================================
>>>>> --- llvm/trunk/lib/CodeGen/StackProtector.cpp (original)
>>>>> +++ llvm/trunk/lib/CodeGen/StackProtector.cpp Tue Dec 22 12:56:14 2015
>>>>> @@ -453,12 +453,13 @@ bool StackProtector::InsertStackProtecto
>>>>> LoadInst *LI1 = B.CreateLoad(StackGuardVar);
>>>>> LoadInst *LI2 = B.CreateLoad(AI);
>>>>> Value *Cmp = B.CreateICmpEQ(LI1, LI2);
>>>>> - unsigned SuccessWeight =
>>>>> - BranchProbabilityInfo::getBranchWeightStackProtector(true);
>>>>> - unsigned FailureWeight =
>>>>> - BranchProbabilityInfo::getBranchWeightStackProtector(false);
>>>>> + auto SuccessProb =
>>>>> + BranchProbabilityInfo::getBranchProbStackProtector(true);
>>>>> + auto FailureProb =
>>>>> + BranchProbabilityInfo::getBranchProbStackProtector(false);
>>>>> MDNode *Weights = MDBuilder(F->getContext())
>>>>> - .createBranchWeights(SuccessWeight,
>>>>> FailureWeight);
>>>>> +
>>>>> .createBranchWeights(SuccessProb.getNumerator(),
>>>>> +
>>>>> FailureProb.getNumerator());
>>>>> B.CreateCondBr(Cmp, NewBB, FailBB, Weights);
>>>>> }
>>>>> }
>>>>>
>>>>> Modified: llvm/trunk/lib/Target/PowerPC/PPCISelDAGToDAG.cpp
>>>>> URL:
>>>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/PowerPC/PPCISelDAGToDAG.cpp?rev=256263&r1=256262&r2=256263&view=diff
>>>>>
>>>>> ==============================================================================
>>>>> --- llvm/trunk/lib/Target/PowerPC/PPCISelDAGToDAG.cpp (original)
>>>>> +++ llvm/trunk/lib/Target/PowerPC/PPCISelDAGToDAG.cpp Tue Dec 22
>>>>> 12:56:14 2015
>>>>> @@ -414,8 +414,8 @@ static unsigned getBranchHint(unsigned P
>>>>> const BasicBlock *TBB = BBTerm->getSuccessor(0);
>>>>> const BasicBlock *FBB = BBTerm->getSuccessor(1);
>>>>>
>>>>> - uint32_t TWeight = FuncInfo->BPI->getEdgeWeight(BB, TBB);
>>>>> - uint32_t FWeight = FuncInfo->BPI->getEdgeWeight(BB, FBB);
>>>>> + auto TProb = FuncInfo->BPI->getEdgeProbability(BB, TBB);
>>>>> + auto FProb = FuncInfo->BPI->getEdgeProbability(BB, FBB);
>>>>>
>>>>> // We only want to handle cases which are easy to predict at static
>>>>> time, e.g.
>>>>> // C++ throw statement, that is very likely not taken, or calling
>>>>> never
>>>>> @@ -432,24 +432,22 @@ static unsigned getBranchHint(unsigned P
>>>>> // 5. PH/ZH/FPH 20:12
>>>>> const uint32_t Threshold = 10000;
>>>>>
>>>>> - // Minimal weight should be at least 1
>>>>> - if (std::max(TWeight, FWeight) /
>>>>> - std::max(1u, std::min(TWeight, FWeight)) < Threshold)
>>>>> + if (std::max(TProb, FProb) / Threshold < std::min(TProb, FProb))
>>>>> return PPC::BR_NO_HINT;
>>>>>
>>>>> DEBUG(dbgs() << "Use branch hint for '" << FuncInfo->Fn->getName()
>>>>> << "::"
>>>>> << BB->getName() << "'\n"
>>>>> - << " -> " << TBB->getName() << ": " << TWeight << "\n"
>>>>> - << " -> " << FBB->getName() << ": " << FWeight <<
>>>>> "\n");
>>>>> + << " -> " << TBB->getName() << ": " << TProb << "\n"
>>>>> + << " -> " << FBB->getName() << ": " << FProb << "\n");
>>>>>
>>>>> const BasicBlockSDNode *BBDN = cast<BasicBlockSDNode>(DestMBB);
>>>>>
>>>>> - // If Dest BasicBlock is False-BasicBlock (FBB), swap branch weight,
>>>>> - // because we want 'TWeight' stands for 'branch weight' to Dest
>>>>> BasicBlock
>>>>> + // If Dest BasicBlock is False-BasicBlock (FBB), swap branch
>>>>> probabilities,
>>>>> + // because we want 'TProb' stands for 'branch probability' to Dest
>>>>> BasicBlock
>>>>> if (BBDN->getBasicBlock()->getBasicBlock() != TBB)
>>>>> - std::swap(TWeight, FWeight);
>>>>> + std::swap(TProb, FProb);
>>>>>
>>>>> - return (TWeight > FWeight) ? PPC::BR_TAKEN_HINT :
>>>>> PPC::BR_NONTAKEN_HINT;
>>>>> + return (TProb > FProb) ? PPC::BR_TAKEN_HINT : PPC::BR_NONTAKEN_HINT;
>>>>> }
>>>>>
>>>>> // isOpcWithIntImmediate - This method tests to see if the node is a
>>>>> specific
>>>>>
>>>>> Modified: llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp
>>>>> URL:
>>>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp?rev=256263&r1=256262&r2=256263&view=diff
>>>>>
>>>>> ==============================================================================
>>>>> --- llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp (original)
>>>>> +++ llvm/trunk/lib/Transforms/Scalar/JumpThreading.cpp Tue Dec 22
>>>>> 12:56:14 2015
>>>>> @@ -1636,7 +1636,7 @@ void JumpThreading::UpdateBlockFreqAndEd
>>>>> BFI->setBlockFreq(BB, BBNewFreq.getFrequency());
>>>>>
>>>>> // Collect updated outgoing edges' frequencies from BB and use them
>>>>> to update
>>>>> - // edge weights.
>>>>> + // edge probabilities.
>>>>> SmallVector<uint64_t, 4> BBSuccFreq;
>>>>> for (auto I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
>>>>> auto SuccFreq = (*I == SuccBB)
>>>>> @@ -1645,18 +1645,26 @@ void JumpThreading::UpdateBlockFreqAndEd
>>>>> BBSuccFreq.push_back(SuccFreq.getFrequency());
>>>>> }
>>>>>
>>>>> - // Normalize edge weights in Weights64 so that the sum of them can
>>>>> fit in
>>>>> - BranchProbability::normalizeEdgeWeights(BBSuccFreq.begin(),
>>>>> BBSuccFreq.end());
>>>>> + uint64_t MaxBBSuccFreq =
>>>>> + *std::max_element(BBSuccFreq.begin(), BBSuccFreq.end());
>>>>> + SmallVector<BranchProbability, 4> BBSuccProbs;
>>>>> + for (uint64_t Freq : BBSuccFreq)
>>>>> + BBSuccProbs.push_back(
>>>>> + BranchProbability::getBranchProbability(Freq, MaxBBSuccFreq));
>>>>>
>>>>> - SmallVector<uint32_t, 4> Weights;
>>>>> - for (auto Freq : BBSuccFreq)
>>>>> - Weights.push_back(static_cast<uint32_t>(Freq));
>>>>> -
>>>>> - // Update edge weights in BPI.
>>>>> - for (int I = 0, E = Weights.size(); I < E; I++)
>>>>> - BPI->setEdgeWeight(BB, I, Weights[I]);
>>>>> + // Normalize edge probabilities so that they sum up to one.
>>>>> + BranchProbability::normalizeProbabilities(BBSuccProbs.begin(),
>>>>> + BBSuccProbs.end());
>>>>> +
>>>>> + // Update edge probabilities in BPI.
>>>>> + for (int I = 0, E = BBSuccProbs.size(); I < E; I++)
>>>>> + BPI->setEdgeProbability(BB, I, BBSuccProbs[I]);
>>>>> +
>>>>> + if (BBSuccProbs.size() >= 2) {
>>>>> + SmallVector<uint32_t, 4> Weights;
>>>>> + for (auto Prob : BBSuccProbs)
>>>>> + Weights.push_back(Prob.getNumerator());
>>>>>
>>>>> - if (Weights.size() >= 2) {
>>>>> auto TI = BB->getTerminator();
>>>>> TI->setMetadata(
>>>>> LLVMContext::MD_prof,
>>>>>
>>>>> Modified: llvm/trunk/test/Analysis/BranchProbabilityInfo/noreturn.ll
>>>>> URL:
>>>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/BranchProbabilityInfo/noreturn.ll?rev=256263&r1=256262&r2=256263&view=diff
>>>>>
>>>>> ==============================================================================
>>>>> --- llvm/trunk/test/Analysis/BranchProbabilityInfo/noreturn.ll
>>>>> (original)
>>>>> +++ llvm/trunk/test/Analysis/BranchProbabilityInfo/noreturn.ll Tue Dec
>>>>> 22 12:56:14 2015
>>>>> @@ -26,11 +26,11 @@ entry:
>>>>> i32 2, label %case_b
>>>>> i32 3, label %case_c
>>>>> i32 4, label %case_d]
>>>>> -; CHECK: edge entry -> exit probability is 0x7fffe000 / 0x80000000 =
>>>>> 100.00% [HOT edge]
>>>>> -; CHECK: edge entry -> case_a probability is 0x00000800 / 0x80000000
>>>>> = 0.00%
>>>>> -; CHECK: edge entry -> case_b probability is 0x00000800 / 0x80000000
>>>>> = 0.00%
>>>>> -; CHECK: edge entry -> case_c probability is 0x00000800 / 0x80000000
>>>>> = 0.00%
>>>>> -; CHECK: edge entry -> case_d probability is 0x00000800 / 0x80000000
>>>>> = 0.00%
>>>>> +; CHECK: edge entry -> exit probability is 0x7ffff800 / 0x80000000 =
>>>>> 100.00% [HOT edge]
>>>>> +; CHECK: edge entry -> case_a probability is 0x00000200 / 0x80000000
>>>>> = 0.00%
>>>>> +; CHECK: edge entry -> case_b probability is 0x00000200 / 0x80000000
>>>>> = 0.00%
>>>>> +; CHECK: edge entry -> case_c probability is 0x00000200 / 0x80000000
>>>>> = 0.00%
>>>>> +; CHECK: edge entry -> case_d probability is 0x00000200 / 0x80000000
>>>>> = 0.00%
>>>>>
>>>>> case_a:
>>>>> br label %case_b
>>>>>
>>>>> Modified:
>>>>> llvm/trunk/test/Transforms/JumpThreading/update-edge-weight.ll
>>>>> URL:
>>>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/JumpThreading/update-edge-weight.ll?rev=256263&r1=256262&r2=256263&view=diff
>>>>>
>>>>> ==============================================================================
>>>>> --- llvm/trunk/test/Transforms/JumpThreading/update-edge-weight.ll
>>>>> (original)
>>>>> +++ llvm/trunk/test/Transforms/JumpThreading/update-edge-weight.ll Tue
>>>>> Dec 22 12:56:14 2015
>>>>> @@ -2,7 +2,7 @@
>>>>>
>>>>> ; Test if edge weights are properly updated after jump threading.
>>>>>
>>>>> -; CHECK: !2 = !{!"branch_weights", i32 22, i32 7}
>>>>> +; CHECK: !2 = !{!"branch_weights", i32 1629125526, i32 518358122}
>>>>>
>>>>> define void @foo(i32 %n) !prof !0 {
>>>>> entry:
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> llvm-commits mailing list
>>>>> llvm-commits at lists.llvm.org
>>>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>>>>>
>>>>
>>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151222/8f371f9e/attachment.html>
More information about the llvm-commits
mailing list