Skip to content

Commit 7e89984

Browse files
committed
Emit the Propeller CFG frequencies.
1 parent 3ed2118 commit 7e89984

File tree

5 files changed

+99
-98
lines changed

5 files changed

+99
-98
lines changed

llvm/include/llvm/CodeGen/BasicBlockSectionsProfileReader.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ struct BBClusterInfo {
4242
unsigned PositionInCluster;
4343
};
4444

45-
// This represents the cfg profile data for a function.
46-
struct CfgProfile {
45+
// This represents the CFG profile data for a function.
46+
struct CFGProfile {
4747
// Node counts for each basic block.
4848
DenseMap<UniqueBBID, uint64_t> NodeCounts;
4949
// Edge counts for each edge, stored as a nested map.
5050
DenseMap<UniqueBBID, DenseMap<UniqueBBID, uint64_t>> EdgeCounts;
5151

5252
// Returns the profile count for the given basic block or zero if it does not
5353
// exist.
54-
uint64_t getNodeCount(const UniqueBBID &BBID) const {
54+
uint64_t getBlockCount(const UniqueBBID &BBID) const {
5555
return NodeCounts.lookup(BBID);
5656
}
5757

@@ -74,8 +74,8 @@ struct FunctionProfile {
7474
// the edge a -> b (a is not cloned). The index of the path in this vector
7575
// determines the `UniqueBBID::CloneID` of the cloned blocks in that path.
7676
SmallVector<SmallVector<unsigned>> ClonePaths;
77-
// Cfg profile data (block and edge frequencies).
78-
CfgProfile Cfg;
77+
// CFG profile data.
78+
CFGProfile CFG;
7979
};
8080

8181
class BasicBlockSectionsProfileReader {
@@ -103,13 +103,13 @@ class BasicBlockSectionsProfileReader {
103103
SmallVector<SmallVector<unsigned>>
104104
getClonePathsForFunction(StringRef FuncName) const;
105105

106-
// Returns a pointer to the CfgProfile for the given function.
106+
// Returns a pointer to the CFGProfile for the given function.
107107
// Returns nullptr if no profile data is available for the function.
108-
const CfgProfile *getFunctionCfgProfile(StringRef FuncName) const {
108+
const CFGProfile *getFunctionCFGProfile(StringRef FuncName) const {
109109
auto It = ProgramPathAndClusterInfo.find(getAliasName(FuncName));
110110
if (It == ProgramPathAndClusterInfo.end())
111111
return nullptr;
112-
return &It->second.Cfg;
112+
return &It->second.CFG;
113113
}
114114

115115
private:
@@ -218,8 +218,6 @@ class BasicBlockSectionsProfileReaderWrapperPass : public ImmutablePass {
218218
SmallVector<SmallVector<unsigned>>
219219
getClonePathsForFunction(StringRef FuncName) const;
220220

221-
const CfgProfile *getFunctionCfgProfile(StringRef FuncName) const;
222-
223221
// Initializes the FunctionNameToDIFilename map for the current module and
224222
// then reads the profile for the matching functions.
225223
bool doInitialization(Module &M) override;

llvm/include/llvm/Object/ELFTypes.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -834,10 +834,13 @@ struct BBAddrMap {
834834
bool OmitBBEntries : 1;
835835
bool CallsiteEndOffsets : 1;
836836
bool BBHash : 1;
837+
bool PropellerCFG : 1;
837838

838-
bool hasPGOAnalysis() const { return FuncEntryCount || BBFreq || BrProb; }
839+
bool hasPGOAnalysis() const {
840+
return FuncEntryCount || BBFreq || BrProb || PropellerCFG;
841+
}
839842

840-
bool hasPGOAnalysisBBData() const { return BBFreq || BrProb; }
843+
bool hasPGOAnalysisBBData() const { return BBFreq || BrProb || PropellerCFG; }
841844

842845
// Encodes to minimum bit width representation.
843846
uint8_t encode() const {
@@ -847,7 +850,8 @@ struct BBAddrMap {
847850
(static_cast<uint8_t>(MultiBBRange) << 3) |
848851
(static_cast<uint8_t>(OmitBBEntries) << 4) |
849852
(static_cast<uint8_t>(CallsiteEndOffsets) << 5) |
850-
(static_cast<uint8_t>(BBHash) << 6);
853+
(static_cast<uint8_t>(BBHash) << 6) |
854+
(static_cast<uint8_t>(PropellerCFG) << 7);
851855
}
852856

853857
// Decodes from minimum bit width representation and validates no
@@ -857,7 +861,7 @@ struct BBAddrMap {
857861
static_cast<bool>(Val & (1 << 0)), static_cast<bool>(Val & (1 << 1)),
858862
static_cast<bool>(Val & (1 << 2)), static_cast<bool>(Val & (1 << 3)),
859863
static_cast<bool>(Val & (1 << 4)), static_cast<bool>(Val & (1 << 5)),
860-
static_cast<bool>(Val & (1 << 6))};
864+
static_cast<bool>(Val & (1 << 6)), static_cast<bool>(Val & (1 << 7))};
861865
if (Feat.encode() != Val)
862866
return createStringError(
863867
std::error_code(), "invalid encoding for BBAddrMap::Features: 0x%x",
@@ -867,10 +871,10 @@ struct BBAddrMap {
867871

868872
bool operator==(const Features &Other) const {
869873
return std::tie(FuncEntryCount, BBFreq, BrProb, MultiBBRange,
870-
OmitBBEntries, CallsiteEndOffsets, BBHash) ==
874+
OmitBBEntries, CallsiteEndOffsets, BBHash, PropellerCFG) ==
871875
std::tie(Other.FuncEntryCount, Other.BBFreq, Other.BrProb,
872876
Other.MultiBBRange, Other.OmitBBEntries,
873-
Other.CallsiteEndOffsets, Other.BBHash);
877+
Other.CallsiteEndOffsets, Other.BBHash, Other.PropellerCFG);
874878
}
875879
};
876880

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
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"
@@ -120,6 +119,7 @@
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
};
154155
static 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-
174171
static 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

486482
bool AsmPrinter::doInitialization(Module &M) {
@@ -1412,7 +1408,7 @@ static uint32_t getBBAddrMapMetadata(const MachineBasicBlock &MBB) {
14121408

14131409
static llvm::object::BBAddrMap::Features
14141410
getBBAddrMapFeature(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

14521454
void 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.
17191716
void AsmPrinter::emitCallGraphSection(const MachineFunction &MF,
17201717
FunctionCallGraphInfo &FuncCGInfo) {
17211718
if (!MF.getTarget().Options.EmitCallGraphSection)

llvm/lib/CodeGen/BasicBlockSectionsProfileReader.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ BasicBlockSectionsProfileReader::getClonePathsForFunction(
100100
// the edge 1->3. Within the given clusters, each cloned block is identified by
101101
// "<original block id>.<clone id>". For instance, 3.1 represents the first
102102
// clone of block 3. Original blocks are specified just with their block ids. A
103-
// block cloned multiple times appears with distinct clone ids. The Cfg for bar
103+
// block cloned multiple times appears with distinct clone ids. The CFG for bar
104104
// is shown below before and after cloning with its final clusters labeled.
105105
//
106106
// f main
@@ -240,12 +240,12 @@ Error BasicBlockSectionsProfileReader::ReadV1Profile() {
240240
}
241241
continue;
242242
}
243-
case 'g': { // Cfg profile specifier.
243+
case 'g': { // CFG profile specifier.
244244
// Skip the profile when we the profile iterator (FI) refers to the
245245
// past-the-end element.
246246
if (FI == ProgramPathAndClusterInfo.end())
247247
continue;
248-
// For each node, its Cfg profile is encoded as
248+
// For each node, its CFG profile is encoded as
249249
// <src>:<count>,<sink_1>:<count_1>,<sink_2>:<count_2>,...
250250
for (auto BasicBlockEdgeProfile : Values) {
251251
if (BasicBlockEdgeProfile.empty())
@@ -264,10 +264,10 @@ Error BasicBlockSectionsProfileReader::ReadV1Profile() {
264264
Twine("unsigned integer expected: '") + CountStr + "'");
265265
if (i == 0) {
266266
// The first element represents the source and its total count.
267-
FI->second.Cfg.NodeCounts[SrcBBID = *BBID] = Count;
267+
FI->second.CFG.NodeCounts[SrcBBID = *BBID] = Count;
268268
continue;
269269
}
270-
FI->second.Cfg.EdgeCounts[SrcBBID][*BBID] = Count;
270+
FI->second.CFG.EdgeCounts[SrcBBID][*BBID] = Count;
271271
}
272272
}
273273
continue;
@@ -472,12 +472,6 @@ BasicBlockSectionsProfileReaderWrapperPass::getClonePathsForFunction(
472472
return BBSPR.getClonePathsForFunction(FuncName);
473473
}
474474

475-
const CfgProfile *
476-
BasicBlockSectionsProfileReaderWrapperPass::getFunctionCfgProfile(
477-
StringRef FuncName) const {
478-
return BBSPR.getFunctionCfgProfile(FuncName);
479-
}
480-
481475
BasicBlockSectionsProfileReader &
482476
BasicBlockSectionsProfileReaderWrapperPass::getBBSPR() {
483477
return BBSPR;

0 commit comments

Comments
 (0)