Skip to content

Commit f179486

Browse files
authored
[AsmPrint] Correctly factor function entry count when dumping MBB frequencies (#67826)
The goal in #66818 was to capture function entry counts, but those are not the same as the frequency of the entry (machine) basic block. This fixes that, and adds explicit profiles to the test. We also increase the precision of `MachineBlockFrequencyInfo::getBlockFreqRelativeToEntryBlock` to double. Existing code uses it as float so should be unaffected.
1 parent 0c63122 commit f179486

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

llvm/include/llvm/CodeGen/MachineBlockFrequencyInfo.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ class MachineBlockFrequencyInfo : public MachineFunctionPass {
6565

6666
/// Compute the frequency of the block, relative to the entry block.
6767
/// This API assumes getEntryFreq() is non-zero.
68-
float getBlockFreqRelativeToEntryBlock(const MachineBasicBlock *MBB) const {
68+
double getBlockFreqRelativeToEntryBlock(const MachineBasicBlock *MBB) const {
6969
assert(getEntryFreq() != 0 && "getEntryFreq() should not return 0 here!");
70-
return getBlockFreq(MBB).getFrequency() * (1.0f / getEntryFreq());
70+
return static_cast<double>(getBlockFreq(MBB).getFrequency()) /
71+
static_cast<double>(getEntryFreq());
7172
}
7273

7374
std::optional<uint64_t>

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,18 +1929,32 @@ void AsmPrinter::emitFunctionBody() {
19291929

19301930
// Output MBB ids, function names, and frequencies if the flag to dump
19311931
// MBB profile information has been set
1932-
if (MBBProfileDumpFileOutput) {
1932+
if (MBBProfileDumpFileOutput && !MF->empty() &&
1933+
MF->getFunction().getEntryCount()) {
19331934
if (!MF->hasBBLabels())
19341935
MF->getContext().reportError(
19351936
SMLoc(),
19361937
"Unable to find BB labels for MBB profile dump. -mbb-profile-dump "
19371938
"must be called with -basic-block-sections=labels");
19381939
MachineBlockFrequencyInfo &MBFI =
19391940
getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI();
1941+
// The entry count and the entry basic block frequency aren't the same. We
1942+
// want to capture "absolute" frequencies, i.e. the frequency with which a
1943+
// MBB is executed when the program is executed. From there, we can derive
1944+
// Function-relative frequencies (divide by the value for the first MBB).
1945+
// We also have the information about frequency with which functions
1946+
// were called. This helps, for example, in a type of integration tests
1947+
// where we want to cross-validate the compiler's profile with a real
1948+
// profile.
1949+
// Using double precision because uint64 values used to encode mbb
1950+
// "frequencies" may be quite large.
1951+
const double EntryCount =
1952+
static_cast<double>(MF->getFunction().getEntryCount()->getCount());
19401953
for (const auto &MBB : *MF) {
1954+
const double MBBRelFreq = MBFI.getBlockFreqRelativeToEntryBlock(&MBB);
1955+
const double AbsMBBFreq = MBBRelFreq * EntryCount;
19411956
*MBBProfileDumpFileOutput.get()
1942-
<< MF->getName() << "," << MBB.getBBID() << ","
1943-
<< MBFI.getBlockFreq(&MBB).getFrequency() << "\n";
1957+
<< MF->getName() << "," << MBB.getBBID() << "," << AbsMBBFreq << "\n";
19441958
}
19451959
}
19461960
}

llvm/test/CodeGen/Generic/bb-profile-dump.ll

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,48 @@
77

88
; Check that given a simple case, we can return the default MBFI
99

10-
define i64 @f2(i64 %a, i64 %b) {
10+
define i64 @f2(i64 %a, i64 %b) !prof !1{
1111
%sum = add i64 %a, %b
1212
ret i64 %sum
1313
}
1414

15-
; CHECK: f2,0,8
15+
; CHECK: f2,0,1.000000e+03
1616

17-
define i64 @f1() {
17+
define i64 @f1() !prof !2{
1818
%sum = call i64 @f2(i64 2, i64 2)
1919
%isEqual = icmp eq i64 %sum, 4
20-
br i1 %isEqual, label %ifEqual, label %ifNotEqual
20+
br i1 %isEqual, label %ifEqual, label %ifNotEqual, !prof !3
2121
ifEqual:
2222
ret i64 0
2323
ifNotEqual:
2424
ret i64 %sum
2525
}
2626

27-
; CHECK-NEXT: f1,0,16
28-
; CHECK-NEXT: f1,1,8
29-
; CHECK-NEXT: f1,2,16
27+
; CHECK-NEXT: f1,0,1.000000e+01
28+
; CHECK-NEXT: f1,2,6.000000e+00
29+
; CHECK-NEXT: f1,1,4.000000e+00
30+
31+
define void @f3(i32 %iter) !prof !4 {
32+
entry:
33+
br label %loop
34+
loop:
35+
%i = phi i32 [0, %entry], [%i_next, %loop]
36+
%i_next = add i32 %i, 1
37+
%exit_cond = icmp slt i32 %i_next, %iter
38+
br i1 %exit_cond, label %loop, label %exit, !prof !5
39+
exit:
40+
ret void
41+
}
42+
43+
; CHECK-NEXT: f3,0,2.000000e+00
44+
; CHECK-NEXT: f3,1,2.002000e+03
45+
; CHECK-NEXT: f3,2,2.000000e+00
46+
47+
!1 = !{!"function_entry_count", i64 1000}
48+
!2 = !{!"function_entry_count", i64 10}
49+
!3 = !{!"branch_weights", i32 2, i32 3}
50+
!4 = !{!"function_entry_count", i64 2}
51+
!5 = !{!"branch_weights", i32 1000, i32 1}
3052

3153
; Check that if we pass -mbb-profile-dump but don't set -basic-block-sections,
3254
; we get an appropriate error message

0 commit comments

Comments
 (0)