1 // Copyright (c) 2015-2016 The Khronos Group Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Assembler tests for instructions in the "Control Flow" section of the
16 // SPIR-V spec.
17
18 #include <sstream>
19 #include <string>
20 #include <tuple>
21 #include <vector>
22
23 #include "gmock/gmock.h"
24 #include "test/test_fixture.h"
25 #include "test/unit_spirv.h"
26
27 namespace spvtools {
28 namespace {
29
30 using spvtest::Concatenate;
31 using spvtest::EnumCase;
32 using spvtest::MakeInstruction;
33 using spvtest::TextToBinaryTest;
34 using ::testing::Combine;
35 using ::testing::Eq;
36 using ::testing::TestWithParam;
37 using ::testing::Values;
38 using ::testing::ValuesIn;
39
40 // Test OpSelectionMerge
41
42 using OpSelectionMergeTest = spvtest::TextToBinaryTestBase<
43 TestWithParam<EnumCase<SpvSelectionControlMask>>>;
44
TEST_P(OpSelectionMergeTest,AnySingleSelectionControlMask)45 TEST_P(OpSelectionMergeTest, AnySingleSelectionControlMask) {
46 const std::string input = "OpSelectionMerge %1 " + GetParam().name();
47 EXPECT_THAT(
48 CompiledInstructions(input),
49 Eq(MakeInstruction(SpvOpSelectionMerge, {1, GetParam().value()})));
50 }
51
52 // clang-format off
53 #define CASE(VALUE,NAME) { SpvSelectionControl##VALUE, NAME}
54 INSTANTIATE_TEST_SUITE_P(TextToBinarySelectionMerge, OpSelectionMergeTest,
55 ValuesIn(std::vector<EnumCase<SpvSelectionControlMask>>{
56 CASE(MaskNone, "None"),
57 CASE(FlattenMask, "Flatten"),
58 CASE(DontFlattenMask, "DontFlatten"),
59 }));
60 #undef CASE
61 // clang-format on
62
TEST_F(OpSelectionMergeTest,CombinedSelectionControlMask)63 TEST_F(OpSelectionMergeTest, CombinedSelectionControlMask) {
64 const std::string input = "OpSelectionMerge %1 Flatten|DontFlatten";
65 const uint32_t expected_mask =
66 SpvSelectionControlFlattenMask | SpvSelectionControlDontFlattenMask;
67 EXPECT_THAT(CompiledInstructions(input),
68 Eq(MakeInstruction(SpvOpSelectionMerge, {1, expected_mask})));
69 }
70
TEST_F(OpSelectionMergeTest,WrongSelectionControl)71 TEST_F(OpSelectionMergeTest, WrongSelectionControl) {
72 // Case sensitive: "flatten" != "Flatten" and thus wrong.
73 EXPECT_THAT(CompileFailure("OpSelectionMerge %1 flatten|DontFlatten"),
74 Eq("Invalid selection control operand 'flatten|DontFlatten'."));
75 }
76
77 // Test OpLoopMerge
78
79 using OpLoopMergeTest = spvtest::TextToBinaryTestBase<
80 TestWithParam<std::tuple<spv_target_env, EnumCase<int>>>>;
81
TEST_P(OpLoopMergeTest,AnySingleLoopControlMask)82 TEST_P(OpLoopMergeTest, AnySingleLoopControlMask) {
83 const auto ctrl = std::get<1>(GetParam());
84 std::ostringstream input;
85 input << "OpLoopMerge %merge %continue " << ctrl.name();
86 for (auto num : ctrl.operands()) input << " " << num;
87 EXPECT_THAT(CompiledInstructions(input.str(), std::get<0>(GetParam())),
88 Eq(MakeInstruction(SpvOpLoopMerge, {1, 2, ctrl.value()},
89 ctrl.operands())));
90 }
91
92 #define CASE(VALUE, NAME) \
93 { SpvLoopControl##VALUE, NAME }
94 #define CASE1(VALUE, NAME, PARM) \
95 { \
96 SpvLoopControl##VALUE, NAME, { PARM } \
97 }
98 INSTANTIATE_TEST_SUITE_P(
99 TextToBinaryLoopMerge, OpLoopMergeTest,
100 Combine(Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1),
101 ValuesIn(std::vector<EnumCase<int>>{
102 // clang-format off
103 CASE(MaskNone, "None"),
104 CASE(UnrollMask, "Unroll"),
105 CASE(DontUnrollMask, "DontUnroll"),
106 // clang-format on
107 })));
108
109 INSTANTIATE_TEST_SUITE_P(
110 TextToBinaryLoopMergeV11, OpLoopMergeTest,
111 Combine(Values(SPV_ENV_UNIVERSAL_1_1),
112 ValuesIn(std::vector<EnumCase<int>>{
113 // clang-format off
114 CASE(DependencyInfiniteMask, "DependencyInfinite"),
115 CASE1(DependencyLengthMask, "DependencyLength", 234),
116 {SpvLoopControlUnrollMask|SpvLoopControlDependencyLengthMask,
117 "DependencyLength|Unroll", {33}},
118 // clang-format on
119 })));
120 #undef CASE
121 #undef CASE1
122
TEST_F(OpLoopMergeTest,CombinedLoopControlMask)123 TEST_F(OpLoopMergeTest, CombinedLoopControlMask) {
124 const std::string input = "OpLoopMerge %merge %continue Unroll|DontUnroll";
125 const uint32_t expected_mask =
126 SpvLoopControlUnrollMask | SpvLoopControlDontUnrollMask;
127 EXPECT_THAT(CompiledInstructions(input),
128 Eq(MakeInstruction(SpvOpLoopMerge, {1, 2, expected_mask})));
129 }
130
TEST_F(OpLoopMergeTest,WrongLoopControl)131 TEST_F(OpLoopMergeTest, WrongLoopControl) {
132 EXPECT_THAT(CompileFailure("OpLoopMerge %m %c none"),
133 Eq("Invalid loop control operand 'none'."));
134 }
135
136 // Test OpSwitch
137
TEST_F(TextToBinaryTest,SwitchGoodZeroTargets)138 TEST_F(TextToBinaryTest, SwitchGoodZeroTargets) {
139 EXPECT_THAT(CompiledInstructions("OpSwitch %selector %default"),
140 Eq(MakeInstruction(SpvOpSwitch, {1, 2})));
141 }
142
TEST_F(TextToBinaryTest,SwitchGoodOneTarget)143 TEST_F(TextToBinaryTest, SwitchGoodOneTarget) {
144 EXPECT_THAT(CompiledInstructions("%1 = OpTypeInt 32 0\n"
145 "%2 = OpConstant %1 52\n"
146 "OpSwitch %2 %default 12 %target0"),
147 Eq(Concatenate({MakeInstruction(SpvOpTypeInt, {1, 32, 0}),
148 MakeInstruction(SpvOpConstant, {1, 2, 52}),
149 MakeInstruction(SpvOpSwitch, {2, 3, 12, 4})})));
150 }
151
TEST_F(TextToBinaryTest,SwitchGoodTwoTargets)152 TEST_F(TextToBinaryTest, SwitchGoodTwoTargets) {
153 EXPECT_THAT(
154 CompiledInstructions("%1 = OpTypeInt 32 0\n"
155 "%2 = OpConstant %1 52\n"
156 "OpSwitch %2 %default 12 %target0 42 %target1"),
157 Eq(Concatenate({
158 MakeInstruction(SpvOpTypeInt, {1, 32, 0}),
159 MakeInstruction(SpvOpConstant, {1, 2, 52}),
160 MakeInstruction(SpvOpSwitch, {2, 3, 12, 4, 42, 5}),
161 })));
162 }
163
TEST_F(TextToBinaryTest,SwitchBadMissingSelector)164 TEST_F(TextToBinaryTest, SwitchBadMissingSelector) {
165 EXPECT_THAT(CompileFailure("OpSwitch"),
166 Eq("Expected operand for OpSwitch instruction, but found the end "
167 "of the stream."));
168 }
169
TEST_F(TextToBinaryTest,SwitchBadInvalidSelector)170 TEST_F(TextToBinaryTest, SwitchBadInvalidSelector) {
171 EXPECT_THAT(CompileFailure("OpSwitch 12"),
172 Eq("Expected id to start with %."));
173 }
174
TEST_F(TextToBinaryTest,SwitchBadMissingDefault)175 TEST_F(TextToBinaryTest, SwitchBadMissingDefault) {
176 EXPECT_THAT(CompileFailure("OpSwitch %selector"),
177 Eq("Expected operand for OpSwitch instruction, but found the end "
178 "of the stream."));
179 }
180
TEST_F(TextToBinaryTest,SwitchBadInvalidDefault)181 TEST_F(TextToBinaryTest, SwitchBadInvalidDefault) {
182 EXPECT_THAT(CompileFailure("OpSwitch %selector 12"),
183 Eq("Expected id to start with %."));
184 }
185
TEST_F(TextToBinaryTest,SwitchBadInvalidLiteral)186 TEST_F(TextToBinaryTest, SwitchBadInvalidLiteral) {
187 // The assembler recognizes "OpSwitch %selector %default" as a complete
188 // instruction. Then it tries to parse "%abc" as the start of a new
189 // instruction, but can't since it hits the end of stream.
190 const auto input = R"(%i32 = OpTypeInt 32 0
191 %selector = OpConstant %i32 42
192 OpSwitch %selector %default %abc)";
193 EXPECT_THAT(CompileFailure(input), Eq("Expected '=', found end of stream."));
194 }
195
TEST_F(TextToBinaryTest,SwitchBadMissingTarget)196 TEST_F(TextToBinaryTest, SwitchBadMissingTarget) {
197 EXPECT_THAT(CompileFailure("%1 = OpTypeInt 32 0\n"
198 "%2 = OpConstant %1 52\n"
199 "OpSwitch %2 %default 12"),
200 Eq("Expected operand for OpSwitch instruction, but found the end "
201 "of the stream."));
202 }
203
204 // A test case for an OpSwitch.
205 // It is also parameterized to test encodings OpConstant
206 // integer literals. This can capture both single and multi-word
207 // integer literal tests.
208 struct SwitchTestCase {
209 std::string constant_type_args;
210 std::string constant_value_arg;
211 std::string case_value_arg;
212 std::vector<uint32_t> expected_instructions;
213 };
214
215 using OpSwitchValidTest =
216 spvtest::TextToBinaryTestBase<TestWithParam<SwitchTestCase>>;
217
218 // Tests the encoding of OpConstant literal values, and also
219 // the literal integer cases in an OpSwitch. This can
220 // test both single and multi-word integer literal encodings.
TEST_P(OpSwitchValidTest,ValidTypes)221 TEST_P(OpSwitchValidTest, ValidTypes) {
222 const std::string input = "%1 = OpTypeInt " + GetParam().constant_type_args +
223 "\n"
224 "%2 = OpConstant %1 " +
225 GetParam().constant_value_arg +
226 "\n"
227 "OpSwitch %2 %default " +
228 GetParam().case_value_arg + " %4\n";
229 std::vector<uint32_t> instructions;
230 EXPECT_THAT(CompiledInstructions(input),
231 Eq(GetParam().expected_instructions));
232 }
233
234 // Constructs a SwitchTestCase from the given integer_width, signedness,
235 // constant value string, and expected encoded constant.
MakeSwitchTestCase(uint32_t integer_width,uint32_t integer_signedness,std::string constant_str,std::vector<uint32_t> encoded_constant,std::string case_value_str,std::vector<uint32_t> encoded_case_value)236 SwitchTestCase MakeSwitchTestCase(uint32_t integer_width,
237 uint32_t integer_signedness,
238 std::string constant_str,
239 std::vector<uint32_t> encoded_constant,
240 std::string case_value_str,
241 std::vector<uint32_t> encoded_case_value) {
242 std::stringstream ss;
243 ss << integer_width << " " << integer_signedness;
244 return SwitchTestCase{
245 ss.str(),
246 constant_str,
247 case_value_str,
248 {Concatenate(
249 {MakeInstruction(SpvOpTypeInt,
250 {1, integer_width, integer_signedness}),
251 MakeInstruction(SpvOpConstant,
252 Concatenate({{1, 2}, encoded_constant})),
253 MakeInstruction(SpvOpSwitch,
254 Concatenate({{2, 3}, encoded_case_value, {4}}))})}};
255 }
256
257 INSTANTIATE_TEST_SUITE_P(
258 TextToBinaryOpSwitchValid1Word, OpSwitchValidTest,
259 ValuesIn(std::vector<SwitchTestCase>({
260 MakeSwitchTestCase(32, 0, "42", {42}, "100", {100}),
261 MakeSwitchTestCase(32, 1, "-1", {0xffffffff}, "100", {100}),
262 // SPIR-V 1.0 Rev 1 clarified that for an integer narrower than 32-bits,
263 // its bits will appear in the lower order bits of the 32-bit word, and
264 // a signed integer is sign-extended.
265 MakeSwitchTestCase(7, 0, "127", {127}, "100", {100}),
266 MakeSwitchTestCase(14, 0, "99", {99}, "100", {100}),
267 MakeSwitchTestCase(16, 0, "65535", {65535}, "100", {100}),
268 MakeSwitchTestCase(16, 1, "101", {101}, "100", {100}),
269 // Demonstrate sign extension
270 MakeSwitchTestCase(16, 1, "-2", {0xfffffffe}, "100", {100}),
271 // Hex cases
272 MakeSwitchTestCase(16, 1, "0x7ffe", {0x7ffe}, "0x1234", {0x1234}),
273 MakeSwitchTestCase(16, 1, "0x8000", {0xffff8000}, "0x8100",
274 {0xffff8100}),
275 MakeSwitchTestCase(16, 0, "0x8000", {0x00008000}, "0x8100", {0x8100}),
276 })));
277
278 // NB: The words LOW ORDER bits show up first.
279 INSTANTIATE_TEST_SUITE_P(
280 TextToBinaryOpSwitchValid2Words, OpSwitchValidTest,
281 ValuesIn(std::vector<SwitchTestCase>({
282 MakeSwitchTestCase(33, 0, "101", {101, 0}, "500", {500, 0}),
283 MakeSwitchTestCase(48, 1, "-1", {0xffffffff, 0xffffffff}, "900",
284 {900, 0}),
285 MakeSwitchTestCase(64, 1, "-2", {0xfffffffe, 0xffffffff}, "-5",
286 {0xfffffffb, uint32_t(-1)}),
287 // Hex cases
288 MakeSwitchTestCase(48, 1, "0x7fffffffffff", {0xffffffff, 0x00007fff},
289 "100", {100, 0}),
290 MakeSwitchTestCase(48, 1, "0x800000000000", {0x00000000, 0xffff8000},
291 "0x800000000000", {0x00000000, 0xffff8000}),
292 MakeSwitchTestCase(48, 0, "0x800000000000", {0x00000000, 0x00008000},
293 "0x800000000000", {0x00000000, 0x00008000}),
294 MakeSwitchTestCase(63, 0, "0x500000000", {0, 5}, "12", {12, 0}),
295 MakeSwitchTestCase(64, 0, "0x600000000", {0, 6}, "12", {12, 0}),
296 MakeSwitchTestCase(64, 1, "0x700000123", {0x123, 7}, "12", {12, 0}),
297 })));
298
299 using ControlFlowRoundTripTest = RoundTripTest;
300
TEST_P(ControlFlowRoundTripTest,DisassemblyEqualsAssemblyInput)301 TEST_P(ControlFlowRoundTripTest, DisassemblyEqualsAssemblyInput) {
302 const std::string assembly = GetParam();
303 EXPECT_THAT(EncodeAndDecodeSuccessfully(assembly), Eq(assembly)) << assembly;
304 }
305
306 INSTANTIATE_TEST_SUITE_P(
307 OpSwitchRoundTripUnsignedIntegers, ControlFlowRoundTripTest,
308 ValuesIn(std::vector<std::string>({
309 // Unsigned 16-bit.
310 "%1 = OpTypeInt 16 0\n%2 = OpConstant %1 65535\nOpSwitch %2 %3\n",
311 // Unsigned 32-bit, three non-default cases.
312 "%1 = OpTypeInt 32 0\n%2 = OpConstant %1 123456\n"
313 "OpSwitch %2 %3 100 %4 102 %5 1000000 %6\n",
314 // Unsigned 48-bit, three non-default cases.
315 "%1 = OpTypeInt 48 0\n%2 = OpConstant %1 5000000000\n"
316 "OpSwitch %2 %3 100 %4 102 %5 6000000000 %6\n",
317 // Unsigned 64-bit, three non-default cases.
318 "%1 = OpTypeInt 64 0\n%2 = OpConstant %1 9223372036854775807\n"
319 "OpSwitch %2 %3 100 %4 102 %5 9000000000000000000 %6\n",
320 })));
321
322 INSTANTIATE_TEST_SUITE_P(
323 OpSwitchRoundTripSignedIntegers, ControlFlowRoundTripTest,
324 ValuesIn(std::vector<std::string>{
325 // Signed 16-bit, with two non-default cases
326 "%1 = OpTypeInt 16 1\n%2 = OpConstant %1 32767\n"
327 "OpSwitch %2 %3 99 %4 -102 %5\n",
328 "%1 = OpTypeInt 16 1\n%2 = OpConstant %1 -32768\n"
329 "OpSwitch %2 %3 99 %4 -102 %5\n",
330 // Signed 32-bit, two non-default cases.
331 "%1 = OpTypeInt 32 1\n%2 = OpConstant %1 -123456\n"
332 "OpSwitch %2 %3 100 %4 -123456 %5\n",
333 "%1 = OpTypeInt 32 1\n%2 = OpConstant %1 123456\n"
334 "OpSwitch %2 %3 100 %4 123456 %5\n",
335 // Signed 48-bit, three non-default cases.
336 "%1 = OpTypeInt 48 1\n%2 = OpConstant %1 5000000000\n"
337 "OpSwitch %2 %3 100 %4 -7000000000 %5 6000000000 %6\n",
338 "%1 = OpTypeInt 48 1\n%2 = OpConstant %1 -5000000000\n"
339 "OpSwitch %2 %3 100 %4 -7000000000 %5 6000000000 %6\n",
340 // Signed 64-bit, three non-default cases.
341 "%1 = OpTypeInt 64 1\n%2 = OpConstant %1 9223372036854775807\n"
342 "OpSwitch %2 %3 100 %4 7000000000 %5 -1000000000000000000 %6\n",
343 "%1 = OpTypeInt 64 1\n%2 = OpConstant %1 -9223372036854775808\n"
344 "OpSwitch %2 %3 100 %4 7000000000 %5 -1000000000000000000 %6\n",
345 }));
346
347 using OpSwitchInvalidTypeTestCase =
348 spvtest::TextToBinaryTestBase<TestWithParam<std::string>>;
349
TEST_P(OpSwitchInvalidTypeTestCase,InvalidTypes)350 TEST_P(OpSwitchInvalidTypeTestCase, InvalidTypes) {
351 const std::string input =
352 "%1 = " + GetParam() +
353 "\n"
354 "%3 = OpCopyObject %1 %2\n" // We only care the type of the expression
355 " OpSwitch %3 %default 32 %c\n";
356 EXPECT_THAT(CompileFailure(input),
357 Eq("The selector operand for OpSwitch must be the result of an "
358 "instruction that generates an integer scalar"));
359 }
360
361 // clang-format off
362 INSTANTIATE_TEST_SUITE_P(
363 TextToBinaryOpSwitchInvalidTests, OpSwitchInvalidTypeTestCase,
364 ValuesIn(std::vector<std::string>{
365 {"OpTypeVoid",
366 "OpTypeBool",
367 "OpTypeFloat 32",
368 "OpTypeVector %a 32",
369 "OpTypeMatrix %a 32",
370 "OpTypeImage %a 1D 0 0 0 0 Unknown",
371 "OpTypeSampler",
372 "OpTypeSampledImage %a",
373 "OpTypeArray %a %b",
374 "OpTypeRuntimeArray %a",
375 "OpTypeStruct %a",
376 "OpTypeOpaque \"Foo\"",
377 "OpTypePointer UniformConstant %a",
378 "OpTypeFunction %a %b",
379 "OpTypeEvent",
380 "OpTypeDeviceEvent",
381 "OpTypeReserveId",
382 "OpTypeQueue",
383 "OpTypePipe ReadOnly",
384
385 // Skip OpTypeForwardPointer because it doesn't even produce a result
386 // ID.
387
388 // At least one thing that isn't a type at all
389 "OpNot %a %b"
390 },
391 }));
392 // clang-format on
393
394 using OpKillTest = spvtest::TextToBinaryTest;
395
396 INSTANTIATE_TEST_SUITE_P(OpKillTest, ControlFlowRoundTripTest,
397 Values("OpKill\n"));
398
TEST_F(OpKillTest,ExtraArgsAssemblyError)399 TEST_F(OpKillTest, ExtraArgsAssemblyError) {
400 const std::string input = "OpKill 1";
401 EXPECT_THAT(CompileFailure(input),
402 Eq("Expected <opcode> or <result-id> at the beginning of an "
403 "instruction, found '1'."));
404 }
405
406 using OpTerminateInvocationTest = spvtest::TextToBinaryTest;
407
408 INSTANTIATE_TEST_SUITE_P(OpTerminateInvocationTest, ControlFlowRoundTripTest,
409 Values("OpTerminateInvocation\n"));
410
TEST_F(OpTerminateInvocationTest,ExtraArgsAssemblyError)411 TEST_F(OpTerminateInvocationTest, ExtraArgsAssemblyError) {
412 const std::string input = "OpTerminateInvocation 1";
413 EXPECT_THAT(CompileFailure(input),
414 Eq("Expected <opcode> or <result-id> at the beginning of an "
415 "instruction, found '1'."));
416 }
417
418 // TODO(dneto): OpPhi
419 // TODO(dneto): OpLoopMerge
420 // TODO(dneto): OpLabel
421 // TODO(dneto): OpBranch
422 // TODO(dneto): OpSwitch
423 // TODO(dneto): OpReturn
424 // TODO(dneto): OpReturnValue
425 // TODO(dneto): OpUnreachable
426 // TODO(dneto): OpLifetimeStart
427 // TODO(dneto): OpLifetimeStop
428
429 } // namespace
430 } // namespace spvtools
431