Skip to content

Commit 7c4211f

Browse files
committed
[FunctionSpecialization] Fix profile count preserving logic
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 055e4ff commit 7c4211f

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

llvm/lib/Transforms/IPO/FunctionSpecialization.cpp

Lines changed: 3 additions & 3 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.
@@ -1067,7 +1067,7 @@ Function *FunctionSpecializer::createSpecialization(Function *F,
10671067
// clone must.
10681068
Clone->setLinkage(GlobalValue::InternalLinkage);
10691069

1070-
if (F->getEntryCount() && !ProfcheckDisableMetadataFixes)
1070+
if (F->getEntryCount())
10711071
Clone->setEntryCount(0);
10721072

10731073
// Initialize the lattice state of the arguments of the function clone,

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,28 @@ 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)
1617
if.then:
1718
%call = call i32 @foo(i32 %x, ptr @A)
19+
%call1 = call i32 @foo(i32 %y, ptr @B)
20+
%call2 = call i32 @foo(i32 %y, ptr @B)
21+
%add = add i32 %call, %call1
22+
%add1 = add i32 %add, %call2
1823
br label %return
1924

2025
; CHECK: if.else:
2126
; CHECK: call i32 @foo.specialized.2(i32 %y, ptr @B)
2227
if.else:
23-
%call1 = call i32 @foo(i32 %y, ptr @B)
28+
%call3 = call i32 @foo(i32 %y, ptr @B)
2429
br label %return
2530

2631
; CHECK: return:
27-
; CHECK: %call2 = call i32 @foo(i32 %x, ptr %z)
32+
; CHECK: %call3 = call i32 @foo(i32 %x, ptr %z)
2833
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
34+
%retval.0 = phi i32 [ %add1, %if.then ], [ %call3, %if.else ]
35+
%call4 = call i32 @foo(i32 %x, ptr %z);
36+
%add2 = add i32 %retval.0, %call4
37+
ret i32 %add2
3338
}
3439

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

4550
; CHECK: ![[BAR_PROF]] = !{!"function_entry_count", i64 1000}
4651
; CHECK: ![[BRANCH_PROF]] = !{!"branch_weights", i32 1, i32 3}
47-
; CHECK: ![[FOO_UNSPEC_PROF]] = !{!"function_entry_count", i64 234}
52+
; CHECK: ![[FOO_UNSPEC_PROF]] = !{!"function_entry_count", i64 500}
4853
; CHECK: ![[FOO_SPEC_1_PROF]] = !{!"function_entry_count", i64 250}
49-
; CHECK: ![[FOO_SPEC_2_PROF]] = !{!"function_entry_count", i64 750}
54+
; CHECK: ![[FOO_SPEC_2_PROF]] = !{!"function_entry_count", i64 1250}
5055
!0 = !{!"function_entry_count", i64 1000}
5156
!1 = !{!"branch_weights", i32 1, i32 3}
52-
!2 = !{!"function_entry_count", i64 1234}
57+
!2 = !{!"function_entry_count", i64 2000}

0 commit comments

Comments
 (0)