[llvm] [SLU][profcheck] Estimate branch weights in partial unswitch cases (PR #164035)

Joel E. Denny via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 23 11:26:54 PDT 2025


================
@@ -271,10 +278,33 @@ static bool areLoopExitPHIsLoopInvariant(const Loop &L,
 /// Copy a set of loop invariant values \p ToDuplicate and insert them at the
 /// end of \p BB and conditionally branch on the copied condition. We only
 /// branch on a single value.
+/// We attempt to estimate the profile of the resulting conditional branch from
+/// \p ComputeProfFrom, which is the original conditional branch we're
+/// unswitching.
 static void buildPartialUnswitchConditionalBranch(
     BasicBlock &BB, ArrayRef<Value *> Invariants, bool Direction,
     BasicBlock &UnswitchedSucc, BasicBlock &NormalSucc, bool InsertFreeze,
-    const Instruction *I, AssumptionCache *AC, const DominatorTree &DT) {
+    const Instruction *I, AssumptionCache *AC, const DominatorTree &DT,
+    const BranchInst &ComputeProfFrom) {
+
+  SmallVector<uint32_t> BranchWeights;
+  bool HasBranchWeights = EstimateProfile && !ProfcheckDisableMetadataFixes &&
+                          extractBranchWeights(ComputeProfFrom, BranchWeights);
+  // If Direction is true, that means we had a disjunction and that the "true"
+  // case exits. The probability of the disjunction of the subset of terms is at
+  // most as high as the original one. So, if the probability is higher than the
+  // one we'd assign in absence of a profile (i.e. 0.5), we will use 0.5,
+  // but if it's lower, we will use the original probability.
+  // Coversely, if Direction is false, that means we had a conjunction, and the
----------------
jdenny-ornl wrote:

```suggestion
  // Conversely, if Direction is false, that means we had a conjunction, and the
```

https://github.com/llvm/llvm-project/pull/164035


More information about the llvm-commits mailing list