Skip to content

Commit 381e9d2

Browse files
committed
[Coverage] Ignore 'unused' functions with non-zero execution counts
Frontends emit 'unused' coverage mapping records for functions which are provably unused in a TU. These unused records contain a single counter with CounterKind::Zero. However, a function may be unused in one TU and used in another. When this happens, prefer the records with a full set of counters instead of arbitrarily picking the first loaded record. There is no impact on the single-TU case. In the multiple-TU case, this resolves issues causing a function to appear unused when it's not. Testing: check-{llvm,clang,compiler-rt} rdar://42981322 llvm-svn: 339194
1 parent e302fc5 commit 381e9d2

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// 1) Compile shared code into different object files and into an executable.
2+
3+
// RUN: %clangxx_profgen -fcoverage-mapping %s -c -o %t.v1.o -D_VERSION_1
4+
// RUN: %clangxx_profgen -fcoverage-mapping %s -c -o %t.v2.o -D_VERSION_2
5+
// RUN: %clangxx_profgen -fcoverage-mapping %t.v1.o %t.v2.o -o %t.exe
6+
7+
// 2) Collect profile data.
8+
9+
// RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t.exe
10+
// RUN: llvm-profdata merge %t.profraw -o %t.profdata
11+
12+
// 3) Generate coverage reports from the different object files and the exe.
13+
14+
// RUN: llvm-cov show %t.v1.o -instr-profile=%t.profdata | FileCheck %s -check-prefixes=V1,V1-ONLY
15+
// RUN: llvm-cov show %t.v2.o -instr-profile=%t.profdata | FileCheck %s -check-prefixes=V2,V2-ONLY
16+
// RUN: llvm-cov show %t.v1.o -object %t.v2.o -instr-profile=%t.profdata | FileCheck %s -check-prefixes=V1,V2
17+
// RUN: llvm-cov show %t.exe -instr-profile=%t.profdata | FileCheck %s -check-prefixes=V1,V2
18+
19+
// 4) Verify that coverage reporting on the aggregate coverage mapping shows
20+
// hits for all code. (We used to arbitrarily pick a mapping from one binary
21+
// and prefer it over others.) When only limited coverage information is
22+
// available (just from one binary), don't try to guess any region counts.
23+
24+
struct A {
25+
A() {} // V1: [[@LINE]]{{ *}}|{{ *}}1
26+
// V1-ONLY: [[@LINE+1]]{{ *}}|{{ *}}|
27+
A(int) {} // V2-ONLY: [[@LINE-2]]{{ *}}|{{ *}}|
28+
// V2: [[@LINE-1]]{{ *}}|{{ *}}1
29+
};
30+
31+
#ifdef _VERSION_1
32+
33+
void foo();
34+
35+
void bar() {
36+
A x; // V1: [[@LINE]]{{ *}}|{{ *}}1
37+
}
38+
39+
int main() {
40+
foo(); // V1: [[@LINE]]{{ *}}|{{ *}}1
41+
bar();
42+
return 0;
43+
}
44+
45+
#endif // _VERSION_1
46+
47+
#ifdef _VERSION_2
48+
49+
void foo() {
50+
A x{0}; // V2: [[@LINE]]{{ *}}|{{ *}}1
51+
}
52+
53+
#endif // _VERSION_2

llvm/lib/ProfileData/Coverage/CoverageMapping.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,6 @@ Error CoverageMapping::loadFunctionRecord(
207207
else
208208
OrigFuncName = getFuncNameWithoutPrefix(OrigFuncName, Record.Filenames[0]);
209209

210-
// Don't load records for (filenames, function) pairs we've already seen.
211-
auto FilenamesHash = hash_combine_range(Record.Filenames.begin(),
212-
Record.Filenames.end());
213-
if (!RecordProvenance[FilenamesHash].insert(hash_value(OrigFuncName)).second)
214-
return Error::success();
215-
216210
CounterMappingContext Ctx(Record.Expressions);
217211

218212
std::vector<uint64_t> Counts;
@@ -230,6 +224,15 @@ Error CoverageMapping::loadFunctionRecord(
230224

231225
assert(!Record.MappingRegions.empty() && "Function has no regions");
232226

227+
// This coverage record is a zero region for a function that's unused in
228+
// some TU, but used in a different TU. Ignore it. The coverage maps from the
229+
// the other TU will either be loaded (providing full region counts) or they
230+
// won't (in which case we don't unintuitively report functions as uncovered
231+
// when they have non-zero counts in the profile).
232+
if (Record.MappingRegions.size() == 1 &&
233+
Record.MappingRegions[0].Count.isZero() && Counts[0] > 0)
234+
return Error::success();
235+
233236
FunctionRecord Function(OrigFuncName, Record.Filenames);
234237
for (const auto &Region : Record.MappingRegions) {
235238
Expected<int64_t> ExecutionCount = Ctx.evaluate(Region.Count);
@@ -240,6 +243,12 @@ Error CoverageMapping::loadFunctionRecord(
240243
Function.pushRegion(Region, *ExecutionCount);
241244
}
242245

246+
// Don't create records for (filenames, function) pairs we've already seen.
247+
auto FilenamesHash = hash_combine_range(Record.Filenames.begin(),
248+
Record.Filenames.end());
249+
if (!RecordProvenance[FilenamesHash].insert(hash_value(OrigFuncName)).second)
250+
return Error::success();
251+
243252
Functions.push_back(std::move(Function));
244253
return Error::success();
245254
}

0 commit comments

Comments
 (0)