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 #include <sstream>
16 #include <string>
17 #include <tuple>
18 #include <vector>
19
20 #include "gmock/gmock.h"
21 #include "source/spirv_constant.h"
22 #include "test/test_fixture.h"
23 #include "test/unit_spirv.h"
24
25 namespace spvtools {
26 namespace {
27
28 using spvtest::AutoText;
29 using spvtest::ScopedContext;
30 using spvtest::TextToBinaryTest;
31 using ::testing::Combine;
32 using ::testing::Eq;
33 using ::testing::HasSubstr;
34
35 class BinaryToText : public ::testing::Test {
36 public:
BinaryToText()37 BinaryToText()
38 : context(spvContextCreate(SPV_ENV_UNIVERSAL_1_0)), binary(nullptr) {}
~BinaryToText()39 ~BinaryToText() override {
40 spvBinaryDestroy(binary);
41 spvContextDestroy(context);
42 }
43
SetUp()44 void SetUp() override {
45 const char* textStr = R"(
46 OpSource OpenCL_C 12
47 OpMemoryModel Physical64 OpenCL
48 OpSourceExtension "PlaceholderExtensionName"
49 OpEntryPoint Kernel %1 "foo"
50 OpExecutionMode %1 LocalSizeHint 1 1 1
51 %2 = OpTypeVoid
52 %3 = OpTypeBool
53 %4 = OpTypeInt 8 0
54 %5 = OpTypeInt 8 1
55 %6 = OpTypeInt 16 0
56 %7 = OpTypeInt 16 1
57 %8 = OpTypeInt 32 0
58 %9 = OpTypeInt 32 1
59 %10 = OpTypeInt 64 0
60 %11 = OpTypeInt 64 1
61 %12 = OpTypeFloat 16
62 %13 = OpTypeFloat 32
63 %14 = OpTypeFloat 64
64 %15 = OpTypeVector %4 2
65 )";
66 spv_text_t text = {textStr, strlen(textStr)};
67 spv_diagnostic diagnostic = nullptr;
68 spv_result_t error =
69 spvTextToBinary(context, text.str, text.length, &binary, &diagnostic);
70 spvDiagnosticPrint(diagnostic);
71 spvDiagnosticDestroy(diagnostic);
72 ASSERT_EQ(SPV_SUCCESS, error);
73 }
74
TearDown()75 void TearDown() override {
76 spvBinaryDestroy(binary);
77 binary = nullptr;
78 }
79
80 // Compiles the given assembly text, and saves it into 'binary'.
CompileSuccessfully(std::string text)81 void CompileSuccessfully(std::string text) {
82 spvBinaryDestroy(binary);
83 binary = nullptr;
84 spv_diagnostic diagnostic = nullptr;
85 EXPECT_EQ(SPV_SUCCESS, spvTextToBinary(context, text.c_str(), text.size(),
86 &binary, &diagnostic));
87 }
88
89 spv_context context;
90 spv_binary binary;
91 };
92
TEST_F(BinaryToText,Default)93 TEST_F(BinaryToText, Default) {
94 spv_text text = nullptr;
95 spv_diagnostic diagnostic = nullptr;
96 ASSERT_EQ(
97 SPV_SUCCESS,
98 spvBinaryToText(context, binary->code, binary->wordCount,
99 SPV_BINARY_TO_TEXT_OPTION_NONE, &text, &diagnostic));
100 printf("%s", text->str);
101 spvTextDestroy(text);
102 }
103
TEST_F(BinaryToText,Print)104 TEST_F(BinaryToText, Print) {
105 spv_text text = nullptr;
106 spv_diagnostic diagnostic = nullptr;
107 ASSERT_EQ(
108 SPV_SUCCESS,
109 spvBinaryToText(context, binary->code, binary->wordCount,
110 SPV_BINARY_TO_TEXT_OPTION_PRINT, &text, &diagnostic));
111 ASSERT_EQ(text, nullptr);
112 spvTextDestroy(text);
113 }
114
TEST_F(BinaryToText,MissingModule)115 TEST_F(BinaryToText, MissingModule) {
116 spv_text text;
117 spv_diagnostic diagnostic = nullptr;
118 EXPECT_EQ(
119 SPV_ERROR_INVALID_BINARY,
120 spvBinaryToText(context, nullptr, 42, SPV_BINARY_TO_TEXT_OPTION_NONE,
121 &text, &diagnostic));
122 EXPECT_THAT(diagnostic->error, Eq(std::string("Missing module.")));
123 if (diagnostic) {
124 spvDiagnosticPrint(diagnostic);
125 spvDiagnosticDestroy(diagnostic);
126 }
127 }
128
TEST_F(BinaryToText,TruncatedModule)129 TEST_F(BinaryToText, TruncatedModule) {
130 // Make a valid module with zero instructions.
131 CompileSuccessfully("");
132 EXPECT_EQ(SPV_INDEX_INSTRUCTION, binary->wordCount);
133
134 for (size_t length = 0; length < SPV_INDEX_INSTRUCTION; length++) {
135 spv_text text = nullptr;
136 spv_diagnostic diagnostic = nullptr;
137 EXPECT_EQ(
138 SPV_ERROR_INVALID_BINARY,
139 spvBinaryToText(context, binary->code, length,
140 SPV_BINARY_TO_TEXT_OPTION_NONE, &text, &diagnostic));
141 ASSERT_NE(nullptr, diagnostic);
142 std::stringstream expected;
143 expected << "Module has incomplete header: only " << length
144 << " words instead of " << SPV_INDEX_INSTRUCTION;
145 EXPECT_THAT(diagnostic->error, Eq(expected.str()));
146 spvDiagnosticDestroy(diagnostic);
147 }
148 }
149
TEST_F(BinaryToText,InvalidMagicNumber)150 TEST_F(BinaryToText, InvalidMagicNumber) {
151 CompileSuccessfully("");
152 std::vector<uint32_t> damaged_binary(binary->code,
153 binary->code + binary->wordCount);
154 damaged_binary[SPV_INDEX_MAGIC_NUMBER] ^= 123;
155
156 spv_diagnostic diagnostic = nullptr;
157 spv_text text;
158 EXPECT_EQ(
159 SPV_ERROR_INVALID_BINARY,
160 spvBinaryToText(context, damaged_binary.data(), damaged_binary.size(),
161 SPV_BINARY_TO_TEXT_OPTION_NONE, &text, &diagnostic));
162 ASSERT_NE(nullptr, diagnostic);
163 std::stringstream expected;
164 expected << "Invalid SPIR-V magic number '" << std::hex
165 << damaged_binary[SPV_INDEX_MAGIC_NUMBER] << "'.";
166 EXPECT_THAT(diagnostic->error, Eq(expected.str()));
167 spvDiagnosticDestroy(diagnostic);
168 }
169
170 struct FailedDecodeCase {
171 std::string source_text;
172 std::vector<uint32_t> appended_instruction;
173 std::string expected_error_message;
174 };
175
176 using BinaryToTextFail =
177 spvtest::TextToBinaryTestBase<::testing::TestWithParam<FailedDecodeCase>>;
178
TEST_P(BinaryToTextFail,EncodeSuccessfullyDecodeFailed)179 TEST_P(BinaryToTextFail, EncodeSuccessfullyDecodeFailed) {
180 EXPECT_THAT(EncodeSuccessfullyDecodeFailed(GetParam().source_text,
181 GetParam().appended_instruction),
182 Eq(GetParam().expected_error_message));
183 }
184
185 INSTANTIATE_TEST_SUITE_P(
186 InvalidIds, BinaryToTextFail,
187 ::testing::ValuesIn(std::vector<FailedDecodeCase>{
188 {"", spvtest::MakeInstruction(SpvOpTypeVoid, {0}),
189 "Error: Result Id is 0"},
190 {"", spvtest::MakeInstruction(SpvOpConstant, {0, 1, 42}),
191 "Error: Type Id is 0"},
192 {"%1 = OpTypeVoid", spvtest::MakeInstruction(SpvOpTypeVoid, {1}),
193 "Id 1 is defined more than once"},
194 {"%1 = OpTypeVoid\n"
195 "%2 = OpNot %1 %foo",
196 spvtest::MakeInstruction(SpvOpNot, {1, 2, 3}),
197 "Id 2 is defined more than once"},
198 {"%1 = OpTypeVoid\n"
199 "%2 = OpNot %1 %foo",
200 spvtest::MakeInstruction(SpvOpNot, {1, 1, 3}),
201 "Id 1 is defined more than once"},
202 // The following are the two failure cases for
203 // Parser::setNumericTypeInfoForType.
204 {"", spvtest::MakeInstruction(SpvOpConstant, {500, 1, 42}),
205 "Type Id 500 is not a type"},
206 {"%1 = OpTypeInt 32 0\n"
207 "%2 = OpTypeVector %1 4",
208 spvtest::MakeInstruction(SpvOpConstant, {2, 3, 999}),
209 "Type Id 2 is not a scalar numeric type"},
210 }));
211
212 INSTANTIATE_TEST_SUITE_P(
213 InvalidIdsCheckedDuringLiteralCaseParsing, BinaryToTextFail,
214 ::testing::ValuesIn(std::vector<FailedDecodeCase>{
215 {"", spvtest::MakeInstruction(SpvOpSwitch, {1, 2, 3, 4}),
216 "Invalid OpSwitch: selector id 1 has no type"},
217 {"%1 = OpTypeVoid\n",
218 spvtest::MakeInstruction(SpvOpSwitch, {1, 2, 3, 4}),
219 "Invalid OpSwitch: selector id 1 is a type, not a value"},
220 {"%1 = OpConstantTrue !500",
221 spvtest::MakeInstruction(SpvOpSwitch, {1, 2, 3, 4}),
222 "Type Id 500 is not a type"},
223 {"%1 = OpTypeFloat 32\n%2 = OpConstant %1 1.5",
224 spvtest::MakeInstruction(SpvOpSwitch, {2, 3, 4, 5}),
225 "Invalid OpSwitch: selector id 2 is not a scalar integer"},
226 }));
227
TEST_F(TextToBinaryTest,OneInstruction)228 TEST_F(TextToBinaryTest, OneInstruction) {
229 const std::string input = "OpSource OpenCL_C 12\n";
230 EXPECT_EQ(input, EncodeAndDecodeSuccessfully(input));
231 }
232
233 // Exercise the case where an operand itself has operands.
234 // This could detect problems in updating the expected-set-of-operands
235 // list.
TEST_F(TextToBinaryTest,OperandWithOperands)236 TEST_F(TextToBinaryTest, OperandWithOperands) {
237 const std::string input = R"(OpEntryPoint Kernel %1 "foo"
238 OpExecutionMode %1 LocalSizeHint 100 200 300
239 %2 = OpTypeVoid
240 %3 = OpTypeFunction %2
241 %1 = OpFunction %1 None %3
242 )";
243 EXPECT_EQ(input, EncodeAndDecodeSuccessfully(input));
244 }
245
246 using RoundTripInstructionsTest = spvtest::TextToBinaryTestBase<
247 ::testing::TestWithParam<std::tuple<spv_target_env, std::string>>>;
248
TEST_P(RoundTripInstructionsTest,Sample)249 TEST_P(RoundTripInstructionsTest, Sample) {
250 EXPECT_THAT(EncodeAndDecodeSuccessfully(std::get<1>(GetParam()),
251 SPV_BINARY_TO_TEXT_OPTION_NONE,
252 std::get<0>(GetParam())),
253 Eq(std::get<1>(GetParam())));
254 }
255
256 // clang-format off
257 INSTANTIATE_TEST_SUITE_P(
258 NumericLiterals, RoundTripInstructionsTest,
259 // This test is independent of environment, so just test the one.
260 Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
261 SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_3),
262 ::testing::ValuesIn(std::vector<std::string>{
263 "%1 = OpTypeInt 12 0\n%2 = OpConstant %1 1867\n",
264 "%1 = OpTypeInt 12 1\n%2 = OpConstant %1 1867\n",
265 "%1 = OpTypeInt 12 1\n%2 = OpConstant %1 -1867\n",
266 "%1 = OpTypeInt 32 0\n%2 = OpConstant %1 1867\n",
267 "%1 = OpTypeInt 32 1\n%2 = OpConstant %1 1867\n",
268 "%1 = OpTypeInt 32 1\n%2 = OpConstant %1 -1867\n",
269 "%1 = OpTypeInt 64 0\n%2 = OpConstant %1 18446744073709551615\n",
270 "%1 = OpTypeInt 64 1\n%2 = OpConstant %1 9223372036854775807\n",
271 "%1 = OpTypeInt 64 1\n%2 = OpConstant %1 -9223372036854775808\n",
272 // 16-bit floats print as hex floats.
273 "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.ff4p+16\n",
274 "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x1.d2cp-10\n",
275 // 32-bit floats
276 "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -3.125\n",
277 "%1 = OpTypeFloat 32\n%2 = OpConstant %1 0x1.8p+128\n", // NaN
278 "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0x1.0002p+128\n", // NaN
279 "%1 = OpTypeFloat 32\n%2 = OpConstant %1 0x1p+128\n", // Inf
280 "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0x1p+128\n", // -Inf
281 // 64-bit floats
282 "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -3.125\n",
283 "%1 = OpTypeFloat 64\n%2 = OpConstant %1 0x1.ffffffffffffap-1023\n", // small normal
284 "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1.ffffffffffffap-1023\n",
285 "%1 = OpTypeFloat 64\n%2 = OpConstant %1 0x1.8p+1024\n", // NaN
286 "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1.0002p+1024\n", // NaN
287 "%1 = OpTypeFloat 64\n%2 = OpConstant %1 0x1p+1024\n", // Inf
288 "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1p+1024\n", // -Inf
289 })));
290 // clang-format on
291
292 INSTANTIATE_TEST_SUITE_P(
293 MemoryAccessMasks, RoundTripInstructionsTest,
294 Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
295 SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_3),
296 ::testing::ValuesIn(std::vector<std::string>{
297 "OpStore %1 %2\n", // 3 words long.
298 "OpStore %1 %2 None\n", // 4 words long, explicit final 0.
299 "OpStore %1 %2 Volatile\n",
300 "OpStore %1 %2 Aligned 8\n",
301 "OpStore %1 %2 Nontemporal\n",
302 // Combinations show the names from LSB to MSB
303 "OpStore %1 %2 Volatile|Aligned 16\n",
304 "OpStore %1 %2 Volatile|Nontemporal\n",
305 "OpStore %1 %2 Volatile|Aligned|Nontemporal 32\n",
306 })));
307
308 INSTANTIATE_TEST_SUITE_P(
309 FPFastMathModeMasks, RoundTripInstructionsTest,
310 Combine(
311 ::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
312 SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_3),
313 ::testing::ValuesIn(std::vector<std::string>{
314 "OpDecorate %1 FPFastMathMode None\n",
315 "OpDecorate %1 FPFastMathMode NotNaN\n",
316 "OpDecorate %1 FPFastMathMode NotInf\n",
317 "OpDecorate %1 FPFastMathMode NSZ\n",
318 "OpDecorate %1 FPFastMathMode AllowRecip\n",
319 "OpDecorate %1 FPFastMathMode Fast\n",
320 // Combinations show the names from LSB to MSB
321 "OpDecorate %1 FPFastMathMode NotNaN|NotInf\n",
322 "OpDecorate %1 FPFastMathMode NSZ|AllowRecip\n",
323 "OpDecorate %1 FPFastMathMode NotNaN|NotInf|NSZ|AllowRecip|Fast\n",
324 })));
325
326 INSTANTIATE_TEST_SUITE_P(
327 LoopControlMasks, RoundTripInstructionsTest,
328 Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
329 SPV_ENV_UNIVERSAL_1_3, SPV_ENV_UNIVERSAL_1_2),
330 ::testing::ValuesIn(std::vector<std::string>{
331 "OpLoopMerge %1 %2 None\n",
332 "OpLoopMerge %1 %2 Unroll\n",
333 "OpLoopMerge %1 %2 DontUnroll\n",
334 "OpLoopMerge %1 %2 Unroll|DontUnroll\n",
335 })));
336
337 INSTANTIATE_TEST_SUITE_P(LoopControlMasksV11, RoundTripInstructionsTest,
338 Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_1,
339 SPV_ENV_UNIVERSAL_1_2,
340 SPV_ENV_UNIVERSAL_1_3),
341 ::testing::ValuesIn(std::vector<std::string>{
342 "OpLoopMerge %1 %2 DependencyInfinite\n",
343 "OpLoopMerge %1 %2 DependencyLength 8\n",
344 })));
345
346 INSTANTIATE_TEST_SUITE_P(
347 SelectionControlMasks, RoundTripInstructionsTest,
348 Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
349 SPV_ENV_UNIVERSAL_1_3, SPV_ENV_UNIVERSAL_1_2),
350 ::testing::ValuesIn(std::vector<std::string>{
351 "OpSelectionMerge %1 None\n",
352 "OpSelectionMerge %1 Flatten\n",
353 "OpSelectionMerge %1 DontFlatten\n",
354 "OpSelectionMerge %1 Flatten|DontFlatten\n",
355 })));
356
357 INSTANTIATE_TEST_SUITE_P(
358 FunctionControlMasks, RoundTripInstructionsTest,
359 Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
360 SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_3),
361 ::testing::ValuesIn(std::vector<std::string>{
362 "%2 = OpFunction %1 None %3\n",
363 "%2 = OpFunction %1 Inline %3\n",
364 "%2 = OpFunction %1 DontInline %3\n",
365 "%2 = OpFunction %1 Pure %3\n",
366 "%2 = OpFunction %1 Const %3\n",
367 "%2 = OpFunction %1 Inline|Pure|Const %3\n",
368 "%2 = OpFunction %1 DontInline|Const %3\n",
369 })));
370
371 INSTANTIATE_TEST_SUITE_P(
372 ImageMasks, RoundTripInstructionsTest,
373 Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
374 SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_3),
375 ::testing::ValuesIn(std::vector<std::string>{
376 "%2 = OpImageFetch %1 %3 %4\n",
377 "%2 = OpImageFetch %1 %3 %4 None\n",
378 "%2 = OpImageFetch %1 %3 %4 Bias %5\n",
379 "%2 = OpImageFetch %1 %3 %4 Lod %5\n",
380 "%2 = OpImageFetch %1 %3 %4 Grad %5 %6\n",
381 "%2 = OpImageFetch %1 %3 %4 ConstOffset %5\n",
382 "%2 = OpImageFetch %1 %3 %4 Offset %5\n",
383 "%2 = OpImageFetch %1 %3 %4 ConstOffsets %5\n",
384 "%2 = OpImageFetch %1 %3 %4 Sample %5\n",
385 "%2 = OpImageFetch %1 %3 %4 MinLod %5\n",
386 "%2 = OpImageFetch %1 %3 %4 Bias|Lod|Grad %5 %6 %7 %8\n",
387 "%2 = OpImageFetch %1 %3 %4 ConstOffset|Offset|ConstOffsets"
388 " %5 %6 %7\n",
389 "%2 = OpImageFetch %1 %3 %4 Sample|MinLod %5 %6\n",
390 "%2 = OpImageFetch %1 %3 %4"
391 " Bias|Lod|Grad|ConstOffset|Offset|ConstOffsets|Sample|MinLod"
392 " %5 %6 %7 %8 %9 %10 %11 %12 %13\n"})));
393
394 INSTANTIATE_TEST_SUITE_P(
395 NewInstructionsInSPIRV1_2, RoundTripInstructionsTest,
396 Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_3),
397 ::testing::ValuesIn(std::vector<std::string>{
398 "OpExecutionModeId %1 SubgroupsPerWorkgroupId %2\n",
399 "OpExecutionModeId %1 LocalSizeId %2 %3 %4\n",
400 "OpExecutionModeId %1 LocalSizeHintId %2 %3 %4\n",
401 "OpDecorateId %1 AlignmentId %2\n",
402 "OpDecorateId %1 MaxByteOffsetId %2\n",
403 })));
404
405 using MaskSorting = TextToBinaryTest;
406
TEST_F(MaskSorting,MasksAreSortedFromLSBToMSB)407 TEST_F(MaskSorting, MasksAreSortedFromLSBToMSB) {
408 EXPECT_THAT(EncodeAndDecodeSuccessfully(
409 "OpStore %1 %2 Nontemporal|Aligned|Volatile 32"),
410 Eq("OpStore %1 %2 Volatile|Aligned|Nontemporal 32\n"));
411 EXPECT_THAT(
412 EncodeAndDecodeSuccessfully(
413 "OpDecorate %1 FPFastMathMode NotInf|Fast|AllowRecip|NotNaN|NSZ"),
414 Eq("OpDecorate %1 FPFastMathMode NotNaN|NotInf|NSZ|AllowRecip|Fast\n"));
415 EXPECT_THAT(
416 EncodeAndDecodeSuccessfully("OpLoopMerge %1 %2 DontUnroll|Unroll"),
417 Eq("OpLoopMerge %1 %2 Unroll|DontUnroll\n"));
418 EXPECT_THAT(
419 EncodeAndDecodeSuccessfully("OpSelectionMerge %1 DontFlatten|Flatten"),
420 Eq("OpSelectionMerge %1 Flatten|DontFlatten\n"));
421 EXPECT_THAT(EncodeAndDecodeSuccessfully(
422 "%2 = OpFunction %1 DontInline|Const|Pure|Inline %3"),
423 Eq("%2 = OpFunction %1 Inline|DontInline|Pure|Const %3\n"));
424 EXPECT_THAT(EncodeAndDecodeSuccessfully(
425 "%2 = OpImageFetch %1 %3 %4"
426 " MinLod|Sample|Offset|Lod|Grad|ConstOffsets|ConstOffset|Bias"
427 " %5 %6 %7 %8 %9 %10 %11 %12 %13\n"),
428 Eq("%2 = OpImageFetch %1 %3 %4"
429 " Bias|Lod|Grad|ConstOffset|Offset|ConstOffsets|Sample|MinLod"
430 " %5 %6 %7 %8 %9 %10 %11 %12 %13\n"));
431 }
432
433 using OperandTypeTest = TextToBinaryTest;
434
TEST_F(OperandTypeTest,OptionalTypedLiteralNumber)435 TEST_F(OperandTypeTest, OptionalTypedLiteralNumber) {
436 const std::string input =
437 "%1 = OpTypeInt 32 0\n"
438 "%2 = OpConstant %1 42\n"
439 "OpSwitch %2 %3 100 %4\n";
440 EXPECT_EQ(input, EncodeAndDecodeSuccessfully(input));
441 }
442
443 using IndentTest = spvtest::TextToBinaryTest;
444
TEST_F(IndentTest,Sample)445 TEST_F(IndentTest, Sample) {
446 const std::string input = R"(
447 OpCapability Shader
448 OpMemoryModel Logical GLSL450
449 %1 = OpTypeInt 32 0
450 %2 = OpTypeStruct %1 %3 %4 %5 %6 %7 %8 %9 %10 ; force IDs into double digits
451 %11 = OpConstant %1 42
452 OpStore %2 %3 Aligned|Volatile 4 ; bogus, but not indented
453 )";
454 const std::string expected =
455 R"( OpCapability Shader
456 OpMemoryModel Logical GLSL450
457 %1 = OpTypeInt 32 0
458 %2 = OpTypeStruct %1 %3 %4 %5 %6 %7 %8 %9 %10
459 %11 = OpConstant %1 42
460 OpStore %2 %3 Volatile|Aligned 4
461 )";
462 EXPECT_THAT(
463 EncodeAndDecodeSuccessfully(input, SPV_BINARY_TO_TEXT_OPTION_INDENT),
464 expected);
465 }
466
467 using FriendlyNameDisassemblyTest = spvtest::TextToBinaryTest;
468
TEST_F(FriendlyNameDisassemblyTest,Sample)469 TEST_F(FriendlyNameDisassemblyTest, Sample) {
470 const std::string input = R"(
471 OpCapability Shader
472 OpMemoryModel Logical GLSL450
473 %1 = OpTypeInt 32 0
474 %2 = OpTypeStruct %1 %3 %4 %5 %6 %7 %8 %9 %10 ; force IDs into double digits
475 %11 = OpConstant %1 42
476 )";
477 const std::string expected =
478 R"(OpCapability Shader
479 OpMemoryModel Logical GLSL450
480 %uint = OpTypeInt 32 0
481 %_struct_2 = OpTypeStruct %uint %3 %4 %5 %6 %7 %8 %9 %10
482 %uint_42 = OpConstant %uint 42
483 )";
484 EXPECT_THAT(EncodeAndDecodeSuccessfully(
485 input, SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES),
486 expected);
487 }
488
TEST_F(TextToBinaryTest,ShowByteOffsetsWhenRequested)489 TEST_F(TextToBinaryTest, ShowByteOffsetsWhenRequested) {
490 const std::string input = R"(
491 OpCapability Shader
492 OpMemoryModel Logical GLSL450
493 %1 = OpTypeInt 32 0
494 %2 = OpTypeVoid
495 )";
496 const std::string expected =
497 R"(OpCapability Shader ; 0x00000014
498 OpMemoryModel Logical GLSL450 ; 0x0000001c
499 %1 = OpTypeInt 32 0 ; 0x00000028
500 %2 = OpTypeVoid ; 0x00000038
501 )";
502 EXPECT_THAT(EncodeAndDecodeSuccessfully(
503 input, SPV_BINARY_TO_TEXT_OPTION_SHOW_BYTE_OFFSET),
504 expected);
505 }
506
507 // Test version string.
TEST_F(TextToBinaryTest,VersionString)508 TEST_F(TextToBinaryTest, VersionString) {
509 auto words = CompileSuccessfully("");
510 spv_text decoded_text = nullptr;
511 EXPECT_THAT(spvBinaryToText(ScopedContext().context, words.data(),
512 words.size(), SPV_BINARY_TO_TEXT_OPTION_NONE,
513 &decoded_text, &diagnostic),
514 Eq(SPV_SUCCESS));
515 EXPECT_EQ(nullptr, diagnostic);
516
517 EXPECT_THAT(decoded_text->str, HasSubstr("Version: 1.0\n"))
518 << EncodeAndDecodeSuccessfully("");
519 spvTextDestroy(decoded_text);
520 }
521
522 // Test generator string.
523
524 // A test case for the generator string. This allows us to
525 // test both of the 16-bit components of the generator word.
526 struct GeneratorStringCase {
527 uint16_t generator;
528 uint16_t misc;
529 std::string expected;
530 };
531
532 using GeneratorStringTest = spvtest::TextToBinaryTestBase<
533 ::testing::TestWithParam<GeneratorStringCase>>;
534
TEST_P(GeneratorStringTest,Sample)535 TEST_P(GeneratorStringTest, Sample) {
536 auto words = CompileSuccessfully("");
537 EXPECT_EQ(2u, SPV_INDEX_GENERATOR_NUMBER);
538 words[SPV_INDEX_GENERATOR_NUMBER] =
539 SPV_GENERATOR_WORD(GetParam().generator, GetParam().misc);
540
541 spv_text decoded_text = nullptr;
542 EXPECT_THAT(spvBinaryToText(ScopedContext().context, words.data(),
543 words.size(), SPV_BINARY_TO_TEXT_OPTION_NONE,
544 &decoded_text, &diagnostic),
545 Eq(SPV_SUCCESS));
546 EXPECT_THAT(diagnostic, Eq(nullptr));
547 EXPECT_THAT(std::string(decoded_text->str), HasSubstr(GetParam().expected));
548 spvTextDestroy(decoded_text);
549 }
550
551 INSTANTIATE_TEST_SUITE_P(GeneratorStrings, GeneratorStringTest,
552 ::testing::ValuesIn(std::vector<GeneratorStringCase>{
553 {SPV_GENERATOR_KHRONOS, 12, "Khronos; 12"},
554 {SPV_GENERATOR_LUNARG, 99, "LunarG; 99"},
555 {SPV_GENERATOR_VALVE, 1, "Valve; 1"},
556 {SPV_GENERATOR_CODEPLAY, 65535, "Codeplay; 65535"},
557 {SPV_GENERATOR_NVIDIA, 19, "NVIDIA; 19"},
558 {SPV_GENERATOR_ARM, 1000, "ARM; 1000"},
559 {SPV_GENERATOR_KHRONOS_LLVM_TRANSLATOR, 38,
560 "Khronos LLVM/SPIR-V Translator; 38"},
561 {SPV_GENERATOR_KHRONOS_ASSEMBLER, 2,
562 "Khronos SPIR-V Tools Assembler; 2"},
563 {SPV_GENERATOR_KHRONOS_GLSLANG, 1,
564 "Khronos Glslang Reference Front End; 1"},
565 {1000, 18, "Unknown(1000); 18"},
566 {65535, 32767, "Unknown(65535); 32767"},
567 }));
568
569 // TODO(dneto): Test new instructions and enums in SPIR-V 1.3
570
571 } // namespace
572 } // namespace spvtools
573