Skip to content

[SimplifyCFG] Fix uint32_t overflow in cbranch to cbranch merge prevention check. #72329

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

Merged
merged 3 commits into from
Nov 15, 2023

Conversation

vpykhtin
Copy link
Contributor

This fixes #72323.

Resulted from f054947

@llvmbot
Copy link
Member

llvmbot commented Nov 15, 2023

@llvm/pr-subscribers-llvm-transforms

Author: Valery Pykhtin (vpykhtin)

Changes

This fixes #72323.

Resulted from f054947


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/SimplifyCFG.cpp (+2-2)
  • (modified) llvm/test/Transforms/SimplifyCFG/branch-cond-dont-merge.ll (+20)
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 6009558efca06af..f15ab6858401fc6 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -4351,10 +4351,10 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
   SmallVector<uint32_t, 2> PredWeights;
   if (!PBI->getMetadata(LLVMContext::MD_unpredictable) &&
       extractBranchWeights(*PBI, PredWeights) &&
-      (PredWeights[0] + PredWeights[1]) != 0) {
+      ((uint64_t)PredWeights[0] + PredWeights[1]) != 0) {
 
     BranchProbability CommonDestProb = BranchProbability::getBranchProbability(
-        PredWeights[PBIOp], PredWeights[0] + PredWeights[1]);
+        PredWeights[PBIOp], (uint64_t)PredWeights[0] + PredWeights[1]);
 
     BranchProbability Likely = TTI.getPredictableBranchThreshold();
     if (CommonDestProb >= Likely)
diff --git a/llvm/test/Transforms/SimplifyCFG/branch-cond-dont-merge.ll b/llvm/test/Transforms/SimplifyCFG/branch-cond-dont-merge.ll
index 5c21f163826ee25..0f540007b242e29 100644
--- a/llvm/test/Transforms/SimplifyCFG/branch-cond-dont-merge.ll
+++ b/llvm/test/Transforms/SimplifyCFG/branch-cond-dont-merge.ll
@@ -79,6 +79,26 @@ exit:
   ret void
 }
 
+define void @uint32_overflow_test(i1 %arg, i1 %arg1) {
+; CHECK-LABEL: @uint32_overflow_test(
+; CHECK-NEXT:  bb:
+; CHECK-NEXT:    [[BRMERGE:%.*]] = select i1 [[ARG:%.*]], i1 true, i1 [[ARG1:%.*]]
+; CHECK-NEXT:    ret void
+;
+bb:
+  br i1 %arg, label %bb4, label %bb2, !prof !3
+
+bb2:
+  br i1 %arg1, label %bb4, label %bb3
+
+bb3:
+  br label %bb4
+
+bb4:
+  ret void
+}
+
 !0 = !{!"branch_weights", i32 1, i32 1000}
 !1 = !{!"branch_weights", i32 1000, i32 1}
 !2 = !{!"branch_weights", i32 3, i32 2}
+!3 = !{!"branch_weights", i32 -258677585, i32 -1212131848}

@vpykhtin vpykhtin requested a review from nikic November 15, 2023 00:27
Copy link
Contributor

@DaniilSuchkov DaniilSuchkov left a comment

Choose a reason for hiding this comment

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

Looks good.

@vpykhtin vpykhtin merged commit b93ff3e into llvm:main Nov 15, 2023
zahiraam pushed a commit to zahiraam/llvm-project that referenced this pull request Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SimplifyCFG: llvm::BranchProbability::getBranchProbability: Assertion `Numerator <= Denominator && "Probability cannot be bigger than 1!"' failed.
4 participants