Skip to content

Commit 41d03b0

Browse files
author
Andres Wearden
committed
changed from hashing arches to hashing executables in llvm-profdata and llvm-cov
1 parent 7696733 commit 41d03b0

File tree

6 files changed

+49
-41
lines changed

6 files changed

+49
-41
lines changed

llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ class CoverageMapping {
10191019
ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
10201020
std::optional<std::reference_wrapper<IndexedInstrProfReader>>
10211021
&ProfileReader,
1022-
CoverageMapping &Coverage, StringRef Arch, StringRef ObjectFilename = "");
1022+
CoverageMapping &Coverage, StringRef Arch, StringRef ObjectFilename = "", bool ShowArchExecutables = false);
10231023

10241024
static Error loadFromReaders(
10251025
ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
@@ -1033,15 +1033,18 @@ class CoverageMapping {
10331033
std::optional<std::reference_wrapper<IndexedInstrProfReader>>
10341034
&ProfileReader,
10351035
CoverageMapping &Coverage, bool &DataFound,
1036-
SmallVectorImpl<object::BuildID> *FoundBinaryIDs = nullptr, StringRef ObjectFilename = "");
1036+
SmallVectorImpl<object::BuildID> *FoundBinaryIDs = nullptr, StringRef ObjectFilename = "",
1037+
bool ShowArchExecutables = false);
10371038

10381039
/// Add a function record corresponding to \p Record.
10391040
Error loadFunctionRecord(
10401041
const CoverageMappingRecord &Record,
10411042
const std::optional<std::reference_wrapper<IndexedInstrProfReader>>
1042-
&ProfileReader, StringRef ObjectFilename = "");
1043+
&ProfileReader, StringRef ObjectFilename = "", bool ShowArchExecutables = false);
10431044

1044-
Error loadFunctionRecord(const CoverageMappingRecord &Record, const std::optional<std::reference_wrapper<IndexedInstrProfReader>> &ProfileReader, const std::string &Arch, StringRef ObjectFilename = "");
1045+
Error loadFunctionRecord(const CoverageMappingRecord &Record,
1046+
const std::optional<std::reference_wrapper<IndexedInstrProfReader>> &ProfileReader,
1047+
const std::string &Arch, StringRef ObjectFilename = "");
10451048

10461049
/// Look up the indices for function records which are at least partially
10471050
/// defined in the specified file. This is guaranteed to return a superset of
@@ -1075,7 +1078,7 @@ class CoverageMapping {
10751078
std::optional<StringRef> ProfileFilename, vfs::FileSystem &FS,
10761079
ArrayRef<StringRef> Arches = {}, StringRef CompilationDir = "",
10771080
const object::BuildIDFetcher *BIDFetcher = nullptr,
1078-
bool CheckBinaryIDs = false);
1081+
bool CheckBinaryIDs = false, bool ShowArchExecutables = false);
10791082

10801083
/// The number of functions that couldn't have their profiles mapped.
10811084
///

llvm/lib/ProfileData/Coverage/CoverageMapping.cpp

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ class MCDCDecisionRecorder {
976976
Error CoverageMapping::loadFunctionRecord(
977977
const CoverageMappingRecord &Record,
978978
const std::optional<std::reference_wrapper<IndexedInstrProfReader>>
979-
&ProfileReader, StringRef ObjectFilename) {
979+
&ProfileReader, StringRef ObjectFilename, bool ShowArchExecutables) {
980980
StringRef OrigFuncName = Record.FunctionName;
981981
if (OrigFuncName.empty())
982982
return make_error<CoverageMapError>(coveragemap_error::malformed,
@@ -991,7 +991,7 @@ Error CoverageMapping::loadFunctionRecord(
991991

992992
uint64_t FuncArchHash = Record.FunctionHash;
993993
if(!Arch.empty()){
994-
std::string HashStr = std::to_string(Record.FunctionHash) + ":" + Arch.str();
994+
std::string HashStr = std::to_string(Record.FunctionHash) + ":" + ObjectFilename.str();
995995
llvm::StringRef HashRef(HashStr);
996996
FuncArchHash = IndexedInstrProf::ComputeHash(HashRef);
997997
}
@@ -1102,28 +1102,29 @@ Error CoverageMapping::loadFunctionRecord(
11021102

11031103
//CHANGES MADE HERE
11041104
auto FilenamesHash = hash_combine_range(Record.Filenames);
1105-
std::string HashStr = OrigFuncName.str(); /*+ ":" + Arch.str();*/
1106-
if(!Arch.empty()){
1105+
std::string HashStr = OrigFuncName.str();
1106+
if(ShowArchExecutables){
11071107
HashStr += ":" + Arch.str();
1108-
// auto LogicalFuncKey = std::make_pair(FilenamesHash, hash_value(OrigFuncName));
1109-
// auto It = RecordIndices.find(LogicalFuncKey);
1110-
// std::vector<llvm::coverage::CountedRegion> RegionsToAdd;
1111-
1112-
// if (It != RecordIndices.end()) {
1113-
// auto &ExistingFunction = Functions[It->second];
1114-
1115-
// for (const auto &NewRegion : Function.CountedRegions) {
1116-
// for (auto &ExistingRegion : ExistingFunction.CountedRegions) {
1117-
// if((NewRegion.ObjectFilename != ExistingRegion.ObjectFilename) &&
1118-
// (NewRegion.startLoc() >= ExistingRegion.startLoc()) &&
1119-
// (NewRegion.endLoc() <= ExistingRegion.endLoc())){
1120-
// RegionsToAdd.push_back(NewRegion);
1121-
// }
1122-
// }
1123-
// }
1124-
// ExistingFunction.CountedRegions.insert(ExistingFunction.CountedRegions.end(), RegionsToAdd.begin(), RegionsToAdd.end());
1125-
// }
1126-
// RecordIndices[LogicalFuncKey] = Functions.size();
1108+
}else{
1109+
auto LogicalFuncKey = std::make_pair(FilenamesHash, hash_value(OrigFuncName));
1110+
auto It = RecordIndices.find(LogicalFuncKey);
1111+
std::vector<llvm::coverage::CountedRegion> RegionsToAdd;
1112+
1113+
if (It != RecordIndices.end()) {
1114+
auto &ExistingFunction = Functions[It->second];
1115+
1116+
for (const auto &NewRegion : Function.CountedRegions) {
1117+
for (auto &ExistingRegion : ExistingFunction.CountedRegions) {
1118+
if((NewRegion.ObjectFilename != ExistingRegion.ObjectFilename) &&
1119+
(NewRegion.startLoc() >= ExistingRegion.startLoc()) &&
1120+
(NewRegion.endLoc() <= ExistingRegion.endLoc())){
1121+
RegionsToAdd.push_back(NewRegion);
1122+
}
1123+
}
1124+
}
1125+
ExistingFunction.CountedRegions.insert(ExistingFunction.CountedRegions.end(), RegionsToAdd.begin(), RegionsToAdd.end());
1126+
}
1127+
RecordIndices[LogicalFuncKey] = Functions.size();
11271128
}
11281129
//CHANGES MADE HERE
11291130

@@ -1180,7 +1181,7 @@ Error CoverageMapping::loadFromReaders(
11801181
ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
11811182
std::optional<std::reference_wrapper<IndexedInstrProfReader>>
11821183
&ProfileReader,
1183-
CoverageMapping &Coverage, StringRef Arch, StringRef ObjectFilename) {
1184+
CoverageMapping &Coverage, StringRef Arch, StringRef ObjectFilename, bool ShowArchExecutables) {
11841185

11851186
Coverage.setArchitecture(Arch);
11861187
assert(!Coverage.SingleByteCoverage || !ProfileReader ||
@@ -1193,7 +1194,7 @@ Error CoverageMapping::loadFromReaders(
11931194
if (Error E = RecordOrErr.takeError())
11941195
return E;
11951196
const auto &Record = *RecordOrErr;
1196-
if (Error E = Coverage.loadFunctionRecord(Record, ProfileReader, ObjectFilename))
1197+
if (Error E = Coverage.loadFunctionRecord(Record, ProfileReader, ObjectFilename, ShowArchExecutables))
11971198
return E;
11981199
}
11991200
}
@@ -1224,7 +1225,7 @@ Error CoverageMapping::loadFromFile(
12241225
std::optional<std::reference_wrapper<IndexedInstrProfReader>>
12251226
&ProfileReader,
12261227
CoverageMapping &Coverage, bool &DataFound,
1227-
SmallVectorImpl<object::BuildID> *FoundBinaryIDs, StringRef ObjectFilename) {
1228+
SmallVectorImpl<object::BuildID> *FoundBinaryIDs, StringRef ObjectFilename, bool ShowArchExecutables) {
12281229
auto CovMappingBufOrErr = MemoryBuffer::getFileOrSTDIN(
12291230
Filename, /*IsText=*/false, /*RequiresNullTerminator=*/false);
12301231
if (std::error_code EC = CovMappingBufOrErr.getError())
@@ -1256,7 +1257,7 @@ Error CoverageMapping::loadFromFile(
12561257
}));
12571258
}
12581259
DataFound |= !Readers.empty();
1259-
if (Error E = loadFromReaders(Readers, ProfileReader, Coverage, Arch, ObjectFilename))
1260+
if (Error E = loadFromReaders(Readers, ProfileReader, Coverage, Arch, ObjectFilename, ShowArchExecutables))
12601261
return createFileError(Filename, std::move(E));
12611262
return Error::success();
12621263
}
@@ -1265,7 +1266,7 @@ Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
12651266
ArrayRef<StringRef> ObjectFilenames,
12661267
std::optional<StringRef> ProfileFilename, vfs::FileSystem &FS,
12671268
ArrayRef<StringRef> Arches, StringRef CompilationDir,
1268-
const object::BuildIDFetcher *BIDFetcher, bool CheckBinaryIDs) {
1269+
const object::BuildIDFetcher *BIDFetcher, bool CheckBinaryIDs, bool ShowArchExecutables) {
12691270
std::unique_ptr<IndexedInstrProfReader> ProfileReader;
12701271
if (ProfileFilename) {
12711272
auto ProfileReaderOrErr =
@@ -1297,7 +1298,8 @@ Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
12971298
for (const auto &File : llvm::enumerate(ObjectFilenames)) {
12981299
if (Error E = loadFromFile(File.value(), GetArch(File.index()),
12991300
CompilationDir, ProfileReaderRef, *Coverage,
1300-
DataFound, &FoundBinaryIDs, ObjectFilenames[File.index()]))
1301+
DataFound, &FoundBinaryIDs, ObjectFilenames[File.index()],
1302+
ShowArchExecutables))
13011303
return std::move(E);
13021304
}
13031305

llvm/tools/llvm-cov/CodeCoverage.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,8 @@ std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
486486
auto FS = vfs::getRealFileSystem();
487487
auto CoverageOrErr = CoverageMapping::load(
488488
ObjectFilenames, PGOFilename, *FS, CoverageArches,
489-
ViewOpts.CompilationDirectory, BIDFetcher.get(), CheckBinaryIDs);
489+
ViewOpts.CompilationDirectory, BIDFetcher.get(), CheckBinaryIDs,
490+
ViewOpts.ShowArchExecutables);
490491
if (Error E = CoverageOrErr.takeError()) {
491492
error("failed to load coverage: " + toString(std::move(E)));
492493
return nullptr;
@@ -824,6 +825,10 @@ int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
824825
cl::opt<bool> CheckBinaryIDs(
825826
"check-binary-ids", cl::desc("Fail if an object couldn't be found for a "
826827
"binary ID in the profile"));
828+
cl::opt<bool> ShowArchExecutables(
829+
"show-arch-executables",
830+
cl::desc("Show coverage per architecture and the associated executable slice"),
831+
cl::init(false));
827832

828833
auto commandLineParser = [&, this](int argc, const char **argv) -> int {
829834
cl::ParseCommandLineOptions(argc, argv, "LLVM code coverage tool\n");
@@ -990,6 +995,7 @@ int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
990995
ViewOpts.ExportSummaryOnly = SummaryOnly;
991996
ViewOpts.NumThreads = NumThreads;
992997
ViewOpts.CompilationDirectory = CompilationDirectory;
998+
ViewOpts.ShowArchExecutables = ShowArchExecutables;
993999

9941000
return 0;
9951001
};

llvm/tools/llvm-cov/CoverageViewOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct CoverageViewOptions {
4747
bool SkipFunctions;
4848
bool SkipBranches;
4949
bool BinaryCounters;
50+
bool ShowArchExecutables;
5051
OutputFormat Format;
5152
BranchOutputType ShowBranches;
5253
std::string ShowOutputDirectory;

llvm/tools/llvm-cov/SourceCoverageViewText.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,7 @@ void SourceCoverageViewText::renderArchLineCoverageColumn(
229229
OS.indent(LineCoverageColumnWidth) << '|';
230230
return;
231231
}
232-
std::string C = "";
233-
for(LineCoverageStats Counts : LineArchStats[Line.getLine()]){
234-
C += formatBinaryCount(Counts.getExecutionCount()) + "/";
235-
}
236-
// std::string C = formatBinaryCount(Line.getExecutionCount());
232+
std::string C = formatBinaryCount(Line.getExecutionCount());
237233
OS.indent(LineCoverageColumnWidth - C.size());
238234
colored_ostream(OS, raw_ostream::MAGENTA, Line.hasMultipleRegions() && getOptions().Colors) << C;
239235
OS << '|';

llvm/tools/llvm-profdata/llvm-profdata.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ loadInput(const WeightedFile &Input, SymbolRemapper *Remapper,
743743
consumeError(ArchOrError.takeError());
744744
Architecture = "unknown";
745745
}
746-
// Architecture = ExeRef;
746+
Architecture = ExeRef;
747747
}
748748
//ANDRES CODE
749749

0 commit comments

Comments
 (0)