@@ -45,6 +45,8 @@ void BoltAddressTranslation::writeEntriesForBB(MapTy &Map,
4545 LLVM_DEBUG (dbgs () << formatv (" Hash: {0:x}\n " ,
4646 getBBHash (HotFuncAddress, BBInputOffset)));
4747 (void )HotFuncAddress;
48+ LLVM_DEBUG (dbgs () << formatv (" Index: {0}\n " ,
49+ getBBIndex (HotFuncAddress, BBInputOffset)));
4850 // In case of conflicts (same Key mapping to different Vals), the last
4951 // update takes precedence. Of course it is not ideal to have conflicts and
5052 // those happen when we have an empty BB that either contained only
@@ -217,6 +219,7 @@ void BoltAddressTranslation::writeMaps(std::map<uint64_t, MapTy> &Maps,
217219 }
218220 size_t Index = 0 ;
219221 uint64_t InOffset = 0 ;
222+ size_t PrevBBIndex = 0 ;
220223 // Output and Input addresses and delta-encoded
221224 for (std::pair<const uint32_t , uint32_t > &KeyVal : Map) {
222225 const uint64_t OutputAddress = KeyVal.first + Address;
@@ -226,11 +229,15 @@ void BoltAddressTranslation::writeMaps(std::map<uint64_t, MapTy> &Maps,
226229 encodeSLEB128 (KeyVal.second - InOffset, OS);
227230 InOffset = KeyVal.second ; // Keeping InOffset as if BRANCHENTRY is encoded
228231 if ((InOffset & BRANCHENTRY) == 0 ) {
229- // Basic block hash
230- size_t BBHash = FuncHashPair.second [InOffset >> 1 ];
232+ unsigned BBIndex;
233+ size_t BBHash;
234+ std::tie (BBIndex, BBHash) = FuncHashPair.second [InOffset >> 1 ];
231235 OS.write (reinterpret_cast <char *>(&BBHash), 8 );
232- LLVM_DEBUG (dbgs () << formatv (" {0:x} -> {1:x} {2:x}\n " , KeyVal.first ,
233- InOffset >> 1 , BBHash));
236+ // Basic block index in the input binary
237+ encodeULEB128 (BBIndex - PrevBBIndex, OS);
238+ PrevBBIndex = BBIndex;
239+ LLVM_DEBUG (dbgs () << formatv (" {0:x} -> {1:x} {2:x} {3}\n " , KeyVal.first ,
240+ InOffset >> 1 , BBHash, BBIndex));
234241 }
235242 }
236243 }
@@ -316,6 +323,7 @@ void BoltAddressTranslation::parseMaps(std::vector<uint64_t> &HotFuncs,
316323 LLVM_DEBUG (dbgs () << " Parsing " << NumEntries << " entries for 0x"
317324 << Twine::utohexstr (Address) << " \n " );
318325 uint64_t InputOffset = 0 ;
326+ size_t BBIndex = 0 ;
319327 for (uint32_t J = 0 ; J < NumEntries; ++J) {
320328 const uint64_t OutputDelta = DE.getULEB128 (&Offset, &Err);
321329 const uint64_t OutputAddress = PrevAddress + OutputDelta;
@@ -330,19 +338,25 @@ void BoltAddressTranslation::parseMaps(std::vector<uint64_t> &HotFuncs,
330338 }
331339 Map.insert (std::pair<uint32_t , uint32_t >(OutputOffset, InputOffset));
332340 size_t BBHash = 0 ;
341+ size_t BBIndexDelta = 0 ;
333342 const bool IsBranchEntry = InputOffset & BRANCHENTRY;
334343 if (!IsBranchEntry) {
335344 BBHash = DE.getU64 (&Offset, &Err);
345+ BBIndexDelta = DE.getULEB128 (&Offset, &Err);
346+ BBIndex += BBIndexDelta;
336347 // Map basic block hash to hot fragment by input offset
337- FuncHashes[HotAddress].second .emplace (InputOffset >> 1 , BBHash);
348+ FuncHashes[HotAddress].second .emplace (InputOffset >> 1 ,
349+ std::pair (BBIndex, BBHash));
338350 }
339351 LLVM_DEBUG ({
340352 dbgs () << formatv (
341353 " {0:x} -> {1:x} ({2}/{3}b -> {4}/{5}b), {6:x}" , OutputOffset,
342354 InputOffset, OutputDelta, getULEB128Size (OutputDelta), InputDelta,
343355 (J < EqualElems) ? 0 : getSLEB128Size (InputDelta), OutputAddress);
344- if (BBHash)
345- dbgs () << formatv (" {0:x}" , BBHash);
356+ if (!IsBranchEntry) {
357+ dbgs () << formatv (" {0:x} {1}/{2}b" , BBHash, BBIndex,
358+ getULEB128Size (BBIndexDelta));
359+ }
346360 dbgs () << ' \n ' ;
347361 });
348362 }
@@ -494,14 +508,19 @@ void BoltAddressTranslation::saveMetadata(BinaryContext &BC) {
494508 FuncHashes[BF.getAddress ()].first = BF.computeHash ();
495509 BF.computeBlockHashes ();
496510 for (const BinaryBasicBlock &BB : BF)
497- FuncHashes[BF.getAddress ()].second .emplace (BB. getInputOffset (),
498- BB.getHash ());
511+ FuncHashes[BF.getAddress ()].second .emplace (
512+ BB. getInputOffset (), std::pair (BB. getIndex (), BB.getHash () ));
499513 }
500514}
501515
516+ unsigned BoltAddressTranslation::getBBIndex (uint64_t FuncOutputAddress,
517+ uint32_t BBInputOffset) const {
518+ return FuncHashes.at (FuncOutputAddress).second .at (BBInputOffset).first ;
519+ }
520+
502521size_t BoltAddressTranslation::getBBHash (uint64_t FuncOutputAddress,
503522 uint32_t BBInputOffset) const {
504- return FuncHashes.at (FuncOutputAddress).second .at (BBInputOffset);
523+ return FuncHashes.at (FuncOutputAddress).second .at (BBInputOffset). second ;
505524}
506525
507526size_t BoltAddressTranslation::getBFHash (uint64_t OutputAddress) const {
0 commit comments