Skip to content

IRGen: Fix the computation of the number of type metadata pointers for distributed thunks under opaque pointer IR #61699

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
13 changes: 8 additions & 5 deletions lib/IRGen/GenDistributed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,14 +681,17 @@ void DistributedAccessor::emit() {
// We need this to determine the expected number of witness tables
// to load from the buffer provided by the caller.
llvm::SmallVector<llvm::Type *, 4> targetGenericArguments;
expandPolymorphicSignature(IGM, targetTy, targetGenericArguments);
auto numDirectGenericArgs =
expandPolymorphicSignature(IGM, targetTy, targetGenericArguments);

// Generic arguments associated with the distributed thunk directly
// e.g. `distributed func echo<T, U>(...)`
auto numDirectGenericArgs =
llvm::count_if(targetGenericArguments, [&](const llvm::Type *type) {
return type == IGM.TypeMetadataPtrTy;
});
assert(
!IGM.getLLVMContext().supportsTypedPointers() ||
numDirectGenericArgs ==
llvm::count_if(targetGenericArguments, [&](const llvm::Type *type) {
return type == IGM.TypeMetadataPtrTy;
}));

auto expectedWitnessTables =
targetGenericArguments.size() - numDirectGenericArgs;
Expand Down
16 changes: 12 additions & 4 deletions lib/IRGen/GenProto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3456,12 +3456,15 @@ void irgen::bindGenericRequirement(IRGenFunction &IGF,
namespace {
/// A class for expanding a polymorphic signature.
class ExpandPolymorphicSignature : public PolymorphicConvention {
unsigned numTypeMetadataPtrs = 0;

public:
ExpandPolymorphicSignature(IRGenModule &IGM, CanSILFunctionType fn)
: PolymorphicConvention(IGM, fn) {}

void expand(SmallVectorImpl<llvm::Type *> &out,
SmallVectorImpl<PolymorphicSignatureExpandedTypeSource> *reqs) {
unsigned
expand(SmallVectorImpl<llvm::Type *> &out,
SmallVectorImpl<PolymorphicSignatureExpandedTypeSource> *reqs) {
auto outStartSize = out.size();
(void)outStartSize;
for (auto &source : getSources())
Expand All @@ -3472,9 +3475,13 @@ namespace {
reqs->push_back(reqt);
out.push_back(reqt.Protocol ? IGM.WitnessTablePtrTy
: IGM.TypeMetadataPtrTy);

if (!reqt.Protocol)
++numTypeMetadataPtrs;
});
assert((!reqs || reqs->size() == (out.size() - outStartSize)) &&
"missing type source for type");
return numTypeMetadataPtrs;
}

private:
Expand All @@ -3488,6 +3495,7 @@ namespace {
case MetadataSource::Kind::GenericLValueMetadata:
if (reqs)
reqs->push_back(source);
++numTypeMetadataPtrs;
return out.push_back(IGM.TypeMetadataPtrTy);
case MetadataSource::Kind::SelfMetadata:
case MetadataSource::Kind::SelfWitnessTable:
Expand All @@ -3501,11 +3509,11 @@ namespace {
} // end anonymous namespace

/// Given a generic signature, add the argument types required in order to call it.
void irgen::expandPolymorphicSignature(
unsigned irgen::expandPolymorphicSignature(
IRGenModule &IGM, CanSILFunctionType polyFn,
SmallVectorImpl<llvm::Type *> &out,
SmallVectorImpl<PolymorphicSignatureExpandedTypeSource> *outReqs) {
ExpandPolymorphicSignature(IGM, polyFn).expand(out, outReqs);
return ExpandPolymorphicSignature(IGM, polyFn).expand(out, outReqs);
}

void irgen::expandTrailingWitnessSignature(IRGenModule &IGM,
Expand Down
3 changes: 2 additions & 1 deletion lib/IRGen/GenProto.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ namespace irgen {

/// Add the witness parameters necessary for calling a function with
/// the given generics clause.
void expandPolymorphicSignature(
/// Returns the number of type metadata pointers added to `types`.
unsigned expandPolymorphicSignature(
IRGenModule &IGM, CanSILFunctionType type,
SmallVectorImpl<llvm::Type *> &types,
SmallVectorImpl<PolymorphicSignatureExpandedTypeSource> *outReqs =
Expand Down