Skip to content

Commit 54e72a8

Browse files
committed
[Distributed] make remote proxy allocation minimally small
1 parent 412fb93 commit 54e72a8

File tree

7 files changed

+39
-4
lines changed

7 files changed

+39
-4
lines changed

include/swift/AST/KnownSDKTypes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ KNOWN_SDK_TYPE_DECL(Distributed, DistributedTargetInvocationEncoder, ProtocolDec
5757
KNOWN_SDK_TYPE_DECL(Distributed, DistributedTargetInvocationDecoder, ProtocolDecl, 0)
5858
KNOWN_SDK_TYPE_DECL(Distributed, RemoteCallTarget, StructDecl, 0)
5959
KNOWN_SDK_TYPE_DECL(Distributed, RemoteCallArgument, StructDecl, 1)
60+
KNOWN_SDK_TYPE_DECL(Distributed, _DistributedRemoteReferenceLayout, ClassDecl, 2)
6061

6162
// String processing
6263
KNOWN_SDK_TYPE_DECL(StringProcessing, Regex, StructDecl, 1)

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2447,7 +2447,7 @@ FUNCTION(NonDefaultDistributedActorInitialize,
24472447
// );
24482448
FUNCTION(DistributedActorInitializeRemote,
24492449
swift_distributedActor_remote_initialize, SwiftCC,
2450-
ConcurrencyAvailability, // TODO(distributed): Introduce DistributedAvailability once shipping somewhere
2450+
ConcurrencyAvailability,
24512451
RETURNS(OpaquePtrTy),
24522452
ARGS(TypeMetadataPtrTy),
24532453
ATTRS(NoUnwind),

lib/SILGen/SILGenDistributed.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,16 +459,30 @@ void SILGenFunction::emitDistributedActorFactory(FuncDecl *fd) { // TODO(distrib
459459
// ==== Case 'none') Create the remote instance
460460
{
461461
B.emitBlock(makeProxyBB);
462+
462463
// ==== Create 'remote' distributed actor instance
463464

464-
// --- Call: _distributedActorRemoteInitialize(Self.self)
465+
// --- Prepare metadata for remote "proxy" object initialization
466+
auto remoteReferenceStubDecl = C.get_DistributedRemoteReferenceLayoutDecl();
467+
auto systemTy = getDistributedActorSystemType(selfTyDecl);
468+
469+
auto remoteReferenceStubType = BoundGenericClassType::get(
470+
remoteReferenceStubDecl, nullptr, {systemTy});
471+
auto remoteReferenceStubMetatypeType =
472+
getLoweredType(MetatypeType::get(remoteReferenceStubType, C));
473+
474+
SILValue remoteReferenceStubMetaTypeValue =
475+
B.createMetatype(loc, remoteReferenceStubMetatypeType);
476+
477+
// --- Call: _distributedActorRemoteInitialize(_DistributedRemoteReferenceLayout.self)
465478
auto builtinName = C.getIdentifier(
466479
getBuiltinName(BuiltinValueKind::InitializeDistributedRemoteActor));
467480
auto *remote = B.createBuiltin(
468481
loc, builtinName,
469482
/*returnTy*/returnTy,
470483
/*subs*/ {},
471-
{selfMetatypeValue});
484+
// {selfMetatypeValue});
485+
{remoteReferenceStubMetaTypeValue});
472486

473487
// ==== Initialize distributed actor properties
474488
loc.markAutoGenerated();

lib/Sema/CodeSynthesisDistributedActor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,8 @@ addDistributedActorCodableConformance(
831831
/******************************************************************************/
832832
/******************************************************************************/
833833

834+
// IMPORTANT: If adding new fields, make sure that the remote stub type has the
835+
// same fields and order: _DistributedRemoteReferenceLayout
834836
void swift::assertRequiredSynthesizedPropertyOrder(ASTContext &Context,
835837
NominalTypeDecl *nominal) {
836838
#ifndef NDEBUG

stdlib/public/Concurrency/Actor.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2311,7 +2311,11 @@ OpaqueValue*
23112311
swift::swift_distributedActor_remote_initialize(const Metadata *actorType) {
23122312
const ClassMetadata *metadata = actorType->getClassObject();
23132313

2314-
// TODO(distributed): make this allocation smaller
2314+
fprintf(stderr, "[%s:%d](%s) initialize remote, size: %d\n", __FILE_NAME__, __LINE__, __FUNCTION__,
2315+
metadata->getInstanceSize());
2316+
fprintf(stderr, "[%s:%d](%s) initialize remote, align mask: %d\n", __FILE_NAME__, __LINE__, __FUNCTION__,
2317+
metadata->getInstanceAlignMask());
2318+
23152319
// ==== Allocate the memory for the remote instance
23162320
HeapObject *alloc = swift_allocObject(metadata,
23172321
metadata->getInstanceSize(),

stdlib/public/Distributed/DistributedActor.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,16 @@ public protocol _DistributedActorStub where Self: DistributedActor {}
449449
public func _distributedStubFatalError(function: String = #function) -> Never {
450450
fatalError("Unexpected invocation of distributed method '\(function)' stub!")
451451
}
452+
453+
/// Type that is used to replicate the expected memory layout of a remote
454+
// distributed actor reference object.
455+
@available(SwiftStdlib 6.0, *)
456+
public class _DistributedRemoteReferenceLayout<ActorSystem>
457+
where ActorSystem: DistributedActorSystem {
458+
// Fields must be in this specific order.
459+
let id: ActorSystem.ActorID
460+
let actorSystem: ActorSystem
461+
let unownedExecutor: UnownedSerialExecutor
462+
463+
private init() { fatalError("Not intended to have instances") }
464+
}

test/Concurrency/Runtime/async_task_locals_groups.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ enum TL {
1717
@available(SwiftStdlib 5.1, *)
1818
@discardableResult
1919
func printTaskLocal<V>(
20+
2021
_ key: TaskLocal<V>,
2122
_ expected: V? = nil,
2223
file: String = #file, line: UInt = #line

0 commit comments

Comments
 (0)