• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(spv::Op::OpTypeVoid, {0}),
189          "Error: Result Id is 0"},
190         {"", spvtest::MakeInstruction(spv::Op::OpConstant, {0, 1, 42}),
191          "Error: Type Id is 0"},
192         {"%1 = OpTypeVoid", spvtest::MakeInstruction(spv::Op::OpTypeVoid, {1}),
193          "Id 1 is defined more than once"},
194         {"%1 = OpTypeVoid\n"
195          "%2 = OpNot %1 %foo",
196          spvtest::MakeInstruction(spv::Op::OpNot, {1, 2, 3}),
197          "Id 2 is defined more than once"},
198         {"%1 = OpTypeVoid\n"
199          "%2 = OpNot %1 %foo",
200          spvtest::MakeInstruction(spv::Op::OpNot, {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(spv::Op::OpConstant, {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(spv::Op::OpConstant, {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(spv::Op::OpSwitch, {1, 2, 3, 4}),
216          "Invalid OpSwitch: selector id 1 has no type"},
217         {"%1 = OpTypeVoid\n",
218          spvtest::MakeInstruction(spv::Op::OpSwitch, {1, 2, 3, 4}),
219          "Invalid OpSwitch: selector id 1 is a type, not a value"},
220         {"%1 = OpConstantTrue !500",
221          spvtest::MakeInstruction(spv::Op::OpSwitch, {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(spv::Op::OpSwitch, {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 
TEST_F(TextToBinaryTest,Comments)507 TEST_F(TextToBinaryTest, Comments) {
508   const std::string input = R"(OpCapability Shader
509 OpMemoryModel Logical GLSL450
510 OpEntryPoint Fragment %63 "main" %4 %22
511 OpExecutionMode %63 OriginUpperLeft
512 OpSource GLSL 450
513 OpName %4 "_ue"
514 OpName %8 "_uf"
515 OpName %11 "_ug"
516 OpName %12 "_uA"
517 OpMemberName %12 0 "_ux"
518 OpName %14 "_uc"
519 OpName %15 "_uB"
520 OpMemberName %15 0 "_ux"
521 OpName %20 "_ud"
522 OpName %22 "_ucol"
523 OpName %26 "ANGLEDepthRangeParams"
524 OpMemberName %26 0 "near"
525 OpMemberName %26 1 "far"
526 OpMemberName %26 2 "diff"
527 OpMemberName %26 3 "reserved"
528 OpName %27 "ANGLEUniformBlock"
529 OpMemberName %27 0 "viewport"
530 OpMemberName %27 1 "clipDistancesEnabled"
531 OpMemberName %27 2 "xfbActiveUnpaused"
532 OpMemberName %27 3 "xfbVerticesPerInstance"
533 OpMemberName %27 4 "numSamples"
534 OpMemberName %27 5 "xfbBufferOffsets"
535 OpMemberName %27 6 "acbBufferOffsets"
536 OpMemberName %27 7 "depthRange"
537 OpName %29 "ANGLEUniforms"
538 OpName %33 "_uc"
539 OpName %32 "_uh"
540 OpName %49 "_ux"
541 OpName %50 "_uy"
542 OpName %48 "_ui"
543 OpName %63 "main"
544 OpName %65 "param"
545 OpName %68 "param"
546 OpName %73 "param"
547 OpDecorate %4 Location 0
548 OpDecorate %8 RelaxedPrecision
549 OpDecorate %8 DescriptorSet 0
550 OpDecorate %8 Binding 0
551 OpDecorate %11 DescriptorSet 0
552 OpDecorate %11 Binding 1
553 OpMemberDecorate %12 0 Offset 0
554 OpMemberDecorate %12 0 RelaxedPrecision
555 OpDecorate %12 Block
556 OpDecorate %14 DescriptorSet 0
557 OpDecorate %14 Binding 2
558 OpMemberDecorate %15 0 Offset 0
559 OpMemberDecorate %15 0 RelaxedPrecision
560 OpDecorate %15 BufferBlock
561 OpDecorate %20 DescriptorSet 0
562 OpDecorate %20 Binding 3
563 OpDecorate %22 RelaxedPrecision
564 OpDecorate %22 Location 0
565 OpMemberDecorate %26 0 Offset 0
566 OpMemberDecorate %26 1 Offset 4
567 OpMemberDecorate %26 2 Offset 8
568 OpMemberDecorate %26 3 Offset 12
569 OpMemberDecorate %27 0 Offset 0
570 OpMemberDecorate %27 1 Offset 16
571 OpMemberDecorate %27 2 Offset 20
572 OpMemberDecorate %27 3 Offset 24
573 OpMemberDecorate %27 4 Offset 28
574 OpMemberDecorate %27 5 Offset 32
575 OpMemberDecorate %27 6 Offset 48
576 OpMemberDecorate %27 7 Offset 64
577 OpMemberDecorate %27 2 RelaxedPrecision
578 OpMemberDecorate %27 4 RelaxedPrecision
579 OpDecorate %27 Block
580 OpDecorate %29 DescriptorSet 0
581 OpDecorate %29 Binding 4
582 OpDecorate %32 RelaxedPrecision
583 OpDecorate %33 RelaxedPrecision
584 OpDecorate %36 RelaxedPrecision
585 OpDecorate %37 RelaxedPrecision
586 OpDecorate %38 RelaxedPrecision
587 OpDecorate %39 RelaxedPrecision
588 OpDecorate %41 RelaxedPrecision
589 OpDecorate %42 RelaxedPrecision
590 OpDecorate %43 RelaxedPrecision
591 OpDecorate %48 RelaxedPrecision
592 OpDecorate %49 RelaxedPrecision
593 OpDecorate %50 RelaxedPrecision
594 OpDecorate %52 RelaxedPrecision
595 OpDecorate %53 RelaxedPrecision
596 OpDecorate %54 RelaxedPrecision
597 OpDecorate %55 RelaxedPrecision
598 OpDecorate %56 RelaxedPrecision
599 OpDecorate %57 RelaxedPrecision
600 OpDecorate %58 RelaxedPrecision
601 OpDecorate %59 RelaxedPrecision
602 OpDecorate %60 RelaxedPrecision
603 OpDecorate %67 RelaxedPrecision
604 OpDecorate %68 RelaxedPrecision
605 OpDecorate %72 RelaxedPrecision
606 OpDecorate %73 RelaxedPrecision
607 OpDecorate %75 RelaxedPrecision
608 OpDecorate %76 RelaxedPrecision
609 OpDecorate %77 RelaxedPrecision
610 OpDecorate %80 RelaxedPrecision
611 OpDecorate %81 RelaxedPrecision
612 %1 = OpTypeFloat 32
613 %2 = OpTypeVector %1 4
614 %5 = OpTypeImage %1 2D 0 0 0 1 Unknown
615 %6 = OpTypeSampledImage %5
616 %9 = OpTypeImage %1 2D 0 0 0 2 Rgba8
617 %12 = OpTypeStruct %2
618 %15 = OpTypeStruct %2
619 %16 = OpTypeInt 32 0
620 %17 = OpConstant %16 2
621 %18 = OpTypeArray %15 %17
622 %23 = OpTypeInt 32 1
623 %24 = OpTypeVector %23 4
624 %25 = OpTypeVector %16 4
625 %26 = OpTypeStruct %1 %1 %1 %1
626 %27 = OpTypeStruct %2 %16 %16 %23 %23 %24 %25 %26
627 %35 = OpTypeVector %1 2
628 %40 = OpTypeVector %23 2
629 %61 = OpTypeVoid
630 %69 = OpConstant %16 0
631 %78 = OpConstant %16 1
632 %3 = OpTypePointer Input %2
633 %7 = OpTypePointer UniformConstant %6
634 %10 = OpTypePointer UniformConstant %9
635 %13 = OpTypePointer Uniform %12
636 %19 = OpTypePointer Uniform %18
637 %21 = OpTypePointer Output %2
638 %28 = OpTypePointer Uniform %27
639 %30 = OpTypePointer Function %2
640 %70 = OpTypePointer Uniform %2
641 %31 = OpTypeFunction %2 %30
642 %47 = OpTypeFunction %2 %30 %30
643 %62 = OpTypeFunction %61
644 %4 = OpVariable %3 Input
645 %8 = OpVariable %7 UniformConstant
646 %11 = OpVariable %10 UniformConstant
647 %14 = OpVariable %13 Uniform
648 %20 = OpVariable %19 Uniform
649 %22 = OpVariable %21 Output
650 %29 = OpVariable %28 Uniform
651 %32 = OpFunction %2 None %31
652 %33 = OpFunctionParameter %30
653 %34 = OpLabel
654 %36 = OpLoad %6 %8
655 %37 = OpLoad %2 %33
656 %38 = OpVectorShuffle %35 %37 %37 0 1
657 %39 = OpImageSampleImplicitLod %2 %36 %38
658 %41 = OpLoad %2 %33
659 %42 = OpVectorShuffle %35 %41 %41 2 3
660 %43 = OpConvertFToS %40 %42
661 %44 = OpLoad %9 %11
662 %45 = OpImageRead %2 %44 %43
663 %46 = OpFAdd %2 %39 %45
664 OpReturnValue %46
665 OpFunctionEnd
666 %48 = OpFunction %2 None %47
667 %49 = OpFunctionParameter %30
668 %50 = OpFunctionParameter %30
669 %51 = OpLabel
670 %52 = OpLoad %2 %49
671 %53 = OpVectorShuffle %35 %52 %52 0 1
672 %54 = OpLoad %2 %50
673 %55 = OpVectorShuffle %35 %54 %54 2 3
674 %56 = OpCompositeExtract %1 %53 0
675 %57 = OpCompositeExtract %1 %53 1
676 %58 = OpCompositeExtract %1 %55 0
677 %59 = OpCompositeExtract %1 %55 1
678 %60 = OpCompositeConstruct %2 %56 %57 %58 %59
679 OpReturnValue %60
680 OpFunctionEnd
681 %63 = OpFunction %61 None %62
682 %64 = OpLabel
683 %65 = OpVariable %30 Function
684 %68 = OpVariable %30 Function
685 %73 = OpVariable %30 Function
686 %66 = OpLoad %2 %4
687 OpStore %65 %66
688 %67 = OpFunctionCall %2 %32 %65
689 %71 = OpAccessChain %70 %14 %69
690 %72 = OpLoad %2 %71
691 OpStore %68 %72
692 %74 = OpAccessChain %70 %20 %69 %69
693 %75 = OpLoad %2 %74
694 OpStore %73 %75
695 %76 = OpFunctionCall %2 %48 %68 %73
696 %77 = OpFAdd %2 %67 %76
697 %79 = OpAccessChain %70 %20 %78 %69
698 %80 = OpLoad %2 %79
699 %81 = OpFAdd %2 %77 %80
700 OpStore %22 %81
701 OpReturn
702 OpFunctionEnd
703 )";
704   const std::string expected = R"(               OpCapability Shader
705                OpMemoryModel Logical GLSL450
706                OpEntryPoint Fragment %1 "main" %2 %3
707                OpExecutionMode %1 OriginUpperLeft
708 
709                ; Debug Information
710                OpSource GLSL 450
711                OpName %2 "_ue"                      ; id %2
712                OpName %4 "_uf"                      ; id %4
713                OpName %5 "_ug"                      ; id %5
714                OpName %6 "_uA"                      ; id %6
715                OpMemberName %6 0 "_ux"
716                OpName %7 "_uc"                      ; id %7
717                OpName %8 "_uB"                      ; id %8
718                OpMemberName %8 0 "_ux"
719                OpName %9 "_ud"                      ; id %9
720                OpName %3 "_ucol"                    ; id %3
721                OpName %10 "ANGLEDepthRangeParams"   ; id %10
722                OpMemberName %10 0 "near"
723                OpMemberName %10 1 "far"
724                OpMemberName %10 2 "diff"
725                OpMemberName %10 3 "reserved"
726                OpName %11 "ANGLEUniformBlock"       ; id %11
727                OpMemberName %11 0 "viewport"
728                OpMemberName %11 1 "clipDistancesEnabled"
729                OpMemberName %11 2 "xfbActiveUnpaused"
730                OpMemberName %11 3 "xfbVerticesPerInstance"
731                OpMemberName %11 4 "numSamples"
732                OpMemberName %11 5 "xfbBufferOffsets"
733                OpMemberName %11 6 "acbBufferOffsets"
734                OpMemberName %11 7 "depthRange"
735                OpName %12 "ANGLEUniforms"           ; id %12
736                OpName %13 "_uc"                     ; id %13
737                OpName %14 "_uh"                     ; id %14
738                OpName %15 "_ux"                     ; id %15
739                OpName %16 "_uy"                     ; id %16
740                OpName %17 "_ui"                     ; id %17
741                OpName %1 "main"                     ; id %1
742                OpName %18 "param"                   ; id %18
743                OpName %19 "param"                   ; id %19
744                OpName %20 "param"                   ; id %20
745 
746                ; Annotations
747                OpDecorate %2 Location 0
748                OpDecorate %4 RelaxedPrecision
749                OpDecorate %4 DescriptorSet 0
750                OpDecorate %4 Binding 0
751                OpDecorate %5 DescriptorSet 0
752                OpDecorate %5 Binding 1
753                OpMemberDecorate %6 0 Offset 0
754                OpMemberDecorate %6 0 RelaxedPrecision
755                OpDecorate %6 Block
756                OpDecorate %7 DescriptorSet 0
757                OpDecorate %7 Binding 2
758                OpMemberDecorate %8 0 Offset 0
759                OpMemberDecorate %8 0 RelaxedPrecision
760                OpDecorate %8 BufferBlock
761                OpDecorate %9 DescriptorSet 0
762                OpDecorate %9 Binding 3
763                OpDecorate %3 RelaxedPrecision
764                OpDecorate %3 Location 0
765                OpMemberDecorate %10 0 Offset 0
766                OpMemberDecorate %10 1 Offset 4
767                OpMemberDecorate %10 2 Offset 8
768                OpMemberDecorate %10 3 Offset 12
769                OpMemberDecorate %11 0 Offset 0
770                OpMemberDecorate %11 1 Offset 16
771                OpMemberDecorate %11 2 Offset 20
772                OpMemberDecorate %11 3 Offset 24
773                OpMemberDecorate %11 4 Offset 28
774                OpMemberDecorate %11 5 Offset 32
775                OpMemberDecorate %11 6 Offset 48
776                OpMemberDecorate %11 7 Offset 64
777                OpMemberDecorate %11 2 RelaxedPrecision
778                OpMemberDecorate %11 4 RelaxedPrecision
779                OpDecorate %11 Block
780                OpDecorate %12 DescriptorSet 0
781                OpDecorate %12 Binding 4
782                OpDecorate %14 RelaxedPrecision
783                OpDecorate %13 RelaxedPrecision
784                OpDecorate %21 RelaxedPrecision
785                OpDecorate %22 RelaxedPrecision
786                OpDecorate %23 RelaxedPrecision
787                OpDecorate %24 RelaxedPrecision
788                OpDecorate %25 RelaxedPrecision
789                OpDecorate %26 RelaxedPrecision
790                OpDecorate %27 RelaxedPrecision
791                OpDecorate %17 RelaxedPrecision
792                OpDecorate %15 RelaxedPrecision
793                OpDecorate %16 RelaxedPrecision
794                OpDecorate %28 RelaxedPrecision
795                OpDecorate %29 RelaxedPrecision
796                OpDecorate %30 RelaxedPrecision
797                OpDecorate %31 RelaxedPrecision
798                OpDecorate %32 RelaxedPrecision
799                OpDecorate %33 RelaxedPrecision
800                OpDecorate %34 RelaxedPrecision
801                OpDecorate %35 RelaxedPrecision
802                OpDecorate %36 RelaxedPrecision
803                OpDecorate %37 RelaxedPrecision
804                OpDecorate %19 RelaxedPrecision
805                OpDecorate %38 RelaxedPrecision
806                OpDecorate %20 RelaxedPrecision
807                OpDecorate %39 RelaxedPrecision
808                OpDecorate %40 RelaxedPrecision
809                OpDecorate %41 RelaxedPrecision
810                OpDecorate %42 RelaxedPrecision
811                OpDecorate %43 RelaxedPrecision
812 
813                ; Types, variables and constants
814          %44 = OpTypeFloat 32
815          %45 = OpTypeVector %44 4
816          %46 = OpTypeImage %44 2D 0 0 0 1 Unknown
817          %47 = OpTypeSampledImage %46
818          %48 = OpTypeImage %44 2D 0 0 0 2 Rgba8
819           %6 = OpTypeStruct %45                     ; Block
820           %8 = OpTypeStruct %45                     ; BufferBlock
821          %49 = OpTypeInt 32 0
822          %50 = OpConstant %49 2
823          %51 = OpTypeArray %8 %50
824          %52 = OpTypeInt 32 1
825          %53 = OpTypeVector %52 4
826          %54 = OpTypeVector %49 4
827          %10 = OpTypeStruct %44 %44 %44 %44
828          %11 = OpTypeStruct %45 %49 %49 %52 %52 %53 %54 %10     ; Block
829          %55 = OpTypeVector %44 2
830          %56 = OpTypeVector %52 2
831          %57 = OpTypeVoid
832          %58 = OpConstant %49 0
833          %59 = OpConstant %49 1
834          %60 = OpTypePointer Input %45
835          %61 = OpTypePointer UniformConstant %47
836          %62 = OpTypePointer UniformConstant %48
837          %63 = OpTypePointer Uniform %6
838          %64 = OpTypePointer Uniform %51
839          %65 = OpTypePointer Output %45
840          %66 = OpTypePointer Uniform %11
841          %67 = OpTypePointer Function %45
842          %68 = OpTypePointer Uniform %45
843          %69 = OpTypeFunction %45 %67
844          %70 = OpTypeFunction %45 %67 %67
845          %71 = OpTypeFunction %57
846           %2 = OpVariable %60 Input                 ; Location 0
847           %4 = OpVariable %61 UniformConstant       ; RelaxedPrecision, DescriptorSet 0, Binding 0
848           %5 = OpVariable %62 UniformConstant       ; DescriptorSet 0, Binding 1
849           %7 = OpVariable %63 Uniform               ; DescriptorSet 0, Binding 2
850           %9 = OpVariable %64 Uniform               ; DescriptorSet 0, Binding 3
851           %3 = OpVariable %65 Output                ; RelaxedPrecision, Location 0
852          %12 = OpVariable %66 Uniform               ; DescriptorSet 0, Binding 4
853 
854                ; Function 14
855          %14 = OpFunction %45 None %69              ; RelaxedPrecision
856          %13 = OpFunctionParameter %67              ; RelaxedPrecision
857          %72 = OpLabel
858          %21 = OpLoad %47 %4                        ; RelaxedPrecision
859          %22 = OpLoad %45 %13                       ; RelaxedPrecision
860          %23 = OpVectorShuffle %55 %22 %22 0 1      ; RelaxedPrecision
861          %24 = OpImageSampleImplicitLod %45 %21 %23     ; RelaxedPrecision
862          %25 = OpLoad %45 %13                           ; RelaxedPrecision
863          %26 = OpVectorShuffle %55 %25 %25 2 3          ; RelaxedPrecision
864          %27 = OpConvertFToS %56 %26                    ; RelaxedPrecision
865          %73 = OpLoad %48 %5
866          %74 = OpImageRead %45 %73 %27
867          %75 = OpFAdd %45 %24 %74
868                OpReturnValue %75
869                OpFunctionEnd
870 
871                ; Function 17
872          %17 = OpFunction %45 None %70              ; RelaxedPrecision
873          %15 = OpFunctionParameter %67              ; RelaxedPrecision
874          %16 = OpFunctionParameter %67              ; RelaxedPrecision
875          %76 = OpLabel
876          %28 = OpLoad %45 %15                       ; RelaxedPrecision
877          %29 = OpVectorShuffle %55 %28 %28 0 1      ; RelaxedPrecision
878          %30 = OpLoad %45 %16                       ; RelaxedPrecision
879          %31 = OpVectorShuffle %55 %30 %30 2 3      ; RelaxedPrecision
880          %32 = OpCompositeExtract %44 %29 0         ; RelaxedPrecision
881          %33 = OpCompositeExtract %44 %29 1         ; RelaxedPrecision
882          %34 = OpCompositeExtract %44 %31 0         ; RelaxedPrecision
883          %35 = OpCompositeExtract %44 %31 1         ; RelaxedPrecision
884          %36 = OpCompositeConstruct %45 %32 %33 %34 %35     ; RelaxedPrecision
885                OpReturnValue %36
886                OpFunctionEnd
887 
888                ; Function 1
889           %1 = OpFunction %57 None %71
890          %77 = OpLabel
891          %18 = OpVariable %67 Function
892          %19 = OpVariable %67 Function              ; RelaxedPrecision
893          %20 = OpVariable %67 Function              ; RelaxedPrecision
894          %78 = OpLoad %45 %2
895                OpStore %18 %78
896          %37 = OpFunctionCall %45 %14 %18           ; RelaxedPrecision
897          %79 = OpAccessChain %68 %7 %58
898          %38 = OpLoad %45 %79                       ; RelaxedPrecision
899                OpStore %19 %38
900          %80 = OpAccessChain %68 %9 %58 %58
901          %39 = OpLoad %45 %80                       ; RelaxedPrecision
902                OpStore %20 %39
903          %40 = OpFunctionCall %45 %17 %19 %20       ; RelaxedPrecision
904          %41 = OpFAdd %45 %37 %40                   ; RelaxedPrecision
905          %81 = OpAccessChain %68 %9 %59 %58
906          %42 = OpLoad %45 %81                       ; RelaxedPrecision
907          %43 = OpFAdd %45 %41 %42                   ; RelaxedPrecision
908                OpStore %3 %43
909                OpReturn
910                OpFunctionEnd
911 )";
912 
913   EXPECT_THAT(
914       EncodeAndDecodeSuccessfully(input, SPV_BINARY_TO_TEXT_OPTION_COMMENT |
915                                              SPV_BINARY_TO_TEXT_OPTION_INDENT),
916       expected);
917 }
918 
919 // Test version string.
TEST_F(TextToBinaryTest,VersionString)920 TEST_F(TextToBinaryTest, VersionString) {
921   auto words = CompileSuccessfully("");
922   spv_text decoded_text = nullptr;
923   EXPECT_THAT(spvBinaryToText(ScopedContext().context, words.data(),
924                               words.size(), SPV_BINARY_TO_TEXT_OPTION_NONE,
925                               &decoded_text, &diagnostic),
926               Eq(SPV_SUCCESS));
927   EXPECT_EQ(nullptr, diagnostic);
928 
929   EXPECT_THAT(decoded_text->str, HasSubstr("Version: 1.0\n"))
930       << EncodeAndDecodeSuccessfully("");
931   spvTextDestroy(decoded_text);
932 }
933 
934 // Test generator string.
935 
936 // A test case for the generator string.  This allows us to
937 // test both of the 16-bit components of the generator word.
938 struct GeneratorStringCase {
939   uint16_t generator;
940   uint16_t misc;
941   std::string expected;
942 };
943 
944 using GeneratorStringTest = spvtest::TextToBinaryTestBase<
945     ::testing::TestWithParam<GeneratorStringCase>>;
946 
TEST_P(GeneratorStringTest,Sample)947 TEST_P(GeneratorStringTest, Sample) {
948   auto words = CompileSuccessfully("");
949   EXPECT_EQ(2u, SPV_INDEX_GENERATOR_NUMBER);
950   words[SPV_INDEX_GENERATOR_NUMBER] =
951       SPV_GENERATOR_WORD(GetParam().generator, GetParam().misc);
952 
953   spv_text decoded_text = nullptr;
954   EXPECT_THAT(spvBinaryToText(ScopedContext().context, words.data(),
955                               words.size(), SPV_BINARY_TO_TEXT_OPTION_NONE,
956                               &decoded_text, &diagnostic),
957               Eq(SPV_SUCCESS));
958   EXPECT_THAT(diagnostic, Eq(nullptr));
959   EXPECT_THAT(std::string(decoded_text->str), HasSubstr(GetParam().expected));
960   spvTextDestroy(decoded_text);
961 }
962 
963 INSTANTIATE_TEST_SUITE_P(GeneratorStrings, GeneratorStringTest,
964                          ::testing::ValuesIn(std::vector<GeneratorStringCase>{
965                              {SPV_GENERATOR_KHRONOS, 12, "Khronos; 12"},
966                              {SPV_GENERATOR_LUNARG, 99, "LunarG; 99"},
967                              {SPV_GENERATOR_VALVE, 1, "Valve; 1"},
968                              {SPV_GENERATOR_CODEPLAY, 65535, "Codeplay; 65535"},
969                              {SPV_GENERATOR_NVIDIA, 19, "NVIDIA; 19"},
970                              {SPV_GENERATOR_ARM, 1000, "ARM; 1000"},
971                              {SPV_GENERATOR_KHRONOS_LLVM_TRANSLATOR, 38,
972                               "Khronos LLVM/SPIR-V Translator; 38"},
973                              {SPV_GENERATOR_KHRONOS_ASSEMBLER, 2,
974                               "Khronos SPIR-V Tools Assembler; 2"},
975                              {SPV_GENERATOR_KHRONOS_GLSLANG, 1,
976                               "Khronos Glslang Reference Front End; 1"},
977                              {1000, 18, "Unknown(1000); 18"},
978                              {65535, 32767, "Unknown(65535); 32767"},
979                          }));
980 
981 // TODO(dneto): Test new instructions and enums in SPIR-V 1.3
982 
983 }  // namespace
984 }  // namespace spvtools
985