Skip to content

Commit 80187e8

Browse files
committed
Expect caller heap pack and rename
Fix comment
1 parent 3dbad6e commit 80187e8

File tree

3 files changed

+53
-70
lines changed

3 files changed

+53
-70
lines changed

include/swift/Runtime/Metadata.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,14 +1047,16 @@ void swift_initRawStructMetadata(StructMetadata *self,
10471047
int32_t count);
10481048

10491049
/// Check if the given generic arguments are valid inputs for the generic type
1050-
/// context and if so call the access function to return
1050+
/// context and if so call the metadata access function and return the metadata.
1051+
///
1052+
/// Note: This expects the caller to heap allocate all pack pointers within the
1053+
/// generic arguments via 'swift_allocateMetadataPack'.
10511054
SWIFT_RUNTIME_STDLIB_SPI
10521055
SWIFT_CC(swift)
1053-
const Metadata *_swift_checkedCreateType(const TypeContextDescriptor *context,
1054-
const void * const *genericArgs,
1055-
size_t genericArgsSize,
1056-
const int32_t *packCounts,
1057-
size_t packCountsSize);
1056+
const Metadata *_swift_instantiateCheckedGenericMetadata(
1057+
const TypeContextDescriptor *context,
1058+
const void * const *genericArgs,
1059+
size_t genericArgsSize);
10581060

10591061
#pragma clang diagnostic pop
10601062

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,45 +2832,25 @@ swift_getOpaqueTypeConformance(const void * const *arguments,
28322832

28332833
SWIFT_RUNTIME_STDLIB_SPI
28342834
SWIFT_CC(swift)
2835-
const Metadata *swift::_swift_checkedCreateType(const TypeContextDescriptor *context,
2836-
const void * const *genericArgs,
2837-
size_t genericArgsSize,
2838-
const int32_t *packCounts,
2839-
size_t packCountsSize) {
2835+
const Metadata *swift::_swift_instantiateCheckedGenericMetadata(
2836+
const TypeContextDescriptor *context,
2837+
const void * const *genericArgs,
2838+
size_t genericArgsSize) {
28402839
context = swift_auth_data_non_address(
28412840
context, SpecialPointerAuthDiscriminators::ContextDescriptor);
28422841

28432842
if (!context->isGeneric()) {
28442843
return nullptr;
28452844
}
28462845

2847-
if (packCounts && genericArgsSize != packCountsSize) {
2848-
return nullptr;
2849-
}
2850-
2851-
llvm::SmallVector<MetadataOrPack, 8> fixedGenericArgs;
2852-
2853-
// Heap allocate all of the potential stack allocated pack pointers
2854-
for (size_t i = 0; i != genericArgsSize; i += 1) {
2855-
// Use -1 to indicate that this is not a pack pointer and instead just
2856-
// metadata. If we were not passed a packCount array, treat all elements as
2857-
// if they were just metadata.
2858-
if ((packCounts && packCounts[i] == -1) || !packCounts) {
2859-
fixedGenericArgs.push_back(MetadataOrPack(genericArgs[i]));
2860-
continue;
2861-
}
2862-
2863-
auto packPointer = swift_allocateMetadataPack(
2864-
reinterpret_cast<const Metadata *const *>(genericArgs[i]), packCounts[i]);
2865-
fixedGenericArgs.push_back(MetadataOrPack(packPointer));
2866-
}
2867-
28682846
DemanglerForRuntimeTypeResolution<StackAllocatedDemangler<2048>> demangler;
28692847

2848+
llvm::ArrayRef<MetadataOrPack> genericArgsRef(
2849+
reinterpret_cast<const MetadataOrPack *>(genericArgs), genericArgsSize);
28702850
llvm::SmallVector<unsigned, 8> genericParamCounts;
28712851
llvm::SmallVector<const void *, 8> allGenericArgs;
28722852

2873-
auto result = _gatherGenericParameters(context, fixedGenericArgs,
2853+
auto result = _gatherGenericParameters(context, genericArgsRef,
28742854
/* parent */ nullptr,
28752855
genericParamCounts, allGenericArgs,
28762856
demangler);

test/Runtime/check_create_type.swift

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ struct Variadic<each T> {
1313
struct Nested<U, each V: Equatable> {}
1414
}
1515

16-
@_silgen_name("_swift_checkedCreateType")
17-
func _checkedCreateType(
16+
@_silgen_name("swift_allocateMetadataPack")
17+
func allocateMetadataPack(
18+
_ packPointer: UnsafeRawPointer,
19+
_ packCount: UInt
20+
) -> UnsafeRawPointer
21+
22+
@_silgen_name("_swift_instantiateCheckedGenericMetadata")
23+
func _instantiateCheckedGenericMetadata(
1824
_ descriptor: UnsafeRawPointer,
1925
_ genericArgs: UnsafeRawPointer,
20-
_ genericArgsSize: UInt,
21-
_ packCounts: UnsafeRawPointer?,
22-
_ packCountsSize: UInt
26+
_ genericArgsSize: UInt
2327
) -> Any.Type?
2428

2529
func metaPointer(_ x: Any.Type) -> UnsafeRawPointer {
@@ -39,12 +43,10 @@ testSuite.test("_swift_checkedCreateType non-variadic") {
3943
let dictGenericArgs: [Any.Type] = [String.self, Double.self]
4044

4145
dictGenericArgs.withUnsafeBufferPointer {
42-
let newDict = _checkedCreateType(
46+
let newDict = _instantiateCheckedGenericMetadata(
4347
dictDesc,
4448
UnsafeRawPointer($0.baseAddress!),
45-
UInt($0.count),
46-
nil,
47-
0
49+
UInt($0.count)
4850
)
4951

5052
expectTrue(newDict == [String: Double].self)
@@ -62,23 +64,22 @@ testSuite.test("_swift_checkedCreateType variadic") {
6264
)
6365

6466
let variPack: [Any.Type] = [Int.self, Int8.self, UInt8.self]
65-
let variPackCounts: [Int32] = [3]
6667

6768
variPack.withUnsafeBufferPointer { pack in
68-
let genericArgs = [UnsafeRawPointer(pack.baseAddress!)]
69+
let packPointer = allocateMetadataPack(
70+
UnsafeRawPointer(pack.baseAddress!),
71+
UInt(pack.count)
72+
)
73+
let genericArgs = [packPointer]
6974

7075
genericArgs.withUnsafeBufferPointer { genericArgs in
71-
variPackCounts.withUnsafeBufferPointer { packCounts in
72-
let newVari = _checkedCreateType(
73-
variDesc,
74-
UnsafeRawPointer(genericArgs.baseAddress!),
75-
UInt(genericArgs.count),
76-
UnsafeRawPointer(packCounts.baseAddress!),
77-
UInt(packCounts.count)
78-
)
76+
let newVari = _instantiateCheckedGenericMetadata(
77+
variDesc,
78+
UnsafeRawPointer(genericArgs.baseAddress!),
79+
UInt(genericArgs.count)
80+
)
7981

80-
expectTrue(newVari == Variadic<Int, Int8, UInt8>.self)
81-
}
82+
expectTrue(newVari == Variadic<Int, Int8, UInt8>.self)
8283
}
8384
}
8485
}
@@ -100,26 +101,26 @@ testSuite.test("_swift_checkedCreateType variadic nested with requirements") {
100101
nestedPack.withUnsafeBufferPointer { nestedPack in
101102
variPack.withUnsafeBufferPointer { variPack in
102103
let nestedGenericArgs = [
103-
UnsafeRawPointer(variPack.baseAddress!),
104+
allocateMetadataPack(
105+
UnsafeRawPointer(variPack.baseAddress!),
106+
UInt(variPack.count)
107+
),
104108
metaPointer(Int16.self),
105-
UnsafeRawPointer(nestedPack.baseAddress!)
109+
allocateMetadataPack(
110+
UnsafeRawPointer(nestedPack.baseAddress!),
111+
UInt(nestedPack.count)
112+
)
106113
]
107114

108115
nestedGenericArgs.withUnsafeBufferPointer { nestedGenericArgs in
109-
// 3 for each T, -1 for U, and 3 for each V
110-
let nestedPackCounts: [Int32] = [3, -1, 3]
111-
112-
nestedPackCounts.withUnsafeBufferPointer { nestedPackCounts in
113-
let newNested = _checkedCreateType(
114-
nestedDesc,
115-
UnsafeRawPointer(nestedGenericArgs.baseAddress!),
116-
UInt(nestedGenericArgs.count),
117-
UnsafeRawPointer(nestedPackCounts.baseAddress!),
118-
UInt(nestedPackCounts.count)
119-
)
120-
121-
expectTrue(newNested == Variadic<String, [Int], UInt64>.Nested<Int16, Int, Substring, Bool>.self)
122-
}
116+
117+
let newNested = _instantiateCheckedGenericMetadata(
118+
nestedDesc,
119+
UnsafeRawPointer(nestedGenericArgs.baseAddress!),
120+
UInt(nestedGenericArgs.count)
121+
)
122+
123+
expectTrue(newNested == Variadic<String, [Int], UInt64>.Nested<Int16, Int, Substring, Bool>.self)
123124
}
124125
}
125126
}

0 commit comments

Comments
 (0)