Skip to content

Commit 142aa42

Browse files
committed
[semantic-arc-opts] Teach semantic-arc-opts how to handle tuples with multiple non-trivial operands.
<rdar://problem/63950481>
1 parent fb99cfc commit 142aa42

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

include/swift/SIL/OwnershipUtils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,10 @@ struct OwnedValueIntroducerKind {
502502
/// owned.
503503
Struct,
504504

505+
/// An owned value that is from a tuple that has multiple operands that are
506+
/// owned.
507+
Tuple,
508+
505509
/// An owned value that is a function argument.
506510
FunctionArgument,
507511

@@ -528,6 +532,8 @@ struct OwnedValueIntroducerKind {
528532
return OwnedValueIntroducerKind(BeginApply);
529533
case ValueKind::StructInst:
530534
return OwnedValueIntroducerKind(Struct);
535+
case ValueKind::TupleInst:
536+
return OwnedValueIntroducerKind(Tuple);
531537
case ValueKind::SILPhiArgument: {
532538
auto *phiArg = cast<SILPhiArgument>(value);
533539
if (dyn_cast_or_null<TryApplyInst>(phiArg->getSingleTerminator())) {
@@ -621,6 +627,7 @@ struct OwnedValueIntroducer {
621627
case OwnedValueIntroducerKind::LoadTake:
622628
case OwnedValueIntroducerKind::Phi:
623629
case OwnedValueIntroducerKind::Struct:
630+
case OwnedValueIntroducerKind::Tuple:
624631
case OwnedValueIntroducerKind::FunctionArgument:
625632
case OwnedValueIntroducerKind::PartialApplyInit:
626633
case OwnedValueIntroducerKind::AllocBoxInit:
@@ -639,6 +646,7 @@ struct OwnedValueIntroducer {
639646
case OwnedValueIntroducerKind::Phi:
640647
return true;
641648
case OwnedValueIntroducerKind::Struct:
649+
case OwnedValueIntroducerKind::Tuple:
642650
case OwnedValueIntroducerKind::Copy:
643651
case OwnedValueIntroducerKind::LoadCopy:
644652
case OwnedValueIntroducerKind::Apply:

lib/SIL/Utils/OwnershipUtils.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,9 @@ void OwnedValueIntroducerKind::print(llvm::raw_ostream &os) const {
488488
case OwnedValueIntroducerKind::Struct:
489489
os << "Struct";
490490
return;
491+
case OwnedValueIntroducerKind::Tuple:
492+
os << "Tuple";
493+
return;
491494
case OwnedValueIntroducerKind::FunctionArgument:
492495
os << "FunctionArgument";
493496
return;

lib/SILOptimizer/Transforms/SemanticARCOpts.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class OwnershipPhiOperand {
5555
enum Kind {
5656
Branch,
5757
Struct,
58+
Tuple,
5859
};
5960

6061
private:
@@ -67,6 +68,7 @@ class OwnershipPhiOperand {
6768
switch (op->getUser()->getKind()) {
6869
case SILInstructionKind::BranchInst:
6970
case SILInstructionKind::StructInst:
71+
case SILInstructionKind::TupleInst:
7072
return {{const_cast<Operand *>(op)}};
7173
default:
7274
return None;
@@ -79,6 +81,8 @@ class OwnershipPhiOperand {
7981
return Kind::Branch;
8082
case SILInstructionKind::StructInst:
8183
return Kind::Struct;
84+
case SILInstructionKind::TupleInst:
85+
return Kind::Tuple;
8286
default:
8387
llvm_unreachable("unhandled case?!");
8488
}
@@ -104,6 +108,7 @@ class OwnershipPhiOperand {
104108
switch (getKind()) {
105109
case Kind::Branch:
106110
return true;
111+
case Kind::Tuple:
107112
case Kind::Struct:
108113
return false;
109114
}
@@ -121,6 +126,8 @@ class OwnershipPhiOperand {
121126
switch (getKind()) {
122127
case Kind::Struct:
123128
return visitor(cast<StructInst>(getInst()));
129+
case Kind::Tuple:
130+
return visitor(cast<TupleInst>(getInst()));
124131
case Kind::Branch: {
125132
auto *br = cast<BranchInst>(getInst());
126133
unsigned opNum = getOperandNumber();
@@ -589,6 +596,11 @@ static SILValue convertIntroducerToGuaranteed(OwnedValueIntroducer introducer) {
589596
si->setOwnershipKind(ValueOwnershipKind::Guaranteed);
590597
return si;
591598
}
599+
case OwnedValueIntroducerKind::Tuple: {
600+
auto *ti = cast<TupleInst>(introducer.value);
601+
ti->setOwnershipKind(ValueOwnershipKind::Guaranteed);
602+
return ti;
603+
}
592604
case OwnedValueIntroducerKind::Copy:
593605
case OwnedValueIntroducerKind::LoadCopy:
594606
case OwnedValueIntroducerKind::Apply:

test/SILOptimizer/semantic-arc-opts-canonical.sil

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,6 @@ bb0(%0 : @guaranteed $StructMemberTest):
323323
// multiple trivial arguments), we can.
324324
//
325325
// CHECK-LABEL: sil [ossa] @multiple_arg_forwarding_inst_test : $@convention(thin) (@guaranteed Builtin.NativeObject, @guaranteed Builtin.NativeObject, Builtin.Int32) -> () {
326-
// CHECK: copy_value
327-
// CHECK: copy_value
328326
// CHECK-NOT: copy_value
329327
// CHECK: } // end sil function 'multiple_arg_forwarding_inst_test'
330328
sil [ossa] @multiple_arg_forwarding_inst_test : $@convention(thin) (@guaranteed Builtin.NativeObject, @guaranteed Builtin.NativeObject, Builtin.Int32) -> () {

test/SILOptimizer/semantic-arc-opts.sil

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,7 @@ bb0(%0 : @guaranteed $StructMemberTest):
347347
return %7 : $Builtin.Int32
348348
}
349349

350-
// Make sure that in a case where we have multiple non-trivial values passed to
351-
// a forwarding instruction we do not optimize... but if we only have one (and
352-
// multiple trivial arguments), we can.
353-
//
354350
// CHECK-LABEL: sil [ossa] @multiple_arg_forwarding_inst_test : $@convention(thin) (@guaranteed Builtin.NativeObject, @guaranteed Builtin.NativeObject, Builtin.Int32) -> () {
355-
// CHECK: copy_value
356-
// CHECK: copy_value
357351
// CHECK-NOT: copy_value
358352
// CHECK: } // end sil function 'multiple_arg_forwarding_inst_test'
359353
sil [ossa] @multiple_arg_forwarding_inst_test : $@convention(thin) (@guaranteed Builtin.NativeObject, @guaranteed Builtin.NativeObject, Builtin.Int32) -> () {
@@ -2560,4 +2554,17 @@ bb0(%0 : @guaranteed $Builtin.NativeObject, %1 : @guaranteed $Builtin.NativeObje
25602554
destroy_value %2 : $NativeObjectPair
25612555
%9999 = tuple()
25622556
return %9999 : $()
2563-
}
2557+
}
2558+
2559+
// CHECK-LABEL: sil [ossa] @tuple_with_multiple_nontrivial_operands : $@convention(thin) (@guaranteed Builtin.NativeObject, @guaranteed Builtin.NativeObject) -> () {
2560+
// CHECK-NOT: copy_value
2561+
// CHECK: } // end sil function 'tuple_with_multiple_nontrivial_operands'
2562+
sil [ossa] @tuple_with_multiple_nontrivial_operands : $@convention(thin) (@guaranteed Builtin.NativeObject, @guaranteed Builtin.NativeObject) -> () {
2563+
bb0(%0 : @guaranteed $Builtin.NativeObject, %1 : @guaranteed $Builtin.NativeObject):
2564+
%0a = copy_value %0 : $Builtin.NativeObject
2565+
%1a = copy_value %1 : $Builtin.NativeObject
2566+
%2 = tuple (%0a : $Builtin.NativeObject, %1a : $Builtin.NativeObject)
2567+
destroy_value %2 : $(Builtin.NativeObject, Builtin.NativeObject)
2568+
%9999 = tuple()
2569+
return %9999 : $()
2570+
}

0 commit comments

Comments
 (0)