Skip to content

Commit 770648f

Browse files
committed
Initial runtime changes to support relative protocol witness tables
1 parent a9b69a1 commit 770648f

File tree

13 files changed

+563
-6
lines changed

13 files changed

+563
-6
lines changed

include/swift/ABI/Metadata.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,42 @@ using AssociatedWitnessTableAccessFunction =
16421642
const Metadata *self,
16431643
const WitnessTable *selfConformance);
16441644

1645+
template<typename Runtime>
1646+
using TargetRelativeProtocolConformanceDescriptorPointer =
1647+
RelativeIndirectablePointer<const TargetProtocolConformanceDescriptor<Runtime>,
1648+
/*nullable*/ false>;
1649+
1650+
/// A relative witness table for a protocol.
1651+
///
1652+
/// With the exception of the initial protocol conformance descriptor,
1653+
/// the layout of a witness table is dependent on the protocol being
1654+
/// represented.
1655+
/// Entries are relative pointers.
1656+
template <typename Runtime>
1657+
class TargetRelativeWitnessTable {
1658+
/// The protocol conformance descriptor from which this witness table
1659+
/// was generated.
1660+
TargetRelativeProtocolConformanceDescriptorPointer<Runtime> Description;
1661+
1662+
public:
1663+
const TargetProtocolConformanceDescriptor<Runtime> *getDescription() const {
1664+
// "Look through" dynamically allocated witness tables.
1665+
uintptr_t selfValue = (uintptr_t)this;
1666+
if (selfValue & 0x1) {
1667+
selfValue = selfValue & ~((uintptr_t)0x1);
1668+
auto selfAddr = (TargetRelativeWitnessTable<Runtime> **)selfValue;
1669+
return (*selfAddr)->Description;
1670+
}
1671+
return Description;
1672+
}
1673+
};
1674+
using RelativeWitnessTable = TargetRelativeWitnessTable<InProcess>;
1675+
1676+
using AssociatedRelativeWitnessTableAccessFunction =
1677+
SWIFT_CC(swift) RelativeWitnessTable *(const Metadata *associatedType,
1678+
const Metadata *self,
1679+
const RelativeWitnessTable *selfConformance);
1680+
16451681
/// The possible physical representations of existential types.
16461682
enum class ExistentialTypeRepresentation {
16471683
/// The type uses an opaque existential representation.

include/swift/Runtime/Metadata.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,8 @@ swift_getWitnessTable(const ProtocolConformanceDescriptor *conformance,
354354
const Metadata *type,
355355
const void * const *instantiationArgs);
356356

357-
const WitnessTable *
357+
SWIFT_RUNTIME_EXPORT
358+
const RelativeWitnessTable *
358359
swift_getWitnessTableRelative(const ProtocolConformanceDescriptor *conformance,
359360
const Metadata *type,
360361
const void * const *instantiationArgs);
@@ -377,7 +378,7 @@ MetadataResponse swift_getAssociatedTypeWitness(
377378
SWIFT_RUNTIME_EXPORT SWIFT_CC(swift)
378379
MetadataResponse swift_getAssociatedTypeWitnessRelative(
379380
MetadataRequest request,
380-
WitnessTable *wtable,
381+
RelativeWitnessTable *wtable,
381382
const Metadata *conformingType,
382383
const ProtocolRequirement *reqBase,
383384
const ProtocolRequirement *assocType);
@@ -401,8 +402,8 @@ const WitnessTable *swift_getAssociatedConformanceWitness(
401402
const ProtocolRequirement *assocConformance);
402403

403404
SWIFT_RUNTIME_EXPORT SWIFT_CC(swift)
404-
const WitnessTable *swift_getAssociatedConformanceWitnessRelative(
405-
WitnessTable *wtable,
405+
const RelativeWitnessTable *swift_getAssociatedConformanceWitnessRelative(
406+
RelativeWitnessTable *wtable,
406407
const Metadata *conformingType,
407408
const Metadata *assocType,
408409
const ProtocolRequirement *reqBase,

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,10 @@ function(_add_target_variant_c_compile_flags)
414414
list(APPEND result "-DSWIFT_STDLIB_CONCURRENCY_TRACING")
415415
endif()
416416

417+
if(SWIFT_STDLIB_USE_RELATIVE_PROTOCOL_WITNESS_TABLES)
418+
list(APPEND result "-DSWIFT_STDLIB_USE_RELATIVE_PROTOCOL_WITNESS_TABLES")
419+
endif()
420+
417421
list(APPEND result ${SWIFT_STDLIB_EXTRA_C_COMPILE_FLAGS})
418422

419423
set("${CFLAGS_RESULT_VAR_NAME}" "${result}" PARENT_SCOPE)

stdlib/cmake/modules/StdlibOptions.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,7 @@ option(SWIFT_STDLIB_CONCURRENCY_TRACING
230230
"Enable concurrency tracing in the runtime; assumes the presence of os_log(3)
231231
and the os_signpost(3) API."
232232
"${SWIFT_STDLIB_CONCURRENCY_TRACING_default}")
233+
234+
option(SWIFT_STDLIB_USE_RELATIVE_PROTOCOL_WITNESS_TABLES
235+
"Use relative protocol witness tables"
236+
FALSE)

stdlib/cmake/modules/SwiftSource.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,10 @@ function(_compile_swift_files
568568
list(APPEND swift_flags "-experimental-hermetic-seal-at-link")
569569
endif()
570570

571+
if (SWIFT_STDLIB_USE_RELATIVE_PROTOCOL_WITNESS_TABLES)
572+
list(APPEND swift_flags "-Xfrontend" "-enable-relative-protocol-witness-tables")
573+
endif()
574+
571575
if(SWIFT_STDLIB_DISABLE_INSTANTIATION_CACHES)
572576
list(APPEND swift_flags "-Xfrontend" "-disable-preallocated-instantiation-caches")
573577
endif()

0 commit comments

Comments
 (0)