|
40 | 40 | #include "llvm/CodeGen/GCMetadataPrinter.h"
|
41 | 41 | #include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
|
42 | 42 | #include "llvm/CodeGen/MachineBasicBlock.h"
|
| 43 | +#include "llvm/CodeGen/MachineBranchProbabilityInfo.h" |
43 | 44 | #include "llvm/CodeGen/MachineConstantPool.h"
|
44 | 45 | #include "llvm/CodeGen/MachineDominators.h"
|
45 | 46 | #include "llvm/CodeGen/MachineFrameInfo.h"
|
@@ -139,6 +140,26 @@ static cl::opt<std::string> BasicBlockProfileDump(
|
139 | 140 | "performed with -basic-block-sections=labels. Enabling this "
|
140 | 141 | "flag during in-process ThinLTO is not supported."));
|
141 | 142 |
|
| 143 | +// This is a replication of fields of object::PGOAnalysisMap::Features. It |
| 144 | +// should match the order of the fields so that |
| 145 | +// `object::PGOAnalysisMap::Features::decode(PgoAnalysisMapFeatures.getBits())` |
| 146 | +// succeeds. |
| 147 | +enum class PGOMapFeaturesEnum { |
| 148 | + FuncEntryCount, |
| 149 | + BBFreq, |
| 150 | + BrProb, |
| 151 | +}; |
| 152 | +static cl::bits<PGOMapFeaturesEnum> PgoAnalysisMapFeatures( |
| 153 | + "pgo-analysis-map", cl::Hidden, cl::CommaSeparated, |
| 154 | + cl::values(clEnumValN(PGOMapFeaturesEnum::FuncEntryCount, "func-entry-count", |
| 155 | + "Function Entry Count"), |
| 156 | + clEnumValN(PGOMapFeaturesEnum::BBFreq, "bb-freq", |
| 157 | + "Basic Block Frequency"), |
| 158 | + clEnumValN(PGOMapFeaturesEnum::BrProb, "br-prob", |
| 159 | + "Branch Probability")), |
| 160 | + cl::desc("Enable extended information within the BBAddrMap that is " |
| 161 | + "extracted from PGO related analysis.")); |
| 162 | + |
142 | 163 | const char DWARFGroupName[] = "dwarf";
|
143 | 164 | const char DWARFGroupDescription[] = "DWARF Emission";
|
144 | 165 | const char DbgTimerName[] = "emit";
|
@@ -427,6 +448,7 @@ void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
|
427 | 448 | AU.addRequired<MachineOptimizationRemarkEmitterPass>();
|
428 | 449 | AU.addRequired<GCModuleInfo>();
|
429 | 450 | AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
|
| 451 | + AU.addRequired<MachineBranchProbabilityInfo>(); |
430 | 452 | }
|
431 | 453 |
|
432 | 454 | bool AsmPrinter::doInitialization(Module &M) {
|
@@ -1377,7 +1399,8 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
|
1377 | 1399 | uint8_t BBAddrMapVersion = OutStreamer->getContext().getBBAddrMapVersion();
|
1378 | 1400 | OutStreamer->emitInt8(BBAddrMapVersion);
|
1379 | 1401 | OutStreamer->AddComment("feature");
|
1380 |
| - OutStreamer->emitInt8(0); |
| 1402 | + auto FeaturesBits = static_cast<uint8_t>(PgoAnalysisMapFeatures.getBits()); |
| 1403 | + OutStreamer->emitInt8(FeaturesBits); |
1381 | 1404 | OutStreamer->AddComment("function address");
|
1382 | 1405 | OutStreamer->emitSymbolValue(FunctionSymbol, getPointerSize());
|
1383 | 1406 | OutStreamer->AddComment("number of basic blocks");
|
@@ -1407,6 +1430,51 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
|
1407 | 1430 | OutStreamer->emitULEB128IntValue(getBBAddrMapMetadata(MBB));
|
1408 | 1431 | PrevMBBEndSymbol = MBB.getEndSymbol();
|
1409 | 1432 | }
|
| 1433 | + |
| 1434 | + if (FeaturesBits != 0) { |
| 1435 | + assert(BBAddrMapVersion >= 2 && |
| 1436 | + "PGOAnalysisMap only supports version 2 or later"); |
| 1437 | + |
| 1438 | + auto FeatEnable = |
| 1439 | + cantFail(object::PGOAnalysisMap::Features::decode(FeaturesBits)); |
| 1440 | + |
| 1441 | + if (FeatEnable.FuncEntryCount) { |
| 1442 | + OutStreamer->AddComment("function entry count"); |
| 1443 | + auto MaybeEntryCount = MF.getFunction().getEntryCount(); |
| 1444 | + OutStreamer->emitULEB128IntValue( |
| 1445 | + MaybeEntryCount ? MaybeEntryCount->getCount() : 0); |
| 1446 | + } |
| 1447 | + const MachineBlockFrequencyInfo *MBFI = |
| 1448 | + FeatEnable.BBFreq |
| 1449 | + ? &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() |
| 1450 | + : nullptr; |
| 1451 | + const MachineBranchProbabilityInfo *MBPI = |
| 1452 | + FeatEnable.BrProb ? &getAnalysis<MachineBranchProbabilityInfo>() |
| 1453 | + : nullptr; |
| 1454 | + |
| 1455 | + if (FeatEnable.BBFreq || FeatEnable.BrProb) { |
| 1456 | + for (const MachineBasicBlock &MBB : MF) { |
| 1457 | + if (FeatEnable.BBFreq) { |
| 1458 | + OutStreamer->AddComment("basic block frequency"); |
| 1459 | + OutStreamer->emitULEB128IntValue( |
| 1460 | + MBFI->getBlockFreq(&MBB).getFrequency()); |
| 1461 | + } |
| 1462 | + if (FeatEnable.BrProb) { |
| 1463 | + unsigned SuccCount = MBB.succ_size(); |
| 1464 | + OutStreamer->AddComment("basic block successor count"); |
| 1465 | + OutStreamer->emitULEB128IntValue(SuccCount); |
| 1466 | + for (const MachineBasicBlock *SuccMBB : MBB.successors()) { |
| 1467 | + OutStreamer->AddComment("successor BB ID"); |
| 1468 | + OutStreamer->emitULEB128IntValue(SuccMBB->getBBID()->BaseID); |
| 1469 | + OutStreamer->AddComment("successor branch probability"); |
| 1470 | + OutStreamer->emitULEB128IntValue( |
| 1471 | + MBPI->getEdgeProbability(&MBB, SuccMBB).getNumerator()); |
| 1472 | + } |
| 1473 | + } |
| 1474 | + } |
| 1475 | + } |
| 1476 | + } |
| 1477 | + |
1410 | 1478 | OutStreamer->popSection();
|
1411 | 1479 | }
|
1412 | 1480 |
|
@@ -1932,8 +2000,15 @@ void AsmPrinter::emitFunctionBody() {
|
1932 | 2000 |
|
1933 | 2001 | // Emit section containing BB address offsets and their metadata, when
|
1934 | 2002 | // BB labels are requested for this function. Skip empty functions.
|
1935 |
| - if (MF->hasBBLabels() && HasAnyRealCode) |
| 2003 | + bool HasLabels = MF->hasBBLabels(); |
| 2004 | + if (HasLabels && HasAnyRealCode) |
1936 | 2005 | emitBBAddrMapSection(*MF);
|
| 2006 | + else if (!HasLabels && HasAnyRealCode && |
| 2007 | + PgoAnalysisMapFeatures.getBits() != 0) |
| 2008 | + MF->getContext().reportWarning( |
| 2009 | + SMLoc(), "pgo-analysis-map is enabled but the following machine " |
| 2010 | + "function was does not have labels: " + |
| 2011 | + MF->getName()); |
1937 | 2012 |
|
1938 | 2013 | // Emit sections containing instruction and function PCs.
|
1939 | 2014 | emitPCSections(*MF);
|
|
0 commit comments