xref: /aosp_15_r20/external/pytorch/c10/util/SmallVector.h (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1 //===- llvm/ADT/SmallVector.h - 'Normally small' vectors --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the SmallVector class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 // ATen: modified from llvm::SmallVector.
14 // used std::is_trivially_{copy,move}_constructible
15 // replaced iterator_range constructor with inline Container&& constructor
16 // replaced LLVM_NODISCARD, LLVM_LIKELY, and LLVM_UNLIKELY with c10 equivalents
17 // removed LLVM_GSL_OWNER
18 // added SmallVector::at
19 // added operator<< for std::ostream
20 // added C10_API to export SmallVectorBase
21 
22 #pragma once
23 
24 #include <c10/macros/Macros.h>
25 #include <c10/util/AlignOf.h>
26 
27 #include <algorithm>
28 #include <cassert>
29 #include <cstddef>
30 #include <cstdlib>
31 #include <cstring>
32 #include <functional>
33 #include <initializer_list>
34 #include <iterator>
35 #include <limits>
36 #include <memory>
37 #include <ostream>
38 #include <type_traits>
39 #include <utility>
40 
41 namespace c10 {
42 
43 /// This is all the stuff common to all SmallVectors.
44 ///
45 /// The template parameter specifies the type which should be used to hold the
46 /// Size and Capacity of the SmallVector, so it can be adjusted.
47 /// Using 32 bit size is desirable to shrink the size of the SmallVector.
48 /// Using 64 bit size is desirable for cases like SmallVector<char>, where a
49 /// 32 bit size would limit the vector to ~4GB. SmallVectors are used for
50 /// buffering bitcode output - which can exceed 4GB.
51 template <class Size_T>
52 class C10_API SmallVectorBase {
53  protected:
54   void* BeginX;
55   Size_T Size = 0, Capacity;
56 
57   /// The maximum value of the Size_T used.
SizeTypeMax()58   static constexpr size_t SizeTypeMax() {
59     return std::numeric_limits<Size_T>::max();
60   }
61 
SmallVectorBase(void * FirstEl,size_t TotalCapacity)62   SmallVectorBase(void* FirstEl, size_t TotalCapacity)
63       : BeginX(FirstEl), Capacity(TotalCapacity) {}
64 
65   /// This is a helper for \a grow() that's out of line to reduce code
66   /// duplication.  This function will report a fatal error if it can't grow at
67   /// least to \p MinSize.
68   void* mallocForGrow(size_t MinSize, size_t TSize, size_t& NewCapacity);
69 
70   /// This is an implementation of the grow() method which only works
71   /// on POD-like data types and is out of line to reduce code duplication.
72   /// This function will report a fatal error if it cannot increase capacity.
73   void grow_pod(const void* FirstEl, size_t MinSize, size_t TSize);
74 
75  public:
76   SmallVectorBase() = delete;
size()77   size_t size() const {
78     return Size;
79   }
capacity()80   size_t capacity() const {
81     return Capacity;
82   }
83 
empty()84   C10_NODISCARD bool empty() const {
85     return !Size;
86   }
87 
88   /// Set the array size to \p N, which the current array must have enough
89   /// capacity for.
90   ///
91   /// This does not construct or destroy any elements in the vector.
92   ///
93   /// Clients can use this in conjunction with capacity() to write past the end
94   /// of the buffer when they know that more elements are available, and only
95   /// update the size later. This avoids the cost of value initializing elements
96   /// which will only be overwritten.
set_size(size_t N)97   void set_size(size_t N) {
98     assert(N <= capacity());
99     Size = N;
100   }
101 };
102 
103 template <class T>
104 using SmallVectorSizeType =
105     std::conditional_t<sizeof(T) < 4 && sizeof(void*) >= 8, uint64_t, uint32_t>;
106 
107 /// Figure out the offset of the first element.
108 template <class T, typename = void>
109 struct SmallVectorAlignmentAndSize {
110   // NOLINTNEXTLINE(*c-arrays*)
111   alignas(SmallVectorBase<SmallVectorSizeType<T>>) char Base[sizeof(
112       SmallVectorBase<SmallVectorSizeType<T>>)];
113   // NOLINTNEXTLINE(*c-arrays*)
114   alignas(T) char FirstEl[sizeof(T)];
115 };
116 
117 /// This is the part of SmallVectorTemplateBase which does not depend on whether
118 /// the type T is a POD. The extra dummy template argument is used by ArrayRef
119 /// to avoid unnecessarily requiring T to be complete.
120 template <typename T, typename = void>
121 class SmallVectorTemplateCommon
122     : public SmallVectorBase<SmallVectorSizeType<T>> {
123   using Base = SmallVectorBase<SmallVectorSizeType<T>>;
124 
125   /// Find the address of the first element.  For this pointer math to be valid
126   /// with small-size of 0 for T with lots of alignment, it's important that
127   /// SmallVectorStorage is properly-aligned even for small-size of 0.
getFirstEl()128   void* getFirstEl() const {
129     return const_cast<void*>(reinterpret_cast<const void*>(
130         reinterpret_cast<const char*>(this) +
131         offsetof(SmallVectorAlignmentAndSize<T>, FirstEl)));
132   }
133   // Space after 'FirstEl' is clobbered, do not add any instance vars after it.
134 
135  protected:
SmallVectorTemplateCommon(size_t Size)136   SmallVectorTemplateCommon(size_t Size) : Base(getFirstEl(), Size) {}
137 
grow_pod(size_t MinSize,size_t TSize)138   void grow_pod(size_t MinSize, size_t TSize) {
139     Base::grow_pod(getFirstEl(), MinSize, TSize);
140   }
141 
142   /// Return true if this is a smallvector which has not had dynamic
143   /// memory allocated for it.
isSmall()144   bool isSmall() const {
145     return this->BeginX == getFirstEl();
146   }
147 
148   /// Put this vector in a state of being small.
resetToSmall()149   void resetToSmall() {
150     this->BeginX = getFirstEl();
151     this->Size = this->Capacity = 0; // FIXME: Setting Capacity to 0 is suspect.
152   }
153 
154   /// Return true if V is an internal reference to the given range.
isReferenceToRange(const void * V,const void * First,const void * Last)155   bool isReferenceToRange(const void* V, const void* First, const void* Last)
156       const {
157     // Use std::less to avoid UB.
158     std::less<> LessThan;
159     return !LessThan(V, First) && LessThan(V, Last);
160   }
161 
162   /// Return true if V is an internal reference to this vector.
isReferenceToStorage(const void * V)163   bool isReferenceToStorage(const void* V) const {
164     return isReferenceToRange(V, this->begin(), this->end());
165   }
166 
167   /// Return true if First and Last form a valid (possibly empty) range in this
168   /// vector's storage.
isRangeInStorage(const void * First,const void * Last)169   bool isRangeInStorage(const void* First, const void* Last) const {
170     // Use std::less to avoid UB.
171     std::less<> LessThan;
172     return !LessThan(First, this->begin()) && !LessThan(Last, First) &&
173         !LessThan(this->end(), Last);
174   }
175 
176   /// Return true unless Elt will be invalidated by resizing the vector to
177   /// NewSize.
isSafeToReferenceAfterResize(const void * Elt,size_t NewSize)178   bool isSafeToReferenceAfterResize(const void* Elt, size_t NewSize) {
179     // Past the end.
180     if (C10_LIKELY(!isReferenceToStorage(Elt)))
181       return true;
182 
183     // Return false if Elt will be destroyed by shrinking.
184     if (NewSize <= this->size())
185       return Elt < this->begin() + NewSize;
186 
187     // Return false if we need to grow.
188     return NewSize <= this->capacity();
189   }
190 
191   /// Check whether Elt will be invalidated by resizing the vector to NewSize.
assertSafeToReferenceAfterResize(const void * Elt,size_t NewSize)192   void assertSafeToReferenceAfterResize(const void* Elt, size_t NewSize) {
193     (void)Elt; // Suppress unused variable warning
194     (void)NewSize; // Suppress unused variable warning
195     assert(
196         isSafeToReferenceAfterResize(Elt, NewSize) &&
197         "Attempting to reference an element of the vector in an operation "
198         "that invalidates it");
199   }
200 
201   /// Check whether Elt will be invalidated by increasing the size of the
202   /// vector by N.
203   void assertSafeToAdd(const void* Elt, size_t N = 1) {
204     this->assertSafeToReferenceAfterResize(Elt, this->size() + N);
205   }
206 
207   /// Check whether any part of the range will be invalidated by clearing.
assertSafeToReferenceAfterClear(const T * From,const T * To)208   void assertSafeToReferenceAfterClear(const T* From, const T* To) {
209     if (From == To)
210       return;
211     this->assertSafeToReferenceAfterResize(From, 0);
212     this->assertSafeToReferenceAfterResize(To - 1, 0);
213   }
214   template <
215       class ItTy,
216       std::enable_if_t<!std::is_same_v<std::remove_const_t<ItTy>, T*>, bool> =
217           false>
assertSafeToReferenceAfterClear(ItTy,ItTy)218   void assertSafeToReferenceAfterClear(ItTy, ItTy) {}
219 
220   /// Check whether any part of the range will be invalidated by growing.
assertSafeToAddRange(const T * From,const T * To)221   void assertSafeToAddRange(const T* From, const T* To) {
222     if (From == To)
223       return;
224     this->assertSafeToAdd(From, To - From);
225     this->assertSafeToAdd(To - 1, To - From);
226   }
227   template <
228       class ItTy,
229       std::enable_if_t<!std::is_same_v<std::remove_const_t<ItTy>, T*>, bool> =
230           false>
assertSafeToAddRange(ItTy,ItTy)231   void assertSafeToAddRange(ItTy, ItTy) {}
232 
233   /// Reserve enough space to add one element, and return the updated element
234   /// pointer in case it was a reference to the storage.
235   template <class U>
reserveForParamAndGetAddressImpl(U * This,const T & Elt,size_t N)236   static const T* reserveForParamAndGetAddressImpl(
237       U* This,
238       const T& Elt,
239       size_t N) {
240     size_t NewSize = This->size() + N;
241     if (C10_LIKELY(NewSize <= This->capacity()))
242       return &Elt;
243 
244     bool ReferencesStorage = false;
245     int64_t Index = -1;
246     if constexpr (!U::TakesParamByValue) {
247       if (C10_UNLIKELY(This->isReferenceToStorage(&Elt))) {
248         ReferencesStorage = true;
249         Index = &Elt - This->begin();
250       }
251     }
252     This->grow(NewSize);
253     return ReferencesStorage ? This->begin() + Index : &Elt;
254   }
255 
256  public:
257   using size_type = size_t;
258   using difference_type = ptrdiff_t;
259   using value_type = T;
260   using iterator = T*;
261   using const_iterator = const T*;
262 
263   using const_reverse_iterator = std::reverse_iterator<const_iterator>;
264   using reverse_iterator = std::reverse_iterator<iterator>;
265 
266   using reference = T&;
267   using const_reference = const T&;
268   using pointer = T*;
269   using const_pointer = const T*;
270 
271   using Base::capacity;
272   using Base::empty;
273   using Base::size;
274 
275   // forward iterator creation methods.
begin()276   iterator begin() {
277     return (iterator)this->BeginX;
278   }
begin()279   const_iterator begin() const {
280     return (const_iterator)this->BeginX;
281   }
end()282   iterator end() {
283     return begin() + size();
284   }
end()285   const_iterator end() const {
286     return begin() + size();
287   }
288 
289   // reverse iterator creation methods.
rbegin()290   reverse_iterator rbegin() {
291     return reverse_iterator(end());
292   }
rbegin()293   const_reverse_iterator rbegin() const {
294     return const_reverse_iterator(end());
295   }
rend()296   reverse_iterator rend() {
297     return reverse_iterator(begin());
298   }
rend()299   const_reverse_iterator rend() const {
300     return const_reverse_iterator(begin());
301   }
302 
size_in_bytes()303   size_type size_in_bytes() const {
304     return size() * sizeof(T);
305   }
max_size()306   constexpr size_type max_size() const {
307     return std::min(this->SizeTypeMax(), size_type(-1) / sizeof(T));
308   }
309 
capacity_in_bytes()310   size_t capacity_in_bytes() const {
311     return capacity() * sizeof(T);
312   }
313 
314   /// Return a pointer to the vector's buffer, even if empty().
data()315   pointer data() {
316     return pointer(begin());
317   }
318   /// Return a pointer to the vector's buffer, even if empty().
data()319   const_pointer data() const {
320     return const_pointer(begin());
321   }
322 
323   // SmallVector::at is NOT from LLVM.
at(size_type idx)324   reference at(size_type idx) {
325     assert(idx < size());
326     return begin()[idx];
327   }
at(size_type idx)328   const_reference at(size_type idx) const {
329     assert(idx < size());
330     return begin()[idx];
331   }
332   reference operator[](size_type idx) {
333     assert(idx < size());
334     return begin()[idx];
335   }
336   const_reference operator[](size_type idx) const {
337     assert(idx < size());
338     return begin()[idx];
339   }
340 
front()341   reference front() {
342     assert(!empty());
343     return begin()[0];
344   }
front()345   const_reference front() const {
346     assert(!empty());
347     return begin()[0];
348   }
349 
back()350   reference back() {
351     assert(!empty());
352     return end()[-1];
353   }
back()354   const_reference back() const {
355     assert(!empty());
356     return end()[-1];
357   }
358 };
359 
360 /// SmallVectorTemplateBase<TriviallyCopyable = false> - This is where we put
361 /// method implementations that are designed to work with non-trivial T's.
362 ///
363 /// We approximate is_trivially_copyable with trivial move/copy construction and
364 /// trivial destruction. While the standard doesn't specify that you're allowed
365 /// copy these types with memcpy, there is no way for the type to observe this.
366 /// This catches the important case of std::pair<POD, POD>, which is not
367 /// trivially assignable.
368 ///
369 /// XXX: if build fails here fall back to C10_IS_TRIVIALLY_COPYABLE and make a
370 /// note
371 template <
372     typename T,
373     bool = (std::is_trivially_copy_constructible_v<T>)&&(
374         std::is_trivially_move_constructible_v<
375             T>)&&std::is_trivially_destructible_v<T>>
376 class SmallVectorTemplateBase : public SmallVectorTemplateCommon<T> {
377   friend class SmallVectorTemplateCommon<T>;
378 
379  protected:
380   static constexpr bool TakesParamByValue = false;
381   using ValueParamT = const T&;
382 
SmallVectorTemplateBase(size_t Size)383   SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
384 
destroy_range(T * S,T * E)385   static void destroy_range(T* S, T* E) {
386     while (S != E) {
387       --E;
388       E->~T();
389     }
390   }
391 
392   /// Move the range [I, E) into the uninitialized memory starting with "Dest",
393   /// constructing elements as needed.
394   template <typename It1, typename It2>
uninitialized_move(It1 I,It1 E,It2 Dest)395   static void uninitialized_move(It1 I, It1 E, It2 Dest) {
396     std::uninitialized_copy(
397         std::make_move_iterator(I), std::make_move_iterator(E), Dest);
398   }
399 
400   /// Copy the range [I, E) onto the uninitialized memory starting with "Dest",
401   /// constructing elements as needed.
402   template <typename It1, typename It2>
uninitialized_copy(It1 I,It1 E,It2 Dest)403   static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
404     std::uninitialized_copy(I, E, Dest);
405   }
406 
407   /// Grow the allocated memory (without initializing new elements), doubling
408   /// the size of the allocated memory. Guarantees space for at least one more
409   /// element, or MinSize more elements if specified.
410   void grow(size_t MinSize = 0);
411 
412   /// Create a new allocation big enough for \p MinSize and pass back its size
413   /// in \p NewCapacity. This is the first section of \a grow().
mallocForGrow(size_t MinSize,size_t & NewCapacity)414   T* mallocForGrow(size_t MinSize, size_t& NewCapacity) {
415     return static_cast<T*>(
416         SmallVectorBase<SmallVectorSizeType<T>>::mallocForGrow(
417             MinSize, sizeof(T), NewCapacity));
418   }
419 
420   /// Move existing elements over to the new allocation \p NewElts, the middle
421   /// section of \a grow().
422   void moveElementsForGrow(T* NewElts);
423 
424   /// Transfer ownership of the allocation, finishing up \a grow().
425   void takeAllocationForGrow(T* NewElts, size_t NewCapacity);
426 
427   /// Reserve enough space to add one element, and return the updated element
428   /// pointer in case it was a reference to the storage.
429   const T* reserveForParamAndGetAddress(const T& Elt, size_t N = 1) {
430     return this->reserveForParamAndGetAddressImpl(this, Elt, N);
431   }
432 
433   /// Reserve enough space to add one element, and return the updated element
434   /// pointer in case it was a reference to the storage.
435   T* reserveForParamAndGetAddress(T& Elt, size_t N = 1) {
436     return const_cast<T*>(this->reserveForParamAndGetAddressImpl(this, Elt, N));
437   }
438 
forward_value_param(T && V)439   static T&& forward_value_param(T&& V) {
440     return std::move(V);
441   }
forward_value_param(const T & V)442   static const T& forward_value_param(const T& V) {
443     return V;
444   }
445 
growAndAssign(size_t NumElts,const T & Elt)446   void growAndAssign(size_t NumElts, const T& Elt) {
447     // Grow manually in case Elt is an internal reference.
448     size_t NewCapacity = 0;
449     T* NewElts = mallocForGrow(NumElts, NewCapacity);
450     std::uninitialized_fill_n(NewElts, NumElts, Elt);
451     this->destroy_range(this->begin(), this->end());
452     takeAllocationForGrow(NewElts, NewCapacity);
453     this->set_size(NumElts);
454   }
455 
456   template <typename... ArgTypes>
growAndEmplaceBack(ArgTypes &&...Args)457   T& growAndEmplaceBack(ArgTypes&&... Args) {
458     // Grow manually in case one of Args is an internal reference.
459     size_t NewCapacity = 0;
460     T* NewElts = mallocForGrow(0, NewCapacity);
461     ::new ((void*)(NewElts + this->size())) T(std::forward<ArgTypes>(Args)...);
462     moveElementsForGrow(NewElts);
463     takeAllocationForGrow(NewElts, NewCapacity);
464     this->set_size(this->size() + 1);
465     return this->back();
466   }
467 
468  public:
push_back(const T & Elt)469   void push_back(const T& Elt) {
470     const T* EltPtr = reserveForParamAndGetAddress(Elt);
471     ::new ((void*)this->end()) T(*EltPtr);
472     this->set_size(this->size() + 1);
473   }
474 
475   // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved)
push_back(T && Elt)476   void push_back(T&& Elt) {
477     T* EltPtr = reserveForParamAndGetAddress(Elt);
478     ::new ((void*)this->end()) T(::std::move(*EltPtr));
479     this->set_size(this->size() + 1);
480   }
481 
pop_back()482   void pop_back() {
483     this->set_size(this->size() - 1);
484     this->end()->~T();
485   }
486 };
487 
488 // Define this out-of-line to dissuade the C++ compiler from inlining it.
489 template <typename T, bool TriviallyCopyable>
grow(size_t MinSize)490 void SmallVectorTemplateBase<T, TriviallyCopyable>::grow(size_t MinSize) {
491   size_t NewCapacity = 0;
492   T* NewElts = mallocForGrow(MinSize, NewCapacity);
493   moveElementsForGrow(NewElts);
494   takeAllocationForGrow(NewElts, NewCapacity);
495 }
496 
497 // Define this out-of-line to dissuade the C++ compiler from inlining it.
498 template <typename T, bool TriviallyCopyable>
moveElementsForGrow(T * NewElts)499 void SmallVectorTemplateBase<T, TriviallyCopyable>::moveElementsForGrow(
500     T* NewElts) {
501   // Move the elements over.
502   this->uninitialized_move(this->begin(), this->end(), NewElts);
503 
504   // Destroy the original elements.
505   destroy_range(this->begin(), this->end());
506 }
507 
508 // Define this out-of-line to dissuade the C++ compiler from inlining it.
509 template <typename T, bool TriviallyCopyable>
takeAllocationForGrow(T * NewElts,size_t NewCapacity)510 void SmallVectorTemplateBase<T, TriviallyCopyable>::takeAllocationForGrow(
511     T* NewElts,
512     size_t NewCapacity) {
513   // If this wasn't grown from the inline copy, deallocate the old space.
514   if (!this->isSmall())
515     free(this->begin());
516 
517   this->BeginX = NewElts;
518   this->Capacity = NewCapacity;
519 }
520 
521 /// SmallVectorTemplateBase<TriviallyCopyable = true> - This is where we put
522 /// method implementations that are designed to work with trivially copyable
523 /// T's. This allows using memcpy in place of copy/move construction and
524 /// skipping destruction.
525 template <typename T>
526 class SmallVectorTemplateBase<T, true> : public SmallVectorTemplateCommon<T> {
527   friend class SmallVectorTemplateCommon<T>;
528 
529  protected:
530   /// True if it's cheap enough to take parameters by value. Doing so avoids
531   /// overhead related to mitigations for reference invalidation.
532   static constexpr bool TakesParamByValue = sizeof(T) <= 2 * sizeof(void*);
533 
534   /// Either const T& or T, depending on whether it's cheap enough to take
535   /// parameters by value.
536   using ValueParamT = std::conditional_t<TakesParamByValue, T, const T&>;
537 
SmallVectorTemplateBase(size_t Size)538   SmallVectorTemplateBase(size_t Size) : SmallVectorTemplateCommon<T>(Size) {}
539 
540   // No need to do a destroy loop for POD's.
destroy_range(T *,T *)541   static void destroy_range(T*, T*) {}
542 
543   /// Move the range [I, E) onto the uninitialized memory
544   /// starting with "Dest", constructing elements into it as needed.
545   template <typename It1, typename It2>
uninitialized_move(It1 I,It1 E,It2 Dest)546   static void uninitialized_move(It1 I, It1 E, It2 Dest) {
547     // Just do a copy.
548     uninitialized_copy(I, E, Dest);
549   }
550 
551   /// Copy the range [I, E) onto the uninitialized memory
552   /// starting with "Dest", constructing elements into it as needed.
553   template <typename It1, typename It2>
uninitialized_copy(It1 I,It1 E,It2 Dest)554   static void uninitialized_copy(It1 I, It1 E, It2 Dest) {
555     // Arbitrary iterator types; just use the basic implementation.
556     std::uninitialized_copy(I, E, Dest);
557   }
558 
559   /// Copy the range [I, E) onto the uninitialized memory
560   /// starting with "Dest", constructing elements into it as needed.
561   template <typename T1, typename T2>
562   static void uninitialized_copy(
563       T1* I,
564       T1* E,
565       T2* Dest,
566       std::enable_if_t<std::is_same_v<std::remove_const_t<T1>, T2>>* =
567           nullptr) {
568     // Use memcpy for PODs iterated by pointers (which includes SmallVector
569     // iterators): std::uninitialized_copy optimizes to memmove, but we can
570     // use memcpy here. Note that I and E are iterators and thus might be
571     // invalid for memcpy if they are equal.
572     if (I != E)
573       memcpy(reinterpret_cast<void*>(Dest), I, (E - I) * sizeof(T));
574   }
575 
576   /// Double the size of the allocated memory, guaranteeing space for at
577   /// least one more element or MinSize if specified.
578   void grow(size_t MinSize = 0) {
579     this->grow_pod(MinSize, sizeof(T));
580   }
581 
582   /// Reserve enough space to add one element, and return the updated element
583   /// pointer in case it was a reference to the storage.
584   const T* reserveForParamAndGetAddress(const T& Elt, size_t N = 1) {
585     return this->reserveForParamAndGetAddressImpl(this, Elt, N);
586   }
587 
588   /// Reserve enough space to add one element, and return the updated element
589   /// pointer in case it was a reference to the storage.
590   T* reserveForParamAndGetAddress(T& Elt, size_t N = 1) {
591     return const_cast<T*>(this->reserveForParamAndGetAddressImpl(this, Elt, N));
592   }
593 
594   /// Copy \p V or return a reference, depending on \a ValueParamT.
forward_value_param(ValueParamT V)595   static ValueParamT forward_value_param(ValueParamT V) {
596     return V;
597   }
598 
growAndAssign(size_t NumElts,T Elt)599   void growAndAssign(size_t NumElts, T Elt) {
600     // Elt has been copied in case it's an internal reference, side-stepping
601     // reference invalidation problems without losing the realloc optimization.
602     this->set_size(0);
603     this->grow(NumElts);
604     std::uninitialized_fill_n(this->begin(), NumElts, Elt);
605     this->set_size(NumElts);
606   }
607 
608   template <typename... ArgTypes>
growAndEmplaceBack(ArgTypes &&...Args)609   T& growAndEmplaceBack(ArgTypes&&... Args) {
610     // Use push_back with a copy in case Args has an internal reference,
611     // side-stepping reference invalidation problems without losing the realloc
612     // optimization.
613     push_back(T(std::forward<ArgTypes>(Args)...));
614     return this->back();
615   }
616 
617  public:
push_back(ValueParamT Elt)618   void push_back(ValueParamT Elt) {
619     const T* EltPtr = reserveForParamAndGetAddress(Elt);
620     memcpy(reinterpret_cast<void*>(this->end()), EltPtr, sizeof(T));
621     this->set_size(this->size() + 1);
622   }
623 
pop_back()624   void pop_back() {
625     this->set_size(this->size() - 1);
626   }
627 };
628 
629 /// This class consists of common code factored out of the SmallVector class to
630 /// reduce code duplication based on the SmallVector 'N' template parameter.
631 template <typename T>
632 class SmallVectorImpl : public SmallVectorTemplateBase<T> {
633   using SuperClass = SmallVectorTemplateBase<T>;
634 
635  public:
636   using iterator = typename SuperClass::iterator;
637   using const_iterator = typename SuperClass::const_iterator;
638   using reference = typename SuperClass::reference;
639   using size_type = typename SuperClass::size_type;
640 
641  protected:
642   using SmallVectorTemplateBase<T>::TakesParamByValue;
643   using ValueParamT = typename SuperClass::ValueParamT;
644 
645   // Default ctor - Initialize to empty.
SmallVectorImpl(unsigned N)646   explicit SmallVectorImpl(unsigned N) : SmallVectorTemplateBase<T>(N) {}
647 
648  public:
649   SmallVectorImpl(const SmallVectorImpl&) = delete;
650 
~SmallVectorImpl()651   ~SmallVectorImpl() {
652     // Subclass has already destructed this vector's elements.
653     // If this wasn't grown from the inline copy, deallocate the old space.
654     if (!this->isSmall())
655       free(this->begin());
656   }
657 
clear()658   void clear() {
659     this->destroy_range(this->begin(), this->end());
660     this->Size = 0;
661   }
662 
663  private:
664   template <bool ForOverwrite>
resizeImpl(size_type N)665   void resizeImpl(size_type N) {
666     if (N < this->size()) {
667       this->pop_back_n(this->size() - N);
668     } else if (N > this->size()) {
669       this->reserve(N);
670       for (auto I = this->end(), E = this->begin() + N; I != E; ++I)
671         if (ForOverwrite)
672           new (&*I) T;
673         else
674           new (&*I) T();
675       this->set_size(N);
676     }
677   }
678 
679  public:
resize(size_type N)680   void resize(size_type N) {
681     resizeImpl<false>(N);
682   }
683 
684   /// Like resize, but \ref T is POD, the new values won't be initialized.
resize_for_overwrite(size_type N)685   void resize_for_overwrite(size_type N) {
686     resizeImpl<true>(N);
687   }
688 
resize(size_type N,ValueParamT NV)689   void resize(size_type N, ValueParamT NV) {
690     if (N == this->size())
691       return;
692 
693     if (N < this->size()) {
694       this->pop_back_n(this->size() - N);
695       return;
696     }
697 
698     // N > this->size(). Defer to append.
699     this->append(N - this->size(), NV);
700   }
701 
reserve(size_type N)702   void reserve(size_type N) {
703     if (this->capacity() < N)
704       this->grow(N);
705   }
706 
pop_back_n(size_type NumItems)707   void pop_back_n(size_type NumItems) {
708     assert(this->size() >= NumItems);
709     this->destroy_range(this->end() - NumItems, this->end());
710     this->set_size(this->size() - NumItems);
711   }
712 
pop_back_val()713   C10_NODISCARD T pop_back_val() {
714     T Result = ::std::move(this->back());
715     this->pop_back();
716     return Result;
717   }
718 
719   void swap(SmallVectorImpl& RHS) noexcept;
720 
721   /// Add the specified range to the end of the SmallVector.
722   template <
723       typename in_iter,
724       typename = std::enable_if_t<std::is_convertible_v<
725           typename std::iterator_traits<in_iter>::iterator_category,
726           std::input_iterator_tag>>>
append(in_iter in_start,in_iter in_end)727   void append(in_iter in_start, in_iter in_end) {
728     this->assertSafeToAddRange(in_start, in_end);
729     size_type NumInputs = std::distance(in_start, in_end);
730     this->reserve(this->size() + NumInputs);
731     this->uninitialized_copy(in_start, in_end, this->end());
732     this->set_size(this->size() + NumInputs);
733   }
734 
735   /// Append \p NumInputs copies of \p Elt to the end.
append(size_type NumInputs,ValueParamT Elt)736   void append(size_type NumInputs, ValueParamT Elt) {
737     const T* EltPtr = this->reserveForParamAndGetAddress(Elt, NumInputs);
738     std::uninitialized_fill_n(this->end(), NumInputs, *EltPtr);
739     this->set_size(this->size() + NumInputs);
740   }
741 
append(std::initializer_list<T> IL)742   void append(std::initializer_list<T> IL) {
743     append(IL.begin(), IL.end());
744   }
745 
append(const SmallVectorImpl & RHS)746   void append(const SmallVectorImpl& RHS) {
747     append(RHS.begin(), RHS.end());
748   }
749 
assign(size_type NumElts,ValueParamT Elt)750   void assign(size_type NumElts, ValueParamT Elt) {
751     // Note that Elt could be an internal reference.
752     if (NumElts > this->capacity()) {
753       this->growAndAssign(NumElts, Elt);
754       return;
755     }
756 
757     // Assign over existing elements.
758     std::fill_n(this->begin(), std::min(NumElts, this->size()), Elt);
759     if (NumElts > this->size())
760       std::uninitialized_fill_n(this->end(), NumElts - this->size(), Elt);
761     else if (NumElts < this->size())
762       this->destroy_range(this->begin() + NumElts, this->end());
763     this->set_size(NumElts);
764   }
765 
766   // FIXME: Consider assigning over existing elements, rather than clearing &
767   // re-initializing them - for all assign(...) variants.
768 
769   template <
770       typename in_iter,
771       typename = std::enable_if_t<std::is_convertible_v<
772           typename std::iterator_traits<in_iter>::iterator_category,
773           std::input_iterator_tag>>>
assign(in_iter in_start,in_iter in_end)774   void assign(in_iter in_start, in_iter in_end) {
775     this->assertSafeToReferenceAfterClear(in_start, in_end);
776     clear();
777     append(in_start, in_end);
778   }
779 
assign(std::initializer_list<T> IL)780   void assign(std::initializer_list<T> IL) {
781     clear();
782     append(IL);
783   }
784 
assign(const SmallVectorImpl & RHS)785   void assign(const SmallVectorImpl& RHS) {
786     assign(RHS.begin(), RHS.end());
787   }
788 
erase(iterator I)789   iterator erase(iterator I) {
790     assert(
791         this->isReferenceToStorage(I) && "Iterator to erase is out of bounds.");
792 
793     iterator N = I;
794     // Shift all elts down one.
795     std::move(I + 1, this->end(), I);
796     // Drop the last elt.
797     this->pop_back();
798     return (N);
799   }
800 
erase(iterator S,iterator E)801   iterator erase(iterator S, iterator E) {
802     assert(this->isRangeInStorage(S, E) && "Range to erase is out of bounds.");
803 
804     iterator N = S;
805     // Shift all elts down.
806     iterator I = std::move(E, this->end(), S);
807     // Drop the last elts.
808     this->destroy_range(I, this->end());
809     this->set_size(I - this->begin());
810     return (N);
811   }
812 
813  private:
814   template <class ArgType>
insert_one_impl(iterator I,ArgType && Elt)815   iterator insert_one_impl(iterator I, ArgType&& Elt) {
816     // Callers ensure that ArgType is derived from T.
817     static_assert(
818         std::is_same<std::remove_const_t<std::remove_reference_t<ArgType>>, T>::
819             value,
820         "ArgType must be derived from T!");
821 
822     if (I == this->end()) { // Important special case for empty vector.
823       this->push_back(::std::forward<ArgType>(Elt));
824       return this->end() - 1;
825     }
826 
827     assert(
828         this->isReferenceToStorage(I) &&
829         "Insertion iterator is out of bounds.");
830 
831     // Grow if necessary.
832     size_t Index = I - this->begin();
833     std::remove_reference_t<ArgType>* EltPtr =
834         this->reserveForParamAndGetAddress(Elt);
835     I = this->begin() + Index;
836 
837     ::new ((void*)this->end()) T(::std::move(this->back()));
838     // Push everything else over.
839     std::move_backward(I, this->end() - 1, this->end());
840     this->set_size(this->size() + 1);
841 
842     // If we just moved the element we're inserting, be sure to update
843     // the reference (never happens if TakesParamByValue).
844     static_assert(
845         !TakesParamByValue || std::is_same<ArgType, T>::value,
846         "ArgType must be 'T' when taking by value!");
847     if (!TakesParamByValue && this->isReferenceToRange(EltPtr, I, this->end()))
848       ++EltPtr;
849 
850     *I = ::std::forward<ArgType>(*EltPtr);
851     return I;
852   }
853 
854  public:
insert(iterator I,T && Elt)855   iterator insert(iterator I, T&& Elt) {
856     return insert_one_impl(I, this->forward_value_param(std::move(Elt)));
857   }
858 
insert(iterator I,const T & Elt)859   iterator insert(iterator I, const T& Elt) {
860     return insert_one_impl(I, this->forward_value_param(Elt));
861   }
862 
insert(iterator I,size_type NumToInsert,ValueParamT Elt)863   iterator insert(iterator I, size_type NumToInsert, ValueParamT Elt) {
864     // Convert iterator to elt# to avoid invalidating iterator when we reserve()
865     size_t InsertElt = I - this->begin();
866 
867     if (I == this->end()) { // Important special case for empty vector.
868       append(NumToInsert, Elt);
869       return this->begin() + InsertElt;
870     }
871 
872     assert(
873         this->isReferenceToStorage(I) &&
874         "Insertion iterator is out of bounds.");
875 
876     // Ensure there is enough space, and get the (maybe updated) address of
877     // Elt.
878     const T* EltPtr = this->reserveForParamAndGetAddress(Elt, NumToInsert);
879 
880     // Uninvalidate the iterator.
881     I = this->begin() + InsertElt;
882 
883     // If there are more elements between the insertion point and the end of the
884     // range than there are being inserted, we can use a simple approach to
885     // insertion.  Since we already reserved space, we know that this won't
886     // reallocate the vector.
887     if (size_t(this->end() - I) >= NumToInsert) {
888       T* OldEnd = this->end();
889       append(
890           std::move_iterator<iterator>(this->end() - NumToInsert),
891           std::move_iterator<iterator>(this->end()));
892 
893       // Copy the existing elements that get replaced.
894       std::move_backward(I, OldEnd - NumToInsert, OldEnd);
895 
896       // If we just moved the element we're inserting, be sure to update
897       // the reference (never happens if TakesParamByValue).
898       if (!TakesParamByValue && I <= EltPtr && EltPtr < this->end())
899         EltPtr += NumToInsert;
900 
901       std::fill_n(I, NumToInsert, *EltPtr);
902       return I;
903     }
904 
905     // Otherwise, we're inserting more elements than exist already, and we're
906     // not inserting at the end.
907 
908     // Move over the elements that we're about to overwrite.
909     T* OldEnd = this->end();
910     this->set_size(this->size() + NumToInsert);
911     size_t NumOverwritten = OldEnd - I;
912     this->uninitialized_move(I, OldEnd, this->end() - NumOverwritten);
913 
914     // If we just moved the element we're inserting, be sure to update
915     // the reference (never happens if TakesParamByValue).
916     if (!TakesParamByValue && I <= EltPtr && EltPtr < this->end())
917       EltPtr += NumToInsert;
918 
919     // Replace the overwritten part.
920     std::fill_n(I, NumOverwritten, *EltPtr);
921 
922     // Insert the non-overwritten middle part.
923     std::uninitialized_fill_n(OldEnd, NumToInsert - NumOverwritten, *EltPtr);
924     return I;
925   }
926 
927   template <
928       typename ItTy,
929       typename = std::enable_if_t<std::is_convertible_v<
930           typename std::iterator_traits<ItTy>::iterator_category,
931           std::input_iterator_tag>>>
insert(iterator I,ItTy From,ItTy To)932   iterator insert(iterator I, ItTy From, ItTy To) {
933     // Convert iterator to elt# to avoid invalidating iterator when we reserve()
934     size_t InsertElt = I - this->begin();
935 
936     if (I == this->end()) { // Important special case for empty vector.
937       append(From, To);
938       return this->begin() + InsertElt;
939     }
940 
941     assert(
942         this->isReferenceToStorage(I) &&
943         "Insertion iterator is out of bounds.");
944 
945     // Check that the reserve that follows doesn't invalidate the iterators.
946     this->assertSafeToAddRange(From, To);
947 
948     size_t NumToInsert = std::distance(From, To);
949 
950     // Ensure there is enough space.
951     reserve(this->size() + NumToInsert);
952 
953     // Uninvalidate the iterator.
954     I = this->begin() + InsertElt;
955 
956     // If there are more elements between the insertion point and the end of the
957     // range than there are being inserted, we can use a simple approach to
958     // insertion.  Since we already reserved space, we know that this won't
959     // reallocate the vector.
960     if (size_t(this->end() - I) >= NumToInsert) {
961       T* OldEnd = this->end();
962       append(
963           std::move_iterator<iterator>(this->end() - NumToInsert),
964           std::move_iterator<iterator>(this->end()));
965 
966       // Copy the existing elements that get replaced.
967       std::move_backward(I, OldEnd - NumToInsert, OldEnd);
968 
969       std::copy(From, To, I);
970       return I;
971     }
972 
973     // Otherwise, we're inserting more elements than exist already, and we're
974     // not inserting at the end.
975 
976     // Move over the elements that we're about to overwrite.
977     T* OldEnd = this->end();
978     this->set_size(this->size() + NumToInsert);
979     size_t NumOverwritten = OldEnd - I;
980     this->uninitialized_move(I, OldEnd, this->end() - NumOverwritten);
981 
982     // Replace the overwritten part.
983     for (T* J = I; NumOverwritten > 0; --NumOverwritten) {
984       *J = *From;
985       ++J;
986       ++From;
987     }
988 
989     // Insert the non-overwritten middle part.
990     this->uninitialized_copy(From, To, OldEnd);
991     return I;
992   }
993 
insert(iterator I,std::initializer_list<T> IL)994   void insert(iterator I, std::initializer_list<T> IL) {
995     insert(I, IL.begin(), IL.end());
996   }
997 
998   template <typename... ArgTypes>
emplace_back(ArgTypes &&...Args)999   reference emplace_back(ArgTypes&&... Args) {
1000     if (C10_UNLIKELY(this->size() >= this->capacity()))
1001       return this->growAndEmplaceBack(std::forward<ArgTypes>(Args)...);
1002 
1003     ::new ((void*)this->end()) T(std::forward<ArgTypes>(Args)...);
1004     this->set_size(this->size() + 1);
1005     return this->back();
1006   }
1007 
1008   SmallVectorImpl& operator=(const SmallVectorImpl& RHS);
1009 
1010   SmallVectorImpl& operator=(SmallVectorImpl&& RHS) noexcept(
1011       std::is_nothrow_move_constructible_v<T> &&
1012       std::is_nothrow_destructible_v<T>);
1013 
1014   bool operator==(const SmallVectorImpl& RHS) const {
1015     if (this->size() != RHS.size())
1016       return false;
1017     return std::equal(this->begin(), this->end(), RHS.begin());
1018   }
1019   bool operator!=(const SmallVectorImpl& RHS) const {
1020     return !(*this == RHS);
1021   }
1022 
1023   bool operator<(const SmallVectorImpl& RHS) const {
1024     return std::lexicographical_compare(
1025         this->begin(), this->end(), RHS.begin(), RHS.end());
1026   }
1027 };
1028 
1029 template <typename T>
swap(SmallVectorImpl<T> & RHS)1030 void SmallVectorImpl<T>::swap(SmallVectorImpl<T>& RHS) noexcept {
1031   if (this == &RHS)
1032     return;
1033 
1034   // We can only avoid copying elements if neither vector is small.
1035   if (!this->isSmall() && !RHS.isSmall()) {
1036     std::swap(this->BeginX, RHS.BeginX);
1037     std::swap(this->Size, RHS.Size);
1038     std::swap(this->Capacity, RHS.Capacity);
1039     return;
1040   }
1041   this->reserve(RHS.size());
1042   RHS.reserve(this->size());
1043 
1044   // Swap the shared elements.
1045   size_t NumShared = this->size();
1046   if (NumShared > RHS.size())
1047     NumShared = RHS.size();
1048   for (size_type i = 0; i != NumShared; ++i)
1049     std::swap((*this)[i], RHS[i]);
1050 
1051   // Copy over the extra elts.
1052   if (this->size() > RHS.size()) {
1053     size_t EltDiff = this->size() - RHS.size();
1054     this->uninitialized_copy(this->begin() + NumShared, this->end(), RHS.end());
1055     RHS.set_size(RHS.size() + EltDiff);
1056     this->destroy_range(this->begin() + NumShared, this->end());
1057     this->set_size(NumShared);
1058   } else if (RHS.size() > this->size()) {
1059     size_t EltDiff = RHS.size() - this->size();
1060     this->uninitialized_copy(RHS.begin() + NumShared, RHS.end(), this->end());
1061     this->set_size(this->size() + EltDiff);
1062     this->destroy_range(RHS.begin() + NumShared, RHS.end());
1063     RHS.set_size(NumShared);
1064   }
1065 }
1066 
1067 template <typename T>
1068 SmallVectorImpl<T>& SmallVectorImpl<T>::operator=(
1069     const SmallVectorImpl<T>& RHS) {
1070   // Avoid self-assignment.
1071   if (this == &RHS)
1072     return *this;
1073 
1074   // If we already have sufficient space, assign the common elements, then
1075   // destroy any excess.
1076   size_t RHSSize = RHS.size();
1077   size_t CurSize = this->size();
1078   if (CurSize >= RHSSize) {
1079     // Assign common elements.
1080     iterator NewEnd;
1081     if (RHSSize)
1082       NewEnd = std::copy(RHS.begin(), RHS.begin() + RHSSize, this->begin());
1083     else
1084       NewEnd = this->begin();
1085 
1086     // Destroy excess elements.
1087     this->destroy_range(NewEnd, this->end());
1088 
1089     // Trim.
1090     this->set_size(RHSSize);
1091     return *this;
1092   }
1093 
1094   // If we have to grow to have enough elements, destroy the current elements.
1095   // This allows us to avoid copying them during the grow.
1096   // FIXME: don't do this if they're efficiently moveable.
1097   if (this->capacity() < RHSSize) {
1098     // Destroy current elements.
1099     this->clear();
1100     CurSize = 0;
1101     this->grow(RHSSize);
1102   } else if (CurSize) {
1103     // Otherwise, use assignment for the already-constructed elements.
1104     std::copy(RHS.begin(), RHS.begin() + CurSize, this->begin());
1105   }
1106 
1107   // Copy construct the new elements in place.
1108   this->uninitialized_copy(
1109       RHS.begin() + CurSize, RHS.end(), this->begin() + CurSize);
1110 
1111   // Set end.
1112   this->set_size(RHSSize);
1113   return *this;
1114 }
1115 
1116 template <typename T>
1117 SmallVectorImpl<T>& SmallVectorImpl<T>::
noexcept(std::is_nothrow_move_constructible_v<T> && std::is_nothrow_destructible_v<T>)1118 operator=(SmallVectorImpl<T>&& RHS) noexcept(
1119     std::is_nothrow_move_constructible_v<T> &&
1120     std::is_nothrow_destructible_v<T>) {
1121   // Avoid self-assignment.
1122   if (this == &RHS)
1123     return *this;
1124 
1125   // If the RHS isn't small, clear this vector and then steal its buffer.
1126   if (!RHS.isSmall()) {
1127     this->destroy_range(this->begin(), this->end());
1128     if (!this->isSmall())
1129       free(this->begin());
1130     this->BeginX = RHS.BeginX;
1131     this->Size = RHS.Size;
1132     this->Capacity = RHS.Capacity;
1133     RHS.resetToSmall();
1134     return *this;
1135   }
1136 
1137   // If we already have sufficient space, assign the common elements, then
1138   // destroy any excess.
1139   size_t RHSSize = RHS.size();
1140   size_t CurSize = this->size();
1141   if (CurSize >= RHSSize) {
1142     // Assign common elements.
1143     iterator NewEnd = this->begin();
1144     if (RHSSize)
1145       NewEnd = std::move(RHS.begin(), RHS.end(), NewEnd);
1146 
1147     // Destroy excess elements and trim the bounds.
1148     this->destroy_range(NewEnd, this->end());
1149     this->set_size(RHSSize);
1150 
1151     // Clear the RHS.
1152     RHS.clear();
1153 
1154     return *this;
1155   }
1156 
1157   // If we have to grow to have enough elements, destroy the current elements.
1158   // This allows us to avoid copying them during the grow.
1159   // FIXME: this may not actually make any sense if we can efficiently move
1160   // elements.
1161   if (this->capacity() < RHSSize) {
1162     // Destroy current elements.
1163     this->clear();
1164     CurSize = 0;
1165     this->grow(RHSSize);
1166   } else if (CurSize) {
1167     // Otherwise, use assignment for the already-constructed elements.
1168     std::move(RHS.begin(), RHS.begin() + CurSize, this->begin());
1169   }
1170 
1171   // Move-construct the new elements in place.
1172   this->uninitialized_move(
1173       RHS.begin() + CurSize, RHS.end(), this->begin() + CurSize);
1174 
1175   // Set end.
1176   this->set_size(RHSSize);
1177 
1178   RHS.clear();
1179   return *this;
1180 }
1181 
1182 /// Storage for the SmallVector elements.  This is specialized for the N=0 case
1183 /// to avoid allocating unnecessary storage.
1184 template <typename T, unsigned N>
1185 struct SmallVectorStorage {
1186   alignas(T) char InlineElts[N * sizeof(T)];
1187 };
1188 
1189 /// We need the storage to be properly aligned even for small-size of 0 so that
1190 /// the pointer math in \a SmallVectorTemplateCommon::getFirstEl() is
1191 /// well-defined.
1192 template <typename T>
1193 struct alignas(T) SmallVectorStorage<T, 0> {};
1194 
1195 /// Forward declaration of SmallVector so that
1196 /// calculateSmallVectorDefaultInlinedElements can reference
1197 /// `sizeof(SmallVector<T, 0>)`.
1198 template <typename T, unsigned N>
1199 class /* LLVM_GSL_OWNER */ SmallVector;
1200 
1201 /// Helper class for calculating the default number of inline elements for
1202 /// `SmallVector<T>`.
1203 ///
1204 /// This should be migrated to a constexpr function when our minimum
1205 /// compiler support is enough for multi-statement constexpr functions.
1206 template <typename T>
1207 struct CalculateSmallVectorDefaultInlinedElements {
1208   // Parameter controlling the default number of inlined elements
1209   // for `SmallVector<T>`.
1210   //
1211   // The default number of inlined elements ensures that
1212   // 1. There is at least one inlined element.
1213   // 2. `sizeof(SmallVector<T>) <= kPreferredSmallVectorSizeof` unless
1214   // it contradicts 1.
1215   static constexpr size_t kPreferredSmallVectorSizeof = 64;
1216 
1217   // static_assert that sizeof(T) is not "too big".
1218   //
1219   // Because our policy guarantees at least one inlined element, it is possible
1220   // for an arbitrarily large inlined element to allocate an arbitrarily large
1221   // amount of inline storage. We generally consider it an antipattern for a
1222   // SmallVector to allocate an excessive amount of inline storage, so we want
1223   // to call attention to these cases and make sure that users are making an
1224   // intentional decision if they request a lot of inline storage.
1225   //
1226   // We want this assertion to trigger in pathological cases, but otherwise
1227   // not be too easy to hit. To accomplish that, the cutoff is actually somewhat
1228   // larger than kPreferredSmallVectorSizeof (otherwise,
1229   // `SmallVector<SmallVector<T>>` would be one easy way to trip it, and that
1230   // pattern seems useful in practice).
1231   //
1232   // One wrinkle is that this assertion is in theory non-portable, since
1233   // sizeof(T) is in general platform-dependent. However, we don't expect this
1234   // to be much of an issue, because most LLVM development happens on 64-bit
1235   // hosts, and therefore sizeof(T) is expected to *decrease* when compiled for
1236   // 32-bit hosts, dodging the issue. The reverse situation, where development
1237   // happens on a 32-bit host and then fails due to sizeof(T) *increasing* on a
1238   // 64-bit host, is expected to be very rare.
1239   static_assert(
1240       sizeof(T) <= 256,
1241       "You are trying to use a default number of inlined elements for "
1242       "`SmallVector<T>` but `sizeof(T)` is really big! Please use an "
1243       "explicit number of inlined elements with `SmallVector<T, N>` to make "
1244       "sure you really want that much inline storage.");
1245 
1246   // Discount the size of the header itself when calculating the maximum inline
1247   // bytes.
1248   static constexpr size_t PreferredInlineBytes =
1249       kPreferredSmallVectorSizeof - sizeof(SmallVector<T, 0>);
1250   static constexpr size_t NumElementsThatFit = PreferredInlineBytes / sizeof(T);
1251   static constexpr size_t value =
1252       NumElementsThatFit == 0 ? 1 : NumElementsThatFit;
1253 };
1254 
1255 /// This is a 'vector' (really, a variable-sized array), optimized
1256 /// for the case when the array is small.  It contains some number of elements
1257 /// in-place, which allows it to avoid heap allocation when the actual number of
1258 /// elements is below that threshold.  This allows normal "small" cases to be
1259 /// fast without losing generality for large inputs.
1260 ///
1261 /// \note
1262 /// In the absence of a well-motivated choice for the number of inlined
1263 /// elements \p N, it is recommended to use \c SmallVector<T> (that is,
1264 /// omitting the \p N). This will choose a default number of inlined elements
1265 /// reasonable for allocation on the stack (for example, trying to keep \c
1266 /// sizeof(SmallVector<T>) around 64 bytes).
1267 ///
1268 /// \warning This does not attempt to be exception safe.
1269 ///
1270 /// \see https://llvm.org/docs/ProgrammersManual.html#llvm-adt-smallvector-h
1271 template <
1272     typename T,
1273     unsigned N = CalculateSmallVectorDefaultInlinedElements<T>::value>
1274 class /* LLVM_GSL_OWNER */ SmallVector : public SmallVectorImpl<T>,
1275                                          SmallVectorStorage<T, N> {
1276  public:
1277   SmallVector() : SmallVectorImpl<T>(N) {}
1278 
1279   ~SmallVector() {
1280     // Destroy the constructed elements in the vector.
1281     this->destroy_range(this->begin(), this->end());
1282   }
1283 
1284   explicit SmallVector(size_t Size, const T& Value = T())
1285       : SmallVectorImpl<T>(N) {
1286     this->assign(Size, Value);
1287   }
1288 
1289   template <
1290       typename ItTy,
1291       typename = std::enable_if_t<std::is_convertible_v<
1292           typename std::iterator_traits<ItTy>::iterator_category,
1293           std::input_iterator_tag>>>
1294   SmallVector(ItTy S, ItTy E) : SmallVectorImpl<T>(N) {
1295     this->append(S, E);
1296   }
1297 
1298   // note: The enable_if restricts Container to types that have a .begin() and
1299   // .end() that return valid input iterators.
1300   template <
1301       typename Container,
1302       std::enable_if_t<
1303           std::is_convertible_v<
1304               typename std::iterator_traits<
1305                   decltype(std::declval<Container>()
1306                                .begin())>::iterator_category,
1307               std::input_iterator_tag> &&
1308               std::is_convertible_v<
1309                   typename std::iterator_traits<
1310                       decltype(std::declval<Container>()
1311                                    .end())>::iterator_category,
1312                   std::input_iterator_tag>,
1313           int> = 0>
1314   explicit SmallVector(Container&& c) : SmallVectorImpl<T>(N) {
1315     this->append(c.begin(), c.end());
1316   }
1317 
1318   SmallVector(std::initializer_list<T> IL) : SmallVectorImpl<T>(N) {
1319     this->assign(IL);
1320   }
1321 
1322   SmallVector(const SmallVector& RHS) : SmallVectorImpl<T>(N) {
1323     if (!RHS.empty())
1324       SmallVectorImpl<T>::operator=(RHS);
1325   }
1326 
1327   SmallVector& operator=(const SmallVector& RHS) {
1328     SmallVectorImpl<T>::operator=(RHS);
1329     return *this;
1330   }
1331 
1332   SmallVector(SmallVector&& RHS) noexcept(
1333       std::is_nothrow_move_assignable_v<SmallVectorImpl<T>>)
1334       : SmallVectorImpl<T>(N) {
1335     if (!RHS.empty())
1336       SmallVectorImpl<T>::operator=(::std::move(RHS));
1337   }
1338 
1339   // note: The enable_if restricts Container to types that have a .begin() and
1340   // .end() that return valid input iterators.
1341   template <
1342       typename Container,
1343       std::enable_if_t<
1344           std::is_convertible_v<
1345               typename std::iterator_traits<
1346                   decltype(std::declval<Container>()
1347                                .begin())>::iterator_category,
1348               std::input_iterator_tag> &&
1349               std::is_convertible_v<
1350                   typename std::iterator_traits<
1351                       decltype(std::declval<Container>()
1352                                    .end())>::iterator_category,
1353                   std::input_iterator_tag>,
1354           int> = 0>
1355   SmallVector& operator=(const Container& RHS) {
1356     this->assign(RHS.begin(), RHS.end());
1357     return *this;
1358   }
1359 
1360   SmallVector(SmallVectorImpl<T>&& RHS) noexcept(
1361       std::is_nothrow_move_assignable_v<SmallVectorImpl<T>>)
1362       : SmallVectorImpl<T>(N) {
1363     if (!RHS.empty())
1364       SmallVectorImpl<T>::operator=(::std::move(RHS));
1365   }
1366 
1367   SmallVector& operator=(SmallVector&& RHS) noexcept(
1368       std::is_nothrow_move_assignable_v<SmallVectorImpl<T>>) {
1369     SmallVectorImpl<T>::operator=(::std::move(RHS));
1370     return *this;
1371   }
1372 
1373   SmallVector& operator=(SmallVectorImpl<T>&& RHS) noexcept(
1374       std::is_nothrow_move_constructible_v<SmallVectorImpl<T>>) {
1375     SmallVectorImpl<T>::operator=(::std::move(RHS));
1376     return *this;
1377   }
1378 
1379   // note: The enable_if restricts Container to types that have a .begin() and
1380   // .end() that return valid input iterators.
1381   template <
1382       typename Container,
1383       std::enable_if_t<
1384           std::is_convertible_v<
1385               typename std::iterator_traits<
1386                   decltype(std::declval<Container>()
1387                                .begin())>::iterator_category,
1388               std::input_iterator_tag> &&
1389               std::is_convertible_v<
1390                   typename std::iterator_traits<
1391                       decltype(std::declval<Container>()
1392                                    .end())>::iterator_category,
1393                   std::input_iterator_tag>,
1394           int> = 0>
1395   // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
1396   SmallVector& operator=(Container&& C) {
1397     this->assign(C.begin(), C.end());
1398     return *this;
1399   }
1400 
1401   SmallVector& operator=(std::initializer_list<T> IL) {
1402     this->assign(IL);
1403     return *this;
1404   }
1405 };
1406 
1407 template <typename T, unsigned N>
1408 inline size_t capacity_in_bytes(const SmallVector<T, N>& X) {
1409   return X.capacity_in_bytes();
1410 }
1411 
1412 template <typename T, unsigned N>
1413 std::ostream& operator<<(std::ostream& out, const SmallVector<T, N>& list) {
1414   int i = 0;
1415   out << "[";
1416   for (auto e : list) {
1417     if (i++ > 0)
1418       out << ", ";
1419     out << e;
1420   }
1421   out << "]";
1422   return out;
1423 }
1424 
1425 template <typename RangeType>
1426 using ValueTypeFromRangeType = std::remove_const_t<
1427     std::remove_reference_t<decltype(*std::begin(std::declval<RangeType&>()))>>;
1428 
1429 /// Given a range of type R, iterate the entire range and return a
1430 /// SmallVector with elements of the vector.  This is useful, for example,
1431 /// when you want to iterate a range and then sort the results.
1432 template <unsigned Size, typename R>
1433 // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
1434 SmallVector<ValueTypeFromRangeType<R>, Size> to_vector(R&& Range) {
1435   return {std::begin(Range), std::end(Range)};
1436 }
1437 template <typename R>
1438 SmallVector<
1439     ValueTypeFromRangeType<R>,
1440     CalculateSmallVectorDefaultInlinedElements<
1441         ValueTypeFromRangeType<R>>::value>
1442 // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward)
1443 to_vector(R&& Range) {
1444   return {std::begin(Range), std::end(Range)};
1445 }
1446 
1447 } // end namespace c10
1448 
1449 namespace std {
1450 
1451 /// Implement std::swap in terms of SmallVector swap.
1452 template <typename T>
1453 inline void swap(
1454     c10::SmallVectorImpl<T>& LHS,
1455     c10::SmallVectorImpl<T>& RHS) noexcept {
1456   LHS.swap(RHS);
1457 }
1458 
1459 /// Implement std::swap in terms of SmallVector swap.
1460 template <typename T, unsigned N>
1461 inline void swap(
1462     c10::SmallVector<T, N>& LHS,
1463     c10::SmallVector<T, N>& RHS) noexcept {
1464   LHS.swap(RHS);
1465 }
1466 
1467 } // end namespace std
1468