[llvm] [BOLT] Expose pseudo probe function checksum and GUID (PR #99389)

Amir Ayupov via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 18 16:27:09 PDT 2024


https://github.com/aaupov updated https://github.com/llvm/llvm-project/pull/99389

>From 1d16605a54cc625287b5091e706387559fce726f Mon Sep 17 00:00:00 2001
From: Amir Ayupov <aaupov at fb.com>
Date: Wed, 17 Jul 2024 14:17:49 -0700
Subject: [PATCH 1/5] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
 =?UTF-8?q?itial=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.4
---
 bolt/include/bolt/Core/BinaryFunction.h        |  9 +++++++++
 bolt/include/bolt/Profile/ProfileYAMLMapping.h |  2 ++
 bolt/lib/Profile/DataAggregator.cpp            |  1 +
 bolt/lib/Profile/YAMLProfileWriter.cpp         |  1 +
 bolt/lib/Rewrite/PseudoProbeRewriter.cpp       | 14 +++++++++++++-
 bolt/test/X86/pseudoprobe-decoding-inline.test | 13 +++++++++++++
 6 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/bolt/include/bolt/Core/BinaryFunction.h b/bolt/include/bolt/Core/BinaryFunction.h
index 7d0afee4d1b78..425617f4bd0ae 100644
--- a/bolt/include/bolt/Core/BinaryFunction.h
+++ b/bolt/include/bolt/Core/BinaryFunction.h
@@ -416,6 +416,10 @@ class BinaryFunction {
   /// different parameters by every pass.
   mutable uint64_t Hash{0};
 
+  /// Function hash assigned by the compiler and stored in pseudo probe
+  /// description of the source function.
+  uint64_t PseudoProbeDescHash{0};
+
   /// For PLT functions it contains a symbol associated with a function
   /// reference. It is nullptr for non-PLT functions.
   const MCSymbol *PLTSymbol{nullptr};
@@ -2256,6 +2260,11 @@ class BinaryFunction {
   /// Returns the last computed hash value of the function.
   size_t getHash() const { return Hash; }
 
+  /// Returns the function hash from pseudo-probe description of the function.
+  size_t getPseudoProbeDescHash() const { return PseudoProbeDescHash; }
+
+  void setPseudoProbeDescHash(uint64_t Hash) { PseudoProbeDescHash = Hash; }
+
   using OperandHashFuncTy =
       function_ref<typename std::string(const MCOperand &)>;
 
diff --git a/bolt/include/bolt/Profile/ProfileYAMLMapping.h b/bolt/include/bolt/Profile/ProfileYAMLMapping.h
index 9dd3920dbf094..1c752a990c099 100644
--- a/bolt/include/bolt/Profile/ProfileYAMLMapping.h
+++ b/bolt/include/bolt/Profile/ProfileYAMLMapping.h
@@ -151,6 +151,7 @@ struct BinaryFunctionProfile {
   llvm::yaml::Hex64 Hash{0};
   uint64_t ExecCount{0};
   std::vector<BinaryBasicBlockProfile> Blocks;
+  llvm::yaml::Hex64 PseudoProbeDescHash{0};
   bool Used{false};
 };
 } // end namespace bolt
@@ -164,6 +165,7 @@ template <> struct MappingTraits<bolt::BinaryFunctionProfile> {
     YamlIO.mapRequired("nblocks", BFP.NumBasicBlocks);
     YamlIO.mapOptional("blocks", BFP.Blocks,
                        std::vector<bolt::BinaryBasicBlockProfile>());
+    YamlIO.mapOptional("pseudo_probe_desc_hash", BFP.PseudoProbeDescHash);
   }
 };
 
diff --git a/bolt/lib/Profile/DataAggregator.cpp b/bolt/lib/Profile/DataAggregator.cpp
index ce6ec0a04ac16..94463980a738a 100644
--- a/bolt/lib/Profile/DataAggregator.cpp
+++ b/bolt/lib/Profile/DataAggregator.cpp
@@ -2345,6 +2345,7 @@ std::error_code DataAggregator::writeBATYAML(BinaryContext &BC,
       YamlBF.Hash = BAT->getBFHash(FuncAddress);
       YamlBF.ExecCount = BF->getKnownExecutionCount();
       YamlBF.NumBasicBlocks = BAT->getNumBasicBlocks(FuncAddress);
+      YamlBF.PseudoProbeDescHash = BF->getPseudoProbeDescHash();
       const BoltAddressTranslation::BBHashMapTy &BlockMap =
           BAT->getBBHashMap(FuncAddress);
       YamlBF.Blocks.resize(YamlBF.NumBasicBlocks);
diff --git a/bolt/lib/Profile/YAMLProfileWriter.cpp b/bolt/lib/Profile/YAMLProfileWriter.cpp
index 9adbfdc5ff089..e7a8239e6b20a 100644
--- a/bolt/lib/Profile/YAMLProfileWriter.cpp
+++ b/bolt/lib/Profile/YAMLProfileWriter.cpp
@@ -69,6 +69,7 @@ YAMLProfileWriter::convert(const BinaryFunction &BF, bool UseDFS,
   YamlBF.Hash = BF.getHash();
   YamlBF.NumBasicBlocks = BF.size();
   YamlBF.ExecCount = BF.getKnownExecutionCount();
+  YamlBF.PseudoProbeDescHash = BF.getPseudoProbeDescHash();
 
   BinaryFunction::BasicBlockOrderType Order;
   llvm::copy(UseDFS ? BF.dfs() : BF.getLayout().blocks(),
diff --git a/bolt/lib/Rewrite/PseudoProbeRewriter.cpp b/bolt/lib/Rewrite/PseudoProbeRewriter.cpp
index 51038dbead330..67219b658f527 100644
--- a/bolt/lib/Rewrite/PseudoProbeRewriter.cpp
+++ b/bolt/lib/Rewrite/PseudoProbeRewriter.cpp
@@ -78,11 +78,17 @@ class PseudoProbeRewriter final : public MetadataRewriter {
   PseudoProbeRewriter(BinaryContext &BC)
       : MetadataRewriter("pseudo-probe-rewriter", BC) {}
 
+  Error preCFGInitializer() override;
   Error postEmitFinalizer() override;
 };
 
-Error PseudoProbeRewriter::postEmitFinalizer() {
+Error PseudoProbeRewriter::preCFGInitializer() {
   parsePseudoProbe();
+
+  return Error::success();
+}
+
+Error PseudoProbeRewriter::postEmitFinalizer() {
   updatePseudoProbes();
 
   return Error::success();
@@ -138,6 +144,12 @@ void PseudoProbeRewriter::parsePseudoProbe() {
     ProbeDecoder.printGUID2FuncDescMap(outs());
     ProbeDecoder.printProbesForAllAddresses(outs());
   }
+
+  for (const auto &[GUID, FuncDesc] : ProbeDecoder.getGUID2FuncDescMap())
+    if (FuncStartAddrs.contains(GUID))
+      if (BinaryFunction *BF =
+              BC.getBinaryFunctionAtAddress(FuncStartAddrs[GUID]))
+        BF->setPseudoProbeDescHash(FuncDesc.FuncHash);
 }
 
 void PseudoProbeRewriter::updatePseudoProbes() {
diff --git a/bolt/test/X86/pseudoprobe-decoding-inline.test b/bolt/test/X86/pseudoprobe-decoding-inline.test
index 15e93b1630a66..8479ee29ee595 100644
--- a/bolt/test/X86/pseudoprobe-decoding-inline.test
+++ b/bolt/test/X86/pseudoprobe-decoding-inline.test
@@ -1,6 +1,19 @@
 # REQUIRES: system-linux
 # RUN: llvm-bolt  %S/../../../llvm/test/tools/llvm-profgen/Inputs/inline-cs-pseudoprobe.perfbin --print-pseudo-probes=all -o %t.bolt 2>&1 | FileCheck %s
 
+# PREAGG: B X:0 #foo# 1 0
+# PREAGG: B X:0 #bar# 1 0
+# PREAGG: B X:0 #main# 1 0
+# RUN: link_fdata %s %S/../../../llvm/test/tools/llvm-profgen/Inputs/inline-cs-pseudoprobe.perfbin %t.preagg PREAGG
+# RUN: perf2bolt %S/../../../llvm/test/tools/llvm-profgen/Inputs/inline-cs-pseudoprobe.perfbin -p %t.preagg --pa -w %t.yaml -o %t.fdata 
+# RUN: FileCheck --input-file %t.yaml %s --check-prefix CHECK-YAML
+# CHECK-YAML: name: bar
+# CHECK-YAML: pseudo_probe_desc_hash: 0x10E852DA94
+# CHECK-YAML: name: foo
+# CHECK-YAML: pseudo_probe_desc_hash: 0x200205A19C5B4
+# CHECK-YAML: name: main
+# CHECK-YAML: pseudo_probe_desc_hash: 0x10000FFFFFFFF
+
 CHECK: Report of decoding input pseudo probe binaries
 
 CHECK-NEXT: Pseudo Probe Desc:

>From 738ce57964817a30f841f075811f8e4dea17e8ef Mon Sep 17 00:00:00 2001
From: Amir Ayupov <aaupov at fb.com>
Date: Wed, 17 Jul 2024 16:23:07 -0700
Subject: [PATCH 2/5] size_t -> uint64_t

Created using spr 1.3.4
---
 bolt/include/bolt/Core/BinaryFunction.h        | 2 +-
 bolt/test/X86/pseudoprobe-decoding-inline.test | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/bolt/include/bolt/Core/BinaryFunction.h b/bolt/include/bolt/Core/BinaryFunction.h
index 425617f4bd0ae..7e2f0bebebe03 100644
--- a/bolt/include/bolt/Core/BinaryFunction.h
+++ b/bolt/include/bolt/Core/BinaryFunction.h
@@ -2261,7 +2261,7 @@ class BinaryFunction {
   size_t getHash() const { return Hash; }
 
   /// Returns the function hash from pseudo-probe description of the function.
-  size_t getPseudoProbeDescHash() const { return PseudoProbeDescHash; }
+  uint64_t getPseudoProbeDescHash() const { return PseudoProbeDescHash; }
 
   void setPseudoProbeDescHash(uint64_t Hash) { PseudoProbeDescHash = Hash; }
 
diff --git a/bolt/test/X86/pseudoprobe-decoding-inline.test b/bolt/test/X86/pseudoprobe-decoding-inline.test
index 8479ee29ee595..af9b96764908e 100644
--- a/bolt/test/X86/pseudoprobe-decoding-inline.test
+++ b/bolt/test/X86/pseudoprobe-decoding-inline.test
@@ -5,7 +5,7 @@
 # PREAGG: B X:0 #bar# 1 0
 # PREAGG: B X:0 #main# 1 0
 # RUN: link_fdata %s %S/../../../llvm/test/tools/llvm-profgen/Inputs/inline-cs-pseudoprobe.perfbin %t.preagg PREAGG
-# RUN: perf2bolt %S/../../../llvm/test/tools/llvm-profgen/Inputs/inline-cs-pseudoprobe.perfbin -p %t.preagg --pa -w %t.yaml -o %t.fdata 
+# RUN: perf2bolt %S/../../../llvm/test/tools/llvm-profgen/Inputs/inline-cs-pseudoprobe.perfbin -p %t.preagg --pa -w %t.yaml -o %t.fdata
 # RUN: FileCheck --input-file %t.yaml %s --check-prefix CHECK-YAML
 # CHECK-YAML: name: bar
 # CHECK-YAML: pseudo_probe_desc_hash: 0x10E852DA94

>From 563bd42b921bdf22d30d1360a2273e5c9c578839 Mon Sep 17 00:00:00 2001
From: Amir Ayupov <aaupov at fb.com>
Date: Wed, 17 Jul 2024 21:54:51 -0700
Subject: [PATCH 3/5] Expose PseudoProbeDecoder via BC and store function GUID
 in BF

Created using spr 1.3.4
---
 bolt/include/bolt/Core/BinaryContext.h   | 15 +++++++++++++++
 bolt/include/bolt/Core/BinaryFunction.h  | 12 ++++++------
 bolt/lib/Profile/DataAggregator.cpp      | 10 +++++++++-
 bolt/lib/Profile/YAMLProfileWriter.cpp   |  9 ++++++++-
 bolt/lib/Rewrite/PseudoProbeRewriter.cpp | 18 +++++++++++-------
 5 files changed, 49 insertions(+), 15 deletions(-)

diff --git a/bolt/include/bolt/Core/BinaryContext.h b/bolt/include/bolt/Core/BinaryContext.h
index 73932c4ca2fb3..ea924ebe1ab54 100644
--- a/bolt/include/bolt/Core/BinaryContext.h
+++ b/bolt/include/bolt/Core/BinaryContext.h
@@ -32,6 +32,7 @@
 #include "llvm/MC/MCContext.h"
 #include "llvm/MC/MCObjectFileInfo.h"
 #include "llvm/MC/MCObjectWriter.h"
+#include "llvm/MC/MCPseudoProbe.h"
 #include "llvm/MC/MCSectionELF.h"
 #include "llvm/MC/MCSectionMachO.h"
 #include "llvm/MC/MCStreamer.h"
@@ -246,6 +247,9 @@ class BinaryContext {
   /// DWP Context.
   std::shared_ptr<DWARFContext> DWPContext;
 
+  /// Decoded pseudo probes.
+  std::unique_ptr<MCPseudoProbeDecoder> PseudoProbeDecoder;
+
   /// A map of DWO Ids to CUs.
   using DWOIdToCUMapType = std::unordered_map<uint64_t, DWARFUnit *>;
   DWOIdToCUMapType DWOCUs;
@@ -377,6 +381,17 @@ class BinaryContext {
     RtLibrary = std::move(Lib);
   }
 
+  const MCPseudoProbeDecoder *getPseudoProbeDecoder() const {
+    return PseudoProbeDecoder.get();
+  }
+
+  MCPseudoProbeDecoder &
+  setPseudoProbeDecoder(std::unique_ptr<MCPseudoProbeDecoder> Decoder) {
+    assert(!PseudoProbeDecoder && "Cannot set pseudo probe decoder twice.");
+    PseudoProbeDecoder = std::move(Decoder);
+    return *PseudoProbeDecoder.get();
+  }
+
   /// Return BinaryFunction containing a given \p Address or nullptr if
   /// no registered function contains the \p Address.
   ///
diff --git a/bolt/include/bolt/Core/BinaryFunction.h b/bolt/include/bolt/Core/BinaryFunction.h
index 7e2f0bebebe03..4bebac33ced3f 100644
--- a/bolt/include/bolt/Core/BinaryFunction.h
+++ b/bolt/include/bolt/Core/BinaryFunction.h
@@ -416,9 +416,9 @@ class BinaryFunction {
   /// different parameters by every pass.
   mutable uint64_t Hash{0};
 
-  /// Function hash assigned by the compiler and stored in pseudo probe
-  /// description of the source function.
-  uint64_t PseudoProbeDescHash{0};
+  /// Function GUID assigned by the compiler to the source function.
+  /// If non-null, is a valid GUID in pseudo probe section.
+  uint64_t PseudoProbeGUID{0};
 
   /// For PLT functions it contains a symbol associated with a function
   /// reference. It is nullptr for non-PLT functions.
@@ -2260,10 +2260,10 @@ class BinaryFunction {
   /// Returns the last computed hash value of the function.
   size_t getHash() const { return Hash; }
 
-  /// Returns the function hash from pseudo-probe description of the function.
-  uint64_t getPseudoProbeDescHash() const { return PseudoProbeDescHash; }
+  /// Returns the function GUID from pseudo-probe description of the function.
+  uint64_t getPseudoProbeGUID() const { return PseudoProbeGUID; }
 
-  void setPseudoProbeDescHash(uint64_t Hash) { PseudoProbeDescHash = Hash; }
+  void setPseudoProbeGUID(uint64_t GUID) { PseudoProbeGUID = GUID; }
 
   using OperandHashFuncTy =
       function_ref<typename std::string(const MCOperand &)>;
diff --git a/bolt/lib/Profile/DataAggregator.cpp b/bolt/lib/Profile/DataAggregator.cpp
index 94463980a738a..ce18a6ae2a85c 100644
--- a/bolt/lib/Profile/DataAggregator.cpp
+++ b/bolt/lib/Profile/DataAggregator.cpp
@@ -2298,6 +2298,8 @@ std::error_code DataAggregator::writeBATYAML(BinaryContext &BC,
 
   yaml::bolt::BinaryProfile BP;
 
+  const MCPseudoProbeDecoder *PseudoProbeDecoder = BC.getPseudoProbeDecoder();
+
   // Fill out the header info.
   BP.Header.Version = 1;
   BP.Header.FileName = std::string(BC.getFilename());
@@ -2345,7 +2347,13 @@ std::error_code DataAggregator::writeBATYAML(BinaryContext &BC,
       YamlBF.Hash = BAT->getBFHash(FuncAddress);
       YamlBF.ExecCount = BF->getKnownExecutionCount();
       YamlBF.NumBasicBlocks = BAT->getNumBasicBlocks(FuncAddress);
-      YamlBF.PseudoProbeDescHash = BF->getPseudoProbeDescHash();
+      if (PseudoProbeDecoder) {
+        if (uint64_t GUID = BF->getPseudoProbeGUID()) {
+          const MCPseudoProbeFuncDesc *FuncDesc =
+              PseudoProbeDecoder->getFuncDescForGUID(GUID);
+          YamlBF.PseudoProbeDescHash = FuncDesc->FuncHash;
+        }
+      }
       const BoltAddressTranslation::BBHashMapTy &BlockMap =
           BAT->getBBHashMap(FuncAddress);
       YamlBF.Blocks.resize(YamlBF.NumBasicBlocks);
diff --git a/bolt/lib/Profile/YAMLProfileWriter.cpp b/bolt/lib/Profile/YAMLProfileWriter.cpp
index e7a8239e6b20a..2c5bc9cda8d79 100644
--- a/bolt/lib/Profile/YAMLProfileWriter.cpp
+++ b/bolt/lib/Profile/YAMLProfileWriter.cpp
@@ -57,6 +57,7 @@ YAMLProfileWriter::convert(const BinaryFunction &BF, bool UseDFS,
                            const BoltAddressTranslation *BAT) {
   yaml::bolt::BinaryFunctionProfile YamlBF;
   const BinaryContext &BC = BF.getBinaryContext();
+  const MCPseudoProbeDecoder *PseudoProbeDecoder = BC.getPseudoProbeDecoder();
 
   const uint16_t LBRProfile = BF.getProfileFlags() & BinaryFunction::PF_LBR;
 
@@ -69,7 +70,13 @@ YAMLProfileWriter::convert(const BinaryFunction &BF, bool UseDFS,
   YamlBF.Hash = BF.getHash();
   YamlBF.NumBasicBlocks = BF.size();
   YamlBF.ExecCount = BF.getKnownExecutionCount();
-  YamlBF.PseudoProbeDescHash = BF.getPseudoProbeDescHash();
+  if (PseudoProbeDecoder) {
+    if (uint64_t GUID = BF.getPseudoProbeGUID()) {
+      const MCPseudoProbeFuncDesc *FuncDesc =
+          PseudoProbeDecoder->getFuncDescForGUID(GUID);
+      YamlBF.PseudoProbeDescHash = FuncDesc->FuncHash;
+    }
+  }
 
   BinaryFunction::BasicBlockOrderType Order;
   llvm::copy(UseDFS ? BF.dfs() : BF.getLayout().blocks(),
diff --git a/bolt/lib/Rewrite/PseudoProbeRewriter.cpp b/bolt/lib/Rewrite/PseudoProbeRewriter.cpp
index 67219b658f527..9249861ba8e3a 100644
--- a/bolt/lib/Rewrite/PseudoProbeRewriter.cpp
+++ b/bolt/lib/Rewrite/PseudoProbeRewriter.cpp
@@ -72,11 +72,13 @@ class PseudoProbeRewriter final : public MetadataRewriter {
   void parsePseudoProbe();
 
   /// PseudoProbe decoder
-  MCPseudoProbeDecoder ProbeDecoder;
+  MCPseudoProbeDecoder &ProbeDecoder;
 
 public:
   PseudoProbeRewriter(BinaryContext &BC)
-      : MetadataRewriter("pseudo-probe-rewriter", BC) {}
+      : MetadataRewriter("pseudo-probe-rewriter", BC),
+        ProbeDecoder(BC.setPseudoProbeDecoder(
+            std::make_unique<MCPseudoProbeDecoder>())) {}
 
   Error preCFGInitializer() override;
   Error postEmitFinalizer() override;
@@ -145,11 +147,13 @@ void PseudoProbeRewriter::parsePseudoProbe() {
     ProbeDecoder.printProbesForAllAddresses(outs());
   }
 
-  for (const auto &[GUID, FuncDesc] : ProbeDecoder.getGUID2FuncDescMap())
-    if (FuncStartAddrs.contains(GUID))
-      if (BinaryFunction *BF =
-              BC.getBinaryFunctionAtAddress(FuncStartAddrs[GUID]))
-        BF->setPseudoProbeDescHash(FuncDesc.FuncHash);
+  for (const auto &[GUID, FuncDesc] : ProbeDecoder.getGUID2FuncDescMap()) {
+    if (!FuncStartAddrs.contains(GUID))
+      continue;
+    BinaryFunction *BF = BC.getBinaryFunctionAtAddress(FuncStartAddrs[GUID]);
+    assert(BF);
+    BF->setPseudoProbeGUID(GUID);
+  }
 }
 
 void PseudoProbeRewriter::updatePseudoProbes() {

>From 833ff8e63309dd9956d8da962de83c76b4501510 Mon Sep 17 00:00:00 2001
From: Amir Ayupov <aaupov at fb.com>
Date: Thu, 18 Jul 2024 12:15:33 -0700
Subject: [PATCH 4/5] Expose GUID

Created using spr 1.3.4
---
 bolt/include/bolt/Profile/ProfileYAMLMapping.h |  5 ++++-
 bolt/lib/Profile/DataAggregator.cpp            | 14 +++++++-------
 bolt/lib/Profile/YAMLProfileWriter.cpp         |  4 ++--
 bolt/lib/Rewrite/RewriteInstance.cpp           |  1 +
 bolt/test/X86/pseudoprobe-decoding-inline.test | 10 +++++++++-
 5 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/bolt/include/bolt/Profile/ProfileYAMLMapping.h b/bolt/include/bolt/Profile/ProfileYAMLMapping.h
index 1c752a990c099..c4e5f2b284a86 100644
--- a/bolt/include/bolt/Profile/ProfileYAMLMapping.h
+++ b/bolt/include/bolt/Profile/ProfileYAMLMapping.h
@@ -151,6 +151,7 @@ struct BinaryFunctionProfile {
   llvm::yaml::Hex64 Hash{0};
   uint64_t ExecCount{0};
   std::vector<BinaryBasicBlockProfile> Blocks;
+  llvm::yaml::Hex64 GUID{0};
   llvm::yaml::Hex64 PseudoProbeDescHash{0};
   bool Used{false};
 };
@@ -165,7 +166,9 @@ template <> struct MappingTraits<bolt::BinaryFunctionProfile> {
     YamlIO.mapRequired("nblocks", BFP.NumBasicBlocks);
     YamlIO.mapOptional("blocks", BFP.Blocks,
                        std::vector<bolt::BinaryBasicBlockProfile>());
-    YamlIO.mapOptional("pseudo_probe_desc_hash", BFP.PseudoProbeDescHash);
+    YamlIO.mapOptional("guid", BFP.GUID, (uint64_t)0);
+    YamlIO.mapOptional("pseudo_probe_desc_hash", BFP.PseudoProbeDescHash,
+                       (uint64_t)0);
   }
 };
 
diff --git a/bolt/lib/Profile/DataAggregator.cpp b/bolt/lib/Profile/DataAggregator.cpp
index ce18a6ae2a85c..beb03a952d658 100644
--- a/bolt/lib/Profile/DataAggregator.cpp
+++ b/bolt/lib/Profile/DataAggregator.cpp
@@ -2347,13 +2347,6 @@ std::error_code DataAggregator::writeBATYAML(BinaryContext &BC,
       YamlBF.Hash = BAT->getBFHash(FuncAddress);
       YamlBF.ExecCount = BF->getKnownExecutionCount();
       YamlBF.NumBasicBlocks = BAT->getNumBasicBlocks(FuncAddress);
-      if (PseudoProbeDecoder) {
-        if (uint64_t GUID = BF->getPseudoProbeGUID()) {
-          const MCPseudoProbeFuncDesc *FuncDesc =
-              PseudoProbeDecoder->getFuncDescForGUID(GUID);
-          YamlBF.PseudoProbeDescHash = FuncDesc->FuncHash;
-        }
-      }
       const BoltAddressTranslation::BBHashMapTy &BlockMap =
           BAT->getBBHashMap(FuncAddress);
       YamlBF.Blocks.resize(YamlBF.NumBasicBlocks);
@@ -2407,6 +2400,13 @@ std::error_code DataAggregator::writeBATYAML(BinaryContext &BC,
         const unsigned BlockIndex = BlockMap.getBBIndex(BI.To.Offset);
         YamlBF.Blocks[BlockIndex].ExecCount += BI.Branches;
       }
+      if (PseudoProbeDecoder) {
+        if ((YamlBF.GUID = BF->getPseudoProbeGUID())) {
+          const MCPseudoProbeFuncDesc *FuncDesc =
+              PseudoProbeDecoder->getFuncDescForGUID(YamlBF.GUID);
+          YamlBF.PseudoProbeDescHash = FuncDesc->FuncHash;
+        }
+      }
       // Drop blocks without a hash, won't be useful for stale matching.
       llvm::erase_if(YamlBF.Blocks,
                      [](const yaml::bolt::BinaryBasicBlockProfile &YamlBB) {
diff --git a/bolt/lib/Profile/YAMLProfileWriter.cpp b/bolt/lib/Profile/YAMLProfileWriter.cpp
index 2c5bc9cda8d79..38345da73df7c 100644
--- a/bolt/lib/Profile/YAMLProfileWriter.cpp
+++ b/bolt/lib/Profile/YAMLProfileWriter.cpp
@@ -71,9 +71,9 @@ YAMLProfileWriter::convert(const BinaryFunction &BF, bool UseDFS,
   YamlBF.NumBasicBlocks = BF.size();
   YamlBF.ExecCount = BF.getKnownExecutionCount();
   if (PseudoProbeDecoder) {
-    if (uint64_t GUID = BF.getPseudoProbeGUID()) {
+    if ((YamlBF.GUID = BF.getPseudoProbeGUID())) {
       const MCPseudoProbeFuncDesc *FuncDesc =
-          PseudoProbeDecoder->getFuncDescForGUID(GUID);
+          PseudoProbeDecoder->getFuncDescForGUID(YamlBF.GUID);
       YamlBF.PseudoProbeDescHash = FuncDesc->FuncHash;
     }
   }
diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index 32562ccb6b345..b69983b95fe45 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -669,6 +669,7 @@ Error RewriteInstance::run() {
         opts::ProfileFormat == opts::ProfileFormatKind::PF_YAML) {
       selectFunctionsToProcess();
       disassembleFunctions();
+      processMetadataPreCFG();
       buildFunctionsCFG();
     }
     processProfileData();
diff --git a/bolt/test/X86/pseudoprobe-decoding-inline.test b/bolt/test/X86/pseudoprobe-decoding-inline.test
index af9b96764908e..c32660c41a09c 100644
--- a/bolt/test/X86/pseudoprobe-decoding-inline.test
+++ b/bolt/test/X86/pseudoprobe-decoding-inline.test
@@ -1,17 +1,25 @@
 # REQUIRES: system-linux
-# RUN: llvm-bolt  %S/../../../llvm/test/tools/llvm-profgen/Inputs/inline-cs-pseudoprobe.perfbin --print-pseudo-probes=all -o %t.bolt 2>&1 | FileCheck %s
+# RUN: llvm-bolt  %S/../../../llvm/test/tools/llvm-profgen/Inputs/inline-cs-pseudoprobe.perfbin --print-pseudo-probes=all -o %t.bolt --lite=0 --enable-bat 2>&1 | FileCheck %s
 
 # PREAGG: B X:0 #foo# 1 0
 # PREAGG: B X:0 #bar# 1 0
 # PREAGG: B X:0 #main# 1 0
+## Check pseudo-probes in regular YAML profile (non-BOLTed binary)
 # RUN: link_fdata %s %S/../../../llvm/test/tools/llvm-profgen/Inputs/inline-cs-pseudoprobe.perfbin %t.preagg PREAGG
 # RUN: perf2bolt %S/../../../llvm/test/tools/llvm-profgen/Inputs/inline-cs-pseudoprobe.perfbin -p %t.preagg --pa -w %t.yaml -o %t.fdata
 # RUN: FileCheck --input-file %t.yaml %s --check-prefix CHECK-YAML
+## Check pseudo-probes in BAT YAML profile (BOLTed binary)
+# RUN: link_fdata %s %t.bolt %t.preagg2 PREAGG
+# RUN: perf2bolt %t.bolt -p %t.preagg2 --pa -w %t.yaml2 -o %t.fdata2
+# RUN: FileCheck --input-file %t.yaml2 %s --check-prefix CHECK-YAML
 # CHECK-YAML: name: bar
+# CHECK-YAML: guid: 0xE413754A191DB537
 # CHECK-YAML: pseudo_probe_desc_hash: 0x10E852DA94
 # CHECK-YAML: name: foo
+# CHECK-YAML: guid: 0x5CF8C24CDB18BDAC
 # CHECK-YAML: pseudo_probe_desc_hash: 0x200205A19C5B4
 # CHECK-YAML: name: main
+# CHECK-YAML: guid: 0xDB956436E78DD5FA
 # CHECK-YAML: pseudo_probe_desc_hash: 0x10000FFFFFFFF
 
 CHECK: Report of decoding input pseudo probe binaries

>From cb614ab5db588bdeaca675544df6ead3082f5540 Mon Sep 17 00:00:00 2001
From: Amir Ayupov <aaupov at fb.com>
Date: Thu, 18 Jul 2024 16:26:58 -0700
Subject: [PATCH 5/5] s/PseudoProbeGUID/GUID

Created using spr 1.3.4
---
 bolt/include/bolt/Core/BinaryFunction.h  | 11 +++++------
 bolt/lib/Profile/DataAggregator.cpp      |  2 +-
 bolt/lib/Profile/YAMLProfileWriter.cpp   |  2 +-
 bolt/lib/Rewrite/PseudoProbeRewriter.cpp |  2 +-
 4 files changed, 8 insertions(+), 9 deletions(-)

diff --git a/bolt/include/bolt/Core/BinaryFunction.h b/bolt/include/bolt/Core/BinaryFunction.h
index 4bebac33ced3f..da3fc433b7a3b 100644
--- a/bolt/include/bolt/Core/BinaryFunction.h
+++ b/bolt/include/bolt/Core/BinaryFunction.h
@@ -416,9 +416,8 @@ class BinaryFunction {
   /// different parameters by every pass.
   mutable uint64_t Hash{0};
 
-  /// Function GUID assigned by the compiler to the source function.
-  /// If non-null, is a valid GUID in pseudo probe section.
-  uint64_t PseudoProbeGUID{0};
+  /// Function GUID assigned externally.
+  uint64_t GUID{0};
 
   /// For PLT functions it contains a symbol associated with a function
   /// reference. It is nullptr for non-PLT functions.
@@ -2260,10 +2259,10 @@ class BinaryFunction {
   /// Returns the last computed hash value of the function.
   size_t getHash() const { return Hash; }
 
-  /// Returns the function GUID from pseudo-probe description of the function.
-  uint64_t getPseudoProbeGUID() const { return PseudoProbeGUID; }
+  /// Returns the function GUID.
+  uint64_t getGUID() const { return GUID; }
 
-  void setPseudoProbeGUID(uint64_t GUID) { PseudoProbeGUID = GUID; }
+  void setGUID(uint64_t Id) { GUID = Id; }
 
   using OperandHashFuncTy =
       function_ref<typename std::string(const MCOperand &)>;
diff --git a/bolt/lib/Profile/DataAggregator.cpp b/bolt/lib/Profile/DataAggregator.cpp
index beb03a952d658..fd0a526e31b79 100644
--- a/bolt/lib/Profile/DataAggregator.cpp
+++ b/bolt/lib/Profile/DataAggregator.cpp
@@ -2401,7 +2401,7 @@ std::error_code DataAggregator::writeBATYAML(BinaryContext &BC,
         YamlBF.Blocks[BlockIndex].ExecCount += BI.Branches;
       }
       if (PseudoProbeDecoder) {
-        if ((YamlBF.GUID = BF->getPseudoProbeGUID())) {
+        if ((YamlBF.GUID = BF->getGUID())) {
           const MCPseudoProbeFuncDesc *FuncDesc =
               PseudoProbeDecoder->getFuncDescForGUID(YamlBF.GUID);
           YamlBF.PseudoProbeDescHash = FuncDesc->FuncHash;
diff --git a/bolt/lib/Profile/YAMLProfileWriter.cpp b/bolt/lib/Profile/YAMLProfileWriter.cpp
index 38345da73df7c..82d70e540e5e1 100644
--- a/bolt/lib/Profile/YAMLProfileWriter.cpp
+++ b/bolt/lib/Profile/YAMLProfileWriter.cpp
@@ -71,7 +71,7 @@ YAMLProfileWriter::convert(const BinaryFunction &BF, bool UseDFS,
   YamlBF.NumBasicBlocks = BF.size();
   YamlBF.ExecCount = BF.getKnownExecutionCount();
   if (PseudoProbeDecoder) {
-    if ((YamlBF.GUID = BF.getPseudoProbeGUID())) {
+    if ((YamlBF.GUID = BF.getGUID())) {
       const MCPseudoProbeFuncDesc *FuncDesc =
           PseudoProbeDecoder->getFuncDescForGUID(YamlBF.GUID);
       YamlBF.PseudoProbeDescHash = FuncDesc->FuncHash;
diff --git a/bolt/lib/Rewrite/PseudoProbeRewriter.cpp b/bolt/lib/Rewrite/PseudoProbeRewriter.cpp
index 5d32ddcc37beb..3704a9ba452b9 100644
--- a/bolt/lib/Rewrite/PseudoProbeRewriter.cpp
+++ b/bolt/lib/Rewrite/PseudoProbeRewriter.cpp
@@ -157,7 +157,7 @@ void PseudoProbeRewriter::parsePseudoProbe() {
       continue;
     BinaryFunction *BF = BC.getBinaryFunctionAtAddress(FuncStartAddrs[GUID]);
     assert(BF);
-    BF->setPseudoProbeGUID(GUID);
+    BF->setGUID(GUID);
   }
 }
 



More information about the llvm-commits mailing list