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

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


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

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

>From 8f3e0a454d4f654bc22cb99ce97d80c9fc834e88 Mon Sep 17 00:00:00 2001
From: Dmitry Nechitaev <nechitaev.da at phystech.edu>
Date: Mon, 24 Jun 2024 22:21:23 +0300
Subject: [PATCH] [PGO] Fix branch weights overflow

---
 llvm/lib/Transforms/IPO/SampleProfile.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

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



More information about the llvm-commits mailing list