Source code

Revision control

Copy as Markdown

Other Tools

From 9e3750d90195f63849228d388780362443840acf Mon Sep 17 00:00:00 2001
From: Mike Hommey <mh@glandium.org>
Date: Tue, 4 Jun 2024 15:29:52 +0900
Subject: [PATCH] Revert "LoopRotate: Add code to update branch weights"
This reverts commit 5d7f84ee17f3f601c49f6124a3a51e557de3ab53.
---
.../Transforms/Utils/LoopRotationUtils.cpp | 136 +-----------------
1 file changed, 4 insertions(+), 132 deletions(-)
diff --git a/llvm/lib/Transforms/Utils/LoopRotationUtils.cpp b/llvm/lib/Transforms/Utils/LoopRotationUtils.cpp
index 504f4430dc2c..49446db4d1d3 100644
--- a/llvm/lib/Transforms/Utils/LoopRotationUtils.cpp
+++ b/llvm/lib/Transforms/Utils/LoopRotationUtils.cpp
@@ -25,8 +25,6 @@
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/IntrinsicInst.h"
-#include "llvm/IR/MDBuilder.h"
-#include "llvm/IR/ProfDataUtils.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
@@ -52,9 +50,6 @@ static cl::opt<bool>
cl::desc("Allow loop rotation multiple times in order to reach "
"a better latch exit"));
-// Probability that a rotated loop has zero trip count / is never entered.
-static constexpr uint32_t ZeroTripCountWeights[] = {1, 127};
-
namespace {
/// A simple loop rotation transformation.
class LoopRotate {
@@ -273,123 +268,6 @@ static bool canRotateDeoptimizingLatchExit(Loop *L) {
return false;
}
-static void updateBranchWeights(BranchInst &PreHeaderBI, BranchInst &LoopBI,
- bool HasConditionalPreHeader,
- bool SuccsSwapped) {
- MDNode *WeightMD = getBranchWeightMDNode(PreHeaderBI);
- if (WeightMD == nullptr)
- return;
-
- // LoopBI should currently be a clone of PreHeaderBI with the same
- // metadata. But we double check to make sure we don't have a degenerate case
- // where instsimplify changed the instructions.
- if (WeightMD != getBranchWeightMDNode(LoopBI))
- return;
-
- SmallVector<uint32_t, 2> Weights;
- extractFromBranchWeightMD(WeightMD, Weights);
- if (Weights.size() != 2)
- return;
- uint32_t OrigLoopExitWeight = Weights[0];
- uint32_t OrigLoopBackedgeWeight = Weights[1];
-
- if (SuccsSwapped)
- std::swap(OrigLoopExitWeight, OrigLoopBackedgeWeight);
-
- // Update branch weights. Consider the following edge-counts:
- //
- // | |-------- |
- // V V | V
- // Br i1 ... | Br i1 ...
- // | | | | |
- // x| y| | becomes: | y0| |-----
- // V V | | V V |
- // Exit Loop | | Loop |
- // | | | Br i1 ... |
- // ----- | | | |
- // x0| x1| y1 | |
- // V V ----
- // Exit
- //
- // The following must hold:
- // - x == x0 + x1 # counts to "exit" must stay the same.
- // - y0 == x - x0 == x1 # how often loop was entered at all.
- // - y1 == y - y0 # How often loop was repeated (after first iter.).
- //
- // We cannot generally deduce how often we had a zero-trip count loop so we
- // have to make a guess for how to distribute x among the new x0 and x1.
-
- uint32_t ExitWeight0; // aka x0
- uint32_t ExitWeight1; // aka x1
- uint32_t EnterWeight; // aka y0
- uint32_t LoopBackWeight; // aka y1
- if (OrigLoopExitWeight > 0 && OrigLoopBackedgeWeight > 0) {
- ExitWeight0 = 0;
- if (HasConditionalPreHeader) {
- // Here we cannot know how many 0-trip count loops we have, so we guess:
- if (OrigLoopBackedgeWeight >= OrigLoopExitWeight) {
- // If the loop count is bigger than the exit count then we set
- // probabilities as if 0-trip count nearly never happens.
- ExitWeight0 = ZeroTripCountWeights[0];
- // Scale up counts if necessary so we can match `ZeroTripCountWeights`
- // for the `ExitWeight0`:`ExitWeight1` (aka `x0`:`x1` ratio`) ratio.
- while (OrigLoopExitWeight < ZeroTripCountWeights[1] + ExitWeight0) {
- // ... but don't overflow.
- uint32_t const HighBit = uint32_t{1} << (sizeof(uint32_t) * 8 - 1);
- if ((OrigLoopBackedgeWeight & HighBit) != 0 ||
- (OrigLoopExitWeight & HighBit) != 0)
- break;
- OrigLoopBackedgeWeight <<= 1;
- OrigLoopExitWeight <<= 1;
- }
- } else {
- // If there's a higher exit-count than backedge-count then we set
- // probabilities as if there are only 0-trip and 1-trip cases.
- ExitWeight0 = OrigLoopExitWeight - OrigLoopBackedgeWeight;
- }
- }
- ExitWeight1 = OrigLoopExitWeight - ExitWeight0;
- EnterWeight = ExitWeight1;
- LoopBackWeight = OrigLoopBackedgeWeight - EnterWeight;
- } else if (OrigLoopExitWeight == 0) {
- if (OrigLoopBackedgeWeight == 0) {
- // degenerate case... keep everything zero...
- ExitWeight0 = 0;
- ExitWeight1 = 0;
- EnterWeight = 0;
- LoopBackWeight = 0;
- } else {
- // Special case "LoopExitWeight == 0" weights which behaves like an
- // endless where we don't want loop-enttry (y0) to be the same as
- // loop-exit (x1).
- ExitWeight0 = 0;
- ExitWeight1 = 0;
- EnterWeight = 1;
- LoopBackWeight = OrigLoopBackedgeWeight;
- }
- } else {
- // loop is never entered.
- assert(OrigLoopBackedgeWeight == 0 && "remaining case is backedge zero");
- ExitWeight0 = 1;
- ExitWeight1 = 1;
- EnterWeight = 0;
- LoopBackWeight = 0;
- }
-
- const uint32_t LoopBIWeights[] = {
- SuccsSwapped ? LoopBackWeight : ExitWeight1,
- SuccsSwapped ? ExitWeight1 : LoopBackWeight,
- };
- setBranchWeights(LoopBI, LoopBIWeights);
- if (HasConditionalPreHeader) {
- const uint32_t PreHeaderBIWeights[] = {
- SuccsSwapped ? EnterWeight : ExitWeight0,
- SuccsSwapped ? ExitWeight0 : EnterWeight,
- };
- setBranchWeights(PreHeaderBI, PreHeaderBIWeights);
- }
-}
-
/// Rotate loop LP. Return true if the loop is rotated.
///
/// \param SimplifiedLatch is true if the latch was just folded into the final
@@ -509,8 +387,7 @@ bool LoopRotate::rotateLoop(Loop *L, bool SimplifiedLatch) {
// loop. Otherwise loop is not suitable for rotation.
BasicBlock *Exit = BI->getSuccessor(0);
BasicBlock *NewHeader = BI->getSuccessor(1);
- bool BISuccsSwapped = L->contains(Exit);
- if (BISuccsSwapped)
+ if (L->contains(Exit))
std::swap(Exit, NewHeader);
assert(NewHeader && "Unable to determine new loop header");
assert(L->contains(NewHeader) && !L->contains(Exit) &&
@@ -814,14 +691,9 @@ bool LoopRotate::rotateLoop(Loop *L, bool SimplifiedLatch) {
// to split as many edges.
BranchInst *PHBI = cast<BranchInst>(OrigPreheader->getTerminator());
assert(PHBI->isConditional() && "Should be clone of BI condbr!");
- const Value *Cond = PHBI->getCondition();
- const bool HasConditionalPreHeader =
- !isa<ConstantInt>(Cond) ||
- PHBI->getSuccessor(cast<ConstantInt>(Cond)->isZero()) != NewHeader;
-
- updateBranchWeights(*PHBI, *BI, HasConditionalPreHeader, BISuccsSwapped);
-
- if (HasConditionalPreHeader) {
+ if (!isa<ConstantInt>(PHBI->getCondition()) ||
+ PHBI->getSuccessor(cast<ConstantInt>(PHBI->getCondition())->isZero()) !=
+ NewHeader) {
// The conditional branch can't be folded, handle the general case.
// Split edges as necessary to preserve LoopSimplify form.
--
2.45.1.2.gf9b0626531