Skip to content

Commit b178843

Browse files
committed
[region-isolation] Add support for yield, switch_enum_addr.
1 parent 5e1ca5c commit b178843

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

lib/SILOptimizer/Mandatory/TransferNonSendable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,17 +2355,17 @@ CONSTANT_TRANSLATION(AbortApplyInst, Ignored)
23552355

23562356
// Ignored terminators.
23572357
CONSTANT_TRANSLATION(CondFailInst, Ignored)
2358-
CONSTANT_TRANSLATION(SwitchEnumAddrInst, Ignored)
23592358
// Switch value inst is ignored since we only switch over integers and
23602359
// function_ref/class_method which are considered sendable.
23612360
CONSTANT_TRANSLATION(SwitchValueInst, Ignored)
23622361
CONSTANT_TRANSLATION(UnreachableInst, Ignored)
23632362
CONSTANT_TRANSLATION(UnwindInst, Ignored)
2364-
CONSTANT_TRANSLATION(YieldInst, Ignored)
23652363

23662364
// Terminators that only need require.
23672365
CONSTANT_TRANSLATION(ReturnInst, Require)
23682366
CONSTANT_TRANSLATION(ThrowInst, Require)
2367+
CONSTANT_TRANSLATION(SwitchEnumAddrInst, Require)
2368+
CONSTANT_TRANSLATION(YieldInst, Require)
23692369

23702370
// Unhandled instructions
23712371
CONSTANT_TRANSLATION(AllocVectorInst, Unhandled)

test/Concurrency/sendnonsendable_basic.sil

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ import Swift
77
class NonSendableKlass {}
88

99
sil @transferKlass : $@convention(thin) @async (@guaranteed NonSendableKlass) -> ()
10+
sil @transferIndirect : $@convention(thin) @async <τ_0_0> (@in_guaranteed τ_0_0) -> ()
1011
sil @useKlass : $@convention(thin) (@guaranteed NonSendableKlass) -> ()
1112
sil @constructKlass : $@convention(thin) () -> @owned NonSendableKlass
1213

14+
enum FakeOptional<T> {
15+
case none
16+
case some(T)
17+
}
18+
1319
/////////////////
1420
// MARK: Tests //
1521
/////////////////
@@ -28,3 +34,57 @@ bb0:
2834
%9999 = tuple ()
2935
return %9999 : $()
3036
}
37+
38+
sil [ossa] @yield_error_test : $@yield_once @convention(thin) @async () -> @yields @in_guaranteed NonSendableKlass {
39+
bb0:
40+
%0 = function_ref @constructKlass : $@convention(thin) () -> @owned NonSendableKlass
41+
%1 = apply %0() : $@convention(thin) () -> @owned NonSendableKlass
42+
%2 = alloc_stack $NonSendableKlass
43+
%3 = store_borrow %1 to %2 : $*NonSendableKlass
44+
%4 = function_ref @transferIndirect : $@convention(thin) @async <τ_0_0> (@in_guaranteed τ_0_0) -> ()
45+
apply [caller_isolation=nonisolated] [callee_isolation=global_actor] %4<NonSendableKlass>(%3) : $@convention(thin) @async <τ_0_0> (@in_guaranteed τ_0_0) -> ()
46+
// expected-warning @-1 {{passing argument of non-sendable type 'NonSendableKlass' from nonisolated context to global actor '<null>'-isolated context at this call site could yield a race with accesses later in this function}}
47+
yield %3 : $*NonSendableKlass, resume bb1, unwind bb2
48+
// expected-note @-1 {{access here could race}}
49+
50+
bb1:
51+
end_borrow %3 : $*NonSendableKlass
52+
dealloc_stack %2 : $*NonSendableKlass
53+
destroy_value %1 : $NonSendableKlass
54+
%9999 = tuple ()
55+
return %9999 : $()
56+
57+
bb2:
58+
end_borrow %3 : $*NonSendableKlass
59+
dealloc_stack %2 : $*NonSendableKlass
60+
destroy_value %1 : $NonSendableKlass
61+
unwind
62+
}
63+
64+
sil [ossa] @switch_enum_addr_inst : $@yield_once @convention(thin) @async () -> () {
65+
bb0:
66+
%0 = function_ref @constructKlass : $@convention(thin) () -> @owned NonSendableKlass
67+
%1 = apply %0() : $@convention(thin) () -> @owned NonSendableKlass
68+
%2 = alloc_stack $FakeOptional<NonSendableKlass>
69+
%1a = enum $FakeOptional<NonSendableKlass>, #FakeOptional.some!enumelt, %1 : $NonSendableKlass
70+
store %1a to [init] %2 : $*FakeOptional<NonSendableKlass>
71+
%4 = function_ref @transferIndirect : $@convention(thin) @async <τ_0_0> (@in_guaranteed τ_0_0) -> ()
72+
apply [caller_isolation=nonisolated] [callee_isolation=global_actor] %4<FakeOptional<NonSendableKlass>>(%2) : $@convention(thin) @async <τ_0_0> (@in_guaranteed τ_0_0) -> ()
73+
// expected-warning @-1 {{passing argument of non-sendable type 'FakeOptional<NonSendableKlass>' from nonisolated context to global actor '<null>'-isolated context at this call site could yield a race with accesses later in this function}}
74+
switch_enum_addr %2 : $*FakeOptional<NonSendableKlass>, case #FakeOptional.some!enumelt: bb1, case #FakeOptional.none!enumelt: bb2
75+
// expected-note @-1 {{access here could race}}
76+
77+
bb1:
78+
destroy_addr %2 : $*FakeOptional<NonSendableKlass>
79+
dealloc_stack %2 : $*FakeOptional<NonSendableKlass>
80+
br bb3
81+
82+
bb2:
83+
destroy_addr %2 : $*FakeOptional<NonSendableKlass>
84+
dealloc_stack %2 : $*FakeOptional<NonSendableKlass>
85+
br bb3
86+
87+
bb3:
88+
%9999 = tuple ()
89+
return %9999 : $()
90+
}

0 commit comments

Comments
 (0)