Skip to content

IRGen support for closures that capture metadata and witness table packs #64679

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 5 commits into from
Mar 29, 2023
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
3 changes: 0 additions & 3 deletions include/swift/IRGen/GenericRequirement.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ class GenericRequirement {
}

static GenericRequirement forShape(CanType type) {
assert(!isa<PackType>(type));
assert(type->isParameterPack() || isa<PackArchetypeType>(type));
return GenericRequirement(Kind::Shape, type, nullptr);
}
Expand All @@ -92,7 +91,6 @@ class GenericRequirement {
}

static GenericRequirement forMetadata(CanType type) {
assert(!isa<PackType>(type));
auto kind = ((type->isParameterPack() ||
isa<PackArchetypeType>(type))
? Kind::MetadataPack : Kind::Metadata);
Expand All @@ -110,7 +108,6 @@ class GenericRequirement {
}

static GenericRequirement forWitnessTable(CanType type, ProtocolDecl *proto) {
assert(!isa<PackType>(type));
auto kind = ((type->isParameterPack() ||
isa<PackArchetypeType>(type))
? Kind::WitnessTablePack
Expand Down
22 changes: 22 additions & 0 deletions include/swift/Runtime/RuntimeFunctions.def
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,28 @@ FUNCTION(CompareProtocolConformanceDescriptors,
ATTRS(NoUnwind, ReadNone, WillReturn),
EFFECT(NoEffect)) // ?

// SWIFT_RUNTIME_EXPORT SWIFT_CC(swift)
// const Metadata * const *
// swift_allocateMetadataPack(const Metadata * const *ptr, size_t count);
FUNCTION(AllocateMetadataPack,
swift_allocateMetadataPack, SwiftCC,
AlwaysAvailable, // FIXME
RETURNS(TypeMetadataPtrPtrTy),
ARGS(TypeMetadataPtrPtrTy, SizeTy),
ATTRS(NoUnwind, WillReturn),
EFFECT(MetaData)) // ?

// SWIFT_RUNTIME_EXPORT SWIFT_CC(swift)
// const WitnessTable * const *
// swift_allocateWitnessTablePack(const WitnessTable * const *ptr, size_t count);
FUNCTION(AllocateWitnessTablePack,
swift_allocateWitnessTablePack, SwiftCC,
AlwaysAvailable, // FIXME
RETURNS(WitnessTablePtrPtrTy),
ARGS(WitnessTablePtrPtrTy, SizeTy),
ATTRS(NoUnwind, WillReturn),
EFFECT(MetaData)) // ?

// Metadata *swift_getMetatypeMetadata(Metadata *instanceTy);
FUNCTION(GetMetatypeMetadata, swift_getMetatypeMetadata, C_CC, AlwaysAvailable,
RETURNS(TypeMetadataPtrTy),
Expand Down
15 changes: 4 additions & 11 deletions lib/IRGen/GenArchetype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,17 +440,10 @@ withOpaqueTypeGenericArgs(IRGenFunction &IGF,
enumerateGenericSignatureRequirements(
opaqueDecl->getGenericSignature().getCanonicalSignature(),
[&](GenericRequirement reqt) {
auto ty = reqt.getTypeParameter().subst(archetype->getSubstitutions())
->getReducedType(opaqueDecl->getGenericSignature());
if (reqt.isAnyWitnessTable()) {
auto ref =
ProtocolConformanceRef(reqt.getProtocol())
.subst(reqt.getTypeParameter(), archetype->getSubstitutions());
args.push_back(emitWitnessTableRef(IGF, ty, ref));
} else {
assert(reqt.isAnyMetadata());
args.push_back(IGF.emitAbstractTypeMetadataRef(ty));
}
auto arg = emitGenericRequirementFromSubstitutions(
IGF, reqt, MetadataState::Abstract,
archetype->getSubstitutions());
args.push_back(arg);
types.push_back(args.back()->getType());
});
auto bufTy = llvm::StructType::get(IGF.IGM.getLLVMContext(), types);
Expand Down
3 changes: 2 additions & 1 deletion lib/IRGen/GenFunc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1949,7 +1949,8 @@ Optional<StackAddress> irgen::emitFunctionPartialApplication(

// Reserve space for polymorphic bindings.
auto bindings = NecessaryBindings::forPartialApplyForwarder(
IGF.IGM, origType, subs, considerParameterSources);
IGF.IGM, origType, subs, outType->isNoEscape(),
considerParameterSources);

if (!bindings.empty()) {
hasSingleSwiftRefcountedContext = No;
Expand Down
13 changes: 4 additions & 9 deletions lib/IRGen/GenKeyPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@ bindPolymorphicArgumentsFromComponentIndices(IRGenFunction &IGF,
args =
IGF.Builder.CreateInBoundsGEP(IGF.IGM.Int8Ty, args, genericArgsOffset);
}

bindFromGenericRequirementsBuffer(
IGF, requirements,
Address(args, IGF.IGM.Int8Ty, IGF.IGM.getPointerAlignment()),
MetadataState::Complete, [&](CanType t) {
return genericEnv->mapTypeIntoContext(t)->getCanonicalType();
});
MetadataState::Complete, genericEnv->getForwardingSubstitutionMap());
}

static llvm::Function *
Expand Down Expand Up @@ -295,9 +294,7 @@ getLayoutFunctionForComputedComponent(IRGenModule &IGM,
bindFromGenericRequirementsBuffer(
IGF, requirements,
Address(args, IGM.Int8Ty, IGF.IGM.getPointerAlignment()),
MetadataState::Complete, [&](CanType t) {
return genericEnv->mapTypeIntoContext(t)->getCanonicalType();
});
MetadataState::Complete, genericEnv->getForwardingSubstitutionMap());
}

// Run through the captured index types to determine the size and alignment
Expand Down Expand Up @@ -583,9 +580,7 @@ getInitializerForComputedComponent(IRGenModule &IGM,
bindFromGenericRequirementsBuffer(
IGF, requirements,
Address(src, IGM.Int8Ty, IGF.IGM.getPointerAlignment()),
MetadataState::Complete, [&](CanType t) {
return genericEnv->mapTypeIntoContext(t)->getCanonicalType();
});
MetadataState::Complete, genericEnv->getForwardingSubstitutionMap());

} else {
offset = llvm::ConstantInt::get(IGM.SizeTy, 0);
Expand Down
9 changes: 4 additions & 5 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2318,11 +2318,10 @@ namespace {
bindFromGenericRequirementsBuffer(
IGF, requirements,
Address(bindingsBufPtr, IGM.Int8Ty, IGM.getPointerAlignment()),
MetadataState::Complete, [&](CanType t) {
return genericEnv ? genericEnv->mapTypeIntoContext(t)
->getCanonicalType()
: t;
});
MetadataState::Complete,
(genericEnv
? genericEnv->getForwardingSubstitutionMap()
: SubstitutionMap()));
}

SmallVector<llvm::BasicBlock *, 4> conditionalTypes;
Expand Down
Loading