Skip to content

Commit d0ae20d

Browse files
committed
Fix SILCombine of unchecked_bitwise_cast
This change ensures the insertion point is after any copies/borrows that OSSA rauw may create. Before this fix we could end up creating invalid SIL where operands do not dominate its instruction. Similar to #64533. Found by inspection.
1 parent abe377c commit d0ae20d

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,9 @@ visitUncheckedBitwiseCastInst(UncheckedBitwiseCastInst *UBCI) {
900900

901901
OwnershipRAUWHelper helper(ownershipFixupContext, UBCI, Oper);
902902
if (helper) {
903+
SILBuilderWithScope localBuilder(std::next(UBCI->getIterator()), Builder);
903904
auto replacement = helper.prepareReplacement();
904-
auto *transformedOper = Builder.createUncheckedBitwiseCast(
905+
auto *transformedOper = localBuilder.createUncheckedBitwiseCast(
905906
UBCI->getLoc(), replacement, UBCI->getType());
906907
helper.perform(transformedOper);
907908
return nullptr;

test/SILOptimizer/sil_combine_nocanonicalize_ossa.sil

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
sil_stage canonical
77

8+
import Swift
89
import Builtin
910

1011
class Klass {}
@@ -81,3 +82,25 @@ bb0:
8182
destroy_value %obj : $KlassWithTailAllocatedElems
8283
return %val : $Builtin.Word
8384
}
85+
86+
sil @get_native_object : $@convention(thin) () -> @owned Builtin.NativeObject
87+
88+
// CHECK-LABEL: sil [ossa] @bitwise_combines_guaranteed :
89+
// CHECK: unchecked_ref_cast
90+
// CHECK-LABEL: } // end sil function 'bitwise_combines_guaranteed'
91+
sil [ossa] @bitwise_combines_guaranteed : $@convention(thin) () -> @owned Optional<Builtin.NativeObject> {
92+
bb0:
93+
%f = function_ref @get_native_object : $@convention(thin) () -> @owned Builtin.NativeObject
94+
%0 = apply %f() : $@convention(thin) () -> @owned Builtin.NativeObject
95+
%b = begin_borrow [lexical] %0 : $Builtin.NativeObject
96+
br bb1
97+
98+
bb1:
99+
%2 = unchecked_trivial_bit_cast %b : $Builtin.NativeObject to $Builtin.Word
100+
%3 = unchecked_bitwise_cast %2 : $Builtin.Word to $Optional<Builtin.NativeObject>
101+
%c = copy_value %3 : $Optional<Builtin.NativeObject>
102+
end_borrow %b : $Builtin.NativeObject
103+
destroy_value %0 : $Builtin.NativeObject
104+
return %c : $Optional<Builtin.NativeObject>
105+
}
106+

0 commit comments

Comments
 (0)