aboutsummaryrefslogtreecommitdiff
path: root/test/opt/strip_atomic_counter_memory_test.cpp
blob: 90daa59c1df33db5b77e3a319827ed31b4d27c9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright (c) 2019 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <vector>

#include "test/opt/pass_fixture.h"
#include "test/opt/pass_utils.h"

namespace spvtools {
namespace opt {
namespace {

typedef std::tuple<std::string, std::string> StripAtomicCounterMemoryParam;

using MemorySemanticsModified =
    PassTest<::testing::TestWithParam<StripAtomicCounterMemoryParam>>;
using NonMemorySemanticsUnmodifiedTest = PassTest<::testing::Test>;

void operator+=(std::vector<const char*>& lhs, const char* rhs) {
  lhs.push_back(rhs);
}

std::string GetConstDecl(std::string val) {
  std::string decl;
  decl += "%uint_" + val + " = OpConstant %uint " + val;
  return decl;
}

std::string GetUnchangedString(std::string(generate_inst)(std::string),
                               std::string val) {
  std::string decl = GetConstDecl(val);
  std::string inst = generate_inst(val);

  std::vector<const char*> result = {
      // clang-format off
              "OpCapability Shader",
              "OpCapability VulkanMemoryModel",
              "OpExtension \"SPV_KHR_vulkan_memory_model\"",
              "OpMemoryModel Logical Vulkan",
              "OpEntryPoint Vertex %1 \"shader\"",
      "%uint = OpTypeInt 32 0",
"%_ptr_Workgroup_uint = OpTypePointer Workgroup %uint",
         "%4 = OpVariable %_ptr_Workgroup_uint Workgroup",
    "%uint_0 = OpConstant %uint 0",
    "%uint_1 = OpConstant %uint 1",
      "%void = OpTypeVoid",
         "%8 = OpTypeFunction %void",
               decl.c_str(),
         "%1 = OpFunction %void None %8",
        "%10 = OpLabel",
               inst.c_str(),
              "OpReturn",
              "OpFunctionEnd"
      // clang-format on
  };
  return JoinAllInsts(result);
}

std::string GetChangedString(std::string(generate_inst)(std::string),
                             std::string orig, std::string changed) {
  std::string orig_decl = GetConstDecl(orig);
  std::string changed_decl = GetConstDecl(changed);
  std::string inst = generate_inst(changed);

  std::vector<const char*> result = {
      // clang-format off
              "OpCapability Shader",
              "OpCapability VulkanMemoryModel",
              "OpExtension \"SPV_KHR_vulkan_memory_model\"",
              "OpMemoryModel Logical Vulkan",
              "OpEntryPoint Vertex %1 \"shader\"",
      "%uint = OpTypeInt 32 0",
"%_ptr_Workgroup_uint = OpTypePointer Workgroup %uint",
         "%4 = OpVariable %_ptr_Workgroup_uint Workgroup",
    "%uint_0 = OpConstant %uint 0",
    "%uint_1 = OpConstant %uint 1",
      "%void = OpTypeVoid",
         "%8 = OpTypeFunction %void",
               orig_decl.c_str() };
  // clang-format on
  if (changed != "0") result += changed_decl.c_str();
  result += "%1 = OpFunction %void None %8";
  result += "%10 = OpLabel";
  result += inst.c_str();
  result += "OpReturn";
  result += "OpFunctionEnd";
  return JoinAllInsts(result);
}

std::tuple<std::string, std::string> GetInputAndExpected(
    std::string(generate_inst)(std::string),
    StripAtomicCounterMemoryParam param) {
  std::string orig = std::get<0>(param);
  std::string changed = std::get<1>(param);
  std::string input = GetUnchangedString(generate_inst, orig);
  std::string expected = orig == changed
                             ? GetUnchangedString(generate_inst, changed)
                             : GetChangedString(generate_inst, orig, changed);
  return std::make_tuple(input, expected);
}

std::string GetOpControlBarrierInst(std::string val) {
  return "OpControlBarrier %uint_1 %uint_1 %uint_" + val;
}

TEST_P(MemorySemanticsModified, OpControlBarrier) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpControlBarrierInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetOpMemoryBarrierInst(std::string val) {
  return "OpMemoryBarrier %uint_1 %uint_" + val;
}

TEST_P(MemorySemanticsModified, OpMemoryBarrier) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpMemoryBarrierInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetOpAtomicLoadInst(std::string val) {
  return "%11 = OpAtomicLoad %uint %4 %uint_1 %uint_" + val;
}

TEST_P(MemorySemanticsModified, OpAtomicLoad) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpAtomicLoadInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetOpAtomicStoreInst(std::string val) {
  return "OpAtomicStore %4 %uint_1 %uint_" + val + " %uint_1";
}

TEST_P(MemorySemanticsModified, OpAtomicStore) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpAtomicStoreInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetOpAtomicExchangeInst(std::string val) {
  return "%11 = OpAtomicExchange %uint %4 %uint_1 %uint_" + val + " %uint_0";
}

TEST_P(MemorySemanticsModified, OpAtomicExchange) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpAtomicExchangeInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetOpAtomicCompareExchangeInst(std::string val) {
  return "%11 = OpAtomicCompareExchange %uint %4 %uint_1 %uint_" + val +
         " %uint_" + val + " %uint_0 %uint_0";
}

TEST_P(MemorySemanticsModified, OpAtomicCompareExchange) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpAtomicCompareExchangeInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetOpAtomicCompareExchangeWeakInst(std::string val) {
  return "%11 = OpAtomicCompareExchangeWeak %uint %4 %uint_1 %uint_" + val +
         " %uint_" + val + " %uint_0 %uint_0";
}

TEST_P(MemorySemanticsModified, OpAtomicCompareExchangeWeak) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpAtomicCompareExchangeWeakInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetOpAtomicIIncrementInst(std::string val) {
  return "%11 = OpAtomicIIncrement %uint %4 %uint_1 %uint_" + val;
}

TEST_P(MemorySemanticsModified, OpAtomicIIncrement) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpAtomicIIncrementInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetOpAtomicIDecrementInst(std::string val) {
  return "%11 = OpAtomicIDecrement %uint %4 %uint_1 %uint_" + val;
}

TEST_P(MemorySemanticsModified, OpAtomicIDecrement) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpAtomicIDecrementInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetOpAtomicIAddInst(std::string val) {
  return "%11 = OpAtomicIAdd %uint %4 %uint_1 %uint_" + val + " %uint_1";
}

TEST_P(MemorySemanticsModified, OpAtomicIAdd) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpAtomicIAddInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetOpAtomicISubInst(std::string val) {
  return "%11 = OpAtomicISub %uint %4 %uint_1 %uint_" + val + " %uint_1";
}

TEST_P(MemorySemanticsModified, OpAtomicISub) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpAtomicISubInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetOpAtomicSMinInst(std::string val) {
  return "%11 = OpAtomicSMin %uint %4 %uint_1 %uint_" + val + " %uint_1";
}

TEST_P(MemorySemanticsModified, OpAtomicSMin) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpAtomicSMinInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetOpAtomicUMinInst(std::string val) {
  return "%11 = OpAtomicUMin %uint %4 %uint_1 %uint_" + val + " %uint_1";
}

TEST_P(MemorySemanticsModified, OpAtomicUMin) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpAtomicUMinInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetOpAtomicSMaxInst(std::string val) {
  return "%11 = OpAtomicSMax %uint %4 %uint_1 %uint_" + val + " %uint_1";
}

TEST_P(MemorySemanticsModified, OpAtomicSMax) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpAtomicSMaxInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetOpAtomicUMaxInst(std::string val) {
  return "%11 = OpAtomicUMax %uint %4 %uint_1 %uint_" + val + " %uint_1";
}

TEST_P(MemorySemanticsModified, OpAtomicUMax) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpAtomicUMaxInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetOpAtomicAndInst(std::string val) {
  return "%11 = OpAtomicAnd %uint %4 %uint_1 %uint_" + val + " %uint_1";
}

TEST_P(MemorySemanticsModified, OpAtomicAnd) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpAtomicAndInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetOpAtomicOrInst(std::string val) {
  return "%11 = OpAtomicOr %uint %4 %uint_1 %uint_" + val + " %uint_1";
}

TEST_P(MemorySemanticsModified, OpAtomicOr) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpAtomicOrInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetOpAtomicXorInst(std::string val) {
  return "%11 = OpAtomicXor %uint %4 %uint_1 %uint_" + val + " %uint_1";
}

TEST_P(MemorySemanticsModified, OpAtomicXor) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpAtomicXorInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetOpAtomicFlagTestAndSetInst(std::string val) {
  return "%11 = OpAtomicFlagTestAndSet %uint %4 %uint_1 %uint_" + val;
}

TEST_P(MemorySemanticsModified, OpAtomicFlagTestAndSet) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpAtomicFlagTestAndSetInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetOpAtomicFlagClearInst(std::string val) {
  return "OpAtomicFlagClear %4 %uint_1 %uint_" + val;
}

TEST_P(MemorySemanticsModified, OpAtomicFlagClear) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpAtomicFlagClearInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetOpMemoryNamedBarrierInst(std::string val) {
  return "OpMemoryNamedBarrier %4 %uint_1 %uint_" + val;
}

TEST_P(MemorySemanticsModified, OpMemoryNamedBarrier) {
  std::string input, expected;
  std::tie(input, expected) =
      GetInputAndExpected(GetOpMemoryNamedBarrierInst, GetParam());
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

// clang-format off
INSTANTIATE_TEST_SUITE_P(
    StripAtomicCounterMemoryTest, MemorySemanticsModified,
    ::testing::ValuesIn(std::vector<StripAtomicCounterMemoryParam>({
       std::make_tuple("1024", "0"),
       std::make_tuple("5", "5"),
       std::make_tuple("1288", "264"),
       std::make_tuple("264", "264")
    })));
// clang-format on

std::string GetNoMemorySemanticsPresentInst(std::string val) {
  return "%11 = OpVariable %_ptr_Workgroup_uint Workgroup %uint_" + val;
}

TEST_F(NonMemorySemanticsUnmodifiedTest, NoMemorySemanticsPresent) {
  std::string input, expected;
  StripAtomicCounterMemoryParam param = std::make_tuple("1288", "1288");
  std::tie(input, expected) =
      GetInputAndExpected(GetNoMemorySemanticsPresentInst, param);
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

std::string GetMemorySemanticsPresentInst(std::string val) {
  return "%11 = OpAtomicIAdd %uint %4 %uint_1 %uint_" + val + " %uint_1288";
}

TEST_F(NonMemorySemanticsUnmodifiedTest, MemorySemanticsPresent) {
  std::string input, expected;
  StripAtomicCounterMemoryParam param = std::make_tuple("1288", "264");
  std::tie(input, expected) =
      GetInputAndExpected(GetMemorySemanticsPresentInst, param);
  SinglePassRunAndCheck<StripAtomicCounterMemoryPass>(input, expected,
                                                      /* skip_nop = */ false);
}

}  // namespace
}  // namespace opt
}  // namespace spvtools