Skip to content

Commit 29d2458

Browse files
committed
[sil-combine] Enable unchecked_addr_cast formation in ownership mode.
1 parent 47f691c commit 29d2458

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

lib/SILOptimizer/SILCombiner/SILCombiner.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,10 @@ class SILCombiner :
433433
/// Returns true if the results of a try_apply are not used.
434434
static bool isTryApplyResultNotUsed(UserListTy &AcceptedUses,
435435
TryApplyInst *TAI);
436+
437+
bool hasOwnership() const {
438+
return Builder.hasOwnership();
439+
}
436440
};
437441

438442
} // end namespace swift

lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#define DEBUG_TYPE "sil-combine"
14+
1415
#include "SILCombiner.h"
16+
1517
#include "swift/SIL/DebugUtils.h"
1618
#include "swift/SIL/DynamicCasts.h"
1719
#include "swift/SIL/PatternMatch.h"
@@ -23,6 +25,7 @@
2325
#include "swift/SILOptimizer/Utils/CFGOptUtils.h"
2426
#include "swift/SILOptimizer/Utils/DebugOptUtils.h"
2527
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
28+
#include "swift/SILOptimizer/Utils/OwnershipOptUtils.h"
2629
#include "llvm/ADT/DenseMap.h"
2730
#include "llvm/ADT/SmallPtrSet.h"
2831
#include "llvm/ADT/SmallVector.h"
@@ -232,11 +235,19 @@ visitPointerToAddressInst(PointerToAddressInst *PTAI) {
232235
if (PTAI->isStrict()) {
233236
// We can not perform this optimization with ownership until we are able to
234237
// handle issues around interior pointers and expanding borrow scopes.
235-
if (!F->hasOwnership()) {
236-
if (auto *ATPI = dyn_cast<AddressToPointerInst>(PTAI->getOperand())) {
238+
if (auto *ATPI = dyn_cast<AddressToPointerInst>(PTAI->getOperand())) {
239+
if (!hasOwnership()) {
237240
return Builder.createUncheckedAddrCast(PTAI->getLoc(), ATPI->getOperand(),
238241
PTAI->getType());
239242
}
243+
244+
OwnershipRAUWHelper helper(ownershipFixupContext, PTAI, ATPI->getOperand());
245+
if (helper) {
246+
auto *newInst = Builder.createUncheckedAddrCast(PTAI->getLoc(), ATPI->getOperand(),
247+
PTAI->getType());
248+
helper.perform(newInst);
249+
return nullptr;
250+
}
240251
}
241252
}
242253

test/SILOptimizer/sil_combine_ossa.sil

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -474,10 +474,11 @@ bb0(%0 : $*Builtin.Word, %1 : $Builtin.RawPointer):
474474
}
475475

476476
// CHECK-LABEL: sil [ossa] @a2p_p2a_reinterpret_cast_word_raw_pointer
477-
// XHECK: bb0
478-
// XHECK-NEXT: unchecked_addr_cast
479-
// XHECK-NEXT: load
480-
// XHECK-NEXT: return
477+
// CHECK: bb0
478+
// CHECK-NEXT: unchecked_addr_cast
479+
// CHECK-NEXT: load
480+
// CHECK-NEXT: return
481+
// CHECK: } // end sil function 'a2p_p2a_reinterpret_cast_word_raw_pointer'
481482
sil [ossa] @a2p_p2a_reinterpret_cast_word_raw_pointer : $@convention(thin) (@inout Builtin.Word, Builtin.RawPointer) -> Int32 {
482483
bb0(%0 : $*Builtin.Word, %1 : $Builtin.RawPointer):
483484
%2 = address_to_pointer %0 : $*Builtin.Word to $Builtin.RawPointer
@@ -1450,10 +1451,11 @@ bb0:
14501451
}
14511452

14521453
// CHECK-LABEL: sil [ossa] @unchecked_addr_cast_formation : $@convention(thin) (@inout Builtin.NativeObject) -> Builtin.Word {
1453-
// XHECK: bb0([[INPUT_PTR:%[0-9]+]] : $*Builtin.NativeObject):
1454-
// XHECK-NEXT: [[ADDR_CAST:%[0-9]+]] = unchecked_addr_cast [[INPUT_PTR]] : $*Builtin.NativeObject to $*Builtin.Word
1455-
// XHECK-NEXT: [[RETURN_VALUE:%[0-9]+]] = load [trivial] [[ADDR_CAST]] : $*Builtin.Word
1456-
// XHECK-NEXT: return [[RETURN_VALUE]]
1454+
// CHECK: bb0([[INPUT_PTR:%[0-9]+]] : $*Builtin.NativeObject):
1455+
// CHECK-NEXT: [[ADDR_CAST:%[0-9]+]] = unchecked_addr_cast [[INPUT_PTR]] : $*Builtin.NativeObject to $*Builtin.Word
1456+
// CHECK-NEXT: [[RETURN_VALUE:%[0-9]+]] = load [trivial] [[ADDR_CAST]] : $*Builtin.Word
1457+
// CHECK-NEXT: return [[RETURN_VALUE]]
1458+
// CHECK: } // end sil function 'unchecked_addr_cast_formation'
14571459
sil [ossa] @unchecked_addr_cast_formation : $@convention(thin) (@inout Builtin.NativeObject) -> Builtin.Word {
14581460
bb0(%0 : $*Builtin.NativeObject):
14591461
%1 = address_to_pointer %0 : $*Builtin.NativeObject to $Builtin.RawPointer

0 commit comments

Comments
 (0)