Skip to content

Commit c1554f6

Browse files
authored
Merge pull request #58499 from apple/jgrynspan/58498-remove-aligned_alloc-template
Revert "runtime: allow over-aligned types in the runtime"
2 parents 857c578 + b749dd3 commit c1554f6

File tree

10 files changed

+16
-171
lines changed

10 files changed

+16
-171
lines changed

include/swift/Runtime/Atomic.h

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
#define SWIFT_RUNTIME_ATOMIC_H
1919

2020
#include "swift/Runtime/Config.h"
21-
#include "swift/Runtime/Heap.h"
22-
2321
#include <assert.h>
2422
#include <atomic>
2523
#if defined(_WIN64)
@@ -45,65 +43,13 @@
4543

4644
namespace swift {
4745
namespace impl {
48-
49-
// FIXME: why can we not use the definitions from Heap.h? It seems that we
50-
// would fail to collapse the structure down in that case and end up with size
51-
// differences.
52-
template <std::size_t Alignment_>
53-
struct requires_aligned_alloc {
54-
#if defined(__cpp_aligned_new)
55-
// If we have C++17 or newer we can use the alignment aware allocation
56-
// implicitly.
57-
static constexpr const bool value = false;
58-
#else
59-
#if defined(__STDCPP_DEFAULT_NEW_ALIGNMENT__)
60-
static constexpr const bool value =
61-
Alignment_ > std::alignment_of<std::max_align_t>::value &&
62-
Alignment_ > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
63-
#else
64-
static constexpr const bool value =
65-
Alignment_ > std::alignment_of<std::max_align_t>::value;
66-
#endif
67-
#endif
68-
};
69-
70-
template <std::size_t Alignment_,
71-
bool = requires_aligned_alloc<Alignment_>::value>
72-
struct aligned_alloc;
73-
74-
template <std::size_t Alignment_>
75-
struct aligned_alloc<Alignment_, false> {};
76-
77-
template <std::size_t Alignment_>
78-
struct aligned_alloc<Alignment_, true> {
79-
[[nodiscard]] void *operator new(std::size_t size) noexcept {
80-
#if defined(_WIN32)
81-
return _aligned_malloc(size, Alignment_);
82-
#else
83-
static_assert(Alignment_ >= sizeof(void *),
84-
"posix_memalign requires minimal alignment of pointer");
85-
void *ptr = nullptr;
86-
(void)posix_memalign(&ptr, Alignment_, size);
87-
return ptr;
88-
#endif
89-
}
90-
91-
void operator delete(void *ptr) noexcept {
92-
#if defined(_WIN32)
93-
_aligned_free(ptr);
94-
#else
95-
free(ptr);
96-
#endif
97-
}
98-
};
99-
10046
/// The default implementation for swift::atomic<T>, which just wraps
10147
/// std::atomic with minor differences.
10248
///
10349
/// TODO: should we make this use non-atomic operations when the runtime
10450
/// is single-threaded?
10551
template <class Value, size_t Size = sizeof(Value)>
106-
class alignas(Size) atomic_impl : public aligned_alloc<Size> {
52+
class alignas(Size) atomic_impl {
10753
std::atomic<Value> value;
10854
public:
10955
constexpr atomic_impl(Value value) : value(value) {}

include/swift/Runtime/Heap.h

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,11 @@
1818
#define SWIFT_RUNTIME_HEAP_H
1919

2020
#include <cstddef>
21-
#include <cstdint>
22-
#include <cstdlib>
2321
#include <new>
24-
#include <type_traits>
2522
#include <utility>
2623

2724
#include "swift/Runtime/Config.h"
2825

29-
#if defined(_WIN32)
30-
#include <malloc.h>
31-
#endif
32-
3326
namespace swift {
3427
// Allocate plain old memory. This is the generalized entry point
3528
// Never returns nil. The returned memory is uninitialized.
@@ -85,77 +78,6 @@ static inline void swift_cxx_deleteObject(T *ptr) {
8578
swift_slowDealloc(ptr, sizeof(T), alignof(T) - 1);
8679
}
8780
}
88-
89-
namespace {
90-
// This is C++17 and newer, so we simply re-define it. Since the codebase is
91-
// C++14, assume that DR1558 is accounted for and that unused parameters in alias
92-
// templates are guaranteed to ensure SFINAE and are not ignored.
93-
template <typename ...>
94-
using void_t = void;
95-
96-
template <typename T, typename = void>
97-
struct is_aligned_alloc_aware : std::false_type {};
98-
99-
template <typename T>
100-
struct is_aligned_alloc_aware<T, void_t<decltype(T::operator new(0))>>
101-
: std::true_type {};
102-
}
103-
104-
template <std::size_t Alignment_>
105-
struct requires_aligned_alloc {
106-
#if defined(__cpp_aligned_new)
107-
// If we have C++17 or newer we can use the alignment aware allocation
108-
// implicitly.
109-
static constexpr const bool value = false;
110-
#else
111-
#if defined(__STDCPP_DEFAULT_NEW_ALIGNMENT__)
112-
static constexpr const bool value =
113-
Alignment_ > std::alignment_of<std::max_align_t>::value &&
114-
Alignment_ > __STDCPP_DEFAULT_NEW_ALIGNMENT__;
115-
#else
116-
static constexpr const bool value =
117-
Alignment_ > std::alignment_of<std::max_align_t>::value;
118-
#endif
119-
#endif
120-
};
121-
122-
template <std::size_t Alignment_,
123-
bool = requires_aligned_alloc<Alignment_>::value>
124-
struct aligned_alloc;
125-
126-
template <std::size_t Alignment_>
127-
struct aligned_alloc<Alignment_, false> {};
128-
129-
template <std::size_t Alignment_>
130-
struct aligned_alloc<Alignment_, true> {
131-
[[nodiscard]] void *operator new(std::size_t size) noexcept {
132-
#if defined(_WIN32)
133-
return _aligned_malloc(size, Alignment_);
134-
#else
135-
static_assert(Alignment_ >= sizeof(void *),
136-
"posix_memalign requires minimal alignment of pointer");
137-
void *ptr = nullptr;
138-
(void)posix_memalign(&ptr, Alignment_, size);
139-
return ptr;
140-
#endif
141-
}
142-
143-
void operator delete(void *ptr) noexcept {
144-
#if defined(_WIN32)
145-
_aligned_free(ptr);
146-
#else
147-
free(ptr);
148-
#endif
149-
}
150-
151-
#if defined(_WIN32)
152-
// FIXME: why is this even needed? This is not permitted as per the C++
153-
// standard new.delete.placement (§17.6.3.4).
154-
[[nodiscard]] void *operator new(std::size_t size, void *where) noexcept {
155-
return ::operator new(size, where);
156-
}
157-
#endif
158-
};
15981
}
16082

16183
#endif // SWIFT_RUNTIME_HEAP_H

include/swift/Runtime/HeapObject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <cstddef>
2121
#include <cstdint>
2222
#include "swift/Runtime/Config.h"
23+
#include "swift/Runtime/Heap.h"
2324

2425
#if SWIFT_OBJC_INTEROP
2526
#include <objc/objc.h>

stdlib/public/Concurrency/Actor.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,7 @@ class JobRef {
580580
/// achieved through careful arrangement of the storage for this in the
581581
/// DefaultActorImpl. The additional alignment requirements are
582582
/// enforced by static asserts below.
583-
class alignas(2 * sizeof(void *)) ActiveActorStatus
584-
: public swift::aligned_alloc<2 * sizeof(void *)> {
583+
class alignas(sizeof(void *) * 2) ActiveActorStatus {
585584
#if SWIFT_CONCURRENCY_ENABLE_PRIORITY_ESCALATION && SWIFT_POINTER_IS_4_BYTES
586585
uint32_t Flags;
587586
dispatch_lock_t DrainLock;

stdlib/public/Concurrency/AsyncLet.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "swift/ABI/Task.h"
2222
#include "swift/ABI/TaskOptions.h"
2323
#include "swift/Runtime/Mutex.h"
24-
#include "swift/Runtime/Heap.h"
2524
#include "swift/Runtime/HeapObject.h"
2625
#include "llvm/ADT/PointerIntPair.h"
2726
#include "TaskPrivate.h"
@@ -36,9 +35,7 @@
3635
using namespace swift;
3736

3837
namespace {
39-
class alignas(Alignment_AsyncLet) AsyncLetImpl
40-
: public swift::aligned_alloc<Alignment_AsyncLet>,
41-
public ChildTaskStatusRecord {
38+
class alignas(Alignment_AsyncLet) AsyncLetImpl: public ChildTaskStatusRecord {
4239
public:
4340
// This is where we could define a Status or other types important for async-let
4441

stdlib/public/Concurrency/TaskPrivate.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "swift/Runtime/DispatchShims.h"
2727
#include "swift/Runtime/Error.h"
2828
#include "swift/Runtime/Exclusivity.h"
29-
#include "swift/Runtime/Heap.h"
3029
#include "swift/Runtime/HeapObject.h"
3130
#include <atomic>
3231
#include <new>
@@ -526,12 +525,6 @@ class alignas(2 * sizeof(void*)) ActiveTaskStatus {
526525
#endif
527526
static_assert(sizeof(ActiveTaskStatus) == ACTIVE_TASK_STATUS_SIZE,
528527
"ActiveTaskStatus is of incorrect size");
529-
#if !defined(_WIN64)
530-
static_assert(sizeof(swift::atomic<ActiveTaskStatus>) == sizeof(std::atomic<ActiveTaskStatus>),
531-
"swift::atomic pads std::atomic, memory aliasing invariants violated");
532-
#endif
533-
static_assert(sizeof(swift::atomic<ActiveTaskStatus>) == sizeof(ActiveTaskStatus),
534-
"swift::atomic pads ActiveTaskStatus, memory aliasing invariants violated");
535528

536529
/// The size of an allocator slab. We want the full allocation to fit into a
537530
/// 1024-byte malloc quantum. We subtract off the slab header size, plus a
@@ -553,7 +546,7 @@ struct AsyncTask::PrivateStorage {
553546

554547
/// Storage for the ActiveTaskStatus. See doc for ActiveTaskStatus for size
555548
/// and alignment requirements.
556-
alignas(alignof(ActiveTaskStatus)) char StatusStorage[sizeof(ActiveTaskStatus)];
549+
alignas(ActiveTaskStatus) char StatusStorage[sizeof(ActiveTaskStatus)];
557550

558551
/// The allocator for the task stack.
559552
/// Currently 2 words + 8 bytes.

stdlib/public/SwiftShims/RefCount.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,9 +626,8 @@ class RefCountBitsT {
626626

627627
typedef RefCountBitsT<RefCountIsInline> InlineRefCountBits;
628628

629-
class alignas(2 * sizeof(void*)) SideTableRefCountBits
630-
: public swift::aligned_alloc<2 * sizeof(void *)>,
631-
public RefCountBitsT<RefCountNotInline> {
629+
class alignas(sizeof(void*) * 2) SideTableRefCountBits : public RefCountBitsT<RefCountNotInline>
630+
{
632631
uint32_t weakBits;
633632

634633
public:

stdlib/public/runtime/KnownMetadata.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "swift/Runtime/Metadata.h"
18-
#include "swift/Runtime/Heap.h"
1918
#include "swift/Runtime/HeapObject.h"
2019
#include "swift/Runtime/Numeric.h"
2120
#include "MetadataImpl.h"
@@ -41,19 +40,19 @@ OpaqueValue *swift::swift_copyPOD(OpaqueValue *dest, OpaqueValue *src,
4140
namespace {
4241
// A type sized and aligned the way Swift wants Int128 (and Float80/Float128)
4342
// to be sized and aligned.
44-
struct alignas(16) int128_like : swift::aligned_alloc<16> {
43+
struct alignas(16) int128_like {
4544
char data[16];
4645
};
4746

4847
static_assert(MaximumAlignment == 16, "max alignment was hardcoded");
49-
struct alignas(16) int256_like : swift::aligned_alloc<16> {
48+
struct alignas(16) int256_like {
5049
char data[32];
5150
};
52-
struct alignas(16) int512_like : swift::aligned_alloc<16> {
51+
struct alignas(16) int512_like {
5352
char data[64];
5453
};
5554

56-
struct alignas(16) float80_like : swift::aligned_alloc<16> {
55+
struct alignas(16) float80_like {
5756
char data[10];
5857
};
5958
} // end anonymous namespace
@@ -90,8 +89,7 @@ namespace ctypes {
9089
// Types that are defined in the _Concurrency library
9190

9291
// Default actor storage type.
93-
struct alignas(2 * alignof(void*)) BD
94-
: swift::aligned_alloc<2 * alignof(void*)> {
92+
struct alignas(2 * alignof(void*)) BD {
9593
void *storage[NumWords_DefaultActor];
9694
};
9795

stdlib/public/runtime/Metadata.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "swift/Runtime/Casting.h"
2525
#include "swift/Runtime/EnvironmentVariables.h"
2626
#include "swift/Runtime/ExistentialContainer.h"
27-
#include "swift/Runtime/Heap.h"
2827
#include "swift/Runtime/HeapObject.h"
2928
#include "swift/Runtime/Mutex.h"
3029
#include "swift/Runtime/Once.h"
@@ -6158,16 +6157,10 @@ void swift::blockOnMetadataDependency(MetadataDependency root,
61586157
#if !SWIFT_STDLIB_PASSTHROUGH_METADATA_ALLOCATOR
61596158

61606159
namespace {
6161-
struct alignas(2 * sizeof(uintptr_t)) PoolRange
6162-
: swift::aligned_alloc<2 * sizeof(uintptr_t)> {
6160+
struct alignas(sizeof(uintptr_t) * 2) PoolRange {
61636161
static constexpr uintptr_t PageSize = 16 * 1024;
61646162
static constexpr uintptr_t MaxPoolAllocationSize = PageSize / 2;
61656163

6166-
constexpr PoolRange(char *Begin, size_t Remaining)
6167-
: Begin(Begin), Remaining(Remaining) {}
6168-
6169-
PoolRange() : Begin(nullptr), Remaining(0) {}
6170-
61716164
/// The start of the allocation.
61726165
char *Begin;
61736166

stdlib/public/runtime/MetadataCache.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "llvm/ADT/Hashing.h"
1616
#include "llvm/ADT/STLExtras.h"
1717
#include "swift/Runtime/Concurrent.h"
18-
#include "swift/Runtime/Heap.h"
1918
#include "swift/Runtime/Metadata.h"
2019
#include "swift/Runtime/Mutex.h"
2120
#include "swift/Runtime/AtomicWaitQueue.h"
@@ -621,9 +620,8 @@ const size_t PrivateMetadataTrackingAlignment = 16;
621620

622621
/// The wait queue object that we create for metadata that are
623622
/// being actively initialized right now.
624-
struct alignas(PrivateMetadataTrackingAlignment) MetadataWaitQueue
625-
: swift::aligned_alloc<PrivateMetadataTrackingAlignment>,
626-
public AtomicWaitQueue<MetadataWaitQueue, ConcurrencyControl::LockType> {
623+
struct alignas(PrivateMetadataTrackingAlignment) MetadataWaitQueue :
624+
public AtomicWaitQueue<MetadataWaitQueue, ConcurrencyControl::LockType> {
627625

628626
/// A pointer to the completion context being used to complete this
629627
/// metadata. This is only actually filled in if:
@@ -682,8 +680,7 @@ struct alignas(PrivateMetadataTrackingAlignment) MetadataWaitQueue
682680

683681
/// A record used to store information about an attempt to
684682
/// complete a metadata when there's no active worker thread.
685-
struct alignas(PrivateMetadataTrackingAlignment) SuspendedMetadataCompletion
686-
: swift::aligned_alloc<PrivateMetadataTrackingAlignment> {
683+
struct alignas(PrivateMetadataTrackingAlignment) SuspendedMetadataCompletion {
687684
MetadataDependency BlockingDependency;
688685
std::unique_ptr<PrivateMetadataCompletionContext> PersistentContext;
689686

0 commit comments

Comments
 (0)