Skip to content

Commit 3729dca

Browse files
committed
Clean impl
1 parent 8e9ff9b commit 3729dca

File tree

4 files changed

+41
-28
lines changed

4 files changed

+41
-28
lines changed

lib/IRGen/GenDistributed.cpp

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ static CanSILFunctionType getAccessorType(IRGenModule &IGM,
219219
// A generic parameter that represents instance of invocation decoder.
220220
auto *decoderType =
221221
GenericTypeParamType::get(/*isParameterPack=*/false,
222-
/*depth=*/1, /*index=*/0, Context);
222+
/*depth=*/0, /*index=*/0, Context);
223+
auto *actorType =
224+
GenericTypeParamType::get(/*isParameterPack=*/false,
225+
/*depth=*/0, /*index=*/1, Context);
223226

224227
// decoder
225228
parameters.push_back(GenericFunctionType::Param(
@@ -246,14 +249,8 @@ static CanSILFunctionType getAccessorType(IRGenModule &IGM,
246249
// number of witness tables
247250
parameters.push_back(GenericFunctionType::Param(Context.getUIntType()));
248251

249-
// actor
250-
{
251-
auto targetTy = Target->getLoweredFunctionType();
252-
auto actorLoc = targetTy->getParameters().back();
253-
254-
parameters.push_back(
255-
GenericFunctionType::Param(actorLoc.getInterfaceType()));
256-
}
252+
// actor type
253+
parameters.push_back(GenericFunctionType::Param(actorType));
257254

258255
auto decoderProtocolTy =
259256
Context
@@ -266,19 +263,17 @@ static CanSILFunctionType getAccessorType(IRGenModule &IGM,
266263
SmallVector<GenericTypeParamType *, 4> genericParams;
267264
SmallVector<Requirement, 4> genericRequirements;
268265

269-
auto *actor = getDistributedActorOf(Target);
270-
assert(actor);
271-
272-
for (auto *genericParam : actor->getInnermostGenericParamTypes())
273-
genericParams.push_back(genericParam);
274-
275266
// Add a generic parameter `D` which stands for decoder type in the
276267
// accessor signature - `inout D`.
277268
genericParams.push_back(decoderType);
278269
// Add a requirement that decoder conforms to the expected protocol.
279270
genericRequirements.push_back(
280271
{RequirementKind::Conformance, decoderType, decoderProtocolTy});
281272

273+
// Add a generic type for the actor type of the target.
274+
// We don't have to put any requirement on it; all we need is a type here to use for the self.
275+
genericParams.push_back(actorType);
276+
282277
signature = GenericSignature::get(genericParams, genericRequirements);
283278
}
284279

@@ -647,11 +642,8 @@ void DistributedAccessor::emit() {
647642
// Metadata that represents passed in the invocation decoder.
648643
auto *decoderType = params.claimNext();
649644

650-
// If the distributed thunk is declared in a protocol that conforms
651-
// to `DistributedActor` protocol, there is an extract parameter that
652-
// represents a type of protocol witness.
653-
if (isa<ProtocolDecl>(actor))
654-
(void)params.claimNext();
645+
// Metadata of the distributed actor the accessor is for.
646+
(void)params.claimNext();
655647

656648
// Witness table for decoder conformance to DistributedTargetInvocationDecoder
657649
auto *decoderProtocolWitness = params.claimNext();

stdlib/public/Distributed/DistributedActor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ using TargetExecutorSignature =
6262
/*numWitnessTables=*/size_t,
6363
/*decoderType=*/Metadata *,
6464
/*decoderWitnessTable=*/void **),
65-
/*throws=*/true>;
65+
/*throws=*/true>;
6666

6767
SWIFT_CC(swiftasync)
6868
SWIFT_EXPORT_FROM(swiftDistributed)
@@ -89,7 +89,7 @@ using DistributedAccessorSignature =
8989
/*actor=*/HeapObject *,
9090
/*decoderType=*/Metadata *,
9191
/*decoderWitnessTable=*/void **),
92-
/*throws=*/true>;
92+
/*throws=*/true>;
9393

9494
SWIFT_CC(swiftasync)
9595
static DistributedAccessorSignature::ContinuationType

test/Distributed/Runtime/distributed_actor_hop_to.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ protocol LifecycleWatch: DistributedActor where ActorSystem == FakeRoundtripActo
2626

2727
extension LifecycleWatch {
2828
func watch<T: Codable>(x: Int, _ y: T) async throws {
29+
self.preconditionIsolated()
2930
print("executed: \(#function) - x = \(x), y = \(y)")
3031
}
3132

@@ -39,15 +40,35 @@ extension LifecycleWatch {
3940
distributed actor Worker: LifecycleWatch {
4041
}
4142

43+
extension Worker {
44+
distributed actor Inside<Something> {
45+
distributed func testSecond<A: Codable>(_: A) async throws {
46+
self.preconditionIsolated()
47+
print("executed: \(#function)")
48+
}
49+
distributed func moreParams<A: Codable, B: Codable>(_: A, _: B) async throws {
50+
self.preconditionIsolated()
51+
print("executed: \(#function)")
52+
}
53+
}
54+
}
55+
4256
@main struct Main {
4357
static func main() async {
44-
let worker: any LifecycleWatch = Worker(actorSystem: DefaultDistributedActorSystem())
58+
let system = DefaultDistributedActorSystem()
59+
let worker: any LifecycleWatch = Worker(actorSystem: system)
4560
try! await worker.test(x: 42, "on protocol")
4661

4762
// CHECK: executed: test(x:_:)
4863
// CHECK: executed: watch(x:_:) - x = 42, y = on protocol
4964
// CHECK: done executed: test(x:_:)
5065

66+
try! await Worker.Inside<String>(actorSystem: system).testSecond(42)
67+
// CHECK: executed: test
68+
69+
try! await Worker.Inside<String>(actorSystem: system).moreParams(21, 42)
70+
// CHECK: executed: moreParams
71+
5172
print("OK") // CHECK: OK
5273
}
5374
}

test/Distributed/distributed_actor_accessor_thunks_64bit.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public distributed actor MyOtherActor {
9494

9595
// CHECK: define hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC7simple1yySiYaKFTE"
9696

97-
// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC7simple1yySiYaKFTETF"(ptr swiftasync %0, ptr %1, ptr %2, ptr %3, {{.*}}, ptr [[ACTOR:%.*]], ptr [[DECODER_TYPE:%.*]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]])
97+
// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC7simple1yySiYaKFTETF"(ptr swiftasync %0, ptr %1, ptr %2, ptr %3, {{.*}}, ptr {{(noalias)?}} [[ACTOR:%.*]], ptr [[DECODER_TYPE:%.*]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]], {{.*}})
9898

9999
/// Read the current offset and cast an element to `Int`
100100

@@ -192,7 +192,7 @@ public distributed actor MyOtherActor {
192192
// CHECK: define hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC7simple3ySiSSYaKFTE"
193193

194194
/// !!! in `simple3` interesting bits are: argument value extraction (because string is exploded into N arguments) and call to distributed thunk
195-
// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC7simple3ySiSSYaKFTETF"(ptr swiftasync %0, ptr [[ARG_DECODER:%.*]], ptr [[ARG_TYPES:%.*]], ptr [[RESULT_BUFF:%.*]], ptr [[SUBS:%.*]], ptr [[WITNESS_TABLES:%.*]], i64 [[NUM_WITNESS_TABLES:%.*]], ptr [[ACTOR]], ptr [[DECODER_TYPE:%.*]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]])
195+
// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC7simple3ySiSSYaKFTETF"(ptr swiftasync %0, ptr [[ARG_DECODER:%.*]], ptr [[ARG_TYPES:%.*]], ptr [[RESULT_BUFF:%.*]], ptr [[SUBS:%.*]], ptr [[WITNESS_TABLES:%.*]], i64 [[NUM_WITNESS_TABLES:%.*]], ptr {{(noalias)?}} [[ACTOR]], ptr [[DECODER_TYPE:%.*]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]], {{.*}})
196196

197197

198198
// CHECK: [[ARG_SIZE:%.*]] = and i64 {{.*}}, -16
@@ -232,7 +232,7 @@ public distributed actor MyOtherActor {
232232

233233
// CHECK: define hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC16single_case_enumyAA7SimpleEOAFYaKFTE"
234234

235-
// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC16single_case_enumyAA7SimpleEOAFYaKFTETF"(ptr swiftasync %0, ptr [[ARG_DECODER:%.*]], ptr [[ARG_TYPES:%.*]], ptr [[RESULT_BUFF:%.*]], ptr [[SUBS:%.*]], ptr [[WITNESS_TABLES:%.*]], i64 [[NUM_WITNESS_TABLES:%.*]], ptr [[ACTOR]], ptr [[DECODER_TYPE]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]])
235+
// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC16single_case_enumyAA7SimpleEOAFYaKFTETF"(ptr swiftasync %0, ptr [[ARG_DECODER:%.*]], ptr [[ARG_TYPES:%.*]], ptr [[RESULT_BUFF:%.*]], ptr [[SUBS:%.*]], ptr [[WITNESS_TABLES:%.*]], i64 [[NUM_WITNESS_TABLES:%.*]], ptr {{(noalias)?}} [[ACTOR]], ptr [[DECODER_TYPE]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]], {{.*}})
236236

237237
/// Let's check that the call doesn't have any arguments and returns nothing.
238238

@@ -272,7 +272,7 @@ public distributed actor MyOtherActor {
272272

273273
// CHECK: define hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC7complexyAA11LargeStructVSaySiG_AA3ObjCSSSgAFtYaKFTE"
274274

275-
// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC7complexyAA11LargeStructVSaySiG_AA3ObjCSSSgAFtYaKFTETF"(ptr swiftasync {{.*}}, ptr [[ARG_DECODER:%.*]], ptr [[ARG_TYPES:%.*]], ptr [[RESULT_BUFF:%.*]], ptr [[SUBS:%.*]], ptr [[WITNESS_TABLES:%.*]], i64 [[NUM_WITNESS_TABLES:%.*]], ptr [[ACTOR]], ptr [[DECODER_TYPE:%.*]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]])
275+
// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC7complexyAA11LargeStructVSaySiG_AA3ObjCSSSgAFtYaKFTETF"(ptr swiftasync {{.*}}, ptr [[ARG_DECODER:%.*]], ptr [[ARG_TYPES:%.*]], ptr [[RESULT_BUFF:%.*]], ptr [[SUBS:%.*]], ptr [[WITNESS_TABLES:%.*]], i64 [[NUM_WITNESS_TABLES:%.*]], ptr noalias [[ACTOR]], ptr [[DECODER_TYPE:%.*]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]], {{.*}})
276276

277277
/// First, let's check that all of the different argument types here are loaded correctly.
278278

@@ -319,7 +319,7 @@ public distributed actor MyOtherActor {
319319

320320
/// ---> Accessor for `genericArgs`
321321

322-
// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC11genericArgsyyx_Sayq_GtYaKSeRzSERzSeR_SER_r0_lFTETF"(ptr swiftasync %0, ptr [[ARG_DECODER:%.*]], ptr [[ARG_TYPES:%.*]], ptr [[RESULT_BUF:%.*]], ptr [[GENERIC_SUBS:%.*]], ptr [[WITNESS_TABLES:%.*]], i64 [[NUM_WITNESS_TABLES:%.*]], ptr [[ACTOR:%.*]], ptr [[DECODER_TYPE:%.*]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]])
322+
// CHECK: define linkonce_odr hidden swift{{(tail)?}}cc void @"$s27distributed_actor_accessors7MyActorC11genericArgsyyx_Sayq_GtYaKSeRzSERzSeR_SER_r0_lFTETF"(ptr swiftasync %0, ptr [[ARG_DECODER:%.*]], ptr [[ARG_TYPES:%.*]], ptr [[RESULT_BUF:%.*]], ptr [[GENERIC_SUBS:%.*]], ptr [[WITNESS_TABLES:%.*]], i64 [[NUM_WITNESS_TABLES:%.*]], ptr {{(noalias)?}} [[ACTOR:%.*]], ptr [[DECODER_TYPE:%.*]], ptr [[DECODER_PROTOCOL_WITNESS:%.*]])
323323

324324
/// ---> Load `T`
325325

0 commit comments

Comments
 (0)