Skip to content

[Metadata/IRGen] NFC: Couple of field metadata related improvements #14776

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 2 commits into from
Feb 22, 2018
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
8 changes: 4 additions & 4 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ IRGenModule::getAddrOfParentContextDescriptor(DeclContext *from) {
return getAddrOfLLVMVariableOrGOTEquivalent(
LinkEntity::forNominalTypeDescriptor(nominal),
Alignment(4),
NominalTypeDescriptorTy,
TypeContextDescriptorTy,
shouldBeIndirect);
}
}
Expand Down Expand Up @@ -2495,8 +2495,8 @@ getTypeEntityInfo(IRGenModule &IGM, CanType conformingType) {
// are represented by referencing the nominal type descriptor.
typeKind = TypeMetadataRecordKind::DirectNominalTypeDescriptor;
entity = LinkEntity::forNominalTypeDescriptor(nom);
defaultTy = IGM.NominalTypeDescriptorTy;
defaultPtrTy = IGM.NominalTypeDescriptorPtrTy;
defaultTy = IGM.TypeContextDescriptorTy;
defaultPtrTy = IGM.TypeContextDescriptorPtrTy;
}

return {typeKind, *entity, defaultTy, defaultPtrTy};
Expand Down Expand Up @@ -3305,7 +3305,7 @@ llvm::Constant *IRGenModule::getAddrOfTypeContextDescriptor(NominalTypeDecl *D,
auto entity = LinkEntity::forNominalTypeDescriptor(D);
return getAddrOfLLVMVariable(entity, Alignment(4),
definition,
NominalTypeDescriptorTy,
TypeContextDescriptorTy,
DebugTypeInfo());
}

Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3030,7 +3030,7 @@ IRGenModule::getAddrOfSharedContextDescriptor(LinkEntity entity,

return getAddrOfLLVMVariable(entity, Alignment(4),
definition,
NominalTypeDescriptorTy,
TypeContextDescriptorTy,
DebugTypeInfo());
}

Expand Down
37 changes: 21 additions & 16 deletions lib/IRGen/IRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,25 +307,30 @@ IRGenModule::IRGenModule(IRGenerator &irgen,
ProtocolConformanceDescriptorPtrTy
= ProtocolConformanceDescriptorTy->getPointerTo(DefaultAS);

NominalTypeDescriptorTy
TypeContextDescriptorTy
= llvm::StructType::create(LLVMContext, "swift.type_descriptor");
NominalTypeDescriptorPtrTy
= NominalTypeDescriptorTy->getPointerTo(DefaultAS);
TypeContextDescriptorPtrTy
= TypeContextDescriptorTy->getPointerTo(DefaultAS);

ClassNominalTypeDescriptorTy =
ClassContextDescriptorTy =
llvm::StructType::get(LLVMContext, {
Int32Ty,
Int32Ty,
Int32Ty,
Int32Ty,
Int32Ty,
Int32Ty,
Int32Ty,
Int32Ty,
Int32Ty,
Int16Ty,
Int16Ty,
Int32Ty,
Int32Ty, // context flags
Int32Ty, // parent
Int32Ty, // name
Int32Ty, // kind
Int32Ty, // accessor function
Int32Ty, // num fields
Int32Ty, // field offset vector
Int32Ty, // is_reflectable flag
Int32Ty, // (Generics Descriptor) argument offset
Int32Ty, // (Generics Descriptor) num params
Int32Ty, // (Generics Descriptor) num requirements
Int32Ty, // (Generics Descriptor) num key arguments
Int32Ty, // (Generics Descriptor) num extra arguments
Int32Ty, // (VTable Descriptor) offset
Int32Ty, // (VTable Descriptor) size
Int32Ty, // (Methods Descriptor) accessor
Int32Ty, // (Methods Descriptor) flags
}, /*packed=*/true);

MethodDescriptorStructTy
Expand Down
6 changes: 3 additions & 3 deletions lib/IRGen/IRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,9 @@ class IRGenModule {
llvm::PointerType *ProtocolRecordPtrTy;
llvm::StructType *ProtocolConformanceDescriptorTy;
llvm::PointerType *ProtocolConformanceDescriptorPtrTy;
llvm::StructType *NominalTypeDescriptorTy;
llvm::PointerType *NominalTypeDescriptorPtrTy;
llvm::StructType *ClassNominalTypeDescriptorTy;
llvm::StructType *TypeContextDescriptorTy;
llvm::PointerType *TypeContextDescriptorPtrTy;
llvm::StructType *ClassContextDescriptorTy;
llvm::StructType *MethodDescriptorStructTy; /// %swift.method_descriptor
llvm::StructType *TypeMetadataRecordTy;
llvm::PointerType *TypeMetadataRecordPtrTy;
Expand Down
66 changes: 34 additions & 32 deletions stdlib/public/runtime/MetadataLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,6 @@ static Demangler getDemanglerForRuntimeTypeResolution() {
return dem;
}

template <typename T> struct DescriptorCacheEntry {
private:
std::string Name;
const T *Description;

public:
DescriptorCacheEntry(const llvm::StringRef name, const T *description)
: Name(name.str()), Description(description) {}

const T *getDescription() { return Description; }

int compareWithKey(llvm::StringRef aName) const {
return aName.compare(Name);
}

template <class... Args>
static size_t getExtraAllocationSize(Args &&... ignored) {
return 0;
}
};

#pragma mark Nominal type descriptor cache
// Type Metadata Cache.

Expand Down Expand Up @@ -333,8 +312,30 @@ namespace {
}
};

struct ProtocolDescriptorCacheEntry {
private:
std::string Name;
const ProtocolDescriptor *Description;

public:
ProtocolDescriptorCacheEntry(const llvm::StringRef name,
const ProtocolDescriptor *description)
: Name(name.str()), Description(description) {}

const ProtocolDescriptor *getDescription() { return Description; }

int compareWithKey(llvm::StringRef aName) const {
return aName.compare(Name);
}

template <class... T>
static size_t getExtraAllocationSize(T &&... ignored) {
return 0;
}
};

struct ProtocolMetadataState {
ConcurrentMap<DescriptorCacheEntry<ProtocolDescriptor>> ProtocolCache;
ConcurrentMap<ProtocolDescriptorCacheEntry> ProtocolCache;
std::vector<ProtocolSection> SectionsToScan;
Mutex SectionsToScanLock;

Expand Down Expand Up @@ -425,19 +426,22 @@ _findProtocolDescriptor(llvm::StringRef mangledName) {

#pragma mark Type field descriptor cache
namespace {
template <typename T> struct FieldDescriptorCacheEntry {
struct FieldDescriptorCacheEntry {
private:
const Metadata *Base;
const T *Description;
const Metadata *Type;
const FieldDescriptor *Description;

public:
FieldDescriptorCacheEntry(const Metadata *Base, const T *description)
: Base(Base), Description(description) {}
FieldDescriptorCacheEntry(const Metadata *type,
const FieldDescriptor *description)
: Type(type), Description(description) {}

const T *getDescription() { return Description; }
const FieldDescriptor *getDescription() { return Description; }

int compareWithKey(const Metadata *other) const {
return Base == other;
auto a = (uintptr_t)Type;
auto b = (uintptr_t)other;
return a == b ? 0 : (a < b ? -1 : 1);
}

template <class... Args>
Expand Down Expand Up @@ -477,7 +481,7 @@ class DynamicFieldSection {
};

struct FieldCacheState {
ConcurrentMap<FieldDescriptorCacheEntry<FieldDescriptor>> FieldCache;
ConcurrentMap<FieldDescriptorCacheEntry> FieldCache;

Mutex SectionsLock;
std::vector<StaticFieldSection> StaticSections;
Expand Down Expand Up @@ -1122,12 +1126,10 @@ void swift::swift_getFieldAt(


// Fast path: If we already have field descriptor cached.
/*
if (auto Value = cache.FieldCache.find(base)) {
getFieldAt(*Value->getDescription());
return;
}
*/

ScopedLock guard(cache.SectionsLock);
// Otherwise let's try to find it in one of the sections.
Expand Down