Skip to content

Commit

Permalink
[sil-combine] Enable unchecked_addr_cast formation in ownership mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
gottesmm committed Jan 28, 2021
1 parent 47f691c commit 29d2458
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
4 changes: 4 additions & 0 deletions lib/SILOptimizer/SILCombiner/SILCombiner.h
Expand Up @@ -433,6 +433,10 @@ class SILCombiner :
/// Returns true if the results of a try_apply are not used.
static bool isTryApplyResultNotUsed(UserListTy &AcceptedUses,
TryApplyInst *TAI);

bool hasOwnership() const {
return Builder.hasOwnership();
}
};

} // end namespace swift
Expand Down
15 changes: 13 additions & 2 deletions lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp
Expand Up @@ -11,7 +11,9 @@
//===----------------------------------------------------------------------===//

#define DEBUG_TYPE "sil-combine"

#include "SILCombiner.h"

#include "swift/SIL/DebugUtils.h"
#include "swift/SIL/DynamicCasts.h"
#include "swift/SIL/PatternMatch.h"
Expand All @@ -23,6 +25,7 @@
#include "swift/SILOptimizer/Utils/CFGOptUtils.h"
#include "swift/SILOptimizer/Utils/DebugOptUtils.h"
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
#include "swift/SILOptimizer/Utils/OwnershipOptUtils.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
Expand Down Expand Up @@ -232,11 +235,19 @@ visitPointerToAddressInst(PointerToAddressInst *PTAI) {
if (PTAI->isStrict()) {
// We can not perform this optimization with ownership until we are able to
// handle issues around interior pointers and expanding borrow scopes.
if (!F->hasOwnership()) {
if (auto *ATPI = dyn_cast<AddressToPointerInst>(PTAI->getOperand())) {
if (auto *ATPI = dyn_cast<AddressToPointerInst>(PTAI->getOperand())) {
if (!hasOwnership()) {
return Builder.createUncheckedAddrCast(PTAI->getLoc(), ATPI->getOperand(),
PTAI->getType());
}

OwnershipRAUWHelper helper(ownershipFixupContext, PTAI, ATPI->getOperand());
if (helper) {
auto *newInst = Builder.createUncheckedAddrCast(PTAI->getLoc(), ATPI->getOperand(),
PTAI->getType());
helper.perform(newInst);
return nullptr;
}
}
}

Expand Down
18 changes: 10 additions & 8 deletions test/SILOptimizer/sil_combine_ossa.sil
Expand Up @@ -474,10 +474,11 @@ bb0(%0 : $*Builtin.Word, %1 : $Builtin.RawPointer):
}

// CHECK-LABEL: sil [ossa] @a2p_p2a_reinterpret_cast_word_raw_pointer
// XHECK: bb0
// XHECK-NEXT: unchecked_addr_cast
// XHECK-NEXT: load
// XHECK-NEXT: return
// 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 {
bb0(%0 : $*Builtin.Word, %1 : $Builtin.RawPointer):
%2 = address_to_pointer %0 : $*Builtin.Word to $Builtin.RawPointer
Expand Down Expand Up @@ -1450,10 +1451,11 @@ bb0:
}

// CHECK-LABEL: sil [ossa] @unchecked_addr_cast_formation : $@convention(thin) (@inout Builtin.NativeObject) -> Builtin.Word {
// XHECK: bb0([[INPUT_PTR:%[0-9]+]] : $*Builtin.NativeObject):
// XHECK-NEXT: [[ADDR_CAST:%[0-9]+]] = unchecked_addr_cast [[INPUT_PTR]] : $*Builtin.NativeObject to $*Builtin.Word
// XHECK-NEXT: [[RETURN_VALUE:%[0-9]+]] = load [trivial] [[ADDR_CAST]] : $*Builtin.Word
// XHECK-NEXT: return [[RETURN_VALUE]]
// CHECK: bb0([[INPUT_PTR:%[0-9]+]] : $*Builtin.NativeObject):
// CHECK-NEXT: [[ADDR_CAST:%[0-9]+]] = unchecked_addr_cast [[INPUT_PTR]] : $*Builtin.NativeObject to $*Builtin.Word
// CHECK-NEXT: [[RETURN_VALUE:%[0-9]+]] = load [trivial] [[ADDR_CAST]] : $*Builtin.Word
// CHECK-NEXT: return [[RETURN_VALUE]]
// CHECK: } // end sil function 'unchecked_addr_cast_formation'
sil [ossa] @unchecked_addr_cast_formation : $@convention(thin) (@inout Builtin.NativeObject) -> Builtin.Word {
bb0(%0 : $*Builtin.NativeObject):
%1 = address_to_pointer %0 : $*Builtin.NativeObject to $Builtin.RawPointer
Expand Down

0 comments on commit 29d2458

Please sign in to comment.