Skip to content

Commit cac7821

Browse files
authored
[compiler-rt] Replace deprecated aligned_storage with aligned byte array (#94171)
`std::aligned_storage` is deprecated with C++23, see [here](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p1413r3.pdf). This replaces the usages of `std::aligned_storage` within compiler-rt with an aligned `std::byte` array. I will provide patches for other subcomponents as well.
1 parent 25506f4 commit cac7821

11 files changed

+50
-71
lines changed

compiler-rt/lib/xray/tests/unit/function_call_trie_test.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,16 +310,14 @@ TEST(FunctionCallTrieTest, MergeInto) {
310310

311311
TEST(FunctionCallTrieTest, PlacementNewOnAlignedStorage) {
312312
profilingFlags()->setDefaults();
313-
typename std::aligned_storage<sizeof(FunctionCallTrie::Allocators),
314-
alignof(FunctionCallTrie::Allocators)>::type
315-
AllocatorsStorage;
313+
alignas(FunctionCallTrie::Allocators)
314+
std::byte AllocatorsStorage[sizeof(FunctionCallTrie::Allocators)];
316315
new (&AllocatorsStorage)
317316
FunctionCallTrie::Allocators(FunctionCallTrie::InitAllocators());
318317
auto *A =
319318
reinterpret_cast<FunctionCallTrie::Allocators *>(&AllocatorsStorage);
320319

321-
typename std::aligned_storage<sizeof(FunctionCallTrie),
322-
alignof(FunctionCallTrie)>::type FCTStorage;
320+
alignas(FunctionCallTrie) std::byte FCTStorage[sizeof(FunctionCallTrie)];
323321
new (&FCTStorage) FunctionCallTrie(*A);
324322
auto *T = reinterpret_cast<FunctionCallTrie *>(&FCTStorage);
325323

compiler-rt/lib/xray/tests/unit/profile_collector_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ struct ExpectedProfilingFileHeader {
3838
void ValidateFileHeaderBlock(XRayBuffer B) {
3939
ASSERT_NE(static_cast<const void *>(B.Data), nullptr);
4040
ASSERT_EQ(B.Size, sizeof(ExpectedProfilingFileHeader));
41-
typename std::aligned_storage<sizeof(ExpectedProfilingFileHeader)>::type
42-
FileHeaderStorage;
41+
alignas(ExpectedProfilingFileHeader)
42+
std::byte FileHeaderStorage[sizeof(ExpectedProfilingFileHeader)];
4343
ExpectedProfilingFileHeader ExpectedHeader;
4444
std::memcpy(&FileHeaderStorage, B.Data, B.Size);
4545
auto &FileHeader =

compiler-rt/lib/xray/tests/unit/segmented_array_test.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,11 @@ TEST(SegmentedArrayTest, SimulateStackBehaviour) {
226226

227227
TEST(SegmentedArrayTest, PlacementNewOnAlignedStorage) {
228228
using AllocatorType = typename Array<ShadowStackEntry>::AllocatorType;
229-
typename std::aligned_storage<sizeof(AllocatorType),
230-
alignof(AllocatorType)>::type AllocatorStorage;
229+
alignas(AllocatorType) std::byte AllocatorStorage[sizeof(AllocatorType)];
231230
new (&AllocatorStorage) AllocatorType(1 << 10);
232231
auto *A = reinterpret_cast<AllocatorType *>(&AllocatorStorage);
233-
typename std::aligned_storage<sizeof(Array<ShadowStackEntry>),
234-
alignof(Array<ShadowStackEntry>)>::type
235-
ArrayStorage;
232+
alignas(Array<ShadowStackEntry>)
233+
std::byte ArrayStorage[sizeof(Array<ShadowStackEntry>)];
236234
new (&ArrayStorage) Array<ShadowStackEntry>(*A);
237235
auto *Data = reinterpret_cast<Array<ShadowStackEntry> *>(&ArrayStorage);
238236

compiler-rt/lib/xray/tests/unit/test_helpers.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ namespace __xray {
6969

7070
std::string serialize(BufferQueue &Buffers, int32_t Version) {
7171
std::string Serialized;
72-
std::aligned_storage<sizeof(XRayFileHeader), alignof(XRayFileHeader)>::type
73-
HeaderStorage;
72+
alignas(XRayFileHeader) std::byte HeaderStorage[sizeof(XRayFileHeader)];
7473
auto *Header = reinterpret_cast<XRayFileHeader *>(&HeaderStorage);
7574
new (Header) XRayFileHeader();
7675
Header->Version = Version;

compiler-rt/lib/xray/xray_fdr_logging.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,12 @@ struct XRAY_TLS_ALIGNAS(64) ThreadLocalData {
5555
BufferQueue::Buffer Buffer{};
5656
BufferQueue *BQ = nullptr;
5757

58-
using LogWriterStorage =
59-
typename std::aligned_storage<sizeof(FDRLogWriter),
60-
alignof(FDRLogWriter)>::type;
61-
62-
LogWriterStorage LWStorage;
58+
using LogWriterStorage = std::byte[sizeof(FDRLogWriter)];
59+
alignas(FDRLogWriter) LogWriterStorage LWStorage;
6360
FDRLogWriter *Writer = nullptr;
6461

65-
using ControllerStorage =
66-
typename std::aligned_storage<sizeof(FDRController<>),
67-
alignof(FDRController<>)>::type;
68-
ControllerStorage CStorage;
62+
using ControllerStorage = std::byte[sizeof(FDRController<>)];
63+
alignas(FDRController<>) ControllerStorage CStorage;
6964
FDRController<> *Controller = nullptr;
7065
};
7166

@@ -78,7 +73,7 @@ static_assert(std::is_trivially_destructible<ThreadLocalData>::value,
7873
static pthread_key_t Key;
7974

8075
// Global BufferQueue.
81-
static std::aligned_storage<sizeof(BufferQueue)>::type BufferQueueStorage;
76+
static std::byte BufferQueueStorage[sizeof(BufferQueue)];
8277
static BufferQueue *BQ = nullptr;
8378

8479
// Global thresholds for function durations.
@@ -129,8 +124,8 @@ static_assert(alignof(ThreadLocalData) >= 64,
129124
"ThreadLocalData must be cache line aligned.");
130125
#endif
131126
static ThreadLocalData &getThreadLocalData() {
132-
thread_local typename std::aligned_storage<
133-
sizeof(ThreadLocalData), alignof(ThreadLocalData)>::type TLDStorage{};
127+
alignas(ThreadLocalData) thread_local std::byte
128+
TLDStorage[sizeof(ThreadLocalData)];
134129

135130
if (pthread_getspecific(Key) == NULL) {
136131
new (reinterpret_cast<ThreadLocalData *>(&TLDStorage)) ThreadLocalData{};

compiler-rt/lib/xray/xray_function_call_trie.h

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,14 @@ class FunctionCallTrie {
139139

140140
// Use hosted aligned storage members to allow for trivial move and init.
141141
// This also allows us to sidestep the potential-failing allocation issue.
142-
typename std::aligned_storage<sizeof(NodeAllocatorType),
143-
alignof(NodeAllocatorType)>::type
144-
NodeAllocatorStorage;
145-
typename std::aligned_storage<sizeof(RootAllocatorType),
146-
alignof(RootAllocatorType)>::type
147-
RootAllocatorStorage;
148-
typename std::aligned_storage<sizeof(ShadowStackAllocatorType),
149-
alignof(ShadowStackAllocatorType)>::type
150-
ShadowStackAllocatorStorage;
151-
typename std::aligned_storage<sizeof(NodeIdPairAllocatorType),
152-
alignof(NodeIdPairAllocatorType)>::type
153-
NodeIdPairAllocatorStorage;
142+
alignas(NodeAllocatorType) std::byte
143+
NodeAllocatorStorage[sizeof(NodeAllocatorType)];
144+
alignas(RootAllocatorType) std::byte
145+
RootAllocatorStorage[sizeof(RootAllocatorType)];
146+
alignas(ShadowStackAllocatorType) std::byte
147+
ShadowStackAllocatorStorage[sizeof(ShadowStackAllocatorType)];
148+
alignas(NodeIdPairAllocatorType) std::byte
149+
NodeIdPairAllocatorStorage[sizeof(NodeIdPairAllocatorType)];
154150

155151
NodeAllocatorType *NodeAllocator = nullptr;
156152
RootAllocatorType *RootAllocator = nullptr;

compiler-rt/lib/xray/xray_profile_collector.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace {
2929
SpinMutex GlobalMutex;
3030
struct ThreadTrie {
3131
tid_t TId;
32-
typename std::aligned_storage<sizeof(FunctionCallTrie)>::type TrieStorage;
32+
alignas(FunctionCallTrie) std::byte TrieStorage[sizeof(FunctionCallTrie)];
3333
};
3434

3535
struct ProfileBuffer {
@@ -71,16 +71,13 @@ using ThreadDataAllocator = ThreadDataArray::AllocatorType;
7171
// by the ThreadData array. This lets us host the buffers, allocators, and tries
7272
// associated with a thread by moving the data into the array instead of
7373
// attempting to copy the data to a separately backed set of tries.
74-
static typename std::aligned_storage<
75-
sizeof(BufferQueue), alignof(BufferQueue)>::type BufferQueueStorage;
74+
alignas(BufferQueue) static std::byte BufferQueueStorage[sizeof(BufferQueue)];
7675
static BufferQueue *BQ = nullptr;
7776
static BufferQueue::Buffer Buffer;
78-
static typename std::aligned_storage<sizeof(ThreadDataAllocator),
79-
alignof(ThreadDataAllocator)>::type
80-
ThreadDataAllocatorStorage;
81-
static typename std::aligned_storage<sizeof(ThreadDataArray),
82-
alignof(ThreadDataArray)>::type
83-
ThreadDataArrayStorage;
77+
alignas(ThreadDataAllocator) static std::byte
78+
ThreadDataAllocatorStorage[sizeof(ThreadDataAllocator)];
79+
alignas(ThreadDataArray) static std::byte
80+
ThreadDataArrayStorage[sizeof(ThreadDataArray)];
8481

8582
static ThreadDataAllocator *TDAllocator = nullptr;
8683
static ThreadDataArray *TDArray = nullptr;
@@ -91,10 +88,10 @@ using ProfileBufferArrayAllocator = typename ProfileBufferArray::AllocatorType;
9188
// These need to be global aligned storage to avoid dynamic initialization. We
9289
// need these to be aligned to allow us to placement new objects into the
9390
// storage, and have pointers to those objects be appropriately aligned.
94-
static typename std::aligned_storage<sizeof(ProfileBufferArray)>::type
95-
ProfileBuffersStorage;
96-
static typename std::aligned_storage<sizeof(ProfileBufferArrayAllocator)>::type
97-
ProfileBufferArrayAllocatorStorage;
91+
alignas(ProfileBufferArray) static std::byte
92+
ProfileBuffersStorage[sizeof(ProfileBufferArray)];
93+
alignas(ProfileBufferArrayAllocator) static std::byte
94+
ProfileBufferArrayAllocatorStorage[sizeof(ProfileBufferArrayAllocator)];
9895

9996
static ProfileBufferArrayAllocator *ProfileBuffersAllocator = nullptr;
10097
static ProfileBufferArray *ProfileBuffers = nullptr;
@@ -382,8 +379,8 @@ XRayBuffer nextBuffer(XRayBuffer B) XRAY_NEVER_INSTRUMENT {
382379
return {nullptr, 0};
383380

384381
static pthread_once_t Once = PTHREAD_ONCE_INIT;
385-
static typename std::aligned_storage<sizeof(XRayProfilingFileHeader)>::type
386-
FileHeaderStorage;
382+
alignas(XRayProfilingFileHeader) static std::byte
383+
FileHeaderStorage[sizeof(XRayProfilingFileHeader)];
387384
pthread_once(
388385
&Once, +[]() XRAY_NEVER_INSTRUMENT {
389386
new (&FileHeaderStorage) XRayProfilingFileHeader{};

compiler-rt/lib/xray/xray_profiling.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,14 @@ static pthread_key_t ProfilingKey;
4848

4949
// We use a global buffer queue, which gets initialized once at initialisation
5050
// time, and gets reset when profiling is "done".
51-
static std::aligned_storage<sizeof(BufferQueue), alignof(BufferQueue)>::type
52-
BufferQueueStorage;
51+
alignas(BufferQueue) static std::byte BufferQueueStorage[sizeof(BufferQueue)];
5352
static BufferQueue *BQ = nullptr;
5453

5554
thread_local FunctionCallTrie::Allocators::Buffers ThreadBuffers;
56-
thread_local std::aligned_storage<sizeof(FunctionCallTrie::Allocators),
57-
alignof(FunctionCallTrie::Allocators)>::type
58-
AllocatorsStorage;
59-
thread_local std::aligned_storage<sizeof(FunctionCallTrie),
60-
alignof(FunctionCallTrie)>::type
61-
FunctionCallTrieStorage;
55+
alignas(FunctionCallTrie::Allocators) thread_local std::byte
56+
AllocatorsStorage[sizeof(FunctionCallTrie::Allocators)];
57+
alignas(FunctionCallTrie) thread_local std::byte
58+
FunctionCallTrieStorage[sizeof(FunctionCallTrie)];
6259
thread_local ProfilingData TLD{{0}, {0}};
6360
thread_local atomic_uint8_t ReentranceGuard{0};
6461

compiler-rt/lib/xray/xray_segmented_array.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ template <class T> class Array {
5656
// kCacheLineSize-multiple segments, minus the size of two pointers.
5757
//
5858
// - Request cacheline-multiple sized elements from the allocator.
59-
static constexpr uint64_t AlignedElementStorageSize =
60-
sizeof(typename std::aligned_storage<sizeof(T), alignof(T)>::type);
59+
static constexpr uint64_t AlignedElementStorageSize = sizeof(T);
6160

6261
static constexpr uint64_t SegmentControlBlockSize = sizeof(Segment *) * 2;
6362

compiler-rt/test/tsan/custom_mutex4.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// RUN: %clangxx_tsan -O1 --std=c++11 %s -o %t && %run %t 2>&1 | FileCheck %s
1+
// RUN: %clangxx_tsan -O1 --std=c++17 %s -o %t && %run %t 2>&1 | FileCheck %s
22
#include "custom_mutex.h"
33

4-
#include <type_traits>
4+
#include <cstddef>
55

66
// Test that the destruction events of a mutex are ignored when the
77
// annotations request this.
@@ -12,14 +12,14 @@
1212
// has run.
1313

1414
int main() {
15-
std::aligned_storage<sizeof(Mutex), alignof(Mutex)>::type mu1_store;
15+
alignas(Mutex) std::byte mu1_store[sizeof(Mutex)];
1616
Mutex* mu1 = reinterpret_cast<Mutex*>(&mu1_store);
1717
new(&mu1_store) Mutex(false, __tsan_mutex_linker_init);
1818
mu1->Lock();
1919
mu1->~Mutex();
2020
mu1->Unlock();
2121

22-
std::aligned_storage<sizeof(Mutex), alignof(Mutex)>::type mu2_store;
22+
alignas(Mutex) std::byte mu2_store[sizeof(Mutex)];
2323
Mutex* mu2 = reinterpret_cast<Mutex*>(&mu2_store);
2424
new(&mu2_store) Mutex(false, 0, __tsan_mutex_not_static);
2525
mu2->Lock();

compiler-rt/test/tsan/custom_mutex5.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
// RUN: %clangxx_tsan -O1 --std=c++11 %s -o %t && %deflake %run %t 2>&1 | FileCheck %s
1+
// RUN: %clangxx_tsan -O1 --std=c++17 %s -o %t && %deflake %run %t 2>&1 | FileCheck %s
22
#include "custom_mutex.h"
33

4-
#include <type_traits>
4+
#include <cstddef>
55

66
// Test that we detect the destruction of an in-use mutex when the
77
// thread annotations don't otherwise disable the check.
88

99
int main() {
10-
std::aligned_storage<sizeof(Mutex), alignof(Mutex)>::type mu1_store;
10+
alignas(Mutex) std::byte mu1_store[sizeof(Mutex)];
1111
Mutex* mu1 = reinterpret_cast<Mutex*>(&mu1_store);
1212
new(&mu1_store) Mutex(false, 0);
1313
mu1->Lock();
1414
mu1->~Mutex();
1515
mu1->Unlock();
1616

17-
std::aligned_storage<sizeof(Mutex), alignof(Mutex)>::type mu2_store;
17+
alignas(Mutex) std::byte mu2_store[sizeof(Mutex)];
1818
Mutex* mu2 = reinterpret_cast<Mutex*>(&mu2_store);
1919
new(&mu2_store)
2020
Mutex(false, __tsan_mutex_not_static, __tsan_mutex_not_static);

0 commit comments

Comments
 (0)