text_to_binary.control_flow_test.cpp 17 KB

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