Skip to content

Commit 5cb302e

Browse files
authored
Merge pull request #8579 from eeckstein/fix-stacknesting
2 parents 43c1ebc + a43a844 commit 5cb302e

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

include/swift/SILOptimizer/Utils/StackNesting.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ class StackNesting {
143143
///
144144
/// Returns true if any deallocations were inserted.
145145
bool insertDeallocs(const BitVector &AliveBefore, const BitVector &AliveAfter,
146-
SILInstruction *InsertionPoint);
146+
SILInstruction *InsertionPoint,
147+
Optional<SILLocation> Location);
147148

148149
/// Modifies the SIL to end up with a correct stack nesting.
149150
///

lib/SILOptimizer/Utils/StackNesting.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,23 +148,24 @@ bool StackNesting::solve() {
148148
}
149149

150150
static SILInstruction *createDealloc(SILInstruction *Alloc,
151-
SILInstruction *InsertionPoint) {
151+
SILInstruction *InsertionPoint,
152+
SILLocation Location) {
152153
SILBuilder B(InsertionPoint);
153154
switch (Alloc->getKind()) {
154155
case ValueKind::AllocStackInst:
155-
return B.createDeallocStack(InsertionPoint->getLoc(), Alloc);
156+
return B.createDeallocStack(Location, Alloc);
156157
case ValueKind::AllocRefInst:
157158
assert(cast<AllocRefInst>(Alloc)->canAllocOnStack());
158-
return B.createDeallocRef(InsertionPoint->getLoc(), Alloc,
159-
/*canBeOnStack*/true);
159+
return B.createDeallocRef(Location, Alloc, /*canBeOnStack*/true);
160160
default:
161161
llvm_unreachable("unknown stack allocation");
162162
}
163163
}
164164

165165
bool StackNesting::insertDeallocs(const BitVector &AliveBefore,
166166
const BitVector &AliveAfter,
167-
SILInstruction *InsertionPoint) {
167+
SILInstruction *InsertionPoint,
168+
Optional<SILLocation> Location) {
168169
if (!AliveBefore.test(AliveAfter))
169170
return false;
170171

@@ -175,7 +176,9 @@ bool StackNesting::insertDeallocs(const BitVector &AliveBefore,
175176
for (int LocNr = AliveBefore.find_first(); LocNr >= 0;
176177
LocNr = AliveBefore.find_next(LocNr)) {
177178
if (!AliveAfter.test(LocNr)) {
178-
InsertionPoint = createDealloc(StackLocs[LocNr].Alloc, InsertionPoint);
179+
SILInstruction *Alloc = StackLocs[LocNr].Alloc;
180+
InsertionPoint = createDealloc(Alloc, InsertionPoint,
181+
Location.hasValue() ? Location.getValue() : Alloc->getLoc());
179182
changesMade = true;
180183
}
181184
}
@@ -234,7 +237,7 @@ StackNesting::Changes StackNesting::adaptDeallocs() {
234237
CFGChanged = true;
235238
}
236239
InstChanged |= insertDeallocs(Bits, SuccBI->AliveStackLocsAtEntry,
237-
&InsertionBlock->front());
240+
&InsertionBlock->front(), None);
238241
}
239242

240243
// Insert/remove deallocations inside blocks.
@@ -264,7 +267,7 @@ StackNesting::Changes StackNesting::adaptDeallocs() {
264267
// Insert deallocations for all locations which are not alive after
265268
// StackInst but _are_ alive at the StackInst.
266269
InstChanged |= insertDeallocs(StackLocs[BitNr].AliveLocs, Bits,
267-
InsertionPoint);
270+
InsertionPoint, StackInst->getLoc());
268271
Bits |= StackLocs[BitNr].AliveLocs;
269272
}
270273
}

test/SILOptimizer/allocbox_to_stack.sil

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -760,9 +760,9 @@ bb5(%25 : $Error): // Preds: bb4 bb3
760760
// CHECK: bb2:
761761
// CHECK: store
762762
// CHECK: [[STACK2:%[0-9]+]] = alloc_stack $Bool
763-
// CHECK-NEXT: dealloc_stack [[STACK2]]
764-
// CHECK-NEXT: dealloc_stack [[BOX]]
765-
// CHECK-NEXT: dealloc_stack [[STACK1]]
763+
// CHECK-NEXT: dealloc_stack [[STACK2]] : $*Bool, loc "testloc":27:27
764+
// CHECK-NEXT: dealloc_stack [[BOX]] : $*Int, loc "testloc":27:27
765+
// CHECK-NEXT: dealloc_stack [[STACK1]] : $*Bool, loc "testloc":27:27
766766
// CHECK: bb3:
767767
// CHECK-NEXT: tuple
768768
// CHECK-NEXT: return
@@ -784,7 +784,7 @@ bb2:
784784
%3 = load %1a : $*Int
785785
%as2 = alloc_stack $Bool
786786
strong_release %1 : ${ var Int }
787-
dealloc_stack %as2 : $*Bool
787+
dealloc_stack %as2 : $*Bool, loc "testloc":27:27
788788
br bb3
789789

790790
bb3:

0 commit comments

Comments
 (0)