Skip to content

Commit 3afd9c8

Browse files
committed
ObjectOutliner: fix for handling set_deallocating instruction correctly.
rdar://problem/36692828
1 parent 5971c91 commit 3afd9c8

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

lib/SILOptimizer/Transforms/ObjectOutliner.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -377,24 +377,26 @@ bool ObjectOutliner::optimizeObjectAllocation(
377377
// Replace the alloc_ref by global_value + strong_retain instructions.
378378
SILBuilder B(ARI);
379379
GlobalValueInst *GVI = B.createGlobalValue(ARI->getLoc(), Glob);
380-
bool hasToRetain = true;
380+
B.createStrongRetain(ARI->getLoc(), GVI, B.getDefaultAtomicity());
381381
llvm::SmallVector<Operand *, 8> Worklist(ARI->use_begin(), ARI->use_end());
382382
while (!Worklist.empty()) {
383383
auto *Use = Worklist.pop_back_val();
384384
SILInstruction *User = Use->getUser();
385385
switch (User->getKind()) {
386-
case SILInstructionKind::DeallocRefInst:
387386
case SILInstructionKind::SetDeallocatingInst:
388-
hasToRetain = false;
387+
// set_deallocating is a replacement for a strong_release. Therefore
388+
// we have to insert a strong_release to balance the strong_retain which
389+
// we inserted after the global_value instruction.
390+
B.setInsertionPoint(User);
391+
B.createStrongRelease(User->getLoc(), GVI, B.getDefaultAtomicity());
392+
LLVM_FALLTHROUGH;
393+
case SILInstructionKind::DeallocRefInst:
389394
ToRemove.push_back(User);
390395
break;
391396
default:
392397
Use->set(GVI);
393398
}
394399
}
395-
if (hasToRetain) {
396-
B.createStrongRetain(ARI->getLoc(), GVI, B.getDefaultAtomicity());
397-
}
398400
if (FindStringCall && NumTailElems > 16) {
399401
assert(&*std::next(ARI->getIterator()) != FindStringCall &&
400402
"FindStringCall must not be the next instruction after ARI because "

test/SILOptimizer/globalopt-iter.sil

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ class E : B { }
88

99
// CHECK: sil @patatino : $@convention(thin) () -> () {
1010
// CHECK: bb0:
11-
// CHECK: %0 = global_value @patatinoTv_ : $B
12-
// CHECK: %1 = tuple ()
13-
// CHECK: return %1 : $()
14-
// CHECK: }
11+
// CHECK-NEXT: %0 = global_value @patatinoTv_ : $B
12+
// CHECK-NEXT: strong_retain
13+
// CHECK-NEXT: strong_release
14+
// CHECK-NEXT: tuple ()
15+
// CHECK-NEXT: return
16+
// CHECK-NEXT: }
1517

1618
sil @patatino : $@convention(thin) () -> () {
1719
%1 = alloc_ref [stack] $B
20+
set_deallocating %1 : $B
1821
dealloc_ref [stack] %1 : $B
1922
%45 = tuple ()
2023
return %45 : $()

test/SILOptimizer/objectoutliner.sil

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ bb0:
7676

7777
// CHECK-LABEL: sil @handle_deallocation
7878
// CHECK: global_value
79+
// CHECK-NEXT: strong_retain
7980
// CHECK-NEXT: ref_element_addr
81+
// CHECK-NEXT: strong_release
8082
// CHECK-NEXT: tuple
8183
// CHECK-NEXT: return
8284
sil @handle_deallocation : $@convention(thin) () -> () {

0 commit comments

Comments
 (0)