Skip to content

Commit 0b47cf3

Browse files
author
git apple-llvm automerger
committed
Merge commit '3365cd454407' from llvm.org/main into next
2 parents cd71fc0 + 3365cd4 commit 0b47cf3

File tree

4 files changed

+60
-12
lines changed

4 files changed

+60
-12
lines changed

llvm/include/llvm/Transforms/Utils/SampleProfileLoaderBaseImpl.h

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,6 @@ template <> struct IRTraits<BasicBlock> {
8888
class PseudoProbeManager {
8989
DenseMap<uint64_t, PseudoProbeDescriptor> GUIDToProbeDescMap;
9090

91-
const PseudoProbeDescriptor *getDesc(const Function &F) const {
92-
auto I = GUIDToProbeDescMap.find(
93-
Function::getGUID(FunctionSamples::getCanonicalFnName(F)));
94-
return I == GUIDToProbeDescMap.end() ? nullptr : &I->second;
95-
}
96-
9791
public:
9892
PseudoProbeManager(const Module &M) {
9993
if (NamedMDNode *FuncInfo =
@@ -109,6 +103,24 @@ class PseudoProbeManager {
109103
}
110104
}
111105

106+
const PseudoProbeDescriptor *getDesc(uint64_t GUID) const {
107+
auto I = GUIDToProbeDescMap.find(GUID);
108+
return I == GUIDToProbeDescMap.end() ? nullptr : &I->second;
109+
}
110+
111+
const PseudoProbeDescriptor *getDesc(StringRef FProfileName) const {
112+
return getDesc(Function::getGUID(FProfileName));
113+
}
114+
115+
const PseudoProbeDescriptor *getDesc(const Function &F) const {
116+
return getDesc(Function::getGUID(FunctionSamples::getCanonicalFnName(F)));
117+
}
118+
119+
bool profileIsHashMismatched(const PseudoProbeDescriptor &FuncDesc,
120+
const FunctionSamples &Samples) const {
121+
return FuncDesc.getFunctionHash() != Samples.getFunctionHash();
122+
}
123+
112124
bool moduleIsProbed(const Module &M) const {
113125
return M.getNamedMetadata(PseudoProbeDescMetadataName);
114126
}

llvm/lib/Transforms/IPO/SampleProfile.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ class SampleProfileMatcher {
477477
std::map<LineLocation, StringRef> &IRAnchors);
478478
void findProfileAnchors(const FunctionSamples &FS,
479479
std::map<LineLocation, StringSet<>> &ProfileAnchors);
480+
void countMismatchedSamples(const FunctionSamples &FS);
480481
void countProfileMismatches(
481482
const Function &F, const FunctionSamples &FS,
482483
const std::map<LineLocation, StringRef> &IRAnchors,
@@ -2166,19 +2167,36 @@ void SampleProfileMatcher::findIRAnchors(
21662167
}
21672168
}
21682169

2170+
void SampleProfileMatcher::countMismatchedSamples(const FunctionSamples &FS) {
2171+
const auto *FuncDesc = ProbeManager->getDesc(FS.getName());
2172+
// Skip the function that is external or renamed.
2173+
if (!FuncDesc)
2174+
return;
2175+
2176+
if (ProbeManager->profileIsHashMismatched(*FuncDesc, FS)) {
2177+
MismatchedFuncHashSamples += FS.getTotalSamples();
2178+
return;
2179+
}
2180+
for (const auto &I : FS.getCallsiteSamples())
2181+
for (const auto &CS : I.second)
2182+
countMismatchedSamples(CS.second);
2183+
}
2184+
21692185
void SampleProfileMatcher::countProfileMismatches(
21702186
const Function &F, const FunctionSamples &FS,
21712187
const std::map<LineLocation, StringRef> &IRAnchors,
21722188
const std::map<LineLocation, StringSet<>> &ProfileAnchors) {
21732189
bool IsFuncHashMismatch = false;
21742190
if (FunctionSamples::ProfileIsProbeBased) {
2175-
uint64_t Count = FS.getTotalSamples();
2176-
TotalFuncHashSamples += Count;
2191+
TotalFuncHashSamples += FS.getTotalSamples();
21772192
TotalProfiledFunc++;
2178-
if (!ProbeManager->profileIsValid(F, FS)) {
2179-
MismatchedFuncHashSamples += Count;
2180-
NumMismatchedFuncHash++;
2181-
IsFuncHashMismatch = true;
2193+
const auto *FuncDesc = ProbeManager->getDesc(F);
2194+
if (FuncDesc) {
2195+
if (ProbeManager->profileIsHashMismatched(*FuncDesc, FS)) {
2196+
NumMismatchedFuncHash++;
2197+
IsFuncHashMismatch = true;
2198+
}
2199+
countMismatchedSamples(FS);
21822200
}
21832201
}
21842202

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
main:300:0
2+
1: 0
3+
12: 10 matched:10
4+
20: 10 bar:10
5+
13: bar:200
6+
1: 100
7+
!CFGChecksum: 123
8+
!CFGChecksum: 844635331715433
9+
bar:11:11
10+
1: 11
11+
# Original CFGChecksum is 4294967295
12+
!CFGChecksum: 123

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
; RUN: llvm-objdump --section-headers %t.obj | FileCheck %s --check-prefix=CHECK-OBJ
77
; RUN: llc < %t.ll -filetype=asm -o - | FileCheck %s --check-prefix=CHECK-ASM
88

9+
; 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
10+
11+
912
; CHECK: (1/3) of functions' profile are invalid and (10/50) of samples are discarded due to function hash mismatch.
1013
; CHECK: (2/3) of callsites' profile are invalid and (20/30) of samples are discarded due to callsite location mismatch.
1114

@@ -47,6 +50,9 @@
4750
; CHECK-ASM: .byte 4
4851
; CHECK-ASM: .ascii "MzA="
4952

53+
; CHECK-NESTED: (1/2) of functions' profile are invalid and (211/311) of samples are discarded due to function hash mismatch.
54+
55+
5056
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
5157
target triple = "x86_64-unknown-linux-gnu"
5258

0 commit comments

Comments
 (0)