@@ -458,6 +458,59 @@ Error InstrProfSymtab::create(Module &M, bool InLTO) {
458
458
return Error::success ();
459
459
}
460
460
461
+ // / \c NameStrings is a string composed of one of more possibly encoded
462
+ // / sub-strings. The substrings are separated by 0 or more zero bytes. This
463
+ // / method decodes the string and calls `NameCallback` for each substring.
464
+ static Error
465
+ readAndDecodeStrings (StringRef NameStrings,
466
+ std::function<Error(StringRef)> NameCallback) {
467
+ const uint8_t *P = NameStrings.bytes_begin ();
468
+ const uint8_t *EndP = NameStrings.bytes_end ();
469
+ while (P < EndP) {
470
+ uint32_t N;
471
+ uint64_t UncompressedSize = decodeULEB128 (P, &N);
472
+ P += N;
473
+ uint64_t CompressedSize = decodeULEB128 (P, &N);
474
+ P += N;
475
+ bool isCompressed = (CompressedSize != 0 );
476
+ SmallVector<uint8_t , 128 > UncompressedNameStrings;
477
+ StringRef NameStrings;
478
+ if (isCompressed) {
479
+ if (!llvm::compression::zlib::isAvailable ())
480
+ return make_error<InstrProfError>(instrprof_error::zlib_unavailable);
481
+
482
+ if (Error E = compression::zlib::decompress (ArrayRef (P, CompressedSize),
483
+ UncompressedNameStrings,
484
+ UncompressedSize)) {
485
+ consumeError (std::move (E));
486
+ return make_error<InstrProfError>(instrprof_error::uncompress_failed);
487
+ }
488
+ P += CompressedSize;
489
+ NameStrings = toStringRef (UncompressedNameStrings);
490
+ } else {
491
+ NameStrings =
492
+ StringRef (reinterpret_cast <const char *>(P), UncompressedSize);
493
+ P += UncompressedSize;
494
+ }
495
+ // Now parse the name strings.
496
+ SmallVector<StringRef, 0 > Names;
497
+ NameStrings.split (Names, getInstrProfNameSeparator ());
498
+ for (StringRef &Name : Names)
499
+ if (Error E = NameCallback (Name))
500
+ return E;
501
+
502
+ while (P < EndP && *P == 0 )
503
+ P++;
504
+ }
505
+ return Error::success ();
506
+ }
507
+
508
+ Error InstrProfSymtab::create (StringRef NameStrings) {
509
+ return readAndDecodeStrings (
510
+ NameStrings,
511
+ std::bind (&InstrProfSymtab::addFuncName, this , std::placeholders::_1));
512
+ }
513
+
461
514
Error InstrProfSymtab::addFuncWithName (Function &F, StringRef PGOFuncName) {
462
515
if (Error E = addFuncName (PGOFuncName))
463
516
return E;
@@ -566,48 +619,6 @@ Error collectPGOFuncNameStrings(ArrayRef<GlobalVariable *> NameVars,
566
619
NameStrs, compression::zlib::isAvailable () && doCompression, Result);
567
620
}
568
621
569
- Error readPGOFuncNameStrings (StringRef NameStrings, InstrProfSymtab &Symtab) {
570
- const uint8_t *P = NameStrings.bytes_begin ();
571
- const uint8_t *EndP = NameStrings.bytes_end ();
572
- while (P < EndP) {
573
- uint32_t N;
574
- uint64_t UncompressedSize = decodeULEB128 (P, &N);
575
- P += N;
576
- uint64_t CompressedSize = decodeULEB128 (P, &N);
577
- P += N;
578
- bool isCompressed = (CompressedSize != 0 );
579
- SmallVector<uint8_t , 128 > UncompressedNameStrings;
580
- StringRef NameStrings;
581
- if (isCompressed) {
582
- if (!llvm::compression::zlib::isAvailable ())
583
- return make_error<InstrProfError>(instrprof_error::zlib_unavailable);
584
-
585
- if (Error E = compression::zlib::decompress (ArrayRef (P, CompressedSize),
586
- UncompressedNameStrings,
587
- UncompressedSize)) {
588
- consumeError (std::move (E));
589
- return make_error<InstrProfError>(instrprof_error::uncompress_failed);
590
- }
591
- P += CompressedSize;
592
- NameStrings = toStringRef (UncompressedNameStrings);
593
- } else {
594
- NameStrings =
595
- StringRef (reinterpret_cast <const char *>(P), UncompressedSize);
596
- P += UncompressedSize;
597
- }
598
- // Now parse the name strings.
599
- SmallVector<StringRef, 0 > Names;
600
- NameStrings.split (Names, getInstrProfNameSeparator ());
601
- for (StringRef &Name : Names)
602
- if (Error E = Symtab.addFuncName (Name))
603
- return E;
604
-
605
- while (P < EndP && *P == 0 )
606
- P++;
607
- }
608
- return Error::success ();
609
- }
610
-
611
622
void InstrProfRecord::accumulateCounts (CountSumOrPercent &Sum) const {
612
623
uint64_t FuncSum = 0 ;
613
624
Sum.NumEntries += Counts.size ();
0 commit comments