diff options
author | Haohai Wen <haohai.wen@intel.com> | 2024-03-28 20:07:15 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-28 20:07:15 +0800 |
commit | 896037c75ace929327e5b0bf5832157f9d81e6e7 (patch) | |
tree | d6675288240ea4544ffdea1f20273cdc54e03002 /llvm/lib/Transforms/Utils/LoopRotationUtils.cpp | |
parent | daa755ba7b7fe11e078f1e6f43d446234023f859 (diff) | |
download | llvm-896037c75ace929327e5b0bf5832157f9d81e6e7.zip llvm-896037c75ace929327e5b0bf5832157f9d81e6e7.tar.gz llvm-896037c75ace929327e5b0bf5832157f9d81e6e7.tar.bz2 |
[LoopRotate] Set loop back edge weight to not less than exit weight (#86496)
Branch weight from sample-based PGO may be not inaccurate due to
sampling. If the loop body must be executed, then origin loop back
edge weight must be not less than exit weight.
Diffstat (limited to 'llvm/lib/Transforms/Utils/LoopRotationUtils.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/LoopRotationUtils.cpp | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/Utils/LoopRotationUtils.cpp b/llvm/lib/Transforms/Utils/LoopRotationUtils.cpp index bc67117..0f55af3 100644 --- a/llvm/lib/Transforms/Utils/LoopRotationUtils.cpp +++ b/llvm/lib/Transforms/Utils/LoopRotationUtils.cpp @@ -347,9 +347,19 @@ static void updateBranchWeights(BranchInst &PreHeaderBI, BranchInst &LoopBI, // probabilities as if there are only 0-trip and 1-trip cases. ExitWeight0 = OrigLoopExitWeight - OrigLoopBackedgeWeight; } + } else { + // Theoretically, if the loop body must be executed at least once, the + // backedge count must be not less than exit count. However the branch + // weight collected by sampling-based PGO may be not very accurate due to + // sampling. Therefore this workaround is required here to avoid underflow + // of unsigned in following update of branch weight. + if (OrigLoopExitWeight > OrigLoopBackedgeWeight) + OrigLoopBackedgeWeight = OrigLoopExitWeight; } + assert(OrigLoopExitWeight >= ExitWeight0 && "Bad branch weight"); ExitWeight1 = OrigLoopExitWeight - ExitWeight0; EnterWeight = ExitWeight1; + assert(OrigLoopBackedgeWeight >= EnterWeight && "Bad branch weight"); LoopBackWeight = OrigLoopBackedgeWeight - EnterWeight; } else if (OrigLoopExitWeight == 0) { if (OrigLoopBackedgeWeight == 0) { |