Skip to content

[move-only] Change global_addr assignable_but_not_consumable accesses such that they are initialized at end of lifetime. #64434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/SILOptimizer/Mandatory/MoveOnlyAddressCheckerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,9 @@ static bool isInOutDefThatNeedsEndOfFunctionLiveness(MarkMustCheckInst *markedAd

if (isa<RefElementAddrInst>(stripAccessMarkers(operand)))
return true;

if (isa<GlobalAddrInst>(stripAccessMarkers(operand)))
return true;
}

return false;
Expand Down
46 changes: 36 additions & 10 deletions test/Interpreter/moveonly.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,49 @@
// RUN: %target-run-simple-swift(-Xfrontend -sil-verify-all) | %FileCheck %s
// RUN: %target-run-simple-swift(-Xfrontend -sil-verify-all)

// REQUIRES: executable_test

// CHECK: MyInt: 5
@inline(never)
func printInt(_ x: Int) { print("MyInt: \(x)") }
import StdlibUnittest

defer { runAllTests() }

var Tests = TestSuite("MoveOnlyTests")

@_moveOnly
struct FD {
var i = 5
var a = LifetimeTracked(0)

deinit {
printInt(i)
}
}

func main() {
let x = FD()
let _ = x
Tests.test("simple deinit called once") {
do {
let s = FD()
}
expectEqual(0, LifetimeTracked.instances)
}

main()
Tests.test("ref element addr destroyed once") {
class CopyableKlass {
var fd = FD()
}

func assignCopyableKlass(_ x: CopyableKlass) {
x.fd = FD()
}

do {
let x = CopyableKlass()
assignCopyableKlass(x)
}
expectEqual(0, LifetimeTracked.instances)
}

var global = FD()

Tests.test("global destroyed once") {
do {
global = FD()
}
expectEqual(0, LifetimeTracked.instances)
}
26 changes: 26 additions & 0 deletions test/SILOptimizer/moveonly_addresschecker.sil
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,29 @@ bb0(%0 : @guaranteed $ClassContainingMoveOnly):
%11 = tuple ()
return %11 : $()
}

// CHECK: sil [ossa] @test_global_addr_write_use : $@convention(thin) () -> () {
// CHECK: bb0
// CHECK-NEXT: // function_ref
// CHECK-NEXT: function_ref
// CHECK-NEXT: apply
// CHECK-NEXT: global_addr
// CHECK-NEXT: begin_access
// CHECK-NEXT: destroy_addr
// CHECK-NEXT: store
// CHECK-NEXT: end_access
// CHECK-NEXT: tuple ()
// CHECK-NEXT: return
// CHECK: } // end sil function 'test_global_addr_write_use'
sil [ossa] @test_global_addr_write_use : $@convention(thin) () -> () {
bb0:
%9 = function_ref @getNonTrivialStruct : $@convention(thin) () -> @owned NonTrivialStruct
%10 = apply %9() : $@convention(thin) () -> @owned NonTrivialStruct
%0 = global_addr @$s23moveonly_addresschecker9varGlobalAA16NonTrivialStructVvp : $*NonTrivialStruct
%1 = begin_access [modify] [dynamic] %0 : $*NonTrivialStruct
%2 = mark_must_check [assignable_but_not_consumable] %0 : $*NonTrivialStruct
store %10 to [assign] %2 : $*NonTrivialStruct
end_access %1 : $*NonTrivialStruct
%8 = tuple ()
return %8 : $()
}