[llvm] [LoopUnroll] Fix freqs for unconditional latches: N>2, fast (PR #182404)
Joel E. Denny via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 29 18:16:13 PDT 2026
================
@@ -625,13 +639,120 @@ static void fixProbContradiction(UnrollLoopOptions ULO,
return Prob;
};
+ // Compute the probability required at CondLatches[ComputeIdx] to get as close
+ // as possible to FreqDesired without replacing probabilities elsewhere in
+ // CondLatches. Return {Prob, Freq} where 0 <= Prob <= 1 and Freq is the new
+ // frequency.
+ auto ComputeProb = [&](unsigned ComputeIdx) -> std::pair<double, double> {
+ assert(ComputeIdx < CondLatches.size());
+
+ // Accumulate the frequency from before ComputeIdx into FreqBeforeCompute,
+ // and accumulate the rest in Freq without yet multiplying the latter by any
+ // probability for ComputeIdx (i.e., treat it as 1 for now).
+ double ProbReaching = 1; // p^0
+ double Freq = IterCounts[0]; // c_0*p^0
+ double FreqBeforeCompute;
+ for (unsigned I = 0, E = CondLatches.size(); I < E; ++I) {
+ // Get the branch probability for CondLatches[I].
+ double Prob;
+ if (I == ComputeIdx) {
+ FreqBeforeCompute = Freq;
+ Freq = 0;
+ Prob = 1;
+ } else {
+ Prob = GetProb(I);
+ }
+ ProbReaching *= Prob; // p^(I+1)
+ Freq += IterCounts[I + 1] * ProbReaching; // c_(I+1)*p^(I+1)
+ }
+
+ // Compute the required probability, and limit it to a valid probability (0
+ // <= p <= 1). See the Freq formula below for how to derive the ProbCompute
+ // formula.
+ double ProbReachingBackedge = CompletelyUnroll ? 0 : ProbReaching;
+ double ProbComputeNumerator = FreqDesired - FreqBeforeCompute;
+ double ProbComputeDenominator = Freq + FreqDesired * ProbReachingBackedge;
+ double ProbCompute;
----------------
jdenny-ornl wrote:
Done. The assert is in the new `SetProb` call's `BranchProbability::getBranchProbability` call. Is that sufficient?
https://github.com/llvm/llvm-project/pull/182404
More information about the llvm-commits
mailing list