Skip to content

Commit 63192ba

Browse files
committed
[AllocBoxToStack] Make alloc_stacks lexical.
When the -enable-experimental-lexical-lifetimes flag is enabled, the alloc_stack instructions which the pass replaces alloc_box instructions with have the lexical attribute.
1 parent 92a304b commit 63192ba

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/SILOptimizer/Transforms/AllocBoxToStack.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,11 +533,15 @@ static bool rewriteAllocBoxAsAllocStack(AllocBoxInst *ABI) {
533533
SILBuilderWithScope Builder(ABI);
534534
assert(ABI->getBoxType()->getLayout()->getFields().size() == 1
535535
&& "rewriting multi-field box not implemented");
536+
bool isLexical = ABI->getFunction()
537+
->getModule()
538+
.getASTContext()
539+
.LangOpts.EnableExperimentalLexicalLifetimes;
536540
auto *ASI = Builder.createAllocStack(
537541
ABI->getLoc(),
538542
getSILBoxFieldType(TypeExpansionContext(*ABI->getFunction()),
539543
ABI->getBoxType(), ABI->getModule().Types, 0),
540-
ABI->getVarInfo(), ABI->hasDynamicLifetime());
544+
ABI->getVarInfo(), ABI->hasDynamicLifetime(), isLexical);
541545

542546
// Transfer a mark_uninitialized if we have one.
543547
SILValue StackBox = ASI;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %target-sil-opt -enable-sil-verify-all %s -allocbox-to-stack -enable-experimental-lexical-lifetimes | %FileCheck %s
2+
3+
import Builtin
4+
5+
struct Int {
6+
var _value: Builtin.Int64
7+
}
8+
9+
struct Bool {
10+
var _value: Builtin.Int1
11+
}
12+
13+
protocol Error {}
14+
15+
// CHECK-LABEL: sil [ossa] @simple_promotion
16+
sil [ossa] @simple_promotion : $@convention(thin) (Int) -> Int {
17+
bb0(%0 : $Int):
18+
%1 = alloc_box ${ var Int }
19+
%1a = project_box %1 : ${ var Int }, 0
20+
store %0 to [trivial] %1a : $*Int
21+
22+
%3 = load [trivial] %1a : $*Int
23+
destroy_value %1 : ${ var Int }
24+
return %3 : $Int
25+
// CHECK: alloc_stack [lexical]
26+
// CHECK-NOT: alloc_box
27+
// CHECK-NOT: destroy_value
28+
// CHECK: return
29+
}
30+

0 commit comments

Comments
 (0)