Skip to content
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
10 changes: 10 additions & 0 deletions lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1118,12 +1118,17 @@ SILInstruction *SILCombiner::visitUncheckedTakeEnumDataAddrInst(
return nullptr;

bool onlyLoads = true;
bool anyLoadCopies = false;
bool onlyDestroys = true;
for (auto U : getNonDebugUses(tedai)) {
// Check if it is load. If it is not a load, bail...
if (!isa<LoadInst>(U->getUser()) && !isa<LoadBorrowInst>(U->getUser()))
onlyLoads = false;

if (auto *li = dyn_cast<LoadInst>(U->getUser()))
if (li->getOwnershipQualifier() == LoadOwnershipQualifier::Copy)
anyLoadCopies = true;

// If we have a load_borrow, perform an additional check that we do not have
// any reborrow uses. We do not handle reborrows in this optimization.
if (auto *lbi = dyn_cast<LoadBorrowInst>(U->getUser())) {
Expand Down Expand Up @@ -1166,6 +1171,11 @@ SILInstruction *SILCombiner::visitUncheckedTakeEnumDataAddrInst(
if (tedai->getOperand()->getType().isAddressOnly(*tedai->getFunction()))
return nullptr;

// If the enum is noncopyable and any loads cause copies, the transformation
// would be illegal because it would introduce a copy of the noncopyable enum.
if (tedai->getOperand()->getType().isMoveOnly() && anyLoadCopies)
return nullptr;

// Grab the EnumAddr.
SILLocation loc = tedai->getLoc();
Builder.setCurrentDebugScope(tedai->getDebugScope());
Expand Down
49 changes: 49 additions & 0 deletions test/SILOptimizer/sil_combine_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ struct MoveOnlyStruct: ~Copyable {
var value: MyInt
}

enum NoncopyableEnum : ~Copyable {
case i(CopyableStruct)
}

struct CopyableStruct {
var guts: AnyObject
}

//////////////////////
// Simple DCE Tests //
//////////////////////
Expand Down Expand Up @@ -5530,3 +5538,44 @@ bb0:
return %6
}

// unchecked_take_enum_data_addr of a noncopyable base must not be combined if
// there are any load [copy] users.
// CHECK-LABEL: sil [ossa] @no_combine_uteda_load_copy_noncopyable {{.*}} {
// CHECK: unchecked_take_enum_data_addr
// CHECK-LABEL: } // end sil function 'no_combine_uteda_load_copy_noncopyable'
sil [ossa] @no_combine_uteda_load_copy_noncopyable : $@convention(thin) (@in NoncopyableEnum) -> () {
entry(%e_addr : $*NoncopyableEnum):
%i_addr = unchecked_take_enum_data_addr %e_addr : $*NoncopyableEnum, #NoncopyableEnum.i!enumelt
%i_copy = load [copy] %i_addr : $*CopyableStruct
apply undef(%i_copy) : $@convention(thin) (@owned CopyableStruct) -> ()
%i = load [take] %i_addr : $*CopyableStruct
apply undef(%i) : $@convention(thin) (@owned CopyableStruct) -> ()
return undef : $()
}

// unchecked_take_enum_data_addr of a noncopyable base should be combined if
// there are only load [take] users.
// CHECK-LABEL: sil [ossa] @combine_uteda_load_take_noncopyable {{.*}} {
// CHECK-NOT: unchecked_take_enum_data_addr
// CHECK-LABEL: } // end sil function 'combine_uteda_load_take_noncopyable'
sil [ossa] @combine_uteda_load_take_noncopyable : $@convention(thin) (@in NoncopyableEnum) -> () {
entry(%e_addr : $*NoncopyableEnum):
%i_addr = unchecked_take_enum_data_addr %e_addr : $*NoncopyableEnum, #NoncopyableEnum.i!enumelt
%i = load [take] %i_addr : $*CopyableStruct
apply undef(%i) : $@convention(thin) (@owned CopyableStruct) -> ()
return undef : $()
}

// unchecked_take_enum_data_addr of a noncopyable base should be combined if
// there are only load_borrow users.
// CHECK-LABEL: sil [ossa] @combine_uteda_load_borrow_noncopyable {{.*}} {
// CHECK-NOT: unchecked_take_enum_data_addr
// CHECK-LABEL: } // end sil function 'combine_uteda_load_borrow_noncopyable'
sil [ossa] @combine_uteda_load_borrow_noncopyable : $@convention(thin) (@in_guaranteed NoncopyableEnum) -> () {
entry(%e_addr : $*NoncopyableEnum):
%i_addr = unchecked_take_enum_data_addr %e_addr : $*NoncopyableEnum, #NoncopyableEnum.i!enumelt
%i = load_borrow %i_addr : $*CopyableStruct
apply undef(%i) : $@convention(thin) (@guaranteed CopyableStruct) -> ()
end_borrow %i : $CopyableStruct
return undef : $()
}