- 
                Notifications
    
You must be signed in to change notification settings  - Fork 15.1k
 
[LoopUnroll] Fix block frequencies for epilogue #159163
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
      
      
            jdenny-ornl
  merged 94 commits into
  main
from
users/jdenny-ornl/fix-blockfreq-unroll-epilogue
  
      
      
   
  Oct 31, 2025 
      
    
  
     Merged
                    Changes from 83 commits
      Commits
    
    
            Show all changes
          
          
            94 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      f413520
              
                [LoopPeel] Fix branch weights' effect on block frequencies
              
              
                jdenny-ornl f821eeb
              
                Run update_test_checks.py on a test
              
              
                jdenny-ornl af8ec56
              
                Fix typo
              
              
                jdenny-ornl a0264ad
              
                Merge branch 'main' into fix-peel-branch-weights
              
              
                jdenny-ornl fd29a49
              
                Merge branch 'main' into fix-peel-branch-weights
              
              
                jdenny-ornl 6303177
              
                Document new metadata
              
              
                jdenny-ornl bbd0e95
              
                Improve LangRef.rst entry
              
              
                jdenny-ornl 715cb0a
              
                Merge branch 'main' into fix-peel-branch-weights
              
              
                jdenny-ornl 67fa67d
              
                Merge branch 'main' into fix-peel-branch-weights
              
              
                jdenny-ornl 37ce859
              
                Update fixmes
              
              
                jdenny-ornl 4337dcd
              
                Merge branch 'main' into fix-peel-branch-weights
              
              
                jdenny-ornl 5193158
              
                Update test for AArch4, which I did not build before
              
              
                jdenny-ornl bbd2f22
              
                Merge branch 'main' into fix-peel-branch-weights
              
              
                jdenny-ornl b23f467
              
                Run update script on test changed by merge from main
              
              
                jdenny-ornl 13d1fbb
              
                [PGO] Add `llvm.loop.estimated_trip_count` metadata
              
              
                jdenny-ornl e250cfc
              
                Merge branch 'main' into fix-peel-branch-weights
              
              
                jdenny-ornl 859b84d
              
                Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
              
              
                jdenny-ornl db5920a
              
                Merge branch 'main' into pgo-estimated-trip-count
              
              
                jdenny-ornl 47fbe85
              
                Add PGOEstimateTripCounts in more cases
              
              
                jdenny-ornl f8097fb
              
                Add unused initialization
              
              
                jdenny-ornl 7b27203
              
                Simplify some test changes
              
              
                jdenny-ornl 4c4669a
              
                Extend verify pass to cover new metadata
              
              
                jdenny-ornl 0f40efd
              
                Fix test for some builds
              
              
                jdenny-ornl 2791a1c
              
                Merge branch 'main' into pgo-estimated-trip-count
              
              
                jdenny-ornl 6148922
              
                Apply some small reviewer suggestions
              
              
                jdenny-ornl 3f6a91a
              
                Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
              
              
                jdenny-ornl e5a0a26
              
                Update for merge from pgo-estimated-trip-count
              
              
                jdenny-ornl 3a49b43
              
                Attempt to fix windows pre-commit CI
              
              
                jdenny-ornl c283ebe
              
                Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
              
              
                jdenny-ornl 2f7daa8
              
                Merge branch 'main' into pgo-estimated-trip-count
              
              
                jdenny-ornl ecbf6e0
              
                Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
              
              
                jdenny-ornl c627fc5
              
                Merge branch 'main' into pgo-estimated-trip-count
              
              
                jdenny-ornl f1fa8d9
              
                Run update script on new test from last merge
              
              
                jdenny-ornl 38ace1e
              
                Reapply 3a18fe33f0763cd9276c99c276448412100f6270
              
              
                jdenny-ornl 92ddaa0
              
                Convert to function pass, avoid needless pass invalidation
              
              
                jdenny-ornl a3e0d72
              
                Fix layering violation
              
              
                jdenny-ornl 67f22cd
              
                Apply clang-format
              
              
                jdenny-ornl f0ff2e2
              
                Merge branch 'main' into pgo-estimated-trip-count
              
              
                jdenny-ornl 69fe051
              
                Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
              
              
                jdenny-ornl e7eb1fe
              
                Merge branch 'main' into pgo-estimated-trip-count
              
              
                jdenny-ornl e4f68c3
              
                Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
              
              
                jdenny-ornl 0973ab3
              
                Merge branch 'main' into pgo-estimated-trip-count
              
              
                jdenny-ornl 680bdc2
              
                Remove PGOEstimateTripCountsPass and no-value form of metadata
              
              
                jdenny-ornl 83531b3
              
                Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
              
              
                jdenny-ornl 59cd184
              
                Fix case where nested loops share latch
              
              
                jdenny-ornl 47051ce
              
                Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
              
              
                jdenny-ornl 5d00250
              
                Merge branch 'main' into pgo-estimated-trip-count
              
              
                jdenny-ornl 5719779
              
                Remove redundant code
              
              
                jdenny-ornl 98cab7b
              
                Clarify recent comments some
              
              
                jdenny-ornl 3cbe07d
              
                Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
              
              
                jdenny-ornl b3831b6
              
                Merge branch 'main' into pgo-estimated-trip-count
              
              
                jdenny-ornl 59ab013
              
                Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
              
              
                jdenny-ornl 12ce70e
              
                [LoopUnroll] Skip remainder loop guard if skip unrolled loop
              
              
                jdenny-ornl 415cb8f
              
                Improve comments
              
              
                jdenny-ornl b8aed9b
              
                Merge branch 'main' into pgo-estimated-trip-count
              
              
                jdenny-ornl 5c9e43e
              
                Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
              
              
                jdenny-ornl cc3283d
              
                Merge branch 'fix-peel-branch-weights' into skip-unroll-epilog-guard
              
              
                jdenny-ornl 75a8df6
              
                [LoopUnroll] Fix block frequencies when no runtime
              
              
                jdenny-ornl 83ac767
              
                Merge branch 'main' into fix-peel-branch-weights
              
              
                jdenny-ornl 1f81310
              
                Empty commit to try to restart pre-commit CI
              
              
                jdenny-ornl 2382fbd
              
                Merge branch 'fix-peel-branch-weights' into skip-unroll-epilog-guard
              
              
                jdenny-ornl 967f8a1
              
                Merge branch 'skip-unroll-epilog-guard' into fix-blockfreq-unroll-no-…
              
              
                jdenny-ornl 2897e64
              
                Improve some code comments
              
              
                jdenny-ornl 5a99593
              
                [LoopUnroll] Fix block frequencies for epilogue
              
              
                jdenny-ornl a95ebd1
              
                Revert accidental change from right before pushing
              
              
                jdenny-ornl 876e055
              
                Remove xfails
              
              
                jdenny-ornl eb00930
              
                Merge branch 'fix-blockfreq-unroll-no-runtime' into fix-blockfreq-unr…
              
              
                jdenny-ornl 04c8ade
              
                Merge branch 'main' into fix-peel-branch-weights
              
              
                jdenny-ornl 9b80c13
              
                Merge branch 'fix-peel-branch-weights' into skip-unroll-epilog-guard
              
              
                jdenny-ornl 9215f47
              
                Merge branch 'skip-unroll-epilog-guard' into fix-blockfreq-unroll-no-…
              
              
                jdenny-ornl 712b282
              
                Merge branch 'fix-blockfreq-unroll-no-runtime' into fix-blockfreq-unr…
              
              
                jdenny-ornl d85294e
              
                Use BranchProbability
              
              
                jdenny-ornl 72c633f
              
                Drop includes no longer needed
              
              
                jdenny-ornl dc1f5f1
              
                Clean up new LoopUtils comments
              
              
                jdenny-ornl a1a5460
              
                Merge branch 'main' into fix-peel-branch-weights
              
              
                jdenny-ornl df9cf8c
              
                Merge branch 'fix-peel-branch-weights' into skip-unroll-epilog-guard
              
              
                jdenny-ornl 99c95b1
              
                Merge branch 'skip-unroll-epilog-guard' into fix-blockfreq-unroll-no-…
              
              
                jdenny-ornl 84e5223
              
                Merge branch 'fix-blockfreq-unroll-no-runtime' into fix-blockfreq-unr…
              
              
                jdenny-ornl 4353f1f
              
                Merge branch 'main' into skip-unroll-epilog-guard
              
              
                jdenny-ornl f66ae02
              
                Merge branch 'skip-unroll-epilog-guard' into fix-blockfreq-unroll-no-…
              
              
                jdenny-ornl 0eeada8
              
                Merge branch 'fix-blockfreq-unroll-no-runtime' into fix-blockfreq-unr…
              
              
                jdenny-ornl 22fdacf
              
                Merge branch 'main' into users/jdenny-ornl/fix-blockfreq-unroll-no-ru…
              
              
                jdenny-ornl e57014c
              
                Merge branch 'users/jdenny-ornl/fix-blockfreq-unroll-no-runtime' into…
              
              
                jdenny-ornl f625d45
              
                Merge branch 'main' into fix-blockfreq-unroll-no-runtime
              
              
                jdenny-ornl a5a4a45
              
                Merge branch 'fix-blockfreq-unroll-no-runtime' into fix-blockfreq-unr…
              
              
                jdenny-ornl 943e95e
              
                `+= 1` -> `++`
              
              
                jdenny-ornl 1e2307e
              
                Use setBranchWeights
              
              
                jdenny-ornl 662e60f
              
                Merge branch 'main' into fix-blockfreq-unroll-no-runtime
              
              
                jdenny-ornl 2ca587b
              
                Merge branch 'fix-blockfreq-unroll-no-runtime' into fix-blockfreq-unr…
              
              
                jdenny-ornl 093ad2a
              
                Merge branch 'main' into fix-blockfreq-unroll-no-runtime
              
              
                jdenny-ornl 21c4f9c
              
                Merge branch 'fix-blockfreq-unroll-no-runtime' into fix-blockfreq-unr…
              
              
                jdenny-ornl 663cf2b
              
                Merge branch 'main' into fix-blockfreq-unroll-no-runtime
              
              
                jdenny-ornl 5ed2dec
              
                Merge branch 'fix-blockfreq-unroll-no-runtime' into fix-blockfreq-unr…
              
              
                jdenny-ornl b95a9e2
              
                Merge branch 'main' into users/jdenny-ornl/fix-blockfreq-unroll-epilogue
              
              
                jdenny-ornl File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -499,9 +499,9 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI, | |
| 
     | 
||
| const unsigned MaxTripCount = SE->getSmallConstantMaxTripCount(L); | ||
| const bool MaxOrZero = SE->isBackedgeTakenCountMaxOrZero(L); | ||
| unsigned EstimatedLoopInvocationWeight = 0; | ||
| std::optional<unsigned> OriginalTripCount = | ||
| llvm::getLoopEstimatedTripCount(L, &EstimatedLoopInvocationWeight); | ||
| llvm::getLoopEstimatedTripCount(L); | ||
| BranchProbability OriginalLoopProb = llvm::getLoopProbability(L); | ||
| 
     | 
||
| // Effectively "DCE" unrolled iterations that are beyond the max tripcount | ||
| // and will never be executed. | ||
| 
          
            
          
           | 
    @@ -592,11 +592,11 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI, | |
| : isEpilogProfitable(L); | ||
| 
     | 
||
| if (ULO.Runtime && | ||
| !UnrollRuntimeLoopRemainder(L, ULO.Count, ULO.AllowExpensiveTripCount, | ||
| EpilogProfitability, ULO.UnrollRemainder, | ||
| ULO.ForgetAllSCEV, LI, SE, DT, AC, TTI, | ||
| PreserveLCSSA, ULO.SCEVExpansionBudget, | ||
| ULO.RuntimeUnrollMultiExit, RemainderLoop)) { | ||
| !UnrollRuntimeLoopRemainder( | ||
| L, ULO.Count, ULO.AllowExpensiveTripCount, EpilogProfitability, | ||
| ULO.UnrollRemainder, ULO.ForgetAllSCEV, LI, SE, DT, AC, TTI, | ||
| PreserveLCSSA, ULO.SCEVExpansionBudget, ULO.RuntimeUnrollMultiExit, | ||
| RemainderLoop, OriginalTripCount, OriginalLoopProb)) { | ||
| if (ULO.Force) | ||
| ULO.Runtime = false; | ||
| else { | ||
| 
          
            
          
           | 
    @@ -1131,11 +1131,46 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI, | |
| LI->erase(L); | ||
| // We shouldn't try to use `L` anymore. | ||
| L = nullptr; | ||
| } else if (OriginalTripCount) { | ||
| // Update the trip count. Note that the remainder has already logic | ||
| // computing it in `UnrollRuntimeLoopRemainder`. | ||
| setLoopEstimatedTripCount(L, *OriginalTripCount / ULO.Count, | ||
| EstimatedLoopInvocationWeight); | ||
| } else { | ||
| // Update metadata for the loop's branch weights and estimated trip count: | ||
| // - If ULO.Runtime, UnrollRuntimeLoopRemainder sets the guard branch | ||
| // weights, latch branch weights, and estimated trip count of the | ||
| // remainder loop it creates. It also sets the branch weights for the | ||
| // unrolled loop guard it creates. The branch weights for the unrolled | ||
| // loop latch are adjusted below. FIXME: Handle prologue loops. | ||
| // - Otherwise, if unrolled loop iteration latches become unconditional, | ||
| // branch weights are adjusted above. FIXME: Actually handle such | ||
| // unconditional latches. | ||
| // - Otherwise, the original loop's branch weights are correct for the | ||
| // unrolled loop, so do not adjust them. | ||
| // - In all cases, the unrolled loop's estimated trip count is set below. | ||
| // | ||
| // As an example of the last case, consider what happens if the unroll count | ||
| // is 4 for a loop with an estimated trip count of 10 when we do not create | ||
| // a remainder loop and all iterations' latches remain conditional. Each | ||
| // unrolled iteration's latch still has the same probability of exiting the | ||
| // loop as it did when in the original loop, and thus it should still have | ||
| // the same branch weights. Each unrolled iteration's non-zero probability | ||
| // of exiting already appropriately reduces the probability of reaching the | ||
| // remaining iterations just as it did in the original loop. Trying to also | ||
| // adjust the branch weights of the final unrolled iteration's latch (i.e., | ||
| // the backedge for the unrolled loop as a whole) to reflect its new trip | ||
| // count of 3 will erroneously further reduce its block frequencies. | ||
| // However, in case an analysis later needs to estimate the trip count of | ||
| // the unrolled loop as a whole without considering the branch weights for | ||
| // each unrolled iteration's latch within it, we store the new trip count as | ||
| // separate metadata. | ||
| if (!OriginalLoopProb.isUnknown() && ULO.Runtime && EpilogProfitability) { | ||
| // Where p is always the probability of executing at least 1 more | ||
| // iteration, the probability for at least n more iterations is p^n. | ||
| setLoopProbability(L, OriginalLoopProb.pow(ULO.Count)); | ||
| } | ||
| if (OriginalTripCount) { | ||
| unsigned NewTripCount = *OriginalTripCount / ULO.Count; | ||
| if (!ULO.Runtime && *OriginalTripCount % ULO.Count) | ||
| NewTripCount += 1; | ||
                
       | 
||
| setLoopEstimatedTripCount(L, NewTripCount); | ||
| } | ||
| } | ||
| 
     | 
||
| // LoopInfo should not be valid, confirm that. | ||
| 
          
            
          
           | 
    ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some guards on
Nfor overflow? like a LLVM_DEBUG maybe, and/or assert.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is an example that would cause overflow here, and why would the
operator*=used here not already handle it? Keep in mind thatBranchProbabilityrepresents a value from 0 to 1, so the result should always be a value that is also in that range.