text_to_binary.constant_test.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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<SpvSamplerAddressingMode>>>;
  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(SpvOpConstantSampler,
  37. {1, 2, GetParam().value(), 0, 0})));
  38. }
  39. // clang-format off
  40. #define CASE(NAME) { SpvSamplerAddressingMode##NAME, #NAME }
  41. INSTANTIATE_TEST_SUITE_P(
  42. TextToBinarySamplerAddressingMode, SamplerAddressingModeTest,
  43. ::testing::ValuesIn(std::vector<EnumCase<SpvSamplerAddressingMode>>{
  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<SpvSamplerFilterMode>>>;
  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(SpvOpConstantSampler,
  64. {1, 2, 2, 0, GetParam().value()})));
  65. }
  66. // clang-format off
  67. #define CASE(NAME) { SpvSamplerFilterMode##NAME, #NAME}
  68. INSTANTIATE_TEST_SUITE_P(
  69. TextToBinarySamplerFilterMode, SamplerFilterModeTest,
  70. ::testing::ValuesIn(std::vector<EnumCase<SpvSamplerFilterMode>>{
  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(SpvOpTypeInt, {1, 16, 0}),
  104. MakeInstruction(SpvOpConstant, {1, 2, 0x1234})})},
  105. {"OpTypeInt 16 0", "0x8000",
  106. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 16, 0}),
  107. MakeInstruction(SpvOpConstant, {1, 2, 0x8000})})},
  108. {"OpTypeInt 16 0", "0",
  109. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 16, 0}),
  110. MakeInstruction(SpvOpConstant, {1, 2, 0})})},
  111. {"OpTypeInt 16 0", "65535",
  112. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 16, 0}),
  113. MakeInstruction(SpvOpConstant, {1, 2, 65535})})},
  114. {"OpTypeInt 16 0", "0xffff",
  115. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 16, 0}),
  116. MakeInstruction(SpvOpConstant, {1, 2, 65535})})},
  117. {"OpTypeInt 16 1", "0x8000", // Test sign extension.
  118. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 16, 1}),
  119. MakeInstruction(SpvOpConstant, {1, 2, 0xffff8000})})},
  120. {"OpTypeInt 16 1", "-32",
  121. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 16, 1}),
  122. MakeInstruction(SpvOpConstant, {1, 2, uint32_t(-32)})})},
  123. {"OpTypeInt 16 1", "0",
  124. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 16, 1}),
  125. MakeInstruction(SpvOpConstant, {1, 2, 0})})},
  126. {"OpTypeInt 16 1", "-0",
  127. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 16, 1}),
  128. MakeInstruction(SpvOpConstant, {1, 2, 0})})},
  129. {"OpTypeInt 16 1", "-0x0",
  130. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 16, 1}),
  131. MakeInstruction(SpvOpConstant, {1, 2, 0})})},
  132. {"OpTypeInt 16 1", "-32768",
  133. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 16, 1}),
  134. MakeInstruction(SpvOpConstant, {1, 2, uint32_t(-32768)})})},
  135. // Check 32 bits
  136. {"OpTypeInt 32 0", "42",
  137. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 32, 0}),
  138. MakeInstruction(SpvOpConstant, {1, 2, 42})})},
  139. {"OpTypeInt 32 1", "-32",
  140. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 32, 1}),
  141. MakeInstruction(SpvOpConstant, {1, 2, uint32_t(-32)})})},
  142. {"OpTypeInt 32 1", "0",
  143. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 32, 1}),
  144. MakeInstruction(SpvOpConstant, {1, 2, 0})})},
  145. {"OpTypeInt 32 1", "-0",
  146. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 32, 1}),
  147. MakeInstruction(SpvOpConstant, {1, 2, 0})})},
  148. {"OpTypeInt 32 1", "-0x0",
  149. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 32, 1}),
  150. MakeInstruction(SpvOpConstant, {1, 2, 0})})},
  151. {"OpTypeInt 32 1", "-0x001",
  152. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 32, 1}),
  153. MakeInstruction(SpvOpConstant, {1, 2, uint32_t(-1)})})},
  154. {"OpTypeInt 32 1", "2147483647",
  155. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 32, 1}),
  156. MakeInstruction(SpvOpConstant, {1, 2, 0x7fffffffu})})},
  157. {"OpTypeInt 32 1", "-2147483648",
  158. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 32, 1}),
  159. MakeInstruction(SpvOpConstant, {1, 2, 0x80000000u})})},
  160. {"OpTypeFloat 32", "1.0",
  161. Concatenate({MakeInstruction(SpvOpTypeFloat, {1, 32}),
  162. MakeInstruction(SpvOpConstant, {1, 2, 0x3f800000})})},
  163. {"OpTypeFloat 32", "10.0",
  164. Concatenate({MakeInstruction(SpvOpTypeFloat, {1, 32}),
  165. MakeInstruction(SpvOpConstant, {1, 2, 0x41200000})})},
  166. {"OpTypeFloat 32", "-0x1p+128", // -infinity
  167. Concatenate({MakeInstruction(SpvOpTypeFloat, {1, 32}),
  168. MakeInstruction(SpvOpConstant, {1, 2, 0xFF800000})})},
  169. {"OpTypeFloat 32", "0x1p+128", // +infinity
  170. Concatenate({MakeInstruction(SpvOpTypeFloat, {1, 32}),
  171. MakeInstruction(SpvOpConstant, {1, 2, 0x7F800000})})},
  172. {"OpTypeFloat 32", "-0x1.8p+128", // A -NaN
  173. Concatenate({MakeInstruction(SpvOpTypeFloat, {1, 32}),
  174. MakeInstruction(SpvOpConstant, {1, 2, 0xFFC00000})})},
  175. {"OpTypeFloat 32", "-0x1.0002p+128", // A +NaN
  176. Concatenate({MakeInstruction(SpvOpTypeFloat, {1, 32}),
  177. MakeInstruction(SpvOpConstant, {1, 2, 0xFF800100})})},
  178. // Check 48 bits
  179. {"OpTypeInt 48 0", "0x1234",
  180. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 48, 0}),
  181. MakeInstruction(SpvOpConstant, {1, 2, 0x1234, 0})})},
  182. {"OpTypeInt 48 0", "0x800000000001",
  183. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 48, 0}),
  184. MakeInstruction(SpvOpConstant, {1, 2, 1, 0x00008000})})},
  185. {"OpTypeInt 48 1", "0x800000000000", // Test sign extension.
  186. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 48, 1}),
  187. MakeInstruction(SpvOpConstant, {1, 2, 0, 0xffff8000})})},
  188. {"OpTypeInt 48 1", "-32",
  189. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 48, 1}),
  190. MakeInstruction(SpvOpConstant, {1, 2, uint32_t(-32), uint32_t(-1)})})},
  191. // Check 64 bits
  192. {"OpTypeInt 64 0", "0x1234",
  193. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 64, 0}),
  194. MakeInstruction(SpvOpConstant, {1, 2, 0x1234, 0})})},
  195. {"OpTypeInt 64 0", "18446744073709551615",
  196. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 64, 0}),
  197. MakeInstruction(SpvOpConstant, {1, 2, 0xffffffffu, 0xffffffffu})})},
  198. {"OpTypeInt 64 0", "0xffffffffffffffff",
  199. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 64, 0}),
  200. MakeInstruction(SpvOpConstant, {1, 2, 0xffffffffu, 0xffffffffu})})},
  201. {"OpTypeInt 64 1", "0x1234",
  202. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 64, 1}),
  203. MakeInstruction(SpvOpConstant, {1, 2, 0x1234, 0})})},
  204. {"OpTypeInt 64 1", "-42",
  205. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 64, 1}),
  206. MakeInstruction(SpvOpConstant, {1, 2, uint32_t(-42), uint32_t(-1)})})},
  207. {"OpTypeInt 64 1", "-0x01",
  208. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 64, 1}),
  209. MakeInstruction(SpvOpConstant, {1, 2, 0xffffffffu, 0xffffffffu})})},
  210. {"OpTypeInt 64 1", "9223372036854775807",
  211. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 64, 1}),
  212. MakeInstruction(SpvOpConstant, {1, 2, 0xffffffffu, 0x7fffffffu})})},
  213. {"OpTypeInt 64 1", "0x7fffffff",
  214. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 64, 1}),
  215. MakeInstruction(SpvOpConstant, {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(SpvOpTypeInt, {1, 16, 0}),
  358. MakeInstruction(SpvOpSpecConstant, {1, 2, 0x1234})})},
  359. {"OpTypeInt 16 0", "0x8000",
  360. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 16, 0}),
  361. MakeInstruction(SpvOpSpecConstant, {1, 2, 0x8000})})},
  362. {"OpTypeInt 16 1", "0x8000", // Test sign extension.
  363. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 16, 1}),
  364. MakeInstruction(SpvOpSpecConstant, {1, 2, 0xffff8000})})},
  365. {"OpTypeInt 16 1", "-32",
  366. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 16, 1}),
  367. MakeInstruction(SpvOpSpecConstant, {1, 2, uint32_t(-32)})})},
  368. // Check 32 bits
  369. {"OpTypeInt 32 0", "42",
  370. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 32, 0}),
  371. MakeInstruction(SpvOpSpecConstant, {1, 2, 42})})},
  372. {"OpTypeInt 32 1", "-32",
  373. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 32, 1}),
  374. MakeInstruction(SpvOpSpecConstant, {1, 2, uint32_t(-32)})})},
  375. {"OpTypeFloat 32", "1.0",
  376. Concatenate({MakeInstruction(SpvOpTypeFloat, {1, 32}),
  377. MakeInstruction(SpvOpSpecConstant, {1, 2, 0x3f800000})})},
  378. {"OpTypeFloat 32", "10.0",
  379. Concatenate({MakeInstruction(SpvOpTypeFloat, {1, 32}),
  380. MakeInstruction(SpvOpSpecConstant, {1, 2, 0x41200000})})},
  381. // Check 48 bits
  382. {"OpTypeInt 48 0", "0x1234",
  383. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 48, 0}),
  384. MakeInstruction(SpvOpSpecConstant, {1, 2, 0x1234, 0})})},
  385. {"OpTypeInt 48 0", "0x800000000001",
  386. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 48, 0}),
  387. MakeInstruction(SpvOpSpecConstant, {1, 2, 1, 0x00008000})})},
  388. {"OpTypeInt 48 1", "0x800000000000", // Test sign extension.
  389. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 48, 1}),
  390. MakeInstruction(SpvOpSpecConstant, {1, 2, 0, 0xffff8000})})},
  391. {"OpTypeInt 48 1", "-32",
  392. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 48, 1}),
  393. MakeInstruction(SpvOpSpecConstant, {1, 2, uint32_t(-32), uint32_t(-1)})})},
  394. // Check 64 bits
  395. {"OpTypeInt 64 0", "0x1234",
  396. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 64, 0}),
  397. MakeInstruction(SpvOpSpecConstant, {1, 2, 0x1234, 0})})},
  398. {"OpTypeInt 64 1", "0x1234",
  399. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 64, 1}),
  400. MakeInstruction(SpvOpSpecConstant, {1, 2, 0x1234, 0})})},
  401. {"OpTypeInt 64 1", "-42",
  402. Concatenate({MakeInstruction(SpvOpTypeInt, {1, 64, 1}),
  403. MakeInstruction(SpvOpSpecConstant, {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. INSTANTIATE_TEST_SUITE_P(
  448. OpConstantRoundTrip, RoundTripTest,
  449. ::testing::ValuesIn(std::vector<std::string>{
  450. // 16 bit
  451. "%1 = OpTypeInt 16 0\n%2 = OpConstant %1 0\n",
  452. "%1 = OpTypeInt 16 0\n%2 = OpConstant %1 65535\n",
  453. "%1 = OpTypeInt 16 1\n%2 = OpConstant %1 -32768\n",
  454. "%1 = OpTypeInt 16 1\n%2 = OpConstant %1 32767\n",
  455. "%1 = OpTypeInt 32 0\n%2 = OpConstant %1 0\n",
  456. // 32 bit
  457. std::string("%1 = OpTypeInt 32 0\n%2 = OpConstant %1 0\n"),
  458. std::string("%1 = OpTypeInt 32 0\n%2 = OpConstant %1 ") +
  459. std::to_string(std::numeric_limits<uint32_t>::max()) + "\n",
  460. std::string("%1 = OpTypeInt 32 1\n%2 = OpConstant %1 ") +
  461. std::to_string(std::numeric_limits<int32_t>::max()) + "\n",
  462. std::string("%1 = OpTypeInt 32 1\n%2 = OpConstant %1 ") +
  463. std::to_string(std::numeric_limits<int32_t>::min()) + "\n",
  464. // 48 bit
  465. std::string("%1 = OpTypeInt 48 0\n%2 = OpConstant %1 0\n"),
  466. std::string("%1 = OpTypeInt 48 0\n%2 = OpConstant %1 ") +
  467. std::to_string(kMaxUnsigned48Bit) + "\n",
  468. std::string("%1 = OpTypeInt 48 1\n%2 = OpConstant %1 ") +
  469. std::to_string(kMaxSigned48Bit) + "\n",
  470. std::string("%1 = OpTypeInt 48 1\n%2 = OpConstant %1 ") +
  471. std::to_string(kMinSigned48Bit) + "\n",
  472. // 64 bit
  473. std::string("%1 = OpTypeInt 64 0\n%2 = OpConstant %1 0\n"),
  474. std::string("%1 = OpTypeInt 64 0\n%2 = OpConstant %1 ") +
  475. std::to_string(std::numeric_limits<uint64_t>::max()) + "\n",
  476. std::string("%1 = OpTypeInt 64 1\n%2 = OpConstant %1 ") +
  477. std::to_string(std::numeric_limits<int64_t>::max()) + "\n",
  478. std::string("%1 = OpTypeInt 64 1\n%2 = OpConstant %1 ") +
  479. std::to_string(std::numeric_limits<int64_t>::min()) + "\n",
  480. // 32-bit float
  481. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 0\n",
  482. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 13.5\n",
  483. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -12.5\n",
  484. // 64-bit float
  485. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 0\n",
  486. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 1.79767e+308\n",
  487. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -1.79767e+308\n",
  488. }));
  489. INSTANTIATE_TEST_SUITE_P(
  490. OpConstantHalfRoundTrip, RoundTripTest,
  491. ::testing::ValuesIn(std::vector<std::string>{
  492. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x0p+0\n",
  493. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x0p+0\n",
  494. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1p+0\n",
  495. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.1p+0\n",
  496. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.01p-1\n",
  497. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.8p+1\n",
  498. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.ffcp+1\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-16\n", // some denorms
  505. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1p-24\n",
  506. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x1p-24\n",
  507. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1p+16\n", // +inf
  508. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x1p+16\n", // -inf
  509. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x1.01p+16\n", // -inf
  510. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.01p+16\n", // nan
  511. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.11p+16\n", // nan
  512. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.ffp+16\n", // nan
  513. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.ffcp+16\n", // nan
  514. "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.004p+16\n", // nan
  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. }));
  521. // clang-format off
  522. // (Clang-format really wants to break up these strings across lines.
  523. INSTANTIATE_TEST_SUITE_P(
  524. OpConstantRoundTripNonFinite, RoundTripTest,
  525. ::testing::ValuesIn(std::vector<std::string>{
  526. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0x1p+128\n", // -inf
  527. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 0x1p+128\n", // inf
  528. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0x1.8p+128\n", // -nan
  529. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0x1.0002p+128\n", // -nan
  530. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0x1.0018p+128\n", // -nan
  531. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0x1.01ep+128\n", // -nan
  532. "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0x1.fffffep+128\n", // -nan
  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 64\n%2 = OpConstant %1 -0x1p+1024\n", // -inf
  539. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 0x1p+1024\n", // +inf
  540. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1.8p+1024\n", // -nan
  541. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1.0fp+1024\n", // -nan
  542. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1.0000000000001p+1024\n", // -nan
  543. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1.00003p+1024\n", // -nan
  544. "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1.fffffffffffffp+1024\n", // -nan
  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. }));
  551. // clang-format on
  552. INSTANTIATE_TEST_SUITE_P(
  553. OpSpecConstantRoundTrip, RoundTripTest,
  554. ::testing::ValuesIn(std::vector<std::string>{
  555. // 16 bit
  556. "%1 = OpTypeInt 16 0\n%2 = OpSpecConstant %1 0\n",
  557. "%1 = OpTypeInt 16 0\n%2 = OpSpecConstant %1 65535\n",
  558. "%1 = OpTypeInt 16 1\n%2 = OpSpecConstant %1 -32768\n",
  559. "%1 = OpTypeInt 16 1\n%2 = OpSpecConstant %1 32767\n",
  560. "%1 = OpTypeInt 32 0\n%2 = OpSpecConstant %1 0\n",
  561. // 32 bit
  562. std::string("%1 = OpTypeInt 32 0\n%2 = OpSpecConstant %1 0\n"),
  563. std::string("%1 = OpTypeInt 32 0\n%2 = OpSpecConstant %1 ") +
  564. std::to_string(std::numeric_limits<uint32_t>::max()) + "\n",
  565. std::string("%1 = OpTypeInt 32 1\n%2 = OpSpecConstant %1 ") +
  566. std::to_string(std::numeric_limits<int32_t>::max()) + "\n",
  567. std::string("%1 = OpTypeInt 32 1\n%2 = OpSpecConstant %1 ") +
  568. std::to_string(std::numeric_limits<int32_t>::min()) + "\n",
  569. // 48 bit
  570. std::string("%1 = OpTypeInt 48 0\n%2 = OpSpecConstant %1 0\n"),
  571. std::string("%1 = OpTypeInt 48 0\n%2 = OpSpecConstant %1 ") +
  572. std::to_string(kMaxUnsigned48Bit) + "\n",
  573. std::string("%1 = OpTypeInt 48 1\n%2 = OpSpecConstant %1 ") +
  574. std::to_string(kMaxSigned48Bit) + "\n",
  575. std::string("%1 = OpTypeInt 48 1\n%2 = OpSpecConstant %1 ") +
  576. std::to_string(kMinSigned48Bit) + "\n",
  577. // 64 bit
  578. std::string("%1 = OpTypeInt 64 0\n%2 = OpSpecConstant %1 0\n"),
  579. std::string("%1 = OpTypeInt 64 0\n%2 = OpSpecConstant %1 ") +
  580. std::to_string(std::numeric_limits<uint64_t>::max()) + "\n",
  581. std::string("%1 = OpTypeInt 64 1\n%2 = OpSpecConstant %1 ") +
  582. std::to_string(std::numeric_limits<int64_t>::max()) + "\n",
  583. std::string("%1 = OpTypeInt 64 1\n%2 = OpSpecConstant %1 ") +
  584. std::to_string(std::numeric_limits<int64_t>::min()) + "\n",
  585. // 32-bit float
  586. "%1 = OpTypeFloat 32\n%2 = OpSpecConstant %1 0\n",
  587. "%1 = OpTypeFloat 32\n%2 = OpSpecConstant %1 13.5\n",
  588. "%1 = OpTypeFloat 32\n%2 = OpSpecConstant %1 -12.5\n",
  589. // 64-bit float
  590. "%1 = OpTypeFloat 64\n%2 = OpSpecConstant %1 0\n",
  591. "%1 = OpTypeFloat 64\n%2 = OpSpecConstant %1 1.79767e+308\n",
  592. "%1 = OpTypeFloat 64\n%2 = OpSpecConstant %1 -1.79767e+308\n",
  593. }));
  594. // Test OpSpecConstantOp
  595. using OpSpecConstantOpTestWithIds =
  596. spvtest::TextToBinaryTestBase<::testing::TestWithParam<EnumCase<SpvOp>>>;
  597. // The operands to the OpSpecConstantOp opcode are all Ids.
  598. TEST_P(OpSpecConstantOpTestWithIds, Assembly) {
  599. std::stringstream input;
  600. input << "%2 = OpSpecConstantOp %1 " << GetParam().name();
  601. for (auto id : GetParam().operands()) input << " %" << id;
  602. input << "\n";
  603. EXPECT_THAT(CompiledInstructions(input.str()),
  604. Eq(MakeInstruction(SpvOpSpecConstantOp,
  605. {1, 2, uint32_t(GetParam().value())},
  606. GetParam().operands())));
  607. // Check the disassembler as well.
  608. EXPECT_THAT(EncodeAndDecodeSuccessfully(input.str()), input.str());
  609. }
  610. // clang-format off
  611. #define CASE1(NAME) { SpvOp##NAME, #NAME, {3} }
  612. #define CASE2(NAME) { SpvOp##NAME, #NAME, {3, 4} }
  613. #define CASE3(NAME) { SpvOp##NAME, #NAME, {3, 4, 5} }
  614. #define CASE4(NAME) { SpvOp##NAME, #NAME, {3, 4, 5, 6} }
  615. #define CASE5(NAME) { SpvOp##NAME, #NAME, {3, 4, 5, 6, 7} }
  616. #define CASE6(NAME) { SpvOp##NAME, #NAME, {3, 4, 5, 6, 7, 8} }
  617. INSTANTIATE_TEST_SUITE_P(
  618. TextToBinaryOpSpecConstantOp, OpSpecConstantOpTestWithIds,
  619. ::testing::ValuesIn(std::vector<EnumCase<SpvOp>>{
  620. // Conversion
  621. CASE1(SConvert),
  622. CASE1(FConvert),
  623. CASE1(ConvertFToS),
  624. CASE1(ConvertSToF),
  625. CASE1(ConvertFToU),
  626. CASE1(ConvertUToF),
  627. CASE1(UConvert),
  628. CASE1(ConvertPtrToU),
  629. CASE1(ConvertUToPtr),
  630. CASE1(GenericCastToPtr),
  631. CASE1(PtrCastToGeneric),
  632. CASE1(Bitcast),
  633. CASE1(QuantizeToF16),
  634. // Arithmetic
  635. CASE1(SNegate),
  636. CASE1(Not),
  637. CASE2(IAdd),
  638. CASE2(ISub),
  639. CASE2(IMul),
  640. CASE2(UDiv),
  641. CASE2(SDiv),
  642. CASE2(UMod),
  643. CASE2(SRem),
  644. CASE2(SMod),
  645. CASE2(ShiftRightLogical),
  646. CASE2(ShiftRightArithmetic),
  647. CASE2(ShiftLeftLogical),
  648. CASE2(BitwiseOr),
  649. CASE2(BitwiseAnd),
  650. CASE2(BitwiseXor),
  651. CASE1(FNegate),
  652. CASE2(FAdd),
  653. CASE2(FSub),
  654. CASE2(FMul),
  655. CASE2(FDiv),
  656. CASE2(FRem),
  657. CASE2(FMod),
  658. // Composite operations use literal numbers. So they're in another test.
  659. // Logical
  660. CASE2(LogicalOr),
  661. CASE2(LogicalAnd),
  662. CASE1(LogicalNot),
  663. CASE2(LogicalEqual),
  664. CASE2(LogicalNotEqual),
  665. CASE3(Select),
  666. // Comparison
  667. CASE2(IEqual),
  668. CASE2(INotEqual), // Allowed in 1.0 Rev 7
  669. CASE2(ULessThan),
  670. CASE2(SLessThan),
  671. CASE2(UGreaterThan),
  672. CASE2(SGreaterThan),
  673. CASE2(ULessThanEqual),
  674. CASE2(SLessThanEqual),
  675. CASE2(UGreaterThanEqual),
  676. CASE2(SGreaterThanEqual),
  677. // Memory
  678. // For AccessChain, there is a base Id, then a sequence of index Ids.
  679. // Having no index Ids is a corner case.
  680. CASE1(AccessChain),
  681. CASE2(AccessChain),
  682. CASE6(AccessChain),
  683. CASE1(InBoundsAccessChain),
  684. CASE2(InBoundsAccessChain),
  685. CASE6(InBoundsAccessChain),
  686. // PtrAccessChain also has an element Id.
  687. CASE2(PtrAccessChain),
  688. CASE3(PtrAccessChain),
  689. CASE6(PtrAccessChain),
  690. CASE2(InBoundsPtrAccessChain),
  691. CASE3(InBoundsPtrAccessChain),
  692. CASE6(InBoundsPtrAccessChain),
  693. }));
  694. #undef CASE1
  695. #undef CASE2
  696. #undef CASE3
  697. #undef CASE4
  698. #undef CASE5
  699. #undef CASE6
  700. // clang-format on
  701. using OpSpecConstantOpTestWithTwoIdsThenLiteralNumbers =
  702. spvtest::TextToBinaryTestBase<::testing::TestWithParam<EnumCase<SpvOp>>>;
  703. // The operands to the OpSpecConstantOp opcode are two Ids followed by a
  704. // sequence of literal numbers.
  705. TEST_P(OpSpecConstantOpTestWithTwoIdsThenLiteralNumbers, Assembly) {
  706. std::stringstream input;
  707. input << "%2 = OpSpecConstantOp %1 " << GetParam().name() << " %3 %4";
  708. for (auto number : GetParam().operands()) input << " " << number;
  709. input << "\n";
  710. EXPECT_THAT(CompiledInstructions(input.str()),
  711. Eq(MakeInstruction(SpvOpSpecConstantOp,
  712. {1, 2, uint32_t(GetParam().value()), 3, 4},
  713. GetParam().operands())));
  714. // Check the disassembler as well.
  715. EXPECT_THAT(EncodeAndDecodeSuccessfully(input.str()), input.str());
  716. }
  717. #define CASE(NAME) SpvOp##NAME, #NAME
  718. INSTANTIATE_TEST_SUITE_P(
  719. TextToBinaryOpSpecConstantOp,
  720. OpSpecConstantOpTestWithTwoIdsThenLiteralNumbers,
  721. ::testing::ValuesIn(std::vector<EnumCase<SpvOp>>{
  722. // For VectorShuffle, there are two vector operands, and at least
  723. // two selector Ids. OpenCL can have up to 16-element vectors.
  724. {CASE(VectorShuffle), {0, 0}},
  725. {CASE(VectorShuffle), {4, 3, 2, 1}},
  726. {CASE(VectorShuffle), {0, 2, 4, 6, 1, 3, 5, 7}},
  727. {CASE(VectorShuffle),
  728. {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}},
  729. // For CompositeInsert, there is an object to insert, the target
  730. // composite, and then literal indices.
  731. {CASE(CompositeInsert), {0}},
  732. {CASE(CompositeInsert), {4, 3, 99, 1}},
  733. }));
  734. using OpSpecConstantOpTestWithOneIdThenLiteralNumbers =
  735. spvtest::TextToBinaryTestBase<::testing::TestWithParam<EnumCase<SpvOp>>>;
  736. // The operands to the OpSpecConstantOp opcode are one Id followed by a
  737. // sequence of literal numbers.
  738. TEST_P(OpSpecConstantOpTestWithOneIdThenLiteralNumbers, Assembly) {
  739. std::stringstream input;
  740. input << "%2 = OpSpecConstantOp %1 " << GetParam().name() << " %3";
  741. for (auto number : GetParam().operands()) input << " " << number;
  742. input << "\n";
  743. EXPECT_THAT(CompiledInstructions(input.str()),
  744. Eq(MakeInstruction(SpvOpSpecConstantOp,
  745. {1, 2, uint32_t(GetParam().value()), 3},
  746. GetParam().operands())));
  747. // Check the disassembler as well.
  748. EXPECT_THAT(EncodeAndDecodeSuccessfully(input.str()), input.str());
  749. }
  750. #define CASE(NAME) SpvOp##NAME, #NAME
  751. INSTANTIATE_TEST_SUITE_P(
  752. TextToBinaryOpSpecConstantOp,
  753. OpSpecConstantOpTestWithOneIdThenLiteralNumbers,
  754. ::testing::ValuesIn(std::vector<EnumCase<SpvOp>>{
  755. // For CompositeExtract, the universal limit permits up to 255 literal
  756. // indices. Let's only test a few.
  757. {CASE(CompositeExtract), {0}},
  758. {CASE(CompositeExtract), {0, 99, 42, 16, 17, 12, 19}},
  759. }));
  760. // TODO(dneto): OpConstantTrue
  761. // TODO(dneto): OpConstantFalse
  762. // TODO(dneto): OpConstantComposite
  763. // TODO(dneto): OpConstantSampler: other variations Param is 0 or 1
  764. // TODO(dneto): OpConstantNull
  765. // TODO(dneto): OpSpecConstantTrue
  766. // TODO(dneto): OpSpecConstantFalse
  767. // TODO(dneto): OpSpecConstantComposite
  768. // TODO(dneto): Negative tests for OpSpecConstantOp
  769. } // namespace
  770. } // namespace spvtools