Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
f413520
[LoopPeel] Fix branch weights' effect on block frequencies
jdenny-ornl Mar 19, 2025
f821eeb
Run update_test_checks.py on a test
jdenny-ornl Mar 26, 2025
af8ec56
Fix typo
jdenny-ornl Apr 4, 2025
a0264ad
Merge branch 'main' into fix-peel-branch-weights
jdenny-ornl Apr 8, 2025
fd29a49
Merge branch 'main' into fix-peel-branch-weights
jdenny-ornl Apr 9, 2025
6303177
Document new metadata
jdenny-ornl Apr 10, 2025
bbd0e95
Improve LangRef.rst entry
jdenny-ornl May 1, 2025
715cb0a
Merge branch 'main' into fix-peel-branch-weights
jdenny-ornl May 5, 2025
67fa67d
Merge branch 'main' into fix-peel-branch-weights
jdenny-ornl Jun 10, 2025
37ce859
Update fixmes
jdenny-ornl Jun 16, 2025
4337dcd
Merge branch 'main' into fix-peel-branch-weights
jdenny-ornl Jun 16, 2025
5193158
Update test for AArch4, which I did not build before
jdenny-ornl Jun 17, 2025
bbd2f22
Merge branch 'main' into fix-peel-branch-weights
jdenny-ornl Jul 10, 2025
b23f467
Run update script on test changed by merge from main
jdenny-ornl Jul 10, 2025
13d1fbb
[PGO] Add `llvm.loop.estimated_trip_count` metadata
jdenny-ornl Jul 15, 2025
e250cfc
Merge branch 'main' into fix-peel-branch-weights
jdenny-ornl Jul 15, 2025
859b84d
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Jul 15, 2025
db5920a
Merge branch 'main' into pgo-estimated-trip-count
jdenny-ornl Jul 21, 2025
47fbe85
Add PGOEstimateTripCounts in more cases
jdenny-ornl Jul 21, 2025
f8097fb
Add unused initialization
jdenny-ornl Jul 21, 2025
7b27203
Simplify some test changes
jdenny-ornl Jul 22, 2025
4c4669a
Extend verify pass to cover new metadata
jdenny-ornl Jul 24, 2025
0f40efd
Fix test for some builds
jdenny-ornl Jul 24, 2025
2791a1c
Merge branch 'main' into pgo-estimated-trip-count
jdenny-ornl Jul 24, 2025
6148922
Apply some small reviewer suggestions
jdenny-ornl Jul 24, 2025
3f6a91a
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Jul 24, 2025
e5a0a26
Update for merge from pgo-estimated-trip-count
jdenny-ornl Jul 24, 2025
3a49b43
Attempt to fix windows pre-commit CI
jdenny-ornl Jul 24, 2025
c283ebe
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Jul 24, 2025
2f7daa8
Merge branch 'main' into pgo-estimated-trip-count
jdenny-ornl Jul 28, 2025
ecbf6e0
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Jul 28, 2025
c627fc5
Merge branch 'main' into pgo-estimated-trip-count
jdenny-ornl Aug 7, 2025
f1fa8d9
Run update script on new test from last merge
jdenny-ornl Aug 7, 2025
38ace1e
Reapply 3a18fe33f0763cd9276c99c276448412100f6270
jdenny-ornl Aug 7, 2025
92ddaa0
Convert to function pass, avoid needless pass invalidation
jdenny-ornl Aug 8, 2025
a3e0d72
Fix layering violation
jdenny-ornl Aug 8, 2025
67f22cd
Apply clang-format
jdenny-ornl Aug 8, 2025
f0ff2e2
Merge branch 'main' into pgo-estimated-trip-count
jdenny-ornl Aug 9, 2025
69fe051
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Aug 9, 2025
e7eb1fe
Merge branch 'main' into pgo-estimated-trip-count
jdenny-ornl Aug 13, 2025
e4f68c3
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Aug 13, 2025
0973ab3
Merge branch 'main' into pgo-estimated-trip-count
jdenny-ornl Aug 18, 2025
680bdc2
Remove PGOEstimateTripCountsPass and no-value form of metadata
jdenny-ornl Aug 18, 2025
83531b3
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Aug 19, 2025
59cd184
Fix case where nested loops share latch
jdenny-ornl Aug 19, 2025
47051ce
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Aug 19, 2025
5d00250
Merge branch 'main' into pgo-estimated-trip-count
jdenny-ornl Aug 25, 2025
5719779
Remove redundant code
jdenny-ornl Aug 25, 2025
98cab7b
Clarify recent comments some
jdenny-ornl Aug 25, 2025
3cbe07d
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Aug 25, 2025
b3831b6
Merge branch 'main' into pgo-estimated-trip-count
jdenny-ornl Sep 1, 2025
59ab013
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Sep 2, 2025
12ce70e
[LoopUnroll] Skip remainder loop guard if skip unrolled loop
jdenny-ornl Sep 2, 2025
415cb8f
Improve comments
jdenny-ornl Sep 3, 2025
b8aed9b
Merge branch 'main' into pgo-estimated-trip-count
jdenny-ornl Sep 9, 2025
5c9e43e
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Sep 9, 2025
cc3283d
Merge branch 'fix-peel-branch-weights' into skip-unroll-epilog-guard
jdenny-ornl Sep 9, 2025
75a8df6
[LoopUnroll] Fix block frequencies when no runtime
jdenny-ornl Sep 9, 2025
83ac767
Merge branch 'main' into fix-peel-branch-weights
jdenny-ornl Sep 15, 2025
1f81310
Empty commit to try to restart pre-commit CI
jdenny-ornl Sep 15, 2025
2382fbd
Merge branch 'fix-peel-branch-weights' into skip-unroll-epilog-guard
jdenny-ornl Sep 15, 2025
967f8a1
Merge branch 'skip-unroll-epilog-guard' into fix-blockfreq-unroll-no-…
jdenny-ornl Sep 15, 2025
2897e64
Improve some code comments
jdenny-ornl Sep 16, 2025
876e055
Remove xfails
jdenny-ornl Sep 17, 2025
04c8ade
Merge branch 'main' into fix-peel-branch-weights
jdenny-ornl Sep 22, 2025
9b80c13
Merge branch 'fix-peel-branch-weights' into skip-unroll-epilog-guard
jdenny-ornl Sep 22, 2025
9215f47
Merge branch 'skip-unroll-epilog-guard' into fix-blockfreq-unroll-no-…
jdenny-ornl Sep 22, 2025
a1a5460
Merge branch 'main' into fix-peel-branch-weights
jdenny-ornl Sep 30, 2025
df9cf8c
Merge branch 'fix-peel-branch-weights' into skip-unroll-epilog-guard
jdenny-ornl Sep 30, 2025
99c95b1
Merge branch 'skip-unroll-epilog-guard' into fix-blockfreq-unroll-no-…
jdenny-ornl Sep 30, 2025
4353f1f
Merge branch 'main' into skip-unroll-epilog-guard
jdenny-ornl Oct 6, 2025
f66ae02
Merge branch 'skip-unroll-epilog-guard' into fix-blockfreq-unroll-no-…
jdenny-ornl Oct 6, 2025
22fdacf
Merge branch 'main' into users/jdenny-ornl/fix-blockfreq-unroll-no-ru…
jdenny-ornl Oct 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions llvm/lib/Transforms/Utils/LoopUnroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,8 @@ 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);

// Effectively "DCE" unrolled iterations that are beyond the max tripcount
// and will never be executed.
Expand Down Expand Up @@ -1132,10 +1131,38 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
// 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);
// 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: Actually handle ULO.Runtime.
// - 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.
unsigned NewTripCount = *OriginalTripCount / ULO.Count;
if (!ULO.Runtime && *OriginalTripCount % ULO.Count)
NewTripCount += 1;
setLoopEstimatedTripCount(L, NewTripCount);
}

// LoopInfo should not be valid, confirm that.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
; Test branch weight metadata, estimated trip count metadata, and block
; frequencies after partial loop unrolling without -unroll-runtime.

; RUN: opt < %s -S -passes='print<block-freq>' 2>&1 | \
; RUN: FileCheck -check-prefix=CHECK %s

; The -implicit-check-not options make sure that no additional labels or calls
; to @f show up.
; RUN: opt < %s -S -passes='loop-unroll,print<block-freq>' \
; RUN: -unroll-count=4 2>&1 | \
; RUN: FileCheck %s -check-prefix=CHECK-UR \
; RUN: -implicit-check-not='{{^( *- )?[^ ;]*:}}' \
; RUN: -implicit-check-not='call void @f'

; CHECK: block-frequency-info: test
; CHECK: do.body: float = 10.0,

; The sum should still be ~10.
;
; CHECK-UR: block-frequency-info: test
; CHECK-UR: - [[ENTRY:.*]]:
; CHECK-UR: - [[DO_BODY:.*]]: float = 2.9078,
; CHECK-UR: - [[DO_BODY_1:.*]]: float = 2.617,
; CHECK-UR: - [[DO_BODY_2:.*]]: float = 2.3553,
; CHECK-UR: - [[DO_BODY_3:.*]]: float = 2.1198,
; CHECK-UR: - [[DO_END:.*]]:

declare void @f(i32)

define void @test(i32 %n) {
; CHECK-UR-LABEL: define void @test(i32 %{{.*}}) {
; CHECK-UR: [[ENTRY]]:
; CHECK-UR: br label %[[DO_BODY]]
; CHECK-UR: [[DO_BODY]]:
; CHECK-UR: call void @f
; CHECK-UR: br i1 %{{.*}}, label %[[DO_END]], label %[[DO_BODY_1]], !prof ![[#PROF:]]
; CHECK-UR: [[DO_BODY_1]]:
; CHECK-UR: call void @f
; CHECK-UR: br i1 %{{.*}}, label %[[DO_END]], label %[[DO_BODY_2]], !prof ![[#PROF]]
; CHECK-UR: [[DO_BODY_2]]:
; CHECK-UR: call void @f
; CHECK-UR: br i1 %{{.*}}, label %[[DO_END]], label %[[DO_BODY_3]], !prof ![[#PROF]]
; CHECK-UR: [[DO_BODY_3]]:
; CHECK-UR: call void @f
; CHECK-UR: br i1 %{{.*}}, label %[[DO_END]], label %[[DO_BODY]], !prof ![[#PROF]], !llvm.loop ![[#LOOP_UR_LATCH:]]
; CHECK-UR: [[DO_END]]:
; CHECK-UR: ret void

entry:
br label %do.body

do.body:
%i = phi i32 [ 0, %entry ], [ %inc, %do.body ]
%inc = add i32 %i, 1
call void @f(i32 %i)
%c = icmp sge i32 %inc, %n
br i1 %c, label %do.end, label %do.body, !prof !0

do.end:
ret void
}

!0 = !{!"branch_weights", i32 1, i32 9}

; CHECK-UR: ![[#PROF]] = !{!"branch_weights", i32 1, i32 9}
; CHECK-UR: ![[#LOOP_UR_LATCH]] = distinct !{![[#LOOP_UR_LATCH]], ![[#LOOP_UR_TC:]], ![[#DISABLE:]]}
; CHECK-UR: ![[#LOOP_UR_TC]] = !{!"llvm.loop.estimated_trip_count", i32 3}
; CHECK-UR: ![[#DISABLE]] = !{!"llvm.loop.unroll.disable"}
5 changes: 4 additions & 1 deletion llvm/test/Transforms/LoopUnroll/runtime-loop-branchweight.ll
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
; CHECK: br i1 [[COND1:%.*]], label %for.end.loopexit.unr-lcssa, label %for.body, !prof ![[#PROF:]], !llvm.loop ![[#LOOP:]]
; CHECK-LABEL: for.body.epil:
; CHECK: br i1 [[COND2:%.*]], label %for.body.epil, label %for.end.loopexit.epilog-lcssa, !prof ![[#PROF2:]], !llvm.loop ![[#LOOP2:]]
; CHECK: ![[#PROF]] = !{!"branch_weights", i32 1, i32 2499}

; FIXME: These branch weights are incorrect and should not be merged into main
; until PR #159163, which fixes them.
; CHECK: ![[#PROF]] = !{!"branch_weights", i32 1, i32 9999}
; CHECK: ![[#PROF2]] = !{!"branch_weights", i32 1, i32 1}

define i3 @test(ptr %a, i3 %n) {
Expand Down
4 changes: 3 additions & 1 deletion llvm/test/Transforms/LoopUnroll/unroll-heuristics-pgo.ll
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,7 @@ loop.end:
!1 = !{!"function_entry_count", i64 1}
!2 = !{!"branch_weights", i32 1, i32 1000}

; CHECK: [[PROF0]] = !{!"branch_weights", i32 1, i32 124}
; FIXME: These branch weights are incorrect and should not be merged into main
; until PR #159163, which fixes them.
; CHECK: [[PROF0]] = !{!"branch_weights", i32 1, i32 1000}
; CHECK: [[PROF1]] = !{!"branch_weights", i32 3, i32 1}