Skip to content

Commit 08a832b

Browse files
committed
[AllocBoxToStack] Transfer var_decl flag.
As with the lexical flag, when creating an alloc_stack corresponding to an alloc_box, transfer the var_decl flag from any begin_borrow users of the box.
1 parent dff0b2e commit 08a832b

16 files changed

+84
-76
lines changed

lib/SILOptimizer/Transforms/AllocBoxToStack.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -624,12 +624,16 @@ static bool rewriteAllocBoxAsAllocStack(AllocBoxInst *ABI) {
624624
&& "rewriting multi-field box not implemented");
625625
auto ty = getSILBoxFieldType(TypeExpansionContext(*ABI->getFunction()),
626626
ABI->getBoxType(), ABI->getModule().Types, 0);
627-
auto isLexical = [&]() -> IsLexical_t {
627+
struct Flags {
628+
IsLexical_t isLexical;
629+
IsFromVarDecl_t isVarDecl;
630+
};
631+
auto getFlags = [&]() -> Flags {
628632
auto &mod = ABI->getFunction()->getModule();
629633
bool lexicalLifetimesEnabled =
630634
mod.getASTContext().SILOpts.supportsLexicalLifetimes(mod);
631-
if (!lexicalLifetimesEnabled)
632-
return IsNotLexical;
635+
bool sawLexical = false;
636+
bool sawVarDecl = false;
633637
// Look for lexical borrows of the alloc_box.
634638
GraphNodeWorklist<Operand *, 4> worklist;
635639
worklist.initializeRange(ABI->getUses());
@@ -642,16 +646,20 @@ static bool rewriteAllocBoxAsAllocStack(AllocBoxInst *ABI) {
642646
worklist.insert(use);
643647
} else if (auto *bbi = dyn_cast<BeginBorrowInst>(use->getUser())) {
644648
if (bbi->isLexical())
645-
return IsLexical;
649+
sawLexical = true;
650+
if (bbi->isFromVarDecl())
651+
sawVarDecl = true;
646652
for (auto *use : bbi->getUses())
647653
worklist.insert(use);
648654
}
649655
}
650-
return IsNotLexical;
656+
return Flags{IsLexical_t(lexicalLifetimesEnabled && sawLexical),
657+
IsFromVarDecl_t(sawVarDecl)};
651658
};
659+
auto flags = getFlags();
652660
auto *ASI = Builder.createAllocStack(
653661
ABI->getLoc(), ty, ABI->getVarInfo(), ABI->hasDynamicLifetime(),
654-
isLexical(), IsNotFromVarDecl, DoesNotUseMoveableValueDebugInfo
662+
flags.isLexical, flags.isVarDecl, DoesNotUseMoveableValueDebugInfo
655663
#ifndef NDEBUG
656664
,
657665
true

test/SILGen/addressors.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct A {
4848

4949
// CHECK-LABEL: sil hidden @$s10addressors5test0yyF : $@convention(thin) () -> () {
5050
func test0() {
51-
// CHECK: [[A:%.*]] = alloc_stack $A
51+
// CHECK: [[A:%.*]] = alloc_stack [var_decl] $A
5252
// CHECK: [[T1:%.*]] = metatype $@thin A.Type
5353
// CHECK: [[T0:%.*]] = function_ref @$s10addressors1AV{{[_0-9a-zA-Z]*}}fC
5454
// CHECK: [[AVAL:%.*]] = apply [[T0]]([[T1]])

test/SILGen/discard.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ final class Wallet {
172172
// CHECK: destroy_value [[DD]] : $Ticket
173173

174174
// CHECK-SIL-LABEL: sil hidden @$s4test6TicketO06changeB08inWalletyAA0E0CSg_tF : $@convention(method) (@guaranteed Optional<Wallet>, @owned Ticket) -> () {
175-
// CHECK-SIL: [[SELF_REF:%.*]] = alloc_stack [lexical] $Ticket, var, name "self", implicit
175+
// CHECK-SIL: [[SELF_REF:%.*]] = alloc_stack [lexical] [var_decl] $Ticket, var, name "self", implicit
176176
// CHECK-SIL: switch_enum {{.*}} : $Optional<Wallet>, case #Optional.some!enumelt: {{.*}}, case #Optional.none!enumelt: [[NO_WALLET_BB:bb[0-9]+]]
177177
//
178178
// >> now we begin the destruction sequence, which involves pattern matching on self to destroy its innards

test/SILGen/reference_bindings.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ func doSomething() {}
3030

3131
// SIL: sil hidden @$s18reference_bindings13testBindToVaryyF : $@convention(thin) () -> () {
3232
// SIL: bb0:
33-
// SIL: [[BOX:%.*]] = alloc_stack $Int, var, name "x"
34-
// SIL: [[INOUT_BOX:%.*]] = alloc_stack $Int, var, name "x2"
33+
// SIL: [[BOX:%.*]] = alloc_stack [var_decl] $Int, var, name "x"
34+
// SIL: [[INOUT_BOX:%.*]] = alloc_stack [var_decl] $Int, var, name "x2"
3535
// SIL: [[ACCESS:%.*]] = begin_access [modify] [static] [[BOX]]
3636
// SIL: store {{%.*}} to [[INOUT_BOX]]
3737
// SIL: [[FUNC:%.*]] = function_ref @$s18reference_bindings11doSomethingyyF : $@convention(thin) () -> ()
@@ -60,7 +60,7 @@ func testBindToVar() {
6060

6161
// SIL-LABEL: sil hidden @$s18reference_bindings15testBindToInOutyySSzF : $@convention(thin) (@inout String) -> () {
6262
// SIL: bb0([[ARG:%.*]] : $*String):
63-
// SIL: [[STACK:%.*]] = alloc_stack $String
63+
// SIL: [[STACK:%.*]] = alloc_stack [var_decl] $String
6464
// SIL: [[ACCESS:%.*]] = begin_access [modify] [static] [[ARG]]
6565
// SIL: [[VAL:%.*]] = load [[ACCESS]]
6666
// SIL: store [[VAL]] to [[STACK]]

test/SILOptimizer/access_enforcement_noescape.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func readRead() {
6060
x = 42
6161
}
6262
// CHECK-LABEL: sil hidden @$s27access_enforcement_noescape8readReadyyF : $@convention(thin) () -> () {
63-
// CHECK: [[ALLOC:%.*]] = alloc_stack $Int, var, name "x"
63+
// CHECK: [[ALLOC:%.*]] = alloc_stack [var_decl] $Int, var, name "x"
6464
// CHECK-NOT: begin_access
6565
// CHECK: apply
6666
// CHECK-LABEL: } // end sil function '$s27access_enforcement_noescape8readReadyyF'

test/SILOptimizer/access_marker_mandatory.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public struct S {
77

88
// CHECK-LABEL: sil [noinline] @$s23access_marker_mandatory5initSyAA1SVSi_yXltF : $@convention(thin) (Int, @guaranteed AnyObject) -> @owned S {
99
// CHECK: bb0(%0 : $Int, %1 : $AnyObject):
10-
// CHECK: [[STK:%.*]] = alloc_stack $S, var, name "s"
10+
// CHECK: [[STK:%.*]] = alloc_stack [var_decl] $S, var, name "s"
1111
// CHECK: cond_br %{{.*}}, bb1, bb2
1212
// CHECK: bb1:
1313
// CHECK: [[WRITE:%.*]] = begin_access [modify] [static] [[STK]] : $*S
@@ -41,7 +41,7 @@ func takeS(_ s: S) {}
4141

4242
// CHECK-LABEL: sil @$s23access_marker_mandatory14modifyAndReadS1oyyXl_tF : $@convention(thin) (@guaranteed AnyObject) -> () {
4343
// CHECK: bb0(%0 : $AnyObject):
44-
// CHECK: [[STK:%.*]] = alloc_stack $S, var, name "s"
44+
// CHECK: [[STK:%.*]] = alloc_stack [var_decl] $S, var, name "s"
4545
// CHECK: [[FINIT:%.*]] = function_ref @$s23access_marker_mandatory5initSyAA1SVSi_yXltF : $@convention(thin) (Int, @guaranteed AnyObject) -> @owned S
4646
// CHECK: [[INITS:%.*]] = apply [[FINIT]](%{{.*}}, %0) : $@convention(thin) (Int, @guaranteed AnyObject) -> @owned S
4747
// CHECK: store [[INITS]] to [[STK]] : $*S
@@ -66,7 +66,7 @@ public func modifyAndReadS(o: AnyObject) {
6666
//
6767
// CHECK-LABEL: sil hidden @$s23access_marker_mandatory19captureStackPromoteSiycyF : $@convention(thin) () -> @owned @callee_guaranteed () -> Int {
6868
// CHECK-LABEL: bb0:
69-
// CHECK: [[STK:%.*]] = alloc_stack $Int, var, name "x"
69+
// CHECK: [[STK:%.*]] = alloc_stack [var_decl] $Int, var, name "x"
7070
// CHECK: [[WRITE:%.*]] = begin_access [modify] [static] [[STK]] : $*Int
7171
// CHECK: store %{{.*}} to [[WRITE]] : $*Int
7272
// CHECK: end_access [[WRITE]] : $*Int

test/SILOptimizer/allocboxtostack_localapply.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ func blackhole<T>(_ x:T) {
88

99
// CHECK-LABEL: sil [noinline] @$s26allocboxtostack_localapply9testapplySiyF :
1010
// CHECK-NOT: alloc_box
11-
// CHECK: [[STK:%.*]] = alloc_stack $Int, var, name "x"
11+
// CHECK: [[STK:%.*]] = alloc_stack [var_decl] $Int, var, name "x"
1212
// CHECK-LABEL: } // end sil function '$s26allocboxtostack_localapply9testapplySiyF'
1313
@inline(never)
1414
public func testapply() -> Int {
@@ -27,7 +27,7 @@ public func testapply() -> Int {
2727

2828
// CHECK-LABEL: sil [noinline] @$s26allocboxtostack_localapply12testtryapplySiyKF :
2929
// CHECK-NOT: alloc_box
30-
// CHECK: [[STK:%.*]] = alloc_stack $Int, var, name "x"
30+
// CHECK: [[STK:%.*]] = alloc_stack [var_decl] $Int, var, name "x"
3131
// CHECK-LABEL: } // end sil function '$s26allocboxtostack_localapply12testtryapplySiyKF'
3232
@inline(never)
3333
public func testtryapply() throws -> Int {
@@ -47,7 +47,7 @@ public func testtryapply() throws -> Int {
4747

4848
// CHECK-LABEL: sil [noinline] @$s26allocboxtostack_localapply16testpartialapplySiyF :
4949
// CHECK-NOT: alloc_box
50-
// CHECK: [[STK:%.*]] = alloc_stack $Int, var, name "x"
50+
// CHECK: [[STK:%.*]] = alloc_stack [var_decl] $Int, var, name "x"
5151
// CHECK-LABEL: } // end sil function '$s26allocboxtostack_localapply16testpartialapplySiyF'
5252
@inline(never)
5353
public func testpartialapply() -> Int {
@@ -66,8 +66,8 @@ public func testpartialapply() -> Int {
6666

6767
// CHECK-LABEL: sil [noinline] @$s26allocboxtostack_localapply12testtwoboxesSiyF :
6868
// CHECK-NOT: alloc_box
69-
// CHECK: [[STK1:%.*]] = alloc_stack $Int, var, name "x"
70-
// CHECK: [[STK2:%.*]] = alloc_stack $Int, var, name "y"
69+
// CHECK: [[STK1:%.*]] = alloc_stack [var_decl] $Int, var, name "x"
70+
// CHECK: [[STK2:%.*]] = alloc_stack [var_decl] $Int, var, name "y"
7171
// CHECK-LABEL: } // end sil function '$s26allocboxtostack_localapply12testtwoboxesSiyF'
7272
@inline(never)
7373
public func testtwoboxes() -> Int {

test/SILOptimizer/definite_init_actor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ actor BoringActor {
149149
// FIXME: the convenience init below is missing a hop after the call to arbitraryAsync (rdar://87485045)
150150

151151
// CHECK-LABEL: sil hidden @$s4test14SingleVarActorC10delegatingACSb_tYacfC : $@convention(method) @async (Bool, @thick SingleVarActor.Type) -> @owned SingleVarActor {
152-
// CHECK: [[SELF_ALLOC:%[0-9]+]] = alloc_stack [lexical] $SingleVarActor, let, name "self", implicit
152+
// CHECK: [[SELF_ALLOC:%[0-9]+]] = alloc_stack [lexical] [var_decl] $SingleVarActor, let, name "self", implicit
153153
// ** first hop is after the call to the synchronous init, right after initializing the allocation.
154154
// CHECK: [[SYNC_FN:%[0-9]+]] = function_ref @$s4test14SingleVarActorC4syncACyt_tcfC : $@convention(method) (@thick SingleVarActor.Type) -> @owned SingleVarActor
155155
// CHECK: [[INIT1:%[0-9]+]] = apply [[SYNC_FN]]({{%[0-9]+}}) : $@convention(method) (@thick SingleVarActor.Type) -> @owned SingleVarActor

0 commit comments

Comments
 (0)