Skip to content

Commit a54a8dd

Browse files
committed
[SIL] Key consume addr checking off var_decl attr.
Previously, the lexical attribute on allock_stack instructions was used. This doesn't work for values without lexical lifetimes which are consumed, e.g. stdlib CoW types. Here, the new var_decl attribute on alloc_stack is keyed off of instead. This flag encodes exactly that a value corresponds to a source-level VarDecl, which is the condition under which checking needs to run.
1 parent 08a832b commit a54a8dd

File tree

5 files changed

+24
-19
lines changed

5 files changed

+24
-19
lines changed

lib/SILOptimizer/Mandatory/ConsumeOperatorCopyableAddressesChecker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2475,8 +2475,8 @@ class ConsumeOperatorCopyableAddressesCheckerPass
24752475
++ii;
24762476

24772477
if (auto *asi = dyn_cast<AllocStackInst>(inst)) {
2478-
// Only check lexical alloc_stack that were not emitted as vars.
2479-
if (asi->isLexical()) {
2478+
// Only check var_decl alloc_stack insts.
2479+
if (asi->isFromVarDecl()) {
24802480
LLVM_DEBUG(llvm::dbgs() << "Found lexical alloc_stack: " << *asi);
24812481
addressesToCheck.insert(asi);
24822482
continue;

test/AutoDiff/SILOptimizer/activity_analysis.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func testNoDerivativeStructProjection(_ s: HasNoDerivativeProperty) -> Float {
1919

2020
// CHECK-LABEL: [AD] Activity info for ${{.*}}testNoDerivativeStructProjection{{.*}} at parameter indices (0) and result indices (0):
2121
// CHECK: [ACTIVE] %0 = argument of bb0 : $HasNoDerivativeProperty
22-
// CHECK: [ACTIVE] %2 = alloc_stack $HasNoDerivativeProperty, var, name "tmp"
22+
// CHECK: [ACTIVE] %2 = alloc_stack [var_decl] $HasNoDerivativeProperty, var, name "tmp"
2323
// CHECK: [ACTIVE] %4 = begin_access [read] [static] %2 : $*HasNoDerivativeProperty
2424
// CHECK: [ACTIVE] %5 = struct_element_addr %4 : $*HasNoDerivativeProperty, #HasNoDerivativeProperty.x
2525
// CHECK: [VARIED] %6 = load [trivial] %5 : $*Float
@@ -43,7 +43,7 @@ func testNondifferentiableTupleElementAddr<T>(_ x: T) -> T {
4343
// CHECK-LABEL: [AD] Activity info for ${{.*}}testNondifferentiableTupleElementAddr{{.*}} at parameter indices (0) and result indices (0):
4444
// CHECK: [ACTIVE] %0 = argument of bb0 : $*T
4545
// CHECK: [ACTIVE] %1 = argument of bb0 : $*T
46-
// CHECK: [ACTIVE] %3 = alloc_stack [lexical] $(Int, Int, (T, Int), Int), var, name "tuple"
46+
// CHECK: [ACTIVE] %3 = alloc_stack [lexical] [var_decl] $(Int, Int, (T, Int), Int), var, name "tuple"
4747
// CHECK: [USEFUL] %4 = tuple_element_addr %3 : $*(Int, Int, (T, Int), Int), 0
4848
// CHECK: [USEFUL] %5 = tuple_element_addr %3 : $*(Int, Int, (T, Int), Int), 1
4949
// CHECK: [ACTIVE] %6 = tuple_element_addr %3 : $*(Int, Int, (T, Int), Int), 2
@@ -77,7 +77,7 @@ func TF_781(_ x: Float, _ y: Float) -> Float {
7777
// CHECK-LABEL: [AD] Activity info for ${{.*}}TF_781{{.*}} at parameter indices (0) and result indices (0)
7878
// CHECK: [ACTIVE] %0 = argument of bb0 : $Float
7979
// CHECK: [USEFUL] %1 = argument of bb0 : $Float
80-
// CHECK: [ACTIVE] %4 = alloc_stack $Float, var, name "result"
80+
// CHECK: [ACTIVE] %4 = alloc_stack [var_decl] $Float, var, name "result"
8181
// CHECK: [ACTIVE] %19 = begin_access [read] [static] %4 : $*Float
8282
// CHECK: [ACTIVE] %20 = load [trivial] %19 : $*Float
8383
// CHECK: [ACTIVE] %23 = apply %22(%20, %0, %18) : $@convention(method) (Float, Float, @thin Float.Type) -> Float
@@ -104,9 +104,9 @@ func TF_954(_ x: Float) -> Float {
104104
// CHECK-LABEL: [AD] Activity info for ${{.*}}TF_954{{.*}} at parameter indices (0) and result indices (0)
105105
// CHECK: bb0:
106106
// CHECK: [ACTIVE] %0 = argument of bb0 : $Float
107-
// CHECK: [ACTIVE] %2 = alloc_stack $Float, var, name "outer"
107+
// CHECK: [ACTIVE] %2 = alloc_stack [var_decl] $Float, var, name "outer"
108108
// CHECK: bb1:
109-
// CHECK: [ACTIVE] %10 = alloc_stack $Float, var, name "inner"
109+
// CHECK: [ACTIVE] %10 = alloc_stack [var_decl] $Float, var, name "inner"
110110
// CHECK: [ACTIVE] %11 = begin_access [read] [static] %2 : $*Float
111111
// CHECK: [USEFUL] %14 = metatype $@thin Float.Type
112112
// CHECK: [ACTIVE] %15 = begin_access [read] [static] %10 : $*Float
@@ -289,7 +289,7 @@ func testArrayUninitializedIntrinsicAddress(_ x: Float, _ y: Float) -> [Float] {
289289
// CHECK-LABEL: [AD] Activity info for ${{.*}}testArrayUninitializedIntrinsicAddress{{.*}} at parameter indices (0, 1) and result indices (0)
290290
// CHECK: [ACTIVE] %0 = argument of bb0 : $Float
291291
// CHECK: [ACTIVE] %1 = argument of bb0 : $Float
292-
// CHECK: [ACTIVE] %4 = alloc_stack $Float, var, name "result"
292+
// CHECK: [ACTIVE] %4 = alloc_stack [var_decl] $Float, var, name "result"
293293
// CHECK: [ACTIVE] %7 = begin_access [read] [static] %4 : $*Float
294294
// CHECK: [ACTIVE] %8 = load [trivial] %7 : $*Float
295295
// CHECK: [NONE] // function_ref static Float.* infix(_:_:)
@@ -448,7 +448,7 @@ func activeInoutArgMutatingMethod(_ x: Mut) -> Mut {
448448

449449
// CHECK-LABEL: [AD] Activity info for ${{.*}}28activeInoutArgMutatingMethodyAA3MutVADF at parameter indices (0) and result indices (0)
450450
// CHECK: [ACTIVE] %0 = argument of bb0 : $Mut
451-
// CHECK: [ACTIVE] %2 = alloc_stack $Mut, var, name "result"
451+
// CHECK: [ACTIVE] %2 = alloc_stack [var_decl] $Mut, var, name "result"
452452
// CHECK: [ACTIVE] %4 = begin_access [read] [static] %2 : $*Mut
453453
// CHECK: [ACTIVE] %5 = load [trivial] %4 : $*Mut
454454
// CHECK: [ACTIVE] %7 = begin_access [modify] [static] %2 : $*Mut
@@ -467,7 +467,7 @@ func activeInoutArgMutatingMethodVar(_ nonactive: inout Mut, _ x: Mut) {
467467
// CHECK-LABEL: [AD] Activity info for ${{.*}}31activeInoutArgMutatingMethodVaryyAA3MutVz_ADtF at parameter indices (0, 1) and result indices (0)
468468
// CHECK: [ACTIVE] %0 = argument of bb0 : $*Mut
469469
// CHECK: [ACTIVE] %1 = argument of bb0 : $Mut
470-
// CHECK: [ACTIVE] %4 = alloc_stack $Mut, var, name "result"
470+
// CHECK: [ACTIVE] %4 = alloc_stack [var_decl] $Mut, var, name "result"
471471
// CHECK: [ACTIVE] %5 = begin_access [read] [static] %0 : $*Mut
472472
// CHECK: [ACTIVE] %8 = begin_access [modify] [static] %4 : $*Mut
473473
// CHECK: [NONE] // function_ref Mut.mutatingMethod(_:)
@@ -488,7 +488,7 @@ func activeInoutArgMutatingMethodTuple(_ nonactive: inout Mut, _ x: Mut) {
488488
// CHECK-LABEL: [AD] Activity info for ${{.*}}33activeInoutArgMutatingMethodTupleyyAA3MutVz_ADtF at parameter indices (0, 1) and result indices (0)
489489
// CHECK: [ACTIVE] %0 = argument of bb0 : $*Mut
490490
// CHECK: [ACTIVE] %1 = argument of bb0 : $Mut
491-
// CHECK: [ACTIVE] %4 = alloc_stack $(Mut, Mut), var, name "result"
491+
// CHECK: [ACTIVE] %4 = alloc_stack [var_decl] $(Mut, Mut), var, name "result"
492492
// CHECK: [ACTIVE] %5 = tuple_element_addr %4 : $*(Mut, Mut), 0
493493
// CHECK: [ACTIVE] %6 = tuple_element_addr %4 : $*(Mut, Mut), 1
494494
// CHECK: [ACTIVE] %7 = begin_access [read] [static] %0 : $*Mut
@@ -515,7 +515,7 @@ func activeInoutArg(_ x: inout Float) -> Float {
515515

516516
// CHECK-LABEL: [AD] Activity info for ${{.*}}activeInoutArg{{.*}} at parameter indices (0) and result indices (0, 1)
517517
// CHECK: [ACTIVE] %0 = argument of bb0 : $*Float
518-
// CHECK: [ACTIVE] %2 = alloc_stack $Float, var, name "result"
518+
// CHECK: [ACTIVE] %2 = alloc_stack [var_decl] $Float, var, name "result"
519519
// CHECK: [ACTIVE] %10 = begin_access [modify] [static] %2 : $*Float
520520
// CHECK: [NONE] // function_ref static Float.+= infix(_:_:)
521521
// CHECK: [NONE] %12 = apply %11(%10, %8, %6) : $@convention(method) (@inout Float, Float, @thin Float.Type) -> ()
@@ -531,7 +531,7 @@ func activeInoutArgNonactiveInitialResult(_ x: inout Float) -> Float {
531531

532532
// CHECK-LABEL: [AD] Activity info for ${{.*}}activeInoutArgNonactiveInitialResult{{.*}} at parameter indices (0) and result indices (0, 1)
533533
// CHECK: [ACTIVE] %0 = argument of bb0 : $*Float
534-
// CHECK: [ACTIVE] %2 = alloc_stack $Float, var, name "result"
534+
// CHECK: [ACTIVE] %2 = alloc_stack [var_decl] $Float, var, name "result"
535535
// CHECK: [NONE] // function_ref Float.init(_builtinIntegerLiteral:)
536536
// CHECK: [USEFUL] %6 = apply %5(%3, %4) : $@convention(method) (Builtin.IntLiteral, @thin Float.Type) -> Float
537537
// CHECK: [USEFUL] %8 = metatype $@thin Float.Type
@@ -592,7 +592,7 @@ func testAccessorCoroutines(_ x: HasCoroutineAccessors) -> HasCoroutineAccessors
592592

593593
// CHECK-LABEL: [AD] Activity info for ${{.*}}testAccessorCoroutines{{.*}} at parameter indices (0) and result indices (0)
594594
// CHECK: [ACTIVE] %0 = argument of bb0 : $HasCoroutineAccessors
595-
// CHECK: [ACTIVE] %2 = alloc_stack $HasCoroutineAccessors, var, name "x"
595+
// CHECK: [ACTIVE] %2 = alloc_stack [var_decl] $HasCoroutineAccessors, var, name "x"
596596
// CHECK: [ACTIVE] %4 = begin_access [read] [static] %2 : $*HasCoroutineAccessors
597597
// CHECK: [ACTIVE] %5 = load [trivial] %4 : $*HasCoroutineAccessors
598598
// CHECK: [NONE] // function_ref HasCoroutineAccessors.computed.read
@@ -625,7 +625,7 @@ func testBeginApplyActiveInoutArgument(array: [Float], x: Float) -> Float {
625625
// CHECK-LABEL: [AD] Activity info for ${{.*}}testBeginApplyActiveInoutArgument{{.*}} at parameter indices (0, 1) and result indices (0)
626626
// CHECK: [ACTIVE] %0 = argument of bb0 : $Array<Float>
627627
// CHECK: [ACTIVE] %1 = argument of bb0 : $Float
628-
// CHECK: [ACTIVE] %4 = alloc_stack $Array<Float>, var, name "array"
628+
// CHECK: [ACTIVE] %4 = alloc_stack [var_decl] $Array<Float>, var, name "array"
629629
// CHECK: [ACTIVE] %5 = copy_value %0 : $Array<Float>
630630
// CHECK: [USEFUL] %7 = integer_literal $Builtin.IntLiteral, 0
631631
// CHECK: [USEFUL] %8 = metatype $@thin Int.Type
@@ -662,7 +662,7 @@ func testBeginApplyActiveButInitiallyNonactiveInoutArgument(x: Float) -> Float {
662662

663663
// CHECK-LABEL: [AD] Activity info for ${{.*}}testBeginApplyActiveButInitiallyNonactiveInoutArgument{{.*}} at parameter indices (0) and result indices (0)
664664
// CHECK: [ACTIVE] %0 = argument of bb0 : $Float
665-
// CHECK: [ACTIVE] %2 = alloc_stack $Array<Float>, var, name "array"
665+
// CHECK: [ACTIVE] %2 = alloc_stack [var_decl] $Array<Float>, var, name "array"
666666
// CHECK: [USEFUL] %3 = integer_literal $Builtin.Word, 1
667667
// CHECK: [NONE] // function_ref _allocateUninitializedArray<A>(_:)
668668
// CHECK: [USEFUL] %5 = apply %4<Float>(%3) : $@convention(thin) <τ_0_0> (Builtin.Word) -> (@owned Array<τ_0_0>, Builtin.RawPointer)
@@ -763,7 +763,7 @@ func testActiveOptional(_ x: Float) -> Float {
763763
// CHECK-LABEL: [AD] Activity info for ${{.*}}testActiveOptional{{.*}} at parameter indices (0) and result indices (0)
764764
// CHECK: bb0:
765765
// CHECK: [ACTIVE] %0 = argument of bb0 : $Float
766-
// CHECK: [ACTIVE] %2 = alloc_stack $Optional<Float>, var, name "maybe"
766+
// CHECK: [ACTIVE] %2 = alloc_stack [var_decl] $Optional<Float>, var, name "maybe"
767767
// CHECK: [USEFUL] %3 = integer_literal $Builtin.IntLiteral, 10
768768
// CHECK: [USEFUL] %4 = metatype $@thin Float.Type
769769
// CHECK: [NONE] // function_ref Float.init(_builtinIntegerLiteral:)

test/DebugInfo/allocstack.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ func main() {
99
// CHECK-SIL-DAG: debug_value {{.*}}: $Int, let, name "x"
1010
// CHECK-DAG: DILocalVariable(name: "x"
1111
let x = 10
12-
// CHECK-SIL-DAG: alloc_stack $Int, var, name "y"
12+
// CHECK-SIL-DAG: alloc_stack [var_decl] $Int, var, name "y"
1313
// CHECK-DAG: DILocalVariable(name: "y"
1414
var y = 10
1515
// The expression x+y may become constant folded.

test/DebugInfo/debug_value_addr.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func use<T>(_ t : T) {}
2828
// CHECK-SIL: sil hidden @$s16debug_value_addr11GenericSelfV1xACyxGx_tcfC : $@convention(method) <T> (@in T, @thin GenericSelf<T>.Type) -> GenericSelf<T> {
2929
// CHECK-SIL: bb0(%0 : $*T, %1 : $@thin GenericSelf<T>.Type):
3030
//
31-
// CHECK-SIL-NEXT: alloc_stack $GenericSelf<T>, var, name "self", implicit, loc {{.*}}
31+
// CHECK-SIL-NEXT: alloc_stack [var_decl] $GenericSelf<T>, var, name "self", implicit, loc {{.*}}
3232
// CHECK-SIL-NEXT: debug_value %0 : $*T, let, name "x", argno 1, expr op_deref, loc {{.*}}
3333
struct GenericSelf<T> {
3434
init(x: T) {

test/SILOptimizer/consume_operator_kills_copyable_loadable_vars.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,11 @@ func multipleCapture2(_ k: Klass) -> () {
708708
print("foo bar")
709709
}
710710

711+
func consumeString() {
712+
var s = "asdf" // expected-warning{{}}
713+
_ = consume s
714+
}
715+
711716
//////////////////////
712717
// Reinit in pieces //
713718
//////////////////////

0 commit comments

Comments
 (0)