[clang] [LoopPeeling] Fix weights updating of peeled off branches (PR #70094)

Matthias Braun via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 30 13:43:57 PDT 2023


================
@@ -636,9 +636,13 @@ static void updateBranchWeights(Instruction *Term, WeightInfo &Info) {
                     MDB.createBranchWeights(Info.Weights));
   for (auto [Idx, SubWeight] : enumerate(Info.SubWeights))
     if (SubWeight != 0)
-      Info.Weights[Idx] = Info.Weights[Idx] > SubWeight
-                              ? Info.Weights[Idx] - SubWeight
-                              : 1;
+      // Don't set the probability of taking the edge from latch to loop header
+      // to less than 1, as this could significantly reduce the loop's hotness,
+      // which would be incorrect in the case of underestimating the trip count.
+      Info.Weights[Idx] =
+          Info.Weights[Idx] > SubWeight
+              ? std::max(Info.Weights[Idx] - SubWeight, SubWeight)
+              : SubWeight;
----------------
MatzeB wrote:

```suggestion
      Info.Weights[Idx] =
          Info.Weights[Idx] > 2 * SubWeight
              ? Info.Weights[Idx] - SubWeight
              : SubWeight;
```

https://github.com/llvm/llvm-project/pull/70094


More information about the cfe-commits mailing list