binary_to_text_test.cpp 22 KB

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