llvm.org GIT mirror llvm / 364eb09
Revert "[InstrProf] Support for external functions in text format." This reverts commit r328132. Breaks FDO selfhost. I'm seeing error: /tmp/profraw: Invalid instrumentation profile data (bad magic) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@328207 91177308-0d34-0410-b5e6-96231b3b80d8 Benjamin Kramer 1 year, 6 months ago
7 changed file(s) with 31 addition(s) and 105 deletion(s). Raw diff Collapse all Expand all
424424 // A map from function runtime address to function name MD5 hash.
425425 // This map is only populated and used by raw instr profile reader.
426426 AddrHashMap AddrToMD5Map;
427 bool Sorted = false;
428
429 static StringRef getExternalSymbol() {
430 return "** External Symbol **";
431 }
432
433 // If the symtab is created by a series of calls to \c addFuncName, \c
434 // finalizeSymtab needs to be called before looking up function names.
435 // This is required because the underlying map is a vector (for space
436 // efficiency) which needs to be sorted.
437 inline void finalizeSymtab();
438427
439428 public:
440429 InstrProfSymtab() = default;
466455 /// \p IterRange. This interface is used by IndexedProfReader.
467456 template Error create(const NameIterRange &IterRange);
468457
458 // If the symtab is created by a series of calls to \c addFuncName, \c
459 // finalizeSymtab needs to be called before looking up function names.
460 // This is required because the underlying map is a vector (for space
461 // efficiency) which needs to be sorted.
462 inline void finalizeSymtab();
463
469464 /// Update the symtab by adding \p FuncName to the table. This interface
470465 /// is used by the raw and text profile readers.
471466 Error addFuncName(StringRef FuncName) {
472467 if (FuncName.empty())
473468 return make_error(instrprof_error::malformed);
474469 auto Ins = NameTab.insert(FuncName);
475 if (Ins.second) {
470 if (Ins.second)
476471 MD5NameMap.push_back(std::make_pair(
477472 IndexedInstrProf::ComputeHash(FuncName), Ins.first->getKey()));
478 Sorted = false;
479 }
480473 return Error::success();
481474 }
482475
497490 /// Return function's PGO name from the name's md5 hash value.
498491 /// If not found, return an empty string.
499492 inline StringRef getFuncName(uint64_t FuncMD5Hash);
500
501 /// Just like getFuncName, except that it will return a non-empty StringRef
502 /// if the function is external to this symbol table. All such cases
503 /// will be represented using the same StringRef value.
504 inline StringRef getFuncNameOrExternalSymbol(uint64_t FuncMD5Hash);
505
506 /// True if Symbol is the value used to represent external symbols.
507 static bool isExternalSymbol(const StringRef &Symbol) {
508 return Symbol == InstrProfSymtab::getExternalSymbol();
509 }
510493
511494 /// Return function from the name's md5 hash. Return nullptr if not found.
512495 inline Function *getFunction(uint64_t FuncMD5Hash);
541524 }
542525
543526 void InstrProfSymtab::finalizeSymtab() {
544 if (Sorted)
545 return;
546527 std::sort(MD5NameMap.begin(), MD5NameMap.end(), less_first());
547528 std::sort(MD5FuncMap.begin(), MD5FuncMap.end(), less_first());
548529 std::sort(AddrToMD5Map.begin(), AddrToMD5Map.end(), less_first());
549530 AddrToMD5Map.erase(std::unique(AddrToMD5Map.begin(), AddrToMD5Map.end()),
550531 AddrToMD5Map.end());
551 Sorted = true;
552 }
553
554 StringRef InstrProfSymtab::getFuncNameOrExternalSymbol(uint64_t FuncMD5Hash) {
555 StringRef ret = getFuncName(FuncMD5Hash);
556 if (ret.empty())
557 return InstrProfSymtab::getExternalSymbol();
558 return ret;
559532 }
560533
561534 StringRef InstrProfSymtab::getFuncName(uint64_t FuncMD5Hash) {
562 finalizeSymtab();
563535 auto Result =
564536 std::lower_bound(MD5NameMap.begin(), MD5NameMap.end(), FuncMD5Hash,
565537 [](const std::pair &LHS,
570542 }
571543
572544 Function* InstrProfSymtab::getFunction(uint64_t FuncMD5Hash) {
573 finalizeSymtab();
574545 auto Result =
575546 std::lower_bound(MD5FuncMap.begin(), MD5FuncMap.end(), FuncMD5Hash,
576547 [](const std::pair &LHS,
100100 return make_error(Err);
101101 }
102102
103 Error error(Error &&E) { return error(InstrProfError::take(std::move(E))); }
103 Error error(Error E) { return error(InstrProfError::take(std::move(E))); }
104104
105105 /// Clear the current error and return a successful one.
106106 Error success() { return error(instrprof_error::success); }
354354 }
355355 }
356356 }
357 Sorted = false;
357
358358 finalizeSymtab();
359359 return Error::success();
360360 }
475475 while (P < EndP && *P == 0)
476476 P++;
477477 }
478 Symtab.finalizeSymtab();
478479 return Error::success();
479480 }
480481
199199 std::pair VD = Line->rsplit(':');
200200 uint64_t TakenCount, Value;
201201 if (ValueKind == IPVK_IndirectCallTarget) {
202 if (InstrProfSymtab::isExternalSymbol(VD.first)) {
203 Value = 0;
204 } else {
205 if (Error E = Symtab->addFuncName(VD.first))
206 return E;
207 Value = IndexedInstrProf::ComputeHash(VD.first);
208 }
202 if (Error E = Symtab->addFuncName(VD.first))
203 return E;
204 Value = IndexedInstrProf::ComputeHash(VD.first);
209205 } else {
210206 READ_NUM(VD.first, Value);
211207 }
230226 ++Line;
231227 // If we hit EOF while looking for a name, we're done.
232228 if (Line.is_at_end()) {
229 Symtab->finalizeSymtab();
233230 return error(instrprof_error::eof);
234231 }
235232
236233 // Read the function name.
237234 Record.Name = *Line++;
238235 if (Error E = Symtab->addFuncName(Record.Name))
239 return error(std::move(E));
236 return E;
240237
241238 // Read the function hash.
242239 if (Line.is_at_end())
267264
268265 // Check if value profile data exists and read it if so.
269266 if (Error E = readValueProfileData(Record))
270 return error(std::move(E));
271
267 return E;
268
269 // This is needed to avoid two pass parsing because llvm-profdata
270 // does dumping while reading.
271 Symtab->finalizeSymtab();
272272 return success();
273273 }
274274
330330 continue;
331331 Symtab.mapAddress(FPtr, I->NameRef);
332332 }
333 Symtab.finalizeSymtab();
333334 return success();
334335 }
335336
447448 if (atEnd())
448449 // At this point, ValueDataStart field points to the next header.
449450 if (Error E = readNextHeader(getNextHeaderPos()))
450 return error(std::move(E));
451 return E;
451452
452453 // Read name ad set it in Record.
453454 if (Error E = readName(Record))
454 return error(std::move(E));
455 return E;
455456
456457 // Read FuncHash and set it in Record.
457458 if (Error E = readFuncHash(Record))
458 return error(std::move(E));
459 return E;
459460
460461 // Read raw counts and set Record.
461462 if (Error E = readRawCounts(Record))
462 return error(std::move(E));
463 return E;
463464
464465 // Read value data and set Record.
465466 if (Error E = readValueProfilingData(Record))
466 return error(std::move(E));
467 return E;
467468
468469 // Iterate.
469470 advanceData();
360360 std::unique_ptr VD = Func.getValueForSite(VK, S);
361361 for (uint32_t I = 0; I < ND; I++) {
362362 if (VK == IPVK_IndirectCallTarget)
363 OS << Symtab.getFuncNameOrExternalSymbol(VD[I].Value) << ":"
364 << VD[I].Count << "\n";
363 OS << Symtab.getFuncName(VD[I].Value) << ":" << VD[I].Count << "\n";
365364 else
366365 OS << VD[I].Value << ":" << VD[I].Count << "\n";
367366 }
379378 if (shouldEncodeData(I.getValue()))
380379 if (Error E = Symtab.addFuncName(I.getKey()))
381380 return E;
381 Symtab.finalizeSymtab();
382382
383383 for (const auto &I : FunctionData)
384384 if (shouldEncodeData(I.getValue()))
+0
-50
test/tools/llvm-profdata/invalid-profdata.test less more
None RUN: echo ":ir" > %t.input
1 RUN: echo "_ZN6Thread5StartEv" >> %t.input
2 RUN: echo "# Func Hash:" >> %t.input
3 RUN: echo "288793635542036872" >> %t.input
4 RUN: echo "# Num Counters:" >> %t.input
5 RUN: echo "3" >> %t.input
6 RUN: echo "# Counter Values:" >> %t.input
7 RUN: echo "0" >> %t.input
8 RUN: echo "12" >> %t.input
9 RUN: echo "12" >> %t.input
10 RUN: echo "# Num Value Kinds:" >> %t.input
11 RUN: echo "1" >> %t.input
12 RUN: echo "# ValueKind = IPVK_IndirectCallTarget:" >> %t.input
13 RUN: echo "0" >> %t.input
14 RUN: echo "# NumValueSites:" >> %t.input
15 RUN: echo "2" >> %t.input
16 RUN: echo "2" >> %t.input
17 RUN: echo "f1:10" >> %t.input
18 RUN: echo "f2:0" >> %t.input
19 RUN: echo "1" >> %t.input
20 RUN: echo ":10" >> %t.input
21
22 RUN: not llvm-profdata merge %t.input -text -o /dev/null 2>&1 | FileCheck %s --check-prefix=BROKEN
23 BROKEN: Malformed instrumentation profile data
24
25 RUN: echo ":ir" > %t.input
26 RUN: echo "_ZN6Thread5StartEv" >> %t.input
27 RUN: echo "# Func Hash:" >> %t.input
28 RUN: echo "288793635542036872" >> %t.input
29 RUN: echo "# Num Counters:" >> %t.input
30 RUN: echo "3" >> %t.input
31 RUN: echo "# Counter Values:" >> %t.input
32 RUN: echo "0" >> %t.input
33 RUN: echo "12" >> %t.input
34 RUN: echo "12" >> %t.input
35 RUN: echo "# Num Value Kinds:" >> %t.input
36 RUN: echo "1" >> %t.input
37 RUN: echo "# ValueKind = IPVK_IndirectCallTarget:" >> %t.input
38 RUN: echo "0" >> %t.input
39 RUN: echo "# NumValueSites:" >> %t.input
40 RUN: echo "2" >> %t.input
41 RUN: echo "2" >> %t.input
42 RUN: echo "f1:10" >> %t.input
43 RUN: echo "f2:0" >> %t.input
44 RUN: echo "1" >> %t.input
45 RUN: echo "** External Symbol **:10" >> %t.input
46
47 # RUN: llvm-profdata merge %t.input -text -output=%t.out && cat %t.out | FileCheck %s
48
49 CHECK: ** External Symbol **:10
768768 Symtab.mapAddress(uint64_t(callee3), 0x3000ULL);
769769 Symtab.mapAddress(uint64_t(callee4), 0x4000ULL);
770770 // Missing mapping for callee5
771 Symtab.finalizeSymtab();
771772
772773 VPData->deserializeTo(Record, &Symtab);
773774
856857 EXPECT_THAT_ERROR(Symtab.addFuncName("blah_1"), Succeeded());
857858 EXPECT_THAT_ERROR(Symtab.addFuncName("blah_2"), Succeeded());
858859 EXPECT_THAT_ERROR(Symtab.addFuncName("blah_3"), Succeeded());
860 // Finalize it
861 Symtab.finalizeSymtab();
859862
860863 // Check again
861864 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_1"));