Skip to content

Commit 206969a

Browse files
committed
[Distributed] Augment parameter/result type decoded to support generic substitutions
1 parent 0e35951 commit 206969a

File tree

4 files changed

+75
-33
lines changed

4 files changed

+75
-33
lines changed

stdlib/public/Distributed/DistributedActorSystem.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,24 @@ extension DistributedActorSystem {
175175
UInt(nameUTF8.endIndex))
176176
}
177177

178+
var substitutionsBuffer: UnsafeMutablePointer<Any.Type>? = nil
179+
180+
if genericEnv != nil {
181+
let subs = try invocationDecoder.decodeGenericSubstitutions()
182+
183+
if subs.isEmpty {
184+
throw ExecuteDistributedTargetError(
185+
message: "Cannot call generic method without generic argument substitutions")
186+
}
187+
188+
substitutionsBuffer = .allocate(capacity: subs.count)
189+
190+
for (offset, substitution) in subs.enumerated() {
191+
let element = substitutionsBuffer?.advanced(by: offset)
192+
element?.initialize(to: substitution)
193+
}
194+
}
195+
178196
let paramCount = nameUTF8.withUnsafeBufferPointer { nameUTF8 in
179197
__getParameterCount(nameUTF8.baseAddress!, UInt(nameUTF8.endIndex))
180198
}
@@ -201,6 +219,7 @@ extension DistributedActorSystem {
201219
__getParameterTypeInfo(
202220
nameUTF8.baseAddress!, UInt(nameUTF8.endIndex),
203221
genericEnv,
222+
substitutionsBuffer,
204223
paramTypesBuffer.baseAddress!._rawValue, Int(paramCount))
205224
}
206225

@@ -229,7 +248,8 @@ extension DistributedActorSystem {
229248
}
230249

231250
guard let returnTypeFromTypeInfo: Any.Type = _getReturnTypeInfo(mangledMethodName: mangledTargetName,
232-
genericEnv: genericEnv) else {
251+
genericEnv: genericEnv,
252+
genericArguments: substitutionsBuffer) else {
233253
throw ExecuteDistributedTargetError(
234254
message: "Failed to decode distributed target return type")
235255
}

stdlib/public/Distributed/DistributedMetadata.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ public // SPI _Distributed
4343
func _getParameterTypeInfo(
4444
mangledMethodName name: String,
4545
genericEnv: UnsafeRawPointer?, // GenericEnvironmentDescriptor *
46+
genericArguments: UnsafeRawPointer?,
4647
into typesBuffer: Builtin.RawPointer, length typesLength: Int
4748
) -> Int32 {
4849
let nameUTF8 = Array(name.utf8)
4950
return nameUTF8.withUnsafeBufferPointer { nameUTF8 in
5051
return __getParameterTypeInfo(
5152
nameUTF8.baseAddress!, UInt(nameUTF8.endIndex),
52-
genericEnv, typesBuffer, typesLength)
53+
genericEnv, genericArguments, typesBuffer, typesLength)
5354
}
5455
}
5556

@@ -61,19 +62,21 @@ public // SPI _Distributed
6162
func __getParameterTypeInfo(
6263
_ typeNameStart: UnsafePointer<UInt8>, _ typeNameLength: UInt,
6364
_ genericEnv: UnsafeRawPointer?, // GenericEnvironmentDescriptor *
65+
_ genericArguments: UnsafeRawPointer?,
6466
_ types: Builtin.RawPointer, _ typesLength: Int
6567
) -> Int32
6668

6769
@available(SwiftStdlib 5.6, *)
6870
public // SPI _Distributed
6971
func _getReturnTypeInfo(
7072
mangledMethodName name: String,
71-
genericEnv: UnsafeRawPointer? // GenericEnvironmentDescriptor *
73+
genericEnv: UnsafeRawPointer?, // GenericEnvironmentDescriptor *
74+
genericArguments: UnsafeRawPointer?
7275
) -> Any.Type? {
7376
let nameUTF8 = Array(name.utf8)
7477
return nameUTF8.withUnsafeBufferPointer { nameUTF8 in
7578
return __getReturnTypeInfo(nameUTF8.baseAddress!, UInt(nameUTF8.endIndex),
76-
genericEnv)
79+
genericEnv, genericArguments)
7780
}
7881
}
7982

@@ -83,7 +86,8 @@ public // SPI _Distributed
8386
func __getReturnTypeInfo(
8487
_ typeNameStart: UnsafePointer<UInt8>,
8588
_ typeNameLength: UInt,
86-
_ genericEnv: UnsafeRawPointer? // GenericEnvironmentDescriptor *
89+
_ genericEnv: UnsafeRawPointer?, // GenericEnvironmentDescriptor *
90+
_ genericArguments: UnsafeRawPointer?
8791
) -> Any.Type?
8892

8993

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,6 +1967,22 @@ static NodePointer getParameterList(NodePointer funcType) {
19671967
return parameterContainer;
19681968
}
19691969

1970+
static const Metadata *decodeType(TypeDecoder<DecodedMetadataBuilder> &decoder,
1971+
NodePointer type) {
1972+
assert(type->getKind() == Node::Kind::Type);
1973+
1974+
auto builtTypeOrError = decoder.decodeMangledType(type);
1975+
1976+
if (builtTypeOrError.isError()) {
1977+
auto err = builtTypeOrError.getError();
1978+
char *errStr = err->copyErrorString();
1979+
err->freeErrorString(errStr);
1980+
return nullptr;
1981+
}
1982+
1983+
return builtTypeOrError.getType();
1984+
}
1985+
19701986
SWIFT_CC(swift)
19711987
SWIFT_RUNTIME_STDLIB_SPI
19721988
unsigned swift_func_getParameterCount(const char *typeNameStart,
@@ -1985,7 +2001,8 @@ unsigned swift_func_getParameterCount(const char *typeNameStart,
19852001
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_SPI
19862002
const Metadata *_Nullable
19872003
swift_func_getReturnTypeInfo(const char *typeNameStart, size_t typeNameLength,
1988-
GenericEnvironmentDescriptor *genericEnv) {
2004+
GenericEnvironmentDescriptor *genericEnv,
2005+
const void * const *genericArguments) {
19892006
StackAllocatedDemangler<1024> demangler;
19902007

19912008
auto *funcType =
@@ -1999,31 +2016,30 @@ swift_func_getReturnTypeInfo(const char *typeNameStart, size_t typeNameLength,
19992016

20002017
assert(resultType->getKind() == Node::Kind::ReturnType);
20012018

2019+
SubstGenericParametersFromMetadata substFn(genericEnv, genericArguments);
2020+
20022021
DecodedMetadataBuilder builder(
20032022
demangler,
20042023
/*substGenericParam=*/
2005-
[](unsigned, unsigned) { return nullptr; },
2024+
[&substFn](unsigned depth, unsigned index) {
2025+
return substFn.getMetadata(depth, index);
2026+
},
20062027
/*SubstDependentWitnessTableFn=*/
2007-
[](const Metadata *, unsigned) { return nullptr; });
2028+
[&substFn](const Metadata *type, unsigned index) {
2029+
return substFn.getWitnessTable(type, index);
2030+
});
20082031

20092032
TypeDecoder<DecodedMetadataBuilder> decoder(builder);
2010-
auto builtTypeOrError =
2011-
decoder.decodeMangledType(resultType->getFirstChild());
2012-
if (builtTypeOrError.isError()) {
2013-
auto err = builtTypeOrError.getError();
2014-
char *errStr = err->copyErrorString();
2015-
err->freeErrorString(errStr);
2016-
return nullptr;
2017-
}
20182033

2019-
return builtTypeOrError.getType();
2034+
return decodeType(decoder, resultType->getFirstChild());
20202035
}
20212036

20222037
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_SPI
20232038
unsigned
20242039
swift_func_getParameterTypeInfo(
20252040
const char *typeNameStart, size_t typeNameLength,
20262041
GenericEnvironmentDescriptor *genericEnv,
2042+
const void * const *genericArguments,
20272043
Metadata const **types, unsigned typesLength) {
20282044
if (typesLength < 0) return -1;
20292045

@@ -2041,35 +2057,36 @@ swift_func_getParameterTypeInfo(
20412057
if (!(parameterList && parameterList->getNumChildren() == typesLength))
20422058
return -2;
20432059

2060+
SubstGenericParametersFromMetadata substFn(genericEnv, genericArguments);
2061+
20442062
DecodedMetadataBuilder builder(
20452063
demangler,
20462064
/*substGenericParam=*/
2047-
[](unsigned, unsigned) { return nullptr; },
2065+
[&substFn](unsigned depth, unsigned index) {
2066+
return substFn.getMetadata(depth, index);
2067+
},
20482068
/*SubstDependentWitnessTableFn=*/
2049-
[](const Metadata *, unsigned) { return nullptr; });
2069+
[&substFn](const Metadata *type, unsigned index) {
2070+
return substFn.getWitnessTable(type, index);
2071+
});
20502072
TypeDecoder<DecodedMetadataBuilder> decoder(builder);
20512073

2052-
auto typeIdx = 0;
20532074
// for each parameter (TupleElement), store it into the provided buffer
2054-
for (auto *parameter : *parameterList) {
2075+
for (unsigned index = 0; index != typesLength; ++index) {
2076+
auto *parameter = parameterList->getChild(index);
2077+
20552078
if (parameter->getKind() == Node::Kind::TupleElement) {
20562079
assert(parameter->getNumChildren() == 1);
20572080
parameter = parameter->getFirstChild();
20582081
}
20592082

20602083
assert(parameter->getKind() == Node::Kind::Type);
20612084

2062-
auto builtTypeOrError = decoder.decodeMangledType(parameter);
2063-
if (builtTypeOrError.isError()) {
2064-
auto err = builtTypeOrError.getError();
2065-
char *errStr = err->copyErrorString();
2066-
err->freeErrorString(errStr);
2067-
typeIdx += 1;
2068-
continue;
2069-
}
2085+
auto type = decodeType(decoder, parameter);
2086+
if (!type)
2087+
return -3; // Failed to decode a type.
20702088

2071-
types[typeIdx] = builtTypeOrError.getType();
2072-
++typeIdx;
2089+
types[index] = type;
20732090
} // end foreach parameter
20742091

20752092
return typesLength;

test/Distributed/Runtime/distributed_func_metadata.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ typealias DefaultDistributedActorSystem = FakeActorSystem
118118
static func test_returnType() {
119119
print("~~ \(#function)")
120120
// CHECK: _getReturnTypeInfo: empty() = ()
121-
print("_getReturnTypeInfo: empty() = \(String(reflecting: _getReturnTypeInfo(mangledMethodName: empty, genericEnv: nil)!))")
121+
print("_getReturnTypeInfo: empty() = \(String(reflecting: _getReturnTypeInfo(mangledMethodName: empty, genericEnv: nil, genericArguments: nil)!))")
122122

123123
// CHECK: _getReturnTypeInfo: one(s:) = Swift.Int
124-
print("_getReturnTypeInfo: one(s:) = \(String(reflecting: _getReturnTypeInfo(mangledMethodName: one, genericEnv: nil)!))")
124+
print("_getReturnTypeInfo: one(s:) = \(String(reflecting: _getReturnTypeInfo(mangledMethodName: one, genericEnv: nil, genericArguments: nil)!))")
125125
}
126126

127127
static func test_paramTypes() {
@@ -179,6 +179,7 @@ func _withParameterTypeInfo(
179179
let decodedNum = __getParameterTypeInfo(
180180
nameUTF8.baseAddress!, UInt(nameUTF8.endIndex),
181181
/*genericEnvironment=*/nil,
182+
/*genericArguments=*/nil,
182183
infoBuffer.baseAddress!._rawValue, Int(paramCount))
183184

184185
// if we failed demangling the types, return an empty array

0 commit comments

Comments
 (0)