[PATCH] D120144: [ADT] Have ArrayRef::copy() return a MutableArrayRef

Jez Ng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 18 10:05:52 PST 2022


int3 created this revision.
int3 added reviewers: bkramer, MaskRay, labath.
Herald added a subscriber: dexonsmith.
Herald added a project: lld-macho.
Herald added a reviewer: lld-macho.
int3 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The allocated memory itself is mutable, so let's expose that to the
caller. LLD has a use case for this.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120144

Files:
  lld/MachO/ICF.cpp
  llvm/include/llvm/ADT/ArrayRef.h


Index: llvm/include/llvm/ADT/ArrayRef.h
===================================================================
--- llvm/include/llvm/ADT/ArrayRef.h
+++ llvm/include/llvm/ADT/ArrayRef.h
@@ -25,6 +25,7 @@
 #include <vector>
 
 namespace llvm {
+  template<typename T> class LLVM_NODISCARD MutableArrayRef;
 
   /// ArrayRef - Represent a constant reference to an array (0 or more elements
   /// consecutively in memory), i.e. a start pointer and a length.  It allows
@@ -175,10 +176,10 @@
     }
 
     // copy - Allocate copy in Allocator and return ArrayRef<T> to it.
-    template <typename Allocator> ArrayRef<T> copy(Allocator &A) {
+    template <typename Allocator> MutableArrayRef<T> copy(Allocator &A) {
       T *Buff = A.template Allocate<T>(Length);
       std::uninitialized_copy(begin(), end(), Buff);
-      return ArrayRef<T>(Buff, Length);
+      return MutableArrayRef<T>(Buff, Length);
     }
 
     /// equals - Check for element-wise equality.
Index: lld/MachO/ICF.cpp
===================================================================
--- lld/MachO/ICF.cpp
+++ lld/MachO/ICF.cpp
@@ -378,11 +378,10 @@
     // mutable copy of the CFString and zero out the embedded addends before
     // performing any hashing / equality checks.
     if (isCfStringSection(isec)) {
-      uint8_t *copy = bAlloc().Allocate<uint8_t>(isec->data.size());
-      std::uninitialized_copy(isec->data.begin(), isec->data.end(), copy);
+      MutableArrayRef<uint8_t> copy = isec->data.copy(bAlloc());
       for (const Reloc &r : isec->relocs)
-        target->relocateOne(copy + r.offset, r, /*va=*/0, /*relocVA=*/0);
-      isec->data = ArrayRef<uint8_t>(copy, isec->data.size());
+        target->relocateOne(copy.data() + r.offset, r, /*va=*/0, /*relocVA=*/0);
+      isec->data = copy;
     }
     assert(isec->icfEqClass[0] == 0); // don't overwrite a unique ID!
     // Turn-on the top bit to guarantee that valid hashes have no collisions


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120144.409967.patch
Type: text/x-patch
Size: 1942 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220218/98ce03b2/attachment.bin>


More information about the llvm-commits mailing list