llvm.org GIT mirror llvm / ab49f7c
[PGO] Add hash to name mapping in InstrProfSymtab Creator and lookup interfaces are added to this symtab class. The new interfaces will be used by InstrProf Readers and writer. A unit test is also added for the new APIs. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@256092 91177308-0d34-0410-b5e6-96231b3b80d8 Xinliang David Li 3 years ago
2 changed file(s) with 66 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
201201 namespace object {
202202 class SectionRef;
203203 }
204
205 namespace IndexedInstrProf {
206 uint64_t ComputeHash(StringRef K);
207 }
208
204209 /// A symbol table used for function PGO name look-up with keys
205210 /// (such as pointers, md5hash values) to the function. A function's
206211 /// PGO name or name's md5hash are used in retrieving the profile
210215 private:
211216 StringRef Data;
212217 uint64_t Address;
218 std::vector> HashNameMap;
213219
214220 public:
215221 InstrProfSymtab() : Data(), Address(0) {}
222228 Address = BaseAddr;
223229 return std::error_code();
224230 }
231 template void create(NameIterRange &IterRange) {
232 for (auto Name : IterRange)
233 HashNameMap.push_back(
234 std::make_pair(IndexedInstrProf::ComputeHash(Name), Name.str()));
235 finalizeSymtab();
236 }
237
238 // If the symtab is created by a series calls to \c addFuncName, \c
239 // finalizeSymtab needs to
240 // be called before function name/symbol lookup using MD5 hash. This is
241 // required because
242 // the underlying map is vector (for space efficiency) which needs to be
243 // sorted.
244 void finalizeSymtab() {
245 std::sort(HashNameMap.begin(), HashNameMap.end(), less_first());
246 HashNameMap.erase(std::unique(HashNameMap.begin(), HashNameMap.end()),
247 HashNameMap.end());
248 }
249
250 void addFuncName(StringRef FuncName) {
251 HashNameMap.push_back(std::make_pair(
252 IndexedInstrProf::ComputeHash(FuncName), FuncName.str()));
253 }
225254
226255 /// Return function's PGO name from the function name's symabol
227256 /// address in the object file. If an error occurs, Return
228257 /// an empty string.
229258 StringRef getFuncName(uint64_t FuncNameAddress, size_t NameSize);
259 /// Return function's PGO name from the name's md5 hash value.
260 /// If not found, return an empty string.
261 StringRef getFuncName(uint64_t FuncMD5Hash) {
262 auto Result =
263 std::lower_bound(HashNameMap.begin(), HashNameMap.end(), FuncMD5Hash,
264 [](const std::pair &LHS,
265 uint64_t RHS) { return LHS.first < RHS; });
266 if (Result != HashNameMap.end())
267 return Result->second;
268 return StringRef();
269 }
230270 };
231271
232272 struct InstrProfStringTable {
480520 return endian::read(Result);
481521 }
482522
483 static inline uint64_t ComputeHash(HashT Type, StringRef K) {
523 inline uint64_t ComputeHash(HashT Type, StringRef K) {
484524 switch (Type) {
485525 case HashT::MD5:
486526 return IndexedInstrProf::MD5Hash(K);
492532 const uint64_t Version = INSTR_PROF_INDEX_VERSION;
493533 const HashT HashType = HashT::MD5;
494534
495 static inline uint64_t ComputeHash(StringRef K) {
496 return ComputeHash(HashType, K);
497 }
535 inline uint64_t ComputeHash(StringRef K) { return ComputeHash(HashType, K); }
498536
499537 // This structure defines the file header of the LLVM profile
500538 // data file in indexed-format.
523523 ASSERT_EQ(20U, Counts[1]);
524524 }
525525
526 TEST_F(InstrProfTest, instr_prof_symtab_test) {
527 std::vector FuncNames;
528 FuncNames.push_back("func1");
529 FuncNames.push_back("func2");
530 FuncNames.push_back("func3");
531 FuncNames.push_back("bar1");
532 FuncNames.push_back("bar2");
533 FuncNames.push_back("bar3");
534 InstrProfSymtab Symtab;
535 Symtab.create(FuncNames);
536 StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func1"));
537 ASSERT_EQ(StringRef("func1"), R);
538 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func2"));
539 ASSERT_EQ(StringRef("func2"), R);
540 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func3"));
541 ASSERT_EQ(StringRef("func3"), R);
542 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar1"));
543 ASSERT_EQ(StringRef("bar1"), R);
544 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar2"));
545 ASSERT_EQ(StringRef("bar2"), R);
546 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar3"));
547 ASSERT_EQ(StringRef("bar3"), R);
548 }
549
526550 } // end anonymous namespace