Skip to content

Commit bf6debc

Browse files
authored
[FunctionSpecialization] Fix profile count preserving logic (#157939)
The previous fix in #157768 had a bug; instead of subtracting the original function's call count per call site of a specialization, we were subtracting the running total of the specialization's calls. Tracking issue: #147390
1 parent 6a71938 commit bf6debc

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

llvm/lib/Transforms/IPO/FunctionSpecialization.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,8 +802,8 @@ bool FunctionSpecializer::run() {
802802
if (std::optional<llvm::Function::ProfileCount> MaybeOriginalCount =
803803
S.F->getEntryCount()) {
804804
uint64_t OriginalCount = MaybeOriginalCount->getCount();
805-
if (OriginalCount >= CallCount) {
806-
S.F->setEntryCount(OriginalCount - CallCount);
805+
if (OriginalCount >= *Count) {
806+
S.F->setEntryCount(OriginalCount - *Count);
807807
} else {
808808
// This should generally not happen as that would mean there are
809809
// more computed calls to the function than what was recorded.

llvm/test/Transforms/FunctionSpecialization/profile-counts.ll

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,29 @@ entry:
1313

1414
; CHECK: if.then:
1515
; CHECK: call i32 @foo.specialized.1(i32 %x, ptr @A)
16+
; CHECK: call i32 @foo.specialized.2(i32 %y, ptr @B)
17+
; CHECK: call i32 @foo.specialized.2(i32 %y, ptr @B)
1618
if.then:
1719
%call = call i32 @foo(i32 %x, ptr @A)
20+
%call1 = call i32 @foo(i32 %y, ptr @B)
21+
%call2 = call i32 @foo(i32 %y, ptr @B)
22+
%add = add i32 %call, %call1
23+
%add1 = add i32 %add, %call2
1824
br label %return
1925

2026
; CHECK: if.else:
2127
; CHECK: call i32 @foo.specialized.2(i32 %y, ptr @B)
2228
if.else:
23-
%call1 = call i32 @foo(i32 %y, ptr @B)
29+
%call3 = call i32 @foo(i32 %y, ptr @B)
2430
br label %return
2531

2632
; CHECK: return:
27-
; CHECK: %call2 = call i32 @foo(i32 %x, ptr %z)
33+
; CHECK: call i32 @foo(i32 %x, ptr %z)
2834
return:
29-
%retval.0 = phi i32 [ %call, %if.then ], [ %call1, %if.else ]
30-
%call2 = call i32 @foo(i32 %x, ptr %z);
31-
%add = add i32 %retval.0, %call2
32-
ret i32 %add
35+
%retval.0 = phi i32 [ %add1, %if.then ], [ %call3, %if.else ]
36+
%call4 = call i32 @foo(i32 %x, ptr %z);
37+
%add2 = add i32 %retval.0, %call4
38+
ret i32 %add2
3339
}
3440

3541
; CHECK: define internal i32 @foo(i32 %x, ptr %b) !prof ![[FOO_UNSPEC_PROF:[0-9]]]
@@ -44,9 +50,9 @@ entry:
4450

4551
; CHECK: ![[BAR_PROF]] = !{!"function_entry_count", i64 1000}
4652
; CHECK: ![[BRANCH_PROF]] = !{!"branch_weights", i32 1, i32 3}
47-
; CHECK: ![[FOO_UNSPEC_PROF]] = !{!"function_entry_count", i64 234}
53+
; CHECK: ![[FOO_UNSPEC_PROF]] = !{!"function_entry_count", i64 500}
4854
; CHECK: ![[FOO_SPEC_1_PROF]] = !{!"function_entry_count", i64 250}
49-
; CHECK: ![[FOO_SPEC_2_PROF]] = !{!"function_entry_count", i64 750}
55+
; CHECK: ![[FOO_SPEC_2_PROF]] = !{!"function_entry_count", i64 1250}
5056
!0 = !{!"function_entry_count", i64 1000}
5157
!1 = !{!"branch_weights", i32 1, i32 3}
52-
!2 = !{!"function_entry_count", i64 1234}
58+
!2 = !{!"function_entry_count", i64 2000}

llvm/utils/profcheck-xfail.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,6 @@ Transforms/FixIrreducible/basic.ll
755755
Transforms/FixIrreducible/bug45623.ll
756756
Transforms/FixIrreducible/nested.ll
757757
Transforms/FixIrreducible/switch.ll
758-
Transforms/FunctionSpecialization/function-specialization3.ll
759758
Transforms/GCOVProfiling/atomic-counter.ll
760759
Transforms/GCOVProfiling/exit-block.ll
761760
Transforms/GCOVProfiling/function-numbering.ll

0 commit comments

Comments
 (0)