Skip to content

[5.9][Runtime] Fast lookups of Concurrency types with standard manglings. #66175

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
15 changes: 15 additions & 0 deletions include/swift/Runtime/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,21 @@ void swift_enableDynamicReplacementScope(const DynamicReplacementScope *scope);
SWIFT_RUNTIME_EXPORT
void swift_disableDynamicReplacementScope(const DynamicReplacementScope *scope);

/// A struct containing pointers to all of the type descriptors in the
/// Concurrency runtime which have standard manglings.
struct ConcurrencyStandardTypeDescriptors {
#define STANDARD_TYPE(KIND, MANGLING, TYPENAME)
#define STANDARD_TYPE_CONCURRENCY(KIND, MANGLING, TYPENAME) \
const ContextDescriptor *TYPENAME;
#include "swift/Demangling/StandardTypesMangling.def"
};

/// Register the type descriptors with standard manglings from the Concurrency
/// runtime. The passed-in struct must be immortal.
SWIFT_RUNTIME_STDLIB_SPI
void _swift_registerConcurrencyStandardTypeDescriptors(
const ConcurrencyStandardTypeDescriptors *descriptors);

#pragma clang diagnostic pop

} // end namespace swift
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/Concurrency/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ add_swift_target_library(swift_Concurrency ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} I
GlobalActor.swift
MainActor.swift
PartialAsyncTask.swift
Setup.cpp
SourceCompatibilityShims.swift
Task.cpp
Task.swift
Expand Down
47 changes: 47 additions & 0 deletions stdlib/public/Concurrency/Setup.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//===--- Setup.cpp - Load-time setup code ---------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "swift/Runtime/Metadata.h"

// Helper macros for figuring out the mangled name of a context descriptor.
#define DESCRIPTOR_MANGLING_SUFFIX_Structure Mn
#define DESCRIPTOR_MANGLING_SUFFIX_Class Mn
#define DESCRIPTOR_MANGLING_SUFFIX_Enum Mn
#define DESCRIPTOR_MANGLING_SUFFIX_Protocol Mp

#define DESCRIPTOR_MANGLING_SUFFIX_(X) X
#define DESCRIPTOR_MANGLING_SUFFIX(KIND) \
DESCRIPTOR_MANGLING_SUFFIX_(DESCRIPTOR_MANGLING_SUFFIX_##KIND)

#define DESCRIPTOR_MANGLING_(CHAR, SUFFIX) $sSc##CHAR##SUFFIX
#define DESCRIPTOR_MANGLING(CHAR, SUFFIX) DESCRIPTOR_MANGLING_(CHAR, SUFFIX)

// Declare context descriptors for all of the concurrency descriptors with
// standard manglings.
#define STANDARD_TYPE(KIND, MANGLING, TYPENAME)
#define STANDARD_TYPE_CONCURRENCY(KIND, MANGLING, TYPENAME) \
extern "C" const swift::ContextDescriptor DESCRIPTOR_MANGLING( \
MANGLING, DESCRIPTOR_MANGLING_SUFFIX(KIND));
#include "swift/Demangling/StandardTypesMangling.def"

// Register our type descriptors with standard manglings when the concurrency
// runtime is loaded. This allows the runtime to quickly resolve those standard
// manglings.
__attribute__((constructor)) static void setupStandardConcurrencyDescriptors() {
static const swift::ConcurrencyStandardTypeDescriptors descriptors = {
#define STANDARD_TYPE(KIND, MANGLING, TYPENAME)
#define STANDARD_TYPE_CONCURRENCY(KIND, MANGLING, TYPENAME) \
&DESCRIPTOR_MANGLING(MANGLING, DESCRIPTOR_MANGLING_SUFFIX(KIND)),
#include "swift/Demangling/StandardTypesMangling.def"
};
_swift_registerConcurrencyStandardTypeDescriptors(&descriptors);
}
12 changes: 11 additions & 1 deletion stdlib/public/runtime/MetadataLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,8 @@ _searchTypeMetadataRecords(TypeMetadataPrivateState &T,

#include "swift/Demangling/StandardTypesMangling.def"

static const ConcurrencyStandardTypeDescriptors *concurrencyDescriptors;

static const ContextDescriptor *
_findContextDescriptor(Demangle::NodePointer node,
Demangle::Demangler &Dem) {
Expand Down Expand Up @@ -797,7 +799,10 @@ _findContextDescriptor(Demangle::NodePointer node,
}
// FIXME: When the _Concurrency library gets merged into the Standard Library,
// we will be able to reference those symbols directly as well.
#define STANDARD_TYPE_CONCURRENCY(KIND, MANGLING, TYPENAME)
#define STANDARD_TYPE_CONCURRENCY(KIND, MANGLING, TYPENAME) \
if (concurrencyDescriptors && name.equals(#TYPENAME)) { \
return concurrencyDescriptors->TYPENAME; \
}
#if !SWIFT_OBJC_INTEROP
# define OBJC_INTEROP_STANDARD_TYPE(KIND, MANGLING, TYPENAME)
#endif
Expand Down Expand Up @@ -851,6 +856,11 @@ _findContextDescriptor(Demangle::NodePointer node,
return foundContext;
}

void swift::_swift_registerConcurrencyStandardTypeDescriptors(
const ConcurrencyStandardTypeDescriptors *descriptors) {
concurrencyDescriptors = descriptors;
}

#pragma mark Protocol descriptor cache
namespace {
struct ProtocolSection {
Expand Down