Skip to content

Commit cff4e50

Browse files
committed
[sil-combine] Update pointer_to_address canonicalizations for OSSA.
For now I am not performing the [strict] elimination until I bring up the interior pointer OSSA utilities. I want to make further progress before I loop back around to that.
1 parent 542cacf commit cff4e50

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ SILInstruction *
114114
SILCombiner::
115115
visitPointerToAddressInst(PointerToAddressInst *PTAI) {
116116
auto *F = PTAI->getFunction();
117-
if (F->hasOwnership())
118-
return nullptr;
119117

120118
Builder.setCurrentDebugScope(PTAI->getDebugScope());
121119

@@ -127,11 +125,20 @@ visitPointerToAddressInst(PointerToAddressInst *PTAI) {
127125
// (pointer-to-address strict (address-to-pointer %x))
128126
// -> (unchecked_addr_cast %x)
129127
if (PTAI->isStrict()) {
130-
if (auto *ATPI = dyn_cast<AddressToPointerInst>(PTAI->getOperand())) {
131-
return Builder.createUncheckedAddrCast(PTAI->getLoc(), ATPI->getOperand(),
132-
PTAI->getType());
128+
// We can not perform this optimization with ownership until we are able to
129+
// handle issues around interior pointers and expanding borrow scopes.
130+
if (!F->hasOwnership()) {
131+
if (auto *ATPI = dyn_cast<AddressToPointerInst>(PTAI->getOperand())) {
132+
return Builder.createUncheckedAddrCast(PTAI->getLoc(), ATPI->getOperand(),
133+
PTAI->getType());
134+
}
133135
}
134136
}
137+
138+
// The rest of these canonicalizations optimize the code around
139+
// pointer_to_address by leave in a pointer_to_address meaning that we do not
140+
// need to worry about moving addresses out of interior pointer scopes.
141+
135142
// Turn this also into an index_addr. We generate this pattern after switching
136143
// the Word type to an explicit Int32 or Int64 in the stdlib.
137144
//
@@ -147,6 +154,10 @@ visitPointerToAddressInst(PointerToAddressInst *PTAI) {
147154
// $Builtin.Word
148155
// %114 = index_raw_pointer %100 : $Builtin.RawPointer, %113 : $Builtin.Word
149156
// %115 = pointer_to_address %114 : $Builtin.RawPointer to [strict] $*Int
157+
//
158+
// This is safe for ownership since our final SIL still has a
159+
// pointer_to_address meaning that we do not need to worry about interior
160+
// pointers.
150161
SILValue Distance;
151162
SILValue TruncOrBitCast;
152163
MetatypeInst *Metatype;
@@ -197,6 +208,7 @@ visitPointerToAddressInst(PointerToAddressInst *PTAI) {
197208
}
198209
}
199210
}
211+
200212
// Turn:
201213
//
202214
// %stride = Builtin.strideof(T) * %distance
@@ -208,6 +220,9 @@ visitPointerToAddressInst(PointerToAddressInst *PTAI) {
208220
// %addr = pointer_to_address %ptr, [strict] $T
209221
// %result = index_addr %addr, %distance
210222
//
223+
// This is safe for ownership since our final SIL still has a
224+
// pointer_to_address meaning that we do not need to worry about interior
225+
// pointers.
211226
BuiltinInst *Bytes = nullptr;
212227
if (match(PTAI->getOperand(),
213228
m_IndexRawPointerInst(

test/SILOptimizer/sil_combine_ossa.sil

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,11 +1458,11 @@ bb0(%0 : $*E, %1 : @owned $E, %2 : $*E):
14581458
}
14591459

14601460
// CHECK-LABEL: sil [ossa] @indexrawpointer_to_indexaddr : $@convention(thin) (Builtin.RawPointer, Builtin.Word) -> Int8 {
1461-
// XHECK: bb0
1462-
// XHECK-NEXT: pointer_to_address
1463-
// XHECK-NEXT: index_addr
1464-
// XHECK-NEXT: load
1465-
// XHECK-NEXT: return
1461+
// CHECK: bb0
1462+
// CHECK-NEXT: pointer_to_address
1463+
// CHECK-NEXT: index_addr
1464+
// CHECK-NEXT: load
1465+
// CHECK-NEXT: return
14661466
sil [ossa] @indexrawpointer_to_indexaddr : $@convention(thin) (Builtin.RawPointer, Builtin.Word) -> Int8 {
14671467
bb0(%0 : $Builtin.RawPointer, %1 : $Builtin.Word):
14681468
%3 = metatype $@thick Int8.Type
@@ -1477,11 +1477,12 @@ bb0(%0 : $Builtin.RawPointer, %1 : $Builtin.Word):
14771477
}
14781478

14791479
// CHECK-LABEL: sil [ossa] @indexrawpointer_to_indexaddr_strideof : $@convention(thin) (Builtin.RawPointer, Builtin.Word) -> Int8 {
1480-
// XHECK: bb0
1481-
// XHECK-NEXT: pointer_to_address
1482-
// XHECK-NEXT: index_addr
1483-
// XHECK-NEXT: load
1484-
// XHECK-NEXT: return
1480+
// CHECK: bb0
1481+
// CHECK-NEXT: pointer_to_address
1482+
// CHECK-NEXT: index_addr
1483+
// CHECK-NEXT: load
1484+
// CHECK-NEXT: return
1485+
// CHECK: } // end sil function 'indexrawpointer_to_indexaddr_strideof'
14851486
sil [ossa] @indexrawpointer_to_indexaddr_strideof : $@convention(thin) (Builtin.RawPointer, Builtin.Word) -> Int8 {
14861487
bb0(%0 : $Builtin.RawPointer, %1 : $Builtin.Word):
14871488
%3 = metatype $@thick Int8.Type
@@ -1512,11 +1513,11 @@ bb0(%0 : $Builtin.RawPointer, %1 : $Builtin.Word):
15121513
}
15131514

15141515
// CHECK-LABEL: sil [ossa] @indexrawpointer_to_indexaddr_with_casts : $@convention(thin) (Builtin.RawPointer, Builtin.Int64) -> Int32
1515-
// XHECK: pointer_to_address
1516-
// XHECK-NEXT: builtin "truncOrBitCast_Int64_Word"
1517-
// XHECK-NEXT: index_addr
1518-
// XHECK-NEXT: load
1519-
// XHECK-NEXT: return
1516+
// CHECK: pointer_to_address
1517+
// CHECK-NEXT: builtin "truncOrBitCast_Int64_Word"
1518+
// CHECK-NEXT: index_addr
1519+
// CHECK-NEXT: load
1520+
// CHECK-NEXT: return
15201521
// CHECK: } // end sil function 'indexrawpointer_to_indexaddr_with_casts'
15211522
sil [ossa] @indexrawpointer_to_indexaddr_with_casts : $@convention(thin) (Builtin.RawPointer, Builtin.Int64) -> Int32 {
15221523
bb0(%0 : $Builtin.RawPointer, %1: $Builtin.Int64):

0 commit comments

Comments
 (0)