@@ -100,7 +100,7 @@ void BoltAddressTranslation::write(const BinaryContext &BC, raw_ostream &OS) {
100
100
LLVM_DEBUG (dbgs () << " Function name: " << Function.getPrintName () << " \n " );
101
101
LLVM_DEBUG (dbgs () << " Address reference: 0x"
102
102
<< Twine::utohexstr (Function.getOutputAddress ()) << " \n " );
103
- LLVM_DEBUG (dbgs () << formatv (" Hash: {0:x}\n " , getBFHash (OutputAddress )));
103
+ LLVM_DEBUG (dbgs () << formatv (" Hash: {0:x}\n " , getBFHash (InputAddress )));
104
104
LLVM_DEBUG (dbgs () << " Secondary Entry Points: " << NumSecondaryEntryPoints
105
105
<< ' \n ' );
106
106
@@ -197,8 +197,9 @@ void BoltAddressTranslation::writeMaps(std::map<uint64_t, MapTy> &Maps,
197
197
? SecondaryEntryPointsMap[Address].size ()
198
198
: 0 ;
199
199
if (Cold) {
200
- size_t HotIndex =
201
- std::distance (ColdPartSource.begin (), ColdPartSource.find (Address));
200
+ auto HotEntryIt = Maps.find (ColdPartSource[Address]);
201
+ assert (HotEntryIt != Maps.end ());
202
+ size_t HotIndex = std::distance (Maps.begin (), HotEntryIt);
202
203
encodeULEB128 (HotIndex - PrevIndex, OS);
203
204
PrevIndex = HotIndex;
204
205
} else {
@@ -207,7 +208,7 @@ void BoltAddressTranslation::writeMaps(std::map<uint64_t, MapTy> &Maps,
207
208
LLVM_DEBUG (dbgs () << " Hash: " << formatv (" {0:x}\n " , BFHash));
208
209
OS.write (reinterpret_cast <char *>(&BFHash), 8 );
209
210
// Number of basic blocks
210
- size_t NumBasicBlocks = getBBHashMap ( HotInputAddress). getNumBasicBlocks () ;
211
+ size_t NumBasicBlocks = NumBasicBlocksMap[ HotInputAddress] ;
211
212
LLVM_DEBUG (dbgs () << " Basic blocks: " << NumBasicBlocks << ' \n ' );
212
213
encodeULEB128 (NumBasicBlocks, OS);
213
214
// Secondary entry points
@@ -425,8 +426,9 @@ void BoltAddressTranslation::dump(raw_ostream &OS) {
425
426
for (const auto &MapEntry : Maps) {
426
427
const uint64_t Address = MapEntry.first ;
427
428
const uint64_t HotAddress = fetchParentAddress (Address);
429
+ const bool IsHotFunction = HotAddress == 0 ;
428
430
OS << " Function Address: 0x" << Twine::utohexstr (Address);
429
- if (HotAddress == 0 )
431
+ if (IsHotFunction )
430
432
OS << formatv (" , hash: {0:x}" , getBFHash (Address));
431
433
OS << " \n " ;
432
434
OS << " BB mappings:\n " ;
@@ -443,6 +445,8 @@ void BoltAddressTranslation::dump(raw_ostream &OS) {
443
445
OS << formatv (" hash: {0:x}" , BBHashMap.getBBHash (Val));
444
446
OS << " \n " ;
445
447
}
448
+ if (IsHotFunction)
449
+ OS << " NumBlocks: " << NumBasicBlocksMap[Address] << ' \n ' ;
446
450
if (SecondaryEntryPointsMap.count (Address)) {
447
451
const std::vector<uint32_t > &SecondaryEntryPoints =
448
452
SecondaryEntryPointsMap[Address];
@@ -574,6 +578,7 @@ void BoltAddressTranslation::saveMetadata(BinaryContext &BC) {
574
578
// Set BF/BB metadata
575
579
for (const BinaryBasicBlock &BB : BF)
576
580
BBHashMap.addEntry (BB.getInputOffset (), BB.getIndex (), BB.getHash ());
581
+ NumBasicBlocksMap.emplace (BF.getAddress (), BF.size ());
577
582
}
578
583
}
579
584
@@ -597,5 +602,49 @@ BoltAddressTranslation::getBFBranches(uint64_t OutputAddress) const {
597
602
return Branches;
598
603
}
599
604
605
+ unsigned
606
+ BoltAddressTranslation::getSecondaryEntryPointId (uint64_t Address,
607
+ uint32_t Offset) const {
608
+ auto FunctionIt = SecondaryEntryPointsMap.find (Address);
609
+ if (FunctionIt == SecondaryEntryPointsMap.end ())
610
+ return 0 ;
611
+ const std::vector<uint32_t > &Offsets = FunctionIt->second ;
612
+ auto OffsetIt = std::find (Offsets.begin (), Offsets.end (), Offset);
613
+ if (OffsetIt == Offsets.end ())
614
+ return 0 ;
615
+ // Adding one here because main entry point is not stored in BAT, and
616
+ // enumeration for secondary entry points starts with 1.
617
+ return OffsetIt - Offsets.begin () + 1 ;
618
+ }
619
+
620
+ std::pair<const BinaryFunction *, unsigned >
621
+ BoltAddressTranslation::translateSymbol (const BinaryContext &BC,
622
+ const MCSymbol &Symbol,
623
+ uint32_t Offset) const {
624
+ // The symbol could be a secondary entry in a cold fragment.
625
+ uint64_t SymbolValue = cantFail (errorOrToExpected (BC.getSymbolValue (Symbol)));
626
+
627
+ const BinaryFunction *Callee = BC.getFunctionForSymbol (&Symbol);
628
+ assert (Callee);
629
+
630
+ // Containing function, not necessarily the same as symbol value.
631
+ const uint64_t CalleeAddress = Callee->getAddress ();
632
+ const uint32_t OutputOffset = SymbolValue - CalleeAddress;
633
+
634
+ const uint64_t ParentAddress = fetchParentAddress (CalleeAddress);
635
+ const uint64_t HotAddress = ParentAddress ? ParentAddress : CalleeAddress;
636
+
637
+ const BinaryFunction *ParentBF = BC.getBinaryFunctionAtAddress (HotAddress);
638
+
639
+ const uint32_t InputOffset =
640
+ translate (CalleeAddress, OutputOffset, /* IsBranchSrc*/ false ) + Offset;
641
+
642
+ unsigned SecondaryEntryId{0 };
643
+ if (InputOffset)
644
+ SecondaryEntryId = getSecondaryEntryPointId (HotAddress, InputOffset);
645
+
646
+ return std::pair (ParentBF, SecondaryEntryId);
647
+ }
648
+
600
649
} // namespace bolt
601
650
} // namespace llvm
0 commit comments