2020#include " WinException.h"
2121#include " llvm/ADT/APFloat.h"
2222#include " llvm/ADT/APInt.h"
23- #include " llvm/ADT/BitmaskEnum.h"
2423#include " llvm/ADT/DenseMap.h"
2524#include " llvm/ADT/STLExtras.h"
2625#include " llvm/ADT/SmallPtrSet.h"
120119#include " llvm/Support/MathExtras.h"
121120#include " llvm/Support/Path.h"
122121#include " llvm/Support/VCSRevision.h"
122+ #include " llvm/Support/VirtualFileSystem.h"
123123#include " llvm/Support/raw_ostream.h"
124124#include " llvm/Target/TargetLoweringObjectFile.h"
125125#include " llvm/Target/TargetMachine.h"
@@ -149,6 +149,7 @@ enum class PGOMapFeaturesEnum {
149149 FuncEntryCount,
150150 BBFreq,
151151 BrProb,
152+ PropellerCFG,
152153 All,
153154};
154155static cl::bits<PGOMapFeaturesEnum> PgoAnalysisMapFeatures (
@@ -160,17 +161,13 @@ static cl::bits<PGOMapFeaturesEnum> PgoAnalysisMapFeatures(
160161 clEnumValN(PGOMapFeaturesEnum::BBFreq, " bb-freq" ,
161162 " Basic Block Frequency" ),
162163 clEnumValN(PGOMapFeaturesEnum::BrProb, " br-prob" , " Branch Probability" ),
164+ clEnumValN(PGOMapFeaturesEnum::PropellerCFG, " propeller-cfg" ,
165+ " Propeller CFG" ),
163166 clEnumValN(PGOMapFeaturesEnum::All, " all" , " Enable all options" )),
164167 cl::desc(
165168 " Enable extended information within the SHT_LLVM_BB_ADDR_MAP that is "
166169 " extracted from PGO related analysis." ));
167170
168- static cl::opt<bool > PgoAnalysisMapUsePropellerCfg (
169- " pgo-analysis-map-use-propeller-cfg" ,
170- cl::desc (
171- " If available, use the Propeller cfg profile in the PGO analysis map." ),
172- cl::Hidden, cl::init(false ));
173-
174171static cl::opt<bool > BBAddrMapSkipEmitBBEntries (
175172 " basic-block-address-map-skip-bb-entries" ,
176173 cl::desc (" Skip emitting basic block entries in the SHT_LLVM_BB_ADDR_MAP "
@@ -480,7 +477,6 @@ void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
480477 AU.addRequired <GCModuleInfo>();
481478 AU.addRequired <LazyMachineBlockFrequencyInfoPass>();
482479 AU.addRequired <MachineBranchProbabilityInfoWrapperPass>();
483- AU.addUsedIfAvailable <BasicBlockSectionsProfileReaderWrapperPass>();
484480}
485481
486482bool AsmPrinter::doInitialization (Module &M) {
@@ -1412,7 +1408,7 @@ static uint32_t getBBAddrMapMetadata(const MachineBasicBlock &MBB) {
14121408
14131409static llvm::object::BBAddrMap::Features
14141410getBBAddrMapFeature (const MachineFunction &MF, int NumMBBSectionRanges,
1415- bool HasCalls) {
1411+ bool HasCalls, const CFGProfile *FuncCFGProfile ) {
14161412 // Ensure that the user has not passed in additional options while also
14171413 // specifying all or none.
14181414 if ((PgoAnalysisMapFeatures.isSet (PGOMapFeaturesEnum::None) ||
@@ -1434,19 +1430,25 @@ getBBAddrMapFeature(const MachineFunction &MF, int NumMBBSectionRanges,
14341430 bool BrProbEnabled =
14351431 AllFeatures ||
14361432 (!NoFeatures && PgoAnalysisMapFeatures.isSet (PGOMapFeaturesEnum::BrProb));
1433+ bool PropellerCFGEnabled =
1434+ FuncCFGProfile &&
1435+ (AllFeatures ||
1436+ (!NoFeatures &&
1437+ PgoAnalysisMapFeatures.isSet (PGOMapFeaturesEnum::PropellerCFG)));
14371438
14381439 if ((BBFreqEnabled || BrProbEnabled) && BBAddrMapSkipEmitBBEntries) {
14391440 MF.getFunction ().getContext ().emitError (
1440- " BB entries info is required for BBFreq and BrProb "
1441- " features" );
1441+ " BB entries info is required for BBFreq and BrProb features" );
14421442 }
1443+
14431444 return {FuncEntryCountEnabled,
14441445 BBFreqEnabled,
14451446 BrProbEnabled,
14461447 MF.hasBBSections () && NumMBBSectionRanges > 1 ,
14471448 static_cast <bool >(BBAddrMapSkipEmitBBEntries),
14481449 HasCalls,
1449- false };
1450+ false ,
1451+ PropellerCFGEnabled};
14501452}
14511453
14521454void AsmPrinter::emitBBAddrMapSection (const MachineFunction &MF) {
@@ -1455,6 +1457,14 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
14551457 assert (BBAddrMapSection && " .llvm_bb_addr_map section is not initialized." );
14561458 bool HasCalls = !CurrentFnCallsiteEndSymbols.empty ();
14571459
1460+ const BasicBlockSectionsProfileReader *BBSPR = nullptr ;
1461+ if (auto *BBSPRPass =
1462+ getAnalysisIfAvailable<BasicBlockSectionsProfileReaderWrapperPass>())
1463+ BBSPR = &BBSPRPass->getBBSPR ();
1464+ const CFGProfile *FuncCFGProfile = nullptr ;
1465+ if (BBSPR)
1466+ FuncCFGProfile = BBSPR->getFunctionCFGProfile (MF.getFunction ().getName ());
1467+
14581468 const MCSymbol *FunctionSymbol = getFunctionBegin ();
14591469
14601470 OutStreamer->pushSection ();
@@ -1463,7 +1473,8 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
14631473 uint8_t BBAddrMapVersion = OutStreamer->getContext ().getBBAddrMapVersion ();
14641474 OutStreamer->emitInt8 (BBAddrMapVersion);
14651475 OutStreamer->AddComment (" feature" );
1466- auto Features = getBBAddrMapFeature (MF, MBBSectionRanges.size (), HasCalls);
1476+ auto Features =
1477+ getBBAddrMapFeature (MF, MBBSectionRanges.size (), HasCalls, FuncCFGProfile);
14671478 OutStreamer->emitInt8 (Features.encode ());
14681479 // Emit BB Information for each basic block in the function.
14691480 if (Features.MultiBBRange ) {
@@ -1540,16 +1551,12 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
15401551 assert (BBAddrMapVersion >= 2 &&
15411552 " PGOAnalysisMap only supports version 2 or later" );
15421553
1543- // We will emit the BBSPR profile data if requested and availale. Otherwise,
1544- // we fall back to MBFI and MBPI.
1545- const CfgProfile *FuncCfgProfile = nullptr ;
1546- if (PgoAnalysisMapUsePropellerCfg) {
1547- if (auto *BBSPR = getAnalysisIfAvailable<
1548- BasicBlockSectionsProfileReaderWrapperPass>())
1549- FuncCfgProfile =
1550- BBSPR->getFunctionCfgProfile (MF.getFunction ().getName ());
1554+ if (Features.FuncEntryCount ) {
1555+ OutStreamer->AddComment (" function entry count" );
1556+ auto MaybeEntryCount = MF.getFunction ().getEntryCount ();
1557+ OutStreamer->emitULEB128IntValue (
1558+ MaybeEntryCount ? MaybeEntryCount->getCount () : 0 );
15511559 }
1552-
15531560 const MachineBlockFrequencyInfo *MBFI =
15541561 Features.BBFreq
15551562 ? &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI ()
@@ -1559,43 +1566,33 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
15591566 ? &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI ()
15601567 : nullptr ;
15611568
1562- if (Features.FuncEntryCount ) {
1563- OutStreamer->AddComment (" function entry count" );
1564- uint64_t EntryCount = 0 ;
1565- if (FuncCfgProfile) {
1566- EntryCount = FuncCfgProfile->getNodeCount (*MF.front ().getBBID ());
1567- } else {
1568- auto MaybeEntryCount = MF.getFunction ().getEntryCount ();
1569- EntryCount = MaybeEntryCount ? MaybeEntryCount->getCount () : 0 ;
1570- }
1571- OutStreamer->emitULEB128IntValue (EntryCount);
1572- }
1573-
15741569 if (Features.BBFreq || Features.BrProb ) {
15751570 for (const MachineBasicBlock &MBB : MF) {
1576-
15771571 if (Features.BBFreq ) {
15781572 OutStreamer->AddComment (" basic block frequency" );
1579- uint64_t BlockFrequency =
1580- FuncCfgProfile ? FuncCfgProfile->getNodeCount (*MBB.getBBID ())
1581- : MBFI->getBlockFreq (&MBB).getFrequency ();
1582- OutStreamer->emitULEB128IntValue (BlockFrequency);
1573+ OutStreamer->emitULEB128IntValue (
1574+ MBFI->getBlockFreq (&MBB).getFrequency ());
1575+ if (Features.PropellerCFG ) {
1576+ OutStreamer->AddComment (" basic block frequency (propeller)" );
1577+ OutStreamer->emitULEB128IntValue (
1578+ FuncCFGProfile->getBlockCount (*MBB.getBBID ()));
1579+ }
15831580 }
15841581 if (Features.BrProb ) {
1582+ unsigned SuccCount = MBB.succ_size ();
15851583 OutStreamer->AddComment (" basic block successor count" );
1586- OutStreamer->emitULEB128IntValue (MBB. succ_size () );
1584+ OutStreamer->emitULEB128IntValue (SuccCount );
15871585 for (const MachineBasicBlock *SuccMBB : MBB.successors ()) {
15881586 OutStreamer->AddComment (" successor BB ID" );
15891587 OutStreamer->emitULEB128IntValue (SuccMBB->getBBID ()->BaseID );
15901588 OutStreamer->AddComment (" successor branch probability" );
1591- // For MPBI, we emit the numerator of the probability. For BBSPR, we
1592- // emit the raw edge count.
1593- uint64_t EdgeFrequency =
1594- FuncCfgProfile
1595- ? FuncCfgProfile->getEdgeCount (*MBB.getBBID (),
1596- *SuccMBB->getBBID ())
1597- : MBPI->getEdgeProbability (&MBB, SuccMBB).getNumerator ();
1598- OutStreamer->emitULEB128IntValue (EdgeFrequency);
1589+ OutStreamer->emitULEB128IntValue (
1590+ MBPI->getEdgeProbability (&MBB, SuccMBB).getNumerator ());
1591+ if (Features.PropellerCFG ) {
1592+ OutStreamer->AddComment (" successor branch frequency (propeller)" );
1593+ OutStreamer->emitULEB128IntValue (FuncCFGProfile->getEdgeCount (
1594+ *MBB.getBBID (), *SuccMBB->getBBID ()));
1595+ }
15991596 }
16001597 }
16011598 }
@@ -1715,7 +1712,7 @@ static ConstantInt *extractNumericCGTypeId(const Function &F) {
17151712 return nullptr ;
17161713}
17171714
1718- // / Emits .callgraph section.
1715+ // / Emits .llvm. callgraph section.
17191716void AsmPrinter::emitCallGraphSection (const MachineFunction &MF,
17201717 FunctionCallGraphInfo &FuncCGInfo) {
17211718 if (!MF.getTarget ().Options .EmitCallGraphSection )
0 commit comments