llvm.org GIT mirror llvm / 06b0783
[InstrProf] Encapsulates access to AddrToMD5Map. Summary: This fixes a unittest failure introduced by D44717 D44717 introduced lazy sorting of the internal data structures of the symbol table. The AddrToMD5Map getter was potentially exposing inconsistent (unsorted) state. We could sort in the accessor, however, a client may store the pointer and thus bypass the internal state management of the symbol table. The alternative in this CL blocks direct access to the state, thus ensuring consistent externally-observable state. Reviewers: davidxl, xur, eraman Reviewed By: xur Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D44757 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@328163 91177308-0d34-0410-b5e6-96231b3b80d8 Mircea Trofin 1 year, 6 months ago
6 changed file(s) with 34 addition(s) and 35 deletion(s). Raw diff Collapse all Expand all
486486 AddrToMD5Map.push_back(std::make_pair(Addr, MD5Val));
487487 }
488488
489 AddrHashMap &getAddrHashMap() { return AddrToMD5Map; }
489 /// Return a function's hash, or 0, if the function isn't in this SymTab.
490 uint64_t getFunctionHashFromAddress(uint64_t Address);
490491
491492 /// Return function's PGO name from the function name's symbol
492493 /// address in the object file. If an error occurs, return
642643 return *this;
643644 }
644645
645 using ValueMapType = std::vector>;
646
647646 /// Return the number of value profile kinds with non-zero number
648647 /// of profile sites.
649648 inline uint32_t getNumValueKinds() const;
677676 /// Add ValueData for ValueKind at value Site.
678677 void addValueData(uint32_t ValueKind, uint32_t Site,
679678 InstrProfValueData *VData, uint32_t N,
680 ValueMapType *ValueMap);
679 InstrProfSymtab *SymTab);
681680
682681 /// Merge the counts in \p Other into this one.
683682 /// Optionally scale merged counts by \p Weight.
751750
752751 // Map indirect call target name hash to name string.
753752 uint64_t remapValue(uint64_t Value, uint32_t ValueKind,
754 ValueMapType *HashKeys);
753 InstrProfSymtab *SymTab);
755754
756755 // Merge Value Profile data from Src record to this record for ValueKind.
757756 // Scale merged value counts by \p Weight.
314314 * \brief Read data from this record and save it to Record.
315315 */
316316 void deserializeTo(InstrProfRecord &Record,
317 InstrProfRecord::ValueMapType *VMap);
317 InstrProfSymtab *SymTab);
318318 /*
319319 * In-place byte swap:
320320 * Do byte swap for this instance. \c Old is the original order before
392392 * Read data from this data and save it to \c Record.
393393 */
394394 void deserializeTo(InstrProfRecord &Record,
395 InstrProfRecord::ValueMapType *VMap);
395 InstrProfSymtab *SymTab);
396396 void operator delete(void *ptr) { ::operator delete(ptr); }
397397 #endif
398398 } ValueProfData;
198198 uint32_t ValueKindLast;
199199 uint32_t CurValueDataSize;
200200
201 InstrProfRecord::ValueMapType FunctionPtrToNameMap;
202
203201 public:
204202 RawInstrProfReader(std::unique_ptr DataBuffer)
205203 : DataBuffer(std::move(DataBuffer)) {}
359359 return Error::success();
360360 }
361361
362 uint64_t InstrProfSymtab::getFunctionHashFromAddress(uint64_t Address) {
363 finalizeSymtab();
364 auto Result =
365 std::lower_bound(AddrToMD5Map.begin(), AddrToMD5Map.end(), Address,
366 [](const std::pair &LHS,
367 uint64_t RHS) { return LHS.first < RHS; });
368 // Raw function pointer collected by value profiler may be from
369 // external functions that are not instrumented. They won't have
370 // mapping data to be used by the deserializer. Force the value to
371 // be 0 in this case.
372 if (Result != AddrToMD5Map.end() && Result->first == Address)
373 return (uint64_t)Result->second;
374 return 0;
375 }
376
362377 Error collectPGOFuncNameStrings(ArrayRef NameStrs,
363378 bool doCompression, std::string &Result) {
364379 assert(!NameStrs.empty() && "No name data to emit");
559574
560575 // Map indirect call target name hash to name string.
561576 uint64_t InstrProfRecord::remapValue(uint64_t Value, uint32_t ValueKind,
562 ValueMapType *ValueMap) {
563 if (!ValueMap)
577 InstrProfSymtab *SymTab) {
578 if (!SymTab)
564579 return Value;
565 switch (ValueKind) {
566 case IPVK_IndirectCallTarget: {
567 auto Result =
568 std::lower_bound(ValueMap->begin(), ValueMap->end(), Value,
569 [](const std::pair &LHS,
570 uint64_t RHS) { return LHS.first < RHS; });
571 // Raw function pointer collected by value profiler may be from
572 // external functions that are not instrumented. They won't have
573 // mapping data to be used by the deserializer. Force the value to
574 // be 0 in this case.
575 if (Result != ValueMap->end() && Result->first == Value)
576 Value = (uint64_t)Result->second;
577 else
578 Value = 0;
579 break;
580 }
581 }
580
581 if (ValueKind == IPVK_IndirectCallTarget)
582 return SymTab->getFunctionHashFromAddress(Value);
583
582584 return Value;
583585 }
584586
585587 void InstrProfRecord::addValueData(uint32_t ValueKind, uint32_t Site,
586588 InstrProfValueData *VData, uint32_t N,
587 ValueMapType *ValueMap) {
589 InstrProfSymtab *ValueMap) {
588590 for (uint32_t I = 0; I < N; I++) {
589591 VData[I].Value = remapValue(VData[I].Value, ValueKind, ValueMap);
590592 }
664666 }
665667
666668 void ValueProfRecord::deserializeTo(InstrProfRecord &Record,
667 InstrProfRecord::ValueMapType *VMap) {
669 InstrProfSymtab *SymTab) {
668670 Record.reserveSites(Kind, NumValueSites);
669671
670672 InstrProfValueData *ValueData = getValueProfRecordValueData(this);
671673 for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) {
672674 uint8_t ValueDataCount = this->SiteCountArray[VSite];
673 Record.addValueData(Kind, VSite, ValueData, ValueDataCount, VMap);
675 Record.addValueData(Kind, VSite, ValueData, ValueDataCount, SymTab);
674676 ValueData += ValueDataCount;
675677 }
676678 }
704706 }
705707
706708 void ValueProfData::deserializeTo(InstrProfRecord &Record,
707 InstrProfRecord::ValueMapType *VMap) {
709 InstrProfSymtab *SymTab) {
708710 if (NumValueKinds == 0)
709711 return;
710712
711713 ValueProfRecord *VR = getFirstValueProfRecord(this);
712714 for (uint32_t K = 0; K < NumValueKinds; K++) {
713 VR->deserializeTo(Record, VMap);
715 VR->deserializeTo(Record, SymTab);
714716 VR = getValueProfRecordNext(VR);
715717 }
716718 }
437437 // Note that besides deserialization, this also performs the conversion for
438438 // indirect call targets. The function pointers from the raw profile are
439439 // remapped into function name hashes.
440 VDataPtrOrErr.get()->deserializeTo(Record, &Symtab->getAddrHashMap());
440 VDataPtrOrErr.get()->deserializeTo(Record, Symtab.get());
441441 CurValueDataSize = VDataPtrOrErr.get()->getSize();
442442 return success();
443443 }
769769 Symtab.mapAddress(uint64_t(callee4), 0x4000ULL);
770770 // Missing mapping for callee5
771771
772 VPData->deserializeTo(Record, &Symtab.getAddrHashMap());
772 VPData->deserializeTo(Record, &Symtab);
773773
774774 // Now read data from Record and sanity check the data
775775 ASSERT_EQ(5U, Record.getNumValueSites(IPVK_IndirectCallTarget));