[llvm] [LoopUnroll] Bound automatic full-unroll cloning work (PR #211683)

Justin Fargnoli via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 29 20:07:08 PDT 2026


================
@@ -918,6 +926,19 @@ static std::optional<unsigned> shouldFullUnroll(
   // When computing the unrolled size, note that BEInsns are not replicated
   // like the rest of the loop body.
   uint64_t UnrolledSize = UCE.getUnrolledLoopSize(UP, FullUnrollTripCount);
+  uint64_t LoopInstructionCount = 0;
+  for (const BasicBlock *BB : L->blocks())
+    LoopInstructionCount += BB->size();
+  uint64_t AdditionalCopies = FullUnrollTripCount - 1;
+  if (!ExplicitUnroll && UP.Threshold != NoThreshold && AdditionalCopies &&
+      LoopInstructionCount >
+          UP.FullUnrollMaxClonedInstructions / AdditionalCopies) {
+    LLVM_DEBUG(dbgs().indent(2)
+               << "Not unrolling: estimated cloned instructions exceed "
+               << UP.FullUnrollMaxClonedInstructions << ".\n");
+    return std::nullopt;
+  }
----------------
justinfargnoli wrote:

I agree it doesn't make sense to add this in the `analyzeLoopUnrollCost` function. But what about adding this to the code that's only triggered when `analyzeLoopUnrollCost` is successful? e.g.

```
  if (std::optional<EstimatedUnrollCost> Cost = analyzeLoopUnrollCost(
          L, FullUnrollTripCount, DT, SE, EphValues, TTI,
          UP.Threshold * UP.MaxPercentThresholdBoost / 100,
          UP.MaxIterationsCountToAnalyze)) {
    unsigned Boost =
        getFullUnrollBoostingFactor(*Cost, UP.MaxPercentThresholdBoost);
    unsigned BoostedThreshold = UP.Threshold * Boost / 100;
    if (Cost->UnrolledCost < BoostedThreshold) {
      // ------ Add our heuristic here ------
      LLVM_DEBUG(dbgs().indent(2) << "Profitable after cost analysis.\n");
      return FullUnrollTripCount;
    }
```

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


More information about the llvm-commits mailing list