Skip to content

[PGO] Fix branch weights overflow #96541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

Nechda
Copy link
Contributor

@Nechda Nechda commented Jun 24, 2024

This MR fixes the issue described in #88361

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added PGO Profile Guided Optimizations llvm:transforms labels Jun 24, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 24, 2024

@llvm/pr-subscribers-pgo

Author: Dmitry Nechitaev (Nechda)

Changes

This MR fixes the issue described in #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 --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

@llvmbot
Copy link
Member

llvmbot commented Jun 24, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Dmitry Nechitaev (Nechda)

Changes

This MR fixes the issue described in #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 --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

@Nechda
Copy link
Contributor Author

Nechda commented Jun 25, 2024

@dtcxzyw

Copy link
Contributor

@david-xl david-xl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible to add a test case?

@MatzeB
Copy link
Contributor

MatzeB commented Jun 25, 2024

This was already fixed in #90217 I believe and this change doesn't add anything on top of it, or am I missing something?

@Nechda
Copy link
Contributor Author

Nechda commented Jun 25, 2024

This was already fixed in #90217 I believe and this change doesn't add anything on top of it, or am I missing something?

Hmm, seems you are right, I'll close this MR & Issue.

@Nechda Nechda closed this Jun 25, 2024
@Nechda Nechda deleted the spgo-fix-uint32-overflow branch February 17, 2025 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:transforms PGO Profile Guided Optimizations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants