[llvm] [BOLT]Fix profile quality reporting for small binaries (PR #130810)

via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 11 18:09:28 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-bolt

Author: ShatianWang (ShatianWang)

<details>
<summary>Changes</summary>

Fix a format issue in profile quality reporting introduced in [127954](https://github.com/llvm/llvm-project/pull/127954). 
The profile quality scores are computed on a subset of eligible functions and basic blocks. For instance, if a function contains a single block, then it is not eligible for the CF flow conservation analysis, meaning that the function will not affect the reported CFG flow conservation gap. 

Previously, when the set of functions or basic blocks for a specific profile quality score is empty, then nothing will be printed for this specific score following the "BOLT-INFO: profile quality metrics..." line. For small test binaries, it is often the case that the eligible sets are empty for all three scores. As a result, the next "BOLT-INFO" will not start a new line. This PR fixes the issue by reporting 0 profile quality gap for each quality metric if the corresponding eligible set is empty. 

Added test `bolt/test/X86/profile-quality-reporting-small-binary.s`.

---
Full diff: https://github.com/llvm/llvm-project/pull/130810.diff


2 Files Affected:

- (modified) bolt/lib/Passes/ProfileQualityStats.cpp (+10-3) 
- (added) bolt/test/X86/profile-quality-reporting-small-binary.s (+35) 


``````````diff
diff --git a/bolt/lib/Passes/ProfileQualityStats.cpp b/bolt/lib/Passes/ProfileQualityStats.cpp
index 332c78da8a1e3..61d67b4f9068e 100644
--- a/bolt/lib/Passes/ProfileQualityStats.cpp
+++ b/bolt/lib/Passes/ProfileQualityStats.cpp
@@ -157,8 +157,10 @@ void printCFGContinuityStats(raw_ostream &OS,
     FractionECUnreachables.push_back(FractionECUnreachable);
   }
 
-  if (FractionECUnreachables.empty())
+  if (FractionECUnreachables.empty()) {
+    OS << "function CFG discontinuity 0.00%; ";
     return;
+  }
 
   llvm::sort(FractionECUnreachables);
   const int Rank = int(FractionECUnreachables.size() *
@@ -251,8 +253,10 @@ void printCallGraphFlowConservationStats(
     }
   }
 
-  if (CallGraphGaps.empty())
+  if (CallGraphGaps.empty()) {
+    OS << "call graph flow conservation gap 0.00%; ";
     return;
+  }
 
   llvm::sort(CallGraphGaps);
   const int Rank =
@@ -340,8 +344,11 @@ void printCFGFlowConservationStats(raw_ostream &OS,
     }
   }
 
-  if (CFGGapsWeightedAvg.empty())
+  if (CFGGapsWeightedAvg.empty()) {
+    OS << "CFG flow conservation gap 0.00% (weighted) 0.00% (worst)\n";
     return;
+  }
+
   llvm::sort(CFGGapsWeightedAvg);
   const int RankWA = int(CFGGapsWeightedAvg.size() *
                          opts::PercentileForProfileQualityCheck / 100);
diff --git a/bolt/test/X86/profile-quality-reporting-small-binary.s b/bolt/test/X86/profile-quality-reporting-small-binary.s
new file mode 100644
index 0000000000000..603d5c3218bc3
--- /dev/null
+++ b/bolt/test/X86/profile-quality-reporting-small-binary.s
@@ -0,0 +1,35 @@
+## Test that BOLT-INFO is correctly formatted after profile quality reporting for
+## a small binary.
+
+# RUN: llvm-mc --filetype=obj --triple x86_64-unknown-unknown %s -o %t.o
+# RUN: link_fdata %s %t.o %t.fdata
+# RUN: llvm-strip --strip-unneeded %t.o
+# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q
+# RUN: llvm-bolt %t.exe -o %t.bolt --data=%t.fdata \
+# RUN:     2>&1 | FileCheck %s
+
+# CHECK: BOLT-INFO: profile quality metrics for the hottest 2 functions (reporting top 5% values): function CFG discontinuity 0.00%; call graph flow conservation gap 0.00%; CFG flow conservation gap 0.00% (weighted) 0.00% (worst)
+# CHECK-NEXT: BOLT-INFO:
+
+        .text
+        .globl  func
+        .type   func, @function
+func:
+        pushq   %rbp
+        ret
+LLfunc_end:
+        .size   func, LLfunc_end-func
+
+
+        .globl  main
+        .type   main, @function
+main:
+        pushq   %rbp
+        movq    %rsp, %rbp
+LLmain_func:
+        call    func
+# FDATA: 1 main #LLmain_func# 1 func 0 0 500
+        movl    $4, %edi
+        retq
+.Lmain_end:
+        .size   main, .Lmain_end-main

``````````

</details>


https://github.com/llvm/llvm-project/pull/130810


More information about the llvm-commits mailing list