Skip to content

Commit 857fcec

Browse files
authored
Merge pull request #35776 from eeckstein/remove-trivial-enum-release
2 parents 303a285 + 5dd322f commit 857fcec

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,7 @@ SILInstruction *SILCombiner::visitIndexAddrInst(IndexAddrInst *IA) {
10001000
/// Walks over all fields of an aggregate and checks if a reference count
10011001
/// operation for \p value is required. This differs from a simple `isTrivial`
10021002
/// check, because it treats a value_to_bridge_object instruction as "trivial".
1003+
/// It can also handle non-trivial enums with trivial cases.
10031004
static bool isTrivial(SILValue value, SILFunction *function) {
10041005
SmallVector<ValueBase *, 32> workList;
10051006
SmallPtrSet<ValueBase *, 16> visited;
@@ -1017,6 +1018,11 @@ static bool isTrivial(SILValue value, SILFunction *function) {
10171018
}
10181019
continue;
10191020
}
1021+
if (auto *en = dyn_cast<EnumInst>(v)) {
1022+
if (en->hasOperand() && visited.insert(en->getOperand()).second)
1023+
workList.push_back(en->getOperand());
1024+
continue;
1025+
}
10201026
return false;
10211027
}
10221028
return true;

test/SILOptimizer/sil_combine.sil

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3855,6 +3855,49 @@ bb0(%0 : $UInt64):
38553855
return %7 : $()
38563856
}
38573857

3858+
enum EnumWithPayload {
3859+
case A(UInt64)
3860+
case B(AnyObject)
3861+
case C
3862+
}
3863+
3864+
// CHECK-LABEL: sil @optimize_arc_with_trivial_payload_enum
3865+
// CHECK: bb0(%0 : $UInt64):
3866+
// CHECK-NEXT: tuple
3867+
// CHECK-NEXT: return
3868+
// CHECK: } // end sil function 'optimize_arc_with_trivial_payload_enum'
3869+
sil @optimize_arc_with_trivial_payload_enum : $@convention(thin) (UInt64) -> () {
3870+
bb0(%0 : $UInt64):
3871+
%1 = enum $EnumWithPayload, #EnumWithPayload.A!enumelt, %0 : $UInt64
3872+
release_value %1 : $EnumWithPayload
3873+
%7 = tuple ()
3874+
return %7 : $()
3875+
}
3876+
3877+
// CHECK-LABEL: sil @optimize_arc_with_trivial_enum
3878+
// CHECK: bb0:
3879+
// CHECK-NEXT: tuple
3880+
// CHECK-NEXT: return
3881+
// CHECK: } // end sil function 'optimize_arc_with_trivial_enum'
3882+
sil @optimize_arc_with_trivial_enum : $@convention(thin) () -> () {
3883+
bb0:
3884+
%1 = enum $EnumWithPayload, #EnumWithPayload.C!enumelt
3885+
release_value %1 : $EnumWithPayload
3886+
%7 = tuple ()
3887+
return %7 : $()
3888+
}
3889+
3890+
// CHECK-LABEL: sil @dont_remove_release_of_nontrivial_enum
3891+
// CHECK: strong_release %0
3892+
// CHECK: } // end sil function 'dont_remove_release_of_nontrivial_enum'
3893+
sil @dont_remove_release_of_nontrivial_enum : $@convention(thin) (@guaranteed AnyObject) -> () {
3894+
bb0(%0 : $AnyObject):
3895+
%1 = enum $EnumWithPayload, #EnumWithPayload.B!enumelt, %0 : $AnyObject
3896+
release_value %1 : $EnumWithPayload
3897+
%7 = tuple ()
3898+
return %7 : $()
3899+
}
3900+
38583901
// CHECK-LABEL: sil @optimize_stringObject_bit_operations
38593902
// CHECK: bb0:
38603903
// CHECK-NEXT: %0 = integer_literal $Builtin.Int64, 4611686018427387904

0 commit comments

Comments
 (0)