Skip to content

Commit f8d6efd

Browse files
committed
SimplifyAllocStack: bail if the address of the allocated existential escapes
This is not a problem if the alloc_stack already contains the opened existential. Fixes a miscompile rdar://148253142
1 parent ff14828 commit f8d6efd

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyAllocStack.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,15 @@ private extension AllocStackInst {
257257
case is CheckedCastAddrBranchInst, is UnconditionalCheckedCastAddrInst:
258258
// To construct a new cast instruction we need a formal type.
259259
requiresLegalFormalType = true
260-
fallthrough
261-
case is UncheckedAddrCastInst:
262260
if use != use.instruction.operands[0] {
263261
return nil
264262
}
263+
case is UncheckedAddrCastInst:
264+
if self.type.isExistential {
265+
// Bail if the address of the original existential escapes.
266+
// This is not a problem if the alloc_stack already contains the opened existential.
267+
return nil
268+
}
265269
default:
266270
return nil
267271
}

test/SILOptimizer/simplify_alloc_stack.sil

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,19 @@ bb0(%0 : @owned $C2):
365365
dealloc_stack %1
366366
return %5
367367
}
368+
369+
// CHECK-LABEL: sil [ossa] @dont_replace_existential_with_escaping_addr :
370+
// CHECK: alloc_stack $any P
371+
// CHECK: } // end sil function 'dont_replace_existential_with_escaping_addr'
372+
sil [ossa] @dont_replace_existential_with_escaping_addr : $@convention(thin) <V> (@thick V.Type, @owned C2) -> @out V {
373+
bb0(%0 : $*V, %1 : $@thick V.Type, %2 : @owned $C2):
374+
%114 = alloc_stack $any P
375+
%117 = init_existential_addr %114, $C2
376+
store %2 to [init] %117
377+
%129 = unchecked_addr_cast %114 to $*V
378+
copy_addr %129 to [init] %0
379+
destroy_addr %114
380+
dealloc_stack %114
381+
%137 = tuple ()
382+
return %137
383+
}

0 commit comments

Comments
 (0)