Skip to content

Commit f9bc1ee

Browse files
[llvm-objdump] Add support for symbolizing PGOBBAddrMap Info (llvm#76386)
This patch adds in support for symbolizing PGO information contained within the SHT_LLVM_BB_ADDR_MAP section in llvm-objdump. The outputs are simply the raw values contained within the section.
1 parent 86eaf60 commit f9bc1ee

File tree

2 files changed

+263
-17
lines changed

2 files changed

+263
-17
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
## Test that in the presence of SHT_LLVM_BB_ADDR_MAP sections which also
2+
## contain PGO data, --symbolize-operands is able to label the basic blocks
3+
## correctly.
4+
5+
## Check the case where we only have entry counts.
6+
7+
# RUN: yaml2obj --docnum=1 %s -o %t1
8+
# RUN: llvm-objdump %t1 -d --symbolize-operands --no-show-raw-insn --no-leading-addr | \
9+
# RUN: FileCheck %s --check-prefix=ENTRYCOUNT
10+
11+
--- !ELF
12+
FileHeader:
13+
Class: ELFCLASS64
14+
Data: ELFDATA2LSB
15+
Type: ET_EXEC
16+
Machine: EM_X86_64
17+
Sections:
18+
- Name: .text.foo
19+
Type: SHT_PROGBITS
20+
Address: 0x0
21+
Flags: [SHF_ALLOC, SHF_EXECINSTR]
22+
Content: '50'
23+
- Name: .llvm_bb_addr_map.foo
24+
Type: SHT_LLVM_BB_ADDR_MAP
25+
Link: .text.foo
26+
Entries:
27+
- Version: 2
28+
Address: 0x0
29+
Feature: 0x1
30+
BBEntries:
31+
- ID: 3
32+
AddressOffset: 0x0
33+
Size: 0x1
34+
Metadata: 0x1
35+
PGOAnalyses:
36+
- FuncEntryCount: 1000
37+
Symbols:
38+
- Name: foo
39+
Section: .text.foo
40+
Value: 0x0
41+
42+
# ENTRYCOUNT: <foo>:
43+
# ENTRYCOUNT: <BB3> (Entry count: 1000):
44+
45+
## Check the case where we have entry points and block frequency information
46+
47+
# RUN: yaml2obj %s --docnum=2 -o %t2
48+
# RUN: llvm-objdump %t2 -d --symbolize-operands --no-show-raw-insn --no-leading-addr | \
49+
# RUN: FileCheck %s --check-prefix=ENTRYCOUNT-BLOCKFREQ
50+
51+
--- !ELF
52+
FileHeader:
53+
Class: ELFCLASS64
54+
Data: ELFDATA2LSB
55+
Type: ET_EXEC
56+
Machine: EM_X86_64
57+
Sections:
58+
- Name: .text.foo
59+
Type: SHT_PROGBITS
60+
Address: 0x0
61+
Flags: [SHF_ALLOC, SHF_EXECINSTR]
62+
Content: '503b0505200000907d02ebf5c3'
63+
- Name: .llvm_bb_addr_map.foo
64+
Type: SHT_LLVM_BB_ADDR_MAP
65+
Link: .text.foo
66+
Entries:
67+
- Version: 2
68+
Address: 0x0
69+
Feature: 0x3
70+
BBEntries:
71+
- ID: 3
72+
AddressOffset: 0x0
73+
Size: 0x1
74+
Metadata: 0x1
75+
- ID: 1
76+
AddressOffset: 0x0
77+
Size: 0x6
78+
Metadata: 0x0
79+
- ID: 2
80+
AddressOffset: 0x1
81+
Size: 0x4
82+
Metadata: 0x0
83+
- ID: 5
84+
AddressOffset: 0x0
85+
Size: 0x1
86+
Metadata: 0x2
87+
PGOAnalyses:
88+
- FuncEntryCount: 1000
89+
PGOBBEntries:
90+
- BBFreq: 1000
91+
- BBFreq: 133
92+
- BBFreq: 18
93+
- BBFreq: 1000
94+
Symbols:
95+
- Name: foo
96+
Section: .text.foo
97+
Value: 0x0
98+
99+
# ENTRYCOUNT-BLOCKFREQ: <foo>:
100+
# ENTRYCOUNT-BLOCKFREQ: <BB3> (Entry count: 1000, Frequency: 1000):
101+
# ENTRYCOUNT-BLOCKFREQ: <BB1> (Frequency: 133):
102+
# ENTRYCOUNT-BLOCKFREQ: <BB2> (Frequency: 18):
103+
# ENTRYCOUNT-BLOCKFREQ: <BB5> (Frequency: 1000):
104+
105+
## Check the case where we have entry points, block frequency, and branch
106+
## proabability information.
107+
108+
# RUN: yaml2obj %s --docnum=3 -o %t3
109+
# RUN: llvm-objdump %t3 -d --symbolize-operands --no-show-raw-insn --no-leading-addr | \
110+
# RUN: FileCheck %s --check-prefix=ENTRY-FREQ-PROB
111+
112+
--- !ELF
113+
FileHeader:
114+
Class: ELFCLASS64
115+
Data: ELFDATA2LSB
116+
Type: ET_EXEC
117+
Machine: EM_X86_64
118+
Sections:
119+
- Name: .text.foo
120+
Type: SHT_PROGBITS
121+
Address: 0x0
122+
Flags: [SHF_ALLOC, SHF_EXECINSTR]
123+
Content: '503b0505200000907d02ebf5c3'
124+
- Name: .llvm_bb_addr_map.foo
125+
Type: SHT_LLVM_BB_ADDR_MAP
126+
Link: .text.foo
127+
Entries:
128+
- Version: 2
129+
Address: 0x0
130+
Feature: 0x7
131+
BBEntries:
132+
- ID: 3
133+
AddressOffset: 0x0
134+
Size: 0x1
135+
Metadata: 0x1
136+
- ID: 1
137+
AddressOffset: 0x0
138+
Size: 0x6
139+
Metadata: 0x0
140+
- ID: 2
141+
AddressOffset: 0x1
142+
Size: 0x4
143+
Metadata: 0x0
144+
- ID: 5
145+
AddressOffset: 0x0
146+
Size: 0x1
147+
Metadata: 0x2
148+
PGOAnalyses:
149+
- FuncEntryCount: 1000
150+
PGOBBEntries:
151+
- BBFreq: 1000
152+
Successors:
153+
- ID: 1
154+
BrProb: 0x22222222
155+
- ID: 2
156+
BrProb: 0x33333333
157+
- ID: 3
158+
BrProb: 0xaaaaaaaa
159+
- BBFreq: 133
160+
Successors:
161+
- ID: 2
162+
BrProb: 0x11111111
163+
- ID: 3
164+
BrProb: 0xeeeeeeee
165+
- BBFreq: 18
166+
Successors:
167+
- ID: 3
168+
BrProb: 0xffffffff
169+
- BBFreq: 1000
170+
Successors: []
171+
Symbols:
172+
- Name: foo
173+
Section: .text.foo
174+
Value: 0x0
175+
176+
# ENTRY-FREQ-PROB: <foo>:
177+
# ENTRY-FREQ-PROB: <BB3> (Entry count: 1000, Frequency: 1000, Successors: BB1:22222222, BB2:33333333, BB3:aaaaaaaa):
178+
# ENTRY-FREQ-PROB: <BB1> (Frequency: 133, Successors: BB2:11111111, BB3:eeeeeeee):
179+
# ENTRY-FREQ-PROB: <BB2> (Frequency: 18, Successors: BB3:ffffffff):
180+
# ENTRY-FREQ-PROB: <BB5> (Frequency: 1000):

llvm/tools/llvm-objdump/llvm-objdump.cpp

Lines changed: 83 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,10 +1263,57 @@ static SymbolInfoTy createDummySymbolInfo(const ObjectFile &Obj,
12631263
return SymbolInfoTy(Addr, Name, Type);
12641264
}
12651265

1266-
static void
1267-
collectBBAddrMapLabels(const std::unordered_map<uint64_t, BBAddrMap> &AddrToBBAddrMap,
1268-
uint64_t SectionAddr, uint64_t Start, uint64_t End,
1269-
std::unordered_map<uint64_t, std::vector<std::string>> &Labels) {
1266+
struct BBAddrMapLabel {
1267+
std::string BlockLabel;
1268+
std::string PGOAnalysis;
1269+
};
1270+
1271+
static std::string constructPGOLabelString(const PGOAnalysisMap &PGOMap,
1272+
size_t BBEntryIndex) {
1273+
std::string PGOString;
1274+
raw_string_ostream PGOSS(PGOString);
1275+
1276+
PGOSS << " (";
1277+
if (PGOMap.FeatEnable.FuncEntryCount && BBEntryIndex == 0) {
1278+
PGOSS << "Entry count: " << Twine(PGOMap.FuncEntryCount);
1279+
if (PGOMap.FeatEnable.BBFreq || PGOMap.FeatEnable.BrProb) {
1280+
PGOSS << ", ";
1281+
}
1282+
}
1283+
1284+
if (PGOMap.FeatEnable.BBFreq || PGOMap.FeatEnable.BrProb) {
1285+
assert(BBEntryIndex < PGOMap.BBEntries.size() &&
1286+
"Expected PGOAnalysisMap and BBAddrMap to have the same entires");
1287+
const PGOAnalysisMap::PGOBBEntry &PGOBBEntry =
1288+
PGOMap.BBEntries[BBEntryIndex];
1289+
1290+
if (PGOMap.FeatEnable.BBFreq) {
1291+
PGOSS << "Frequency: " << Twine(PGOBBEntry.BlockFreq.getFrequency());
1292+
if (PGOMap.FeatEnable.BrProb && PGOBBEntry.Successors.size() > 0) {
1293+
PGOSS << ", ";
1294+
}
1295+
}
1296+
if (PGOMap.FeatEnable.BrProb && PGOBBEntry.Successors.size() > 0) {
1297+
PGOSS << "Successors: ";
1298+
interleaveComma(
1299+
PGOBBEntry.Successors, PGOSS,
1300+
[&PGOSS](const PGOAnalysisMap::PGOBBEntry::SuccessorEntry &SE) {
1301+
PGOSS << "BB" << SE.ID << ":";
1302+
PGOSS.write_hex(SE.Prob.getNumerator());
1303+
});
1304+
}
1305+
}
1306+
PGOSS << ")";
1307+
1308+
return PGOString;
1309+
}
1310+
1311+
static void collectBBAddrMapLabels(
1312+
const std::unordered_map<uint64_t, BBAddrMap> &AddrToBBAddrMap,
1313+
const std::unordered_map<uint64_t, PGOAnalysisMap> &AddrToPGOAnalysisMap,
1314+
uint64_t SectionAddr, uint64_t Start, uint64_t End,
1315+
std::unordered_map<uint64_t, std::vector<BBAddrMapLabel>> &Labels,
1316+
const StringRef FileName) {
12701317
if (AddrToBBAddrMap.empty())
12711318
return;
12721319
Labels.clear();
@@ -1275,11 +1322,21 @@ collectBBAddrMapLabels(const std::unordered_map<uint64_t, BBAddrMap> &AddrToBBAd
12751322
auto Iter = AddrToBBAddrMap.find(StartAddress);
12761323
if (Iter == AddrToBBAddrMap.end())
12771324
return;
1278-
for (const BBAddrMap::BBEntry &BBEntry : Iter->second.getBBEntries()) {
1325+
auto PGOIter = AddrToPGOAnalysisMap.find(StartAddress);
1326+
1327+
for (size_t I = 0; I < Iter->second.getBBEntries().size(); ++I) {
1328+
const BBAddrMap::BBEntry &BBEntry = Iter->second.getBBEntries()[I];
12791329
uint64_t BBAddress = BBEntry.Offset + Iter->second.getFunctionAddress();
12801330
if (BBAddress >= EndAddress)
12811331
continue;
1282-
Labels[BBAddress].push_back(("BB" + Twine(BBEntry.ID)).str());
1332+
1333+
std::string LabelString = ("BB" + Twine(BBEntry.ID)).str();
1334+
std::string PGOString;
1335+
1336+
if (PGOIter != AddrToPGOAnalysisMap.end())
1337+
PGOString = constructPGOLabelString(PGOIter->second, I);
1338+
1339+
Labels[BBAddress].push_back({LabelString, PGOString});
12831340
}
12841341
}
12851342

@@ -1637,18 +1694,24 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
16371694
LLVM_DEBUG(LVP.dump());
16381695

16391696
std::unordered_map<uint64_t, BBAddrMap> AddrToBBAddrMap;
1697+
std::unordered_map<uint64_t, PGOAnalysisMap> AddrToPGOAnalysisMap;
16401698
auto ReadBBAddrMap = [&](std::optional<unsigned> SectionIndex =
16411699
std::nullopt) {
16421700
AddrToBBAddrMap.clear();
16431701
if (const auto *Elf = dyn_cast<ELFObjectFileBase>(&Obj)) {
1644-
auto BBAddrMapsOrErr = Elf->readBBAddrMap(SectionIndex);
1702+
std::vector<PGOAnalysisMap> PGOAnalyses;
1703+
auto BBAddrMapsOrErr = Elf->readBBAddrMap(SectionIndex, &PGOAnalyses);
16451704
if (!BBAddrMapsOrErr) {
16461705
reportWarning(toString(BBAddrMapsOrErr.takeError()), Obj.getFileName());
16471706
return;
16481707
}
1649-
for (auto &FunctionBBAddrMap : *BBAddrMapsOrErr)
1650-
AddrToBBAddrMap.emplace(FunctionBBAddrMap.Addr,
1651-
std::move(FunctionBBAddrMap));
1708+
for (const auto &[FunctionBBAddrMap, FunctionPGOAnalysis] :
1709+
zip_equal(*std::move(BBAddrMapsOrErr), std::move(PGOAnalyses))) {
1710+
uint64_t Addr = FunctionBBAddrMap.Addr;
1711+
AddrToBBAddrMap.emplace(Addr, std::move(FunctionBBAddrMap));
1712+
if (FunctionPGOAnalysis.FeatEnable.anyEnabled())
1713+
AddrToPGOAnalysisMap.emplace(Addr, std::move(FunctionPGOAnalysis));
1714+
}
16521715
}
16531716
};
16541717

@@ -1977,14 +2040,15 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
19772040
FOS.SetUnbuffered();
19782041

19792042
std::unordered_map<uint64_t, std::string> AllLabels;
1980-
std::unordered_map<uint64_t, std::vector<std::string>> BBAddrMapLabels;
2043+
std::unordered_map<uint64_t, std::vector<BBAddrMapLabel>> BBAddrMapLabels;
19812044
if (SymbolizeOperands) {
19822045
collectLocalBranchTargets(Bytes, DT->InstrAnalysis.get(),
19832046
DT->DisAsm.get(), DT->InstPrinter.get(),
19842047
PrimaryTarget.SubtargetInfo.get(),
19852048
SectionAddr, Index, End, AllLabels);
1986-
collectBBAddrMapLabels(AddrToBBAddrMap, SectionAddr, Index, End,
1987-
BBAddrMapLabels);
2049+
collectBBAddrMapLabels(AddrToBBAddrMap, AddrToPGOAnalysisMap,
2050+
SectionAddr, Index, End, BBAddrMapLabels,
2051+
FileName);
19882052
}
19892053

19902054
if (DT->InstrAnalysis)
@@ -2082,8 +2146,9 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
20822146
// Print local label if there's any.
20832147
auto Iter1 = BBAddrMapLabels.find(SectionAddr + Index);
20842148
if (Iter1 != BBAddrMapLabels.end()) {
2085-
for (StringRef Label : Iter1->second)
2086-
FOS << "<" << Label << ">:\n";
2149+
for (const auto &BBLabel : Iter1->second)
2150+
FOS << "<" << BBLabel.BlockLabel << ">" << BBLabel.PGOAnalysis
2151+
<< ":\n";
20872152
} else {
20882153
auto Iter2 = AllLabels.find(SectionAddr + Index);
20892154
if (Iter2 != AllLabels.end())
@@ -2260,7 +2325,7 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
22602325
} else if (!Disp) {
22612326
*TargetOS << TargetName;
22622327
} else if (BBAddrMapLabelAvailable) {
2263-
*TargetOS << BBAddrMapLabels[Target].front();
2328+
*TargetOS << BBAddrMapLabels[Target].front().BlockLabel;
22642329
} else if (LabelAvailable) {
22652330
*TargetOS << AllLabels[Target];
22662331
} else {
@@ -2276,7 +2341,8 @@ disassembleObject(ObjectFile &Obj, const ObjectFile &DbgObj,
22762341
}
22772342

22782343
} else if (BBAddrMapLabelAvailable) {
2279-
*TargetOS << " <" << BBAddrMapLabels[Target].front() << ">";
2344+
*TargetOS << " <" << BBAddrMapLabels[Target].front().BlockLabel
2345+
<< ">";
22802346
} else if (LabelAvailable) {
22812347
*TargetOS << " <" << AllLabels[Target] << ">";
22822348
}

0 commit comments

Comments
 (0)