Skip to content

Fix dependent protocol conformance records #15573

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
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 @@ -145,6 +145,7 @@ NODE(ProtocolListWithAnyObject)
NODE(ProtocolWitness)
NODE(ProtocolWitnessTable)
NODE(ProtocolWitnessTableAccessor)
NODE(ProtocolWitnessTablePattern)
NODE(QualifiedArchetype)
NODE(ReabstractionThunk)
NODE(ReabstractionThunkHelper)
Expand Down
11 changes: 11 additions & 0 deletions include/swift/IRGen/Linking.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ class LinkEntity {
/// ProtocolConformance*.
DirectProtocolWitnessTable,

/// A protocol witness table pattern. The secondary pointer is a
/// ProtocolConformance*.
ProtocolWitnessTablePattern,

/// A witness accessor function. The secondary pointer is a
/// ProtocolConformance*.
ProtocolWitnessTableAccessFunction,
Expand Down Expand Up @@ -615,6 +619,13 @@ class LinkEntity {
return entity;
}

static LinkEntity
forProtocolWitnessTablePattern(const ProtocolConformance *C) {
LinkEntity entity;
entity.setForProtocolConformance(Kind::ProtocolWitnessTablePattern, C);
return entity;
}

static LinkEntity
forProtocolWitnessTableAccessFunction(const ProtocolConformance *C) {
LinkEntity entity;
Expand Down
3 changes: 3 additions & 0 deletions lib/Demangling/Demangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2072,6 +2072,9 @@ NodePointer Demangler::demangleWitness() {
case 'P':
return createWithChild(Node::Kind::ProtocolWitnessTable,
popProtocolConformance());
case 'p':
return createWithChild(Node::Kind::ProtocolWitnessTablePattern,
popProtocolConformance());
case 'G':
return createWithChild(Node::Kind::GenericProtocolWitnessTable,
popProtocolConformance());
Expand Down
5 changes: 5 additions & 0 deletions lib/Demangling/NodePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ class NodePrinter {
case Node::Kind::ProtocolWitness:
case Node::Kind::ProtocolWitnessTable:
case Node::Kind::ProtocolWitnessTableAccessor:
case Node::Kind::ProtocolWitnessTablePattern:
case Node::Kind::ReabstractionThunk:
case Node::Kind::ReabstractionThunkHelper:
case Node::Kind::RelatedEntityDeclName:
Expand Down Expand Up @@ -1303,6 +1304,10 @@ NodePointer NodePrinter::print(NodePointer Node, bool asPrefixContext) {
Printer << "protocol witness table for ";
print(Node->getFirstChild());
return nullptr;
case Node::Kind::ProtocolWitnessTablePattern:
Printer << "protocol witness table pattern for ";
print(Node->getFirstChild());
return nullptr;
case Node::Kind::GenericProtocolWitnessTable:
Printer << "generic protocol witness table for ";
print(Node->getFirstChild());
Expand Down
4 changes: 4 additions & 0 deletions lib/Demangling/OldRemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,10 @@ void Remangler::mangleProtocolRequirementArray(Node *node) {
unreachable("todo");
}

void Remangler::mangleProtocolWitnessTablePattern(Node *node) {
unreachable("todo");
}

void Remangler::mangleProtocolConformanceDescriptor(Node *node) {
Out << "Mc";
mangleProtocolConformance(node->begin()[0]);
Expand Down
5 changes: 5 additions & 0 deletions lib/Demangling/Remangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1562,6 +1562,11 @@ void Remangler::mangleProtocolWitnessTable(Node *node) {
Buffer << "WP";
}

void Remangler::mangleProtocolWitnessTablePattern(Node *node) {
mangleSingleChildNode(node);
Buffer << "Wp";
}

void Remangler::mangleProtocolWitnessTableAccessor(Node *node) {
mangleSingleChildNode(node);
Buffer << "Wa";
Expand Down
64 changes: 40 additions & 24 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#include "GenMeta.h"
#include "GenObjC.h"
#include "GenOpaque.h"
#include "GenProto.h"
#include "GenType.h"
#include "IRGenDebugInfo.h"
#include "IRGenFunction.h"
Expand Down Expand Up @@ -1566,6 +1567,11 @@ SILLinkage LinkEntity::getLinkage(ForDefinition_t forDefinition) const {
case Kind::ProtocolConformanceDescriptor:
return getLinkageAsConformance();

case Kind::ProtocolWitnessTablePattern:
if (getLinkageAsConformance() == SILLinkage::Shared)
return SILLinkage::Shared;
return SILLinkage::Private;

case Kind::ProtocolWitnessTableLazyAccessFunction:
case Kind::ProtocolWitnessTableLazyCacheVariable: {
auto *nominal = getType().getAnyNominal();
Expand Down Expand Up @@ -1662,7 +1668,6 @@ bool LinkEntity::isAvailableExternally(IRGenModule &IGM) const {
case Kind::PropertyDescriptor:
case Kind::NominalTypeDescriptor:
case Kind::ProtocolDescriptor:
case Kind::ProtocolRequirementArray:
return ::isAvailableExternally(IGM, getDecl());

case Kind::EnumCase:
Expand All @@ -1672,6 +1677,8 @@ bool LinkEntity::isAvailableExternally(IRGenModule &IGM) const {
case Kind::ProtocolConformanceDescriptor:
return ::isAvailableExternally(IGM, getProtocolConformance()->getDeclContext());

case Kind::ProtocolWitnessTablePattern:
case Kind::ProtocolRequirementArray:
case Kind::ObjCClassRef:
case Kind::ModuleDescriptor:
case Kind::ExtensionDescriptor:
Expand Down Expand Up @@ -2730,31 +2737,29 @@ namespace {
void addWitnessTable() {
using ConformanceKind = ConformanceFlags::ConformanceKind;

// Figure out what kind of witness table we have.
auto proto = Conformance->getProtocol();
// Figure out what kind of witness table we have.
llvm::Constant *witnessTableVar;
if (!IGM.isResilient(proto, ResilienceExpansion::Maximal) &&
Conformance->getConditionalRequirements().empty()) {
Flags = Flags.withConformanceKind(ConformanceKind::WitnessTable);

// If the conformance is in this object's table, then the witness table
// should also be in this object file, so we can always directly
// reference it.
witnessTableVar = IGM.getAddrOfWitnessTable(Conformance);
} else {
if (Conformance->getConditionalRequirements().empty()) {

if (Conformance->getConditionalRequirements().empty()) {
if (!isDependentConformance(IGM, Conformance,
ResilienceExpansion::Maximal)) {
Flags = Flags.withConformanceKind(ConformanceKind::WitnessTable);
witnessTableVar = IGM.getAddrOfWitnessTable(Conformance);
} else {
Flags = Flags.withConformanceKind(
ConformanceKind::WitnessTableAccessor);
} else {
Flags =
Flags.withConformanceKind(
ConformanceKind::ConditionalWitnessTableAccessor)
.withNumConditionalRequirements(
Conformance->getConditionalRequirements().size());
witnessTableVar = IGM.getAddrOfWitnessTableAccessFunction(
Conformance, ForDefinition);
}
} else {
Flags =
Flags.withConformanceKind(
ConformanceKind::ConditionalWitnessTableAccessor)
.withNumConditionalRequirements(
Conformance->getConditionalRequirements().size());

witnessTableVar = IGM.getAddrOfWitnessTableAccessFunction(
Conformance, ForDefinition);
Conformance, ForDefinition);
}

// Relative reference to the witness table.
Expand Down Expand Up @@ -4126,10 +4131,7 @@ IRGenModule::getAddrOfWitnessTableLazyCacheVariable(

/// Look up the address of a witness table.
///
/// TODO: This needs to take a flag for the access mode of the witness table,
/// which may be direct, lazy, or a runtime instantiation template.
/// TODO: Use name from witness table here to lookup witness table instead of
/// recomputing it.
/// This can only be used with non-dependent conformances.
llvm::Constant*
IRGenModule::getAddrOfWitnessTable(const NormalProtocolConformance *conf,
ConstantInit definition) {
Expand All @@ -4140,6 +4142,20 @@ IRGenModule::getAddrOfWitnessTable(const NormalProtocolConformance *conf,
WitnessTableTy, DebugTypeInfo());
}

/// Look up the address of a witness table pattern.
///
/// This can only be used with dependent conformances from inside the
/// defining module.
llvm::Constant*
IRGenModule::getAddrOfWitnessTablePattern(const NormalProtocolConformance *conf,
ConstantInit definition) {
IRGen.addLazyWitnessTable(conf);

auto entity = LinkEntity::forProtocolWitnessTablePattern(conf);
return getAddrOfLLVMVariable(entity, getPointerAlignment(), definition,
WitnessTableTy, DebugTypeInfo());
}

llvm::Function *
IRGenModule::getAddrOfAssociatedTypeMetadataAccessFunction(
const NormalProtocolConformance *conformance,
Expand Down
14 changes: 3 additions & 11 deletions lib/IRGen/GenFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,7 @@ void irgen::emitFunctionPartialApplication(
SmallVector<SILType, 4> argValTypes;
SmallVector<ParameterConvention, 4> argConventions;

bool isNoEscapeFunction = outType->isNoEscape();
assert(!outType->isNoEscape());

// Reserve space for polymorphic bindings.
SubstitutionMap subMap;
Expand Down Expand Up @@ -1476,10 +1476,7 @@ void irgen::emitFunctionPartialApplication(
fnPtr = IGF.Builder.CreateBitCast(fnPtr, IGF.IGM.Int8PtrTy);
out.add(fnPtr);
llvm::Value *ctx = args.claimNext();
if (isNoEscapeFunction)
ctx = IGF.Builder.CreateBitCast(ctx, IGF.IGM.OpaquePtrTy);
else
ctx = IGF.Builder.CreateBitCast(ctx, IGF.IGM.RefCountedPtrTy);
ctx = IGF.Builder.CreateBitCast(ctx, IGF.IGM.RefCountedPtrTy);
out.add(ctx);
return;
}
Expand Down Expand Up @@ -1520,10 +1517,7 @@ void irgen::emitFunctionPartialApplication(
llvm::Value *ctx = args.claimNext();
if (isIndirectFormalParameter(*singleRefcountedConvention))
ctx = IGF.Builder.CreateLoad(ctx, IGF.IGM.getPointerAlignment());
if (isNoEscapeFunction)
ctx = IGF.Builder.CreateBitCast(ctx, IGF.IGM.OpaquePtrTy);
else
ctx = IGF.Builder.CreateBitCast(ctx, IGF.IGM.RefCountedPtrTy);
ctx = IGF.Builder.CreateBitCast(ctx, IGF.IGM.RefCountedPtrTy);
out.add(ctx);
return;
}
Expand Down Expand Up @@ -1614,8 +1608,6 @@ void irgen::emitFunctionPartialApplication(
argConventions);
forwarder = IGF.Builder.CreateBitCast(forwarder, IGF.IGM.Int8PtrTy);
out.add(forwarder);
if (isNoEscapeFunction)
data = IGF.Builder.CreateBitCast(data, IGF.IGM.OpaquePtrTy);
out.add(data);
}

Expand Down
22 changes: 15 additions & 7 deletions lib/IRGen/GenProto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ static bool isResilientConformance(const NormalProtocolConformance *conformance)

/// Is there anything about the given conformance that requires witness
/// tables to be dependently-generated?
static bool isDependentConformance(IRGenModule &IGM,
bool irgen::isDependentConformance(IRGenModule &IGM,
const NormalProtocolConformance *conformance,
ResilienceExpansion expansion) {
// If the conformance is resilient, this is always true.
Expand All @@ -946,6 +946,9 @@ static bool isDependentConformance(IRGenModule &IGM,

// Check whether any of the inherited conformances are dependent.
for (auto inherited : conformance->getProtocol()->getInheritedProtocols()) {
if (inherited->isObjC())
continue;

if (isDependentConformance(IGM,
conformance->getInheritedConformance(inherited)
->getRootNormalConformance(),
Expand Down Expand Up @@ -2066,6 +2069,8 @@ void IRGenModule::emitSILWitnessTable(SILWitnessTable *wt) {
if (isAvailableExternally(wt->getLinkage()))
return;

auto *conf = wt->getConformance();

// Build the witnesses.
ConstantInitBuilder builder(*this);
auto wtableContents = builder.beginArray(Int8PtrTy);
Expand All @@ -2080,23 +2085,26 @@ void IRGenModule::emitSILWitnessTable(SILWitnessTable *wt) {
// Produce the initializer value.
auto initializer = wtableContents.finishAndCreateFuture();

bool isDependent = isDependentConformance(*this, conf,
ResilienceExpansion::Maximal);
auto global = cast<llvm::GlobalVariable>(
getAddrOfWitnessTable(wt->getConformance(), initializer));
isDependent
? getAddrOfWitnessTablePattern(conf, initializer)
: getAddrOfWitnessTable(conf, initializer));
global->setConstant(true);
global->setAlignment(getWitnessTableAlignment().getValue());

// FIXME: resilience; this should use the conformance's publishing scope.
// Always emit an accessor function.
wtableBuilder.buildAccessFunction(global);

// Behavior conformances can't be reflected.
if (wt->getConformance()->isBehaviorConformance())
if (conf->isBehaviorConformance())
return;

NormalProtocolConformance *Conf = wt->getConformance();
addProtocolConformance(Conf);
addProtocolConformance(conf);

// Trigger the lazy emission of the foreign type metadata.
CanType conformingType = Conf->getType()->getCanonicalType();
CanType conformingType = conf->getType()->getCanonicalType();
if (requiresForeignTypeMetadata(conformingType))
(void)getAddrOfForeignTypeMetadataCandidate(conformingType);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/IRGen/GenProto.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ namespace irgen {
CanSILFunctionType fnType,
GenericParamFulfillmentCallback callback);

bool isDependentConformance(IRGenModule &IGM,
const NormalProtocolConformance *conformance,
ResilienceExpansion expansion);

} // end namespace irgen
} // end namespace swift

Expand Down
4 changes: 4 additions & 0 deletions lib/IRGen/IRGenMangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ class IRGenMangler : public Mangle::ASTMangler {
return mangleConformanceSymbol(Type(), C, "WP");
}

std::string mangleProtocolWitnessTablePattern(const ProtocolConformance *C) {
return mangleConformanceSymbol(Type(), C, "Wp");
}

std::string mangleGenericProtocolWitnessTableCache(
const ProtocolConformance *C) {
return mangleConformanceSymbol(Type(), C, "WG");
Expand Down
5 changes: 4 additions & 1 deletion lib/IRGen/IRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,10 @@ private: \
CanType conformingType,
ForDefinition_t forDefinition);
llvm::Constant *getAddrOfWitnessTable(const NormalProtocolConformance *C,
ConstantInit definition = ConstantInit());
ConstantInit definition = ConstantInit());
llvm::Constant *getAddrOfWitnessTablePattern(const NormalProtocolConformance *C,
ConstantInit definition = ConstantInit());

llvm::Constant *
getAddrOfGenericWitnessTableCache(const NormalProtocolConformance *C,
ForDefinition_t forDefinition);
Expand Down
3 changes: 3 additions & 0 deletions lib/IRGen/Linking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ std::string LinkEntity::mangleAsString() const {
return mangler.mangleProtocolWitnessTableAccessFunction(
getProtocolConformance());

case Kind::ProtocolWitnessTablePattern:
return mangler.mangleProtocolWitnessTablePattern(getProtocolConformance());

case Kind::ProtocolWitnessTableLazyAccessFunction:
return mangler.mangleProtocolWitnessTableLazyAccessFunction(getType(),
getProtocolConformance());
Expand Down
8 changes: 4 additions & 4 deletions stdlib/public/runtime/ErrorObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -229,22 +229,22 @@ static Class getSwiftNativeNSErrorClass() {
// Swift source.

auto TheWitnessTable = SWIFT_LAZY_CONSTANT(dlsym(RTLD_DEFAULT,
MANGLE_AS_STRING(MANGLE_SYM(So10CFErrorRefas5Error10FoundationWP))));
MANGLE_AS_STRING(MANGLE_SYM(So10CFErrorRefas5Error10FoundationWa))));
assert(TheWitnessTable &&
"Foundation overlay not loaded, or 'CFError : Error' conformance "
"not available");

return reinterpret_cast<const WitnessTable *>(TheWitnessTable);
return reinterpret_cast<const SWIFT_CC(swift) WitnessTable *(*)()>(TheWitnessTable)();
}

static const HashableWitnessTable *getNSErrorConformanceToHashable() {
auto TheWitnessTable = SWIFT_LAZY_CONSTANT(dlsym(RTLD_DEFAULT,
MANGLE_AS_STRING(MANGLE_SYM(So8NSObjectCs8Hashable10ObjectiveCWP))));
MANGLE_AS_STRING(MANGLE_SYM(So8NSObjectCs8Hashable10ObjectiveCWa))));
assert(TheWitnessTable &&
"ObjectiveC overlay not loaded, or 'NSObject : Hashable' conformance "
"not available");

return reinterpret_cast<const HashableWitnessTable *>(TheWitnessTable);
return reinterpret_cast<const SWIFT_CC(swift) HashableWitnessTable *(*)()>(TheWitnessTable)();
}

bool SwiftError::isPureNSError() const {
Expand Down
Loading