[llvm] [Analysis] Migrate to a new version of getValueProfDataFromInst (PR #96420)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 28 16:09:03 PDT 2024


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

>From 08ef7e92135df578122404e666d8e6ddab929a15 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Mon, 17 Jun 2024 19:04:28 -0700
Subject: [PATCH] [Analysis] Migrate to a new version of
 getValueProfDataFromInst

While I am at is, this patch cleans up a few things:

- getPromotionCandidatesForInstruction now returns SmallVector instead
  of ArrayRef.

- getProfitablePromotionCandidates now takes
  ArrayRef<InstrProfValueData> instead of NumVals.

- The two changes above remove all uses of member variable
  ValueDataArray, so this patch removes it and updates the
  constructor of ICallPromotionAnalysis.
---
 .../Analysis/IndirectCallPromotionAnalysis.h  | 14 ++++-----
 .../IndirectCallPromotionAnalysis.cpp         | 30 +++++++------------
 2 files changed, 16 insertions(+), 28 deletions(-)

diff --git a/llvm/include/llvm/Analysis/IndirectCallPromotionAnalysis.h b/llvm/include/llvm/Analysis/IndirectCallPromotionAnalysis.h
index e0e8a7cda9369..d2b5d3d8ab894 100644
--- a/llvm/include/llvm/Analysis/IndirectCallPromotionAnalysis.h
+++ b/llvm/include/llvm/Analysis/IndirectCallPromotionAnalysis.h
@@ -23,9 +23,6 @@ class Instruction;
 // the indirect-call value profile metadata is available.
 class ICallPromotionAnalysis {
 private:
-  // Allocate space to read the profile annotation.
-  std::unique_ptr<InstrProfValueData[]> ValueDataArray;
-
   // Count is the call count for the direct-call target.
   // TotalCount is the total call count for the indirect-call callsite.
   // RemainingCount is the TotalCount minus promoted-direct-call count.
@@ -35,9 +32,10 @@ class ICallPromotionAnalysis {
 
   // Returns the number of profitable candidates to promote for the
   // current ValueDataArray and the given \p Inst.
-  uint32_t getProfitablePromotionCandidates(const Instruction *Inst,
-                                            uint32_t NumVals,
-                                            uint64_t TotalCount);
+  uint32_t
+  getProfitablePromotionCandidates(const Instruction *Inst,
+                                   ArrayRef<InstrProfValueData> ValueDataRef,
+                                   uint64_t TotalCount);
 
   // Noncopyable
   ICallPromotionAnalysis(const ICallPromotionAnalysis &other) = delete;
@@ -45,7 +43,7 @@ class ICallPromotionAnalysis {
   operator=(const ICallPromotionAnalysis &other) = delete;
 
 public:
-  ICallPromotionAnalysis();
+  ICallPromotionAnalysis() = default;
 
   /// Returns reference to array of InstrProfValueData for the given
   /// instruction \p I.
@@ -57,7 +55,7 @@ class ICallPromotionAnalysis {
   ///
   /// The returned array space is owned by this class, and overwritten on
   /// subsequent calls.
-  ArrayRef<InstrProfValueData> getPromotionCandidatesForInstruction(
+  SmallVector<InstrProfValueData, 4> getPromotionCandidatesForInstruction(
       const Instruction *I, uint64_t &TotalCount, uint32_t &NumCandidates);
 };
 
diff --git a/llvm/lib/Analysis/IndirectCallPromotionAnalysis.cpp b/llvm/lib/Analysis/IndirectCallPromotionAnalysis.cpp
index a71ab23a30902..694bb9e566ee5 100644
--- a/llvm/lib/Analysis/IndirectCallPromotionAnalysis.cpp
+++ b/llvm/lib/Analysis/IndirectCallPromotionAnalysis.cpp
@@ -49,10 +49,6 @@ cl::opt<unsigned> MaxNumVTableAnnotations(
     "icp-max-num-vtables", cl::init(6), cl::Hidden,
     cl::desc("Max number of vtables annotated for a vtable load instruction."));
 
-ICallPromotionAnalysis::ICallPromotionAnalysis() {
-  ValueDataArray = std::make_unique<InstrProfValueData[]>(MaxNumPromotions);
-}
-
 bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count,
                                                    uint64_t TotalCount,
                                                    uint64_t RemainingCount) {
@@ -64,15 +60,14 @@ bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count,
 // the count. Stop at the first target that is not promoted. Returns the
 // number of candidates deemed profitable.
 uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
-    const Instruction *Inst, uint32_t NumVals, uint64_t TotalCount) {
-  ArrayRef<InstrProfValueData> ValueDataRef(ValueDataArray.get(), NumVals);
-
+    const Instruction *Inst, ArrayRef<InstrProfValueData> ValueDataRef,
+    uint64_t TotalCount) {
   LLVM_DEBUG(dbgs() << " \nWork on callsite " << *Inst
-                    << " Num_targets: " << NumVals << "\n");
+                    << " Num_targets: " << ValueDataRef.size() << "\n");
 
   uint32_t I = 0;
   uint64_t RemainingCount = TotalCount;
-  for (; I < MaxNumPromotions && I < NumVals; I++) {
+  for (; I < MaxNumPromotions && I < ValueDataRef.size(); I++) {
     uint64_t Count = ValueDataRef[I].Count;
     assert(Count <= RemainingCount);
     LLVM_DEBUG(dbgs() << " Candidate " << I << " Count=" << Count
@@ -87,17 +82,12 @@ uint32_t ICallPromotionAnalysis::getProfitablePromotionCandidates(
   return I;
 }
 
-ArrayRef<InstrProfValueData>
+SmallVector<InstrProfValueData, 4>
 ICallPromotionAnalysis::getPromotionCandidatesForInstruction(
     const Instruction *I, uint64_t &TotalCount, uint32_t &NumCandidates) {
-  uint32_t NumVals;
-  auto Res = getValueProfDataFromInst(*I, IPVK_IndirectCallTarget,
-                                      MaxNumPromotions, NumVals, TotalCount);
-  if (!Res) {
-    NumCandidates = 0;
-    return ArrayRef<InstrProfValueData>();
-  }
-  ValueDataArray = std::move(Res);
-  NumCandidates = getProfitablePromotionCandidates(I, NumVals, TotalCount);
-  return ArrayRef<InstrProfValueData>(ValueDataArray.get(), NumVals);
+  auto VDs = getValueProfDataFromInst(*I, IPVK_IndirectCallTarget,
+                                      MaxNumPromotions, TotalCount);
+  NumCandidates =
+      VDs.empty() ? 0 : getProfitablePromotionCandidates(I, VDs, TotalCount);
+  return VDs;
 }



More information about the llvm-commits mailing list