Skip to content

Commit 96d988a

Browse files
authored
[Runtime] Move bytecode layout handling into runtime (swiftlang#63901)
rdar://105904548
1 parent d7c31aa commit 96d988a

File tree

11 files changed

+89
-77
lines changed

11 files changed

+89
-77
lines changed

include/swift/Runtime/Metadata.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,13 @@ swift_allocateGenericClassMetadata(const ClassDescriptor *description,
319319
const void *arguments,
320320
const GenericClassMetadataPattern *pattern);
321321

322+
SWIFT_EXTERN_C SWIFT_RETURNS_NONNULL SWIFT_NODISCARD SWIFT_RUNTIME_EXPORT_ATTRIBUTE
323+
ClassMetadata *
324+
swift_allocateGenericClassMetadataWithLayoutString(
325+
const ClassDescriptor *description,
326+
const void *arguments,
327+
const GenericClassMetadataPattern *pattern);
328+
322329
/// Allocate a generic value metadata object. This is intended to be
323330
/// called by the metadata instantiation function of a generic struct or
324331
/// enum.
@@ -329,6 +336,14 @@ swift_allocateGenericValueMetadata(const ValueTypeDescriptor *description,
329336
const GenericValueMetadataPattern *pattern,
330337
size_t extraDataSize);
331338

339+
SWIFT_EXTERN_C SWIFT_RETURNS_NONNULL SWIFT_NODISCARD SWIFT_RUNTIME_EXPORT_ATTRIBUTE
340+
ValueMetadata *
341+
swift_allocateGenericValueMetadataWithLayoutString(
342+
const ValueTypeDescriptor *description,
343+
const void *arguments,
344+
const GenericValueMetadataPattern *pattern,
345+
size_t extraDataSize);
346+
332347
/// Check that the given metadata has the right state.
333348
SWIFT_RUNTIME_EXPORT SWIFT_CC(swift)
334349
MetadataResponse swift_checkMetadataState(MetadataRequest request,

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,18 @@ FUNCTION(AllocateGenericClassMetadata, swift_allocateGenericClassMetadata,
876876
ATTRS(NoUnwind),
877877
EFFECT(MetaData))
878878

879+
// Metadata *swift_allocateGenericClassMetadataWithLayoutString(
880+
// ClassDescriptor *type,
881+
// const void * const *arguments,
882+
// const void *template);
883+
FUNCTION(AllocateGenericClassMetadataWithLayoutString,
884+
swift_allocateGenericClassMetadataWithLayoutString,
885+
C_CC, AlwaysAvailable,
886+
RETURNS(TypeMetadataPtrTy),
887+
ARGS(TypeContextDescriptorPtrTy, Int8PtrPtrTy, Int8PtrTy),
888+
ATTRS(NoUnwind),
889+
EFFECT(MetaData))
890+
879891
// Metadata *swift_allocateGenericValueMetadata(ValueTypeDescriptor *type,
880892
// const void * const *arguments,
881893
// const void *template,
@@ -887,6 +899,19 @@ FUNCTION(AllocateGenericValueMetadata, swift_allocateGenericValueMetadata,
887899
ATTRS(NoUnwind),
888900
EFFECT(MetaData))
889901

902+
// Metadata *swift_allocateGenericValueMetadataWithLayoutString(
903+
// ValueTypeDescriptor *type,
904+
// const void * const *arguments,
905+
// const void *template,
906+
// size_t extraSize);
907+
FUNCTION(AllocateGenericValueMetadataWithLayoutString,
908+
swift_allocateGenericValueMetadataWithLayoutString,
909+
C_CC, AlwaysAvailable,
910+
RETURNS(TypeMetadataPtrTy),
911+
ARGS(TypeContextDescriptorPtrTy, Int8PtrPtrTy, Int8PtrTy, SizeTy),
912+
ATTRS(NoUnwind),
913+
EFFECT(MetaData))
914+
890915
// MetadataResponse swift_checkMetadataState(MetadataRequest request,
891916
// const Metadata *type);
892917
FUNCTION(CheckMetadataState, swift_checkMetadataState,

lib/IRGen/GenDecl.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -540,11 +540,6 @@ void IRGenModule::emitSourceFile(SourceFile &SF) {
540540
#define BACK_DEPLOYMENT_LIB(Version, Filter, LibraryName) \
541541
addBackDeployLib(llvm::VersionTuple Version, LibraryName);
542542
#include "swift/Frontend/BackDeploymentLibs.def"
543-
544-
if (IRGen.Opts.AutolinkRuntimeCompatibilityBytecodeLayoutsLibrary)
545-
this->addLinkLibrary(LinkLibrary("swiftCompatibilityBytecodeLayouts",
546-
LibraryKind::Library,
547-
/*forceLoad*/ true));
548543
}
549544
}
550545

lib/IRGen/GenMeta.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4258,7 +4258,9 @@ namespace {
42584258
}
42594259

42604260
auto metadata = IGF.Builder.CreateCall(
4261-
IGM.getAllocateGenericClassMetadataFunctionPointer(),
4261+
getLayoutString() ?
4262+
IGM.getAllocateGenericClassMetadataWithLayoutStringFunctionPointer() :
4263+
IGM.getAllocateGenericClassMetadataFunctionPointer(),
42624264
{descriptor, arguments, templatePointer});
42634265

42644266
return metadata;
@@ -4985,6 +4987,8 @@ namespace {
49854987
}
49864988

49874989
return IGF.Builder.CreateCall(
4990+
getLayoutString() ?
4991+
IGM.getAllocateGenericValueMetadataWithLayoutStringFunctionPointer() :
49884992
IGM.getAllocateGenericValueMetadataFunctionPointer(),
49894993
{descriptor, arguments, templatePointer, extraSizeV});
49904994
}
@@ -5412,7 +5416,9 @@ namespace {
54125416
}
54135417

54145418
return IGF.Builder.CreateCall(
5415-
IGM.getAllocateGenericValueMetadataFunctionPointer(),
5419+
getLayoutString() ?
5420+
IGM.getAllocateGenericValueMetadataWithLayoutStringFunctionPointer() :
5421+
IGM.getAllocateGenericValueMetadataFunctionPointer(),
54165422
{descriptor, arguments, templatePointer, extraSizeV});
54175423
}
54185424

stdlib/toolchain/CompatibilityBytecodeLayouts/BytecodeLayouts.cpp renamed to stdlib/public/runtime/BytecodeLayouts.cpp

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,8 @@
3535

3636
using namespace swift;
3737

38-
// Pointers my have spare bits stored in the high and low bits. Mask them out
39-
// before we pass them to retain/release functions
40-
#define MASK_PTR(x) \
41-
((__swift_uintptr_t)x & ~heap_object_abi::SwiftSpareBitsMask)
42-
4338
static const size_t layoutStringHeaderSize = sizeof(size_t);
4439

45-
/// Get the generic argument vector for the passed in metadata
46-
///
47-
/// NB: We manually compute the offset instead of using Metadata::getGenericArgs
48-
/// because Metadata::getGenericArgs checks the isSwift bit which we cannot do
49-
/// in a compatibility library. Once we merge this into the runtime, we can
50-
/// safely use Metadata::getGenericArgs. For our purposes right now anyways,
51-
/// struct and enums always have their generic argument vector at offset + 2 so
52-
/// we can hard code that.
53-
Metadata **getGenericArgs(Metadata *metadata) {
54-
return ((Metadata **)metadata) + 2;
55-
}
56-
5740
/// Given a pointer and an offset, read the requested data and increment the
5841
/// offset
5942
template <typename T>
@@ -74,7 +57,7 @@ Metadata *getExistentialTypeMetadata(OpaqueValue *object) {
7457
return reinterpret_cast<Metadata**>(object)[NumWords_ValueBuffer];
7558
}
7659

77-
typedef Metadata* (*MetadataAccessor)(Metadata**);
60+
typedef Metadata* (*MetadataAccessor)(const Metadata* const *);
7861

7962
const Metadata *getResilientTypeMetadata(Metadata* metadata, const uint8_t *layoutStr, size_t &offset) {
8063
auto absolute = layoutStr + offset;
@@ -89,7 +72,7 @@ const Metadata *getResilientTypeMetadata(Metadata* metadata, const uint8_t *layo
8972
fn = (MetadataAccessor)((uintptr_t) + absolute + relativeOffset);
9073
#endif
9174

92-
return fn(getGenericArgs(metadata));
75+
return fn(metadata->getGenericArgs());
9376
}
9477

9578
typedef void (*DestrFn)(void*);
@@ -135,7 +118,7 @@ const DestroyFuncAndMask destroyTable[] = {
135118
{(DestrFn)&existential_destroy, UINTPTR_MAX, false},
136119
};
137120

138-
__attribute__((weak)) extern "C" void
121+
extern "C" void
139122
swift_generic_destroy(void *address, void *metadata) {
140123
uint8_t *addr = (uint8_t *)address;
141124
Metadata *typedMetadata = (Metadata *)metadata;
@@ -161,12 +144,11 @@ swift_generic_destroy(void *address, void *metadata) {
161144
} else if (SWIFT_UNLIKELY(tag == RefCountingKind::Resilient)) {
162145
auto *type = getResilientTypeMetadata(typedMetadata, typeLayout, offset);
163146
type->vw_destroy((OpaqueValue *)(addr + addrOffset));
164-
//addrOffset += type->vw_size();
165147
} else {
166148
const auto &destroyFunc = destroyTable[static_cast<uint8_t>(tag)];
167149
if (SWIFT_LIKELY(destroyFunc.isIndirect)) {
168150
destroyFunc.fn(
169-
(void *)((*(uintptr_t *)(addr + addrOffset))));// & destroyFunc.mask));
151+
(void *)((*(uintptr_t *)(addr + addrOffset))));
170152
} else {
171153
destroyFunc.fn(((void *)(addr + addrOffset)));
172154
}
@@ -220,7 +202,7 @@ const RetainFuncAndMask retainTable[] = {
220202
{(void*)&existential_initializeWithCopy, UINTPTR_MAX, false},
221203
};
222204

223-
__attribute__((weak)) extern "C" void *
205+
extern "C" void *
224206
swift_generic_initWithCopy(void *dest, void *src, void *metadata) {
225207
uintptr_t addrOffset = 0;
226208
Metadata *typedMetadata = (Metadata *)metadata;
@@ -245,24 +227,22 @@ swift_generic_initWithCopy(void *dest, void *src, void *metadata) {
245227
auto *type = reinterpret_cast<Metadata*>(typePtr);
246228
type->vw_initializeWithCopy((OpaqueValue*)((uintptr_t)dest + addrOffset),
247229
(OpaqueValue*)((uintptr_t)src + addrOffset));
248-
//addrOffset += type->vw_size();
249230
} else if (SWIFT_UNLIKELY(tag == RefCountingKind::Resilient)) {
250231
auto *type = getResilientTypeMetadata(typedMetadata, typeLayout, offset);
251232
type->vw_initializeWithCopy((OpaqueValue*)((uintptr_t)dest + addrOffset),
252233
(OpaqueValue*)((uintptr_t)src + addrOffset));
253-
//addrOffset += type->vw_size();
254234
} else {
255235
const auto &retainFunc = retainTable[static_cast<uint8_t>(tag)];
256236
if (SWIFT_LIKELY(retainFunc.isSingle)) {
257-
((RetainFn)retainFunc.fn)(*(void**)(((uintptr_t)dest + addrOffset))); // & retainFunc.mask));
237+
((RetainFn)retainFunc.fn)(*(void**)(((uintptr_t)dest + addrOffset)));
258238
} else {
259239
((CopyInitFn)retainFunc.fn)((void*)((uintptr_t)dest + addrOffset), (void*)((uintptr_t)src + addrOffset));
260240
}
261241
}
262242
}
263243
}
264244

265-
__attribute__((weak)) extern "C" void *
245+
extern "C" void *
266246
swift_generic_initWithTake(void *dest, void *src, void *metadata) {
267247
Metadata *typedMetadata = (Metadata *)metadata;
268248
const uint8_t *typeLayout = typedMetadata->getLayoutString();
@@ -323,19 +303,19 @@ swift_generic_initWithTake(void *dest, void *src, void *metadata) {
323303
return dest;
324304
}
325305

326-
__attribute__((weak)) extern "C" void *
306+
extern "C" void *
327307
swift_generic_assignWithCopy(void *dest, void *src, void *metadata) {
328308
swift_generic_destroy(dest, metadata);
329309
return swift_generic_initWithCopy(dest, src, metadata);
330310
}
331311

332-
__attribute__((weak)) extern "C" void *
312+
extern "C" void *
333313
swift_generic_assignWithTake(void *dest, void *src, void *metadata) {
334314
swift_generic_destroy(dest, metadata);
335315
return swift_generic_initWithTake(dest, src, metadata);
336316
}
337317

338-
__attribute__((weak)) extern "C" void
318+
extern "C" void
339319
swift_generic_instantiateLayoutString(const uint8_t* layoutStr,
340320
Metadata* type) {
341321
size_t offset = 0;
@@ -357,12 +337,12 @@ swift_generic_instantiateLayoutString(const uint8_t* layoutStr,
357337
const Metadata *genericType;
358338
if (tag == 2) {
359339
auto index = readBytes<uint32_t>(layoutStr, offset);
360-
genericType = getGenericArgs(type)[index];
340+
genericType = type->getGenericArgs()[index];
361341
} else {
362342
genericType = getResilientTypeMetadata(type, layoutStr, offset);
363343
}
364344

365-
if ((false)) {//genericType->getTypeContextDescriptor()->hasLayoutString()) {
345+
if (genericType->getTypeContextDescriptor()->hasLayoutString()) {
366346
const uint8_t *genericLayoutStr = genericType->getLayoutString();
367347
size_t countOffset = 0;
368348
genericRefCountSize += readBytes<size_t>(genericLayoutStr, countOffset);
@@ -416,12 +396,12 @@ swift_generic_instantiateLayoutString(const uint8_t* layoutStr,
416396
const Metadata *genericType;
417397
if (tag == 2) {
418398
auto index = readBytes<uint32_t>(layoutStr, offset);
419-
genericType = getGenericArgs(type)[index];
399+
genericType = type->getGenericArgs()[index];
420400
} else {
421401
genericType = getResilientTypeMetadata(type, layoutStr, offset);
422402
}
423403

424-
if ((false)) {//genericType->getTypeContextDescriptor()->hasLayoutString()) {
404+
if (genericType->getTypeContextDescriptor()->hasLayoutString()) {
425405
const uint8_t *genericLayoutStr = genericType->getLayoutString();
426406
size_t countOffset = 0;
427407
auto genericRefCountSize = readBytes<size_t>(genericLayoutStr, countOffset);
@@ -486,7 +466,3 @@ swift_generic_instantiateLayoutString(const uint8_t* layoutStr,
486466

487467
type->setLayoutString(instancedLayoutStr);
488468
}
489-
490-
// Allow this library to get force-loaded by autolinking
491-
__attribute__((weak, visibility("hidden"))) extern "C" char
492-
_swift_FORCE_LOAD_$_swiftCompatibilityBytecodeLayouts = 0;

stdlib/public/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ set(swift_runtime_sources
3131
Array.cpp
3232
AutoDiffSupport.cpp
3333
Bincompat.cpp
34+
BytecodeLayouts.cpp
3435
Casting.cpp
3536
CrashReporter.cpp
3637
Demangle.cpp

stdlib/public/runtime/Metadata.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,16 @@ swift::swift_allocateGenericClassMetadata(const ClassDescriptor *description,
677677
return metadata;
678678
}
679679

680+
ClassMetadata *
681+
swift::swift_allocateGenericClassMetadataWithLayoutString(
682+
const ClassDescriptor *description,
683+
const void *arguments,
684+
const GenericClassMetadataPattern *pattern) {
685+
return swift::swift_allocateGenericClassMetadata(description,
686+
arguments,
687+
pattern);
688+
}
689+
680690
static void
681691
initializeValueMetadataFromPattern(ValueMetadata *metadata,
682692
const ValueTypeDescriptor *description,
@@ -749,6 +759,18 @@ swift::swift_allocateGenericValueMetadata(const ValueTypeDescriptor *description
749759
return metadata;
750760
}
751761

762+
ValueMetadata *
763+
swift::swift_allocateGenericValueMetadataWithLayoutString(
764+
const ValueTypeDescriptor *description,
765+
const void *arguments,
766+
const GenericValueMetadataPattern *pattern,
767+
size_t extraDataSize) {
768+
return swift::swift_allocateGenericValueMetadata(description,
769+
arguments,
770+
pattern,
771+
extraDataSize);
772+
}
773+
752774
// Look into the canonical prespecialized metadata attached to the type
753775
// descriptor and add them to the metadata cache.
754776
static void

stdlib/toolchain/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ endif()
4646
# runtime being patched only through public ABI.
4747
list(APPEND CXX_COMPILE_FLAGS "-DSWIFT_COMPATIBILITY_LIBRARY=1")
4848

49-
if(SWIFT_BUILD_STDLIB)
50-
add_subdirectory(CompatibilityBytecodeLayouts)
51-
endif()
52-
5349
if(SWIFT_STDLIB_SUPPORT_BACK_DEPLOYMENT)
5450
add_subdirectory(legacy_layouts)
5551
add_subdirectory(Compatibility50)

stdlib/toolchain/CompatibilityBytecodeLayouts/CMakeLists.txt

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)