text_to_binary_test.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 <algorithm>
  15. #include <cstring>
  16. #include <string>
  17. #include <utility>
  18. #include <vector>
  19. #include "gmock/gmock.h"
  20. #include "source/spirv_constant.h"
  21. #include "source/util/bitutils.h"
  22. #include "source/util/hex_float.h"
  23. #include "test/test_fixture.h"
  24. #include "test/unit_spirv.h"
  25. namespace spvtools {
  26. namespace {
  27. using spvtest::AutoText;
  28. using spvtest::Concatenate;
  29. using spvtest::MakeInstruction;
  30. using spvtest::ScopedContext;
  31. using spvtest::TextToBinaryTest;
  32. using testing::Eq;
  33. using testing::IsNull;
  34. using testing::NotNull;
  35. // An mask parsing test case.
  36. struct MaskCase {
  37. spv_operand_type_t which_enum;
  38. uint32_t expected_value;
  39. const char* expression;
  40. };
  41. using GoodMaskParseTest = ::testing::TestWithParam<MaskCase>;
  42. TEST_P(GoodMaskParseTest, GoodMaskExpressions) {
  43. spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_0);
  44. uint32_t value;
  45. EXPECT_EQ(SPV_SUCCESS,
  46. AssemblyGrammar(context).parseMaskOperand(
  47. GetParam().which_enum, GetParam().expression, &value));
  48. EXPECT_EQ(GetParam().expected_value, value);
  49. spvContextDestroy(context);
  50. }
  51. INSTANTIATE_TEST_SUITE_P(
  52. ParseMask, GoodMaskParseTest,
  53. ::testing::ValuesIn(std::vector<MaskCase>{
  54. {SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 0, "None"},
  55. {SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 1, "NotNaN"},
  56. {SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 2, "NotInf"},
  57. {SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 3, "NotNaN|NotInf"},
  58. // Mask experssions are symmetric.
  59. {SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 3, "NotInf|NotNaN"},
  60. // Repeating a value has no effect.
  61. {SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 3, "NotInf|NotNaN|NotInf"},
  62. // Using 3 operands still works.
  63. {SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, 0x13, "NotInf|NotNaN|Fast"},
  64. {SPV_OPERAND_TYPE_SELECTION_CONTROL, 0, "None"},
  65. {SPV_OPERAND_TYPE_SELECTION_CONTROL, 1, "Flatten"},
  66. {SPV_OPERAND_TYPE_SELECTION_CONTROL, 2, "DontFlatten"},
  67. // Weirdly, you can specify to flatten and don't flatten a selection.
  68. {SPV_OPERAND_TYPE_SELECTION_CONTROL, 3, "Flatten|DontFlatten"},
  69. {SPV_OPERAND_TYPE_LOOP_CONTROL, 0, "None"},
  70. {SPV_OPERAND_TYPE_LOOP_CONTROL, 1, "Unroll"},
  71. {SPV_OPERAND_TYPE_LOOP_CONTROL, 2, "DontUnroll"},
  72. // Weirdly, you can specify to unroll and don't unroll a loop.
  73. {SPV_OPERAND_TYPE_LOOP_CONTROL, 3, "Unroll|DontUnroll"},
  74. {SPV_OPERAND_TYPE_FUNCTION_CONTROL, 0, "None"},
  75. {SPV_OPERAND_TYPE_FUNCTION_CONTROL, 1, "Inline"},
  76. {SPV_OPERAND_TYPE_FUNCTION_CONTROL, 2, "DontInline"},
  77. {SPV_OPERAND_TYPE_FUNCTION_CONTROL, 4, "Pure"},
  78. {SPV_OPERAND_TYPE_FUNCTION_CONTROL, 8, "Const"},
  79. {SPV_OPERAND_TYPE_FUNCTION_CONTROL, 0xd, "Inline|Const|Pure"},
  80. }));
  81. using BadFPFastMathMaskParseTest = ::testing::TestWithParam<const char*>;
  82. TEST_P(BadFPFastMathMaskParseTest, BadMaskExpressions) {
  83. spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_0);
  84. uint32_t value;
  85. EXPECT_NE(SPV_SUCCESS,
  86. AssemblyGrammar(context).parseMaskOperand(
  87. SPV_OPERAND_TYPE_FP_FAST_MATH_MODE, GetParam(), &value));
  88. spvContextDestroy(context);
  89. }
  90. INSTANTIATE_TEST_SUITE_P(ParseMask, BadFPFastMathMaskParseTest,
  91. ::testing::ValuesIn(std::vector<const char*>{
  92. nullptr, "", "NotValidEnum", "|", "NotInf|",
  93. "|NotInf", "NotInf||NotNaN",
  94. "Unroll" // A good word, but for the wrong enum
  95. }));
  96. TEST_F(TextToBinaryTest, InvalidText) {
  97. ASSERT_EQ(SPV_ERROR_INVALID_TEXT,
  98. spvTextToBinary(ScopedContext().context, nullptr, 0, &binary,
  99. &diagnostic));
  100. EXPECT_NE(nullptr, diagnostic);
  101. EXPECT_THAT(diagnostic->error, Eq(std::string("Missing assembly text.")));
  102. }
  103. TEST_F(TextToBinaryTest, InvalidPointer) {
  104. SetText(
  105. "OpEntryPoint Kernel 0 \"\"\nOpExecutionMode 0 LocalSizeHint 1 1 1\n");
  106. ASSERT_EQ(SPV_ERROR_INVALID_POINTER,
  107. spvTextToBinary(ScopedContext().context, text.str, text.length,
  108. nullptr, &diagnostic));
  109. }
  110. TEST_F(TextToBinaryTest, InvalidPrefix) {
  111. EXPECT_EQ(
  112. "Expected <opcode> or <result-id> at the beginning of an instruction, "
  113. "found 'Invalid'.",
  114. CompileFailure("Invalid"));
  115. }
  116. TEST_F(TextToBinaryTest, EmptyAssemblyString) {
  117. // An empty assembly module is valid!
  118. // It should produce a valid module with zero instructions.
  119. EXPECT_THAT(CompiledInstructions(""), Eq(std::vector<uint32_t>{}));
  120. }
  121. TEST_F(TextToBinaryTest, StringSpace) {
  122. const std::string code = ("OpSourceExtension \"string with spaces\"\n");
  123. EXPECT_EQ(code, EncodeAndDecodeSuccessfully(code));
  124. }
  125. TEST_F(TextToBinaryTest, UnknownBeginningOfInstruction) {
  126. EXPECT_EQ(
  127. "Expected <opcode> or <result-id> at the beginning of an instruction, "
  128. "found 'Google'.",
  129. CompileFailure(
  130. "\nOpSource OpenCL_C 12\nOpMemoryModel Physical64 OpenCL\nGoogle\n"));
  131. EXPECT_EQ(4u, diagnostic->position.line + 1);
  132. EXPECT_EQ(1u, diagnostic->position.column + 1);
  133. }
  134. TEST_F(TextToBinaryTest, NoEqualSign) {
  135. EXPECT_EQ("Expected '=', found end of stream.",
  136. CompileFailure("\nOpSource OpenCL_C 12\n"
  137. "OpMemoryModel Physical64 OpenCL\n%2\n"));
  138. EXPECT_EQ(5u, diagnostic->position.line + 1);
  139. EXPECT_EQ(1u, diagnostic->position.column + 1);
  140. }
  141. TEST_F(TextToBinaryTest, NoOpCode) {
  142. EXPECT_EQ("Expected opcode, found end of stream.",
  143. CompileFailure("\nOpSource OpenCL_C 12\n"
  144. "OpMemoryModel Physical64 OpenCL\n%2 =\n"));
  145. EXPECT_EQ(5u, diagnostic->position.line + 1);
  146. EXPECT_EQ(1u, diagnostic->position.column + 1);
  147. }
  148. TEST_F(TextToBinaryTest, WrongOpCode) {
  149. EXPECT_EQ("Invalid Opcode prefix 'Wahahaha'.",
  150. CompileFailure("\nOpSource OpenCL_C 12\n"
  151. "OpMemoryModel Physical64 OpenCL\n%2 = Wahahaha\n"));
  152. EXPECT_EQ(4u, diagnostic->position.line + 1);
  153. EXPECT_EQ(6u, diagnostic->position.column + 1);
  154. }
  155. TEST_F(TextToBinaryTest, CRLF) {
  156. const std::string input =
  157. "%i32 = OpTypeInt 32 1\r\n%c = OpConstant %i32 123\r\n";
  158. EXPECT_THAT(CompiledInstructions(input),
  159. Eq(Concatenate({MakeInstruction(SpvOpTypeInt, {1, 32, 1}),
  160. MakeInstruction(SpvOpConstant, {1, 2, 123})})));
  161. }
  162. using TextToBinaryFloatValueTest = spvtest::TextToBinaryTestBase<
  163. ::testing::TestWithParam<std::pair<std::string, uint32_t>>>;
  164. TEST_P(TextToBinaryFloatValueTest, Samples) {
  165. const std::string input =
  166. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 " + GetParam().first;
  167. EXPECT_THAT(CompiledInstructions(input),
  168. Eq(Concatenate({MakeInstruction(SpvOpTypeFloat, {1, 32}),
  169. MakeInstruction(SpvOpConstant,
  170. {1, 2, GetParam().second})})));
  171. }
  172. INSTANTIATE_TEST_SUITE_P(
  173. FloatValues, TextToBinaryFloatValueTest,
  174. ::testing::ValuesIn(std::vector<std::pair<std::string, uint32_t>>{
  175. {"0.0", 0x00000000}, // +0
  176. {"!0x00000001", 0x00000001}, // +denorm
  177. {"!0x00800000", 0x00800000}, // +norm
  178. {"1.5", 0x3fc00000},
  179. {"!0x7f800000", 0x7f800000}, // +inf
  180. {"!0x7f800001", 0x7f800001}, // NaN
  181. {"-0.0", 0x80000000}, // -0
  182. {"!0x80000001", 0x80000001}, // -denorm
  183. {"!0x80800000", 0x80800000}, // -norm
  184. {"-2.5", 0xc0200000},
  185. {"!0xff800000", 0xff800000}, // -inf
  186. {"!0xff800001", 0xff800001}, // NaN
  187. }));
  188. using TextToBinaryHalfValueTest = spvtest::TextToBinaryTestBase<
  189. ::testing::TestWithParam<std::pair<std::string, uint32_t>>>;
  190. TEST_P(TextToBinaryHalfValueTest, Samples) {
  191. const std::string input =
  192. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 " + GetParam().first;
  193. EXPECT_THAT(CompiledInstructions(input),
  194. Eq(Concatenate({MakeInstruction(SpvOpTypeFloat, {1, 16}),
  195. MakeInstruction(SpvOpConstant,
  196. {1, 2, GetParam().second})})));
  197. }
  198. INSTANTIATE_TEST_SUITE_P(
  199. HalfValues, TextToBinaryHalfValueTest,
  200. ::testing::ValuesIn(std::vector<std::pair<std::string, uint32_t>>{
  201. {"0.0", 0x00000000},
  202. {"1.0", 0x00003c00},
  203. {"1.000844", 0x00003c00}, // Truncate to 1.0
  204. {"1.000977", 0x00003c01}, // Don't have to truncate
  205. {"1.001465", 0x00003c01}, // Truncate to 1.0000977
  206. {"1.5", 0x00003e00},
  207. {"-1.0", 0x0000bc00},
  208. {"2.0", 0x00004000},
  209. {"-2.0", 0x0000c000},
  210. {"0x1p1", 0x00004000},
  211. {"-0x1p1", 0x0000c000},
  212. {"0x1.8p1", 0x00004200},
  213. {"0x1.8p4", 0x00004e00},
  214. {"0x1.801p4", 0x00004e00},
  215. {"0x1.804p4", 0x00004e01},
  216. }));
  217. TEST(CreateContext, InvalidEnvironment) {
  218. spv_target_env env;
  219. std::memset(&env, 99, sizeof(env));
  220. EXPECT_THAT(spvContextCreate(env), IsNull());
  221. }
  222. TEST(CreateContext, UniversalEnvironment) {
  223. auto c = spvContextCreate(SPV_ENV_UNIVERSAL_1_0);
  224. EXPECT_THAT(c, NotNull());
  225. spvContextDestroy(c);
  226. }
  227. TEST(CreateContext, VulkanEnvironment) {
  228. auto c = spvContextCreate(SPV_ENV_VULKAN_1_0);
  229. EXPECT_THAT(c, NotNull());
  230. spvContextDestroy(c);
  231. }
  232. } // namespace
  233. } // namespace spvtools