[llvm] Eliminate pointless copies (PR #95097)

Braden Helmer via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 12 15:47:17 PDT 2024


https://github.com/bradenhelmer updated https://github.com/llvm/llvm-project/pull/95097

>From 0785efda9f726978f9a2a6c278c9b37d3728b548 Mon Sep 17 00:00:00 2001
From: Braden Helmer <bradenhelmeraus at gmail.com>
Date: Tue, 11 Jun 2024 06:52:05 -0400
Subject: [PATCH 1/3] Eliminate pointless copies

---
 llvm/include/llvm/Object/ELFTypes.h                 | 4 ++--
 llvm/include/llvm/Passes/StandardInstrumentations.h | 6 +++---
 llvm/include/llvm/Support/Caching.h                 | 5 ++---
 3 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/llvm/include/llvm/Object/ELFTypes.h b/llvm/include/llvm/Object/ELFTypes.h
index 4ab23e4ea81b1..a4f3ad6348c10 100644
--- a/llvm/include/llvm/Object/ELFTypes.h
+++ b/llvm/include/llvm/Object/ELFTypes.h
@@ -717,14 +717,14 @@ template <class ELFT> class Elf_Note_Iterator_Impl {
     advanceNhdr(NhdrPos, NoteSize);
     return *this;
   }
-  bool operator==(Elf_Note_Iterator_Impl Other) const {
+  bool operator==(const Elf_Note_Iterator_Impl &Other) const {
     if (!Nhdr && Other.Err)
       (void)(bool)(*Other.Err);
     if (!Other.Nhdr && Err)
       (void)(bool)(*Err);
     return Nhdr == Other.Nhdr;
   }
-  bool operator!=(Elf_Note_Iterator_Impl Other) const {
+  bool operator!=(const Elf_Note_Iterator_Impl &Other) const {
     return !(*this == Other);
   }
   Elf_Note_Impl<ELFT> operator*() const {
diff --git a/llvm/include/llvm/Passes/StandardInstrumentations.h b/llvm/include/llvm/Passes/StandardInstrumentations.h
index 84d1b541171bf..b3caa689c84b4 100644
--- a/llvm/include/llvm/Passes/StandardInstrumentations.h
+++ b/llvm/include/llvm/Passes/StandardInstrumentations.h
@@ -382,13 +382,13 @@ class EmptyData {
 template <typename T>
 class FuncDataT : public OrderedChangedData<BlockDataT<T>> {
 public:
-  FuncDataT(std::string S) : EntryBlockName(S) {}
+  FuncDataT(StringRef S) : EntryBlockName(S) {}
 
   // Return the name of the entry block
-  std::string getEntryBlockName() const { return EntryBlockName; }
+  StringRef getEntryBlockName() const { return EntryBlockName; }
 
 protected:
-  std::string EntryBlockName;
+  StringRef EntryBlockName;
 };
 
 // The data saved for comparing IRs.
diff --git a/llvm/include/llvm/Support/Caching.h b/llvm/include/llvm/Support/Caching.h
index 4fa57cc92e51f..cdf2bc2ecee19 100644
--- a/llvm/include/llvm/Support/Caching.h
+++ b/llvm/include/llvm/Support/Caching.h
@@ -27,11 +27,10 @@ class MemoryBuffer;
 /// that can be done by deriving from this class and overriding the destructor.
 class CachedFileStream {
 public:
-  CachedFileStream(std::unique_ptr<raw_pwrite_stream> OS,
-                   std::string OSPath = "")
+  CachedFileStream(std::unique_ptr<raw_pwrite_stream> OS, StringRef OSPath = "")
       : OS(std::move(OS)), ObjectPathName(OSPath) {}
   std::unique_ptr<raw_pwrite_stream> OS;
-  std::string ObjectPathName;
+  StringRef ObjectPathName;
   virtual ~CachedFileStream() = default;
 };
 

>From 50669a0beee9e238bc32d7ce0a5765533c56104d Mon Sep 17 00:00:00 2001
From: Braden Helmer <bradenhelmeraus at gmail.com>
Date: Wed, 12 Jun 2024 18:24:17 -0400
Subject: [PATCH 2/3] Changed to const string refs rather than StringRef

---
 llvm/include/llvm/Passes/StandardInstrumentations.h | 6 +++---
 llvm/include/llvm/Support/Caching.h                 | 5 +++--
 llvm/lib/Passes/StandardInstrumentations.cpp        | 2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/llvm/include/llvm/Passes/StandardInstrumentations.h b/llvm/include/llvm/Passes/StandardInstrumentations.h
index b3caa689c84b4..12e9b78278130 100644
--- a/llvm/include/llvm/Passes/StandardInstrumentations.h
+++ b/llvm/include/llvm/Passes/StandardInstrumentations.h
@@ -382,13 +382,13 @@ class EmptyData {
 template <typename T>
 class FuncDataT : public OrderedChangedData<BlockDataT<T>> {
 public:
-  FuncDataT(StringRef S) : EntryBlockName(S) {}
+  FuncDataT(const std::string &S) : EntryBlockName(S) {}
 
   // Return the name of the entry block
-  StringRef getEntryBlockName() const { return EntryBlockName; }
+  const std::string &getEntryBlockName() const { return EntryBlockName; }
 
 protected:
-  StringRef EntryBlockName;
+  std::string EntryBlockName;
 };
 
 // The data saved for comparing IRs.
diff --git a/llvm/include/llvm/Support/Caching.h b/llvm/include/llvm/Support/Caching.h
index cdf2bc2ecee19..4fa57cc92e51f 100644
--- a/llvm/include/llvm/Support/Caching.h
+++ b/llvm/include/llvm/Support/Caching.h
@@ -27,10 +27,11 @@ class MemoryBuffer;
 /// that can be done by deriving from this class and overriding the destructor.
 class CachedFileStream {
 public:
-  CachedFileStream(std::unique_ptr<raw_pwrite_stream> OS, StringRef OSPath = "")
+  CachedFileStream(std::unique_ptr<raw_pwrite_stream> OS,
+                   std::string OSPath = "")
       : OS(std::move(OS)), ObjectPathName(OSPath) {}
   std::unique_ptr<raw_pwrite_stream> OS;
-  StringRef ObjectPathName;
+  std::string ObjectPathName;
   virtual ~CachedFileStream() = default;
 };
 
diff --git a/llvm/lib/Passes/StandardInstrumentations.cpp b/llvm/lib/Passes/StandardInstrumentations.cpp
index c7adc7668b9a1..df10f0032b532 100644
--- a/llvm/lib/Passes/StandardInstrumentations.cpp
+++ b/llvm/lib/Passes/StandardInstrumentations.cpp
@@ -2242,7 +2242,7 @@ void DotCfgChangeReporter::handleFunctionCompare(
 
   DotCfgDiff Diff(Text, Before, After);
   std::string EntryBlockName = After.getEntryBlockName();
-  // Use the before entry block if the after entry block was removed.
+  /*// Use the before entry block if the after entry block was removed.*/
   if (EntryBlockName == "")
     EntryBlockName = Before.getEntryBlockName();
   assert(EntryBlockName != "" && "Expected to find entry block");

>From 61c9372a747c01a7f3d8cdae36b515d5e5e40e88 Mon Sep 17 00:00:00 2001
From: Braden Helmer <bradenhelmeraus at gmail.com>
Date: Wed, 12 Jun 2024 18:47:03 -0400
Subject: [PATCH 3/3] Fix caching and revert changes to
 StandardInstrumentations.cpp

---
 llvm/include/llvm/Support/Caching.h          | 2 +-
 llvm/lib/Passes/StandardInstrumentations.cpp | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/Support/Caching.h b/llvm/include/llvm/Support/Caching.h
index 4fa57cc92e51f..b44f38beff8d0 100644
--- a/llvm/include/llvm/Support/Caching.h
+++ b/llvm/include/llvm/Support/Caching.h
@@ -28,7 +28,7 @@ class MemoryBuffer;
 class CachedFileStream {
 public:
   CachedFileStream(std::unique_ptr<raw_pwrite_stream> OS,
-                   std::string OSPath = "")
+                   const std::string &OSPath = "")
       : OS(std::move(OS)), ObjectPathName(OSPath) {}
   std::unique_ptr<raw_pwrite_stream> OS;
   std::string ObjectPathName;
diff --git a/llvm/lib/Passes/StandardInstrumentations.cpp b/llvm/lib/Passes/StandardInstrumentations.cpp
index df10f0032b532..c7adc7668b9a1 100644
--- a/llvm/lib/Passes/StandardInstrumentations.cpp
+++ b/llvm/lib/Passes/StandardInstrumentations.cpp
@@ -2242,7 +2242,7 @@ void DotCfgChangeReporter::handleFunctionCompare(
 
   DotCfgDiff Diff(Text, Before, After);
   std::string EntryBlockName = After.getEntryBlockName();
-  /*// Use the before entry block if the after entry block was removed.*/
+  // Use the before entry block if the after entry block was removed.
   if (EntryBlockName == "")
     EntryBlockName = Before.getEntryBlockName();
   assert(EntryBlockName != "" && "Expected to find entry block");



More information about the llvm-commits mailing list