Skip to content

Commit c028fcc

Browse files
committed
Fix SILCombine of pointer_to_address instruction
This change ensures the insertion point of the unchecked_addr_cast 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.
1 parent 6b0c22c commit c028fcc

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,10 @@ visitPointerToAddressInst(PointerToAddressInst *PTAI) {
337337
OwnershipRAUWHelper helper(ownershipFixupContext, PTAI,
338338
ATPI->getOperand());
339339
if (helper) {
340+
SILBuilderWithScope localBuilder(std::next(PTAI->getIterator()), Builder);
340341
auto replacement = helper.prepareReplacement();
341-
auto *newInst = Builder.createUncheckedAddrCast(PTAI->getLoc(),
342-
replacement,
343-
PTAI->getType());
342+
auto *newInst = localBuilder.createUncheckedAddrCast(
343+
PTAI->getLoc(), replacement, PTAI->getType());
344344
helper.perform(newInst);
345345
return nullptr;
346346
}

test/SILOptimizer/sil_combine_ossa.sil

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,20 +505,42 @@ bb0(%0 : $*Builtin.Word, %1 : $Builtin.RawPointer):
505505
return %7 : $(Builtin.Word, Builtin.RawPointer)
506506
}
507507

508-
// CHECK-LABEL: sil [ossa] @a2p_p2a_reinterpret_cast_word_raw_pointer
508+
// CHECK-LABEL: sil [ossa] @a2p_p2a_reinterpret_cast_word_raw_pointer1
509509
// CHECK: bb0
510510
// CHECK-NEXT: unchecked_addr_cast
511511
// CHECK-NEXT: load
512512
// CHECK-NEXT: return
513-
// CHECK: } // end sil function 'a2p_p2a_reinterpret_cast_word_raw_pointer'
514-
sil [ossa] @a2p_p2a_reinterpret_cast_word_raw_pointer : $@convention(thin) (@inout Builtin.Word, Builtin.RawPointer) -> Int32 {
513+
// CHECK: } // end sil function 'a2p_p2a_reinterpret_cast_word_raw_pointer1'
514+
sil [ossa] @a2p_p2a_reinterpret_cast_word_raw_pointer1 : $@convention(thin) (@inout Builtin.Word, Builtin.RawPointer) -> Int32 {
515515
bb0(%0 : $*Builtin.Word, %1 : $Builtin.RawPointer):
516516
%2 = address_to_pointer %0 : $*Builtin.Word to $Builtin.RawPointer
517517
%3 = pointer_to_address %2 : $Builtin.RawPointer to [strict] $*Int32
518518
%4 = load [trivial] %3 : $*Int32
519519
return %4 : $Int32
520520
}
521521

522+
sil @useInt : $@convention(thin) (Int32) -> ()
523+
524+
// CHECK-LABEL: sil [ossa] @a2p_p2a_reinterpret_cast_word_raw_pointer2
525+
// CHECK: unchecked_addr_cast
526+
// CHECK: } // end sil function 'a2p_p2a_reinterpret_cast_word_raw_pointer2'
527+
sil [ossa] @a2p_p2a_reinterpret_cast_word_raw_pointer2 : $@convention(thin) (@owned ContiguousArray<UInt16>) -> () {
528+
bb0(%0 : @owned $ContiguousArray<UInt16>):
529+
%1 = begin_borrow %0 : $ContiguousArray<UInt16>
530+
%2 = struct_extract %1 : $ContiguousArray<UInt16>, #ContiguousArray._buffer
531+
%3 = struct_extract %2 : $_ContiguousArrayBuffer<UInt16>, #_ContiguousArrayBuffer._storage
532+
%4 = ref_tail_addr %3 : $__ContiguousArrayStorageBase, $UInt16
533+
%5 = address_to_pointer %4 : $*UInt16 to $Builtin.RawPointer
534+
%6 = pointer_to_address %5 : $Builtin.RawPointer to [strict] $*Int32
535+
%7 = load [trivial] %6 : $*Int32
536+
%8 = function_ref @useInt : $@convention(thin) (Int32) -> ()
537+
%9 = apply %8(%7) : $@convention(thin) (Int32) -> ()
538+
end_borrow %1 : $ContiguousArray<UInt16>
539+
destroy_value %0 : $ContiguousArray<UInt16>
540+
%12 = tuple ()
541+
return %12 : $()
542+
}
543+
522544
// CHECK-LABEL: sil [ossa] @sil_extract_of_string
523545
//
524546
// Make sure we only forward the first field of the string_literal

0 commit comments

Comments
 (0)