Skip to content

Commit 0d80c8b

Browse files
committed
[Distributed] Undo new record and mangling scheme for dist.p.witnesses
This change undoes an approach we thought viable to encode distributed actor remote targets, as encoding their protocol method names. This also needed extra metadata sections and tricky "find if this is a witness or not" methods; Instead, we will be mangling the concrete names; and special handle proxy types that are prefixed with `$`. This way we will not need extra metadata records, and can handle generic calls more properly. This XFAILs one test which was testing new functionality, that is pending now until we implement the new mangling scheme.
1 parent cccaf01 commit 0d80c8b

25 files changed

+95
-576
lines changed

include/swift/RemoteInspection/RuntimeHeaders/llvm/BinaryFormat/Swift.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,4 @@ HANDLE_SWIFT_SECTION(protocs, "__swift5_protos", "swift5_protocols",
3030
".sw5prt$B")
3131
HANDLE_SWIFT_SECTION(acfuncs, "__swift5_acfuncs", "swift5_accessible_functions",
3232
".sw5acfn$B")
33-
HANDLE_SWIFT_SECTION(dacfuncs, "__swift5_acpfuns", "swift5_accessible_protocol_requirement_functions",
34-
".sw5acpfn$B")
3533
HANDLE_SWIFT_SECTION(mpenum, "__swift5_mpenum", "swift5_mpenum", ".sw5mpen$B")

include/swift/Runtime/Config.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,6 @@ extern uintptr_t __COMPATIBILITY_LIBRARIES_CANNOT_CHECK_THE_IS_SWIFT_BIT_DIRECTL
307307
#define __ptrauth_swift_accessible_function_record \
308308
__ptrauth(ptrauth_key_process_independent_data, 1, \
309309
SpecialPointerAuthDiscriminators::AccessibleFunctionRecord)
310-
#define __ptrauth_swift_accessible_protocol_requirement_function_record \
311-
__ptrauth(ptrauth_key_process_independent_data, 1, \
312-
SpecialPointerAuthDiscriminators:: \
313-
AccessibleProtocolRequirementFunctionRecord)
314310
#define __ptrauth_swift_objc_superclass \
315311
__ptrauth(ptrauth_key_process_independent_data, 1, \
316312
swift::SpecialPointerAuthDiscriminators::ObjCSuperclass)
@@ -359,7 +355,6 @@ extern uintptr_t __COMPATIBILITY_LIBRARIES_CANNOT_CHECK_THE_IS_SWIFT_BIT_DIRECTL
359355
#define __ptrauth_swift_escalation_notification_function
360356
#define __ptrauth_swift_dispatch_invoke_function
361357
#define __ptrauth_swift_accessible_function_record
362-
#define __ptrauth_swift_accessible_protocol_requirement_function_record
363358
#define __ptrauth_swift_objc_superclass
364359
#define __ptrauth_swift_runtime_function_entry
365360
#define __ptrauth_swift_runtime_function_entry_with_key(__key)

lib/AST/DistributedDecl.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ bool swift::checkDistributedSerializationRequirementIsExactlyCodable(
390390
std::count(protocols.begin(), protocols.end(), decodable) == 1;
391391
}
392392

393+
// TODO(distributed): probably can be removed?
393394
llvm::ArrayRef<ValueDecl *>
394395
AbstractFunctionDecl::getDistributedMethodWitnessedProtocolRequirements() const {
395396
auto mutableThis = const_cast<AbstractFunctionDecl *>(this);

lib/IRGen/GenDecl.cpp

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4417,14 +4417,6 @@ void IRGenModule::addAccessibleFunction(SILFunction *func) {
44174417
AccessibleFunctions.push_back(func);
44184418
}
44194419

4420-
void IRGenModule::addAccessibleFunctionDistributedAliased(
4421-
std::string mangledRecordName,
4422-
std::optional<std::string> mangledActorTypeName,
4423-
SILFunction *func) {
4424-
AccessibleProtocolFunctions.push_back(AccessibleProtocolFunctionsData(
4425-
func, mangledRecordName, mangledActorTypeName));
4426-
}
4427-
44284420
/// Emit the protocol conformance list and return it (if asContiguousArray is
44294421
/// true, otherwise the records are emitted as individual globals and
44304422
/// nullptr is returned).
@@ -4748,11 +4740,10 @@ void IRGenModule::emitAccessibleFunction(
47484740
}
47494741

47504742
void IRGenModule::emitAccessibleFunctions() {
4751-
if (AccessibleFunctions.empty() && AccessibleProtocolFunctions.empty())
4743+
if (AccessibleFunctions.empty())
47524744
return;
47534745

47544746
StringRef fnsSectionName;
4755-
StringRef protocolFnsSectionName;
47564747
switch (TargetInfo.OutputObjectFormat) {
47574748
case llvm::Triple::DXContainer:
47584749
case llvm::Triple::GOFF:
@@ -4762,17 +4753,14 @@ void IRGenModule::emitAccessibleFunctions() {
47624753
"the selected object format.");
47634754
case llvm::Triple::MachO:
47644755
fnsSectionName = "__TEXT, __swift5_acfuncs, regular";
4765-
protocolFnsSectionName = "__TEXT, __swift5_acpfuns, regular";
47664756
break;
47674757
case llvm::Triple::ELF:
47684758
case llvm::Triple::Wasm:
47694759
fnsSectionName = "swift5_accessible_functions";
4770-
protocolFnsSectionName = "swift5_accessible_protocol_requirement_functions";
47714760
break;
47724761
case llvm::Triple::XCOFF:
47734762
case llvm::Triple::COFF:
47744763
fnsSectionName = ".sw5acfn$B";
4775-
protocolFnsSectionName = ".sw5acpfn$B";
47764764
break;
47774765
}
47784766

@@ -4786,24 +4774,6 @@ void IRGenModule::emitAccessibleFunctions() {
47864774
fnsSectionName, mangledRecordName,
47874775
/*mangledActorName=*/{}, mangledFunctionName, func);
47884776
}
4789-
4790-
for (auto accessibleInfo : AccessibleProtocolFunctions) {
4791-
auto func = accessibleInfo.function;
4792-
4793-
std::string mangledRecordName =
4794-
accessibleInfo.mangledRecordName
4795-
? (*accessibleInfo.mangledRecordName)
4796-
: LinkEntity::forAccessibleFunctionRecord(func).mangleAsString();
4797-
std::string mangledFunctionName =
4798-
LinkEntity::forSILFunction(func).mangleAsString();
4799-
std::string mangledActorName = accessibleInfo.concreteMangledTypeName
4800-
? *accessibleInfo.concreteMangledTypeName
4801-
: "<none>";
4802-
4803-
emitAccessibleFunction(
4804-
protocolFnsSectionName, mangledRecordName,
4805-
mangledActorName, mangledFunctionName, func);
4806-
}
48074777
}
48084778

48094779
/// Fetch a global reference to a reference to the given Objective-C class.

lib/IRGen/IRGenModule.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,8 +2232,3 @@ bool swift::writeEmptyOutputFilesFor(
22322232
}
22332233
return false;
22342234
}
2235-
IRGenModule::AccessibleProtocolFunctionsData::AccessibleProtocolFunctionsData(
2236-
SILFunction *function, const std::optional<std::string> &mangledRecordName,
2237-
const std::optional<std::string> &concreteMangledTypeName)
2238-
: function(function), mangledRecordName(mangledRecordName),
2239-
concreteMangledTypeName(concreteMangledTypeName) {}

lib/IRGen/IRGenModule.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,10 +1152,6 @@ class IRGenModule {
11521152
void addObjCClassStub(llvm::Constant *addr);
11531153
void addProtocolConformance(ConformanceDescription &&conformance);
11541154
void addAccessibleFunction(SILFunction *func);
1155-
void addAccessibleFunctionDistributedAliased(
1156-
std::string mangledRecordName,
1157-
std::optional<std::string> mangledActorTypeName,
1158-
SILFunction *func);
11591155

11601156
llvm::Constant *emitSwiftProtocols(bool asContiguousArray);
11611157
llvm::Constant *emitProtocolConformances(bool asContiguousArray);
@@ -1314,25 +1310,6 @@ class IRGenModule {
13141310
/// up at runtime.
13151311
SmallVector<SILFunction *, 4> AccessibleFunctions;
13161312

1317-
struct AccessibleProtocolFunctionsData {
1318-
SILFunction *function;
1319-
/// Mangled name of the requirement function.
1320-
std::optional<std::string> mangledRecordName;
1321-
std::optional<std::string> concreteMangledTypeName;
1322-
AccessibleProtocolFunctionsData(
1323-
SILFunction *function,
1324-
const std::optional<std::string> &mangledRecordName,
1325-
const std::optional<std::string> &concreteMangledTypeName);
1326-
};
1327-
/// List of all functions which are protocol *requirements* which may be
1328-
/// looked up by name at runtime. The record can be used to pair
1329-
/// a concrete implementation type with the protocol (record) name,
1330-
/// in order to locate the *witness* function name on this specific type.
1331-
///
1332-
/// The witness function name can then be used to look up the witness at
1333-
/// runtime by inspecting `AccessibleFunctions`.
1334-
SmallVector<AccessibleProtocolFunctionsData, 4> AccessibleProtocolFunctions;
1335-
13361313
/// Map of Objective-C protocols and protocol references, bitcast to i8*.
13371314
/// The interesting global variables relating to an ObjC protocol.
13381315
struct ObjCProtocolPair {

lib/IRGen/IRGenSIL.cpp

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2476,36 +2476,7 @@ void IRGenSILFunction::emitSILFunction() {
24762476
IGM.emitDistributedTargetAccessor(CurSILFn);
24772477
IGM.addAccessibleFunction(CurSILFn);
24782478

2479-
if (auto val = CurSILFn->getLocation().castToASTNode<ValueDecl>()) {
2480-
if (auto attr =
2481-
val->getAttrs().getAttribute<DistributedThunkTargetAttr>()) {
2482-
2483-
// the original `distributed func`
2484-
auto func = attr->getTargetFunction();
2485-
2486-
auto distributedRequirements = func->getDistributedMethodWitnessedProtocolRequirements();
2487-
if (distributedRequirements.size() == 1) {
2488-
auto protocolFunc = distributedRequirements.front();
2489-
Mangle::ASTMangler mangler;
2490-
// The mangled name of the requirement is the name of the record
2491-
auto mangledProtocolFuncName =
2492-
mangler.mangleDistributedThunk(cast<FuncDecl>(protocolFunc));
2493-
2494-
std::optional<std::string> mangledActorTypeName;
2495-
if (isa<ClassDecl>(func->getDeclContext()->getAsDecl())) {
2496-
// a concrete type, not a "distributed" protocol
2497-
mangledActorTypeName = mangler.mangleAnyDecl(
2498-
func->getDeclContext()->getSelfNominalTypeDecl(),
2499-
/*prefix=*/true);
2500-
}
2501-
2502-
IGM.addAccessibleFunctionDistributedAliased(
2503-
/*mangledRecordName=*/mangledProtocolFuncName,
2504-
/*mangledActorTypeName=*/mangledActorTypeName,
2505-
CurSILFn);
2506-
}
2507-
}
2508-
}
2479+
// TODO(distributed): for protocols emit a special accessor
25092480
}
25102481

25112482
// Configure the dominance resolver.

lib/Sema/CodeSynthesisDistributedActor.cpp

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,17 @@ static void forwardParameters(AbstractFunctionDecl *afd,
195195
}
196196
}
197197

198+
static llvm::StringRef
199+
mangleDistributedThunkForAccessorRecordName(
200+
ASTContext &C, AbstractFunctionDecl *thunk) {
201+
Mangle::ASTMangler mangler;
202+
203+
// default mangling
204+
auto mangled =
205+
C.AllocateCopy(mangler.mangleDistributedThunk(cast<FuncDecl>(thunk)));
206+
return mangled;
207+
}
208+
198209
static std::pair<BraceStmt *, bool>
199210
deriveBodyDistributed_thunk(AbstractFunctionDecl *thunk, void *context) {
200211
auto implicit = true;
@@ -519,34 +530,12 @@ deriveBodyDistributed_thunk(AbstractFunctionDecl *thunk, void *context) {
519530

520531
{
521532
// --- Mangle the thunk name
522-
Mangle::ASTMangler mangler;
523-
524-
// FIXME: cleanup
525-
StringLiteralExpr *mangledTargetStringLiteral = nullptr;
526-
auto witnessedDistributedRequirements =
527-
func->getDistributedMethodWitnessedProtocolRequirements();
528-
529-
if (witnessedDistributedRequirements.size() == 1) {
530-
auto protocolFunc = witnessedDistributedRequirements.front();
531-
532-
// we expect to witness exactly one distributed requirement,
533-
// otherwise we should have diagnosed errors about more than 1 already.
534-
std::string mangledString =
535-
mangler.mangleDistributedThunk(cast<FuncDecl>(protocolFunc));
536-
// FIXME: make it THUNK so the mangling is right
537-
// MUST BE LIKE: s4main28GreeterP_ConcreteSystem_StubC5greetSSyYaKFTE
538-
539-
StringRef mangled = C.AllocateCopy(mangledString);
540-
mangledTargetStringLiteral =
541-
new (C) StringLiteralExpr(mangled, SourceRange(), implicit);
542-
} else {
543-
// default mangling
544-
auto mangled =
545-
C.AllocateCopy(mangler.mangleDistributedThunk(cast<FuncDecl>(thunk)));
546-
mangledTargetStringLiteral =
547-
new (C) StringLiteralExpr(mangled, SourceRange(), implicit);
548-
}
549-
assert(mangledTargetStringLiteral && "must be initialized");
533+
auto mangledAccessorRecordName =
534+
mangleDistributedThunkForAccessorRecordName(C, thunk);
535+
536+
StringLiteralExpr *mangledTargetStringLiteral =
537+
new (C) StringLiteralExpr(mangledAccessorRecordName,
538+
SourceRange(), implicit);
550539

551540
// --- let target = RemoteCallTarget(<mangled name>)
552541
targetVar->setInterfaceType(remoteCallTargetTy);

stdlib/public/Distributed/DistributedActor.cpp

Lines changed: 1 addition & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -32,94 +32,6 @@ findDistributedAccessor(const char *targetNameStart, size_t targetNameLength) {
3232
return nullptr;
3333
}
3434

35-
/// Find an accessible function record given a concrete actor type we're
36-
/// attempting to make the call on, and the remote call method identifier.
37-
static const AccessibleFunctionRecord *findDistributedProtocolMethodAccessor(
38-
bool findConcreteWitness, const char *targetActorTypeNameStart,
39-
size_t targetActorTypeNameLength, const char *targetNameStart,
40-
size_t targetNameLength) {
41-
// Find using just the method identifier;
42-
// This will work if the call is a concrete method identifier
43-
if (auto *func = runtime::swift_findAccessibleFunctionForConcreteType(
44-
findConcreteWitness, targetActorTypeNameStart,
45-
targetActorTypeNameLength, targetNameStart, targetNameLength)) {
46-
assert(func->Flags.isDistributed());
47-
return func;
48-
}
49-
50-
return nullptr;
51-
}
52-
53-
/// Given the presence of protocol witness distributed invocation targets,
54-
/// obtain a concrete target name.
55-
///
56-
/// A distributed target can be identified by a protocol method name:
57-
/// ```
58-
/// protocol WorkerProtocol {
59-
/// associatedtype Ret
60-
/// distributed func func test() -> Ret
61-
/// }
62-
/// ```
63-
///
64-
/// So the remote call identifier may be "WorkerProtocol.test", however in order
65-
/// to perform the invocation on a concrete target actor, we need to obtain
66-
/// the concrete function we are about to invoke -- not least because of
67-
/// its generic context details.
68-
///
69-
/// A concrete type on the server may be:
70-
///
71-
/// ```
72-
/// distributed actor WorkerImpl: WorkerProtocol {
73-
/// distributed func func test() -> String { "Hello" }
74-
/// }
75-
/// ```
76-
///
77-
/// Thus this method allows mapping the "protocol method identifier" and
78-
/// concrete actor name, into the target witness name.
79-
///
80-
/// This way the generic context and other concrete information may be obtained
81-
/// in order to perform the call on the concrete `WorkerImpl` type.
82-
SWIFT_CC(swift)
83-
SWIFT_EXPORT_FROM(swiftDistributed)
84-
TypeNamePair swift_distributed_getConcreteAccessibleWitnessName(
85-
DefaultActor *actor, const char *targetNameStart, size_t targetNameLength) {
86-
87-
// TODO(distributed): Avoid mangling twice; we do it here and in `execute_`
88-
auto actorTy = swift_getObjectType(actor);
89-
auto actorTyNamePair = swift_getMangledTypeName(actorTy);
90-
std::string actorTyName = actorTyNamePair.data;
91-
92-
auto accessor = findDistributedProtocolMethodAccessor(
93-
/*findConcreteWitness=*/true, actorTyNamePair.data,
94-
actorTyNamePair.length, targetNameStart, targetNameLength);
95-
96-
if (!accessor) {
97-
return {"", 0};
98-
}
99-
100-
auto concreteNameData = accessor->Name.get();
101-
auto concreteNameLength = strlen(accessor->Name.get());
102-
return {concreteNameData, concreteNameLength};
103-
}
104-
105-
SWIFT_CC(swift)
106-
SWIFT_EXPORT_FROM(swiftDistributed)
107-
void *swift_distributed_getGenericEnvironmentForConcreteActor(
108-
DefaultActor *actor, const char *targetNameStart, size_t targetNameLength) {
109-
110-
// TODO(distributed): Avoid mangling twice; we do it here and in `execute_`
111-
auto actorTy = swift_getObjectType(actor);
112-
auto actorTyNamePair = swift_getMangledTypeName(actorTy);
113-
114-
auto *accessor = findDistributedProtocolMethodAccessor(
115-
/*findConcreteWitness=*/true, actorTyNamePair.data,
116-
actorTyNamePair.length, targetNameStart, targetNameLength);
117-
if (!accessor) {
118-
return nullptr;
119-
}
120-
121-
return accessor->GenericEnvironment.get();
122-
}
12335

12436
SWIFT_CC(swift)
12537
SWIFT_EXPORT_FROM(swiftDistributed)
@@ -141,9 +53,7 @@ void *swift_distributed_getGenericEnvironment(const char *targetNameStart,
14153
/// numWitnessTables: UInt
14254
/// ) async throws
14355
using TargetExecutorSignature =
144-
AsyncSignature<void(/*on=*/DefaultActor *, // FIXME(distributed): Make this
145-
// accept AnyObject and not
146-
// witness tables
56+
AsyncSignature<void(/*on=*/DefaultActor *,
14757
/*targetName=*/const char *, /*targetNameSize=*/size_t,
14858
/*argumentDecoder=*/HeapObject *,
14959
/*argumentTypes=*/const Metadata *const *,

0 commit comments

Comments
 (0)