Skip to content

Commit 19f95eb

Browse files
authored
Merge pull request #40325 from eeckstein/fix-escape-analysis
EscapeAnalysis: fix a bug which results in not detecting an escape through an unowned reference.
2 parents ac6c08f + 4aae415 commit 19f95eb

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

lib/SILOptimizer/Analysis/EscapeAnalysis.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,21 +202,22 @@ SILValue EscapeAnalysis::getPointerBase(SILValue value) {
202202
case ValueKind::RefToRawPointerInst:
203203
case ValueKind::RefToBridgeObjectInst:
204204
case ValueKind::BridgeObjectToRefInst:
205+
return cast<SingleValueInstruction>(value)->getOperand(0);
206+
205207
case ValueKind::UnconditionalCheckedCastInst:
208+
case ValueKind::UncheckedAddrCastInst:
206209
// DO NOT use LOADABLE_REF_STORAGE because unchecked references don't have
207210
// retain/release instructions that trigger the 'default' case.
208211
#define ALWAYS_OR_SOMETIMES_LOADABLE_CHECKED_REF_STORAGE(Name, ...) \
209212
case ValueKind::RefTo##Name##Inst: \
210213
case ValueKind::Name##ToRefInst:
211214
#include "swift/AST/ReferenceStorage.def"
212-
return cast<SingleValueInstruction>(value)->getOperand(0);
213-
214-
case ValueKind::UncheckedAddrCastInst: {
215-
auto *uac = cast<UncheckedAddrCastInst>(value);
216-
SILValue op = uac->getOperand();
215+
{
216+
auto *svi = cast<SingleValueInstruction>(value);
217+
SILValue op = svi->getOperand(0);
217218
SILType srcTy = op->getType().getObjectType();
218219
SILType destTy = value->getType().getObjectType();
219-
SILFunction *f = uac->getFunction();
220+
SILFunction *f = svi->getFunction();
220221
// If the source and destination of the cast don't agree on being a pointer,
221222
// we bail. Otherwise we would miss important edges in the connection graph:
222223
// e.g. loads of non-pointers are ignored, while it could be an escape of

test/SILOptimizer/stack_promotion.sil

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,3 +854,24 @@ bb1(%4 : $XX):
854854
bb2(%5 : $Error):
855855
throw %5 : $Error
856856
}
857+
858+
final class UnownedLink {
859+
@_hasStorage unowned var link: @sil_unowned UnownedLink
860+
}
861+
862+
// CHECK-LABEL: sil @dont_promote_escaping_through_unowned
863+
// CHECK: alloc_ref $UnownedLink
864+
// CHECK-NOT: dealloc_ref
865+
// CHECK-LABEL: } // end sil function 'dont_promote_escaping_through_unowned'
866+
sil @dont_promote_escaping_through_unowned : $@convention(method) (@guaranteed UnownedLink) -> () {
867+
bb0(%0 : $UnownedLink):
868+
%298 = alloc_ref $UnownedLink
869+
%20 = ref_to_unowned %298 : $UnownedLink to $@sil_unowned UnownedLink
870+
%44 = ref_element_addr %0 : $UnownedLink, #UnownedLink.link
871+
%45 = begin_access [modify] [dynamic] %44 : $*@sil_unowned UnownedLink
872+
store %20 to %45 : $*@sil_unowned UnownedLink
873+
end_access %45 : $*@sil_unowned UnownedLink
874+
strong_release %298 : $UnownedLink
875+
%r = tuple ()
876+
return %r : $()
877+
}

0 commit comments

Comments
 (0)