Skip to content

Commit 73c424e

Browse files
authored
Merge pull request #72160 from meg-gupta/motest
Add test to show ~Copyable struct has OwnershipKind::None in SIL
2 parents d49445b + 6fef369 commit 73c424e

File tree

3 files changed

+196
-1
lines changed

3 files changed

+196
-1
lines changed

lib/SIL/IR/SILType.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/SIL/AbstractionPattern.h"
2323
#include "swift/SIL/SILFunctionConventions.h"
2424
#include "swift/SIL/SILModule.h"
25+
#include "swift/SIL/Test.h"
2526
#include "swift/SIL/TypeLowering.h"
2627
#include <tuple>
2728

@@ -1288,3 +1289,21 @@ SILType SILType::removingMoveOnlyWrapperToBoxedType(const SILFunction *fn) {
12881289
bool SILType::isSendable(SILFunction *fn) const {
12891290
return getASTType()->isSendableType();
12901291
}
1292+
1293+
namespace swift::test {
1294+
// Arguments:
1295+
// - SILValue: value
1296+
// Dumps:
1297+
// - message
1298+
static FunctionTest IsSILTrivial("is-sil-trivial", [](auto &function,
1299+
auto &arguments,
1300+
auto &test) {
1301+
SILValue value = arguments.takeValue();
1302+
llvm::outs() << value;
1303+
if (value->getType().isTrivial(value->getFunction())) {
1304+
llvm::outs() << " is trivial\n";
1305+
} else {
1306+
llvm::outs() << " is not trivial\n";
1307+
}
1308+
});
1309+
} // end namespace swift::test

lib/SIL/IR/ValueOwnership.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "swift/SIL/SILVisitor.h"
1413
#include "swift/SIL/SILBuiltinVisitor.h"
1514
#include "swift/SIL/SILModule.h"
15+
#include "swift/SIL/SILVisitor.h"
16+
#include "swift/SIL/Test.h"
1617

1718
using namespace swift;
1819

@@ -685,3 +686,17 @@ ValueOwnershipKind ValueBase::getOwnershipKind() const {
685686
assert(result && "Returned ownership kind invalid on values");
686687
return result;
687688
}
689+
690+
namespace swift::test {
691+
// Arguments:
692+
// - SILValue: value
693+
// Dumps:
694+
// - message
695+
static FunctionTest GetOwnershipKind("get-ownership-kind", [](auto &function,
696+
auto &arguments,
697+
auto &test) {
698+
SILValue value = arguments.takeValue();
699+
llvm::outs() << value;
700+
llvm::outs() << "OwnershipKind: " << value->getOwnershipKind() << "\n";
701+
});
702+
} // end namespace swift::test

test/SIL/moveonly_ownership.sil

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
// RUN: %target-sil-opt -test-runner %s -o /dev/null 2>&1 | %FileCheck %s
2+
3+
sil_stage raw
4+
5+
import Builtin
6+
import Swift
7+
import SwiftShims
8+
9+
@_moveOnly struct MO_DEINIT {
10+
@_hasStorage var value: Int { get set }
11+
deinit
12+
init(value: Int)
13+
}
14+
15+
struct NC_DEINIT : ~Copyable {
16+
@_hasStorage var value: Int { get set }
17+
deinit
18+
init(value: Int)
19+
}
20+
21+
@_moveOnly struct MO {
22+
@_hasStorage var value: Int { get set }
23+
}
24+
25+
struct NC : ~Copyable {
26+
@_hasStorage var value: Int { get set }
27+
}
28+
29+
// CHECK: begin running test 1 of 4 on motest1: is-sil-trivial with: @instruction[2]
30+
// CHECK: %2 = struct $MO_DEINIT (%1 : $Int)
31+
// CHECK: is not trivial
32+
// CHECK: end running test 1 of 4 on motest1: is-sil-trivial with: @instruction[2]
33+
// CHECK: begin running test 2 of 4 on motest1: get-ownership-kind with: @instruction[2]
34+
// CHECK: %2 = struct $MO_DEINIT (%1 : $Int)
35+
// CHECK: OwnershipKind: none
36+
// CHECK: end running test 2 of 4 on motest1: get-ownership-kind with: @instruction[2]
37+
// CHECK: begin running test 3 of 4 on motest1: is-sil-trivial with: @instruction[4]
38+
// CHECK: %4 = move_value [lexical] %2 : $MO_DEINIT
39+
// CHECK: is not trivial
40+
// CHECK: end running test 3 of 4 on motest1: is-sil-trivial with: @instruction[4]
41+
// CHECK: begin running test 4 of 4 on motest1: get-ownership-kind with: @instruction[4]
42+
// CHECK: %4 = move_value [lexical] %2 : $MO_DEINIT
43+
// CHECK: OwnershipKind: owned
44+
// CHECK: end running test 4 of 4 on motest1: get-ownership-kind with: @instruction[4]
45+
sil hidden [ossa] @motest1 : $@convention(thin) () -> () {
46+
[global: read,write,copy,destroy,allocate,deinit_barrier]
47+
bb0:
48+
specify_test "is-sil-trivial @instruction[2]"
49+
specify_test "get-ownership-kind @instruction[2]"
50+
specify_test "is-sil-trivial @instruction[4]"
51+
specify_test "get-ownership-kind @instruction[4]"
52+
%0 = integer_literal $Builtin.Int64, 38
53+
%1 = struct $Int (%0 : $Builtin.Int64)
54+
%2 = struct $MO_DEINIT (%1 : $Int)
55+
debug_value %2 : $MO_DEINIT, let, name "b"
56+
%4 = move_value [lexical] %2 : $MO_DEINIT
57+
destroy_value %4 : $MO_DEINIT
58+
%6 = tuple ()
59+
return %6 : $()
60+
}
61+
62+
// CHECK: begin running test 1 of 4 on motest2: is-sil-trivial with: @instruction[2]
63+
// CHECK: %2 = struct $MO (%1 : $Int)
64+
// CHECK: is not trivial
65+
// CHECK: end running test 1 of 4 on motest2: is-sil-trivial with: @instruction[2]
66+
// CHECK: begin running test 2 of 4 on motest2: get-ownership-kind with: @instruction[2]
67+
// CHECK: %2 = struct $MO (%1 : $Int)
68+
// CHECK: OwnershipKind: none
69+
// CHECK: end running test 2 of 4 on motest2: get-ownership-kind with: @instruction[2]
70+
// CHECK: begin running test 3 of 4 on motest2: is-sil-trivial with: @instruction[4]
71+
// CHECK: %4 = move_value [lexical] %2 : $MO
72+
// CHECK: is not trivial
73+
// CHECK: end running test 3 of 4 on motest2: is-sil-trivial with: @instruction[4]
74+
// CHECK: begin running test 4 of 4 on motest2: get-ownership-kind with: @instruction[4]
75+
// CHECK: %4 = move_value [lexical] %2 : $MO
76+
// CHECK: OwnershipKind: owned
77+
// CHECK: end running test 4 of 4 on motest2: get-ownership-kind with: @instruction[4]
78+
sil hidden [ossa] @motest2 : $@convention(thin) () -> () {
79+
[global: read,write,copy,destroy,allocate,deinit_barrier]
80+
bb0:
81+
specify_test "is-sil-trivial @instruction[2]"
82+
specify_test "get-ownership-kind @instruction[2]"
83+
specify_test "is-sil-trivial @instruction[4]"
84+
specify_test "get-ownership-kind @instruction[4]"
85+
%0 = integer_literal $Builtin.Int64, 38
86+
%1 = struct $Int (%0 : $Builtin.Int64)
87+
%2 = struct $MO (%1 : $Int)
88+
debug_value %2 : $MO, let, name "b"
89+
%4 = move_value [lexical] %2 : $MO
90+
destroy_value %4 : $MO
91+
%6 = tuple ()
92+
return %6 : $()
93+
}
94+
95+
// CHECK: begin running test 1 of 4 on nctest1: is-sil-trivial with: @instruction[2]
96+
// CHECK: %2 = struct $NC_DEINIT (%1 : $Int)
97+
// CHECK: is not trivial
98+
// CHECK: end running test 1 of 4 on nctest1: is-sil-trivial with: @instruction[2]
99+
// CHECK: begin running test 2 of 4 on nctest1: get-ownership-kind with: @instruction[2]
100+
// CHECK: %2 = struct $NC_DEINIT (%1 : $Int)
101+
// CHECK: OwnershipKind: none
102+
// CHECK: end running test 2 of 4 on nctest1: get-ownership-kind with: @instruction[2]
103+
// CHECK: begin running test 3 of 4 on nctest1: is-sil-trivial with: @instruction[4]
104+
// CHECK: %4 = move_value [lexical] %2 : $NC_DEINIT
105+
// CHECK: is not trivial
106+
// CHECK: end running test 3 of 4 on nctest1: is-sil-trivial with: @instruction[4]
107+
// CHECK: begin running test 4 of 4 on nctest1: get-ownership-kind with: @instruction[4]
108+
// CHECK: %4 = move_value [lexical] %2 : $NC_DEINIT
109+
// CHECK: OwnershipKind: owned
110+
// CHECK: end running test 4 of 4 on nctest1: get-ownership-kind with: @instruction[4]
111+
112+
sil hidden [ossa] @nctest1 : $@convention(thin) () -> () {
113+
[global: read,write,copy,destroy,allocate,deinit_barrier]
114+
bb0:
115+
specify_test "is-sil-trivial @instruction[2]"
116+
specify_test "get-ownership-kind @instruction[2]"
117+
specify_test "is-sil-trivial @instruction[4]"
118+
specify_test "get-ownership-kind @instruction[4]"
119+
%0 = integer_literal $Builtin.Int64, 38
120+
%1 = struct $Int (%0 : $Builtin.Int64)
121+
%2 = struct $NC_DEINIT (%1 : $Int)
122+
debug_value %2 : $NC_DEINIT, let, name "b"
123+
%4 = move_value [lexical] %2 : $NC_DEINIT
124+
destroy_value %4 : $NC_DEINIT
125+
%6 = tuple ()
126+
return %6 : $()
127+
}
128+
129+
// CHECK: begin running test 1 of 4 on nctest2: is-sil-trivial with: @instruction[2]
130+
// CHECK: %2 = struct $NC (%1 : $Int)
131+
// CHECK: is not trivial
132+
// CHECK: end running test 1 of 4 on nctest2: is-sil-trivial with: @instruction[2]
133+
// CHECK: begin running test 2 of 4 on nctest2: get-ownership-kind with: @instruction[2]
134+
// CHECK: %2 = struct $NC (%1 : $Int)
135+
// CHECK: OwnershipKind: none
136+
// CHECK: end running test 2 of 4 on nctest2: get-ownership-kind with: @instruction[2]
137+
// CHECK: begin running test 3 of 4 on nctest2: is-sil-trivial with: @instruction[4]
138+
// CHECK: %4 = move_value [lexical] %2 : $NC
139+
// CHECK: is not trivial
140+
// CHECK: end running test 3 of 4 on nctest2: is-sil-trivial with: @instruction[4]
141+
// CHECK: begin running test 4 of 4 on nctest2: get-ownership-kind with: @instruction[4]
142+
// CHECK: %4 = move_value [lexical] %2 : $NC
143+
// CHECK: OwnershipKind: owned
144+
// CHECK: end running test 4 of 4 on nctest2: get-ownership-kind with: @instruction[4]
145+
sil hidden [ossa] @nctest2 : $@convention(thin) () -> () {
146+
[global: read,write,copy,destroy,allocate,deinit_barrier]
147+
bb0:
148+
specify_test "is-sil-trivial @instruction[2]"
149+
specify_test "get-ownership-kind @instruction[2]"
150+
specify_test "is-sil-trivial @instruction[4]"
151+
specify_test "get-ownership-kind @instruction[4]"
152+
%0 = integer_literal $Builtin.Int64, 38
153+
%1 = struct $Int (%0 : $Builtin.Int64)
154+
%2 = struct $NC (%1 : $Int)
155+
debug_value %2 : $NC, let, name "b"
156+
%4 = move_value [lexical] %2 : $NC
157+
destroy_value %4 : $NC
158+
%6 = tuple ()
159+
return %6 : $()
160+
}
161+

0 commit comments

Comments
 (0)