[llvm] [PGO] Fix branch weights overflow (PR #96541)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 24 12:29:28 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Dmitry Nechitaev (Nechda)

<details>
<summary>Changes</summary>

This MR fixes the issue described in https://github.com/llvm/llvm-project/issues/88361

---
Full diff: https://github.com/llvm/llvm-project/pull/96541.diff


1 Files Affected:

- (modified) llvm/lib/Transforms/IPO/SampleProfile.cpp (+2-2) 


``````````diff
diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp
index 61078c4194b81..8e6f982373a6a 100644
--- a/llvm/lib/Transforms/IPO/SampleProfile.cpp
+++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp
@@ -1711,9 +1711,9 @@ void SampleProfileLoader::generateMDProfMetadata(Function &F) {
       // Use uint32_t saturated arithmetic to adjust the incoming weights,
       // if needed. Sample counts in profiles are 64-bit unsigned values,
       // but internally branch weights are expressed as 32-bit values.
-      if (Weight > std::numeric_limits<uint32_t>::max()) {
+      if (Weight >= std::numeric_limits<uint32_t>::max()) {
         LLVM_DEBUG(dbgs() << " (saturated due to uint32_t overflow)\n");
-        Weight = std::numeric_limits<uint32_t>::max();
+        Weight = std::numeric_limits<uint32_t>::max() - 1;
       }
       if (!SampleProfileUseProfi) {
         // Weight is added by one to avoid propagation errors introduced by

``````````

</details>


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


More information about the llvm-commits mailing list