[llvm] 35714e3 - [MLGO] Change MBB Profile Dump from using MBB numbers to MBB IDs

Aiden Grossman via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 14 00:04:24 PDT 2023


Author: Aiden Grossman
Date: 2023-04-14T07:04:07Z
New Revision: 35714e3a9c6ae6ec63845020babede1860a6d4de

URL: https://github.com/llvm/llvm-project/commit/35714e3a9c6ae6ec63845020babede1860a6d4de
DIFF: https://github.com/llvm/llvm-project/commit/35714e3a9c6ae6ec63845020babede1860a6d4de.diff

LOG: [MLGO] Change MBB Profile Dump from using MBB numbers to MBB IDs

Currenty, setting the -mbb-profile-dump dumps a CSV file with blocks
inside an individual function identified by their MBB numbers. This
patch changes the MBBs to be identified by their ID which is set at MBB
creation and not changed afterwards, making it inherently stable
throughout the backend. This alleviates concerns with the MBB IDs
changing between the profile dump and what ends up in the final object
file. The MBBs inside the SHT_LLVM_BB_ADDR_MAP sections are also
identified using their MBB ID rather than number, so if we want to match
them up we need to identify the MBBs here by number.

Reviewed By: mtrofin, rahmanl

Differential Revision: https://reviews.llvm.org/D147366

Added: 
    

Modified: 
    llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
    llvm/test/CodeGen/MLRegalloc/bb-profile-dump.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index a663c957dac02..10b6e52dc9649 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -133,7 +133,7 @@ static cl::opt<std::string> BasicBlockProfileDump(
     "mbb-profile-dump", cl::Hidden,
     cl::desc("Basic block profile dump for external cost modelling. If "
              "matching up BBs with afterwards, the compilation must be "
-             "performed with -fbasic-block-sections=labels. Enabling this "
+             "performed with -basic-block-sections=labels. Enabling this "
              "flag during in-process ThinLTO is not supported."));
 
 const char DWARFGroupName[] = "dwarf";
@@ -1924,14 +1924,19 @@ void AsmPrinter::emitFunctionBody() {
 
   OutStreamer->addBlankLine();
 
-  // Output MBB numbers, function names, and frequencies if the flag to dump
+  // Output MBB ids, function names, and frequencies if the flag to dump
   // MBB profile information has been set
   if (MBBProfileDumpFileOutput) {
+    if (!MF->hasBBLabels())
+      MF->getContext().reportError(
+          SMLoc(),
+          "Unable to find BB labels for MBB profile dump. -mbb-profile-dump "
+          "must be called with -basic-block-sections=labels");
     MachineBlockFrequencyInfo &MBFI =
         getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI();
     for (const auto &MBB : *MF) {
       *MBBProfileDumpFileOutput.get()
-          << MF->getName() << "," << MBB.getNumber() << ","
+          << MF->getName() << "," << MBB.getBBID() << ","
           << MBFI.getBlockFreqRelativeToEntryBlock(&MBB) << "\n";
     }
   }

diff  --git a/llvm/test/CodeGen/MLRegalloc/bb-profile-dump.ll b/llvm/test/CodeGen/MLRegalloc/bb-profile-dump.ll
index d876d73997482..34244e7500173 100644
--- a/llvm/test/CodeGen/MLRegalloc/bb-profile-dump.ll
+++ b/llvm/test/CodeGen/MLRegalloc/bb-profile-dump.ll
@@ -3,11 +3,13 @@
 ; Check that the basic block profile dump outputs data and in the correct
 ; format.
 ;
-; RUN: llc -mtriple=x86_64-linux-unknown -o /dev/null -mbb-profile-dump=- %s | FileCheck %s
+; RUN: llc -mtriple=x86_64-linux-unknown -o /dev/null -basic-block-sections=labels -mbb-profile-dump=- %s | FileCheck %s
 
 ; bb profile dump is not supported on NVPTX
 ; UNSUPPORTED: target=nvptx{{.*}}
 
+; Check that given a simple case, we can return the default MBFI
+
 define i64 @f2(i64 %a, i64 %b) {
     %sum = add i64 %a, %b
     ret i64 %sum
@@ -27,3 +29,11 @@ ifNotEqual:
 ; CHECK-NEXT: f1,0,1.000000e+00
 ; CHECK-NEXT: f1,1,5.000000e-01
 ; CHECK-NEXT: f1,2,1.000000e+00
+
+; Check that if we pass -mbb-profile-dump but don't set -basic-block-sections,
+; we get an appropriate error message
+
+; RUN: not llc -mtriple=x86_64-linux-unknown -o /dev/null -mbb-profile-dump=- %s 2>&1 | FileCheck --check-prefix=NO-SECTIONS %s
+
+; NO-SECTIONS: <unknown>:0: error: Unable to find BB labels for MBB profile dump. -mbb-profile-dump must be called with -basic-block-sections=labels
+


        


More information about the llvm-commits mailing list