fusion_illegal.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  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 <vector>
  16. #include "gmock/gmock.h"
  17. #include "source/opt/loop_descriptor.h"
  18. #include "source/opt/loop_fusion.h"
  19. #include "test/opt/pass_fixture.h"
  20. namespace spvtools {
  21. namespace opt {
  22. namespace {
  23. using FusionIllegalTest = PassTest<::testing::Test>;
  24. /*
  25. Generated from the following GLSL + --eliminate-local-multi-store
  26. #version 440 core
  27. void main() {
  28. int[10] a;
  29. int[10] b;
  30. int[10] c;
  31. // Illegal, loop-independent dependence will become a
  32. // backward loop-carried antidependence
  33. for (int i = 0; i < 10; i++) {
  34. a[i] = b[i] + 1;
  35. }
  36. for (int i = 0; i < 10; i++) {
  37. c[i] = a[i+1] + 2;
  38. }
  39. }
  40. */
  41. TEST_F(FusionIllegalTest, PositiveDistanceCreatedRAW) {
  42. std::string text = R"(
  43. OpCapability Shader
  44. %1 = OpExtInstImport "GLSL.std.450"
  45. OpMemoryModel Logical GLSL450
  46. OpEntryPoint Fragment %4 "main"
  47. OpExecutionMode %4 OriginUpperLeft
  48. OpSource GLSL 440
  49. OpName %4 "main"
  50. OpName %8 "i"
  51. OpName %23 "a"
  52. OpName %25 "b"
  53. OpName %34 "i"
  54. OpName %42 "c"
  55. %2 = OpTypeVoid
  56. %3 = OpTypeFunction %2
  57. %6 = OpTypeInt 32 1
  58. %7 = OpTypePointer Function %6
  59. %9 = OpConstant %6 0
  60. %16 = OpConstant %6 10
  61. %17 = OpTypeBool
  62. %19 = OpTypeInt 32 0
  63. %20 = OpConstant %19 10
  64. %21 = OpTypeArray %6 %20
  65. %22 = OpTypePointer Function %21
  66. %29 = OpConstant %6 1
  67. %48 = OpConstant %6 2
  68. %4 = OpFunction %2 None %3
  69. %5 = OpLabel
  70. %8 = OpVariable %7 Function
  71. %23 = OpVariable %22 Function
  72. %25 = OpVariable %22 Function
  73. %34 = OpVariable %7 Function
  74. %42 = OpVariable %22 Function
  75. OpStore %8 %9
  76. OpBranch %10
  77. %10 = OpLabel
  78. %53 = OpPhi %6 %9 %5 %33 %13
  79. OpLoopMerge %12 %13 None
  80. OpBranch %14
  81. %14 = OpLabel
  82. %18 = OpSLessThan %17 %53 %16
  83. OpBranchConditional %18 %11 %12
  84. %11 = OpLabel
  85. %27 = OpAccessChain %7 %25 %53
  86. %28 = OpLoad %6 %27
  87. %30 = OpIAdd %6 %28 %29
  88. %31 = OpAccessChain %7 %23 %53
  89. OpStore %31 %30
  90. OpBranch %13
  91. %13 = OpLabel
  92. %33 = OpIAdd %6 %53 %29
  93. OpStore %8 %33
  94. OpBranch %10
  95. %12 = OpLabel
  96. OpStore %34 %9
  97. OpBranch %35
  98. %35 = OpLabel
  99. %54 = OpPhi %6 %9 %12 %52 %38
  100. OpLoopMerge %37 %38 None
  101. OpBranch %39
  102. %39 = OpLabel
  103. %41 = OpSLessThan %17 %54 %16
  104. OpBranchConditional %41 %36 %37
  105. %36 = OpLabel
  106. %45 = OpIAdd %6 %54 %29
  107. %46 = OpAccessChain %7 %23 %45
  108. %47 = OpLoad %6 %46
  109. %49 = OpIAdd %6 %47 %48
  110. %50 = OpAccessChain %7 %42 %54
  111. OpStore %50 %49
  112. OpBranch %38
  113. %38 = OpLabel
  114. %52 = OpIAdd %6 %54 %29
  115. OpStore %34 %52
  116. OpBranch %35
  117. %37 = OpLabel
  118. OpReturn
  119. OpFunctionEnd
  120. )";
  121. std::unique_ptr<IRContext> context =
  122. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  123. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  124. Module* module = context->module();
  125. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  126. << text << std::endl;
  127. Function& f = *module->begin();
  128. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  129. EXPECT_EQ(ld.NumLoops(), 2u);
  130. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  131. LoopFusion fusion(context.get(), loops[0], loops[1]);
  132. EXPECT_TRUE(fusion.AreCompatible());
  133. EXPECT_FALSE(fusion.IsLegal());
  134. }
  135. /*
  136. Generated from the following GLSL + --eliminate-local-multi-store
  137. #version 440 core
  138. int func() {
  139. return 10;
  140. }
  141. void main() {
  142. int[10] a;
  143. int[10] b;
  144. // Illegal, function call
  145. for (int i = 0; i < 10; i++) {
  146. a[i] = func();
  147. }
  148. for (int i = 0; i < 10; i++) {
  149. b[i] = a[i];
  150. }
  151. }
  152. */
  153. TEST_F(FusionIllegalTest, FunctionCall) {
  154. std::string text = R"(
  155. OpCapability Shader
  156. %1 = OpExtInstImport "GLSL.std.450"
  157. OpMemoryModel Logical GLSL450
  158. OpEntryPoint Fragment %4 "main"
  159. OpExecutionMode %4 OriginUpperLeft
  160. OpSource GLSL 440
  161. OpName %4 "main"
  162. OpName %8 "func("
  163. OpName %14 "i"
  164. OpName %28 "a"
  165. OpName %35 "i"
  166. OpName %43 "b"
  167. %2 = OpTypeVoid
  168. %3 = OpTypeFunction %2
  169. %6 = OpTypeInt 32 1
  170. %7 = OpTypeFunction %6
  171. %10 = OpConstant %6 10
  172. %13 = OpTypePointer Function %6
  173. %15 = OpConstant %6 0
  174. %22 = OpTypeBool
  175. %24 = OpTypeInt 32 0
  176. %25 = OpConstant %24 10
  177. %26 = OpTypeArray %6 %25
  178. %27 = OpTypePointer Function %26
  179. %33 = OpConstant %6 1
  180. %4 = OpFunction %2 None %3
  181. %5 = OpLabel
  182. %14 = OpVariable %13 Function
  183. %28 = OpVariable %27 Function
  184. %35 = OpVariable %13 Function
  185. %43 = OpVariable %27 Function
  186. OpStore %14 %15
  187. OpBranch %16
  188. %16 = OpLabel
  189. %51 = OpPhi %6 %15 %5 %34 %19
  190. OpLoopMerge %18 %19 None
  191. OpBranch %20
  192. %20 = OpLabel
  193. %23 = OpSLessThan %22 %51 %10
  194. OpBranchConditional %23 %17 %18
  195. %17 = OpLabel
  196. %30 = OpFunctionCall %6 %8
  197. %31 = OpAccessChain %13 %28 %51
  198. OpStore %31 %30
  199. OpBranch %19
  200. %19 = OpLabel
  201. %34 = OpIAdd %6 %51 %33
  202. OpStore %14 %34
  203. OpBranch %16
  204. %18 = OpLabel
  205. OpStore %35 %15
  206. OpBranch %36
  207. %36 = OpLabel
  208. %52 = OpPhi %6 %15 %18 %50 %39
  209. OpLoopMerge %38 %39 None
  210. OpBranch %40
  211. %40 = OpLabel
  212. %42 = OpSLessThan %22 %52 %10
  213. OpBranchConditional %42 %37 %38
  214. %37 = OpLabel
  215. %46 = OpAccessChain %13 %28 %52
  216. %47 = OpLoad %6 %46
  217. %48 = OpAccessChain %13 %43 %52
  218. OpStore %48 %47
  219. OpBranch %39
  220. %39 = OpLabel
  221. %50 = OpIAdd %6 %52 %33
  222. OpStore %35 %50
  223. OpBranch %36
  224. %38 = OpLabel
  225. OpReturn
  226. OpFunctionEnd
  227. %8 = OpFunction %6 None %7
  228. %9 = OpLabel
  229. OpReturnValue %10
  230. OpFunctionEnd
  231. )";
  232. std::unique_ptr<IRContext> context =
  233. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  234. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  235. Module* module = context->module();
  236. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  237. << text << std::endl;
  238. Function& f = *module->begin();
  239. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  240. EXPECT_EQ(ld.NumLoops(), 2u);
  241. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  242. LoopFusion fusion(context.get(), loops[0], loops[1]);
  243. EXPECT_TRUE(fusion.AreCompatible());
  244. EXPECT_FALSE(fusion.IsLegal());
  245. }
  246. /*
  247. Generated from the following GLSL + --eliminate-local-multi-store
  248. // 16
  249. #version 440 core
  250. void main() {
  251. int[10][10] a;
  252. int[10][10] b;
  253. int[10][10] c;
  254. // Illegal outer.
  255. for (int i = 0; i < 10; i++) {
  256. for (int j = 0; j < 10; j++) {
  257. c[i][j] = a[i][j] + 2;
  258. }
  259. }
  260. for (int i = 0; i < 10; i++) {
  261. for (int j = 0; j < 10; j++) {
  262. b[i][j] = c[i+1][j] + 10;
  263. }
  264. }
  265. }
  266. */
  267. TEST_F(FusionIllegalTest, PositiveDistanceCreatedRAWOuterLoop) {
  268. std::string text = R"(
  269. OpCapability Shader
  270. %1 = OpExtInstImport "GLSL.std.450"
  271. OpMemoryModel Logical GLSL450
  272. OpEntryPoint Fragment %4 "main"
  273. OpExecutionMode %4 OriginUpperLeft
  274. OpSource GLSL 440
  275. OpName %4 "main"
  276. OpName %8 "i"
  277. OpName %19 "j"
  278. OpName %32 "c"
  279. OpName %35 "a"
  280. OpName %48 "i"
  281. OpName %56 "j"
  282. OpName %64 "b"
  283. %2 = OpTypeVoid
  284. %3 = OpTypeFunction %2
  285. %6 = OpTypeInt 32 1
  286. %7 = OpTypePointer Function %6
  287. %9 = OpConstant %6 0
  288. %16 = OpConstant %6 10
  289. %17 = OpTypeBool
  290. %27 = OpTypeInt 32 0
  291. %28 = OpConstant %27 10
  292. %29 = OpTypeArray %6 %28
  293. %30 = OpTypeArray %29 %28
  294. %31 = OpTypePointer Function %30
  295. %40 = OpConstant %6 2
  296. %44 = OpConstant %6 1
  297. %4 = OpFunction %2 None %3
  298. %5 = OpLabel
  299. %8 = OpVariable %7 Function
  300. %19 = OpVariable %7 Function
  301. %32 = OpVariable %31 Function
  302. %35 = OpVariable %31 Function
  303. %48 = OpVariable %7 Function
  304. %56 = OpVariable %7 Function
  305. %64 = OpVariable %31 Function
  306. OpStore %8 %9
  307. OpBranch %10
  308. %10 = OpLabel
  309. %78 = OpPhi %6 %9 %5 %47 %13
  310. OpLoopMerge %12 %13 None
  311. OpBranch %14
  312. %14 = OpLabel
  313. %18 = OpSLessThan %17 %78 %16
  314. OpBranchConditional %18 %11 %12
  315. %11 = OpLabel
  316. OpStore %19 %9
  317. OpBranch %20
  318. %20 = OpLabel
  319. %82 = OpPhi %6 %9 %11 %45 %23
  320. OpLoopMerge %22 %23 None
  321. OpBranch %24
  322. %24 = OpLabel
  323. %26 = OpSLessThan %17 %82 %16
  324. OpBranchConditional %26 %21 %22
  325. %21 = OpLabel
  326. %38 = OpAccessChain %7 %35 %78 %82
  327. %39 = OpLoad %6 %38
  328. %41 = OpIAdd %6 %39 %40
  329. %42 = OpAccessChain %7 %32 %78 %82
  330. OpStore %42 %41
  331. OpBranch %23
  332. %23 = OpLabel
  333. %45 = OpIAdd %6 %82 %44
  334. OpStore %19 %45
  335. OpBranch %20
  336. %22 = OpLabel
  337. OpBranch %13
  338. %13 = OpLabel
  339. %47 = OpIAdd %6 %78 %44
  340. OpStore %8 %47
  341. OpBranch %10
  342. %12 = OpLabel
  343. OpStore %48 %9
  344. OpBranch %49
  345. %49 = OpLabel
  346. %79 = OpPhi %6 %9 %12 %77 %52
  347. OpLoopMerge %51 %52 None
  348. OpBranch %53
  349. %53 = OpLabel
  350. %55 = OpSLessThan %17 %79 %16
  351. OpBranchConditional %55 %50 %51
  352. %50 = OpLabel
  353. OpStore %56 %9
  354. OpBranch %57
  355. %57 = OpLabel
  356. %80 = OpPhi %6 %9 %50 %75 %60
  357. OpLoopMerge %59 %60 None
  358. OpBranch %61
  359. %61 = OpLabel
  360. %63 = OpSLessThan %17 %80 %16
  361. OpBranchConditional %63 %58 %59
  362. %58 = OpLabel
  363. %68 = OpIAdd %6 %79 %44
  364. %70 = OpAccessChain %7 %32 %68 %80
  365. %71 = OpLoad %6 %70
  366. %72 = OpIAdd %6 %71 %16
  367. %73 = OpAccessChain %7 %64 %79 %80
  368. OpStore %73 %72
  369. OpBranch %60
  370. %60 = OpLabel
  371. %75 = OpIAdd %6 %80 %44
  372. OpStore %56 %75
  373. OpBranch %57
  374. %59 = OpLabel
  375. OpBranch %52
  376. %52 = OpLabel
  377. %77 = OpIAdd %6 %79 %44
  378. OpStore %48 %77
  379. OpBranch %49
  380. %51 = OpLabel
  381. OpReturn
  382. OpFunctionEnd
  383. )";
  384. std::unique_ptr<IRContext> context =
  385. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  386. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  387. Module* module = context->module();
  388. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  389. << text << std::endl;
  390. Function& f = *module->begin();
  391. {
  392. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  393. EXPECT_EQ(ld.NumLoops(), 4u);
  394. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  395. auto loop_0 = loops[0];
  396. auto loop_1 = loops[1];
  397. auto loop_2 = loops[2];
  398. auto loop_3 = loops[3];
  399. {
  400. LoopFusion fusion(context.get(), loop_0, loop_1);
  401. EXPECT_FALSE(fusion.AreCompatible());
  402. }
  403. {
  404. LoopFusion fusion(context.get(), loop_0, loop_2);
  405. EXPECT_TRUE(fusion.AreCompatible());
  406. EXPECT_FALSE(fusion.IsLegal());
  407. }
  408. {
  409. LoopFusion fusion(context.get(), loop_1, loop_2);
  410. EXPECT_FALSE(fusion.AreCompatible());
  411. }
  412. {
  413. LoopFusion fusion(context.get(), loop_2, loop_3);
  414. EXPECT_FALSE(fusion.AreCompatible());
  415. }
  416. }
  417. }
  418. /*
  419. Generated from the following GLSL + --eliminate-local-multi-store
  420. // 19
  421. #version 440 core
  422. void main() {
  423. int[10] a;
  424. int[10] b;
  425. int[10] c;
  426. // Illegal, would create a backward loop-carried anti-dependence.
  427. for (int i = 0; i < 10; i++) {
  428. c[i] = a[i] + 1;
  429. }
  430. for (int i = 0; i < 10; i++) {
  431. a[i+1] = c[i] + 2;
  432. }
  433. }
  434. */
  435. TEST_F(FusionIllegalTest, PositiveDistanceCreatedWAR) {
  436. std::string text = R"(
  437. OpCapability Shader
  438. %1 = OpExtInstImport "GLSL.std.450"
  439. OpMemoryModel Logical GLSL450
  440. OpEntryPoint Fragment %4 "main"
  441. OpExecutionMode %4 OriginUpperLeft
  442. OpSource GLSL 440
  443. OpName %4 "main"
  444. OpName %8 "i"
  445. OpName %23 "c"
  446. OpName %25 "a"
  447. OpName %34 "i"
  448. %2 = OpTypeVoid
  449. %3 = OpTypeFunction %2
  450. %6 = OpTypeInt 32 1
  451. %7 = OpTypePointer Function %6
  452. %9 = OpConstant %6 0
  453. %16 = OpConstant %6 10
  454. %17 = OpTypeBool
  455. %19 = OpTypeInt 32 0
  456. %20 = OpConstant %19 10
  457. %21 = OpTypeArray %6 %20
  458. %22 = OpTypePointer Function %21
  459. %29 = OpConstant %6 1
  460. %47 = OpConstant %6 2
  461. %4 = OpFunction %2 None %3
  462. %5 = OpLabel
  463. %8 = OpVariable %7 Function
  464. %23 = OpVariable %22 Function
  465. %25 = OpVariable %22 Function
  466. %34 = OpVariable %7 Function
  467. OpStore %8 %9
  468. OpBranch %10
  469. %10 = OpLabel
  470. %52 = OpPhi %6 %9 %5 %33 %13
  471. OpLoopMerge %12 %13 None
  472. OpBranch %14
  473. %14 = OpLabel
  474. %18 = OpSLessThan %17 %52 %16
  475. OpBranchConditional %18 %11 %12
  476. %11 = OpLabel
  477. %27 = OpAccessChain %7 %25 %52
  478. %28 = OpLoad %6 %27
  479. %30 = OpIAdd %6 %28 %29
  480. %31 = OpAccessChain %7 %23 %52
  481. OpStore %31 %30
  482. OpBranch %13
  483. %13 = OpLabel
  484. %33 = OpIAdd %6 %52 %29
  485. OpStore %8 %33
  486. OpBranch %10
  487. %12 = OpLabel
  488. OpStore %34 %9
  489. OpBranch %35
  490. %35 = OpLabel
  491. %53 = OpPhi %6 %9 %12 %51 %38
  492. OpLoopMerge %37 %38 None
  493. OpBranch %39
  494. %39 = OpLabel
  495. %41 = OpSLessThan %17 %53 %16
  496. OpBranchConditional %41 %36 %37
  497. %36 = OpLabel
  498. %43 = OpIAdd %6 %53 %29
  499. %45 = OpAccessChain %7 %23 %53
  500. %46 = OpLoad %6 %45
  501. %48 = OpIAdd %6 %46 %47
  502. %49 = OpAccessChain %7 %25 %43
  503. OpStore %49 %48
  504. OpBranch %38
  505. %38 = OpLabel
  506. %51 = OpIAdd %6 %53 %29
  507. OpStore %34 %51
  508. OpBranch %35
  509. %37 = OpLabel
  510. OpReturn
  511. OpFunctionEnd
  512. )";
  513. std::unique_ptr<IRContext> context =
  514. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  515. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  516. Module* module = context->module();
  517. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  518. << text << std::endl;
  519. Function& f = *module->begin();
  520. {
  521. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  522. EXPECT_EQ(ld.NumLoops(), 2u);
  523. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  524. LoopFusion fusion(context.get(), loops[0], loops[1]);
  525. EXPECT_TRUE(fusion.AreCompatible());
  526. EXPECT_FALSE(fusion.IsLegal());
  527. }
  528. }
  529. /*
  530. Generated from the following GLSL + --eliminate-local-multi-store
  531. // 21
  532. #version 440 core
  533. void main() {
  534. int[10] a;
  535. int[10] b;
  536. int[10] c;
  537. // Illegal, would create a backward loop-carried anti-dependence.
  538. for (int i = 0; i < 10; i++) {
  539. a[i] = b[i] + 1;
  540. }
  541. for (int i = 0; i < 10; i++) {
  542. a[i+1] = c[i+1] + 2;
  543. }
  544. }
  545. */
  546. TEST_F(FusionIllegalTest, PositiveDistanceCreatedWAW) {
  547. std::string text = R"(
  548. OpCapability Shader
  549. %1 = OpExtInstImport "GLSL.std.450"
  550. OpMemoryModel Logical GLSL450
  551. OpEntryPoint Fragment %4 "main"
  552. OpExecutionMode %4 OriginUpperLeft
  553. OpSource GLSL 440
  554. OpName %4 "main"
  555. OpName %8 "i"
  556. OpName %23 "a"
  557. OpName %25 "b"
  558. OpName %34 "i"
  559. OpName %44 "c"
  560. %2 = OpTypeVoid
  561. %3 = OpTypeFunction %2
  562. %6 = OpTypeInt 32 1
  563. %7 = OpTypePointer Function %6
  564. %9 = OpConstant %6 0
  565. %16 = OpConstant %6 10
  566. %17 = OpTypeBool
  567. %19 = OpTypeInt 32 0
  568. %20 = OpConstant %19 10
  569. %21 = OpTypeArray %6 %20
  570. %22 = OpTypePointer Function %21
  571. %29 = OpConstant %6 1
  572. %49 = OpConstant %6 2
  573. %4 = OpFunction %2 None %3
  574. %5 = OpLabel
  575. %8 = OpVariable %7 Function
  576. %23 = OpVariable %22 Function
  577. %25 = OpVariable %22 Function
  578. %34 = OpVariable %7 Function
  579. %44 = OpVariable %22 Function
  580. OpStore %8 %9
  581. OpBranch %10
  582. %10 = OpLabel
  583. %54 = OpPhi %6 %9 %5 %33 %13
  584. OpLoopMerge %12 %13 None
  585. OpBranch %14
  586. %14 = OpLabel
  587. %18 = OpSLessThan %17 %54 %16
  588. OpBranchConditional %18 %11 %12
  589. %11 = OpLabel
  590. %27 = OpAccessChain %7 %25 %54
  591. %28 = OpLoad %6 %27
  592. %30 = OpIAdd %6 %28 %29
  593. %31 = OpAccessChain %7 %23 %54
  594. OpStore %31 %30
  595. OpBranch %13
  596. %13 = OpLabel
  597. %33 = OpIAdd %6 %54 %29
  598. OpStore %8 %33
  599. OpBranch %10
  600. %12 = OpLabel
  601. OpStore %34 %9
  602. OpBranch %35
  603. %35 = OpLabel
  604. %55 = OpPhi %6 %9 %12 %53 %38
  605. OpLoopMerge %37 %38 None
  606. OpBranch %39
  607. %39 = OpLabel
  608. %41 = OpSLessThan %17 %55 %16
  609. OpBranchConditional %41 %36 %37
  610. %36 = OpLabel
  611. %43 = OpIAdd %6 %55 %29
  612. %46 = OpIAdd %6 %55 %29
  613. %47 = OpAccessChain %7 %44 %46
  614. %48 = OpLoad %6 %47
  615. %50 = OpIAdd %6 %48 %49
  616. %51 = OpAccessChain %7 %23 %43
  617. OpStore %51 %50
  618. OpBranch %38
  619. %38 = OpLabel
  620. %53 = OpIAdd %6 %55 %29
  621. OpStore %34 %53
  622. OpBranch %35
  623. %37 = OpLabel
  624. OpReturn
  625. OpFunctionEnd
  626. )";
  627. std::unique_ptr<IRContext> context =
  628. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  629. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  630. Module* module = context->module();
  631. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  632. << text << std::endl;
  633. Function& f = *module->begin();
  634. {
  635. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  636. EXPECT_EQ(ld.NumLoops(), 2u);
  637. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  638. LoopFusion fusion(context.get(), loops[0], loops[1]);
  639. EXPECT_TRUE(fusion.AreCompatible());
  640. EXPECT_FALSE(fusion.IsLegal());
  641. }
  642. }
  643. /*
  644. Generated from the following GLSL + --eliminate-local-multi-store
  645. // 28
  646. #version 440 core
  647. void main() {
  648. int[10] a;
  649. int[10] b;
  650. int sum_0 = 0;
  651. // Illegal
  652. for (int i = 0; i < 10; i++) {
  653. sum_0 += a[i];
  654. }
  655. for (int j = 0; j < 10; j++) {
  656. sum_0 += b[j];
  657. }
  658. }
  659. */
  660. TEST_F(FusionIllegalTest, SameReductionVariable) {
  661. std::string text = R"(
  662. OpCapability Shader
  663. %1 = OpExtInstImport "GLSL.std.450"
  664. OpMemoryModel Logical GLSL450
  665. OpEntryPoint Fragment %4 "main"
  666. OpExecutionMode %4 OriginUpperLeft
  667. OpSource GLSL 440
  668. OpName %4 "main"
  669. OpName %8 "sum_0"
  670. OpName %10 "i"
  671. OpName %24 "a"
  672. OpName %33 "j"
  673. OpName %41 "b"
  674. %2 = OpTypeVoid
  675. %3 = OpTypeFunction %2
  676. %6 = OpTypeInt 32 1
  677. %7 = OpTypePointer Function %6
  678. %9 = OpConstant %6 0
  679. %17 = OpConstant %6 10
  680. %18 = OpTypeBool
  681. %20 = OpTypeInt 32 0
  682. %21 = OpConstant %20 10
  683. %22 = OpTypeArray %6 %21
  684. %23 = OpTypePointer Function %22
  685. %31 = OpConstant %6 1
  686. %4 = OpFunction %2 None %3
  687. %5 = OpLabel
  688. %8 = OpVariable %7 Function
  689. %10 = OpVariable %7 Function
  690. %24 = OpVariable %23 Function
  691. %33 = OpVariable %7 Function
  692. %41 = OpVariable %23 Function
  693. OpStore %8 %9
  694. OpStore %10 %9
  695. OpBranch %11
  696. %11 = OpLabel
  697. %52 = OpPhi %6 %9 %5 %29 %14
  698. %49 = OpPhi %6 %9 %5 %32 %14
  699. OpLoopMerge %13 %14 None
  700. OpBranch %15
  701. %15 = OpLabel
  702. %19 = OpSLessThan %18 %49 %17
  703. OpBranchConditional %19 %12 %13
  704. %12 = OpLabel
  705. %26 = OpAccessChain %7 %24 %49
  706. %27 = OpLoad %6 %26
  707. %29 = OpIAdd %6 %52 %27
  708. OpStore %8 %29
  709. OpBranch %14
  710. %14 = OpLabel
  711. %32 = OpIAdd %6 %49 %31
  712. OpStore %10 %32
  713. OpBranch %11
  714. %13 = OpLabel
  715. OpStore %33 %9
  716. OpBranch %34
  717. %34 = OpLabel
  718. %51 = OpPhi %6 %52 %13 %46 %37
  719. %50 = OpPhi %6 %9 %13 %48 %37
  720. OpLoopMerge %36 %37 None
  721. OpBranch %38
  722. %38 = OpLabel
  723. %40 = OpSLessThan %18 %50 %17
  724. OpBranchConditional %40 %35 %36
  725. %35 = OpLabel
  726. %43 = OpAccessChain %7 %41 %50
  727. %44 = OpLoad %6 %43
  728. %46 = OpIAdd %6 %51 %44
  729. OpStore %8 %46
  730. OpBranch %37
  731. %37 = OpLabel
  732. %48 = OpIAdd %6 %50 %31
  733. OpStore %33 %48
  734. OpBranch %34
  735. %36 = OpLabel
  736. OpReturn
  737. OpFunctionEnd
  738. )";
  739. std::unique_ptr<IRContext> context =
  740. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  741. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  742. Module* module = context->module();
  743. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  744. << text << std::endl;
  745. Function& f = *module->begin();
  746. {
  747. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  748. EXPECT_EQ(ld.NumLoops(), 2u);
  749. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  750. LoopFusion fusion(context.get(), loops[0], loops[1]);
  751. EXPECT_TRUE(fusion.AreCompatible());
  752. EXPECT_FALSE(fusion.IsLegal());
  753. }
  754. }
  755. /*
  756. Generated from the following GLSL + --eliminate-local-multi-store
  757. // 28
  758. #version 440 core
  759. void main() {
  760. int[10] a;
  761. int[10] b;
  762. int sum_0 = 0;
  763. // Illegal
  764. for (int i = 0; i < 10; i++) {
  765. sum_0 += a[i];
  766. }
  767. for (int j = 0; j < 10; j++) {
  768. sum_0 += b[j];
  769. }
  770. }
  771. */
  772. TEST_F(FusionIllegalTest, SameReductionVariableLCSSA) {
  773. std::string text = R"(
  774. OpCapability Shader
  775. %1 = OpExtInstImport "GLSL.std.450"
  776. OpMemoryModel Logical GLSL450
  777. OpEntryPoint Fragment %4 "main"
  778. OpExecutionMode %4 OriginUpperLeft
  779. OpSource GLSL 440
  780. OpName %4 "main"
  781. OpName %8 "sum_0"
  782. OpName %10 "i"
  783. OpName %24 "a"
  784. OpName %33 "j"
  785. OpName %41 "b"
  786. %2 = OpTypeVoid
  787. %3 = OpTypeFunction %2
  788. %6 = OpTypeInt 32 1
  789. %7 = OpTypePointer Function %6
  790. %9 = OpConstant %6 0
  791. %17 = OpConstant %6 10
  792. %18 = OpTypeBool
  793. %20 = OpTypeInt 32 0
  794. %21 = OpConstant %20 10
  795. %22 = OpTypeArray %6 %21
  796. %23 = OpTypePointer Function %22
  797. %31 = OpConstant %6 1
  798. %4 = OpFunction %2 None %3
  799. %5 = OpLabel
  800. %8 = OpVariable %7 Function
  801. %10 = OpVariable %7 Function
  802. %24 = OpVariable %23 Function
  803. %33 = OpVariable %7 Function
  804. %41 = OpVariable %23 Function
  805. OpStore %8 %9
  806. OpStore %10 %9
  807. OpBranch %11
  808. %11 = OpLabel
  809. %52 = OpPhi %6 %9 %5 %29 %14
  810. %49 = OpPhi %6 %9 %5 %32 %14
  811. OpLoopMerge %13 %14 None
  812. OpBranch %15
  813. %15 = OpLabel
  814. %19 = OpSLessThan %18 %49 %17
  815. OpBranchConditional %19 %12 %13
  816. %12 = OpLabel
  817. %26 = OpAccessChain %7 %24 %49
  818. %27 = OpLoad %6 %26
  819. %29 = OpIAdd %6 %52 %27
  820. OpStore %8 %29
  821. OpBranch %14
  822. %14 = OpLabel
  823. %32 = OpIAdd %6 %49 %31
  824. OpStore %10 %32
  825. OpBranch %11
  826. %13 = OpLabel
  827. OpStore %33 %9
  828. OpBranch %34
  829. %34 = OpLabel
  830. %51 = OpPhi %6 %52 %13 %46 %37
  831. %50 = OpPhi %6 %9 %13 %48 %37
  832. OpLoopMerge %36 %37 None
  833. OpBranch %38
  834. %38 = OpLabel
  835. %40 = OpSLessThan %18 %50 %17
  836. OpBranchConditional %40 %35 %36
  837. %35 = OpLabel
  838. %43 = OpAccessChain %7 %41 %50
  839. %44 = OpLoad %6 %43
  840. %46 = OpIAdd %6 %51 %44
  841. OpStore %8 %46
  842. OpBranch %37
  843. %37 = OpLabel
  844. %48 = OpIAdd %6 %50 %31
  845. OpStore %33 %48
  846. OpBranch %34
  847. %36 = OpLabel
  848. OpReturn
  849. OpFunctionEnd
  850. )";
  851. std::unique_ptr<IRContext> context =
  852. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  853. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  854. Module* module = context->module();
  855. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  856. << text << std::endl;
  857. Function& f = *module->begin();
  858. {
  859. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  860. EXPECT_EQ(ld.NumLoops(), 2u);
  861. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  862. LoopUtils utils_0(context.get(), loops[0]);
  863. utils_0.MakeLoopClosedSSA();
  864. LoopFusion fusion(context.get(), loops[0], loops[1]);
  865. EXPECT_TRUE(fusion.AreCompatible());
  866. EXPECT_FALSE(fusion.IsLegal());
  867. }
  868. }
  869. /*
  870. Generated from the following GLSL + --eliminate-local-multi-store
  871. // 30
  872. #version 440 core
  873. int x;
  874. void main() {
  875. int[10] a;
  876. int[10] b;
  877. // Illegal, x is unknown.
  878. for (int i = 0; i < 10; i++) {
  879. a[x] = a[i];
  880. }
  881. for (int j = 0; j < 10; j++) {
  882. a[j] = b[j];
  883. }
  884. }
  885. */
  886. TEST_F(FusionIllegalTest, UnknownIndexVariable) {
  887. std::string text = R"(
  888. OpCapability Shader
  889. %1 = OpExtInstImport "GLSL.std.450"
  890. OpMemoryModel Logical GLSL450
  891. OpEntryPoint Fragment %4 "main"
  892. OpExecutionMode %4 OriginUpperLeft
  893. OpSource GLSL 440
  894. OpName %4 "main"
  895. OpName %8 "i"
  896. OpName %23 "a"
  897. OpName %25 "x"
  898. OpName %34 "j"
  899. OpName %43 "b"
  900. %2 = OpTypeVoid
  901. %3 = OpTypeFunction %2
  902. %6 = OpTypeInt 32 1
  903. %7 = OpTypePointer Function %6
  904. %9 = OpConstant %6 0
  905. %16 = OpConstant %6 10
  906. %17 = OpTypeBool
  907. %19 = OpTypeInt 32 0
  908. %20 = OpConstant %19 10
  909. %21 = OpTypeArray %6 %20
  910. %22 = OpTypePointer Function %21
  911. %24 = OpTypePointer Private %6
  912. %25 = OpVariable %24 Private
  913. %32 = OpConstant %6 1
  914. %4 = OpFunction %2 None %3
  915. %5 = OpLabel
  916. %8 = OpVariable %7 Function
  917. %23 = OpVariable %22 Function
  918. %34 = OpVariable %7 Function
  919. %43 = OpVariable %22 Function
  920. OpStore %8 %9
  921. OpBranch %10
  922. %10 = OpLabel
  923. %50 = OpPhi %6 %9 %5 %33 %13
  924. OpLoopMerge %12 %13 None
  925. OpBranch %14
  926. %14 = OpLabel
  927. %18 = OpSLessThan %17 %50 %16
  928. OpBranchConditional %18 %11 %12
  929. %11 = OpLabel
  930. %26 = OpLoad %6 %25
  931. %28 = OpAccessChain %7 %23 %50
  932. %29 = OpLoad %6 %28
  933. %30 = OpAccessChain %7 %23 %26
  934. OpStore %30 %29
  935. OpBranch %13
  936. %13 = OpLabel
  937. %33 = OpIAdd %6 %50 %32
  938. OpStore %8 %33
  939. OpBranch %10
  940. %12 = OpLabel
  941. OpStore %34 %9
  942. OpBranch %35
  943. %35 = OpLabel
  944. %51 = OpPhi %6 %9 %12 %49 %38
  945. OpLoopMerge %37 %38 None
  946. OpBranch %39
  947. %39 = OpLabel
  948. %41 = OpSLessThan %17 %51 %16
  949. OpBranchConditional %41 %36 %37
  950. %36 = OpLabel
  951. %45 = OpAccessChain %7 %43 %51
  952. %46 = OpLoad %6 %45
  953. %47 = OpAccessChain %7 %23 %51
  954. OpStore %47 %46
  955. OpBranch %38
  956. %38 = OpLabel
  957. %49 = OpIAdd %6 %51 %32
  958. OpStore %34 %49
  959. OpBranch %35
  960. %37 = OpLabel
  961. OpReturn
  962. OpFunctionEnd
  963. )";
  964. std::unique_ptr<IRContext> context =
  965. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  966. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  967. Module* module = context->module();
  968. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  969. << text << std::endl;
  970. Function& f = *module->begin();
  971. {
  972. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  973. EXPECT_EQ(ld.NumLoops(), 2u);
  974. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  975. LoopFusion fusion(context.get(), loops[0], loops[1]);
  976. EXPECT_TRUE(fusion.AreCompatible());
  977. EXPECT_FALSE(fusion.IsLegal());
  978. }
  979. }
  980. /*
  981. Generated from the following GLSL + --eliminate-local-multi-store
  982. #version 440 core
  983. void main() {
  984. int[10] a;
  985. int[10] b;
  986. int sum = 0;
  987. // Illegal, accumulator used for indexing.
  988. for (int i = 0; i < 10; i++) {
  989. sum += a[i];
  990. b[sum] = a[i];
  991. }
  992. for (int j = 0; j < 10; j++) {
  993. b[j] = b[j]+1;
  994. }
  995. }
  996. */
  997. TEST_F(FusionIllegalTest, AccumulatorIndexing) {
  998. std::string text = R"(
  999. OpCapability Shader
  1000. %1 = OpExtInstImport "GLSL.std.450"
  1001. OpMemoryModel Logical GLSL450
  1002. OpEntryPoint Fragment %4 "main"
  1003. OpExecutionMode %4 OriginUpperLeft
  1004. OpSource GLSL 440
  1005. OpName %4 "main"
  1006. OpName %8 "sum"
  1007. OpName %10 "i"
  1008. OpName %24 "a"
  1009. OpName %30 "b"
  1010. OpName %39 "j"
  1011. %2 = OpTypeVoid
  1012. %3 = OpTypeFunction %2
  1013. %6 = OpTypeInt 32 1
  1014. %7 = OpTypePointer Function %6
  1015. %9 = OpConstant %6 0
  1016. %17 = OpConstant %6 10
  1017. %18 = OpTypeBool
  1018. %20 = OpTypeInt 32 0
  1019. %21 = OpConstant %20 10
  1020. %22 = OpTypeArray %6 %21
  1021. %23 = OpTypePointer Function %22
  1022. %37 = OpConstant %6 1
  1023. %4 = OpFunction %2 None %3
  1024. %5 = OpLabel
  1025. %8 = OpVariable %7 Function
  1026. %10 = OpVariable %7 Function
  1027. %24 = OpVariable %23 Function
  1028. %30 = OpVariable %23 Function
  1029. %39 = OpVariable %7 Function
  1030. OpStore %8 %9
  1031. OpStore %10 %9
  1032. OpBranch %11
  1033. %11 = OpLabel
  1034. %57 = OpPhi %6 %9 %5 %29 %14
  1035. %55 = OpPhi %6 %9 %5 %38 %14
  1036. OpLoopMerge %13 %14 None
  1037. OpBranch %15
  1038. %15 = OpLabel
  1039. %19 = OpSLessThan %18 %55 %17
  1040. OpBranchConditional %19 %12 %13
  1041. %12 = OpLabel
  1042. %26 = OpAccessChain %7 %24 %55
  1043. %27 = OpLoad %6 %26
  1044. %29 = OpIAdd %6 %57 %27
  1045. OpStore %8 %29
  1046. %33 = OpAccessChain %7 %24 %55
  1047. %34 = OpLoad %6 %33
  1048. %35 = OpAccessChain %7 %30 %29
  1049. OpStore %35 %34
  1050. OpBranch %14
  1051. %14 = OpLabel
  1052. %38 = OpIAdd %6 %55 %37
  1053. OpStore %10 %38
  1054. OpBranch %11
  1055. %13 = OpLabel
  1056. OpStore %39 %9
  1057. OpBranch %40
  1058. %40 = OpLabel
  1059. %56 = OpPhi %6 %9 %13 %54 %43
  1060. OpLoopMerge %42 %43 None
  1061. OpBranch %44
  1062. %44 = OpLabel
  1063. %46 = OpSLessThan %18 %56 %17
  1064. OpBranchConditional %46 %41 %42
  1065. %41 = OpLabel
  1066. %49 = OpAccessChain %7 %30 %56
  1067. %50 = OpLoad %6 %49
  1068. %51 = OpIAdd %6 %50 %37
  1069. %52 = OpAccessChain %7 %30 %56
  1070. OpStore %52 %51
  1071. OpBranch %43
  1072. %43 = OpLabel
  1073. %54 = OpIAdd %6 %56 %37
  1074. OpStore %39 %54
  1075. OpBranch %40
  1076. %42 = OpLabel
  1077. OpReturn
  1078. OpFunctionEnd
  1079. )";
  1080. std::unique_ptr<IRContext> context =
  1081. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1082. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1083. Module* module = context->module();
  1084. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  1085. << text << std::endl;
  1086. Function& f = *module->begin();
  1087. {
  1088. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  1089. EXPECT_EQ(ld.NumLoops(), 2u);
  1090. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  1091. LoopFusion fusion(context.get(), loops[0], loops[1]);
  1092. EXPECT_TRUE(fusion.AreCompatible());
  1093. EXPECT_FALSE(fusion.IsLegal());
  1094. }
  1095. }
  1096. /*
  1097. Generated from the following GLSL + --eliminate-local-multi-store
  1098. // 33
  1099. #version 440 core
  1100. void main() {
  1101. int[10] a;
  1102. int[10] b;
  1103. // Illegal, barrier.
  1104. for (int i = 0; i < 10; i++) {
  1105. a[i] = a[i] * 2;
  1106. memoryBarrier();
  1107. }
  1108. for (int j = 0; j < 10; j++) {
  1109. b[j] = b[j] + 1;
  1110. }
  1111. }
  1112. */
  1113. TEST_F(FusionIllegalTest, Barrier) {
  1114. std::string text = R"(
  1115. OpCapability Shader
  1116. %1 = OpExtInstImport "GLSL.std.450"
  1117. OpMemoryModel Logical GLSL450
  1118. OpEntryPoint Fragment %4 "main"
  1119. OpExecutionMode %4 OriginUpperLeft
  1120. OpSource GLSL 440
  1121. OpName %4 "main"
  1122. OpName %8 "i"
  1123. OpName %23 "a"
  1124. OpName %36 "j"
  1125. OpName %44 "b"
  1126. %2 = OpTypeVoid
  1127. %3 = OpTypeFunction %2
  1128. %6 = OpTypeInt 32 1
  1129. %7 = OpTypePointer Function %6
  1130. %9 = OpConstant %6 0
  1131. %16 = OpConstant %6 10
  1132. %17 = OpTypeBool
  1133. %19 = OpTypeInt 32 0
  1134. %20 = OpConstant %19 10
  1135. %21 = OpTypeArray %6 %20
  1136. %22 = OpTypePointer Function %21
  1137. %28 = OpConstant %6 2
  1138. %31 = OpConstant %19 1
  1139. %32 = OpConstant %19 3400
  1140. %34 = OpConstant %6 1
  1141. %4 = OpFunction %2 None %3
  1142. %5 = OpLabel
  1143. %8 = OpVariable %7 Function
  1144. %23 = OpVariable %22 Function
  1145. %36 = OpVariable %7 Function
  1146. %44 = OpVariable %22 Function
  1147. OpStore %8 %9
  1148. OpBranch %10
  1149. %10 = OpLabel
  1150. %53 = OpPhi %6 %9 %5 %35 %13
  1151. OpLoopMerge %12 %13 None
  1152. OpBranch %14
  1153. %14 = OpLabel
  1154. %18 = OpSLessThan %17 %53 %16
  1155. OpBranchConditional %18 %11 %12
  1156. %11 = OpLabel
  1157. %26 = OpAccessChain %7 %23 %53
  1158. %27 = OpLoad %6 %26
  1159. %29 = OpIMul %6 %27 %28
  1160. %30 = OpAccessChain %7 %23 %53
  1161. OpStore %30 %29
  1162. OpMemoryBarrier %31 %32
  1163. OpBranch %13
  1164. %13 = OpLabel
  1165. %35 = OpIAdd %6 %53 %34
  1166. OpStore %8 %35
  1167. OpBranch %10
  1168. %12 = OpLabel
  1169. OpStore %36 %9
  1170. OpBranch %37
  1171. %37 = OpLabel
  1172. %54 = OpPhi %6 %9 %12 %52 %40
  1173. OpLoopMerge %39 %40 None
  1174. OpBranch %41
  1175. %41 = OpLabel
  1176. %43 = OpSLessThan %17 %54 %16
  1177. OpBranchConditional %43 %38 %39
  1178. %38 = OpLabel
  1179. %47 = OpAccessChain %7 %44 %54
  1180. %48 = OpLoad %6 %47
  1181. %49 = OpIAdd %6 %48 %34
  1182. %50 = OpAccessChain %7 %44 %54
  1183. OpStore %50 %49
  1184. OpBranch %40
  1185. %40 = OpLabel
  1186. %52 = OpIAdd %6 %54 %34
  1187. OpStore %36 %52
  1188. OpBranch %37
  1189. %39 = OpLabel
  1190. OpReturn
  1191. OpFunctionEnd
  1192. )";
  1193. std::unique_ptr<IRContext> context =
  1194. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1195. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1196. Module* module = context->module();
  1197. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  1198. << text << std::endl;
  1199. Function& f = *module->begin();
  1200. {
  1201. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  1202. EXPECT_EQ(ld.NumLoops(), 2u);
  1203. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  1204. LoopFusion fusion(context.get(), loops[0], loops[1]);
  1205. EXPECT_TRUE(fusion.AreCompatible());
  1206. EXPECT_FALSE(fusion.IsLegal());
  1207. }
  1208. }
  1209. /*
  1210. Generated from the following GLSL + --eliminate-local-multi-store
  1211. #version 440 core
  1212. struct TestStruct {
  1213. int[10] a;
  1214. int b;
  1215. };
  1216. void main() {
  1217. TestStruct test_0;
  1218. TestStruct test_1;
  1219. for (int i = 0; i < 10; i++) {
  1220. test_0.a[i] = i;
  1221. }
  1222. for (int j = 0; j < 10; j++) {
  1223. test_0 = test_1;
  1224. }
  1225. }
  1226. */
  1227. TEST_F(FusionIllegalTest, ArrayInStruct) {
  1228. std::string text = R"(
  1229. OpCapability Shader
  1230. %1 = OpExtInstImport "GLSL.std.450"
  1231. OpMemoryModel Logical GLSL450
  1232. OpEntryPoint Fragment %4 "main"
  1233. OpExecutionMode %4 OriginUpperLeft
  1234. OpSource GLSL 440
  1235. OpName %4 "main"
  1236. OpName %8 "i"
  1237. OpName %22 "TestStruct"
  1238. OpMemberName %22 0 "a"
  1239. OpMemberName %22 1 "b"
  1240. OpName %24 "test_0"
  1241. OpName %31 "j"
  1242. OpName %39 "test_1"
  1243. %2 = OpTypeVoid
  1244. %3 = OpTypeFunction %2
  1245. %6 = OpTypeInt 32 1
  1246. %7 = OpTypePointer Function %6
  1247. %9 = OpConstant %6 0
  1248. %16 = OpConstant %6 10
  1249. %17 = OpTypeBool
  1250. %19 = OpTypeInt 32 0
  1251. %20 = OpConstant %19 10
  1252. %21 = OpTypeArray %6 %20
  1253. %22 = OpTypeStruct %21 %6
  1254. %23 = OpTypePointer Function %22
  1255. %29 = OpConstant %6 1
  1256. %47 = OpUndef %22
  1257. %4 = OpFunction %2 None %3
  1258. %5 = OpLabel
  1259. %8 = OpVariable %7 Function
  1260. %24 = OpVariable %23 Function
  1261. %31 = OpVariable %7 Function
  1262. %39 = OpVariable %23 Function
  1263. OpStore %8 %9
  1264. OpBranch %10
  1265. %10 = OpLabel
  1266. %43 = OpPhi %6 %9 %5 %30 %13
  1267. OpLoopMerge %12 %13 None
  1268. OpBranch %14
  1269. %14 = OpLabel
  1270. %18 = OpSLessThan %17 %43 %16
  1271. OpBranchConditional %18 %11 %12
  1272. %11 = OpLabel
  1273. %27 = OpAccessChain %7 %24 %9 %43
  1274. OpStore %27 %43
  1275. OpBranch %13
  1276. %13 = OpLabel
  1277. %30 = OpIAdd %6 %43 %29
  1278. OpStore %8 %30
  1279. OpBranch %10
  1280. %12 = OpLabel
  1281. OpStore %31 %9
  1282. OpBranch %32
  1283. %32 = OpLabel
  1284. %44 = OpPhi %6 %9 %12 %42 %35
  1285. OpLoopMerge %34 %35 None
  1286. OpBranch %36
  1287. %36 = OpLabel
  1288. %38 = OpSLessThan %17 %44 %16
  1289. OpBranchConditional %38 %33 %34
  1290. %33 = OpLabel
  1291. OpStore %24 %47
  1292. OpBranch %35
  1293. %35 = OpLabel
  1294. %42 = OpIAdd %6 %44 %29
  1295. OpStore %31 %42
  1296. OpBranch %32
  1297. %34 = OpLabel
  1298. OpReturn
  1299. OpFunctionEnd
  1300. )";
  1301. std::unique_ptr<IRContext> context =
  1302. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1303. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1304. Module* module = context->module();
  1305. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  1306. << text << std::endl;
  1307. Function& f = *module->begin();
  1308. {
  1309. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  1310. EXPECT_EQ(ld.NumLoops(), 2u);
  1311. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  1312. LoopFusion fusion(context.get(), loops[0], loops[1]);
  1313. EXPECT_TRUE(fusion.AreCompatible());
  1314. EXPECT_FALSE(fusion.IsLegal());
  1315. }
  1316. }
  1317. /*
  1318. Generated from the following GLSL + --eliminate-local-multi-store
  1319. #version 450
  1320. struct P {float x,y,z;};
  1321. uniform G { int a; P b[2]; int c; } g;
  1322. layout(location = 0) out float o;
  1323. void main()
  1324. {
  1325. P p[2];
  1326. for (int i = 0; i < 2; ++i) {
  1327. p = g.b;
  1328. }
  1329. for (int j = 0; j < 2; ++j) {
  1330. o = p[g.a].x;
  1331. }
  1332. }
  1333. */
  1334. TEST_F(FusionIllegalTest, NestedAccessChain) {
  1335. std::string text = R"(
  1336. OpCapability Shader
  1337. %1 = OpExtInstImport "GLSL.std.450"
  1338. OpMemoryModel Logical GLSL450
  1339. OpEntryPoint Fragment %4 "main" %64
  1340. OpExecutionMode %4 OriginUpperLeft
  1341. OpSource GLSL 450
  1342. OpName %4 "main"
  1343. OpName %8 "i"
  1344. OpName %20 "P"
  1345. OpMemberName %20 0 "x"
  1346. OpMemberName %20 1 "y"
  1347. OpMemberName %20 2 "z"
  1348. OpName %25 "p"
  1349. OpName %26 "P"
  1350. OpMemberName %26 0 "x"
  1351. OpMemberName %26 1 "y"
  1352. OpMemberName %26 2 "z"
  1353. OpName %28 "G"
  1354. OpMemberName %28 0 "a"
  1355. OpMemberName %28 1 "b"
  1356. OpMemberName %28 2 "c"
  1357. OpName %30 "g"
  1358. OpName %55 "j"
  1359. OpName %64 "o"
  1360. OpMemberDecorate %26 0 Offset 0
  1361. OpMemberDecorate %26 1 Offset 4
  1362. OpMemberDecorate %26 2 Offset 8
  1363. OpDecorate %27 ArrayStride 16
  1364. OpMemberDecorate %28 0 Offset 0
  1365. OpMemberDecorate %28 1 Offset 16
  1366. OpMemberDecorate %28 2 Offset 48
  1367. OpDecorate %28 Block
  1368. OpDecorate %30 DescriptorSet 0
  1369. OpDecorate %64 Location 0
  1370. %2 = OpTypeVoid
  1371. %3 = OpTypeFunction %2
  1372. %6 = OpTypeInt 32 1
  1373. %7 = OpTypePointer Function %6
  1374. %9 = OpConstant %6 0
  1375. %16 = OpConstant %6 2
  1376. %17 = OpTypeBool
  1377. %19 = OpTypeFloat 32
  1378. %20 = OpTypeStruct %19 %19 %19
  1379. %21 = OpTypeInt 32 0
  1380. %22 = OpConstant %21 2
  1381. %23 = OpTypeArray %20 %22
  1382. %24 = OpTypePointer Function %23
  1383. %26 = OpTypeStruct %19 %19 %19
  1384. %27 = OpTypeArray %26 %22
  1385. %28 = OpTypeStruct %6 %27 %6
  1386. %29 = OpTypePointer Uniform %28
  1387. %30 = OpVariable %29 Uniform
  1388. %31 = OpConstant %6 1
  1389. %32 = OpTypePointer Uniform %27
  1390. %36 = OpTypePointer Function %20
  1391. %39 = OpTypePointer Function %19
  1392. %63 = OpTypePointer Output %19
  1393. %64 = OpVariable %63 Output
  1394. %65 = OpTypePointer Uniform %6
  1395. %4 = OpFunction %2 None %3
  1396. %5 = OpLabel
  1397. %8 = OpVariable %7 Function
  1398. %25 = OpVariable %24 Function
  1399. %55 = OpVariable %7 Function
  1400. OpStore %8 %9
  1401. OpBranch %10
  1402. %10 = OpLabel
  1403. %72 = OpPhi %6 %9 %5 %54 %13
  1404. OpLoopMerge %12 %13 None
  1405. OpBranch %14
  1406. %14 = OpLabel
  1407. %18 = OpSLessThan %17 %72 %16
  1408. OpBranchConditional %18 %11 %12
  1409. %11 = OpLabel
  1410. %33 = OpAccessChain %32 %30 %31
  1411. %34 = OpLoad %27 %33
  1412. %35 = OpCompositeExtract %26 %34 0
  1413. %37 = OpAccessChain %36 %25 %9
  1414. %38 = OpCompositeExtract %19 %35 0
  1415. %40 = OpAccessChain %39 %37 %9
  1416. OpStore %40 %38
  1417. %41 = OpCompositeExtract %19 %35 1
  1418. %42 = OpAccessChain %39 %37 %31
  1419. OpStore %42 %41
  1420. %43 = OpCompositeExtract %19 %35 2
  1421. %44 = OpAccessChain %39 %37 %16
  1422. OpStore %44 %43
  1423. %45 = OpCompositeExtract %26 %34 1
  1424. %46 = OpAccessChain %36 %25 %31
  1425. %47 = OpCompositeExtract %19 %45 0
  1426. %48 = OpAccessChain %39 %46 %9
  1427. OpStore %48 %47
  1428. %49 = OpCompositeExtract %19 %45 1
  1429. %50 = OpAccessChain %39 %46 %31
  1430. OpStore %50 %49
  1431. %51 = OpCompositeExtract %19 %45 2
  1432. %52 = OpAccessChain %39 %46 %16
  1433. OpStore %52 %51
  1434. OpBranch %13
  1435. %13 = OpLabel
  1436. %54 = OpIAdd %6 %72 %31
  1437. OpStore %8 %54
  1438. OpBranch %10
  1439. %12 = OpLabel
  1440. OpStore %55 %9
  1441. OpBranch %56
  1442. %56 = OpLabel
  1443. %73 = OpPhi %6 %9 %12 %71 %59
  1444. OpLoopMerge %58 %59 None
  1445. OpBranch %60
  1446. %60 = OpLabel
  1447. %62 = OpSLessThan %17 %73 %16
  1448. OpBranchConditional %62 %57 %58
  1449. %57 = OpLabel
  1450. %66 = OpAccessChain %65 %30 %9
  1451. %67 = OpLoad %6 %66
  1452. %68 = OpAccessChain %39 %25 %67 %9
  1453. %69 = OpLoad %19 %68
  1454. OpStore %64 %69
  1455. OpBranch %59
  1456. %59 = OpLabel
  1457. %71 = OpIAdd %6 %73 %31
  1458. OpStore %55 %71
  1459. OpBranch %56
  1460. %58 = OpLabel
  1461. OpReturn
  1462. OpFunctionEnd
  1463. )";
  1464. std::unique_ptr<IRContext> context =
  1465. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1466. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1467. Module* module = context->module();
  1468. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  1469. << text << std::endl;
  1470. Function& f = *module->begin();
  1471. {
  1472. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  1473. EXPECT_EQ(ld.NumLoops(), 2u);
  1474. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  1475. LoopFusion fusion(context.get(), loops[0], loops[1]);
  1476. EXPECT_TRUE(fusion.AreCompatible());
  1477. EXPECT_FALSE(fusion.IsLegal());
  1478. }
  1479. }
  1480. } // namespace
  1481. } // namespace opt
  1482. } // namespace spvtools