Skip to content

Opaque result types: dynamic replacement #24201

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
4 changes: 4 additions & 0 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4685,6 +4685,10 @@ class AbstractStorageDecl : public ValueDecl {

bool hasDidSetOrWillSetDynamicReplacement() const;

bool hasAnyNativeDynamicAccessors() const;

bool hasAnyDynamicReplacementAccessors() const;

OpaqueTypeDecl *getOpaqueResultTypeDecl() const {
return OpaqueReturn;
}
Expand Down
4 changes: 3 additions & 1 deletion include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@ enum class TypeMatchFlags {
///
/// This is necessary because Objective-C allows optional function paramaters
/// to be non-escaping, but Swift currently does not.
IgnoreNonEscapingForOptionalFunctionParam = 1 << 4
IgnoreNonEscapingForOptionalFunctionParam = 1 << 4,
/// Allow compatible opaque archetypes.
AllowCompatibleOpaqueTypeArchetypes = 1 << 5
};
using TypeMatchOptions = OptionSet<TypeMatchFlags>;

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 @@ -266,6 +266,10 @@ NODE(AccessorFunctionReference)
NODE(OpaqueType)
NODE(OpaqueTypeDescriptorSymbolicReference)
NODE(OpaqueTypeDescriptor)
NODE(OpaqueTypeDescriptorAccessor)
NODE(OpaqueTypeDescriptorAccessorImpl)
NODE(OpaqueTypeDescriptorAccessorKey)
NODE(OpaqueTypeDescriptorAccessorVar)
NODE(OpaqueReturnType)
CONTEXT_NODE(OpaqueReturnTypeOf)

Expand Down
42 changes: 42 additions & 0 deletions include/swift/IRGen/Linking.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,24 @@ class LinkEntity {
/// The pointer is an OpaqueTypeDecl*.
OpaqueTypeDescriptor,

/// The descriptor accessor for an opaque type used for dynamic functions.
/// The pointer is an OpaqueTypeDecl*.
OpaqueTypeDescriptorAccessor,
Copy link
Contributor

Choose a reason for hiding this comment

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

These should have comments


/// The descriptor accessor implementation for an opaque type used for
/// dynamic functions.
/// The pointer is an OpaqueTypeDecl*.
OpaqueTypeDescriptorAccessorImpl,

/// The descriptor accessor key of dynamic replacements for an opaque type.
/// The pointer is an OpaqueTypeDecl*.
OpaqueTypeDescriptorAccessorKey,

/// The descriptor accessor variable of dynamic replacements for an opaque
/// type.
/// The pointer is an OpaqueTypeDecl*.
OpaqueTypeDescriptorAccessorVar,

/// The metadata pattern for a generic nominal type.
/// The pointer is a NominalTypeDecl*.
TypeMetadataPattern,
Expand Down Expand Up @@ -706,6 +724,30 @@ class LinkEntity {
return entity;
}

static LinkEntity forOpaqueTypeDescriptorAccessor(OpaqueTypeDecl *decl) {
LinkEntity entity;
entity.setForDecl(Kind::OpaqueTypeDescriptorAccessor, decl);
return entity;
}

static LinkEntity forOpaqueTypeDescriptorAccessorImpl(OpaqueTypeDecl *decl) {
LinkEntity entity;
entity.setForDecl(Kind::OpaqueTypeDescriptorAccessorImpl, decl);
return entity;
}

static LinkEntity forOpaqueTypeDescriptorAccessorKey(OpaqueTypeDecl *decl) {
LinkEntity entity;
entity.setForDecl(Kind::OpaqueTypeDescriptorAccessorKey, decl);
return entity;
}

static LinkEntity forOpaqueTypeDescriptorAccessorVar(OpaqueTypeDecl *decl) {
LinkEntity entity;
entity.setForDecl(Kind::OpaqueTypeDescriptorAccessorVar, decl);
return entity;
}

static LinkEntity forPropertyDescriptor(AbstractStorageDecl *decl) {
assert(decl->exportsPropertyDescriptor());
LinkEntity entity;
Expand Down
15 changes: 15 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4518,6 +4518,21 @@ bool AbstractStorageDecl::hasDidSetOrWillSetDynamicReplacement() const {
return false;
}

bool AbstractStorageDecl::hasAnyNativeDynamicAccessors() const {
for (auto accessor : getAllAccessors()) {
if (accessor->isNativeDynamic())
return true;
}
return false;
}

bool AbstractStorageDecl::hasAnyDynamicReplacementAccessors() const {
for (auto accessor : getAllAccessors()) {
if (accessor->getAttrs().hasAttribute<DynamicReplacementAttr>())
return true;
}
return false;
}
void AbstractStorageDecl::setAccessors(StorageImplInfo implInfo,
SourceLoc lbraceLoc,
ArrayRef<AccessorDecl *> accessors,
Expand Down
7 changes: 7 additions & 0 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2293,6 +2293,13 @@ static bool matches(CanType t1, CanType t2, TypeMatchOptions matchMode,
if (isABICompatibleEvenAddingOptional(t1, t2))
return true;

if (matchMode.contains(TypeMatchFlags::AllowCompatibleOpaqueTypeArchetypes))
if (auto opaque1 = t1->getAs<OpaqueTypeArchetypeType>())
if (auto opaque2 = t2->getAs<OpaqueTypeArchetypeType>())
return opaque1->getBoundSignature() == opaque2->getBoundSignature() &&
opaque1->getInterfaceType()->getCanonicalType()->matches(
opaque2->getInterfaceType()->getCanonicalType(), matchMode);

return false;
}

Expand Down
12 changes: 12 additions & 0 deletions lib/Demangling/Demangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,18 @@ NodePointer Demangler::demangleMetatype() {
return createWithPoppedType(Node::Kind::GenericTypeMetadataPattern);
case 'a':
return createWithPoppedType(Node::Kind::TypeMetadataAccessFunction);
case 'g':
return createWithChild(Node::Kind::OpaqueTypeDescriptorAccessor,
popNode());
case 'h':
return createWithChild(Node::Kind::OpaqueTypeDescriptorAccessorImpl,
popNode());
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':
Expand Down
20 changes: 20 additions & 0 deletions lib/Demangling/NodePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,10 @@ class NodePrinter {
case Node::Kind::ObjCMetadataUpdateFunction:
case Node::Kind::ObjCResilientClassStub:
case Node::Kind::OpaqueTypeDescriptor:
case Node::Kind::OpaqueTypeDescriptorAccessor:
case Node::Kind::OpaqueTypeDescriptorAccessorImpl:
case Node::Kind::OpaqueTypeDescriptorAccessorKey:
case Node::Kind::OpaqueTypeDescriptorAccessorVar:
case Node::Kind::Owned:
case Node::Kind::OwningAddressor:
case Node::Kind::OwningMutableAddressor:
Expand Down Expand Up @@ -1733,6 +1737,22 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) {
Printer << "opaque type descriptor for ";
print(Node->getChild(0));
return nullptr;
case Node::Kind::OpaqueTypeDescriptorAccessor:
Printer << "opaque type descriptor accessor for ";
print(Node->getChild(0));
return nullptr;
case Node::Kind::OpaqueTypeDescriptorAccessorImpl:
Printer << "opaque type descriptor accessor impl for ";
print(Node->getChild(0));
return nullptr;
case Node::Kind::OpaqueTypeDescriptorAccessorKey:
Printer << "opaque type descriptor accessor key for ";
print(Node->getChild(0));
return nullptr;
case Node::Kind::OpaqueTypeDescriptorAccessorVar:
Printer << "opaque type descriptor accessor var for ";
print(Node->getChild(0));
return nullptr;
case Node::Kind::CoroutineContinuationPrototype:
Printer << "coroutine continuation prototype for ";
print(Node->getChild(0));
Expand Down
12 changes: 12 additions & 0 deletions lib/Demangling/OldRemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2038,6 +2038,18 @@ void Remangler::mangleOpaqueType(Node *node) {
void Remangler::mangleOpaqueTypeDescriptor(Node *node) {
unreachable("unsupported");
}
void Remangler::mangleOpaqueTypeDescriptorAccessor(Node *node) {
unreachable("unsupported");
}
void Remangler::mangleOpaqueTypeDescriptorAccessorImpl(Node *node) {
unreachable("unsupported");
}
void Remangler::mangleOpaqueTypeDescriptorAccessorKey(Node *node) {
unreachable("unsupported");
}
void Remangler::mangleOpaqueTypeDescriptorAccessorVar(Node *node) {
unreachable("unsupported");
}
void Remangler::mangleAccessorFunctionReference(Node *node) {
unreachable("can't remangle");
}
Expand Down
20 changes: 20 additions & 0 deletions lib/Demangling/Remangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1599,6 +1599,26 @@ void Remangler::mangleOpaqueTypeDescriptor(Node *node) {
Buffer << "MQ";
}

void Remangler::mangleOpaqueTypeDescriptorAccessor(Node *node) {
mangleSingleChildNode(node);
Buffer << "Mg";
}

void Remangler::mangleOpaqueTypeDescriptorAccessorImpl(Node *node) {
mangleSingleChildNode(node);
Buffer << "Mh";
}

void Remangler::mangleOpaqueTypeDescriptorAccessorKey(Node *node) {
mangleSingleChildNode(node);
Buffer << "Mj";
}

void Remangler::mangleOpaqueTypeDescriptorAccessorVar(Node *node) {
mangleSingleChildNode(node);
Buffer << "Mk";
}

void Remangler::manglePropertyDescriptor(Node *node) {
mangleSingleChildNode(node);
Buffer << "MV";
Expand Down
48 changes: 43 additions & 5 deletions lib/IRGen/GenArchetype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/IRGenOptions.h"
#include "swift/AST/Types.h"
#include "swift/IRGen/Linking.h"
#include "swift/SIL/SILValue.h"
Expand Down Expand Up @@ -389,7 +390,6 @@ llvm::Value *irgen::emitDynamicTypeOfOpaqueArchetype(IRGenFunction &IGF,
Address addr,
SILType type) {
auto archetype = type.castTo<ArchetypeType>();

// Acquire the archetype's static metadata.
llvm::Value *metadata =
emitArchetypeTypeMetadataRef(IGF, archetype, MetadataState::Complete)
Expand Down Expand Up @@ -450,14 +450,51 @@ withOpaqueTypeGenericArgs(IRGenFunction &IGF,
}
}

bool shouldUseOpaqueTypeDescriptorAccessor(OpaqueTypeDecl *opaque) {
auto *namingDecl = opaque->getNamingDecl();
auto *abstractStorage = dyn_cast<AbstractStorageDecl>(namingDecl);

// Don't emit accessors for abstract storage that is not dynamic or a dynamic
// replacement.
if (abstractStorage) {
return abstractStorage->hasAnyNativeDynamicAccessors() ||
abstractStorage->hasAnyDynamicReplacementAccessors();
}

// Don't emit accessors for functions that are not dynamic or dynamic
// replacements.
return opaque->getNamingDecl()->isNativeDynamic() ||
opaque->getNamingDecl()
->getAttrs()
.hasAttribute<DynamicReplacementAttr>();
}

static llvm::Value *
getAddressOfOpaqueTypeDescriptor(IRGenFunction &IGF,
OpaqueTypeDecl *opaqueDecl) {
auto &IGM = IGF.IGM;

// Support dynamically replacing the return type as part of dynamic function
// replacement.
if (!IGM.getOptions().shouldOptimize() &&
shouldUseOpaqueTypeDescriptorAccessor(opaqueDecl)) {
auto descriptorAccessor = IGM.getAddrOfOpaqueTypeDescriptorAccessFunction(
opaqueDecl, NotForDefinition, false);
auto desc = IGF.Builder.CreateCall(descriptorAccessor, {});
desc->setDoesNotThrow();
desc->setCallingConv(IGM.SwiftCC);
return desc;
}
return IGM.getAddrOfOpaqueTypeDescriptor(opaqueDecl, ConstantInit());
}

MetadataResponse irgen::emitOpaqueTypeMetadataRef(IRGenFunction &IGF,
CanOpaqueTypeArchetypeType archetype,
DynamicMetadataRequest request) {
auto accessorFn = IGF.IGM.getGetOpaqueTypeMetadataFn();
auto opaqueDecl = archetype->getDecl();
auto descriptor = IGF.IGM
.getAddrOfOpaqueTypeDescriptor(opaqueDecl, ConstantInit());

auto *descriptor = getAddressOfOpaqueTypeDescriptor(IGF, opaqueDecl);
auto indexValue = llvm::ConstantInt::get(IGF.IGM.SizeTy, 0);

llvm::CallInst *result = nullptr;
Expand All @@ -482,8 +519,9 @@ llvm::Value *irgen::emitOpaqueTypeWitnessTableRef(IRGenFunction &IGF,
ProtocolDecl *protocol) {
auto accessorFn = IGF.IGM.getGetOpaqueTypeConformanceFn();
auto opaqueDecl = archetype->getDecl();
auto descriptor = IGF.IGM
.getAddrOfOpaqueTypeDescriptor(opaqueDecl, ConstantInit());


llvm::Value *descriptor = getAddressOfOpaqueTypeDescriptor(IGF, opaqueDecl);

auto foundProtocol = std::find(archetype->getConformsTo().begin(),
archetype->getConformsTo().end(),
Expand Down
Loading