Skip to content

Initial runtime changes to support relative protocol witness tables #63329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions include/swift/ABI/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -1642,6 +1642,42 @@ using AssociatedWitnessTableAccessFunction =
const Metadata *self,
const WitnessTable *selfConformance);

template<typename Runtime>
using TargetRelativeProtocolConformanceDescriptorPointer =
RelativeIndirectablePointer<const TargetProtocolConformanceDescriptor<Runtime>,
/*nullable*/ false>;

/// A relative witness table for a protocol.
///
/// With the exception of the initial protocol conformance descriptor,
/// the layout of a witness table is dependent on the protocol being
/// represented.
/// Entries are relative pointers.
template <typename Runtime>
class TargetRelativeWitnessTable {
/// The protocol conformance descriptor from which this witness table
/// was generated.
TargetRelativeProtocolConformanceDescriptorPointer<Runtime> Description;

public:
const TargetProtocolConformanceDescriptor<Runtime> *getDescription() const {
// "Look through" dynamically allocated witness tables.
uintptr_t selfValue = (uintptr_t)this;
if (selfValue & 0x1) {
selfValue = selfValue & ~((uintptr_t)0x1);
auto selfAddr = (TargetRelativeWitnessTable<Runtime> **)selfValue;
return (*selfAddr)->Description;
}
return Description;
}
};
using RelativeWitnessTable = TargetRelativeWitnessTable<InProcess>;

using AssociatedRelativeWitnessTableAccessFunction =
SWIFT_CC(swift) RelativeWitnessTable *(const Metadata *associatedType,
const Metadata *self,
const RelativeWitnessTable *selfConformance);

/// The possible physical representations of existential types.
enum class ExistentialTypeRepresentation {
/// The type uses an opaque existential representation.
Expand Down
9 changes: 5 additions & 4 deletions include/swift/Runtime/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ swift_getWitnessTable(const ProtocolConformanceDescriptor *conformance,
const Metadata *type,
const void * const *instantiationArgs);

const WitnessTable *
SWIFT_RUNTIME_EXPORT
const RelativeWitnessTable *
swift_getWitnessTableRelative(const ProtocolConformanceDescriptor *conformance,
const Metadata *type,
const void * const *instantiationArgs);
Expand All @@ -377,7 +378,7 @@ MetadataResponse swift_getAssociatedTypeWitness(
SWIFT_RUNTIME_EXPORT SWIFT_CC(swift)
MetadataResponse swift_getAssociatedTypeWitnessRelative(
MetadataRequest request,
WitnessTable *wtable,
RelativeWitnessTable *wtable,
const Metadata *conformingType,
const ProtocolRequirement *reqBase,
const ProtocolRequirement *assocType);
Expand All @@ -401,8 +402,8 @@ const WitnessTable *swift_getAssociatedConformanceWitness(
const ProtocolRequirement *assocConformance);

SWIFT_RUNTIME_EXPORT SWIFT_CC(swift)
const WitnessTable *swift_getAssociatedConformanceWitnessRelative(
WitnessTable *wtable,
const RelativeWitnessTable *swift_getAssociatedConformanceWitnessRelative(
RelativeWitnessTable *wtable,
const Metadata *conformingType,
const Metadata *assocType,
const ProtocolRequirement *reqBase,
Expand Down
4 changes: 4 additions & 0 deletions stdlib/cmake/modules/AddSwiftStdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,10 @@ function(_add_target_variant_c_compile_flags)
list(APPEND result "-DSWIFT_STDLIB_CONCURRENCY_TRACING")
endif()

if(SWIFT_STDLIB_USE_RELATIVE_PROTOCOL_WITNESS_TABLES)
list(APPEND result "-DSWIFT_STDLIB_USE_RELATIVE_PROTOCOL_WITNESS_TABLES")
endif()

list(APPEND result ${SWIFT_STDLIB_EXTRA_C_COMPILE_FLAGS})

set("${CFLAGS_RESULT_VAR_NAME}" "${result}" PARENT_SCOPE)
Expand Down
4 changes: 4 additions & 0 deletions stdlib/cmake/modules/StdlibOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,7 @@ option(SWIFT_STDLIB_CONCURRENCY_TRACING
"Enable concurrency tracing in the runtime; assumes the presence of os_log(3)
and the os_signpost(3) API."
"${SWIFT_STDLIB_CONCURRENCY_TRACING_default}")

option(SWIFT_STDLIB_USE_RELATIVE_PROTOCOL_WITNESS_TABLES
"Use relative protocol witness tables"
FALSE)
4 changes: 4 additions & 0 deletions stdlib/cmake/modules/SwiftSource.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,10 @@ function(_compile_swift_files
list(APPEND swift_flags "-experimental-hermetic-seal-at-link")
endif()

if (SWIFT_STDLIB_USE_RELATIVE_PROTOCOL_WITNESS_TABLES)
list(APPEND swift_flags "-Xfrontend" "-enable-relative-protocol-witness-tables")
endif()

if(SWIFT_STDLIB_DISABLE_INSTANTIATION_CACHES)
list(APPEND swift_flags "-Xfrontend" "-disable-preallocated-instantiation-caches")
endif()
Expand Down
Loading