Skip to content

IRGen: Access concrete type metadata by mangled name. #26455

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 3 commits into from
Aug 5, 2019
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
1 change: 1 addition & 0 deletions include/swift/Demangling/DemangleNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ NODE(TypeMetadataCompletionFunction)
NODE(TypeMetadataInstantiationCache)
NODE(TypeMetadataInstantiationFunction)
NODE(TypeMetadataSingletonInitializationCache)
NODE(TypeMetadataDemanglingCache)
NODE(TypeMetadataLazyCache)
NODE(UncurriedFunctionType)
NODE(UnknownIndex)
Expand Down
10 changes: 10 additions & 0 deletions include/swift/IRGen/Linking.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ class LinkEntity {
/// The pointer is a canonical TypeBase*.
TypeMetadataLazyCacheVariable,

/// A lazy cache variable for fetching type metadata from a mangled name.
/// The pointer is a canonical TypeBase*.
TypeMetadataDemanglingCacheVariable,

/// A reflection metadata descriptor for a builtin or imported type.
ReflectionBuiltinDescriptor,

Expand Down Expand Up @@ -705,6 +709,12 @@ class LinkEntity {
return entity;
}

static LinkEntity forTypeMetadataDemanglingCacheVariable(CanType type) {
LinkEntity entity;
entity.setForType(Kind::TypeMetadataDemanglingCacheVariable, type);
return entity;
}

static LinkEntity forClassMetadataBaseOffset(ClassDecl *decl) {
LinkEntity entity;
entity.setForDecl(Kind::ClassMetadataBaseOffset, decl);
Expand Down
13 changes: 12 additions & 1 deletion include/swift/Runtime/RuntimeFunctions.def
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ FUNCTION(IsOptionalType,
FUNCTION(Once, swift_once, C_CC, AlwaysAvailable,
RETURNS(VoidTy),
ARGS(OnceTy->getPointerTo(), Int8PtrTy, Int8PtrTy),
ATTRS())
ATTRS(NoUnwind))

// void swift_registerProtocols(const ProtocolRecord *begin,
// const ProtocolRecord *end)
Expand Down Expand Up @@ -1398,6 +1398,17 @@ FUNCTION(IntToFloat64, swift_intToFloat64, SwiftCC, AlwaysAvailable,
ARGS(SizeTy->getPointerTo(), SizeTy),
ATTRS(NoUnwind, ReadOnly))

// const Metadata *swift_getTypeByMangledNameInContext(
// const char *typeNameStart,
// size_t typeNameLength,
// const TargetContextDescriptor<InProcess> *context,
// const void * const *genericArgs)
FUNCTION(GetTypeByMangledNameInContext, swift_getTypeByMangledNameInContext,
SwiftCC, AlwaysAvailable,
RETURNS(TypeMetadataPtrTy),
ARGS(Int8PtrTy, SizeTy, TypeContextDescriptorPtrTy, Int8PtrPtrTy),
ATTRS(NoUnwind, ArgMemOnly))

#undef RETURNS
#undef ARGS
#undef ATTRS
Expand Down
64 changes: 33 additions & 31 deletions lib/Demangling/Demangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1788,36 +1788,50 @@ NodePointer Demangler::demangleImplFunctionType() {

NodePointer Demangler::demangleMetatype() {
switch (nextChar()) {
case 'a':
return createWithPoppedType(Node::Kind::TypeMetadataAccessFunction);
case 'A':
return createWithChild(Node::Kind::ReflectionMetadataAssocTypeDescriptor,
popProtocolConformance());
case 'B':
return createWithChild(Node::Kind::ReflectionMetadataBuiltinDescriptor,
popNode(Node::Kind::Type));
case 'c':
return createWithChild(Node::Kind::ProtocolConformanceDescriptor,
popProtocolConformance());
case 'C': {
NodePointer Ty = popNode(Node::Kind::Type);
if (!Ty || !isAnyGeneric(Ty->getChild(0)->getKind()))
return nullptr;
return createWithChild(Node::Kind::ReflectionMetadataSuperclassDescriptor,
Ty->getChild(0));
}
case 'D':
return createWithPoppedType(Node::Kind::TypeMetadataDemanglingCache);
case 'f':
return createWithPoppedType(Node::Kind::FullTypeMetadata);
case 'P':
return createWithPoppedType(Node::Kind::GenericTypeMetadataPattern);
case 'a':
return createWithPoppedType(Node::Kind::TypeMetadataAccessFunction);
case 'F':
return createWithChild(Node::Kind::ReflectionMetadataFieldDescriptor,
popNode(Node::Kind::Type));
case 'g':
return createWithChild(Node::Kind::OpaqueTypeDescriptorAccessor,
popNode());
case 'h':
return createWithChild(Node::Kind::OpaqueTypeDescriptorAccessorImpl,
popNode());
case 'i':
return createWithPoppedType(Node::Kind::TypeMetadataInstantiationFunction);
case 'I':
return createWithPoppedType(Node::Kind::TypeMetadataInstantiationCache);
case 'j':
return createWithChild(Node::Kind::OpaqueTypeDescriptorAccessorKey,
popNode());
case 'k':
return createWithChild(Node::Kind::OpaqueTypeDescriptorAccessorVar,
popNode());
case 'I':
return createWithPoppedType(Node::Kind::TypeMetadataInstantiationCache);
case 'i':
return createWithPoppedType(Node::Kind::TypeMetadataInstantiationFunction);
case 'r':
return createWithPoppedType(Node::Kind::TypeMetadataCompletionFunction);
case 'l':
return createWithPoppedType(
Node::Kind::TypeMetadataSingletonInitializationCache);
Node::Kind::TypeMetadataSingletonInitializationCache);
case 'L':
return createWithPoppedType(Node::Kind::TypeMetadataLazyCache);
case 'm':
Expand All @@ -1828,35 +1842,23 @@ NodePointer Demangler::demangleMetatype() {
return createWithPoppedType(Node::Kind::ClassMetadataBaseOffset);
case 'p':
return createWithChild(Node::Kind::ProtocolDescriptor, popProtocol());
case 'P':
return createWithPoppedType(Node::Kind::GenericTypeMetadataPattern);
case 'Q':
return createWithChild(Node::Kind::OpaqueTypeDescriptor, popNode());
case 'r':
return createWithPoppedType(Node::Kind::TypeMetadataCompletionFunction);
case 's':
return createWithPoppedType(Node::Kind::ObjCResilientClassStub);
case 'S':
return createWithChild(Node::Kind::ProtocolSelfConformanceDescriptor,
popProtocol());
case 't':
return createWithPoppedType(Node::Kind::FullObjCResilientClassStub);
case 'u':
return createWithPoppedType(Node::Kind::MethodLookupFunction);
case 'U':
return createWithPoppedType(Node::Kind::ObjCMetadataUpdateFunction);
case 's':
return createWithPoppedType(Node::Kind::ObjCResilientClassStub);
case 't':
return createWithPoppedType(Node::Kind::FullObjCResilientClassStub);
case 'B':
return createWithChild(Node::Kind::ReflectionMetadataBuiltinDescriptor,
popNode(Node::Kind::Type));
case 'F':
return createWithChild(Node::Kind::ReflectionMetadataFieldDescriptor,
popNode(Node::Kind::Type));
case 'A':
return createWithChild(Node::Kind::ReflectionMetadataAssocTypeDescriptor,
popProtocolConformance());
case 'C': {
NodePointer Ty = popNode(Node::Kind::Type);
if (!Ty || !isAnyGeneric(Ty->getChild(0)->getKind()))
return nullptr;
return createWithChild(Node::Kind::ReflectionMetadataSuperclassDescriptor,
Ty->getChild(0));
}
case 'V':
return createWithChild(Node::Kind::PropertyDescriptor,
popNode(isEntity));
Expand Down
5 changes: 5 additions & 0 deletions lib/Demangling/NodePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ class NodePrinter {
case Node::Kind::TypeMetadataInstantiationCache:
case Node::Kind::TypeMetadataInstantiationFunction:
case Node::Kind::TypeMetadataSingletonInitializationCache:
case Node::Kind::TypeMetadataDemanglingCache:
case Node::Kind::TypeMetadataLazyCache:
case Node::Kind::UncurriedFunctionType:
#define REF_STORAGE(Name, ...) \
Expand Down Expand Up @@ -1679,6 +1680,10 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) {
Printer << "type metadata completion function for ";
print(Node->getChild(0));
return nullptr;
case Node::Kind::TypeMetadataDemanglingCache:
Printer << "demangling cache variable for type metadata for ";
print(Node->getChild(0));
return nullptr;
case Node::Kind::TypeMetadataLazyCache:
Printer << "lazy cache variable for type metadata for ";
print(Node->getChild(0));
Expand Down
4 changes: 4 additions & 0 deletions lib/Demangling/OldRemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,10 @@ void Remangler::mangleTypeMetadataCompletionFunction(Node *node) {
mangleSingleChildNode(node); // type
}

void Remangler::mangleTypeMetadataDemanglingCache(Node *node) {
unreachable("not supported");
}

void Remangler::mangleTypeMetadataLazyCache(Node *node) {
Buffer << "ML";
mangleSingleChildNode(node); // type
Expand Down
5 changes: 5 additions & 0 deletions lib/Demangling/Remangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2065,6 +2065,11 @@ void Remangler::mangleTypeMetadataCompletionFunction(Node *node) {
Buffer << "Mr";
}

void Remangler::mangleTypeMetadataDemanglingCache(Node *node) {
mangleChildNodes(node);
Buffer << "MD";
}

void Remangler::mangleTypeMetadataLazyCache(Node *node) {
mangleChildNodes(node);
Buffer << "ML";
Expand Down
7 changes: 7 additions & 0 deletions lib/IRGen/GenBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -799,18 +799,24 @@ if (Builtin.ID == BuiltinValueKind::id) { \

// If we know the platform runtime's "done" value, emit the check inline.
llvm::BasicBlock *doneBB = nullptr;

llvm::BasicBlock *beforeBB = IGF.Builder.GetInsertBlock();

if (auto ExpectedPred = IGF.IGM.TargetInfo.OnceDonePredicateValue) {
auto PredValue = IGF.Builder.CreateLoad(PredPtr,
IGF.IGM.getPointerAlignment());
auto ExpectedPredValue = llvm::ConstantInt::getSigned(IGF.IGM.OnceTy,
*ExpectedPred);
auto PredIsDone = IGF.Builder.CreateICmpEQ(PredValue, ExpectedPredValue);
PredIsDone = IGF.Builder.CreateExpect(PredIsDone,
llvm::ConstantInt::get(IGF.IGM.Int1Ty, 1));

auto notDoneBB = IGF.createBasicBlock("once_not_done");
doneBB = IGF.createBasicBlock("once_done");

IGF.Builder.CreateCondBr(PredIsDone, doneBB, notDoneBB);

IGF.Builder.SetInsertPoint(&IGF.CurFn->back());
IGF.Builder.emitBlock(notDoneBB);
}

Expand All @@ -822,6 +828,7 @@ if (Builtin.ID == BuiltinValueKind::id) { \
// If we emitted the "done" check inline, join the branches.
if (auto ExpectedPred = IGF.IGM.TargetInfo.OnceDonePredicateValue) {
IGF.Builder.CreateBr(doneBB);
IGF.Builder.SetInsertPoint(beforeBB);
IGF.Builder.emitBlock(doneBB);
// We can assume the once predicate is in the "done" state now.
auto PredValue = IGF.Builder.CreateLoad(PredPtr,
Expand Down
2 changes: 0 additions & 2 deletions lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1538,8 +1538,6 @@ void CallEmission::emitToUnmappedExplosion(Explosion &out) {
// for methods that have covariant ABI-compatible overrides.
auto expectedNativeResultType = nativeSchema.getExpandedType(IGF.IGM);
if (result->getType() != expectedNativeResultType) {
assert(origFnType->getLanguage() == SILFunctionLanguage::C ||
origFnType->getRepresentation() == SILFunctionTypeRepresentation::Method);
result =
IGF.coerceValue(result, expectedNativeResultType, IGF.IGM.DataLayout);
}
Expand Down
36 changes: 28 additions & 8 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/SaveAndRestore.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"

#include "Callee.h"
Expand Down Expand Up @@ -1248,6 +1249,14 @@ void IRGenerator::noteUseOfTypeGlobals(NominalTypeDecl *type,
RequireMetadata_t requireMetadata) {
if (!type)
return;

// Force emission of ObjC protocol descriptors used by type refs.
if (auto proto = dyn_cast<ProtocolDecl>(type)) {
if (proto->isObjC()) {
PrimaryIGM->getAddrOfObjCProtocolRecord(proto, NotForDefinition);
return;
}
}

if (!hasLazyMetadata(type))
return;
Expand Down Expand Up @@ -2562,7 +2571,11 @@ static llvm::GlobalVariable *createGOTEquivalent(IRGenModule &IGM,
// rdar://problem/50968433: Unnamed_addr constants appear to get emitted
// with incorrect alignment by the LLVM JIT in some cases. Don't use
// unnamed_addr as a workaround.
if (!IGM.getOptions().UseJIT) {
// rdar://problem/53836960: i386 ld64 also mis-links relative references
// to GOT entries.
if (!IGM.getOptions().UseJIT
&& (!IGM.Triple.isOSDarwin()
|| IGM.Triple.getArch() != llvm::Triple::x86)) {
gotEquivalent->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
} else {
ApplyIRLinkage(IRLinkage::InternalLinkOnceODR)
Expand Down Expand Up @@ -3411,22 +3424,29 @@ IRGenModule::getAddrOfGenericTypeMetadataAccessFunction(
/// Get or create a type metadata cache variable. These are an
/// implementation detail of type metadata access functions.
llvm::Constant *
IRGenModule::getAddrOfTypeMetadataLazyCacheVariable(CanType type,
ForDefinition_t forDefinition) {
IRGenModule::getAddrOfTypeMetadataLazyCacheVariable(CanType type) {
assert(!type->hasArchetype() && !type->hasTypeParameter());
LinkEntity entity = LinkEntity::forTypeMetadataLazyCacheVariable(type);
auto variable =
getAddrOfLLVMVariable(entity, forDefinition, DebugTypeInfo());
getAddrOfLLVMVariable(entity, ForDefinition, DebugTypeInfo());

// Zero-initialize if we're asking for a definition.
if (forDefinition) {
cast<llvm::GlobalVariable>(variable)->setInitializer(
llvm::ConstantPointerNull::get(TypeMetadataPtrTy));
}
cast<llvm::GlobalVariable>(variable)->setInitializer(
llvm::ConstantPointerNull::get(TypeMetadataPtrTy));

return variable;
}

/// Get or create a type metadata cache variable. These are an
/// implementation detail of type metadata access functions.
llvm::Constant *
IRGenModule::getAddrOfTypeMetadataDemanglingCacheVariable(CanType type,
ConstantInit definition) {
assert(!type->hasArchetype() && !type->hasTypeParameter());
LinkEntity entity = LinkEntity::forTypeMetadataDemanglingCacheVariable(type);
return getAddrOfLLVMVariable(entity, definition, DebugTypeInfo());
}

llvm::Constant *
IRGenModule::getAddrOfTypeMetadataSingletonInitializationCache(
NominalTypeDecl *D,
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenKeyPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ getInitializerForComputedComponent(IRGenModule &IGM,
static llvm::Constant *
emitMetadataTypeRefForKeyPath(IRGenModule &IGM, CanType type) {
// Produce a mangled name for the type.
auto constant = IGM.getTypeRef(type, MangledTypeRefRole::Metadata);
auto constant = IGM.getTypeRef(type, MangledTypeRefRole::Metadata).first;

// Mask the bottom bit to tell the key path runtime this is a mangled name
// rather than a direct reference.
Expand Down
11 changes: 6 additions & 5 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ namespace {
void addExtendedContext() {
auto string = IGM.getTypeRef(E->getSelfInterfaceType(),
E->getGenericSignature(),
MangledTypeRefRole::Metadata);
MangledTypeRefRole::Metadata).first;
B.addRelativeAddress(string);
}

Expand Down Expand Up @@ -1594,7 +1594,8 @@ namespace {
GenericSignature *genericSig = getType()->getGenericSignature();
B.addRelativeAddress(IGM.getTypeRef(superclassType->getCanonicalType(),
genericSig,
MangledTypeRefRole::Metadata));
MangledTypeRefRole::Metadata)
.first);
} else {
B.addInt32(0);
}
Expand Down Expand Up @@ -1685,7 +1686,7 @@ namespace {
->getCanonicalType(O->getOpaqueInterfaceGenericSignature());

B.addRelativeAddress(IGM.getTypeRef(underlyingType,
MangledTypeRefRole::Metadata));
MangledTypeRefRole::Metadata).first);

auto opaqueType = O->getDeclaredInterfaceType()
->castTo<OpaqueTypeArchetypeType>();
Expand Down Expand Up @@ -4266,7 +4267,7 @@ static void addGenericRequirement(IRGenModule &IGM, ConstantStructBuilder &B,

B.addInt(IGM.Int32Ty, flags.getIntValue());
auto typeName =
IGM.getTypeRef(paramType, nullptr, MangledTypeRefRole::Metadata);
IGM.getTypeRef(paramType, nullptr, MangledTypeRefRole::Metadata).first;
B.addRelativeAddress(typeName);
addReference();
}
Expand Down Expand Up @@ -4334,7 +4335,7 @@ GenericRequirementsMetadata irgen::addGenericRequirements(
auto flags = GenericRequirementFlags(abiKind, false, false);
auto typeName =
IGM.getTypeRef(requirement.getSecondType(), nullptr,
MangledTypeRefRole::Metadata);
MangledTypeRefRole::Metadata).first;

addGenericRequirement(IGM, B, metadata, sig, flags,
requirement.getFirstType(),
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenProto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,7 @@ llvm::Constant *IRGenModule::getAssociatedTypeWitness(Type type,
auto role = inProtocolContext
? MangledTypeRefRole::DefaultAssociatedTypeWitness
: MangledTypeRefRole::Metadata;
auto typeRef = getTypeRef(type, /*generic signature*/nullptr, role);
auto typeRef = getTypeRef(type, /*generic signature*/nullptr, role).first;

// Set the low bit to indicate that this is a mangled name.
auto witness = llvm::ConstantExpr::getPtrToInt(typeRef, IntPtrTy);
Expand Down
Loading