Skip to content

Commit 7d4f8c2

Browse files
committed
[region-isolation] Add support for weak_copy_value.
1 parent ab3204b commit 7d4f8c2

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

lib/SILOptimizer/Analysis/RegionAnalysis.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ static bool isStaticallyLookThroughInst(SILInstruction *inst) {
223223
case SILInstructionKind::UnownedToRefInst:
224224
case SILInstructionKind::UpcastInst:
225225
case SILInstructionKind::ValueToBridgeObjectInst:
226+
case SILInstructionKind::WeakCopyValueInst:
226227
return true;
227228
case SILInstructionKind::UnconditionalCheckedCastInst: {
228229
auto cast = SILDynamicCastInst::getAs(inst);
@@ -2342,6 +2343,7 @@ CONSTANT_TRANSLATION(DropDeinitInst, LookThrough)
23422343
CONSTANT_TRANSLATION(ValueToBridgeObjectInst, LookThrough)
23432344
CONSTANT_TRANSLATION(BeginCOWMutationInst, LookThrough)
23442345
CONSTANT_TRANSLATION(OpenExistentialValueInst, LookThrough)
2346+
CONSTANT_TRANSLATION(WeakCopyValueInst, LookThrough)
23452347

23462348
//===---
23472349
// Store
@@ -2473,7 +2475,6 @@ CONSTANT_TRANSLATION(DeallocExistentialBoxInst, Ignored)
24732475
// Unhandled Instructions
24742476
//
24752477

2476-
CONSTANT_TRANSLATION(WeakCopyValueInst, Unhandled)
24772478
CONSTANT_TRANSLATION(StrongCopyWeakValueInst, Unhandled)
24782479
CONSTANT_TRANSLATION(StrongCopyUnmanagedValueInst, Unhandled)
24792480
CONSTANT_TRANSLATION(InitExistentialValueInst, Unhandled)

test/Concurrency/transfernonsendable_instruction_matching_opaquevalues.sil

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ protocol P {
5656
func doSomething()
5757
}
5858

59+
sil @initP : $@convention(thin) () -> @owned P
60+
sil @transferP : $@async @convention(thin) (@guaranteed P) -> ()
61+
62+
protocol PAnyObject : AnyObject {}
63+
64+
sil @initPAnyObject : $@convention(thin) () -> @owned PAnyObject
65+
sil @initPAnyObjectOptional : $@convention(thin) () -> @owned Optional<PAnyObject>
66+
sil @transferPAnyObject : $@async @convention(thin) (@guaranteed PAnyObject) -> ()
67+
sil @usePAnyObjectWeak : $@convention(thin) (@guaranteed @sil_weak Optional<PAnyObject>) -> ()
68+
5969
/////////////////
6070
// MARK: Tests //
6171
/////////////////
@@ -97,9 +107,6 @@ bb0:
97107
return %9999 : $()
98108
}
99109

100-
sil @initP : $@convention(thin) () -> @owned P
101-
sil @transferP : $@async @convention(thin) (@guaranteed P) -> ()
102-
103110
sil [ossa] @open_existential_value_test : $@async @convention(thin) () -> () {
104111
bb0:
105112
%f = function_ref @initP : $@convention(thin) () -> @owned P
@@ -118,3 +125,25 @@ bb0:
118125
%9999 = tuple ()
119126
return %9999 : $()
120127
}
128+
129+
sil [ossa] @weak_copy_value_test : $@async @convention(thin) () -> () {
130+
bb0:
131+
%f = function_ref @initPAnyObjectOptional : $@convention(thin) () -> @owned Optional<PAnyObject>
132+
%p = apply %f() : $@convention(thin) () -> @owned Optional<PAnyObject>
133+
134+
%pBorrowed = begin_borrow %p : $Optional<PAnyObject>
135+
%pExt = unchecked_enum_data %pBorrowed : $Optional<PAnyObject>, #Optional.some!enumelt
136+
%transferP = function_ref @transferPAnyObject : $@async @convention(thin) (@guaranteed PAnyObject) -> ()
137+
apply [caller_isolation=nonisolated] [callee_isolation=global_actor] %transferP(%pExt) : $@async @convention(thin) (@guaranteed PAnyObject) -> () // expected-warning {{transferring value of non-Sendable type 'any PAnyObject' from nonisolated context to global actor '<null>'-isolated context}}
138+
139+
%w = weak_copy_value %pBorrowed : $Optional<PAnyObject>
140+
%weakFunc = function_ref @usePAnyObjectWeak : $@convention(thin) (@guaranteed @sil_weak Optional<PAnyObject>) -> ()
141+
apply %weakFunc(%w) : $@convention(thin) (@guaranteed @sil_weak Optional<PAnyObject>) -> () // expected-note {{access here could race}}
142+
destroy_value %w : $@sil_weak Optional<PAnyObject>
143+
144+
end_borrow %pBorrowed : $Optional<PAnyObject>
145+
destroy_value %p : $Optional<PAnyObject>
146+
147+
%9999 = tuple ()
148+
return %9999 : $()
149+
}

0 commit comments

Comments
 (0)