Skip to content

Commit 5964bf2

Browse files
authored
Merge pull request #64434 from gottesmm/pr-82dd301a9c8e2b6fb947d5fa43dbb8dc282ff4e2
[move-only] Change global_addr assignable_but_not_consumable accesses such that they are initialized at end of lifetime.
2 parents f853e8f + 3de646e commit 5964bf2

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

lib/SILOptimizer/Mandatory/MoveOnlyAddressCheckerUtils.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,9 @@ static bool isInOutDefThatNeedsEndOfFunctionLiveness(MarkMustCheckInst *markedAd
497497

498498
if (isa<RefElementAddrInst>(stripAccessMarkers(operand)))
499499
return true;
500+
501+
if (isa<GlobalAddrInst>(stripAccessMarkers(operand)))
502+
return true;
500503
}
501504

502505
return false;

test/Interpreter/moveonly.swift

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,49 @@
1-
// RUN: %target-run-simple-swift(-Xfrontend -sil-verify-all) | %FileCheck %s
1+
// RUN: %target-run-simple-swift(-Xfrontend -sil-verify-all)
22

33
// REQUIRES: executable_test
44

5-
// CHECK: MyInt: 5
6-
@inline(never)
7-
func printInt(_ x: Int) { print("MyInt: \(x)") }
5+
import StdlibUnittest
6+
7+
defer { runAllTests() }
8+
9+
var Tests = TestSuite("MoveOnlyTests")
810

911
@_moveOnly
1012
struct FD {
11-
var i = 5
13+
var a = LifetimeTracked(0)
1214

1315
deinit {
14-
printInt(i)
1516
}
1617
}
1718

18-
func main() {
19-
let x = FD()
20-
let _ = x
19+
Tests.test("simple deinit called once") {
20+
do {
21+
let s = FD()
22+
}
23+
expectEqual(0, LifetimeTracked.instances)
2124
}
2225

23-
main()
26+
Tests.test("ref element addr destroyed once") {
27+
class CopyableKlass {
28+
var fd = FD()
29+
}
30+
31+
func assignCopyableKlass(_ x: CopyableKlass) {
32+
x.fd = FD()
33+
}
34+
35+
do {
36+
let x = CopyableKlass()
37+
assignCopyableKlass(x)
38+
}
39+
expectEqual(0, LifetimeTracked.instances)
40+
}
41+
42+
var global = FD()
43+
44+
Tests.test("global destroyed once") {
45+
do {
46+
global = FD()
47+
}
48+
expectEqual(0, LifetimeTracked.instances)
49+
}

test/SILOptimizer/moveonly_addresschecker.sil

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,3 +493,29 @@ bb0(%0 : @guaranteed $ClassContainingMoveOnly):
493493
%11 = tuple ()
494494
return %11 : $()
495495
}
496+
497+
// CHECK: sil [ossa] @test_global_addr_write_use : $@convention(thin) () -> () {
498+
// CHECK: bb0
499+
// CHECK-NEXT: // function_ref
500+
// CHECK-NEXT: function_ref
501+
// CHECK-NEXT: apply
502+
// CHECK-NEXT: global_addr
503+
// CHECK-NEXT: begin_access
504+
// CHECK-NEXT: destroy_addr
505+
// CHECK-NEXT: store
506+
// CHECK-NEXT: end_access
507+
// CHECK-NEXT: tuple ()
508+
// CHECK-NEXT: return
509+
// CHECK: } // end sil function 'test_global_addr_write_use'
510+
sil [ossa] @test_global_addr_write_use : $@convention(thin) () -> () {
511+
bb0:
512+
%9 = function_ref @getNonTrivialStruct : $@convention(thin) () -> @owned NonTrivialStruct
513+
%10 = apply %9() : $@convention(thin) () -> @owned NonTrivialStruct
514+
%0 = global_addr @$s23moveonly_addresschecker9varGlobalAA16NonTrivialStructVvp : $*NonTrivialStruct
515+
%1 = begin_access [modify] [dynamic] %0 : $*NonTrivialStruct
516+
%2 = mark_must_check [assignable_but_not_consumable] %0 : $*NonTrivialStruct
517+
store %10 to [assign] %2 : $*NonTrivialStruct
518+
end_access %1 : $*NonTrivialStruct
519+
%8 = tuple ()
520+
return %8 : $()
521+
}

0 commit comments

Comments
 (0)