Skip to content

Commit d0fdf20

Browse files
committed
[Distributed] Runtime: Switch to use argument decoder instead of heterogeneous buffer
1 parent 7b159d3 commit d0fdf20

15 files changed

+147
-204
lines changed

stdlib/public/Distributed/DistributedActor.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void *swift_distributed_getGenericEnvironment(const char *targetNameStart,
4444
/// on: AnyObject,
4545
/// _ targetName: UnsafePointer<UInt8>,
4646
/// _ targetNameLength: UInt,
47-
/// argumentBuffer: Builtin.RawPointer,
47+
/// argumentDecoder: AnyObject,
4848
/// argumentTypes: UnsafeBufferPointer<Any.Type>,
4949
/// resultBuffer: Builtin.RawPointer,
5050
/// substitutions: UnsafeRawPointer?,
@@ -54,7 +54,7 @@ void *swift_distributed_getGenericEnvironment(const char *targetNameStart,
5454
using TargetExecutorSignature =
5555
AsyncSignature<void(/*on=*/DefaultActor *,
5656
/*targetName=*/const char *, /*targetNameSize=*/size_t,
57-
/*argumentBuffer=*/void *,
57+
/*argumentDecoder=*/HeapObject *,
5858
/*argumentTypes=*/const Metadata *const *,
5959
/*resultBuffer=*/void *,
6060
/*substitutions=*/void *,
@@ -70,15 +70,15 @@ TargetExecutorSignature::FunctionType swift_distributed_execute_target;
7070

7171
/// Accessor takes:
7272
/// - an async context
73-
/// - an argument buffer as a raw pointer
73+
/// - an argument decoder as an instance of type conforming to `InvocationDecoder`
7474
/// - a list of all argument types (with substitutions applied)
7575
/// - a result buffer as a raw pointer
7676
/// - a list of substitutions
7777
/// - a list of witness tables
7878
/// - a number of witness tables in the buffer
7979
/// - a reference to an actor to execute method on.
8080
using DistributedAccessorSignature =
81-
AsyncSignature<void(/*argumentBuffer=*/void *,
81+
AsyncSignature<void(/*argumentDecoder=*/HeapObject *,
8282
/*argumentTypes=*/const Metadata *const *,
8383
/*resultBuffer=*/void *,
8484
/*substitutions=*/void *,
@@ -110,7 +110,7 @@ void ::swift_distributed_execute_target(
110110
SWIFT_ASYNC_CONTEXT AsyncContext *callerContext,
111111
DefaultActor *actor,
112112
const char *targetNameStart, size_t targetNameLength,
113-
void *argumentBuffer,
113+
HeapObject *argumentDecoder,
114114
const Metadata *const *argumentTypes,
115115
void *resultBuffer,
116116
void *substitutions,
@@ -150,7 +150,7 @@ void ::swift_distributed_execute_target(
150150
swift_distributed_execute_target_resume);
151151

152152
accessorEntry(calleeContext,
153-
argumentBuffer, argumentTypes,
153+
argumentDecoder, argumentTypes,
154154
resultBuffer,
155155
substitutions,
156156
witnessTables,

stdlib/public/Distributed/DistributedActorSystem.swift

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ extension DistributedActorSystem {
154154
public func executeDistributedTarget<Act, ResultHandler>(
155155
on actor: Act,
156156
mangledTargetName: String,
157-
invocationDecoder: inout InvocationDecoder,
157+
invocationDecoder: InvocationDecoder,
158158
handler: ResultHandler
159159
) async throws where Act: DistributedActor,
160160
// Act.ID == ActorID, // FIXME(distributed): can we bring this back?
@@ -281,48 +281,16 @@ extension DistributedActorSystem {
281281
_openExistential(returnTypeFromTypeInfo, do: destroyReturnTypeBuffer)
282282
}
283283

284-
// Prepare the buffer to decode the argument values into
285-
let hargs = HeterogeneousBuffer.allocate(forTypes: argumentTypes)
286-
defer {
287-
hargs.deinitialize()
288-
hargs.deallocate()
289-
}
290-
291284
do {
292-
// Decode the invocation and pack arguments into the h-buffer
293-
294-
// TODO(distributed): decode the generics info
295-
// TODO(distributed): move this into the IRGen synthesized funcs, so we don't need hargs at all and can specialize the decodeNextArgument calls
296-
do {
297-
var argumentIdx = 0
298-
for unsafeRawArgPointer in hargs {
299-
guard argumentIdx < paramCount else {
300-
throw ExecuteDistributedTargetError(
301-
message: "Unexpected attempt to decode more parameters than expected: \(argumentIdx + 1)")
302-
}
303-
let argumentType = argumentTypes[argumentIdx]
304-
argumentIdx += 1
305-
306-
// FIXME(distributed): func doDecode<Arg: SerializationRequirement>(_: Arg.Type) throws {
307-
// FIXME: but how would we call this...?
308-
// FIXME: > type 'Arg' constrained to non-protocol, non-class type 'Self.Invocation.SerializationRequirement'
309-
func doDecodeArgument<Arg>(_: Arg.Type) throws {
310-
let unsafeArgPointer = unsafeRawArgPointer
311-
.bindMemory(to: Arg.self, capacity: 1)
312-
try invocationDecoder.decodeNextArgument(Arg.self, into: unsafeArgPointer)
313-
}
314-
try _openExistential(argumentType, do: doDecodeArgument)
315-
}
316-
}
317-
318285
let returnType = try invocationDecoder.decodeReturnType() ?? returnTypeFromTypeInfo
319286
// let errorType = try invocation.decodeErrorType() // TODO: decide how to use?
320287

288+
var decoderAny = invocationDecoder as Any
321289
// Execute the target!
322290
try await _executeDistributedTarget(
323291
on: actor,
324292
mangledTargetName, UInt(mangledTargetName.count),
325-
argumentBuffer: hargs.buffer._rawValue, // TODO(distributed): pass the invocationDecoder instead, so we can decode inside IRGen directly into the argument explosion
293+
argumentDecoder: invocationDecoder,
326294
argumentTypes: argumentTypesBuffer.baseAddress!._rawValue,
327295
resultBuffer: resultBuffer._rawValue,
328296
substitutions: UnsafeRawPointer(substitutionsBuffer),
@@ -345,7 +313,7 @@ extension DistributedActorSystem {
345313
func _executeDistributedTarget(
346314
on actor: AnyObject, // DistributedActor
347315
_ targetName: UnsafePointer<UInt8>, _ targetNameLength: UInt,
348-
argumentBuffer: Builtin.RawPointer, // HeterogeneousBuffer of arguments
316+
argumentDecoder: AnyObject, // concrete type for `InvocationDecoder`
349317
argumentTypes: Builtin.RawPointer,
350318
resultBuffer: Builtin.RawPointer,
351319
substitutions: UnsafeRawPointer?,
@@ -433,7 +401,7 @@ public protocol DistributedTargetInvocationEncoder {
433401

434402
/// Decoder that must be provided to `executeDistributedTarget` and is used
435403
/// by the Swift runtime to decode arguments of the invocation.
436-
public protocol DistributedTargetInvocationDecoder {
404+
public protocol DistributedTargetInvocationDecoder : AnyObject {
437405
associatedtype SerializationRequirement
438406

439407
func decodeGenericSubstitutions() throws -> [Any.Type]
@@ -456,10 +424,7 @@ public protocol DistributedTargetInvocationDecoder {
456424
// ) throws
457425

458426
// FIXME(distributed): remove this since it must have the ': SerializationRequirement'
459-
mutating func decodeNextArgument<Argument>(
460-
_ argumentType: Argument.Type,
461-
into pointer: UnsafeMutablePointer<Argument> // pointer to our hbuffer
462-
) throws
427+
func decodeNextArgument<Argument>() throws -> Argument
463428

464429
func decodeErrorType() throws -> Any.Type?
465430

test/Distributed/Inputs/FakeDistributedActorSystems.swift

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,19 @@ public struct FakeActorSystem: DistributedActorSystem {
103103
}
104104
}
105105

106-
public struct FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
106+
public class FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
107107
public typealias SerializationRequirement = Codable
108108

109-
public mutating func recordGenericSubstitution<T>(_ type: T.Type) throws {}
110-
public mutating func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
111-
public mutating func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
112-
public mutating func recordErrorType<E: Error>(_ type: E.Type) throws {}
113-
public mutating func doneRecording() throws {}
109+
public func recordGenericSubstitution<T>(_ type: T.Type) throws {}
110+
public func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
111+
public func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
112+
public func recordErrorType<E: Error>(_ type: E.Type) throws {}
113+
public func doneRecording() throws {}
114114

115115
// === Receiving / decoding -------------------------------------------------
116116

117117
public func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
118-
public mutating func decodeNextArgument<Argument>(
119-
_ argumentType: Argument.Type,
120-
into pointer: UnsafeMutablePointer<Argument> // pointer to our hbuffer
121-
) throws { /* ... */ }
118+
public func decodeNextArgument<Argument>() throws -> Argument { fatalError() }
122119
public func decodeReturnType() throws -> Any.Type? { nil }
123120
public func decodeErrorType() throws -> Any.Type? { nil }
124121
}

test/Distributed/Inputs/dynamic_replacement_da_decl.swift

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,19 @@ struct FakeDistributedSystemError: DistributedActorSystemError {
8282
let message: String
8383
}
8484

85-
struct FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
85+
class FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
8686
typealias SerializationRequirement = Codable
8787

88-
mutating func recordGenericSubstitution<T>(_ type: T.Type) throws {}
89-
mutating func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
90-
mutating func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
91-
mutating func recordErrorType<E: Error>(_ type: E.Type) throws {}
92-
mutating func doneRecording() throws {}
88+
func recordGenericSubstitution<T>(_ type: T.Type) throws {}
89+
func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
90+
func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
91+
func recordErrorType<E: Error>(_ type: E.Type) throws {}
92+
func doneRecording() throws {}
9393

9494
// === Receiving / decoding -------------------------------------------------
9595

9696
func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
97-
mutating func decodeNextArgument<Argument>(
98-
_ argumentType: Argument.Type,
99-
into pointer: UnsafeMutablePointer<Argument> // pointer to our hbuffer
100-
) throws { /* ... */ }
97+
func decodeNextArgument<Argument>() throws -> Argument { fatalError() }
10198
func decodeReturnType() throws -> Any.Type? { nil }
10299
func decodeErrorType() throws -> Any.Type? { nil }
103100
}

test/Distributed/Runtime/distributed_actor_decode.swift

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,19 @@ final class FakeActorSystem: DistributedActorSystem {
101101

102102
}
103103

104-
struct FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
104+
class FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
105105
typealias SerializationRequirement = Codable
106106

107-
mutating func recordGenericSubstitution<T>(_ type: T.Type) throws {}
108-
mutating func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
109-
mutating func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
110-
mutating func recordErrorType<E: Error>(_ type: E.Type) throws {}
111-
mutating func doneRecording() throws {}
107+
func recordGenericSubstitution<T>(_ type: T.Type) throws {}
108+
func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
109+
func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
110+
func recordErrorType<E: Error>(_ type: E.Type) throws {}
111+
func doneRecording() throws {}
112112

113113
// === Receiving / decoding -------------------------------------------------
114114

115115
func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
116-
mutating func decodeNextArgument<Argument>(
117-
_ argumentType: Argument.Type,
118-
into pointer: UnsafeMutablePointer<Argument> // pointer to our hbuffer
119-
) throws { /* ... */ }
116+
func decodeNextArgument<Argument>() throws -> Argument { fatalError() }
120117
func decodeReturnType() throws -> Any.Type? { nil }
121118
func decodeErrorType() throws -> Any.Type? { nil }
122119
}

test/Distributed/Runtime/distributed_actor_deinit.swift

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,22 @@ final class FakeActorSystem: @unchecked Sendable, DistributedActorSystem {
119119
}
120120
}
121121

122-
struct FakeDistributedInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
122+
class FakeDistributedInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
123123
typealias SerializationRequirement = Codable
124124

125-
mutating func recordGenericSubstitution<T>(_ type: T.Type) throws { }
126-
mutating func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws { }
127-
mutating func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws { }
128-
mutating func recordErrorType<E: Error>(_ type: E.Type) throws { }
129-
mutating func doneRecording() throws { }
125+
func recordGenericSubstitution<T>(_ type: T.Type) throws { }
126+
func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws { }
127+
func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws { }
128+
func recordErrorType<E: Error>(_ type: E.Type) throws { }
129+
func doneRecording() throws { }
130130

131131
// === Receiving / decoding -------------------------------------------------
132132

133133
func decodeGenericSubstitutions() throws -> [Any.Type] {
134134
[]
135135
}
136-
func decodeNextArgument<Argument>(
137-
_ argumentType: Argument.Type,
138-
into pointer: UnsafeMutablePointer<Argument> // pointer to our hbuffer
139-
) throws {
140-
// ...
136+
func decodeNextArgument<Argument>() throws -> Argument {
137+
fatalError()
141138
}
142139
func decodeReturnType() throws -> Any.Type? {
143140
nil

test/Distributed/Runtime/distributed_actor_isRemote.swift

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,24 +93,19 @@ struct FakeActorSystem: DistributedActorSystem {
9393
}
9494
}
9595

96-
struct FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
96+
class FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
9797
typealias SerializationRequirement = Codable
9898

99-
let string: String = "" // FIXME(distributed): cannot deal with trivial types yet
100-
101-
mutating func recordGenericSubstitution<T>(_ type: T.Type) throws {}
102-
mutating func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
103-
mutating func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
104-
mutating func recordErrorType<E: Error>(_ type: E.Type) throws {}
105-
mutating func doneRecording() throws {}
99+
func recordGenericSubstitution<T>(_ type: T.Type) throws {}
100+
func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
101+
func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
102+
func recordErrorType<E: Error>(_ type: E.Type) throws {}
103+
func doneRecording() throws {}
106104

107105
// === Receiving / decoding -------------------------------------------------
108106

109107
func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
110-
mutating func decodeNextArgument<Argument>(
111-
_ argumentType: Argument.Type,
112-
into pointer: UnsafeMutablePointer<Argument> // pointer to our hbuffer
113-
) throws { /* ... */ }
108+
func decodeNextArgument<Argument>() throws -> Argument { fatalError() }
114109
func decodeReturnType() throws -> Any.Type? { nil }
115110
func decodeErrorType() throws -> Any.Type? { nil }
116111
}

test/Distributed/Runtime/distributed_actor_local.swift

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,19 @@ struct FakeActorSystem: DistributedActorSystem {
9797

9898
}
9999

100-
struct FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
100+
class FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
101101
typealias SerializationRequirement = Codable
102102

103-
mutating func recordGenericSubstitution<T>(_ type: T.Type) throws {}
104-
mutating func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
105-
mutating func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
106-
mutating func recordErrorType<E: Error>(_ type: E.Type) throws {}
107-
mutating func doneRecording() throws {}
103+
func recordGenericSubstitution<T>(_ type: T.Type) throws {}
104+
func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
105+
func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
106+
func recordErrorType<E: Error>(_ type: E.Type) throws {}
107+
func doneRecording() throws {}
108108

109109
// === Receiving / decoding -------------------------------------------------
110110

111111
func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
112-
mutating func decodeNextArgument<Argument>(
113-
_ argumentType: Argument.Type,
114-
into pointer: UnsafeMutablePointer<Argument> // pointer to our hbuffer
115-
) throws { /* ... */ }
112+
func decodeNextArgument<Argument>() throws -> Argument { fatalError() }
116113
func decodeReturnType() throws -> Any.Type? { nil }
117114
func decodeErrorType() throws -> Any.Type? { nil }
118115
}

0 commit comments

Comments
 (0)