Skip to content

Commit ecad335

Browse files
authored
Merge pull request #32476 from nate-chandler/runtime/add-swift_compareTypeContextDescriptors
2 parents aa5d671 + 2c6d7d7 commit ecad335

File tree

6 files changed

+71
-0
lines changed

6 files changed

+71
-0
lines changed

include/swift/AST/ASTContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,10 @@ class ASTContext final {
649649
/// metadata.
650650
AvailabilityContext getPrespecializedGenericMetadataAvailability();
651651

652+
/// Get the runtime availability of the swift_compareTypeContextDescriptors
653+
/// for the target platform.
654+
AvailabilityContext getCompareTypeContextDescriptorsAvailability();
655+
652656
/// Get the runtime availability of features introduced in the Swift 5.2
653657
/// compiler for the target platform.
654658
AvailabilityContext getSwift52Availability();

include/swift/Runtime/Metadata.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,20 @@ const
294294
/// the same context.
295295
bool equalContexts(const ContextDescriptor *a, const ContextDescriptor *b);
296296

297+
/// Determines whether two type context descriptors describe the same type
298+
/// context.
299+
///
300+
/// Runtime availability: Swift 5.4.
301+
///
302+
/// \param lhs The first type context descriptor to compare.
303+
/// \param rhs The second type context descriptor to compare.
304+
///
305+
/// \returns true if both describe the same type context, false otherwise.
306+
SWIFT_RUNTIME_EXPORT
307+
SWIFT_CC(swift)
308+
bool swift_compareTypeContextDescriptors(const TypeContextDescriptor *lhs,
309+
const TypeContextDescriptor *rhs);
310+
297311
/// Compute the bounds of class metadata with a resilient superclass.
298312
ClassMetadataBounds getResilientMetadataBounds(
299313
const ClassDescriptor *descriptor);

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,18 @@ FUNCTION(GetForeignTypeMetadata, swift_getForeignTypeMetadata,
616616
ARGS(SizeTy, TypeMetadataPtrTy),
617617
ATTRS(NoUnwind, ReadNone)) // only writes to runtime-private fields
618618

619+
// SWIFT_RUNTIME_EXPORT
620+
// SWIFT_CC(swift)
621+
// bool swift_compareTypeContextDescriptors(const TypeContextDescriptor *lhs,
622+
// const TypeContextDescriptor *rhs);
623+
FUNCTION(CompareTypeContextDescriptors,
624+
swift_compareTypeContextDescriptors, SwiftCC,
625+
CompareTypeContextDescriptorsAvailability,
626+
RETURNS(Int1Ty),
627+
ARGS(TypeContextDescriptorPtrTy,
628+
TypeContextDescriptorPtrTy),
629+
ATTRS(NoUnwind, ReadNone))
630+
619631
// MetadataResponse swift_getSingletonMetadata(MetadataRequest request,
620632
// TypeContextDescriptor *type);
621633
FUNCTION(GetSingletonMetadata, swift_getSingletonMetadata,

lib/AST/Availability.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ AvailabilityContext ASTContext::getPrespecializedGenericMetadataAvailability() {
295295
return getSwift53Availability();
296296
}
297297

298+
AvailabilityContext ASTContext::getCompareTypeContextDescriptorsAvailability() {
299+
return getSwiftFutureAvailability();
300+
}
301+
298302
AvailabilityContext ASTContext::getSwift52Availability() {
299303
auto target = LangOpts.Target;
300304

lib/IRGen/IRGenModule.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,16 @@ namespace RuntimeConstants {
650650
}
651651
return RuntimeAvailability::AlwaysAvailable;
652652
}
653+
654+
RuntimeAvailability
655+
CompareTypeContextDescriptorsAvailability(ASTContext &Context) {
656+
auto featureAvailability =
657+
Context.getCompareTypeContextDescriptorsAvailability();
658+
if (!isDeploymentAvailabilityContainedIn(Context, featureAvailability)) {
659+
return RuntimeAvailability::ConditionallyAvailable;
660+
}
661+
return RuntimeAvailability::AlwaysAvailable;
662+
}
653663
} // namespace RuntimeConstants
654664

655665
// We don't use enough attributes to justify generalizing the

stdlib/public/runtime/Metadata.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,6 +1913,33 @@ bool swift::equalContexts(const ContextDescriptor *a,
19131913
}
19141914
}
19151915

1916+
SWIFT_CC(swift)
1917+
bool swift::swift_compareTypeContextDescriptors(
1918+
const TypeContextDescriptor *a, const TypeContextDescriptor *b) {
1919+
// The implementation is the same as the implementation of
1920+
// swift::equalContexts except that the handling of non-type
1921+
// context descriptors and casts to TypeContextDescriptor are removed.
1922+
1923+
// Fast path: pointer equality.
1924+
if (a == b) return true;
1925+
1926+
// If either context is null, we're done.
1927+
if (a == nullptr || b == nullptr)
1928+
return false;
1929+
1930+
// If either descriptor is known to be unique, we're done.
1931+
if (a->isUnique() || b->isUnique()) return false;
1932+
1933+
// Do the kinds match?
1934+
if (a->getKind() != b->getKind()) return false;
1935+
1936+
// Do the parents match?
1937+
if (!equalContexts(a->Parent.get(), b->Parent.get()))
1938+
return false;
1939+
1940+
return TypeContextIdentity(a) == TypeContextIdentity(b);
1941+
}
1942+
19161943
/***************************************************************************/
19171944
/*** Common value witnesses ************************************************/
19181945
/***************************************************************************/

0 commit comments

Comments
 (0)