Skip to content

Commit d97a24a

Browse files
committed
SILSROA: Propagate lexical flag on alloc_stacks.
1 parent 02ec0cd commit d97a24a

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

lib/SILOptimizer/Transforms/SILSROA.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ createAllocas(llvm::SmallVector<AllocStackInst *, 4> &NewAllocations) {
202202
// TODO: Add op_fragment support for tuple type
203203
for (unsigned EltNo : indices(TT->getElementTypes())) {
204204
SILType EltTy = Type.getTupleElementType(EltNo);
205-
NewAllocations.push_back(
206-
B.createAllocStack(Loc, EltTy, {}, AI->hasDynamicLifetime()));
205+
NewAllocations.push_back(B.createAllocStack(
206+
Loc, EltTy, {}, AI->hasDynamicLifetime(), AI->isLexical()));
207207
}
208208
} else {
209209
assert(SD && "SD should not be null since either it or TT must be set at "
@@ -218,7 +218,7 @@ createAllocas(llvm::SmallVector<AllocStackInst *, 4> &NewAllocations) {
218218

219219
NewAllocations.push_back(B.createAllocStack(
220220
Loc, Type.getFieldType(VD, M, TypeExpansionContext(B.getFunction())),
221-
NewDebugVarInfo, AI->hasDynamicLifetime()));
221+
NewDebugVarInfo, AI->hasDynamicLifetime(), AI->isLexical()));
222222
}
223223
}
224224
}

test/SILOptimizer/sroa_lifetime.sil

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// RUN: %target-sil-opt -enable-sil-verify-all -sroa %s -enable-experimental-lexical-lifetimes | %FileCheck %s
2+
3+
sil_stage canonical
4+
5+
import Builtin
6+
import Swift
7+
8+
///////////////////////////////
9+
// Struct with Scalar Fields //
10+
///////////////////////////////
11+
12+
struct S1 {
13+
var x : Builtin.Int1
14+
var y : Builtin.Int32
15+
var z : Builtin.Int64
16+
}
17+
18+
sil [ossa] @use_int32 : $@convention(thin) (Builtin.Int32) -> ()
19+
20+
// CHECK-LABEL: sil [ossa] @struct_with_scalar_fields : $@convention(thin) (S1) -> ()
21+
// CHECK: bb0([[VAR_0:%[0-9]+]] : $S1):
22+
23+
// CHECK-NEXT: [[VAR_1:%[0-9]+]] = alloc_stack [lexical] $Builtin.Int1
24+
// CHECK-NEXT: [[VAR_2:%[0-9]+]] = alloc_stack [lexical] $Builtin.Int32
25+
// CHECK-NEXT: [[VAR_3:%[0-9]+]] = alloc_stack [lexical] $Builtin.Int64
26+
// CHECK-NEXT: ([[VAR_4:%[0-9]+]], [[VAR_6:%[0-9]+]], [[VAR_8:%[0-9]+]]) = destructure_struct [[VAR_0]]
27+
// CHECK-NEXT: store [[VAR_4]] to [trivial] [[VAR_1]] : $*Builtin.Int1
28+
// CHECK-NEXT: store [[VAR_6]] to [trivial] [[VAR_2]] : $*Builtin.Int32
29+
// CHECK-NEXT: store [[VAR_8]] to [trivial] [[VAR_3]] : $*Builtin.Int64
30+
31+
// CHECK-NEXT: function_ref
32+
// CHECK-NEXT: [[VAR_10:%[0-9]+]] = function_ref @use_int32 : $@convention(thin) (Builtin.Int32) -> ()
33+
// CHECK-NEXT: [[VAR_11:%[0-9]+]] = load [trivial] [[VAR_2]] : $*Builtin.Int32
34+
// CHECK-NEXT: [[VAR_12:%[0-9]+]] = apply [[VAR_10]]([[VAR_11]]) : $@convention(thin) (Builtin.Int32) -> ()
35+
// CHECK-NEXT: [[VAR_13:%[0-9]+]] = load [trivial] [[VAR_1]] : $*Builtin.Int1
36+
// CHECK-NEXT: [[VAR_14:%[0-9]+]] = load [trivial] [[VAR_2]] : $*Builtin.Int32
37+
// CHECK-NEXT: [[VAR_15:%[0-9]+]] = load [trivial] [[VAR_3]] : $*Builtin.Int64
38+
// CHECK-NEXT: [[VAR_16:%[0-9]+]] = struct $S1 ([[VAR_13]] : $Builtin.Int1, [[VAR_14]] : $Builtin.Int32, [[VAR_15]] : $Builtin.Int64)
39+
40+
// CHECK: dealloc_stack [[VAR_3]] : $*Builtin.Int64
41+
// CHECK: dealloc_stack [[VAR_2]] : $*Builtin.Int32
42+
// CHECK: dealloc_stack [[VAR_1]] : $*Builtin.Int1
43+
44+
// CHECK: [[VAR_20:%[0-9]+]] = tuple ()
45+
// CHECK: return [[VAR_20]] : $()
46+
sil [ossa] @struct_with_scalar_fields : $@convention(thin) (S1) -> () {
47+
bb0(%0 : $S1):
48+
%1 = alloc_stack [lexical] $S1
49+
debug_value %1 : $*S1 // should not prevent the optimization
50+
debug_value %1 : $*S1 // should not prevent the optimization
51+
store %0 to [trivial] %1 : $*S1
52+
%2 = function_ref @use_int32 : $@convention(thin) (Builtin.Int32) -> ()
53+
%3 = struct_element_addr %1 : $*S1, #S1.y
54+
%4 = load [trivial] %3 : $*Builtin.Int32
55+
apply %2(%4) : $@convention(thin) (Builtin.Int32) -> ()
56+
%5 = load [trivial] %1 : $*S1
57+
dealloc_stack %1 : $*S1
58+
%6 = tuple ()
59+
return %6 : $()
60+
}
61+

0 commit comments

Comments
 (0)