[llvm] Prefer const auto& to copying the entire object (PR #99830)

via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 21 16:38:48 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-directx

Author: AtariDreams (AtariDreams)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/99830.diff


8 Files Affected:

- (modified) llvm/lib/ExecutionEngine/Orc/Core.cpp (+1-1) 
- (modified) llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp (+1-1) 
- (modified) llvm/lib/Object/ArchiveWriter.cpp (+6-6) 
- (modified) llvm/lib/ObjectYAML/DXContainerEmitter.cpp (+3-3) 
- (modified) llvm/lib/Support/VirtualFileSystem.cpp (+1-1) 
- (modified) llvm/lib/Transforms/Utils/CodeExtractor.cpp (+1-1) 
- (modified) llvm/tools/llvm-readobj/ObjDumper.h (+1-1) 
- (modified) llvm/tools/llvm-remarkutil/RemarkCounter.cpp (+3-3) 


``````````diff
diff --git a/llvm/lib/ExecutionEngine/Orc/Core.cpp b/llvm/lib/ExecutionEngine/Orc/Core.cpp
index 3e6de62c8b7dd..56112312a5d05 100644
--- a/llvm/lib/ExecutionEngine/Orc/Core.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/Core.cpp
@@ -1692,7 +1692,7 @@ Error ExecutionSession::removeJITDylibs(std::vector<JITDylibSP> JDsToRemove) {
 
   // Clear JITDylibs and notify the platform.
   Error Err = Error::success();
-  for (auto JD : JDsToRemove) {
+  for (const auto& JD : JDsToRemove) {
     Err = joinErrors(std::move(Err), JD->clear());
     if (P)
       Err = joinErrors(std::move(Err), P->teardownJITDylib(*JD));
diff --git a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
index 075455c034154..167b2527c35ef 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObjcopy.cpp
@@ -255,7 +255,7 @@ Error Object::compressOrDecompressSections(const CommonConfig &Config) {
   }
 
   DenseMap<SectionBase *, SectionBase *> FromTo;
-  for (auto [S, Func] : ToReplace)
+  for (const auto& [S, Func] : ToReplace)
     FromTo[S] = Func();
   return replaceSections(FromTo);
 }
diff --git a/llvm/lib/Object/ArchiveWriter.cpp b/llvm/lib/Object/ArchiveWriter.cpp
index 114045561366d..53343c1bdf3e0 100644
--- a/llvm/lib/Object/ArchiveWriter.cpp
+++ b/llvm/lib/Object/ArchiveWriter.cpp
@@ -415,7 +415,7 @@ static uint64_t computeSymbolMapSize(uint64_t NumObj, SymMap &SymMap,
   uint64_t Size = sizeof(uint32_t) * 2; // Number of symbols and objects entries
   Size += NumObj * sizeof(uint32_t);    // Offset table
 
-  for (auto S : SymMap.Map)
+  for (const auto& S : SymMap.Map)
     Size += sizeof(uint16_t) + S.first.length() + 1;
 
   uint32_t Pad = offsetToAlignment(Size, Align(2));
@@ -429,7 +429,7 @@ static uint64_t computeECSymbolsSize(SymMap &SymMap,
                                      uint32_t *Padding = nullptr) {
   uint64_t Size = sizeof(uint32_t); // Number of symbols
 
-  for (auto S : SymMap.ECMap)
+  for (const auto& S : SymMap.ECMap)
     Size += sizeof(uint16_t) + S.first.length() + 1;
 
   uint32_t Pad = offsetToAlignment(Size, Align(2));
@@ -659,9 +659,9 @@ static void writeSymbolMap(raw_ostream &Out, object::Archive::Kind Kind,
 
   printLE<uint32_t>(Out, SymMap.Map.size());
 
-  for (auto S : SymMap.Map)
+  for (const auto& S : SymMap.Map)
     printLE(Out, S.second);
-  for (auto S : SymMap.Map)
+  for (const auto& S : SymMap.Map)
     Out << S.first << '\0';
 
   while (Pad--)
@@ -678,9 +678,9 @@ static void writeECSymbols(raw_ostream &Out, object::Archive::Kind Kind,
 
   printLE<uint32_t>(Out, SymMap.ECMap.size());
 
-  for (auto S : SymMap.ECMap)
+  for (const auto& S : SymMap.ECMap)
     printLE(Out, S.second);
-  for (auto S : SymMap.ECMap)
+  for (const auto& S : SymMap.ECMap)
     Out << S.first << '\0';
   while (Pad--)
     Out.write(uint8_t(0));
diff --git a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
index 175f1a12f9314..a41b0ca3fb4e7 100644
--- a/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
+++ b/llvm/lib/ObjectYAML/DXContainerEmitter.cpp
@@ -202,19 +202,19 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
       PSV.Resources = P.Info->Resources;
       PSV.EntryName = P.Info->EntryName;
 
-      for (auto El : P.Info->SigInputElements)
+      for (const auto& El : P.Info->SigInputElements)
         PSV.InputElements.push_back(mcdxbc::PSVSignatureElement{
             El.Name, El.Indices, El.StartRow, El.Cols, El.StartCol,
             El.Allocated, El.Kind, El.Type, El.Mode, El.DynamicMask,
             El.Stream});
 
-      for (auto El : P.Info->SigOutputElements)
+      for (const auto& El : P.Info->SigOutputElements)
         PSV.OutputElements.push_back(mcdxbc::PSVSignatureElement{
             El.Name, El.Indices, El.StartRow, El.Cols, El.StartCol,
             El.Allocated, El.Kind, El.Type, El.Mode, El.DynamicMask,
             El.Stream});
 
-      for (auto El : P.Info->SigPatchOrPrimElements)
+      for (const auto& El : P.Info->SigPatchOrPrimElements)
         PSV.PatchOrPrimElements.push_back(mcdxbc::PSVSignatureElement{
             El.Name, El.Indices, El.StartRow, El.Cols, El.StartCol,
             El.Allocated, El.Kind, El.Type, El.Mode, El.DynamicMask,
diff --git a/llvm/lib/Support/VirtualFileSystem.cpp b/llvm/lib/Support/VirtualFileSystem.cpp
index ce2bf2b4193a5..d9a3f86f0892d 100644
--- a/llvm/lib/Support/VirtualFileSystem.cpp
+++ b/llvm/lib/Support/VirtualFileSystem.cpp
@@ -498,7 +498,7 @@ std::error_code OverlayFileSystem::getRealPath(const Twine &Path,
 }
 
 void OverlayFileSystem::visitChildFileSystems(VisitCallbackTy Callback) {
-  for (IntrusiveRefCntPtr<FileSystem> FS : overlays_range()) {
+  for (const IntrusiveRefCntPtr<FileSystem>& FS : overlays_range()) {
     Callback(*FS);
     FS->visitChildFileSystems(Callback);
   }
diff --git a/llvm/lib/Transforms/Utils/CodeExtractor.cpp b/llvm/lib/Transforms/Utils/CodeExtractor.cpp
index 5bca5cf8ff91f..3c22fd6b137ac 100644
--- a/llvm/lib/Transforms/Utils/CodeExtractor.cpp
+++ b/llvm/lib/Transforms/Utils/CodeExtractor.cpp
@@ -1930,7 +1930,7 @@ CodeExtractor::extractCodeRegion(const CodeExtractorAnalysisCache &CEAC,
 bool CodeExtractor::verifyAssumptionCache(const Function &OldFunc,
                                           const Function &NewFunc,
                                           AssumptionCache *AC) {
-  for (auto AssumeVH : AC->assumptions()) {
+  for (const auto& AssumeVH : AC->assumptions()) {
     auto *I = dyn_cast_or_null<CallInst>(AssumeVH);
     if (!I)
       continue;
diff --git a/llvm/tools/llvm-readobj/ObjDumper.h b/llvm/tools/llvm-readobj/ObjDumper.h
index cd744e3bbfb71..f51e4ad03c65a 100644
--- a/llvm/tools/llvm-readobj/ObjDumper.h
+++ b/llvm/tools/llvm-readobj/ObjDumper.h
@@ -49,7 +49,7 @@ class SymbolComparator {
   void addPredicate(CompPredicate Pred) { Predicates.push_back(Pred); }
 
   bool operator()(object::SymbolRef LHS, object::SymbolRef RHS) {
-    for (CompPredicate Pred : Predicates) {
+    for (const CompPredicate& Pred : Predicates) {
       if (Pred(LHS, RHS))
         return true;
       if (Pred(RHS, LHS))
diff --git a/llvm/tools/llvm-remarkutil/RemarkCounter.cpp b/llvm/tools/llvm-remarkutil/RemarkCounter.cpp
index dc0685f342886..b52bc035847ed 100644
--- a/llvm/tools/llvm-remarkutil/RemarkCounter.cpp
+++ b/llvm/tools/llvm-remarkutil/RemarkCounter.cpp
@@ -222,7 +222,7 @@ Error ArgumentCounter::print(StringRef OutputFileName) {
     Idx++;
   }
   OF->os() << "\n";
-  for (auto [Header, CountVector] : CountByKeysMap) {
+  for (const auto& [Header, CountVector] : CountByKeysMap) {
     OF->os() << Header << ",";
     unsigned Idx = 0;
     for (auto Count : CountVector) {
@@ -245,7 +245,7 @@ Error RemarkCounter::print(StringRef OutputFileName) {
   auto OF = std::move(*MaybeOF);
   OF->os() << groupByToStr(Group) << ","
            << "Count\n";
-  for (auto [Key, Count] : CountedByRemarksMap)
+  for (const auto& [Key, Count] : CountedByRemarksMap)
     OF->os() << Key << "," << Count << "\n";
   OF->keep();
   return Error::success();
@@ -319,7 +319,7 @@ static Error collectRemarks() {
       for (auto &Key : Keys)
         ArgumentsVector.push_back({Key, false});
     } else if (!RKeys.empty())
-      for (auto Key : RKeys)
+      for (const auto& Key : RKeys)
         ArgumentsVector.push_back({Key, true});
     else
       ArgumentsVector.push_back({".*", true});

``````````

</details>


https://github.com/llvm/llvm-project/pull/99830


More information about the llvm-commits mailing list