Skip to content

Commit aae0355

Browse files
committed
IRGen: Basic support for computing function signatures with shape parameters
1 parent 32d3905 commit aae0355

File tree

4 files changed

+57
-24
lines changed

4 files changed

+57
-24
lines changed

lib/IRGen/GenDistributed.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -680,22 +680,21 @@ void DistributedAccessor::emit() {
680680
// We need this to determine the expected number of witness tables
681681
// to load from the buffer provided by the caller.
682682
llvm::SmallVector<llvm::Type *, 4> targetGenericArguments;
683-
auto numDirectGenericArgs =
683+
auto expandedSignature =
684684
expandPolymorphicSignature(IGM, targetTy, targetGenericArguments);
685+
assert(expandedSignature.numShapes == 0 &&
686+
"Distributed actors don't support variadic generics");
685687

686688
// Generic arguments associated with the distributed thunk directly
687689
// e.g. `distributed func echo<T, U>(...)`
688690
assert(
689691
!IGM.getLLVMContext().supportsTypedPointers() ||
690-
numDirectGenericArgs ==
692+
expandedSignature.numTypeMetadataPtrs ==
691693
llvm::count_if(targetGenericArguments, [&](const llvm::Type *type) {
692694
return type == IGM.TypeMetadataPtrTy;
693695
}));
694696

695-
auto expectedWitnessTables =
696-
targetGenericArguments.size() - numDirectGenericArgs;
697-
698-
for (unsigned index = 0; index < numDirectGenericArgs; ++index) {
697+
for (unsigned index = 0; index < expandedSignature.numTypeMetadataPtrs; ++index) {
699698
auto offset =
700699
Size(index * IGM.DataLayout.getTypeAllocSize(IGM.TypeMetadataPtrTy));
701700
auto alignment =
@@ -708,7 +707,7 @@ void DistributedAccessor::emit() {
708707
}
709708

710709
emitLoadOfWitnessTables(witnessTables, numWitnessTables,
711-
expectedWitnessTables, arguments);
710+
expandedSignature.numWitnessTablePtrs, arguments);
712711
}
713712

714713
// Step two, let's form and emit a call to the distributed method

lib/IRGen/GenProto.cpp

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3426,31 +3426,46 @@ void irgen::bindGenericRequirement(IRGenFunction &IGF,
34263426
// Get the corresponding context type.
34273427
auto type = getInContext(requirement.getTypeParameter());
34283428

3429-
if (requirement.isWitnessTable()) {
3429+
switch (requirement.getKind()) {
3430+
case GenericRequirement::Kind::Shape: {
3431+
assert(isa<ArchetypeType>(type));
3432+
assert(value->getType() == IGF.IGM.SizeTy);
3433+
auto kind = LocalTypeDataKind::forPackShapeExpression();
3434+
IGF.setUnscopedLocalTypeData(type, kind, value);
3435+
break;
3436+
}
3437+
3438+
case GenericRequirement::Kind::Metadata: {
3439+
assert(value->getType() == IGF.IGM.TypeMetadataPtrTy);
3440+
setTypeMetadataName(IGF.IGM, value, type);
3441+
IGF.bindLocalTypeDataFromTypeMetadata(type, IsExact, value, metadataState);
3442+
break;
3443+
}
3444+
3445+
case GenericRequirement::Kind::WitnessTable: {
34303446
auto proto = requirement.getProtocol();
34313447
assert(isa<ArchetypeType>(type));
34323448
assert(value->getType() == IGF.IGM.WitnessTablePtrTy);
34333449
setProtocolWitnessTableName(IGF.IGM, value, type, proto);
34343450
auto kind = LocalTypeDataKind::forAbstractProtocolWitnessTable(proto);
34353451
IGF.setUnscopedLocalTypeData(type, kind, value);
3436-
} else {
3437-
assert(requirement.isMetadata());
3438-
assert(value->getType() == IGF.IGM.TypeMetadataPtrTy);
3439-
setTypeMetadataName(IGF.IGM, value, type);
3440-
IGF.bindLocalTypeDataFromTypeMetadata(type, IsExact, value, metadataState);
3452+
break;
3453+
}
34413454
}
34423455
}
34433456

34443457
namespace {
34453458
/// A class for expanding a polymorphic signature.
34463459
class ExpandPolymorphicSignature : public PolymorphicConvention {
3460+
unsigned numShapes = 0;
34473461
unsigned numTypeMetadataPtrs = 0;
3462+
unsigned numWitnessTablePtrs = 0;
34483463

34493464
public:
34503465
ExpandPolymorphicSignature(IRGenModule &IGM, CanSILFunctionType fn)
34513466
: PolymorphicConvention(IGM, fn) {}
34523467

3453-
unsigned
3468+
ExpandedSignature
34543469
expand(SmallVectorImpl<llvm::Type *> &out,
34553470
SmallVectorImpl<PolymorphicSignatureExpandedTypeSource> *reqs) {
34563471
auto outStartSize = out.size();
@@ -3461,17 +3476,24 @@ namespace {
34613476
enumerateUnfulfilledRequirements([&](GenericRequirement reqt) {
34623477
if (reqs)
34633478
reqs->push_back(reqt);
3464-
out.push_back(reqt.isWitnessTable() ? IGM.WitnessTablePtrTy
3465-
: IGM.TypeMetadataPtrTy);
3466-
3467-
if (reqt.isMetadata())
3479+
switch (reqt.getKind()) {
3480+
case GenericRequirement::Kind::Shape:
3481+
out.push_back(IGM.SizeTy);
3482+
++numShapes;
3483+
break;
3484+
case GenericRequirement::Kind::Metadata:
3485+
out.push_back(IGM.TypeMetadataPtrTy);
34683486
++numTypeMetadataPtrs;
3469-
else
3470-
assert(reqt.isWitnessTable());
3487+
break;
3488+
case GenericRequirement::Kind::WitnessTable:
3489+
out.push_back(IGM.WitnessTablePtrTy);
3490+
++numWitnessTablePtrs;
3491+
break;
3492+
}
34713493
});
34723494
assert((!reqs || reqs->size() == (out.size() - outStartSize)) &&
34733495
"missing type source for type");
3474-
return numTypeMetadataPtrs;
3496+
return {numShapes, numTypeMetadataPtrs, numWitnessTablePtrs};
34753497
}
34763498

34773499
private:
@@ -3499,7 +3521,7 @@ namespace {
34993521
} // end anonymous namespace
35003522

35013523
/// Given a generic signature, add the argument types required in order to call it.
3502-
unsigned irgen::expandPolymorphicSignature(
3524+
ExpandedSignature irgen::expandPolymorphicSignature(
35033525
IRGenModule &IGM, CanSILFunctionType polyFn,
35043526
SmallVectorImpl<llvm::Type *> &out,
35053527
SmallVectorImpl<PolymorphicSignatureExpandedTypeSource> *outReqs) {

lib/IRGen/GenProto.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,16 @@ namespace irgen {
9999
return -1 - (int)index;
100100
}
101101

102+
struct ExpandedSignature {
103+
unsigned numShapes;
104+
unsigned numTypeMetadataPtrs;
105+
unsigned numWitnessTablePtrs;
106+
};
107+
102108
/// Add the witness parameters necessary for calling a function with
103109
/// the given generics clause.
104-
/// Returns the number of type metadata pointers added to `types`.
105-
unsigned expandPolymorphicSignature(
110+
/// Returns the number of lowered parameters of each kind.
111+
ExpandedSignature expandPolymorphicSignature(
106112
IRGenModule &IGM, CanSILFunctionType type,
107113
SmallVectorImpl<llvm::Type *> &types,
108114
SmallVectorImpl<PolymorphicSignatureExpandedTypeSource> *outReqs =

lib/IRGen/LocalTypeDataKind.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class LocalTypeDataKind {
4949
FormalTypeMetadata,
5050
RepresentationTypeMetadata,
5151
ValueWitnessTable,
52+
Shape,
5253
// <- add more special cases here
5354

5455
// The first enumerator for an individual value witness.
@@ -95,6 +96,11 @@ class LocalTypeDataKind {
9596
return LocalTypeDataKind(ValueWitnessDiscriminatorBase + (unsigned)witness);
9697
}
9798

99+
/// A reference to the shape expression of a pack type.
100+
static LocalTypeDataKind forPackShapeExpression() {
101+
return LocalTypeDataKind(Shape);
102+
}
103+
98104
/// A reference to a protocol witness table for an archetype.
99105
///
100106
/// This only works for non-concrete types because in principle we might

0 commit comments

Comments
 (0)