eliminate_dead_const_test.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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 <algorithm>
  15. #include <cstdarg>
  16. #include <iostream>
  17. #include <sstream>
  18. #include <string>
  19. #include <unordered_set>
  20. #include <vector>
  21. #include "test/opt/assembly_builder.h"
  22. #include "test/opt/pass_fixture.h"
  23. #include "test/opt/pass_utils.h"
  24. namespace spvtools {
  25. namespace opt {
  26. namespace {
  27. using EliminateDeadConstantBasicTest = PassTest<::testing::Test>;
  28. TEST_F(EliminateDeadConstantBasicTest, BasicAllDeadConstants) {
  29. const std::vector<const char*> text = {
  30. // clang-format off
  31. "OpCapability Shader",
  32. "OpCapability Float64",
  33. "%1 = OpExtInstImport \"GLSL.std.450\"",
  34. "OpMemoryModel Logical GLSL450",
  35. "OpEntryPoint Vertex %main \"main\"",
  36. "OpName %main \"main\"",
  37. "%void = OpTypeVoid",
  38. "%4 = OpTypeFunction %void",
  39. "%bool = OpTypeBool",
  40. "%true = OpConstantTrue %bool",
  41. "%false = OpConstantFalse %bool",
  42. "%int = OpTypeInt 32 1",
  43. "%9 = OpConstant %int 1",
  44. "%uint = OpTypeInt 32 0",
  45. "%11 = OpConstant %uint 2",
  46. "%float = OpTypeFloat 32",
  47. "%13 = OpConstant %float 3.1415",
  48. "%double = OpTypeFloat 64",
  49. "%15 = OpConstant %double 3.14159265358979",
  50. "%main = OpFunction %void None %4",
  51. "%16 = OpLabel",
  52. "OpReturn",
  53. "OpFunctionEnd",
  54. // clang-format on
  55. };
  56. // None of the above constants is ever used, so all of them should be
  57. // eliminated.
  58. const char* const_decl_opcodes[] = {
  59. " OpConstantTrue ",
  60. " OpConstantFalse ",
  61. " OpConstant ",
  62. };
  63. // Skip lines that have any one of const_decl_opcodes.
  64. const std::string expected_disassembly =
  65. SelectiveJoin(text, [&const_decl_opcodes](const char* line) {
  66. return std::any_of(
  67. std::begin(const_decl_opcodes), std::end(const_decl_opcodes),
  68. [&line](const char* const_decl_op) {
  69. return std::string(line).find(const_decl_op) != std::string::npos;
  70. });
  71. });
  72. SinglePassRunAndCheck<EliminateDeadConstantPass>(
  73. JoinAllInsts(text), expected_disassembly, /* skip_nop = */ true);
  74. }
  75. TEST_F(EliminateDeadConstantBasicTest, BasicNoneDeadConstants) {
  76. const std::vector<const char*> text = {
  77. // clang-format off
  78. "OpCapability Shader",
  79. "OpCapability Float64",
  80. "%1 = OpExtInstImport \"GLSL.std.450\"",
  81. "OpMemoryModel Logical GLSL450",
  82. "OpEntryPoint Vertex %main \"main\"",
  83. "OpName %main \"main\"",
  84. "OpName %btv \"btv\"",
  85. "OpName %bfv \"bfv\"",
  86. "OpName %iv \"iv\"",
  87. "OpName %uv \"uv\"",
  88. "OpName %fv \"fv\"",
  89. "OpName %dv \"dv\"",
  90. "%void = OpTypeVoid",
  91. "%10 = OpTypeFunction %void",
  92. "%bool = OpTypeBool",
  93. "%_ptr_Function_bool = OpTypePointer Function %bool",
  94. "%true = OpConstantTrue %bool",
  95. "%false = OpConstantFalse %bool",
  96. "%int = OpTypeInt 32 1",
  97. "%_ptr_Function_int = OpTypePointer Function %int",
  98. "%int_1 = OpConstant %int 1",
  99. "%uint = OpTypeInt 32 0",
  100. "%_ptr_Function_uint = OpTypePointer Function %uint",
  101. "%uint_2 = OpConstant %uint 2",
  102. "%float = OpTypeFloat 32",
  103. "%_ptr_Function_float = OpTypePointer Function %float",
  104. "%float_3_1415 = OpConstant %float 3.1415",
  105. "%double = OpTypeFloat 64",
  106. "%_ptr_Function_double = OpTypePointer Function %double",
  107. "%double_3_14159265358979 = OpConstant %double 3.14159265358979",
  108. "%main = OpFunction %void None %10",
  109. "%27 = OpLabel",
  110. "%btv = OpVariable %_ptr_Function_bool Function",
  111. "%bfv = OpVariable %_ptr_Function_bool Function",
  112. "%iv = OpVariable %_ptr_Function_int Function",
  113. "%uv = OpVariable %_ptr_Function_uint Function",
  114. "%fv = OpVariable %_ptr_Function_float Function",
  115. "%dv = OpVariable %_ptr_Function_double Function",
  116. "OpStore %btv %true",
  117. "OpStore %bfv %false",
  118. "OpStore %iv %int_1",
  119. "OpStore %uv %uint_2",
  120. "OpStore %fv %float_3_1415",
  121. "OpStore %dv %double_3_14159265358979",
  122. "OpReturn",
  123. "OpFunctionEnd",
  124. // clang-format on
  125. };
  126. // All constants are used, so none of them should be eliminated.
  127. SinglePassRunAndCheck<EliminateDeadConstantPass>(
  128. JoinAllInsts(text), JoinAllInsts(text), /* skip_nop = */ true);
  129. }
  130. struct EliminateDeadConstantTestCase {
  131. // Type declarations and constants that should be kept.
  132. std::vector<std::string> used_consts;
  133. // Instructions that refer to constants, this is added to create uses for
  134. // some constants so they won't be treated as dead constants.
  135. std::vector<std::string> main_insts;
  136. // Dead constants that should be removed.
  137. std::vector<std::string> dead_consts;
  138. };
  139. // All types that are potentially required in EliminateDeadConstantTest.
  140. const std::vector<std::string> CommonTypes = {
  141. // clang-format off
  142. // scalar types
  143. "%bool = OpTypeBool",
  144. "%uint = OpTypeInt 32 0",
  145. "%int = OpTypeInt 32 1",
  146. "%float = OpTypeFloat 32",
  147. "%double = OpTypeFloat 64",
  148. // vector types
  149. "%v2bool = OpTypeVector %bool 2",
  150. "%v2uint = OpTypeVector %uint 2",
  151. "%v2int = OpTypeVector %int 2",
  152. "%v3int = OpTypeVector %int 3",
  153. "%v4int = OpTypeVector %int 4",
  154. "%v2float = OpTypeVector %float 2",
  155. "%v3float = OpTypeVector %float 3",
  156. "%v2double = OpTypeVector %double 2",
  157. // variable pointer types
  158. "%_pf_bool = OpTypePointer Function %bool",
  159. "%_pf_uint = OpTypePointer Function %uint",
  160. "%_pf_int = OpTypePointer Function %int",
  161. "%_pf_float = OpTypePointer Function %float",
  162. "%_pf_double = OpTypePointer Function %double",
  163. "%_pf_v2int = OpTypePointer Function %v2int",
  164. "%_pf_v3int = OpTypePointer Function %v3int",
  165. "%_pf_v2float = OpTypePointer Function %v2float",
  166. "%_pf_v3float = OpTypePointer Function %v3float",
  167. "%_pf_v2double = OpTypePointer Function %v2double",
  168. // struct types
  169. "%inner_struct = OpTypeStruct %bool %int %float %double",
  170. "%outer_struct = OpTypeStruct %inner_struct %int %double",
  171. "%flat_struct = OpTypeStruct %bool %int %float %double",
  172. // clang-format on
  173. };
  174. using EliminateDeadConstantTest =
  175. PassTest<::testing::TestWithParam<EliminateDeadConstantTestCase>>;
  176. TEST_P(EliminateDeadConstantTest, Custom) {
  177. auto& tc = GetParam();
  178. AssemblyBuilder builder;
  179. builder.AppendTypesConstantsGlobals(CommonTypes)
  180. .AppendTypesConstantsGlobals(tc.used_consts)
  181. .AppendInMain(tc.main_insts);
  182. const std::string expected = builder.GetCode();
  183. builder.AppendTypesConstantsGlobals(tc.dead_consts);
  184. const std::string assembly_with_dead_const = builder.GetCode();
  185. SinglePassRunAndCheck<EliminateDeadConstantPass>(
  186. assembly_with_dead_const, expected, /* skip_nop = */ true);
  187. }
  188. INSTANTIATE_TEST_SUITE_P(
  189. ScalarTypeConstants, EliminateDeadConstantTest,
  190. ::testing::ValuesIn(std::vector<EliminateDeadConstantTestCase>({
  191. // clang-format off
  192. // Scalar type constants, one dead constant and one used constant.
  193. {
  194. /* .used_consts = */
  195. {
  196. "%used_const_int = OpConstant %int 1",
  197. },
  198. /* .main_insts = */
  199. {
  200. "%int_var = OpVariable %_pf_int Function",
  201. "OpStore %int_var %used_const_int",
  202. },
  203. /* .dead_consts = */
  204. {
  205. "%dead_const_int = OpConstant %int 1",
  206. },
  207. },
  208. {
  209. /* .used_consts = */
  210. {
  211. "%used_const_uint = OpConstant %uint 1",
  212. },
  213. /* .main_insts = */
  214. {
  215. "%uint_var = OpVariable %_pf_uint Function",
  216. "OpStore %uint_var %used_const_uint",
  217. },
  218. /* .dead_consts = */
  219. {
  220. "%dead_const_uint = OpConstant %uint 1",
  221. },
  222. },
  223. {
  224. /* .used_consts = */
  225. {
  226. "%used_const_float = OpConstant %float 3.1415",
  227. },
  228. /* .main_insts = */
  229. {
  230. "%float_var = OpVariable %_pf_float Function",
  231. "OpStore %float_var %used_const_float",
  232. },
  233. /* .dead_consts = */
  234. {
  235. "%dead_const_float = OpConstant %float 3.1415",
  236. },
  237. },
  238. {
  239. /* .used_consts = */
  240. {
  241. "%used_const_double = OpConstant %double 3.141592653",
  242. },
  243. /* .main_insts = */
  244. {
  245. "%double_var = OpVariable %_pf_double Function",
  246. "OpStore %double_var %used_const_double",
  247. },
  248. /* .dead_consts = */
  249. {
  250. "%dead_const_double = OpConstant %double 3.141592653",
  251. },
  252. },
  253. // clang-format on
  254. })));
  255. INSTANTIATE_TEST_SUITE_P(
  256. VectorTypeConstants, EliminateDeadConstantTest,
  257. ::testing::ValuesIn(std::vector<EliminateDeadConstantTestCase>({
  258. // clang-format off
  259. // Tests eliminating dead constant type ivec2. One dead constant vector
  260. // and one used constant vector, each built from its own group of
  261. // scalar constants.
  262. {
  263. /* .used_consts = */
  264. {
  265. "%used_int_x = OpConstant %int 1",
  266. "%used_int_y = OpConstant %int 2",
  267. "%used_v2int = OpConstantComposite %v2int %used_int_x %used_int_y",
  268. },
  269. /* .main_insts = */
  270. {
  271. "%v2int_var = OpVariable %_pf_v2int Function",
  272. "OpStore %v2int_var %used_v2int",
  273. },
  274. /* .dead_consts = */
  275. {
  276. "%dead_int_x = OpConstant %int 1",
  277. "%dead_int_y = OpConstant %int 2",
  278. "%dead_v2int = OpConstantComposite %v2int %dead_int_x %dead_int_y",
  279. },
  280. },
  281. // Tests eliminating dead constant ivec2. One dead constant vector and
  282. // one used constant vector. But both built from a same group of
  283. // scalar constants.
  284. {
  285. /* .used_consts = */
  286. {
  287. "%used_int_x = OpConstant %int 1",
  288. "%used_int_y = OpConstant %int 2",
  289. "%used_int_z = OpConstant %int 3",
  290. "%used_v3int = OpConstantComposite %v3int %used_int_x %used_int_y %used_int_z",
  291. },
  292. /* .main_insts = */
  293. {
  294. "%v3int_var = OpVariable %_pf_v3int Function",
  295. "OpStore %v3int_var %used_v3int",
  296. },
  297. /* .dead_consts = */
  298. {
  299. "%dead_v3int = OpConstantComposite %v3int %used_int_x %used_int_y %used_int_z",
  300. },
  301. },
  302. // Tests eliminating dead cosntant vec2. One dead constant vector and
  303. // one used constant vector. Each built from its own group of scalar
  304. // constants.
  305. {
  306. /* .used_consts = */
  307. {
  308. "%used_float_x = OpConstant %float 3.1415",
  309. "%used_float_y = OpConstant %float 4.25",
  310. "%used_v2float = OpConstantComposite %v2float %used_float_x %used_float_y",
  311. },
  312. /* .main_insts = */
  313. {
  314. "%v2float_var = OpVariable %_pf_v2float Function",
  315. "OpStore %v2float_var %used_v2float",
  316. },
  317. /* .dead_consts = */
  318. {
  319. "%dead_float_x = OpConstant %float 3.1415",
  320. "%dead_float_y = OpConstant %float 4.25",
  321. "%dead_v2float = OpConstantComposite %v2float %dead_float_x %dead_float_y",
  322. },
  323. },
  324. // Tests eliminating dead cosntant vec2. One dead constant vector and
  325. // one used constant vector. Both built from a same group of scalar
  326. // constants.
  327. {
  328. /* .used_consts = */
  329. {
  330. "%used_float_x = OpConstant %float 3.1415",
  331. "%used_float_y = OpConstant %float 4.25",
  332. "%used_float_z = OpConstant %float 4.75",
  333. "%used_v3float = OpConstantComposite %v3float %used_float_x %used_float_y %used_float_z",
  334. },
  335. /* .main_insts = */
  336. {
  337. "%v3float_var = OpVariable %_pf_v3float Function",
  338. "OpStore %v3float_var %used_v3float",
  339. },
  340. /* .dead_consts = */
  341. {
  342. "%dead_v3float = OpConstantComposite %v3float %used_float_x %used_float_y %used_float_z",
  343. },
  344. },
  345. // clang-format on
  346. })));
  347. INSTANTIATE_TEST_SUITE_P(
  348. StructTypeConstants, EliminateDeadConstantTest,
  349. ::testing::ValuesIn(std::vector<EliminateDeadConstantTestCase>({
  350. // clang-format off
  351. // A plain struct type dead constants. All of its components are dead
  352. // constants too.
  353. {
  354. /* .used_consts = */ {},
  355. /* .main_insts = */ {},
  356. /* .dead_consts = */
  357. {
  358. "%dead_bool = OpConstantTrue %bool",
  359. "%dead_int = OpConstant %int 1",
  360. "%dead_float = OpConstant %float 2.5",
  361. "%dead_double = OpConstant %double 3.14159265358979",
  362. "%dead_struct = OpConstantComposite %flat_struct %dead_bool %dead_int %dead_float %dead_double",
  363. },
  364. },
  365. // A plain struct type dead constants. Some of its components are dead
  366. // constants while others are not.
  367. {
  368. /* .used_consts = */
  369. {
  370. "%used_int = OpConstant %int 1",
  371. "%used_double = OpConstant %double 3.14159265358979",
  372. },
  373. /* .main_insts = */
  374. {
  375. "%int_var = OpVariable %_pf_int Function",
  376. "OpStore %int_var %used_int",
  377. "%double_var = OpVariable %_pf_double Function",
  378. "OpStore %double_var %used_double",
  379. },
  380. /* .dead_consts = */
  381. {
  382. "%dead_bool = OpConstantTrue %bool",
  383. "%dead_float = OpConstant %float 2.5",
  384. "%dead_struct = OpConstantComposite %flat_struct %dead_bool %used_int %dead_float %used_double",
  385. },
  386. },
  387. // A nesting struct type dead constants. All components of both outer
  388. // and inner structs are dead and should be removed after dead constant
  389. // elimination.
  390. {
  391. /* .used_consts = */ {},
  392. /* .main_insts = */ {},
  393. /* .dead_consts = */
  394. {
  395. "%dead_bool = OpConstantTrue %bool",
  396. "%dead_int = OpConstant %int 1",
  397. "%dead_float = OpConstant %float 2.5",
  398. "%dead_double = OpConstant %double 3.1415926535",
  399. "%dead_inner_struct = OpConstantComposite %inner_struct %dead_bool %dead_int %dead_float %dead_double",
  400. "%dead_int2 = OpConstant %int 2",
  401. "%dead_double2 = OpConstant %double 1.428571428514",
  402. "%dead_outer_struct = OpConstantComposite %outer_struct %dead_inner_struct %dead_int2 %dead_double2",
  403. },
  404. },
  405. // A nesting struct type dead constants. Some of its components are
  406. // dead constants while others are not.
  407. {
  408. /* .used_consts = */
  409. {
  410. "%used_int = OpConstant %int 1",
  411. "%used_double = OpConstant %double 3.14159265358979",
  412. },
  413. /* .main_insts = */
  414. {
  415. "%int_var = OpVariable %_pf_int Function",
  416. "OpStore %int_var %used_int",
  417. "%double_var = OpVariable %_pf_double Function",
  418. "OpStore %double_var %used_double",
  419. },
  420. /* .dead_consts = */
  421. {
  422. "%dead_bool = OpConstantTrue %bool",
  423. "%dead_float = OpConstant %float 2.5",
  424. "%dead_inner_struct = OpConstantComposite %inner_struct %dead_bool %used_int %dead_float %used_double",
  425. "%dead_int = OpConstant %int 2",
  426. "%dead_outer_struct = OpConstantComposite %outer_struct %dead_inner_struct %dead_int %used_double",
  427. },
  428. },
  429. // A nesting struct case. The inner struct is used while the outer struct is not
  430. {
  431. /* .used_const = */
  432. {
  433. "%used_bool = OpConstantTrue %bool",
  434. "%used_int = OpConstant %int 1",
  435. "%used_float = OpConstant %float 1.25",
  436. "%used_double = OpConstant %double 1.23456789012345",
  437. "%used_inner_struct = OpConstantComposite %inner_struct %used_bool %used_int %used_float %used_double",
  438. },
  439. /* .main_insts = */
  440. {
  441. "%bool_var = OpVariable %_pf_bool Function",
  442. "%bool_from_inner_struct = OpCompositeExtract %bool %used_inner_struct 0",
  443. "OpStore %bool_var %bool_from_inner_struct",
  444. },
  445. /* .dead_consts = */
  446. {
  447. "%dead_int = OpConstant %int 2",
  448. "%dead_outer_struct = OpConstantComposite %outer_struct %used_inner_struct %dead_int %used_double"
  449. },
  450. },
  451. // A nesting struct case. The outer struct is used, so the inner struct should not
  452. // be removed even though it is not used anywhere.
  453. {
  454. /* .used_const = */
  455. {
  456. "%used_bool = OpConstantTrue %bool",
  457. "%used_int = OpConstant %int 1",
  458. "%used_float = OpConstant %float 1.25",
  459. "%used_double = OpConstant %double 1.23456789012345",
  460. "%used_inner_struct = OpConstantComposite %inner_struct %used_bool %used_int %used_float %used_double",
  461. "%used_outer_struct = OpConstantComposite %outer_struct %used_inner_struct %used_int %used_double"
  462. },
  463. /* .main_insts = */
  464. {
  465. "%int_var = OpVariable %_pf_int Function",
  466. "%int_from_outer_struct = OpCompositeExtract %int %used_outer_struct 1",
  467. "OpStore %int_var %int_from_outer_struct",
  468. },
  469. /* .dead_consts = */ {},
  470. },
  471. // clang-format on
  472. })));
  473. INSTANTIATE_TEST_SUITE_P(
  474. ScalarTypeSpecConstants, EliminateDeadConstantTest,
  475. ::testing::ValuesIn(std::vector<EliminateDeadConstantTestCase>({
  476. // clang-format off
  477. // All scalar type spec constants.
  478. {
  479. /* .used_consts = */
  480. {
  481. "%used_bool = OpSpecConstantTrue %bool",
  482. "%used_uint = OpSpecConstant %uint 2",
  483. "%used_int = OpSpecConstant %int 2",
  484. "%used_float = OpSpecConstant %float 2.5",
  485. "%used_double = OpSpecConstant %double 1.42857142851",
  486. },
  487. /* .main_insts = */
  488. {
  489. "%bool_var = OpVariable %_pf_bool Function",
  490. "%uint_var = OpVariable %_pf_uint Function",
  491. "%int_var = OpVariable %_pf_int Function",
  492. "%float_var = OpVariable %_pf_float Function",
  493. "%double_var = OpVariable %_pf_double Function",
  494. "OpStore %bool_var %used_bool", "OpStore %uint_var %used_uint",
  495. "OpStore %int_var %used_int", "OpStore %float_var %used_float",
  496. "OpStore %double_var %used_double",
  497. },
  498. /* .dead_consts = */
  499. {
  500. "%dead_bool = OpSpecConstantTrue %bool",
  501. "%dead_uint = OpSpecConstant %uint 2",
  502. "%dead_int = OpSpecConstant %int 2",
  503. "%dead_float = OpSpecConstant %float 2.5",
  504. "%dead_double = OpSpecConstant %double 1.42857142851",
  505. },
  506. },
  507. // clang-format on
  508. })));
  509. INSTANTIATE_TEST_SUITE_P(
  510. VectorTypeSpecConstants, EliminateDeadConstantTest,
  511. ::testing::ValuesIn(std::vector<EliminateDeadConstantTestCase>({
  512. // clang-format off
  513. // Bool vector type spec constants. One vector has all component dead,
  514. // another vector has one dead boolean and one used boolean.
  515. {
  516. /* .used_consts = */
  517. {
  518. "%used_bool = OpSpecConstantTrue %bool",
  519. },
  520. /* .main_insts = */
  521. {
  522. "%bool_var = OpVariable %_pf_bool Function",
  523. "OpStore %bool_var %used_bool",
  524. },
  525. /* .dead_consts = */
  526. {
  527. "%dead_bool = OpSpecConstantFalse %bool",
  528. "%dead_bool_vec1 = OpSpecConstantComposite %v2bool %dead_bool %dead_bool",
  529. "%dead_bool_vec2 = OpSpecConstantComposite %v2bool %dead_bool %used_bool",
  530. },
  531. },
  532. // Uint vector type spec constants. One vector has all component dead,
  533. // another vector has one dead unsigend integer and one used unsigned
  534. // integer.
  535. {
  536. /* .used_consts = */
  537. {
  538. "%used_uint = OpSpecConstant %uint 3",
  539. },
  540. /* .main_insts = */
  541. {
  542. "%uint_var = OpVariable %_pf_uint Function",
  543. "OpStore %uint_var %used_uint",
  544. },
  545. /* .dead_consts = */
  546. {
  547. "%dead_uint = OpSpecConstant %uint 1",
  548. "%dead_uint_vec1 = OpSpecConstantComposite %v2uint %dead_uint %dead_uint",
  549. "%dead_uint_vec2 = OpSpecConstantComposite %v2uint %dead_uint %used_uint",
  550. },
  551. },
  552. // Int vector type spec constants. One vector has all component dead,
  553. // another vector has one dead integer and one used integer.
  554. {
  555. /* .used_consts = */
  556. {
  557. "%used_int = OpSpecConstant %int 3",
  558. },
  559. /* .main_insts = */
  560. {
  561. "%int_var = OpVariable %_pf_int Function",
  562. "OpStore %int_var %used_int",
  563. },
  564. /* .dead_consts = */
  565. {
  566. "%dead_int = OpSpecConstant %int 1",
  567. "%dead_int_vec1 = OpSpecConstantComposite %v2int %dead_int %dead_int",
  568. "%dead_int_vec2 = OpSpecConstantComposite %v2int %dead_int %used_int",
  569. },
  570. },
  571. // Int vector type spec constants built with both spec constants and
  572. // front-end constants.
  573. {
  574. /* .used_consts = */
  575. {
  576. "%used_spec_int = OpSpecConstant %int 3",
  577. "%used_front_end_int = OpConstant %int 3",
  578. },
  579. /* .main_insts = */
  580. {
  581. "%int_var1 = OpVariable %_pf_int Function",
  582. "OpStore %int_var1 %used_spec_int",
  583. "%int_var2 = OpVariable %_pf_int Function",
  584. "OpStore %int_var2 %used_front_end_int",
  585. },
  586. /* .dead_consts = */
  587. {
  588. "%dead_spec_int = OpSpecConstant %int 1",
  589. "%dead_front_end_int = OpConstant %int 1",
  590. // Dead front-end and dead spec constants
  591. "%dead_int_vec1 = OpSpecConstantComposite %v2int %dead_spec_int %dead_front_end_int",
  592. // Used front-end and dead spec constants
  593. "%dead_int_vec2 = OpSpecConstantComposite %v2int %dead_spec_int %used_front_end_int",
  594. // Dead front-end and used spec constants
  595. "%dead_int_vec3 = OpSpecConstantComposite %v2int %dead_front_end_int %used_spec_int",
  596. },
  597. },
  598. // clang-format on
  599. })));
  600. INSTANTIATE_TEST_SUITE_P(
  601. SpecConstantOp, EliminateDeadConstantTest,
  602. ::testing::ValuesIn(std::vector<EliminateDeadConstantTestCase>({
  603. // clang-format off
  604. // Cast operations: uint <-> int <-> bool
  605. {
  606. /* .used_consts = */ {},
  607. /* .main_insts = */ {},
  608. /* .dead_consts = */
  609. {
  610. // Assistant constants, only used in dead spec constant
  611. // operations.
  612. "%signed_zero = OpConstant %int 0",
  613. "%signed_zero_vec = OpConstantComposite %v2int %signed_zero %signed_zero",
  614. "%unsigned_zero = OpConstant %uint 0",
  615. "%unsigned_zero_vec = OpConstantComposite %v2uint %unsigned_zero %unsigned_zero",
  616. "%signed_one = OpConstant %int 1",
  617. "%signed_one_vec = OpConstantComposite %v2int %signed_one %signed_one",
  618. "%unsigned_one = OpConstant %uint 1",
  619. "%unsigned_one_vec = OpConstantComposite %v2uint %unsigned_one %unsigned_one",
  620. // Spec constants that support casting to each other.
  621. "%dead_bool = OpSpecConstantTrue %bool",
  622. "%dead_uint = OpSpecConstant %uint 1",
  623. "%dead_int = OpSpecConstant %int 2",
  624. "%dead_bool_vec = OpSpecConstantComposite %v2bool %dead_bool %dead_bool",
  625. "%dead_uint_vec = OpSpecConstantComposite %v2uint %dead_uint %dead_uint",
  626. "%dead_int_vec = OpSpecConstantComposite %v2int %dead_int %dead_int",
  627. // Scalar cast to boolean spec constant.
  628. "%int_to_bool = OpSpecConstantOp %bool INotEqual %dead_int %signed_zero",
  629. "%uint_to_bool = OpSpecConstantOp %bool INotEqual %dead_uint %unsigned_zero",
  630. // Vector cast to boolean spec constant.
  631. "%int_to_bool_vec = OpSpecConstantOp %v2bool INotEqual %dead_int_vec %signed_zero_vec",
  632. "%uint_to_bool_vec = OpSpecConstantOp %v2bool INotEqual %dead_uint_vec %unsigned_zero_vec",
  633. // Scalar cast to int spec constant.
  634. "%bool_to_int = OpSpecConstantOp %int Select %dead_bool %signed_one %signed_zero",
  635. "%uint_to_int = OpSpecConstantOp %uint IAdd %dead_uint %unsigned_zero",
  636. // Vector cast to int spec constant.
  637. "%bool_to_int_vec = OpSpecConstantOp %v2int Select %dead_bool_vec %signed_one_vec %signed_zero_vec",
  638. "%uint_to_int_vec = OpSpecConstantOp %v2uint IAdd %dead_uint_vec %unsigned_zero_vec",
  639. // Scalar cast to uint spec constant.
  640. "%bool_to_uint = OpSpecConstantOp %uint Select %dead_bool %unsigned_one %unsigned_zero",
  641. "%int_to_uint_vec = OpSpecConstantOp %uint IAdd %dead_int %signed_zero",
  642. // Vector cast to uint spec constant.
  643. "%bool_to_uint_vec = OpSpecConstantOp %v2uint Select %dead_bool_vec %unsigned_one_vec %unsigned_zero_vec",
  644. "%int_to_uint = OpSpecConstantOp %v2uint IAdd %dead_int_vec %signed_zero_vec",
  645. },
  646. },
  647. // Add, sub, mul, div, rem.
  648. {
  649. /* .used_consts = */ {},
  650. /* .main_insts = */ {},
  651. /* .dead_consts = */
  652. {
  653. "%dead_spec_int_a = OpSpecConstant %int 1",
  654. "%dead_spec_int_a_vec = OpSpecConstantComposite %v2int %dead_spec_int_a %dead_spec_int_a",
  655. "%dead_spec_int_b = OpSpecConstant %int 2",
  656. "%dead_spec_int_b_vec = OpSpecConstantComposite %v2int %dead_spec_int_b %dead_spec_int_b",
  657. "%dead_const_int_c = OpConstant %int 3",
  658. "%dead_const_int_c_vec = OpConstantComposite %v2int %dead_const_int_c %dead_const_int_c",
  659. // Add
  660. "%add_a_b = OpSpecConstantOp %int IAdd %dead_spec_int_a %dead_spec_int_b",
  661. "%add_a_b_vec = OpSpecConstantOp %v2int IAdd %dead_spec_int_a_vec %dead_spec_int_b_vec",
  662. // Sub
  663. "%sub_a_b = OpSpecConstantOp %int ISub %dead_spec_int_a %dead_spec_int_b",
  664. "%sub_a_b_vec = OpSpecConstantOp %v2int ISub %dead_spec_int_a_vec %dead_spec_int_b_vec",
  665. // Mul
  666. "%mul_a_b = OpSpecConstantOp %int IMul %dead_spec_int_a %dead_spec_int_b",
  667. "%mul_a_b_vec = OpSpecConstantOp %v2int IMul %dead_spec_int_a_vec %dead_spec_int_b_vec",
  668. // Div
  669. "%div_a_b = OpSpecConstantOp %int SDiv %dead_spec_int_a %dead_spec_int_b",
  670. "%div_a_b_vec = OpSpecConstantOp %v2int SDiv %dead_spec_int_a_vec %dead_spec_int_b_vec",
  671. // Bitwise Xor
  672. "%xor_a_b = OpSpecConstantOp %int BitwiseXor %dead_spec_int_a %dead_spec_int_b",
  673. "%xor_a_b_vec = OpSpecConstantOp %v2int BitwiseXor %dead_spec_int_a_vec %dead_spec_int_b_vec",
  674. // Scalar Comparison
  675. "%less_a_b = OpSpecConstantOp %bool SLessThan %dead_spec_int_a %dead_spec_int_b",
  676. },
  677. },
  678. // Vectors without used swizzles should be removed.
  679. {
  680. /* .used_consts = */
  681. {
  682. "%used_int = OpConstant %int 3",
  683. },
  684. /* .main_insts = */
  685. {
  686. "%int_var = OpVariable %_pf_int Function",
  687. "OpStore %int_var %used_int",
  688. },
  689. /* .dead_consts = */
  690. {
  691. "%dead_int = OpConstant %int 3",
  692. "%dead_spec_int_a = OpSpecConstant %int 1",
  693. "%vec_a = OpSpecConstantComposite %v4int %dead_spec_int_a %dead_spec_int_a %dead_int %dead_int",
  694. "%dead_spec_int_b = OpSpecConstant %int 2",
  695. "%vec_b = OpSpecConstantComposite %v4int %dead_spec_int_b %dead_spec_int_b %used_int %used_int",
  696. // Extract scalar
  697. "%a_x = OpSpecConstantOp %int CompositeExtract %vec_a 0",
  698. "%b_x = OpSpecConstantOp %int CompositeExtract %vec_b 0",
  699. // Extract vector
  700. "%a_xy = OpSpecConstantOp %v2int VectorShuffle %vec_a %vec_a 0 1",
  701. "%b_xy = OpSpecConstantOp %v2int VectorShuffle %vec_b %vec_b 0 1",
  702. },
  703. },
  704. // Vectors with used swizzles should not be removed.
  705. {
  706. /* .used_consts = */
  707. {
  708. "%used_int = OpConstant %int 3",
  709. "%used_spec_int_a = OpSpecConstant %int 1",
  710. "%used_spec_int_b = OpSpecConstant %int 2",
  711. // Create vectors
  712. "%vec_a = OpSpecConstantComposite %v4int %used_spec_int_a %used_spec_int_a %used_int %used_int",
  713. "%vec_b = OpSpecConstantComposite %v4int %used_spec_int_b %used_spec_int_b %used_int %used_int",
  714. // Extract vector
  715. "%a_xy = OpSpecConstantOp %v2int VectorShuffle %vec_a %vec_a 0 1",
  716. "%b_xy = OpSpecConstantOp %v2int VectorShuffle %vec_b %vec_b 0 1",
  717. },
  718. /* .main_insts = */
  719. {
  720. "%v2int_var_a = OpVariable %_pf_v2int Function",
  721. "%v2int_var_b = OpVariable %_pf_v2int Function",
  722. "OpStore %v2int_var_a %a_xy",
  723. "OpStore %v2int_var_b %b_xy",
  724. },
  725. /* .dead_consts = */ {},
  726. },
  727. // clang-format on
  728. })));
  729. INSTANTIATE_TEST_SUITE_P(
  730. LongDefUseChain, EliminateDeadConstantTest,
  731. ::testing::ValuesIn(std::vector<EliminateDeadConstantTestCase>({
  732. // clang-format off
  733. // Long Def-Use chain with binary operations.
  734. {
  735. /* .used_consts = */
  736. {
  737. "%array_size = OpConstant %int 4",
  738. "%type_arr_int_4 = OpTypeArray %int %array_size",
  739. "%used_int_0 = OpConstant %int 100",
  740. "%used_int_1 = OpConstant %int 1",
  741. "%used_int_2 = OpSpecConstantOp %int IAdd %used_int_0 %used_int_1",
  742. "%used_int_3 = OpSpecConstantOp %int ISub %used_int_0 %used_int_2",
  743. "%used_int_4 = OpSpecConstantOp %int IAdd %used_int_0 %used_int_3",
  744. "%used_int_5 = OpSpecConstantOp %int ISub %used_int_0 %used_int_4",
  745. "%used_int_6 = OpSpecConstantOp %int IAdd %used_int_0 %used_int_5",
  746. "%used_int_7 = OpSpecConstantOp %int ISub %used_int_0 %used_int_6",
  747. "%used_int_8 = OpSpecConstantOp %int IAdd %used_int_0 %used_int_7",
  748. "%used_int_9 = OpSpecConstantOp %int ISub %used_int_0 %used_int_8",
  749. "%used_int_10 = OpSpecConstantOp %int IAdd %used_int_0 %used_int_9",
  750. "%used_int_11 = OpSpecConstantOp %int ISub %used_int_0 %used_int_10",
  751. "%used_int_12 = OpSpecConstantOp %int IAdd %used_int_0 %used_int_11",
  752. "%used_int_13 = OpSpecConstantOp %int ISub %used_int_0 %used_int_12",
  753. "%used_int_14 = OpSpecConstantOp %int IAdd %used_int_0 %used_int_13",
  754. "%used_int_15 = OpSpecConstantOp %int ISub %used_int_0 %used_int_14",
  755. "%used_int_16 = OpSpecConstantOp %int ISub %used_int_0 %used_int_15",
  756. "%used_int_17 = OpSpecConstantOp %int IAdd %used_int_0 %used_int_16",
  757. "%used_int_18 = OpSpecConstantOp %int ISub %used_int_0 %used_int_17",
  758. "%used_int_19 = OpSpecConstantOp %int IAdd %used_int_0 %used_int_18",
  759. "%used_int_20 = OpSpecConstantOp %int ISub %used_int_0 %used_int_19",
  760. "%used_vec_a = OpSpecConstantComposite %v2int %used_int_18 %used_int_19",
  761. "%used_vec_b = OpSpecConstantOp %v2int IMul %used_vec_a %used_vec_a",
  762. "%used_int_21 = OpSpecConstantOp %int CompositeExtract %used_vec_b 0",
  763. "%used_array = OpConstantComposite %type_arr_int_4 %used_int_20 %used_int_20 %used_int_21 %used_int_21",
  764. },
  765. /* .main_insts = */
  766. {
  767. "%int_var = OpVariable %_pf_int Function",
  768. "%used_array_2 = OpCompositeExtract %int %used_array 2",
  769. "OpStore %int_var %used_array_2",
  770. },
  771. /* .dead_consts = */
  772. {
  773. "%dead_int_1 = OpConstant %int 2",
  774. "%dead_int_2 = OpSpecConstantOp %int IAdd %used_int_0 %dead_int_1",
  775. "%dead_int_3 = OpSpecConstantOp %int ISub %used_int_0 %dead_int_2",
  776. "%dead_int_4 = OpSpecConstantOp %int IAdd %used_int_0 %dead_int_3",
  777. "%dead_int_5 = OpSpecConstantOp %int ISub %used_int_0 %dead_int_4",
  778. "%dead_int_6 = OpSpecConstantOp %int IAdd %used_int_0 %dead_int_5",
  779. "%dead_int_7 = OpSpecConstantOp %int ISub %used_int_0 %dead_int_6",
  780. "%dead_int_8 = OpSpecConstantOp %int IAdd %used_int_0 %dead_int_7",
  781. "%dead_int_9 = OpSpecConstantOp %int ISub %used_int_0 %dead_int_8",
  782. "%dead_int_10 = OpSpecConstantOp %int IAdd %used_int_0 %dead_int_9",
  783. "%dead_int_11 = OpSpecConstantOp %int ISub %used_int_0 %dead_int_10",
  784. "%dead_int_12 = OpSpecConstantOp %int IAdd %used_int_0 %dead_int_11",
  785. "%dead_int_13 = OpSpecConstantOp %int ISub %used_int_0 %dead_int_12",
  786. "%dead_int_14 = OpSpecConstantOp %int IAdd %used_int_0 %dead_int_13",
  787. "%dead_int_15 = OpSpecConstantOp %int ISub %used_int_0 %dead_int_14",
  788. "%dead_int_16 = OpSpecConstantOp %int ISub %used_int_0 %dead_int_15",
  789. "%dead_int_17 = OpSpecConstantOp %int IAdd %used_int_0 %dead_int_16",
  790. "%dead_int_18 = OpSpecConstantOp %int ISub %used_int_0 %dead_int_17",
  791. "%dead_int_19 = OpSpecConstantOp %int IAdd %used_int_0 %dead_int_18",
  792. "%dead_int_20 = OpSpecConstantOp %int ISub %used_int_0 %dead_int_19",
  793. "%dead_vec_a = OpSpecConstantComposite %v2int %dead_int_18 %dead_int_19",
  794. "%dead_vec_b = OpSpecConstantOp %v2int IMul %dead_vec_a %dead_vec_a",
  795. "%dead_int_21 = OpSpecConstantOp %int CompositeExtract %dead_vec_b 0",
  796. "%dead_array = OpConstantComposite %type_arr_int_4 %dead_int_20 %used_int_20 %dead_int_19 %used_int_19",
  797. },
  798. },
  799. // Long Def-Use chain with swizzle
  800. // clang-format on
  801. })));
  802. } // namespace
  803. } // namespace opt
  804. } // namespace spvtools