[llvm] [SampleProfile] Introduce SampleProfileNameSet (NFC) (PR #208114)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 7 15:50:55 PDT 2026


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

This patch introduces a helper class SampleProfileNameSet to
encapsulate the construction of the name set and provide a contains
method.

I'm planning to speed up the membership queries into the name table.
With this patch, changes to the underlying data structure won't affect
use sites.

Assisted-by: Antigravity


>From 934d5d7bfa5e9d71c7cef9e05b7614efb5013a08 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Tue, 7 Jul 2026 15:34:26 -0700
Subject: [PATCH] [SampleProfile] Introduce SampleProfileNameSet (NFC)

This patch introduces a helper class SampleProfileNameSet to
encapsulate the construction of the name set and provide a contains
method.

I'm planning to speed up the membership queries into the name table.
With this patch, changes to the underlying data structure won't affect
use sites.

Assisted-by: Antigravity
---
 .../include/llvm/ProfileData/SampleProfReader.h | 17 +++++++++++++++++
 .../lib/Transforms/IPO/SampleProfileMatcher.cpp |  6 ++----
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm/ProfileData/SampleProfReader.h b/llvm/include/llvm/ProfileData/SampleProfReader.h
index 251e6a8f4843a..aaddd5a5693e0 100644
--- a/llvm/include/llvm/ProfileData/SampleProfReader.h
+++ b/llvm/include/llvm/ProfileData/SampleProfReader.h
@@ -1157,6 +1157,23 @@ class LLVM_ABI SampleProfileReaderGCC : public SampleProfileReader {
   static const uint32_t GCOVTagAFDOFunction = 0xac000000;
 };
 
+/// A helper class that wraps a local set of string names from NameTable.
+class SampleProfileNameSet {
+  const SampleProfileReader &Reader;
+  StringSet<> NamesInProfile;
+
+public:
+  explicit SampleProfileNameSet(const SampleProfileReader &R) : Reader(R) {
+    for (FunctionId Name : Reader.getNameTable())
+      NamesInProfile.insert(Name.stringRef());
+  }
+
+  /// Check if a canonical function name exists in the profile name table.
+  bool contains(StringRef CanonName) const {
+    return NamesInProfile.contains(CanonName);
+  }
+};
+
 } // end namespace sampleprof
 
 } // end namespace llvm
diff --git a/llvm/lib/Transforms/IPO/SampleProfileMatcher.cpp b/llvm/lib/Transforms/IPO/SampleProfileMatcher.cpp
index 6621684232812..d3aec9d7c363c 100644
--- a/llvm/lib/Transforms/IPO/SampleProfileMatcher.cpp
+++ b/llvm/lib/Transforms/IPO/SampleProfileMatcher.cpp
@@ -771,9 +771,7 @@ void SampleProfileMatcher::findFunctionsWithoutProfile() {
   // TODO: Support MD5 profile.
   if (FunctionSamples::UseMD5)
     return;
-  StringSet<> NamesInProfile;
-  for (FunctionId Name : Reader.getNameTable())
-    NamesInProfile.insert(Name.stringRef());
+  SampleProfileNameSet NamesInProfile(Reader);
 
   for (auto &F : M) {
     // Skip declarations, as even if the function can be matched, we have
@@ -789,7 +787,7 @@ void SampleProfileMatcher::findFunctionsWithoutProfile() {
     // For extended binary, functions fully inlined may not be loaded in the
     // top-level profile, so check the NameTable which has the all symbol names
     // in profile.
-    if (NamesInProfile.count(CanonFName))
+    if (NamesInProfile.contains(CanonFName))
       continue;
 
     // For extended binary, non-profiled function symbols are in the profile



More information about the llvm-commits mailing list