Skip to content

Commit 5f20a5b

Browse files
Merge pull request swiftlang#83962 from nate-chandler/cherrypick/release/6.2/rdar158149082
6.2: [AllocBoxToStack] Don't destroy in dead-ends.
2 parents 6713168 + 5798e71 commit 5f20a5b

File tree

7 files changed

+471
-23
lines changed

7 files changed

+471
-23
lines changed

include/swift/SIL/SILFunction.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1676,7 +1676,8 @@ class SILFunction
16761676
}
16771677

16781678
/// Verifies the lifetime of memory locations in the function.
1679-
void verifyMemoryLifetime(CalleeCache *calleeCache);
1679+
void verifyMemoryLifetime(CalleeCache *calleeCache,
1680+
DeadEndBlocks *deadEndBlocks);
16801681

16811682
/// Verifies ownership of the function.
16821683
/// Since we don't have complete lifetimes everywhere, computes DeadEndBlocks

lib/SIL/Verifier/MemoryLifetimeVerifier.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212

1313
#define DEBUG_TYPE "sil-memory-lifetime-verifier"
1414
#include "swift/Basic/Assertions.h"
15-
#include "swift/SIL/MemoryLocations.h"
15+
#include "swift/SIL/ApplySite.h"
16+
#include "swift/SIL/BasicBlockDatastructures.h"
17+
#include "swift/SIL/BasicBlockUtils.h"
1618
#include "swift/SIL/BitDataflow.h"
1719
#include "swift/SIL/CalleeCache.h"
20+
#include "swift/SIL/MemoryLocations.h"
1821
#include "swift/SIL/SILBasicBlock.h"
1922
#include "swift/SIL/SILFunction.h"
20-
#include "swift/SIL/ApplySite.h"
21-
#include "swift/SIL/BasicBlockDatastructures.h"
2223
#include "llvm/Support/CommandLine.h"
2324

2425
using namespace swift;
@@ -43,6 +44,7 @@ class MemoryLifetimeVerifier {
4344

4445
SILFunction *function;
4546
CalleeCache *calleeCache;
47+
DeadEndBlocks *deadEndBlocks;
4648
MemoryLocations locations;
4749

4850
/// alloc_stack memory locations which are used for store_borrow.
@@ -140,11 +142,12 @@ class MemoryLifetimeVerifier {
140142
}
141143

142144
public:
143-
MemoryLifetimeVerifier(SILFunction *function, CalleeCache *calleeCache) :
144-
function(function),
145-
calleeCache(calleeCache),
146-
locations(/*handleNonTrivialProjections*/ true,
147-
/*handleTrivialLocations*/ true) {}
145+
MemoryLifetimeVerifier(SILFunction *function, CalleeCache *calleeCache,
146+
DeadEndBlocks *deadEndBlocks)
147+
: function(function), calleeCache(calleeCache),
148+
deadEndBlocks(deadEndBlocks),
149+
locations(/*handleNonTrivialProjections*/ true,
150+
/*handleTrivialLocations*/ true) {}
148151

149152
/// The main entry point to verify the lifetime of all memory locations in
150153
/// the function.
@@ -883,7 +886,12 @@ void MemoryLifetimeVerifier::checkBlock(SILBasicBlock *block, Bits &bits) {
883886
}
884887
case SILInstructionKind::DeallocStackInst: {
885888
SILValue opVal = cast<DeallocStackInst>(&I)->getOperand();
886-
requireBitsClear(bits & nonTrivialLocations, opVal, &I);
889+
if (!deadEndBlocks->isDeadEnd(I.getParent())) {
890+
// TODO: rdar://159311784: Maybe at some point the invariant will be
891+
// enforced that values stored into addresses
892+
// don't leak in dead-ends.
893+
requireBitsClear(bits & nonTrivialLocations, opVal, &I);
894+
}
887895
// Needed to clear any bits of trivial locations (which are not required
888896
// to be zero).
889897
locations.clearBits(bits, opVal);
@@ -973,7 +981,8 @@ void MemoryLifetimeVerifier::verify() {
973981

974982
} // anonymous namespace
975983

976-
void SILFunction::verifyMemoryLifetime(CalleeCache *calleeCache) {
977-
MemoryLifetimeVerifier verifier(this, calleeCache);
984+
void SILFunction::verifyMemoryLifetime(CalleeCache *calleeCache,
985+
DeadEndBlocks *deadEndBlocks) {
986+
MemoryLifetimeVerifier verifier(this, calleeCache, deadEndBlocks);
978987
verifier.verify();
979988
}

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7380,7 +7380,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
73807380

73817381
if (F->hasOwnership() && F->shouldVerifyOwnership() &&
73827382
!mod.getASTContext().hadError()) {
7383-
F->verifyMemoryLifetime(calleeCache);
7383+
F->verifyMemoryLifetime(calleeCache, &getDeadEndBlocks());
73847384
}
73857385
}
73867386

lib/SILOptimizer/Transforms/AllocBoxToStack.cpp

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "swift/SIL/SILArgument.h"
2626
#include "swift/SIL/SILBuilder.h"
2727
#include "swift/SIL/SILCloner.h"
28+
#include "swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h"
29+
#include "swift/SILOptimizer/Analysis/LoopAnalysis.h"
2830
#include "swift/SILOptimizer/PassManager/Passes.h"
2931
#include "swift/SILOptimizer/PassManager/Transforms.h"
3032
#include "swift/SILOptimizer/Utils/InstOptUtils.h"
@@ -601,7 +603,9 @@ static void hoistMarkUnresolvedNonCopyableValueInsts(
601603

602604
/// rewriteAllocBoxAsAllocStack - Replace uses of the alloc_box with a
603605
/// new alloc_stack, but do not delete the alloc_box yet.
604-
static bool rewriteAllocBoxAsAllocStack(AllocBoxInst *ABI) {
606+
static bool rewriteAllocBoxAsAllocStack(AllocBoxInst *ABI,
607+
DeadEndBlocksAnalysis &deba,
608+
SILLoopAnalysis &la) {
605609
LLVM_DEBUG(llvm::dbgs() << "*** Promoting alloc_box to stack: " << *ABI);
606610

607611
SILValue HeapBox = ABI;
@@ -693,9 +697,31 @@ static bool rewriteAllocBoxAsAllocStack(AllocBoxInst *ABI) {
693697
ABI->getBoxType(), ABI->getModule().Types, 0));
694698
auto Loc = CleanupLocation(ABI->getLoc());
695699

700+
auto *deb = deba.get(ABI->getFunction());
696701
for (auto LastRelease : FinalReleases) {
702+
auto *dbi = dyn_cast<DeallocBoxInst>(LastRelease);
703+
if (!dbi && deb->isDeadEnd(LastRelease->getParent()) &&
704+
!la.get(ABI->getFunction())->getLoopFor(LastRelease->getParent())) {
705+
// "Last" releases in dead-end regions may not actually destroy the box
706+
// and consequently may not actually release the stored value. That's
707+
// because values (including boxes) may be leaked along paths into
708+
// dead-end regions. Thus it is invalid to lower such final releases of
709+
// the box to destroy_addr's/dealloc_box's of the stack-promoted storage.
710+
//
711+
// There is one exception: if the alloc_box is in a dead-end loop. In
712+
// that case SIL invariants require that the final releases actually
713+
// destroy the box; otherwise, a box would leak once per loop. To check
714+
// for this, it is sufficient check that the LastRelease is in a dead-end
715+
// loop: if the alloc_box is not in that loop, then the entire loop is in
716+
// the live range, so no release within the loop would be a "final
717+
// release".
718+
//
719+
// None of this applies to dealloc_box instructions which always destroy
720+
// the box.
721+
continue;
722+
}
697723
SILBuilderWithScope Builder(LastRelease);
698-
if (!isa<DeallocBoxInst>(LastRelease)&& !Lowering.isTrivial()) {
724+
if (!dbi && !Lowering.isTrivial()) {
699725
// If we have a mark_unresolved_non_copyable_value use of our stack box,
700726
// we want to destroy that.
701727
SILValue valueToDestroy = StackBox;
@@ -709,7 +735,6 @@ static bool rewriteAllocBoxAsAllocStack(AllocBoxInst *ABI) {
709735
// instruction we found that isn't an explicit dealloc_box.
710736
Builder.emitDestroyAddrAndFold(Loc, valueToDestroy);
711737
}
712-
auto *dbi = dyn_cast<DeallocBoxInst>(LastRelease);
713738
if (dbi && dbi->isDeadEnd()) {
714739
// Don't bother to create dealloc_stack instructions in dead-ends.
715740
continue;
@@ -1265,7 +1290,9 @@ static void rewriteApplySites(AllocBoxToStackState &pass) {
12651290

12661291
/// Clone closure bodies and rewrite partial applies. Returns the number of
12671292
/// alloc_box allocations promoted.
1268-
static unsigned rewritePromotedBoxes(AllocBoxToStackState &pass) {
1293+
static unsigned rewritePromotedBoxes(AllocBoxToStackState &pass,
1294+
DeadEndBlocksAnalysis &deba,
1295+
SILLoopAnalysis &la) {
12691296
// First we'll rewrite any ApplySite that we can to remove
12701297
// the box container pointer from the operands.
12711298
rewriteApplySites(pass);
@@ -1274,7 +1301,7 @@ static unsigned rewritePromotedBoxes(AllocBoxToStackState &pass) {
12741301
auto rend = pass.Promotable.rend();
12751302
for (auto I = pass.Promotable.rbegin(); I != rend; ++I) {
12761303
auto *ABI = *I;
1277-
if (rewriteAllocBoxAsAllocStack(ABI)) {
1304+
if (rewriteAllocBoxAsAllocStack(ABI, deba, la)) {
12781305
++Count;
12791306
ABI->eraseFromParent();
12801307
}
@@ -1299,7 +1326,9 @@ class AllocBoxToStack : public SILFunctionTransform {
12991326
}
13001327

13011328
if (!pass.Promotable.empty()) {
1302-
auto Count = rewritePromotedBoxes(pass);
1329+
auto *deba = getAnalysis<DeadEndBlocksAnalysis>();
1330+
auto *la = getAnalysis<SILLoopAnalysis>();
1331+
auto Count = rewritePromotedBoxes(pass, *deba, *la);
13031332
NumStackPromoted += Count;
13041333
if (Count) {
13051334
if (StackNesting::fixNesting(getFunction()) == StackNesting::Changes::CFG)

test/SIL/memory_lifetime.sil

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,3 +864,11 @@ bb0:
864864
%10 = tuple ()
865865
return %10
866866
}
867+
868+
sil [ossa] @storage_leaked_into_dead_ends : $@convention(thin) () -> () {
869+
%t = apply undef() : $@convention(thin) () -> (@owned T)
870+
%t_addr = alloc_stack $T
871+
store %t to [init] %t_addr
872+
dealloc_stack %t_addr
873+
unreachable
874+
}

0 commit comments

Comments
 (0)