Skip to content

Commit b3207de

Browse files
ellishgPriyanshu3820
authored andcommitted
[profdata] Skip probes with missing counter and function pointers (llvm#163254)
1 parent 7aefa73 commit b3207de

File tree

3 files changed

+196
-24
lines changed

3 files changed

+196
-24
lines changed

compiler-rt/test/profile/Linux/instrprof-debug-info-correlate-warnings.c

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

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",

0 commit comments

Comments
 (0)