Skip to content

Commit e88c4ea

Browse files
author
Andres Wearden
committed
committing to save updates
1 parent 4b25f41 commit e88c4ea

File tree

8 files changed

+141
-33
lines changed

8 files changed

+141
-33
lines changed

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/ADT/DenseMap.h"
2020
#include "llvm/ADT/DenseSet.h"
2121
#include "llvm/ADT/Hashing.h"
22+
#include "llvm/ADT/SmallVector.h"
2223
#include "llvm/ADT/StringRef.h"
2324
#include "llvm/ADT/iterator.h"
2425
#include "llvm/ADT/iterator_range.h"
@@ -375,6 +376,7 @@ struct CountedRegion : public CounterMappingRegion {
375376
uint64_t FalseExecutionCount;
376377
bool TrueFolded;
377378
bool FalseFolded;
379+
StringRef ObjectFilename;
378380

379381
CountedRegion(const CounterMappingRegion &R, uint64_t ExecutionCount)
380382
: CounterMappingRegion(R), ExecutionCount(ExecutionCount),
@@ -385,6 +387,12 @@ struct CountedRegion : public CounterMappingRegion {
385387
: CounterMappingRegion(R), ExecutionCount(ExecutionCount),
386388
FalseExecutionCount(FalseExecutionCount), TrueFolded(false),
387389
FalseFolded(false) {}
390+
391+
CountedRegion(const CounterMappingRegion &R, uint64_t ExecutionCount,
392+
uint64_t FalseExecutionCount, StringRef ObjectFilename)
393+
: CounterMappingRegion(R), ExecutionCount(ExecutionCount),
394+
FalseExecutionCount(FalseExecutionCount), TrueFolded(false),
395+
FalseFolded(false), ObjectFilename(ObjectFilename) {}
388396
};
389397

390398
/// MCDC Record grouping all information together.
@@ -731,9 +739,11 @@ struct FunctionRecord {
731739
std::vector<MCDCRecord> MCDCRecords;
732740
/// The number of times this function was executed.
733741
uint64_t ExecutionCount = 0;
742+
/// The executable this function was compiled in
743+
StringRef ObjectFilename;
734744

735-
FunctionRecord(StringRef Name, ArrayRef<StringRef> Filenames)
736-
: Name(Name), Filenames(Filenames.begin(), Filenames.end()) {}
745+
FunctionRecord(StringRef Name, ArrayRef<StringRef> Filenames, StringRef ObjectFilename = "")
746+
: Name(Name), Filenames(Filenames.begin(), Filenames.end()), ObjectFilename(ObjectFilename) {}
737747

738748
FunctionRecord(FunctionRecord &&FR) = default;
739749
FunctionRecord &operator=(FunctionRecord &&) = default;
@@ -752,9 +762,10 @@ struct FunctionRecord {
752762
CountedBranchRegions.back().FalseFolded = Region.FalseCount.isZero();
753763
return;
754764
}
755-
if (CountedRegions.empty())
765+
if (CountedRegions.empty()){
756766
ExecutionCount = Count;
757-
CountedRegions.emplace_back(Region, Count, FalseCount);
767+
}
768+
CountedRegions.emplace_back(Region, Count, FalseCount, ObjectFilename);
758769
}
759770
};
760771

@@ -994,6 +1005,9 @@ class CoverageMapping {
9941005
DenseMap<size_t, SmallVector<unsigned, 0>> FilenameHash2RecordIndices;
9951006
std::vector<std::pair<std::string, uint64_t>> FuncHashMismatches;
9961007
StringRef Arch;
1008+
DenseMap<std::pair<size_t, hash_code>, unsigned> RecordIndices;
1009+
1010+
std::map<std::pair<std::string, std::string>, std::vector<uint64_t>> AggregatedCounts;
9971011

9981012
std::optional<bool> SingleByteCoverage;
9991013

@@ -1004,7 +1018,7 @@ class CoverageMapping {
10041018
ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
10051019
std::optional<std::reference_wrapper<IndexedInstrProfReader>>
10061020
&ProfileReader,
1007-
CoverageMapping &Coverage, StringRef Arch);
1021+
CoverageMapping &Coverage, StringRef Arch, StringRef ObjectFilename = "");
10081022

10091023
static Error loadFromReaders(
10101024
ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
@@ -1018,15 +1032,15 @@ class CoverageMapping {
10181032
std::optional<std::reference_wrapper<IndexedInstrProfReader>>
10191033
&ProfileReader,
10201034
CoverageMapping &Coverage, bool &DataFound,
1021-
SmallVectorImpl<object::BuildID> *FoundBinaryIDs = nullptr);
1035+
SmallVectorImpl<object::BuildID> *FoundBinaryIDs = nullptr, StringRef ObjectFilename = "");
10221036

10231037
/// Add a function record corresponding to \p Record.
10241038
Error loadFunctionRecord(
10251039
const CoverageMappingRecord &Record,
10261040
const std::optional<std::reference_wrapper<IndexedInstrProfReader>>
1027-
&ProfileReader);
1041+
&ProfileReader, StringRef ObjectFilename = "");
10281042

1029-
Error loadFunctionRecord(const CoverageMappingRecord &Record, const std::optional<std::reference_wrapper<IndexedInstrProfReader>> &ProfileReader, const std::string &Arch);
1043+
Error loadFunctionRecord(const CoverageMappingRecord &Record, const std::optional<std::reference_wrapper<IndexedInstrProfReader>> &ProfileReader, const std::string &Arch, StringRef ObjectFilename = "");
10301044

10311045
/// Look up the indices for function records which are at least partially
10321046
/// defined in the specified file. This is guaranteed to return a superset of

llvm/include/llvm/ProfileData/InstrProf.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,9 +635,6 @@ class InstrProfSymtab {
635635

636636
// Map the MD5 of the symbol name to the name.
637637
Error addSymbolName(StringRef SymbolName) {
638-
// StringRef FuncName;
639-
// StringRef ArchName = Architecture;
640-
// std::tie(FuncName, ArchName) = SymbolName.split("#");
641638
if (SymbolName.empty())
642639
return make_error<InstrProfError>(instrprof_error::malformed,
643640
"symbol name is empty");
@@ -1075,6 +1072,7 @@ struct InstrProfRecord {
10751072
struct NamedInstrProfRecord : InstrProfRecord {
10761073
StringRef Name;
10771074
uint64_t Hash;
1075+
StringRef Filename;
10781076

10791077
// We reserve this bit as the flag for context sensitive profile record.
10801078
static const int CS_FLAG_IN_FUNC_HASH = 60;

llvm/lib/ProfileData/Coverage/CoverageMapping.cpp

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <stack>
4141
#include <string>
4242
#include <system_error>
43+
#include <unordered_map>
4344
#include <utility>
4445
#include <vector>
4546

@@ -975,7 +976,7 @@ class MCDCDecisionRecorder {
975976
Error CoverageMapping::loadFunctionRecord(
976977
const CoverageMappingRecord &Record,
977978
const std::optional<std::reference_wrapper<IndexedInstrProfReader>>
978-
&ProfileReader) {
979+
&ProfileReader, StringRef ObjectFilename) {
979980
StringRef OrigFuncName = Record.FunctionName;
980981
if (OrigFuncName.empty())
981982
return make_error<CoverageMapError>(coveragemap_error::malformed,
@@ -997,12 +998,10 @@ Error CoverageMapping::loadFunctionRecord(
997998

998999
std::vector<uint64_t> Counts;
9991000
if (ProfileReader) {
1000-
if (Error E = ProfileReader.value().get().getFunctionCounts(
1001-
Record.FunctionName, FuncArchHash, Counts)) {
1001+
if (Error E = ProfileReader.value().get().getFunctionCounts(Record.FunctionName, FuncArchHash, Counts)) {
10021002
instrprof_error IPE = std::get<0>(InstrProfError::take(std::move(E)));
10031003
if (IPE == instrprof_error::hash_mismatch) {
1004-
FuncHashMismatches.emplace_back(std::string(Record.FunctionName),
1005-
FuncArchHash);
1004+
FuncHashMismatches.emplace_back(std::string(Record.FunctionName), FuncArchHash);
10061005
return Error::success();
10071006
}
10081007
if (IPE != instrprof_error::unknown_function)
@@ -1035,6 +1034,7 @@ Error CoverageMapping::loadFunctionRecord(
10351034
} else {
10361035
Bitmap = BitVector(getMaxBitmapSize(Record, false));
10371036
}
1037+
10381038
Ctx.setBitmap(std::move(Bitmap));
10391039

10401040
assert(!Record.MappingRegions.empty() && "Function has no regions");
@@ -1049,7 +1049,7 @@ Error CoverageMapping::loadFunctionRecord(
10491049
return Error::success();
10501050

10511051
MCDCDecisionRecorder MCDCDecisions;
1052-
FunctionRecord Function(OrigFuncName, Record.Filenames);
1052+
FunctionRecord Function(OrigFuncName, Record.Filenames, ObjectFilename);
10531053
for (const auto &Region : Record.MappingRegions) {
10541054
// MCDCDecisionRegion should be handled first since it overlaps with
10551055
// others inside.
@@ -1102,9 +1102,9 @@ Error CoverageMapping::loadFunctionRecord(
11021102

11031103
// Don't create records for (filenames, function) pairs we've already seen.
11041104
auto FilenamesHash = hash_combine_range(Record.Filenames);
1105-
if (!RecordProvenance[FilenamesHash].insert(hash_value(OrigFuncName)).second)
1105+
if (!RecordProvenance[FilenamesHash].insert(FuncArchHash).second){
11061106
return Error::success();
1107-
1107+
}
11081108
Functions.push_back(std::move(Function));
11091109

11101110
// Performance optimization: keep track of the indices of the function records
@@ -1152,7 +1152,7 @@ Error CoverageMapping::loadFromReaders(
11521152
ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
11531153
std::optional<std::reference_wrapper<IndexedInstrProfReader>>
11541154
&ProfileReader,
1155-
CoverageMapping &Coverage, StringRef Arch) {
1155+
CoverageMapping &Coverage, StringRef Arch, StringRef ObjectFilename) {
11561156

11571157
Coverage.setArchitecture(Arch);
11581158
assert(!Coverage.SingleByteCoverage || !ProfileReader ||
@@ -1165,7 +1165,7 @@ Error CoverageMapping::loadFromReaders(
11651165
if (Error E = RecordOrErr.takeError())
11661166
return E;
11671167
const auto &Record = *RecordOrErr;
1168-
if (Error E = Coverage.loadFunctionRecord(Record, ProfileReader))
1168+
if (Error E = Coverage.loadFunctionRecord(Record, ProfileReader, ObjectFilename))
11691169
return E;
11701170
}
11711171
}
@@ -1196,7 +1196,7 @@ Error CoverageMapping::loadFromFile(
11961196
std::optional<std::reference_wrapper<IndexedInstrProfReader>>
11971197
&ProfileReader,
11981198
CoverageMapping &Coverage, bool &DataFound,
1199-
SmallVectorImpl<object::BuildID> *FoundBinaryIDs) {
1199+
SmallVectorImpl<object::BuildID> *FoundBinaryIDs, StringRef ObjectFilename) {
12001200
auto CovMappingBufOrErr = MemoryBuffer::getFileOrSTDIN(
12011201
Filename, /*IsText=*/false, /*RequiresNullTerminator=*/false);
12021202
if (std::error_code EC = CovMappingBufOrErr.getError())
@@ -1228,7 +1228,7 @@ Error CoverageMapping::loadFromFile(
12281228
}));
12291229
}
12301230
DataFound |= !Readers.empty();
1231-
if (Error E = loadFromReaders(Readers, ProfileReader, Coverage, Arch))
1231+
if (Error E = loadFromReaders(Readers, ProfileReader, Coverage, Arch, ObjectFilename))
12321232
return createFileError(Filename, std::move(E));
12331233
return Error::success();
12341234
}
@@ -1262,14 +1262,20 @@ Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
12621262
return Arches[Idx];
12631263
};
12641264

1265+
//I beleive there is an error in this area of code, it's iterating through all arch's of the object files
1266+
// but its only filling *Coverage with last architecture it gets to
1267+
12651268
SmallVector<object::BuildID> FoundBinaryIDs;
12661269
for (const auto &File : llvm::enumerate(ObjectFilenames)) {
12671270
if (Error E = loadFromFile(File.value(), GetArch(File.index()),
12681271
CompilationDir, ProfileReaderRef, *Coverage,
1269-
DataFound, &FoundBinaryIDs))
1272+
DataFound, &FoundBinaryIDs, ObjectFilenames[File.index()]))
12701273
return std::move(E);
12711274
}
12721275

1276+
//I beleive there is an error in this area of code, it's iterating through all arch's of the object files
1277+
// but its only filling *Coverage with last architecture it gets to
1278+
12731279
if (BIDFetcher) {
12741280
std::vector<object::BuildID> ProfileBinaryIDs;
12751281
if (ProfileReader)
@@ -1508,19 +1514,24 @@ class SegmentBuilder {
15081514
}
15091515

15101516
/// Combine counts of regions which cover the same area.
1517+
//[(3:12, 10:1), (4:1, 6:7), (6:7, 8:1)]
15111518
static ArrayRef<CountedRegion>
15121519
combineRegions(MutableArrayRef<CountedRegion> Regions) {
15131520
if (Regions.empty())
15141521
return Regions;
1522+
1523+
15151524
auto Active = Regions.begin();
15161525
auto End = Regions.end();
15171526
for (auto I = Regions.begin() + 1; I != End; ++I) {
1518-
if (Active->startLoc() != I->startLoc() ||
1519-
Active->endLoc() != I->endLoc()) {
1527+
if (Active->startLoc() != I->startLoc() || Active->endLoc() != I->endLoc()) {
15201528
// Shift to the next region.
15211529
++Active;
1522-
if (Active != I)
1530+
if (Active != I){
15231531
*Active = *I;
1532+
// Active->ExecutionCount = 1;
1533+
// Active->Kind = CounterMappingRegion::CodeRegion;
1534+
}
15241535
continue;
15251536
}
15261537
// Merge duplicate region.
@@ -1549,6 +1560,29 @@ class SegmentBuilder {
15491560
SegmentBuilder Builder(Segments);
15501561

15511562
sortNestedRegions(Regions);
1563+
1564+
for(auto *I = Regions.begin(); I != Regions.end(); ++I){
1565+
bool FoundMatchInOtherBinary = false;
1566+
for(auto *J = I + 1; J != Regions.end(); ++J){
1567+
if(I->ObjectFilename != J->ObjectFilename &&
1568+
J->Kind == CounterMappingRegion::SkippedRegion
1569+
&& I->Kind != CounterMappingRegion::SkippedRegion &&
1570+
J->startLoc() >= I->startLoc() && J->endLoc() <= I->endLoc()){
1571+
for(auto *K = J + 1; K != Regions.end(); ++K){
1572+
if(K->ObjectFilename == I->ObjectFilename &&
1573+
J->startLoc() == K->startLoc() && J->endLoc() == K->endLoc()){
1574+
FoundMatchInOtherBinary = true;
1575+
}
1576+
}
1577+
if(!FoundMatchInOtherBinary){
1578+
J->Kind = I->Kind;
1579+
J->ExecutionCount = I->ExecutionCount;
1580+
}
1581+
}
1582+
}
1583+
}
1584+
1585+
15521586
ArrayRef<CountedRegion> CombinedRegions = combineRegions(Regions);
15531587

15541588
LLVM_DEBUG({

llvm/lib/ProfileData/Coverage/CoverageMappingReader.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ Error RawCoverageMappingReader::readMappingRegionsSubArray(
288288
// Don't do anything when we have a code region with a zero counter.
289289
break;
290290
case CounterMappingRegion::SkippedRegion:
291-
Kind = CounterMappingRegion::SkippedRegion;
291+
Kind = CounterMappingRegion::SkippedRegion; //if you change this to "CodeRegion, then all skipped regions have counter of 0"
292292
break;
293293
case CounterMappingRegion::BranchRegion:
294294
// For a Branch Region, read two successive counters.
@@ -381,7 +381,6 @@ Error RawCoverageMappingReader::readMappingRegionsSubArray(
381381
CounterMappingContext(Expressions).dump(C, dbgs());
382382
dbgs() << "\n";
383383
});
384-
385384
auto CMR = CounterMappingRegion(
386385
C, C2, InferredFileID, ExpandedFileID, LineStart, ColumnStart,
387386
LineStart + NumLines, ColumnEnd, Kind, Params);

llvm/lib/ProfileData/InstrProfWriter.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
#include "llvm/ADT/STLExtras.h"
1616
#include "llvm/ADT/SetVector.h"
1717
#include "llvm/ADT/StringRef.h"
18+
#include "llvm/ADT/bit.h"
19+
#include "llvm/DebugInfo/DIContext.h"
1820
#include "llvm/IR/ProfileSummary.h"
21+
#include "llvm/Object/Binary.h"
22+
#include "llvm/ProfileData/Coverage/CoverageMapping.h"
1923
#include "llvm/ProfileData/DataAccessProf.h"
2024
#include "llvm/ProfileData/IndexedMemProfData.h"
2125
#include "llvm/ProfileData/InstrProf.h"
@@ -216,16 +220,47 @@ void InstrProfWriter::overlapRecord(NamedInstrProfRecord &&Other,
216220
// Dest.sortValueData();
217221
// }
218222

223+
224+
225+
// #include "llvm/DebugInfo/DWARF/DWARFContext.h"
226+
227+
228+
#include <llvm/Support/raw_ostream.h>
229+
#include <llvm/Support/MemoryBuffer.h>
230+
#include <llvm/Support/SHA1.h>
231+
#include <array>
232+
233+
StringRef hashSourceFile(llvm::StringRef FilePath) {
234+
auto FileOrErr = llvm::MemoryBuffer::getFile(FilePath);
235+
if (!FileOrErr) {
236+
llvm::errs() << "Error reading file: " << FilePath << "\n";
237+
return "";
238+
}
239+
240+
const auto &Buffer = *FileOrErr.get();
241+
llvm::SHA1 Hasher;
242+
Hasher.update(Buffer.getBuffer());
243+
244+
std::array<uint8_t, 20> Hash = Hasher.final();
245+
246+
std::string HexStr = llvm::toHex(llvm::ArrayRef<uint8_t>(Hash));
247+
llvm::StringRef HashRef(HexStr);
248+
return HashRef;
249+
}
250+
251+
252+
219253
void InstrProfWriter::addRecord(StringRef Name, uint64_t Hash,
220254
InstrProfRecord &&I, uint64_t Weight,
221255
function_ref<void(Error)> Warn, StringRef Architecture) {
222256
auto &ProfileDataMap = FunctionData[Name];
257+
StringRef SHAHash = hashSourceFile(Architecture);
258+
llvm::errs() << "SHA Hashing: " << SHAHash << "\n";
223259
if(!Architecture.empty()){
224260
std::string HashStr = std::to_string(Hash) + ":" + Architecture.str();
225261
llvm::StringRef HashRef(HashStr);
226262
Hash = IndexedInstrProf::ComputeHash(HashRef);
227263
}
228-
229264
auto [Where, NewFunc] = ProfileDataMap.try_emplace(Hash);
230265
InstrProfRecord &Dest = Where->second;
231266

0 commit comments

Comments
 (0)