Line data Source code
1 : // Copyright 2017 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "test/cctest/wasm/wasm-atomics-utils.h"
6 : #include "test/common/wasm/wasm-macro-gen.h"
7 :
8 : namespace v8 {
9 : namespace internal {
10 : namespace wasm {
11 : namespace test_run_wasm_atomics {
12 :
13 90 : void RunU32BinOp(ExecutionTier execution_tier, WasmOpcode wasm_op,
14 : Uint32BinOp expected_op) {
15 : EXPERIMENTAL_FLAG_SCOPE(threads);
16 90 : WasmRunner<uint32_t, uint32_t> r(execution_tier);
17 302760 : uint32_t* memory =
18 : r.builder().AddMemoryElems<uint32_t>(kWasmPageSize / sizeof(uint32_t));
19 : r.builder().SetHasSharedMemory();
20 :
21 90 : BUILD(r, WASM_ATOMICS_BINOP(wasm_op, WASM_I32V_1(0), WASM_GET_LOCAL(0),
22 : MachineRepresentation::kWord32));
23 :
24 5310 : FOR_UINT32_INPUTS(i) {
25 5220 : uint32_t initial = *i;
26 307980 : FOR_UINT32_INPUTS(j) {
27 : r.builder().WriteMemory(&memory[0], initial);
28 302760 : CHECK_EQ(initial, r.Call(*j));
29 302760 : uint32_t expected = expected_op(*i, *j);
30 302760 : CHECK_EQ(expected, r.builder().ReadMemory(&memory[0]));
31 : }
32 : }
33 90 : }
34 :
35 : #define TEST_OPERATION(Name) \
36 : WASM_EXEC_TEST(I32Atomic##Name) { \
37 : RunU32BinOp(execution_tier, kExprI32Atomic##Name, Name); \
38 : }
39 28427 : OPERATION_LIST(TEST_OPERATION)
40 : #undef TEST_OPERATION
41 :
42 90 : void RunU16BinOp(ExecutionTier tier, WasmOpcode wasm_op,
43 : Uint16BinOp expected_op) {
44 : EXPERIMENTAL_FLAG_SCOPE(threads);
45 90 : WasmRunner<uint32_t, uint32_t> r(tier);
46 : r.builder().SetHasSharedMemory();
47 7290 : uint16_t* memory =
48 : r.builder().AddMemoryElems<uint16_t>(kWasmPageSize / sizeof(uint16_t));
49 :
50 90 : BUILD(r, WASM_ATOMICS_BINOP(wasm_op, WASM_I32V_1(0), WASM_GET_LOCAL(0),
51 : MachineRepresentation::kWord16));
52 :
53 900 : FOR_UINT16_INPUTS(i) {
54 810 : uint16_t initial = *i;
55 8100 : FOR_UINT16_INPUTS(j) {
56 : r.builder().WriteMemory(&memory[0], initial);
57 14580 : CHECK_EQ(initial, r.Call(*j));
58 7290 : uint16_t expected = expected_op(*i, *j);
59 7290 : CHECK_EQ(expected, r.builder().ReadMemory(&memory[0]));
60 : }
61 : }
62 90 : }
63 :
64 : #define TEST_OPERATION(Name) \
65 : WASM_EXEC_TEST(I32Atomic##Name##16U) { \
66 : RunU16BinOp(execution_tier, kExprI32Atomic##Name##16U, Name); \
67 : }
68 28427 : OPERATION_LIST(TEST_OPERATION)
69 : #undef TEST_OPERATION
70 :
71 90 : void RunU8BinOp(ExecutionTier execution_tier, WasmOpcode wasm_op,
72 : Uint8BinOp expected_op) {
73 : EXPERIMENTAL_FLAG_SCOPE(threads);
74 90 : WasmRunner<uint32_t, uint32_t> r(execution_tier);
75 : r.builder().SetHasSharedMemory();
76 7290 : uint8_t* memory = r.builder().AddMemoryElems<uint8_t>(kWasmPageSize);
77 :
78 90 : BUILD(r, WASM_ATOMICS_BINOP(wasm_op, WASM_I32V_1(0), WASM_GET_LOCAL(0),
79 : MachineRepresentation::kWord8));
80 :
81 900 : FOR_UINT8_INPUTS(i) {
82 810 : uint8_t initial = *i;
83 8100 : FOR_UINT8_INPUTS(j) {
84 : r.builder().WriteMemory(&memory[0], initial);
85 14580 : CHECK_EQ(initial, r.Call(*j));
86 7290 : uint8_t expected = expected_op(*i, *j);
87 7290 : CHECK_EQ(expected, r.builder().ReadMemory(&memory[0]));
88 : }
89 : }
90 90 : }
91 :
92 : #define TEST_OPERATION(Name) \
93 : WASM_EXEC_TEST(I32Atomic##Name##8U) { \
94 : RunU8BinOp(execution_tier, kExprI32Atomic##Name##8U, Name); \
95 : }
96 28427 : OPERATION_LIST(TEST_OPERATION)
97 : #undef TEST_OPERATION
98 :
99 28367 : WASM_EXEC_TEST(I32AtomicCompareExchange) {
100 : EXPERIMENTAL_FLAG_SCOPE(threads);
101 15 : WasmRunner<uint32_t, uint32_t, uint32_t> r(execution_tier);
102 : r.builder().SetHasSharedMemory();
103 50460 : uint32_t* memory =
104 : r.builder().AddMemoryElems<uint32_t>(kWasmPageSize / sizeof(uint32_t));
105 15 : BUILD(r, WASM_ATOMICS_TERNARY_OP(
106 : kExprI32AtomicCompareExchange, WASM_I32V_1(0), WASM_GET_LOCAL(0),
107 : WASM_GET_LOCAL(1), MachineRepresentation::kWord32));
108 :
109 885 : FOR_UINT32_INPUTS(i) {
110 870 : uint32_t initial = *i;
111 51330 : FOR_UINT32_INPUTS(j) {
112 : r.builder().WriteMemory(&memory[0], initial);
113 50460 : CHECK_EQ(initial, r.Call(*i, *j));
114 50460 : uint32_t expected = CompareExchange(initial, *i, *j);
115 50460 : CHECK_EQ(expected, r.builder().ReadMemory(&memory[0]));
116 : }
117 : }
118 15 : }
119 :
120 28367 : WASM_EXEC_TEST(I32AtomicCompareExchange16U) {
121 : EXPERIMENTAL_FLAG_SCOPE(threads);
122 15 : WasmRunner<uint32_t, uint32_t, uint32_t> r(execution_tier);
123 : r.builder().SetHasSharedMemory();
124 1215 : uint16_t* memory =
125 : r.builder().AddMemoryElems<uint16_t>(kWasmPageSize / sizeof(uint16_t));
126 15 : BUILD(r, WASM_ATOMICS_TERNARY_OP(kExprI32AtomicCompareExchange16U,
127 : WASM_I32V_1(0), WASM_GET_LOCAL(0),
128 : WASM_GET_LOCAL(1),
129 : MachineRepresentation::kWord16));
130 :
131 150 : FOR_UINT16_INPUTS(i) {
132 135 : uint16_t initial = *i;
133 1350 : FOR_UINT16_INPUTS(j) {
134 : r.builder().WriteMemory(&memory[0], initial);
135 2430 : CHECK_EQ(initial, r.Call(*i, *j));
136 1215 : uint16_t expected = CompareExchange(initial, *i, *j);
137 1215 : CHECK_EQ(expected, r.builder().ReadMemory(&memory[0]));
138 : }
139 : }
140 15 : }
141 :
142 28367 : WASM_EXEC_TEST(I32AtomicCompareExchange8U) {
143 : EXPERIMENTAL_FLAG_SCOPE(threads);
144 15 : WasmRunner<uint32_t, uint32_t, uint32_t> r(execution_tier);
145 : r.builder().SetHasSharedMemory();
146 1215 : uint8_t* memory = r.builder().AddMemoryElems<uint8_t>(kWasmPageSize);
147 15 : BUILD(r,
148 : WASM_ATOMICS_TERNARY_OP(kExprI32AtomicCompareExchange8U, WASM_I32V_1(0),
149 : WASM_GET_LOCAL(0), WASM_GET_LOCAL(1),
150 : MachineRepresentation::kWord8));
151 :
152 150 : FOR_UINT8_INPUTS(i) {
153 135 : uint8_t initial = *i;
154 1350 : FOR_UINT8_INPUTS(j) {
155 : r.builder().WriteMemory(&memory[0], initial);
156 2430 : CHECK_EQ(initial, r.Call(*i, *j));
157 1215 : uint8_t expected = CompareExchange(initial, *i, *j);
158 1215 : CHECK_EQ(expected, r.builder().ReadMemory(&memory[0]));
159 : }
160 : }
161 15 : }
162 :
163 28367 : WASM_EXEC_TEST(I32AtomicLoad) {
164 : EXPERIMENTAL_FLAG_SCOPE(threads);
165 15 : WasmRunner<uint32_t> r(execution_tier);
166 : r.builder().SetHasSharedMemory();
167 : uint32_t* memory =
168 : r.builder().AddMemoryElems<uint32_t>(kWasmPageSize / sizeof(uint32_t));
169 15 : BUILD(r, WASM_ATOMICS_LOAD_OP(kExprI32AtomicLoad, WASM_ZERO,
170 : MachineRepresentation::kWord32));
171 :
172 885 : FOR_UINT32_INPUTS(i) {
173 870 : uint32_t expected = *i;
174 : r.builder().WriteMemory(&memory[0], expected);
175 870 : CHECK_EQ(expected, r.Call());
176 : }
177 15 : }
178 :
179 28367 : WASM_EXEC_TEST(I32AtomicLoad16U) {
180 : EXPERIMENTAL_FLAG_SCOPE(threads);
181 15 : WasmRunner<uint32_t> r(execution_tier);
182 : r.builder().SetHasSharedMemory();
183 : uint16_t* memory =
184 : r.builder().AddMemoryElems<uint16_t>(kWasmPageSize / sizeof(uint16_t));
185 15 : BUILD(r, WASM_ATOMICS_LOAD_OP(kExprI32AtomicLoad16U, WASM_ZERO,
186 : MachineRepresentation::kWord16));
187 :
188 150 : FOR_UINT16_INPUTS(i) {
189 135 : uint16_t expected = *i;
190 : r.builder().WriteMemory(&memory[0], expected);
191 270 : CHECK_EQ(expected, r.Call());
192 : }
193 15 : }
194 :
195 28367 : WASM_EXEC_TEST(I32AtomicLoad8U) {
196 : EXPERIMENTAL_FLAG_SCOPE(threads);
197 15 : WasmRunner<uint32_t> r(execution_tier);
198 : r.builder().SetHasSharedMemory();
199 : uint8_t* memory = r.builder().AddMemoryElems<uint8_t>(kWasmPageSize);
200 15 : BUILD(r, WASM_ATOMICS_LOAD_OP(kExprI32AtomicLoad8U, WASM_ZERO,
201 : MachineRepresentation::kWord8));
202 :
203 150 : FOR_UINT8_INPUTS(i) {
204 135 : uint8_t expected = *i;
205 : r.builder().WriteMemory(&memory[0], expected);
206 270 : CHECK_EQ(expected, r.Call());
207 : }
208 15 : }
209 :
210 28367 : WASM_EXEC_TEST(I32AtomicStoreLoad) {
211 : EXPERIMENTAL_FLAG_SCOPE(threads);
212 15 : WasmRunner<uint32_t, uint32_t> r(execution_tier);
213 : r.builder().SetHasSharedMemory();
214 870 : uint32_t* memory =
215 : r.builder().AddMemoryElems<uint32_t>(kWasmPageSize / sizeof(uint32_t));
216 :
217 15 : BUILD(r,
218 : WASM_ATOMICS_STORE_OP(kExprI32AtomicStore, WASM_ZERO, WASM_GET_LOCAL(0),
219 : MachineRepresentation::kWord32),
220 : WASM_ATOMICS_LOAD_OP(kExprI32AtomicLoad, WASM_ZERO,
221 : MachineRepresentation::kWord32));
222 :
223 885 : FOR_UINT32_INPUTS(i) {
224 870 : uint32_t expected = *i;
225 870 : CHECK_EQ(expected, r.Call(*i));
226 870 : CHECK_EQ(expected, r.builder().ReadMemory(&memory[0]));
227 : }
228 15 : }
229 :
230 28367 : WASM_EXEC_TEST(I32AtomicStoreLoad16U) {
231 : EXPERIMENTAL_FLAG_SCOPE(threads);
232 15 : WasmRunner<uint32_t, uint32_t> r(execution_tier);
233 : r.builder().SetHasSharedMemory();
234 135 : uint16_t* memory =
235 : r.builder().AddMemoryElems<uint16_t>(kWasmPageSize / sizeof(uint16_t));
236 :
237 15 : BUILD(
238 : r,
239 : WASM_ATOMICS_STORE_OP(kExprI32AtomicStore16U, WASM_ZERO,
240 : WASM_GET_LOCAL(0), MachineRepresentation::kWord16),
241 : WASM_ATOMICS_LOAD_OP(kExprI32AtomicLoad16U, WASM_ZERO,
242 : MachineRepresentation::kWord16));
243 :
244 150 : FOR_UINT16_INPUTS(i) {
245 135 : uint16_t expected = *i;
246 135 : CHECK_EQ(expected, r.Call(*i));
247 135 : CHECK_EQ(expected, r.builder().ReadMemory(&memory[0]));
248 : }
249 15 : }
250 :
251 28367 : WASM_EXEC_TEST(I32AtomicStoreLoad8U) {
252 : EXPERIMENTAL_FLAG_SCOPE(threads);
253 15 : WasmRunner<uint32_t, uint32_t> r(execution_tier);
254 : r.builder().SetHasSharedMemory();
255 135 : uint8_t* memory = r.builder().AddMemoryElems<uint8_t>(kWasmPageSize);
256 :
257 15 : BUILD(r,
258 : WASM_ATOMICS_STORE_OP(kExprI32AtomicStore8U, WASM_ZERO,
259 : WASM_GET_LOCAL(0), MachineRepresentation::kWord8),
260 : WASM_ATOMICS_LOAD_OP(kExprI32AtomicLoad8U, WASM_ZERO,
261 : MachineRepresentation::kWord8));
262 :
263 150 : FOR_UINT8_INPUTS(i) {
264 135 : uint8_t expected = *i;
265 135 : CHECK_EQ(expected, r.Call(*i));
266 135 : CHECK_EQ(*i, r.builder().ReadMemory(&memory[0]));
267 : }
268 15 : }
269 :
270 28367 : WASM_EXEC_TEST(I32AtomicStoreParameter) {
271 : EXPERIMENTAL_FLAG_SCOPE(threads);
272 15 : WasmRunner<uint32_t, uint32_t> r(execution_tier);
273 15 : uint32_t* memory =
274 : r.builder().AddMemoryElems<uint32_t>(kWasmPageSize / sizeof(uint32_t));
275 : r.builder().SetHasSharedMemory();
276 :
277 15 : BUILD(r,
278 : WASM_ATOMICS_STORE_OP(kExprI32AtomicStore, WASM_ZERO, WASM_GET_LOCAL(0),
279 : MachineRepresentation::kWord8),
280 : WASM_ATOMICS_BINOP(kExprI32AtomicAdd, WASM_I32V_1(0), WASM_GET_LOCAL(0),
281 : MachineRepresentation::kWord32));
282 15 : CHECK_EQ(10, r.Call(10));
283 15 : CHECK_EQ(20, r.builder().ReadMemory(&memory[0]));
284 15 : }
285 : } // namespace test_run_wasm_atomics
286 : } // namespace wasm
287 : } // namespace internal
288 85011 : } // namespace v8
|