Skip to content

Commit 8c64b8a

Browse files
committed
IRGen: Destroy enums with deinit when they have one.
1 parent 03a2393 commit 8c64b8a

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

lib/IRGen/GenEnum.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,10 @@ namespace {
628628
void consume(IRGenFunction &IGF, Explosion &src,
629629
Atomicity atomicity,
630630
SILType T) const override {
631+
if (tryEmitConsumeUsingDeinit(IGF, src, T)) {
632+
return;
633+
}
634+
631635
if (getLoadableSingleton())
632636
getLoadableSingleton()->consume(IGF, src, atomicity,
633637
getSingletonType(IGF.IGM, T));
@@ -639,6 +643,10 @@ namespace {
639643

640644
void destroy(IRGenFunction &IGF, Address addr, SILType T,
641645
bool isOutlined) const override {
646+
if (tryEmitDestroyUsingDeinit(IGF, addr, T)) {
647+
return;
648+
}
649+
642650
if (getSingleton() &&
643651
!getSingleton()->isTriviallyDestroyable(ResilienceExpansion::Maximal)) {
644652
if (!ElementsAreABIAccessible) {
@@ -2660,6 +2668,9 @@ namespace {
26602668

26612669
void consume(IRGenFunction &IGF, Explosion &src,
26622670
Atomicity atomicity, SILType T) const override {
2671+
if (tryEmitConsumeUsingDeinit(IGF, src, T)) {
2672+
return;
2673+
}
26632674
assert(TIK >= Loadable);
26642675

26652676
switch (CopyDestroyKind) {
@@ -2784,6 +2795,10 @@ namespace {
27842795

27852796
void destroy(IRGenFunction &IGF, Address addr, SILType T,
27862797
bool isOutlined) const override {
2798+
if (tryEmitDestroyUsingDeinit(IGF, addr, T)) {
2799+
return;
2800+
}
2801+
27872802
if (CopyDestroyKind == TriviallyDestroyable) {
27882803
return;
27892804
}
@@ -4663,6 +4678,9 @@ namespace {
46634678

46644679
void consume(IRGenFunction &IGF, Explosion &src,
46654680
Atomicity atomicity, SILType T) const override {
4681+
if (tryEmitConsumeUsingDeinit(IGF, src, T)) {
4682+
return;
4683+
}
46664684
assert(TIK >= Loadable);
46674685
switch (CopyDestroyKind) {
46684686
case TriviallyDestroyable:
@@ -5006,6 +5024,10 @@ namespace {
50065024

50075025
void destroy(IRGenFunction &IGF, Address addr, SILType T,
50085026
bool isOutlined) const override {
5027+
if (tryEmitDestroyUsingDeinit(IGF, addr, T)) {
5028+
return;
5029+
}
5030+
50095031
if (CopyDestroyKind == TriviallyDestroyable) {
50105032
return;
50115033
}

lib/IRGen/GenType.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2861,13 +2861,12 @@ static bool tryEmitDeinitCall(IRGenFunction &IGF,
28612861
auto ty = T.getASTType();
28622862
auto nominal = ty->getAnyNominal();
28632863
// We are only concerned with move-only type deinits here.
2864-
if (!nominal || isa<ClassDecl>(nominal)) {
2864+
if (!nominal || !nominal->getValueTypeDestructor()) {
28652865
return false;
28662866
}
28672867

28682868
auto deinit = IGF.getSILModule().lookUpMoveOnlyDeinit(nominal);
2869-
if (!deinit)
2870-
return false;
2869+
assert(deinit && "type has a deinit declared in AST but SIL deinit record is not present!");
28712870

28722871
// The deinit should take a single value parameter of the nominal type, either
28732872
// by @owned or indirect @in convention.

test/IRGen/moveonly_deinit.sil

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,28 @@ entry(%b : $*MOStruct):
143143
return undef : $()
144144
}
145145

146+
// CHECK-LABEL: define{{.*}}@destroy_enum_value(
147+
// CHECK-NEXT: entry:
148+
// CHECK-NEXT: trunc
149+
// CHECK-NEXT: zext
150+
// CHECK-NEXT: call{{.*}}@destroy_enum(
151+
// CHECK-NEXT: ret void
146152
sil @destroy_enum_value : $@convention(thin) (@owned MOEnum) -> () {
147153
entry(%b : $MOEnum):
148154
release_value %b : $MOEnum
149155
return undef : $()
150156
}
151157

158+
// CHECK-LABEL: define{{.*}}@destroy_enum_value_indirect(
159+
// CHECK-NEXT: entry:
160+
// CHECK-NEXT: bitcast
161+
// CHECK-NEXT: load
162+
// CHECK-NEXT: getelementptr
163+
// CHECK-NEXT: bitcast
164+
// CHECK-NEXT: load
165+
// CHECK-NEXT: zext
166+
// CHECK-NEXT: call{{.*}}@destroy_enum(
167+
// CHECK-NEXT: ret void
152168
sil @destroy_enum_value_indirect : $@convention(thin) (@in MOEnum) -> () {
153169
entry(%b : $*MOEnum):
154170
destroy_addr %b : $*MOEnum
@@ -167,7 +183,7 @@ entry(%b : $MOComboStruct):
167183

168184
// CHECK: define{{.*}}@[[COMBO_STRUCT_OUTLINED_DESTROY]](
169185
// CHECK: call {{.*}} @destroy_struct
170-
// CH/ECK: call {{.*}} @destroy_enum
186+
// CHECK: call {{.*}} @destroy_enum
171187
// CHECK: call {{.*}} @swift_release
172188
// CHECK: ret {{.*}} %0
173189

@@ -190,7 +206,7 @@ entry(%b : $MOComboEnum):
190206
// CHECK: [[STRUCT]]:
191207
// CHECK: call {{.*}} @destroy_struct
192208
// CHECK: [[ENUM]]:
193-
// C/HECK: call {{.*}} @destroy_enum
209+
// CHECK: call {{.*}} @destroy_enum
194210
// CHECK: [[CLASS]]:
195211
// CHECK: call {{.*}} @swift_release
196212

@@ -199,7 +215,7 @@ entry(%b : $MOComboEnum):
199215

200216
// CHECK: define{{.*}}@[[BOX_STRUCT_DESTRUCTOR]](
201217
// CHECK: call {{.*}} @destroy_struct
202-
// C/HECK: call {{.*}} @destroy_enum
218+
// CHECK: call {{.*}} @destroy_enum
203219
// CHECK: call {{.*}} @swift_release
204220
sil @box_struct : $@convention(thin) (@owned MOComboStruct) -> Builtin.NativeObject {
205221
entry(%0 : $MOComboStruct):
@@ -230,10 +246,10 @@ sil_moveonlydeinit MOSingleRefcountLikeStruct { @destroy_single_refcount_like_st
230246
// CHECK-LABEL: define {{.*}} @"{{.*}}8MOStructVwxx"
231247
// CHECK: call {{.*}} @destroy_struct(
232248
// CHECK-LABEL: define {{.*}} @"{{.*}}6MOEnumOwxx"
233-
// C/HECK: call {{.*}} @destroy_enum(
249+
// CHECK: call {{.*}} @destroy_enum(
234250
// CHECK-LABEL: define {{.*}} @"{{.*}}13MOComboStructVwxx"
235251
// CHECK: call {{.*}} @destroy_struct(
236-
// C/HECK: call {{.*}} @destroy_enum(
252+
// CHECK: call {{.*}} @destroy_enum(
237253
// CHECK: call {{.*}} @swift_retain(
238254
// CHECK-LABEL: define {{.*}} @"{{.*}}11MOComboEnumOwxx"
239255
// CHECK: call {{.*}} @[[COMBO_ENUM_OUTLINED_DESTROY]]

0 commit comments

Comments
 (0)