Skip to content

Commit 8da1e73

Browse files
authored
Merge pull request swiftlang#66053 from hamishknight/filing-cabinet
2 parents f0595af + 9dc2d04 commit 8da1e73

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

lib/IRGen/GenCoverage.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,19 @@ void IRGenModule::emitCoverageMaps(ArrayRef<const SILCoverageMap *> Mappings) {
8484
llvm::getCoverageUnusedNamesVarName());
8585
}
8686

87-
std::vector<StringRef> Files;
87+
llvm::DenseMap<StringRef, unsigned> RawFileIndices;
88+
llvm::SmallVector<StringRef, 8> RawFiles;
8889
for (const auto &M : Mappings) {
89-
if (std::find(Files.begin(), Files.end(), M->getFilename()) == Files.end())
90-
Files.push_back(M->getFilename());
90+
auto Filename = M->getFilename();
91+
auto Inserted = RawFileIndices.insert({Filename, RawFiles.size()}).second;
92+
if (!Inserted)
93+
continue;
94+
RawFiles.push_back(Filename);
9195
}
9296
const auto &Remapper = getOptions().CoveragePrefixMap;
9397

9498
llvm::SmallVector<std::string, 8> FilenameStrs;
95-
FilenameStrs.reserve(Files.size() + 1);
99+
FilenameStrs.reserve(RawFiles.size() + 1);
96100

97101
// First element needs to be the current working directory. Note if this
98102
// scheme ever changes, the FileID computation below will need updating.
@@ -103,7 +107,7 @@ void IRGenModule::emitCoverageMaps(ArrayRef<const SILCoverageMap *> Mappings) {
103107
// Following elements are the filenames present. We use their relative path,
104108
// which llvm-cov will turn back into absolute paths using the working
105109
// directory element.
106-
for (auto Name : Files)
110+
for (auto Name : RawFiles)
107111
FilenameStrs.emplace_back(Remapper.remapPath(Name));
108112

109113
// Encode the filenames.
@@ -130,9 +134,11 @@ void IRGenModule::emitCoverageMaps(ArrayRef<const SILCoverageMap *> Mappings) {
130134

131135
// The file ID needs to be bumped by 1 to account for the working directory
132136
// as the first element.
133-
unsigned FileID = 1 +
134-
std::find(Files.begin(), Files.end(), M->getFilename()) -
135-
Files.begin();
137+
unsigned FileID = [&]() {
138+
auto Result = RawFileIndices.find(M->getFilename());
139+
assert(Result != RawFileIndices.end());
140+
return Result->second + 1;
141+
}();
136142
assert(FileID < FilenameStrs.size());
137143

138144
std::vector<CounterMappingRegion> Regions;

test/Profiler/coverage_relative_path.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
//
1111
// ABSOLUTE: @__llvm_coverage_mapping = {{.*"\\02.*root[^/\\]*nested[/\\]*coverage_relative_path\.swift}}
1212

13-
// RUN: %target-swift-frontend -profile-generate -profile-coverage-mapping -Xllvm -enable-name-compression=false -coverage-prefix-map %/t/root=. -emit-ir %/t/root/nested/coverage_relative_path.swift | %FileCheck -check-prefix=RELATIVE %s
13+
// RUN: %target-swift-frontend -profile-generate -profile-coverage-mapping -Xllvm -enable-name-compression=false -coverage-prefix-map %t/root=. -emit-ir %t/root/nested/coverage_relative_path.swift | %FileCheck -check-prefix=RELATIVE %s
1414
//
1515
// RELATIVE: @__llvm_coverage_mapping = {{.*"\\02.*\\01[^/]*\.[/\\]*nested[/\\]*coverage_relative_path\.swift}}

test/Profiler/coverage_relative_path_exec.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@
44

55
// This test is to make sure llvm-cov can deal with a coverage-prefix-map.
66

7-
// To make sure this test is resilient to directory changes, we create nested directories inside of the
8-
// temporary test directory and assert those exist, or don't exist, in the emitted ir
7+
// To make sure this test is resilient to directory changes, we create nested
8+
// directories inside of the temporary test directory.
99
//
1010
// RUN: %empty-directory(%t)
1111
// RUN: mkdir -p %t/root/nested
1212
// RUN: echo "func coverage() {}" > %t/root/nested/coverage_relative_path.swift
1313
// RUN: cd %t/root
1414

15-
// RUN: %target-build-swift -profile-generate -profile-coverage-mapping -Xfrontend -coverage-prefix-map -Xfrontend %/t/root=. -Xfrontend -disable-incremental-llvm-codegen -o %t/main %/t/root/nested/coverage_relative_path.swift
15+
// RUN: %target-build-swift -profile-generate -profile-coverage-mapping -Xfrontend -coverage-prefix-map -Xfrontend %t/root=. -Xfrontend -disable-incremental-llvm-codegen -o %t/main %t/root/nested/coverage_relative_path.swift
1616

1717
// This unusual use of 'sh' allows the path of the profraw file to be
1818
// substituted by %target-run.
1919
// RUN: %target-codesign %t/main
2020
// RUN: %target-run sh -c 'env LLVM_PROFILE_FILE=$1 $2' -- %t/default.profraw %t/main
2121

2222
// RUN: %llvm-profdata merge %t/default.profraw -o %t/default.profdata
23-
// RUN: %llvm-cov show %t/main -instr-profile=%t/default.profdata | %FileCheck %s
23+
// RUN: %llvm-cov show %t/main -instr-profile=%t/default.profdata | %FileCheck --check-prefix SHOW %s
24+
// RUN: %llvm-cov report %t/main -instr-profile=%t/default.profdata | %FileCheck --check-prefix REPORT %s
2425

25-
// CHECK: func coverage
26+
// SHOW: func coverage
27+
// REPORT: coverage_relative_path.swift

0 commit comments

Comments
 (0)