[PATCH] D124302: [llvm][misexpect] Replace assertion with early return

Paul Kirth via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 22 14:32:07 PDT 2022


paulkirth created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
paulkirth requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Misexpect diagnostcs shoud not prevent compilation from succeeding, and the
assertion is insuffient to prevent division by zero in release builds.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D124302

Files:
  llvm/lib/Transforms/Utils/MisExpect.cpp


Index: llvm/lib/Transforms/Utils/MisExpect.cpp
===================================================================
--- llvm/lib/Transforms/Utils/MisExpect.cpp
+++ llvm/lib/Transforms/Utils/MisExpect.cpp
@@ -185,8 +185,11 @@
   uint64_t TotalBranchWeight =
       LikelyBranchWeight + (UnlikelyBranchWeight * NumUnlikelyTargets);
 
-  assert((TotalBranchWeight >= LikelyBranchWeight) && (TotalBranchWeight > 0) &&
-         "TotalBranchWeight is less than the Likely branch weight");
+  // We cannot calculate branch probability if either of these invariants aren't
+  // met. However, MisExpect diagnostics should not prevent code from compiling,
+  // so we simply forgo emitting diagnostics here, and return early.
+  if ((TotalBranchWeight == 0) || (TotalBranchWeight <= LikelyBranchWeight))
+    return;
 
   // To determine our threshold value we need to obtain the branch probability
   // for the weights added by llvm.expect and use that proportion to calculate


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124302.424614.patch
Type: text/x-patch
Size: 970 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220422/65c2bbb7/attachment.bin>


More information about the llvm-commits mailing list