Skip to content

Commit 9ca8db3

Browse files
authored
[SHT_LLVM_BB_ADDR_MAP] Adds pretty printing of BFI and BPI for PGO Analysis Map in tools. (llvm#82292)
Primary change is to add a flag `--pretty-pgo-analysis-map` to llvm-readobj and llvm-objdump that prints block frequencies and branch probabilities in the same manner as BFI and BPI respectively. This can be helpful if you are manually inspecting the outputs from the tools. In order to print, I moved the `printBlockFreqImpl` function from Analysis to Support and renamed it to `printRelativeBlockFreq`.
1 parent 19181f2 commit 9ca8db3

File tree

16 files changed

+164
-71
lines changed

16 files changed

+164
-71
lines changed

llvm/docs/CommandGuide/llvm-objdump.rst

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,12 @@ OPTIONS
271271

272272
When printing a PC-relative global symbol reference, print it as an offset from the leading symbol.
273273

274-
When a bb-address-map section is present (i.e., the object file is built with ``-fbasic-block-sections=labels``), labels are retrieved from that section instead.
274+
When a bb-address-map section is present (i.e., the object file is built with
275+
``-fbasic-block-sections=labels``), labels are retrieved from that section
276+
instead. If a pgo-analysis-map is present alongside the bb-address-map, any
277+
available analyses are printed after the relevant block label. By default,
278+
any analysis with a special representation (i.e. BlockFrequency,
279+
BranchProbability, etc) are printed as raw hex values.
275280

276281
Only works with PowerPC objects or X86 linked images.
277282

@@ -291,6 +296,15 @@ OPTIONS
291296
cmp eax, dword ptr <g>
292297
jge <L0>
293298
299+
.. option:: --pretty-pgo-analysis-map
300+
301+
When using :option:`--symbolize-operands` with bb-address-map and
302+
pgo-analysis-map, print analyses using the same format as their analysis
303+
passes would. An example of pretty format would be printing block frequencies
304+
relative to the entry block, the same as BFI.
305+
306+
Only works when :option:`--symbolize-operands` is enabled.
307+
294308
.. option:: --triple=<string>
295309

296310
Target triple to disassemble for, see ``--version`` for available targets.

llvm/docs/CommandGuide/llvm-readobj.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ The following options are implemented only for the ELF file format.
164164
Display the contents of the basic block address map section(s), which contain the
165165
address of each function, along with the relative offset of each basic block.
166166

167+
When pgo analysis maps are present, all analyses are printed as their raw
168+
value.
169+
170+
.. option:: --pretty-pgo-analysis-map
171+
172+
When pgo analysis maps are present in the basic block address map section(s),
173+
analyses with special formats (i.e. BlockFrequency, BranchProbability, etc)
174+
are printed using the same format as their respective analysis pass.
175+
176+
Requires :option:`--bb-addr-map` to have an effect.
177+
167178
.. option:: --demangle, -C
168179

169180
Display demangled symbol names in the output.

llvm/include/llvm/Analysis/BlockFrequencyInfoImpl.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,6 @@ class BlockFrequencyInfoImplBase {
539539
}
540540
};
541541

542-
void printBlockFreqImpl(raw_ostream &OS, BlockFrequency EntryFreq,
543-
BlockFrequency Freq);
544-
545542
namespace bfi_detail {
546543

547544
template <class BlockT> struct TypeMap {};

llvm/include/llvm/Support/BlockFrequency.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
namespace llvm {
2121

22+
class raw_ostream;
2223
class BranchProbability;
2324

2425
// This class represents Block Frequency as a 64-bit value.
@@ -119,6 +120,9 @@ class BlockFrequency {
119120
}
120121
};
121122

123+
void printRelativeBlockFreq(raw_ostream &OS, BlockFrequency EntryFreq,
124+
BlockFrequency Freq);
125+
122126
} // namespace llvm
123127

124128
#endif

llvm/lib/Analysis/BlockFrequencyInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ void BlockFrequencyInfo::verifyMatch(BlockFrequencyInfo &Other) const {
284284
Printable llvm::printBlockFreq(const BlockFrequencyInfo &BFI,
285285
BlockFrequency Freq) {
286286
return Printable([&BFI, Freq](raw_ostream &OS) {
287-
printBlockFreqImpl(OS, BFI.getEntryFreq(), Freq);
287+
printRelativeBlockFreq(OS, BFI.getEntryFreq(), Freq);
288288
});
289289
}
290290

llvm/lib/Analysis/BlockFrequencyInfoImpl.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -634,21 +634,6 @@ BlockFrequencyInfoImplBase::getLoopName(const LoopData &Loop) const {
634634
return getBlockName(Loop.getHeader()) + (Loop.isIrreducible() ? "**" : "*");
635635
}
636636

637-
void llvm::printBlockFreqImpl(raw_ostream &OS, BlockFrequency EntryFreq,
638-
BlockFrequency Freq) {
639-
if (Freq == BlockFrequency(0)) {
640-
OS << "0";
641-
return;
642-
}
643-
if (EntryFreq == BlockFrequency(0)) {
644-
OS << "<invalid BFI>";
645-
return;
646-
}
647-
Scaled64 Block(Freq.getFrequency(), 0);
648-
Scaled64 Entry(EntryFreq.getFrequency(), 0);
649-
OS << Block / Entry;
650-
}
651-
652637
void IrreducibleGraph::addNodesInLoop(const BFIBase::LoopData &OuterLoop) {
653638
Start = OuterLoop.getHeader();
654639
Nodes.reserve(OuterLoop.Nodes.size());

llvm/lib/CodeGen/MachineBlockFrequencyInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ BlockFrequency MachineBlockFrequencyInfo::getEntryFreq() const {
280280
Printable llvm::printBlockFreq(const MachineBlockFrequencyInfo &MBFI,
281281
BlockFrequency Freq) {
282282
return Printable([&MBFI, Freq](raw_ostream &OS) {
283-
printBlockFreqImpl(OS, MBFI.getEntryFreq(), Freq);
283+
printRelativeBlockFreq(OS, MBFI.getEntryFreq(), Freq);
284284
});
285285
}
286286

llvm/lib/Support/BlockFrequency.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include "llvm/Support/BlockFrequency.h"
1414
#include "llvm/Support/BranchProbability.h"
1515
#include "llvm/Support/MathExtras.h"
16+
#include "llvm/Support/ScaledNumber.h"
17+
#include "llvm/Support/raw_ostream.h"
1618

1719
using namespace llvm;
1820

@@ -45,3 +47,18 @@ std::optional<BlockFrequency> BlockFrequency::mul(uint64_t Factor) const {
4547
return {};
4648
return BlockFrequency(ResultFrequency);
4749
}
50+
51+
void llvm::printRelativeBlockFreq(raw_ostream &OS, BlockFrequency EntryFreq,
52+
BlockFrequency Freq) {
53+
if (Freq == BlockFrequency(0)) {
54+
OS << "0";
55+
return;
56+
}
57+
if (EntryFreq == BlockFrequency(0)) {
58+
OS << "<invalid BFI>";
59+
return;
60+
}
61+
ScaledNumber<uint64_t> Block(Freq.getFrequency(), 0);
62+
ScaledNumber<uint64_t> Entry(EntryFreq.getFrequency(), 0);
63+
OS << Block / Entry;
64+
}

llvm/test/tools/llvm-objdump/X86/elf-pgoanalysismap.yaml

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ Symbols:
4747

4848
# RUN: yaml2obj %s --docnum=2 -o %t2
4949
# RUN: llvm-objdump %t2 -d --symbolize-operands --no-show-raw-insn --no-leading-addr | \
50-
# RUN: FileCheck %s --check-prefix=ENTRYCOUNT-BLOCKFREQ
50+
# RUN: FileCheck --match-full-lines --strict-whitespace %s --check-prefix=ENTRYCOUNT-BLOCKFREQ
51+
# RUN: llvm-objdump %t2 -d --symbolize-operands --pretty-pgo-analysis-map --no-show-raw-insn --no-leading-addr | \
52+
# RUN: FileCheck --match-full-lines --strict-whitespace %s --check-prefix=ENTRYCOUNT-BLOCKFREQ-PRETTY
5153

5254
--- !ELF
5355
FileHeader:
@@ -98,18 +100,28 @@ Symbols:
98100
Section: .text.foo
99101
Value: 0x0
100102

101-
# ENTRYCOUNT-BLOCKFREQ: <foo>:
102-
# ENTRYCOUNT-BLOCKFREQ: <BB3> (Entry count: 1000, Frequency: 1000):
103-
# ENTRYCOUNT-BLOCKFREQ: <BB1> (Frequency: 133):
104-
# ENTRYCOUNT-BLOCKFREQ: <BB2> (Frequency: 18):
105-
# ENTRYCOUNT-BLOCKFREQ: <BB5> (Frequency: 1000):
103+
# ENTRYCOUNT-BLOCKFREQ:<foo>:
104+
# ENTRYCOUNT-BLOCKFREQ:<BB3> (Entry count: 1000, Frequency: 1000):
105+
# ENTRYCOUNT-BLOCKFREQ:<BB1> (Frequency: 133):
106+
# ENTRYCOUNT-BLOCKFREQ:<BB2> (Frequency: 18):
107+
# ENTRYCOUNT-BLOCKFREQ:<BB5> (Frequency: 1000):
108+
109+
# ENTRYCOUNT-BLOCKFREQ-PRETTY:<foo>:
110+
# ENTRYCOUNT-BLOCKFREQ-PRETTY:<BB3> (Entry count: 1000, Frequency: 1.0):
111+
# ENTRYCOUNT-BLOCKFREQ-PRETTY:<BB1> (Frequency: 0.133):
112+
# ENTRYCOUNT-BLOCKFREQ-PRETTY:<BB2> (Frequency: 0.018):
113+
# ENTRYCOUNT-BLOCKFREQ-PRETTY:<BB5> (Frequency: 1.0):
106114

107115
## Check the case where we have entry points, block frequency, and branch
108116
## proabability information.
109117

110118
# RUN: yaml2obj %s --docnum=3 -o %t3
111119
# RUN: llvm-objdump %t3 -d --symbolize-operands --no-show-raw-insn --no-leading-addr | \
112-
# RUN: FileCheck %s --check-prefix=ENTRY-FREQ-PROB
120+
# RUN: FileCheck --match-full-lines --strict-whitespace %s --check-prefix=ENTRY-FREQ-PROB
121+
# RUN: llvm-objdump %t3 -d --symbolize-operands --pretty-pgo-analysis-map --no-show-raw-insn --no-leading-addr | \
122+
# RUN: FileCheck --match-full-lines --strict-whitespace %s --check-prefix=ENTRY-FREQ-PROB-PRETTY
123+
# RUN: llvm-objdump %t3 -d --pretty-pgo-analysis-map --no-show-raw-insn --no-leading-addr 2>&1 | \
124+
# RUN: FileCheck %s --check-prefix=MISSING-SYMBOLIZE-OPERANDS
113125

114126
--- !ELF
115127
FileHeader:
@@ -154,30 +166,38 @@ Sections:
154166
- BBFreq: 1000
155167
Successors:
156168
- ID: 1
157-
BrProb: 0x22222222
169+
BrProb: 0x10000000
158170
- ID: 2
159-
BrProb: 0x33333333
171+
BrProb: 0x15000000
160172
- ID: 3
161-
BrProb: 0xaaaaaaaa
173+
BrProb: 0x50000000
162174
- BBFreq: 133
163175
Successors:
164176
- ID: 2
165-
BrProb: 0x11111111
177+
BrProb: 0x10000000
166178
- ID: 3
167-
BrProb: 0xeeeeeeee
179+
BrProb: 0x70000000
168180
- BBFreq: 18
169181
Successors:
170182
- ID: 3
171-
BrProb: 0xffffffff
183+
BrProb: 0x80000000
172184
- BBFreq: 1000
173185
Successors: []
174186
Symbols:
175187
- Name: foo
176188
Section: .text.foo
177189
Value: 0x0
178190

179-
# ENTRY-FREQ-PROB: <foo>:
180-
# ENTRY-FREQ-PROB: <BB3> (Entry count: 1000, Frequency: 1000, Successors: BB1:22222222, BB2:33333333, BB3:aaaaaaaa):
181-
# ENTRY-FREQ-PROB: <BB1> (Frequency: 133, Successors: BB2:11111111, BB3:eeeeeeee):
182-
# ENTRY-FREQ-PROB: <BB2> (Frequency: 18, Successors: BB3:ffffffff):
183-
# ENTRY-FREQ-PROB: <BB5> (Frequency: 1000):
191+
# ENTRY-FREQ-PROB:<foo>:
192+
# ENTRY-FREQ-PROB:<BB3> (Entry count: 1000, Frequency: 1000, Successors: BB1:10000000, BB2:15000000, BB3:50000000):
193+
# ENTRY-FREQ-PROB:<BB1> (Frequency: 133, Successors: BB2:10000000, BB3:70000000):
194+
# ENTRY-FREQ-PROB:<BB2> (Frequency: 18, Successors: BB3:80000000):
195+
# ENTRY-FREQ-PROB:<BB5> (Frequency: 1000):
196+
197+
# ENTRY-FREQ-PROB-PRETTY:<foo>:
198+
# ENTRY-FREQ-PROB-PRETTY:<BB3> (Entry count: 1000, Frequency: 1.0, Successors: BB1:[0x10000000 / 0x80000000 = 12.50%], BB2:[0x15000000 / 0x80000000 = 16.41%], BB3:[0x50000000 / 0x80000000 = 62.50%]):
199+
# ENTRY-FREQ-PROB-PRETTY:<BB1> (Frequency: 0.133, Successors: BB2:[0x10000000 / 0x80000000 = 12.50%], BB3:[0x70000000 / 0x80000000 = 87.50%]):
200+
# ENTRY-FREQ-PROB-PRETTY:<BB2> (Frequency: 0.018, Successors: BB3:[0x80000000 / 0x80000000 = 100.00%]):
201+
# ENTRY-FREQ-PROB-PRETTY:<BB5> (Frequency: 1.0):
202+
203+
# MISSING-SYMBOLIZE-OPERANDS: warning: --symbolize-operands must be enabled for --pretty-pgo-analysis-map to have an effect

llvm/test/tools/llvm-readobj/ELF/bb-addr-map-pgo-analysis-map.test

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33

44
## Check 64-bit:
55
# RUN: yaml2obj %s -DBITS=64 -DADDR=0x999999999 -o %t1.x64.o
6-
# RUN: llvm-readobj %t1.x64.o --bb-addr-map 2>&1 | FileCheck %s -DADDR=0x999999999 -DFILE=%t1.x64.o --check-prefix=CHECK
6+
# RUN: llvm-readobj %t1.x64.o --bb-addr-map 2>&1 | FileCheck --match-full-lines %s -DADDR=0x999999999 -DFILE=%t1.x64.o --check-prefixes=CHECK,RAW
7+
# RUN: llvm-readobj %t1.x64.o --bb-addr-map --pretty-pgo-analysis-map 2>&1 | FileCheck --match-full-lines %s -DADDR=0x999999999 -DFILE=%t1.x64.o --check-prefixes=CHECK,PRETTY
78
# RUN: llvm-readelf %t1.x64.o --bb-addr-map | FileCheck %s --check-prefix=GNU
9+
# RUN: llvm-readobj %t1.x64.o --pretty-pgo-analysis-map 2>&1 | FileCheck %s --check-prefix=PRETTY-NO-BAM
810

911
## Check 32-bit:
1012
# RUN: yaml2obj %s -DBITS=32 -o %t1.x32.o
11-
# RUN: llvm-readobj %t1.x32.o --bb-addr-map 2>&1 | FileCheck -DADDR=0x11111 %s -DFILE=%t1.x32.o --check-prefix=CHECK
13+
# RUN: llvm-readobj %t1.x32.o --bb-addr-map 2>&1 | FileCheck --match-full-lines -DADDR=0x11111 %s -DFILE=%t1.x32.o --check-prefixes=CHECK,RAW
1214
# RUN: llvm-readelf %t1.x32.o --bb-addr-map | FileCheck %s --check-prefix=GNU
1315

1416
## Check that a malformed section can be handled.
1517
# RUN: yaml2obj %s -DBITS=32 -DSIZE=24 -o %t2.o
16-
# RUN: llvm-readobj %t2.o --bb-addr-map 2>&1 | FileCheck %s -DOFFSET=0x00000018 -DFILE=%t2.o --check-prefix=TRUNCATED
18+
# RUN: llvm-readobj %t2.o --bb-addr-map 2>&1 | FileCheck --match-full-lines %s -DOFFSET=0x00000018 -DFILE=%t2.o --check-prefix=TRUNCATED
1719

1820
## Check that missing features can be handled.
1921
# RUN: yaml2obj %s -DBITS=32 -DFEATURE=0x2 -o %t3.o
@@ -22,7 +24,7 @@
2224
# CHECK: BBAddrMap [
2325
# CHECK-NEXT: Function {
2426
# CHECK-NEXT: At: [[ADDR]]
25-
# CHECK-NEXT: warning: '[[FILE]]': could not identify function symbol for address ([[ADDR]]) in SHT_LLVM_BB_ADDR_MAP section with index 3
27+
# CHECK-NEXT: {{.*}}: warning: '[[FILE]]': could not identify function symbol for address ([[ADDR]]) in SHT_LLVM_BB_ADDR_MAP section with index 3
2628
# CHECK-NEXT: Name: <?>
2729
# CHECK-NEXT: BB Ranges [
2830
# CHECK-NEXT: {
@@ -55,16 +57,19 @@
5557
# CHECK-NEXT: FuncEntryCount: 100
5658
# CHECK-NEXT: PGO BB entries [
5759
# CHECK-NEXT: {
58-
# CHECK-NEXT: Frequency: 100
60+
# RAW-NEXT: Frequency: 100
61+
# PRETTY-NEXT: Frequency: 1.0
5962
# CHECK-NEXT: Successors [
6063
# CHECK-NEXT: {
6164
# CHECK-NEXT: ID: 2
62-
# CHECK-NEXT: Probability: 0xFFFFFFFF
65+
# RAW-NEXT: Probability: 0x80000000
66+
# PRETTY-NEXT: Probability: 0x80000000 / 0x80000000 = 100.00%
6367
# CHECK-NEXT: }
6468
# CHECK-NEXT: ]
6569
# CHECK-NEXT: }
6670
# CHECK-NEXT: {
67-
# CHECK-NEXT: Frequency: 100
71+
# RAW-NEXT: Frequency: 100
72+
# PRETTY-NEXT: Frequency: 1.0
6873
# CHECK-NEXT: Successors [
6974
# CHECK-NEXT: ]
7075
# CHECK-NEXT: }
@@ -95,7 +100,8 @@
95100
# CHECK-NEXT: FuncEntryCount: 8888
96101
# CHECK-NEXT: PGO BB entries [
97102
# CHECK-NEXT: {
98-
# CHECK-NEXT: Frequency: 9000
103+
# RAW-NEXT: Frequency: 9000
104+
# PRETTY-NEXT: Frequency: 1.0
99105
# CHECK-NEXT: }
100106
# CHECK-NEXT: ]
101107
# CHECK-NEXT: }
@@ -104,8 +110,10 @@
104110

105111
# GNU: GNUStyle::printBBAddrMaps not implemented
106112

113+
# PRETTY-NO-BAM: warning: --bb-addr-map must be enabled for --pretty-pgo-analysis-map to have an effect
114+
107115
# TRUNCATED: BBAddrMap [
108-
# TRUNCATED-NEXT: warning: '[[FILE]]': unable to dump SHT_LLVM_BB_ADDR_MAP section with index 3: unable to decode LEB128 at offset [[OFFSET]]: malformed uleb128, extends past end
116+
# TRUNCATED-NEXT: {{.*}}: warning: '[[FILE]]': unable to dump SHT_LLVM_BB_ADDR_MAP section with index 3: unable to decode LEB128 at offset [[OFFSET]]: malformed uleb128, extends past end
109117
# TRUNCATED-NEXT: ]
110118
## Check that the other valid section is properly dumped.
111119
# TRUNCATED-NEXT: BBAddrMap [
@@ -192,7 +200,7 @@ Sections:
192200
- BBFreq: 100
193201
Successors:
194202
- ID: 2
195-
BrProb: 0xFFFFFFFF
203+
BrProb: 0x80000000
196204
- BBFreq: 100
197205
Successors: []
198206
- FuncEntryCount: 8888

llvm/tools/llvm-objdump/ObjdumpOpts.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ def : Flag<["-"], "t">, Alias<syms>, HelpText<"Alias for --syms">;
210210
def symbolize_operands : Flag<["--"], "symbolize-operands">,
211211
HelpText<"Symbolize instruction operands when disassembling">;
212212

213+
def pretty_pgo_analysis_map : Flag<["--"], "pretty-pgo-analysis-map">,
214+
HelpText<"Display PGO analysis values with "
215+
"formatting rather than raw numbers">;
216+
213217
def dynamic_syms : Flag<["--"], "dynamic-syms">,
214218
HelpText<"Display the contents of the dynamic symbol table">;
215219
def : Flag<["-"], "T">, Alias<dynamic_syms>,

0 commit comments

Comments
 (0)