Skip to content

Commit 8f022b9

Browse files
authored
Merge pull request #64008 from slavapestov/variadic-generic-types-metadata-cache-key
Runtime metadata cache key support for packs
2 parents cf8abc9 + 86248e4 commit 8f022b9

File tree

3 files changed

+290
-71
lines changed

3 files changed

+290
-71
lines changed

include/swift/ABI/GenericContext.h

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class GenericContextDescriptorFlags {
4646

4747
/// Whether this generic context has at least one type parameter
4848
/// pack, in which case the generic context will have a trailing
49-
/// GenericParamPackShapeHeader.
49+
/// GenericPackShapeHeader.
5050
constexpr bool hasTypePacks() const {
5151
return (Value & 0x1) != 0;
5252
}
@@ -245,23 +245,51 @@ class TargetGenericRequirementDescriptor {
245245
using GenericRequirementDescriptor =
246246
TargetGenericRequirementDescriptor<InProcess>;
247247

248-
struct GenericParamPackShapeHeader {
249-
/// The number of generic parameters which are packs.
248+
struct GenericPackShapeHeader {
249+
/// The number of generic parameters and conformance requirements
250+
/// which are packs.
250251
///
251-
/// Must equal the number of GenericParamDescriptors whose kind is
252-
/// GenericParamKind::TypePack.
253-
uint16_t NumTypePacks;
252+
/// Must equal the sum of:
253+
/// - the number of GenericParamDescriptors whose kind is
254+
/// GenericParamKind::TypePack and isKeyArgument bits set;
255+
/// - the number of GenericRequirementDescriptors with the
256+
/// isPackRequirement and isKeyArgument bits set
257+
uint16_t NumPacks;
254258

255259
/// The number of equivalence classes in the same-shape relation.
256260
uint16_t NumShapeClasses;
257261
};
258262

259-
struct GenericParamPackShapeDescriptor {
260-
/// The equivalence class of this generic parameter pack under
261-
/// the same-shape relation.
263+
enum class GenericPackKind: uint16_t {
264+
Metadata = 0,
265+
WitnessTable = 1
266+
};
267+
268+
/// The GenericPackShapeHeader is followed by an array of these descriptors,
269+
/// whose length is given by the header's NumPacks field.
270+
///
271+
/// The invariant is that all pack descriptors with GenericPackKind::Metadata
272+
/// must precede those with GenericPackKind::WitnessTable, and for each kind,
273+
/// the pack descriptors are ordered by their Index.
274+
///
275+
/// This allows us to iterate over the generic arguments array in parallel
276+
/// with the array of pack shape descriptors. We know we have a metadata
277+
/// or witness table when we reach the generic argument whose index is
278+
/// stored in the next descriptor; we increment the descriptor pointer in
279+
/// this case.
280+
struct GenericPackShapeDescriptor {
281+
GenericPackKind Kind;
282+
283+
/// The index of this metadata pack or witness table pack in the
284+
/// generic arguments array.
285+
uint16_t Index;
286+
287+
/// The equivalence class of this pack under the same-shape relation.
262288
///
263-
/// Must be less than GenericParamPackShapeHeader::NumShapeClasses.
289+
/// Must be less than GenericPackShapeHeader::NumShapeClasses.
264290
uint16_t ShapeClass;
291+
292+
uint16_t Unused;
265293
};
266294

267295
/// An array of generic parameter descriptors, all
@@ -299,8 +327,8 @@ class RuntimeGenericSignature {
299327
TargetGenericContextDescriptorHeader<Runtime> Header;
300328
const GenericParamDescriptor *Params;
301329
const TargetGenericRequirementDescriptor<Runtime> *Requirements;
302-
GenericParamPackShapeHeader PackShapeHeader;
303-
const GenericParamPackShapeDescriptor *PackShapeDescriptors;
330+
GenericPackShapeHeader PackShapeHeader;
331+
const GenericPackShapeDescriptor *PackShapeDescriptors;
304332

305333
public:
306334
RuntimeGenericSignature()
@@ -310,8 +338,8 @@ class RuntimeGenericSignature {
310338
RuntimeGenericSignature(const TargetGenericContextDescriptorHeader<Runtime> &header,
311339
const GenericParamDescriptor *params,
312340
const TargetGenericRequirementDescriptor<Runtime> *requirements,
313-
const GenericParamPackShapeHeader &packShapeHeader,
314-
const GenericParamPackShapeDescriptor *packShapeDescriptors)
341+
const GenericPackShapeHeader &packShapeHeader,
342+
const GenericPackShapeDescriptor *packShapeDescriptors)
315343
: Header(header), Params(params), Requirements(requirements),
316344
PackShapeHeader(packShapeHeader), PackShapeDescriptors(packShapeDescriptors) {}
317345

@@ -323,8 +351,12 @@ class RuntimeGenericSignature {
323351
return llvm::makeArrayRef(Requirements, Header.NumRequirements);
324352
}
325353

326-
llvm::ArrayRef<GenericParamPackShapeDescriptor> getPackShapeDescriptors() const {
327-
return llvm::makeArrayRef(PackShapeDescriptors, PackShapeHeader.NumTypePacks);
354+
const GenericPackShapeHeader &getGenericPackShapeHeader() const {
355+
return PackShapeHeader;
356+
}
357+
358+
llvm::ArrayRef<GenericPackShapeDescriptor> getGenericPackShapeDescriptors() const {
359+
return llvm::makeArrayRef(PackShapeDescriptors, PackShapeHeader.NumPacks);
328360
}
329361

330362
size_t getArgumentLayoutSizeInWords() const {
@@ -417,8 +449,8 @@ class TrailingGenericContextObjects<TargetSelf<Runtime>,
417449
TargetGenericContextHeaderType<Runtime>,
418450
GenericParamDescriptor,
419451
TargetGenericRequirementDescriptor<Runtime>,
420-
GenericParamPackShapeHeader,
421-
GenericParamPackShapeDescriptor,
452+
GenericPackShapeHeader,
453+
GenericPackShapeDescriptor,
422454
FollowingTrailingObjects...>
423455
{
424456
protected:
@@ -431,8 +463,8 @@ class TrailingGenericContextObjects<TargetSelf<Runtime>,
431463
GenericContextHeaderType,
432464
GenericParamDescriptor,
433465
GenericRequirementDescriptor,
434-
GenericParamPackShapeHeader,
435-
GenericParamPackShapeDescriptor,
466+
GenericPackShapeHeader,
467+
GenericPackShapeDescriptor,
436468
FollowingTrailingObjects...>;
437469
friend TrailingObjects;
438470

@@ -487,21 +519,21 @@ class TrailingGenericContextObjects<TargetSelf<Runtime>,
487519
getGenericContextHeader().NumRequirements};
488520
}
489521

490-
GenericParamPackShapeHeader getGenericParamPackShapeHeader() const {
522+
GenericPackShapeHeader getGenericPackShapeHeader() const {
491523
if (!asSelf()->isGeneric())
492524
return {0, 0};
493525
if (!getGenericContextHeader().Flags.hasTypePacks())
494526
return {0, 0};
495-
return *this->template getTrailingObjects<GenericParamPackShapeHeader>();
527+
return *this->template getTrailingObjects<GenericPackShapeHeader>();
496528
}
497529

498-
llvm::ArrayRef<GenericParamPackShapeDescriptor> getGenericParamPackShapeDescriptors() const {
499-
auto header = getGenericParamPackShapeHeader();
500-
if (header.NumTypePacks == 0)
530+
llvm::ArrayRef<GenericPackShapeDescriptor> getGenericPackShapeDescriptors() const {
531+
auto header = getGenericPackShapeHeader();
532+
if (header.NumPacks == 0)
501533
return {};
502534

503-
return {this->template getTrailingObjects<GenericParamPackShapeDescriptor>(),
504-
header.NumTypePacks};
535+
return {this->template getTrailingObjects<GenericPackShapeDescriptor>(),
536+
header.NumPacks};
505537
}
506538

507539
/// Return the amount of space that the generic arguments take up in
@@ -516,8 +548,8 @@ class TrailingGenericContextObjects<TargetSelf<Runtime>,
516548
return {getGenericContextHeader(),
517549
getGenericParams().data(),
518550
getGenericRequirements().data(),
519-
getGenericParamPackShapeHeader(),
520-
getGenericParamPackShapeDescriptors().data()};
551+
getGenericPackShapeHeader(),
552+
getGenericPackShapeDescriptors().data()};
521553
}
522554

523555
protected:
@@ -533,21 +565,21 @@ class TrailingGenericContextObjects<TargetSelf<Runtime>,
533565
return asSelf()->isGeneric() ? getGenericContextHeader().NumRequirements : 0;
534566
}
535567

536-
size_t numTrailingObjects(OverloadToken<GenericParamPackShapeHeader>) const {
568+
size_t numTrailingObjects(OverloadToken<GenericPackShapeHeader>) const {
537569
if (!asSelf()->isGeneric())
538570
return 0;
539571

540572
return getGenericContextHeader().Flags.hasTypePacks() ? 1 : 0;
541573
}
542574

543-
size_t numTrailingObjects(OverloadToken<GenericParamPackShapeDescriptor>) const {
575+
size_t numTrailingObjects(OverloadToken<GenericPackShapeDescriptor>) const {
544576
if (!asSelf()->isGeneric())
545577
return 0;
546578

547579
if (!getGenericContextHeader().Flags.hasTypePacks())
548580
return 0;
549581

550-
return getGenericParamPackShapeHeader().NumTypePacks;
582+
return getGenericPackShapeHeader().NumPacks;
551583
}
552584

553585
#if defined(_MSC_VER) && _MSC_VER < 1920

include/swift/ABI/Metadata.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,10 +1932,10 @@ struct TargetExtendedExistentialTypeShape
19321932
TargetGenericRequirementDescriptor<Runtime>,
19331933
// Optional header describing any type packs in the generalization
19341934
// signature.
1935-
GenericParamPackShapeHeader,
1935+
GenericPackShapeHeader,
19361936
// For each type pack in the generalization signature, a descriptor
19371937
// storing the shape class.
1938-
GenericParamPackShapeDescriptor> {
1938+
GenericPackShapeDescriptor> {
19391939
private:
19401940
using RelativeValueWitnessTablePointer =
19411941
TargetRelativeIndirectablePointer<Runtime,
@@ -1949,8 +1949,8 @@ struct TargetExtendedExistentialTypeShape
19491949
RelativeValueWitnessTablePointer,
19501950
GenericParamDescriptor,
19511951
TargetGenericRequirementDescriptor<Runtime>,
1952-
GenericParamPackShapeHeader,
1953-
GenericParamPackShapeDescriptor>;
1952+
GenericPackShapeHeader,
1953+
GenericPackShapeDescriptor>;
19541954
friend TrailingObjects;
19551955

19561956
template<typename T>
@@ -1977,11 +1977,11 @@ struct TargetExtendedExistentialTypeShape
19771977
return getNumGenSigRequirements() + getNumReqSigRequirements();
19781978
}
19791979

1980-
size_t numTrailingObjects(OverloadToken<GenericParamPackShapeHeader>) const {
1980+
size_t numTrailingObjects(OverloadToken<GenericPackShapeHeader>) const {
19811981
return (Flags.hasTypePacks() ? 1 : 0);
19821982
}
19831983

1984-
size_t numTrailingObjects(OverloadToken<GenericParamPackShapeDescriptor>) const {
1984+
size_t numTrailingObjects(OverloadToken<GenericPackShapeDescriptor>) const {
19851985
if (!Flags.hasTypePacks())
19861986
return 0;
19871987

@@ -2142,18 +2142,18 @@ struct TargetExtendedExistentialTypeShape
21422142
return getReqSigRequirements() + ReqSigHeader.NumRequirements;
21432143
}
21442144

2145-
GenericParamPackShapeHeader getGenSigPackShapeHeader() const {
2145+
GenericPackShapeHeader getGenSigPackShapeHeader() const {
21462146
assert(hasGeneralizationSignature());
21472147
if (!Flags.hasTypePacks())
21482148
return {0, 0};
2149-
return *this->template getTrailingObjects<GenericParamPackShapeHeader>();
2149+
return *this->template getTrailingObjects<GenericPackShapeHeader>();
21502150
}
21512151

2152-
const GenericParamPackShapeDescriptor *getGenSigPackShapeDescriptors() const {
2152+
const GenericPackShapeDescriptor *getGenSigPackShapeDescriptors() const {
21532153
assert(hasGeneralizationSignature());
21542154
if (!Flags.hasTypePacks())
21552155
return nullptr;
2156-
return this->template getTrailingObjects<GenericParamPackShapeDescriptor>();
2156+
return this->template getTrailingObjects<GenericPackShapeDescriptor>();
21572157
}
21582158

21592159
/// Return the amount of space used in ExtendedExistentialTypeMetadata
@@ -4915,23 +4915,28 @@ class TargetPackPointer {
49154915
public:
49164916
explicit TargetPackPointer(typename Runtime::StoredSize rawPtr) : Ptr(rawPtr) {}
49174917

4918+
explicit TargetPackPointer(const void *rawPtr)
4919+
: Ptr(reinterpret_cast<typename Runtime::StoredSize>(rawPtr)) {}
4920+
49184921
explicit TargetPackPointer(PointerType const *ptr, PackLifetime lifetime)
49194922
: Ptr(reinterpret_cast<typename Runtime::StoredSize>(ptr) |
49204923
(lifetime == PackLifetime::OnHeap ? 1 : 0)) {}
49214924

4925+
// Strips off the LSB.
49224926
const PointerType *getElements() const {
49234927
return reinterpret_cast<const PointerType *>(Ptr & ~1);
49244928
}
49254929

4930+
// Strips off the LSB.
49264931
PointerType *getElements() {
49274932
return reinterpret_cast<PointerType *>(Ptr & ~1);
49284933
}
49294934

4935+
// Leaves the LSB.
49304936
const PointerType *getPointer() const {
49314937
return reinterpret_cast<const PointerType *>(Ptr);
49324938
}
49334939

4934-
49354940
PackLifetime getLifetime() const {
49364941
return (bool)(Ptr & 1) ? PackLifetime::OnHeap : PackLifetime::OnStack;
49374942
}

0 commit comments

Comments
 (0)