James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 1 | From ef26f059859d3a0d08b06a70519928bcd5f9bb79 Mon Sep 17 00:00:00 2001 |
| 2 | From: PJ Reiniger <pj.reiniger@gmail.com> |
| 3 | Date: Thu, 5 May 2022 23:18:34 -0400 |
| 4 | Subject: [PATCH 11/28] Detemplatize SmallVectorBase |
| 5 | |
| 6 | --- |
| 7 | llvm/include/llvm/ADT/SmallVector.h | 21 +++++++----------- |
| 8 | llvm/lib/Support/SmallVector.cpp | 34 +++++------------------------ |
| 9 | 2 files changed, 13 insertions(+), 42 deletions(-) |
| 10 | |
| 11 | diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h |
| 12 | index 1e311ea56..4b6bbdeb2 100644 |
| 13 | --- a/llvm/include/llvm/ADT/SmallVector.h |
| 14 | +++ b/llvm/include/llvm/ADT/SmallVector.h |
| 15 | @@ -50,14 +50,14 @@ template <typename IteratorT> class iterator_range; |
| 16 | /// Using 64 bit size is desirable for cases like SmallVector<char>, where a |
| 17 | /// 32 bit size would limit the vector to ~4GB. SmallVectors are used for |
| 18 | /// buffering bitcode output - which can exceed 4GB. |
| 19 | -template <class Size_T> class SmallVectorBase { |
| 20 | +class SmallVectorBase { |
| 21 | protected: |
| 22 | void *BeginX; |
| 23 | - Size_T Size = 0, Capacity; |
| 24 | + unsigned Size = 0, Capacity; |
| 25 | |
| 26 | /// The maximum value of the Size_T used. |
| 27 | static constexpr size_t SizeTypeMax() { |
| 28 | - return (std::numeric_limits<Size_T>::max)(); |
| 29 | + return (std::numeric_limits<unsigned>::max)(); |
| 30 | } |
| 31 | |
| 32 | SmallVectorBase() = delete; |
| 33 | @@ -91,15 +91,10 @@ protected: |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | -template <class T> |
| 38 | -using SmallVectorSizeType = |
| 39 | - typename std::conditional<sizeof(T) < 4 && sizeof(void *) >= 8, uint64_t, |
| 40 | - uint32_t>::type; |
| 41 | - |
| 42 | /// Figure out the offset of the first element. |
| 43 | template <class T, typename = void> struct SmallVectorAlignmentAndSize { |
| 44 | - alignas(SmallVectorBase<SmallVectorSizeType<T>>) char Base[sizeof( |
| 45 | - SmallVectorBase<SmallVectorSizeType<T>>)]; |
| 46 | + alignas(SmallVectorBase) char Base[sizeof( |
| 47 | + SmallVectorBase)]; |
| 48 | alignas(T) char FirstEl[sizeof(T)]; |
| 49 | }; |
| 50 | |
| 51 | @@ -108,8 +103,8 @@ template <class T, typename = void> struct SmallVectorAlignmentAndSize { |
| 52 | /// to avoid unnecessarily requiring T to be complete. |
| 53 | template <typename T, typename = void> |
| 54 | class SmallVectorTemplateCommon |
| 55 | - : public SmallVectorBase<SmallVectorSizeType<T>> { |
| 56 | - using Base = SmallVectorBase<SmallVectorSizeType<T>>; |
| 57 | + : public SmallVectorBase { |
| 58 | + using Base = SmallVectorBase; |
| 59 | |
| 60 | /// Find the address of the first element. For this pointer math to be valid |
| 61 | /// with small-size of 0 for T with lots of alignment, it's important that |
| 62 | @@ -356,7 +351,7 @@ protected: |
| 63 | /// in \p NewCapacity. This is the first section of \a grow(). |
| 64 | T *mallocForGrow(size_t MinSize, size_t &NewCapacity) { |
| 65 | return static_cast<T *>( |
| 66 | - SmallVectorBase<SmallVectorSizeType<T>>::mallocForGrow( |
| 67 | + SmallVectorBase::mallocForGrow( |
| 68 | MinSize, sizeof(T), NewCapacity)); |
| 69 | } |
| 70 | |
| 71 | diff --git a/llvm/lib/Support/SmallVector.cpp b/llvm/lib/Support/SmallVector.cpp |
| 72 | index a2b4899e1..bdfc963d7 100644 |
| 73 | --- a/llvm/lib/Support/SmallVector.cpp |
| 74 | +++ b/llvm/lib/Support/SmallVector.cpp |
| 75 | @@ -51,10 +51,6 @@ static_assert(sizeof(SmallVector<void *, 1>) == |
| 76 | sizeof(unsigned) * 2 + sizeof(void *) * 2, |
| 77 | "wasted space in SmallVector size 1"); |
| 78 | |
| 79 | -static_assert(sizeof(SmallVector<char, 0>) == |
| 80 | - sizeof(void *) * 2 + sizeof(void *), |
| 81 | - "1 byte elements have word-sized type for size and capacity"); |
| 82 | - |
| 83 | /// Report that MinSize doesn't fit into this vector's size type. Throws |
| 84 | /// std::length_error or calls report_fatal_error. |
| 85 | [[noreturn]] static void report_size_overflow(size_t MinSize, size_t MaxSize); |
| 86 | @@ -85,9 +81,8 @@ static void report_at_maximum_capacity(size_t MaxSize) { |
| 87 | } |
| 88 | |
| 89 | // Note: Moving this function into the header may cause performance regression. |
| 90 | -template <class Size_T> |
| 91 | static size_t getNewCapacity(size_t MinSize, size_t TSize, size_t OldCapacity) { |
| 92 | - constexpr size_t MaxSize = std::numeric_limits<Size_T>::max(); |
| 93 | + constexpr size_t MaxSize = std::numeric_limits<unsigned>::max(); |
| 94 | |
| 95 | // Ensure we can fit the new capacity. |
| 96 | // This is only going to be applicable when the capacity is 32 bit. |
| 97 | @@ -108,18 +103,16 @@ static size_t getNewCapacity(size_t MinSize, size_t TSize, size_t OldCapacity) { |
| 98 | } |
| 99 | |
| 100 | // Note: Moving this function into the header may cause performance regression. |
| 101 | -template <class Size_T> |
| 102 | -void *SmallVectorBase<Size_T>::mallocForGrow(size_t MinSize, size_t TSize, |
| 103 | +void *SmallVectorBase::mallocForGrow(size_t MinSize, size_t TSize, |
| 104 | size_t &NewCapacity) { |
| 105 | - NewCapacity = getNewCapacity<Size_T>(MinSize, TSize, this->capacity()); |
| 106 | + NewCapacity = getNewCapacity(MinSize, TSize, this->capacity()); |
| 107 | return llvm::safe_malloc(NewCapacity * TSize); |
| 108 | } |
| 109 | |
| 110 | // Note: Moving this function into the header may cause performance regression. |
| 111 | -template <class Size_T> |
| 112 | -void SmallVectorBase<Size_T>::grow_pod(void *FirstEl, size_t MinSize, |
| 113 | +void SmallVectorBase::grow_pod(void *FirstEl, size_t MinSize, |
| 114 | size_t TSize) { |
| 115 | - size_t NewCapacity = getNewCapacity<Size_T>(MinSize, TSize, this->capacity()); |
| 116 | + size_t NewCapacity = getNewCapacity(MinSize, TSize, this->capacity()); |
| 117 | void *NewElts; |
| 118 | if (BeginX == FirstEl) { |
| 119 | NewElts = safe_malloc(NewCapacity * TSize); |
| 120 | @@ -134,20 +127,3 @@ void SmallVectorBase<Size_T>::grow_pod(void *FirstEl, size_t MinSize, |
| 121 | this->BeginX = NewElts; |
| 122 | this->Capacity = NewCapacity; |
| 123 | } |
| 124 | - |
| 125 | -template class llvm::SmallVectorBase<uint32_t>; |
| 126 | - |
| 127 | -// Disable the uint64_t instantiation for 32-bit builds. |
| 128 | -// Both uint32_t and uint64_t instantiations are needed for 64-bit builds. |
| 129 | -// This instantiation will never be used in 32-bit builds, and will cause |
| 130 | -// warnings when sizeof(Size_T) > sizeof(size_t). |
| 131 | -#if SIZE_MAX > UINT32_MAX |
| 132 | -template class llvm::SmallVectorBase<uint64_t>; |
| 133 | - |
| 134 | -// Assertions to ensure this #if stays in sync with SmallVectorSizeType. |
| 135 | -static_assert(sizeof(SmallVectorSizeType<char>) == sizeof(uint64_t), |
| 136 | - "Expected SmallVectorBase<uint64_t> variant to be in use."); |
| 137 | -#else |
| 138 | -static_assert(sizeof(SmallVectorSizeType<char>) == sizeof(uint32_t), |
| 139 | - "Expected SmallVectorBase<uint32_t> variant to be in use."); |
| 140 | -#endif |