Skip to content

Commit 14fb9a0

Browse files
authored
Merge pull request #41871 from xedin/fix-mark-protocol-issues-related-to-distributd
[Distributed] Strip marker protocols from distributed thunks and accessible functions
2 parents 0dd1311 + ab03005 commit 14fb9a0

11 files changed

+96
-13
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ class ASTMangler : public Mangler {
186186
Type GlobalActorBound,
187187
ModuleDecl *Module);
188188

189+
std::string mangleDistributedThunk(const FuncDecl *thunk);
190+
189191
/// Mangle a completion handler block implementation function, used for importing ObjC
190192
/// APIs as async.
191193
///

include/swift/AST/GenericSignature.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ class GenericSignature {
122122
ArrayRef<Requirement> requirements,
123123
bool isKnownCanonical = false);
124124

125+
/// Produce a new generic signature which drops all of the marker
126+
/// protocol conformance requirements associated with this one.
127+
GenericSignature withoutMarkerProtocols() const;
128+
125129
public:
126130
static ASTContext &getASTContext(TypeArrayView<GenericTypeParamType> params,
127131
ArrayRef<Requirement> requirements);

lib/AST/ASTMangler.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3457,3 +3457,11 @@ ASTMangler::mangleOpaqueTypeDescriptorRecord(const OpaqueTypeDecl *decl) {
34573457
appendOperator("Ho");
34583458
return finalize();
34593459
}
3460+
3461+
std::string ASTMangler::mangleDistributedThunk(const FuncDecl *thunk) {
3462+
// Marker protocols cannot be checked at runtime, so there is no point
3463+
// in recording them for distributed thunks.
3464+
llvm::SaveAndRestore<bool> savedAllowMarkerProtocols(AllowMarkerProtocols,
3465+
false);
3466+
return mangleEntity(thunk, SymbolKind::DistributedThunk);
3467+
}

lib/AST/GenericSignature.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,3 +1163,24 @@ swift::buildGenericSignature(ASTContext &ctx,
11631163
addedRequirements},
11641164
GenericSignatureWithError()).getPointer();
11651165
}
1166+
1167+
GenericSignature GenericSignature::withoutMarkerProtocols() const {
1168+
auto requirements = getRequirements();
1169+
SmallVector<Requirement, 4> reducedRequirements;
1170+
1171+
// Drop all conformance requirements to marker protocols (if any).
1172+
llvm::copy_if(requirements, std::back_inserter(reducedRequirements),
1173+
[](const Requirement &requirement) {
1174+
if (requirement.getKind() == RequirementKind::Conformance) {
1175+
auto *protocol = requirement.getProtocolDecl();
1176+
return !protocol->isMarkerProtocol();
1177+
}
1178+
return true;
1179+
});
1180+
1181+
// If nothing changed, let's return this signature back.
1182+
if (requirements.size() == reducedRequirements.size())
1183+
return *this;
1184+
1185+
return GenericSignature::get(getGenericParams(), reducedRequirements);
1186+
}

lib/IRGen/GenDecl.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4172,7 +4172,10 @@ void IRGenModule::emitAccessibleFunctions() {
41724172

41734173
GenericSignature signature;
41744174
if (auto *env = func->getGenericEnvironment()) {
4175-
signature = env->getGenericSignature();
4175+
// Drop all of the marker protocols because they are effect-less
4176+
// at runtime.
4177+
signature = env->getGenericSignature().withoutMarkerProtocols();
4178+
41764179
genericEnvironment =
41774180
getAddrOfGenericEnvironment(signature.getCanonicalSignature());
41784181
}

lib/SIL/IR/SILDeclRef.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,10 @@ std::string SILDeclRef::mangle(ManglingKind MKind) const {
914914
return CDeclA->Name.str();
915915
}
916916

917+
if (SKind == ASTMangler::SymbolKind::DistributedThunk) {
918+
return mangler.mangleDistributedThunk(cast<FuncDecl>(getDecl()));
919+
}
920+
917921
// Otherwise, fall through into the 'other decl' case.
918922
LLVM_FALLTHROUGH;
919923

lib/Sema/CodeSynthesisDistributedActor.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -429,11 +429,10 @@ deriveBodyDistributed_thunk(AbstractFunctionDecl *thunk, void *context) {
429429
{
430430
// --- Mangle the thunk name
431431
Mangle::ASTMangler mangler;
432-
auto symbolKind = swift::Mangle::ASTMangler::SymbolKind::DistributedThunk;
433-
auto mangled = C.AllocateCopy(mangler.mangleEntity(thunk, symbolKind));
434-
StringRef mangledTargetStringRef = StringRef(mangled);
435-
auto mangledTargetStringLiteral = new (C)
436-
StringLiteralExpr(mangledTargetStringRef, SourceRange(), implicit);
432+
auto mangled =
433+
C.AllocateCopy(mangler.mangleDistributedThunk(cast<FuncDecl>(thunk)));
434+
auto mangledTargetStringLiteral =
435+
new (C) StringLiteralExpr(mangled, SourceRange(), implicit);
437436

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

test/Distributed/Runtime/distributed_actor_remoteCallTarget_demanglingTargetNames.swift

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import FakeDistributedActorSystems
1919

2020
typealias DefaultDistributedActorSystem = FakeRoundtripActorSystem
2121

22+
protocol SomeProtocol {}
23+
extension String: SomeProtocol {}
24+
2225
distributed actor Greeter {
2326
distributed func noParams() {}
2427
distributed func noParamsThrows() throws {}
@@ -28,9 +31,9 @@ distributed actor Greeter {
2831
distributed func oneLabel(value: String, _ value2: String, _ value3: String) {}
2932
distributed func parameterSingle(first: String) {}
3033
distributed func parameterPair(first: String, second: Int) {}
31-
// FIXME(distributed): rdar://90293494 fails to get
32-
// distributed func generic<A: Codable & Sendable>(first: A) {}
33-
// distributed func genericNoLabel<A: Codable & Sendable>(_ first: A) {}
34+
distributed func generic<A: Codable & Sendable>(first: A) {}
35+
distributed func genericThree<A: Codable & Sendable & SomeProtocol>(first: A) {}
36+
distributed func genericThreeTwo<A: Codable & Sendable, B: Codable & SomeProtocol>(first: A, second: B) {}
3437
}
3538
extension Greeter {
3639
distributed func parameterTriple(first: String, second: Int, third: Double) {}
@@ -65,16 +68,25 @@ func test() async throws {
6568
_ = try await greeter.parameterTriple(first: "X", second: 2, third: 3.0)
6669
// CHECK: >> remoteCallVoid: on:main.Greeter, target:main.Greeter.parameterTriple(first:second:third:)
6770

68-
// FIXME: rdar://90293494 seems to fail getting the substitutions?
69-
// _ = try await greeter.generic(first: "X")
70-
// // TODO: >> remoteCallVoid: on:main.Greeter, target:main.Greeter.parameterTriple(first:second:third:)
71+
_ = try await greeter.generic(first: "X")
72+
// CHECK: >> remoteCallVoid: on:main.Greeter, target:main.Greeter.generic(first:)
73+
74+
_ = try await greeter.genericThree(first: "X")
75+
// CHECK: >> remoteCallVoid: on:main.Greeter, target:main.Greeter.genericThree(first:)
76+
77+
_ = try await greeter.genericThreeTwo(first: "X", second: "SecondValue")
78+
// CHECK: >> remoteCallVoid: on:main.Greeter, target:main.Greeter.genericThreeTwo(first:second:)
7179

7280
print("done")
7381
// CHECK: done
7482
}
7583

7684
@main struct Main {
7785
static func main() async {
78-
try! await test()
86+
do {
87+
try await test()
88+
} catch {
89+
print("ERROR: \(error)")
90+
}
7991
}
8092
}

test/Distributed/distributed_actor_accessor_section_coff.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public distributed actor MyActor {
7676
distributed func complex(_: [Int], _: Obj, _: String?, _: LargeStruct) -> LargeStruct {
7777
fatalError()
7878
}
79+
80+
// Make sure that Sendable doesn't show up in the mangled name
81+
distributed func generic<T: Codable & Sendable>(_: T) {
82+
}
7983
}
8084

8185
@available(SwiftStdlib 5.7, *)
@@ -123,6 +127,12 @@ public distributed actor MyOtherActor {
123127
// CHECK-SAME: (%swift.async_func_pointer* @"$s27distributed_actor_accessors7MyActorC7complexyAA11LargeStructVSaySiG_AA3ObjCSSSgAFtYaKFTETFTu" to i{{32|64}})
124128
// CHECK-SAME: , section ".sw5acfn$B", {{.*}}
125129

130+
/// -> `MyActor.generic`
131+
// CHECK: @"$s27distributed_actor_accessors7MyActorC7genericyyxYaKSeRzSERzlFTEHF" = private constant
132+
// CHECK-SAME: @"symbolic x___________pSeRzSERzlIetMHngzo_ 27distributed_actor_accessors7MyActorC s5ErrorP"
133+
// CHECK-SAME: (%swift.async_func_pointer* @"$s27distributed_actor_accessors7MyActorC7genericyyxYaKSeRzSERzlFTETFTu" to i{{32|64}})
134+
// CHECK-SAME: , section ".sw5acfn$B", {{.*}}
135+
126136
/// -> `MyOtherActor.empty`
127137
// CHECK: @"$s27distributed_actor_accessors12MyOtherActorC5emptyyyYaKFTEHF" = private constant
128138
// CHECK-SAME: @"symbolic ___________pIetMHgzo_ 27distributed_actor_accessors12MyOtherActorC s5ErrorP"

test/Distributed/distributed_actor_accessor_section_elf.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public distributed actor MyActor {
7474
distributed func complex(_: [Int], _: Obj, _: String?, _: LargeStruct) -> LargeStruct {
7575
fatalError()
7676
}
77+
78+
// Make sure that Sendable doesn't show up in the mangled name
79+
distributed func generic<T: Codable & Sendable>(_: T) {
80+
}
7781
}
7882

7983
@available(SwiftStdlib 5.7, *)
@@ -121,6 +125,12 @@ public distributed actor MyOtherActor {
121125
// CHECK-SAME: (%swift.async_func_pointer* @"$s27distributed_actor_accessors7MyActorC7complexyAA11LargeStructVSaySiG_AA3ObjCSSSgAFtYaKFTETFTu" to i{{32|64}})
122126
// CHECK-SAME: , section "swift5_accessible_functions", {{.*}}
123127

128+
/// -> `MyActor.generic`
129+
// CHECK: @"$s27distributed_actor_accessors7MyActorC7genericyyxYaKSeRzSERzlFTEHF" = private constant
130+
// CHECK-SAME: @"symbolic x___________pSeRzSERzlIetMHngzo_ 27distributed_actor_accessors7MyActorC s5ErrorP"
131+
// CHECK-SAME: (%swift.async_func_pointer* @"$s27distributed_actor_accessors7MyActorC7genericyyxYaKSeRzSERzlFTETFTu" to i{{32|64}})
132+
// CHECK-SAME: , section "swift5_accessible_functions", {{.*}}
133+
124134
/// -> `MyOtherActor.empty`
125135
// CHECK: @"$s27distributed_actor_accessors12MyOtherActorC5emptyyyYaKFTEHF" = private constant
126136
// CHECK-SAME: @"symbolic ___________pIetMHgzo_ 27distributed_actor_accessors12MyOtherActorC s5ErrorP"

test/Distributed/distributed_actor_accessor_section_macho.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public distributed actor MyActor {
7474
distributed func complex(_: [Int], _: Obj, _: String?, _: LargeStruct) -> LargeStruct {
7575
fatalError()
7676
}
77+
78+
// Make sure that Sendable doesn't show up in the mangled name
79+
distributed func generic<T: Codable & Sendable>(_: T) {
80+
}
7781
}
7882

7983
@available(SwiftStdlib 5.7, *)
@@ -121,6 +125,12 @@ public distributed actor MyOtherActor {
121125
// CHECK-SAME: (%swift.async_func_pointer* @"$s27distributed_actor_accessors7MyActorC7complexyAA11LargeStructVSaySiG_AA3ObjCSSSgAFtYaKFTETFTu" to i{{32|64}})
122126
// CHECK-SAME: , section {{"swift5_accessible_functions"|".sw5acfn$B"|"__TEXT, __swift5_acfuncs, regular"}}
123127

128+
/// -> `MyActor.generic`
129+
// CHECK: @"$s27distributed_actor_accessors7MyActorC7genericyyxYaKSeRzSERzlFTEHF" = private constant
130+
// CHECK-SAME: @"symbolic x___________pSeRzSERzlIetMHngzo_ 27distributed_actor_accessors7MyActorC s5ErrorP"
131+
// CHECK-SAME: (%swift.async_func_pointer* @"$s27distributed_actor_accessors7MyActorC7genericyyxYaKSeRzSERzlFTETFTu" to i{{32|64}})
132+
// CHECK-SAME: , section {{"swift5_accessible_functions"|".sw5acfn$B"|"__TEXT, __swift5_acfuncs, regular"}}
133+
124134
/// -> `MyOtherActor.empty`
125135
// CHECK: @"$s27distributed_actor_accessors12MyOtherActorC5emptyyyYaKFTEHF" = private constant
126136
// CHECK-SAME: @"symbolic ___________pIetMHgzo_ 27distributed_actor_accessors12MyOtherActorC s5ErrorP"

0 commit comments

Comments
 (0)