Skip to content

Commit f94168c

Browse files
committed
[AllocBoxToStack] Restore isDeadEnd check.
The rewrite was missing the intentional omission of `dealloc_stack`s corresponding to `[dead_end]` `dealloc_box`es. Add the necessary bridging to get to parity with the original. Without this check, `dealloc_box [dead_end]`s are promoted to `dealloc_stack`s but the memory projected out of such `alloc_box`s need not be valid. rdar://159271158
1 parent 3728c77 commit f94168c

File tree

5 files changed

+12
-2
lines changed

5 files changed

+12
-2
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/AllocBoxToStack.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ private func createAllocStack(for allocBox: AllocBoxInst, flags: Flags, _ contex
365365
if !unboxedType.isTrivial(in: allocBox.parentFunction), !(destroy is DeallocBoxInst) {
366366
builder.createDestroyAddr(address: stackLocation)
367367
}
368+
if let dbi = destroy as? DeallocBoxInst, dbi.isDeadEnd {
369+
// Don't bother to create dealloc_stack instructions in dead-ends.
370+
return
371+
}
368372
builder.createDeallocStack(asi)
369373
}
370374
}

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,9 @@ final public class DeallocRefInst : Instruction, UnaryInstruction, Deallocation
699699

700700
final public class DeallocPartialRefInst : Instruction, Deallocation {}
701701

702-
final public class DeallocBoxInst : Instruction, UnaryInstruction, Deallocation {}
702+
final public class DeallocBoxInst : Instruction, UnaryInstruction, Deallocation {
703+
public var isDeadEnd: Bool { bridged.DeallocBoxInst_isDeadEnd() }
704+
}
703705

704706
final public class DeallocExistentialBoxInst : Instruction, UnaryInstruction, Deallocation {}
705707

include/swift/SIL/SILBridging.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,7 @@ struct BridgedInstruction {
827827
BRIDGED_INLINE bool CopyAddrInst_isInitializationOfDest() const;
828828
BRIDGED_INLINE void CopyAddrInst_setIsTakeOfSrc(bool isTakeOfSrc) const;
829829
BRIDGED_INLINE void CopyAddrInst_setIsInitializationOfDest(bool isInitializationOfDest) const;
830+
BRIDGED_INLINE bool DeallocBoxInst_isDeadEnd() const;
830831
BRIDGED_INLINE bool ExplicitCopyAddrInst_isTakeOfSrc() const;
831832
BRIDGED_INLINE bool ExplicitCopyAddrInst_isInitializationOfDest() const;
832833
BRIDGED_INLINE SwiftInt MarkUninitializedInst_getKind() const;

include/swift/SIL/SILBridgingImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,10 @@ void BridgedInstruction::CopyAddrInst_setIsInitializationOfDest(bool isInitializ
15081508
isInitializationOfDest ? swift::IsInitialization : swift::IsNotInitialization);
15091509
}
15101510

1511+
bool BridgedInstruction::DeallocBoxInst_isDeadEnd() const {
1512+
return getAs<swift::DeallocBoxInst>()->isDeadEnd();
1513+
}
1514+
15111515
bool BridgedInstruction::ExplicitCopyAddrInst_isTakeOfSrc() const {
15121516
return getAs<swift::ExplicitCopyAddrInst>()->isTakeOfSrc();
15131517
}

test/SILOptimizer/allocbox_to_stack_lifetime.sil

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ bb0(%0 : $Int):
5454
// CHECK: [[STACK:%[^,]+]] = alloc_stack
5555
// CHECK: cond_br undef, [[DIE:bb[0-9]+]]
5656
// CHECK: [[DIE]]:
57-
// CHECK-NEXT: dealloc_stack [[STACK]]
5857
// CHECK-NEXT: unreachable
5958
// CHECK-LABEL: } // end sil function 'keep_dead_end'
6059
sil [ossa] @keep_dead_end : $@convention(thin) () -> () {

0 commit comments

Comments
 (0)