[llvm] [memprof] Use llvm::function_ref instead of std::function (PR #116306)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 15 10:50:51 PST 2024


https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/116306

>From 77211c10d1d3caa4b36132c5c708f15e0afd4152 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Thu, 14 Nov 2024 16:35:00 -0800
Subject: [PATCH 1/4] [memprof] Use llvm::function_ref instead of std::function

We've seen bugs where we lost track of error states stored in the
functor because we passed the functor by value (that is,
std::function) as opposed to reference (llvm::function_ref).

This patch fixes a couple of places we pass functors by value.

While we are at it, this patch adds curly braces around a "for" loop
spanning multiple lines.
---
 llvm/include/llvm/ProfileData/MemProf.h  | 14 ++++++++------
 llvm/lib/ProfileData/InstrProfReader.cpp |  3 ++-
 2 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/llvm/include/llvm/ProfileData/MemProf.h b/llvm/include/llvm/ProfileData/MemProf.h
index bfd91407769bb3..ae262060718a7c 100644
--- a/llvm/include/llvm/ProfileData/MemProf.h
+++ b/llvm/include/llvm/ProfileData/MemProf.h
@@ -895,11 +895,12 @@ struct LinearFrameIdConverter {
 // call stack array in the profile.
 struct LinearCallStackIdConverter {
   const unsigned char *CallStackBase;
-  std::function<Frame(LinearFrameId)> FrameIdToFrame;
+  llvm::function_ref<Frame(LinearFrameId)> FrameIdToFrame;
 
   LinearCallStackIdConverter() = delete;
-  LinearCallStackIdConverter(const unsigned char *CallStackBase,
-                             std::function<Frame(LinearFrameId)> FrameIdToFrame)
+  LinearCallStackIdConverter(
+      const unsigned char *CallStackBase,
+      llvm::function_ref<Frame(LinearFrameId)> FrameIdToFrame)
       : CallStackBase(CallStackBase), FrameIdToFrame(FrameIdToFrame) {}
 
   std::vector<Frame> operator()(LinearCallStackId LinearCSId) {
@@ -966,13 +967,14 @@ struct CallerCalleePairExtractor {
   // The base address of the radix tree array.
   const unsigned char *CallStackBase;
   // A functor to convert a linear FrameId to a Frame.
-  std::function<Frame(LinearFrameId)> FrameIdToFrame;
+  llvm::function_ref<Frame(LinearFrameId)> FrameIdToFrame;
   // A map from caller GUIDs to lists of call sites in respective callers.
   DenseMap<uint64_t, SmallVector<CallEdgeTy, 0>> CallerCalleePairs;
 
   CallerCalleePairExtractor() = delete;
-  CallerCalleePairExtractor(const unsigned char *CallStackBase,
-                            std::function<Frame(LinearFrameId)> FrameIdToFrame)
+  CallerCalleePairExtractor(
+      const unsigned char *CallStackBase,
+      llvm::function_ref<Frame(LinearFrameId)> FrameIdToFrame)
       : CallStackBase(CallStackBase), FrameIdToFrame(FrameIdToFrame) {}
 
   void operator()(LinearCallStackId LinearCSId) {
diff --git a/llvm/lib/ProfileData/InstrProfReader.cpp b/llvm/lib/ProfileData/InstrProfReader.cpp
index 54a7dea59b1aea..5a2a3352c4b07d 100644
--- a/llvm/lib/ProfileData/InstrProfReader.cpp
+++ b/llvm/lib/ProfileData/InstrProfReader.cpp
@@ -1688,10 +1688,11 @@ IndexedMemProfReader::getMemProfCallerCalleePairs() const {
   // duplicates, we first collect them in the form of a bit vector before
   // processing them.
   for (const memprof::IndexedMemProfRecord &IndexedRecord :
-       MemProfRecordTable->data())
+       MemProfRecordTable->data()) {
     for (const memprof::IndexedAllocationInfo &IndexedAI :
          IndexedRecord.AllocSites)
       Worklist.set(IndexedAI.CSId);
+  }
 
   // Collect caller-callee pairs for each linear call stack ID in Worklist.
   for (unsigned CS : Worklist.set_bits())

>From af980d3d2f90d08ee8b7048957616d696d13bb08 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Thu, 14 Nov 2024 17:43:20 -0800
Subject: [PATCH 2/4] Trigger build


>From 246e862e7aa7b3f211095af1e8afcbf8941dda6a Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Fri, 15 Nov 2024 07:09:52 -0800
Subject: [PATCH 3/4] Trigger build


>From b294022827d5b70409926fe0ad728dbc6425d785 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Fri, 15 Nov 2024 09:10:47 -0800
Subject: [PATCH 4/4] Trigger build




More information about the llvm-commits mailing list