text_to_binary.constant_test.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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 "Group Instrucions" section of the
  15. // SPIR-V spec.
  16. #include <cstdint>
  17. #include <limits>
  18. #include <string>
  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 ::testing::Eq;
  29. // Test Sampler Addressing Mode enum values
  30. using SamplerAddressingModeTest = spvtest::TextToBinaryTestBase<
  31. ::testing::TestWithParam<EnumCase<spv::SamplerAddressingMode>>>;
  32. TEST_P(SamplerAddressingModeTest, AnySamplerAddressingMode) {
  33. const std::string input =
  34. "%result = OpConstantSampler %type " + GetParam().name() + " 0 Nearest";
  35. EXPECT_THAT(CompiledInstructions(input),
  36. Eq(MakeInstruction(spv::Op::OpConstantSampler,
  37. {1, 2, uint32_t(GetParam().value()), 0, 0})));
  38. }
  39. // clang-format off
  40. #define CASE(NAME) { spv::SamplerAddressingMode::NAME, #NAME }
  41. INSTANTIATE_TEST_SUITE_P(
  42. TextToBinarySamplerAddressingMode, SamplerAddressingModeTest,
  43. ::testing::ValuesIn(std::vector<EnumCase<spv::SamplerAddressingMode>>{
  44. CASE(None),
  45. CASE(ClampToEdge),
  46. CASE(Clamp),
  47. CASE(Repeat),
  48. CASE(RepeatMirrored),
  49. }));
  50. #undef CASE
  51. // clang-format on
  52. TEST_F(SamplerAddressingModeTest, WrongMode) {
  53. EXPECT_THAT(CompileFailure("%r = OpConstantSampler %t xxyyzz 0 Nearest"),
  54. Eq("Invalid sampler addressing mode 'xxyyzz'."));
  55. }
  56. // Test Sampler Filter Mode enum values
  57. using SamplerFilterModeTest = spvtest::TextToBinaryTestBase<
  58. ::testing::TestWithParam<EnumCase<spv::SamplerFilterMode>>>;
  59. TEST_P(SamplerFilterModeTest, AnySamplerFilterMode) {
  60. const std::string input =
  61. "%result = OpConstantSampler %type Clamp 0 " + GetParam().name();
  62. EXPECT_THAT(CompiledInstructions(input),
  63. Eq(MakeInstruction(spv::Op::OpConstantSampler,
  64. {1, 2, 2, 0, uint32_t(GetParam().value())})));
  65. }
  66. // clang-format off
  67. #define CASE(NAME) { spv::SamplerFilterMode::NAME, #NAME}
  68. INSTANTIATE_TEST_SUITE_P(
  69. TextToBinarySamplerFilterMode, SamplerFilterModeTest,
  70. ::testing::ValuesIn(std::vector<EnumCase<spv::SamplerFilterMode>>{
  71. CASE(Nearest),
  72. CASE(Linear),
  73. }));
  74. #undef CASE
  75. // clang-format on
  76. TEST_F(SamplerFilterModeTest, WrongMode) {
  77. EXPECT_THAT(CompileFailure("%r = OpConstantSampler %t Clamp 0 xxyyzz"),
  78. Eq("Invalid sampler filter mode 'xxyyzz'."));
  79. }
  80. struct ConstantTestCase {
  81. std::string constant_type;
  82. std::string constant_value;
  83. std::vector<uint32_t> expected_instructions;
  84. };
  85. using OpConstantValidTest =
  86. spvtest::TextToBinaryTestBase<::testing::TestWithParam<ConstantTestCase>>;
  87. TEST_P(OpConstantValidTest, ValidTypes) {
  88. const std::string input = "%1 = " + GetParam().constant_type +
  89. "\n"
  90. "%2 = OpConstant %1 " +
  91. GetParam().constant_value + "\n";
  92. std::vector<uint32_t> instructions;
  93. EXPECT_THAT(CompiledInstructions(input), Eq(GetParam().expected_instructions))
  94. << " type: " << GetParam().constant_type
  95. << " literal: " << GetParam().constant_value;
  96. }
  97. // clang-format off
  98. INSTANTIATE_TEST_SUITE_P(
  99. TextToBinaryOpConstantValid, OpConstantValidTest,
  100. ::testing::ValuesIn(std::vector<ConstantTestCase>{
  101. // Check 16 bits
  102. {"OpTypeInt 16 0", "0x1234",
  103. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 16, 0}),
  104. MakeInstruction(spv::Op::OpConstant, {1, 2, 0x1234})})},
  105. {"OpTypeInt 16 0", "0x8000",
  106. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 16, 0}),
  107. MakeInstruction(spv::Op::OpConstant, {1, 2, 0x8000})})},
  108. {"OpTypeInt 16 0", "0",
  109. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 16, 0}),
  110. MakeInstruction(spv::Op::OpConstant, {1, 2, 0})})},
  111. {"OpTypeInt 16 0", "65535",
  112. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 16, 0}),
  113. MakeInstruction(spv::Op::OpConstant, {1, 2, 65535})})},
  114. {"OpTypeInt 16 0", "0xffff",
  115. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 16, 0}),
  116. MakeInstruction(spv::Op::OpConstant, {1, 2, 65535})})},
  117. {"OpTypeInt 16 1", "0x8000", // Test sign extension.
  118. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 16, 1}),
  119. MakeInstruction(spv::Op::OpConstant, {1, 2, 0xffff8000})})},
  120. {"OpTypeInt 16 1", "-32",
  121. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 16, 1}),
  122. MakeInstruction(spv::Op::OpConstant, {1, 2, uint32_t(-32)})})},
  123. {"OpTypeInt 16 1", "0",
  124. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 16, 1}),
  125. MakeInstruction(spv::Op::OpConstant, {1, 2, 0})})},
  126. {"OpTypeInt 16 1", "-0",
  127. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 16, 1}),
  128. MakeInstruction(spv::Op::OpConstant, {1, 2, 0})})},
  129. {"OpTypeInt 16 1", "-0x0",
  130. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 16, 1}),
  131. MakeInstruction(spv::Op::OpConstant, {1, 2, 0})})},
  132. {"OpTypeInt 16 1", "-32768",
  133. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 16, 1}),
  134. MakeInstruction(spv::Op::OpConstant, {1, 2, uint32_t(-32768)})})},
  135. // Check 32 bits
  136. {"OpTypeInt 32 0", "42",
  137. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 32, 0}),
  138. MakeInstruction(spv::Op::OpConstant, {1, 2, 42})})},
  139. {"OpTypeInt 32 1", "-32",
  140. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 32, 1}),
  141. MakeInstruction(spv::Op::OpConstant, {1, 2, uint32_t(-32)})})},
  142. {"OpTypeInt 32 1", "0",
  143. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 32, 1}),
  144. MakeInstruction(spv::Op::OpConstant, {1, 2, 0})})},
  145. {"OpTypeInt 32 1", "-0",
  146. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 32, 1}),
  147. MakeInstruction(spv::Op::OpConstant, {1, 2, 0})})},
  148. {"OpTypeInt 32 1", "-0x0",
  149. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 32, 1}),
  150. MakeInstruction(spv::Op::OpConstant, {1, 2, 0})})},
  151. {"OpTypeInt 32 1", "-0x001",
  152. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 32, 1}),
  153. MakeInstruction(spv::Op::OpConstant, {1, 2, uint32_t(-1)})})},
  154. {"OpTypeInt 32 1", "2147483647",
  155. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 32, 1}),
  156. MakeInstruction(spv::Op::OpConstant, {1, 2, 0x7fffffffu})})},
  157. {"OpTypeInt 32 1", "-2147483648",
  158. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 32, 1}),
  159. MakeInstruction(spv::Op::OpConstant, {1, 2, 0x80000000u})})},
  160. {"OpTypeFloat 32", "1.0",
  161. Concatenate({MakeInstruction(spv::Op::OpTypeFloat, {1, 32}),
  162. MakeInstruction(spv::Op::OpConstant, {1, 2, 0x3f800000})})},
  163. {"OpTypeFloat 32", "10.0",
  164. Concatenate({MakeInstruction(spv::Op::OpTypeFloat, {1, 32}),
  165. MakeInstruction(spv::Op::OpConstant, {1, 2, 0x41200000})})},
  166. {"OpTypeFloat 32", "-0x1p+128", // -infinity
  167. Concatenate({MakeInstruction(spv::Op::OpTypeFloat, {1, 32}),
  168. MakeInstruction(spv::Op::OpConstant, {1, 2, 0xFF800000})})},
  169. {"OpTypeFloat 32", "0x1p+128", // +infinity
  170. Concatenate({MakeInstruction(spv::Op::OpTypeFloat, {1, 32}),
  171. MakeInstruction(spv::Op::OpConstant, {1, 2, 0x7F800000})})},
  172. {"OpTypeFloat 32", "-0x1.8p+128", // A -NaN
  173. Concatenate({MakeInstruction(spv::Op::OpTypeFloat, {1, 32}),
  174. MakeInstruction(spv::Op::OpConstant, {1, 2, 0xFFC00000})})},
  175. {"OpTypeFloat 32", "-0x1.0002p+128", // A +NaN
  176. Concatenate({MakeInstruction(spv::Op::OpTypeFloat, {1, 32}),
  177. MakeInstruction(spv::Op::OpConstant, {1, 2, 0xFF800100})})},
  178. // Check 48 bits
  179. {"OpTypeInt 48 0", "0x1234",
  180. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 48, 0}),
  181. MakeInstruction(spv::Op::OpConstant, {1, 2, 0x1234, 0})})},
  182. {"OpTypeInt 48 0", "0x800000000001",
  183. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 48, 0}),
  184. MakeInstruction(spv::Op::OpConstant, {1, 2, 1, 0x00008000})})},
  185. {"OpTypeInt 48 1", "0x800000000000", // Test sign extension.
  186. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 48, 1}),
  187. MakeInstruction(spv::Op::OpConstant, {1, 2, 0, 0xffff8000})})},
  188. {"OpTypeInt 48 1", "-32",
  189. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 48, 1}),
  190. MakeInstruction(spv::Op::OpConstant, {1, 2, uint32_t(-32), uint32_t(-1)})})},
  191. // Check 64 bits
  192. {"OpTypeInt 64 0", "0x1234",
  193. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 64, 0}),
  194. MakeInstruction(spv::Op::OpConstant, {1, 2, 0x1234, 0})})},
  195. {"OpTypeInt 64 0", "18446744073709551615",
  196. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 64, 0}),
  197. MakeInstruction(spv::Op::OpConstant, {1, 2, 0xffffffffu, 0xffffffffu})})},
  198. {"OpTypeInt 64 0", "0xffffffffffffffff",
  199. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 64, 0}),
  200. MakeInstruction(spv::Op::OpConstant, {1, 2, 0xffffffffu, 0xffffffffu})})},
  201. {"OpTypeInt 64 1", "0x1234",
  202. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 64, 1}),
  203. MakeInstruction(spv::Op::OpConstant, {1, 2, 0x1234, 0})})},
  204. {"OpTypeInt 64 1", "-42",
  205. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 64, 1}),
  206. MakeInstruction(spv::Op::OpConstant, {1, 2, uint32_t(-42), uint32_t(-1)})})},
  207. {"OpTypeInt 64 1", "-0x01",
  208. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 64, 1}),
  209. MakeInstruction(spv::Op::OpConstant, {1, 2, 0xffffffffu, 0xffffffffu})})},
  210. {"OpTypeInt 64 1", "9223372036854775807",
  211. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 64, 1}),
  212. MakeInstruction(spv::Op::OpConstant, {1, 2, 0xffffffffu, 0x7fffffffu})})},
  213. {"OpTypeInt 64 1", "0x7fffffff",
  214. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 64, 1}),
  215. MakeInstruction(spv::Op::OpConstant, {1, 2, 0x7fffffffu, 0})})},
  216. }));
  217. // clang-format on
  218. // A test case for checking OpConstant with invalid literals with a leading
  219. // minus.
  220. struct InvalidLeadingMinusCase {
  221. std::string type;
  222. std::string literal;
  223. };
  224. using OpConstantInvalidLeadingMinusTest = spvtest::TextToBinaryTestBase<
  225. ::testing::TestWithParam<InvalidLeadingMinusCase>>;
  226. TEST_P(OpConstantInvalidLeadingMinusTest, InvalidCase) {
  227. const std::string input = "%1 = " + GetParam().type +
  228. "\n"
  229. "%2 = OpConstant %1 " +
  230. GetParam().literal;
  231. EXPECT_THAT(CompileFailure(input),
  232. Eq("Cannot put a negative number in an unsigned literal"));
  233. }
  234. // clang-format off
  235. INSTANTIATE_TEST_SUITE_P(
  236. TextToBinaryOpConstantInvalidLeadingMinus, OpConstantInvalidLeadingMinusTest,
  237. ::testing::ValuesIn(std::vector<InvalidLeadingMinusCase>{
  238. {"OpTypeInt 16 0", "-0"},
  239. {"OpTypeInt 16 0", "-0x0"},
  240. {"OpTypeInt 16 0", "-1"},
  241. {"OpTypeInt 32 0", "-0"},
  242. {"OpTypeInt 32 0", "-0x0"},
  243. {"OpTypeInt 32 0", "-1"},
  244. {"OpTypeInt 64 0", "-0"},
  245. {"OpTypeInt 64 0", "-0x0"},
  246. {"OpTypeInt 64 0", "-1"},
  247. }));
  248. // clang-format on
  249. // A test case for invalid floating point literals.
  250. struct InvalidFloatConstantCase {
  251. uint32_t width;
  252. std::string literal;
  253. };
  254. using OpConstantInvalidFloatConstant = spvtest::TextToBinaryTestBase<
  255. ::testing::TestWithParam<InvalidFloatConstantCase>>;
  256. TEST_P(OpConstantInvalidFloatConstant, Samples) {
  257. // Check both kinds of instructions that take literal floats.
  258. for (const auto& instruction : {"OpConstant", "OpSpecConstant"}) {
  259. std::stringstream input;
  260. input << "%1 = OpTypeFloat " << GetParam().width << "\n"
  261. << "%2 = " << instruction << " %1 " << GetParam().literal;
  262. std::stringstream expected_error;
  263. expected_error << "Invalid " << GetParam().width
  264. << "-bit float literal: " << GetParam().literal;
  265. EXPECT_THAT(CompileFailure(input.str()), Eq(expected_error.str()));
  266. }
  267. }
  268. // clang-format off
  269. INSTANTIATE_TEST_SUITE_P(
  270. TextToBinaryInvalidFloatConstant, OpConstantInvalidFloatConstant,
  271. ::testing::ValuesIn(std::vector<InvalidFloatConstantCase>{
  272. {16, "abc"},
  273. {16, "--1"},
  274. {16, "-+1"},
  275. {16, "+-1"},
  276. {16, "++1"},
  277. {16, "1e30"}, // Overflow is an error for 16-bit floats.
  278. {16, "-1e30"},
  279. {16, "1e40"},
  280. {16, "-1e40"},
  281. {16, "1e400"},
  282. {16, "-1e400"},
  283. {32, "abc"},
  284. {32, "--1"},
  285. {32, "-+1"},
  286. {32, "+-1"},
  287. {32, "++1"},
  288. {32, "1e40"}, // Overflow is an error for 32-bit floats.
  289. {32, "-1e40"},
  290. {32, "1e400"},
  291. {32, "-1e400"},
  292. {64, "abc"},
  293. {64, "--1"},
  294. {64, "-+1"},
  295. {64, "+-1"},
  296. {64, "++1"},
  297. {32, "1e400"}, // Overflow is an error for 64-bit floats.
  298. {32, "-1e400"},
  299. }));
  300. // clang-format on
  301. using OpConstantInvalidTypeTest =
  302. spvtest::TextToBinaryTestBase<::testing::TestWithParam<std::string>>;
  303. TEST_P(OpConstantInvalidTypeTest, InvalidTypes) {
  304. const std::string input = "%1 = " + GetParam() +
  305. "\n"
  306. "%2 = OpConstant %1 0\n";
  307. EXPECT_THAT(
  308. CompileFailure(input),
  309. Eq("Type for Constant must be a scalar floating point or integer type"));
  310. }
  311. // clang-format off
  312. INSTANTIATE_TEST_SUITE_P(
  313. TextToBinaryOpConstantInvalidValidType, OpConstantInvalidTypeTest,
  314. ::testing::ValuesIn(std::vector<std::string>{
  315. {"OpTypeVoid",
  316. "OpTypeBool",
  317. "OpTypeVector %a 32",
  318. "OpTypeMatrix %a 32",
  319. "OpTypeImage %a 1D 0 0 0 0 Unknown",
  320. "OpTypeSampler",
  321. "OpTypeSampledImage %a",
  322. "OpTypeArray %a %b",
  323. "OpTypeRuntimeArray %a",
  324. "OpTypeStruct %a",
  325. "OpTypeOpaque \"Foo\"",
  326. "OpTypePointer UniformConstant %a",
  327. "OpTypeFunction %a %b",
  328. "OpTypeEvent",
  329. "OpTypeDeviceEvent",
  330. "OpTypeReserveId",
  331. "OpTypeQueue",
  332. "OpTypePipe ReadOnly",
  333. // Skip OpTypeForwardPointer doesn't even produce a result ID.
  334. // The assembler errors out if we try to check it in this scenario.
  335. // Try at least one thing that isn't a type at all
  336. "OpNot %a %b"
  337. },
  338. }));
  339. // clang-format on
  340. using OpSpecConstantValidTest =
  341. spvtest::TextToBinaryTestBase<::testing::TestWithParam<ConstantTestCase>>;
  342. TEST_P(OpSpecConstantValidTest, ValidTypes) {
  343. const std::string input = "%1 = " + GetParam().constant_type +
  344. "\n"
  345. "%2 = OpSpecConstant %1 " +
  346. GetParam().constant_value + "\n";
  347. std::vector<uint32_t> instructions;
  348. EXPECT_THAT(CompiledInstructions(input),
  349. Eq(GetParam().expected_instructions));
  350. }
  351. // clang-format off
  352. INSTANTIATE_TEST_SUITE_P(
  353. TextToBinaryOpSpecConstantValid, OpSpecConstantValidTest,
  354. ::testing::ValuesIn(std::vector<ConstantTestCase>{
  355. // Check 16 bits
  356. {"OpTypeInt 16 0", "0x1234",
  357. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 16, 0}),
  358. MakeInstruction(spv::Op::OpSpecConstant, {1, 2, 0x1234})})},
  359. {"OpTypeInt 16 0", "0x8000",
  360. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 16, 0}),
  361. MakeInstruction(spv::Op::OpSpecConstant, {1, 2, 0x8000})})},
  362. {"OpTypeInt 16 1", "0x8000", // Test sign extension.
  363. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 16, 1}),
  364. MakeInstruction(spv::Op::OpSpecConstant, {1, 2, 0xffff8000})})},
  365. {"OpTypeInt 16 1", "-32",
  366. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 16, 1}),
  367. MakeInstruction(spv::Op::OpSpecConstant, {1, 2, uint32_t(-32)})})},
  368. // Check 32 bits
  369. {"OpTypeInt 32 0", "42",
  370. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 32, 0}),
  371. MakeInstruction(spv::Op::OpSpecConstant, {1, 2, 42})})},
  372. {"OpTypeInt 32 1", "-32",
  373. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 32, 1}),
  374. MakeInstruction(spv::Op::OpSpecConstant, {1, 2, uint32_t(-32)})})},
  375. {"OpTypeFloat 32", "1.0",
  376. Concatenate({MakeInstruction(spv::Op::OpTypeFloat, {1, 32}),
  377. MakeInstruction(spv::Op::OpSpecConstant, {1, 2, 0x3f800000})})},
  378. {"OpTypeFloat 32", "10.0",
  379. Concatenate({MakeInstruction(spv::Op::OpTypeFloat, {1, 32}),
  380. MakeInstruction(spv::Op::OpSpecConstant, {1, 2, 0x41200000})})},
  381. // Check 48 bits
  382. {"OpTypeInt 48 0", "0x1234",
  383. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 48, 0}),
  384. MakeInstruction(spv::Op::OpSpecConstant, {1, 2, 0x1234, 0})})},
  385. {"OpTypeInt 48 0", "0x800000000001",
  386. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 48, 0}),
  387. MakeInstruction(spv::Op::OpSpecConstant, {1, 2, 1, 0x00008000})})},
  388. {"OpTypeInt 48 1", "0x800000000000", // Test sign extension.
  389. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 48, 1}),
  390. MakeInstruction(spv::Op::OpSpecConstant, {1, 2, 0, 0xffff8000})})},
  391. {"OpTypeInt 48 1", "-32",
  392. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 48, 1}),
  393. MakeInstruction(spv::Op::OpSpecConstant, {1, 2, uint32_t(-32), uint32_t(-1)})})},
  394. // Check 64 bits
  395. {"OpTypeInt 64 0", "0x1234",
  396. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 64, 0}),
  397. MakeInstruction(spv::Op::OpSpecConstant, {1, 2, 0x1234, 0})})},
  398. {"OpTypeInt 64 1", "0x1234",
  399. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 64, 1}),
  400. MakeInstruction(spv::Op::OpSpecConstant, {1, 2, 0x1234, 0})})},
  401. {"OpTypeInt 64 1", "-42",
  402. Concatenate({MakeInstruction(spv::Op::OpTypeInt, {1, 64, 1}),
  403. MakeInstruction(spv::Op::OpSpecConstant, {1, 2, uint32_t(-42), uint32_t(-1)})})},
  404. }));
  405. // clang-format on
  406. using OpSpecConstantInvalidTypeTest =
  407. spvtest::TextToBinaryTestBase<::testing::TestWithParam<std::string>>;
  408. TEST_P(OpSpecConstantInvalidTypeTest, InvalidTypes) {
  409. const std::string input = "%1 = " + GetParam() +
  410. "\n"
  411. "%2 = OpSpecConstant %1 0\n";
  412. EXPECT_THAT(CompileFailure(input),
  413. Eq("Type for SpecConstant must be a scalar floating point or "
  414. "integer type"));
  415. }
  416. // clang-format off
  417. INSTANTIATE_TEST_SUITE_P(
  418. TextToBinaryOpSpecConstantInvalidValidType, OpSpecConstantInvalidTypeTest,
  419. ::testing::ValuesIn(std::vector<std::string>{
  420. {"OpTypeVoid",
  421. "OpTypeBool",
  422. "OpTypeVector %a 32",
  423. "OpTypeMatrix %a 32",
  424. "OpTypeImage %a 1D 0 0 0 0 Unknown",
  425. "OpTypeSampler",
  426. "OpTypeSampledImage %a",
  427. "OpTypeArray %a %b",
  428. "OpTypeRuntimeArray %a",
  429. "OpTypeStruct %a",
  430. "OpTypeOpaque \"Foo\"",
  431. "OpTypePointer UniformConstant %a",
  432. "OpTypeFunction %a %b",
  433. "OpTypeEvent",
  434. "OpTypeDeviceEvent",
  435. "OpTypeReserveId",
  436. "OpTypeQueue",
  437. "OpTypePipe ReadOnly",
  438. // Skip testing OpTypeForwardPointer because it doesn't even produce a result ID.
  439. // Try at least one thing that isn't a type at all
  440. "OpNot %a %b"
  441. },
  442. }));
  443. // clang-format on
  444. const int64_t kMaxUnsigned48Bit = (int64_t(1) << 48) - 1;
  445. const int64_t kMaxSigned48Bit = (int64_t(1) << 47) - 1;
  446. const int64_t kMinSigned48Bit = -kMaxSigned48Bit - 1;
  447. using ConstantRoundTripTest = RoundTripTest;
  448. TEST_P(ConstantRoundTripTest, DisassemblyEqualsAssemblyInput) {
  449. const std::string assembly = GetParam();
  450. EXPECT_THAT(EncodeAndDecodeSuccessfully(assembly), Eq(assembly)) << assembly;
  451. }
  452. INSTANTIATE_TEST_SUITE_P(
  453. OpConstantRoundTrip, ConstantRoundTripTest,
  454. ::testing::ValuesIn(std::vector<std::string>{
  455. // 16 bit
  456. "%1 = OpTypeInt 16 0\n%2 = OpConstant %1 0\n",
  457. "%1 = OpTypeInt 16 0\n%2 = OpConstant %1 65535\n",
  458. "%1 = OpTypeInt 16 1\n%2 = OpConstant %1 -32768\n",
  459. "%1 = OpTypeInt 16 1\n%2 = OpConstant %1 32767\n",
  460. "%1 = OpTypeInt 32 0\n%2 = OpConstant %1 0\n",
  461. // 32 bit
  462. std::string("%1 = OpTypeInt 32 0\n%2 = OpConstant %1 0\n"),
  463. std::string("%1 = OpTypeInt 32 0\n%2 = OpConstant %1 ") +
  464. std::to_string(std::numeric_limits<uint32_t>::max()) + "\n",
  465. std::string("%1 = OpTypeInt 32 1\n%2 = OpConstant %1 ") +
  466. std::to_string(std::numeric_limits<int32_t>::max()) + "\n",
  467. std::string("%1 = OpTypeInt 32 1\n%2 = OpConstant %1 ") +
  468. std::to_string(std::numeric_limits<int32_t>::min()) + "\n",
  469. // 48 bit
  470. std::string("%1 = OpTypeInt 48 0\n%2 = OpConstant %1 0\n"),
  471. std::string("%1 = OpTypeInt 48 0\n%2 = OpConstant %1 ") +
  472. std::to_string(kMaxUnsigned48Bit) + "\n",
  473. std::string("%1 = OpTypeInt 48 1\n%2 = OpConstant %1 ") +
  474. std::to_string(kMaxSigned48Bit) + "\n",
  475. std::string("%1 = OpTypeInt 48 1\n%2 = OpConstant %1 ") +
  476. std::to_string(kMinSigned48Bit) + "\n",
  477. // 64 bit
  478. std::string("%1 = OpTypeInt 64 0\n%2 = OpConstant %1 0\n"),
  479. std::string("%1 = OpTypeInt 64 0\n%2 = OpConstant %1 ") +
  480. std::to_string(std::numeric_limits<uint64_t>::max()) + "\n",
  481. std::string("%1 = OpTypeInt 64 1\n%2 = OpConstant %1 ") +
  482. std::to_string(std::numeric_limits<int64_t>::max()) + "\n",
  483. std::string("%1 = OpTypeInt 64 1\n%2 = OpConstant %1 ") +
  484. std::to_string(std::numeric_limits<int64_t>::min()) + "\n",
  485. // 32-bit float
  486. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 0\n",
  487. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 13.5\n",
  488. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -12.5\n",
  489. // 64-bit float
  490. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 0\n",
  491. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 1.79767e+308\n",
  492. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -1.79767e+308\n",
  493. }));
  494. INSTANTIATE_TEST_SUITE_P(
  495. OpConstantHalfRoundTrip, ConstantRoundTripTest,
  496. ::testing::ValuesIn(std::vector<std::string>{
  497. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x0p+0\n",
  498. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x0p+0\n",
  499. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1p+0\n",
  500. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.1p+0\n",
  501. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.01p-1\n",
  502. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.8p+1\n",
  503. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.ffcp+1\n",
  504. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x1p+0\n",
  505. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x1.1p+0\n",
  506. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x1.01p-1\n",
  507. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x1.8p+1\n",
  508. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x1.ffcp+1\n",
  509. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1p-16\n", // some denorms
  510. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1p-24\n",
  511. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x1p-24\n",
  512. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1p+16\n", // +inf
  513. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x1p+16\n", // -inf
  514. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x1.01p+16\n", // -inf
  515. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.01p+16\n", // nan
  516. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.11p+16\n", // nan
  517. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.ffp+16\n", // nan
  518. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.ffcp+16\n", // nan
  519. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.004p+16\n", // nan
  520. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x1.01p+16\n", // -nan
  521. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x1.11p+16\n", // -nan
  522. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x1.ffp+16\n", // -nan
  523. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x1.ffcp+16\n", // -nan
  524. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x1.004p+16\n", // -nan
  525. }));
  526. // clang-format off
  527. // (Clang-format really wants to break up these strings across lines.
  528. INSTANTIATE_TEST_SUITE_P(
  529. OpConstantRoundTripNonFinite, ConstantRoundTripTest,
  530. ::testing::ValuesIn(std::vector<std::string>{
  531. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0x1p+128\n", // -inf
  532. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 0x1p+128\n", // inf
  533. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0x1.8p+128\n", // -nan
  534. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0x1.0002p+128\n", // -nan
  535. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0x1.0018p+128\n", // -nan
  536. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0x1.01ep+128\n", // -nan
  537. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0x1.fffffep+128\n", // -nan
  538. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 0x1.8p+128\n", // +nan
  539. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 0x1.0002p+128\n", // +nan
  540. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 0x1.0018p+128\n", // +nan
  541. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 0x1.01ep+128\n", // +nan
  542. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 0x1.fffffep+128\n", // +nan
  543. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1p+1024\n", // -inf
  544. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 0x1p+1024\n", // +inf
  545. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1.8p+1024\n", // -nan
  546. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1.0fp+1024\n", // -nan
  547. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1.0000000000001p+1024\n", // -nan
  548. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1.00003p+1024\n", // -nan
  549. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1.fffffffffffffp+1024\n", // -nan
  550. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 0x1.8p+1024\n", // +nan
  551. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 0x1.0fp+1024\n", // +nan
  552. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 0x1.0000000000001p+1024\n", // -nan
  553. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 0x1.00003p+1024\n", // -nan
  554. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 0x1.fffffffffffffp+1024\n", // -nan
  555. }));
  556. // clang-format on
  557. INSTANTIATE_TEST_SUITE_P(
  558. OpSpecConstantRoundTrip, ConstantRoundTripTest,
  559. ::testing::ValuesIn(std::vector<std::string>{
  560. // 16 bit
  561. "%1 = OpTypeInt 16 0\n%2 = OpSpecConstant %1 0\n",
  562. "%1 = OpTypeInt 16 0\n%2 = OpSpecConstant %1 65535\n",
  563. "%1 = OpTypeInt 16 1\n%2 = OpSpecConstant %1 -32768\n",
  564. "%1 = OpTypeInt 16 1\n%2 = OpSpecConstant %1 32767\n",
  565. "%1 = OpTypeInt 32 0\n%2 = OpSpecConstant %1 0\n",
  566. // 32 bit
  567. std::string("%1 = OpTypeInt 32 0\n%2 = OpSpecConstant %1 0\n"),
  568. std::string("%1 = OpTypeInt 32 0\n%2 = OpSpecConstant %1 ") +
  569. std::to_string(std::numeric_limits<uint32_t>::max()) + "\n",
  570. std::string("%1 = OpTypeInt 32 1\n%2 = OpSpecConstant %1 ") +
  571. std::to_string(std::numeric_limits<int32_t>::max()) + "\n",
  572. std::string("%1 = OpTypeInt 32 1\n%2 = OpSpecConstant %1 ") +
  573. std::to_string(std::numeric_limits<int32_t>::min()) + "\n",
  574. // 48 bit
  575. std::string("%1 = OpTypeInt 48 0\n%2 = OpSpecConstant %1 0\n"),
  576. std::string("%1 = OpTypeInt 48 0\n%2 = OpSpecConstant %1 ") +
  577. std::to_string(kMaxUnsigned48Bit) + "\n",
  578. std::string("%1 = OpTypeInt 48 1\n%2 = OpSpecConstant %1 ") +
  579. std::to_string(kMaxSigned48Bit) + "\n",
  580. std::string("%1 = OpTypeInt 48 1\n%2 = OpSpecConstant %1 ") +
  581. std::to_string(kMinSigned48Bit) + "\n",
  582. // 64 bit
  583. std::string("%1 = OpTypeInt 64 0\n%2 = OpSpecConstant %1 0\n"),
  584. std::string("%1 = OpTypeInt 64 0\n%2 = OpSpecConstant %1 ") +
  585. std::to_string(std::numeric_limits<uint64_t>::max()) + "\n",
  586. std::string("%1 = OpTypeInt 64 1\n%2 = OpSpecConstant %1 ") +
  587. std::to_string(std::numeric_limits<int64_t>::max()) + "\n",
  588. std::string("%1 = OpTypeInt 64 1\n%2 = OpSpecConstant %1 ") +
  589. std::to_string(std::numeric_limits<int64_t>::min()) + "\n",
  590. // 32-bit float
  591. "%1 = OpTypeFloat 32\n%2 = OpSpecConstant %1 0\n",
  592. "%1 = OpTypeFloat 32\n%2 = OpSpecConstant %1 13.5\n",
  593. "%1 = OpTypeFloat 32\n%2 = OpSpecConstant %1 -12.5\n",
  594. // 64-bit float
  595. "%1 = OpTypeFloat 64\n%2 = OpSpecConstant %1 0\n",
  596. "%1 = OpTypeFloat 64\n%2 = OpSpecConstant %1 1.79767e+308\n",
  597. "%1 = OpTypeFloat 64\n%2 = OpSpecConstant %1 -1.79767e+308\n",
  598. }));
  599. // Test OpSpecConstantOp
  600. using OpSpecConstantOpTestWithIds =
  601. spvtest::TextToBinaryTestBase<::testing::TestWithParam<EnumCase<spv::Op>>>;
  602. // The operands to the OpSpecConstantOp opcode are all Ids.
  603. TEST_P(OpSpecConstantOpTestWithIds, Assembly) {
  604. std::stringstream input;
  605. input << "%2 = OpSpecConstantOp %1 " << GetParam().name();
  606. for (auto id : GetParam().operands()) input << " %" << id;
  607. input << "\n";
  608. EXPECT_THAT(CompiledInstructions(input.str()),
  609. Eq(MakeInstruction(spv::Op::OpSpecConstantOp,
  610. {1, 2, uint32_t(GetParam().value())},
  611. GetParam().operands())));
  612. // Check the disassembler as well.
  613. EXPECT_THAT(EncodeAndDecodeSuccessfully(input.str()), input.str());
  614. }
  615. // clang-format off
  616. #define CASE1(NAME) { spv::Op::Op##NAME, #NAME, {3} }
  617. #define CASE2(NAME) { spv::Op::Op##NAME, #NAME, {3, 4} }
  618. #define CASE3(NAME) { spv::Op::Op##NAME, #NAME, {3, 4, 5} }
  619. #define CASE4(NAME) { spv::Op::Op##NAME, #NAME, {3, 4, 5, 6} }
  620. #define CASE5(NAME) { spv::Op::Op##NAME, #NAME, {3, 4, 5, 6, 7} }
  621. #define CASE6(NAME) { spv::Op::Op##NAME, #NAME, {3, 4, 5, 6, 7, 8} }
  622. INSTANTIATE_TEST_SUITE_P(
  623. TextToBinaryOpSpecConstantOp, OpSpecConstantOpTestWithIds,
  624. ::testing::ValuesIn(std::vector<EnumCase<spv::Op>>{
  625. // Conversion
  626. CASE1(SConvert),
  627. CASE1(FConvert),
  628. CASE1(ConvertFToS),
  629. CASE1(ConvertSToF),
  630. CASE1(ConvertFToU),
  631. CASE1(ConvertUToF),
  632. CASE1(UConvert),
  633. CASE1(ConvertPtrToU),
  634. CASE1(ConvertUToPtr),
  635. CASE1(GenericCastToPtr),
  636. CASE1(PtrCastToGeneric),
  637. CASE1(Bitcast),
  638. CASE1(QuantizeToF16),
  639. // Arithmetic
  640. CASE1(SNegate),
  641. CASE1(Not),
  642. CASE2(IAdd),
  643. CASE2(ISub),
  644. CASE2(IMul),
  645. CASE2(UDiv),
  646. CASE2(SDiv),
  647. CASE2(UMod),
  648. CASE2(SRem),
  649. CASE2(SMod),
  650. CASE2(ShiftRightLogical),
  651. CASE2(ShiftRightArithmetic),
  652. CASE2(ShiftLeftLogical),
  653. CASE2(BitwiseOr),
  654. CASE2(BitwiseAnd),
  655. CASE2(BitwiseXor),
  656. CASE1(FNegate),
  657. CASE2(FAdd),
  658. CASE2(FSub),
  659. CASE2(FMul),
  660. CASE2(FDiv),
  661. CASE2(FRem),
  662. CASE2(FMod),
  663. // Composite operations use literal numbers. So they're in another test.
  664. // Logical
  665. CASE2(LogicalOr),
  666. CASE2(LogicalAnd),
  667. CASE1(LogicalNot),
  668. CASE2(LogicalEqual),
  669. CASE2(LogicalNotEqual),
  670. CASE3(Select),
  671. // Comparison
  672. CASE2(IEqual),
  673. CASE2(INotEqual), // Allowed in 1.0 Rev 7
  674. CASE2(ULessThan),
  675. CASE2(SLessThan),
  676. CASE2(UGreaterThan),
  677. CASE2(SGreaterThan),
  678. CASE2(ULessThanEqual),
  679. CASE2(SLessThanEqual),
  680. CASE2(UGreaterThanEqual),
  681. CASE2(SGreaterThanEqual),
  682. // Memory
  683. // For AccessChain, there is a base Id, then a sequence of index Ids.
  684. // Having no index Ids is a corner case.
  685. CASE1(AccessChain),
  686. CASE2(AccessChain),
  687. CASE6(AccessChain),
  688. CASE1(InBoundsAccessChain),
  689. CASE2(InBoundsAccessChain),
  690. CASE6(InBoundsAccessChain),
  691. // PtrAccessChain also has an element Id.
  692. CASE2(PtrAccessChain),
  693. CASE3(PtrAccessChain),
  694. CASE6(PtrAccessChain),
  695. CASE2(InBoundsPtrAccessChain),
  696. CASE3(InBoundsPtrAccessChain),
  697. CASE6(InBoundsPtrAccessChain),
  698. }));
  699. #undef CASE1
  700. #undef CASE2
  701. #undef CASE3
  702. #undef CASE4
  703. #undef CASE5
  704. #undef CASE6
  705. // clang-format on
  706. using OpSpecConstantOpTestWithTwoIdsThenLiteralNumbers =
  707. spvtest::TextToBinaryTestBase<::testing::TestWithParam<EnumCase<spv::Op>>>;
  708. // The operands to the OpSpecConstantOp opcode are two Ids followed by a
  709. // sequence of literal numbers.
  710. TEST_P(OpSpecConstantOpTestWithTwoIdsThenLiteralNumbers, Assembly) {
  711. std::stringstream input;
  712. input << "%2 = OpSpecConstantOp %1 " << GetParam().name() << " %3 %4";
  713. for (auto number : GetParam().operands()) input << " " << number;
  714. input << "\n";
  715. EXPECT_THAT(CompiledInstructions(input.str()),
  716. Eq(MakeInstruction(spv::Op::OpSpecConstantOp,
  717. {1, 2, uint32_t(GetParam().value()), 3, 4},
  718. GetParam().operands())));
  719. // Check the disassembler as well.
  720. EXPECT_THAT(EncodeAndDecodeSuccessfully(input.str()), input.str());
  721. }
  722. #define CASE(NAME) spv::Op::Op##NAME, #NAME
  723. INSTANTIATE_TEST_SUITE_P(
  724. TextToBinaryOpSpecConstantOp,
  725. OpSpecConstantOpTestWithTwoIdsThenLiteralNumbers,
  726. ::testing::ValuesIn(std::vector<EnumCase<spv::Op>>{
  727. // For VectorShuffle, there are two vector operands, and at least
  728. // two selector Ids. OpenCL can have up to 16-element vectors.
  729. {CASE(VectorShuffle), {0, 0}},
  730. {CASE(VectorShuffle), {4, 3, 2, 1}},
  731. {CASE(VectorShuffle), {0, 2, 4, 6, 1, 3, 5, 7}},
  732. {CASE(VectorShuffle),
  733. {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}},
  734. // For CompositeInsert, there is an object to insert, the target
  735. // composite, and then literal indices.
  736. {CASE(CompositeInsert), {0}},
  737. {CASE(CompositeInsert), {4, 3, 99, 1}},
  738. }));
  739. using OpSpecConstantOpTestWithOneIdThenLiteralNumbers =
  740. spvtest::TextToBinaryTestBase<::testing::TestWithParam<EnumCase<spv::Op>>>;
  741. // The operands to the OpSpecConstantOp opcode are one Id followed by a
  742. // sequence of literal numbers.
  743. TEST_P(OpSpecConstantOpTestWithOneIdThenLiteralNumbers, Assembly) {
  744. std::stringstream input;
  745. input << "%2 = OpSpecConstantOp %1 " << GetParam().name() << " %3";
  746. for (auto number : GetParam().operands()) input << " " << number;
  747. input << "\n";
  748. EXPECT_THAT(CompiledInstructions(input.str()),
  749. Eq(MakeInstruction(spv::Op::OpSpecConstantOp,
  750. {1, 2, uint32_t(GetParam().value()), 3},
  751. GetParam().operands())));
  752. // Check the disassembler as well.
  753. EXPECT_THAT(EncodeAndDecodeSuccessfully(input.str()), input.str());
  754. }
  755. #define CASE(NAME) spv::Op::Op##NAME, #NAME
  756. INSTANTIATE_TEST_SUITE_P(
  757. TextToBinaryOpSpecConstantOp,
  758. OpSpecConstantOpTestWithOneIdThenLiteralNumbers,
  759. ::testing::ValuesIn(std::vector<EnumCase<spv::Op>>{
  760. // For CompositeExtract, the universal limit permits up to 255 literal
  761. // indices. Let's only test a few.
  762. {CASE(CompositeExtract), {0}},
  763. {CASE(CompositeExtract), {0, 99, 42, 16, 17, 12, 19}},
  764. }));
  765. // TODO(dneto): OpConstantTrue
  766. // TODO(dneto): OpConstantFalse
  767. // TODO(dneto): OpConstantComposite
  768. // TODO(dneto): OpConstantSampler: other variations Param is 0 or 1
  769. // TODO(dneto): OpConstantNull
  770. // TODO(dneto): OpSpecConstantTrue
  771. // TODO(dneto): OpSpecConstantFalse
  772. // TODO(dneto): OpSpecConstantComposite
  773. // TODO(dneto): Negative tests for OpSpecConstantOp
  774. } // namespace
  775. } // namespace spvtools