Skip to content

Account for whether artificial subclasses are skipped on type caching #68404

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 1 commit into from
Sep 12, 2023
Merged
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
53 changes: 35 additions & 18 deletions include/swift/Remote/MetadataReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef SWIFT_REMOTE_METADATAREADER_H
#define SWIFT_REMOTE_METADATAREADER_H

#include "llvm/ADT/Hashing.h"

#include "swift/Runtime/Metadata.h"
#include "swift/Remote/MemoryReader.h"
#include "swift/Demangling/Demangler.h"
Expand Down Expand Up @@ -193,8 +195,18 @@ class MetadataReader {
/// amounts of data when we encounter corrupt values for sizes/counts.
static const uint64_t MaxMetadataSize = 1048576; // 1MB

/// A cache of built types, keyed by the address of the type.
std::unordered_map<StoredPointer, BuiltType> TypeCache;
/// Define a has function for a std::pair<StoredPointer, bool>
struct TypeCacheKeyHash {
std::size_t operator()(const std::pair<StoredPointer, bool> &x) const {
return llvm::hash_combine(x.first, x.second);
}
};

/// A cache of built types, keyed by the address of the type and whether the
/// request ignored articial superclasses or not.
std::unordered_map<std::pair<StoredPointer, bool>, BuiltType,
TypeCacheKeyHash>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious: If we are using the LLVM libraries anyway, why not use a DenseMap?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we can, I'll change this in a separate patch.

TypeCache;

using MetadataRef = RemoteRef<const TargetMetadata<Runtime>>;
using OwnedMetadataRef = MemoryReader::ReadBytesResult;
Expand Down Expand Up @@ -821,7 +833,9 @@ class MetadataReader {
readTypeFromMetadata(StoredPointer MetadataAddress,
bool skipArtificialSubclasses = false,
int recursion_limit = defaultTypeRecursionLimit) {
auto Cached = TypeCache.find(MetadataAddress);
std::pair<StoredPointer, bool> TypeCacheKey(MetadataAddress,
skipArtificialSubclasses);
auto Cached = TypeCache.find(TypeCacheKey);
if (Cached != TypeCache.end())
return Cached->second;

Expand All @@ -841,7 +855,7 @@ class MetadataReader {
// Insert a negative result into the cache now so that, if we recur with
// the same address, we will return the negative result with the check
// just above.
TypeCache.insert({MetadataAddress, BuiltType()});
TypeCache.insert({TypeCacheKey, BuiltType()});

auto Meta = readMetadata(MetadataAddress);
if (!Meta) return BuiltType();
Expand Down Expand Up @@ -890,7 +904,7 @@ class MetadataReader {

auto BuiltTuple =
Builder.createTupleType(elementTypes, labels);
TypeCache[MetadataAddress] = BuiltTuple;
TypeCache[TypeCacheKey] = BuiltTuple;
return BuiltTuple;
}
case MetadataKind::Function: {
Expand Down Expand Up @@ -947,7 +961,7 @@ class MetadataReader {

auto BuiltFunction = Builder.createFunctionType(
Parameters, Result, flags, diffKind, globalActor);
TypeCache[MetadataAddress] = BuiltFunction;
TypeCache[TypeCacheKey] = BuiltFunction;
return BuiltFunction;
}
case MetadataKind::Existential: {
Expand Down Expand Up @@ -998,7 +1012,7 @@ class MetadataReader {
}
auto BuiltExist = Builder.createProtocolCompositionType(
Protocols, SuperclassType, HasExplicitAnyObject);
TypeCache[MetadataAddress] = BuiltExist;
TypeCache[TypeCacheKey] = BuiltExist;
return BuiltExist;
}
case MetadataKind::ExtendedExistential: {
Expand Down Expand Up @@ -1076,7 +1090,7 @@ class MetadataReader {
}
}

TypeCache[MetadataAddress] = builtProto;
TypeCache[TypeCacheKey] = builtProto;
return builtProto;
}

Expand All @@ -1086,7 +1100,7 @@ class MetadataReader {
readTypeFromMetadata(Metatype->InstanceType, false, recursion_limit);
if (!Instance) return BuiltType();
auto BuiltMetatype = Builder.createMetatypeType(Instance);
TypeCache[MetadataAddress] = BuiltMetatype;
TypeCache[TypeCacheKey] = BuiltMetatype;
return BuiltMetatype;
}
case MetadataKind::ObjCClassWrapper: {
Expand All @@ -1098,7 +1112,7 @@ class MetadataReader {
return BuiltType();

auto BuiltObjCClass = Builder.createObjCClassType(std::move(className));
TypeCache[MetadataAddress] = BuiltObjCClass;
TypeCache[TypeCacheKey] = BuiltObjCClass;
return BuiltObjCClass;
}
case MetadataKind::ExistentialMetatype: {
Expand All @@ -1107,7 +1121,7 @@ class MetadataReader {
readTypeFromMetadata(Exist->InstanceType, false, recursion_limit);
if (!Instance) return BuiltType();
auto BuiltExist = Builder.createExistentialMetatypeType(Instance);
TypeCache[MetadataAddress] = BuiltExist;
TypeCache[TypeCacheKey] = BuiltExist;
return BuiltExist;
}
case MetadataKind::ForeignReferenceType:
Expand All @@ -1131,7 +1145,7 @@ class MetadataReader {
auto name = mangling.result();

auto BuiltForeign = Builder.createForeignClassType(std::move(name));
TypeCache[MetadataAddress] = BuiltForeign;
TypeCache[TypeCacheKey] = BuiltForeign;
return BuiltForeign;
}
case MetadataKind::HeapLocalVariable:
Expand All @@ -1142,7 +1156,7 @@ class MetadataReader {
case MetadataKind::Opaque:
default: {
auto BuiltOpaque = Builder.getOpaqueType();
TypeCache[MetadataAddress] = BuiltOpaque;
TypeCache[TypeCacheKey] = BuiltOpaque;
return BuiltOpaque;
}
}
Expand Down Expand Up @@ -3084,9 +3098,12 @@ class MetadataReader {
// If we've skipped an artificial subclasses, check the cache at
// the superclass. (This also protects against recursion.)
if (skipArtificialSubclasses && metadata != origMetadata) {
auto it = TypeCache.find(getAddress(metadata));
if (it != TypeCache.end())
auto it =
TypeCache.find({getAddress(metadata), skipArtificialSubclasses});
if (it != TypeCache.end()) {
TypeCache.erase({getAddress(origMetadata), skipArtificialSubclasses});
return it->second;
}
}

// Read the nominal type descriptor.
Expand Down Expand Up @@ -3115,12 +3132,12 @@ class MetadataReader {
if (!nominal)
return BuiltType();

TypeCache[getAddress(metadata)] = nominal;
TypeCache[{getAddress(metadata), skipArtificialSubclasses}] = nominal;

// If we've skipped an artificial subclass, remove the
// recursion-protection entry we made for it.
if (skipArtificialSubclasses && metadata != origMetadata) {
TypeCache.erase(getAddress(origMetadata));
TypeCache.erase({getAddress(origMetadata), skipArtificialSubclasses});
}

return nominal;
Expand Down Expand Up @@ -3151,7 +3168,7 @@ class MetadataReader {
skipArtificialSubclasses, recursion_limit);
}

TypeCache[origMetadataPtr] = BuiltObjCClass;
TypeCache[{origMetadataPtr, skipArtificialSubclasses}] = BuiltObjCClass;
return BuiltObjCClass;
}

Expand Down