[llvm] [ADT] Always use 32-bit size type for SmallVector with 16-bit elements (PR #95536)
Jay Foad via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 26 08:46:36 PDT 2024
https://github.com/jayfoad updated https://github.com/llvm/llvm-project/pull/95536
>From 01d0beab64ec3b116af082cf3c8425915f4e82b4 Mon Sep 17 00:00:00 2001
From: Jay Foad <jay.foad at amd.com>
Date: Fri, 14 Jun 2024 13:58:27 +0100
Subject: [PATCH 1/3] [ADT] Always use 32-bit size type for SmallVector with
16-bit elements
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.
---
llvm/include/llvm/ADT/SmallVector.h | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h
index 09676d792dfeb..d7e889cc7b1e4 100644
--- a/llvm/include/llvm/ADT/SmallVector.h
+++ b/llvm/include/llvm/ADT/SmallVector.h
@@ -116,8 +116,7 @@ template <class Size_T> class SmallVectorBase {
template <class T>
using SmallVectorSizeType =
- std::conditional_t<sizeof(T) < 4 && sizeof(void *) >= 8, uint64_t,
- uint32_t>;
+ std::conditional_t<sizeof(T) == 1, uintptr_t, uint32_t>;
/// Figure out the offset of the first element.
template <class T, typename = void> struct SmallVectorAlignmentAndSize {
>From d025afacff02feda70d492dcb5d070022ffa006f Mon Sep 17 00:00:00 2001
From: Jay Foad <jay.foad at gmail.com>
Date: Fri, 14 Jun 2024 14:43:24 +0100
Subject: [PATCH 2/3] Update llvm/include/llvm/ADT/SmallVector.h
Co-authored-by: Nikita Popov <github at npopov.com>
---
llvm/include/llvm/ADT/SmallVector.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/include/llvm/ADT/SmallVector.h b/llvm/include/llvm/ADT/SmallVector.h
index d7e889cc7b1e4..db34b16ecf9e7 100644
--- a/llvm/include/llvm/ADT/SmallVector.h
+++ b/llvm/include/llvm/ADT/SmallVector.h
@@ -116,7 +116,7 @@ template <class Size_T> class SmallVectorBase {
template <class T>
using SmallVectorSizeType =
- std::conditional_t<sizeof(T) == 1, uintptr_t, uint32_t>;
+ std::conditional_t<sizeof(T) == 1, size_t, uint32_t>;
/// Figure out the offset of the first element.
template <class T, typename = void> struct SmallVectorAlignmentAndSize {
>From 04d78ce1acd965cf75ca2e94eaeeb16660c3c44d Mon Sep 17 00:00:00 2001
From: Jay Foad <jay.foad at amd.com>
Date: Sat, 15 Jun 2024 09:14:52 +0100
Subject: [PATCH 3/3] Add static_assert
---
llvm/lib/Support/SmallVector.cpp | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Support/SmallVector.cpp b/llvm/lib/Support/SmallVector.cpp
index b6ce37842040b..e77b747984173 100644
--- a/llvm/lib/Support/SmallVector.cpp
+++ b/llvm/lib/Support/SmallVector.cpp
@@ -37,9 +37,7 @@ struct Struct32B {
#pragma GCC diagnostic pop
#endif
}
-static_assert(sizeof(SmallVector<void *, 0>) ==
- sizeof(unsigned) * 2 + sizeof(void *),
- "wasted space in SmallVector size 0");
+
static_assert(alignof(SmallVector<Struct16B, 0>) >= alignof(Struct16B),
"wrong alignment for 16-byte aligned T");
static_assert(alignof(SmallVector<Struct32B, 0>) >= alignof(Struct32B),
@@ -48,13 +46,19 @@ static_assert(sizeof(SmallVector<Struct16B, 0>) >= alignof(Struct16B),
"missing padding for 16-byte aligned T");
static_assert(sizeof(SmallVector<Struct32B, 0>) >= alignof(Struct32B),
"missing padding for 32-byte aligned T");
+
+static_assert(sizeof(SmallVector<void *, 0>) ==
+ sizeof(unsigned) * 2 + sizeof(void *),
+ "wasted space in SmallVector size 0");
static_assert(sizeof(SmallVector<void *, 1>) ==
sizeof(unsigned) * 2 + sizeof(void *) * 2,
"wasted space in SmallVector size 1");
-
static_assert(sizeof(SmallVector<char, 0>) ==
sizeof(void *) * 2 + sizeof(void *),
"1 byte elements have word-sized type for size and capacity");
+static_assert(sizeof(SmallVector<int16_t, 0>) ==
+ sizeof(unsigned) * 2 + sizeof(void *),
+ "2 byte elements have 32-bit type for size and capacity");
/// Report that MinSize doesn't fit into this vector's size type. Throws
/// std::length_error or calls report_fatal_error.
More information about the llvm-commits
mailing list