blob: 44705f2a529d107911de486b4e9aa386766da7af [file] [log] [blame]
Dejan Mircevskib6fe02f2016-01-07 13:44:22 -05001// Copyright (c) 2015-2016 The Khronos Group Inc.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01002//
David Neto9fc86582016-09-01 15:33:59 -04003// 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
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01006//
David Neto9fc86582016-09-01 15:33:59 -04007// http://www.apache.org/licenses/LICENSE-2.0
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +01008//
David Neto9fc86582016-09-01 15:33:59 -04009// 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.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010014
David Neto0ca6b592015-10-30 16:06:15 -040015#include <sstream>
dan sinclaireda2cfb2018-08-03 15:06:09 -040016#include <string>
17#include <tuple>
18#include <vector>
David Neto0ca6b592015-10-30 16:06:15 -040019
David Neto0ca6b592015-10-30 16:06:15 -040020#include "gmock/gmock.h"
Dejan Mircevski3d27da42016-03-31 12:16:51 -040021#include "source/spirv_constant.h"
dan sinclaireda2cfb2018-08-03 15:06:09 -040022#include "test/test_fixture.h"
23#include "test/unit_spirv.h"
Lei Zhangaa056cd2015-11-11 14:24:04 -050024
dan sinclair2cce2c52018-07-11 09:24:49 -040025namespace spvtools {
Dejan Mircevski2ea54f52016-04-21 15:46:08 -040026namespace {
27
David Neto3d2bf532015-10-01 16:58:17 -040028using spvtest::AutoText;
Dejan Mircevski2ea54f52016-04-21 15:46:08 -040029using spvtest::ScopedContext;
Lei Zhang39b01b92015-11-12 10:45:36 -050030using spvtest::TextToBinaryTest;
Diego Novillo83228132017-11-27 10:16:41 -050031using ::testing::Combine;
32using ::testing::Eq;
33using ::testing::HasSubstr;
David Neto619db262015-09-25 12:43:37 -040034
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010035class BinaryToText : public ::testing::Test {
36 public:
David Neto70404a92019-01-16 16:53:10 -050037 BinaryToText()
38 : context(spvContextCreate(SPV_ENV_UNIVERSAL_1_0)), binary(nullptr) {}
Corentin Wallez4a59fd42021-03-09 13:16:43 +000039 ~BinaryToText() override {
David Neto70404a92019-01-16 16:53:10 -050040 spvBinaryDestroy(binary);
41 spvContextDestroy(context);
42 }
Lei Zhang972788b2015-11-12 13:48:30 -050043
Corentin Wallez4a59fd42021-03-09 13:16:43 +000044 void SetUp() override {
Lei Zhang8a375202015-08-24 15:52:26 -040045 const char* textStr = R"(
David Netod02f68a2015-11-11 12:32:21 -050046 OpSource OpenCL_C 12
Lei Zhangabafd5e2015-08-21 11:52:29 -040047 OpMemoryModel Physical64 OpenCL
48 OpSourceExtension "PlaceholderExtensionName"
David Neto78c3b432015-08-27 13:03:52 -040049 OpEntryPoint Kernel %1 "foo"
Lei Zhangabafd5e2015-08-21 11:52:29 -040050 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
Andrew Woloszyn13804e52015-09-22 15:50:33 -040064%15 = OpTypeVector %4 2
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010065)";
66 spv_text_t text = {textStr, strlen(textStr)};
67 spv_diagnostic diagnostic = nullptr;
Andrew Woloszyncfeac482015-09-09 13:04:32 -040068 spv_result_t error =
Lei Zhang972788b2015-11-12 13:48:30 -050069 spvTextToBinary(context, text.str, text.length, &binary, &diagnostic);
David Neto70404a92019-01-16 16:53:10 -050070 spvDiagnosticPrint(diagnostic);
71 spvDiagnosticDestroy(diagnostic);
72 ASSERT_EQ(SPV_SUCCESS, error);
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010073 }
74
Corentin Wallez4a59fd42021-03-09 13:16:43 +000075 void TearDown() override {
David Neto70404a92019-01-16 16:53:10 -050076 spvBinaryDestroy(binary);
77 binary = nullptr;
78 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010079
David Neto0ca6b592015-10-30 16:06:15 -040080 // Compiles the given assembly text, and saves it into 'binary'.
81 void CompileSuccessfully(std::string text) {
David Neto70404a92019-01-16 16:53:10 -050082 spvBinaryDestroy(binary);
83 binary = nullptr;
David Neto0ca6b592015-10-30 16:06:15 -040084 spv_diagnostic diagnostic = nullptr;
Lei Zhang972788b2015-11-12 13:48:30 -050085 EXPECT_EQ(SPV_SUCCESS, spvTextToBinary(context, text.c_str(), text.size(),
86 &binary, &diagnostic));
David Neto0ca6b592015-10-30 16:06:15 -040087 }
88
Lei Zhang972788b2015-11-12 13:48:30 -050089 spv_context context;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010090 spv_binary binary;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010091};
92
93TEST_F(BinaryToText, Default) {
94 spv_text text = nullptr;
95 spv_diagnostic diagnostic = nullptr;
Lei Zhang972788b2015-11-12 13:48:30 -050096 ASSERT_EQ(
97 SPV_SUCCESS,
98 spvBinaryToText(context, binary->code, binary->wordCount,
99 SPV_BINARY_TO_TEXT_OPTION_NONE, &text, &diagnostic));
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100100 printf("%s", text->str);
101 spvTextDestroy(text);
102}
103
Mike0b824322022-06-30 00:05:00 +0800104TEST_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
David Neto0ca6b592015-10-30 16:06:15 -0400115TEST_F(BinaryToText, MissingModule) {
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100116 spv_text text;
117 spv_diagnostic diagnostic = nullptr;
Lei Zhang972788b2015-11-12 13:48:30 -0500118 EXPECT_EQ(
119 SPV_ERROR_INVALID_BINARY,
120 spvBinaryToText(context, nullptr, 42, SPV_BINARY_TO_TEXT_OPTION_NONE,
121 &text, &diagnostic));
David Neto0ca6b592015-10-30 16:06:15 -0400122 EXPECT_THAT(diagnostic->error, Eq(std::string("Missing module.")));
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100123 if (diagnostic) {
124 spvDiagnosticPrint(diagnostic);
125 spvDiagnosticDestroy(diagnostic);
126 }
127}
128
David Neto0ca6b592015-10-30 16:06:15 -0400129TEST_F(BinaryToText, TruncatedModule) {
130 // Make a valid module with zero instructions.
131 CompileSuccessfully("");
132 EXPECT_EQ(SPV_INDEX_INSTRUCTION, binary->wordCount);
133
Lei Zhang8bd75d62015-11-18 09:22:10 -0500134 for (size_t length = 0; length < SPV_INDEX_INSTRUCTION; length++) {
David Neto0ca6b592015-10-30 16:06:15 -0400135 spv_text text = nullptr;
136 spv_diagnostic diagnostic = nullptr;
Lei Zhangdf920ec2015-11-11 11:33:26 -0500137 EXPECT_EQ(
138 SPV_ERROR_INVALID_BINARY,
Lei Zhang972788b2015-11-12 13:48:30 -0500139 spvBinaryToText(context, binary->code, length,
140 SPV_BINARY_TO_TEXT_OPTION_NONE, &text, &diagnostic));
David Neto0ca6b592015-10-30 16:06:15 -0400141 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
150TEST_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;
Lei Zhangdf920ec2015-11-11 11:33:26 -0500158 EXPECT_EQ(
159 SPV_ERROR_INVALID_BINARY,
Lei Zhang972788b2015-11-12 13:48:30 -0500160 spvBinaryToText(context, damaged_binary.data(), damaged_binary.size(),
Lei Zhangdf920ec2015-11-11 11:33:26 -0500161 SPV_BINARY_TO_TEXT_OPTION_NONE, &text, &diagnostic));
David Neto0ca6b592015-10-30 16:06:15 -0400162 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
Andrew Woloszyn157e41b2015-10-16 15:11:00 -0400170struct FailedDecodeCase {
171 std::string source_text;
172 std::vector<uint32_t> appended_instruction;
173 std::string expected_error_message;
174};
175
176using BinaryToTextFail =
Lei Zhang1a0334e2015-11-02 09:41:20 -0500177 spvtest::TextToBinaryTestBase<::testing::TestWithParam<FailedDecodeCase>>;
Andrew Woloszyn157e41b2015-10-16 15:11:00 -0400178
179TEST_P(BinaryToTextFail, EncodeSuccessfullyDecodeFailed) {
180 EXPECT_THAT(EncodeSuccessfullyDecodeFailed(GetParam().source_text,
181 GetParam().appended_instruction),
182 Eq(GetParam().expected_error_message));
183}
184
Steven Perron464111e2019-01-29 18:56:52 -0500185INSTANTIATE_TEST_SUITE_P(
David Neto0ca6b592015-10-30 16:06:15 -0400186 InvalidIds, BinaryToTextFail,
187 ::testing::ValuesIn(std::vector<FailedDecodeCase>{
188 {"", spvtest::MakeInstruction(SpvOpTypeVoid, {0}),
189 "Error: Result Id is 0"},
190 {"", spvtest::MakeInstruction(SpvOpConstant, {0, 1, 42}),
191 "Error: Type Id is 0"},
192 {"%1 = OpTypeVoid", spvtest::MakeInstruction(SpvOpTypeVoid, {1}),
193 "Id 1 is defined more than once"},
194 {"%1 = OpTypeVoid\n"
195 "%2 = OpNot %1 %foo",
196 spvtest::MakeInstruction(SpvOpNot, {1, 2, 3}),
197 "Id 2 is defined more than once"},
198 {"%1 = OpTypeVoid\n"
199 "%2 = OpNot %1 %foo",
200 spvtest::MakeInstruction(SpvOpNot, {1, 1, 3}),
201 "Id 1 is defined more than once"},
202 // The following are the two failure cases for
203 // Parser::setNumericTypeInfoForType.
204 {"", spvtest::MakeInstruction(SpvOpConstant, {500, 1, 42}),
205 "Type Id 500 is not a type"},
206 {"%1 = OpTypeInt 32 0\n"
207 "%2 = OpTypeVector %1 4",
208 spvtest::MakeInstruction(SpvOpConstant, {2, 3, 999}),
209 "Type Id 2 is not a scalar numeric type"},
Steven Perron464111e2019-01-29 18:56:52 -0500210 }));
David Neto0ca6b592015-10-30 16:06:15 -0400211
Steven Perron464111e2019-01-29 18:56:52 -0500212INSTANTIATE_TEST_SUITE_P(
David Neto0ca6b592015-10-30 16:06:15 -0400213 InvalidIdsCheckedDuringLiteralCaseParsing, BinaryToTextFail,
214 ::testing::ValuesIn(std::vector<FailedDecodeCase>{
215 {"", spvtest::MakeInstruction(SpvOpSwitch, {1, 2, 3, 4}),
216 "Invalid OpSwitch: selector id 1 has no type"},
217 {"%1 = OpTypeVoid\n",
218 spvtest::MakeInstruction(SpvOpSwitch, {1, 2, 3, 4}),
219 "Invalid OpSwitch: selector id 1 is a type, not a value"},
220 {"%1 = OpConstantTrue !500",
221 spvtest::MakeInstruction(SpvOpSwitch, {1, 2, 3, 4}),
222 "Type Id 500 is not a type"},
223 {"%1 = OpTypeFloat 32\n%2 = OpConstant %1 1.5",
224 spvtest::MakeInstruction(SpvOpSwitch, {2, 3, 4, 5}),
225 "Invalid OpSwitch: selector id 2 is not a scalar integer"},
Steven Perron464111e2019-01-29 18:56:52 -0500226 }));
Andrew Woloszyn157e41b2015-10-16 15:11:00 -0400227
Lei Zhang39b01b92015-11-12 10:45:36 -0500228TEST_F(TextToBinaryTest, OneInstruction) {
229 const std::string input = "OpSource OpenCL_C 12\n";
230 EXPECT_EQ(input, EncodeAndDecodeSuccessfully(input));
David Netoc9a23a62015-08-31 13:34:28 -0400231}
232
David Neto9fa91572015-08-31 14:07:22 -0400233// Exercise the case where an operand itself has operands.
234// This could detect problems in updating the expected-set-of-operands
235// list.
Lei Zhang39b01b92015-11-12 10:45:36 -0500236TEST_F(TextToBinaryTest, OperandWithOperands) {
Lei Zhang39b01b92015-11-12 10:45:36 -0500237 const std::string input = R"(OpEntryPoint Kernel %1 "foo"
238OpExecutionMode %1 LocalSizeHint 100 200 300
239%2 = OpTypeVoid
240%3 = OpTypeFunction %2
241%1 = OpFunction %1 None %3
Lei Zhangb41d1502015-09-14 15:22:23 -0400242)";
Lei Zhang39b01b92015-11-12 10:45:36 -0500243 EXPECT_EQ(input, EncodeAndDecodeSuccessfully(input));
Lei Zhangb41d1502015-09-14 15:22:23 -0400244}
245
David Netoe5900732016-07-05 10:21:21 -0400246using RoundTripInstructionsTest = spvtest::TextToBinaryTestBase<
dan sinclaira5a5ea02018-08-01 14:58:12 -0400247 ::testing::TestWithParam<std::tuple<spv_target_env, std::string>>>;
David Neto619db262015-09-25 12:43:37 -0400248
249TEST_P(RoundTripInstructionsTest, Sample) {
dan sinclaira5a5ea02018-08-01 14:58:12 -0400250 EXPECT_THAT(EncodeAndDecodeSuccessfully(std::get<1>(GetParam()),
David Netoe5900732016-07-05 10:21:21 -0400251 SPV_BINARY_TO_TEXT_OPTION_NONE,
dan sinclaira5a5ea02018-08-01 14:58:12 -0400252 std::get<0>(GetParam())),
253 Eq(std::get<1>(GetParam())));
Andrew Woloszyn4c657bf2016-03-18 14:13:16 -0400254}
David Neto619db262015-09-25 12:43:37 -0400255
David Netodbc20492017-03-14 12:43:41 -0400256// clang-format off
Steven Perron464111e2019-01-29 18:56:52 -0500257INSTANTIATE_TEST_SUITE_P(
David Neto4f750c02016-10-13 15:17:11 -0400258 NumericLiterals, RoundTripInstructionsTest,
259 // This test is independent of environment, so just test the one.
David Netodbc20492017-03-14 12:43:41 -0400260 Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
David Neto00fa3932018-02-09 14:29:02 -0500261 SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_3),
David Neto4f750c02016-10-13 15:17:11 -0400262 ::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
Neil Roberts57a24412018-03-31 01:35:45 +0200276 "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -3.125\n",
David Neto4f750c02016-10-13 15:17:11 -0400277 "%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
Neil Roberts57a24412018-03-31 01:35:45 +0200282 "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -3.125\n",
David Neto4f750c02016-10-13 15:17:11 -0400283 "%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
Steven Perron464111e2019-01-29 18:56:52 -0500289 })));
David Netodbc20492017-03-14 12:43:41 -0400290// clang-format on
David Neto4f750c02016-10-13 15:17:11 -0400291
Steven Perron464111e2019-01-29 18:56:52 -0500292INSTANTIATE_TEST_SUITE_P(
David Neto619db262015-09-25 12:43:37 -0400293 MemoryAccessMasks, RoundTripInstructionsTest,
David Netodbc20492017-03-14 12:43:41 -0400294 Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
David Neto00fa3932018-02-09 14:29:02 -0500295 SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_3),
David Netoe5900732016-07-05 10:21:21 -0400296 ::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.
Diego Novillo83228132017-11-27 10:16:41 -0500299 "OpStore %1 %2 Volatile\n",
300 "OpStore %1 %2 Aligned 8\n",
David Netoe5900732016-07-05 10:21:21 -0400301 "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",
Steven Perron464111e2019-01-29 18:56:52 -0500306 })));
David Neto619db262015-09-25 12:43:37 -0400307
Steven Perron464111e2019-01-29 18:56:52 -0500308INSTANTIATE_TEST_SUITE_P(
David Neto619db262015-09-25 12:43:37 -0400309 FPFastMathModeMasks, RoundTripInstructionsTest,
David Netoe5900732016-07-05 10:21:21 -0400310 Combine(
David Netodbc20492017-03-14 12:43:41 -0400311 ::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
David Neto00fa3932018-02-09 14:29:02 -0500312 SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_3),
David Netoe5900732016-07-05 10:21:21 -0400313 ::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",
Steven Perron464111e2019-01-29 18:56:52 -0500324 })));
David Neto619db262015-09-25 12:43:37 -0400325
Steven Perron464111e2019-01-29 18:56:52 -0500326INSTANTIATE_TEST_SUITE_P(
David Neto00fa3932018-02-09 14:29:02 -0500327 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",
Steven Perron464111e2019-01-29 18:56:52 -0500335 })));
David Neto619db262015-09-25 12:43:37 -0400336
Steven Perron464111e2019-01-29 18:56:52 -0500337INSTANTIATE_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 })));
David Neto619db262015-09-25 12:43:37 -0400345
Steven Perron464111e2019-01-29 18:56:52 -0500346INSTANTIATE_TEST_SUITE_P(
David Neto00fa3932018-02-09 14:29:02 -0500347 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",
Steven Perron464111e2019-01-29 18:56:52 -0500355 })));
David Netoe5900732016-07-05 10:21:21 -0400356
Steven Perron464111e2019-01-29 18:56:52 -0500357INSTANTIATE_TEST_SUITE_P(
David Neto00fa3932018-02-09 14:29:02 -0500358 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",
Steven Perron464111e2019-01-29 18:56:52 -0500369 })));
David Neto619db262015-09-25 12:43:37 -0400370
Steven Perron464111e2019-01-29 18:56:52 -0500371INSTANTIATE_TEST_SUITE_P(
David Neto619db262015-09-25 12:43:37 -0400372 ImageMasks, RoundTripInstructionsTest,
David Netodbc20492017-03-14 12:43:41 -0400373 Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
David Neto00fa3932018-02-09 14:29:02 -0500374 SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_3),
David Netoe5900732016-07-05 10:21:21 -0400375 ::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"
Steven Perron464111e2019-01-29 18:56:52 -0500392 " %5 %6 %7 %8 %9 %10 %11 %12 %13\n"})));
David Neto619db262015-09-25 12:43:37 -0400393
Steven Perron464111e2019-01-29 18:56:52 -0500394INSTANTIATE_TEST_SUITE_P(
David Netodbc20492017-03-14 12:43:41 -0400395 NewInstructionsInSPIRV1_2, RoundTripInstructionsTest,
David Neto00fa3932018-02-09 14:29:02 -0500396 Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_2, SPV_ENV_UNIVERSAL_1_3),
David Netodbc20492017-03-14 12:43:41 -0400397 ::testing::ValuesIn(std::vector<std::string>{
398 "OpExecutionModeId %1 SubgroupsPerWorkgroupId %2\n",
399 "OpExecutionModeId %1 LocalSizeId %2 %3 %4\n",
alan-baker2a7a5612021-07-22 15:23:22 -0400400 "OpExecutionModeId %1 LocalSizeHintId %2 %3 %4\n",
David Netodbc20492017-03-14 12:43:41 -0400401 "OpDecorateId %1 AlignmentId %2\n",
402 "OpDecorateId %1 MaxByteOffsetId %2\n",
Steven Perron464111e2019-01-29 18:56:52 -0500403 })));
David Netodbc20492017-03-14 12:43:41 -0400404
Lei Zhang39b01b92015-11-12 10:45:36 -0500405using MaskSorting = TextToBinaryTest;
David Netof1b64712015-09-25 14:52:17 -0400406
407TEST_F(MaskSorting, MasksAreSortedFromLSBToMSB) {
David Netoe169a7c2015-10-22 13:24:41 -0400408 EXPECT_THAT(EncodeAndDecodeSuccessfully(
409 "OpStore %1 %2 Nontemporal|Aligned|Volatile 32"),
410 Eq("OpStore %1 %2 Volatile|Aligned|Nontemporal 32\n"));
David Netof1b64712015-09-25 14:52:17 -0400411 EXPECT_THAT(
412 EncodeAndDecodeSuccessfully(
413 "OpDecorate %1 FPFastMathMode NotInf|Fast|AllowRecip|NotNaN|NSZ"),
David Netoe169a7c2015-10-22 13:24:41 -0400414 Eq("OpDecorate %1 FPFastMathMode NotNaN|NotInf|NSZ|AllowRecip|Fast\n"));
David Netof1b64712015-09-25 14:52:17 -0400415 EXPECT_THAT(
416 EncodeAndDecodeSuccessfully("OpLoopMerge %1 %2 DontUnroll|Unroll"),
David Netoe169a7c2015-10-22 13:24:41 -0400417 Eq("OpLoopMerge %1 %2 Unroll|DontUnroll\n"));
David Netof1b64712015-09-25 14:52:17 -0400418 EXPECT_THAT(
419 EncodeAndDecodeSuccessfully("OpSelectionMerge %1 DontFlatten|Flatten"),
David Netoe169a7c2015-10-22 13:24:41 -0400420 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"));
David Netof1b64712015-09-25 14:52:17 -0400424 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"),
David Netoe169a7c2015-10-22 13:24:41 -0400428 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"));
David Netof1b64712015-09-25 14:52:17 -0400431}
432
Lei Zhang39b01b92015-11-12 10:45:36 -0500433using OperandTypeTest = TextToBinaryTest;
Lei Zhangaa3cd5a2015-11-10 14:29:35 -0500434
435TEST_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
David Netoe7c426a2015-11-12 13:53:27 -0500443using IndentTest = spvtest::TextToBinaryTest;
444
445TEST_F(IndentTest, Sample) {
446 const std::string input = R"(
447OpCapability Shader
448OpMemoryModel 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
452OpStore %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
David Neto0bdcc232016-07-08 14:29:52 -0400467using FriendlyNameDisassemblyTest = spvtest::TextToBinaryTest;
468
469TEST_F(FriendlyNameDisassemblyTest, Sample) {
470 const std::string input = R"(
471OpCapability Shader
472OpMemoryModel 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
479OpMemoryModel Logical GLSL450
480%uint = OpTypeInt 32 0
481%_struct_2 = OpTypeStruct %uint %3 %4 %5 %6 %7 %8 %9 %10
David Netoc9352532016-10-13 16:22:04 -0400482%uint_42 = OpConstant %uint 42
David Neto0bdcc232016-07-08 14:29:52 -0400483)";
484 EXPECT_THAT(EncodeAndDecodeSuccessfully(
485 input, SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES),
486 expected);
487}
488
David Netoc62f41e2016-01-18 15:29:15 -0500489TEST_F(TextToBinaryTest, ShowByteOffsetsWhenRequested) {
490 const std::string input = R"(
491OpCapability Shader
492OpMemoryModel Logical GLSL450
493%1 = OpTypeInt 32 0
494%2 = OpTypeVoid
495)";
496 const std::string expected =
497 R"(OpCapability Shader ; 0x00000014
498OpMemoryModel 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
David Neto3d348a82015-11-12 19:40:21 -0500507// Test version string.
508TEST_F(TextToBinaryTest, VersionString) {
509 auto words = CompileSuccessfully("");
510 spv_text decoded_text = nullptr;
Dejan Mircevski7b334852016-04-18 22:25:35 -0400511 EXPECT_THAT(spvBinaryToText(ScopedContext().context, words.data(),
512 words.size(), SPV_BINARY_TO_TEXT_OPTION_NONE,
513 &decoded_text, &diagnostic),
David Neto3d348a82015-11-12 19:40:21 -0500514 Eq(SPV_SUCCESS));
515 EXPECT_EQ(nullptr, diagnostic);
516
David Neto8ddd4ec2015-11-17 16:37:10 -0500517 EXPECT_THAT(decoded_text->str, HasSubstr("Version: 1.0\n"))
David Neto3d348a82015-11-12 19:40:21 -0500518 << EncodeAndDecodeSuccessfully("");
519 spvTextDestroy(decoded_text);
520}
521
David Neto14b93e42015-11-12 18:33:47 -0500522// Test generator string.
523
524// A test case for the generator string. This allows us to
525// test both of the 16-bit components of the generator word.
526struct GeneratorStringCase {
527 uint16_t generator;
528 uint16_t misc;
529 std::string expected;
530};
531
532using GeneratorStringTest = spvtest::TextToBinaryTestBase<
533 ::testing::TestWithParam<GeneratorStringCase>>;
534
535TEST_P(GeneratorStringTest, Sample) {
536 auto words = CompileSuccessfully("");
Lei Zhang8bd75d62015-11-18 09:22:10 -0500537 EXPECT_EQ(2u, SPV_INDEX_GENERATOR_NUMBER);
David Neto14b93e42015-11-12 18:33:47 -0500538 words[SPV_INDEX_GENERATOR_NUMBER] =
539 SPV_GENERATOR_WORD(GetParam().generator, GetParam().misc);
540
541 spv_text decoded_text = nullptr;
Dejan Mircevski7b334852016-04-18 22:25:35 -0400542 EXPECT_THAT(spvBinaryToText(ScopedContext().context, words.data(),
543 words.size(), SPV_BINARY_TO_TEXT_OPTION_NONE,
544 &decoded_text, &diagnostic),
David Neto14b93e42015-11-12 18:33:47 -0500545 Eq(SPV_SUCCESS));
546 EXPECT_THAT(diagnostic, Eq(nullptr));
547 EXPECT_THAT(std::string(decoded_text->str), HasSubstr(GetParam().expected));
548 spvTextDestroy(decoded_text);
549}
550
Steven Perron464111e2019-01-29 18:56:52 -0500551INSTANTIATE_TEST_SUITE_P(GeneratorStrings, GeneratorStringTest,
552 ::testing::ValuesIn(std::vector<GeneratorStringCase>{
553 {SPV_GENERATOR_KHRONOS, 12, "Khronos; 12"},
554 {SPV_GENERATOR_LUNARG, 99, "LunarG; 99"},
555 {SPV_GENERATOR_VALVE, 1, "Valve; 1"},
556 {SPV_GENERATOR_CODEPLAY, 65535, "Codeplay; 65535"},
557 {SPV_GENERATOR_NVIDIA, 19, "NVIDIA; 19"},
558 {SPV_GENERATOR_ARM, 1000, "ARM; 1000"},
559 {SPV_GENERATOR_KHRONOS_LLVM_TRANSLATOR, 38,
560 "Khronos LLVM/SPIR-V Translator; 38"},
561 {SPV_GENERATOR_KHRONOS_ASSEMBLER, 2,
562 "Khronos SPIR-V Tools Assembler; 2"},
563 {SPV_GENERATOR_KHRONOS_GLSLANG, 1,
564 "Khronos Glslang Reference Front End; 1"},
565 {1000, 18, "Unknown(1000); 18"},
566 {65535, 32767, "Unknown(65535); 32767"},
567 }));
David Neto14b93e42015-11-12 18:33:47 -0500568
David Neto00fa3932018-02-09 14:29:02 -0500569// TODO(dneto): Test new instructions and enums in SPIR-V 1.3
570
dan sinclair2cce2c52018-07-11 09:24:49 -0400571} // namespace
572} // namespace spvtools