Skip to content

[metadata prespecialization] Support for classes. #30089

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
2 changes: 1 addition & 1 deletion include/swift/ABI/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ struct TargetAnyClassMetadata : public TargetHeapMetadata<Runtime> {
using TargetMetadata<Runtime>::setClassISA;
#endif

// Note that ObjC classes does not have a metadata header.
// Note that ObjC classes do not have a metadata header.

/// The metadata for the superclass. This is null for the root class.
ConstTargetMetadataPointer<Runtime, swift::TargetClassMetadata> Superclass;
Expand Down
12 changes: 11 additions & 1 deletion include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,17 @@ class alignas(1 << TypeAlignInBits) TypeBase {
/// \returns The superclass of this type, or a null type if it has no
/// superclass.
Type getSuperclass(bool useArchetypes = true);


/// Retrieve the root class of this type by repeatedly retrieving the
/// superclass.
///
/// \param useArchetypes Whether to use context archetypes for outer generic
/// parameters if the class is nested inside a generic function.
///
/// \returns The base class of this type, or this type itself if it has no
/// superclasses.
Type getRootClass(bool useArchetypes = true);

/// True if this type is the exact superclass of another type.
///
/// \param ty The potential subclass.
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Demangling/DemangleNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -287,5 +287,9 @@ NODE(OpaqueTypeDescriptorAccessorVar)
NODE(OpaqueReturnType)
CONTEXT_NODE(OpaqueReturnTypeOf)

// Added in Swift 5.3
NODE(CanonicalSpecializedGenericMetaclass)
NODE(CanonicalSpecializedGenericTypeMetadataAccessFunction)

#undef CONTEXT_NODE
#undef NODE
31 changes: 28 additions & 3 deletions include/swift/IRGen/Linking.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class LinkEntity {
/// The nominal type descriptor for a nominal type.
/// The pointer is a NominalTypeDecl*.
NominalTypeDescriptor,

/// The descriptor for an opaque type.
/// The pointer is an OpaqueTypeDecl*.
OpaqueTypeDescriptor,
Expand Down Expand Up @@ -295,12 +295,12 @@ class LinkEntity {
/// The descriptor for an extension.
/// The pointer is an ExtensionDecl*.
ExtensionDescriptor,

/// The descriptor for a runtime-anonymous context.
/// The pointer is the DeclContext* of a child of the context that should
/// be considered private.
AnonymousDescriptor,

/// A SIL global variable. The pointer is a SILGlobalVariable*.
SILGlobalVariable,

Expand Down Expand Up @@ -384,6 +384,15 @@ class LinkEntity {

/// A global function pointer for dynamically replaceable functions.
DynamicallyReplaceableFunctionVariable,

/// A reference to a metaclass-stub for a statically specialized generic
/// class.
/// The pointer is a canonical TypeBase*.
CanonicalSpecializedGenericSwiftMetaclassStub,

/// An access function for prespecialized type metadata.
/// The pointer is a canonical TypeBase*.
CanonicalSpecializedGenericTypeMetadataAccessFunction,
};
friend struct llvm::DenseMapInfo<LinkEntity>;

Expand Down Expand Up @@ -1020,6 +1029,22 @@ class LinkEntity {
return entity;
}

static LinkEntity
forSpecializedGenericSwiftMetaclassStub(CanType concreteType) {
LinkEntity entity;
entity.setForType(Kind::CanonicalSpecializedGenericSwiftMetaclassStub,
concreteType);
return entity;
}

static LinkEntity
forPrespecializedTypeMetadataAccessFunction(CanType theType) {
LinkEntity entity;
entity.setForType(
Kind::CanonicalSpecializedGenericTypeMetadataAccessFunction, theType);
return entity;
}

void mangle(llvm::raw_ostream &out) const;
void mangle(SmallVectorImpl<char> &buffer) const;
std::string mangleAsString() const;
Expand Down
11 changes: 11 additions & 0 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,17 @@ Type TypeBase::getSuperclass(bool useArchetypes) {
return superclassTy.subst(subMap);
}

Type TypeBase::getRootClass(bool useArchetypes) {
Type iterator = this;
assert(iterator);

while (auto superclass = iterator->getSuperclass(useArchetypes)) {
iterator = superclass;
}

return iterator;
}

bool TypeBase::isExactSuperclassOf(Type ty) {
// For there to be a superclass relationship, we must be a class, and
// the potential subtype must be a class, superclass-bounded archetype,
Expand Down
6 changes: 6 additions & 0 deletions lib/Demangling/Demangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1874,6 +1874,9 @@ NodePointer Demangler::demangleMetatype() {
case 'A':
return createWithChild(Node::Kind::ReflectionMetadataAssocTypeDescriptor,
popProtocolConformance());
case 'b':
return createWithPoppedType(
Node::Kind::CanonicalSpecializedGenericTypeMetadataAccessFunction);
case 'B':
return createWithChild(Node::Kind::ReflectionMetadataBuiltinDescriptor,
popNode(Node::Kind::Type));
Expand Down Expand Up @@ -1917,6 +1920,9 @@ NodePointer Demangler::demangleMetatype() {
return createWithPoppedType(Node::Kind::TypeMetadataLazyCache);
case 'm':
return createWithPoppedType(Node::Kind::Metaclass);
case 'M':
return createWithPoppedType(
Node::Kind::CanonicalSpecializedGenericMetaclass);
case 'n':
return createWithPoppedType(Node::Kind::NominalTypeDescriptor);
case 'o':
Expand Down
10 changes: 10 additions & 0 deletions lib/Demangling/NodePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,8 @@ class NodePrinter {
case Node::Kind::OpaqueTypeDescriptorSymbolicReference:
case Node::Kind::OpaqueReturnType:
case Node::Kind::OpaqueReturnTypeOf:
case Node::Kind::CanonicalSpecializedGenericMetaclass:
case Node::Kind::CanonicalSpecializedGenericTypeMetadataAccessFunction:
return false;
}
printer_unreachable("bad node kind");
Expand Down Expand Up @@ -2418,6 +2420,14 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) {
case Node::Kind::AccessorFunctionReference:
Printer << "accessor function at " << Node->getIndex();
return nullptr;
case Node::Kind::CanonicalSpecializedGenericMetaclass:
Printer << "specialized generic metaclass for ";
print(Node->getFirstChild());
return nullptr;
case Node::Kind::CanonicalSpecializedGenericTypeMetadataAccessFunction:
Printer << "canonical specialized generic type metadata accessor for ";
print(Node->getChild(0));
return nullptr;
}
printer_unreachable("bad node kind!");
}
Expand Down
11 changes: 11 additions & 0 deletions lib/Demangling/OldRemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2133,6 +2133,17 @@ void Remangler::mangleAccessorFunctionReference(Node *node) {
unreachable("can't remangle");
}

void Remangler::mangleCanonicalSpecializedGenericMetaclass(Node *node) {
Buffer << "MM";
mangleSingleChildNode(node); // type
}

void Remangler::mangleCanonicalSpecializedGenericTypeMetadataAccessFunction(
Node *node) {
mangleSingleChildNode(node);
Buffer << "Mb";
}

/// The top-level interface to the remangler.
std::string Demangle::mangleNodeOld(NodePointer node) {
if (!node) return "";
Expand Down
11 changes: 11 additions & 0 deletions lib/Demangling/Remangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2533,6 +2533,17 @@ void Remangler::mangleAccessorFunctionReference(Node *node) {
unreachable("can't remangle");
}

void Remangler::mangleCanonicalSpecializedGenericMetaclass(Node *node) {
mangleChildNodes(node);
Buffer << "MM";
}

void Remangler::mangleCanonicalSpecializedGenericTypeMetadataAccessFunction(
Node *node) {
mangleSingleChildNode(node);
Buffer << "Mb";
}

} // anonymous namespace

/// The top-level interface to the remangler.
Expand Down
66 changes: 66 additions & 0 deletions lib/IRGen/ClassTypeInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//===--- ClassTypeInfo.h - The layout info for class types. -----*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 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
//
//===----------------------------------------------------------------------===//
//
// This file contains layout information for class types.
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_IRGEN_CLASSTYPEINFO_H
#define SWIFT_IRGEN_CLASSTYPEINFO_H

#include "ClassLayout.h"
#include "HeapTypeInfo.h"

namespace swift {
namespace irgen {

/// Layout information for class types.
class ClassTypeInfo : public HeapTypeInfo<ClassTypeInfo> {
ClassDecl *TheClass;

// The resilient layout of the class, without making any assumptions
// that violate resilience boundaries. This is used to allocate
// and deallocate instances of the class, and to access fields.
mutable Optional<ClassLayout> ResilientLayout;

// A completely fragile layout, used for metadata emission.
mutable Optional<ClassLayout> FragileLayout;

/// Can we use swift reference-counting, or do we have to use
/// objc_retain/release?
const ReferenceCounting Refcount;

ClassLayout generateLayout(IRGenModule &IGM, SILType classType,
bool forBackwardDeployment) const;

public:
ClassTypeInfo(llvm::PointerType *irType, Size size, SpareBitVector spareBits,
Alignment align, ClassDecl *theClass,
ReferenceCounting refcount)
: HeapTypeInfo(irType, size, std::move(spareBits), align),
TheClass(theClass), Refcount(refcount) {}

ReferenceCounting getReferenceCounting() const { return Refcount; }

ClassDecl *getClass() const { return TheClass; }

const ClassLayout &getClassLayout(IRGenModule &IGM, SILType type,
bool forBackwardDeployment) const;

StructLayout *createLayoutWithTailElems(IRGenModule &IGM, SILType classType,
ArrayRef<SILType> tailTypes) const;
};

} // namespace irgen
} // namespace swift

#endif
Loading