Skip to content

Commit 7cdcff6

Browse files
committed
update match state
1 parent 353f0ac commit 7cdcff6

File tree

3 files changed

+49
-35
lines changed

3 files changed

+49
-35
lines changed

llvm/lib/Transforms/IPO/SampleProfile.cpp

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -446,13 +446,19 @@ class SampleProfileMatcher {
446446

447447
// Match state for an anchor/callsite.
448448
enum class MatchState {
449-
InitialMatch = 0,
450-
InitialMismatch = 1,
451-
// From initial mismatch to final match.
452-
RecoveredMismatch = 2,
453-
// From initial match to final mismatch.
454-
RemovedMatch = 3,
455-
Unknown = 32,
449+
Unknown = 0,
450+
// Initial match in pre-match.
451+
InitialMatch = 1,
452+
// Initial mismatch in pre-match.
453+
InitialMismatch = 2,
454+
// InitialMatch stays matched in post-match.
455+
UnchangedMatch = 3,
456+
// InitialMismatch stays mismatched in post-match.
457+
UnchangedMismatch = 4,
458+
// InitialMismatch is recovered in post-match.
459+
RecoveredMismatch = 5,
460+
// InitialMatch is removed and becomes mismatched in post-match.
461+
RemovedMatch = 6,
456462
};
457463

458464
// For each function, store every callsite and its matching state into this
@@ -508,6 +514,12 @@ class SampleProfileMatcher {
508514
&ProfileAnchors,
509515
const LocToLocMap *IRToProfileLocationMap);
510516

517+
bool isMismatchState(const enum MatchState &State) {
518+
return State == MatchState::InitialMismatch ||
519+
State == MatchState::UnchangedMismatch ||
520+
State == MatchState::RemovedMatch;
521+
};
522+
511523
// Count the samples of checksum mismatched function for the top-level
512524
// function and all inlinees.
513525
void countMismatchedFuncSamples(const FunctionSamples &FS, bool IsTopLevel);
@@ -2434,12 +2446,15 @@ void SampleProfileMatcher::recordCallsiteMatchStates(
24342446
IsCallsiteMatched = true;
24352447

24362448
if (IsCallsiteMatched) {
2437-
auto R =
2438-
CallsiteMatchStates.emplace(ProfileLoc, MatchState::InitialMatch);
2439-
// When there is an existing state, we know it's in post-match phrase.
2440-
// Update the matching state accordingly.
2441-
if (!R.second && R.first->second == MatchState::InitialMismatch)
2442-
R.first->second = MatchState::RecoveredMismatch;
2449+
auto It = CallsiteMatchStates.find(ProfileLoc);
2450+
if (It == CallsiteMatchStates.end())
2451+
CallsiteMatchStates.emplace(ProfileLoc, MatchState::InitialMatch);
2452+
else if (IsPostMatch) {
2453+
if (It->second == MatchState::InitialMatch)
2454+
It->second = MatchState::UnchangedMatch;
2455+
else if (It->second == MatchState::InitialMismatch)
2456+
It->second = MatchState::RecoveredMismatch;
2457+
}
24432458
}
24442459
}
24452460

@@ -2452,9 +2467,14 @@ void SampleProfileMatcher::recordCallsiteMatchStates(
24522467
auto It = CallsiteMatchStates.find(Loc);
24532468
if (It == CallsiteMatchStates.end())
24542469
CallsiteMatchStates.emplace(Loc, MatchState::InitialMismatch);
2455-
// The inital match is removed in post-match.
2456-
else if (IsPostMatch && It->second == MatchState::InitialMatch)
2457-
CallsiteMatchStates.emplace(Loc, MatchState::RemovedMatch);
2470+
else if (IsPostMatch) {
2471+
// Update the state if it's not matched(UnchangedMatch or
2472+
// RecoveredMismatch).
2473+
if (It->second == MatchState::InitialMismatch)
2474+
It->second = MatchState::UnchangedMismatch;
2475+
else if (It->second == MatchState::InitialMatch)
2476+
It->second = MatchState::RemovedMatch;
2477+
}
24582478
}
24592479
}
24602480

@@ -2501,14 +2521,9 @@ void SampleProfileMatcher::countMismatchedCallsiteSamples(
25012521
return It->second;
25022522
};
25032523

2504-
auto IsMismatchState = [&](const enum MatchState &State) {
2505-
return State == MatchState::InitialMismatch ||
2506-
State == MatchState::RemovedMatch;
2507-
};
2508-
25092524
auto AttributeMismatchedSamples = [&](const enum MatchState &State,
25102525
uint64_t Samples) {
2511-
if (IsMismatchState(State))
2526+
if (isMismatchState(State))
25122527
MismatchedCallsiteSamples += Samples;
25132528
else if (State == MatchState::RecoveredMismatch)
25142529
RecoveredCallsiteSamples += Samples;
@@ -2527,7 +2542,7 @@ void SampleProfileMatcher::countMismatchedCallsiteSamples(
25272542
CallsiteSamples += CS.second.getTotalSamples();
25282543
AttributeMismatchedSamples(State, CallsiteSamples);
25292544

2530-
if (IsMismatchState(State))
2545+
if (isMismatchState(State))
25312546
continue;
25322547

25332548
// When the current level of inlined call site matches the profiled call
@@ -2546,8 +2561,7 @@ void SampleProfileMatcher::countMismatchCallsites(const FunctionSamples &FS) {
25462561
const auto &MatchStates = It->second;
25472562
for (const auto &I : MatchStates) {
25482563
TotalProfiledCallsites++;
2549-
if (I.second == MatchState::InitialMismatch ||
2550-
I.second == MatchState::RemovedMatch)
2564+
if (isMismatchState(I.second))
25512565
NumMismatchedCallsites++;
25522566
else if (I.second == MatchState::RecoveredMismatch)
25532567
NumRecoveredCallsites++;

llvm/test/Transforms/SampleProfile/Inputs/pseudo-probe-profile-mismatch.prof

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ main:30:0
55
13: foo_mismatch:10
66
1: 10
77
!CFGChecksum: 4294967295
8-
!CFGChecksum: 844635331715433
8+
!CFGChecksum: 84463533171543
99
bar:10:10
1010
1: 10
1111
!CFGChecksum: 42949671295

llvm/test/Transforms/SampleProfile/pseudo-probe-profile-mismatch.ll

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
; RUN: opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/pseudo-probe-profile-mismatch-nested.prof -report-profile-staleness -persist-profile-staleness -S 2>&1 | FileCheck %s --check-prefix=CHECK-NESTED
1010

1111

12-
; CHECK: (1/3) of functions' profile are invalid and (10/50) of samples are discarded due to function hash mismatch.
12+
; CHECK: (2/3) of functions' profile are invalid and (40/50) of samples are discarded due to function hash mismatch.
1313
; CHECK: (2/3) of callsites' profile are invalid and (20/50) of samples are discarded due to callsite location mismatch.
14-
; CHECK: (0/2) of callsites and (0/20) of samples are recovered by stale profile matching.
14+
; CHECK: (1/2) of callsites and (10/20) of samples are recovered by stale profile matching.
1515

16-
; CHECK-MD: ![[#]] = !{!"NumStaleProfileFunc", i64 1, !"TotalProfiledFunc", i64 3, !"MismatchedFunctionSamples", i64 10, !"TotalFunctionSamples", i64 50, !"NumMismatchedCallsites", i64 2, !"NumRecoveredCallsites", i64 0, !"TotalProfiledCallsites", i64 3, !"MismatchedCallsiteSamples", i64 20, !"RecoveredCallsiteSamples", i64 0}
16+
; CHECK-MD: ![[#]] = !{!"NumStaleProfileFunc", i64 2, !"TotalProfiledFunc", i64 3, !"MismatchedFunctionSamples", i64 40, !"TotalFunctionSamples", i64 50, !"NumMismatchedCallsites", i64 1, !"NumRecoveredCallsites", i64 1, !"TotalProfiledCallsites", i64 3, !"MismatchedCallsiteSamples", i64 10, !"RecoveredCallsiteSamples", i64 10}
1717

1818

1919
; CHECK-OBJ: .llvm_stats
@@ -22,39 +22,39 @@
2222
; CHECK-ASM: .byte 19
2323
; CHECK-ASM: .ascii "NumStaleProfileFunc"
2424
; CHECK-ASM: .byte 4
25-
; CHECK-ASM: .ascii "MQ=="
25+
; CHECK-ASM: .ascii "Mg=="
2626
; CHECK-ASM: .byte 17
2727
; CHECK-ASM: .ascii "TotalProfiledFunc"
2828
; CHECK-ASM: .byte 4
2929
; CHECK-ASM: .ascii "Mw=="
3030
; CHECK-ASM: .byte 25
3131
; CHECK-ASM: .ascii "MismatchedFunctionSamples"
3232
; CHECK-ASM: .byte 4
33-
; CHECK-ASM: .ascii "MTA="
33+
; CHECK-ASM: .ascii "NDA="
3434
; CHECK-ASM: .byte 20
3535
; CHECK-ASM: .ascii "TotalFunctionSamples"
3636
; CHECK-ASM: .byte 4
3737
; CHECK-ASM: .ascii "NTA="
3838
; CHECK-ASM: .byte 22
3939
; CHECK-ASM: .ascii "NumMismatchedCallsites"
4040
; CHECK-ASM: .byte 4
41-
; CHECK-ASM: .ascii "Mg=="
41+
; CHECK-ASM: .ascii "MQ=="
4242
; CHECK-ASM: .byte 21
4343
; CHECK-ASM: .ascii "NumRecoveredCallsites"
4444
; CHECK-ASM: .byte 4
45-
; CHECK-ASM: .ascii "MA=="
45+
; CHECK-ASM: .ascii "MQ=="
4646
; CHECK-ASM: .byte 22
4747
; CHECK-ASM: .ascii "TotalProfiledCallsites"
4848
; CHECK-ASM: .byte 4
4949
; CHECK-ASM: .ascii "Mw=="
5050
; CHECK-ASM: .byte 25
5151
; CHECK-ASM: .ascii "MismatchedCallsiteSamples"
5252
; CHECK-ASM: .byte 4
53-
; CHECK-ASM: .ascii "MjA="
53+
; CHECK-ASM: .ascii "MTA="
5454
; CHECK-ASM: .byte 24
5555
; CHECK-ASM: .ascii "RecoveredCallsiteSamples"
5656
; CHECK-ASM: .byte 4
57-
; CHECK-ASM: .ascii "MA=="
57+
; CHECK-ASM: .ascii "MTA="
5858

5959

6060
; CHECK-NESTED: (1/2) of functions' profile are invalid and (211/311) of samples are discarded due to function hash mismatch.

0 commit comments

Comments
 (0)