binary_to_text_test.cpp 23 KB

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