text_to_binary.control_flow_test.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. // Assembler tests for instructions in the "Control Flow" section of the
  15. // SPIR-V spec.
  16. #include <sstream>
  17. #include <string>
  18. #include <tuple>
  19. #include <vector>
  20. #include "gmock/gmock.h"
  21. #include "test/test_fixture.h"
  22. #include "test/unit_spirv.h"
  23. namespace spvtools {
  24. namespace {
  25. using spvtest::Concatenate;
  26. using spvtest::EnumCase;
  27. using spvtest::MakeInstruction;
  28. using spvtest::TextToBinaryTest;
  29. using ::testing::Combine;
  30. using ::testing::Eq;
  31. using ::testing::TestWithParam;
  32. using ::testing::Values;
  33. using ::testing::ValuesIn;
  34. // Test OpSelectionMerge
  35. using OpSelectionMergeTest = spvtest::TextToBinaryTestBase<
  36. TestWithParam<EnumCase<SpvSelectionControlMask>>>;
  37. TEST_P(OpSelectionMergeTest, AnySingleSelectionControlMask) {
  38. const std::string input = "OpSelectionMerge %1 " + GetParam().name();
  39. EXPECT_THAT(
  40. CompiledInstructions(input),
  41. Eq(MakeInstruction(SpvOpSelectionMerge, {1, GetParam().value()})));
  42. }
  43. // clang-format off
  44. #define CASE(VALUE,NAME) { SpvSelectionControl##VALUE, NAME}
  45. INSTANTIATE_TEST_SUITE_P(TextToBinarySelectionMerge, OpSelectionMergeTest,
  46. ValuesIn(std::vector<EnumCase<SpvSelectionControlMask>>{
  47. CASE(MaskNone, "None"),
  48. CASE(FlattenMask, "Flatten"),
  49. CASE(DontFlattenMask, "DontFlatten"),
  50. }));
  51. #undef CASE
  52. // clang-format on
  53. TEST_F(OpSelectionMergeTest, CombinedSelectionControlMask) {
  54. const std::string input = "OpSelectionMerge %1 Flatten|DontFlatten";
  55. const uint32_t expected_mask =
  56. SpvSelectionControlFlattenMask | SpvSelectionControlDontFlattenMask;
  57. EXPECT_THAT(CompiledInstructions(input),
  58. Eq(MakeInstruction(SpvOpSelectionMerge, {1, expected_mask})));
  59. }
  60. TEST_F(OpSelectionMergeTest, WrongSelectionControl) {
  61. // Case sensitive: "flatten" != "Flatten" and thus wrong.
  62. EXPECT_THAT(CompileFailure("OpSelectionMerge %1 flatten|DontFlatten"),
  63. Eq("Invalid selection control operand 'flatten|DontFlatten'."));
  64. }
  65. // Test OpLoopMerge
  66. using OpLoopMergeTest = spvtest::TextToBinaryTestBase<
  67. TestWithParam<std::tuple<spv_target_env, EnumCase<int>>>>;
  68. TEST_P(OpLoopMergeTest, AnySingleLoopControlMask) {
  69. const auto ctrl = std::get<1>(GetParam());
  70. std::ostringstream input;
  71. input << "OpLoopMerge %merge %continue " << ctrl.name();
  72. for (auto num : ctrl.operands()) input << " " << num;
  73. EXPECT_THAT(CompiledInstructions(input.str(), std::get<0>(GetParam())),
  74. Eq(MakeInstruction(SpvOpLoopMerge, {1, 2, ctrl.value()},
  75. ctrl.operands())));
  76. }
  77. #define CASE(VALUE, NAME) \
  78. { SpvLoopControl##VALUE, NAME }
  79. #define CASE1(VALUE, NAME, PARM) \
  80. { \
  81. SpvLoopControl##VALUE, NAME, { PARM } \
  82. }
  83. INSTANTIATE_TEST_SUITE_P(
  84. TextToBinaryLoopMerge, OpLoopMergeTest,
  85. Combine(Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1),
  86. ValuesIn(std::vector<EnumCase<int>>{
  87. // clang-format off
  88. CASE(MaskNone, "None"),
  89. CASE(UnrollMask, "Unroll"),
  90. CASE(DontUnrollMask, "DontUnroll"),
  91. // clang-format on
  92. })));
  93. INSTANTIATE_TEST_SUITE_P(
  94. TextToBinaryLoopMergeV11, OpLoopMergeTest,
  95. Combine(Values(SPV_ENV_UNIVERSAL_1_1),
  96. ValuesIn(std::vector<EnumCase<int>>{
  97. // clang-format off
  98. CASE(DependencyInfiniteMask, "DependencyInfinite"),
  99. CASE1(DependencyLengthMask, "DependencyLength", 234),
  100. {SpvLoopControlUnrollMask|SpvLoopControlDependencyLengthMask,
  101. "DependencyLength|Unroll", {33}},
  102. // clang-format on
  103. })));
  104. #undef CASE
  105. #undef CASE1
  106. TEST_F(OpLoopMergeTest, CombinedLoopControlMask) {
  107. const std::string input = "OpLoopMerge %merge %continue Unroll|DontUnroll";
  108. const uint32_t expected_mask =
  109. SpvLoopControlUnrollMask | SpvLoopControlDontUnrollMask;
  110. EXPECT_THAT(CompiledInstructions(input),
  111. Eq(MakeInstruction(SpvOpLoopMerge, {1, 2, expected_mask})));
  112. }
  113. TEST_F(OpLoopMergeTest, WrongLoopControl) {
  114. EXPECT_THAT(CompileFailure("OpLoopMerge %m %c none"),
  115. Eq("Invalid loop control operand 'none'."));
  116. }
  117. // Test OpSwitch
  118. TEST_F(TextToBinaryTest, SwitchGoodZeroTargets) {
  119. EXPECT_THAT(CompiledInstructions("OpSwitch %selector %default"),
  120. Eq(MakeInstruction(SpvOpSwitch, {1, 2})));
  121. }
  122. TEST_F(TextToBinaryTest, SwitchGoodOneTarget) {
  123. EXPECT_THAT(CompiledInstructions("%1 = OpTypeInt 32 0\n"
  124. "%2 = OpConstant %1 52\n"
  125. "OpSwitch %2 %default 12 %target0"),
  126. Eq(Concatenate({MakeInstruction(SpvOpTypeInt, {1, 32, 0}),
  127. MakeInstruction(SpvOpConstant, {1, 2, 52}),
  128. MakeInstruction(SpvOpSwitch, {2, 3, 12, 4})})));
  129. }
  130. TEST_F(TextToBinaryTest, SwitchGoodTwoTargets) {
  131. EXPECT_THAT(
  132. CompiledInstructions("%1 = OpTypeInt 32 0\n"
  133. "%2 = OpConstant %1 52\n"
  134. "OpSwitch %2 %default 12 %target0 42 %target1"),
  135. Eq(Concatenate({
  136. MakeInstruction(SpvOpTypeInt, {1, 32, 0}),
  137. MakeInstruction(SpvOpConstant, {1, 2, 52}),
  138. MakeInstruction(SpvOpSwitch, {2, 3, 12, 4, 42, 5}),
  139. })));
  140. }
  141. TEST_F(TextToBinaryTest, SwitchBadMissingSelector) {
  142. EXPECT_THAT(CompileFailure("OpSwitch"),
  143. Eq("Expected operand, found end of stream."));
  144. }
  145. TEST_F(TextToBinaryTest, SwitchBadInvalidSelector) {
  146. EXPECT_THAT(CompileFailure("OpSwitch 12"),
  147. Eq("Expected id to start with %."));
  148. }
  149. TEST_F(TextToBinaryTest, SwitchBadMissingDefault) {
  150. EXPECT_THAT(CompileFailure("OpSwitch %selector"),
  151. Eq("Expected operand, found end of stream."));
  152. }
  153. TEST_F(TextToBinaryTest, SwitchBadInvalidDefault) {
  154. EXPECT_THAT(CompileFailure("OpSwitch %selector 12"),
  155. Eq("Expected id to start with %."));
  156. }
  157. TEST_F(TextToBinaryTest, SwitchBadInvalidLiteral) {
  158. // The assembler recognizes "OpSwitch %selector %default" as a complete
  159. // instruction. Then it tries to parse "%abc" as the start of a new
  160. // instruction, but can't since it hits the end of stream.
  161. const auto input = R"(%i32 = OpTypeInt 32 0
  162. %selector = OpConstant %i32 42
  163. OpSwitch %selector %default %abc)";
  164. EXPECT_THAT(CompileFailure(input), Eq("Expected '=', found end of stream."));
  165. }
  166. TEST_F(TextToBinaryTest, SwitchBadMissingTarget) {
  167. EXPECT_THAT(CompileFailure("%1 = OpTypeInt 32 0\n"
  168. "%2 = OpConstant %1 52\n"
  169. "OpSwitch %2 %default 12"),
  170. Eq("Expected operand, found end of stream."));
  171. }
  172. // A test case for an OpSwitch.
  173. // It is also parameterized to test encodings OpConstant
  174. // integer literals. This can capture both single and multi-word
  175. // integer literal tests.
  176. struct SwitchTestCase {
  177. std::string constant_type_args;
  178. std::string constant_value_arg;
  179. std::string case_value_arg;
  180. std::vector<uint32_t> expected_instructions;
  181. };
  182. using OpSwitchValidTest =
  183. spvtest::TextToBinaryTestBase<TestWithParam<SwitchTestCase>>;
  184. // Tests the encoding of OpConstant literal values, and also
  185. // the literal integer cases in an OpSwitch. This can
  186. // test both single and multi-word integer literal encodings.
  187. TEST_P(OpSwitchValidTest, ValidTypes) {
  188. const std::string input = "%1 = OpTypeInt " + GetParam().constant_type_args +
  189. "\n"
  190. "%2 = OpConstant %1 " +
  191. GetParam().constant_value_arg +
  192. "\n"
  193. "OpSwitch %2 %default " +
  194. GetParam().case_value_arg + " %4\n";
  195. std::vector<uint32_t> instructions;
  196. EXPECT_THAT(CompiledInstructions(input),
  197. Eq(GetParam().expected_instructions));
  198. }
  199. // Constructs a SwitchTestCase from the given integer_width, signedness,
  200. // constant value string, and expected encoded constant.
  201. SwitchTestCase MakeSwitchTestCase(uint32_t integer_width,
  202. uint32_t integer_signedness,
  203. std::string constant_str,
  204. std::vector<uint32_t> encoded_constant,
  205. std::string case_value_str,
  206. std::vector<uint32_t> encoded_case_value) {
  207. std::stringstream ss;
  208. ss << integer_width << " " << integer_signedness;
  209. return SwitchTestCase{
  210. ss.str(),
  211. constant_str,
  212. case_value_str,
  213. {Concatenate(
  214. {MakeInstruction(SpvOpTypeInt,
  215. {1, integer_width, integer_signedness}),
  216. MakeInstruction(SpvOpConstant,
  217. Concatenate({{1, 2}, encoded_constant})),
  218. MakeInstruction(SpvOpSwitch,
  219. Concatenate({{2, 3}, encoded_case_value, {4}}))})}};
  220. }
  221. INSTANTIATE_TEST_SUITE_P(
  222. TextToBinaryOpSwitchValid1Word, OpSwitchValidTest,
  223. ValuesIn(std::vector<SwitchTestCase>({
  224. MakeSwitchTestCase(32, 0, "42", {42}, "100", {100}),
  225. MakeSwitchTestCase(32, 1, "-1", {0xffffffff}, "100", {100}),
  226. // SPIR-V 1.0 Rev 1 clarified that for an integer narrower than 32-bits,
  227. // its bits will appear in the lower order bits of the 32-bit word, and
  228. // a signed integer is sign-extended.
  229. MakeSwitchTestCase(7, 0, "127", {127}, "100", {100}),
  230. MakeSwitchTestCase(14, 0, "99", {99}, "100", {100}),
  231. MakeSwitchTestCase(16, 0, "65535", {65535}, "100", {100}),
  232. MakeSwitchTestCase(16, 1, "101", {101}, "100", {100}),
  233. // Demonstrate sign extension
  234. MakeSwitchTestCase(16, 1, "-2", {0xfffffffe}, "100", {100}),
  235. // Hex cases
  236. MakeSwitchTestCase(16, 1, "0x7ffe", {0x7ffe}, "0x1234", {0x1234}),
  237. MakeSwitchTestCase(16, 1, "0x8000", {0xffff8000}, "0x8100",
  238. {0xffff8100}),
  239. MakeSwitchTestCase(16, 0, "0x8000", {0x00008000}, "0x8100", {0x8100}),
  240. })));
  241. // NB: The words LOW ORDER bits show up first.
  242. INSTANTIATE_TEST_SUITE_P(
  243. TextToBinaryOpSwitchValid2Words, OpSwitchValidTest,
  244. ValuesIn(std::vector<SwitchTestCase>({
  245. MakeSwitchTestCase(33, 0, "101", {101, 0}, "500", {500, 0}),
  246. MakeSwitchTestCase(48, 1, "-1", {0xffffffff, 0xffffffff}, "900",
  247. {900, 0}),
  248. MakeSwitchTestCase(64, 1, "-2", {0xfffffffe, 0xffffffff}, "-5",
  249. {0xfffffffb, uint32_t(-1)}),
  250. // Hex cases
  251. MakeSwitchTestCase(48, 1, "0x7fffffffffff", {0xffffffff, 0x00007fff},
  252. "100", {100, 0}),
  253. MakeSwitchTestCase(48, 1, "0x800000000000", {0x00000000, 0xffff8000},
  254. "0x800000000000", {0x00000000, 0xffff8000}),
  255. MakeSwitchTestCase(48, 0, "0x800000000000", {0x00000000, 0x00008000},
  256. "0x800000000000", {0x00000000, 0x00008000}),
  257. MakeSwitchTestCase(63, 0, "0x500000000", {0, 5}, "12", {12, 0}),
  258. MakeSwitchTestCase(64, 0, "0x600000000", {0, 6}, "12", {12, 0}),
  259. MakeSwitchTestCase(64, 1, "0x700000123", {0x123, 7}, "12", {12, 0}),
  260. })));
  261. INSTANTIATE_TEST_SUITE_P(
  262. OpSwitchRoundTripUnsignedIntegers, RoundTripTest,
  263. ValuesIn(std::vector<std::string>({
  264. // Unsigned 16-bit.
  265. "%1 = OpTypeInt 16 0\n%2 = OpConstant %1 65535\nOpSwitch %2 %3\n",
  266. // Unsigned 32-bit, three non-default cases.
  267. "%1 = OpTypeInt 32 0\n%2 = OpConstant %1 123456\n"
  268. "OpSwitch %2 %3 100 %4 102 %5 1000000 %6\n",
  269. // Unsigned 48-bit, three non-default cases.
  270. "%1 = OpTypeInt 48 0\n%2 = OpConstant %1 5000000000\n"
  271. "OpSwitch %2 %3 100 %4 102 %5 6000000000 %6\n",
  272. // Unsigned 64-bit, three non-default cases.
  273. "%1 = OpTypeInt 64 0\n%2 = OpConstant %1 9223372036854775807\n"
  274. "OpSwitch %2 %3 100 %4 102 %5 9000000000000000000 %6\n",
  275. })));
  276. INSTANTIATE_TEST_SUITE_P(
  277. OpSwitchRoundTripSignedIntegers, RoundTripTest,
  278. ValuesIn(std::vector<std::string>{
  279. // Signed 16-bit, with two non-default cases
  280. "%1 = OpTypeInt 16 1\n%2 = OpConstant %1 32767\n"
  281. "OpSwitch %2 %3 99 %4 -102 %5\n",
  282. "%1 = OpTypeInt 16 1\n%2 = OpConstant %1 -32768\n"
  283. "OpSwitch %2 %3 99 %4 -102 %5\n",
  284. // Signed 32-bit, two non-default cases.
  285. "%1 = OpTypeInt 32 1\n%2 = OpConstant %1 -123456\n"
  286. "OpSwitch %2 %3 100 %4 -123456 %5\n",
  287. "%1 = OpTypeInt 32 1\n%2 = OpConstant %1 123456\n"
  288. "OpSwitch %2 %3 100 %4 123456 %5\n",
  289. // Signed 48-bit, three non-default cases.
  290. "%1 = OpTypeInt 48 1\n%2 = OpConstant %1 5000000000\n"
  291. "OpSwitch %2 %3 100 %4 -7000000000 %5 6000000000 %6\n",
  292. "%1 = OpTypeInt 48 1\n%2 = OpConstant %1 -5000000000\n"
  293. "OpSwitch %2 %3 100 %4 -7000000000 %5 6000000000 %6\n",
  294. // Signed 64-bit, three non-default cases.
  295. "%1 = OpTypeInt 64 1\n%2 = OpConstant %1 9223372036854775807\n"
  296. "OpSwitch %2 %3 100 %4 7000000000 %5 -1000000000000000000 %6\n",
  297. "%1 = OpTypeInt 64 1\n%2 = OpConstant %1 -9223372036854775808\n"
  298. "OpSwitch %2 %3 100 %4 7000000000 %5 -1000000000000000000 %6\n",
  299. }));
  300. using OpSwitchInvalidTypeTestCase =
  301. spvtest::TextToBinaryTestBase<TestWithParam<std::string>>;
  302. TEST_P(OpSwitchInvalidTypeTestCase, InvalidTypes) {
  303. const std::string input =
  304. "%1 = " + GetParam() +
  305. "\n"
  306. "%3 = OpCopyObject %1 %2\n" // We only care the type of the expression
  307. " OpSwitch %3 %default 32 %c\n";
  308. EXPECT_THAT(CompileFailure(input),
  309. Eq("The selector operand for OpSwitch must be the result of an "
  310. "instruction that generates an integer scalar"));
  311. }
  312. // clang-format off
  313. INSTANTIATE_TEST_SUITE_P(
  314. TextToBinaryOpSwitchInvalidTests, OpSwitchInvalidTypeTestCase,
  315. ValuesIn(std::vector<std::string>{
  316. {"OpTypeVoid",
  317. "OpTypeBool",
  318. "OpTypeFloat 32",
  319. "OpTypeVector %a 32",
  320. "OpTypeMatrix %a 32",
  321. "OpTypeImage %a 1D 0 0 0 0 Unknown",
  322. "OpTypeSampler",
  323. "OpTypeSampledImage %a",
  324. "OpTypeArray %a %b",
  325. "OpTypeRuntimeArray %a",
  326. "OpTypeStruct %a",
  327. "OpTypeOpaque \"Foo\"",
  328. "OpTypePointer UniformConstant %a",
  329. "OpTypeFunction %a %b",
  330. "OpTypeEvent",
  331. "OpTypeDeviceEvent",
  332. "OpTypeReserveId",
  333. "OpTypeQueue",
  334. "OpTypePipe ReadOnly",
  335. // Skip OpTypeForwardPointer becasuse it doesn't even produce a result
  336. // ID.
  337. // At least one thing that isn't a type at all
  338. "OpNot %a %b"
  339. },
  340. }));
  341. // clang-format on
  342. // TODO(dneto): OpPhi
  343. // TODO(dneto): OpLoopMerge
  344. // TODO(dneto): OpLabel
  345. // TODO(dneto): OpBranch
  346. // TODO(dneto): OpSwitch
  347. // TODO(dneto): OpKill
  348. // TODO(dneto): OpReturn
  349. // TODO(dneto): OpReturnValue
  350. // TODO(dneto): OpUnreachable
  351. // TODO(dneto): OpLifetimeStart
  352. // TODO(dneto): OpLifetimeStop
  353. } // namespace
  354. } // namespace spvtools