Skip to content

Commit 61674f2

Browse files
jayfoadnikic
authored andcommitted
[ADT] Always use 32-bit size type for SmallVector with 16-bit elements (#95536)
`SmallVector` has a special case to allow vector of char to exceed 4 GB in size on 64-bit hosts. Apply this special case only for 8-bit element types, instead of all element types < 32 bits. This makes `SmallVector<MCPhysReg>` more compact because `MCPhysReg` is `uint16_t`. --------- Co-authored-by: Nikita Popov <[email protected]>
1 parent e15d28d commit 61674f2

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

llvm/include/llvm/ADT/SmallVector.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ template <class Size_T> class SmallVectorBase {
116116

117117
template <class T>
118118
using SmallVectorSizeType =
119-
std::conditional_t<sizeof(T) < 4 && sizeof(void *) >= 8, uint64_t,
120-
uint32_t>;
119+
std::conditional_t<sizeof(T) == 1, size_t, uint32_t>;
121120

122121
/// Figure out the offset of the first element.
123122
template <class T, typename = void> struct SmallVectorAlignmentAndSize {

llvm/lib/Support/SmallVector.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ struct Struct32B {
3737
#pragma GCC diagnostic pop
3838
#endif
3939
}
40-
static_assert(sizeof(SmallVector<void *, 0>) ==
41-
sizeof(unsigned) * 2 + sizeof(void *),
42-
"wasted space in SmallVector size 0");
40+
4341
static_assert(alignof(SmallVector<Struct16B, 0>) >= alignof(Struct16B),
4442
"wrong alignment for 16-byte aligned T");
4543
static_assert(alignof(SmallVector<Struct32B, 0>) >= alignof(Struct32B),
@@ -48,13 +46,19 @@ static_assert(sizeof(SmallVector<Struct16B, 0>) >= alignof(Struct16B),
4846
"missing padding for 16-byte aligned T");
4947
static_assert(sizeof(SmallVector<Struct32B, 0>) >= alignof(Struct32B),
5048
"missing padding for 32-byte aligned T");
49+
50+
static_assert(sizeof(SmallVector<void *, 0>) ==
51+
sizeof(unsigned) * 2 + sizeof(void *),
52+
"wasted space in SmallVector size 0");
5153
static_assert(sizeof(SmallVector<void *, 1>) ==
5254
sizeof(unsigned) * 2 + sizeof(void *) * 2,
5355
"wasted space in SmallVector size 1");
54-
5556
static_assert(sizeof(SmallVector<char, 0>) ==
5657
sizeof(void *) * 2 + sizeof(void *),
5758
"1 byte elements have word-sized type for size and capacity");
59+
static_assert(sizeof(SmallVector<int16_t, 0>) ==
60+
sizeof(unsigned) * 2 + sizeof(void *),
61+
"2 byte elements have 32-bit type for size and capacity");
5862

5963
/// Report that MinSize doesn't fit into this vector's size type. Throws
6064
/// std::length_error or calls report_fatal_error.

0 commit comments

Comments
 (0)