Skip to content

Commit 5cb1780

Browse files
committed
[profdata] Skip probes with missing counter and function pointers
1 parent ad69fef commit 5cb1780

File tree

2 files changed

+192
-11
lines changed

2 files changed

+192
-11
lines changed

llvm/lib/ProfileData/InstrProfCorrelator.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
using namespace llvm;
2626

2727
/// Get profile section.
28-
Expected<object::SectionRef> getInstrProfSection(const object::ObjectFile &Obj,
29-
InstrProfSectKind IPSK) {
28+
static Expected<object::SectionRef>
29+
getInstrProfSection(const object::ObjectFile &Obj, InstrProfSectKind IPSK) {
3030
// On COFF, the getInstrProfSectionName returns the section names may followed
3131
// by "$M". The linker removes the dollar and everything after it in the final
3232
// binary. Do the same to match.
@@ -173,11 +173,10 @@ InstrProfCorrelator::get(std::unique_ptr<MemoryBuffer> Buffer,
173173
}
174174

175175
std::optional<size_t> InstrProfCorrelator::getDataSize() const {
176-
if (auto *C = dyn_cast<InstrProfCorrelatorImpl<uint32_t>>(this)) {
176+
if (auto *C = dyn_cast<InstrProfCorrelatorImpl<uint32_t>>(this))
177177
return C->getDataSize();
178-
} else if (auto *C = dyn_cast<InstrProfCorrelatorImpl<uint64_t>>(this)) {
178+
if (auto *C = dyn_cast<InstrProfCorrelatorImpl<uint64_t>>(this))
179179
return C->getDataSize();
180-
}
181180
return {};
182181
}
183182

@@ -318,9 +317,9 @@ DwarfInstrProfCorrelator<IntPtrT>::getLocation(const DWARFDie &Die) const {
318317
DataExtractor Data(Location.Expr, DICtx->isLittleEndian(), AddressSize);
319318
DWARFExpression Expr(Data, AddressSize);
320319
for (auto &Op : Expr) {
321-
if (Op.getCode() == dwarf::DW_OP_addr) {
320+
if (Op.getCode() == dwarf::DW_OP_addr)
322321
return Op.getRawOperand(0);
323-
} else if (Op.getCode() == dwarf::DW_OP_addrx) {
322+
if (Op.getCode() == dwarf::DW_OP_addrx) {
324323
uint64_t Index = Op.getRawOperand(0);
325324
if (auto SA = DU.getAddrOffsetSectionItem(Index))
326325
return SA->Address;
@@ -352,7 +351,7 @@ void DwarfInstrProfCorrelator<IntPtrT>::correlateProfileDataImpl(
352351
bool UnlimitedWarnings = (MaxWarnings == 0);
353352
// -N suppressed warnings means we can emit up to N (unsuppressed) warnings
354353
int NumSuppressedWarnings = -MaxWarnings;
355-
auto maybeAddProbe = [&](DWARFDie Die) {
354+
auto MaybeAddProbe = [&](DWARFDie Die) {
356355
if (!isDIEOfProbe(Die))
357356
return;
358357
std::optional<const char *> FunctionName;
@@ -385,6 +384,9 @@ void DwarfInstrProfCorrelator<IntPtrT>::correlateProfileDataImpl(
385384
NumCounters = AnnotationFormValue->getAsUnsignedConstant();
386385
}
387386
}
387+
// If there is no function and no counter, assume it was dead-stripped
388+
if (!FunctionPtr && !CounterPtr)
389+
return;
388390
if (!FunctionName || !CFGHash || !CounterPtr || !NumCounters) {
389391
if (UnlimitedWarnings || ++NumSuppressedWarnings < 1) {
390392
WithColor::warning()
@@ -418,7 +420,7 @@ void DwarfInstrProfCorrelator<IntPtrT>::correlateProfileDataImpl(
418420
if (Data) {
419421
InstrProfCorrelator::Probe P;
420422
P.FunctionName = *FunctionName;
421-
if (auto Name = FnDie.getName(DINameKind::LinkageName))
423+
if (const char *Name = FnDie.getName(DINameKind::LinkageName))
422424
P.LinkageName = Name;
423425
P.CFGHash = *CFGHash;
424426
P.CounterOffset = CounterOffset;
@@ -438,10 +440,10 @@ void DwarfInstrProfCorrelator<IntPtrT>::correlateProfileDataImpl(
438440
};
439441
for (auto &CU : DICtx->normal_units())
440442
for (const auto &Entry : CU->dies())
441-
maybeAddProbe(DWARFDie(CU.get(), &Entry));
443+
MaybeAddProbe(DWARFDie(CU.get(), &Entry));
442444
for (auto &CU : DICtx->dwo_units())
443445
for (const auto &Entry : CU->dies())
444-
maybeAddProbe(DWARFDie(CU.get(), &Entry));
446+
MaybeAddProbe(DWARFDie(CU.get(), &Entry));
445447

446448
if (!UnlimitedWarnings && NumSuppressedWarnings > 0)
447449
WithColor::warning() << format("Suppressed %d additional warnings\n",
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
; RUN: split-file %s %t
2+
; RUN: clang %t/a.ll -o %t/a.out
3+
; RUN: llvm-profdata merge --debug-info=%t/a.out %t/a.proftext -o %t/a.profdata 2>&1 | FileCheck %s --implicit-check-not=warning
4+
5+
; CHECK: warning: Incomplete DIE for function None:
6+
; CHECK: warning: Incomplete DIE for function no_cfg: CFGHash=None
7+
; CHECK: warning: Incomplete DIE for function no_counter: {{.*}} NumCounters=None
8+
; CHECK: warning: Incomplete DIE for function no_profc: {{.*}} CounterPtr=None
9+
; CHECK: warning: Could not find address of function no_func
10+
11+
;--- a.proftext
12+
:ir
13+
14+
;--- a.c
15+
int main() { return 0; }
16+
17+
void removed() {}
18+
void no_name() {}
19+
void no_cfg() {}
20+
void no_counter() {}
21+
void no_profc() {}
22+
void no_func() {}
23+
;--- gen
24+
clang --target=x86_64-linux -fprofile-generate -mllvm -debug-info-correlate -S -emit-llvm -g a.c -o -
25+
# NOTE: After generating the IR below, manually remove the follwing pieces
26+
# 1. Remove "@removed" function and "@__profc_removed" global
27+
# 2. Remove "Function Name" annotation for "@no_name"
28+
# 3. Remove "CFG Hash" annotation for "@no_cfg"
29+
# 4. Remove "Num Counters" annotation for "@no_counter"
30+
# 5. Remove "@__profc_no_profc"
31+
# 6. Remove "@no_func"
32+
;--- a.ll
33+
; ModuleID = 'a.c'
34+
source_filename = "a.c"
35+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
36+
target triple = "x86_64-unknown-linux"
37+
38+
$__llvm_profile_raw_version = comdat any
39+
40+
$__profc_main = comdat nodeduplicate
41+
42+
$__profc_removed = comdat nodeduplicate
43+
44+
$__profc_no_name = comdat nodeduplicate
45+
46+
$__profc_no_cfg = comdat nodeduplicate
47+
48+
$__profc_no_counter = comdat nodeduplicate
49+
50+
$__profc_no_profc = comdat nodeduplicate
51+
52+
$__profc_no_func = comdat nodeduplicate
53+
54+
$__llvm_profile_filename = comdat any
55+
56+
@__llvm_profile_raw_version = hidden constant i64 648518346341351434, comdat
57+
@__profn_main = private constant [4 x i8] c"main"
58+
@__profn_removed = private constant [7 x i8] c"removed"
59+
@__profn_no_name = private constant [7 x i8] c"no_name"
60+
@__profn_no_cfg = private constant [6 x i8] c"no_cfg"
61+
@__profn_no_counter = private constant [10 x i8] c"no_counter"
62+
@__profn_no_profc = private constant [8 x i8] c"no_profc"
63+
@__profn_no_func = private constant [7 x i8] c"no_func"
64+
@__profc_main = private global [1 x i64] zeroinitializer, section "__llvm_prf_cnts", comdat, align 8, !dbg !0
65+
@__profc_no_name = private global [1 x i64] zeroinitializer, section "__llvm_prf_cnts", comdat, align 8, !dbg !19
66+
@__profc_no_cfg = private global [1 x i64] zeroinitializer, section "__llvm_prf_cnts", comdat, align 8, !dbg !24
67+
@__profc_no_counter = private global [1 x i64] zeroinitializer, section "__llvm_prf_cnts", comdat, align 8, !dbg !29
68+
@__profc_no_func = private global [1 x i64] zeroinitializer, section "__llvm_prf_cnts", comdat, align 8, !dbg !39
69+
@llvm.compiler.used = appending global [5 x ptr] [ptr @__profc_main, ptr @__profc_no_name, ptr @__profc_no_cfg, ptr @__profc_no_counter, ptr @__profc_no_func], section "llvm.metadata"
70+
@__llvm_profile_filename = hidden constant [20 x i8] c"default_%m.proflite\00", comdat
71+
72+
; Function Attrs: noinline nounwind optnone uwtable
73+
define dso_local i32 @main() #0 !dbg !2 {
74+
%1 = alloca i32, align 4
75+
%2 = load i64, ptr @__profc_main, align 8
76+
%3 = add i64 %2, 1
77+
store i64 %3, ptr @__profc_main, align 8
78+
store i32 0, ptr %1, align 4
79+
ret i32 0, !dbg !53
80+
}
81+
82+
; Function Attrs: noinline nounwind optnone uwtable
83+
define dso_local void @no_name() #0 !dbg !21 {
84+
%1 = load i64, ptr @__profc_no_name, align 8, !dbg !55
85+
%2 = add i64 %1, 1, !dbg !55
86+
store i64 %2, ptr @__profc_no_name, align 8, !dbg !55
87+
ret void, !dbg !55
88+
}
89+
90+
; Function Attrs: noinline nounwind optnone uwtable
91+
define dso_local void @no_cfg() #0 !dbg !26 {
92+
%1 = load i64, ptr @__profc_no_cfg, align 8, !dbg !56
93+
%2 = add i64 %1, 1, !dbg !56
94+
store i64 %2, ptr @__profc_no_cfg, align 8, !dbg !56
95+
ret void, !dbg !56
96+
}
97+
98+
; Function Attrs: noinline nounwind optnone uwtable
99+
define dso_local void @no_counter() #0 !dbg !31 {
100+
%1 = load i64, ptr @__profc_no_counter, align 8, !dbg !57
101+
%2 = add i64 %1, 1, !dbg !57
102+
store i64 %2, ptr @__profc_no_counter, align 8, !dbg !57
103+
ret void, !dbg !57
104+
}
105+
106+
; Function Attrs: noinline nounwind optnone uwtable
107+
define dso_local void @no_profc() #0 !dbg !36 {
108+
ret void, !dbg !58
109+
}
110+
111+
; Function Attrs: nounwind
112+
declare void @llvm.instrprof.increment(ptr, i64, i32, i32) #1
113+
114+
attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
115+
attributes #1 = { nounwind }
116+
117+
!llvm.dbg.cu = !{!7}
118+
!llvm.module.flags = !{!46, !47, !48, !49, !50, !51, !52}
119+
120+
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
121+
!1 = distinct !DIGlobalVariable(name: "__profc_main", scope: !2, file: !3, type: !14, isLocal: true, isDefinition: true, annotations: !44)
122+
!2 = distinct !DISubprogram(name: "main", scope: !3, file: !3, line: 1, type: !4, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !7)
123+
!3 = !DIFile(filename: "a.c", directory: "/proc/self/cwd", checksumkind: CSK_MD5, checksum: "22eee0eada6e6964fca794aa5a0966d0")
124+
!4 = !DISubroutineType(types: !5)
125+
!5 = !{!6}
126+
!6 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
127+
!7 = distinct !DICompileUnit(language: DW_LANG_C11, file: !3, isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, globals: !8, splitDebugInlining: false, nameTableKind: None)
128+
!8 = !{!0, !9, !19, !24, !29, !34, !39}
129+
!9 = !DIGlobalVariableExpression(var: !10, expr: !DIExpression())
130+
!10 = distinct !DIGlobalVariable(name: "__profc_removed", scope: !11, file: !3, type: !14, isLocal: true, isDefinition: true, annotations: !15)
131+
!11 = distinct !DISubprogram(name: "removed", scope: !3, file: !3, line: 3, type: !12, scopeLine: 3, spFlags: DISPFlagDefinition, unit: !7)
132+
!12 = !DISubroutineType(types: !13)
133+
!13 = !{null}
134+
!14 = !DIBasicType(tag: DW_TAG_unspecified_type, name: "Profile Data Type")
135+
!15 = !{!16, !17, !18}
136+
!16 = !{!"Function Name", !"removed"}
137+
!17 = !{!"CFG Hash", i64 742261418966908927}
138+
!18 = !{!"Num Counters", i32 1}
139+
!19 = !DIGlobalVariableExpression(var: !20, expr: !DIExpression())
140+
!20 = distinct !DIGlobalVariable(name: "__profc_no_name", scope: !21, file: !3, type: !14, isLocal: true, isDefinition: true, annotations: !22)
141+
!21 = distinct !DISubprogram(name: "no_name", scope: !3, file: !3, line: 4, type: !12, scopeLine: 4, spFlags: DISPFlagDefinition, unit: !7)
142+
!22 = !{!17, !18}
143+
!23 = !{!"Function Name", !"no_name"}
144+
!24 = !DIGlobalVariableExpression(var: !25, expr: !DIExpression())
145+
!25 = distinct !DIGlobalVariable(name: "__profc_no_cfg", scope: !26, file: !3, type: !14, isLocal: true, isDefinition: true, annotations: !27)
146+
!26 = distinct !DISubprogram(name: "no_cfg", scope: !3, file: !3, line: 5, type: !12, scopeLine: 5, spFlags: DISPFlagDefinition, unit: !7)
147+
!27 = !{!28, !18}
148+
!28 = !{!"Function Name", !"no_cfg"}
149+
!29 = !DIGlobalVariableExpression(var: !30, expr: !DIExpression())
150+
!30 = distinct !DIGlobalVariable(name: "__profc_no_counter", scope: !31, file: !3, type: !14, isLocal: true, isDefinition: true, annotations: !32)
151+
!31 = distinct !DISubprogram(name: "no_counter", scope: !3, file: !3, line: 6, type: !12, scopeLine: 6, spFlags: DISPFlagDefinition, unit: !7)
152+
!32 = !{!33, !17}
153+
!33 = !{!"Function Name", !"no_counter"}
154+
!34 = !DIGlobalVariableExpression(var: !35, expr: !DIExpression())
155+
!35 = distinct !DIGlobalVariable(name: "__profc_no_profc", scope: !36, file: !3, type: !14, isLocal: true, isDefinition: true, annotations: !37)
156+
!36 = distinct !DISubprogram(name: "no_profc", scope: !3, file: !3, line: 7, type: !12, scopeLine: 7, spFlags: DISPFlagDefinition, unit: !7)
157+
!37 = !{!38, !17, !18}
158+
!38 = !{!"Function Name", !"no_profc"}
159+
!39 = !DIGlobalVariableExpression(var: !40, expr: !DIExpression())
160+
!40 = distinct !DIGlobalVariable(name: "__profc_no_func", scope: !41, file: !3, type: !14, isLocal: true, isDefinition: true, annotations: !42)
161+
!41 = distinct !DISubprogram(name: "no_func", scope: !3, file: !3, line: 8, type: !12, scopeLine: 8, spFlags: DISPFlagDefinition, unit: !7)
162+
!42 = !{!43, !17, !18}
163+
!43 = !{!"Function Name", !"no_func"}
164+
!44 = !{!45, !17, !18}
165+
!45 = !{!"Function Name", !"main"}
166+
!46 = !{i32 7, !"Dwarf Version", i32 5}
167+
!47 = !{i32 2, !"Debug Info Version", i32 3}
168+
!48 = !{i32 1, !"wchar_size", i32 4}
169+
!49 = !{i32 8, !"PIC Level", i32 2}
170+
!50 = !{i32 7, !"PIE Level", i32 2}
171+
!51 = !{i32 7, !"uwtable", i32 2}
172+
!52 = !{i32 7, !"frame-pointer", i32 2}
173+
!53 = !DILocation(line: 1, column: 14, scope: !2)
174+
!54 = !DILocation(line: 3, column: 17, scope: !11)
175+
!55 = !DILocation(line: 4, column: 17, scope: !21)
176+
!56 = !DILocation(line: 5, column: 16, scope: !26)
177+
!57 = !DILocation(line: 6, column: 20, scope: !31)
178+
!58 = !DILocation(line: 7, column: 18, scope: !36)
179+
!59 = !DILocation(line: 8, column: 17, scope: !41)

0 commit comments

Comments
 (0)