Skip to content

Commit 54b9aff

Browse files
authored
Merge pull request #58688 from grynspan/jgrynspan/58686-nonnull-allocation-annotations
[WIP] #58686 `swift_slowAlloc()` _et al._ should be marked returns-nonnull to improve codegen
2 parents 223d73c + 770fd10 commit 54b9aff

File tree

8 files changed

+27
-11
lines changed

8 files changed

+27
-11
lines changed

include/swift/Runtime/Heap.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
#include <utility>
2323

2424
#include "swift/Runtime/Config.h"
25+
#include "../../../stdlib/public/SwiftShims/Visibility.h"
2526

2627
namespace swift {
2728
// Allocate plain old memory. This is the generalized entry point
2829
// Never returns nil. The returned memory is uninitialized.
2930
//
3031
// An "alignment mask" is just the alignment (a power of 2) minus 1.
31-
SWIFT_RUNTIME_EXPORT
32+
SWIFT_RETURNS_NONNULL SWIFT_RUNTIME_EXPORT
3233
void *swift_slowAlloc(size_t bytes, size_t alignMask);
3334

3435
// If the caller cannot promise to zero the object during destruction,
@@ -52,6 +53,7 @@ void swift_slowDealloc(void *ptr, size_t bytes, size_t alignMask);
5253
/// This function is capable of returning well-aligned memory even on platforms
5354
/// that do not implement the C++17 "over-aligned new" feature.
5455
template <typename T, typename... Args>
56+
SWIFT_RETURNS_NONNULL
5557
static inline T *swift_cxx_newObject(Args &&... args) {
5658
auto result = reinterpret_cast<T *>(swift_slowAlloc(sizeof(T),
5759
alignof(T) - 1));

include/swift/Runtime/HeapObject.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
// Bring in the definition of HeapObject
3030
#include "../../../stdlib/public/SwiftShims/HeapObject.h"
31+
#include "../../../stdlib/public/SwiftShims/Visibility.h"
3132

3233
namespace swift {
3334

@@ -60,7 +61,7 @@ struct OpaqueValue;
6061
///
6162
/// POSSIBILITIES: The argument order is fair game. It may be useful
6263
/// to have a variant which guarantees zero-initialized memory.
63-
SWIFT_RUNTIME_EXPORT
64+
SWIFT_RETURNS_NONNULL SWIFT_RUNTIME_EXPORT
6465
HeapObject *swift_allocObject(HeapMetadata const *metadata,
6566
size_t requiredSize,
6667
size_t requiredAlignmentMask);
@@ -116,7 +117,7 @@ BoxPair swift_makeBoxUnique(OpaqueValue *buffer, Metadata const *type,
116117
size_t alignMask);
117118

118119
/// Returns the address of a heap object representing all empty box types.
119-
SWIFT_RUNTIME_EXPORT
120+
SWIFT_RETURNS_NONNULL SWIFT_RUNTIME_EXPORT
120121
HeapObject* swift_allocEmptyBox();
121122

122123
/// Atomically increments the retain count of an object.

include/swift/Runtime/Metadata.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/ABI/Metadata.h"
2121
#include "swift/Reflection/Records.h"
2222
#include "swift/Runtime/Once.h"
23+
#include "../../../stdlib/public/SwiftShims/Visibility.h"
2324

2425
namespace swift {
2526

@@ -306,7 +307,7 @@ swift_getGenericMetadata(MetadataRequest request,
306307
/// - installing new v-table entries and overrides; and
307308
/// - registering the class with the runtime under ObjC interop.
308309
/// Most of this work can be achieved by calling swift_initClassMetadata.
309-
SWIFT_RUNTIME_EXPORT
310+
SWIFT_RETURNS_NONNULL SWIFT_RUNTIME_EXPORT
310311
ClassMetadata *
311312
swift_allocateGenericClassMetadata(const ClassDescriptor *description,
312313
const void *arguments,
@@ -315,7 +316,7 @@ swift_allocateGenericClassMetadata(const ClassDescriptor *description,
315316
/// Allocate a generic value metadata object. This is intended to be
316317
/// called by the metadata instantiation function of a generic struct or
317318
/// enum.
318-
SWIFT_RUNTIME_EXPORT
319+
SWIFT_RETURNS_NONNULL SWIFT_RUNTIME_EXPORT
319320
ValueMetadata *
320321
swift_allocateGenericValueMetadata(const ValueTypeDescriptor *description,
321322
const void *arguments,

stdlib/public/SwiftShims/Visibility.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,13 @@
236236
#define SWIFT_NODISCARD
237237
#endif
238238

239+
#if __has_cpp_attribute(gnu::returns_nonnull)
240+
#define SWIFT_RETURNS_NONNULL [[gnu::returns_nonnull]]
241+
#elif defined(_MSC_VER) && defined(_Ret_notnull_)
242+
#define SWIFT_RETURNS_NONNULL _Ret_notnull_
243+
#else
244+
#define SWIFT_RETURNS_NONNULL
245+
#endif
239246

240247
/// Attributes for runtime-stdlib interfaces.
241248
/// Use these for C implementations that are imported into Swift via SwiftShims

stdlib/public/runtime/HeapObject.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static inline bool isValidPointerForNativeRetain(const void *p) {
9393
// not to emit a bunch of ptrauth instructions just to perform the comparison.
9494
// We only want to authenticate the function pointer if we actually call it. We
9595
// can revert to a straight comparison once rdar://problem/55267009 is fixed.
96+
SWIFT_RETURNS_NONNULL
9697
static HeapObject *_swift_allocObject_(HeapMetadata const *metadata,
9798
size_t requiredSize,
9899
size_t requiredAlignmentMask)

stdlib/public/runtime/Metadata.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6313,7 +6313,8 @@ void *MetadataAllocator::Allocate(size_t size, size_t alignment) {
63136313
if (SWIFT_UNLIKELY(_swift_debug_metadataAllocationIterationEnabled))
63146314
poolSize -= sizeof(PoolTrailer);
63156315
allocatedNewPage = true;
6316-
allocation = new char[PoolRange::PageSize];
6316+
allocation = reinterpret_cast<char *>(swift_slowAlloc(PoolRange::PageSize,
6317+
alignof(char) - 1));
63176318
memsetScribble(allocation, PoolRange::PageSize);
63186319

63196320
if (SWIFT_UNLIKELY(_swift_debug_metadataAllocationIterationEnabled)) {
@@ -6371,7 +6372,7 @@ void *MetadataAllocator::Allocate(size_t size, size_t alignment) {
63716372

63726373
// If it failed, go back to a neutral state and try again.
63736374
if (allocatedNewPage) {
6374-
delete[] allocation;
6375+
swift_slowDealloc(allocation, PoolRange::PageSize, alignof(char) - 1);
63756376
}
63766377
}
63776378
}

stdlib/public/runtime/MetadataCache.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "swift/Runtime/Metadata.h"
1919
#include "swift/Runtime/Mutex.h"
2020
#include "swift/Runtime/AtomicWaitQueue.h"
21+
#include "../SwiftShims/Visibility.h"
2122
#include <condition_variable>
2223
#include <thread>
2324

@@ -39,7 +40,7 @@ class MetadataAllocator : public llvm::AllocatorBase<MetadataAllocator> {
3940

4041
void Reset() {}
4142

42-
LLVM_ATTRIBUTE_RETURNS_NONNULL void *Allocate(size_t size, size_t alignment);
43+
SWIFT_RETURNS_NONNULL void *Allocate(size_t size, size_t alignment);
4344
using AllocatorBase<MetadataAllocator>::Allocate;
4445

4546
void Deallocate(const void *Ptr, size_t size, size_t Alignment);
@@ -59,11 +60,11 @@ class MetadataAllocator : public llvm::AllocatorBase<MetadataAllocator> {
5960
class MetadataAllocator {
6061
public:
6162
MetadataAllocator(uint16_t tag) {}
62-
void *Allocate(size_t size, size_t alignment) {
63+
SWIFT_RETURNS_NONNULL void *Allocate(size_t size, size_t alignment) {
6364
if (alignment < sizeof(void*)) alignment = sizeof(void*);
6465
void *ptr = nullptr;
65-
if (posix_memalign(&ptr, alignment, size) != 0) {
66-
return nullptr;
66+
if (SWIFT_UNLIKELY(posix_memalign(&ptr, alignment, size) != 0 || !ptr)) {
67+
swift::crash("Could not allocate memory for type metadata.");
6768
}
6869
return ptr;
6970
}

stdlib/public/runtime/Private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/Demangling/TypeLookupError.h"
2424
#include "swift/Runtime/Config.h"
2525
#include "swift/Runtime/Metadata.h"
26+
#include "../SwiftShims/Visibility.h"
2627

2728
#if defined(__APPLE__) && __has_include(<TargetConditionals.h>)
2829
#include <TargetConditionals.h>
@@ -515,6 +516,7 @@ class TypeInfo {
515516
}
516517
}
517518

519+
SWIFT_RETURNS_NONNULL
518520
void *allocateMetadata(size_t size, size_t align);
519521

520522
/// Gather the set of generic arguments that would be written in the

0 commit comments

Comments
 (0)