[llvm] [SHT_LLVM_BB_ADDR_MAP] Add an option to skip emitting bb entries (PR #114447)

Lei Wang via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 13 11:35:22 PST 2024


https://github.com/wlei-llvm updated https://github.com/llvm/llvm-project/pull/114447

>From 81242ce998f90ac60c493ddb9f1231fdff5f1f22 Mon Sep 17 00:00:00 2001
From: wlei <wlei at fb.com>
Date: Tue, 29 Oct 2024 13:16:01 -0700
Subject: [PATCH 01/11] add an option to skip bb entries

---
 llvm/include/llvm/Object/ELFTypes.h           | 12 ++-
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp    | 52 ++++++----
 llvm/lib/Object/ELF.cpp                       | 48 +++++-----
 llvm/lib/ObjectYAML/ELFEmitter.cpp            |  2 +
 .../basic-block-address-map-pgo-features.ll   | 10 ++
 .../ELF/bb-addr-map-skip-bb-entries.test      | 96 +++++++++++++++++++
 .../ELF/bb-addr-map-pgo-analysis-map.yaml     |  5 +-
 llvm/unittests/Object/ELFObjectFileTest.cpp   | 12 +--
 llvm/unittests/Object/ELFTypesTest.cpp        | 24 ++---
 9 files changed, 196 insertions(+), 65 deletions(-)
 create mode 100644 llvm/test/tools/llvm-readobj/ELF/bb-addr-map-skip-bb-entries.test

diff --git a/llvm/include/llvm/Object/ELFTypes.h b/llvm/include/llvm/Object/ELFTypes.h
index 0f8c73f81cfa6d..ca05ab7e0b1fb7 100644
--- a/llvm/include/llvm/Object/ELFTypes.h
+++ b/llvm/include/llvm/Object/ELFTypes.h
@@ -830,6 +830,7 @@ struct BBAddrMap {
     bool BBFreq : 1;
     bool BrProb : 1;
     bool MultiBBRange : 1;
+    bool NoBBEntries : 1;
 
     bool hasPGOAnalysis() const { return FuncEntryCount || BBFreq || BrProb; }
 
@@ -840,7 +841,8 @@ struct BBAddrMap {
       return (static_cast<uint8_t>(FuncEntryCount) << 0) |
              (static_cast<uint8_t>(BBFreq) << 1) |
              (static_cast<uint8_t>(BrProb) << 2) |
-             (static_cast<uint8_t>(MultiBBRange) << 3);
+             (static_cast<uint8_t>(MultiBBRange) << 3) |
+             (static_cast<uint8_t>(NoBBEntries) << 4);
     }
 
     // Decodes from minimum bit width representation and validates no
@@ -848,7 +850,8 @@ struct BBAddrMap {
     static Expected<Features> decode(uint8_t Val) {
       Features Feat{
           static_cast<bool>(Val & (1 << 0)), static_cast<bool>(Val & (1 << 1)),
-          static_cast<bool>(Val & (1 << 2)), static_cast<bool>(Val & (1 << 3))};
+          static_cast<bool>(Val & (1 << 2)), static_cast<bool>(Val & (1 << 3)),
+          static_cast<bool>(Val & (1 << 4))};
       if (Feat.encode() != Val)
         return createStringError(
             std::error_code(), "invalid encoding for BBAddrMap::Features: 0x%x",
@@ -857,9 +860,10 @@ struct BBAddrMap {
     }
 
     bool operator==(const Features &Other) const {
-      return std::tie(FuncEntryCount, BBFreq, BrProb, MultiBBRange) ==
+      return std::tie(FuncEntryCount, BBFreq, BrProb, MultiBBRange,
+                      NoBBEntries) ==
              std::tie(Other.FuncEntryCount, Other.BBFreq, Other.BrProb,
-                      Other.MultiBBRange);
+                      Other.MultiBBRange, Other.NoBBEntries);
     }
   };
 
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 459ad15163ae5e..960c9e3252d1d9 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -161,6 +161,12 @@ static cl::bits<PGOMapFeaturesEnum> PgoAnalysisMapFeatures(
         "Enable extended information within the SHT_LLVM_BB_ADDR_MAP that is "
         "extracted from PGO related analysis."));
 
+static cl::opt<bool>
+    SkipEmitBBEntries("skip-emit-bb-entries",
+                      cl::desc("Skip emitting basic block entries in the "
+                               "SHT_LLVM_BB_ADDR_MAP section"),
+                      cl::Hidden, cl::init(false));
+
 static cl::opt<bool> EmitJumpTableSizesSection(
     "emit-jump-table-sizes-section",
     cl::desc("Emit a section containing jump table addresses and sizes"),
@@ -1392,8 +1398,14 @@ getBBAddrMapFeature(const MachineFunction &MF, int NumMBBSectionRanges) {
   bool BrProbEnabled =
       AllFeatures ||
       (!NoFeatures && PgoAnalysisMapFeatures.isSet(PGOMapFeaturesEnum::BrProb));
+
+  if ((BBFreqEnabled || BrProbEnabled) && SkipEmitBBEntries) {
+    MF.getFunction().getContext().emitError(
+        "BB entries info is required for BBFreq and BrProb "
+        "features");
+  }
   return {FuncEntryCountEnabled, BBFreqEnabled, BrProbEnabled,
-          MF.hasBBSections() && NumMBBSectionRanges > 1};
+          MF.hasBBSections() && NumMBBSectionRanges > 1, SkipEmitBBEntries};
 }
 
 void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
@@ -1450,24 +1462,28 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
       OutStreamer->emitULEB128IntValue(MBBSectionNumBlocks[MBB.getSectionID()]);
       PrevMBBEndSymbol = MBBSymbol;
     }
-    // TODO: Remove this check when version 1 is deprecated.
-    if (BBAddrMapVersion > 1) {
-      OutStreamer->AddComment("BB id");
-      // Emit the BB ID for this basic block.
-      // We only emit BaseID since CloneID is unset for
-      // -basic-block-adress-map.
-      // TODO: Emit the full BBID when labels and sections can be mixed
-      // together.
-      OutStreamer->emitULEB128IntValue(MBB.getBBID()->BaseID);
+
+    if (!Features.NoBBEntries) {
+      // TODO: Remove this check when version 1 is deprecated.
+      if (BBAddrMapVersion > 1) {
+        OutStreamer->AddComment("BB id");
+        // Emit the BB ID for this basic block.
+        // We only emit BaseID since CloneID is unset for
+        // -basic-block-adress-map.
+        // TODO: Emit the full BBID when labels and sections can be mixed
+        // together.
+        OutStreamer->emitULEB128IntValue(MBB.getBBID()->BaseID);
+      }
+      // Emit the basic block offset relative to the end of the previous block.
+      // This is zero unless the block is padded due to alignment.
+      emitLabelDifferenceAsULEB128(MBBSymbol, PrevMBBEndSymbol);
+      // Emit the basic block size. When BBs have alignments, their size cannot
+      // always be computed from their offsets.
+      emitLabelDifferenceAsULEB128(MBB.getEndSymbol(), MBBSymbol);
+      // Emit the Metadata.
+      OutStreamer->emitULEB128IntValue(getBBAddrMapMetadata(MBB));
     }
-    // Emit the basic block offset relative to the end of the previous block.
-    // This is zero unless the block is padded due to alignment.
-    emitLabelDifferenceAsULEB128(MBBSymbol, PrevMBBEndSymbol);
-    // Emit the basic block size. When BBs have alignments, their size cannot
-    // always be computed from their offsets.
-    emitLabelDifferenceAsULEB128(MBB.getEndSymbol(), MBBSymbol);
-    // Emit the Metadata.
-    OutStreamer->emitULEB128IntValue(getBBAddrMapMetadata(MBB));
+
     PrevMBBEndSymbol = MBB.getEndSymbol();
   }
 
diff --git a/llvm/lib/Object/ELF.cpp b/llvm/lib/Object/ELF.cpp
index 9a9e762d1e51d6..dafd1d4db267cf 100644
--- a/llvm/lib/Object/ELF.cpp
+++ b/llvm/lib/Object/ELF.cpp
@@ -824,6 +824,7 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
     uint32_t NumBBRanges = 1;
     typename ELFFile<ELFT>::uintX_t RangeBaseAddress = 0;
     std::vector<BBAddrMap::BBEntry> BBEntries;
+    uint32_t TotalNumBlocks = 0;
     if (FeatEnable.MultiBBRange) {
       NumBBRanges = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
       if (!Cur || ULEBSizeErr)
@@ -840,7 +841,6 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
       NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
     }
     std::vector<BBAddrMap::BBRangeEntry> BBRangeEntries;
-    uint32_t TotalNumBlocks = 0;
     for (uint32_t BBRangeIndex = 0; BBRangeIndex < NumBBRanges;
          ++BBRangeIndex) {
       uint32_t PrevBBEndOffset = 0;
@@ -851,29 +851,33 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
         RangeBaseAddress = *AddressOrErr;
         NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
       }
-      for (uint32_t BlockIndex = 0; !MetadataDecodeErr && !ULEBSizeErr && Cur &&
-                                    (BlockIndex < NumBlocksInBBRange);
-           ++BlockIndex) {
-        uint32_t ID = Version >= 2
-                          ? readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr)
-                          : BlockIndex;
-        uint32_t Offset = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
-        uint32_t Size = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
-        uint32_t MD = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
-        if (Version >= 1) {
-          // Offset is calculated relative to the end of the previous BB.
-          Offset += PrevBBEndOffset;
-          PrevBBEndOffset = Offset + Size;
-        }
-        Expected<BBAddrMap::BBEntry::Metadata> MetadataOrErr =
-            BBAddrMap::BBEntry::Metadata::decode(MD);
-        if (!MetadataOrErr) {
-          MetadataDecodeErr = MetadataOrErr.takeError();
-          break;
+
+      if (!FeatEnable.NoBBEntries) {
+
+        for (uint32_t BlockIndex = 0; !MetadataDecodeErr && !ULEBSizeErr &&
+                                      Cur && (BlockIndex < NumBlocksInBBRange);
+             ++BlockIndex) {
+          uint32_t ID = Version >= 2
+                            ? readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr)
+                            : BlockIndex;
+          uint32_t Offset = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
+          uint32_t Size = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
+          uint32_t MD = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
+          if (Version >= 1) {
+            // Offset is calculated relative to the end of the previous BB.
+            Offset += PrevBBEndOffset;
+            PrevBBEndOffset = Offset + Size;
+          }
+          Expected<BBAddrMap::BBEntry::Metadata> MetadataOrErr =
+              BBAddrMap::BBEntry::Metadata::decode(MD);
+          if (!MetadataOrErr) {
+            MetadataDecodeErr = MetadataOrErr.takeError();
+            break;
+          }
+          BBEntries.push_back({ID, Offset, Size, *MetadataOrErr});
         }
-        BBEntries.push_back({ID, Offset, Size, *MetadataOrErr});
+        TotalNumBlocks += BBEntries.size();
       }
-      TotalNumBlocks += BBEntries.size();
       BBRangeEntries.push_back({RangeBaseAddress, std::move(BBEntries)});
     }
     FunctionEntries.push_back({std::move(BBRangeEntries)});
diff --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp
index fc234581a45a70..13b3ed2cbfb419 100644
--- a/llvm/lib/ObjectYAML/ELFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp
@@ -1502,6 +1502,8 @@ void ELFState<ELFT>::writeSectionContent(
       // Write all BBEntries in this BBRange.
       if (!BBR.BBEntries)
         continue;
+      if (FeatureOrErr->NoBBEntries)
+        continue;
       for (const ELFYAML::BBAddrMapEntry::BBEntry &BBE : *BBR.BBEntries) {
         ++TotalNumBlocks;
         if (Section.Type == llvm::ELF::SHT_LLVM_BB_ADDR_MAP && E.Version > 1)
diff --git a/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll b/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll
index fca5aa046b03b9..a7d7a4d465a35b 100644
--- a/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll
+++ b/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll
@@ -11,6 +11,10 @@
 ; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=bb-freq | FileCheck %s --check-prefixes=CHECK,PGO-BBF,BBF-ONLY
 ; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=br-prob | FileCheck %s --check-prefixes=CHECK,PGO-BRP,BRP-ONLY
 
+; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=func-entry-count -skip-emit-bb-entries | FileCheck %s --check-prefixes=SKIP-BB-ENTRIES
+; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=bb-freq -skip-emit-bb-entries
+; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=br-prob -skip-emit-bb-entries
+
 ;; Verify that we emit an error if we try and specify values in addition to all/none
 ; RUN: not llc < %s -mtriple=x86_64 -basic-block-address-map -pgo-analysis-map=all,bb-freq
 ; RUN: not llc < %s -mtriple=x86_64 -basic-block-address-map -pgo-analysis-map=none,bb-freq
@@ -134,3 +138,9 @@ declare i32 @__gxx_personality_v0(...)
 ; PGO-BRP-NEXT:	.byte	5		# successor BB ID
 ; PGO-BRP-NEXT:	.ascii	"\200\200\200\200\b"	# successor branch probability
 
+
+; SKIP-BB-ENTRIES:	    .byte	17                              # feature
+; SKIP-BB-ENTRIES-NEXT:	.quad	.Lfunc_begin0                   # function address
+; SKIP-BB-ENTRIES-NEXT:	.byte	6                               # number of basic blocks
+; SKIP-BB-ENTRIES-NEXT:	.byte	100                             # function entry count
+; SKIP-BB-ENTRIES-NOT:  # BB id
diff --git a/llvm/test/tools/llvm-readobj/ELF/bb-addr-map-skip-bb-entries.test b/llvm/test/tools/llvm-readobj/ELF/bb-addr-map-skip-bb-entries.test
new file mode 100644
index 00000000000000..13dc15932ede92
--- /dev/null
+++ b/llvm/test/tools/llvm-readobj/ELF/bb-addr-map-skip-bb-entries.test
@@ -0,0 +1,96 @@
+## This test checks how llvm-readobj prints for skipped BB entries(-skip-emit-bb-entries) file.
+
+## Check 64-bit:
+# RUN: yaml2obj %s -DBITS=64 -o %t1.x64.o
+# RUN: llvm-readobj %t1.x64.o --bb-addr-map 2>&1 | FileCheck --match-full-lines %s  -DFILE=%t1.x64.o
+
+## Check 32-bit:
+# RUN: yaml2obj %s -DBITS=32 -o %t1.x32.o
+# RUN: llvm-readobj %t1.x32.o --bb-addr-map 2>&1 | FileCheck --match-full-lines  %s -DFILE=%t1.x32.o
+
+
+# CHECK:     BBAddrMap [
+# CHECK-NEXT:  Function {
+# CHECK-NEXT:    At: 0x11111
+# CHECK-NEXT:    Name: foo
+# CHECK-NEXT:    BB Ranges [
+# CHECK-NEXT:      {
+# CHECK-NEXT:        Base Address: 0x11111
+# CHECK-NEXT:        BB Entries [
+# CHECK-NEXT:        ]
+# CHECK-NEXT:      }
+# CHECK-NEXT:    ]
+# CHECK-NEXT:    PGO analyses {
+# CHECK-NEXT:      FuncEntryCount: 100
+# CHECK-NEXT:      PGO BB entries [
+# CHECK-NEXT:      ]
+# CHECK-NEXT:    }
+# CHECK-NEXT:  }
+# CHECK-NEXT:]
+# CHECK-NEXT:BBAddrMap [
+# CHECK-NEXT:  Function {
+# CHECK-NEXT:    At: 0x33333
+# CHECK-NEXT:    Name: bar
+# CHECK-NEXT:    BB Ranges [
+# CHECK-NEXT:      {
+# CHECK-NEXT:        Base Address: 0x33333
+# CHECK-NEXT:        BB Entries [
+# CHECK-NEXT:        ]
+# CHECK-NEXT:      }
+# CHECK-NEXT:    ]
+# CHECK-NEXT:    PGO analyses {
+# CHECK-NEXT:      FuncEntryCount: 89
+# CHECK-NEXT:      PGO BB entries [
+# CHECK-NEXT:      ]
+# CHECK-NEXT:    }
+# CHECK-NEXT:  }
+# CHECK-NEXT:]
+
+
+
+
+
+--- !ELF
+FileHeader:
+  Class: ELFCLASS[[BITS]]
+  Data:  ELFDATA2LSB
+  Type:  ET_EXEC
+Sections:
+  - Name:   .text
+    Type:   SHT_PROGBITS
+    Flags:  [SHF_ALLOC]
+  - Name:   .text.bar
+    Type:   SHT_PROGBITS
+    Flags:  [SHF_ALLOC]
+  - Name:   .llvm_bb_addr_map
+    Type:   SHT_LLVM_BB_ADDR_MAP
+    ShSize: [[SIZE=<none>]]
+    Link:   .text
+    Entries:
+      - Version: 2
+        Feature: 0x17
+        BBRanges:
+          - BaseAddress: 0x11111
+            BBEntries:
+    PGOAnalyses:
+      - FuncEntryCount: 100
+  - Name: '.llvm_bb_addr_map (1)'
+    Type: SHT_LLVM_BB_ADDR_MAP
+    Link: .text.bar
+    Entries:
+      - Version: 2
+        Feature: 0x17
+        BBRanges:
+          - BaseAddress: 0x33333
+            BBEntries:
+    PGOAnalyses:
+      - FuncEntryCount: 89
+Symbols:
+  - Name:    foo
+    Section: .text
+    Type:    STT_FUNC
+    Value:   0x11111
+  - Name:    bar
+    Section: .text.bar
+    Type:    STT_FUNC
+    Value:   0x33333
diff --git a/llvm/test/tools/yaml2obj/ELF/bb-addr-map-pgo-analysis-map.yaml b/llvm/test/tools/yaml2obj/ELF/bb-addr-map-pgo-analysis-map.yaml
index 4dfaf60be3c0ed..a4cb572e6d9932 100644
--- a/llvm/test/tools/yaml2obj/ELF/bb-addr-map-pgo-analysis-map.yaml
+++ b/llvm/test/tools/yaml2obj/ELF/bb-addr-map-pgo-analysis-map.yaml
@@ -66,7 +66,7 @@ Sections:
 
 ## Check that yaml2obj generates a warning when we use unsupported feature.
 # RUN: yaml2obj --docnum=2  %s 2>&1 | FileCheck %s --check-prefix=INVALID-FEATURE
-# INVALID-FEATURE: warning: invalid encoding for BBAddrMap::Features: 0xff
+# INVALID-FEATURE: warning: invalid encoding for BBAddrMap::Features: 0xf0
 
 --- !ELF
 FileHeader:
@@ -79,5 +79,4 @@ Sections:
     Entries:
       - Version: 2
 ##  Specify unsupported feature
-        Feature: 0xFF
-
+        Feature: 0xF0
diff --git a/llvm/unittests/Object/ELFObjectFileTest.cpp b/llvm/unittests/Object/ELFObjectFileTest.cpp
index c13dc0e3fab898..2a0921690914b4 100644
--- a/llvm/unittests/Object/ELFObjectFileTest.cpp
+++ b/llvm/unittests/Object/ELFObjectFileTest.cpp
@@ -1148,11 +1148,11 @@ TEST(ELFObjectFileTest, ReadPGOAnalysisMap) {
 
   BBAddrMap E1 = {
       {{0x11111, {{1, 0x0, 0x1, {false, true, false, false, false}}}}}};
-  PGOAnalysisMap P1 = {892, {}, {true, false, false, false}};
+  PGOAnalysisMap P1 = {892, {}, {true, false, false, false, false}};
   BBAddrMap E2 = {
       {{0x22222, {{2, 0x0, 0x2, {false, false, true, false, false}}}}}};
   PGOAnalysisMap P2 = {
-      {}, {{BlockFrequency(343), {}}}, {false, true, false, false}};
+      {}, {{BlockFrequency(343), {}}}, {false, true, false, false, false}};
   BBAddrMap E3 = {{{0x33333,
                     {{0, 0x0, 0x3, {false, true, true, false, false}},
                      {1, 0x3, 0x3, {false, false, true, false, false}},
@@ -1163,7 +1163,7 @@ TEST(ELFObjectFileTest, ReadPGOAnalysisMap) {
                           {2, BranchProbability::getRaw(0xeeee'eeee)}}},
                         {{}, {{2, BranchProbability::getRaw(0xffff'ffff)}}},
                         {{}, {}}},
-                       {false, false, true, false}};
+                       {false, false, true, false, false}};
   BBAddrMap E4 = {{{0x44444,
                     {{0, 0x0, 0x4, {false, false, false, true, true}},
                      {1, 0x4, 0x4, {false, false, false, false, false}},
@@ -1180,10 +1180,10 @@ TEST(ELFObjectFileTest, ReadPGOAnalysisMap) {
          {3, BranchProbability::getRaw(0xeeee'eeee)}}},
        {BlockFrequency(18), {{3, BranchProbability::getRaw(0xffff'ffff)}}},
        {BlockFrequency(1000), {}}},
-      {true, true, true, false}};
+      {true, true, true, false, false}};
   BBAddrMap E5 = {
       {{0x55555, {{2, 0x0, 0x2, {false, false, true, false, false}}}}}};
-  PGOAnalysisMap P5 = {{}, {}, {false, false, false, false}};
+  PGOAnalysisMap P5 = {{}, {}, {false, false, false, false, false}};
   BBAddrMap E6 = {
       {{0x66666,
         {{0, 0x0, 0x6, {false, true, true, false, false}},
@@ -1195,7 +1195,7 @@ TEST(ELFObjectFileTest, ReadPGOAnalysisMap) {
                           {2, BranchProbability::getRaw(0xcccc'cccc)}}},
                         {{}, {{2, BranchProbability::getRaw(0x8888'8888)}}},
                         {{}, {}}},
-                       {false, false, true, true}};
+                       {false, false, true, true, false}};
 
   std::vector<BBAddrMap> Section0BBAddrMaps = {E4, E5, E6};
   std::vector<BBAddrMap> Section1BBAddrMaps = {E3};
diff --git a/llvm/unittests/Object/ELFTypesTest.cpp b/llvm/unittests/Object/ELFTypesTest.cpp
index f04d45cf0983c7..13130dde80ef10 100644
--- a/llvm/unittests/Object/ELFTypesTest.cpp
+++ b/llvm/unittests/Object/ELFTypesTest.cpp
@@ -102,15 +102,15 @@ static_assert(
 
 TEST(ELFTypesTest, BBAddrMapFeaturesEncodingTest) {
   const std::array<BBAddrMap::Features, 9> Decoded = {
-      {{false, false, false, false},
-       {true, false, false, false},
-       {false, true, false, false},
-       {false, false, true, false},
-       {false, false, false, true},
-       {true, true, false, false},
-       {false, true, true, false},
-       {false, true, true, true},
-       {true, true, true, true}}};
+      {{false, false, false, false, false},
+       {true, false, false, false, false},
+       {false, true, false, false, false},
+       {false, false, true, false, false},
+       {false, false, false, true, false},
+       {true, true, false, false, false},
+       {false, true, true, false, false},
+       {false, true, true, true, false},
+       {true, true, true, true, false}}};
   const std::array<uint8_t, 9> Encoded = {
       {0b0000, 0b0001, 0b0010, 0b0100, 0b1000, 0b0011, 0b0110, 0b1110, 0b1111}};
   for (const auto &[Feat, EncodedVal] : llvm::zip(Decoded, Encoded))
@@ -125,9 +125,9 @@ TEST(ELFTypesTest, BBAddrMapFeaturesEncodingTest) {
 
 TEST(ELFTypesTest, BBAddrMapFeaturesInvalidEncodingTest) {
   const std::array<std::string, 2> Errors = {
-      "invalid encoding for BBAddrMap::Features: 0x10",
-      "invalid encoding for BBAddrMap::Features: 0xff"};
-  const std::array<uint8_t, 2> Values = {{0b10000, 0b1111'1111}};
+      "invalid encoding for BBAddrMap::Features: 0x20",
+      "invalid encoding for BBAddrMap::Features: 0xf0"};
+  const std::array<uint8_t, 2> Values = {{0b10'0000, 0b1111'0000}};
   for (const auto &[Val, Error] : llvm::zip(Values, Errors)) {
     EXPECT_THAT_ERROR(BBAddrMap::Features::decode(Val).takeError(),
                       FailedWithMessage(Error));

>From 6f8d1bde2834c124374a7c6a51ab7ff9960115b6 Mon Sep 17 00:00:00 2001
From: wlei <wlei at fb.com>
Date: Fri, 1 Nov 2024 11:18:09 -0700
Subject: [PATCH 02/11] keep one blank line in test

---
 .../tools/llvm-readobj/ELF/bb-addr-map-skip-bb-entries.test  | 5 -----
 1 file changed, 5 deletions(-)

diff --git a/llvm/test/tools/llvm-readobj/ELF/bb-addr-map-skip-bb-entries.test b/llvm/test/tools/llvm-readobj/ELF/bb-addr-map-skip-bb-entries.test
index 13dc15932ede92..cce0b35d14a4f0 100644
--- a/llvm/test/tools/llvm-readobj/ELF/bb-addr-map-skip-bb-entries.test
+++ b/llvm/test/tools/llvm-readobj/ELF/bb-addr-map-skip-bb-entries.test
@@ -8,7 +8,6 @@
 # RUN: yaml2obj %s -DBITS=32 -o %t1.x32.o
 # RUN: llvm-readobj %t1.x32.o --bb-addr-map 2>&1 | FileCheck --match-full-lines  %s -DFILE=%t1.x32.o
 
-
 # CHECK:     BBAddrMap [
 # CHECK-NEXT:  Function {
 # CHECK-NEXT:    At: 0x11111
@@ -46,10 +45,6 @@
 # CHECK-NEXT:  }
 # CHECK-NEXT:]
 
-
-
-
-
 --- !ELF
 FileHeader:
   Class: ELFCLASS[[BITS]]

>From aa0c8d4e91a2b7a818f876bf301db70efbf6d92e Mon Sep 17 00:00:00 2001
From: wlei <wlei at fb.com>
Date: Wed, 6 Nov 2024 12:38:36 -0800
Subject: [PATCH 03/11] try fix test on windows

---
 .../CodeGen/X86/basic-block-address-map-pgo-features.ll     | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll b/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll
index a7d7a4d465a35b..55d2ae1b0daca5 100644
--- a/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll
+++ b/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll
@@ -11,9 +11,9 @@
 ; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=bb-freq | FileCheck %s --check-prefixes=CHECK,PGO-BBF,BBF-ONLY
 ; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=br-prob | FileCheck %s --check-prefixes=CHECK,PGO-BRP,BRP-ONLY
 
-; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=func-entry-count -skip-emit-bb-entries | FileCheck %s --check-prefixes=SKIP-BB-ENTRIES
-; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=bb-freq -skip-emit-bb-entries
-; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=br-prob -skip-emit-bb-entries
+; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map  -skip-emit-bb-entries=true  -pgo-analysis-map=func-entry-count | FileCheck %s --check-prefixes=SKIP-BB-ENTRIES
+; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -skip-emit-bb-entries -pgo-analysis-map=bb-freq
+; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -skip-emit-bb-entries -pgo-analysis-map=br-prob
 
 ;; Verify that we emit an error if we try and specify values in addition to all/none
 ; RUN: not llc < %s -mtriple=x86_64 -basic-block-address-map -pgo-analysis-map=all,bb-freq

>From e41238a88c4939a41350f87b8f1993d02c659832 Mon Sep 17 00:00:00 2001
From: wlei <wlei at fb.com>
Date: Thu, 7 Nov 2024 15:17:50 -0800
Subject: [PATCH 04/11] debug windows test

---
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp                | 8 ++++++++
 .../CodeGen/X86/basic-block-address-map-pgo-features.ll   | 3 ++-
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index c3887f1ab1cbb4..0bec4cf47339f7 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -1423,6 +1423,8 @@ getBBAddrMapFeature(const MachineFunction &MF, int NumMBBSectionRanges) {
         "BB entries info is required for BBFreq and BrProb "
         "features");
   }
+  dbgs() << "getBBAddrMapFeature::SkipEmitBBEntries: " << SkipEmitBBEntries
+         << "\n";
   return {FuncEntryCountEnabled, BBFreqEnabled, BrProbEnabled,
           MF.hasBBSections() && NumMBBSectionRanges > 1, SkipEmitBBEntries};
 }
@@ -1442,6 +1444,12 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
   OutStreamer->AddComment("feature");
   auto Features = getBBAddrMapFeature(MF, MBBSectionRanges.size());
   OutStreamer->emitInt8(Features.encode());
+  if (Features.NoBBEntries)
+    dbgs() << "NoBBEntries feature is enabled\n";
+  else if (SkipEmitBBEntries)
+    dbgs()
+        << "--SkipEmitBBEntries is enabled but NoBBEntries is set to false\n";
+
   // Emit BB Information for each basic block in the function.
   if (Features.MultiBBRange) {
     OutStreamer->AddComment("number of basic block ranges");
diff --git a/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll b/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll
index 55d2ae1b0daca5..2ea9d0364ef5d0 100644
--- a/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll
+++ b/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll
@@ -11,7 +11,8 @@
 ; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=bb-freq | FileCheck %s --check-prefixes=CHECK,PGO-BBF,BBF-ONLY
 ; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=br-prob | FileCheck %s --check-prefixes=CHECK,PGO-BRP,BRP-ONLY
 
-; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map  -skip-emit-bb-entries=true  -pgo-analysis-map=func-entry-count | FileCheck %s --check-prefixes=SKIP-BB-ENTRIES
+; RUN: llc < %s -mtriple=x86_64  -basic-block-address-map  -skip-emit-bb-entries=true  -pgo-analysis-map=func-entry-count | FileCheck %s --check-prefixes=SKIP-BB-ENTRIES
+; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true  -basic-block-address-map  -skip-emit-bb-entries=true  -pgo-analysis-map=func-entry-count | FileCheck %s --check-prefixes=SKIP-BB-ENTRIES
 ; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -skip-emit-bb-entries -pgo-analysis-map=bb-freq
 ; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -skip-emit-bb-entries -pgo-analysis-map=br-prob
 

>From 51f24bf847f0e9e56c313cc736369c5d2a552670 Mon Sep 17 00:00:00 2001
From: wlei <wlei at fb.com>
Date: Thu, 7 Nov 2024 20:54:32 -0800
Subject: [PATCH 05/11] debug windows bitfield

---
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 0bec4cf47339f7..133f237140eea5 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -1425,8 +1425,15 @@ getBBAddrMapFeature(const MachineFunction &MF, int NumMBBSectionRanges) {
   }
   dbgs() << "getBBAddrMapFeature::SkipEmitBBEntries: " << SkipEmitBBEntries
          << "\n";
-  return {FuncEntryCountEnabled, BBFreqEnabled, BrProbEnabled,
-          MF.hasBBSections() && NumMBBSectionRanges > 1, SkipEmitBBEntries};
+  llvm::object::BBAddrMap::Features F1 = {true, false, false, false, true};
+  dbgs() << "F1.NoBBEntries: " << F1.NoBBEntries << "\n";
+  llvm::object::BBAddrMap::Features F2 =
+      llvm::object::BBAddrMap::Features{true, false, false, false, true};
+  dbgs() << "F2.NoBBEntries: " << F2.NoBBEntries << "\n";
+
+  return llvm::object::BBAddrMap::Features{
+      FuncEntryCountEnabled, BBFreqEnabled, BrProbEnabled,
+      MF.hasBBSections() && NumMBBSectionRanges > 1, SkipEmitBBEntries};
 }
 
 void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {

>From 0646a8cb83e0d17d9816392dd2f14e2aefa37ee6 Mon Sep 17 00:00:00 2001
From: wlei <wlei at fb.com>
Date: Fri, 8 Nov 2024 15:55:54 -0800
Subject: [PATCH 06/11] debug windows bitfield

---
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 57 ++++++++++++++++++++--
 1 file changed, 53 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 133f237140eea5..e3d8ad0da405c3 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -1394,6 +1394,44 @@ static uint32_t getBBAddrMapMetadata(const MachineBasicBlock &MBB) {
       .encode();
 }
 
+static llvm::object::BBAddrMap::Features
+debugGetFeature1(const MachineFunction &MF, int NumMBBSectionRanges) {
+  return llvm::object::BBAddrMap::Features{true, false, false, false, true};
+}
+
+static llvm::object::BBAddrMap::Features
+debugGetFeature2(const MachineFunction &MF, int NumMBBSectionRanges) {
+  return llvm::object::BBAddrMap::Features{true, false, false, false,
+                                           SkipEmitBBEntries};
+}
+
+static llvm::object::BBAddrMap::Features
+debugGetFeature3(const MachineFunction &MF, int NumMBBSectionRanges) {
+  return {true, false, false, MF.hasBBSections() && NumMBBSectionRanges > 1,
+          static_cast<bool>(SkipEmitBBEntries)};
+}
+
+static llvm::object::BBAddrMap::Features
+debugGetFeature4(const MachineFunction &MF, int NumMBBSectionRanges) {
+  llvm::object::BBAddrMap::Features F = llvm::object::BBAddrMap::Features{
+      true, false, false, false, static_cast<bool>(SkipEmitBBEntries)};
+  return F;
+}
+
+static llvm::object::BBAddrMap::Features
+debugGetFeature5(const MachineFunction &MF, int NumMBBSectionRanges) {
+  llvm::object::BBAddrMap::Features F;
+  F.NoBBEntries = true;
+  return F;
+}
+
+static llvm::object::BBAddrMap::Features
+debugGetFeature6(const MachineFunction &MF, int NumMBBSectionRanges) {
+  llvm::object::BBAddrMap::Features F;
+  F.NoBBEntries = SkipEmitBBEntries;
+  return F;
+}
+
 static llvm::object::BBAddrMap::Features
 getBBAddrMapFeature(const MachineFunction &MF, int NumMBBSectionRanges) {
   // Ensure that the user has not passed in additional options while also
@@ -1425,11 +1463,22 @@ getBBAddrMapFeature(const MachineFunction &MF, int NumMBBSectionRanges) {
   }
   dbgs() << "getBBAddrMapFeature::SkipEmitBBEntries: " << SkipEmitBBEntries
          << "\n";
-  llvm::object::BBAddrMap::Features F1 = {true, false, false, false, true};
+  llvm::object::BBAddrMap::Features F1 = llvm::object::BBAddrMap::Features{
+      FuncEntryCountEnabled, BBFreqEnabled, BrProbEnabled,
+      MF.hasBBSections() && NumMBBSectionRanges > 1, SkipEmitBBEntries};
   dbgs() << "F1.NoBBEntries: " << F1.NoBBEntries << "\n";
-  llvm::object::BBAddrMap::Features F2 =
-      llvm::object::BBAddrMap::Features{true, false, false, false, true};
-  dbgs() << "F2.NoBBEntries: " << F2.NoBBEntries << "\n";
+  auto DF1 = debugGetFeature1(MF, NumMBBSectionRanges);
+  dbgs() << "DF1.NoBBEntries: " << DF1.NoBBEntries << "\n";
+  auto DF2 = debugGetFeature2(MF, NumMBBSectionRanges);
+  dbgs() << "DF2.NoBBEntries: " << DF2.NoBBEntries << "\n";
+  auto DF3 = debugGetFeature3(MF, NumMBBSectionRanges);
+  dbgs() << "DF3.NoBBEntries: " << DF3.NoBBEntries << "\n";
+  auto DF4 = debugGetFeature4(MF, NumMBBSectionRanges);
+  dbgs() << "DF4.NoBBEntries: " << DF4.NoBBEntries << "\n";
+  auto DF5 = debugGetFeature5(MF, NumMBBSectionRanges);
+  dbgs() << "DF5.NoBBEntries: " << DF5.NoBBEntries << "\n";
+  auto DF6 = debugGetFeature6(MF, NumMBBSectionRanges);
+  dbgs() << "DF6.NoBBEntries: " << DF6.NoBBEntries << "\n";
 
   return llvm::object::BBAddrMap::Features{
       FuncEntryCountEnabled, BBFreqEnabled, BrProbEnabled,

>From 8ff44226b3ce47d939eff3379c96e6c4cf67affb Mon Sep 17 00:00:00 2001
From: wlei <wlei at fb.com>
Date: Sun, 10 Nov 2024 19:28:35 -0800
Subject: [PATCH 07/11] fix test on windows

---
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp    | 69 +------------------
 .../basic-block-address-map-pgo-features.ll   |  7 +-
 2 files changed, 6 insertions(+), 70 deletions(-)

diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index e3d8ad0da405c3..63255749733357 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -1394,44 +1394,6 @@ static uint32_t getBBAddrMapMetadata(const MachineBasicBlock &MBB) {
       .encode();
 }
 
-static llvm::object::BBAddrMap::Features
-debugGetFeature1(const MachineFunction &MF, int NumMBBSectionRanges) {
-  return llvm::object::BBAddrMap::Features{true, false, false, false, true};
-}
-
-static llvm::object::BBAddrMap::Features
-debugGetFeature2(const MachineFunction &MF, int NumMBBSectionRanges) {
-  return llvm::object::BBAddrMap::Features{true, false, false, false,
-                                           SkipEmitBBEntries};
-}
-
-static llvm::object::BBAddrMap::Features
-debugGetFeature3(const MachineFunction &MF, int NumMBBSectionRanges) {
-  return {true, false, false, MF.hasBBSections() && NumMBBSectionRanges > 1,
-          static_cast<bool>(SkipEmitBBEntries)};
-}
-
-static llvm::object::BBAddrMap::Features
-debugGetFeature4(const MachineFunction &MF, int NumMBBSectionRanges) {
-  llvm::object::BBAddrMap::Features F = llvm::object::BBAddrMap::Features{
-      true, false, false, false, static_cast<bool>(SkipEmitBBEntries)};
-  return F;
-}
-
-static llvm::object::BBAddrMap::Features
-debugGetFeature5(const MachineFunction &MF, int NumMBBSectionRanges) {
-  llvm::object::BBAddrMap::Features F;
-  F.NoBBEntries = true;
-  return F;
-}
-
-static llvm::object::BBAddrMap::Features
-debugGetFeature6(const MachineFunction &MF, int NumMBBSectionRanges) {
-  llvm::object::BBAddrMap::Features F;
-  F.NoBBEntries = SkipEmitBBEntries;
-  return F;
-}
-
 static llvm::object::BBAddrMap::Features
 getBBAddrMapFeature(const MachineFunction &MF, int NumMBBSectionRanges) {
   // Ensure that the user has not passed in additional options while also
@@ -1461,28 +1423,9 @@ getBBAddrMapFeature(const MachineFunction &MF, int NumMBBSectionRanges) {
         "BB entries info is required for BBFreq and BrProb "
         "features");
   }
-  dbgs() << "getBBAddrMapFeature::SkipEmitBBEntries: " << SkipEmitBBEntries
-         << "\n";
-  llvm::object::BBAddrMap::Features F1 = llvm::object::BBAddrMap::Features{
-      FuncEntryCountEnabled, BBFreqEnabled, BrProbEnabled,
-      MF.hasBBSections() && NumMBBSectionRanges > 1, SkipEmitBBEntries};
-  dbgs() << "F1.NoBBEntries: " << F1.NoBBEntries << "\n";
-  auto DF1 = debugGetFeature1(MF, NumMBBSectionRanges);
-  dbgs() << "DF1.NoBBEntries: " << DF1.NoBBEntries << "\n";
-  auto DF2 = debugGetFeature2(MF, NumMBBSectionRanges);
-  dbgs() << "DF2.NoBBEntries: " << DF2.NoBBEntries << "\n";
-  auto DF3 = debugGetFeature3(MF, NumMBBSectionRanges);
-  dbgs() << "DF3.NoBBEntries: " << DF3.NoBBEntries << "\n";
-  auto DF4 = debugGetFeature4(MF, NumMBBSectionRanges);
-  dbgs() << "DF4.NoBBEntries: " << DF4.NoBBEntries << "\n";
-  auto DF5 = debugGetFeature5(MF, NumMBBSectionRanges);
-  dbgs() << "DF5.NoBBEntries: " << DF5.NoBBEntries << "\n";
-  auto DF6 = debugGetFeature6(MF, NumMBBSectionRanges);
-  dbgs() << "DF6.NoBBEntries: " << DF6.NoBBEntries << "\n";
-
-  return llvm::object::BBAddrMap::Features{
-      FuncEntryCountEnabled, BBFreqEnabled, BrProbEnabled,
-      MF.hasBBSections() && NumMBBSectionRanges > 1, SkipEmitBBEntries};
+  return {FuncEntryCountEnabled, BBFreqEnabled, BrProbEnabled,
+          MF.hasBBSections() && NumMBBSectionRanges > 1,
+          static_cast<bool>(SkipEmitBBEntries)};
 }
 
 void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
@@ -1500,12 +1443,6 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
   OutStreamer->AddComment("feature");
   auto Features = getBBAddrMapFeature(MF, MBBSectionRanges.size());
   OutStreamer->emitInt8(Features.encode());
-  if (Features.NoBBEntries)
-    dbgs() << "NoBBEntries feature is enabled\n";
-  else if (SkipEmitBBEntries)
-    dbgs()
-        << "--SkipEmitBBEntries is enabled but NoBBEntries is set to false\n";
-
   // Emit BB Information for each basic block in the function.
   if (Features.MultiBBRange) {
     OutStreamer->AddComment("number of basic block ranges");
diff --git a/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll b/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll
index 2ea9d0364ef5d0..a7d7a4d465a35b 100644
--- a/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll
+++ b/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll
@@ -11,10 +11,9 @@
 ; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=bb-freq | FileCheck %s --check-prefixes=CHECK,PGO-BBF,BBF-ONLY
 ; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=br-prob | FileCheck %s --check-prefixes=CHECK,PGO-BRP,BRP-ONLY
 
-; RUN: llc < %s -mtriple=x86_64  -basic-block-address-map  -skip-emit-bb-entries=true  -pgo-analysis-map=func-entry-count | FileCheck %s --check-prefixes=SKIP-BB-ENTRIES
-; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true  -basic-block-address-map  -skip-emit-bb-entries=true  -pgo-analysis-map=func-entry-count | FileCheck %s --check-prefixes=SKIP-BB-ENTRIES
-; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -skip-emit-bb-entries -pgo-analysis-map=bb-freq
-; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -skip-emit-bb-entries -pgo-analysis-map=br-prob
+; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=func-entry-count -skip-emit-bb-entries | FileCheck %s --check-prefixes=SKIP-BB-ENTRIES
+; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=bb-freq -skip-emit-bb-entries
+; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=br-prob -skip-emit-bb-entries
 
 ;; Verify that we emit an error if we try and specify values in addition to all/none
 ; RUN: not llc < %s -mtriple=x86_64 -basic-block-address-map -pgo-analysis-map=all,bb-freq

>From c9de67540abfc44cbebb842a03f16b73ec6ceb12 Mon Sep 17 00:00:00 2001
From: wlei <wlei at fb.com>
Date: Tue, 12 Nov 2024 19:06:06 -0800
Subject: [PATCH 08/11] addressing comments

---
 llvm/include/llvm/Object/ELFTypes.h             |  8 ++++----
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp      | 17 +++++++++--------
 llvm/lib/Object/ELF.cpp                         |  3 +--
 llvm/lib/ObjectYAML/ELFEmitter.cpp              |  4 +---
 .../X86/basic-block-address-map-pgo-features.ll |  8 ++++----
 5 files changed, 19 insertions(+), 21 deletions(-)

diff --git a/llvm/include/llvm/Object/ELFTypes.h b/llvm/include/llvm/Object/ELFTypes.h
index ca05ab7e0b1fb7..87e4dbe4480910 100644
--- a/llvm/include/llvm/Object/ELFTypes.h
+++ b/llvm/include/llvm/Object/ELFTypes.h
@@ -830,7 +830,7 @@ struct BBAddrMap {
     bool BBFreq : 1;
     bool BrProb : 1;
     bool MultiBBRange : 1;
-    bool NoBBEntries : 1;
+    bool OmitBBEntries : 1;
 
     bool hasPGOAnalysis() const { return FuncEntryCount || BBFreq || BrProb; }
 
@@ -842,7 +842,7 @@ struct BBAddrMap {
              (static_cast<uint8_t>(BBFreq) << 1) |
              (static_cast<uint8_t>(BrProb) << 2) |
              (static_cast<uint8_t>(MultiBBRange) << 3) |
-             (static_cast<uint8_t>(NoBBEntries) << 4);
+             (static_cast<uint8_t>(OmitBBEntries) << 4);
     }
 
     // Decodes from minimum bit width representation and validates no
@@ -861,9 +861,9 @@ struct BBAddrMap {
 
     bool operator==(const Features &Other) const {
       return std::tie(FuncEntryCount, BBFreq, BrProb, MultiBBRange,
-                      NoBBEntries) ==
+                      OmitBBEntries) ==
              std::tie(Other.FuncEntryCount, Other.BBFreq, Other.BrProb,
-                      Other.MultiBBRange, Other.NoBBEntries);
+                      Other.MultiBBRange, Other.OmitBBEntries);
     }
   };
 
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 63255749733357..256384728eedab 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -161,11 +161,12 @@ static cl::bits<PGOMapFeaturesEnum> PgoAnalysisMapFeatures(
         "Enable extended information within the SHT_LLVM_BB_ADDR_MAP that is "
         "extracted from PGO related analysis."));
 
-static cl::opt<bool>
-    SkipEmitBBEntries("skip-emit-bb-entries",
-                      cl::desc("Skip emitting basic block entries in the "
-                               "SHT_LLVM_BB_ADDR_MAP section"),
-                      cl::Hidden, cl::init(false));
+static cl::opt<bool> BBAddrMapSkipEmitBBEntries(
+    "basic-block-address-map-skip-bb-entries",
+    cl::desc("Skip emitting basic block entries in the SHT_LLVM_BB_ADDR_MAP "
+             "section. It's used to save binary size when BB entries are "
+             "unnecessary for some PGOAnalysisMap features."),
+    cl::Hidden, cl::init(false));
 
 static cl::opt<bool> EmitJumpTableSizesSection(
     "emit-jump-table-sizes-section",
@@ -1418,14 +1419,14 @@ getBBAddrMapFeature(const MachineFunction &MF, int NumMBBSectionRanges) {
       AllFeatures ||
       (!NoFeatures && PgoAnalysisMapFeatures.isSet(PGOMapFeaturesEnum::BrProb));
 
-  if ((BBFreqEnabled || BrProbEnabled) && SkipEmitBBEntries) {
+  if ((BBFreqEnabled || BrProbEnabled) && BBAddrMapSkipEmitBBEntries) {
     MF.getFunction().getContext().emitError(
         "BB entries info is required for BBFreq and BrProb "
         "features");
   }
   return {FuncEntryCountEnabled, BBFreqEnabled, BrProbEnabled,
           MF.hasBBSections() && NumMBBSectionRanges > 1,
-          static_cast<bool>(SkipEmitBBEntries)};
+          static_cast<bool>(BBAddrMapSkipEmitBBEntries)};
 }
 
 void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
@@ -1483,7 +1484,7 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
       PrevMBBEndSymbol = MBBSymbol;
     }
 
-    if (!Features.NoBBEntries) {
+    if (!Features.OmitBBEntries) {
       // TODO: Remove this check when version 1 is deprecated.
       if (BBAddrMapVersion > 1) {
         OutStreamer->AddComment("BB id");
diff --git a/llvm/lib/Object/ELF.cpp b/llvm/lib/Object/ELF.cpp
index dafd1d4db267cf..c55b270c0c7a73 100644
--- a/llvm/lib/Object/ELF.cpp
+++ b/llvm/lib/Object/ELF.cpp
@@ -852,8 +852,7 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
         NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
       }
 
-      if (!FeatEnable.NoBBEntries) {
-
+      if (!FeatEnable.OmitBBEntries) {
         for (uint32_t BlockIndex = 0; !MetadataDecodeErr && !ULEBSizeErr &&
                                       Cur && (BlockIndex < NumBlocksInBBRange);
              ++BlockIndex) {
diff --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp
index 13b3ed2cbfb419..20ad6eddb60296 100644
--- a/llvm/lib/ObjectYAML/ELFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp
@@ -1500,9 +1500,7 @@ void ELFState<ELFT>::writeSectionContent(
           BBR.NumBlocks.value_or(BBR.BBEntries ? BBR.BBEntries->size() : 0);
       SHeader.sh_size += sizeof(uintX_t) + CBA.writeULEB128(NumBlocks);
       // Write all BBEntries in this BBRange.
-      if (!BBR.BBEntries)
-        continue;
-      if (FeatureOrErr->NoBBEntries)
+      if (!BBR.BBEntries || FeatureOrErr->OmitBBEntries)
         continue;
       for (const ELFYAML::BBAddrMapEntry::BBEntry &BBE : *BBR.BBEntries) {
         ++TotalNumBlocks;
diff --git a/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll b/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll
index a7d7a4d465a35b..de517bd68388ac 100644
--- a/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll
+++ b/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll
@@ -11,9 +11,9 @@
 ; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=bb-freq | FileCheck %s --check-prefixes=CHECK,PGO-BBF,BBF-ONLY
 ; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=br-prob | FileCheck %s --check-prefixes=CHECK,PGO-BRP,BRP-ONLY
 
-; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=func-entry-count -skip-emit-bb-entries | FileCheck %s --check-prefixes=SKIP-BB-ENTRIES
-; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=bb-freq -skip-emit-bb-entries
-; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=br-prob -skip-emit-bb-entries
+; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=func-entry-count -basic-block-address-map-skip-bb-entries | FileCheck %s --check-prefixes=SKIP-BB-ENTRIES
+; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=bb-freq -basic-block-address-map-skip-bb-entries
+; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=br-prob -basic-block-address-map-skip-bb-entries
 
 ;; Verify that we emit an error if we try and specify values in addition to all/none
 ; RUN: not llc < %s -mtriple=x86_64 -basic-block-address-map -pgo-analysis-map=all,bb-freq
@@ -139,7 +139,7 @@ declare i32 @__gxx_personality_v0(...)
 ; PGO-BRP-NEXT:	.ascii	"\200\200\200\200\b"	# successor branch probability
 
 
-; SKIP-BB-ENTRIES:	    .byte	17                              # feature
+; SKIP-BB-ENTRIES:      .byte	17                              # feature
 ; SKIP-BB-ENTRIES-NEXT:	.quad	.Lfunc_begin0                   # function address
 ; SKIP-BB-ENTRIES-NEXT:	.byte	6                               # number of basic blocks
 ; SKIP-BB-ENTRIES-NEXT:	.byte	100                             # function entry count

>From 339f7272bc0fefc4d6d67b662474d91d45864b43 Mon Sep 17 00:00:00 2001
From: wlei <wlei at fb.com>
Date: Tue, 12 Nov 2024 21:06:36 -0800
Subject: [PATCH 09/11] fix lint

---
 llvm/lib/Object/ELF.cpp | 35 +++++++++++++++--------------------
 1 file changed, 15 insertions(+), 20 deletions(-)

diff --git a/llvm/lib/Object/ELF.cpp b/llvm/lib/Object/ELF.cpp
index 281c17f32b3584..5d9e7abe283da1 100644
--- a/llvm/lib/Object/ELF.cpp
+++ b/llvm/lib/Object/ELF.cpp
@@ -823,11 +823,7 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
     uint32_t NumBlocksInBBRange = 0;
     uint32_t NumBBRanges = 1;
     typename ELFFile<ELFT>::uintX_t RangeBaseAddress = 0;
-<<<<<<< HEAD
-    std::vector<BBAddrMap::BBEntry> BBEntries;
     uint32_t TotalNumBlocks = 0;
-=======
->>>>>>> main
     if (FeatEnable.MultiBBRange) {
       NumBBRanges = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
       if (!Cur || ULEBSizeErr)
@@ -855,24 +851,23 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
         NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
       }
       std::vector<BBAddrMap::BBEntry> BBEntries;
-      if (!FeatEnable.OmitBBEntries) {      
-      for (uint32_t BlockIndex = 0; !MetadataDecodeErr && !ULEBSizeErr && Cur &&
-                                    (BlockIndex < NumBlocksInBBRange);
-           ++BlockIndex) {
-        uint32_t ID = Version >= 2
-                          ? readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr)
-                          : BlockIndex;
-        uint32_t Offset = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
-        uint32_t Size = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
-        uint32_t MD = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
-        if (Version >= 1) {
-          // Offset is calculated relative to the end of the previous BB.
-          Offset += PrevBBEndOffset;
-          PrevBBEndOffset = Offset + Size;
-        }
+      if (!FeatEnable.OmitBBEntries) {
+        for (uint32_t BlockIndex = 0; !MetadataDecodeErr && !ULEBSizeErr &&
+                                      Cur && (BlockIndex < NumBlocksInBBRange);
+             ++BlockIndex) {
+          uint32_t ID = Version >= 2
+                            ? readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr)
+                            : BlockIndex;
+          uint32_t Offset = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
+          uint32_t Size = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
+          uint32_t MD = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
+          if (Version >= 1) {
+            // Offset is calculated relative to the end of the previous BB.
+            Offset += PrevBBEndOffset;
+            PrevBBEndOffset = Offset + Size;
+          }
         }
         TotalNumBlocks += BBEntries.size();
-        }
       }
       BBRangeEntries.push_back({RangeBaseAddress, std::move(BBEntries)});
     }

>From 37c2c3dfecba6280b56e9cdd1415daf381dc94db Mon Sep 17 00:00:00 2001
From: wlei <wlei at fb.com>
Date: Wed, 13 Nov 2024 10:52:39 -0800
Subject: [PATCH 10/11] addressing comment, fix test

---
 llvm/lib/Object/ELF.cpp                                  | 9 ++++++++-
 .../CodeGen/X86/basic-block-address-map-pgo-features.ll  | 7 ++++---
 .../llvm-readobj/ELF/bb-addr-map-skip-bb-entries.test    | 2 +-
 3 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Object/ELF.cpp b/llvm/lib/Object/ELF.cpp
index 5d9e7abe283da1..d70657571dc840 100644
--- a/llvm/lib/Object/ELF.cpp
+++ b/llvm/lib/Object/ELF.cpp
@@ -823,7 +823,6 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
     uint32_t NumBlocksInBBRange = 0;
     uint32_t NumBBRanges = 1;
     typename ELFFile<ELFT>::uintX_t RangeBaseAddress = 0;
-    uint32_t TotalNumBlocks = 0;
     if (FeatEnable.MultiBBRange) {
       NumBBRanges = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
       if (!Cur || ULEBSizeErr)
@@ -839,6 +838,7 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
       RangeBaseAddress = *AddressOrErr;
       NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
     }
+    uint32_t TotalNumBlocks = 0;
     std::vector<BBAddrMap::BBRangeEntry> BBRangeEntries;
     for (uint32_t BBRangeIndex = 0; BBRangeIndex < NumBBRanges;
          ++BBRangeIndex) {
@@ -866,6 +866,13 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
             Offset += PrevBBEndOffset;
             PrevBBEndOffset = Offset + Size;
           }
+          Expected<BBAddrMap::BBEntry::Metadata> MetadataOrErr =
+              BBAddrMap::BBEntry::Metadata::decode(MD);
+          if (!MetadataOrErr) {
+            MetadataDecodeErr = MetadataOrErr.takeError();
+            break;
+          }
+          BBEntries.push_back({ID, Offset, Size, *MetadataOrErr});
         }
         TotalNumBlocks += BBEntries.size();
       }
diff --git a/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll b/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll
index de517bd68388ac..63779727ec72c6 100644
--- a/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll
+++ b/llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll
@@ -12,8 +12,8 @@
 ; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=br-prob | FileCheck %s --check-prefixes=CHECK,PGO-BRP,BRP-ONLY
 
 ; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=func-entry-count -basic-block-address-map-skip-bb-entries | FileCheck %s --check-prefixes=SKIP-BB-ENTRIES
-; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=bb-freq -basic-block-address-map-skip-bb-entries
-; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=br-prob -basic-block-address-map-skip-bb-entries
+; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=bb-freq -basic-block-address-map-skip-bb-entries 2>&1 | FileCheck %s --check-prefixes=SKIP-BB-ENTRIES-ERROR
+; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=br-prob -basic-block-address-map-skip-bb-entries 2>&1 | FileCheck %s --check-prefixes=SKIP-BB-ENTRIES-ERROR
 
 ;; Verify that we emit an error if we try and specify values in addition to all/none
 ; RUN: not llc < %s -mtriple=x86_64 -basic-block-address-map -pgo-analysis-map=all,bb-freq
@@ -138,9 +138,10 @@ declare i32 @__gxx_personality_v0(...)
 ; PGO-BRP-NEXT:	.byte	5		# successor BB ID
 ; PGO-BRP-NEXT:	.ascii	"\200\200\200\200\b"	# successor branch probability
 
-
 ; SKIP-BB-ENTRIES:      .byte	17                              # feature
 ; SKIP-BB-ENTRIES-NEXT:	.quad	.Lfunc_begin0                   # function address
 ; SKIP-BB-ENTRIES-NEXT:	.byte	6                               # number of basic blocks
 ; SKIP-BB-ENTRIES-NEXT:	.byte	100                             # function entry count
 ; SKIP-BB-ENTRIES-NOT:  # BB id
+
+; SKIP-BB-ENTRIES-ERROR: error: BB entries info is required for BBFreq and BrProb features
diff --git a/llvm/test/tools/llvm-readobj/ELF/bb-addr-map-skip-bb-entries.test b/llvm/test/tools/llvm-readobj/ELF/bb-addr-map-skip-bb-entries.test
index cce0b35d14a4f0..0ead746ef5e406 100644
--- a/llvm/test/tools/llvm-readobj/ELF/bb-addr-map-skip-bb-entries.test
+++ b/llvm/test/tools/llvm-readobj/ELF/bb-addr-map-skip-bb-entries.test
@@ -1,4 +1,4 @@
-## This test checks how llvm-readobj prints for skipped BB entries(-skip-emit-bb-entries) file.
+## This test checks how llvm-readobj prints for skipped BB entries(-basic-block-address-map-skip-emit-bb-entries) file.
 
 ## Check 64-bit:
 # RUN: yaml2obj %s -DBITS=64 -o %t1.x64.o

>From 8f2f4347795c8c6186bba9fcfec56c8785ce0643 Mon Sep 17 00:00:00 2001
From: wlei <wlei at fb.com>
Date: Wed, 13 Nov 2024 11:34:59 -0800
Subject: [PATCH 11/11] addressing comments

---
 llvm/lib/Object/ELF.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Object/ELF.cpp b/llvm/lib/Object/ELF.cpp
index d70657571dc840..b6d0699ee4fe08 100644
--- a/llvm/lib/Object/ELF.cpp
+++ b/llvm/lib/Object/ELF.cpp
@@ -838,8 +838,8 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
       RangeBaseAddress = *AddressOrErr;
       NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
     }
-    uint32_t TotalNumBlocks = 0;
     std::vector<BBAddrMap::BBRangeEntry> BBRangeEntries;
+    uint32_t TotalNumBlocks = 0;
     for (uint32_t BBRangeIndex = 0; BBRangeIndex < NumBBRanges;
          ++BBRangeIndex) {
       uint32_t PrevBBEndOffset = 0;



More information about the llvm-commits mailing list