[llvm] [ProfileData] Use std::array for ValueProfData (PR #96440)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 23 11:59:40 PDT 2024


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/96440

This patch uses std::array for ValueProfData.  Aside from reducing the
line count and code duplication, the use of std::array here makes it
easier to add a new type of value profiling without touching as many
places.

>From fbd49eb93612955ffe274fe9932b51904bb6b381 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sun, 23 Jun 2024 11:25:26 -0700
Subject: [PATCH] [ProfileData] Use std::array for ValueProfData

This patch uses std::array for ValueProfData.  Aside from reducing the
line count and code duplication, the use of std::array here makes it
easier to add a new type of value profiling without touching as many
places.
---
 llvm/include/llvm/ProfileData/InstrProf.h | 33 ++++++-----------------
 1 file changed, 8 insertions(+), 25 deletions(-)

diff --git a/llvm/include/llvm/ProfileData/InstrProf.h b/llvm/include/llvm/ProfileData/InstrProf.h
index 9bb1d8cbddc5e..7fa6d44990a14 100644
--- a/llvm/include/llvm/ProfileData/InstrProf.h
+++ b/llvm/include/llvm/ProfileData/InstrProf.h
@@ -935,11 +935,8 @@ struct InstrProfRecord {
   }
 
 private:
-  struct ValueProfData {
-    std::vector<InstrProfValueSiteRecord> IndirectCallSites;
-    std::vector<InstrProfValueSiteRecord> MemOPSizes;
-    std::vector<InstrProfValueSiteRecord> VTableTargets;
-  };
+  using ValueProfData = std::array<std::vector<InstrProfValueSiteRecord>,
+                                   IPVK_Last - IPVK_First + 1>;
   std::unique_ptr<ValueProfData> ValueData;
 
   MutableArrayRef<InstrProfValueSiteRecord>
@@ -956,32 +953,18 @@ struct InstrProfRecord {
   getValueSitesForKind(uint32_t ValueKind) const {
     if (!ValueData)
       return std::nullopt;
-    switch (ValueKind) {
-    case IPVK_IndirectCallTarget:
-      return ValueData->IndirectCallSites;
-    case IPVK_MemOPSize:
-      return ValueData->MemOPSizes;
-    case IPVK_VTableTarget:
-      return ValueData->VTableTargets;
-    default:
-      llvm_unreachable("Unknown value kind!");
-    }
+    assert(IPVK_First <= ValueKind && ValueKind <= IPVK_Last &&
+           "Unknown value kind!");
+    return (*ValueData)[ValueKind - IPVK_First];
   }
 
   std::vector<InstrProfValueSiteRecord> &
   getOrCreateValueSitesForKind(uint32_t ValueKind) {
     if (!ValueData)
       ValueData = std::make_unique<ValueProfData>();
-    switch (ValueKind) {
-    case IPVK_IndirectCallTarget:
-      return ValueData->IndirectCallSites;
-    case IPVK_MemOPSize:
-      return ValueData->MemOPSizes;
-    case IPVK_VTableTarget:
-      return ValueData->VTableTargets;
-    default:
-      llvm_unreachable("Unknown value kind!");
-    }
+    assert(IPVK_First <= ValueKind && ValueKind <= IPVK_Last &&
+           "Unknown value kind!");
+    return (*ValueData)[ValueKind - IPVK_First];
   }
 
   // Map indirect call target name hash to name string.



More information about the llvm-commits mailing list