Skip to content

Commit d0ec10c

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 swiftlang#64533. Found by inspection.
1 parent abe377c commit d0ec10c

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,25 @@ bb0:
8181
destroy_value %obj : $KlassWithTailAllocatedElems
8282
return %val : $Builtin.Word
8383
}
84+
85+
sil @get_native_object : $@convention(thin) () -> @owned Builtin.NativeObject
86+
87+
// CHECK-LABEL: sil [ossa] @bitwise_combines_guaranteed :
88+
// CHECK: unchecked_ref_cast
89+
// CHECK-LABEL: } // end sil function 'bitwise_combines_guaranteed'
90+
sil [ossa] @bitwise_combines_guaranteed : $@convention(thin) () -> @owned Optional<Builtin.NativeObject> {
91+
bb0:
92+
%f = function_ref @get_native_object : $@convention(thin) () -> @owned Builtin.NativeObject
93+
%0 = apply %f() : $@convention(thin) () -> @owned Builtin.NativeObject
94+
%b = begin_borrow [lexical] %0 : $Builtin.NativeObject
95+
br bb1
96+
97+
bb1:
98+
%2 = unchecked_trivial_bit_cast %b : $Builtin.NativeObject to $Builtin.Word
99+
%3 = unchecked_bitwise_cast %2 : $Builtin.Word to $Optional<Builtin.NativeObject>
100+
%c = copy_value %3 : $Optional<Builtin.NativeObject>
101+
end_borrow %b : $Builtin.NativeObject
102+
destroy_value %0 : $Builtin.NativeObject
103+
return %c : $Optional<Builtin.NativeObject>
104+
}
105+

0 commit comments

Comments
 (0)