2
0

register_liveness.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. // Copyright (c) 2018 Google LLC.
  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 <memory>
  15. #include <unordered_set>
  16. #include <vector>
  17. #include "gmock/gmock.h"
  18. #include "source/opt/register_pressure.h"
  19. #include "test/opt/assembly_builder.h"
  20. #include "test/opt/function_utils.h"
  21. #include "test/opt/pass_fixture.h"
  22. #include "test/opt/pass_utils.h"
  23. namespace spvtools {
  24. namespace opt {
  25. namespace {
  26. using ::testing::UnorderedElementsAre;
  27. using PassClassTest = PassTest<::testing::Test>;
  28. void CompareSets(const std::unordered_set<Instruction*>& computed,
  29. const std::unordered_set<uint32_t>& expected) {
  30. for (Instruction* insn : computed) {
  31. EXPECT_TRUE(expected.count(insn->result_id()))
  32. << "Unexpected instruction in live set: " << *insn;
  33. }
  34. EXPECT_EQ(computed.size(), expected.size());
  35. }
  36. /*
  37. Generated from the following GLSL
  38. #version 330
  39. in vec4 BaseColor;
  40. flat in int Count;
  41. void main()
  42. {
  43. vec4 color = BaseColor;
  44. vec4 acc;
  45. if (Count == 0) {
  46. acc = color;
  47. }
  48. else {
  49. acc = color + vec4(0,1,2,0);
  50. }
  51. gl_FragColor = acc + color;
  52. }
  53. */
  54. TEST_F(PassClassTest, LivenessWithIf) {
  55. const std::string text = R"(
  56. OpCapability Shader
  57. %1 = OpExtInstImport "GLSL.std.450"
  58. OpMemoryModel Logical GLSL450
  59. OpEntryPoint Fragment %4 "main" %11 %15 %32
  60. OpExecutionMode %4 OriginLowerLeft
  61. OpSource GLSL 330
  62. OpName %4 "main"
  63. OpName %11 "BaseColor"
  64. OpName %15 "Count"
  65. OpName %32 "gl_FragColor"
  66. OpDecorate %11 Location 0
  67. OpDecorate %15 Flat
  68. OpDecorate %15 Location 0
  69. OpDecorate %32 Location 0
  70. %2 = OpTypeVoid
  71. %3 = OpTypeFunction %2
  72. %6 = OpTypeFloat 32
  73. %7 = OpTypeVector %6 4
  74. %10 = OpTypePointer Input %7
  75. %11 = OpVariable %10 Input
  76. %13 = OpTypeInt 32 1
  77. %14 = OpTypePointer Input %13
  78. %15 = OpVariable %14 Input
  79. %17 = OpConstant %13 0
  80. %18 = OpTypeBool
  81. %26 = OpConstant %6 0
  82. %27 = OpConstant %6 1
  83. %28 = OpConstant %6 2
  84. %29 = OpConstantComposite %7 %26 %27 %28 %26
  85. %31 = OpTypePointer Output %7
  86. %32 = OpVariable %31 Output
  87. %4 = OpFunction %2 None %3
  88. %5 = OpLabel
  89. %12 = OpLoad %7 %11
  90. %16 = OpLoad %13 %15
  91. %19 = OpIEqual %18 %16 %17
  92. OpSelectionMerge %21 None
  93. OpBranchConditional %19 %20 %24
  94. %20 = OpLabel
  95. OpBranch %21
  96. %24 = OpLabel
  97. %30 = OpFAdd %7 %12 %29
  98. OpBranch %21
  99. %21 = OpLabel
  100. %36 = OpPhi %7 %12 %20 %30 %24
  101. %35 = OpFAdd %7 %36 %12
  102. OpStore %32 %35
  103. OpReturn
  104. OpFunctionEnd
  105. )";
  106. std::unique_ptr<IRContext> context =
  107. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  108. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  109. Module* module = context->module();
  110. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  111. << text << std::endl;
  112. Function* f = &*module->begin();
  113. LivenessAnalysis* liveness_analysis = context->GetLivenessAnalysis();
  114. const RegisterLiveness* register_liveness = liveness_analysis->Get(f);
  115. {
  116. SCOPED_TRACE("Block 5");
  117. auto live_sets = register_liveness->Get(5);
  118. std::unordered_set<uint32_t> live_in{
  119. 11, // %11 = OpVariable %10 Input
  120. 15, // %15 = OpVariable %14 Input
  121. 32, // %32 = OpVariable %31 Output
  122. };
  123. CompareSets(live_sets->live_in_, live_in);
  124. std::unordered_set<uint32_t> live_out{
  125. 12, // %12 = OpLoad %7 %11
  126. 32, // %32 = OpVariable %31 Output
  127. };
  128. CompareSets(live_sets->live_out_, live_out);
  129. }
  130. {
  131. SCOPED_TRACE("Block 20");
  132. auto live_sets = register_liveness->Get(20);
  133. std::unordered_set<uint32_t> live_inout{
  134. 12, // %12 = OpLoad %7 %11
  135. 32, // %32 = OpVariable %31 Output
  136. };
  137. CompareSets(live_sets->live_in_, live_inout);
  138. CompareSets(live_sets->live_out_, live_inout);
  139. }
  140. {
  141. SCOPED_TRACE("Block 24");
  142. auto live_sets = register_liveness->Get(24);
  143. std::unordered_set<uint32_t> live_in{
  144. 12, // %12 = OpLoad %7 %11
  145. 32, // %32 = OpVariable %31 Output
  146. };
  147. CompareSets(live_sets->live_in_, live_in);
  148. std::unordered_set<uint32_t> live_out{
  149. 12, // %12 = OpLoad %7 %11
  150. 30, // %30 = OpFAdd %7 %12 %29
  151. 32, // %32 = OpVariable %31 Output
  152. };
  153. CompareSets(live_sets->live_out_, live_out);
  154. }
  155. {
  156. SCOPED_TRACE("Block 21");
  157. auto live_sets = register_liveness->Get(21);
  158. std::unordered_set<uint32_t> live_in{
  159. 12, // %12 = OpLoad %7 %11
  160. 32, // %32 = OpVariable %31 Output
  161. 36, // %36 = OpPhi %7 %12 %20 %30 %24
  162. };
  163. CompareSets(live_sets->live_in_, live_in);
  164. std::unordered_set<uint32_t> live_out{};
  165. CompareSets(live_sets->live_out_, live_out);
  166. }
  167. }
  168. /*
  169. Generated from the following GLSL
  170. #version 330
  171. in vec4 bigColor;
  172. in vec4 BaseColor;
  173. in float f;
  174. flat in int Count;
  175. flat in uvec4 v4;
  176. void main()
  177. {
  178. vec4 color = BaseColor;
  179. for (int i = 0; i < Count; ++i)
  180. color += bigColor;
  181. float sum = 0.0;
  182. for (int i = 0; i < 4; ++i) {
  183. float acc = 0.0;
  184. if (sum == 0.0) {
  185. acc = v4[i];
  186. }
  187. else {
  188. acc = BaseColor[i];
  189. }
  190. sum += acc + v4[i];
  191. }
  192. vec4 tv4;
  193. for (int i = 0; i < 4; ++i)
  194. tv4[i] = v4[i] * 4u;
  195. color += vec4(sum) + tv4;
  196. vec4 r;
  197. r.xyz = BaseColor.xyz;
  198. for (int i = 0; i < Count; ++i)
  199. r.w = f;
  200. color.xyz += r.xyz;
  201. for (int i = 0; i < 16; i += 4)
  202. for (int j = 0; j < 4; j++)
  203. color *= f;
  204. gl_FragColor = color + tv4;
  205. }
  206. */
  207. TEST_F(PassClassTest, RegisterLiveness) {
  208. const std::string text = R"(
  209. OpCapability Shader
  210. %1 = OpExtInstImport "GLSL.std.450"
  211. OpMemoryModel Logical GLSL450
  212. OpEntryPoint Fragment %4 "main" %11 %24 %28 %55 %124 %176
  213. OpExecutionMode %4 OriginLowerLeft
  214. OpSource GLSL 330
  215. OpName %4 "main"
  216. OpName %11 "BaseColor"
  217. OpName %24 "Count"
  218. OpName %28 "bigColor"
  219. OpName %55 "v4"
  220. OpName %84 "tv4"
  221. OpName %124 "f"
  222. OpName %176 "gl_FragColor"
  223. OpDecorate %11 Location 0
  224. OpDecorate %24 Flat
  225. OpDecorate %24 Location 0
  226. OpDecorate %28 Location 0
  227. OpDecorate %55 Flat
  228. OpDecorate %55 Location 0
  229. OpDecorate %124 Location 0
  230. OpDecorate %176 Location 0
  231. %2 = OpTypeVoid
  232. %3 = OpTypeFunction %2
  233. %6 = OpTypeFloat 32
  234. %7 = OpTypeVector %6 4
  235. %8 = OpTypePointer Function %7
  236. %10 = OpTypePointer Input %7
  237. %11 = OpVariable %10 Input
  238. %13 = OpTypeInt 32 1
  239. %16 = OpConstant %13 0
  240. %23 = OpTypePointer Input %13
  241. %24 = OpVariable %23 Input
  242. %26 = OpTypeBool
  243. %28 = OpVariable %10 Input
  244. %33 = OpConstant %13 1
  245. %35 = OpTypePointer Function %6
  246. %37 = OpConstant %6 0
  247. %45 = OpConstant %13 4
  248. %52 = OpTypeInt 32 0
  249. %53 = OpTypeVector %52 4
  250. %54 = OpTypePointer Input %53
  251. %55 = OpVariable %54 Input
  252. %57 = OpTypePointer Input %52
  253. %63 = OpTypePointer Input %6
  254. %89 = OpConstant %52 4
  255. %102 = OpTypeVector %6 3
  256. %124 = OpVariable %63 Input
  257. %158 = OpConstant %13 16
  258. %175 = OpTypePointer Output %7
  259. %176 = OpVariable %175 Output
  260. %195 = OpUndef %7
  261. %4 = OpFunction %2 None %3
  262. %5 = OpLabel
  263. %84 = OpVariable %8 Function
  264. %12 = OpLoad %7 %11
  265. OpBranch %17
  266. %17 = OpLabel
  267. %191 = OpPhi %7 %12 %5 %31 %18
  268. %184 = OpPhi %13 %16 %5 %34 %18
  269. %25 = OpLoad %13 %24
  270. %27 = OpSLessThan %26 %184 %25
  271. OpLoopMerge %19 %18 None
  272. OpBranchConditional %27 %18 %19
  273. %18 = OpLabel
  274. %29 = OpLoad %7 %28
  275. %31 = OpFAdd %7 %191 %29
  276. %34 = OpIAdd %13 %184 %33
  277. OpBranch %17
  278. %19 = OpLabel
  279. OpBranch %39
  280. %39 = OpLabel
  281. %188 = OpPhi %6 %37 %19 %73 %51
  282. %185 = OpPhi %13 %16 %19 %75 %51
  283. %46 = OpSLessThan %26 %185 %45
  284. OpLoopMerge %41 %51 None
  285. OpBranchConditional %46 %40 %41
  286. %40 = OpLabel
  287. %49 = OpFOrdEqual %26 %188 %37
  288. OpSelectionMerge %51 None
  289. OpBranchConditional %49 %50 %61
  290. %50 = OpLabel
  291. %58 = OpAccessChain %57 %55 %185
  292. %59 = OpLoad %52 %58
  293. %60 = OpConvertUToF %6 %59
  294. OpBranch %51
  295. %61 = OpLabel
  296. %64 = OpAccessChain %63 %11 %185
  297. %65 = OpLoad %6 %64
  298. OpBranch %51
  299. %51 = OpLabel
  300. %210 = OpPhi %6 %60 %50 %65 %61
  301. %68 = OpAccessChain %57 %55 %185
  302. %69 = OpLoad %52 %68
  303. %70 = OpConvertUToF %6 %69
  304. %71 = OpFAdd %6 %210 %70
  305. %73 = OpFAdd %6 %188 %71
  306. %75 = OpIAdd %13 %185 %33
  307. OpBranch %39
  308. %41 = OpLabel
  309. OpBranch %77
  310. %77 = OpLabel
  311. %186 = OpPhi %13 %16 %41 %94 %78
  312. %83 = OpSLessThan %26 %186 %45
  313. OpLoopMerge %79 %78 None
  314. OpBranchConditional %83 %78 %79
  315. %78 = OpLabel
  316. %87 = OpAccessChain %57 %55 %186
  317. %88 = OpLoad %52 %87
  318. %90 = OpIMul %52 %88 %89
  319. %91 = OpConvertUToF %6 %90
  320. %92 = OpAccessChain %35 %84 %186
  321. OpStore %92 %91
  322. %94 = OpIAdd %13 %186 %33
  323. OpBranch %77
  324. %79 = OpLabel
  325. %96 = OpCompositeConstruct %7 %188 %188 %188 %188
  326. %97 = OpLoad %7 %84
  327. %98 = OpFAdd %7 %96 %97
  328. %100 = OpFAdd %7 %191 %98
  329. %104 = OpVectorShuffle %102 %12 %12 0 1 2
  330. %106 = OpVectorShuffle %7 %195 %104 4 5 6 3
  331. OpBranch %108
  332. %108 = OpLabel
  333. %197 = OpPhi %7 %106 %79 %208 %133
  334. %196 = OpPhi %13 %16 %79 %143 %133
  335. %115 = OpSLessThan %26 %196 %25
  336. OpLoopMerge %110 %133 None
  337. OpBranchConditional %115 %109 %110
  338. %109 = OpLabel
  339. OpBranch %117
  340. %117 = OpLabel
  341. %209 = OpPhi %7 %197 %109 %181 %118
  342. %204 = OpPhi %13 %16 %109 %129 %118
  343. %123 = OpSLessThan %26 %204 %45
  344. OpLoopMerge %119 %118 None
  345. OpBranchConditional %123 %118 %119
  346. %118 = OpLabel
  347. %125 = OpLoad %6 %124
  348. %181 = OpCompositeInsert %7 %125 %209 3
  349. %129 = OpIAdd %13 %204 %33
  350. OpBranch %117
  351. %119 = OpLabel
  352. OpBranch %131
  353. %131 = OpLabel
  354. %208 = OpPhi %7 %209 %119 %183 %132
  355. %205 = OpPhi %13 %16 %119 %141 %132
  356. %137 = OpSLessThan %26 %205 %45
  357. OpLoopMerge %133 %132 None
  358. OpBranchConditional %137 %132 %133
  359. %132 = OpLabel
  360. %138 = OpLoad %6 %124
  361. %183 = OpCompositeInsert %7 %138 %208 3
  362. %141 = OpIAdd %13 %205 %33
  363. OpBranch %131
  364. %133 = OpLabel
  365. %143 = OpIAdd %13 %196 %33
  366. OpBranch %108
  367. %110 = OpLabel
  368. %145 = OpVectorShuffle %102 %197 %197 0 1 2
  369. %147 = OpVectorShuffle %102 %100 %100 0 1 2
  370. %148 = OpFAdd %102 %147 %145
  371. %150 = OpVectorShuffle %7 %100 %148 4 5 6 3
  372. OpBranch %152
  373. %152 = OpLabel
  374. %200 = OpPhi %7 %150 %110 %203 %163
  375. %199 = OpPhi %13 %16 %110 %174 %163
  376. %159 = OpSLessThan %26 %199 %158
  377. OpLoopMerge %154 %163 None
  378. OpBranchConditional %159 %153 %154
  379. %153 = OpLabel
  380. OpBranch %161
  381. %161 = OpLabel
  382. %203 = OpPhi %7 %200 %153 %170 %162
  383. %201 = OpPhi %13 %16 %153 %172 %162
  384. %167 = OpSLessThan %26 %201 %45
  385. OpLoopMerge %163 %162 None
  386. OpBranchConditional %167 %162 %163
  387. %162 = OpLabel
  388. %168 = OpLoad %6 %124
  389. %170 = OpVectorTimesScalar %7 %203 %168
  390. %172 = OpIAdd %13 %201 %33
  391. OpBranch %161
  392. %163 = OpLabel
  393. %174 = OpIAdd %13 %199 %45
  394. OpBranch %152
  395. %154 = OpLabel
  396. %178 = OpLoad %7 %84
  397. %179 = OpFAdd %7 %200 %178
  398. OpStore %176 %179
  399. OpReturn
  400. OpFunctionEnd
  401. )";
  402. std::unique_ptr<IRContext> context =
  403. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  404. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  405. Module* module = context->module();
  406. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  407. << text << std::endl;
  408. Function* f = &*module->begin();
  409. LivenessAnalysis* liveness_analysis = context->GetLivenessAnalysis();
  410. const RegisterLiveness* register_liveness = liveness_analysis->Get(f);
  411. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  412. {
  413. SCOPED_TRACE("Block 5");
  414. auto live_sets = register_liveness->Get(5);
  415. std::unordered_set<uint32_t> live_in{
  416. 11, // %11 = OpVariable %10 Input
  417. 24, // %24 = OpVariable %23 Input
  418. 28, // %28 = OpVariable %10 Input
  419. 55, // %55 = OpVariable %54 Input
  420. 124, // %124 = OpVariable %63 Input
  421. 176, // %176 = OpVariable %175 Output
  422. };
  423. CompareSets(live_sets->live_in_, live_in);
  424. std::unordered_set<uint32_t> live_out{
  425. 11, // %11 = OpVariable %10 Input
  426. 12, // %12 = OpLoad %7 %11
  427. 24, // %24 = OpVariable %23 Input
  428. 28, // %28 = OpVariable %10 Input
  429. 55, // %55 = OpVariable %54 Input
  430. 84, // %84 = OpVariable %8 Function
  431. 124, // %124 = OpVariable %63 Input
  432. 176, // %176 = OpVariable %175 Output
  433. };
  434. CompareSets(live_sets->live_out_, live_out);
  435. EXPECT_EQ(live_sets->used_registers_, 8u);
  436. }
  437. {
  438. SCOPED_TRACE("Block 17");
  439. auto live_sets = register_liveness->Get(17);
  440. std::unordered_set<uint32_t> live_in{
  441. 11, // %11 = OpVariable %10 Input
  442. 12, // %12 = OpLoad %7 %11
  443. 24, // %24 = OpVariable %23 Input
  444. 28, // %28 = OpVariable %10 Input
  445. 55, // %55 = OpVariable %54 Input
  446. 84, // %84 = OpVariable %8 Function
  447. 124, // %124 = OpVariable %63 Input
  448. 176, // %176 = OpVariable %175 Output
  449. 184, // %184 = OpPhi %13 %16 %5 %34 %18
  450. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  451. };
  452. CompareSets(live_sets->live_in_, live_in);
  453. std::unordered_set<uint32_t> live_out{
  454. 11, // %11 = OpVariable %10 Input
  455. 12, // %12 = OpLoad %7 %11
  456. 25, // %25 = OpLoad %13 %24
  457. 28, // %28 = OpVariable %10 Input
  458. 55, // %55 = OpVariable %54 Input
  459. 84, // %84 = OpVariable %8 Function
  460. 124, // %124 = OpVariable %63 Input
  461. 176, // %176 = OpVariable %175 Output
  462. 184, // %184 = OpPhi %13 %16 %5 %34 %18
  463. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  464. };
  465. CompareSets(live_sets->live_out_, live_out);
  466. EXPECT_EQ(live_sets->used_registers_, 11u);
  467. }
  468. {
  469. SCOPED_TRACE("Block 18");
  470. auto live_sets = register_liveness->Get(18);
  471. std::unordered_set<uint32_t> live_in{
  472. 11, // %11 = OpVariable %10 Input
  473. 12, // %12 = OpLoad %7 %11
  474. 24, // %24 = OpVariable %23 Input
  475. 28, // %28 = OpVariable %10 Input
  476. 55, // %55 = OpVariable %54 Input
  477. 84, // %84 = OpVariable %8 Function
  478. 124, // %124 = OpVariable %63 Input
  479. 176, // %176 = OpVariable %175 Output
  480. 184, // %184 = OpPhi %13 %16 %5 %34 %18
  481. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  482. };
  483. CompareSets(live_sets->live_in_, live_in);
  484. std::unordered_set<uint32_t> live_out{
  485. 11, // %11 = OpVariable %10 Input
  486. 12, // %12 = OpLoad %7 %11
  487. 24, // %24 = OpVariable %23 Input
  488. 28, // %28 = OpVariable %10 Input
  489. 31, // %31 = OpFAdd %7 %191 %29
  490. 34, // %34 = OpIAdd %13 %184 %33
  491. 55, // %55 = OpVariable %54 Input
  492. 84, // %84 = OpVariable %8 Function
  493. 124, // %124 = OpVariable %63 Input
  494. 176, // %176 = OpVariable %175 Output
  495. };
  496. CompareSets(live_sets->live_out_, live_out);
  497. EXPECT_EQ(live_sets->used_registers_, 12u);
  498. }
  499. {
  500. SCOPED_TRACE("Block 19");
  501. auto live_sets = register_liveness->Get(19);
  502. std::unordered_set<uint32_t> live_inout{
  503. 11, // %11 = OpVariable %10 Input
  504. 12, // %12 = OpLoad %7 %11
  505. 25, // %25 = OpLoad %13 %24
  506. 55, // %55 = OpVariable %54 Input
  507. 84, // %84 = OpVariable %8 Function
  508. 124, // %124 = OpVariable %63 Input
  509. 176, // %176 = OpVariable %175 Output
  510. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  511. };
  512. CompareSets(live_sets->live_in_, live_inout);
  513. CompareSets(live_sets->live_out_, live_inout);
  514. EXPECT_EQ(live_sets->used_registers_, 8u);
  515. }
  516. {
  517. SCOPED_TRACE("Block 39");
  518. auto live_sets = register_liveness->Get(39);
  519. std::unordered_set<uint32_t> live_inout{
  520. 11, // %11 = OpVariable %10 Input
  521. 12, // %12 = OpLoad %7 %11
  522. 25, // %25 = OpLoad %13 %24
  523. 55, // %55 = OpVariable %54 Input
  524. 84, // %84 = OpVariable %8 Function
  525. 124, // %124 = OpVariable %63 Input
  526. 176, // %176 = OpVariable %175 Output
  527. 185, // %185 = OpPhi %13 %16 %19 %75 %51
  528. 188, // %188 = OpPhi %6 %37 %19 %73 %51
  529. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  530. };
  531. CompareSets(live_sets->live_in_, live_inout);
  532. CompareSets(live_sets->live_out_, live_inout);
  533. EXPECT_EQ(live_sets->used_registers_, 11u);
  534. }
  535. {
  536. SCOPED_TRACE("Block 40");
  537. auto live_sets = register_liveness->Get(40);
  538. std::unordered_set<uint32_t> live_inout{
  539. 11, // %11 = OpVariable %10 Input
  540. 12, // %12 = OpLoad %7 %11
  541. 25, // %25 = OpLoad %13 %24
  542. 55, // %55 = OpVariable %54 Input
  543. 84, // %84 = OpVariable %8 Function
  544. 124, // %124 = OpVariable %63 Input
  545. 176, // %176 = OpVariable %175 Output
  546. 185, // %185 = OpPhi %13 %16 %19 %75 %51
  547. 188, // %188 = OpPhi %6 %37 %19 %73 %51
  548. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  549. };
  550. CompareSets(live_sets->live_in_, live_inout);
  551. CompareSets(live_sets->live_out_, live_inout);
  552. EXPECT_EQ(live_sets->used_registers_, 11u);
  553. }
  554. {
  555. SCOPED_TRACE("Block 50");
  556. auto live_sets = register_liveness->Get(50);
  557. std::unordered_set<uint32_t> live_in{
  558. 11, // %11 = OpVariable %10 Input
  559. 12, // %12 = OpLoad %7 %11
  560. 25, // %25 = OpLoad %13 %24
  561. 55, // %55 = OpVariable %54 Input
  562. 84, // %84 = OpVariable %8 Function
  563. 124, // %124 = OpVariable %63 Input
  564. 176, // %176 = OpVariable %175 Output
  565. 185, // %185 = OpPhi %13 %16 %19 %75 %51
  566. 188, // %188 = OpPhi %6 %37 %19 %73 %51
  567. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  568. };
  569. CompareSets(live_sets->live_in_, live_in);
  570. std::unordered_set<uint32_t> live_out{
  571. 11, // %11 = OpVariable %10 Input
  572. 12, // %12 = OpLoad %7 %11
  573. 25, // %25 = OpLoad %13 %24
  574. 55, // %55 = OpVariable %54 Input
  575. 60, // %60 = OpConvertUToF %6 %59
  576. 84, // %84 = OpVariable %8 Function
  577. 124, // %124 = OpVariable %63 Input
  578. 176, // %176 = OpVariable %175 Output
  579. 185, // %185 = OpPhi %13 %16 %19 %75 %51
  580. 188, // %188 = OpPhi %6 %37 %19 %73 %51
  581. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  582. };
  583. CompareSets(live_sets->live_out_, live_out);
  584. EXPECT_EQ(live_sets->used_registers_, 12u);
  585. }
  586. {
  587. SCOPED_TRACE("Block 61");
  588. auto live_sets = register_liveness->Get(61);
  589. std::unordered_set<uint32_t> live_in{
  590. 11, // %11 = OpVariable %10 Input
  591. 12, // %12 = OpLoad %7 %11
  592. 25, // %25 = OpLoad %13 %24
  593. 55, // %55 = OpVariable %54 Input
  594. 84, // %84 = OpVariable %8 Function
  595. 124, // %124 = OpVariable %63 Input
  596. 176, // %176 = OpVariable %175 Output
  597. 185, // %185 = OpPhi %13 %16 %19 %75 %51
  598. 188, // %188 = OpPhi %6 %37 %19 %73 %51
  599. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  600. };
  601. CompareSets(live_sets->live_in_, live_in);
  602. std::unordered_set<uint32_t> live_out{
  603. 11, // %11 = OpVariable %10 Input
  604. 12, // %12 = OpLoad %7 %11
  605. 25, // %25 = OpLoad %13 %24
  606. 55, // %55 = OpVariable %54 Input
  607. 65, // %65 = OpLoad %6 %64
  608. 84, // %84 = OpVariable %8 Function
  609. 124, // %124 = OpVariable %63 Input
  610. 176, // %176 = OpVariable %175 Output
  611. 185, // %185 = OpPhi %13 %16 %19 %75 %51
  612. 188, // %188 = OpPhi %6 %37 %19 %73 %51
  613. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  614. };
  615. CompareSets(live_sets->live_out_, live_out);
  616. EXPECT_EQ(live_sets->used_registers_, 12u);
  617. }
  618. {
  619. SCOPED_TRACE("Block 51");
  620. auto live_sets = register_liveness->Get(51);
  621. std::unordered_set<uint32_t> live_in{
  622. 11, // %11 = OpVariable %10 Input
  623. 12, // %12 = OpLoad %7 %11
  624. 25, // %25 = OpLoad %13 %24
  625. 55, // %55 = OpVariable %54 Input
  626. 84, // %84 = OpVariable %8 Function
  627. 124, // %124 = OpVariable %63 Input
  628. 176, // %176 = OpVariable %175 Output
  629. 185, // %185 = OpPhi %13 %16 %19 %75 %51
  630. 188, // %188 = OpPhi %6 %37 %19 %73 %51
  631. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  632. 210, // %210 = OpPhi %6 %60 %50 %65 %61
  633. };
  634. CompareSets(live_sets->live_in_, live_in);
  635. std::unordered_set<uint32_t> live_out{
  636. 11, // %11 = OpVariable %10 Input
  637. 12, // %12 = OpLoad %7 %11
  638. 25, // %25 = OpLoad %13 %24
  639. 55, // %55 = OpVariable %54 Input
  640. 73, // %73 = OpFAdd %6 %188 %71
  641. 75, // %75 = OpIAdd %13 %185 %33
  642. 84, // %84 = OpVariable %8 Function
  643. 124, // %124 = OpVariable %63 Input
  644. 176, // %176 = OpVariable %175 Output
  645. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  646. };
  647. CompareSets(live_sets->live_out_, live_out);
  648. EXPECT_EQ(live_sets->used_registers_, 13u);
  649. }
  650. {
  651. SCOPED_TRACE("Block 41");
  652. auto live_sets = register_liveness->Get(41);
  653. std::unordered_set<uint32_t> live_inout{
  654. 12, // %12 = OpLoad %7 %11
  655. 25, // %25 = OpLoad %13 %24
  656. 55, // %55 = OpVariable %54 Input
  657. 84, // %84 = OpVariable %8 Function
  658. 124, // %124 = OpVariable %63 Input
  659. 176, // %176 = OpVariable %175 Output
  660. 188, // %188 = OpPhi %6 %37 %19 %73 %51
  661. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  662. };
  663. CompareSets(live_sets->live_in_, live_inout);
  664. CompareSets(live_sets->live_out_, live_inout);
  665. EXPECT_EQ(live_sets->used_registers_, 8u);
  666. }
  667. {
  668. SCOPED_TRACE("Block 77");
  669. auto live_sets = register_liveness->Get(77);
  670. std::unordered_set<uint32_t> live_inout{
  671. 12, // %12 = OpLoad %7 %11
  672. 25, // %25 = OpLoad %13 %24
  673. 55, // %55 = OpVariable %54 Input
  674. 84, // %84 = OpVariable %8 Function
  675. 124, // %124 = OpVariable %63 Input
  676. 176, // %176 = OpVariable %175 Output
  677. 186, // %186 = OpPhi %13 %16 %41 %94 %78
  678. 188, // %188 = OpPhi %6 %37 %19 %73 %51
  679. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  680. };
  681. CompareSets(live_sets->live_in_, live_inout);
  682. CompareSets(live_sets->live_out_, live_inout);
  683. EXPECT_EQ(live_sets->used_registers_, 10u);
  684. }
  685. {
  686. SCOPED_TRACE("Block 78");
  687. auto live_sets = register_liveness->Get(78);
  688. std::unordered_set<uint32_t> live_in{
  689. 12, // %12 = OpLoad %7 %11
  690. 25, // %25 = OpLoad %13 %24
  691. 55, // %55 = OpVariable %54 Input
  692. 84, // %84 = OpVariable %8 Function
  693. 124, // %124 = OpVariable %63 Input
  694. 176, // %176 = OpVariable %175 Output
  695. 186, // %186 = OpPhi %13 %16 %41 %94 %78
  696. 188, // %188 = OpPhi %6 %37 %19 %73 %51
  697. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  698. };
  699. CompareSets(live_sets->live_in_, live_in);
  700. std::unordered_set<uint32_t> live_out{
  701. 12, // %12 = OpLoad %7 %11
  702. 25, // %25 = OpLoad %13 %24
  703. 55, // %55 = OpVariable %54 Input
  704. 84, // %84 = OpVariable %8 Function
  705. 94, // %94 = OpIAdd %13 %186 %33
  706. 124, // %124 = OpVariable %63 Input
  707. 176, // %176 = OpVariable %175 Output
  708. 188, // %188 = OpPhi %6 %37 %19 %73 %51
  709. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  710. };
  711. CompareSets(live_sets->live_out_, live_out);
  712. EXPECT_EQ(live_sets->used_registers_, 11u);
  713. }
  714. {
  715. SCOPED_TRACE("Block 79");
  716. auto live_sets = register_liveness->Get(79);
  717. std::unordered_set<uint32_t> live_in{
  718. 12, // %12 = OpLoad %7 %11
  719. 25, // %25 = OpLoad %13 %24
  720. 84, // %84 = OpVariable %8 Function
  721. 124, // %124 = OpVariable %63 Input
  722. 176, // %176 = OpVariable %175 Output
  723. 188, // %188 = OpPhi %6 %37 %19 %73 %51
  724. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  725. };
  726. CompareSets(live_sets->live_in_, live_in);
  727. std::unordered_set<uint32_t> live_out{
  728. 25, // %25 = OpLoad %13 %24
  729. 84, // %84 = OpVariable %8 Function
  730. 100, // %100 = OpFAdd %7 %191 %98
  731. 106, // %106 = OpVectorShuffle %7 %195 %104 4 5 6 3
  732. 124, // %124 = OpVariable %63 Input
  733. 176, // %176 = OpVariable %175 Output
  734. };
  735. CompareSets(live_sets->live_out_, live_out);
  736. EXPECT_EQ(live_sets->used_registers_, 9u);
  737. }
  738. {
  739. SCOPED_TRACE("Block 108");
  740. auto live_sets = register_liveness->Get(108);
  741. std::unordered_set<uint32_t> live_in{
  742. 25, // %25 = OpLoad %13 %24
  743. 84, // %84 = OpVariable %8 Function
  744. 100, // %100 = OpFAdd %7 %191 %98
  745. 124, // %124 = OpVariable %63 Input
  746. 176, // %176 = OpVariable %175 Output
  747. 196, // %196 = OpPhi %13 %16 %79 %143 %133
  748. 197, // %197 = OpPhi %7 %106 %79 %208 %133
  749. };
  750. CompareSets(live_sets->live_in_, live_in);
  751. std::unordered_set<uint32_t> live_out{
  752. 84, // %84 = OpVariable %8 Function
  753. 100, // %100 = OpFAdd %7 %191 %98
  754. 124, // %124 = OpVariable %63 Input
  755. 176, // %176 = OpVariable %175 Output
  756. 196, // %196 = OpPhi %13 %16 %79 %143 %133
  757. 197, // %197 = OpPhi %7 %106 %79 %208 %133
  758. };
  759. CompareSets(live_sets->live_out_, live_out);
  760. EXPECT_EQ(live_sets->used_registers_, 8u);
  761. }
  762. {
  763. SCOPED_TRACE("Block 109");
  764. auto live_sets = register_liveness->Get(109);
  765. std::unordered_set<uint32_t> live_inout{
  766. 25, // %25 = OpLoad %13 %24
  767. 84, // %84 = OpVariable %8 Function
  768. 100, // %100 = OpFAdd %7 %191 %98
  769. 124, // %124 = OpVariable %63 Input
  770. 176, // %176 = OpVariable %175 Output
  771. 196, // %196 = OpPhi %13 %16 %79 %143 %133
  772. 197, // %197 = OpPhi %7 %106 %79 %208 %133
  773. };
  774. CompareSets(live_sets->live_in_, live_inout);
  775. CompareSets(live_sets->live_out_, live_inout);
  776. EXPECT_EQ(live_sets->used_registers_, 7u);
  777. }
  778. {
  779. SCOPED_TRACE("Block 117");
  780. auto live_sets = register_liveness->Get(117);
  781. std::unordered_set<uint32_t> live_inout{
  782. 25, // %25 = OpLoad %13 %24
  783. 84, // %84 = OpVariable %8 Function
  784. 100, // %100 = OpFAdd %7 %191 %98
  785. 124, // %124 = OpVariable %63 Input
  786. 176, // %176 = OpVariable %175 Output
  787. 196, // %196 = OpPhi %13 %16 %79 %143 %133
  788. 204, // %204 = OpPhi %13 %16 %109 %129 %118
  789. 209, // %209 = OpPhi %7 %197 %109 %181 %118
  790. };
  791. CompareSets(live_sets->live_in_, live_inout);
  792. CompareSets(live_sets->live_out_, live_inout);
  793. EXPECT_EQ(live_sets->used_registers_, 9u);
  794. }
  795. {
  796. SCOPED_TRACE("Block 118");
  797. auto live_sets = register_liveness->Get(118);
  798. std::unordered_set<uint32_t> live_in{
  799. 25, // %25 = OpLoad %13 %24
  800. 84, // %84 = OpVariable %8 Function
  801. 100, // %100 = OpFAdd %7 %191 %98
  802. 124, // %124 = OpVariable %63 Input
  803. 176, // %176 = OpVariable %175 Output
  804. 196, // %196 = OpPhi %13 %16 %79 %143 %133
  805. 204, // %204 = OpPhi %13 %16 %109 %129 %118
  806. 209, // %209 = OpPhi %7 %197 %109 %181 %118
  807. };
  808. CompareSets(live_sets->live_in_, live_in);
  809. std::unordered_set<uint32_t> live_out{
  810. 25, // %25 = OpLoad %13 %24
  811. 84, // %84 = OpVariable %8 Function
  812. 100, // %100 = OpFAdd %7 %191 %98
  813. 124, // %124 = OpVariable %63 Input
  814. 129, // %129 = OpIAdd %13 %204 %33
  815. 176, // %176 = OpVariable %175 Output
  816. 181, // %181 = OpCompositeInsert %7 %125 %209 3
  817. 196, // %196 = OpPhi %13 %16 %79 %143 %133
  818. };
  819. CompareSets(live_sets->live_out_, live_out);
  820. EXPECT_EQ(live_sets->used_registers_, 10u);
  821. }
  822. {
  823. SCOPED_TRACE("Block 119");
  824. auto live_sets = register_liveness->Get(119);
  825. std::unordered_set<uint32_t> live_inout{
  826. 25, // %25 = OpLoad %13 %24
  827. 84, // %84 = OpVariable %8 Function
  828. 100, // %100 = OpFAdd %7 %191 %98
  829. 124, // %124 = OpVariable %63 Input
  830. 176, // %176 = OpVariable %175 Output
  831. 196, // %196 = OpPhi %13 %16 %79 %143 %133
  832. 209, // %209 = OpPhi %7 %197 %109 %181 %118
  833. };
  834. CompareSets(live_sets->live_in_, live_inout);
  835. CompareSets(live_sets->live_out_, live_inout);
  836. EXPECT_EQ(live_sets->used_registers_, 7u);
  837. }
  838. {
  839. SCOPED_TRACE("Block 131");
  840. auto live_sets = register_liveness->Get(131);
  841. std::unordered_set<uint32_t> live_inout{
  842. 25, // %25 = OpLoad %13 %24
  843. 84, // %84 = OpVariable %8 Function
  844. 100, // %100 = OpFAdd %7 %191 %98
  845. 124, // %124 = OpVariable %63 Input
  846. 176, // %176 = OpVariable %175 Output
  847. 196, // %196 = OpPhi %13 %16 %79 %143 %133
  848. 205, // %205 = OpPhi %13 %16 %119 %141 %132
  849. 208, // %208 = OpPhi %7 %209 %119 %183 %132
  850. };
  851. CompareSets(live_sets->live_in_, live_inout);
  852. CompareSets(live_sets->live_out_, live_inout);
  853. EXPECT_EQ(live_sets->used_registers_, 9u);
  854. }
  855. {
  856. SCOPED_TRACE("Block 132");
  857. auto live_sets = register_liveness->Get(132);
  858. std::unordered_set<uint32_t> live_in{
  859. 25, // %25 = OpLoad %13 %24
  860. 84, // %84 = OpVariable %8 Function
  861. 100, // %100 = OpFAdd %7 %191 %98
  862. 124, // %124 = OpVariable %63 Input
  863. 176, // %176 = OpVariable %175 Output
  864. 196, // %196 = OpPhi %13 %16 %79 %143 %133
  865. 205, // %205 = OpPhi %13 %16 %119 %141 %132
  866. 208, // %208 = OpPhi %7 %209 %119 %183 %132
  867. };
  868. CompareSets(live_sets->live_in_, live_in);
  869. std::unordered_set<uint32_t> live_out{
  870. 25, // %25 = OpLoad %13 %24
  871. 84, // %84 = OpVariable %8 Function
  872. 100, // %100 = OpFAdd %7 %191 %98
  873. 124, // %124 = OpVariable %63 Input
  874. 141, // %141 = OpIAdd %13 %205 %33
  875. 176, // %176 = OpVariable %175 Output
  876. 183, // %183 = OpCompositeInsert %7 %138 %208 3
  877. 196, // %196 = OpPhi %13 %16 %79 %143 %133
  878. };
  879. CompareSets(live_sets->live_out_, live_out);
  880. EXPECT_EQ(live_sets->used_registers_, 10u);
  881. }
  882. {
  883. SCOPED_TRACE("Block 133");
  884. auto live_sets = register_liveness->Get(133);
  885. std::unordered_set<uint32_t> live_in{
  886. 25, // %25 = OpLoad %13 %24
  887. 84, // %84 = OpVariable %8 Function
  888. 100, // %100 = OpFAdd %7 %191 %98
  889. 124, // %124 = OpVariable %63 Input
  890. 176, // %176 = OpVariable %175 Output
  891. 196, // %196 = OpPhi %13 %16 %79 %143 %133
  892. 208, // %208 = OpPhi %7 %209 %119 %183 %132
  893. };
  894. CompareSets(live_sets->live_in_, live_in);
  895. std::unordered_set<uint32_t> live_out{
  896. 25, // %25 = OpLoad %13 %24
  897. 84, // %84 = OpVariable %8 Function
  898. 100, // %100 = OpFAdd %7 %191 %98
  899. 124, // %124 = OpVariable %63 Input
  900. 143, // %143 = OpIAdd %13 %196 %33
  901. 176, // %176 = OpVariable %175 Output
  902. 208, // %208 = OpPhi %7 %209 %119 %183 %132
  903. };
  904. CompareSets(live_sets->live_out_, live_out);
  905. EXPECT_EQ(live_sets->used_registers_, 8u);
  906. }
  907. {
  908. SCOPED_TRACE("Block 110");
  909. auto live_sets = register_liveness->Get(110);
  910. std::unordered_set<uint32_t> live_in{
  911. 84, // %84 = OpVariable %8 Function
  912. 100, // %100 = OpFAdd %7 %191 %98
  913. 124, // %124 = OpVariable %63 Input
  914. 176, // %176 = OpVariable %175 Output
  915. 197, // %197 = OpPhi %7 %106 %79 %208 %133
  916. };
  917. CompareSets(live_sets->live_in_, live_in);
  918. std::unordered_set<uint32_t> live_out{
  919. 84, // %84 = OpVariable %8 Function
  920. 124, // %124 = OpVariable %63 Input
  921. 150, // %150 = OpVectorShuffle %7 %100 %148 4 5 6 3
  922. 176, // %176 = OpVariable %175 Output
  923. };
  924. CompareSets(live_sets->live_out_, live_out);
  925. EXPECT_EQ(live_sets->used_registers_, 7u);
  926. }
  927. {
  928. SCOPED_TRACE("Block 152");
  929. auto live_sets = register_liveness->Get(152);
  930. std::unordered_set<uint32_t> live_inout{
  931. 84, // %84 = OpVariable %8 Function
  932. 124, // %124 = OpVariable %63 Input
  933. 176, // %176 = OpVariable %175 Output
  934. 199, // %199 = OpPhi %13 %16 %110 %174 %163
  935. 200, // %200 = OpPhi %7 %150 %110 %203 %163
  936. };
  937. CompareSets(live_sets->live_in_, live_inout);
  938. CompareSets(live_sets->live_out_, live_inout);
  939. EXPECT_EQ(live_sets->used_registers_, 6u);
  940. }
  941. {
  942. SCOPED_TRACE("Block 153");
  943. auto live_sets = register_liveness->Get(153);
  944. std::unordered_set<uint32_t> live_inout{
  945. 84, // %84 = OpVariable %8 Function
  946. 124, // %124 = OpVariable %63 Input
  947. 176, // %176 = OpVariable %175 Output
  948. 199, // %199 = OpPhi %13 %16 %110 %174 %163
  949. 200, // %200 = OpPhi %7 %150 %110 %203 %163
  950. };
  951. CompareSets(live_sets->live_in_, live_inout);
  952. CompareSets(live_sets->live_out_, live_inout);
  953. EXPECT_EQ(live_sets->used_registers_, 5u);
  954. }
  955. {
  956. SCOPED_TRACE("Block 161");
  957. auto live_sets = register_liveness->Get(161);
  958. std::unordered_set<uint32_t> live_inout{
  959. 84, // %84 = OpVariable %8 Function
  960. 124, // %124 = OpVariable %63 Input
  961. 176, // %176 = OpVariable %175 Output
  962. 199, // %199 = OpPhi %13 %16 %110 %174 %163
  963. 201, // %201 = OpPhi %13 %16 %153 %172 %162
  964. 203, // %203 = OpPhi %7 %200 %153 %170 %162
  965. };
  966. CompareSets(live_sets->live_in_, live_inout);
  967. CompareSets(live_sets->live_out_, live_inout);
  968. EXPECT_EQ(live_sets->used_registers_, 7u);
  969. }
  970. {
  971. SCOPED_TRACE("Block 162");
  972. auto live_sets = register_liveness->Get(162);
  973. std::unordered_set<uint32_t> live_in{
  974. 84, // %84 = OpVariable %8 Function
  975. 124, // %124 = OpVariable %63 Input
  976. 176, // %176 = OpVariable %175 Output
  977. 199, // %199 = OpPhi %13 %16 %110 %174 %163
  978. 201, // %201 = OpPhi %13 %16 %153 %172 %162
  979. 203, // %203 = OpPhi %7 %200 %153 %170 %162
  980. };
  981. CompareSets(live_sets->live_in_, live_in);
  982. std::unordered_set<uint32_t> live_out{
  983. 84, // %84 = OpVariable %8 Function
  984. 124, // %124 = OpVariable %63 Input
  985. 170, // %170 = OpVectorTimesScalar %7 %203 %168
  986. 172, // %172 = OpIAdd %13 %201 %33
  987. 176, // %176 = OpVariable %175 Output
  988. 199, // %199 = OpPhi %13 %16 %110 %174 %163
  989. };
  990. CompareSets(live_sets->live_out_, live_out);
  991. EXPECT_EQ(live_sets->used_registers_, 8u);
  992. }
  993. {
  994. SCOPED_TRACE("Block 163");
  995. auto live_sets = register_liveness->Get(163);
  996. std::unordered_set<uint32_t> live_in{
  997. 84, // %84 = OpVariable %8 Function
  998. 124, // %124 = OpVariable %63 Input
  999. 176, // %176 = OpVariable %175 Output
  1000. 199, // %199 = OpPhi %13 %16 %110 %174 %163
  1001. 203, // %203 = OpPhi %7 %200 %153 %170 %162
  1002. };
  1003. CompareSets(live_sets->live_in_, live_in);
  1004. std::unordered_set<uint32_t> live_out{
  1005. 84, // %84 = OpVariable %8 Function
  1006. 124, // %124 = OpVariable %63 Input
  1007. 174, // %174 = OpIAdd %13 %199 %45
  1008. 176, // %176 = OpVariable %175 Output
  1009. 203, // %203 = OpPhi %7 %200 %153 %170 %162
  1010. };
  1011. CompareSets(live_sets->live_out_, live_out);
  1012. EXPECT_EQ(live_sets->used_registers_, 6u);
  1013. }
  1014. {
  1015. SCOPED_TRACE("Block 154");
  1016. auto live_sets = register_liveness->Get(154);
  1017. std::unordered_set<uint32_t> live_in{
  1018. 84, // %84 = OpVariable %8 Function
  1019. 176, // %176 = OpVariable %175 Output
  1020. 200, // %200 = OpPhi %7 %150 %110 %203 %163
  1021. };
  1022. CompareSets(live_sets->live_in_, live_in);
  1023. std::unordered_set<uint32_t> live_out{};
  1024. CompareSets(live_sets->live_out_, live_out);
  1025. EXPECT_EQ(live_sets->used_registers_, 4u);
  1026. }
  1027. {
  1028. SCOPED_TRACE("Compute loop pressure");
  1029. RegisterLiveness::RegionRegisterLiveness loop_reg_pressure;
  1030. register_liveness->ComputeLoopRegisterPressure(*ld[39], &loop_reg_pressure);
  1031. // Generate(*context->cfg()->block(39), &loop_reg_pressure);
  1032. std::unordered_set<uint32_t> live_in{
  1033. 11, // %11 = OpVariable %10 Input
  1034. 12, // %12 = OpLoad %7 %11
  1035. 25, // %25 = OpLoad %13 %24
  1036. 55, // %55 = OpVariable %54 Input
  1037. 84, // %84 = OpVariable %8 Function
  1038. 124, // %124 = OpVariable %63 Input
  1039. 176, // %176 = OpVariable %175 Output
  1040. 185, // %185 = OpPhi %13 %16 %19 %75 %51
  1041. 188, // %188 = OpPhi %6 %37 %19 %73 %51
  1042. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  1043. };
  1044. CompareSets(loop_reg_pressure.live_in_, live_in);
  1045. std::unordered_set<uint32_t> live_out{
  1046. 12, // %12 = OpLoad %7 %11
  1047. 25, // %25 = OpLoad %13 %24
  1048. 55, // %55 = OpVariable %54 Input
  1049. 84, // %84 = OpVariable %8 Function
  1050. 124, // %124 = OpVariable %63 Input
  1051. 176, // %176 = OpVariable %175 Output
  1052. 188, // %188 = OpPhi %6 %37 %19 %73 %51
  1053. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  1054. };
  1055. CompareSets(loop_reg_pressure.live_out_, live_out);
  1056. EXPECT_EQ(loop_reg_pressure.used_registers_, 13u);
  1057. }
  1058. {
  1059. SCOPED_TRACE("Loop Fusion simulation");
  1060. RegisterLiveness::RegionRegisterLiveness simulation_resut;
  1061. register_liveness->SimulateFusion(*ld[17], *ld[39], &simulation_resut);
  1062. std::unordered_set<uint32_t> live_in{
  1063. 11, // %11 = OpVariable %10 Input
  1064. 12, // %12 = OpLoad %7 %11
  1065. 24, // %24 = OpVariable %23 Input
  1066. 25, // %25 = OpLoad %13 %24
  1067. 28, // %28 = OpVariable %10 Input
  1068. 55, // %55 = OpVariable %54 Input
  1069. 84, // %84 = OpVariable %8 Function
  1070. 124, // %124 = OpVariable %63 Input
  1071. 176, // %176 = OpVariable %175 Output
  1072. 184, // %184 = OpPhi %13 %16 %5 %34 %18
  1073. 185, // %185 = OpPhi %13 %16 %19 %75 %51
  1074. 188, // %188 = OpPhi %6 %37 %19 %73 %51
  1075. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  1076. };
  1077. CompareSets(simulation_resut.live_in_, live_in);
  1078. std::unordered_set<uint32_t> live_out{
  1079. 12, // %12 = OpLoad %7 %11
  1080. 25, // %25 = OpLoad %13 %24
  1081. 55, // %55 = OpVariable %54 Input
  1082. 84, // %84 = OpVariable %8 Function
  1083. 124, // %124 = OpVariable %63 Input
  1084. 176, // %176 = OpVariable %175 Output
  1085. 188, // %188 = OpPhi %6 %37 %19 %73 %51
  1086. 191, // %191 = OpPhi %7 %12 %5 %31 %18
  1087. };
  1088. CompareSets(simulation_resut.live_out_, live_out);
  1089. EXPECT_EQ(simulation_resut.used_registers_, 17u);
  1090. }
  1091. }
  1092. TEST_F(PassClassTest, FissionSimulation) {
  1093. const std::string source = R"(
  1094. OpCapability Shader
  1095. %1 = OpExtInstImport "GLSL.std.450"
  1096. OpMemoryModel Logical GLSL450
  1097. OpEntryPoint Fragment %2 "main"
  1098. OpExecutionMode %2 OriginUpperLeft
  1099. OpSource GLSL 430
  1100. OpName %2 "main"
  1101. OpName %3 "i"
  1102. OpName %4 "A"
  1103. OpName %5 "B"
  1104. %6 = OpTypeVoid
  1105. %7 = OpTypeFunction %6
  1106. %8 = OpTypeInt 32 1
  1107. %9 = OpTypePointer Function %8
  1108. %10 = OpConstant %8 0
  1109. %11 = OpConstant %8 10
  1110. %12 = OpTypeBool
  1111. %13 = OpTypeFloat 32
  1112. %14 = OpTypeInt 32 0
  1113. %15 = OpConstant %14 10
  1114. %16 = OpTypeArray %13 %15
  1115. %17 = OpTypePointer Function %16
  1116. %18 = OpTypePointer Function %13
  1117. %19 = OpConstant %8 1
  1118. %2 = OpFunction %6 None %7
  1119. %20 = OpLabel
  1120. %3 = OpVariable %9 Function
  1121. %4 = OpVariable %17 Function
  1122. %5 = OpVariable %17 Function
  1123. OpBranch %21
  1124. %21 = OpLabel
  1125. %22 = OpPhi %8 %10 %20 %23 %24
  1126. OpLoopMerge %25 %24 None
  1127. OpBranch %26
  1128. %26 = OpLabel
  1129. %27 = OpSLessThan %12 %22 %11
  1130. OpBranchConditional %27 %28 %25
  1131. %28 = OpLabel
  1132. %29 = OpAccessChain %18 %5 %22
  1133. %30 = OpLoad %13 %29
  1134. %31 = OpAccessChain %18 %4 %22
  1135. OpStore %31 %30
  1136. %32 = OpAccessChain %18 %4 %22
  1137. %33 = OpLoad %13 %32
  1138. %34 = OpAccessChain %18 %5 %22
  1139. OpStore %34 %33
  1140. OpBranch %24
  1141. %24 = OpLabel
  1142. %23 = OpIAdd %8 %22 %19
  1143. OpBranch %21
  1144. %25 = OpLabel
  1145. OpStore %3 %22
  1146. OpReturn
  1147. OpFunctionEnd
  1148. )";
  1149. std::unique_ptr<IRContext> context =
  1150. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, source,
  1151. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1152. Module* module = context->module();
  1153. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  1154. << source << std::endl;
  1155. Function* f = &*module->begin();
  1156. LivenessAnalysis* liveness_analysis = context->GetLivenessAnalysis();
  1157. const RegisterLiveness* register_liveness = liveness_analysis->Get(f);
  1158. LoopDescriptor& ld = *context->GetLoopDescriptor(f);
  1159. analysis::DefUseManager& def_use_mgr = *context->get_def_use_mgr();
  1160. {
  1161. RegisterLiveness::RegionRegisterLiveness l1_sim_resut;
  1162. RegisterLiveness::RegionRegisterLiveness l2_sim_resut;
  1163. std::unordered_set<Instruction*> moved_instructions{
  1164. def_use_mgr.GetDef(29), def_use_mgr.GetDef(30), def_use_mgr.GetDef(31),
  1165. def_use_mgr.GetDef(31)->NextNode()};
  1166. std::unordered_set<Instruction*> copied_instructions{
  1167. def_use_mgr.GetDef(22), def_use_mgr.GetDef(27),
  1168. def_use_mgr.GetDef(27)->NextNode(), def_use_mgr.GetDef(23)};
  1169. register_liveness->SimulateFission(*ld[21], moved_instructions,
  1170. copied_instructions, &l1_sim_resut,
  1171. &l2_sim_resut);
  1172. {
  1173. SCOPED_TRACE("L1 simulation");
  1174. std::unordered_set<uint32_t> live_in{
  1175. 3, // %3 = OpVariable %9 Function
  1176. 4, // %4 = OpVariable %17 Function
  1177. 5, // %5 = OpVariable %17 Function
  1178. 22, // %22 = OpPhi %8 %10 %20 %23 %24
  1179. };
  1180. CompareSets(l1_sim_resut.live_in_, live_in);
  1181. std::unordered_set<uint32_t> live_out{
  1182. 3, // %3 = OpVariable %9 Function
  1183. 4, // %4 = OpVariable %17 Function
  1184. 5, // %5 = OpVariable %17 Function
  1185. 22, // %22 = OpPhi %8 %10 %20 %23 %24
  1186. };
  1187. CompareSets(l1_sim_resut.live_out_, live_out);
  1188. EXPECT_EQ(l1_sim_resut.used_registers_, 6u);
  1189. }
  1190. {
  1191. SCOPED_TRACE("L2 simulation");
  1192. std::unordered_set<uint32_t> live_in{
  1193. 3, // %3 = OpVariable %9 Function
  1194. 4, // %4 = OpVariable %17 Function
  1195. 5, // %5 = OpVariable %17 Function
  1196. 22, // %22 = OpPhi %8 %10 %20 %23 %24
  1197. };
  1198. CompareSets(l2_sim_resut.live_in_, live_in);
  1199. std::unordered_set<uint32_t> live_out{
  1200. 3, // %3 = OpVariable %9 Function
  1201. 22, // %22 = OpPhi %8 %10 %20 %23 %24
  1202. };
  1203. CompareSets(l2_sim_resut.live_out_, live_out);
  1204. EXPECT_EQ(l2_sim_resut.used_registers_, 6u);
  1205. }
  1206. }
  1207. }
  1208. // Test that register liveness does not fail when there is an unreachable block.
  1209. // We are not testing if the liveness is computed correctly because the specific
  1210. // results do not matter for unreachable blocks.
  1211. TEST_F(PassClassTest, RegisterLivenessWithUnreachableBlock) {
  1212. const std::string text = R"(
  1213. OpCapability Shader
  1214. %1 = OpExtInstImport "GLSL.std.450"
  1215. OpMemoryModel Logical GLSL450
  1216. OpEntryPoint Fragment %2 "main"
  1217. OpExecutionMode %2 OriginLowerLeft
  1218. OpSource GLSL 330
  1219. OpSourceExtension "GL_ARB_shading_language_420pack"
  1220. %void = OpTypeVoid
  1221. %4 = OpTypeFunction %void
  1222. %2 = OpFunction %void None %4
  1223. %5 = OpLabel
  1224. OpBranch %6
  1225. %6 = OpLabel
  1226. OpLoopMerge %7 %8 None
  1227. OpBranch %9
  1228. %9 = OpLabel
  1229. OpBranch %7
  1230. %8 = OpLabel
  1231. OpBranch %6
  1232. %7 = OpLabel
  1233. OpReturn
  1234. OpFunctionEnd
  1235. )";
  1236. std::unique_ptr<IRContext> context =
  1237. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1238. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1239. Module* module = context->module();
  1240. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  1241. << text << std::endl;
  1242. Function* f = &*module->begin();
  1243. LivenessAnalysis* liveness_analysis = context->GetLivenessAnalysis();
  1244. liveness_analysis->Get(f);
  1245. }
  1246. } // namespace
  1247. } // namespace opt
  1248. } // namespace spvtools