Skip to content

Commit a890c60

Browse files
committed
[sending] Do not mangle shared into function signatures if they are paired with sending.
I am doing this b/c we are going to ban borrowing sending so that we can leave open that space for further design. In the short term, we need the ability to create +0 sending parameters without messing with mangling. By special casing this, we get what we want. rdar://129116141
1 parent 2bf497d commit a890c60

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

lib/AST/ASTMangler.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3147,6 +3147,8 @@ static ParameterTypeFlags
31473147
getParameterFlagsForMangling(ParameterTypeFlags flags,
31483148
ParamSpecifier defaultSpecifier,
31493149
bool isInRecursion = true) {
3150+
bool initiallySending = flags.isSending();
3151+
31503152
// If we have been recursed into, then remove sending from our flags.
31513153
if (!isInRecursion) {
31523154
flags = flags.withSending(false);
@@ -3158,12 +3160,10 @@ getParameterFlagsForMangling(ParameterTypeFlags flags,
31583160
case ParamSpecifier::Default:
31593161
// If the legacy `__shared` or `__owned` modifier was provided, mangle as-is,
31603162
// because we need to maintain compatibility with their existing behavior.
3161-
case ParamSpecifier::LegacyShared:
31623163
case ParamSpecifier::LegacyOwned:
31633164
// `inout` should already be specified in the flags.
31643165
case ParamSpecifier::InOut:
31653166
return flags;
3166-
31673167
case ParamSpecifier::ImplicitlyCopyableConsuming:
31683168
case ParamSpecifier::Consuming:
31693169
case ParamSpecifier::Borrowing:
@@ -3172,6 +3172,16 @@ getParameterFlagsForMangling(ParameterTypeFlags flags,
31723172
flags = flags.withOwnershipSpecifier(ParamSpecifier::Default);
31733173
}
31743174
return flags;
3175+
case ParamSpecifier::LegacyShared:
3176+
// If we were originally sending and by default we are borrowing, suppress
3177+
// this and set ownership specifier to default so we do not mangle in
3178+
// __shared.
3179+
//
3180+
// This is a work around in the short term since shared borrow is not
3181+
// supported.
3182+
if (initiallySending && ParamSpecifier::Borrowing == defaultSpecifier)
3183+
return flags.withOwnershipSpecifier(ParamSpecifier::Default);
3184+
return flags;
31753185
}
31763186
}
31773187

test/Concurrency/sending_mangling.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,25 @@ extension SendingProtocol {
8282
// CHECK: sil hidden [ossa] @(extension in sending_mangling):sending_mangling.SendingProtocol.sendingArgWithFunctionSendingResult() -> (sending __owned sending_mangling.NonSendableKlass) -> () : $@convention(method) <Self where Self : SendingProtocol> (@in_guaranteed Self) -> @sil_sending @owned @callee_guaranteed (@sil_sending @owned NonSendableKlass) -> () {
8383
func sendingArgWithFunctionSendingResult() -> sending (sending NonSendableKlass) -> () { fatalError() }
8484
}
85+
86+
// Make sure we only do not mangle in __shared if we are borrowed by default and
87+
// have sending.
88+
//
89+
// CHECK: sil hidden [ossa] @sending_mangling.sendingArgWithShared(sending_mangling.NonSendableKlass) -> () : $@convention(thin) (@sil_sending @guaranteed NonSendableKlass) -> () {
90+
func sendingArgWithShared(_ x: __shared sending NonSendableKlass) {}
91+
92+
// CHECK: sil hidden [ossa] @sending_mangling.argWithShared(__shared sending_mangling.NonSendableKlass) -> () : $@convention(thin) (@guaranteed NonSendableKlass) -> () {
93+
func argWithShared(_ x: __shared NonSendableKlass) {}
94+
95+
struct ConstructorSharedTest {
96+
// Inits take their value at +1, so we need to mangle in shared even if we do
97+
// not mangle in sending itself.
98+
//
99+
// CHECK: sil hidden [ossa] @sending_mangling.ConstructorSharedTest.init(__shared sending_mangling.NonSendableKlass) -> sending_mangling.ConstructorSharedTest : $@convention(method) (@sil_sending @guaranteed NonSendableKlass, @thin ConstructorSharedTest.Type) -> ConstructorSharedTest {
100+
init(_ x: __shared sending NonSendableKlass) {}
101+
102+
// This is a func which takes its parameter at +0 so we should suppress both.
103+
//
104+
// CHECK: sil hidden [ossa] @sending_mangling.ConstructorSharedTest.functionSupressed(sending_mangling.NonSendableKlass) -> () : $@convention(method) (@sil_sending @guaranteed NonSendableKlass, ConstructorSharedTest) -> () {
105+
func functionSupressed(_ x: __shared sending NonSendableKlass) {}
106+
}

test/api-digester/stability-concurrency-abi.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ Func withCheckedContinuation(function:_:) has mangled name changing from '_Concu
9292
// AsyncStream.init(unfolding:onCancel:) uses @_silgen_name to preserve mangling after adding @preconcurrency.
9393
Constructor AsyncStream.init(unfolding:onCancel:) has mangled name changing from 'Swift.AsyncStream.init(unfolding: () async -> Swift.Optional<A>, onCancel: Swift.Optional<@Sendable () -> ()>) -> Swift.AsyncStream<A>' to 'Swift.AsyncStream.init(unfolding: () async -> Swift.Optional<A>, onCancel: Swift.Optional<() -> ()>) -> Swift.AsyncStream<A>'
9494

95+
// We did not change the mangling, we did change from Default to Shared. Both
96+
// took their arg at +0, so there is no actual ABI difference.
97+
Func AsyncStream.Continuation.yield(with:) has parameter 0 changing from Default to Shared
98+
Func AsyncThrowingStream.Continuation.yield(with:) has parameter 0 changing from Default to Shared
99+
95100
// SerialExecutor gained `enqueue(_: __owned Job)`, protocol requirements got default implementations
96101
Func SerialExecutor.enqueue(_:) has been added as a protocol requirement
97102

0 commit comments

Comments
 (0)