Skip to content

Fix SILCombine of pointer_to_address instruction #64533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,10 @@ visitPointerToAddressInst(PointerToAddressInst *PTAI) {
OwnershipRAUWHelper helper(ownershipFixupContext, PTAI,
ATPI->getOperand());
if (helper) {
SILBuilderWithScope localBuilder(std::next(PTAI->getIterator()), Builder);
auto replacement = helper.prepareReplacement();
auto *newInst = Builder.createUncheckedAddrCast(PTAI->getLoc(),
replacement,
PTAI->getType());
auto *newInst = localBuilder.createUncheckedAddrCast(
PTAI->getLoc(), replacement, PTAI->getType());
helper.perform(newInst);
return nullptr;
}
Expand Down
31 changes: 28 additions & 3 deletions test/SILOptimizer/sil_combine_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -505,20 +505,45 @@ bb0(%0 : $*Builtin.Word, %1 : $Builtin.RawPointer):
return %7 : $(Builtin.Word, Builtin.RawPointer)
}

// CHECK-LABEL: sil [ossa] @a2p_p2a_reinterpret_cast_word_raw_pointer
// CHECK-LABEL: sil [ossa] @a2p_p2a_reinterpret_cast_word_raw_pointer1
// CHECK: bb0
// CHECK-NEXT: unchecked_addr_cast
// CHECK-NEXT: load
// CHECK-NEXT: return
// CHECK: } // end sil function 'a2p_p2a_reinterpret_cast_word_raw_pointer'
sil [ossa] @a2p_p2a_reinterpret_cast_word_raw_pointer : $@convention(thin) (@inout Builtin.Word, Builtin.RawPointer) -> Int32 {
// CHECK: } // end sil function 'a2p_p2a_reinterpret_cast_word_raw_pointer1'
sil [ossa] @a2p_p2a_reinterpret_cast_word_raw_pointer1 : $@convention(thin) (@inout Builtin.Word, Builtin.RawPointer) -> Int32 {
bb0(%0 : $*Builtin.Word, %1 : $Builtin.RawPointer):
%2 = address_to_pointer %0 : $*Builtin.Word to $Builtin.RawPointer
%3 = pointer_to_address %2 : $Builtin.RawPointer to [strict] $*Int32
%4 = load [trivial] %3 : $*Int32
return %4 : $Int32
}

sil @useInt : $@convention(thin) (Int32) -> ()

// CHECK-LABEL: sil [ossa] @a2p_p2a_reinterpret_cast_word_raw_pointer2
// CHECK: unchecked_addr_cast
// CHECK: } // end sil function 'a2p_p2a_reinterpret_cast_word_raw_pointer2'
sil [ossa] @a2p_p2a_reinterpret_cast_word_raw_pointer2 : $@convention(thin) (@owned ContiguousArray<UInt16>) -> () {
Copy link
Contributor

@nate-chandler nate-chandler Mar 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this test currently fail? Running sil-opt -sil-combine on this function with a not too old main (70b982f), valid code seems to be generated even when specifying -sil-opt-pass-count=1.6 to stop after adding the unchecked_addr_cast.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I had minimized the test too much.. Modified to a test that will fail without the patch now.

bb0(%0 : @owned $ContiguousArray<UInt16>):
%1 = begin_borrow %0 : $ContiguousArray<UInt16>
%2 = struct_extract %1 : $ContiguousArray<UInt16>, #ContiguousArray._buffer
%3 = struct_extract %2 : $_ContiguousArrayBuffer<UInt16>, #_ContiguousArrayBuffer._storage
%4 = ref_tail_addr %3 : $__ContiguousArrayStorageBase, $UInt16
%5 = address_to_pointer %4 : $*UInt16 to $Builtin.RawPointer
br bb1

bb1:
%6 = pointer_to_address %5 : $Builtin.RawPointer to [strict] $*Int32
%7 = load [trivial] %6 : $*Int32
%8 = function_ref @useInt : $@convention(thin) (Int32) -> ()
%9 = apply %8(%7) : $@convention(thin) (Int32) -> ()
end_borrow %1 : $ContiguousArray<UInt16>
destroy_value %0 : $ContiguousArray<UInt16>
%12 = tuple ()
return %12 : $()
}

// CHECK-LABEL: sil [ossa] @sil_extract_of_string
//
// Make sure we only forward the first field of the string_literal
Expand Down