set_spec_const_default_value_test.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. // Copyright (c) 2016 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <vector>
  15. #include "gmock/gmock.h"
  16. #include "test/opt/pass_fixture.h"
  17. namespace spvtools {
  18. namespace opt {
  19. namespace {
  20. using testing::Eq;
  21. using SpecIdToValueStrMap =
  22. SetSpecConstantDefaultValuePass::SpecIdToValueStrMap;
  23. using SpecIdToValueBitPatternMap =
  24. SetSpecConstantDefaultValuePass::SpecIdToValueBitPatternMap;
  25. struct DefaultValuesStringParsingTestCase {
  26. const char* default_values_str;
  27. bool expect_success;
  28. SpecIdToValueStrMap expected_map;
  29. };
  30. using DefaultValuesStringParsingTest =
  31. ::testing::TestWithParam<DefaultValuesStringParsingTestCase>;
  32. TEST_P(DefaultValuesStringParsingTest, TestCase) {
  33. const auto& tc = GetParam();
  34. auto actual_map = SetSpecConstantDefaultValuePass::ParseDefaultValuesString(
  35. tc.default_values_str);
  36. if (tc.expect_success) {
  37. EXPECT_NE(nullptr, actual_map);
  38. if (actual_map) {
  39. EXPECT_THAT(*actual_map, Eq(tc.expected_map));
  40. }
  41. } else {
  42. EXPECT_EQ(nullptr, actual_map);
  43. }
  44. }
  45. INSTANTIATE_TEST_SUITE_P(
  46. ValidString, DefaultValuesStringParsingTest,
  47. ::testing::ValuesIn(std::vector<DefaultValuesStringParsingTestCase>{
  48. // 0. empty map
  49. {"", true, SpecIdToValueStrMap{}},
  50. // 1. one pair
  51. {"100:1024", true, SpecIdToValueStrMap{{100, "1024"}}},
  52. // 2. two pairs
  53. {"100:1024 200:2048", true,
  54. SpecIdToValueStrMap{{100, "1024"}, {200, "2048"}}},
  55. // 3. spaces between entries
  56. {"100:1024 \n \r \t \v \f 200:2048", true,
  57. SpecIdToValueStrMap{{100, "1024"}, {200, "2048"}}},
  58. // 4. \t, \n, \r and spaces before spec id
  59. {" \n \r\t \t \v \f 100:1024", true,
  60. SpecIdToValueStrMap{{100, "1024"}}},
  61. // 5. \t, \n, \r and spaces after value string
  62. {"100:1024 \n \r\t \t \v \f ", true,
  63. SpecIdToValueStrMap{{100, "1024"}}},
  64. // 6. maximum spec id
  65. {"4294967295:0", true, SpecIdToValueStrMap{{4294967295, "0"}}},
  66. // 7. minimum spec id
  67. {"0:100", true, SpecIdToValueStrMap{{0, "100"}}},
  68. // 8. random content without spaces are allowed
  69. {"200:random_stuff", true, SpecIdToValueStrMap{{200, "random_stuff"}}},
  70. // 9. support hex format spec id (just because we use the
  71. // ParseNumber() utility)
  72. {"0x100:1024", true, SpecIdToValueStrMap{{256, "1024"}}},
  73. // 10. multiple entries
  74. {"101:1 102:2 103:3 104:4 200:201 9999:1000 0x100:333", true,
  75. SpecIdToValueStrMap{{101, "1"},
  76. {102, "2"},
  77. {103, "3"},
  78. {104, "4"},
  79. {200, "201"},
  80. {9999, "1000"},
  81. {256, "333"}}},
  82. // 11. default value in hex float format
  83. {"100:0x0.3p10", true, SpecIdToValueStrMap{{100, "0x0.3p10"}}},
  84. // 12. default value in decimal float format
  85. {"100:1.5e-13", true, SpecIdToValueStrMap{{100, "1.5e-13"}}},
  86. }));
  87. INSTANTIATE_TEST_SUITE_P(
  88. InvalidString, DefaultValuesStringParsingTest,
  89. ::testing::ValuesIn(std::vector<DefaultValuesStringParsingTestCase>{
  90. // 0. missing default value
  91. {"100:", false, SpecIdToValueStrMap{}},
  92. // 1. spec id is not an integer
  93. {"100.0:200", false, SpecIdToValueStrMap{}},
  94. // 2. spec id is not a number
  95. {"something_not_a_number:1", false, SpecIdToValueStrMap{}},
  96. // 3. only spec id number
  97. {"100", false, SpecIdToValueStrMap{}},
  98. // 4. same spec id defined multiple times
  99. {"100:20 100:21", false, SpecIdToValueStrMap{}},
  100. // 5. Multiple definition of an identical spec id in different forms
  101. // is not allowed
  102. {"0x100:100 256:200", false, SpecIdToValueStrMap{}},
  103. // 6. empty spec id
  104. {":3", false, SpecIdToValueStrMap{}},
  105. // 7. only colon
  106. {":", false, SpecIdToValueStrMap{}},
  107. // 8. spec id overflow
  108. {"4294967296:200", false, SpecIdToValueStrMap{}},
  109. // 9. spec id less than 0
  110. {"-1:200", false, SpecIdToValueStrMap{}},
  111. // 10. nullptr
  112. {nullptr, false, SpecIdToValueStrMap{}},
  113. // 11. only a number is invalid
  114. {"1234", false, SpecIdToValueStrMap{}},
  115. // 12. invalid entry separator
  116. {"12:34;23:14", false, SpecIdToValueStrMap{}},
  117. // 13. invalid spec id and default value separator
  118. {"12@34", false, SpecIdToValueStrMap{}},
  119. // 14. spaces before colon
  120. {"100 :1024", false, SpecIdToValueStrMap{}},
  121. // 15. spaces after colon
  122. {"100: 1024", false, SpecIdToValueStrMap{}},
  123. // 16. spec id represented in hex float format is invalid
  124. {"0x3p10:200", false, SpecIdToValueStrMap{}},
  125. }));
  126. struct SetSpecConstantDefaultValueInStringFormTestCase {
  127. const char* code;
  128. SpecIdToValueStrMap default_values;
  129. const char* expected;
  130. };
  131. using SetSpecConstantDefaultValueInStringFormParamTest = PassTest<
  132. ::testing::TestWithParam<SetSpecConstantDefaultValueInStringFormTestCase>>;
  133. TEST_P(SetSpecConstantDefaultValueInStringFormParamTest, TestCase) {
  134. const auto& tc = GetParam();
  135. SinglePassRunAndCheck<SetSpecConstantDefaultValuePass>(
  136. tc.code, tc.expected, /* skip_nop = */ false, tc.default_values);
  137. }
  138. INSTANTIATE_TEST_SUITE_P(
  139. ValidCases, SetSpecConstantDefaultValueInStringFormParamTest,
  140. ::testing::ValuesIn(std::vector<
  141. SetSpecConstantDefaultValueInStringFormTestCase>{
  142. // 0. Empty.
  143. {"", SpecIdToValueStrMap{}, ""},
  144. // 1. Empty with non-empty values to set.
  145. {"", SpecIdToValueStrMap{{1, "100"}, {2, "200"}}, ""},
  146. // 2. Bool type.
  147. {
  148. // code
  149. "OpDecorate %1 SpecId 100\n"
  150. "OpDecorate %2 SpecId 101\n"
  151. "%bool = OpTypeBool\n"
  152. "%1 = OpSpecConstantTrue %bool\n"
  153. "%2 = OpSpecConstantFalse %bool\n",
  154. // default values
  155. SpecIdToValueStrMap{{100, "false"}, {101, "true"}},
  156. // expected
  157. "OpDecorate %1 SpecId 100\n"
  158. "OpDecorate %2 SpecId 101\n"
  159. "%bool = OpTypeBool\n"
  160. "%1 = OpSpecConstantFalse %bool\n"
  161. "%2 = OpSpecConstantTrue %bool\n",
  162. },
  163. // 3. 32-bit int type.
  164. {
  165. // code
  166. "OpDecorate %1 SpecId 100\n"
  167. "OpDecorate %2 SpecId 101\n"
  168. "OpDecorate %3 SpecId 102\n"
  169. "%int = OpTypeInt 32 1\n"
  170. "%1 = OpSpecConstant %int 10\n"
  171. "%2 = OpSpecConstant %int 11\n"
  172. "%3 = OpSpecConstant %int 11\n",
  173. // default values
  174. SpecIdToValueStrMap{
  175. {100, "2147483647"}, {101, "0xffffffff"}, {102, "-42"}},
  176. // expected
  177. "OpDecorate %1 SpecId 100\n"
  178. "OpDecorate %2 SpecId 101\n"
  179. "OpDecorate %3 SpecId 102\n"
  180. "%int = OpTypeInt 32 1\n"
  181. "%1 = OpSpecConstant %int 2147483647\n"
  182. "%2 = OpSpecConstant %int -1\n"
  183. "%3 = OpSpecConstant %int -42\n",
  184. },
  185. // 4. 64-bit uint type.
  186. {
  187. // code
  188. "OpDecorate %1 SpecId 100\n"
  189. "OpDecorate %2 SpecId 101\n"
  190. "%ulong = OpTypeInt 64 0\n"
  191. "%1 = OpSpecConstant %ulong 10\n"
  192. "%2 = OpSpecConstant %ulong 11\n",
  193. // default values
  194. SpecIdToValueStrMap{{100, "18446744073709551614"}, {101, "0x100"}},
  195. // expected
  196. "OpDecorate %1 SpecId 100\n"
  197. "OpDecorate %2 SpecId 101\n"
  198. "%ulong = OpTypeInt 64 0\n"
  199. "%1 = OpSpecConstant %ulong 18446744073709551614\n"
  200. "%2 = OpSpecConstant %ulong 256\n",
  201. },
  202. // 5. 32-bit float type.
  203. {
  204. // code
  205. "OpDecorate %1 SpecId 101\n"
  206. "OpDecorate %2 SpecId 102\n"
  207. "%float = OpTypeFloat 32\n"
  208. "%1 = OpSpecConstant %float 200\n"
  209. "%2 = OpSpecConstant %float 201\n",
  210. // default values
  211. SpecIdToValueStrMap{{101, "-0x1.fffffep+128"}, {102, "2.5"}},
  212. // expected
  213. "OpDecorate %1 SpecId 101\n"
  214. "OpDecorate %2 SpecId 102\n"
  215. "%float = OpTypeFloat 32\n"
  216. "%1 = OpSpecConstant %float -0x1.fffffep+128\n"
  217. "%2 = OpSpecConstant %float 2.5\n",
  218. },
  219. // 6. 64-bit float type.
  220. {
  221. // code
  222. "OpDecorate %1 SpecId 201\n"
  223. "OpDecorate %2 SpecId 202\n"
  224. "%double = OpTypeFloat 64\n"
  225. "%1 = OpSpecConstant %double 3.14159265358979\n"
  226. "%2 = OpSpecConstant %double 0.14285\n",
  227. // default values
  228. SpecIdToValueStrMap{{201, "0x1.fffffffffffffp+1024"},
  229. {202, "-32.5"}},
  230. // expected
  231. "OpDecorate %1 SpecId 201\n"
  232. "OpDecorate %2 SpecId 202\n"
  233. "%double = OpTypeFloat 64\n"
  234. "%1 = OpSpecConstant %double 0x1.fffffffffffffp+1024\n"
  235. "%2 = OpSpecConstant %double -32.5\n",
  236. },
  237. // 7. SpecId not found, expect no modification.
  238. {
  239. // code
  240. "OpDecorate %1 SpecId 201\n"
  241. "%double = OpTypeFloat 64\n"
  242. "%1 = OpSpecConstant %double 3.14159265358979\n",
  243. // default values
  244. SpecIdToValueStrMap{{8888, "0.0"}},
  245. // expected
  246. "OpDecorate %1 SpecId 201\n"
  247. "%double = OpTypeFloat 64\n"
  248. "%1 = OpSpecConstant %double 3.14159265358979\n",
  249. },
  250. // 8. Multiple types of spec constants.
  251. {
  252. // code
  253. "OpDecorate %1 SpecId 201\n"
  254. "OpDecorate %2 SpecId 202\n"
  255. "OpDecorate %3 SpecId 203\n"
  256. "%bool = OpTypeBool\n"
  257. "%int = OpTypeInt 32 1\n"
  258. "%double = OpTypeFloat 64\n"
  259. "%1 = OpSpecConstant %double 3.14159265358979\n"
  260. "%2 = OpSpecConstant %int 1024\n"
  261. "%3 = OpSpecConstantTrue %bool\n",
  262. // default values
  263. SpecIdToValueStrMap{
  264. {201, "0x1.fffffffffffffp+1024"},
  265. {202, "2048"},
  266. {203, "false"},
  267. },
  268. // expected
  269. "OpDecorate %1 SpecId 201\n"
  270. "OpDecorate %2 SpecId 202\n"
  271. "OpDecorate %3 SpecId 203\n"
  272. "%bool = OpTypeBool\n"
  273. "%int = OpTypeInt 32 1\n"
  274. "%double = OpTypeFloat 64\n"
  275. "%1 = OpSpecConstant %double 0x1.fffffffffffffp+1024\n"
  276. "%2 = OpSpecConstant %int 2048\n"
  277. "%3 = OpSpecConstantFalse %bool\n",
  278. },
  279. // 9. Ignore other decorations.
  280. {
  281. // code
  282. "OpDecorate %1 ArrayStride 4\n"
  283. "%int = OpTypeInt 32 1\n"
  284. "%1 = OpSpecConstant %int 100\n",
  285. // default values
  286. SpecIdToValueStrMap{{4, "0x7fffffff"}},
  287. // expected
  288. "OpDecorate %1 ArrayStride 4\n"
  289. "%int = OpTypeInt 32 1\n"
  290. "%1 = OpSpecConstant %int 100\n",
  291. },
  292. // 10. Distinguish from other decorations.
  293. {
  294. // code
  295. "OpDecorate %1 SpecId 100\n"
  296. "OpDecorate %1 ArrayStride 4\n"
  297. "%int = OpTypeInt 32 1\n"
  298. "%1 = OpSpecConstant %int 100\n",
  299. // default values
  300. SpecIdToValueStrMap{{4, "0x7fffffff"}, {100, "0xffffffff"}},
  301. // expected
  302. "OpDecorate %1 SpecId 100\n"
  303. "OpDecorate %1 ArrayStride 4\n"
  304. "%int = OpTypeInt 32 1\n"
  305. "%1 = OpSpecConstant %int -1\n",
  306. },
  307. // 11. Decorate through decoration group.
  308. {
  309. // code
  310. "OpDecorate %1 SpecId 100\n"
  311. "%1 = OpDecorationGroup\n"
  312. "OpGroupDecorate %1 %2\n"
  313. "%int = OpTypeInt 32 1\n"
  314. "%2 = OpSpecConstant %int 100\n",
  315. // default values
  316. SpecIdToValueStrMap{{100, "0x7fffffff"}},
  317. // expected
  318. "OpDecorate %1 SpecId 100\n"
  319. "%1 = OpDecorationGroup\n"
  320. "OpGroupDecorate %1 %2\n"
  321. "%int = OpTypeInt 32 1\n"
  322. "%2 = OpSpecConstant %int 2147483647\n",
  323. },
  324. // 12. Ignore other decorations in decoration group.
  325. {
  326. // code
  327. "OpDecorate %1 ArrayStride 4\n"
  328. "%1 = OpDecorationGroup\n"
  329. "OpGroupDecorate %1 %2\n"
  330. "%int = OpTypeInt 32 1\n"
  331. "%2 = OpSpecConstant %int 100\n",
  332. // default values
  333. SpecIdToValueStrMap{{4, "0x7fffffff"}},
  334. // expected
  335. "OpDecorate %1 ArrayStride 4\n"
  336. "%1 = OpDecorationGroup\n"
  337. "OpGroupDecorate %1 %2\n"
  338. "%int = OpTypeInt 32 1\n"
  339. "%2 = OpSpecConstant %int 100\n",
  340. },
  341. // 13. Distinguish from other decorations in decoration group.
  342. {
  343. // code
  344. "OpDecorate %1 SpecId 100\n"
  345. "OpDecorate %1 ArrayStride 4\n"
  346. "%1 = OpDecorationGroup\n"
  347. "OpGroupDecorate %1 %2\n"
  348. "%int = OpTypeInt 32 1\n"
  349. "%2 = OpSpecConstant %int 100\n",
  350. // default values
  351. SpecIdToValueStrMap{{100, "0x7fffffff"}, {4, "0x00000001"}},
  352. // expected
  353. "OpDecorate %1 SpecId 100\n"
  354. "OpDecorate %1 ArrayStride 4\n"
  355. "%1 = OpDecorationGroup\n"
  356. "OpGroupDecorate %1 %2\n"
  357. "%int = OpTypeInt 32 1\n"
  358. "%2 = OpSpecConstant %int 2147483647\n",
  359. },
  360. // 14. Unchanged bool default value
  361. {
  362. // code
  363. "OpDecorate %1 SpecId 100\n"
  364. "OpDecorate %2 SpecId 101\n"
  365. "%bool = OpTypeBool\n"
  366. "%1 = OpSpecConstantTrue %bool\n"
  367. "%2 = OpSpecConstantFalse %bool\n",
  368. // default values
  369. SpecIdToValueStrMap{{100, "true"}, {101, "false"}},
  370. // expected
  371. "OpDecorate %1 SpecId 100\n"
  372. "OpDecorate %2 SpecId 101\n"
  373. "%bool = OpTypeBool\n"
  374. "%1 = OpSpecConstantTrue %bool\n"
  375. "%2 = OpSpecConstantFalse %bool\n",
  376. },
  377. // 15. Unchanged int default values
  378. {
  379. // code
  380. "OpDecorate %1 SpecId 100\n"
  381. "OpDecorate %2 SpecId 101\n"
  382. "%int = OpTypeInt 32 1\n"
  383. "%ulong = OpTypeInt 64 0\n"
  384. "%1 = OpSpecConstant %int 10\n"
  385. "%2 = OpSpecConstant %ulong 11\n",
  386. // default values
  387. SpecIdToValueStrMap{{100, "10"}, {101, "11"}},
  388. // expected
  389. "OpDecorate %1 SpecId 100\n"
  390. "OpDecorate %2 SpecId 101\n"
  391. "%int = OpTypeInt 32 1\n"
  392. "%ulong = OpTypeInt 64 0\n"
  393. "%1 = OpSpecConstant %int 10\n"
  394. "%2 = OpSpecConstant %ulong 11\n",
  395. },
  396. // 16. Unchanged float default values
  397. {
  398. // code
  399. "OpDecorate %1 SpecId 201\n"
  400. "OpDecorate %2 SpecId 202\n"
  401. "%float = OpTypeFloat 32\n"
  402. "%double = OpTypeFloat 64\n"
  403. "%1 = OpSpecConstant %float 3.1415\n"
  404. "%2 = OpSpecConstant %double 0.14285\n",
  405. // default values
  406. SpecIdToValueStrMap{{201, "3.1415"}, {202, "0.14285"}},
  407. // expected
  408. "OpDecorate %1 SpecId 201\n"
  409. "OpDecorate %2 SpecId 202\n"
  410. "%float = OpTypeFloat 32\n"
  411. "%double = OpTypeFloat 64\n"
  412. "%1 = OpSpecConstant %float 3.1415\n"
  413. "%2 = OpSpecConstant %double 0.14285\n",
  414. },
  415. // 17. OpGroupDecorate may have multiple target ids defined by the same
  416. // eligible spec constant
  417. {
  418. // code
  419. "OpDecorate %1 SpecId 100\n"
  420. "%1 = OpDecorationGroup\n"
  421. "OpGroupDecorate %1 %2 %2 %2\n"
  422. "%int = OpTypeInt 32 1\n"
  423. "%2 = OpSpecConstant %int 100\n",
  424. // default values
  425. SpecIdToValueStrMap{{100, "0xffffffff"}},
  426. // expected
  427. "OpDecorate %1 SpecId 100\n"
  428. "%1 = OpDecorationGroup\n"
  429. "OpGroupDecorate %1 %2 %2 %2\n"
  430. "%int = OpTypeInt 32 1\n"
  431. "%2 = OpSpecConstant %int -1\n",
  432. },
  433. }));
  434. INSTANTIATE_TEST_SUITE_P(
  435. InvalidCases, SetSpecConstantDefaultValueInStringFormParamTest,
  436. ::testing::ValuesIn(std::vector<
  437. SetSpecConstantDefaultValueInStringFormTestCase>{
  438. // 0. Do not crash when decoration group is not used.
  439. {
  440. // code
  441. "OpDecorate %1 SpecId 100\n"
  442. "%1 = OpDecorationGroup\n"
  443. "%int = OpTypeInt 32 1\n"
  444. "%3 = OpSpecConstant %int 100\n",
  445. // default values
  446. SpecIdToValueStrMap{{100, "0x7fffffff"}},
  447. // expected
  448. "OpDecorate %1 SpecId 100\n"
  449. "%1 = OpDecorationGroup\n"
  450. "%int = OpTypeInt 32 1\n"
  451. "%3 = OpSpecConstant %int 100\n",
  452. },
  453. // 1. Do not crash when target does not exist.
  454. {
  455. // code
  456. "OpDecorate %1 SpecId 100\n"
  457. "%1 = OpDecorationGroup\n"
  458. "%int = OpTypeInt 32 1\n",
  459. // default values
  460. SpecIdToValueStrMap{{100, "0x7fffffff"}},
  461. // expected
  462. "OpDecorate %1 SpecId 100\n"
  463. "%1 = OpDecorationGroup\n"
  464. "%int = OpTypeInt 32 1\n",
  465. },
  466. // 2. Do nothing when SpecId decoration is not attached to a
  467. // non-spec-constant instruction.
  468. {
  469. // code
  470. "OpDecorate %1 SpecId 100\n"
  471. "%1 = OpDecorationGroup\n"
  472. "%int = OpTypeInt 32 1\n"
  473. "%int_101 = OpConstant %int 101\n",
  474. // default values
  475. SpecIdToValueStrMap{{100, "0x7fffffff"}},
  476. // expected
  477. "OpDecorate %1 SpecId 100\n"
  478. "%1 = OpDecorationGroup\n"
  479. "%int = OpTypeInt 32 1\n"
  480. "%int_101 = OpConstant %int 101\n",
  481. },
  482. // 3. Do nothing when SpecId decoration is not attached to a
  483. // OpSpecConstant{|True|False} instruction.
  484. {
  485. // code
  486. "OpDecorate %1 SpecId 100\n"
  487. "%int = OpTypeInt 32 1\n"
  488. "%3 = OpSpecConstant %int 101\n"
  489. "%1 = OpSpecConstantOp %int IAdd %3 %3\n",
  490. // default values
  491. SpecIdToValueStrMap{{100, "0x7fffffff"}},
  492. // expected
  493. "OpDecorate %1 SpecId 100\n"
  494. "%int = OpTypeInt 32 1\n"
  495. "%3 = OpSpecConstant %int 101\n"
  496. "%1 = OpSpecConstantOp %int IAdd %3 %3\n",
  497. },
  498. // 4. Do not crash and do nothing when SpecId decoration is applied to
  499. // multiple spec constants.
  500. {
  501. // code
  502. "OpDecorate %1 SpecId 100\n"
  503. "%1 = OpDecorationGroup\n"
  504. "OpGroupDecorate %1 %2 %3 %4\n"
  505. "%int = OpTypeInt 32 1\n"
  506. "%2 = OpSpecConstant %int 100\n"
  507. "%3 = OpSpecConstant %int 200\n"
  508. "%4 = OpSpecConstant %int 300\n",
  509. // default values
  510. SpecIdToValueStrMap{{100, "0xffffffff"}},
  511. // expected
  512. "OpDecorate %1 SpecId 100\n"
  513. "%1 = OpDecorationGroup\n"
  514. "OpGroupDecorate %1 %2 %3 %4\n"
  515. "%int = OpTypeInt 32 1\n"
  516. "%2 = OpSpecConstant %int 100\n"
  517. "%3 = OpSpecConstant %int 200\n"
  518. "%4 = OpSpecConstant %int 300\n",
  519. },
  520. // 5. Do not crash and do nothing when SpecId decoration is attached to
  521. // non-spec-constants (invalid case).
  522. {
  523. // code
  524. "OpDecorate %1 SpecId 100\n"
  525. "%1 = OpDecorationGroup\n"
  526. "%2 = OpDecorationGroup\n"
  527. "OpGroupDecorate %1 %2\n"
  528. "%int = OpTypeInt 32 1\n"
  529. "%int_100 = OpConstant %int 100\n",
  530. // default values
  531. SpecIdToValueStrMap{{100, "0xffffffff"}},
  532. // expected
  533. "OpDecorate %1 SpecId 100\n"
  534. "%1 = OpDecorationGroup\n"
  535. "%2 = OpDecorationGroup\n"
  536. "OpGroupDecorate %1 %2\n"
  537. "%int = OpTypeInt 32 1\n"
  538. "%int_100 = OpConstant %int 100\n",
  539. },
  540. // 6. Boolean type spec constant cannot be set with numeric values in
  541. // string form. i.e. only 'true' and 'false' are acceptable for setting
  542. // boolean type spec constants. Nothing should be done if numeric values
  543. // in string form are provided.
  544. {
  545. // code
  546. "OpDecorate %1 SpecId 100\n"
  547. "OpDecorate %2 SpecId 101\n"
  548. "OpDecorate %3 SpecId 102\n"
  549. "OpDecorate %4 SpecId 103\n"
  550. "OpDecorate %5 SpecId 104\n"
  551. "OpDecorate %6 SpecId 105\n"
  552. "%bool = OpTypeBool\n"
  553. "%1 = OpSpecConstantTrue %bool\n"
  554. "%2 = OpSpecConstantFalse %bool\n"
  555. "%3 = OpSpecConstantTrue %bool\n"
  556. "%4 = OpSpecConstantTrue %bool\n"
  557. "%5 = OpSpecConstantTrue %bool\n"
  558. "%6 = OpSpecConstantFalse %bool\n",
  559. // default values
  560. SpecIdToValueStrMap{{100, "0"},
  561. {101, "1"},
  562. {102, "0x0"},
  563. {103, "0.0"},
  564. {104, "-0.0"},
  565. {105, "0x12345678"}},
  566. // expected
  567. "OpDecorate %1 SpecId 100\n"
  568. "OpDecorate %2 SpecId 101\n"
  569. "OpDecorate %3 SpecId 102\n"
  570. "OpDecorate %4 SpecId 103\n"
  571. "OpDecorate %5 SpecId 104\n"
  572. "OpDecorate %6 SpecId 105\n"
  573. "%bool = OpTypeBool\n"
  574. "%1 = OpSpecConstantTrue %bool\n"
  575. "%2 = OpSpecConstantFalse %bool\n"
  576. "%3 = OpSpecConstantTrue %bool\n"
  577. "%4 = OpSpecConstantTrue %bool\n"
  578. "%5 = OpSpecConstantTrue %bool\n"
  579. "%6 = OpSpecConstantFalse %bool\n",
  580. },
  581. }));
  582. struct SetSpecConstantDefaultValueInBitPatternFormTestCase {
  583. const char* code;
  584. SpecIdToValueBitPatternMap default_values;
  585. const char* expected;
  586. };
  587. using SetSpecConstantDefaultValueInBitPatternFormParamTest =
  588. PassTest<::testing::TestWithParam<
  589. SetSpecConstantDefaultValueInBitPatternFormTestCase>>;
  590. TEST_P(SetSpecConstantDefaultValueInBitPatternFormParamTest, TestCase) {
  591. const auto& tc = GetParam();
  592. SinglePassRunAndCheck<SetSpecConstantDefaultValuePass>(
  593. tc.code, tc.expected, /* skip_nop = */ false, tc.default_values);
  594. }
  595. INSTANTIATE_TEST_SUITE_P(
  596. ValidCases, SetSpecConstantDefaultValueInBitPatternFormParamTest,
  597. ::testing::ValuesIn(std::vector<
  598. SetSpecConstantDefaultValueInBitPatternFormTestCase>{
  599. // 0. Empty.
  600. {"", SpecIdToValueBitPatternMap{}, ""},
  601. // 1. Empty with non-empty values to set.
  602. {"", SpecIdToValueBitPatternMap{{1, {100}}, {2, {200}}}, ""},
  603. // 2. Basic bool type.
  604. {
  605. // code
  606. "OpDecorate %1 SpecId 100\n"
  607. "OpDecorate %2 SpecId 101\n"
  608. "%bool = OpTypeBool\n"
  609. "%1 = OpSpecConstantTrue %bool\n"
  610. "%2 = OpSpecConstantFalse %bool\n",
  611. // default values
  612. SpecIdToValueBitPatternMap{{100, {0x0}}, {101, {0x1}}},
  613. // expected
  614. "OpDecorate %1 SpecId 100\n"
  615. "OpDecorate %2 SpecId 101\n"
  616. "%bool = OpTypeBool\n"
  617. "%1 = OpSpecConstantFalse %bool\n"
  618. "%2 = OpSpecConstantTrue %bool\n",
  619. },
  620. // 3. 32-bit int type.
  621. {
  622. // code
  623. "OpDecorate %1 SpecId 100\n"
  624. "OpDecorate %2 SpecId 101\n"
  625. "OpDecorate %3 SpecId 102\n"
  626. "%int = OpTypeInt 32 1\n"
  627. "%1 = OpSpecConstant %int 10\n"
  628. "%2 = OpSpecConstant %int 11\n"
  629. "%3 = OpSpecConstant %int 11\n",
  630. // default values
  631. SpecIdToValueBitPatternMap{
  632. {100, {2147483647}}, {101, {0xffffffff}}, {102, {0xffffffd6}}},
  633. // expected
  634. "OpDecorate %1 SpecId 100\n"
  635. "OpDecorate %2 SpecId 101\n"
  636. "OpDecorate %3 SpecId 102\n"
  637. "%int = OpTypeInt 32 1\n"
  638. "%1 = OpSpecConstant %int 2147483647\n"
  639. "%2 = OpSpecConstant %int -1\n"
  640. "%3 = OpSpecConstant %int -42\n",
  641. },
  642. // 4. 64-bit uint type.
  643. {
  644. // code
  645. "OpDecorate %1 SpecId 100\n"
  646. "OpDecorate %2 SpecId 101\n"
  647. "%ulong = OpTypeInt 64 0\n"
  648. "%1 = OpSpecConstant %ulong 10\n"
  649. "%2 = OpSpecConstant %ulong 11\n",
  650. // default values
  651. SpecIdToValueBitPatternMap{{100, {0xFFFFFFFE, 0xFFFFFFFF}},
  652. {101, {0x100, 0x0}}},
  653. // expected
  654. "OpDecorate %1 SpecId 100\n"
  655. "OpDecorate %2 SpecId 101\n"
  656. "%ulong = OpTypeInt 64 0\n"
  657. "%1 = OpSpecConstant %ulong 18446744073709551614\n"
  658. "%2 = OpSpecConstant %ulong 256\n",
  659. },
  660. // 5. 32-bit float type.
  661. {
  662. // code
  663. "OpDecorate %1 SpecId 101\n"
  664. "OpDecorate %2 SpecId 102\n"
  665. "%float = OpTypeFloat 32\n"
  666. "%1 = OpSpecConstant %float 200\n"
  667. "%2 = OpSpecConstant %float 201\n",
  668. // default values
  669. SpecIdToValueBitPatternMap{{101, {0xffffffff}},
  670. {102, {0x40200000}}},
  671. // expected
  672. "OpDecorate %1 SpecId 101\n"
  673. "OpDecorate %2 SpecId 102\n"
  674. "%float = OpTypeFloat 32\n"
  675. "%1 = OpSpecConstant %float -0x1.fffffep+128\n"
  676. "%2 = OpSpecConstant %float 2.5\n",
  677. },
  678. // 6. 64-bit float type.
  679. {
  680. // code
  681. "OpDecorate %1 SpecId 201\n"
  682. "OpDecorate %2 SpecId 202\n"
  683. "%double = OpTypeFloat 64\n"
  684. "%1 = OpSpecConstant %double 3.14159265358979\n"
  685. "%2 = OpSpecConstant %double 0.14285\n",
  686. // default values
  687. SpecIdToValueBitPatternMap{{201, {0xffffffff, 0x7fffffff}},
  688. {202, {0x00000000, 0xc0404000}}},
  689. // expected
  690. "OpDecorate %1 SpecId 201\n"
  691. "OpDecorate %2 SpecId 202\n"
  692. "%double = OpTypeFloat 64\n"
  693. "%1 = OpSpecConstant %double 0x1.fffffffffffffp+1024\n"
  694. "%2 = OpSpecConstant %double -32.5\n",
  695. },
  696. // 7. SpecId not found, expect no modification.
  697. {
  698. // code
  699. "OpDecorate %1 SpecId 201\n"
  700. "%double = OpTypeFloat 64\n"
  701. "%1 = OpSpecConstant %double 3.14159265358979\n",
  702. // default values
  703. SpecIdToValueBitPatternMap{{8888, {0x0}}},
  704. // expected
  705. "OpDecorate %1 SpecId 201\n"
  706. "%double = OpTypeFloat 64\n"
  707. "%1 = OpSpecConstant %double 3.14159265358979\n",
  708. },
  709. // 8. Multiple types of spec constants.
  710. {
  711. // code
  712. "OpDecorate %1 SpecId 201\n"
  713. "OpDecorate %2 SpecId 202\n"
  714. "OpDecorate %3 SpecId 203\n"
  715. "%bool = OpTypeBool\n"
  716. "%int = OpTypeInt 32 1\n"
  717. "%double = OpTypeFloat 64\n"
  718. "%1 = OpSpecConstant %double 3.14159265358979\n"
  719. "%2 = OpSpecConstant %int 1024\n"
  720. "%3 = OpSpecConstantTrue %bool\n",
  721. // default values
  722. SpecIdToValueBitPatternMap{
  723. {201, {0xffffffff, 0x7fffffff}},
  724. {202, {0x00000800}},
  725. {203, {0x0}},
  726. },
  727. // expected
  728. "OpDecorate %1 SpecId 201\n"
  729. "OpDecorate %2 SpecId 202\n"
  730. "OpDecorate %3 SpecId 203\n"
  731. "%bool = OpTypeBool\n"
  732. "%int = OpTypeInt 32 1\n"
  733. "%double = OpTypeFloat 64\n"
  734. "%1 = OpSpecConstant %double 0x1.fffffffffffffp+1024\n"
  735. "%2 = OpSpecConstant %int 2048\n"
  736. "%3 = OpSpecConstantFalse %bool\n",
  737. },
  738. // 9. Ignore other decorations.
  739. {
  740. // code
  741. "OpDecorate %1 ArrayStride 4\n"
  742. "%int = OpTypeInt 32 1\n"
  743. "%1 = OpSpecConstant %int 100\n",
  744. // default values
  745. SpecIdToValueBitPatternMap{{4, {0x7fffffff}}},
  746. // expected
  747. "OpDecorate %1 ArrayStride 4\n"
  748. "%int = OpTypeInt 32 1\n"
  749. "%1 = OpSpecConstant %int 100\n",
  750. },
  751. // 10. Distinguish from other decorations.
  752. {
  753. // code
  754. "OpDecorate %1 SpecId 100\n"
  755. "OpDecorate %1 ArrayStride 4\n"
  756. "%int = OpTypeInt 32 1\n"
  757. "%1 = OpSpecConstant %int 100\n",
  758. // default values
  759. SpecIdToValueBitPatternMap{{4, {0x7fffffff}}, {100, {0xffffffff}}},
  760. // expected
  761. "OpDecorate %1 SpecId 100\n"
  762. "OpDecorate %1 ArrayStride 4\n"
  763. "%int = OpTypeInt 32 1\n"
  764. "%1 = OpSpecConstant %int -1\n",
  765. },
  766. // 11. Decorate through decoration group.
  767. {
  768. // code
  769. "OpDecorate %1 SpecId 100\n"
  770. "%1 = OpDecorationGroup\n"
  771. "OpGroupDecorate %1 %2\n"
  772. "%int = OpTypeInt 32 1\n"
  773. "%2 = OpSpecConstant %int 100\n",
  774. // default values
  775. SpecIdToValueBitPatternMap{{100, {0x7fffffff}}},
  776. // expected
  777. "OpDecorate %1 SpecId 100\n"
  778. "%1 = OpDecorationGroup\n"
  779. "OpGroupDecorate %1 %2\n"
  780. "%int = OpTypeInt 32 1\n"
  781. "%2 = OpSpecConstant %int 2147483647\n",
  782. },
  783. // 12. Ignore other decorations in decoration group.
  784. {
  785. // code
  786. "OpDecorate %1 ArrayStride 4\n"
  787. "%1 = OpDecorationGroup\n"
  788. "OpGroupDecorate %1 %2\n"
  789. "%int = OpTypeInt 32 1\n"
  790. "%2 = OpSpecConstant %int 100\n",
  791. // default values
  792. SpecIdToValueBitPatternMap{{4, {0x7fffffff}}},
  793. // expected
  794. "OpDecorate %1 ArrayStride 4\n"
  795. "%1 = OpDecorationGroup\n"
  796. "OpGroupDecorate %1 %2\n"
  797. "%int = OpTypeInt 32 1\n"
  798. "%2 = OpSpecConstant %int 100\n",
  799. },
  800. // 13. Distinguish from other decorations in decoration group.
  801. {
  802. // code
  803. "OpDecorate %1 SpecId 100\n"
  804. "OpDecorate %1 ArrayStride 4\n"
  805. "%1 = OpDecorationGroup\n"
  806. "OpGroupDecorate %1 %2\n"
  807. "%int = OpTypeInt 32 1\n"
  808. "%2 = OpSpecConstant %int 100\n",
  809. // default values
  810. SpecIdToValueBitPatternMap{{100, {0x7fffffff}}, {4, {0x00000001}}},
  811. // expected
  812. "OpDecorate %1 SpecId 100\n"
  813. "OpDecorate %1 ArrayStride 4\n"
  814. "%1 = OpDecorationGroup\n"
  815. "OpGroupDecorate %1 %2\n"
  816. "%int = OpTypeInt 32 1\n"
  817. "%2 = OpSpecConstant %int 2147483647\n",
  818. },
  819. // 14. Unchanged bool default value
  820. {
  821. // code
  822. "OpDecorate %1 SpecId 100\n"
  823. "OpDecorate %2 SpecId 101\n"
  824. "%bool = OpTypeBool\n"
  825. "%1 = OpSpecConstantTrue %bool\n"
  826. "%2 = OpSpecConstantFalse %bool\n",
  827. // default values
  828. SpecIdToValueBitPatternMap{{100, {0x1}}, {101, {0x0}}},
  829. // expected
  830. "OpDecorate %1 SpecId 100\n"
  831. "OpDecorate %2 SpecId 101\n"
  832. "%bool = OpTypeBool\n"
  833. "%1 = OpSpecConstantTrue %bool\n"
  834. "%2 = OpSpecConstantFalse %bool\n",
  835. },
  836. // 15. Unchanged int default values
  837. {
  838. // code
  839. "OpDecorate %1 SpecId 100\n"
  840. "OpDecorate %2 SpecId 101\n"
  841. "%int = OpTypeInt 32 1\n"
  842. "%ulong = OpTypeInt 64 0\n"
  843. "%1 = OpSpecConstant %int 10\n"
  844. "%2 = OpSpecConstant %ulong 11\n",
  845. // default values
  846. SpecIdToValueBitPatternMap{{100, {10}}, {101, {11, 0}}},
  847. // expected
  848. "OpDecorate %1 SpecId 100\n"
  849. "OpDecorate %2 SpecId 101\n"
  850. "%int = OpTypeInt 32 1\n"
  851. "%ulong = OpTypeInt 64 0\n"
  852. "%1 = OpSpecConstant %int 10\n"
  853. "%2 = OpSpecConstant %ulong 11\n",
  854. },
  855. // 16. Unchanged float default values
  856. {
  857. // code
  858. "OpDecorate %1 SpecId 201\n"
  859. "OpDecorate %2 SpecId 202\n"
  860. "%float = OpTypeFloat 32\n"
  861. "%double = OpTypeFloat 64\n"
  862. "%1 = OpSpecConstant %float 3.25\n"
  863. "%2 = OpSpecConstant %double 1.25\n",
  864. // default values
  865. SpecIdToValueBitPatternMap{{201, {0x40500000}},
  866. {202, {0x00000000, 0x3ff40000}}},
  867. // expected
  868. "OpDecorate %1 SpecId 201\n"
  869. "OpDecorate %2 SpecId 202\n"
  870. "%float = OpTypeFloat 32\n"
  871. "%double = OpTypeFloat 64\n"
  872. "%1 = OpSpecConstant %float 3.25\n"
  873. "%2 = OpSpecConstant %double 1.25\n",
  874. },
  875. // 17. OpGroupDecorate may have multiple target ids defined by the same
  876. // eligible spec constant
  877. {
  878. // code
  879. "OpDecorate %1 SpecId 100\n"
  880. "%1 = OpDecorationGroup\n"
  881. "OpGroupDecorate %1 %2 %2 %2\n"
  882. "%int = OpTypeInt 32 1\n"
  883. "%2 = OpSpecConstant %int 100\n",
  884. // default values
  885. SpecIdToValueBitPatternMap{{100, {0xffffffff}}},
  886. // expected
  887. "OpDecorate %1 SpecId 100\n"
  888. "%1 = OpDecorationGroup\n"
  889. "OpGroupDecorate %1 %2 %2 %2\n"
  890. "%int = OpTypeInt 32 1\n"
  891. "%2 = OpSpecConstant %int -1\n",
  892. },
  893. // 18. For Boolean type spec constants,if any word in the bit pattern
  894. // is not zero, it can be considered as a 'true', otherwise, it can be
  895. // considered as a 'false'.
  896. {
  897. // code
  898. "OpDecorate %1 SpecId 100\n"
  899. "OpDecorate %2 SpecId 101\n"
  900. "OpDecorate %3 SpecId 102\n"
  901. "%bool = OpTypeBool\n"
  902. "%1 = OpSpecConstantTrue %bool\n"
  903. "%2 = OpSpecConstantFalse %bool\n"
  904. "%3 = OpSpecConstantFalse %bool\n",
  905. // default values
  906. SpecIdToValueBitPatternMap{
  907. {100, {0x0, 0x0, 0x0, 0x0}},
  908. {101, {0x10101010}},
  909. {102, {0x0, 0x0, 0x0, 0x2}},
  910. },
  911. // expected
  912. "OpDecorate %1 SpecId 100\n"
  913. "OpDecorate %2 SpecId 101\n"
  914. "OpDecorate %3 SpecId 102\n"
  915. "%bool = OpTypeBool\n"
  916. "%1 = OpSpecConstantFalse %bool\n"
  917. "%2 = OpSpecConstantTrue %bool\n"
  918. "%3 = OpSpecConstantTrue %bool\n",
  919. },
  920. // 19. 16-bit signed int type.
  921. {
  922. // code
  923. "OpDecorate %1 SpecId 100\n"
  924. "OpDecorate %2 SpecId 101\n"
  925. "OpDecorate %3 SpecId 102\n"
  926. "%short = OpTypeInt 16 1\n"
  927. "%1 = OpSpecConstant %short 10\n"
  928. "%2 = OpSpecConstant %short 11\n"
  929. "%3 = OpSpecConstant %short 11\n",
  930. // default values
  931. SpecIdToValueBitPatternMap{
  932. {100, {32767}}, {101, {0xffff}}, {102, {0xffffffd6}}},
  933. // expected. These are sign-extended
  934. "OpDecorate %1 SpecId 100\n"
  935. "OpDecorate %2 SpecId 101\n"
  936. "OpDecorate %3 SpecId 102\n"
  937. "%short = OpTypeInt 16 1\n"
  938. "%1 = OpSpecConstant %short 32767\n"
  939. "%2 = OpSpecConstant %short -1\n"
  940. "%3 = OpSpecConstant %short -42\n",
  941. },
  942. // 20. 16-bit unsigned int type.
  943. {
  944. // code
  945. "OpDecorate %1 SpecId 100\n"
  946. "OpDecorate %2 SpecId 101\n"
  947. "OpDecorate %3 SpecId 102\n"
  948. "%ushort = OpTypeInt 16 0\n"
  949. "%1 = OpSpecConstant %ushort 10\n"
  950. "%2 = OpSpecConstant %ushort 11\n"
  951. "%3 = OpSpecConstant %ushort 11\n",
  952. // default values
  953. SpecIdToValueBitPatternMap{
  954. {100, {32767}}, {101, {0xffff}}, {102, {0xffffffd6}}},
  955. // expected. Upper bits are always zero.
  956. "OpDecorate %1 SpecId 100\n"
  957. "OpDecorate %2 SpecId 101\n"
  958. "OpDecorate %3 SpecId 102\n"
  959. "%ushort = OpTypeInt 16 0\n"
  960. "%1 = OpSpecConstant %ushort 32767\n"
  961. "%2 = OpSpecConstant %ushort 65535\n"
  962. "%3 = OpSpecConstant %ushort 65494\n",
  963. },
  964. // 21. 8-bit signed int type.
  965. {
  966. // code
  967. "OpDecorate %1 SpecId 100\n"
  968. "OpDecorate %2 SpecId 101\n"
  969. "OpDecorate %3 SpecId 102\n"
  970. "%char = OpTypeInt 8 1\n"
  971. "%1 = OpSpecConstant %char 10\n"
  972. "%2 = OpSpecConstant %char 11\n"
  973. "%3 = OpSpecConstant %char 11\n",
  974. // default values
  975. SpecIdToValueBitPatternMap{
  976. {100, {127}}, {101, {128}}, {102, {0xd6}}},
  977. // expected. These are sign extended
  978. "OpDecorate %1 SpecId 100\n"
  979. "OpDecorate %2 SpecId 101\n"
  980. "OpDecorate %3 SpecId 102\n"
  981. "%char = OpTypeInt 8 1\n"
  982. "%1 = OpSpecConstant %char 127\n"
  983. "%2 = OpSpecConstant %char -128\n"
  984. "%3 = OpSpecConstant %char -42\n",
  985. },
  986. // 22. 8-bit unsigned int type.
  987. {
  988. // code
  989. "OpDecorate %1 SpecId 100\n"
  990. "OpDecorate %2 SpecId 101\n"
  991. "OpDecorate %3 SpecId 102\n"
  992. "OpDecorate %4 SpecId 103\n"
  993. "%uchar = OpTypeInt 8 0\n"
  994. "%1 = OpSpecConstant %uchar 10\n"
  995. "%2 = OpSpecConstant %uchar 11\n"
  996. "%3 = OpSpecConstant %uchar 11\n"
  997. "%4 = OpSpecConstant %uchar 11\n",
  998. // default values
  999. SpecIdToValueBitPatternMap{
  1000. {100, {127}}, {101, {128}}, {102, {256}}, {103, {0xffffffd6}}},
  1001. // expected. Upper bits are always zero.
  1002. "OpDecorate %1 SpecId 100\n"
  1003. "OpDecorate %2 SpecId 101\n"
  1004. "OpDecorate %3 SpecId 102\n"
  1005. "OpDecorate %4 SpecId 103\n"
  1006. "%uchar = OpTypeInt 8 0\n"
  1007. "%1 = OpSpecConstant %uchar 127\n"
  1008. "%2 = OpSpecConstant %uchar 128\n"
  1009. "%3 = OpSpecConstant %uchar 0\n"
  1010. "%4 = OpSpecConstant %uchar 214\n",
  1011. },
  1012. }));
  1013. INSTANTIATE_TEST_SUITE_P(
  1014. InvalidCases, SetSpecConstantDefaultValueInBitPatternFormParamTest,
  1015. ::testing::ValuesIn(std::vector<
  1016. SetSpecConstantDefaultValueInBitPatternFormTestCase>{
  1017. // 0. Do not crash when decoration group is not used.
  1018. {
  1019. // code
  1020. "OpDecorate %1 SpecId 100\n"
  1021. "%1 = OpDecorationGroup\n"
  1022. "%int = OpTypeInt 32 1\n"
  1023. "%3 = OpSpecConstant %int 100\n",
  1024. // default values
  1025. SpecIdToValueBitPatternMap{{100, {0x7fffffff}}},
  1026. // expected
  1027. "OpDecorate %1 SpecId 100\n"
  1028. "%1 = OpDecorationGroup\n"
  1029. "%int = OpTypeInt 32 1\n"
  1030. "%3 = OpSpecConstant %int 100\n",
  1031. },
  1032. // 1. Do not crash when target does not exist.
  1033. {
  1034. // code
  1035. "OpDecorate %1 SpecId 100\n"
  1036. "%1 = OpDecorationGroup\n"
  1037. "%int = OpTypeInt 32 1\n",
  1038. // default values
  1039. SpecIdToValueBitPatternMap{{100, {0x7fffffff}}},
  1040. // expected
  1041. "OpDecorate %1 SpecId 100\n"
  1042. "%1 = OpDecorationGroup\n"
  1043. "%int = OpTypeInt 32 1\n",
  1044. },
  1045. // 2. Do nothing when SpecId decoration is not attached to a
  1046. // non-spec-constant instruction.
  1047. {
  1048. // code
  1049. "OpDecorate %1 SpecId 100\n"
  1050. "%1 = OpDecorationGroup\n"
  1051. "%int = OpTypeInt 32 1\n"
  1052. "%int_101 = OpConstant %int 101\n",
  1053. // default values
  1054. SpecIdToValueBitPatternMap{{100, {0x7fffffff}}},
  1055. // expected
  1056. "OpDecorate %1 SpecId 100\n"
  1057. "%1 = OpDecorationGroup\n"
  1058. "%int = OpTypeInt 32 1\n"
  1059. "%int_101 = OpConstant %int 101\n",
  1060. },
  1061. // 3. Do nothing when SpecId decoration is not attached to a
  1062. // OpSpecConstant{|True|False} instruction.
  1063. {
  1064. // code
  1065. "OpDecorate %1 SpecId 100\n"
  1066. "%int = OpTypeInt 32 1\n"
  1067. "%3 = OpSpecConstant %int 101\n"
  1068. "%1 = OpSpecConstantOp %int IAdd %3 %3\n",
  1069. // default values
  1070. SpecIdToValueBitPatternMap{{100, {0x7fffffff}}},
  1071. // expected
  1072. "OpDecorate %1 SpecId 100\n"
  1073. "%int = OpTypeInt 32 1\n"
  1074. "%3 = OpSpecConstant %int 101\n"
  1075. "%1 = OpSpecConstantOp %int IAdd %3 %3\n",
  1076. },
  1077. // 4. Do not crash and do nothing when SpecId decoration is applied to
  1078. // multiple spec constants.
  1079. {
  1080. // code
  1081. "OpDecorate %1 SpecId 100\n"
  1082. "%1 = OpDecorationGroup\n"
  1083. "OpGroupDecorate %1 %2 %3 %4\n"
  1084. "%int = OpTypeInt 32 1\n"
  1085. "%2 = OpSpecConstant %int 100\n"
  1086. "%3 = OpSpecConstant %int 200\n"
  1087. "%4 = OpSpecConstant %int 300\n",
  1088. // default values
  1089. SpecIdToValueBitPatternMap{{100, {0xffffffff}}},
  1090. // expected
  1091. "OpDecorate %1 SpecId 100\n"
  1092. "%1 = OpDecorationGroup\n"
  1093. "OpGroupDecorate %1 %2 %3 %4\n"
  1094. "%int = OpTypeInt 32 1\n"
  1095. "%2 = OpSpecConstant %int 100\n"
  1096. "%3 = OpSpecConstant %int 200\n"
  1097. "%4 = OpSpecConstant %int 300\n",
  1098. },
  1099. // 5. Do not crash and do nothing when SpecId decoration is attached to
  1100. // non-spec-constants (invalid case).
  1101. {
  1102. // code
  1103. "OpDecorate %1 SpecId 100\n"
  1104. "%1 = OpDecorationGroup\n"
  1105. "%2 = OpDecorationGroup\n"
  1106. "OpGroupDecorate %1 %2\n"
  1107. "%int = OpTypeInt 32 1\n"
  1108. "%int_100 = OpConstant %int 100\n",
  1109. // default values
  1110. SpecIdToValueBitPatternMap{{100, {0xffffffff}}},
  1111. // expected
  1112. "OpDecorate %1 SpecId 100\n"
  1113. "%1 = OpDecorationGroup\n"
  1114. "%2 = OpDecorationGroup\n"
  1115. "OpGroupDecorate %1 %2\n"
  1116. "%int = OpTypeInt 32 1\n"
  1117. "%int_100 = OpConstant %int 100\n",
  1118. },
  1119. // 6. Incompatible input bit pattern with the type. Nothing should be
  1120. // done in such a case.
  1121. {
  1122. // code
  1123. "OpDecorate %1 SpecId 100\n"
  1124. "OpDecorate %2 SpecId 101\n"
  1125. "OpDecorate %3 SpecId 102\n"
  1126. "%int = OpTypeInt 32 1\n"
  1127. "%ulong = OpTypeInt 64 0\n"
  1128. "%double = OpTypeFloat 64\n"
  1129. "%1 = OpSpecConstant %int 100\n"
  1130. "%2 = OpSpecConstant %ulong 200\n"
  1131. "%3 = OpSpecConstant %double 3.141592653\n",
  1132. // default values
  1133. SpecIdToValueBitPatternMap{
  1134. {100, {10, 0}}, {101, {11}}, {102, {0xffffffff}}},
  1135. // expected
  1136. "OpDecorate %1 SpecId 100\n"
  1137. "OpDecorate %2 SpecId 101\n"
  1138. "OpDecorate %3 SpecId 102\n"
  1139. "%int = OpTypeInt 32 1\n"
  1140. "%ulong = OpTypeInt 64 0\n"
  1141. "%double = OpTypeFloat 64\n"
  1142. "%1 = OpSpecConstant %int 100\n"
  1143. "%2 = OpSpecConstant %ulong 200\n"
  1144. "%3 = OpSpecConstant %double 3.141592653\n",
  1145. },
  1146. }));
  1147. } // namespace
  1148. } // namespace opt
  1149. } // namespace spvtools