Skip to content

[Distributed] make remote proxy allocation minimally small #73099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swift/AST/KnownSDKTypes.def
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ KNOWN_SDK_TYPE_DECL(Distributed, DistributedTargetInvocationEncoder, ProtocolDec
KNOWN_SDK_TYPE_DECL(Distributed, DistributedTargetInvocationDecoder, ProtocolDecl, 0)
KNOWN_SDK_TYPE_DECL(Distributed, RemoteCallTarget, StructDecl, 0)
KNOWN_SDK_TYPE_DECL(Distributed, RemoteCallArgument, StructDecl, 1)
KNOWN_SDK_TYPE_DECL(Distributed, _DistributedRemoteReferenceLayout, ClassDecl, 1)

// String processing
KNOWN_SDK_TYPE_DECL(StringProcessing, Regex, StructDecl, 1)
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Runtime/RuntimeFunctions.def
Original file line number Diff line number Diff line change
Expand Up @@ -2447,7 +2447,7 @@ FUNCTION(NonDefaultDistributedActorInitialize,
// );
FUNCTION(DistributedActorInitializeRemote,
swift_distributedActor_remote_initialize, SwiftCC,
ConcurrencyAvailability, // TODO(distributed): Introduce DistributedAvailability once shipping somewhere
ConcurrencyAvailability,
RETURNS(OpaquePtrTy),
ARGS(TypeMetadataPtrTy),
ATTRS(NoUnwind),
Expand Down
18 changes: 16 additions & 2 deletions lib/SILGen/SILGenDistributed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,16 +459,30 @@ void SILGenFunction::emitDistributedActorFactory(FuncDecl *fd) { // TODO(distrib
// ==== Case 'none') Create the remote instance
{
B.emitBlock(makeProxyBB);

// ==== Create 'remote' distributed actor instance

// --- Call: _distributedActorRemoteInitialize(Self.self)
// --- Prepare metadata for remote "proxy" object initialization
auto remoteReferenceStubDecl = C.get_DistributedRemoteReferenceLayoutDecl();
auto systemTy = getDistributedActorSystemType(selfTyDecl);

auto remoteReferenceStubType = BoundGenericClassType::get(
remoteReferenceStubDecl, nullptr, {systemTy});
auto remoteReferenceStubMetatypeType =
getLoweredType(MetatypeType::get(remoteReferenceStubType, C));

SILValue remoteReferenceStubMetaTypeValue =
B.createMetatype(loc, remoteReferenceStubMetatypeType);

// --- Call: _distributedActorRemoteInitialize(_DistributedRemoteReferenceLayout.self)
auto builtinName = C.getIdentifier(
getBuiltinName(BuiltinValueKind::InitializeDistributedRemoteActor));
auto *remote = B.createBuiltin(
loc, builtinName,
/*returnTy*/returnTy,
/*subs*/ {},
{selfMetatypeValue});
// {selfMetatypeValue});
{remoteReferenceStubMetaTypeValue});

// ==== Initialize distributed actor properties
loc.markAutoGenerated();
Expand Down
2 changes: 2 additions & 0 deletions lib/Sema/CodeSynthesisDistributedActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,8 @@ addDistributedActorCodableConformance(
/******************************************************************************/
/******************************************************************************/

// IMPORTANT: If adding new fields, make sure that the remote stub type has the
// same fields and order: _DistributedRemoteReferenceLayout
void swift::assertRequiredSynthesizedPropertyOrder(ASTContext &Context,
NominalTypeDecl *nominal) {
#ifndef NDEBUG
Expand Down
6 changes: 5 additions & 1 deletion stdlib/public/Concurrency/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2311,7 +2311,11 @@ OpaqueValue*
swift::swift_distributedActor_remote_initialize(const Metadata *actorType) {
const ClassMetadata *metadata = actorType->getClassObject();

// TODO(distributed): make this allocation smaller
fprintf(stderr, "[%s:%d](%s) initialize remote, size: %d\n", __FILE_NAME__, __LINE__, __FUNCTION__,
metadata->getInstanceSize());
fprintf(stderr, "[%s:%d](%s) initialize remote, align mask: %d\n", __FILE_NAME__, __LINE__, __FUNCTION__,
metadata->getInstanceAlignMask());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: remove prints when we cleanup


// ==== Allocate the memory for the remote instance
HeapObject *alloc = swift_allocObject(metadata,
metadata->getInstanceSize(),
Expand Down
13 changes: 13 additions & 0 deletions stdlib/public/Distributed/DistributedActor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,16 @@ public protocol _DistributedActorStub where Self: DistributedActor {}
public func _distributedStubFatalError(function: String = #function) -> Never {
fatalError("Unexpected invocation of distributed method '\(function)' stub!")
}

/// Type that is used to replicate the expected memory layout of a remote
// distributed actor reference object.
@available(SwiftStdlib 6.0, *)
public class _DistributedRemoteReferenceLayout<ActorSystem>
where ActorSystem: DistributedActorSystem {
// Fields must be in this specific order.
let id: ActorSystem.ActorID
let actorSystem: ActorSystem
let unownedExecutor: UnownedSerialExecutor

private init() { fatalError("Not intended to have instances") }
}
1 change: 1 addition & 0 deletions test/Concurrency/Runtime/async_task_locals_groups.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum TL {
@available(SwiftStdlib 5.1, *)
@discardableResult
func printTaskLocal<V>(

_ key: TaskLocal<V>,
_ expected: V? = nil,
file: String = #file, line: UInt = #line
Expand Down