Skip to content

Commit 3a398c1

Browse files
author
Andres Wearden
committed
added new way of hashing the functions so they are more consistent with hashes from .profraw file
1 parent 7e74608 commit 3a398c1

File tree

5 files changed

+52
-8
lines changed

5 files changed

+52
-8
lines changed

llvm/include/llvm/ProfileData/InstrProf.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -644,13 +644,19 @@ class InstrProfSymtab {
644644

645645
// Insert into NameTab so that MD5NameMap (a vector that will be sorted)
646646
// won't have duplicated entries in the first place.
647-
uint64_t HashValue = IndexedInstrProf::ComputeHash(SymbolName);
647+
648+
uint64_t HashValue = IndexedInstrProf::ComputeHash(FuncName);
649+
std::string HashStr = std::to_string(HashValue);
650+
std::string CombinedStr = HashStr + ":" + ArchName.str();
651+
llvm::StringRef HashRef(CombinedStr);
652+
HashValue = IndexedInstrProf::ComputeHash(HashRef);
653+
648654
printf("Hash Value for %.*s: %" PRIu64 "\n", static_cast<int>(SymbolName.size()), SymbolName.data(), HashValue);
649655
auto Ins = NameTab.insert(FuncName);
650656
printf("mapped value for %" PRIu64 " hash: %.*s\n", HashValue, static_cast<int>(Ins.first->getKey().size()), Ins.first->getKey().data());
651657
if (Ins.second) {
652658
MD5NameMap.push_back(std::make_pair(
653-
IndexedInstrProf::ComputeHash(SymbolName), Ins.first->getKey()));
659+
IndexedInstrProf::ComputeHash(HashRef), Ins.first->getKey()));
654660
Sorted = false;
655661
}
656662
return Error::success();
@@ -779,10 +785,14 @@ StringRef InstrProfSymtab::getFuncOrVarNameIfDefined(uint64_t MD5Hash) {
779785

780786
StringRef InstrProfSymtab::getFuncOrVarName(uint64_t MD5Hash) {
781787
finalizeSymtab();
782-
auto Result = llvm::lower_bound(MD5NameMap, MD5Hash,
788+
std::string TempMD5HashStr = std::to_string(MD5Hash);
789+
std::string CombinedHashStr = TempMD5HashStr + ":" + Architecture;
790+
llvm::StringRef CombinedHashRef(CombinedHashStr);
791+
uint64_t NewMD5Hash = IndexedInstrProf::ComputeHash(CombinedHashRef);
792+
auto Result = llvm::lower_bound(MD5NameMap, NewMD5Hash,
783793
[](const std::pair<uint64_t, StringRef> &LHS,
784794
uint64_t RHS) { return LHS.first < RHS; });
785-
if (Result != MD5NameMap.end() && Result->first == MD5Hash)
795+
if (Result != MD5NameMap.end() && Result->first == NewMD5Hash)
786796
return Result->second;
787797
return StringRef();
788798
}

llvm/include/llvm/ProfileData/InstrProfWriter.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ class InstrProfWriter {
114114
/// summed. Optionally scale counts by \p Weight.
115115
LLVM_ABI void addRecord(NamedInstrProfRecord &&I, uint64_t Weight,
116116
function_ref<void(Error)> Warn);
117+
LLVM_ABI void addRecord(StringRef Name, uint64_t Hash,
118+
InstrProfRecord &&I, uint64_t Weight,
119+
function_ref<void(Error)> Warn, const std::string &Architecture);
120+
void addRecord(NamedInstrProfRecord &&I, uint64_t Weight, const std::string &Architecture,
121+
function_ref<void(Error)> Warn);
117122
void addRecord(NamedInstrProfRecord &&I, function_ref<void(Error)> Warn) {
118123
addRecord(std::move(I), 1, Warn);
119124
}

llvm/lib/ProfileData/InstrProf.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ std::string getPGOFuncName(StringRef Name, GlobalValue::LinkageTypes Linkage,
320320
else
321321
NewName = NewName.insert(0, FileName.str() + ":");
322322
}
323+
324+
323325
return NewName;
324326
}
325327

@@ -606,7 +608,6 @@ Error readAndDecodeStrings(StringRef NameStrings,
606608
SmallVector<StringRef, 0> Names;
607609
StringRef ArchRef(Architecture);
608610
NameStrings.split(Names, getInstrProfNameSeparator());
609-
printf("=====================READER DATA====================");
610611
for (StringRef &Name : Names){
611612
std::string ConcHashString = Name.str() + ":" + ArchRef.str();
612613
printf("The string %s will get hashed and mapped to %s\n", ConcHashString.c_str(), Name.str().c_str());

llvm/lib/ProfileData/InstrProfWriter.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ void InstrProfWriter::setValueProfDataEndianness(llvm::endianness Endianness) {
153153

154154
void InstrProfWriter::setOutputSparse(bool Sparse) { this->Sparse = Sparse; }
155155

156-
void InstrProfWriter::addRecord(NamedInstrProfRecord &&I, uint64_t Weight,
156+
void InstrProfWriter::addRecord(NamedInstrProfRecord &&I, uint64_t Weight, const std::string &Architecture,
157157
function_ref<void(Error)> Warn) {
158158
auto Name = I.Name;
159159
auto Hash = I.Hash;
160-
addRecord(Name, Hash, std::move(I), Weight, Warn);
160+
addRecord(Name, Hash, std::move(I), Weight, Warn, Architecture);
161161
}
162162

163163
void InstrProfWriter::overlapRecord(NamedInstrProfRecord &&Other,
@@ -216,6 +216,34 @@ void InstrProfWriter::addRecord(StringRef Name, uint64_t Hash,
216216
Dest.sortValueData();
217217
}
218218

219+
void InstrProfWriter::addRecord(StringRef Name, uint64_t Hash,
220+
InstrProfRecord &&I, uint64_t Weight,
221+
function_ref<void(Error)> Warn, const std::string &Architecture) {
222+
auto &ProfileDataMap = FunctionData[Name];
223+
std::string HashStr = std::to_string(Hash) + ":" + Architecture;
224+
llvm::StringRef HashRef(HashStr);
225+
uint64_t NewHash = IndexedInstrProf::ComputeHash(HashRef);
226+
227+
auto [Where, NewFunc] = ProfileDataMap.try_emplace(NewHash);
228+
InstrProfRecord &Dest = Where->second;
229+
230+
auto MapWarn = [&](instrprof_error E) {
231+
Warn(make_error<InstrProfError>(E));
232+
};
233+
234+
if (NewFunc) {
235+
// We've never seen a function with this name and hash, add it.
236+
Dest = std::move(I);
237+
if (Weight > 1)
238+
Dest.scale(Weight, 1, MapWarn);
239+
} else {
240+
// We're updating a function we've seen before.
241+
Dest.merge(I, Weight, MapWarn);
242+
}
243+
244+
Dest.sortValueData();
245+
}
246+
219247
void InstrProfWriter::addMemProfRecord(
220248
const Function::GUID Id, const memprof::IndexedMemProfRecord &Record) {
221249
auto NewRecord = Record;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ loadInput(const WeightedFile &Input, SymbolRemapper *Remapper,
862862
I.Name = (*Remapper)(I.Name);
863863
const StringRef FuncName = I.Name;
864864
bool Reported = false;
865-
WC->Writer.addRecord(std::move(I), Input.Weight, [&](Error E) {
865+
WC->Writer.addRecord(std::move(I), Input.Weight, Architecture, [&](Error E) {
866866
if (Reported) {
867867
consumeError(std::move(E));
868868
return;

0 commit comments

Comments
 (0)