Skip to content

Commit bee76bf

Browse files
committed
[region-isolation] Add support for unchecked_value_cast.
Same as unchecked_trivial_bit_cast.
1 parent fbc8b53 commit bee76bf

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed

lib/SILOptimizer/Mandatory/TransferNonSendable.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ static SILValue getUnderlyingTrackedObjectValue(SILValue value) {
215215

216216
// If we have a cast and our operand and result are non-Sendable, treat it
217217
// as a look through.
218-
if (isa<UncheckedTrivialBitCastInst, UncheckedBitwiseCastInst>(svi)) {
218+
if (isa<UncheckedTrivialBitCastInst, UncheckedBitwiseCastInst,
219+
UncheckedValueCastInst>(svi)) {
219220
if (isNonSendableType(svi->getType(), fn) &&
220221
isNonSendableType(svi->getOperand(0)->getType(), fn)) {
221222
temp = svi->getOperand(0);
@@ -2481,7 +2482,6 @@ CONSTANT_TRANSLATION(DeallocExistentialBoxInst, Ignored)
24812482
// Unhandled Instructions
24822483
//
24832484

2484-
CONSTANT_TRANSLATION(UncheckedValueCastInst, Unhandled)
24852485
CONSTANT_TRANSLATION(RefToUnownedInst, Unhandled)
24862486
CONSTANT_TRANSLATION(UnownedToRefInst, Unhandled)
24872487
CONSTANT_TRANSLATION(BridgeObjectToWordInst, Unhandled)
@@ -2654,6 +2654,7 @@ IGNORE_IF_SENDABLE_RESULT_ASSIGN_OTHERWISE(StructExtractInst)
26542654

26552655
CAST_WITH_MAYBE_SENDABLE_NONSENDABLE_OP_AND_RESULT(UncheckedTrivialBitCastInst)
26562656
CAST_WITH_MAYBE_SENDABLE_NONSENDABLE_OP_AND_RESULT(UncheckedBitwiseCastInst)
2657+
CAST_WITH_MAYBE_SENDABLE_NONSENDABLE_OP_AND_RESULT(UncheckedValueCastInst)
26572658

26582659
#undef CAST_WITH_MAYBE_SENDABLE_NONSENDABLE_OP_AND_RESULT
26592660

test/Concurrency/transfernonsendable_instruction_matching.sil

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,6 @@ bb0:
657657
return %9999 : $()
658658
}
659659

660-
////
661-
662660
sil [ossa] @unchecked_bitwise_cast_test_nonsendable_to_nonsendable : $@convention(thin) @async () -> () {
663661
bb0:
664662
%constructFn = function_ref @constructNonSendableKlass : $@convention(thin) () -> @owned NonSendableKlass
@@ -730,6 +728,90 @@ bb0:
730728

731729
%word = unchecked_bitwise_cast %value : $SendableKlass to $Builtin.Word
732730

731+
destroy_value %value : $SendableKlass
732+
%9999 = tuple ()
733+
return %9999 : $()
734+
}
735+
736+
sil [ossa] @unchecked_value_cast_test_nonsendable_to_nonsendable : $@convention(thin) @async () -> () {
737+
bb0:
738+
%constructFn = function_ref @constructNonSendableKlass : $@convention(thin) () -> @owned NonSendableKlass
739+
%value = apply %constructFn() : $@convention(thin) () -> @owned NonSendableKlass
740+
%valueB = begin_borrow %value : $NonSendableKlass
741+
742+
%rawPointer = unchecked_value_cast %valueB : $NonSendableKlass to $Builtin.RawPointer
743+
744+
%transferRawPointer = function_ref @transferRawPointer : $@convention(thin) @async (Builtin.RawPointer) -> ()
745+
apply [caller_isolation=nonisolated] [callee_isolation=global_actor] %transferRawPointer(%rawPointer) : $@convention(thin) @async (Builtin.RawPointer) -> ()
746+
// expected-warning @-1 {{passing argument of non-sendable type 'Builtin.RawPointer' from nonisolated context to global actor '<null>'-isolated context at this call site could yield a race with accesses later in this function}}
747+
748+
fix_lifetime %value : $NonSendableKlass
749+
// expected-note @-1 {{access here could race}}
750+
751+
end_borrow %valueB : $NonSendableKlass
752+
destroy_value %value : $NonSendableKlass
753+
%9999 = tuple ()
754+
return %9999 : $()
755+
}
756+
757+
sil [ossa] @unchecked_value_cast_test_sendable_to_nonsendable : $@convention(thin) @async () -> () {
758+
bb0:
759+
%constructFn = function_ref @constructSendableKlass : $@convention(thin) () -> @owned SendableKlass
760+
%value = apply %constructFn() : $@convention(thin) () -> @owned SendableKlass
761+
762+
%valueB = begin_borrow %value : $SendableKlass
763+
%rawPointer = unchecked_value_cast %valueB : $SendableKlass to $Builtin.RawPointer
764+
765+
%transferRawPointer = function_ref @transferRawPointer : $@convention(thin) @async (Builtin.RawPointer) -> ()
766+
apply [caller_isolation=nonisolated] [callee_isolation=global_actor] %transferRawPointer(%rawPointer) : $@convention(thin) @async (Builtin.RawPointer) -> ()
767+
// expected-warning @-1 {{passing argument of non-sendable type 'Builtin.RawPointer' from nonisolated context to global actor '<null>'-isolated context at this call site could yield a race with accesses later in this function}}
768+
769+
fix_lifetime %value : $SendableKlass
770+
771+
apply [caller_isolation=nonisolated] [callee_isolation=global_actor] %transferRawPointer(%rawPointer) : $@convention(thin) @async (Builtin.RawPointer) -> ()
772+
// expected-warning @-1 {{passing argument of non-sendable type 'Builtin.RawPointer' from nonisolated context to global actor '<null>'-isolated context at this call site could yield a race with accesses later in this function}}
773+
// expected-note @-2 {{access here could race}}
774+
775+
fix_lifetime %rawPointer : $Builtin.RawPointer
776+
// expected-note @-1 {{access here could race}}
777+
778+
end_borrow %valueB : $SendableKlass
779+
destroy_value %value : $SendableKlass
780+
%9999 = tuple ()
781+
return %9999 : $()
782+
}
783+
784+
sil [ossa] @unchecked_value_cast_test_nonsendable_to_sendable : $@convention(thin) @async () -> () {
785+
bb0:
786+
%constructFn = function_ref @constructNonSendableKlass : $@convention(thin) () -> @owned NonSendableKlass
787+
%value = apply %constructFn() : $@convention(thin) () -> @owned NonSendableKlass
788+
789+
%transferNonSendableKlass = function_ref @transferNonSendableKlass : $@convention(thin) @async (@guaranteed NonSendableKlass) -> ()
790+
apply [caller_isolation=nonisolated] [callee_isolation=global_actor] %transferNonSendableKlass(%value) : $@convention(thin) @async (@guaranteed NonSendableKlass) -> ()
791+
// 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}}
792+
793+
%valueB = begin_borrow %value : $NonSendableKlass
794+
%word = unchecked_value_cast %valueB : $NonSendableKlass to $Builtin.Word
795+
// expected-note @-1 {{access here could race}}
796+
end_borrow %valueB : $NonSendableKlass
797+
798+
destroy_value %value : $NonSendableKlass
799+
%9999 = tuple ()
800+
return %9999 : $()
801+
}
802+
803+
sil [ossa] @unchecked_value_cast_test_sendable_to_sendable : $@convention(thin) @async () -> () {
804+
bb0:
805+
%constructFn = function_ref @constructSendableKlass : $@convention(thin) () -> @owned SendableKlass
806+
%value = apply %constructFn() : $@convention(thin) () -> @owned SendableKlass
807+
808+
%transferSendableKlass = function_ref @transferSendableKlass : $@convention(thin) @async (@guaranteed SendableKlass) -> ()
809+
apply [caller_isolation=nonisolated] [callee_isolation=global_actor] %transferSendableKlass(%value) : $@convention(thin) @async (@guaranteed SendableKlass) -> ()
810+
811+
%valueB = begin_borrow %value : $SendableKlass
812+
%word = unchecked_value_cast %valueB : $SendableKlass to $Builtin.Word
813+
end_borrow %valueB : $SendableKlass
814+
733815
destroy_value %value : $SendableKlass
734816
%9999 = tuple ()
735817
return %9999 : $()

0 commit comments

Comments
 (0)