fusion_illegal.cpp 46 KB

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