Skip to content

Commit 81cd889

Browse files
authored
Merge pull request #40816 from ktoso/distributed-invocation-params
2 parents 7dd1704 + 50ae3bd commit 81cd889

26 files changed

+582
-419
lines changed

include/swift/AST/KnownIdentifiers.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,12 @@ IDENTIFIER(assignID)
267267
IDENTIFIER(resignID)
268268
IDENTIFIER(resolve)
269269
IDENTIFIER(remoteCall)
270-
IDENTIFIER(makeInvocation)
270+
IDENTIFIER(makeInvocationEncoder)
271271
IDENTIFIER(system)
272272
IDENTIFIER(ID)
273273
IDENTIFIER(id)
274274
IDENTIFIER(Invocation)
275+
IDENTIFIER(invocationDecoder)
275276
IDENTIFIER(_distributedActorRemoteInitialize)
276277
IDENTIFIER(_distributedActorDestroy)
277278
IDENTIFIER(__isRemoteActor)

include/swift/AST/KnownProtocols.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ PROTOCOL(Differentiable)
9999
PROTOCOL(DistributedActor)
100100
PROTOCOL(ActorIdentity)
101101
PROTOCOL(DistributedActorSystem)
102+
PROTOCOL(DistributedTargetInvocationEncoder)
103+
PROTOCOL(DistributedTargetInvocationDecoder)
102104

103105
PROTOCOL(AsyncSequence)
104106
PROTOCOL(AsyncIteratorProtocol)

lib/IRGen/GenMeta.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5340,8 +5340,10 @@ SpecialProtocol irgen::getSpecialProtocolID(ProtocolDecl *P) {
53405340
case KnownProtocolKind::Differentiable:
53415341
case KnownProtocolKind::FloatingPoint:
53425342
case KnownProtocolKind::Actor:
5343-
case KnownProtocolKind::DistributedActorSystem:
53445343
case KnownProtocolKind::DistributedActor:
5344+
case KnownProtocolKind::DistributedActorSystem:
5345+
case KnownProtocolKind::DistributedTargetInvocationEncoder:
5346+
case KnownProtocolKind::DistributedTargetInvocationDecoder:
53455347
case KnownProtocolKind::ActorIdentity:
53465348
case KnownProtocolKind::SerialExecutor:
53475349
case KnownProtocolKind::Sendable:

stdlib/public/Distributed/DistributedActorSystem.swift

Lines changed: 115 additions & 108 deletions
Large diffs are not rendered by default.

stdlib/public/Distributed/HeterogeneousBuffer.swift

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
import Swift
1414

1515
@available(SwiftStdlib 5.6, *)
16-
struct HeterogeneousBuffer {
16+
struct HeterogeneousBuffer: Sequence {
17+
typealias Element = UnsafeMutableRawPointer
18+
1719
let types: [Any.Type]
1820
let buffer: UnsafeMutableRawPointer
1921

@@ -47,6 +49,40 @@ struct HeterogeneousBuffer {
4749
return HeterogeneousBuffer(types: types, buffer: buffer)
4850
}
4951

52+
/// Iterator exposing writable unsafe pointers into the appropriate offsets
53+
/// of the underlying `buffer`.
54+
///
55+
/// Use this to initialize the buffer after allocating it "for types".
56+
func makeIterator() -> UnsafeHeterogeneousBufferIterator {
57+
UnsafeHeterogeneousBufferIterator(self)
58+
}
59+
struct UnsafeHeterogeneousBufferIterator: IteratorProtocol {
60+
typealias Element = UnsafeMutableRawPointer
61+
62+
let hbuf: HeterogeneousBuffer
63+
64+
var i: Int = 0
65+
var offset: Int = 0
66+
67+
init(_ hbuf: HeterogeneousBuffer) {
68+
self.hbuf = hbuf
69+
}
70+
71+
mutating func next() -> UnsafeMutableRawPointer? {
72+
guard i < hbuf.types.count else {
73+
return nil
74+
}
75+
76+
func nextOffset<T>(_: T.Type) -> Int {
77+
return MemoryLayout<T>.nextAlignedOffset(offset)
78+
}
79+
offset = _openExistential(hbuf.types[i], do: nextOffset)
80+
i += 1
81+
82+
return hbuf.buffer.advanced(by: offset)
83+
}
84+
}
85+
5086
func deinitialize() {
5187
var offset = 0
5288
func destroy<T>(_ type: T.Type) {
@@ -135,37 +171,3 @@ extension HeterogeneousBuffer {
135171
return offset
136172
}
137173
}
138-
139-
//func myCompute(_ i: Int8, _ s: String, _ d: Double) {
140-
// print("myCompute: i = \(i), s = \(s), d = \(d)")
141-
//}
142-
//
143-
//func myComputeThunk(buffer: UnsafeMutableRawPointer) {
144-
// var offset = 0
145-
//
146-
// offset = MemoryLayout<Int8>.nextAlignedOffset(offset)
147-
// let i = buffer.load(fromByteOffset: offset, as: Int8.self)
148-
// offset += MemoryLayout<Int>.size
149-
//
150-
// offset = MemoryLayout<String>.nextAlignedOffset(offset)
151-
// let s = buffer.load(fromByteOffset: offset, as: String.self)
152-
// offset += MemoryLayout<String>.size
153-
//
154-
// offset = MemoryLayout<Double>.nextAlignedOffset(offset)
155-
// let d = buffer.load(fromByteOffset: offset, as: Double.self)
156-
// offset += MemoryLayout<Double>.size
157-
//
158-
// myCompute(i, s, d)
159-
//}
160-
//
161-
//let values: [Any] = [1 as Int8, "hello", 3.14159]
162-
//print("Original array of existential values: \(values)")
163-
//let buffer = HeterogeneousBuffer.allocate(values: values)
164-
//print("Mapped back into an array of existential values: \(buffer.existentialValues)")
165-
//myComputeThunk(buffer: buffer.buffer)
166-
//buffer.deinitialize()
167-
//buffer.deallocate()
168-
//
169-
//// [1, "hello", 3.14159]
170-
//// [1, "hello", 3.14159]
171-
//// i = 1, s = hello, d = 3.14159

test/Distributed/Inputs/FakeDistributedActorSystems.swift

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public struct ActorAddress: Hashable, Sendable, Codable {
3737

3838
public struct FakeActorSystem: DistributedActorSystem {
3939
public typealias ActorID = ActorAddress
40-
public typealias Invocation = FakeInvocation
40+
public typealias InvocationDecoder = FakeInvocation
41+
public typealias InvocationEncoder = FakeInvocation
4142
public typealias SerializationRequirement = Codable
4243

4344
// just so that the struct does not get optimized away entirely
@@ -70,27 +71,29 @@ public struct FakeActorSystem: DistributedActorSystem {
7071
public func resignID(_ id: ActorID) {
7172
}
7273

73-
public func makeInvocation() -> Invocation {
74+
public func makeInvocationEncoder() -> InvocationDecoder {
7475
.init()
7576
}
7677
}
7778

78-
public struct FakeInvocation: DistributedTargetInvocation {
79-
public typealias ArgumentDecoder = FakeArgumentDecoder
79+
public struct FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
8080
public typealias SerializationRequirement = Codable
8181

82-
public mutating func recordGenericSubstitution<T>(mangledType: T.Type) throws {}
83-
public mutating func recordArgument<Argument: SerializationRequirement>(argument: Argument) throws {}
84-
public mutating func recordReturnType<R: SerializationRequirement>(mangledType: R.Type) throws {}
85-
public mutating func recordErrorType<E: Error>(mangledType: E.Type) throws {}
82+
public mutating func recordGenericSubstitution<T>(_ type: T.Type) throws {}
83+
public mutating func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
84+
public mutating func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
85+
public mutating func recordErrorType<E: Error>(_ type: E.Type) throws {}
8686
public mutating func doneRecording() throws {}
8787

8888
// === Receiving / decoding -------------------------------------------------
8989

90-
public mutating func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
91-
public mutating func argumentDecoder() -> FakeArgumentDecoder { .init() }
92-
public mutating func decodeReturnType() throws -> Any.Type? { nil }
93-
public mutating func decodeErrorType() throws -> Any.Type? { nil }
90+
public func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
91+
public mutating func decodeNextArgument<Argument>(
92+
_ argumentType: Argument.Type,
93+
into pointer: UnsafeMutablePointer<Argument> // pointer to our hbuffer
94+
) throws { /* ... */ }
95+
public func decodeReturnType() throws -> Any.Type? { nil }
96+
public func decodeErrorType() throws -> Any.Type? { nil }
9497

9598
public struct FakeArgumentDecoder: DistributedTargetInvocationArgumentDecoder {
9699
public typealias SerializationRequirement = Codable

test/Distributed/Inputs/dynamic_replacement_da_decl.swift

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ struct ActorAddress: Hashable, Sendable, Codable {
3636

3737
final class FakeActorSystem: DistributedActorSystem {
3838
typealias ActorID = ActorAddress
39-
typealias Invocation = FakeInvocation
39+
typealias InvocationDecoder = FakeInvocation
40+
typealias InvocationEncoder = FakeInvocation
4041
typealias SerializationRequirement = Codable
4142

4243
func resolve<Act>(id: ActorID, as actorType: Act.Type) throws -> Act?
@@ -59,27 +60,29 @@ final class FakeActorSystem: DistributedActorSystem {
5960
func resignID(_ id: ActorID) {
6061
}
6162

62-
func makeInvocation() -> Invocation {
63+
func makeInvocationEncoder() -> InvocationDecoder {
6364
.init()
6465
}
6566
}
6667

67-
struct FakeInvocation: DistributedTargetInvocation {
68-
typealias ArgumentDecoder = FakeArgumentDecoder
68+
struct FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
6969
typealias SerializationRequirement = Codable
7070

71-
mutating func recordGenericSubstitution<T>(mangledType: T.Type) throws {}
72-
mutating func recordArgument<Argument: SerializationRequirement>(argument: Argument) throws {}
73-
mutating func recordReturnType<R: SerializationRequirement>(mangledType: R.Type) throws {}
74-
mutating func recordErrorType<E: Error>(mangledType: E.Type) throws {}
71+
mutating func recordGenericSubstitution<T>(_ type: T.Type) throws {}
72+
mutating func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
73+
mutating func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
74+
mutating func recordErrorType<E: Error>(_ type: E.Type) throws {}
7575
mutating func doneRecording() throws {}
7676

7777
// === Receiving / decoding -------------------------------------------------
7878

79-
mutating func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
80-
mutating func argumentDecoder() -> FakeArgumentDecoder { .init() }
81-
mutating func decodeReturnType() throws -> Any.Type? { nil }
82-
mutating func decodeErrorType() throws -> Any.Type? { nil }
79+
func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
80+
mutating func decodeNextArgument<Argument>(
81+
_ argumentType: Argument.Type,
82+
into pointer: UnsafeMutablePointer<Argument> // pointer to our hbuffer
83+
) throws { /* ... */ }
84+
func decodeReturnType() throws -> Any.Type? { nil }
85+
func decodeErrorType() throws -> Any.Type? { nil }
8386

8487
struct FakeArgumentDecoder: DistributedTargetInvocationArgumentDecoder {
8588
typealias SerializationRequirement = Codable

test/Distributed/Runtime/distributed_actor_decode.swift

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ struct ActorAddress: Hashable, Sendable, Codable {
4242

4343
final class FakeActorSystem: DistributedActorSystem {
4444
typealias ActorID = ActorAddress
45-
typealias Invocation = FakeInvocation
45+
typealias InvocationDecoder = FakeInvocation
46+
typealias InvocationEncoder = FakeInvocation
4647
typealias SerializationRequirement = Codable
4748

4849
func resolve<Act>(id: ActorID, as actorType: Act.Type) throws -> Act?
@@ -69,27 +70,29 @@ final class FakeActorSystem: DistributedActorSystem {
6970
print("resign address:\(id)")
7071
}
7172

72-
func makeInvocation() -> Invocation {
73+
func makeInvocationEncoder() -> InvocationDecoder {
7374
.init()
7475
}
7576
}
7677

77-
struct FakeInvocation: DistributedTargetInvocation {
78-
typealias ArgumentDecoder = FakeArgumentDecoder
78+
struct FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
7979
typealias SerializationRequirement = Codable
8080

81-
mutating func recordGenericSubstitution<T>(mangledType: T.Type) throws {}
82-
mutating func recordArgument<Argument: SerializationRequirement>(argument: Argument) throws {}
83-
mutating func recordReturnType<R: SerializationRequirement>(mangledType: R.Type) throws {}
84-
mutating func recordErrorType<E: Error>(mangledType: E.Type) throws {}
81+
mutating func recordGenericSubstitution<T>(_ type: T.Type) throws {}
82+
mutating func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {}
83+
mutating func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws {}
84+
mutating func recordErrorType<E: Error>(_ type: E.Type) throws {}
8585
mutating func doneRecording() throws {}
8686

8787
// === Receiving / decoding -------------------------------------------------
8888

89-
mutating func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
90-
mutating func argumentDecoder() -> FakeArgumentDecoder { .init() }
91-
mutating func decodeReturnType() throws -> Any.Type? { nil }
92-
mutating func decodeErrorType() throws -> Any.Type? { nil }
89+
func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
90+
mutating func decodeNextArgument<Argument>(
91+
_ argumentType: Argument.Type,
92+
into pointer: UnsafeMutablePointer<Argument> // pointer to our hbuffer
93+
) throws { /* ... */ }
94+
func decodeReturnType() throws -> Any.Type? { nil }
95+
func decodeErrorType() throws -> Any.Type? { nil }
9396

9497
struct FakeArgumentDecoder: DistributedTargetInvocationArgumentDecoder {
9598
typealias SerializationRequirement = Codable

test/Distributed/Runtime/distributed_actor_deinit.swift

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ struct ActorAddress: Sendable, Hashable, Codable {
5656

5757
final class FakeActorSystem: @unchecked Sendable, DistributedActorSystem {
5858
typealias ActorID = ActorAddress
59-
typealias Invocation = FakeDistributedInvocation
59+
typealias SerializationRequirement = Codable
60+
typealias InvocationDecoder = FakeDistributedInvocation
61+
typealias InvocationEncoder = FakeDistributedInvocation
6062

6163
var n = 0
6264

@@ -86,27 +88,38 @@ final class FakeActorSystem: @unchecked Sendable, DistributedActorSystem {
8688
print("resign address:\(id)")
8789
}
8890

89-
@inlinable func makeInvocation() throws -> Invocation {
91+
@inlinable func makeInvocationEncoder() throws -> InvocationEncoder {
9092
.init()
9193
}
9294
}
9395

94-
struct FakeDistributedInvocation: DistributedTargetInvocation {
96+
struct FakeDistributedInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
9597
typealias ArgumentDecoder = FakeDistributedTargetInvocationArgumentDecoder
9698
typealias SerializationRequirement = Codable
9799

98-
mutating func recordGenericSubstitution<T>(mangledType: T.Type) throws { }
99-
mutating func recordArgument<Argument: SerializationRequirement>(argument: Argument) throws { }
100-
mutating func recordReturnType<R: SerializationRequirement>(mangledType: R.Type) throws { }
101-
mutating func recordErrorType<E: Error>(mangledType: E.Type) throws { }
100+
mutating func recordGenericSubstitution<T>(_ type: T.Type) throws { }
101+
mutating func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws { }
102+
mutating func recordReturnType<R: SerializationRequirement>(_ type: R.Type) throws { }
103+
mutating func recordErrorType<E: Error>(_ type: E.Type) throws { }
102104
mutating func doneRecording() throws { }
103105

104106
// === Receiving / decoding -------------------------------------------------
105107

106-
mutating func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
107-
mutating func argumentDecoder() -> Self.ArgumentDecoder { .init() }
108-
mutating func decodeReturnType() throws -> Any.Type? { nil }
109-
mutating func decodeErrorType() throws -> Any.Type? { nil }
108+
func decodeGenericSubstitutions() throws -> [Any.Type] {
109+
[]
110+
}
111+
func decodeNextArgument<Argument>(
112+
_ argumentType: Argument.Type,
113+
into pointer: UnsafeMutablePointer<Argument> // pointer to our hbuffer
114+
) throws {
115+
// ...
116+
}
117+
func decodeReturnType() throws -> Any.Type? {
118+
nil
119+
}
120+
func decodeErrorType() throws -> Any.Type? {
121+
nil
122+
}
110123
}
111124

112125
struct FakeDistributedTargetInvocationArgumentDecoder: DistributedTargetInvocationArgumentDecoder {

test/Distributed/Runtime/distributed_actor_dynamic_remote_func.swift

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ struct ActorAddress: Sendable, Hashable, Codable {
4848

4949
struct FakeActorSystem: DistributedActorSystem {
5050
typealias ActorID = ActorAddress
51-
typealias Invocation = FakeInvocation
51+
typealias InvocationDecoder = FakeInvocation
52+
typealias InvocationEncoder = FakeInvocation
5253
typealias SerializationRequirement = Codable
5354

5455
func resolve<Act>(id: ActorID, as actorType: Act.Type) throws -> Act?
@@ -75,27 +76,29 @@ struct FakeActorSystem: DistributedActorSystem {
7576
print("ready id:\(id)")
7677
}
7778

78-
func makeInvocation() -> Invocation {
79+
func makeInvocationEncoder() -> InvocationDecoder {
7980
.init()
8081
}
8182
}
8283

83-
struct FakeInvocation: DistributedTargetInvocation {
84-
typealias ArgumentDecoder = FakeArgumentDecoder
84+
struct FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
8585
typealias SerializationRequirement = Codable
8686

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

9393
// === Receiving / decoding -------------------------------------------------
9494

95-
mutating func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
96-
mutating func argumentDecoder() -> FakeArgumentDecoder { .init() }
97-
mutating func decodeReturnType() throws -> Any.Type? { nil }
98-
mutating func decodeErrorType() throws -> Any.Type? { nil }
95+
func decodeGenericSubstitutions() throws -> [Any.Type] { [] }
96+
mutating func decodeNextArgument<Argument>(
97+
_ argumentType: Argument.Type,
98+
into pointer: UnsafeMutablePointer<Argument> // pointer to our hbuffer
99+
) throws { /* ... */ }
100+
func decodeReturnType() throws -> Any.Type? { nil }
101+
func decodeErrorType() throws -> Any.Type? { nil }
99102

100103
struct FakeArgumentDecoder: DistributedTargetInvocationArgumentDecoder {
101104
typealias SerializationRequirement = Codable

0 commit comments

Comments
 (0)