Skip to content

Commit 3e77c45

Browse files
authored
Merge pull request #11682 from gottesmm/semantic_sil_objc_deallocator_fixes
2 parents 7f29b36 + 9d5f280 commit 3e77c45

File tree

7 files changed

+62
-24
lines changed

7 files changed

+62
-24
lines changed

lib/SILGen/SILGenBuilder.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,21 @@ AllocExistentialBoxInst *SILGenBuilder::createAllocExistentialBox(
192192
ManagedValue SILGenBuilder::createStructExtract(SILLocation loc,
193193
ManagedValue base,
194194
VarDecl *decl) {
195-
ManagedValue borrowedBase = SGF.emitManagedBeginBorrow(loc, base.getValue());
195+
ManagedValue borrowedBase = base.borrow(SGF, loc);
196196
SILValue extract =
197197
SILBuilder::createStructExtract(loc, borrowedBase.getValue(), decl);
198198
return ManagedValue::forUnmanaged(extract);
199199
}
200200

201+
ManagedValue SILGenBuilder::createRefElementAddr(SILLocation loc,
202+
ManagedValue operand,
203+
VarDecl *field,
204+
SILType resultTy) {
205+
operand = operand.borrow(SGF, loc);
206+
SILValue result = createRefElementAddr(loc, operand.getValue(), field);
207+
return ManagedValue::forUnmanaged(result);
208+
}
209+
201210
ManagedValue SILGenBuilder::createCopyValue(SILLocation loc,
202211
ManagedValue originalValue) {
203212
auto &lowering = SGF.getTypeLowering(originalValue.getType());

lib/SILGen/SILGenBuilder.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,16 @@ class SILGenBuilder : public SILBuilder {
134134
//
135135

136136
using SILBuilder::createStructExtract;
137-
using SILBuilder::createCopyValue;
138-
using SILBuilder::createCopyUnownedValue;
139137
ManagedValue createStructExtract(SILLocation loc, ManagedValue base,
140138
VarDecl *decl);
141139

140+
using SILBuilder::createRefElementAddr;
141+
ManagedValue createRefElementAddr(SILLocation loc, ManagedValue operand,
142+
VarDecl *field, SILType resultTy);
143+
144+
using SILBuilder::createCopyValue;
145+
using SILBuilder::createCopyUnownedValue;
146+
142147
/// Emit a +1 copy on \p originalValue that lives until the end of the current
143148
/// lexical scope.
144149
ManagedValue createCopyValue(SILLocation loc, ManagedValue originalValue);

lib/SILGen/SILGenDestructor.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ void SILGenFunction::emitDestroyingDestructor(DestructorDecl *dd) {
7878

7979
{
8080
Scope S(Cleanups, cleanupLoc);
81-
ManagedValue borrowedResultSelfValue =
82-
emitManagedBeginBorrow(cleanupLoc, resultSelfValue);
83-
SILValue borrowedValue = borrowedResultSelfValue.getUnmanagedValue();
84-
if (classTy != borrowedValue->getType()) {
81+
ManagedValue borrowedValue =
82+
ManagedValue::forUnmanaged(resultSelfValue).borrow(*this, cleanupLoc);
83+
84+
if (classTy != borrowedValue.getType()) {
8585
borrowedValue =
8686
B.createUncheckedRefCast(cleanupLoc, borrowedValue, classTy);
8787
}
@@ -168,23 +168,30 @@ void SILGenFunction::emitIVarDestroyer(SILDeclRef ivarDestroyer) {
168168
RegularLocation loc(cd);
169169
loc.markAutoGenerated();
170170

171-
SILValue selfValue = emitSelfDecl(cd->getDestructor()->getImplicitSelfDecl());
171+
ManagedValue selfValue = ManagedValue::forUnmanaged(
172+
emitSelfDecl(cd->getDestructor()->getImplicitSelfDecl()));
172173

173174
auto cleanupLoc = CleanupLocation::get(loc);
174175
prepareEpilog(TupleType::getEmpty(getASTContext()), false, cleanupLoc);
175-
emitClassMemberDestruction(selfValue, cd, cleanupLoc);
176+
{
177+
Scope S(*this, cleanupLoc);
178+
emitClassMemberDestruction(selfValue, cd, cleanupLoc);
179+
}
180+
176181
B.createReturn(loc, emitEmptyTuple(loc));
177182
emitEpilog(loc);
178183
}
179184

180-
void SILGenFunction::emitClassMemberDestruction(SILValue selfValue,
185+
void SILGenFunction::emitClassMemberDestruction(ManagedValue selfValue,
181186
ClassDecl *cd,
182187
CleanupLocation cleanupLoc) {
188+
selfValue = selfValue.borrow(*this, cleanupLoc);
183189
for (VarDecl *vd : cd->getStoredProperties()) {
184190
const TypeLowering &ti = getTypeLowering(vd->getType());
185191
if (!ti.isTrivial()) {
186-
SILValue addr = B.createRefElementAddr(cleanupLoc, selfValue, vd,
187-
ti.getLoweredType().getAddressType());
192+
SILValue addr =
193+
B.createRefElementAddr(cleanupLoc, selfValue.getValue(), vd,
194+
ti.getLoweredType().getAddressType());
188195
B.createDestroyAddr(cleanupLoc, addr);
189196
}
190197
}

lib/SILGen/SILGenExpr.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3473,8 +3473,20 @@ RValue RValueEmitter::visitRebindSelfInConstructorExpr(
34733473
!calledCtor->isImplicit() &&
34743474
usesObjCAllocator(classDecl)) {
34753475

3476-
// Check whether the new self is null.
3477-
SILValue isNonnullSelf = SGF.B.createIsNonnull(E, newSelf.getValue());
3476+
// Check whether the new self is null. *NOTE* At this point, we can not
3477+
// access the actual new value using newSelf anymore. We need to grab self
3478+
// via the normal manner of doing so.
3479+
SILValue isNonnullSelf;
3480+
{
3481+
Scope S(SGF, E);
3482+
RValue selfRValue =
3483+
SGF.emitRValueForDecl(E, selfDecl, selfTy->getCanonicalType(),
3484+
AccessSemantics::DirectToStorage,
3485+
SGFContext::AllowGuaranteedPlusZero);
3486+
ManagedValue reloadedSelf =
3487+
std::move(selfRValue).getAsSingleValue(SGF, E);
3488+
isNonnullSelf = SGF.B.createIsNonnull(E, reloadedSelf.getValue());
3489+
}
34783490
Condition cond = SGF.emitCondition(isNonnullSelf, E,
34793491
/*hasFalseCode=*/false,
34803492
/*invertValue=*/true,

lib/SILGen/SILGenFunction.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,9 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
500500
///
501501
/// \param selfValue The 'self' value.
502502
/// \param cd The class declaration whose members are being destroyed.
503-
void emitClassMemberDestruction(SILValue selfValue, ClassDecl *cd,
503+
void emitClassMemberDestruction(ManagedValue selfValue, ClassDecl *cd,
504504
CleanupLocation cleanupLoc);
505+
505506
/// Generates code for a curry thunk from one uncurry level
506507
/// of a function to another.
507508
void emitCurryThunk(SILDeclRef thunk);

test/SILGen/objc_dealloc.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -sdk %S/Inputs -I %S/Inputs -enable-source-import %s -emit-silgen | %FileCheck %s
1+
// RUN: %target-swift-frontend -sdk %S/Inputs -I %S/Inputs -enable-source-import %s -emit-silgen -enable-sil-ownership | %FileCheck %s
22

33
// REQUIRES: objc_interop
44

@@ -18,7 +18,7 @@ class SwiftGizmo : Gizmo {
1818
// CHECK-NEXT: return [[RESULT]] : $X
1919

2020
// CHECK-LABEL: sil hidden @_T012objc_dealloc10SwiftGizmoC{{[_0-9a-zA-Z]*}}fc
21-
// CHECK: bb0([[SELF_PARAM:%[0-9]+]] : $SwiftGizmo):
21+
// CHECK: bb0([[SELF_PARAM:%[0-9]+]] : @owned $SwiftGizmo):
2222
override init() {
2323
// CHECK: [[SELF_BOX:%.*]] = alloc_box ${ var SwiftGizmo }, let, name "self"
2424
// CHECK: [[SELF_UNINIT:%.*]] = mark_uninitialized [derivedselfonly] [[SELF_BOX]] : ${ var SwiftGizmo }
@@ -36,7 +36,7 @@ class SwiftGizmo : Gizmo {
3636

3737
// CHECK-LABEL: sil hidden @_T012objc_dealloc10SwiftGizmoCfD : $@convention(method) (@owned SwiftGizmo) -> ()
3838
deinit {
39-
// CHECK: bb0([[SELF:%[0-9]+]] : $SwiftGizmo):
39+
// CHECK: bb0([[SELF:%[0-9]+]] : @owned $SwiftGizmo):
4040
// Call onDestruct()
4141
// CHECK: [[ONDESTRUCT_REF:%[0-9]+]] = function_ref @_T012objc_dealloc10onDestructyyF : $@convention(thin) () -> ()
4242
// CHECK: [[ONDESTRUCT_RESULT:%[0-9]+]] = apply [[ONDESTRUCT_REF]]() : $@convention(thin) () -> ()
@@ -56,7 +56,7 @@ class SwiftGizmo : Gizmo {
5656

5757
// Objective-C deallocation deinit thunk (i.e., -dealloc).
5858
// CHECK-LABEL: sil hidden [thunk] @_T012objc_dealloc10SwiftGizmoCfDTo : $@convention(objc_method) (SwiftGizmo) -> ()
59-
// CHECK: bb0([[SELF:%[0-9]+]] : $SwiftGizmo):
59+
// CHECK: bb0([[SELF:%[0-9]+]] : @unowned $SwiftGizmo):
6060
// CHECK: [[SELF_COPY:%.*]] = copy_value [[SELF]]
6161

6262
// CHECK: [[GIZMO_DTOR:%[0-9]+]] = function_ref @_T012objc_dealloc10SwiftGizmoCfD : $@convention(method) (@owned SwiftGizmo) -> ()
@@ -65,7 +65,7 @@ class SwiftGizmo : Gizmo {
6565

6666
// Objective-C IVar initializer (i.e., -.cxx_construct)
6767
// CHECK-LABEL: sil hidden @_T012objc_dealloc10SwiftGizmoCfeTo : $@convention(objc_method) (@owned SwiftGizmo) -> @owned SwiftGizmo
68-
// CHECK: bb0([[SELF_PARAM:%[0-9]+]] : $SwiftGizmo):
68+
// CHECK: bb0([[SELF_PARAM:%[0-9]+]] : @owned $SwiftGizmo):
6969
// CHECK-NEXT: debug_value [[SELF_PARAM]] : $SwiftGizmo, let, name "self"
7070
// CHECK-NEXT: [[SELF:%[0-9]+]] = mark_uninitialized [rootself] [[SELF_PARAM]] : $SwiftGizmo
7171
// CHECK: [[XINIT:%[0-9]+]] = function_ref @_T012objc_dealloc10SwiftGizmoC1xAA1XCvfi
@@ -80,10 +80,12 @@ class SwiftGizmo : Gizmo {
8080

8181
// Objective-C IVar destroyer (i.e., -.cxx_destruct)
8282
// CHECK-LABEL: sil hidden @_T012objc_dealloc10SwiftGizmoCfETo : $@convention(objc_method) (SwiftGizmo) -> ()
83-
// CHECK: bb0([[SELF:%[0-9]+]] : $SwiftGizmo):
84-
// CHECK-NEXT: debug_value [[SELF]] : $SwiftGizmo, let, name "self"
85-
// CHECK-NEXT: [[X:%[0-9]+]] = ref_element_addr [[SELF]] : $SwiftGizmo, #SwiftGizmo.x
83+
// CHECK: bb0([[SELF:%[0-9]+]] : @unowned $SwiftGizmo):
84+
// CHECK-NEXT: debug_value [[SELF]] : $SwiftGizmo, let, name "self"
85+
// CHECK-NEXT: [[SELF_BORROW:%.*]] = begin_borrow [[SELF]]
86+
// CHECK-NEXT: [[X:%[0-9]+]] = ref_element_addr [[SELF_BORROW]] : $SwiftGizmo, #SwiftGizmo.x
8687
// CHECK-NEXT: destroy_addr [[X]] : $*X
88+
// CHECK-NEXT: end_borrow [[SELF_BORROW]] from [[SELF]]
8789
// CHECK-NEXT: [[RESULT:%[0-9]+]] = tuple ()
8890
// CHECK-NEXT: return [[RESULT]] : $()
8991
}

test/SILGen/objc_thunks.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,9 @@ extension Hoozit {
427427
// CHECK: [[CTOR:%[0-9]+]] = class_method [volatile] [[SELF:%[0-9]+]] : $Hoozit, #Hoozit.init!initializer.1.foreign : (Hoozit.Type) -> (Int) -> Hoozit, $@convention(objc_method) (Int, @owned Hoozit) -> @owned Hoozit
428428
// CHECK: [[NEW_SELF:%[0-9]+]] = apply [[CTOR]]
429429
// CHECK: store [[NEW_SELF]] to [init] [[PB_BOX]] : $*Hoozit
430-
// CHECK: [[NONNULL:%[0-9]+]] = is_nonnull [[NEW_SELF]] : $Hoozit
430+
// CHECK: [[RELOADED_SELF:%.*]] = load_borrow [[PB_BOX]]
431+
// CHECK: [[NONNULL:%[0-9]+]] = is_nonnull [[RELOADED_SELF]] : $Hoozit
432+
// CHECK: end_borrow [[RELOADED_SELF]] from [[PB_BOX]]
431433
// CHECK-NEXT: cond_br [[NONNULL]], [[NONNULL_BB:bb[0-9]+]], [[NULL_BB:bb[0-9]+]]
432434
// CHECK: [[NULL_BB]]:
433435
// CHECK-NEXT: destroy_value [[X_BOX]] : ${ var X }

0 commit comments

Comments
 (0)