Skip to content

Conversation

@mtrofin
Copy link
Member

@mtrofin mtrofin commented Oct 21, 2025

We don't have sufficient information to know when the versioned (or unversioned) loop variant will be taken, so we mark the branch as having "unknown" probabilities.

Issue #147390

Copy link
Member Author

mtrofin commented Oct 21, 2025

@github-actions
Copy link

github-actions bot commented Oct 21, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@mtrofin mtrofin force-pushed the users/mtrofin/10-21-_lver_profcheck_explicitly_set_unknown_branch_weights_for_the_versioned/unversioned_selector branch from 6810b9e to 63c8f11 Compare October 21, 2025 22:28
@mtrofin mtrofin marked this pull request as ready for review October 21, 2025 22:33
@mtrofin mtrofin force-pushed the users/mtrofin/10-21-_lver_profcheck_explicitly_set_unknown_branch_weights_for_the_versioned/unversioned_selector branch from 63c8f11 to 7b55a08 Compare October 21, 2025 22:34
@llvmbot
Copy link
Member

llvmbot commented Oct 21, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Mircea Trofin (mtrofin)

Changes

We don't have sufficient information to know when the versioned (or unversioned) loop variant will be taken, so we mark the branch as having "unknown" probabilities.

Issue #147390


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/LoopVersioning.cpp (+8-2)
  • (modified) llvm/test/Transforms/LoopDistribute/basic-with-memchecks.ll (+3-2)
diff --git a/llvm/lib/Transforms/Utils/LoopVersioning.cpp b/llvm/lib/Transforms/Utils/LoopVersioning.cpp
index ec2e6c1ab796b..4786819d18fa4 100644
--- a/llvm/lib/Transforms/Utils/LoopVersioning.cpp
+++ b/llvm/lib/Transforms/Utils/LoopVersioning.cpp
@@ -23,6 +23,7 @@
 #include "llvm/IR/Dominators.h"
 #include "llvm/IR/MDBuilder.h"
 #include "llvm/IR/PassManager.h"
+#include "llvm/IR/ProfDataUtils.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
 #include "llvm/Transforms/Utils/Cloning.h"
@@ -109,8 +110,13 @@ void LoopVersioning::versionLoop(
   // Insert the conditional branch based on the result of the memchecks.
   Instruction *OrigTerm = RuntimeCheckBB->getTerminator();
   Builder.SetInsertPoint(OrigTerm);
-  Builder.CreateCondBr(RuntimeCheck, NonVersionedLoop->getLoopPreheader(),
-                       VersionedLoop->getLoopPreheader());
+  auto *BI =
+      Builder.CreateCondBr(RuntimeCheck, NonVersionedLoop->getLoopPreheader(),
+                           VersionedLoop->getLoopPreheader());
+  // We don't know what the probability of executing the versioned vs the
+  // unversioned variants is.
+  setExplicitlyUnknownBranchWeightsIfProfiled(
+      *BI, *BI->getParent()->getParent(), DEBUG_TYPE);
   OrigTerm->eraseFromParent();
 
   // The loops merge in the original exit block.  This is now dominated by the
diff --git a/llvm/test/Transforms/LoopDistribute/basic-with-memchecks.ll b/llvm/test/Transforms/LoopDistribute/basic-with-memchecks.ll
index 97ea2c6708dad..2828882afe779 100644
--- a/llvm/test/Transforms/LoopDistribute/basic-with-memchecks.ll
+++ b/llvm/test/Transforms/LoopDistribute/basic-with-memchecks.ll
@@ -28,7 +28,7 @@ target triple = "x86_64-apple-macosx10.10.0"
 @E = common global ptr null, align 8
 
 ; CHECK-LABEL: @f(
-define void @f() {
+define void @f() !prof !{!"function_entry_count", i32 10} {
 entry:
   %a = load ptr, ptr @A, align 8
   %b = load ptr, ptr @B, align 8
@@ -55,7 +55,7 @@ entry:
 ; CHECK:     = icmp
 
 ; CHECK-NOT: = icmp
-; CHECK:     br i1 %conflict.rdx15, label %for.body.ph.lver.orig, label %for.body.ph.ldist1
+; CHECK:     br i1 %conflict.rdx15, label %for.body.ph.lver.orig, label %for.body.ph.ldist1, !prof ![[PROF1:[0-9]]]
 
 ; The non-distributed loop that the memchecks fall back on.
 
@@ -289,3 +289,4 @@ attributes #1 = { nounwind convergent }
 
 !0 = distinct !{!0, !1}
 !1 = !{!"llvm.loop.distribute.enable", i1 true}
+; CHECK: ![[PROF1]] = !{!"unknown", !"loop-versioning"}

@mtrofin mtrofin force-pushed the users/mtrofin/10-21-_lver_profcheck_explicitly_set_unknown_branch_weights_for_the_versioned/unversioned_selector branch from 7b55a08 to 577971f Compare October 29, 2025 15:02
@mtrofin mtrofin force-pushed the users/mtrofin/10-21-_slu_profcheck_propagate_profile_for_branches_on_injected_conditions branch 2 times, most recently from 54eacf7 to f696a5d Compare October 30, 2025 05:30
@mtrofin mtrofin force-pushed the users/mtrofin/10-21-_lver_profcheck_explicitly_set_unknown_branch_weights_for_the_versioned/unversioned_selector branch from 577971f to e745aaf Compare October 30, 2025 05:30
@mtrofin mtrofin force-pushed the users/mtrofin/10-21-_slu_profcheck_propagate_profile_for_branches_on_injected_conditions branch from f696a5d to c34f582 Compare October 30, 2025 15:08
@mtrofin mtrofin force-pushed the users/mtrofin/10-21-_lver_profcheck_explicitly_set_unknown_branch_weights_for_the_versioned/unversioned_selector branch from e745aaf to 452c2f5 Compare October 30, 2025 15:09
@mtrofin mtrofin force-pushed the users/mtrofin/10-21-_slu_profcheck_propagate_profile_for_branches_on_injected_conditions branch from c34f582 to a2080ca Compare October 30, 2025 17:41
@mtrofin mtrofin force-pushed the users/mtrofin/10-21-_lver_profcheck_explicitly_set_unknown_branch_weights_for_the_versioned/unversioned_selector branch 2 times, most recently from ac02f48 to 9d2017c Compare October 31, 2025 22:47
@mtrofin mtrofin force-pushed the users/mtrofin/10-21-_slu_profcheck_propagate_profile_for_branches_on_injected_conditions branch 2 times, most recently from 13775b3 to cf1d8b6 Compare October 31, 2025 22:52
@mtrofin mtrofin force-pushed the users/mtrofin/10-21-_lver_profcheck_explicitly_set_unknown_branch_weights_for_the_versioned/unversioned_selector branch from 9d2017c to 19c883d Compare October 31, 2025 22:52
// We don't know what the probability of executing the versioned vs the
// unversioned variants is.
setExplicitlyUnknownBranchWeightsIfProfiled(
*BI, *BI->getParent()->getParent(), DEBUG_TYPE);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
*BI, *BI->getParent()->getParent(), DEBUG_TYPE);
*BI, *BI->getFunction(), DEBUG_TYPE);

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, looks like the argument can be removed alltogether #166028

Copy link
Contributor

Choose a reason for hiding this comment

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

Or not, looks like InstCombine passes disconnected instructions

@mtrofin mtrofin force-pushed the users/mtrofin/10-21-_slu_profcheck_propagate_profile_for_branches_on_injected_conditions branch from cf1d8b6 to 7a5b8b0 Compare November 3, 2025 18:56
@mtrofin mtrofin force-pushed the users/mtrofin/10-21-_lver_profcheck_explicitly_set_unknown_branch_weights_for_the_versioned/unversioned_selector branch from 19c883d to a5d8823 Compare November 3, 2025 18:56
@mtrofin mtrofin force-pushed the users/mtrofin/10-21-_slu_profcheck_propagate_profile_for_branches_on_injected_conditions branch from 7a5b8b0 to 3778df5 Compare November 3, 2025 22:40
@mtrofin mtrofin force-pushed the users/mtrofin/10-21-_lver_profcheck_explicitly_set_unknown_branch_weights_for_the_versioned/unversioned_selector branch from a5d8823 to df8cbd1 Compare November 3, 2025 22:41
@mtrofin mtrofin force-pushed the users/mtrofin/10-21-_slu_profcheck_propagate_profile_for_branches_on_injected_conditions branch from 3778df5 to d8f5eb6 Compare November 4, 2025 01:40
@mtrofin mtrofin force-pushed the users/mtrofin/10-21-_lver_profcheck_explicitly_set_unknown_branch_weights_for_the_versioned/unversioned_selector branch 2 times, most recently from 2e48d8d to 7a7a7f2 Compare November 4, 2025 02:12
@mtrofin mtrofin force-pushed the users/mtrofin/10-21-_slu_profcheck_propagate_profile_for_branches_on_injected_conditions branch from d8f5eb6 to 4e408ee Compare November 4, 2025 02:12
@mtrofin mtrofin force-pushed the users/mtrofin/10-21-_lver_profcheck_explicitly_set_unknown_branch_weights_for_the_versioned/unversioned_selector branch from 7a7a7f2 to 3cbf2c9 Compare November 4, 2025 02:15
@mtrofin mtrofin force-pushed the users/mtrofin/10-21-_slu_profcheck_propagate_profile_for_branches_on_injected_conditions branch from 4e408ee to 0bf540a Compare November 4, 2025 02:15
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.

5 participants