Skip to content

[Runtime] Cache protocol conformance descriptors, not witness tables. #20491

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
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
19 changes: 19 additions & 0 deletions include/swift/Basic/Lazy.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ template <class T> class Lazy {

T &get(void (*initCallback)(void *) = defaultInitCallback);

template<typename Arg1>
T &getWithInit(Arg1 &&arg1);

/// Get the value, assuming it must have already been initialized by this
/// point.
T &unsafeGetAlreadyInitialized() { return *reinterpret_cast<T *>(&Value); }
Expand All @@ -80,6 +83,22 @@ template <typename T> inline T &Lazy<T>::get(void (*initCallback)(void*)) {
return unsafeGetAlreadyInitialized();
}

template <typename T>
template <typename Arg1> inline T &Lazy<T>::getWithInit(Arg1 &&arg1) {
struct Data {
void *address;
Arg1 &&arg1;

static void init(void *context) {
Data *data = static_cast<Data *>(context);
::new (data->address) T(static_cast<Arg1&&>(data->arg1));
}
} data{&Value, static_cast<Arg1&&>(arg1)};

SWIFT_ONCE_F(OnceToken, &Data::init, &data);
return unsafeGetAlreadyInitialized();
}

} // end namespace swift

#define SWIFT_LAZY_CONSTANT(INITIAL_VALUE) \
Expand Down
15 changes: 15 additions & 0 deletions lib/Demangling/OldRemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,20 @@ void Remangler::mangleEntityContext(Node *node, EntityContext &ctx) {
// Remember that we're mangling a context.
EntityContext::ManglingContextRAII raii(ctx);

// Deal with bound generic types.
switch (node->getKind()) {
case Node::Kind::BoundGenericStructure:
case Node::Kind::BoundGenericEnum:
case Node::Kind::BoundGenericClass:
case Node::Kind::BoundGenericOtherNominalType:
case Node::Kind::BoundGenericTypeAlias:
mangleAnyNominalType(node, ctx);
return;

default:
break;
}

switch (node->getKind()) {
#define NODE(ID) \
case Node::Kind::ID:
Expand Down Expand Up @@ -1731,6 +1745,7 @@ void Remangler::mangleExtension(Node *node, EntityContext &ctx) {
if (node->getNumChildren() == 3) {
mangleDependentGenericSignature(node->begin()[2]); // generic sig
}

mangleEntityContext(node->begin()[1], ctx); // context
}

Expand Down
11 changes: 7 additions & 4 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,8 @@ void IRGenModule::emitRuntimeRegistration() {
/// For any other kind of context, this returns an anonymous context descriptor
/// for the context.
ConstantReference
IRGenModule::getAddrOfParentContextDescriptor(DeclContext *from) {
IRGenModule::getAddrOfParentContextDescriptor(DeclContext *from,
bool fromAnonymousContext) {
// Some types get special treatment.
if (auto Type = dyn_cast<NominalTypeDecl>(from)) {
// Use a special module context if we have one.
Expand All @@ -757,7 +758,7 @@ IRGenModule::getAddrOfParentContextDescriptor(DeclContext *from) {

// Wrap up private types in an anonymous context for the containing file
// unit so that the runtime knows they have unstable identity.
if (Type->isOutermostPrivateOrFilePrivateScope())
if (!fromAnonymousContext && Type->isOutermostPrivateOrFilePrivateScope())
return {getAddrOfAnonymousContextDescriptor(Type),
ConstantReference::Direct};
}
Expand All @@ -771,15 +772,17 @@ IRGenModule::getAddrOfParentContextDescriptor(DeclContext *from) {
case DeclContextKind::TopLevelCodeDecl:
case DeclContextKind::Initializer:
case DeclContextKind::SerializedLocal:
return {getAddrOfAnonymousContextDescriptor(from),
return {getAddrOfAnonymousContextDescriptor(
fromAnonymousContext ? parent : from),
ConstantReference::Direct};

case DeclContextKind::GenericTypeDecl:
if (auto nomTy = dyn_cast<NominalTypeDecl>(parent)) {
return {getAddrOfTypeContextDescriptor(nomTy, DontRequireMetadata),
ConstantReference::Direct};
}
return {getAddrOfAnonymousContextDescriptor(from),
return {getAddrOfAnonymousContextDescriptor(
fromAnonymousContext ? parent : from),
ConstantReference::Direct};

case DeclContextKind::ExtensionDecl: {
Expand Down
10 changes: 6 additions & 4 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,8 @@ namespace {
}

ConstantReference getParent() {
return {IGM.getAddrOfModuleContextDescriptor(DC->getParentModule()),
ConstantReference::Direct};
return IGM.getAddrOfParentContextDescriptor(
DC, /*fromAnonymousContext=*/true);
}

ContextDescriptorKind getContextKind() {
Expand Down Expand Up @@ -565,7 +565,8 @@ namespace {
}

ConstantReference getParent() {
return IGM.getAddrOfParentContextDescriptor(Proto);
return IGM.getAddrOfParentContextDescriptor(
Proto, /*fromAnonymousContext=*/false);
}

ContextDescriptorKind getContextKind() {
Expand Down Expand Up @@ -1014,7 +1015,8 @@ namespace {
}

ConstantReference getParent() {
return IGM.getAddrOfParentContextDescriptor(Type);
return IGM.getAddrOfParentContextDescriptor(
Type, /*fromAnonymousContext=*/false);
}

GenericSignature *getGenericSignature() {
Expand Down
3 changes: 2 additions & 1 deletion lib/IRGen/GenProto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1937,7 +1937,8 @@ namespace {
auto moduleContext =
normal->getDeclContext()->getModuleScopeContext();
ConstantReference moduleContextRef =
IGM.getAddrOfParentContextDescriptor(moduleContext);
IGM.getAddrOfParentContextDescriptor(moduleContext,
/*fromAnonymousContext=*/false);
B.addRelativeAddress(moduleContextRef);
}

Expand Down
3 changes: 2 additions & 1 deletion lib/IRGen/IRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,8 @@ private: \
ConstantInit definition = ConstantInit());
llvm::Constant *getAddrOfObjCModuleContextDescriptor();
llvm::Constant *getAddrOfClangImporterModuleContextDescriptor();
ConstantReference getAddrOfParentContextDescriptor(DeclContext *from);
ConstantReference getAddrOfParentContextDescriptor(DeclContext *from,
bool fromAnonymousContext);
llvm::Constant *getAddrOfGenericEnvironment(CanGenericSignature signature);
llvm::Constant *getAddrOfProtocolRequirementsBaseDescriptor(
ProtocolDecl *proto);
Expand Down
51 changes: 42 additions & 9 deletions stdlib/public/runtime/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,39 @@ namespace {
};
} // end anonymous namespace

using GenericMetadataCache = MetadataCache<GenericCacheEntry, false>;
using LazyGenericMetadataCache = Lazy<GenericMetadataCache>;
namespace {
class GenericMetadataCache : public MetadataCache<GenericCacheEntry, false> {
public:
uint16_t NumKeyParameters;
uint16_t NumWitnessTables;

GenericMetadataCache(const TargetGenericContext<InProcess> &genericContext)
: NumKeyParameters(0), NumWitnessTables(0) {
// Count up the # of key parameters and # of witness tables.

// Find key generic parameters.
for (const auto &gp : genericContext.getGenericParams()) {
if (gp.hasKeyArgument())
++NumKeyParameters;
}

// Find witness tables.
for (const auto &req : genericContext.getGenericRequirements()) {
if (req.Flags.hasKeyArgument() &&
req.getKind() == GenericRequirementKind::Protocol)
++NumWitnessTables;
}
}
};

using LazyGenericMetadataCache = Lazy<GenericMetadataCache>;
}

/// Fetch the metadata cache for a generic metadata structure.
static GenericMetadataCache &getCache(
const TypeGenericContextDescriptorHeader &generics) {
const TypeContextDescriptor &description) {
auto &generics = description.getFullGenericContextHeader();

// Keep this assert even if you change the representation above.
static_assert(sizeof(LazyGenericMetadataCache) <=
sizeof(GenericMetadataInstantiationCache::PrivateData),
Expand All @@ -282,7 +309,7 @@ static GenericMetadataCache &getCache(
auto lazyCache =
reinterpret_cast<LazyGenericMetadataCache*>(
generics.getInstantiationCache()->PrivateData);
return lazyCache->get();
return lazyCache->getWithInit(*description.getGenericContext());
}

/// Fetch the metadata cache for a generic metadata structure,
Expand Down Expand Up @@ -527,9 +554,11 @@ swift::swift_getGenericMetadata(MetadataRequest request,
auto &generics = description->getFullGenericContextHeader();
size_t numGenericArgs = generics.Base.NumKeyArguments;

auto key = MetadataCacheKey(arguments, numGenericArgs);
auto result =
getCache(generics).getOrInsert(key, request, description, arguments);
auto &cache = getCache(*description);
assert(numGenericArgs == cache.NumKeyParameters + cache.NumWitnessTables);
auto key = MetadataCacheKey(cache.NumKeyParameters, cache.NumWitnessTables,
arguments);
auto result = cache.getOrInsert(key, request, description, arguments);

return result.second;
}
Expand Down Expand Up @@ -4337,11 +4366,15 @@ static Result performOnMetadataCache(const Metadata *metadata,
auto genericArgs =
reinterpret_cast<const void * const *>(
description->getGenericArguments(metadata));
auto &cache = getCache(*description);
size_t numGenericArgs = generics.Base.NumKeyArguments;
auto key = MetadataCacheKey(genericArgs, numGenericArgs);
assert(numGenericArgs == cache.NumKeyParameters + cache.NumWitnessTables);
(void)numGenericArgs;
auto key = MetadataCacheKey(cache.NumKeyParameters, cache.NumWitnessTables,
genericArgs);

return std::move(callbacks).forGenericMetadata(metadata, description,
getCache(generics), key);
cache, key);
}

bool swift::addToMetadataQueue(MetadataCompletionQueueEntry *queueEntry,
Expand Down
Loading