Skip to content

Commit 0af4c41

Browse files
authored
Merge pull request #14072 from eeckstein/fix-objoutliner
2 parents cd3b34c + e081392 commit 0af4c41

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

lib/SILOptimizer/Transforms/ObjectOutliner.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,19 +377,24 @@ 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-
B.createStrongRetain(ARI->getLoc(), GVI, B.getDefaultAtomicity());
380+
bool hasToRetain = true;
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()) {
386386
case SILInstructionKind::DeallocRefInst:
387+
case SILInstructionKind::SetDeallocatingInst:
388+
hasToRetain = false;
387389
ToRemove.push_back(User);
388390
break;
389391
default:
390392
Use->set(GVI);
391393
}
392394
}
395+
if (hasToRetain) {
396+
B.createStrongRetain(ARI->getLoc(), GVI, B.getDefaultAtomicity());
397+
}
393398
if (FindStringCall && NumTailElems > 16) {
394399
assert(&*std::next(ARI->getIterator()) != FindStringCall &&
395400
"FindStringCall must not be the next instruction after ARI because "

test/SILOptimizer/globalopt-iter.sil

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ class E : B { }
88

99
// CHECK: sil @patatino : $@convention(thin) () -> () {
1010
// CHECK: bb0:
11-
// CHECK: %0 = global_value @patatinoTv_ : $B // user: %1
12-
// CHECK: strong_retain %0 : $B // id: %1
13-
// CHECK: %2 = tuple () // user: %3
14-
// CHECK: return %2 : $() // id: %3
11+
// CHECK: %0 = global_value @patatinoTv_ : $B
12+
// CHECK: %1 = tuple ()
13+
// CHECK: return %1 : $()
1514
// CHECK: }
1615

1716
sil @patatino : $@convention(thin) () -> () {

test/SILOptimizer/objectoutliner.sil

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ bb0:
7474
return %r : $()
7575
}
7676

77+
// CHECK-LABEL: sil @handle_deallocation
78+
// CHECK: global_value
79+
// CHECK-NEXT: ref_element_addr
80+
// CHECK-NEXT: tuple
81+
// CHECK-NEXT: return
82+
sil @handle_deallocation : $@convention(thin) () -> () {
83+
bb0:
84+
%3 = integer_literal $Builtin.Int64, 3
85+
%4 = struct $Int64 (%3 : $Builtin.Int64)
86+
%5 = alloc_ref $Obj
87+
%6 = ref_element_addr %5 : $Obj, #Obj.value
88+
store %4 to %6 : $*Int64
89+
set_deallocating %5 : $Obj
90+
dealloc_ref %5 : $Obj
91+
%r = tuple ()
92+
return %r : $()
93+
}
94+
7795
// CHECK-LABEL: sil @dont_outline_global_double_store
7896
// CHECK: alloc_ref
7997
// CHECK: store
@@ -203,5 +221,3 @@ bb0:
203221
%r = tuple ()
204222
return %r : $()
205223
}
206-
207-

0 commit comments

Comments
 (0)