Debian Codegraph @

test/binary_to_text_test.cpp

raw
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:
37 BinaryToText()
38 : context(spvContextCreate(SPV_ENV_UNIVERSAL_1_0)), binary(nullptr) {}
39 ~BinaryToText() override {
40 spvBinaryDestroy(binary);
41 spvContextDestroy(context);
42 }
43
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
75 void TearDown() override {
76 spvBinaryDestroy(binary);
77 binary = nullptr;
78 }
79
80 // Compiles the given assembly text, and saves it into 'binary'.
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
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
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
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
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
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
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
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.
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
249 TEST_P(RoundTripInstructionsTest, Sample) {
250 EXPECT_THAT(EncodeAndDecodeSuccessfully(
251 std::get<1>(GetParam()), SPV_BINARY_TO_TEXT_OPTION_NONE,
252 SPV_TEXT_TO_BINARY_OPTION_NONE, 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 INSTANTIATE_TEST_SUITE_P(
406 CacheControlsINTEL, RoundTripInstructionsTest,
407 Combine(
408 ::testing::Values(SPV_ENV_UNIVERSAL_1_0),
409 ::testing::ValuesIn(std::vector<std::string>{
410 "OpDecorate %1 CacheControlLoadINTEL 0 UncachedINTEL\n",
411 "OpDecorate %1 CacheControlLoadINTEL 1 CachedINTEL\n",
412 "OpDecorate %1 CacheControlLoadINTEL 2 StreamingINTEL\n",
413 "OpDecorate %1 CacheControlLoadINTEL 3 InvalidateAfterReadINTEL\n",
414 "OpDecorate %1 CacheControlLoadINTEL 4 ConstCachedINTEL\n",
415 "OpDecorate %1 CacheControlStoreINTEL 0 UncachedINTEL\n",
416 "OpDecorate %1 CacheControlStoreINTEL 1 WriteThroughINTEL\n",
417 "OpDecorate %1 CacheControlStoreINTEL 2 WriteBackINTEL\n",
418 "OpDecorate %1 CacheControlStoreINTEL 3 StreamingINTEL\n",
419 })));
420
421 INSTANTIATE_TEST_SUITE_P(
422 HostAccessINTEL, RoundTripInstructionsTest,
423 Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0),
424 ::testing::ValuesIn(std::vector<std::string>{
425 "OpDecorate %1 HostAccessINTEL NoneINTEL \"none\"\n",
426 "OpDecorate %1 HostAccessINTEL ReadINTEL \"read\"\n",
427 "OpDecorate %1 HostAccessINTEL WriteINTEL \"write\"\n",
428 "OpDecorate %1 HostAccessINTEL ReadWriteINTEL \"readwrite\"\n",
429 })));
430
431 // clang-format off
432 INSTANTIATE_TEST_SUITE_P(
433 MatrixMultiplyAccumulateOperands, RoundTripInstructionsTest,
434 Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0),
435 ::testing::ValuesIn(std::vector<std::string>{
436 "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6\n",
437 "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 None\n",
438 "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixASignedComponentsINTEL\n",
439 "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixBSignedComponentsINTEL\n",
440 "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixCBFloat16INTEL\n",
441 "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixResultBFloat16INTEL\n",
442 "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixAPackedInt8INTEL\n",
443 "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixBPackedInt8INTEL\n",
444 "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixAPackedInt4INTEL\n",
445 "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixBPackedInt4INTEL\n",
446 "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixATF32INTEL\n",
447 "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixBTF32INTEL\n",
448 "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixCBFloat16INTEL\n",
449 "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixAPackedFloat16INTEL\n",
450 "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixBPackedFloat16INTEL\n",
451 "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixAPackedBFloat16INTEL\n",
452 "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 MatrixBPackedBFloat16INTEL\n",
453 "%2 = OpSubgroupMatrixMultiplyAccumulateINTEL %1 %3 %4 %5 %6 "
454 "MatrixASignedComponentsINTEL|MatrixBSignedComponentsINTEL|MatrixAPackedInt8INTEL|MatrixBPackedInt8INTEL\n",
455 })));
456 // clang-format on
457
458 using MaskSorting = TextToBinaryTest;
459
460 TEST_F(MaskSorting, MasksAreSortedFromLSBToMSB) {
461 EXPECT_THAT(EncodeAndDecodeSuccessfully(
462 "OpStore %1 %2 Nontemporal|Aligned|Volatile 32"),
463 Eq("OpStore %1 %2 Volatile|Aligned|Nontemporal 32\n"));
464 EXPECT_THAT(
465 EncodeAndDecodeSuccessfully(
466 "OpDecorate %1 FPFastMathMode NotInf|Fast|AllowRecip|NotNaN|NSZ"),
467 Eq("OpDecorate %1 FPFastMathMode NotNaN|NotInf|NSZ|AllowRecip|Fast\n"));
468 EXPECT_THAT(
469 EncodeAndDecodeSuccessfully("OpLoopMerge %1 %2 DontUnroll|Unroll"),
470 Eq("OpLoopMerge %1 %2 Unroll|DontUnroll\n"));
471 EXPECT_THAT(
472 EncodeAndDecodeSuccessfully("OpSelectionMerge %1 DontFlatten|Flatten"),
473 Eq("OpSelectionMerge %1 Flatten|DontFlatten\n"));
474 EXPECT_THAT(EncodeAndDecodeSuccessfully(
475 "%2 = OpFunction %1 DontInline|Const|Pure|Inline %3"),
476 Eq("%2 = OpFunction %1 Inline|DontInline|Pure|Const %3\n"));
477 EXPECT_THAT(EncodeAndDecodeSuccessfully(
478 "%2 = OpImageFetch %1 %3 %4"
479 " MinLod|Sample|Offset|Lod|Grad|ConstOffsets|ConstOffset|Bias"
480 " %5 %6 %7 %8 %9 %10 %11 %12 %13\n"),
481 Eq("%2 = OpImageFetch %1 %3 %4"
482 " Bias|Lod|Grad|ConstOffset|Offset|ConstOffsets|Sample|MinLod"
483 " %5 %6 %7 %8 %9 %10 %11 %12 %13\n"));
484 }
485
486 using OperandTypeTest = TextToBinaryTest;
487
488 TEST_F(OperandTypeTest, OptionalTypedLiteralNumber) {
489 const std::string input =
490 "%1 = OpTypeInt 32 0\n"
491 "%2 = OpConstant %1 42\n"
492 "OpSwitch %2 %3 100 %4\n";
493 EXPECT_EQ(input, EncodeAndDecodeSuccessfully(input));
494 }
495
496 using IndentTest = spvtest::TextToBinaryTest;
497
498 TEST_F(IndentTest, Sample) {
499 const std::string input = R"(
500 OpCapability Shader
501 OpMemoryModel Logical GLSL450
502 %1 = OpTypeInt 32 0
503 %2 = OpTypeStruct %1 %3 %4 %5 %6 %7 %8 %9 %10 ; force IDs into double digits
504 %11 = OpConstant %1 42
505 OpStore %2 %3 Aligned|Volatile 4 ; bogus, but not indented
506 )";
507 const std::string expected =
508 R"( OpCapability Shader
509 OpMemoryModel Logical GLSL450
510 %1 = OpTypeInt 32 0
511 %2 = OpTypeStruct %1 %3 %4 %5 %6 %7 %8 %9 %10
512 %11 = OpConstant %1 42
513 OpStore %2 %3 Volatile|Aligned 4
514 )";
515 EXPECT_THAT(
516 EncodeAndDecodeSuccessfully(input, SPV_BINARY_TO_TEXT_OPTION_INDENT),
517 expected);
518 }
519
520 TEST_F(IndentTest, NestedIf) {
521 const std::string input = R"(
522 OpCapability Shader
523 OpMemoryModel Logical Simple
524 OpEntryPoint Fragment %100 "main"
525 OpExecutionMode %100 OriginUpperLeft
526 OpName %var "var"
527 %void = OpTypeVoid
528 %3 = OpTypeFunction %void
529 %bool = OpTypeBool
530 %5 = OpConstantNull %bool
531 %true = OpConstantTrue %bool
532 %false = OpConstantFalse %bool
533 %uint = OpTypeInt 32 0
534 %int = OpTypeInt 32 1
535 %uint_42 = OpConstant %uint 42
536 %int_42 = OpConstant %int 42
537 %13 = OpTypeFunction %uint
538 %uint_0 = OpConstant %uint 0
539 %uint_1 = OpConstant %uint 1
540 %uint_2 = OpConstant %uint 2
541 %uint_3 = OpConstant %uint 3
542 %uint_4 = OpConstant %uint 4
543 %uint_5 = OpConstant %uint 5
544 %uint_6 = OpConstant %uint 6
545 %uint_7 = OpConstant %uint 7
546 %uint_8 = OpConstant %uint 8
547 %uint_10 = OpConstant %uint 10
548 %uint_20 = OpConstant %uint 20
549 %uint_30 = OpConstant %uint 30
550 %uint_40 = OpConstant %uint 40
551 %uint_50 = OpConstant %uint 50
552 %uint_90 = OpConstant %uint 90
553 %uint_99 = OpConstant %uint 99
554 %_ptr_Private_uint = OpTypePointer Private %uint
555 %var = OpVariable %_ptr_Private_uint Private
556 %uint_999 = OpConstant %uint 999
557 %100 = OpFunction %void None %3
558 %10 = OpLabel
559 OpStore %var %uint_0
560 OpSelectionMerge %99 None
561 OpBranchConditional %5 %30 %40
562 %30 = OpLabel
563 OpStore %var %uint_1
564 OpBranch %99
565 %40 = OpLabel
566 OpStore %var %uint_2
567 OpBranch %99
568 %99 = OpLabel
569 OpStore %var %uint_999
570 OpReturn
571 OpFunctionEnd
572 )";
573 const std::string expected =
574 R"( OpCapability Shader
575 OpMemoryModel Logical Simple
576 OpEntryPoint Fragment %100 "main"
577 OpExecutionMode %100 OriginUpperLeft
578 OpName %1 "var"
579 %2 = OpTypeVoid
580 %3 = OpTypeFunction %2
581 %4 = OpTypeBool
582 %5 = OpConstantNull %4
583 %6 = OpConstantTrue %4
584 %7 = OpConstantFalse %4
585 %8 = OpTypeInt 32 0
586 %9 = OpTypeInt 32 1
587 %11 = OpConstant %8 42
588 %12 = OpConstant %9 42
589 %13 = OpTypeFunction %8
590 %14 = OpConstant %8 0
591 %15 = OpConstant %8 1
592 %16 = OpConstant %8 2
593 %17 = OpConstant %8 3
594 %18 = OpConstant %8 4
595 %19 = OpConstant %8 5
596 %20 = OpConstant %8 6
597 %21 = OpConstant %8 7
598 %22 = OpConstant %8 8
599 %23 = OpConstant %8 10
600 %24 = OpConstant %8 20
601 %25 = OpConstant %8 30
602 %26 = OpConstant %8 40
603 %27 = OpConstant %8 50
604 %28 = OpConstant %8 90
605 %29 = OpConstant %8 99
606 %31 = OpTypePointer Private %8
607 %1 = OpVariable %31 Private
608 %32 = OpConstant %8 999
609 %100 = OpFunction %2 None %3
610
611 %10 = OpLabel
612 OpStore %1 %14
613 OpSelectionMerge %99 None
614 OpBranchConditional %5 %30 %40
615
616 %30 = OpLabel
617 OpStore %1 %15
618 OpBranch %99
619
620 %40 = OpLabel
621 OpStore %1 %16
622 OpBranch %99
623
624 %99 = OpLabel
625 OpStore %1 %32
626 OpReturn
627 OpFunctionEnd
628 )";
629 EXPECT_THAT(EncodeAndDecodeSuccessfully(
630 input,
631 SPV_BINARY_TO_TEXT_OPTION_INDENT |
632 SPV_BINARY_TO_TEXT_OPTION_NESTED_INDENT,
633 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS),
634 expected);
635 }
636
637 TEST_F(IndentTest, NestedWhile) {
638 const std::string input = R"(
639 OpCapability Shader
640 OpMemoryModel Logical Simple
641 OpEntryPoint Fragment %100 "main"
642 OpExecutionMode %100 OriginUpperLeft
643 OpName %var "var"
644 %void = OpTypeVoid
645 %3 = OpTypeFunction %void
646 %bool = OpTypeBool
647 %5 = OpConstantNull %bool
648 %true = OpConstantTrue %bool
649 %false = OpConstantFalse %bool
650 %uint = OpTypeInt 32 0
651 %int = OpTypeInt 32 1
652 %uint_42 = OpConstant %uint 42
653 %int_42 = OpConstant %int 42
654 %13 = OpTypeFunction %uint
655 %uint_0 = OpConstant %uint 0
656 %uint_1 = OpConstant %uint 1
657 %uint_2 = OpConstant %uint 2
658 %uint_3 = OpConstant %uint 3
659 %uint_4 = OpConstant %uint 4
660 %uint_5 = OpConstant %uint 5
661 %uint_6 = OpConstant %uint 6
662 %uint_7 = OpConstant %uint 7
663 %uint_8 = OpConstant %uint 8
664 %uint_10 = OpConstant %uint 10
665 %uint_20 = OpConstant %uint 20
666 %uint_30 = OpConstant %uint 30
667 %uint_40 = OpConstant %uint 40
668 %uint_50 = OpConstant %uint 50
669 %uint_90 = OpConstant %uint 90
670 %uint_99 = OpConstant %uint 99
671 %_ptr_Private_uint = OpTypePointer Private %uint
672 %var = OpVariable %_ptr_Private_uint Private
673 %uint_999 = OpConstant %uint 999
674 %100 = OpFunction %void None %3
675 %10 = OpLabel
676 OpStore %var %uint_0
677 OpBranch %20
678 %20 = OpLabel
679 OpStore %var %uint_1
680 OpLoopMerge %99 %20 None
681 OpBranch %80
682 %80 = OpLabel
683 OpStore %var %uint_2
684 OpBranchConditional %5 %99 %20
685 %99 = OpLabel
686 OpStore %var %uint_3
687 OpReturn
688 OpFunctionEnd
689 )";
690 const std::string expected =
691 R"( OpCapability Shader
692 OpMemoryModel Logical Simple
693 OpEntryPoint Fragment %100 "main"
694 OpExecutionMode %100 OriginUpperLeft
695 OpName %1 "var"
696 %2 = OpTypeVoid
697 %3 = OpTypeFunction %2
698 %4 = OpTypeBool
699 %5 = OpConstantNull %4
700 %6 = OpConstantTrue %4
701 %7 = OpConstantFalse %4
702 %8 = OpTypeInt 32 0
703 %9 = OpTypeInt 32 1
704 %11 = OpConstant %8 42
705 %12 = OpConstant %9 42
706 %13 = OpTypeFunction %8
707 %14 = OpConstant %8 0
708 %15 = OpConstant %8 1
709 %16 = OpConstant %8 2
710 %17 = OpConstant %8 3
711 %18 = OpConstant %8 4
712 %19 = OpConstant %8 5
713 %21 = OpConstant %8 6
714 %22 = OpConstant %8 7
715 %23 = OpConstant %8 8
716 %24 = OpConstant %8 10
717 %25 = OpConstant %8 20
718 %26 = OpConstant %8 30
719 %27 = OpConstant %8 40
720 %28 = OpConstant %8 50
721 %29 = OpConstant %8 90
722 %30 = OpConstant %8 99
723 %31 = OpTypePointer Private %8
724 %1 = OpVariable %31 Private
725 %32 = OpConstant %8 999
726 %100 = OpFunction %2 None %3
727
728 %10 = OpLabel
729 OpStore %1 %14
730 OpBranch %20
731
732 %20 = OpLabel
733 OpStore %1 %15
734 OpLoopMerge %99 %20 None
735 OpBranch %80
736
737 %80 = OpLabel
738 OpStore %1 %16
739 OpBranchConditional %5 %99 %20
740
741 %99 = OpLabel
742 OpStore %1 %17
743 OpReturn
744 OpFunctionEnd
745 )";
746 EXPECT_THAT(EncodeAndDecodeSuccessfully(
747 input,
748 SPV_BINARY_TO_TEXT_OPTION_INDENT |
749 SPV_BINARY_TO_TEXT_OPTION_NESTED_INDENT,
750 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS),
751 expected);
752 }
753
754 TEST_F(IndentTest, NestedLoopInLoop) {
755 const std::string input = R"(
756 OpCapability Shader
757 OpMemoryModel Logical Simple
758 OpEntryPoint Fragment %100 "main"
759 OpExecutionMode %100 OriginUpperLeft
760 OpName %var "var"
761 %void = OpTypeVoid
762 %3 = OpTypeFunction %void
763 %bool = OpTypeBool
764 %5 = OpConstantNull %bool
765 %true = OpConstantTrue %bool
766 %false = OpConstantFalse %bool
767 %uint = OpTypeInt 32 0
768 %int = OpTypeInt 32 1
769 %uint_42 = OpConstant %uint 42
770 %int_42 = OpConstant %int 42
771 %13 = OpTypeFunction %uint
772 %uint_0 = OpConstant %uint 0
773 %uint_1 = OpConstant %uint 1
774 %uint_2 = OpConstant %uint 2
775 %uint_3 = OpConstant %uint 3
776 %uint_4 = OpConstant %uint 4
777 %uint_5 = OpConstant %uint 5
778 %uint_6 = OpConstant %uint 6
779 %uint_7 = OpConstant %uint 7
780 %uint_8 = OpConstant %uint 8
781 %uint_10 = OpConstant %uint 10
782 %uint_20 = OpConstant %uint 20
783 %uint_30 = OpConstant %uint 30
784 %uint_40 = OpConstant %uint 40
785 %uint_50 = OpConstant %uint 50
786 %uint_90 = OpConstant %uint 90
787 %uint_99 = OpConstant %uint 99
788 %_ptr_Private_uint = OpTypePointer Private %uint
789 %var = OpVariable %_ptr_Private_uint Private
790 %uint_999 = OpConstant %uint 999
791 %100 = OpFunction %void None %3
792 %10 = OpLabel
793 OpBranch %20
794 %20 = OpLabel
795 OpLoopMerge %99 %50 None
796 OpBranchConditional %5 %30 %99
797 %30 = OpLabel
798 OpLoopMerge %49 %40 None
799 OpBranchConditional %true %35 %49
800 %35 = OpLabel
801 OpBranch %37
802 %37 = OpLabel
803 OpBranch %40
804 %40 = OpLabel
805 OpBranch %30
806 %49 = OpLabel
807 OpBranch %50
808 %50 = OpLabel
809 OpBranch %20
810 %99 = OpLabel
811 OpReturn
812 OpFunctionEnd
813 )";
814 const std::string expected =
815 R"( OpCapability Shader
816 OpMemoryModel Logical Simple
817 OpEntryPoint Fragment %100 "main"
818 OpExecutionMode %100 OriginUpperLeft
819 OpName %1 "var"
820 %2 = OpTypeVoid
821 %3 = OpTypeFunction %2
822 %4 = OpTypeBool
823 %5 = OpConstantNull %4
824 %6 = OpConstantTrue %4
825 %7 = OpConstantFalse %4
826 %8 = OpTypeInt 32 0
827 %9 = OpTypeInt 32 1
828 %11 = OpConstant %8 42
829 %12 = OpConstant %9 42
830 %13 = OpTypeFunction %8
831 %14 = OpConstant %8 0
832 %15 = OpConstant %8 1
833 %16 = OpConstant %8 2
834 %17 = OpConstant %8 3
835 %18 = OpConstant %8 4
836 %19 = OpConstant %8 5
837 %21 = OpConstant %8 6
838 %22 = OpConstant %8 7
839 %23 = OpConstant %8 8
840 %24 = OpConstant %8 10
841 %25 = OpConstant %8 20
842 %26 = OpConstant %8 30
843 %27 = OpConstant %8 40
844 %28 = OpConstant %8 50
845 %29 = OpConstant %8 90
846 %31 = OpConstant %8 99
847 %32 = OpTypePointer Private %8
848 %1 = OpVariable %32 Private
849 %33 = OpConstant %8 999
850 %100 = OpFunction %2 None %3
851
852 %10 = OpLabel
853 OpBranch %20
854
855 %20 = OpLabel
856 OpLoopMerge %99 %50 None
857 OpBranchConditional %5 %30 %99
858
859 %30 = OpLabel
860 OpLoopMerge %49 %40 None
861 OpBranchConditional %6 %35 %49
862
863 %35 = OpLabel
864 OpBranch %37
865
866 %37 = OpLabel
867 OpBranch %40
868
869 %40 = OpLabel
870 OpBranch %30
871
872 %49 = OpLabel
873 OpBranch %50
874
875 %50 = OpLabel
876 OpBranch %20
877
878 %99 = OpLabel
879 OpReturn
880 OpFunctionEnd
881 )";
882 EXPECT_THAT(EncodeAndDecodeSuccessfully(
883 input,
884 SPV_BINARY_TO_TEXT_OPTION_INDENT |
885 SPV_BINARY_TO_TEXT_OPTION_NESTED_INDENT,
886 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS),
887 expected);
888 }
889
890 TEST_F(IndentTest, NestedSwitch) {
891 const std::string input = R"(
892 OpCapability Shader
893 OpMemoryModel Logical Simple
894 OpEntryPoint Fragment %100 "main"
895 OpExecutionMode %100 OriginUpperLeft
896 OpName %var "var"
897 %void = OpTypeVoid
898 %3 = OpTypeFunction %void
899 %bool = OpTypeBool
900 %5 = OpConstantNull %bool
901 %true = OpConstantTrue %bool
902 %false = OpConstantFalse %bool
903 %uint = OpTypeInt 32 0
904 %int = OpTypeInt 32 1
905 %uint_42 = OpConstant %uint 42
906 %int_42 = OpConstant %int 42
907 %13 = OpTypeFunction %uint
908 %uint_0 = OpConstant %uint 0
909 %uint_1 = OpConstant %uint 1
910 %uint_2 = OpConstant %uint 2
911 %uint_3 = OpConstant %uint 3
912 %uint_4 = OpConstant %uint 4
913 %uint_5 = OpConstant %uint 5
914 %uint_6 = OpConstant %uint 6
915 %uint_7 = OpConstant %uint 7
916 %uint_8 = OpConstant %uint 8
917 %uint_10 = OpConstant %uint 10
918 %uint_20 = OpConstant %uint 20
919 %uint_30 = OpConstant %uint 30
920 %uint_40 = OpConstant %uint 40
921 %uint_50 = OpConstant %uint 50
922 %uint_90 = OpConstant %uint 90
923 %uint_99 = OpConstant %uint 99
924 %_ptr_Private_uint = OpTypePointer Private %uint
925 %var = OpVariable %_ptr_Private_uint Private
926 %uint_999 = OpConstant %uint 999
927 %100 = OpFunction %void None %3
928 %10 = OpLabel
929 OpSelectionMerge %99 None
930 OpSwitch %uint_42 %80 20 %20 30 %30
931 %20 = OpLabel
932 OpBranch %80
933 %80 = OpLabel
934 OpBranch %30
935 %30 = OpLabel
936 OpBranch %99
937 %99 = OpLabel
938 OpReturn
939 OpFunctionEnd
940 )";
941 const std::string expected =
942 R"( OpCapability Shader
943 OpMemoryModel Logical Simple
944 OpEntryPoint Fragment %100 "main"
945 OpExecutionMode %100 OriginUpperLeft
946 OpName %1 "var"
947 %2 = OpTypeVoid
948 %3 = OpTypeFunction %2
949 %4 = OpTypeBool
950 %5 = OpConstantNull %4
951 %6 = OpConstantTrue %4
952 %7 = OpConstantFalse %4
953 %8 = OpTypeInt 32 0
954 %9 = OpTypeInt 32 1
955 %11 = OpConstant %8 42
956 %12 = OpConstant %9 42
957 %13 = OpTypeFunction %8
958 %14 = OpConstant %8 0
959 %15 = OpConstant %8 1
960 %16 = OpConstant %8 2
961 %17 = OpConstant %8 3
962 %18 = OpConstant %8 4
963 %19 = OpConstant %8 5
964 %21 = OpConstant %8 6
965 %22 = OpConstant %8 7
966 %23 = OpConstant %8 8
967 %24 = OpConstant %8 10
968 %25 = OpConstant %8 20
969 %26 = OpConstant %8 30
970 %27 = OpConstant %8 40
971 %28 = OpConstant %8 50
972 %29 = OpConstant %8 90
973 %31 = OpConstant %8 99
974 %32 = OpTypePointer Private %8
975 %1 = OpVariable %32 Private
976 %33 = OpConstant %8 999
977 %100 = OpFunction %2 None %3
978
979 %10 = OpLabel
980 OpSelectionMerge %99 None
981 OpSwitch %11 %80 20 %20 30 %30
982
983 %20 = OpLabel
984 OpBranch %80
985
986 %80 = OpLabel
987 OpBranch %30
988
989 %30 = OpLabel
990 OpBranch %99
991
992 %99 = OpLabel
993 OpReturn
994 OpFunctionEnd
995 )";
996 EXPECT_THAT(EncodeAndDecodeSuccessfully(
997 input,
998 SPV_BINARY_TO_TEXT_OPTION_INDENT |
999 SPV_BINARY_TO_TEXT_OPTION_NESTED_INDENT,
1000 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS),
1001 expected);
1002 }
1003
1004 TEST_F(IndentTest, ReorderedIf) {
1005 const std::string input = R"(
1006 OpCapability Shader
1007 OpMemoryModel Logical Simple
1008 OpEntryPoint Fragment %100 "main"
1009 OpExecutionMode %100 OriginUpperLeft
1010 OpName %1 "var"
1011 %2 = OpTypeVoid
1012 %3 = OpTypeFunction %2
1013 %4 = OpTypeBool
1014 %5 = OpConstantNull %4
1015 %6 = OpConstantTrue %4
1016 %7 = OpConstantFalse %4
1017 %8 = OpTypeInt 32 0
1018 %9 = OpTypeInt 32 1
1019 %11 = OpConstant %8 42
1020 %12 = OpConstant %9 42
1021 %13 = OpTypeFunction %8
1022 %14 = OpConstant %8 0
1023 %15 = OpConstant %8 1
1024 %16 = OpConstant %8 2
1025 %17 = OpConstant %8 3
1026 %18 = OpConstant %8 4
1027 %19 = OpConstant %8 5
1028 %21 = OpConstant %8 6
1029 %22 = OpConstant %8 7
1030 %23 = OpConstant %8 8
1031 %24 = OpConstant %8 10
1032 %25 = OpConstant %8 20
1033 %26 = OpConstant %8 30
1034 %27 = OpConstant %8 40
1035 %28 = OpConstant %8 50
1036 %29 = OpConstant %8 90
1037 %31 = OpConstant %8 99
1038 %32 = OpTypePointer Private %8
1039 %1 = OpVariable %32 Private
1040 %33 = OpConstant %8 999
1041 %100 = OpFunction %2 None %3
1042 %10 = OpLabel
1043 OpSelectionMerge %99 None
1044 OpBranchConditional %5 %20 %50
1045 %99 = OpLabel
1046 OpReturn
1047 %20 = OpLabel
1048 OpSelectionMerge %49 None
1049 OpBranchConditional %5 %30 %40
1050 %49 = OpLabel
1051 OpBranch %99
1052 %40 = OpLabel
1053 OpBranch %49
1054 %30 = OpLabel
1055 OpBranch %49
1056 %50 = OpLabel
1057 OpSelectionMerge %79 None
1058 OpBranchConditional %5 %60 %70
1059 %79 = OpLabel
1060 OpBranch %99
1061 %60 = OpLabel
1062 OpBranch %79
1063 %70 = OpLabel
1064 OpBranch %79
1065 OpFunctionEnd
1066 )";
1067 const std::string expected =
1068 R"( OpCapability Shader
1069 OpMemoryModel Logical Simple
1070 OpEntryPoint Fragment %100 "main"
1071 OpExecutionMode %100 OriginUpperLeft
1072 OpName %1 "var"
1073 %2 = OpTypeVoid
1074 %3 = OpTypeFunction %2
1075 %4 = OpTypeBool
1076 %5 = OpConstantNull %4
1077 %6 = OpConstantTrue %4
1078 %7 = OpConstantFalse %4
1079 %8 = OpTypeInt 32 0
1080 %9 = OpTypeInt 32 1
1081 %11 = OpConstant %8 42
1082 %12 = OpConstant %9 42
1083 %13 = OpTypeFunction %8
1084 %14 = OpConstant %8 0
1085 %15 = OpConstant %8 1
1086 %16 = OpConstant %8 2
1087 %17 = OpConstant %8 3
1088 %18 = OpConstant %8 4
1089 %19 = OpConstant %8 5
1090 %21 = OpConstant %8 6
1091 %22 = OpConstant %8 7
1092 %23 = OpConstant %8 8
1093 %24 = OpConstant %8 10
1094 %25 = OpConstant %8 20
1095 %26 = OpConstant %8 30
1096 %27 = OpConstant %8 40
1097 %28 = OpConstant %8 50
1098 %29 = OpConstant %8 90
1099 %31 = OpConstant %8 99
1100 %32 = OpTypePointer Private %8
1101 %1 = OpVariable %32 Private
1102 %33 = OpConstant %8 999
1103 %100 = OpFunction %2 None %3
1104 %10 = OpLabel
1105 OpSelectionMerge %99 None
1106 OpBranchConditional %5 %20 %50
1107 %20 = OpLabel
1108 OpSelectionMerge %49 None
1109 OpBranchConditional %5 %30 %40
1110 %30 = OpLabel
1111 OpBranch %49
1112 %40 = OpLabel
1113 OpBranch %49
1114 %49 = OpLabel
1115 OpBranch %99
1116 %50 = OpLabel
1117 OpSelectionMerge %79 None
1118 OpBranchConditional %5 %60 %70
1119 %60 = OpLabel
1120 OpBranch %79
1121 %70 = OpLabel
1122 OpBranch %79
1123 %79 = OpLabel
1124 OpBranch %99
1125 %99 = OpLabel
1126 OpReturn
1127 OpFunctionEnd
1128 )";
1129 EXPECT_THAT(EncodeAndDecodeSuccessfully(
1130 input,
1131 SPV_BINARY_TO_TEXT_OPTION_INDENT |
1132 SPV_BINARY_TO_TEXT_OPTION_REORDER_BLOCKS,
1133 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS),
1134 expected);
1135 }
1136
1137 TEST_F(IndentTest, ReorderedFallThroughInSwitch) {
1138 const std::string input = R"(
1139 OpCapability Shader
1140 OpMemoryModel Logical Simple
1141 OpEntryPoint Fragment %100 "main"
1142 OpExecutionMode %100 OriginUpperLeft
1143 OpName %1 "var"
1144 %2 = OpTypeVoid
1145 %3 = OpTypeFunction %2
1146 %4 = OpTypeBool
1147 %5 = OpConstantNull %4
1148 %6 = OpConstantTrue %4
1149 %7 = OpConstantFalse %4
1150 %8 = OpTypeInt 32 0
1151 %9 = OpTypeInt 32 1
1152 %11 = OpConstant %8 42
1153 %12 = OpConstant %9 42
1154 %13 = OpTypeFunction %8
1155 %14 = OpConstant %8 0
1156 %15 = OpConstant %8 1
1157 %16 = OpConstant %8 2
1158 %17 = OpConstant %8 3
1159 %18 = OpConstant %8 4
1160 %19 = OpConstant %8 5
1161 %21 = OpConstant %8 6
1162 %22 = OpConstant %8 7
1163 %23 = OpConstant %8 8
1164 %24 = OpConstant %8 10
1165 %25 = OpConstant %8 20
1166 %26 = OpConstant %8 30
1167 %27 = OpConstant %8 40
1168 %28 = OpConstant %8 50
1169 %29 = OpConstant %8 90
1170 %31 = OpConstant %8 99
1171 %32 = OpTypePointer Private %8
1172 %1 = OpVariable %32 Private
1173 %33 = OpConstant %8 999
1174 %100 = OpFunction %2 None %3
1175 %10 = OpLabel
1176 OpSelectionMerge %99 None
1177 OpSwitch %11 %50 20 %20 50 %50
1178 %99 = OpLabel
1179 OpReturn
1180 %20 = OpLabel
1181 OpSelectionMerge %49 None
1182 OpBranchConditional %5 %30 %40
1183 %49 = OpLabel
1184 OpBranchConditional %5 %99 %50
1185 %30 = OpLabel
1186 OpBranch %49
1187 %40 = OpLabel
1188 OpBranch %49
1189 %50 = OpLabel
1190 OpSelectionMerge %79 None
1191 OpBranchConditional %5 %60 %70
1192 %79 = OpLabel
1193 OpBranch %99
1194 %60 = OpLabel
1195 OpBranch %79
1196 %70 = OpLabel
1197 OpBranch %79
1198 OpFunctionEnd
1199 )";
1200 const std::string expected =
1201 R"( OpCapability Shader
1202 OpMemoryModel Logical Simple
1203 OpEntryPoint Fragment %100 "main"
1204 OpExecutionMode %100 OriginUpperLeft
1205 OpName %1 "var"
1206 %2 = OpTypeVoid
1207 %3 = OpTypeFunction %2
1208 %4 = OpTypeBool
1209 %5 = OpConstantNull %4
1210 %6 = OpConstantTrue %4
1211 %7 = OpConstantFalse %4
1212 %8 = OpTypeInt 32 0
1213 %9 = OpTypeInt 32 1
1214 %11 = OpConstant %8 42
1215 %12 = OpConstant %9 42
1216 %13 = OpTypeFunction %8
1217 %14 = OpConstant %8 0
1218 %15 = OpConstant %8 1
1219 %16 = OpConstant %8 2
1220 %17 = OpConstant %8 3
1221 %18 = OpConstant %8 4
1222 %19 = OpConstant %8 5
1223 %21 = OpConstant %8 6
1224 %22 = OpConstant %8 7
1225 %23 = OpConstant %8 8
1226 %24 = OpConstant %8 10
1227 %25 = OpConstant %8 20
1228 %26 = OpConstant %8 30
1229 %27 = OpConstant %8 40
1230 %28 = OpConstant %8 50
1231 %29 = OpConstant %8 90
1232 %31 = OpConstant %8 99
1233 %32 = OpTypePointer Private %8
1234 %1 = OpVariable %32 Private
1235 %33 = OpConstant %8 999
1236 %100 = OpFunction %2 None %3
1237 %10 = OpLabel
1238 OpSelectionMerge %99 None
1239 OpSwitch %11 %50 20 %20 50 %50
1240 %20 = OpLabel
1241 OpSelectionMerge %49 None
1242 OpBranchConditional %5 %30 %40
1243 %30 = OpLabel
1244 OpBranch %49
1245 %40 = OpLabel
1246 OpBranch %49
1247 %49 = OpLabel
1248 OpBranchConditional %5 %99 %50
1249 %50 = OpLabel
1250 OpSelectionMerge %79 None
1251 OpBranchConditional %5 %60 %70
1252 %60 = OpLabel
1253 OpBranch %79
1254 %70 = OpLabel
1255 OpBranch %79
1256 %79 = OpLabel
1257 OpBranch %99
1258 %99 = OpLabel
1259 OpReturn
1260 OpFunctionEnd
1261 )";
1262 EXPECT_THAT(EncodeAndDecodeSuccessfully(
1263 input,
1264 SPV_BINARY_TO_TEXT_OPTION_INDENT |
1265 SPV_BINARY_TO_TEXT_OPTION_REORDER_BLOCKS,
1266 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS),
1267 expected);
1268 }
1269
1270 TEST_F(IndentTest, ReorderedNested) {
1271 const std::string input = R"(
1272 OpCapability Shader
1273 %1 = OpExtInstImport "GLSL.std.450"
1274 OpMemoryModel Logical GLSL450
1275 OpEntryPoint Fragment %4 "main" %204
1276 OpExecutionMode %4 OriginUpperLeft
1277 OpSource GLSL 450
1278 OpName %4 "main"
1279 OpName %16 "ff(vf2;f1;"
1280 OpName %14 "g"
1281 OpName %15 "f"
1282 OpName %19 "vg"
1283 OpName %20 "Block140"
1284 OpMemberName %20 0 "a"
1285 OpMemberName %20 1 "b"
1286 OpName %22 "b140"
1287 OpName %35 "sv"
1288 OpName %39 "s"
1289 OpName %46 "f"
1290 OpName %51 "g"
1291 OpName %57 "x"
1292 OpName %69 "param"
1293 OpName %75 "i"
1294 OpName %80 "vc"
1295 OpName %88 "j"
1296 OpName %95 "size"
1297 OpName %174 "v"
1298 OpName %187 "i"
1299 OpName %204 "o_color"
1300 OpMemberDecorate %20 0 Offset 0
1301 OpMemberDecorate %20 1 Offset 16
1302 OpDecorate %20 Block
1303 OpDecorate %22 DescriptorSet 1
1304 OpDecorate %22 Binding 0
1305 OpDecorate %39 DescriptorSet 0
1306 OpDecorate %39 Binding 1
1307 OpDecorate %95 SpecId 20
1308 OpDecorate %204 Location 2
1309 %2 = OpTypeVoid
1310 %3 = OpTypeFunction %2
1311 %6 = OpTypeFloat 32
1312 %7 = OpTypeVector %6 2
1313 %8 = OpTypePointer Function %7
1314 %9 = OpTypeVector %6 4
1315 %10 = OpTypeInt 32 0
1316 %11 = OpConstant %10 2
1317 %12 = OpTypeArray %9 %11
1318 %13 = OpTypeFunction %12 %8 %6
1319 %18 = OpTypePointer Private %9
1320 %19 = OpVariable %18 Private
1321 %20 = OpTypeStruct %6 %9
1322 %21 = OpTypePointer Uniform %20
1323 %22 = OpVariable %21 Uniform
1324 %23 = OpTypeInt 32 1
1325 %24 = OpConstant %23 1
1326 %25 = OpTypePointer Uniform %9
1327 %28 = OpConstant %6 0
1328 %29 = OpConstantComposite %9 %28 %28 %28 %28
1329 %34 = OpTypePointer Function %9
1330 %36 = OpTypeImage %6 2D 0 0 0 1 Unknown
1331 %37 = OpTypeSampledImage %36
1332 %38 = OpTypePointer UniformConstant %37
1333 %39 = OpVariable %38 UniformConstant
1334 %41 = OpConstantComposite %7 %28 %28
1335 %45 = OpTypePointer Function %6
1336 %47 = OpConstant %23 0
1337 %48 = OpTypePointer Uniform %6
1338 %53 = OpConstant %6 1
1339 %55 = OpTypeBool
1340 %56 = OpTypePointer Function %55
1341 %58 = OpConstant %10 0
1342 %59 = OpTypePointer Private %6
1343 %74 = OpTypePointer Function %23
1344 %87 = OpTypePointer Function %10
1345 %95 = OpSpecConstant %10 2
1346 %100 = OpConstant %10 1
1347 %109 = OpConstantComposite %9 %53 %53 %53 %53
1348 %127 = OpConstant %23 10
1349 %139 = OpConstant %6 2
1350 %143 = OpConstant %6 3
1351 %158 = OpConstant %6 4
1352 %177 = OpConstant %6 0.5
1353 %195 = OpConstant %23 100
1354 %202 = OpTypeVector %10 4
1355 %203 = OpTypePointer Output %202
1356 %204 = OpVariable %203 Output
1357 %4 = OpFunction %2 None %3
1358 %5 = OpLabel
1359 %35 = OpVariable %34 Function
1360 %46 = OpVariable %45 Function
1361 %51 = OpVariable %45 Function
1362 %57 = OpVariable %56 Function
1363 %69 = OpVariable %8 Function
1364 %75 = OpVariable %74 Function
1365 %80 = OpVariable %34 Function
1366 %88 = OpVariable %87 Function
1367 %174 = OpVariable %45 Function
1368 %187 = OpVariable %74 Function
1369 %26 = OpAccessChain %25 %22 %24
1370 %27 = OpLoad %9 %26
1371 OpStore %19 %27
1372 %40 = OpLoad %37 %39
1373 %42 = OpImageSampleImplicitLod %9 %40 %41
1374 %43 = OpLoad %9 %19
1375 %44 = OpFAdd %9 %42 %43
1376 OpStore %35 %44
1377 %49 = OpAccessChain %48 %22 %47
1378 %50 = OpLoad %6 %49
1379 OpStore %46 %50
1380 %52 = OpLoad %6 %46
1381 %54 = OpFAdd %6 %52 %53
1382 OpStore %51 %54
1383 %60 = OpAccessChain %59 %19 %58
1384 %61 = OpLoad %6 %60
1385 %62 = OpFOrdGreaterThan %55 %61 %28
1386 OpSelectionMerge %64 None
1387 OpBranchConditional %62 %63 %64
1388 %64 = OpLabel
1389 %73 = OpPhi %55 %62 %5 %72 %63
1390 OpStore %57 %73
1391 OpStore %75 %47
1392 OpBranch %76
1393 %197 = OpLabel
1394 OpBranch %190
1395 %63 = OpLabel
1396 %65 = OpLoad %6 %46
1397 %66 = OpLoad %6 %51
1398 %67 = OpCompositeConstruct %7 %65 %66
1399 %68 = OpLoad %6 %51
1400 OpStore %69 %67
1401 %70 = OpFunctionCall %12 %16 %69 %68
1402 %71 = OpCompositeExtract %6 %70 0 0
1403 %72 = OpFOrdGreaterThan %55 %71 %28
1404 OpBranch %64
1405 %77 = OpLabel
1406 %81 = OpLoad %9 %19
1407 OpStore %80 %81
1408 %82 = OpAccessChain %45 %80 %58
1409 %83 = OpLoad %6 %82
1410 %84 = OpFOrdGreaterThan %55 %83 %28
1411 OpSelectionMerge %86 None
1412 OpBranchConditional %84 %85 %113
1413 %85 = OpLabel
1414 OpStore %88 %58
1415 OpBranch %89
1416 %89 = OpLabel
1417 OpLoopMerge %91 %92 None
1418 OpBranch %93
1419 %93 = OpLabel
1420 %94 = OpLoad %10 %88
1421 %96 = OpULessThan %55 %94 %95
1422 OpBranchConditional %96 %90 %91
1423 %105 = OpLabel
1424 OpBranch %92
1425 %198 = OpLabel
1426 OpBranch %191
1427 %163 = OpLabel
1428 OpBranch %136
1429 %104 = OpLabel
1430 OpBranch %91
1431 %76 = OpLabel
1432 OpLoopMerge %78 %79 None
1433 OpBranch %77
1434 %92 = OpLabel
1435 %107 = OpLoad %10 %88
1436 %108 = OpIAdd %10 %107 %24
1437 OpStore %88 %108
1438 OpBranch %89
1439 %91 = OpLabel
1440 %110 = OpLoad %9 %80
1441 %111 = OpFAdd %9 %110 %109
1442 OpStore %80 %111
1443 OpBranch %79
1444 %113 = OpLabel
1445 %114 = OpLoad %9 %80
1446 %115 = OpFSub %9 %114 %109
1447 OpStore %80 %115
1448 OpBranch %86
1449 %132 = OpLabel
1450 %137 = OpLoad %6 %51
1451 %138 = OpFAdd %6 %137 %53
1452 OpStore %51 %138
1453 OpBranch %133
1454 %86 = OpLabel
1455 %116 = OpAccessChain %45 %80 %100
1456 %117 = OpLoad %6 %116
1457 %118 = OpFOrdGreaterThan %55 %117 %28
1458 OpSelectionMerge %120 None
1459 OpBranchConditional %118 %119 %120
1460 %119 = OpLabel
1461 OpBranch %78
1462 %120 = OpLabel
1463 %122 = OpAccessChain %45 %80 %11
1464 %123 = OpLoad %6 %122
1465 %124 = OpFAdd %6 %123 %53
1466 %125 = OpAccessChain %45 %80 %11
1467 OpStore %125 %124
1468 OpBranch %79
1469 %79 = OpLabel
1470 %126 = OpLoad %23 %75
1471 %128 = OpSLessThan %55 %126 %127
1472 OpBranchConditional %128 %76 %78
1473 %78 = OpLabel
1474 %129 = OpAccessChain %48 %22 %47
1475 %130 = OpLoad %6 %129
1476 %131 = OpConvertFToS %23 %130
1477 OpSelectionMerge %136 None
1478 OpSwitch %131 %135 0 %132 1 %132 2 %132 3 %133 4 %134
1479 %90 = OpLabel
1480 %97 = OpLoad %9 %19
1481 %98 = OpLoad %9 %80
1482 %99 = OpFAdd %9 %98 %97
1483 OpStore %80 %99
1484 %101 = OpAccessChain %45 %80 %100
1485 %102 = OpLoad %6 %101
1486 %103 = OpFOrdLessThan %55 %102 %28
1487 OpSelectionMerge %105 None
1488 OpBranchConditional %103 %104 %105
1489 %161 = OpLabel
1490 OpLoopMerge %163 %164 None
1491 OpBranch %165
1492 %165 = OpLabel
1493 %166 = OpLoad %6 %51
1494 %167 = OpFOrdLessThan %55 %166 %139
1495 OpBranchConditional %167 %162 %163
1496 %164 = OpLabel
1497 OpBranch %161
1498 %162 = OpLabel
1499 %168 = OpLoad %6 %46
1500 %169 = OpFOrdLessThan %55 %168 %53
1501 OpSelectionMerge %171 None
1502 OpBranchConditional %169 %170 %171
1503 %135 = OpLabel
1504 %159 = OpLoad %6 %51
1505 %160 = OpFAdd %6 %159 %158
1506 OpStore %51 %160
1507 OpBranch %161
1508 %133 = OpLabel
1509 %140 = OpLoad %6 %51
1510 %141 = OpFAdd %6 %140 %139
1511 OpStore %51 %141
1512 OpBranch %136
1513 %134 = OpLabel
1514 %144 = OpLoad %6 %51
1515 %145 = OpFAdd %6 %144 %143
1516 OpStore %51 %145
1517 OpBranch %146
1518 %146 = OpLabel
1519 OpLoopMerge %148 %149 None
1520 OpBranch %150
1521 %150 = OpLabel
1522 %151 = OpLoad %6 %51
1523 %152 = OpFOrdLessThan %55 %151 %139
1524 OpBranchConditional %152 %147 %148
1525 %147 = OpLabel
1526 %153 = OpLoad %6 %46
1527 %154 = OpFOrdLessThan %55 %153 %53
1528 OpSelectionMerge %156 None
1529 OpBranchConditional %154 %155 %156
1530 %155 = OpLabel
1531 OpBranch %148
1532 %156 = OpLabel
1533 OpBranch %149
1534 %149 = OpLabel
1535 OpBranch %146
1536 %148 = OpLabel
1537 OpBranch %135
1538 %136 = OpLabel
1539 OpStore %174 %53
1540 %175 = OpAccessChain %45 %35 %58
1541 %176 = OpLoad %6 %175
1542 %178 = OpFOrdLessThanEqual %55 %176 %177
1543 OpSelectionMerge %180 None
1544 OpBranchConditional %178 %179 %181
1545 %179 = OpLabel
1546 OpStore %174 %28
1547 OpBranch %180
1548 %185 = OpLabel
1549 OpStore %174 %139
1550 OpBranch %186
1551 %181 = OpLabel
1552 %182 = OpAccessChain %45 %35 %58
1553 %183 = OpLoad %6 %182
1554 %184 = OpFOrdGreaterThanEqual %55 %183 %177
1555 OpSelectionMerge %186 None
1556 OpBranchConditional %184 %185 %186
1557 %170 = OpLabel
1558 OpBranch %163
1559 %171 = OpLabel
1560 OpBranch %164
1561 %186 = OpLabel
1562 OpBranch %180
1563 %188 = OpLabel
1564 OpLoopMerge %190 %191 None
1565 OpBranch %189
1566 %189 = OpLabel
1567 %192 = OpLoad %9 %19
1568 %193 = OpFAdd %9 %192 %109
1569 OpStore %19 %193
1570 %194 = OpLoad %23 %187
1571 %196 = OpSGreaterThan %55 %194 %195
1572 OpSelectionMerge %198 None
1573 OpBranchConditional %196 %197 %198
1574 %180 = OpLabel
1575 OpStore %187 %47
1576 OpBranch %188
1577 %191 = OpLabel
1578 %200 = OpLoad %23 %187
1579 %201 = OpIAdd %23 %200 %24
1580 OpStore %187 %201
1581 OpBranch %188
1582 %190 = OpLabel
1583 OpReturn
1584 OpFunctionEnd
1585 %16 = OpFunction %12 None %13
1586 %14 = OpFunctionParameter %8
1587 %15 = OpFunctionParameter %6
1588 %17 = OpLabel
1589 %30 = OpCompositeConstruct %9 %15 %15 %15 %15
1590 %31 = OpCompositeConstruct %12 %29 %30
1591 OpReturnValue %31
1592 OpFunctionEnd
1593 )";
1594 const std::string expected =
1595 R"( OpCapability Shader
1596 %1 = OpExtInstImport "GLSL.std.450"
1597 OpMemoryModel Logical GLSL450
1598 OpEntryPoint Fragment %4 "main" %204
1599 OpExecutionMode %4 OriginUpperLeft
1600 OpSource GLSL 450
1601 OpName %4 "main"
1602 OpName %16 "ff(vf2;f1;"
1603 OpName %14 "g"
1604 OpName %15 "f"
1605 OpName %19 "vg"
1606 OpName %20 "Block140"
1607 OpMemberName %20 0 "a"
1608 OpMemberName %20 1 "b"
1609 OpName %22 "b140"
1610 OpName %35 "sv"
1611 OpName %39 "s"
1612 OpName %46 "f"
1613 OpName %51 "g"
1614 OpName %57 "x"
1615 OpName %69 "param"
1616 OpName %75 "i"
1617 OpName %80 "vc"
1618 OpName %88 "j"
1619 OpName %95 "size"
1620 OpName %174 "v"
1621 OpName %187 "i"
1622 OpName %204 "o_color"
1623 OpMemberDecorate %20 0 Offset 0
1624 OpMemberDecorate %20 1 Offset 16
1625 OpDecorate %20 Block
1626 OpDecorate %22 DescriptorSet 1
1627 OpDecorate %22 Binding 0
1628 OpDecorate %39 DescriptorSet 0
1629 OpDecorate %39 Binding 1
1630 OpDecorate %95 SpecId 20
1631 OpDecorate %204 Location 2
1632 %2 = OpTypeVoid
1633 %3 = OpTypeFunction %2
1634 %6 = OpTypeFloat 32
1635 %7 = OpTypeVector %6 2
1636 %8 = OpTypePointer Function %7
1637 %9 = OpTypeVector %6 4
1638 %10 = OpTypeInt 32 0
1639 %11 = OpConstant %10 2
1640 %12 = OpTypeArray %9 %11
1641 %13 = OpTypeFunction %12 %8 %6
1642 %18 = OpTypePointer Private %9
1643 %19 = OpVariable %18 Private
1644 %20 = OpTypeStruct %6 %9
1645 %21 = OpTypePointer Uniform %20
1646 %22 = OpVariable %21 Uniform
1647 %23 = OpTypeInt 32 1
1648 %24 = OpConstant %23 1
1649 %25 = OpTypePointer Uniform %9
1650 %28 = OpConstant %6 0
1651 %29 = OpConstantComposite %9 %28 %28 %28 %28
1652 %34 = OpTypePointer Function %9
1653 %36 = OpTypeImage %6 2D 0 0 0 1 Unknown
1654 %37 = OpTypeSampledImage %36
1655 %38 = OpTypePointer UniformConstant %37
1656 %39 = OpVariable %38 UniformConstant
1657 %41 = OpConstantComposite %7 %28 %28
1658 %45 = OpTypePointer Function %6
1659 %47 = OpConstant %23 0
1660 %48 = OpTypePointer Uniform %6
1661 %53 = OpConstant %6 1
1662 %55 = OpTypeBool
1663 %56 = OpTypePointer Function %55
1664 %58 = OpConstant %10 0
1665 %59 = OpTypePointer Private %6
1666 %74 = OpTypePointer Function %23
1667 %87 = OpTypePointer Function %10
1668 %95 = OpSpecConstant %10 2
1669 %100 = OpConstant %10 1
1670 %109 = OpConstantComposite %9 %53 %53 %53 %53
1671 %127 = OpConstant %23 10
1672 %139 = OpConstant %6 2
1673 %143 = OpConstant %6 3
1674 %158 = OpConstant %6 4
1675 %177 = OpConstant %6 0.5
1676 %195 = OpConstant %23 100
1677 %202 = OpTypeVector %10 4
1678 %203 = OpTypePointer Output %202
1679 %204 = OpVariable %203 Output
1680 %4 = OpFunction %2 None %3
1681
1682 %5 = OpLabel
1683 %35 = OpVariable %34 Function
1684 %46 = OpVariable %45 Function
1685 %51 = OpVariable %45 Function
1686 %57 = OpVariable %56 Function
1687 %69 = OpVariable %8 Function
1688 %75 = OpVariable %74 Function
1689 %80 = OpVariable %34 Function
1690 %88 = OpVariable %87 Function
1691 %174 = OpVariable %45 Function
1692 %187 = OpVariable %74 Function
1693 %26 = OpAccessChain %25 %22 %24
1694 %27 = OpLoad %9 %26
1695 OpStore %19 %27
1696 %40 = OpLoad %37 %39
1697 %42 = OpImageSampleImplicitLod %9 %40 %41
1698 %43 = OpLoad %9 %19
1699 %44 = OpFAdd %9 %42 %43
1700 OpStore %35 %44
1701 %49 = OpAccessChain %48 %22 %47
1702 %50 = OpLoad %6 %49
1703 OpStore %46 %50
1704 %52 = OpLoad %6 %46
1705 %54 = OpFAdd %6 %52 %53
1706 OpStore %51 %54
1707 %60 = OpAccessChain %59 %19 %58
1708 %61 = OpLoad %6 %60
1709 %62 = OpFOrdGreaterThan %55 %61 %28
1710 OpSelectionMerge %64 None
1711 OpBranchConditional %62 %63 %64
1712
1713 %63 = OpLabel
1714 %65 = OpLoad %6 %46
1715 %66 = OpLoad %6 %51
1716 %67 = OpCompositeConstruct %7 %65 %66
1717 %68 = OpLoad %6 %51
1718 OpStore %69 %67
1719 %70 = OpFunctionCall %12 %16 %69 %68
1720 %71 = OpCompositeExtract %6 %70 0 0
1721 %72 = OpFOrdGreaterThan %55 %71 %28
1722 OpBranch %64
1723
1724 %64 = OpLabel
1725 %73 = OpPhi %55 %62 %5 %72 %63
1726 OpStore %57 %73
1727 OpStore %75 %47
1728 OpBranch %76
1729
1730 %76 = OpLabel
1731 OpLoopMerge %78 %79 None
1732 OpBranch %77
1733
1734 %77 = OpLabel
1735 %81 = OpLoad %9 %19
1736 OpStore %80 %81
1737 %82 = OpAccessChain %45 %80 %58
1738 %83 = OpLoad %6 %82
1739 %84 = OpFOrdGreaterThan %55 %83 %28
1740 OpSelectionMerge %86 None
1741 OpBranchConditional %84 %85 %113
1742
1743 %85 = OpLabel
1744 OpStore %88 %58
1745 OpBranch %89
1746
1747 %89 = OpLabel
1748 OpLoopMerge %91 %92 None
1749 OpBranch %93
1750
1751 %93 = OpLabel
1752 %94 = OpLoad %10 %88
1753 %96 = OpULessThan %55 %94 %95
1754 OpBranchConditional %96 %90 %91
1755
1756 %90 = OpLabel
1757 %97 = OpLoad %9 %19
1758 %98 = OpLoad %9 %80
1759 %99 = OpFAdd %9 %98 %97
1760 OpStore %80 %99
1761 %101 = OpAccessChain %45 %80 %100
1762 %102 = OpLoad %6 %101
1763 %103 = OpFOrdLessThan %55 %102 %28
1764 OpSelectionMerge %105 None
1765 OpBranchConditional %103 %104 %105
1766
1767 %104 = OpLabel
1768 OpBranch %91
1769
1770 %105 = OpLabel
1771 OpBranch %92
1772
1773 %92 = OpLabel
1774 %107 = OpLoad %10 %88
1775 %108 = OpIAdd %10 %107 %24
1776 OpStore %88 %108
1777 OpBranch %89
1778
1779 %91 = OpLabel
1780 %110 = OpLoad %9 %80
1781 %111 = OpFAdd %9 %110 %109
1782 OpStore %80 %111
1783 OpBranch %79
1784
1785 %113 = OpLabel
1786 %114 = OpLoad %9 %80
1787 %115 = OpFSub %9 %114 %109
1788 OpStore %80 %115
1789 OpBranch %86
1790
1791 %86 = OpLabel
1792 %116 = OpAccessChain %45 %80 %100
1793 %117 = OpLoad %6 %116
1794 %118 = OpFOrdGreaterThan %55 %117 %28
1795 OpSelectionMerge %120 None
1796 OpBranchConditional %118 %119 %120
1797
1798 %119 = OpLabel
1799 OpBranch %78
1800
1801 %120 = OpLabel
1802 %122 = OpAccessChain %45 %80 %11
1803 %123 = OpLoad %6 %122
1804 %124 = OpFAdd %6 %123 %53
1805 %125 = OpAccessChain %45 %80 %11
1806 OpStore %125 %124
1807 OpBranch %79
1808
1809 %79 = OpLabel
1810 %126 = OpLoad %23 %75
1811 %128 = OpSLessThan %55 %126 %127
1812 OpBranchConditional %128 %76 %78
1813
1814 %78 = OpLabel
1815 %129 = OpAccessChain %48 %22 %47
1816 %130 = OpLoad %6 %129
1817 %131 = OpConvertFToS %23 %130
1818 OpSelectionMerge %136 None
1819 OpSwitch %131 %135 0 %132 1 %132 2 %132 3 %133 4 %134
1820
1821 %132 = OpLabel
1822 %137 = OpLoad %6 %51
1823 %138 = OpFAdd %6 %137 %53
1824 OpStore %51 %138
1825 OpBranch %133
1826
1827 %133 = OpLabel
1828 %140 = OpLoad %6 %51
1829 %141 = OpFAdd %6 %140 %139
1830 OpStore %51 %141
1831 OpBranch %136
1832
1833 %134 = OpLabel
1834 %144 = OpLoad %6 %51
1835 %145 = OpFAdd %6 %144 %143
1836 OpStore %51 %145
1837 OpBranch %146
1838
1839 %146 = OpLabel
1840 OpLoopMerge %148 %149 None
1841 OpBranch %150
1842
1843 %150 = OpLabel
1844 %151 = OpLoad %6 %51
1845 %152 = OpFOrdLessThan %55 %151 %139
1846 OpBranchConditional %152 %147 %148
1847
1848 %147 = OpLabel
1849 %153 = OpLoad %6 %46
1850 %154 = OpFOrdLessThan %55 %153 %53
1851 OpSelectionMerge %156 None
1852 OpBranchConditional %154 %155 %156
1853
1854 %155 = OpLabel
1855 OpBranch %148
1856
1857 %156 = OpLabel
1858 OpBranch %149
1859
1860 %149 = OpLabel
1861 OpBranch %146
1862
1863 %148 = OpLabel
1864 OpBranch %135
1865
1866 %135 = OpLabel
1867 %159 = OpLoad %6 %51
1868 %160 = OpFAdd %6 %159 %158
1869 OpStore %51 %160
1870 OpBranch %161
1871
1872 %161 = OpLabel
1873 OpLoopMerge %163 %164 None
1874 OpBranch %165
1875
1876 %165 = OpLabel
1877 %166 = OpLoad %6 %51
1878 %167 = OpFOrdLessThan %55 %166 %139
1879 OpBranchConditional %167 %162 %163
1880
1881 %162 = OpLabel
1882 %168 = OpLoad %6 %46
1883 %169 = OpFOrdLessThan %55 %168 %53
1884 OpSelectionMerge %171 None
1885 OpBranchConditional %169 %170 %171
1886
1887 %170 = OpLabel
1888 OpBranch %163
1889
1890 %171 = OpLabel
1891 OpBranch %164
1892
1893 %164 = OpLabel
1894 OpBranch %161
1895
1896 %163 = OpLabel
1897 OpBranch %136
1898
1899 %136 = OpLabel
1900 OpStore %174 %53
1901 %175 = OpAccessChain %45 %35 %58
1902 %176 = OpLoad %6 %175
1903 %178 = OpFOrdLessThanEqual %55 %176 %177
1904 OpSelectionMerge %180 None
1905 OpBranchConditional %178 %179 %181
1906
1907 %179 = OpLabel
1908 OpStore %174 %28
1909 OpBranch %180
1910
1911 %181 = OpLabel
1912 %182 = OpAccessChain %45 %35 %58
1913 %183 = OpLoad %6 %182
1914 %184 = OpFOrdGreaterThanEqual %55 %183 %177
1915 OpSelectionMerge %186 None
1916 OpBranchConditional %184 %185 %186
1917
1918 %185 = OpLabel
1919 OpStore %174 %139
1920 OpBranch %186
1921
1922 %186 = OpLabel
1923 OpBranch %180
1924
1925 %180 = OpLabel
1926 OpStore %187 %47
1927 OpBranch %188
1928
1929 %188 = OpLabel
1930 OpLoopMerge %190 %191 None
1931 OpBranch %189
1932
1933 %189 = OpLabel
1934 %192 = OpLoad %9 %19
1935 %193 = OpFAdd %9 %192 %109
1936 OpStore %19 %193
1937 %194 = OpLoad %23 %187
1938 %196 = OpSGreaterThan %55 %194 %195
1939 OpSelectionMerge %198 None
1940 OpBranchConditional %196 %197 %198
1941
1942 %197 = OpLabel
1943 OpBranch %190
1944
1945 %198 = OpLabel
1946 OpBranch %191
1947
1948 %191 = OpLabel
1949 %200 = OpLoad %23 %187
1950 %201 = OpIAdd %23 %200 %24
1951 OpStore %187 %201
1952 OpBranch %188
1953
1954 %190 = OpLabel
1955 OpReturn
1956 OpFunctionEnd
1957 %16 = OpFunction %12 None %13
1958 %14 = OpFunctionParameter %8
1959 %15 = OpFunctionParameter %6
1960
1961 %17 = OpLabel
1962 %30 = OpCompositeConstruct %9 %15 %15 %15 %15
1963 %31 = OpCompositeConstruct %12 %29 %30
1964 OpReturnValue %31
1965 OpFunctionEnd
1966 )";
1967 EXPECT_THAT(EncodeAndDecodeSuccessfully(
1968 input,
1969 SPV_BINARY_TO_TEXT_OPTION_INDENT |
1970 SPV_BINARY_TO_TEXT_OPTION_NESTED_INDENT |
1971 SPV_BINARY_TO_TEXT_OPTION_REORDER_BLOCKS,
1972 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS),
1973 expected);
1974 }
1975
1976 using FriendlyNameDisassemblyTest = spvtest::TextToBinaryTest;
1977
1978 TEST_F(FriendlyNameDisassemblyTest, Sample) {
1979 const std::string input = R"(
1980 OpCapability Shader
1981 OpMemoryModel Logical GLSL450
1982 %1 = OpTypeInt 32 0
1983 %2 = OpTypeStruct %1 %3 %4 %5 %6 %7 %8 %9 %10 ; force IDs into double digits
1984 %11 = OpConstant %1 42
1985 )";
1986 const std::string expected =
1987 R"(OpCapability Shader
1988 OpMemoryModel Logical GLSL450
1989 %uint = OpTypeInt 32 0
1990 %_struct_2 = OpTypeStruct %uint %3 %4 %5 %6 %7 %8 %9 %10
1991 %uint_42 = OpConstant %uint 42
1992 )";
1993 EXPECT_THAT(EncodeAndDecodeSuccessfully(
1994 input, SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES),
1995 expected);
1996 }
1997
1998 TEST_F(TextToBinaryTest, ShowByteOffsetsWhenRequested) {
1999 const std::string input = R"(
2000 OpCapability Shader
2001 OpMemoryModel Logical GLSL450
2002 %1 = OpTypeInt 32 0
2003 %2 = OpTypeVoid
2004 )";
2005 const std::string expected =
2006 R"(OpCapability Shader ; 0x00000014
2007 OpMemoryModel Logical GLSL450 ; 0x0000001c
2008 %1 = OpTypeInt 32 0 ; 0x00000028
2009 %2 = OpTypeVoid ; 0x00000038
2010 )";
2011 EXPECT_THAT(EncodeAndDecodeSuccessfully(
2012 input, SPV_BINARY_TO_TEXT_OPTION_SHOW_BYTE_OFFSET),
2013 expected);
2014 }
2015
2016 TEST_F(TextToBinaryTest, Comments) {
2017 const std::string input = R"(OpCapability Shader
2018 OpMemoryModel Logical GLSL450
2019 OpEntryPoint Fragment %63 "main" %4 %22
2020 OpExecutionMode %63 OriginUpperLeft
2021 OpSource GLSL 450
2022 OpName %4 "_ue"
2023 OpName %8 "_uf"
2024 OpName %11 "_ug"
2025 OpName %12 "_uA"
2026 OpMemberName %12 0 "_ux"
2027 OpName %14 "_uc"
2028 OpName %15 "_uB"
2029 OpMemberName %15 0 "_ux"
2030 OpName %20 "_ud"
2031 OpName %22 "_ucol"
2032 OpName %26 "ANGLEDepthRangeParams"
2033 OpMemberName %26 0 "near"
2034 OpMemberName %26 1 "far"
2035 OpMemberName %26 2 "diff"
2036 OpMemberName %26 3 "reserved"
2037 OpName %27 "ANGLEUniformBlock"
2038 OpMemberName %27 0 "viewport"
2039 OpMemberName %27 1 "clipDistancesEnabled"
2040 OpMemberName %27 2 "xfbActiveUnpaused"
2041 OpMemberName %27 3 "xfbVerticesPerInstance"
2042 OpMemberName %27 4 "numSamples"
2043 OpMemberName %27 5 "xfbBufferOffsets"
2044 OpMemberName %27 6 "acbBufferOffsets"
2045 OpMemberName %27 7 "depthRange"
2046 OpName %29 "ANGLEUniforms"
2047 OpName %33 "_uc"
2048 OpName %32 "_uh"
2049 OpName %49 "_ux"
2050 OpName %50 "_uy"
2051 OpName %48 "_ui"
2052 OpName %63 "main"
2053 OpName %65 "param"
2054 OpName %68 "param"
2055 OpName %73 "param"
2056 OpDecorate %4 Location 0
2057 OpDecorate %8 RelaxedPrecision
2058 OpDecorate %8 DescriptorSet 0
2059 OpDecorate %8 Binding 0
2060 OpDecorate %11 DescriptorSet 0
2061 OpDecorate %11 Binding 1
2062 OpMemberDecorate %12 0 Offset 0
2063 OpMemberDecorate %12 0 RelaxedPrecision
2064 OpDecorate %12 Block
2065 OpDecorate %14 DescriptorSet 0
2066 OpDecorate %14 Binding 2
2067 OpMemberDecorate %15 0 Offset 0
2068 OpMemberDecorate %15 0 RelaxedPrecision
2069 OpDecorate %15 BufferBlock
2070 OpDecorate %20 DescriptorSet 0
2071 OpDecorate %20 Binding 3
2072 OpDecorate %22 RelaxedPrecision
2073 OpDecorate %22 Location 0
2074 OpMemberDecorate %26 0 Offset 0
2075 OpMemberDecorate %26 1 Offset 4
2076 OpMemberDecorate %26 2 Offset 8
2077 OpMemberDecorate %26 3 Offset 12
2078 OpMemberDecorate %27 0 Offset 0
2079 OpMemberDecorate %27 1 Offset 16
2080 OpMemberDecorate %27 2 Offset 20
2081 OpMemberDecorate %27 3 Offset 24
2082 OpMemberDecorate %27 4 Offset 28
2083 OpMemberDecorate %27 5 Offset 32
2084 OpMemberDecorate %27 6 Offset 48
2085 OpMemberDecorate %27 7 Offset 64
2086 OpMemberDecorate %27 2 RelaxedPrecision
2087 OpMemberDecorate %27 4 RelaxedPrecision
2088 OpDecorate %27 Block
2089 OpDecorate %29 DescriptorSet 0
2090 OpDecorate %29 Binding 4
2091 OpDecorate %32 RelaxedPrecision
2092 OpDecorate %33 RelaxedPrecision
2093 OpDecorate %36 RelaxedPrecision
2094 OpDecorate %37 RelaxedPrecision
2095 OpDecorate %38 RelaxedPrecision
2096 OpDecorate %39 RelaxedPrecision
2097 OpDecorate %41 RelaxedPrecision
2098 OpDecorate %42 RelaxedPrecision
2099 OpDecorate %43 RelaxedPrecision
2100 OpDecorate %48 RelaxedPrecision
2101 OpDecorate %49 RelaxedPrecision
2102 OpDecorate %50 RelaxedPrecision
2103 OpDecorate %52 RelaxedPrecision
2104 OpDecorate %53 RelaxedPrecision
2105 OpDecorate %54 RelaxedPrecision
2106 OpDecorate %55 RelaxedPrecision
2107 OpDecorate %56 RelaxedPrecision
2108 OpDecorate %57 RelaxedPrecision
2109 OpDecorate %58 RelaxedPrecision
2110 OpDecorate %59 RelaxedPrecision
2111 OpDecorate %60 RelaxedPrecision
2112 OpDecorate %67 RelaxedPrecision
2113 OpDecorate %68 RelaxedPrecision
2114 OpDecorate %72 RelaxedPrecision
2115 OpDecorate %73 RelaxedPrecision
2116 OpDecorate %75 RelaxedPrecision
2117 OpDecorate %76 RelaxedPrecision
2118 OpDecorate %77 RelaxedPrecision
2119 OpDecorate %80 RelaxedPrecision
2120 OpDecorate %81 RelaxedPrecision
2121 %1 = OpTypeFloat 32
2122 %2 = OpTypeVector %1 4
2123 %5 = OpTypeImage %1 2D 0 0 0 1 Unknown
2124 %6 = OpTypeSampledImage %5
2125 %9 = OpTypeImage %1 2D 0 0 0 2 Rgba8
2126 %12 = OpTypeStruct %2
2127 %15 = OpTypeStruct %2
2128 %16 = OpTypeInt 32 0
2129 %17 = OpConstant %16 2
2130 %18 = OpTypeArray %15 %17
2131 %23 = OpTypeInt 32 1
2132 %24 = OpTypeVector %23 4
2133 %25 = OpTypeVector %16 4
2134 %26 = OpTypeStruct %1 %1 %1 %1
2135 %27 = OpTypeStruct %2 %16 %16 %23 %23 %24 %25 %26
2136 %35 = OpTypeVector %1 2
2137 %40 = OpTypeVector %23 2
2138 %61 = OpTypeVoid
2139 %69 = OpConstant %16 0
2140 %78 = OpConstant %16 1
2141 %3 = OpTypePointer Input %2
2142 %7 = OpTypePointer UniformConstant %6
2143 %10 = OpTypePointer UniformConstant %9
2144 %13 = OpTypePointer Uniform %12
2145 %19 = OpTypePointer Uniform %18
2146 %21 = OpTypePointer Output %2
2147 %28 = OpTypePointer Uniform %27
2148 %30 = OpTypePointer Function %2
2149 %70 = OpTypePointer Uniform %2
2150 %31 = OpTypeFunction %2 %30
2151 %47 = OpTypeFunction %2 %30 %30
2152 %62 = OpTypeFunction %61
2153 %4 = OpVariable %3 Input
2154 %8 = OpVariable %7 UniformConstant
2155 %11 = OpVariable %10 UniformConstant
2156 %14 = OpVariable %13 Uniform
2157 %20 = OpVariable %19 Uniform
2158 %22 = OpVariable %21 Output
2159 %29 = OpVariable %28 Uniform
2160 %32 = OpFunction %2 None %31
2161 %33 = OpFunctionParameter %30
2162 %34 = OpLabel
2163 %36 = OpLoad %6 %8
2164 %37 = OpLoad %2 %33
2165 %38 = OpVectorShuffle %35 %37 %37 0 1
2166 %39 = OpImageSampleImplicitLod %2 %36 %38
2167 %41 = OpLoad %2 %33
2168 %42 = OpVectorShuffle %35 %41 %41 2 3
2169 %43 = OpConvertFToS %40 %42
2170 %44 = OpLoad %9 %11
2171 %45 = OpImageRead %2 %44 %43
2172 %46 = OpFAdd %2 %39 %45
2173 OpReturnValue %46
2174 OpFunctionEnd
2175 %48 = OpFunction %2 None %47
2176 %49 = OpFunctionParameter %30
2177 %50 = OpFunctionParameter %30
2178 %51 = OpLabel
2179 %52 = OpLoad %2 %49
2180 %53 = OpVectorShuffle %35 %52 %52 0 1
2181 %54 = OpLoad %2 %50
2182 %55 = OpVectorShuffle %35 %54 %54 2 3
2183 %56 = OpCompositeExtract %1 %53 0
2184 %57 = OpCompositeExtract %1 %53 1
2185 %58 = OpCompositeExtract %1 %55 0
2186 %59 = OpCompositeExtract %1 %55 1
2187 %60 = OpCompositeConstruct %2 %56 %57 %58 %59
2188 OpReturnValue %60
2189 OpFunctionEnd
2190 %63 = OpFunction %61 None %62
2191 %64 = OpLabel
2192 %65 = OpVariable %30 Function
2193 %68 = OpVariable %30 Function
2194 %73 = OpVariable %30 Function
2195 %66 = OpLoad %2 %4
2196 OpStore %65 %66
2197 %67 = OpFunctionCall %2 %32 %65
2198 %71 = OpAccessChain %70 %14 %69
2199 %72 = OpLoad %2 %71
2200 OpStore %68 %72
2201 %74 = OpAccessChain %70 %20 %69 %69
2202 %75 = OpLoad %2 %74
2203 OpStore %73 %75
2204 %76 = OpFunctionCall %2 %48 %68 %73
2205 %77 = OpFAdd %2 %67 %76
2206 %79 = OpAccessChain %70 %20 %78 %69
2207 %80 = OpLoad %2 %79
2208 %81 = OpFAdd %2 %77 %80
2209 OpStore %22 %81
2210 OpReturn
2211 OpFunctionEnd
2212 )";
2213 const std::string expected = R"( OpCapability Shader
2214 OpMemoryModel Logical GLSL450
2215 OpEntryPoint Fragment %63 "main" %4 %22
2216 OpExecutionMode %63 OriginUpperLeft
2217
2218 ; Debug Information
2219 OpSource GLSL 450
2220 OpName %4 "_ue" ; id %4
2221 OpName %8 "_uf" ; id %8
2222 OpName %11 "_ug" ; id %11
2223 OpName %12 "_uA" ; id %12
2224 OpMemberName %12 0 "_ux"
2225 OpName %14 "_uc" ; id %14
2226 OpName %15 "_uB" ; id %15
2227 OpMemberName %15 0 "_ux"
2228 OpName %20 "_ud" ; id %20
2229 OpName %22 "_ucol" ; id %22
2230 OpName %26 "ANGLEDepthRangeParams" ; id %26
2231 OpMemberName %26 0 "near"
2232 OpMemberName %26 1 "far"
2233 OpMemberName %26 2 "diff"
2234 OpMemberName %26 3 "reserved"
2235 OpName %27 "ANGLEUniformBlock" ; id %27
2236 OpMemberName %27 0 "viewport"
2237 OpMemberName %27 1 "clipDistancesEnabled"
2238 OpMemberName %27 2 "xfbActiveUnpaused"
2239 OpMemberName %27 3 "xfbVerticesPerInstance"
2240 OpMemberName %27 4 "numSamples"
2241 OpMemberName %27 5 "xfbBufferOffsets"
2242 OpMemberName %27 6 "acbBufferOffsets"
2243 OpMemberName %27 7 "depthRange"
2244 OpName %29 "ANGLEUniforms" ; id %29
2245 OpName %33 "_uc" ; id %33
2246 OpName %32 "_uh" ; id %32
2247 OpName %49 "_ux" ; id %49
2248 OpName %50 "_uy" ; id %50
2249 OpName %48 "_ui" ; id %48
2250 OpName %63 "main" ; id %63
2251 OpName %65 "param" ; id %65
2252 OpName %68 "param" ; id %68
2253 OpName %73 "param" ; id %73
2254
2255 ; Annotations
2256 OpDecorate %4 Location 0
2257 OpDecorate %8 RelaxedPrecision
2258 OpDecorate %8 DescriptorSet 0
2259 OpDecorate %8 Binding 0
2260 OpDecorate %11 DescriptorSet 0
2261 OpDecorate %11 Binding 1
2262 OpMemberDecorate %12 0 Offset 0
2263 OpMemberDecorate %12 0 RelaxedPrecision
2264 OpDecorate %12 Block
2265 OpDecorate %14 DescriptorSet 0
2266 OpDecorate %14 Binding 2
2267 OpMemberDecorate %15 0 Offset 0
2268 OpMemberDecorate %15 0 RelaxedPrecision
2269 OpDecorate %15 BufferBlock
2270 OpDecorate %20 DescriptorSet 0
2271 OpDecorate %20 Binding 3
2272 OpDecorate %22 RelaxedPrecision
2273 OpDecorate %22 Location 0
2274 OpMemberDecorate %26 0 Offset 0
2275 OpMemberDecorate %26 1 Offset 4
2276 OpMemberDecorate %26 2 Offset 8
2277 OpMemberDecorate %26 3 Offset 12
2278 OpMemberDecorate %27 0 Offset 0
2279 OpMemberDecorate %27 1 Offset 16
2280 OpMemberDecorate %27 2 Offset 20
2281 OpMemberDecorate %27 3 Offset 24
2282 OpMemberDecorate %27 4 Offset 28
2283 OpMemberDecorate %27 5 Offset 32
2284 OpMemberDecorate %27 6 Offset 48
2285 OpMemberDecorate %27 7 Offset 64
2286 OpMemberDecorate %27 2 RelaxedPrecision
2287 OpMemberDecorate %27 4 RelaxedPrecision
2288 OpDecorate %27 Block
2289 OpDecorate %29 DescriptorSet 0
2290 OpDecorate %29 Binding 4
2291 OpDecorate %32 RelaxedPrecision
2292 OpDecorate %33 RelaxedPrecision
2293 OpDecorate %36 RelaxedPrecision
2294 OpDecorate %37 RelaxedPrecision
2295 OpDecorate %38 RelaxedPrecision
2296 OpDecorate %39 RelaxedPrecision
2297 OpDecorate %41 RelaxedPrecision
2298 OpDecorate %42 RelaxedPrecision
2299 OpDecorate %43 RelaxedPrecision
2300 OpDecorate %48 RelaxedPrecision
2301 OpDecorate %49 RelaxedPrecision
2302 OpDecorate %50 RelaxedPrecision
2303 OpDecorate %52 RelaxedPrecision
2304 OpDecorate %53 RelaxedPrecision
2305 OpDecorate %54 RelaxedPrecision
2306 OpDecorate %55 RelaxedPrecision
2307 OpDecorate %56 RelaxedPrecision
2308 OpDecorate %57 RelaxedPrecision
2309 OpDecorate %58 RelaxedPrecision
2310 OpDecorate %59 RelaxedPrecision
2311 OpDecorate %60 RelaxedPrecision
2312 OpDecorate %67 RelaxedPrecision
2313 OpDecorate %68 RelaxedPrecision
2314 OpDecorate %72 RelaxedPrecision
2315 OpDecorate %73 RelaxedPrecision
2316 OpDecorate %75 RelaxedPrecision
2317 OpDecorate %76 RelaxedPrecision
2318 OpDecorate %77 RelaxedPrecision
2319 OpDecorate %80 RelaxedPrecision
2320 OpDecorate %81 RelaxedPrecision
2321
2322 ; Types, variables and constants
2323 %1 = OpTypeFloat 32
2324 %2 = OpTypeVector %1 4
2325 %5 = OpTypeImage %1 2D 0 0 0 1 Unknown
2326 %6 = OpTypeSampledImage %5
2327 %9 = OpTypeImage %1 2D 0 0 0 2 Rgba8
2328 %12 = OpTypeStruct %2 ; Block
2329 %15 = OpTypeStruct %2 ; BufferBlock
2330 %16 = OpTypeInt 32 0
2331 %17 = OpConstant %16 2
2332 %18 = OpTypeArray %15 %17
2333 %23 = OpTypeInt 32 1
2334 %24 = OpTypeVector %23 4
2335 %25 = OpTypeVector %16 4
2336 %26 = OpTypeStruct %1 %1 %1 %1
2337 %27 = OpTypeStruct %2 %16 %16 %23 %23 %24 %25 %26 ; Block
2338 %35 = OpTypeVector %1 2
2339 %40 = OpTypeVector %23 2
2340 %61 = OpTypeVoid
2341 %69 = OpConstant %16 0
2342 %78 = OpConstant %16 1
2343 %3 = OpTypePointer Input %2
2344 %7 = OpTypePointer UniformConstant %6
2345 %10 = OpTypePointer UniformConstant %9
2346 %13 = OpTypePointer Uniform %12
2347 %19 = OpTypePointer Uniform %18
2348 %21 = OpTypePointer Output %2
2349 %28 = OpTypePointer Uniform %27
2350 %30 = OpTypePointer Function %2
2351 %70 = OpTypePointer Uniform %2
2352 %31 = OpTypeFunction %2 %30
2353 %47 = OpTypeFunction %2 %30 %30
2354 %62 = OpTypeFunction %61
2355 %4 = OpVariable %3 Input ; Location 0
2356 %8 = OpVariable %7 UniformConstant ; RelaxedPrecision, DescriptorSet 0, Binding 0
2357 %11 = OpVariable %10 UniformConstant ; DescriptorSet 0, Binding 1
2358 %14 = OpVariable %13 Uniform ; DescriptorSet 0, Binding 2
2359 %20 = OpVariable %19 Uniform ; DescriptorSet 0, Binding 3
2360 %22 = OpVariable %21 Output ; RelaxedPrecision, Location 0
2361 %29 = OpVariable %28 Uniform ; DescriptorSet 0, Binding 4
2362
2363 ; Function 32
2364 %32 = OpFunction %2 None %31 ; RelaxedPrecision
2365 %33 = OpFunctionParameter %30 ; RelaxedPrecision
2366 %34 = OpLabel
2367 %36 = OpLoad %6 %8 ; RelaxedPrecision
2368 %37 = OpLoad %2 %33 ; RelaxedPrecision
2369 %38 = OpVectorShuffle %35 %37 %37 0 1 ; RelaxedPrecision
2370 %39 = OpImageSampleImplicitLod %2 %36 %38 ; RelaxedPrecision
2371 %41 = OpLoad %2 %33 ; RelaxedPrecision
2372 %42 = OpVectorShuffle %35 %41 %41 2 3 ; RelaxedPrecision
2373 %43 = OpConvertFToS %40 %42 ; RelaxedPrecision
2374 %44 = OpLoad %9 %11
2375 %45 = OpImageRead %2 %44 %43
2376 %46 = OpFAdd %2 %39 %45
2377 OpReturnValue %46
2378 OpFunctionEnd
2379
2380 ; Function 48
2381 %48 = OpFunction %2 None %47 ; RelaxedPrecision
2382 %49 = OpFunctionParameter %30 ; RelaxedPrecision
2383 %50 = OpFunctionParameter %30 ; RelaxedPrecision
2384 %51 = OpLabel
2385 %52 = OpLoad %2 %49 ; RelaxedPrecision
2386 %53 = OpVectorShuffle %35 %52 %52 0 1 ; RelaxedPrecision
2387 %54 = OpLoad %2 %50 ; RelaxedPrecision
2388 %55 = OpVectorShuffle %35 %54 %54 2 3 ; RelaxedPrecision
2389 %56 = OpCompositeExtract %1 %53 0 ; RelaxedPrecision
2390 %57 = OpCompositeExtract %1 %53 1 ; RelaxedPrecision
2391 %58 = OpCompositeExtract %1 %55 0 ; RelaxedPrecision
2392 %59 = OpCompositeExtract %1 %55 1 ; RelaxedPrecision
2393 %60 = OpCompositeConstruct %2 %56 %57 %58 %59 ; RelaxedPrecision
2394 OpReturnValue %60
2395 OpFunctionEnd
2396
2397 ; Function 63
2398 %63 = OpFunction %61 None %62
2399 %64 = OpLabel
2400 %65 = OpVariable %30 Function
2401 %68 = OpVariable %30 Function ; RelaxedPrecision
2402 %73 = OpVariable %30 Function ; RelaxedPrecision
2403 %66 = OpLoad %2 %4
2404 OpStore %65 %66
2405 %67 = OpFunctionCall %2 %32 %65 ; RelaxedPrecision
2406 %71 = OpAccessChain %70 %14 %69
2407 %72 = OpLoad %2 %71 ; RelaxedPrecision
2408 OpStore %68 %72
2409 %74 = OpAccessChain %70 %20 %69 %69
2410 %75 = OpLoad %2 %74 ; RelaxedPrecision
2411 OpStore %73 %75
2412 %76 = OpFunctionCall %2 %48 %68 %73 ; RelaxedPrecision
2413 %77 = OpFAdd %2 %67 %76 ; RelaxedPrecision
2414 %79 = OpAccessChain %70 %20 %78 %69
2415 %80 = OpLoad %2 %79 ; RelaxedPrecision
2416 %81 = OpFAdd %2 %77 %80 ; RelaxedPrecision
2417 OpStore %22 %81
2418 OpReturn
2419 OpFunctionEnd
2420 )";
2421
2422 EXPECT_THAT(
2423 EncodeAndDecodeSuccessfully(
2424 input,
2425 SPV_BINARY_TO_TEXT_OPTION_COMMENT | SPV_BINARY_TO_TEXT_OPTION_INDENT,
2426 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS),
2427 expected);
2428 }
2429
2430 TEST_F(TextToBinaryTest, NestedWithComments) {
2431 const std::string input = R"(OpCapability Shader
2432 %1 = OpExtInstImport "GLSL.std.450"
2433 OpMemoryModel Logical GLSL450
2434 OpEntryPoint Fragment %4 "main" %8 %44
2435 OpExecutionMode %4 OriginUpperLeft
2436 OpSource ESSL 310
2437 OpName %4 "main"
2438 OpName %8 "v"
2439 OpName %44 "color"
2440 OpDecorate %8 RelaxedPrecision
2441 OpDecorate %8 Location 0
2442 OpDecorate %9 RelaxedPrecision
2443 OpDecorate %18 RelaxedPrecision
2444 OpDecorate %19 RelaxedPrecision
2445 OpDecorate %20 RelaxedPrecision
2446 OpDecorate %23 RelaxedPrecision
2447 OpDecorate %24 RelaxedPrecision
2448 OpDecorate %25 RelaxedPrecision
2449 OpDecorate %26 RelaxedPrecision
2450 OpDecorate %27 RelaxedPrecision
2451 OpDecorate %28 RelaxedPrecision
2452 OpDecorate %29 RelaxedPrecision
2453 OpDecorate %30 RelaxedPrecision
2454 OpDecorate %31 RelaxedPrecision
2455 OpDecorate %33 RelaxedPrecision
2456 OpDecorate %34 RelaxedPrecision
2457 OpDecorate %35 RelaxedPrecision
2458 OpDecorate %36 RelaxedPrecision
2459 OpDecorate %37 RelaxedPrecision
2460 OpDecorate %39 RelaxedPrecision
2461 OpDecorate %40 RelaxedPrecision
2462 OpDecorate %41 RelaxedPrecision
2463 OpDecorate %42 RelaxedPrecision
2464 OpDecorate %44 RelaxedPrecision
2465 OpDecorate %44 Location 0
2466 OpDecorate %45 RelaxedPrecision
2467 %2 = OpTypeVoid
2468 %3 = OpTypeFunction %2
2469 %6 = OpTypeFloat 32
2470 %7 = OpTypePointer Input %6
2471 %8 = OpVariable %7 Input
2472 %10 = OpConstant %6 0
2473 %11 = OpTypeBool
2474 %15 = OpTypeVector %6 4
2475 %16 = OpTypePointer Function %15
2476 %21 = OpConstant %6 -0.5
2477 %22 = OpConstant %6 -0.300000012
2478 %38 = OpConstant %6 0.5
2479 %43 = OpTypePointer Output %15
2480 %44 = OpVariable %43 Output
2481 %4 = OpFunction %2 None %3
2482 %5 = OpLabel
2483 %9 = OpLoad %6 %8
2484 %12 = OpFOrdLessThanEqual %11 %9 %10
2485 OpSelectionMerge %14 None
2486 OpBranchConditional %12 %13 %32
2487 %13 = OpLabel
2488 %18 = OpLoad %6 %8
2489 %19 = OpExtInst %6 %1 Log %18
2490 %20 = OpLoad %6 %8
2491 %23 = OpExtInst %6 %1 FClamp %20 %21 %22
2492 %24 = OpFMul %6 %19 %23
2493 %25 = OpLoad %6 %8
2494 %26 = OpExtInst %6 %1 Sin %25
2495 %27 = OpLoad %6 %8
2496 %28 = OpExtInst %6 %1 Cos %27
2497 %29 = OpLoad %6 %8
2498 %30 = OpExtInst %6 %1 Exp %29
2499 %31 = OpCompositeConstruct %15 %24 %26 %28 %30
2500 OpBranch %14
2501 %32 = OpLabel
2502 %33 = OpLoad %6 %8
2503 %34 = OpExtInst %6 %1 Sqrt %33
2504 %35 = OpLoad %6 %8
2505 %36 = OpExtInst %6 %1 FSign %35
2506 %37 = OpLoad %6 %8
2507 %39 = OpExtInst %6 %1 FMax %37 %38
2508 %40 = OpLoad %6 %8
2509 %41 = OpExtInst %6 %1 Floor %40
2510 %42 = OpCompositeConstruct %15 %34 %36 %39 %41
2511 OpBranch %14
2512 %14 = OpLabel
2513 %45 = OpPhi %15 %31 %13 %42 %32
2514 OpStore %44 %45
2515 OpReturn
2516 OpFunctionEnd
2517 )";
2518 const std::string expected = R"( OpCapability Shader
2519 %1 = OpExtInstImport "GLSL.std.450"
2520 OpMemoryModel Logical GLSL450
2521 OpEntryPoint Fragment %4 "main" %8 %44
2522 OpExecutionMode %4 OriginUpperLeft
2523
2524 ; Debug Information
2525 OpSource ESSL 310
2526 OpName %4 "main" ; id %4
2527 OpName %8 "v" ; id %8
2528 OpName %44 "color" ; id %44
2529
2530 ; Annotations
2531 OpDecorate %8 RelaxedPrecision
2532 OpDecorate %8 Location 0
2533 OpDecorate %9 RelaxedPrecision
2534 OpDecorate %18 RelaxedPrecision
2535 OpDecorate %19 RelaxedPrecision
2536 OpDecorate %20 RelaxedPrecision
2537 OpDecorate %23 RelaxedPrecision
2538 OpDecorate %24 RelaxedPrecision
2539 OpDecorate %25 RelaxedPrecision
2540 OpDecorate %26 RelaxedPrecision
2541 OpDecorate %27 RelaxedPrecision
2542 OpDecorate %28 RelaxedPrecision
2543 OpDecorate %29 RelaxedPrecision
2544 OpDecorate %30 RelaxedPrecision
2545 OpDecorate %31 RelaxedPrecision
2546 OpDecorate %33 RelaxedPrecision
2547 OpDecorate %34 RelaxedPrecision
2548 OpDecorate %35 RelaxedPrecision
2549 OpDecorate %36 RelaxedPrecision
2550 OpDecorate %37 RelaxedPrecision
2551 OpDecorate %39 RelaxedPrecision
2552 OpDecorate %40 RelaxedPrecision
2553 OpDecorate %41 RelaxedPrecision
2554 OpDecorate %42 RelaxedPrecision
2555 OpDecorate %44 RelaxedPrecision
2556 OpDecorate %44 Location 0
2557 OpDecorate %45 RelaxedPrecision
2558
2559 ; Types, variables and constants
2560 %2 = OpTypeVoid
2561 %3 = OpTypeFunction %2
2562 %6 = OpTypeFloat 32
2563 %7 = OpTypePointer Input %6
2564 %8 = OpVariable %7 Input ; RelaxedPrecision, Location 0
2565 %10 = OpConstant %6 0
2566 %11 = OpTypeBool
2567 %15 = OpTypeVector %6 4
2568 %16 = OpTypePointer Function %15
2569 %21 = OpConstant %6 -0.5
2570 %22 = OpConstant %6 -0.300000012
2571 %38 = OpConstant %6 0.5
2572 %43 = OpTypePointer Output %15
2573 %44 = OpVariable %43 Output ; RelaxedPrecision, Location 0
2574
2575
2576 ; Function 4
2577 %4 = OpFunction %2 None %3
2578
2579 %5 = OpLabel
2580 %9 = OpLoad %6 %8 ; RelaxedPrecision
2581 %12 = OpFOrdLessThanEqual %11 %9 %10
2582 OpSelectionMerge %14 None
2583 OpBranchConditional %12 %13 %32
2584
2585 %13 = OpLabel
2586 %18 = OpLoad %6 %8 ; RelaxedPrecision
2587 %19 = OpExtInst %6 %1 Log %18 ; RelaxedPrecision
2588 %20 = OpLoad %6 %8 ; RelaxedPrecision
2589 %23 = OpExtInst %6 %1 FClamp %20 %21 %22 ; RelaxedPrecision
2590 %24 = OpFMul %6 %19 %23 ; RelaxedPrecision
2591 %25 = OpLoad %6 %8 ; RelaxedPrecision
2592 %26 = OpExtInst %6 %1 Sin %25 ; RelaxedPrecision
2593 %27 = OpLoad %6 %8 ; RelaxedPrecision
2594 %28 = OpExtInst %6 %1 Cos %27 ; RelaxedPrecision
2595 %29 = OpLoad %6 %8 ; RelaxedPrecision
2596 %30 = OpExtInst %6 %1 Exp %29 ; RelaxedPrecision
2597 %31 = OpCompositeConstruct %15 %24 %26 %28 %30 ; RelaxedPrecision
2598 OpBranch %14
2599
2600 %32 = OpLabel
2601 %33 = OpLoad %6 %8 ; RelaxedPrecision
2602 %34 = OpExtInst %6 %1 Sqrt %33 ; RelaxedPrecision
2603 %35 = OpLoad %6 %8 ; RelaxedPrecision
2604 %36 = OpExtInst %6 %1 FSign %35 ; RelaxedPrecision
2605 %37 = OpLoad %6 %8 ; RelaxedPrecision
2606 %39 = OpExtInst %6 %1 FMax %37 %38 ; RelaxedPrecision
2607 %40 = OpLoad %6 %8 ; RelaxedPrecision
2608 %41 = OpExtInst %6 %1 Floor %40 ; RelaxedPrecision
2609 %42 = OpCompositeConstruct %15 %34 %36 %39 %41 ; RelaxedPrecision
2610 OpBranch %14
2611
2612 %14 = OpLabel
2613 %45 = OpPhi %15 %31 %13 %42 %32 ; RelaxedPrecision
2614 OpStore %44 %45
2615 OpReturn
2616 OpFunctionEnd
2617 )";
2618
2619 EXPECT_THAT(
2620 EncodeAndDecodeSuccessfully(
2621 input,
2622 SPV_BINARY_TO_TEXT_OPTION_COMMENT | SPV_BINARY_TO_TEXT_OPTION_INDENT |
2623 SPV_BINARY_TO_TEXT_OPTION_NESTED_INDENT,
2624 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS),
2625 expected);
2626 }
2627
2628 // Test version string.
2629 TEST_F(TextToBinaryTest, VersionString) {
2630 auto words = CompileSuccessfully("");
2631 spv_text decoded_text = nullptr;
2632 EXPECT_THAT(spvBinaryToText(ScopedContext().context, words.data(),
2633 words.size(), SPV_BINARY_TO_TEXT_OPTION_NONE,
2634 &decoded_text, &diagnostic),
2635 Eq(SPV_SUCCESS));
2636 EXPECT_EQ(nullptr, diagnostic);
2637
2638 EXPECT_THAT(decoded_text->str, HasSubstr("Version: 1.0\n"))
2639 << EncodeAndDecodeSuccessfully("");
2640 spvTextDestroy(decoded_text);
2641 }
2642
2643 // Test generator string.
2644
2645 // A test case for the generator string. This allows us to
2646 // test both of the 16-bit components of the generator word.
2647 struct GeneratorStringCase {
2648 uint16_t generator;
2649 uint16_t misc;
2650 std::string expected;
2651 };
2652
2653 using GeneratorStringTest = spvtest::TextToBinaryTestBase<
2654 ::testing::TestWithParam<GeneratorStringCase>>;
2655
2656 TEST_P(GeneratorStringTest, Sample) {
2657 auto words = CompileSuccessfully("");
2658 EXPECT_EQ(2u, SPV_INDEX_GENERATOR_NUMBER);
2659 words[SPV_INDEX_GENERATOR_NUMBER] =
2660 SPV_GENERATOR_WORD(GetParam().generator, GetParam().misc);
2661
2662 spv_text decoded_text = nullptr;
2663 EXPECT_THAT(spvBinaryToText(ScopedContext().context, words.data(),
2664 words.size(), SPV_BINARY_TO_TEXT_OPTION_NONE,
2665 &decoded_text, &diagnostic),
2666 Eq(SPV_SUCCESS));
2667 EXPECT_THAT(diagnostic, Eq(nullptr));
2668 EXPECT_THAT(std::string(decoded_text->str), HasSubstr(GetParam().expected));
2669 spvTextDestroy(decoded_text);
2670 }
2671
2672 INSTANTIATE_TEST_SUITE_P(GeneratorStrings, GeneratorStringTest,
2673 ::testing::ValuesIn(std::vector<GeneratorStringCase>{
2674 {SPV_GENERATOR_KHRONOS, 12, "Khronos; 12"},
2675 {SPV_GENERATOR_LUNARG, 99, "LunarG; 99"},
2676 {SPV_GENERATOR_VALVE, 1, "Valve; 1"},
2677 {SPV_GENERATOR_CODEPLAY, 65535, "Codeplay; 65535"},
2678 {SPV_GENERATOR_NVIDIA, 19, "NVIDIA; 19"},
2679 {SPV_GENERATOR_ARM, 1000, "ARM; 1000"},
2680 {SPV_GENERATOR_KHRONOS_LLVM_TRANSLATOR, 38,
2681 "Khronos LLVM/SPIR-V Translator; 38"},
2682 {SPV_GENERATOR_KHRONOS_ASSEMBLER, 2,
2683 "Khronos SPIR-V Tools Assembler; 2"},
2684 {SPV_GENERATOR_KHRONOS_GLSLANG, 1,
2685 "Khronos Glslang Reference Front End; 1"},
2686 {1000, 18, "Unknown(1000); 18"},
2687 {65535, 32767, "Unknown(65535); 32767"},
2688 }));
2689
2690 // TODO(dneto): Test new instructions and enums in SPIR-V 1.3
2691
2692 } // namespace
2693 } // namespace spvtools