[llvm] [CGData] Use StringRef instead of const std::string (PR #118749)

via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 4 22:56:38 PST 2024


https://github.com/abhishek-kaushik22 updated https://github.com/llvm/llvm-project/pull/118749

>From b8d790bab4419e9f61670e45ec9c33716169cbdf Mon Sep 17 00:00:00 2001
From: abhishek-kaushik22 <abhishek.kaushik at intel.com>
Date: Thu, 5 Dec 2024 12:17:18 +0530
Subject: [PATCH 1/2] [CGData] Use StringRef instead of const std::string

---
 llvm/include/llvm/CGData/StableFunctionMap.h | 14 +++++++-------
 llvm/lib/CGData/StableFunctionMap.cpp        |  4 ++--
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/llvm/include/llvm/CGData/StableFunctionMap.h b/llvm/include/llvm/CGData/StableFunctionMap.h
index 8881adf5889d0d..06e4efb5ac9648 100644
--- a/llvm/include/llvm/CGData/StableFunctionMap.h
+++ b/llvm/include/llvm/CGData/StableFunctionMap.h
@@ -31,16 +31,16 @@ struct StableFunction {
   /// The combined stable hash of the function.
   stable_hash Hash;
   /// The name of the function.
-  std::string FunctionName;
+  StringRef FunctionName;
   /// The name of the module the function is in.
-  std::string ModuleName;
+  StringRef ModuleName;
   /// The number of instructions.
   unsigned InstCount;
   /// A vector of pairs of IndexPair and operand hash which was skipped.
   IndexOperandHashVecType IndexOperandHashes;
 
-  StableFunction(stable_hash Hash, const std::string FunctionName,
-                 const std::string ModuleName, unsigned InstCount,
+  StableFunction(stable_hash Hash, StringRef FunctionName,
+                 StringRef ModuleName, unsigned InstCount,
                  IndexOperandHashVecType &&IndexOperandHashes)
       : Hash(Hash), FunctionName(FunctionName), ModuleName(ModuleName),
         InstCount(InstCount),
@@ -78,14 +78,14 @@ struct StableFunctionMap {
   const HashFuncsMapType &getFunctionMap() const { return HashToFuncs; }
 
   /// Get the NameToId vector for serialization.
-  const SmallVector<std::string> getNames() const { return IdToName; }
+  const SmallVector<StringRef> getNames() const { return IdToName; }
 
   /// Get an existing ID associated with the given name or create a new ID if it
   /// doesn't exist.
   unsigned getIdOrCreateForName(StringRef Name);
 
   /// Get the name associated with a given ID
-  std::optional<std::string> getNameForId(unsigned Id) const;
+  std::optional<StringRef> getNameForId(unsigned Id) const;
 
   /// Insert a `StableFunction` object into the function map. This method
   /// handles the uniquing of string names and create a `StableFunctionEntry`
@@ -124,7 +124,7 @@ struct StableFunctionMap {
   /// A map from a stable_hash to a vector of functions with that hash.
   HashFuncsMapType HashToFuncs;
   /// A vector of strings to hold names.
-  SmallVector<std::string> IdToName;
+  SmallVector<StringRef> IdToName;
   /// A map from StringRef (name) to an ID.
   StringMap<unsigned> NameToId;
   /// True if the function map is finalized with minimal content.
diff --git a/llvm/lib/CGData/StableFunctionMap.cpp b/llvm/lib/CGData/StableFunctionMap.cpp
index 4afe77d78a4fe5..78bad015e9bdf0 100644
--- a/llvm/lib/CGData/StableFunctionMap.cpp
+++ b/llvm/lib/CGData/StableFunctionMap.cpp
@@ -67,12 +67,12 @@ unsigned StableFunctionMap::getIdOrCreateForName(StringRef Name) {
     return It->second;
   unsigned Id = IdToName.size();
   assert(Id == NameToId.size() && "ID collision");
-  IdToName.emplace_back(Name.str());
+  IdToName.emplace_back(Name);
   NameToId[IdToName.back()] = Id;
   return Id;
 }
 
-std::optional<std::string> StableFunctionMap::getNameForId(unsigned Id) const {
+std::optional<StringRef> StableFunctionMap::getNameForId(unsigned Id) const {
   if (Id >= IdToName.size())
     return std::nullopt;
   return IdToName[Id];

>From 02d52babb3ba28e39dfad3dfb23b7706513c399f Mon Sep 17 00:00:00 2001
From: abhishek-kaushik22 <abhishek.kaushik at intel.com>
Date: Thu, 5 Dec 2024 12:26:14 +0530
Subject: [PATCH 2/2] Update StableFunctionMap.h

---
 llvm/include/llvm/CGData/StableFunctionMap.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/CGData/StableFunctionMap.h b/llvm/include/llvm/CGData/StableFunctionMap.h
index 06e4efb5ac9648..0635fcad62e360 100644
--- a/llvm/include/llvm/CGData/StableFunctionMap.h
+++ b/llvm/include/llvm/CGData/StableFunctionMap.h
@@ -39,8 +39,8 @@ struct StableFunction {
   /// A vector of pairs of IndexPair and operand hash which was skipped.
   IndexOperandHashVecType IndexOperandHashes;
 
-  StableFunction(stable_hash Hash, StringRef FunctionName,
-                 StringRef ModuleName, unsigned InstCount,
+  StableFunction(stable_hash Hash, StringRef FunctionName, StringRef ModuleName,
+                 unsigned InstCount,
                  IndexOperandHashVecType &&IndexOperandHashes)
       : Hash(Hash), FunctionName(FunctionName), ModuleName(ModuleName),
         InstCount(InstCount),



More information about the llvm-commits mailing list