fusion_legal.cpp 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577
  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 "effcee/effcee.h"
  17. #include "gmock/gmock.h"
  18. #include "source/opt/loop_descriptor.h"
  19. #include "source/opt/loop_fusion.h"
  20. #include "test/opt/pass_fixture.h"
  21. namespace spvtools {
  22. namespace opt {
  23. namespace {
  24. using FusionLegalTest = PassTest<::testing::Test>;
  25. bool Validate(const std::vector<uint32_t>& bin) {
  26. spv_target_env target_env = SPV_ENV_UNIVERSAL_1_2;
  27. spv_context spvContext = spvContextCreate(target_env);
  28. spv_diagnostic diagnostic = nullptr;
  29. spv_const_binary_t binary = {bin.data(), bin.size()};
  30. spv_result_t error = spvValidate(spvContext, &binary, &diagnostic);
  31. if (error != 0) spvDiagnosticPrint(diagnostic);
  32. spvDiagnosticDestroy(diagnostic);
  33. spvContextDestroy(spvContext);
  34. return error == 0;
  35. }
  36. void Match(const std::string& checks, IRContext* context) {
  37. // Silence unused warnings with !defined(SPIRV_EFFCE)
  38. (void)checks;
  39. std::vector<uint32_t> bin;
  40. context->module()->ToBinary(&bin, true);
  41. EXPECT_TRUE(Validate(bin));
  42. std::string assembly;
  43. SpirvTools tools(SPV_ENV_UNIVERSAL_1_2);
  44. EXPECT_TRUE(
  45. tools.Disassemble(bin, &assembly, SPV_BINARY_TO_TEXT_OPTION_NO_HEADER))
  46. << "Disassembling failed for shader:\n"
  47. << assembly << std::endl;
  48. auto match_result = effcee::Match(assembly, checks);
  49. EXPECT_EQ(effcee::Result::Status::Ok, match_result.status())
  50. << match_result.message() << "\nChecking result:\n"
  51. << assembly;
  52. }
  53. /*
  54. Generated from the following GLSL + --eliminate-local-multi-store
  55. #version 440 core
  56. void main() {
  57. int[10] a;
  58. int[10] b;
  59. // No dependence, legal
  60. for (int i = 0; i < 10; i++) {
  61. a[i] = a[i]*2;
  62. }
  63. for (int i = 0; i < 10; i++) {
  64. b[i] = b[i]+2;
  65. }
  66. }
  67. */
  68. TEST_F(FusionLegalTest, DifferentArraysInLoops) {
  69. std::string text = R"(
  70. OpCapability Shader
  71. %1 = OpExtInstImport "GLSL.std.450"
  72. OpMemoryModel Logical GLSL450
  73. OpEntryPoint Fragment %4 "main"
  74. OpExecutionMode %4 OriginUpperLeft
  75. OpSource GLSL 440
  76. OpName %4 "main"
  77. OpName %8 "i"
  78. OpName %23 "a"
  79. OpName %34 "i"
  80. OpName %42 "b"
  81. %2 = OpTypeVoid
  82. %3 = OpTypeFunction %2
  83. %6 = OpTypeInt 32 1
  84. %7 = OpTypePointer Function %6
  85. %9 = OpConstant %6 0
  86. %16 = OpConstant %6 10
  87. %17 = OpTypeBool
  88. %19 = OpTypeInt 32 0
  89. %20 = OpConstant %19 10
  90. %21 = OpTypeArray %6 %20
  91. %22 = OpTypePointer Function %21
  92. %28 = OpConstant %6 2
  93. %32 = OpConstant %6 1
  94. %4 = OpFunction %2 None %3
  95. %5 = OpLabel
  96. %8 = OpVariable %7 Function
  97. %23 = OpVariable %22 Function
  98. %34 = OpVariable %7 Function
  99. %42 = OpVariable %22 Function
  100. OpStore %8 %9
  101. OpBranch %10
  102. %10 = OpLabel
  103. %51 = OpPhi %6 %9 %5 %33 %13
  104. OpLoopMerge %12 %13 None
  105. OpBranch %14
  106. %14 = OpLabel
  107. %18 = OpSLessThan %17 %51 %16
  108. OpBranchConditional %18 %11 %12
  109. %11 = OpLabel
  110. %26 = OpAccessChain %7 %23 %51
  111. %27 = OpLoad %6 %26
  112. %29 = OpIMul %6 %27 %28
  113. %30 = OpAccessChain %7 %23 %51
  114. OpStore %30 %29
  115. OpBranch %13
  116. %13 = OpLabel
  117. %33 = OpIAdd %6 %51 %32
  118. OpStore %8 %33
  119. OpBranch %10
  120. %12 = OpLabel
  121. OpStore %34 %9
  122. OpBranch %35
  123. %35 = OpLabel
  124. %52 = OpPhi %6 %9 %12 %50 %38
  125. OpLoopMerge %37 %38 None
  126. OpBranch %39
  127. %39 = OpLabel
  128. %41 = OpSLessThan %17 %52 %16
  129. OpBranchConditional %41 %36 %37
  130. %36 = OpLabel
  131. %45 = OpAccessChain %7 %42 %52
  132. %46 = OpLoad %6 %45
  133. %47 = OpIAdd %6 %46 %28
  134. %48 = OpAccessChain %7 %42 %52
  135. OpStore %48 %47
  136. OpBranch %38
  137. %38 = OpLabel
  138. %50 = OpIAdd %6 %52 %32
  139. OpStore %34 %50
  140. OpBranch %35
  141. %37 = OpLabel
  142. OpReturn
  143. OpFunctionEnd
  144. )";
  145. std::unique_ptr<IRContext> context =
  146. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  147. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  148. Module* module = context->module();
  149. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  150. << text << std::endl;
  151. Function& f = *module->begin();
  152. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  153. EXPECT_EQ(ld.NumLoops(), 2u);
  154. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  155. LoopFusion fusion(context.get(), loops[0], loops[1]);
  156. EXPECT_TRUE(fusion.AreCompatible());
  157. EXPECT_TRUE(fusion.IsLegal());
  158. fusion.Fuse();
  159. std::string checks = R"(
  160. CHECK: [[PHI:%\w+]] = OpPhi
  161. CHECK-NEXT: OpLoopMerge
  162. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  163. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  164. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  165. CHECK-NEXT: OpStore [[STORE_0]]
  166. CHECK-NOT: OpPhi
  167. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  168. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  169. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  170. CHECK-NEXT: OpStore [[STORE_1]]
  171. )";
  172. Match(checks, context.get());
  173. auto& ld_final = *context->GetLoopDescriptor(&f);
  174. EXPECT_EQ(ld_final.NumLoops(), 1u);
  175. }
  176. /*
  177. Generated from the following GLSL + --eliminate-local-multi-store
  178. #version 440 core
  179. void main() {
  180. int[10] a;
  181. int[10] b;
  182. int[10] c;
  183. // Only loads to the same array, legal
  184. for (int i = 0; i < 10; i++) {
  185. b[i] = a[i]*2;
  186. }
  187. for (int i = 0; i < 10; i++) {
  188. c[i] = a[i]+2;
  189. }
  190. }
  191. */
  192. TEST_F(FusionLegalTest, OnlyLoadsToSameArray) {
  193. std::string text = R"(
  194. OpCapability Shader
  195. %1 = OpExtInstImport "GLSL.std.450"
  196. OpMemoryModel Logical GLSL450
  197. OpEntryPoint Fragment %4 "main"
  198. OpExecutionMode %4 OriginUpperLeft
  199. OpSource GLSL 440
  200. OpName %4 "main"
  201. OpName %8 "i"
  202. OpName %23 "b"
  203. OpName %25 "a"
  204. OpName %35 "i"
  205. OpName %43 "c"
  206. %2 = OpTypeVoid
  207. %3 = OpTypeFunction %2
  208. %6 = OpTypeInt 32 1
  209. %7 = OpTypePointer Function %6
  210. %9 = OpConstant %6 0
  211. %16 = OpConstant %6 10
  212. %17 = OpTypeBool
  213. %19 = OpTypeInt 32 0
  214. %20 = OpConstant %19 10
  215. %21 = OpTypeArray %6 %20
  216. %22 = OpTypePointer Function %21
  217. %29 = OpConstant %6 2
  218. %33 = OpConstant %6 1
  219. %4 = OpFunction %2 None %3
  220. %5 = OpLabel
  221. %8 = OpVariable %7 Function
  222. %23 = OpVariable %22 Function
  223. %25 = OpVariable %22 Function
  224. %35 = OpVariable %7 Function
  225. %43 = OpVariable %22 Function
  226. OpStore %8 %9
  227. OpBranch %10
  228. %10 = OpLabel
  229. %52 = OpPhi %6 %9 %5 %34 %13
  230. OpLoopMerge %12 %13 None
  231. OpBranch %14
  232. %14 = OpLabel
  233. %18 = OpSLessThan %17 %52 %16
  234. OpBranchConditional %18 %11 %12
  235. %11 = OpLabel
  236. %27 = OpAccessChain %7 %25 %52
  237. %28 = OpLoad %6 %27
  238. %30 = OpIMul %6 %28 %29
  239. %31 = OpAccessChain %7 %23 %52
  240. OpStore %31 %30
  241. OpBranch %13
  242. %13 = OpLabel
  243. %34 = OpIAdd %6 %52 %33
  244. OpStore %8 %34
  245. OpBranch %10
  246. %12 = OpLabel
  247. OpStore %35 %9
  248. OpBranch %36
  249. %36 = OpLabel
  250. %53 = OpPhi %6 %9 %12 %51 %39
  251. OpLoopMerge %38 %39 None
  252. OpBranch %40
  253. %40 = OpLabel
  254. %42 = OpSLessThan %17 %53 %16
  255. OpBranchConditional %42 %37 %38
  256. %37 = OpLabel
  257. %46 = OpAccessChain %7 %25 %53
  258. %47 = OpLoad %6 %46
  259. %48 = OpIAdd %6 %47 %29
  260. %49 = OpAccessChain %7 %43 %53
  261. OpStore %49 %48
  262. OpBranch %39
  263. %39 = OpLabel
  264. %51 = OpIAdd %6 %53 %33
  265. OpStore %35 %51
  266. OpBranch %36
  267. %38 = OpLabel
  268. OpReturn
  269. OpFunctionEnd
  270. )";
  271. std::unique_ptr<IRContext> context =
  272. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  273. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  274. Module* module = context->module();
  275. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  276. << text << std::endl;
  277. Function& f = *module->begin();
  278. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  279. EXPECT_EQ(ld.NumLoops(), 2u);
  280. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  281. LoopFusion fusion(context.get(), loops[0], loops[1]);
  282. EXPECT_TRUE(fusion.AreCompatible());
  283. EXPECT_TRUE(fusion.IsLegal());
  284. fusion.Fuse();
  285. std::string checks = R"(
  286. CHECK: [[PHI:%\w+]] = OpPhi
  287. CHECK-NEXT: OpLoopMerge
  288. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  289. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  290. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  291. CHECK-NEXT: OpStore [[STORE_0]]
  292. CHECK-NOT: OpPhi
  293. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  294. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  295. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  296. CHECK-NEXT: OpStore [[STORE_1]]
  297. )";
  298. Match(checks, context.get());
  299. auto& ld_final = *context->GetLoopDescriptor(&f);
  300. EXPECT_EQ(ld_final.NumLoops(), 1u);
  301. }
  302. /*
  303. Generated from the following GLSL + --eliminate-local-multi-store
  304. #version 440 core
  305. void main() {
  306. int[10] a;
  307. int[10] b;
  308. // No loop-carried dependences, legal
  309. for (int i = 0; i < 10; i++) {
  310. a[i] = a[i]*2;
  311. }
  312. for (int i = 0; i < 10; i++) {
  313. b[i] = a[i]+2;
  314. }
  315. }
  316. */
  317. TEST_F(FusionLegalTest, NoLoopCarriedDependences) {
  318. std::string text = R"(
  319. OpCapability Shader
  320. %1 = OpExtInstImport "GLSL.std.450"
  321. OpMemoryModel Logical GLSL450
  322. OpEntryPoint Fragment %4 "main"
  323. OpExecutionMode %4 OriginUpperLeft
  324. OpSource GLSL 440
  325. OpName %4 "main"
  326. OpName %8 "i"
  327. OpName %23 "a"
  328. OpName %34 "i"
  329. OpName %42 "b"
  330. %2 = OpTypeVoid
  331. %3 = OpTypeFunction %2
  332. %6 = OpTypeInt 32 1
  333. %7 = OpTypePointer Function %6
  334. %9 = OpConstant %6 0
  335. %16 = OpConstant %6 10
  336. %17 = OpTypeBool
  337. %19 = OpTypeInt 32 0
  338. %20 = OpConstant %19 10
  339. %21 = OpTypeArray %6 %20
  340. %22 = OpTypePointer Function %21
  341. %28 = OpConstant %6 2
  342. %32 = OpConstant %6 1
  343. %4 = OpFunction %2 None %3
  344. %5 = OpLabel
  345. %8 = OpVariable %7 Function
  346. %23 = OpVariable %22 Function
  347. %34 = OpVariable %7 Function
  348. %42 = OpVariable %22 Function
  349. OpStore %8 %9
  350. OpBranch %10
  351. %10 = OpLabel
  352. %51 = OpPhi %6 %9 %5 %33 %13
  353. OpLoopMerge %12 %13 None
  354. OpBranch %14
  355. %14 = OpLabel
  356. %18 = OpSLessThan %17 %51 %16
  357. OpBranchConditional %18 %11 %12
  358. %11 = OpLabel
  359. %26 = OpAccessChain %7 %23 %51
  360. %27 = OpLoad %6 %26
  361. %29 = OpIMul %6 %27 %28
  362. %30 = OpAccessChain %7 %23 %51
  363. OpStore %30 %29
  364. OpBranch %13
  365. %13 = OpLabel
  366. %33 = OpIAdd %6 %51 %32
  367. OpStore %8 %33
  368. OpBranch %10
  369. %12 = OpLabel
  370. OpStore %34 %9
  371. OpBranch %35
  372. %35 = OpLabel
  373. %52 = OpPhi %6 %9 %12 %50 %38
  374. OpLoopMerge %37 %38 None
  375. OpBranch %39
  376. %39 = OpLabel
  377. %41 = OpSLessThan %17 %52 %16
  378. OpBranchConditional %41 %36 %37
  379. %36 = OpLabel
  380. %45 = OpAccessChain %7 %23 %52
  381. %46 = OpLoad %6 %45
  382. %47 = OpIAdd %6 %46 %28
  383. %48 = OpAccessChain %7 %42 %52
  384. OpStore %48 %47
  385. OpBranch %38
  386. %38 = OpLabel
  387. %50 = OpIAdd %6 %52 %32
  388. OpStore %34 %50
  389. OpBranch %35
  390. %37 = OpLabel
  391. OpReturn
  392. OpFunctionEnd
  393. )";
  394. std::unique_ptr<IRContext> context =
  395. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  396. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  397. Module* module = context->module();
  398. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  399. << text << std::endl;
  400. Function& f = *module->begin();
  401. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  402. EXPECT_EQ(ld.NumLoops(), 2u);
  403. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  404. LoopFusion fusion(context.get(), loops[0], loops[1]);
  405. EXPECT_TRUE(fusion.AreCompatible());
  406. EXPECT_TRUE(fusion.IsLegal());
  407. fusion.Fuse();
  408. std::string checks = R"(
  409. CHECK: [[PHI:%\w+]] = OpPhi
  410. CHECK-NEXT: OpLoopMerge
  411. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  412. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  413. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  414. CHECK-NEXT: OpStore [[STORE_0]]
  415. CHECK-NOT: OpPhi
  416. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  417. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  418. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  419. CHECK-NEXT: OpStore [[STORE_1]]
  420. )";
  421. Match(checks, context.get());
  422. auto& ld_final = *context->GetLoopDescriptor(&f);
  423. EXPECT_EQ(ld_final.NumLoops(), 1u);
  424. }
  425. /*
  426. Generated from the following GLSL + --eliminate-local-multi-store
  427. #version 440 core
  428. void main() {
  429. int[10] a;
  430. int[10] b;
  431. int[10] c;
  432. // Parallelism inhibiting, but legal.
  433. for (int i = 0; i < 10; i++) {
  434. a[i] = b[i] + 1;
  435. }
  436. for (int i = 0; i < 10; i++) {
  437. c[i] = a[i] + c[i-1];
  438. }
  439. }
  440. */
  441. TEST_F(FusionLegalTest, ExistingLoopCarriedDependence) {
  442. std::string text = R"(
  443. OpCapability Shader
  444. %1 = OpExtInstImport "GLSL.std.450"
  445. OpMemoryModel Logical GLSL450
  446. OpEntryPoint Fragment %4 "main"
  447. OpExecutionMode %4 OriginUpperLeft
  448. OpSource GLSL 440
  449. OpName %4 "main"
  450. OpName %8 "i"
  451. OpName %23 "a"
  452. OpName %25 "b"
  453. OpName %34 "i"
  454. OpName %42 "c"
  455. %2 = OpTypeVoid
  456. %3 = OpTypeFunction %2
  457. %6 = OpTypeInt 32 1
  458. %7 = OpTypePointer Function %6
  459. %9 = OpConstant %6 0
  460. %16 = OpConstant %6 10
  461. %17 = OpTypeBool
  462. %19 = OpTypeInt 32 0
  463. %20 = OpConstant %19 10
  464. %21 = OpTypeArray %6 %20
  465. %22 = OpTypePointer Function %21
  466. %29 = OpConstant %6 1
  467. %4 = OpFunction %2 None %3
  468. %5 = OpLabel
  469. %8 = OpVariable %7 Function
  470. %23 = OpVariable %22 Function
  471. %25 = OpVariable %22 Function
  472. %34 = OpVariable %7 Function
  473. %42 = OpVariable %22 Function
  474. OpStore %8 %9
  475. OpBranch %10
  476. %10 = OpLabel
  477. %55 = OpPhi %6 %9 %5 %33 %13
  478. OpLoopMerge %12 %13 None
  479. OpBranch %14
  480. %14 = OpLabel
  481. %18 = OpSLessThan %17 %55 %16
  482. OpBranchConditional %18 %11 %12
  483. %11 = OpLabel
  484. %27 = OpAccessChain %7 %25 %55
  485. %28 = OpLoad %6 %27
  486. %30 = OpIAdd %6 %28 %29
  487. %31 = OpAccessChain %7 %23 %55
  488. OpStore %31 %30
  489. OpBranch %13
  490. %13 = OpLabel
  491. %33 = OpIAdd %6 %55 %29
  492. OpStore %8 %33
  493. OpBranch %10
  494. %12 = OpLabel
  495. OpStore %34 %9
  496. OpBranch %35
  497. %35 = OpLabel
  498. %56 = OpPhi %6 %9 %12 %54 %38
  499. OpLoopMerge %37 %38 None
  500. OpBranch %39
  501. %39 = OpLabel
  502. %41 = OpSLessThan %17 %56 %16
  503. OpBranchConditional %41 %36 %37
  504. %36 = OpLabel
  505. %45 = OpAccessChain %7 %23 %56
  506. %46 = OpLoad %6 %45
  507. %48 = OpISub %6 %56 %29
  508. %49 = OpAccessChain %7 %42 %48
  509. %50 = OpLoad %6 %49
  510. %51 = OpIAdd %6 %46 %50
  511. %52 = OpAccessChain %7 %42 %56
  512. OpStore %52 %51
  513. OpBranch %38
  514. %38 = OpLabel
  515. %54 = OpIAdd %6 %56 %29
  516. OpStore %34 %54
  517. OpBranch %35
  518. %37 = OpLabel
  519. OpReturn
  520. OpFunctionEnd
  521. )";
  522. std::unique_ptr<IRContext> context =
  523. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  524. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  525. Module* module = context->module();
  526. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  527. << text << std::endl;
  528. Function& f = *module->begin();
  529. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  530. EXPECT_EQ(ld.NumLoops(), 2u);
  531. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  532. LoopFusion fusion(context.get(), loops[0], loops[1]);
  533. EXPECT_TRUE(fusion.AreCompatible());
  534. EXPECT_TRUE(fusion.IsLegal());
  535. fusion.Fuse();
  536. std::string checks = R"(
  537. CHECK: [[PHI:%\w+]] = OpPhi
  538. CHECK-NEXT: OpLoopMerge
  539. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  540. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  541. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  542. CHECK-NEXT: OpStore [[STORE_0]]
  543. CHECK-NOT: OpPhi
  544. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  545. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  546. CHECK: [[I_1:%\w+]] = OpISub {{%\w+}} [[PHI]] {{%\w+}}
  547. CHECK-NEXT: [[LOAD_2:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_1]]
  548. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_2]]
  549. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  550. CHECK-NEXT: OpStore [[STORE_1]]
  551. )";
  552. Match(checks, context.get());
  553. auto& ld_final = *context->GetLoopDescriptor(&f);
  554. EXPECT_EQ(ld_final.NumLoops(), 1u);
  555. }
  556. /*
  557. Generated from the following GLSL + --eliminate-local-multi-store
  558. #version 440 core
  559. void main() {
  560. int[10] a;
  561. int[10] b;
  562. int[10] c;
  563. // Creates a loop-carried dependence, but negative, so legal
  564. for (int i = 0; i < 10; i++) {
  565. a[i+1] = b[i] + 1;
  566. }
  567. for (int i = 0; i < 10; i++) {
  568. c[i] = a[i] + 2;
  569. }
  570. }
  571. */
  572. TEST_F(FusionLegalTest, NegativeDistanceCreatedRAW) {
  573. std::string text = R"(
  574. OpCapability Shader
  575. %1 = OpExtInstImport "GLSL.std.450"
  576. OpMemoryModel Logical GLSL450
  577. OpEntryPoint Fragment %4 "main"
  578. OpExecutionMode %4 OriginUpperLeft
  579. OpSource GLSL 440
  580. OpName %4 "main"
  581. OpName %8 "i"
  582. OpName %23 "a"
  583. OpName %27 "b"
  584. OpName %35 "i"
  585. OpName %43 "c"
  586. %2 = OpTypeVoid
  587. %3 = OpTypeFunction %2
  588. %6 = OpTypeInt 32 1
  589. %7 = OpTypePointer Function %6
  590. %9 = OpConstant %6 0
  591. %16 = OpConstant %6 10
  592. %17 = OpTypeBool
  593. %19 = OpTypeInt 32 0
  594. %20 = OpConstant %19 10
  595. %21 = OpTypeArray %6 %20
  596. %22 = OpTypePointer Function %21
  597. %25 = OpConstant %6 1
  598. %48 = OpConstant %6 2
  599. %4 = OpFunction %2 None %3
  600. %5 = OpLabel
  601. %8 = OpVariable %7 Function
  602. %23 = OpVariable %22 Function
  603. %27 = OpVariable %22 Function
  604. %35 = OpVariable %7 Function
  605. %43 = OpVariable %22 Function
  606. OpStore %8 %9
  607. OpBranch %10
  608. %10 = OpLabel
  609. %53 = OpPhi %6 %9 %5 %34 %13
  610. OpLoopMerge %12 %13 None
  611. OpBranch %14
  612. %14 = OpLabel
  613. %18 = OpSLessThan %17 %53 %16
  614. OpBranchConditional %18 %11 %12
  615. %11 = OpLabel
  616. %26 = OpIAdd %6 %53 %25
  617. %29 = OpAccessChain %7 %27 %53
  618. %30 = OpLoad %6 %29
  619. %31 = OpIAdd %6 %30 %25
  620. %32 = OpAccessChain %7 %23 %26
  621. OpStore %32 %31
  622. OpBranch %13
  623. %13 = OpLabel
  624. %34 = OpIAdd %6 %53 %25
  625. OpStore %8 %34
  626. OpBranch %10
  627. %12 = OpLabel
  628. OpStore %35 %9
  629. OpBranch %36
  630. %36 = OpLabel
  631. %54 = OpPhi %6 %9 %12 %52 %39
  632. OpLoopMerge %38 %39 None
  633. OpBranch %40
  634. %40 = OpLabel
  635. %42 = OpSLessThan %17 %54 %16
  636. OpBranchConditional %42 %37 %38
  637. %37 = OpLabel
  638. %46 = OpAccessChain %7 %23 %54
  639. %47 = OpLoad %6 %46
  640. %49 = OpIAdd %6 %47 %48
  641. %50 = OpAccessChain %7 %43 %54
  642. OpStore %50 %49
  643. OpBranch %39
  644. %39 = OpLabel
  645. %52 = OpIAdd %6 %54 %25
  646. OpStore %35 %52
  647. OpBranch %36
  648. %38 = OpLabel
  649. OpReturn
  650. OpFunctionEnd
  651. )";
  652. std::unique_ptr<IRContext> context =
  653. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  654. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  655. Module* module = context->module();
  656. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  657. << text << std::endl;
  658. Function& f = *module->begin();
  659. {
  660. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  661. EXPECT_EQ(ld.NumLoops(), 2u);
  662. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  663. LoopFusion fusion(context.get(), loops[0], loops[1]);
  664. EXPECT_TRUE(fusion.AreCompatible());
  665. EXPECT_TRUE(fusion.IsLegal());
  666. fusion.Fuse();
  667. std::string checks = R"(
  668. CHECK: [[PHI:%\w+]] = OpPhi
  669. CHECK-NEXT: OpLoopMerge
  670. CHECK: [[I_1:%\w+]] = OpIAdd {{%\w+}} [[PHI]] {{%\w+}}
  671. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  672. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  673. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_1]]
  674. CHECK-NEXT: OpStore [[STORE_0]]
  675. CHECK-NOT: OpPhi
  676. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  677. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  678. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  679. CHECK-NEXT: OpStore [[STORE_1]]
  680. )";
  681. Match(checks, context.get());
  682. }
  683. {
  684. auto& ld = *context->GetLoopDescriptor(&f);
  685. EXPECT_EQ(ld.NumLoops(), 1u);
  686. }
  687. }
  688. /*
  689. Generated from the following GLSL + --eliminate-local-multi-store
  690. #version 440 core
  691. void main() {
  692. int[10] a;
  693. int[10] b;
  694. int[10] c;
  695. // Legal
  696. for (int i = 0; i < 10; i++) {
  697. a[i+1] = b[i] + 1;
  698. }
  699. for (int i = 0; i < 10; i++) {
  700. c[i] = a[i+1] + 2;
  701. }
  702. }
  703. */
  704. TEST_F(FusionLegalTest, NoLoopCarriedDependencesAdjustedIndex) {
  705. std::string text = R"(
  706. OpCapability Shader
  707. %1 = OpExtInstImport "GLSL.std.450"
  708. OpMemoryModel Logical GLSL450
  709. OpEntryPoint Fragment %4 "main"
  710. OpExecutionMode %4 OriginUpperLeft
  711. OpSource GLSL 440
  712. OpName %4 "main"
  713. OpName %8 "i"
  714. OpName %23 "a"
  715. OpName %27 "b"
  716. OpName %35 "i"
  717. OpName %43 "c"
  718. %2 = OpTypeVoid
  719. %3 = OpTypeFunction %2
  720. %6 = OpTypeInt 32 1
  721. %7 = OpTypePointer Function %6
  722. %9 = OpConstant %6 0
  723. %16 = OpConstant %6 10
  724. %17 = OpTypeBool
  725. %19 = OpTypeInt 32 0
  726. %20 = OpConstant %19 10
  727. %21 = OpTypeArray %6 %20
  728. %22 = OpTypePointer Function %21
  729. %25 = OpConstant %6 1
  730. %49 = OpConstant %6 2
  731. %4 = OpFunction %2 None %3
  732. %5 = OpLabel
  733. %8 = OpVariable %7 Function
  734. %23 = OpVariable %22 Function
  735. %27 = OpVariable %22 Function
  736. %35 = OpVariable %7 Function
  737. %43 = OpVariable %22 Function
  738. OpStore %8 %9
  739. OpBranch %10
  740. %10 = OpLabel
  741. %54 = OpPhi %6 %9 %5 %34 %13
  742. OpLoopMerge %12 %13 None
  743. OpBranch %14
  744. %14 = OpLabel
  745. %18 = OpSLessThan %17 %54 %16
  746. OpBranchConditional %18 %11 %12
  747. %11 = OpLabel
  748. %26 = OpIAdd %6 %54 %25
  749. %29 = OpAccessChain %7 %27 %54
  750. %30 = OpLoad %6 %29
  751. %31 = OpIAdd %6 %30 %25
  752. %32 = OpAccessChain %7 %23 %26
  753. OpStore %32 %31
  754. OpBranch %13
  755. %13 = OpLabel
  756. %34 = OpIAdd %6 %54 %25
  757. OpStore %8 %34
  758. OpBranch %10
  759. %12 = OpLabel
  760. OpStore %35 %9
  761. OpBranch %36
  762. %36 = OpLabel
  763. %55 = OpPhi %6 %9 %12 %53 %39
  764. OpLoopMerge %38 %39 None
  765. OpBranch %40
  766. %40 = OpLabel
  767. %42 = OpSLessThan %17 %55 %16
  768. OpBranchConditional %42 %37 %38
  769. %37 = OpLabel
  770. %46 = OpIAdd %6 %55 %25
  771. %47 = OpAccessChain %7 %23 %46
  772. %48 = OpLoad %6 %47
  773. %50 = OpIAdd %6 %48 %49
  774. %51 = OpAccessChain %7 %43 %55
  775. OpStore %51 %50
  776. OpBranch %39
  777. %39 = OpLabel
  778. %53 = OpIAdd %6 %55 %25
  779. OpStore %35 %53
  780. OpBranch %36
  781. %38 = OpLabel
  782. OpReturn
  783. OpFunctionEnd
  784. )";
  785. std::unique_ptr<IRContext> context =
  786. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  787. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  788. Module* module = context->module();
  789. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  790. << text << std::endl;
  791. Function& f = *module->begin();
  792. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  793. EXPECT_EQ(ld.NumLoops(), 2u);
  794. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  795. LoopFusion fusion(context.get(), loops[0], loops[1]);
  796. EXPECT_TRUE(fusion.AreCompatible());
  797. EXPECT_TRUE(fusion.IsLegal());
  798. fusion.Fuse();
  799. std::string checks = R"(
  800. CHECK: [[PHI:%\w+]] = OpPhi
  801. CHECK-NEXT: OpLoopMerge
  802. CHECK: [[I_1:%\w+]] = OpIAdd {{%\w+}} [[PHI]] {{%\w+}}
  803. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  804. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  805. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_1]]
  806. CHECK-NEXT: OpStore [[STORE_0]]
  807. CHECK-NOT: OpPhi
  808. CHECK: [[I_1:%\w+]] = OpIAdd {{%\w+}} [[PHI]] {{%\w+}}
  809. CHECK-NEXT: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_1]]
  810. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  811. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  812. CHECK-NEXT: OpStore [[STORE_1]]
  813. )";
  814. Match(checks, context.get());
  815. auto& ld_final = *context->GetLoopDescriptor(&f);
  816. EXPECT_EQ(ld_final.NumLoops(), 1u);
  817. }
  818. /*
  819. Generated from the following GLSL + --eliminate-local-multi-store
  820. #version 440 core
  821. void main() {
  822. int[10] a;
  823. int[10] b;
  824. int[10] c;
  825. // Legal, independent locations in |a|, SIV
  826. for (int i = 0; i < 10; i++) {
  827. a[2*i+1] = b[i] + 1;
  828. }
  829. for (int i = 0; i < 10; i++) {
  830. c[i] = a[2*i] + 2;
  831. }
  832. }
  833. */
  834. TEST_F(FusionLegalTest, IndependentSIV) {
  835. std::string text = R"(
  836. OpCapability Shader
  837. %1 = OpExtInstImport "GLSL.std.450"
  838. OpMemoryModel Logical GLSL450
  839. OpEntryPoint Fragment %4 "main"
  840. OpExecutionMode %4 OriginUpperLeft
  841. OpSource GLSL 440
  842. OpName %4 "main"
  843. OpName %8 "i"
  844. OpName %23 "a"
  845. OpName %29 "b"
  846. OpName %37 "i"
  847. OpName %45 "c"
  848. %2 = OpTypeVoid
  849. %3 = OpTypeFunction %2
  850. %6 = OpTypeInt 32 1
  851. %7 = OpTypePointer Function %6
  852. %9 = OpConstant %6 0
  853. %16 = OpConstant %6 10
  854. %17 = OpTypeBool
  855. %19 = OpTypeInt 32 0
  856. %20 = OpConstant %19 10
  857. %21 = OpTypeArray %6 %20
  858. %22 = OpTypePointer Function %21
  859. %24 = OpConstant %6 2
  860. %27 = OpConstant %6 1
  861. %4 = OpFunction %2 None %3
  862. %5 = OpLabel
  863. %8 = OpVariable %7 Function
  864. %23 = OpVariable %22 Function
  865. %29 = OpVariable %22 Function
  866. %37 = OpVariable %7 Function
  867. %45 = OpVariable %22 Function
  868. OpStore %8 %9
  869. OpBranch %10
  870. %10 = OpLabel
  871. %55 = OpPhi %6 %9 %5 %36 %13
  872. OpLoopMerge %12 %13 None
  873. OpBranch %14
  874. %14 = OpLabel
  875. %18 = OpSLessThan %17 %55 %16
  876. OpBranchConditional %18 %11 %12
  877. %11 = OpLabel
  878. %26 = OpIMul %6 %24 %55
  879. %28 = OpIAdd %6 %26 %27
  880. %31 = OpAccessChain %7 %29 %55
  881. %32 = OpLoad %6 %31
  882. %33 = OpIAdd %6 %32 %27
  883. %34 = OpAccessChain %7 %23 %28
  884. OpStore %34 %33
  885. OpBranch %13
  886. %13 = OpLabel
  887. %36 = OpIAdd %6 %55 %27
  888. OpStore %8 %36
  889. OpBranch %10
  890. %12 = OpLabel
  891. OpStore %37 %9
  892. OpBranch %38
  893. %38 = OpLabel
  894. %56 = OpPhi %6 %9 %12 %54 %41
  895. OpLoopMerge %40 %41 None
  896. OpBranch %42
  897. %42 = OpLabel
  898. %44 = OpSLessThan %17 %56 %16
  899. OpBranchConditional %44 %39 %40
  900. %39 = OpLabel
  901. %48 = OpIMul %6 %24 %56
  902. %49 = OpAccessChain %7 %23 %48
  903. %50 = OpLoad %6 %49
  904. %51 = OpIAdd %6 %50 %24
  905. %52 = OpAccessChain %7 %45 %56
  906. OpStore %52 %51
  907. OpBranch %41
  908. %41 = OpLabel
  909. %54 = OpIAdd %6 %56 %27
  910. OpStore %37 %54
  911. OpBranch %38
  912. %40 = OpLabel
  913. OpReturn
  914. OpFunctionEnd
  915. )";
  916. std::unique_ptr<IRContext> context =
  917. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  918. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  919. Module* module = context->module();
  920. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  921. << text << std::endl;
  922. Function& f = *module->begin();
  923. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  924. EXPECT_EQ(ld.NumLoops(), 2u);
  925. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  926. LoopFusion fusion(context.get(), loops[0], loops[1]);
  927. EXPECT_TRUE(fusion.AreCompatible());
  928. EXPECT_TRUE(fusion.IsLegal());
  929. fusion.Fuse();
  930. std::string checks = R"(
  931. CHECK: [[PHI:%\w+]] = OpPhi
  932. CHECK-NEXT: OpLoopMerge
  933. CHECK: [[I_2:%\w+]] = OpIMul {{%\w+}} {{%\w+}} [[PHI]]
  934. CHECK-NEXT: [[I_2_1:%\w+]] = OpIAdd {{%\w+}} [[I_2]] {{%\w+}}
  935. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  936. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  937. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_2_1]]
  938. CHECK-NEXT: OpStore [[STORE_0]]
  939. CHECK-NOT: OpPhi
  940. CHECK: [[I_2:%\w+]] = OpIMul {{%\w+}} {{%\w+}} [[PHI]]
  941. CHECK-NEXT: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_2]]
  942. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  943. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  944. CHECK-NEXT: OpStore [[STORE_1]]
  945. )";
  946. Match(checks, context.get());
  947. auto& ld_final = *context->GetLoopDescriptor(&f);
  948. EXPECT_EQ(ld_final.NumLoops(), 1u);
  949. }
  950. /*
  951. Generated from the following GLSL + --eliminate-local-multi-store
  952. #version 440 core
  953. void main() {
  954. int[10] a;
  955. int[10] b;
  956. int[10] c;
  957. // Legal, independent locations in |a|, ZIV
  958. for (int i = 0; i < 10; i++) {
  959. a[1] = b[i] + 1;
  960. }
  961. for (int i = 0; i < 10; i++) {
  962. c[i] = a[9] + 2;
  963. }
  964. }
  965. */
  966. TEST_F(FusionLegalTest, IndependentZIV) {
  967. std::string text = R"(
  968. OpCapability Shader
  969. %1 = OpExtInstImport "GLSL.std.450"
  970. OpMemoryModel Logical GLSL450
  971. OpEntryPoint Fragment %4 "main"
  972. OpExecutionMode %4 OriginUpperLeft
  973. OpSource GLSL 440
  974. OpName %4 "main"
  975. OpName %8 "i"
  976. OpName %23 "a"
  977. OpName %25 "b"
  978. OpName %33 "i"
  979. OpName %41 "c"
  980. %2 = OpTypeVoid
  981. %3 = OpTypeFunction %2
  982. %6 = OpTypeInt 32 1
  983. %7 = OpTypePointer Function %6
  984. %9 = OpConstant %6 0
  985. %16 = OpConstant %6 10
  986. %17 = OpTypeBool
  987. %19 = OpTypeInt 32 0
  988. %20 = OpConstant %19 10
  989. %21 = OpTypeArray %6 %20
  990. %22 = OpTypePointer Function %21
  991. %24 = OpConstant %6 1
  992. %43 = OpConstant %6 9
  993. %46 = OpConstant %6 2
  994. %4 = OpFunction %2 None %3
  995. %5 = OpLabel
  996. %8 = OpVariable %7 Function
  997. %23 = OpVariable %22 Function
  998. %25 = OpVariable %22 Function
  999. %33 = OpVariable %7 Function
  1000. %41 = OpVariable %22 Function
  1001. OpStore %8 %9
  1002. OpBranch %10
  1003. %10 = OpLabel
  1004. %51 = OpPhi %6 %9 %5 %32 %13
  1005. OpLoopMerge %12 %13 None
  1006. OpBranch %14
  1007. %14 = OpLabel
  1008. %18 = OpSLessThan %17 %51 %16
  1009. OpBranchConditional %18 %11 %12
  1010. %11 = OpLabel
  1011. %27 = OpAccessChain %7 %25 %51
  1012. %28 = OpLoad %6 %27
  1013. %29 = OpIAdd %6 %28 %24
  1014. %30 = OpAccessChain %7 %23 %24
  1015. OpStore %30 %29
  1016. OpBranch %13
  1017. %13 = OpLabel
  1018. %32 = OpIAdd %6 %51 %24
  1019. OpStore %8 %32
  1020. OpBranch %10
  1021. %12 = OpLabel
  1022. OpStore %33 %9
  1023. OpBranch %34
  1024. %34 = OpLabel
  1025. %52 = OpPhi %6 %9 %12 %50 %37
  1026. OpLoopMerge %36 %37 None
  1027. OpBranch %38
  1028. %38 = OpLabel
  1029. %40 = OpSLessThan %17 %52 %16
  1030. OpBranchConditional %40 %35 %36
  1031. %35 = OpLabel
  1032. %44 = OpAccessChain %7 %23 %43
  1033. %45 = OpLoad %6 %44
  1034. %47 = OpIAdd %6 %45 %46
  1035. %48 = OpAccessChain %7 %41 %52
  1036. OpStore %48 %47
  1037. OpBranch %37
  1038. %37 = OpLabel
  1039. %50 = OpIAdd %6 %52 %24
  1040. OpStore %33 %50
  1041. OpBranch %34
  1042. %36 = OpLabel
  1043. OpReturn
  1044. OpFunctionEnd
  1045. )";
  1046. std::unique_ptr<IRContext> context =
  1047. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1048. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1049. Module* module = context->module();
  1050. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  1051. << text << std::endl;
  1052. Function& f = *module->begin();
  1053. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  1054. EXPECT_EQ(ld.NumLoops(), 2u);
  1055. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  1056. LoopFusion fusion(context.get(), loops[0], loops[1]);
  1057. EXPECT_TRUE(fusion.AreCompatible());
  1058. EXPECT_TRUE(fusion.IsLegal());
  1059. fusion.Fuse();
  1060. std::string checks = R"(
  1061. CHECK: [[PHI:%\w+]] = OpPhi
  1062. CHECK-NEXT: OpLoopMerge
  1063. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  1064. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  1065. CHECK-NOT: OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  1066. CHECK: OpStore
  1067. CHECK-NOT: OpPhi
  1068. CHECK-NOT: OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  1069. CHECK: OpLoad
  1070. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  1071. CHECK-NEXT: OpStore [[STORE_1]]
  1072. )";
  1073. Match(checks, context.get());
  1074. auto& ld_final = *context->GetLoopDescriptor(&f);
  1075. EXPECT_EQ(ld_final.NumLoops(), 1u);
  1076. }
  1077. /*
  1078. Generated from the following GLSL + --eliminate-local-multi-store
  1079. #version 440 core
  1080. void main() {
  1081. int[20] a;
  1082. int[10] b;
  1083. int[10] c;
  1084. // Legal, non-overlapping sections in |a|
  1085. for (int i = 0; i < 10; i++) {
  1086. a[i] = b[i] + 1;
  1087. }
  1088. for (int i = 0; i < 10; i++) {
  1089. c[i] = a[i+10] + 2;
  1090. }
  1091. }
  1092. */
  1093. TEST_F(FusionLegalTest, NonOverlappingAccesses) {
  1094. std::string text = R"(
  1095. OpCapability Shader
  1096. %1 = OpExtInstImport "GLSL.std.450"
  1097. OpMemoryModel Logical GLSL450
  1098. OpEntryPoint Fragment %4 "main"
  1099. OpExecutionMode %4 OriginUpperLeft
  1100. OpSource GLSL 440
  1101. OpName %4 "main"
  1102. OpName %8 "i"
  1103. OpName %23 "a"
  1104. OpName %28 "b"
  1105. OpName %37 "i"
  1106. OpName %45 "c"
  1107. %2 = OpTypeVoid
  1108. %3 = OpTypeFunction %2
  1109. %6 = OpTypeInt 32 1
  1110. %7 = OpTypePointer Function %6
  1111. %9 = OpConstant %6 0
  1112. %16 = OpConstant %6 10
  1113. %17 = OpTypeBool
  1114. %19 = OpTypeInt 32 0
  1115. %20 = OpConstant %19 20
  1116. %21 = OpTypeArray %6 %20
  1117. %22 = OpTypePointer Function %21
  1118. %25 = OpConstant %19 10
  1119. %26 = OpTypeArray %6 %25
  1120. %27 = OpTypePointer Function %26
  1121. %32 = OpConstant %6 1
  1122. %51 = OpConstant %6 2
  1123. %4 = OpFunction %2 None %3
  1124. %5 = OpLabel
  1125. %8 = OpVariable %7 Function
  1126. %23 = OpVariable %22 Function
  1127. %28 = OpVariable %27 Function
  1128. %37 = OpVariable %7 Function
  1129. %45 = OpVariable %27 Function
  1130. OpStore %8 %9
  1131. OpBranch %10
  1132. %10 = OpLabel
  1133. %56 = OpPhi %6 %9 %5 %36 %13
  1134. OpLoopMerge %12 %13 None
  1135. OpBranch %14
  1136. %14 = OpLabel
  1137. %18 = OpSLessThan %17 %56 %16
  1138. OpBranchConditional %18 %11 %12
  1139. %11 = OpLabel
  1140. %30 = OpAccessChain %7 %28 %56
  1141. %31 = OpLoad %6 %30
  1142. %33 = OpIAdd %6 %31 %32
  1143. %34 = OpAccessChain %7 %23 %56
  1144. OpStore %34 %33
  1145. OpBranch %13
  1146. %13 = OpLabel
  1147. %36 = OpIAdd %6 %56 %32
  1148. OpStore %8 %36
  1149. OpBranch %10
  1150. %12 = OpLabel
  1151. OpStore %37 %9
  1152. OpBranch %38
  1153. %38 = OpLabel
  1154. %57 = OpPhi %6 %9 %12 %55 %41
  1155. OpLoopMerge %40 %41 None
  1156. OpBranch %42
  1157. %42 = OpLabel
  1158. %44 = OpSLessThan %17 %57 %16
  1159. OpBranchConditional %44 %39 %40
  1160. %39 = OpLabel
  1161. %48 = OpIAdd %6 %57 %16
  1162. %49 = OpAccessChain %7 %23 %48
  1163. %50 = OpLoad %6 %49
  1164. %52 = OpIAdd %6 %50 %51
  1165. %53 = OpAccessChain %7 %45 %57
  1166. OpStore %53 %52
  1167. OpBranch %41
  1168. %41 = OpLabel
  1169. %55 = OpIAdd %6 %57 %32
  1170. OpStore %37 %55
  1171. OpBranch %38
  1172. %40 = OpLabel
  1173. OpReturn
  1174. OpFunctionEnd
  1175. )";
  1176. std::unique_ptr<IRContext> context =
  1177. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1178. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1179. Module* module = context->module();
  1180. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  1181. << text << std::endl;
  1182. Function& f = *module->begin();
  1183. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  1184. EXPECT_EQ(ld.NumLoops(), 2u);
  1185. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  1186. LoopFusion fusion(context.get(), loops[0], loops[1]);
  1187. EXPECT_TRUE(fusion.AreCompatible());
  1188. EXPECT_TRUE(fusion.IsLegal());
  1189. fusion.Fuse();
  1190. std::string checks = R"(
  1191. CHECK: [[PHI:%\w+]] = OpPhi
  1192. CHECK-NEXT: OpLoopMerge
  1193. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  1194. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  1195. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  1196. CHECK-NOT: OpPhi
  1197. CHECK: [[I_10:%\w+]] = OpIAdd {{%\w+}} [[PHI]] {{%\w+}}
  1198. CHECK-NEXT: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_10]]
  1199. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  1200. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  1201. CHECK-NEXT: OpStore [[STORE_1]]
  1202. )";
  1203. Match(checks, context.get());
  1204. auto& ld_final = *context->GetLoopDescriptor(&f);
  1205. EXPECT_EQ(ld_final.NumLoops(), 1u);
  1206. }
  1207. /*
  1208. Generated from the following GLSL + --eliminate-local-multi-store
  1209. #version 440 core
  1210. void main() {
  1211. int[10] a;
  1212. int[10] b;
  1213. int[10] c;
  1214. // Legal, 3 adjacent loops
  1215. for (int i = 0; i < 10; i++) {
  1216. a[i] = b[i] + 1;
  1217. }
  1218. for (int i = 0; i < 10; i++) {
  1219. c[i] = a[i] + 2;
  1220. }
  1221. for (int i = 0; i < 10; i++) {
  1222. b[i] = c[i] + 10;
  1223. }
  1224. }
  1225. */
  1226. TEST_F(FusionLegalTest, AdjacentLoops) {
  1227. std::string text = R"(
  1228. OpCapability Shader
  1229. %1 = OpExtInstImport "GLSL.std.450"
  1230. OpMemoryModel Logical GLSL450
  1231. OpEntryPoint Fragment %4 "main"
  1232. OpExecutionMode %4 OriginUpperLeft
  1233. OpSource GLSL 440
  1234. OpName %4 "main"
  1235. OpName %8 "i"
  1236. OpName %23 "a"
  1237. OpName %25 "b"
  1238. OpName %34 "i"
  1239. OpName %42 "c"
  1240. OpName %52 "i"
  1241. %2 = OpTypeVoid
  1242. %3 = OpTypeFunction %2
  1243. %6 = OpTypeInt 32 1
  1244. %7 = OpTypePointer Function %6
  1245. %9 = OpConstant %6 0
  1246. %16 = OpConstant %6 10
  1247. %17 = OpTypeBool
  1248. %19 = OpTypeInt 32 0
  1249. %20 = OpConstant %19 10
  1250. %21 = OpTypeArray %6 %20
  1251. %22 = OpTypePointer Function %21
  1252. %29 = OpConstant %6 1
  1253. %47 = OpConstant %6 2
  1254. %4 = OpFunction %2 None %3
  1255. %5 = OpLabel
  1256. %8 = OpVariable %7 Function
  1257. %23 = OpVariable %22 Function
  1258. %25 = OpVariable %22 Function
  1259. %34 = OpVariable %7 Function
  1260. %42 = OpVariable %22 Function
  1261. %52 = OpVariable %7 Function
  1262. OpStore %8 %9
  1263. OpBranch %10
  1264. %10 = OpLabel
  1265. %68 = OpPhi %6 %9 %5 %33 %13
  1266. OpLoopMerge %12 %13 None
  1267. OpBranch %14
  1268. %14 = OpLabel
  1269. %18 = OpSLessThan %17 %68 %16
  1270. OpBranchConditional %18 %11 %12
  1271. %11 = OpLabel
  1272. %27 = OpAccessChain %7 %25 %68
  1273. %28 = OpLoad %6 %27
  1274. %30 = OpIAdd %6 %28 %29
  1275. %31 = OpAccessChain %7 %23 %68
  1276. OpStore %31 %30
  1277. OpBranch %13
  1278. %13 = OpLabel
  1279. %33 = OpIAdd %6 %68 %29
  1280. OpStore %8 %33
  1281. OpBranch %10
  1282. %12 = OpLabel
  1283. OpStore %34 %9
  1284. OpBranch %35
  1285. %35 = OpLabel
  1286. %69 = OpPhi %6 %9 %12 %51 %38
  1287. OpLoopMerge %37 %38 None
  1288. OpBranch %39
  1289. %39 = OpLabel
  1290. %41 = OpSLessThan %17 %69 %16
  1291. OpBranchConditional %41 %36 %37
  1292. %36 = OpLabel
  1293. %45 = OpAccessChain %7 %23 %69
  1294. %46 = OpLoad %6 %45
  1295. %48 = OpIAdd %6 %46 %47
  1296. %49 = OpAccessChain %7 %42 %69
  1297. OpStore %49 %48
  1298. OpBranch %38
  1299. %38 = OpLabel
  1300. %51 = OpIAdd %6 %69 %29
  1301. OpStore %34 %51
  1302. OpBranch %35
  1303. %37 = OpLabel
  1304. OpStore %52 %9
  1305. OpBranch %53
  1306. %53 = OpLabel
  1307. %70 = OpPhi %6 %9 %37 %67 %56
  1308. OpLoopMerge %55 %56 None
  1309. OpBranch %57
  1310. %57 = OpLabel
  1311. %59 = OpSLessThan %17 %70 %16
  1312. OpBranchConditional %59 %54 %55
  1313. %54 = OpLabel
  1314. %62 = OpAccessChain %7 %42 %70
  1315. %63 = OpLoad %6 %62
  1316. %64 = OpIAdd %6 %63 %16
  1317. %65 = OpAccessChain %7 %25 %70
  1318. OpStore %65 %64
  1319. OpBranch %56
  1320. %56 = OpLabel
  1321. %67 = OpIAdd %6 %70 %29
  1322. OpStore %52 %67
  1323. OpBranch %53
  1324. %55 = OpLabel
  1325. OpReturn
  1326. OpFunctionEnd
  1327. )";
  1328. std::unique_ptr<IRContext> context =
  1329. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1330. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1331. Module* module = context->module();
  1332. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  1333. << text << std::endl;
  1334. Function& f = *module->begin();
  1335. {
  1336. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  1337. EXPECT_EQ(ld.NumLoops(), 3u);
  1338. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  1339. LoopFusion fusion(context.get(), loops[1], loops[2]);
  1340. EXPECT_TRUE(fusion.AreCompatible());
  1341. EXPECT_TRUE(fusion.IsLegal());
  1342. fusion.Fuse();
  1343. }
  1344. std::string checks = R"(
  1345. CHECK: [[PHI_0:%\w+]] = OpPhi
  1346. CHECK-NEXT: OpLoopMerge
  1347. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]]
  1348. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  1349. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]]
  1350. CHECK-NEXT: OpStore [[STORE_0]]
  1351. CHECK: [[PHI_1:%\w+]] = OpPhi
  1352. CHECK-NEXT: OpLoopMerge
  1353. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_1]]
  1354. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  1355. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_1]]
  1356. CHECK-NEXT: OpStore [[STORE_1]]
  1357. CHECK-NOT: OpPhi
  1358. CHECK: [[LOAD_2:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_1]]
  1359. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_2]]
  1360. CHECK: [[STORE_2:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_1]]
  1361. CHECK-NEXT: OpStore [[STORE_2]]
  1362. )";
  1363. Match(checks, context.get());
  1364. {
  1365. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  1366. EXPECT_EQ(ld.NumLoops(), 2u);
  1367. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  1368. LoopFusion fusion(context.get(), loops[0], loops[1]);
  1369. EXPECT_TRUE(fusion.AreCompatible());
  1370. EXPECT_TRUE(fusion.IsLegal());
  1371. fusion.Fuse();
  1372. }
  1373. std::string checks_ = R"(
  1374. CHECK: [[PHI:%\w+]] = OpPhi
  1375. CHECK-NEXT: OpLoopMerge
  1376. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  1377. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  1378. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  1379. CHECK-NEXT: OpStore [[STORE_0]]
  1380. CHECK-NOT: OpPhi
  1381. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  1382. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  1383. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  1384. CHECK-NEXT: OpStore [[STORE_1]]
  1385. CHECK-NOT: OpPhi
  1386. CHECK: [[LOAD_2:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  1387. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_2]]
  1388. CHECK: [[STORE_2:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  1389. CHECK-NEXT: OpStore [[STORE_2]]
  1390. )";
  1391. Match(checks_, context.get());
  1392. auto& ld_final = *context->GetLoopDescriptor(&f);
  1393. EXPECT_EQ(ld_final.NumLoops(), 1u);
  1394. }
  1395. /*
  1396. Generated from the following GLSL + --eliminate-local-multi-store
  1397. #version 440 core
  1398. void main() {
  1399. int[10][10] a;
  1400. int[10][10] b;
  1401. int[10][10] c;
  1402. // Legal inner loop fusion
  1403. for (int i = 0; i < 10; i++) {
  1404. for (int j = 0; j < 10; j++) {
  1405. c[i][j] = a[i][j] + 2;
  1406. }
  1407. for (int j = 0; j < 10; j++) {
  1408. b[i][j] = c[i][j] + 10;
  1409. }
  1410. }
  1411. }
  1412. */
  1413. TEST_F(FusionLegalTest, InnerLoopFusion) {
  1414. std::string text = R"(
  1415. OpCapability Shader
  1416. %1 = OpExtInstImport "GLSL.std.450"
  1417. OpMemoryModel Logical GLSL450
  1418. OpEntryPoint Fragment %4 "main"
  1419. OpExecutionMode %4 OriginUpperLeft
  1420. OpSource GLSL 440
  1421. OpName %4 "main"
  1422. OpName %8 "i"
  1423. OpName %19 "j"
  1424. OpName %32 "c"
  1425. OpName %35 "a"
  1426. OpName %46 "j"
  1427. OpName %54 "b"
  1428. %2 = OpTypeVoid
  1429. %3 = OpTypeFunction %2
  1430. %6 = OpTypeInt 32 1
  1431. %7 = OpTypePointer Function %6
  1432. %9 = OpConstant %6 0
  1433. %16 = OpConstant %6 10
  1434. %17 = OpTypeBool
  1435. %27 = OpTypeInt 32 0
  1436. %28 = OpConstant %27 10
  1437. %29 = OpTypeArray %6 %28
  1438. %30 = OpTypeArray %29 %28
  1439. %31 = OpTypePointer Function %30
  1440. %40 = OpConstant %6 2
  1441. %44 = OpConstant %6 1
  1442. %4 = OpFunction %2 None %3
  1443. %5 = OpLabel
  1444. %8 = OpVariable %7 Function
  1445. %19 = OpVariable %7 Function
  1446. %32 = OpVariable %31 Function
  1447. %35 = OpVariable %31 Function
  1448. %46 = OpVariable %7 Function
  1449. %54 = OpVariable %31 Function
  1450. OpStore %8 %9
  1451. OpBranch %10
  1452. %10 = OpLabel
  1453. %67 = OpPhi %6 %9 %5 %66 %13
  1454. OpLoopMerge %12 %13 None
  1455. OpBranch %14
  1456. %14 = OpLabel
  1457. %18 = OpSLessThan %17 %67 %16
  1458. OpBranchConditional %18 %11 %12
  1459. %11 = OpLabel
  1460. OpStore %19 %9
  1461. OpBranch %20
  1462. %20 = OpLabel
  1463. %68 = OpPhi %6 %9 %11 %45 %23
  1464. OpLoopMerge %22 %23 None
  1465. OpBranch %24
  1466. %24 = OpLabel
  1467. %26 = OpSLessThan %17 %68 %16
  1468. OpBranchConditional %26 %21 %22
  1469. %21 = OpLabel
  1470. %38 = OpAccessChain %7 %35 %67 %68
  1471. %39 = OpLoad %6 %38
  1472. %41 = OpIAdd %6 %39 %40
  1473. %42 = OpAccessChain %7 %32 %67 %68
  1474. OpStore %42 %41
  1475. OpBranch %23
  1476. %23 = OpLabel
  1477. %45 = OpIAdd %6 %68 %44
  1478. OpStore %19 %45
  1479. OpBranch %20
  1480. %22 = OpLabel
  1481. OpStore %46 %9
  1482. OpBranch %47
  1483. %47 = OpLabel
  1484. %69 = OpPhi %6 %9 %22 %64 %50
  1485. OpLoopMerge %49 %50 None
  1486. OpBranch %51
  1487. %51 = OpLabel
  1488. %53 = OpSLessThan %17 %69 %16
  1489. OpBranchConditional %53 %48 %49
  1490. %48 = OpLabel
  1491. %59 = OpAccessChain %7 %32 %67 %69
  1492. %60 = OpLoad %6 %59
  1493. %61 = OpIAdd %6 %60 %16
  1494. %62 = OpAccessChain %7 %54 %67 %69
  1495. OpStore %62 %61
  1496. OpBranch %50
  1497. %50 = OpLabel
  1498. %64 = OpIAdd %6 %69 %44
  1499. OpStore %46 %64
  1500. OpBranch %47
  1501. %49 = OpLabel
  1502. OpBranch %13
  1503. %13 = OpLabel
  1504. %66 = OpIAdd %6 %67 %44
  1505. OpStore %8 %66
  1506. OpBranch %10
  1507. %12 = OpLabel
  1508. OpReturn
  1509. OpFunctionEnd
  1510. )";
  1511. std::unique_ptr<IRContext> context =
  1512. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1513. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1514. Module* module = context->module();
  1515. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  1516. << text << std::endl;
  1517. Function& f = *module->begin();
  1518. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  1519. EXPECT_EQ(ld.NumLoops(), 3u);
  1520. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  1521. auto loop_0 = loops[0];
  1522. auto loop_1 = loops[1];
  1523. auto loop_2 = loops[2];
  1524. {
  1525. LoopFusion fusion(context.get(), loop_0, loop_1);
  1526. EXPECT_FALSE(fusion.AreCompatible());
  1527. }
  1528. {
  1529. LoopFusion fusion(context.get(), loop_0, loop_2);
  1530. EXPECT_FALSE(fusion.AreCompatible());
  1531. }
  1532. {
  1533. LoopFusion fusion(context.get(), loop_1, loop_2);
  1534. EXPECT_TRUE(fusion.AreCompatible());
  1535. EXPECT_TRUE(fusion.IsLegal());
  1536. fusion.Fuse();
  1537. }
  1538. std::string checks = R"(
  1539. CHECK: [[PHI_0:%\w+]] = OpPhi
  1540. CHECK-NEXT: OpLoopMerge
  1541. CHECK: [[PHI_1:%\w+]] = OpPhi
  1542. CHECK-NEXT: OpLoopMerge
  1543. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  1544. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  1545. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  1546. CHECK-NEXT: OpStore [[STORE_0]]
  1547. CHECK-NOT: OpPhi
  1548. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  1549. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  1550. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  1551. CHECK-NEXT: OpStore [[STORE_1]]
  1552. )";
  1553. Match(checks, context.get());
  1554. auto& ld_final = *context->GetLoopDescriptor(&f);
  1555. EXPECT_EQ(ld_final.NumLoops(), 2u);
  1556. }
  1557. /*
  1558. Generated from the following GLSL + --eliminate-local-multi-store
  1559. // 12
  1560. #version 440 core
  1561. void main() {
  1562. int[10][10] a;
  1563. int[10][10] b;
  1564. int[10][10] c;
  1565. // Legal both
  1566. for (int i = 0; i < 10; i++) {
  1567. for (int j = 0; j < 10; j++) {
  1568. c[i][j] = a[i][j] + 2;
  1569. }
  1570. }
  1571. for (int i = 0; i < 10; i++) {
  1572. for (int j = 0; j < 10; j++) {
  1573. b[i][j] = c[i][j] + 10;
  1574. }
  1575. }
  1576. }
  1577. */
  1578. TEST_F(FusionLegalTest, OuterAndInnerLoop) {
  1579. std::string text = R"(
  1580. OpCapability Shader
  1581. %1 = OpExtInstImport "GLSL.std.450"
  1582. OpMemoryModel Logical GLSL450
  1583. OpEntryPoint Fragment %4 "main"
  1584. OpExecutionMode %4 OriginUpperLeft
  1585. OpSource GLSL 440
  1586. OpName %4 "main"
  1587. OpName %8 "i"
  1588. OpName %19 "j"
  1589. OpName %32 "c"
  1590. OpName %35 "a"
  1591. OpName %48 "i"
  1592. OpName %56 "j"
  1593. OpName %64 "b"
  1594. %2 = OpTypeVoid
  1595. %3 = OpTypeFunction %2
  1596. %6 = OpTypeInt 32 1
  1597. %7 = OpTypePointer Function %6
  1598. %9 = OpConstant %6 0
  1599. %16 = OpConstant %6 10
  1600. %17 = OpTypeBool
  1601. %27 = OpTypeInt 32 0
  1602. %28 = OpConstant %27 10
  1603. %29 = OpTypeArray %6 %28
  1604. %30 = OpTypeArray %29 %28
  1605. %31 = OpTypePointer Function %30
  1606. %40 = OpConstant %6 2
  1607. %44 = OpConstant %6 1
  1608. %4 = OpFunction %2 None %3
  1609. %5 = OpLabel
  1610. %8 = OpVariable %7 Function
  1611. %19 = OpVariable %7 Function
  1612. %32 = OpVariable %31 Function
  1613. %35 = OpVariable %31 Function
  1614. %48 = OpVariable %7 Function
  1615. %56 = OpVariable %7 Function
  1616. %64 = OpVariable %31 Function
  1617. OpStore %8 %9
  1618. OpBranch %10
  1619. %10 = OpLabel
  1620. %77 = OpPhi %6 %9 %5 %47 %13
  1621. OpLoopMerge %12 %13 None
  1622. OpBranch %14
  1623. %14 = OpLabel
  1624. %18 = OpSLessThan %17 %77 %16
  1625. OpBranchConditional %18 %11 %12
  1626. %11 = OpLabel
  1627. OpStore %19 %9
  1628. OpBranch %20
  1629. %20 = OpLabel
  1630. %81 = OpPhi %6 %9 %11 %45 %23
  1631. OpLoopMerge %22 %23 None
  1632. OpBranch %24
  1633. %24 = OpLabel
  1634. %26 = OpSLessThan %17 %81 %16
  1635. OpBranchConditional %26 %21 %22
  1636. %21 = OpLabel
  1637. %38 = OpAccessChain %7 %35 %77 %81
  1638. %39 = OpLoad %6 %38
  1639. %41 = OpIAdd %6 %39 %40
  1640. %42 = OpAccessChain %7 %32 %77 %81
  1641. OpStore %42 %41
  1642. OpBranch %23
  1643. %23 = OpLabel
  1644. %45 = OpIAdd %6 %81 %44
  1645. OpStore %19 %45
  1646. OpBranch %20
  1647. %22 = OpLabel
  1648. OpBranch %13
  1649. %13 = OpLabel
  1650. %47 = OpIAdd %6 %77 %44
  1651. OpStore %8 %47
  1652. OpBranch %10
  1653. %12 = OpLabel
  1654. OpStore %48 %9
  1655. OpBranch %49
  1656. %49 = OpLabel
  1657. %78 = OpPhi %6 %9 %12 %76 %52
  1658. OpLoopMerge %51 %52 None
  1659. OpBranch %53
  1660. %53 = OpLabel
  1661. %55 = OpSLessThan %17 %78 %16
  1662. OpBranchConditional %55 %50 %51
  1663. %50 = OpLabel
  1664. OpStore %56 %9
  1665. OpBranch %57
  1666. %57 = OpLabel
  1667. %79 = OpPhi %6 %9 %50 %74 %60
  1668. OpLoopMerge %59 %60 None
  1669. OpBranch %61
  1670. %61 = OpLabel
  1671. %63 = OpSLessThan %17 %79 %16
  1672. OpBranchConditional %63 %58 %59
  1673. %58 = OpLabel
  1674. %69 = OpAccessChain %7 %32 %78 %79
  1675. %70 = OpLoad %6 %69
  1676. %71 = OpIAdd %6 %70 %16
  1677. %72 = OpAccessChain %7 %64 %78 %79
  1678. OpStore %72 %71
  1679. OpBranch %60
  1680. %60 = OpLabel
  1681. %74 = OpIAdd %6 %79 %44
  1682. OpStore %56 %74
  1683. OpBranch %57
  1684. %59 = OpLabel
  1685. OpBranch %52
  1686. %52 = OpLabel
  1687. %76 = OpIAdd %6 %78 %44
  1688. OpStore %48 %76
  1689. OpBranch %49
  1690. %51 = OpLabel
  1691. OpReturn
  1692. OpFunctionEnd
  1693. )";
  1694. std::unique_ptr<IRContext> context =
  1695. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1696. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1697. Module* module = context->module();
  1698. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  1699. << text << std::endl;
  1700. Function& f = *module->begin();
  1701. {
  1702. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  1703. EXPECT_EQ(ld.NumLoops(), 4u);
  1704. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  1705. auto loop_0 = loops[0];
  1706. auto loop_1 = loops[1];
  1707. auto loop_2 = loops[2];
  1708. auto loop_3 = loops[3];
  1709. {
  1710. LoopFusion fusion(context.get(), loop_0, loop_1);
  1711. EXPECT_FALSE(fusion.AreCompatible());
  1712. }
  1713. {
  1714. LoopFusion fusion(context.get(), loop_1, loop_2);
  1715. EXPECT_FALSE(fusion.AreCompatible());
  1716. }
  1717. {
  1718. LoopFusion fusion(context.get(), loop_2, loop_3);
  1719. EXPECT_FALSE(fusion.AreCompatible());
  1720. }
  1721. {
  1722. LoopFusion fusion(context.get(), loop_1, loop_3);
  1723. EXPECT_FALSE(fusion.AreCompatible());
  1724. }
  1725. {
  1726. LoopFusion fusion(context.get(), loop_0, loop_2);
  1727. EXPECT_TRUE(fusion.AreCompatible());
  1728. EXPECT_TRUE(fusion.IsLegal());
  1729. fusion.Fuse();
  1730. }
  1731. std::string checks = R"(
  1732. CHECK: [[PHI_0:%\w+]] = OpPhi
  1733. CHECK-NEXT: OpLoopMerge
  1734. CHECK: [[PHI_1:%\w+]] = OpPhi
  1735. CHECK-NEXT: OpLoopMerge
  1736. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  1737. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  1738. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  1739. CHECK-NEXT: OpStore [[STORE_0]]
  1740. CHECK: [[PHI_2:%\w+]] = OpPhi
  1741. CHECK-NEXT: OpLoopMerge
  1742. CHECK-NOT: OpPhi
  1743. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_2]]
  1744. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  1745. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_2]]
  1746. CHECK-NEXT: OpStore [[STORE_1]]
  1747. )";
  1748. Match(checks, context.get());
  1749. }
  1750. {
  1751. auto& ld = *context->GetLoopDescriptor(&f);
  1752. EXPECT_EQ(ld.NumLoops(), 3u);
  1753. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  1754. auto loop_0 = loops[0];
  1755. auto loop_1 = loops[1];
  1756. auto loop_2 = loops[2];
  1757. {
  1758. LoopFusion fusion(context.get(), loop_0, loop_1);
  1759. EXPECT_FALSE(fusion.AreCompatible());
  1760. }
  1761. {
  1762. LoopFusion fusion(context.get(), loop_0, loop_2);
  1763. EXPECT_FALSE(fusion.AreCompatible());
  1764. }
  1765. {
  1766. LoopFusion fusion(context.get(), loop_1, loop_2);
  1767. EXPECT_TRUE(fusion.AreCompatible());
  1768. EXPECT_TRUE(fusion.IsLegal());
  1769. fusion.Fuse();
  1770. }
  1771. std::string checks = R"(
  1772. CHECK: [[PHI_0:%\w+]] = OpPhi
  1773. CHECK-NEXT: OpLoopMerge
  1774. CHECK: [[PHI_1:%\w+]] = OpPhi
  1775. CHECK-NEXT: OpLoopMerge
  1776. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  1777. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  1778. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  1779. CHECK-NEXT: OpStore [[STORE_0]]
  1780. CHECK-NOT: OpPhi
  1781. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  1782. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  1783. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  1784. CHECK-NEXT: OpStore [[STORE_1]]
  1785. )";
  1786. Match(checks, context.get());
  1787. }
  1788. {
  1789. auto& ld = *context->GetLoopDescriptor(&f);
  1790. EXPECT_EQ(ld.NumLoops(), 2u);
  1791. }
  1792. }
  1793. /*
  1794. Generated from the following GLSL + --eliminate-local-multi-store
  1795. #version 440 core
  1796. void main() {
  1797. int[10][10] a;
  1798. int[10][10] b;
  1799. int[10][10] c;
  1800. // Legal both, more complex
  1801. for (int i = 0; i < 10; i++) {
  1802. for (int j = 0; j < 10; j++) {
  1803. if (i % 2 == 0 && j % 2 == 0) {
  1804. c[i][j] = a[i][j] + 2;
  1805. }
  1806. }
  1807. }
  1808. for (int i = 0; i < 10; i++) {
  1809. for (int j = 0; j < 10; j++) {
  1810. b[i][j] = c[i][j] + 10;
  1811. }
  1812. }
  1813. }
  1814. */
  1815. TEST_F(FusionLegalTest, OuterAndInnerLoopMoreComplex) {
  1816. std::string text = R"(
  1817. OpCapability Shader
  1818. %1 = OpExtInstImport "GLSL.std.450"
  1819. OpMemoryModel Logical GLSL450
  1820. OpEntryPoint Fragment %4 "main"
  1821. OpExecutionMode %4 OriginUpperLeft
  1822. OpSource GLSL 440
  1823. OpName %4 "main"
  1824. OpName %8 "i"
  1825. OpName %19 "j"
  1826. OpName %44 "c"
  1827. OpName %47 "a"
  1828. OpName %59 "i"
  1829. OpName %67 "j"
  1830. OpName %75 "b"
  1831. %2 = OpTypeVoid
  1832. %3 = OpTypeFunction %2
  1833. %6 = OpTypeInt 32 1
  1834. %7 = OpTypePointer Function %6
  1835. %9 = OpConstant %6 0
  1836. %16 = OpConstant %6 10
  1837. %17 = OpTypeBool
  1838. %28 = OpConstant %6 2
  1839. %39 = OpTypeInt 32 0
  1840. %40 = OpConstant %39 10
  1841. %41 = OpTypeArray %6 %40
  1842. %42 = OpTypeArray %41 %40
  1843. %43 = OpTypePointer Function %42
  1844. %55 = OpConstant %6 1
  1845. %4 = OpFunction %2 None %3
  1846. %5 = OpLabel
  1847. %8 = OpVariable %7 Function
  1848. %19 = OpVariable %7 Function
  1849. %44 = OpVariable %43 Function
  1850. %47 = OpVariable %43 Function
  1851. %59 = OpVariable %7 Function
  1852. %67 = OpVariable %7 Function
  1853. %75 = OpVariable %43 Function
  1854. OpStore %8 %9
  1855. OpBranch %10
  1856. %10 = OpLabel
  1857. %88 = OpPhi %6 %9 %5 %58 %13
  1858. OpLoopMerge %12 %13 None
  1859. OpBranch %14
  1860. %14 = OpLabel
  1861. %18 = OpSLessThan %17 %88 %16
  1862. OpBranchConditional %18 %11 %12
  1863. %11 = OpLabel
  1864. OpStore %19 %9
  1865. OpBranch %20
  1866. %20 = OpLabel
  1867. %92 = OpPhi %6 %9 %11 %56 %23
  1868. OpLoopMerge %22 %23 None
  1869. OpBranch %24
  1870. %24 = OpLabel
  1871. %26 = OpSLessThan %17 %92 %16
  1872. OpBranchConditional %26 %21 %22
  1873. %21 = OpLabel
  1874. %29 = OpSMod %6 %88 %28
  1875. %30 = OpIEqual %17 %29 %9
  1876. OpSelectionMerge %32 None
  1877. OpBranchConditional %30 %31 %32
  1878. %31 = OpLabel
  1879. %34 = OpSMod %6 %92 %28
  1880. %35 = OpIEqual %17 %34 %9
  1881. OpBranch %32
  1882. %32 = OpLabel
  1883. %36 = OpPhi %17 %30 %21 %35 %31
  1884. OpSelectionMerge %38 None
  1885. OpBranchConditional %36 %37 %38
  1886. %37 = OpLabel
  1887. %50 = OpAccessChain %7 %47 %88 %92
  1888. %51 = OpLoad %6 %50
  1889. %52 = OpIAdd %6 %51 %28
  1890. %53 = OpAccessChain %7 %44 %88 %92
  1891. OpStore %53 %52
  1892. OpBranch %38
  1893. %38 = OpLabel
  1894. OpBranch %23
  1895. %23 = OpLabel
  1896. %56 = OpIAdd %6 %92 %55
  1897. OpStore %19 %56
  1898. OpBranch %20
  1899. %22 = OpLabel
  1900. OpBranch %13
  1901. %13 = OpLabel
  1902. %58 = OpIAdd %6 %88 %55
  1903. OpStore %8 %58
  1904. OpBranch %10
  1905. %12 = OpLabel
  1906. OpStore %59 %9
  1907. OpBranch %60
  1908. %60 = OpLabel
  1909. %89 = OpPhi %6 %9 %12 %87 %63
  1910. OpLoopMerge %62 %63 None
  1911. OpBranch %64
  1912. %64 = OpLabel
  1913. %66 = OpSLessThan %17 %89 %16
  1914. OpBranchConditional %66 %61 %62
  1915. %61 = OpLabel
  1916. OpStore %67 %9
  1917. OpBranch %68
  1918. %68 = OpLabel
  1919. %90 = OpPhi %6 %9 %61 %85 %71
  1920. OpLoopMerge %70 %71 None
  1921. OpBranch %72
  1922. %72 = OpLabel
  1923. %74 = OpSLessThan %17 %90 %16
  1924. OpBranchConditional %74 %69 %70
  1925. %69 = OpLabel
  1926. %80 = OpAccessChain %7 %44 %89 %90
  1927. %81 = OpLoad %6 %80
  1928. %82 = OpIAdd %6 %81 %16
  1929. %83 = OpAccessChain %7 %75 %89 %90
  1930. OpStore %83 %82
  1931. OpBranch %71
  1932. %71 = OpLabel
  1933. %85 = OpIAdd %6 %90 %55
  1934. OpStore %67 %85
  1935. OpBranch %68
  1936. %70 = OpLabel
  1937. OpBranch %63
  1938. %63 = OpLabel
  1939. %87 = OpIAdd %6 %89 %55
  1940. OpStore %59 %87
  1941. OpBranch %60
  1942. %62 = OpLabel
  1943. OpReturn
  1944. OpFunctionEnd
  1945. )";
  1946. std::unique_ptr<IRContext> context =
  1947. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1948. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1949. Module* module = context->module();
  1950. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  1951. << text << std::endl;
  1952. Function& f = *module->begin();
  1953. {
  1954. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  1955. EXPECT_EQ(ld.NumLoops(), 4u);
  1956. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  1957. auto loop_0 = loops[0];
  1958. auto loop_1 = loops[1];
  1959. auto loop_2 = loops[2];
  1960. auto loop_3 = loops[3];
  1961. {
  1962. LoopFusion fusion(context.get(), loop_0, loop_1);
  1963. EXPECT_FALSE(fusion.AreCompatible());
  1964. }
  1965. {
  1966. LoopFusion fusion(context.get(), loop_1, loop_2);
  1967. EXPECT_FALSE(fusion.AreCompatible());
  1968. }
  1969. {
  1970. LoopFusion fusion(context.get(), loop_2, loop_3);
  1971. EXPECT_FALSE(fusion.AreCompatible());
  1972. }
  1973. {
  1974. LoopFusion fusion(context.get(), loop_1, loop_3);
  1975. EXPECT_FALSE(fusion.AreCompatible());
  1976. }
  1977. {
  1978. LoopFusion fusion(context.get(), loop_0, loop_2);
  1979. EXPECT_TRUE(fusion.AreCompatible());
  1980. EXPECT_TRUE(fusion.IsLegal());
  1981. fusion.Fuse();
  1982. }
  1983. std::string checks = R"(
  1984. CHECK: [[PHI_0:%\w+]] = OpPhi
  1985. CHECK-NEXT: OpLoopMerge
  1986. CHECK: [[PHI_1:%\w+]] = OpPhi
  1987. CHECK-NEXT: OpLoopMerge
  1988. CHECK: OpPhi
  1989. CHECK-NEXT: OpSelectionMerge
  1990. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  1991. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  1992. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  1993. CHECK-NEXT: OpStore [[STORE_0]]
  1994. CHECK: [[PHI_2:%\w+]] = OpPhi
  1995. CHECK-NEXT: OpLoopMerge
  1996. CHECK-NOT: OpPhi
  1997. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_2]]
  1998. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  1999. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_2]]
  2000. CHECK-NEXT: OpStore [[STORE_1]]
  2001. )";
  2002. Match(checks, context.get());
  2003. }
  2004. {
  2005. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  2006. EXPECT_EQ(ld.NumLoops(), 3u);
  2007. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  2008. auto loop_0 = loops[0];
  2009. auto loop_1 = loops[1];
  2010. auto loop_2 = loops[2];
  2011. {
  2012. LoopFusion fusion(context.get(), loop_0, loop_1);
  2013. EXPECT_FALSE(fusion.AreCompatible());
  2014. }
  2015. {
  2016. LoopFusion fusion(context.get(), loop_0, loop_2);
  2017. EXPECT_FALSE(fusion.AreCompatible());
  2018. }
  2019. {
  2020. LoopFusion fusion(context.get(), loop_1, loop_2);
  2021. EXPECT_TRUE(fusion.AreCompatible());
  2022. EXPECT_TRUE(fusion.IsLegal());
  2023. fusion.Fuse();
  2024. }
  2025. std::string checks = R"(
  2026. CHECK: [[PHI_0:%\w+]] = OpPhi
  2027. CHECK-NEXT: OpLoopMerge
  2028. CHECK: [[PHI_1:%\w+]] = OpPhi
  2029. CHECK-NEXT: OpLoopMerge
  2030. CHECK: OpPhi
  2031. CHECK-NEXT: OpSelectionMerge
  2032. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  2033. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  2034. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  2035. CHECK-NEXT: OpStore [[STORE_0]]
  2036. CHECK-NOT: OpPhi
  2037. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  2038. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  2039. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  2040. CHECK-NEXT: OpStore [[STORE_1]]
  2041. )";
  2042. Match(checks, context.get());
  2043. }
  2044. {
  2045. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  2046. EXPECT_EQ(ld.NumLoops(), 2u);
  2047. }
  2048. }
  2049. /*
  2050. Generated from the following GLSL + --eliminate-local-multi-store
  2051. #version 440 core
  2052. void main() {
  2053. int[10][10] a;
  2054. int[10][10] b;
  2055. int[10][10] c;
  2056. // Outer would have been illegal to fuse, but since written
  2057. // like this, inner loop fusion is legal.
  2058. for (int i = 0; i < 10; i++) {
  2059. for (int j = 0; j < 10; j++) {
  2060. c[i][j] = a[i][j] + 2;
  2061. }
  2062. for (int j = 0; j < 10; j++) {
  2063. b[i][j] = c[i+1][j] + 10;
  2064. }
  2065. }
  2066. }
  2067. */
  2068. TEST_F(FusionLegalTest, InnerWithExistingDependenceOnOuter) {
  2069. std::string text = R"(
  2070. OpCapability Shader
  2071. %1 = OpExtInstImport "GLSL.std.450"
  2072. OpMemoryModel Logical GLSL450
  2073. OpEntryPoint Fragment %4 "main"
  2074. OpExecutionMode %4 OriginUpperLeft
  2075. OpSource GLSL 440
  2076. OpName %4 "main"
  2077. OpName %8 "i"
  2078. OpName %19 "j"
  2079. OpName %32 "c"
  2080. OpName %35 "a"
  2081. OpName %46 "j"
  2082. OpName %54 "b"
  2083. %2 = OpTypeVoid
  2084. %3 = OpTypeFunction %2
  2085. %6 = OpTypeInt 32 1
  2086. %7 = OpTypePointer Function %6
  2087. %9 = OpConstant %6 0
  2088. %16 = OpConstant %6 10
  2089. %17 = OpTypeBool
  2090. %27 = OpTypeInt 32 0
  2091. %28 = OpConstant %27 10
  2092. %29 = OpTypeArray %6 %28
  2093. %30 = OpTypeArray %29 %28
  2094. %31 = OpTypePointer Function %30
  2095. %40 = OpConstant %6 2
  2096. %44 = OpConstant %6 1
  2097. %4 = OpFunction %2 None %3
  2098. %5 = OpLabel
  2099. %8 = OpVariable %7 Function
  2100. %19 = OpVariable %7 Function
  2101. %32 = OpVariable %31 Function
  2102. %35 = OpVariable %31 Function
  2103. %46 = OpVariable %7 Function
  2104. %54 = OpVariable %31 Function
  2105. OpStore %8 %9
  2106. OpBranch %10
  2107. %10 = OpLabel
  2108. %68 = OpPhi %6 %9 %5 %67 %13
  2109. OpLoopMerge %12 %13 None
  2110. OpBranch %14
  2111. %14 = OpLabel
  2112. %18 = OpSLessThan %17 %68 %16
  2113. OpBranchConditional %18 %11 %12
  2114. %11 = OpLabel
  2115. OpStore %19 %9
  2116. OpBranch %20
  2117. %20 = OpLabel
  2118. %69 = OpPhi %6 %9 %11 %45 %23
  2119. OpLoopMerge %22 %23 None
  2120. OpBranch %24
  2121. %24 = OpLabel
  2122. %26 = OpSLessThan %17 %69 %16
  2123. OpBranchConditional %26 %21 %22
  2124. %21 = OpLabel
  2125. %38 = OpAccessChain %7 %35 %68 %69
  2126. %39 = OpLoad %6 %38
  2127. %41 = OpIAdd %6 %39 %40
  2128. %42 = OpAccessChain %7 %32 %68 %69
  2129. OpStore %42 %41
  2130. OpBranch %23
  2131. %23 = OpLabel
  2132. %45 = OpIAdd %6 %69 %44
  2133. OpStore %19 %45
  2134. OpBranch %20
  2135. %22 = OpLabel
  2136. OpStore %46 %9
  2137. OpBranch %47
  2138. %47 = OpLabel
  2139. %70 = OpPhi %6 %9 %22 %65 %50
  2140. OpLoopMerge %49 %50 None
  2141. OpBranch %51
  2142. %51 = OpLabel
  2143. %53 = OpSLessThan %17 %70 %16
  2144. OpBranchConditional %53 %48 %49
  2145. %48 = OpLabel
  2146. %58 = OpIAdd %6 %68 %44
  2147. %60 = OpAccessChain %7 %32 %58 %70
  2148. %61 = OpLoad %6 %60
  2149. %62 = OpIAdd %6 %61 %16
  2150. %63 = OpAccessChain %7 %54 %68 %70
  2151. OpStore %63 %62
  2152. OpBranch %50
  2153. %50 = OpLabel
  2154. %65 = OpIAdd %6 %70 %44
  2155. OpStore %46 %65
  2156. OpBranch %47
  2157. %49 = OpLabel
  2158. OpBranch %13
  2159. %13 = OpLabel
  2160. %67 = OpIAdd %6 %68 %44
  2161. OpStore %8 %67
  2162. OpBranch %10
  2163. %12 = OpLabel
  2164. OpReturn
  2165. OpFunctionEnd
  2166. )";
  2167. std::unique_ptr<IRContext> context =
  2168. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  2169. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2170. Module* module = context->module();
  2171. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  2172. << text << std::endl;
  2173. Function& f = *module->begin();
  2174. {
  2175. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  2176. EXPECT_EQ(ld.NumLoops(), 3u);
  2177. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  2178. auto loop_0 = loops[0];
  2179. auto loop_1 = loops[1];
  2180. auto loop_2 = loops[2];
  2181. {
  2182. LoopFusion fusion(context.get(), loop_0, loop_1);
  2183. EXPECT_FALSE(fusion.AreCompatible());
  2184. }
  2185. {
  2186. LoopFusion fusion(context.get(), loop_0, loop_2);
  2187. EXPECT_FALSE(fusion.AreCompatible());
  2188. }
  2189. {
  2190. LoopFusion fusion(context.get(), loop_1, loop_2);
  2191. EXPECT_TRUE(fusion.AreCompatible());
  2192. EXPECT_TRUE(fusion.IsLegal());
  2193. fusion.Fuse();
  2194. }
  2195. }
  2196. {
  2197. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  2198. EXPECT_EQ(ld.NumLoops(), 2u);
  2199. std::string checks = R"(
  2200. CHECK: [[PHI_0:%\w+]] = OpPhi
  2201. CHECK-NEXT: OpLoopMerge
  2202. CHECK: [[PHI_1:%\w+]] = OpPhi
  2203. CHECK-NEXT: OpLoopMerge
  2204. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  2205. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  2206. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  2207. CHECK-NEXT: OpStore [[STORE_0]]
  2208. CHECK-NOT: OpPhi
  2209. CHECK: [[I_1:%\w+]] = OpIAdd {{%\w+}} [[PHI_0]] {{%\w+}}
  2210. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_1]] [[PHI_1]]
  2211. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  2212. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  2213. CHECK-NEXT: OpStore [[STORE_1]]
  2214. )";
  2215. Match(checks, context.get());
  2216. }
  2217. }
  2218. /*
  2219. Generated from the following GLSL + --eliminate-local-multi-store
  2220. #version 440 core
  2221. void main() {
  2222. int[10] a;
  2223. int[10] b;
  2224. int[10] c;
  2225. // One dimensional arrays. Legal, outer dist 0, inner independent.
  2226. for (int i = 0; i < 10; i++) {
  2227. for (int j = 0; j < 10; j++) {
  2228. c[i] = a[j] + 2;
  2229. }
  2230. }
  2231. for (int i = 0; i < 10; i++) {
  2232. for (int j = 0; j < 10; j++) {
  2233. b[j] = c[i] + 10;
  2234. }
  2235. }
  2236. }
  2237. */
  2238. TEST_F(FusionLegalTest, OuterAndInnerLoopOneDimArrays) {
  2239. std::string text = R"(
  2240. OpCapability Shader
  2241. %1 = OpExtInstImport "GLSL.std.450"
  2242. OpMemoryModel Logical GLSL450
  2243. OpEntryPoint Fragment %4 "main"
  2244. OpExecutionMode %4 OriginUpperLeft
  2245. OpSource GLSL 440
  2246. OpName %4 "main"
  2247. OpName %8 "i"
  2248. OpName %19 "j"
  2249. OpName %31 "c"
  2250. OpName %33 "a"
  2251. OpName %45 "i"
  2252. OpName %53 "j"
  2253. OpName %61 "b"
  2254. %2 = OpTypeVoid
  2255. %3 = OpTypeFunction %2
  2256. %6 = OpTypeInt 32 1
  2257. %7 = OpTypePointer Function %6
  2258. %9 = OpConstant %6 0
  2259. %16 = OpConstant %6 10
  2260. %17 = OpTypeBool
  2261. %27 = OpTypeInt 32 0
  2262. %28 = OpConstant %27 10
  2263. %29 = OpTypeArray %6 %28
  2264. %30 = OpTypePointer Function %29
  2265. %37 = OpConstant %6 2
  2266. %41 = OpConstant %6 1
  2267. %4 = OpFunction %2 None %3
  2268. %5 = OpLabel
  2269. %8 = OpVariable %7 Function
  2270. %19 = OpVariable %7 Function
  2271. %31 = OpVariable %30 Function
  2272. %33 = OpVariable %30 Function
  2273. %45 = OpVariable %7 Function
  2274. %53 = OpVariable %7 Function
  2275. %61 = OpVariable %30 Function
  2276. OpStore %8 %9
  2277. OpBranch %10
  2278. %10 = OpLabel
  2279. %72 = OpPhi %6 %9 %5 %44 %13
  2280. OpLoopMerge %12 %13 None
  2281. OpBranch %14
  2282. %14 = OpLabel
  2283. %18 = OpSLessThan %17 %72 %16
  2284. OpBranchConditional %18 %11 %12
  2285. %11 = OpLabel
  2286. OpStore %19 %9
  2287. OpBranch %20
  2288. %20 = OpLabel
  2289. %76 = OpPhi %6 %9 %11 %42 %23
  2290. OpLoopMerge %22 %23 None
  2291. OpBranch %24
  2292. %24 = OpLabel
  2293. %26 = OpSLessThan %17 %76 %16
  2294. OpBranchConditional %26 %21 %22
  2295. %21 = OpLabel
  2296. %35 = OpAccessChain %7 %33 %76
  2297. %36 = OpLoad %6 %35
  2298. %38 = OpIAdd %6 %36 %37
  2299. %39 = OpAccessChain %7 %31 %72
  2300. OpStore %39 %38
  2301. OpBranch %23
  2302. %23 = OpLabel
  2303. %42 = OpIAdd %6 %76 %41
  2304. OpStore %19 %42
  2305. OpBranch %20
  2306. %22 = OpLabel
  2307. OpBranch %13
  2308. %13 = OpLabel
  2309. %44 = OpIAdd %6 %72 %41
  2310. OpStore %8 %44
  2311. OpBranch %10
  2312. %12 = OpLabel
  2313. OpStore %45 %9
  2314. OpBranch %46
  2315. %46 = OpLabel
  2316. %73 = OpPhi %6 %9 %12 %71 %49
  2317. OpLoopMerge %48 %49 None
  2318. OpBranch %50
  2319. %50 = OpLabel
  2320. %52 = OpSLessThan %17 %73 %16
  2321. OpBranchConditional %52 %47 %48
  2322. %47 = OpLabel
  2323. OpStore %53 %9
  2324. OpBranch %54
  2325. %54 = OpLabel
  2326. %74 = OpPhi %6 %9 %47 %69 %57
  2327. OpLoopMerge %56 %57 None
  2328. OpBranch %58
  2329. %58 = OpLabel
  2330. %60 = OpSLessThan %17 %74 %16
  2331. OpBranchConditional %60 %55 %56
  2332. %55 = OpLabel
  2333. %64 = OpAccessChain %7 %31 %73
  2334. %65 = OpLoad %6 %64
  2335. %66 = OpIAdd %6 %65 %16
  2336. %67 = OpAccessChain %7 %61 %74
  2337. OpStore %67 %66
  2338. OpBranch %57
  2339. %57 = OpLabel
  2340. %69 = OpIAdd %6 %74 %41
  2341. OpStore %53 %69
  2342. OpBranch %54
  2343. %56 = OpLabel
  2344. OpBranch %49
  2345. %49 = OpLabel
  2346. %71 = OpIAdd %6 %73 %41
  2347. OpStore %45 %71
  2348. OpBranch %46
  2349. %48 = OpLabel
  2350. OpReturn
  2351. OpFunctionEnd
  2352. )";
  2353. std::unique_ptr<IRContext> context =
  2354. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  2355. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2356. Module* module = context->module();
  2357. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  2358. << text << std::endl;
  2359. Function& f = *module->begin();
  2360. {
  2361. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  2362. EXPECT_EQ(ld.NumLoops(), 4u);
  2363. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  2364. auto loop_0 = loops[0];
  2365. auto loop_1 = loops[1];
  2366. auto loop_2 = loops[2];
  2367. auto loop_3 = loops[3];
  2368. {
  2369. LoopFusion fusion(context.get(), loop_0, loop_1);
  2370. EXPECT_FALSE(fusion.AreCompatible());
  2371. }
  2372. {
  2373. LoopFusion fusion(context.get(), loop_1, loop_2);
  2374. EXPECT_FALSE(fusion.AreCompatible());
  2375. }
  2376. {
  2377. LoopFusion fusion(context.get(), loop_2, loop_3);
  2378. EXPECT_FALSE(fusion.AreCompatible());
  2379. }
  2380. {
  2381. LoopFusion fusion(context.get(), loop_0, loop_2);
  2382. EXPECT_TRUE(fusion.AreCompatible());
  2383. EXPECT_TRUE(fusion.IsLegal());
  2384. fusion.Fuse();
  2385. }
  2386. std::string checks = R"(
  2387. CHECK: [[PHI_0:%\w+]] = OpPhi
  2388. CHECK-NEXT: OpLoopMerge
  2389. CHECK: [[PHI_1:%\w+]] = OpPhi
  2390. CHECK-NEXT: OpLoopMerge
  2391. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_1]]
  2392. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  2393. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]]
  2394. CHECK-NEXT: OpStore [[STORE_0]]
  2395. CHECK: [[PHI_2:%\w+]] = OpPhi
  2396. CHECK-NEXT: OpLoopMerge
  2397. CHECK-NOT: OpPhi
  2398. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]]
  2399. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  2400. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_2]]
  2401. CHECK-NEXT: OpStore [[STORE_1]]
  2402. )";
  2403. Match(checks, context.get());
  2404. }
  2405. {
  2406. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  2407. EXPECT_EQ(ld.NumLoops(), 3u);
  2408. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  2409. auto loop_0 = loops[0];
  2410. auto loop_1 = loops[1];
  2411. auto loop_2 = loops[2];
  2412. {
  2413. LoopFusion fusion(context.get(), loop_0, loop_1);
  2414. EXPECT_FALSE(fusion.AreCompatible());
  2415. }
  2416. {
  2417. LoopFusion fusion(context.get(), loop_0, loop_2);
  2418. EXPECT_FALSE(fusion.AreCompatible());
  2419. }
  2420. {
  2421. LoopFusion fusion(context.get(), loop_1, loop_2);
  2422. EXPECT_TRUE(fusion.AreCompatible());
  2423. EXPECT_TRUE(fusion.IsLegal());
  2424. fusion.Fuse();
  2425. }
  2426. std::string checks = R"(
  2427. CHECK: [[PHI_0:%\w+]] = OpPhi
  2428. CHECK-NEXT: OpLoopMerge
  2429. CHECK: [[PHI_1:%\w+]] = OpPhi
  2430. CHECK-NEXT: OpLoopMerge
  2431. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_1]]
  2432. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  2433. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]]
  2434. CHECK-NEXT: OpStore [[STORE_0]]
  2435. CHECK-NOT: OpPhi
  2436. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]]
  2437. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  2438. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_1]]
  2439. CHECK-NEXT: OpStore [[STORE_1]]
  2440. )";
  2441. Match(checks, context.get());
  2442. }
  2443. }
  2444. /*
  2445. Generated from the following GLSL + --eliminate-local-multi-store
  2446. #version 440 core
  2447. void main() {
  2448. int[10] a;
  2449. int[10] b;
  2450. int[10] c;
  2451. // Legal, creates a loop-carried dependence, but has negative distance
  2452. for (int i = 0; i < 10; i++) {
  2453. c[i] = a[i+1] + 1;
  2454. }
  2455. for (int i = 0; i < 10; i++) {
  2456. a[i] = c[i] + 2;
  2457. }
  2458. }
  2459. */
  2460. TEST_F(FusionLegalTest, NegativeDistanceCreatedWAR) {
  2461. std::string text = R"(
  2462. OpCapability Shader
  2463. %1 = OpExtInstImport "GLSL.std.450"
  2464. OpMemoryModel Logical GLSL450
  2465. OpEntryPoint Fragment %4 "main"
  2466. OpExecutionMode %4 OriginUpperLeft
  2467. OpSource GLSL 440
  2468. OpName %4 "main"
  2469. OpName %8 "i"
  2470. OpName %23 "c"
  2471. OpName %25 "a"
  2472. OpName %35 "i"
  2473. %2 = OpTypeVoid
  2474. %3 = OpTypeFunction %2
  2475. %6 = OpTypeInt 32 1
  2476. %7 = OpTypePointer Function %6
  2477. %9 = OpConstant %6 0
  2478. %16 = OpConstant %6 10
  2479. %17 = OpTypeBool
  2480. %19 = OpTypeInt 32 0
  2481. %20 = OpConstant %19 10
  2482. %21 = OpTypeArray %6 %20
  2483. %22 = OpTypePointer Function %21
  2484. %27 = OpConstant %6 1
  2485. %47 = OpConstant %6 2
  2486. %4 = OpFunction %2 None %3
  2487. %5 = OpLabel
  2488. %8 = OpVariable %7 Function
  2489. %23 = OpVariable %22 Function
  2490. %25 = OpVariable %22 Function
  2491. %35 = OpVariable %7 Function
  2492. OpStore %8 %9
  2493. OpBranch %10
  2494. %10 = OpLabel
  2495. %52 = OpPhi %6 %9 %5 %34 %13
  2496. OpLoopMerge %12 %13 None
  2497. OpBranch %14
  2498. %14 = OpLabel
  2499. %18 = OpSLessThan %17 %52 %16
  2500. OpBranchConditional %18 %11 %12
  2501. %11 = OpLabel
  2502. %28 = OpIAdd %6 %52 %27
  2503. %29 = OpAccessChain %7 %25 %28
  2504. %30 = OpLoad %6 %29
  2505. %31 = OpIAdd %6 %30 %27
  2506. %32 = OpAccessChain %7 %23 %52
  2507. OpStore %32 %31
  2508. OpBranch %13
  2509. %13 = OpLabel
  2510. %34 = OpIAdd %6 %52 %27
  2511. OpStore %8 %34
  2512. OpBranch %10
  2513. %12 = OpLabel
  2514. OpStore %35 %9
  2515. OpBranch %36
  2516. %36 = OpLabel
  2517. %53 = OpPhi %6 %9 %12 %51 %39
  2518. OpLoopMerge %38 %39 None
  2519. OpBranch %40
  2520. %40 = OpLabel
  2521. %42 = OpSLessThan %17 %53 %16
  2522. OpBranchConditional %42 %37 %38
  2523. %37 = OpLabel
  2524. %45 = OpAccessChain %7 %23 %53
  2525. %46 = OpLoad %6 %45
  2526. %48 = OpIAdd %6 %46 %47
  2527. %49 = OpAccessChain %7 %25 %53
  2528. OpStore %49 %48
  2529. OpBranch %39
  2530. %39 = OpLabel
  2531. %51 = OpIAdd %6 %53 %27
  2532. OpStore %35 %51
  2533. OpBranch %36
  2534. %38 = OpLabel
  2535. OpReturn
  2536. OpFunctionEnd
  2537. )";
  2538. std::unique_ptr<IRContext> context =
  2539. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  2540. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2541. Module* module = context->module();
  2542. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  2543. << text << std::endl;
  2544. Function& f = *module->begin();
  2545. {
  2546. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  2547. EXPECT_EQ(ld.NumLoops(), 2u);
  2548. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  2549. LoopFusion fusion(context.get(), loops[0], loops[1]);
  2550. EXPECT_TRUE(fusion.AreCompatible());
  2551. EXPECT_TRUE(fusion.IsLegal());
  2552. fusion.Fuse();
  2553. std::string checks = R"(
  2554. CHECK: [[PHI:%\w+]] = OpPhi
  2555. CHECK: [[I_1:%\w+]] = OpIAdd {{%\w+}} [[PHI]] {{%\w+}}
  2556. CHECK-NEXT: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_1]]
  2557. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  2558. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  2559. CHECK-NEXT: OpStore [[STORE_0]]
  2560. CHECK-NOT: OpPhi
  2561. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  2562. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  2563. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  2564. CHECK-NEXT: OpStore [[STORE_1]]
  2565. )";
  2566. Match(checks, context.get());
  2567. }
  2568. {
  2569. auto& ld = *context->GetLoopDescriptor(&f);
  2570. EXPECT_EQ(ld.NumLoops(), 1u);
  2571. }
  2572. }
  2573. /*
  2574. Generated from the following GLSL + --eliminate-local-multi-store
  2575. #version 440 core
  2576. void main() {
  2577. int[10] a;
  2578. int[10] b;
  2579. int[10] c;
  2580. // Legal, creates a loop-carried dependence, but has negative distance
  2581. for (int i = 0; i < 10; i++) {
  2582. a[i+1] = b[i] + 1;
  2583. }
  2584. for (int i = 0; i < 10; i++) {
  2585. a[i] = c[i+1] + 2;
  2586. }
  2587. }
  2588. */
  2589. TEST_F(FusionLegalTest, NegativeDistanceCreatedWAW) {
  2590. std::string text = R"(
  2591. OpCapability Shader
  2592. %1 = OpExtInstImport "GLSL.std.450"
  2593. OpMemoryModel Logical GLSL450
  2594. OpEntryPoint Fragment %4 "main"
  2595. OpExecutionMode %4 OriginUpperLeft
  2596. OpSource GLSL 440
  2597. OpName %4 "main"
  2598. OpName %8 "i"
  2599. OpName %23 "a"
  2600. OpName %27 "b"
  2601. OpName %35 "i"
  2602. OpName %44 "c"
  2603. %2 = OpTypeVoid
  2604. %3 = OpTypeFunction %2
  2605. %6 = OpTypeInt 32 1
  2606. %7 = OpTypePointer Function %6
  2607. %9 = OpConstant %6 0
  2608. %16 = OpConstant %6 10
  2609. %17 = OpTypeBool
  2610. %19 = OpTypeInt 32 0
  2611. %20 = OpConstant %19 10
  2612. %21 = OpTypeArray %6 %20
  2613. %22 = OpTypePointer Function %21
  2614. %25 = OpConstant %6 1
  2615. %49 = OpConstant %6 2
  2616. %4 = OpFunction %2 None %3
  2617. %5 = OpLabel
  2618. %8 = OpVariable %7 Function
  2619. %23 = OpVariable %22 Function
  2620. %27 = OpVariable %22 Function
  2621. %35 = OpVariable %7 Function
  2622. %44 = OpVariable %22 Function
  2623. OpStore %8 %9
  2624. OpBranch %10
  2625. %10 = OpLabel
  2626. %54 = OpPhi %6 %9 %5 %34 %13
  2627. OpLoopMerge %12 %13 None
  2628. OpBranch %14
  2629. %14 = OpLabel
  2630. %18 = OpSLessThan %17 %54 %16
  2631. OpBranchConditional %18 %11 %12
  2632. %11 = OpLabel
  2633. %26 = OpIAdd %6 %54 %25
  2634. %29 = OpAccessChain %7 %27 %54
  2635. %30 = OpLoad %6 %29
  2636. %31 = OpIAdd %6 %30 %25
  2637. %32 = OpAccessChain %7 %23 %26
  2638. OpStore %32 %31
  2639. OpBranch %13
  2640. %13 = OpLabel
  2641. %34 = OpIAdd %6 %54 %25
  2642. OpStore %8 %34
  2643. OpBranch %10
  2644. %12 = OpLabel
  2645. OpStore %35 %9
  2646. OpBranch %36
  2647. %36 = OpLabel
  2648. %55 = OpPhi %6 %9 %12 %53 %39
  2649. OpLoopMerge %38 %39 None
  2650. OpBranch %40
  2651. %40 = OpLabel
  2652. %42 = OpSLessThan %17 %55 %16
  2653. OpBranchConditional %42 %37 %38
  2654. %37 = OpLabel
  2655. %46 = OpIAdd %6 %55 %25
  2656. %47 = OpAccessChain %7 %44 %46
  2657. %48 = OpLoad %6 %47
  2658. %50 = OpIAdd %6 %48 %49
  2659. %51 = OpAccessChain %7 %23 %55
  2660. OpStore %51 %50
  2661. OpBranch %39
  2662. %39 = OpLabel
  2663. %53 = OpIAdd %6 %55 %25
  2664. OpStore %35 %53
  2665. OpBranch %36
  2666. %38 = OpLabel
  2667. OpReturn
  2668. OpFunctionEnd
  2669. )";
  2670. std::unique_ptr<IRContext> context =
  2671. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  2672. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2673. Module* module = context->module();
  2674. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  2675. << text << std::endl;
  2676. Function& f = *module->begin();
  2677. {
  2678. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  2679. EXPECT_EQ(ld.NumLoops(), 2u);
  2680. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  2681. LoopFusion fusion(context.get(), loops[0], loops[1]);
  2682. EXPECT_TRUE(fusion.AreCompatible());
  2683. EXPECT_TRUE(fusion.IsLegal());
  2684. fusion.Fuse();
  2685. }
  2686. {
  2687. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  2688. EXPECT_EQ(ld.NumLoops(), 1u);
  2689. std::string checks = R"(
  2690. CHECK: [[PHI:%\w+]] = OpPhi
  2691. CHECK-NEXT: OpLoopMerge
  2692. CHECK: [[I_1:%\w+]] = OpIAdd {{%\w+}} [[PHI]] {{%\w+}}
  2693. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  2694. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  2695. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_1]]
  2696. CHECK-NEXT: OpStore
  2697. CHECK-NOT: OpPhi
  2698. CHECK: [[I_1:%\w+]] = OpIAdd {{%\w+}} [[PHI]] {{%\w+}}
  2699. CHECK-NEXT: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_1]]
  2700. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  2701. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  2702. CHECK-NEXT: OpStore [[STORE_1]]
  2703. )";
  2704. Match(checks, context.get());
  2705. }
  2706. }
  2707. /*
  2708. Generated from the following GLSL + --eliminate-local-multi-store
  2709. #version 440 core
  2710. void main() {
  2711. int[10] a;
  2712. int[10] b;
  2713. int[10] c;
  2714. // Legal, no loop-carried dependence
  2715. for (int i = 0; i < 10; i++) {
  2716. a[i] = b[i] + 1;
  2717. }
  2718. for (int i = 0; i < 10; i++) {
  2719. a[i] = c[i+1] + 2;
  2720. }
  2721. }
  2722. */
  2723. TEST_F(FusionLegalTest, NoLoopCarriedDependencesWAW) {
  2724. std::string text = R"(
  2725. OpCapability Shader
  2726. %1 = OpExtInstImport "GLSL.std.450"
  2727. OpMemoryModel Logical GLSL450
  2728. OpEntryPoint Fragment %4 "main"
  2729. OpExecutionMode %4 OriginUpperLeft
  2730. OpSource GLSL 440
  2731. OpName %4 "main"
  2732. OpName %8 "i"
  2733. OpName %23 "a"
  2734. OpName %25 "b"
  2735. OpName %34 "i"
  2736. OpName %43 "c"
  2737. %2 = OpTypeVoid
  2738. %3 = OpTypeFunction %2
  2739. %6 = OpTypeInt 32 1
  2740. %7 = OpTypePointer Function %6
  2741. %9 = OpConstant %6 0
  2742. %16 = OpConstant %6 10
  2743. %17 = OpTypeBool
  2744. %19 = OpTypeInt 32 0
  2745. %20 = OpConstant %19 10
  2746. %21 = OpTypeArray %6 %20
  2747. %22 = OpTypePointer Function %21
  2748. %29 = OpConstant %6 1
  2749. %48 = OpConstant %6 2
  2750. %4 = OpFunction %2 None %3
  2751. %5 = OpLabel
  2752. %8 = OpVariable %7 Function
  2753. %23 = OpVariable %22 Function
  2754. %25 = OpVariable %22 Function
  2755. %34 = OpVariable %7 Function
  2756. %43 = OpVariable %22 Function
  2757. OpStore %8 %9
  2758. OpBranch %10
  2759. %10 = OpLabel
  2760. %53 = OpPhi %6 %9 %5 %33 %13
  2761. OpLoopMerge %12 %13 None
  2762. OpBranch %14
  2763. %14 = OpLabel
  2764. %18 = OpSLessThan %17 %53 %16
  2765. OpBranchConditional %18 %11 %12
  2766. %11 = OpLabel
  2767. %27 = OpAccessChain %7 %25 %53
  2768. %28 = OpLoad %6 %27
  2769. %30 = OpIAdd %6 %28 %29
  2770. %31 = OpAccessChain %7 %23 %53
  2771. OpStore %31 %30
  2772. OpBranch %13
  2773. %13 = OpLabel
  2774. %33 = OpIAdd %6 %53 %29
  2775. OpStore %8 %33
  2776. OpBranch %10
  2777. %12 = OpLabel
  2778. OpStore %34 %9
  2779. OpBranch %35
  2780. %35 = OpLabel
  2781. %54 = OpPhi %6 %9 %12 %52 %38
  2782. OpLoopMerge %37 %38 None
  2783. OpBranch %39
  2784. %39 = OpLabel
  2785. %41 = OpSLessThan %17 %54 %16
  2786. OpBranchConditional %41 %36 %37
  2787. %36 = OpLabel
  2788. %45 = OpIAdd %6 %54 %29
  2789. %46 = OpAccessChain %7 %43 %45
  2790. %47 = OpLoad %6 %46
  2791. %49 = OpIAdd %6 %47 %48
  2792. %50 = OpAccessChain %7 %23 %54
  2793. OpStore %50 %49
  2794. OpBranch %38
  2795. %38 = OpLabel
  2796. %52 = OpIAdd %6 %54 %29
  2797. OpStore %34 %52
  2798. OpBranch %35
  2799. %37 = OpLabel
  2800. OpReturn
  2801. OpFunctionEnd
  2802. )";
  2803. std::unique_ptr<IRContext> context =
  2804. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  2805. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2806. Module* module = context->module();
  2807. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  2808. << text << std::endl;
  2809. Function& f = *module->begin();
  2810. {
  2811. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  2812. EXPECT_EQ(ld.NumLoops(), 2u);
  2813. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  2814. LoopFusion fusion(context.get(), loops[0], loops[1]);
  2815. EXPECT_TRUE(fusion.AreCompatible());
  2816. EXPECT_TRUE(fusion.IsLegal());
  2817. fusion.Fuse();
  2818. }
  2819. {
  2820. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  2821. EXPECT_EQ(ld.NumLoops(), 1u);
  2822. std::string checks = R"(
  2823. CHECK: [[PHI:%\w+]] = OpPhi
  2824. CHECK-NEXT: OpLoopMerge
  2825. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  2826. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  2827. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  2828. CHECK-NEXT: OpStore [[STORE_0]]
  2829. CHECK-NOT: OpPhi
  2830. CHECK: [[I_1:%\w+]] = OpIAdd {{%\w+}} [[PHI]] {{%\w+}}
  2831. CHECK-NEXT: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_1]]
  2832. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  2833. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  2834. CHECK-NEXT: OpStore [[STORE_1]]
  2835. )";
  2836. Match(checks, context.get());
  2837. }
  2838. }
  2839. /*
  2840. Generated from the following GLSL + --eliminate-local-multi-store
  2841. #version 440 core
  2842. void main() {
  2843. int[10][10] a;
  2844. int[10][10] b;
  2845. int[10][10] c;
  2846. // Legal outer. Continue and break are fine if nested in inner loops
  2847. for (int i = 0; i < 10; i++) {
  2848. for (int j = 0; j < 10; j++) {
  2849. if (j % 2 == 0) {
  2850. c[i][j] = a[i][j] + 2;
  2851. } else {
  2852. continue;
  2853. }
  2854. }
  2855. }
  2856. for (int i = 0; i < 10; i++) {
  2857. for (int j = 0; j < 10; j++) {
  2858. if (j % 2 == 0) {
  2859. b[i][j] = c[i][j] + 10;
  2860. } else {
  2861. break;
  2862. }
  2863. }
  2864. }
  2865. }
  2866. */
  2867. TEST_F(FusionLegalTest, OuterloopWithBreakContinueInInner) {
  2868. std::string text = R"(
  2869. OpCapability Shader
  2870. %1 = OpExtInstImport "GLSL.std.450"
  2871. OpMemoryModel Logical GLSL450
  2872. OpEntryPoint Fragment %4 "main"
  2873. OpExecutionMode %4 OriginUpperLeft
  2874. OpSource GLSL 440
  2875. OpName %4 "main"
  2876. OpName %8 "i"
  2877. OpName %19 "j"
  2878. OpName %38 "c"
  2879. OpName %41 "a"
  2880. OpName %55 "i"
  2881. OpName %63 "j"
  2882. OpName %76 "b"
  2883. %2 = OpTypeVoid
  2884. %3 = OpTypeFunction %2
  2885. %6 = OpTypeInt 32 1
  2886. %7 = OpTypePointer Function %6
  2887. %9 = OpConstant %6 0
  2888. %16 = OpConstant %6 10
  2889. %17 = OpTypeBool
  2890. %28 = OpConstant %6 2
  2891. %33 = OpTypeInt 32 0
  2892. %34 = OpConstant %33 10
  2893. %35 = OpTypeArray %6 %34
  2894. %36 = OpTypeArray %35 %34
  2895. %37 = OpTypePointer Function %36
  2896. %51 = OpConstant %6 1
  2897. %4 = OpFunction %2 None %3
  2898. %5 = OpLabel
  2899. %8 = OpVariable %7 Function
  2900. %19 = OpVariable %7 Function
  2901. %38 = OpVariable %37 Function
  2902. %41 = OpVariable %37 Function
  2903. %55 = OpVariable %7 Function
  2904. %63 = OpVariable %7 Function
  2905. %76 = OpVariable %37 Function
  2906. OpStore %8 %9
  2907. OpBranch %10
  2908. %10 = OpLabel
  2909. %91 = OpPhi %6 %9 %5 %54 %13
  2910. OpLoopMerge %12 %13 None
  2911. OpBranch %14
  2912. %14 = OpLabel
  2913. %18 = OpSLessThan %17 %91 %16
  2914. OpBranchConditional %18 %11 %12
  2915. %11 = OpLabel
  2916. OpStore %19 %9
  2917. OpBranch %20
  2918. %20 = OpLabel
  2919. %96 = OpPhi %6 %9 %11 %52 %23
  2920. OpLoopMerge %22 %23 None
  2921. OpBranch %24
  2922. %24 = OpLabel
  2923. %26 = OpSLessThan %17 %96 %16
  2924. OpBranchConditional %26 %21 %22
  2925. %21 = OpLabel
  2926. %29 = OpSMod %6 %96 %28
  2927. %30 = OpIEqual %17 %29 %9
  2928. OpSelectionMerge %sel_merge None
  2929. OpBranchConditional %30 %31 %48
  2930. %31 = OpLabel
  2931. %44 = OpAccessChain %7 %41 %91 %96
  2932. %45 = OpLoad %6 %44
  2933. %46 = OpIAdd %6 %45 %28
  2934. %47 = OpAccessChain %7 %38 %91 %96
  2935. OpStore %47 %46
  2936. OpBranch %32
  2937. %48 = OpLabel
  2938. OpBranch %sel_merge
  2939. %32 = OpLabel
  2940. OpBranch %sel_merge
  2941. %sel_merge = OpLabel
  2942. OpBranch %23
  2943. %23 = OpLabel
  2944. %52 = OpIAdd %6 %96 %51
  2945. OpStore %19 %52
  2946. OpBranch %20
  2947. %22 = OpLabel
  2948. OpBranch %13
  2949. %13 = OpLabel
  2950. %54 = OpIAdd %6 %91 %51
  2951. OpStore %8 %54
  2952. OpBranch %10
  2953. %12 = OpLabel
  2954. OpStore %55 %9
  2955. OpBranch %56
  2956. %56 = OpLabel
  2957. %92 = OpPhi %6 %9 %12 %90 %59
  2958. OpLoopMerge %58 %59 None
  2959. OpBranch %60
  2960. %60 = OpLabel
  2961. %62 = OpSLessThan %17 %92 %16
  2962. OpBranchConditional %62 %57 %58
  2963. %57 = OpLabel
  2964. OpStore %63 %9
  2965. OpBranch %64
  2966. %64 = OpLabel
  2967. %93 = OpPhi %6 %9 %57 %88 %67
  2968. OpLoopMerge %66 %67 None
  2969. OpBranch %68
  2970. %68 = OpLabel
  2971. %70 = OpSLessThan %17 %93 %16
  2972. OpBranchConditional %70 %65 %66
  2973. %65 = OpLabel
  2974. %72 = OpSMod %6 %93 %28
  2975. %73 = OpIEqual %17 %72 %9
  2976. OpSelectionMerge %75 None
  2977. OpBranchConditional %73 %74 %66
  2978. %74 = OpLabel
  2979. %81 = OpAccessChain %7 %38 %92 %93
  2980. %82 = OpLoad %6 %81
  2981. %83 = OpIAdd %6 %82 %16
  2982. %84 = OpAccessChain %7 %76 %92 %93
  2983. OpStore %84 %83
  2984. OpBranch %75
  2985. %75 = OpLabel
  2986. OpBranch %67
  2987. %67 = OpLabel
  2988. %88 = OpIAdd %6 %93 %51
  2989. OpStore %63 %88
  2990. OpBranch %64
  2991. %66 = OpLabel
  2992. OpBranch %59
  2993. %59 = OpLabel
  2994. %90 = OpIAdd %6 %92 %51
  2995. OpStore %55 %90
  2996. OpBranch %56
  2997. %58 = OpLabel
  2998. OpReturn
  2999. OpFunctionEnd
  3000. )";
  3001. std::unique_ptr<IRContext> context =
  3002. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  3003. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3004. Module* module = context->module();
  3005. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  3006. << text << std::endl;
  3007. Function& f = *module->begin();
  3008. {
  3009. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  3010. EXPECT_EQ(ld.NumLoops(), 4u);
  3011. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  3012. LoopFusion fusion(context.get(), loops[0], loops[2]);
  3013. EXPECT_TRUE(fusion.AreCompatible());
  3014. EXPECT_TRUE(fusion.IsLegal());
  3015. fusion.Fuse();
  3016. }
  3017. {
  3018. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  3019. EXPECT_EQ(ld.NumLoops(), 3u);
  3020. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  3021. LoopFusion fusion(context.get(), loops[1], loops[2]);
  3022. EXPECT_FALSE(fusion.AreCompatible());
  3023. std::string checks = R"(
  3024. CHECK: [[PHI_0:%\w+]] = OpPhi
  3025. CHECK-NEXT: OpLoopMerge
  3026. CHECK: [[PHI_1:%\w+]] = OpPhi
  3027. CHECK-NEXT: OpLoopMerge
  3028. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  3029. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  3030. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
  3031. CHECK-NEXT: OpStore [[STORE_0]]
  3032. CHECK: [[PHI_2:%\w+]] = OpPhi
  3033. CHECK-NEXT: OpLoopMerge
  3034. CHECK-NOT: OpPhi
  3035. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_2]]
  3036. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  3037. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_2]]
  3038. CHECK-NEXT: OpStore [[STORE_1]]
  3039. )";
  3040. Match(checks, context.get());
  3041. }
  3042. }
  3043. /*
  3044. Generated from the following GLSL + --eliminate-local-multi-store
  3045. // j loop preheader removed manually
  3046. #version 440 core
  3047. void main() {
  3048. int[10] a;
  3049. int[10] b;
  3050. int i = 0;
  3051. int j = 0;
  3052. // No loop-carried dependences, legal
  3053. for (; i < 10; i++) {
  3054. a[i] = a[i]*2;
  3055. }
  3056. for (; j < 10; j++) {
  3057. b[j] = a[j]+2;
  3058. }
  3059. }
  3060. */
  3061. TEST_F(FusionLegalTest, DifferentArraysInLoopsNoPreheader) {
  3062. std::string text = R"(
  3063. OpCapability Shader
  3064. %1 = OpExtInstImport "GLSL.std.450"
  3065. OpMemoryModel Logical GLSL450
  3066. OpEntryPoint Fragment %4 "main"
  3067. OpExecutionMode %4 OriginUpperLeft
  3068. OpSource GLSL 440
  3069. OpName %4 "main"
  3070. OpName %8 "i"
  3071. OpName %10 "j"
  3072. OpName %24 "a"
  3073. OpName %42 "b"
  3074. %2 = OpTypeVoid
  3075. %3 = OpTypeFunction %2
  3076. %6 = OpTypeInt 32 1
  3077. %7 = OpTypePointer Function %6
  3078. %9 = OpConstant %6 0
  3079. %17 = OpConstant %6 10
  3080. %18 = OpTypeBool
  3081. %20 = OpTypeInt 32 0
  3082. %21 = OpConstant %20 10
  3083. %22 = OpTypeArray %6 %21
  3084. %23 = OpTypePointer Function %22
  3085. %29 = OpConstant %6 2
  3086. %33 = OpConstant %6 1
  3087. %4 = OpFunction %2 None %3
  3088. %5 = OpLabel
  3089. %8 = OpVariable %7 Function
  3090. %10 = OpVariable %7 Function
  3091. %24 = OpVariable %23 Function
  3092. %42 = OpVariable %23 Function
  3093. OpStore %8 %9
  3094. OpStore %10 %9
  3095. OpBranch %11
  3096. %11 = OpLabel
  3097. %51 = OpPhi %6 %9 %5 %34 %14
  3098. OpLoopMerge %35 %14 None
  3099. OpBranch %15
  3100. %15 = OpLabel
  3101. %19 = OpSLessThan %18 %51 %17
  3102. OpBranchConditional %19 %12 %35
  3103. %12 = OpLabel
  3104. %27 = OpAccessChain %7 %24 %51
  3105. %28 = OpLoad %6 %27
  3106. %30 = OpIMul %6 %28 %29
  3107. %31 = OpAccessChain %7 %24 %51
  3108. OpStore %31 %30
  3109. OpBranch %14
  3110. %14 = OpLabel
  3111. %34 = OpIAdd %6 %51 %33
  3112. OpStore %8 %34
  3113. OpBranch %11
  3114. %35 = OpLabel
  3115. %52 = OpPhi %6 %9 %15 %50 %38
  3116. OpLoopMerge %37 %38 None
  3117. OpBranch %39
  3118. %39 = OpLabel
  3119. %41 = OpSLessThan %18 %52 %17
  3120. OpBranchConditional %41 %36 %37
  3121. %36 = OpLabel
  3122. %45 = OpAccessChain %7 %24 %52
  3123. %46 = OpLoad %6 %45
  3124. %47 = OpIAdd %6 %46 %29
  3125. %48 = OpAccessChain %7 %42 %52
  3126. OpStore %48 %47
  3127. OpBranch %38
  3128. %38 = OpLabel
  3129. %50 = OpIAdd %6 %52 %33
  3130. OpStore %10 %50
  3131. OpBranch %35
  3132. %37 = OpLabel
  3133. OpReturn
  3134. OpFunctionEnd
  3135. )";
  3136. std::unique_ptr<IRContext> context =
  3137. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  3138. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3139. Module* module = context->module();
  3140. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  3141. << text << std::endl;
  3142. Function& f = *module->begin();
  3143. {
  3144. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  3145. EXPECT_EQ(ld.NumLoops(), 2u);
  3146. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  3147. {
  3148. LoopFusion fusion(context.get(), loops[0], loops[1]);
  3149. EXPECT_FALSE(fusion.AreCompatible());
  3150. }
  3151. ld.CreatePreHeaderBlocksIfMissing();
  3152. {
  3153. LoopFusion fusion(context.get(), loops[0], loops[1]);
  3154. EXPECT_TRUE(fusion.AreCompatible());
  3155. EXPECT_TRUE(fusion.IsLegal());
  3156. fusion.Fuse();
  3157. }
  3158. }
  3159. {
  3160. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  3161. EXPECT_EQ(ld.NumLoops(), 1u);
  3162. std::string checks = R"(
  3163. CHECK: [[PHI:%\w+]] = OpPhi
  3164. CHECK-NEXT: OpLoopMerge
  3165. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  3166. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  3167. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  3168. CHECK-NEXT: OpStore [[STORE_0]]
  3169. CHECK-NOT: OpPhi
  3170. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  3171. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  3172. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  3173. CHECK-NEXT: OpStore [[STORE_1]]
  3174. )";
  3175. Match(checks, context.get());
  3176. }
  3177. }
  3178. /*
  3179. Generated from the following GLSL + --eliminate-local-multi-store
  3180. // j & k loop preheaders removed manually
  3181. #version 440 core
  3182. void main() {
  3183. int[10] a;
  3184. int[10] b;
  3185. int i = 0;
  3186. int j = 0;
  3187. int k = 0;
  3188. // No loop-carried dependences, legal
  3189. for (; i < 10; i++) {
  3190. a[i] = a[i]*2;
  3191. }
  3192. for (; j < 10; j++) {
  3193. b[j] = a[j]+2;
  3194. }
  3195. for (; k < 10; k++) {
  3196. a[k] = a[k]*2;
  3197. }
  3198. }
  3199. */
  3200. TEST_F(FusionLegalTest, AdjacentLoopsNoPreheaders) {
  3201. std::string text = R"(
  3202. OpCapability Shader
  3203. %1 = OpExtInstImport "GLSL.std.450"
  3204. OpMemoryModel Logical GLSL450
  3205. OpEntryPoint Fragment %4 "main"
  3206. OpExecutionMode %4 OriginUpperLeft
  3207. OpSource GLSL 440
  3208. OpName %4 "main"
  3209. OpName %8 "i"
  3210. OpName %10 "j"
  3211. OpName %11 "k"
  3212. OpName %25 "a"
  3213. OpName %43 "b"
  3214. %2 = OpTypeVoid
  3215. %3 = OpTypeFunction %2
  3216. %6 = OpTypeInt 32 1
  3217. %7 = OpTypePointer Function %6
  3218. %9 = OpConstant %6 0
  3219. %18 = OpConstant %6 10
  3220. %19 = OpTypeBool
  3221. %21 = OpTypeInt 32 0
  3222. %22 = OpConstant %21 10
  3223. %23 = OpTypeArray %6 %22
  3224. %24 = OpTypePointer Function %23
  3225. %30 = OpConstant %6 2
  3226. %34 = OpConstant %6 1
  3227. %4 = OpFunction %2 None %3
  3228. %5 = OpLabel
  3229. %8 = OpVariable %7 Function
  3230. %10 = OpVariable %7 Function
  3231. %11 = OpVariable %7 Function
  3232. %25 = OpVariable %24 Function
  3233. %43 = OpVariable %24 Function
  3234. OpStore %8 %9
  3235. OpStore %10 %9
  3236. OpStore %11 %9
  3237. OpBranch %12
  3238. %12 = OpLabel
  3239. %67 = OpPhi %6 %9 %5 %35 %15
  3240. OpLoopMerge %36 %15 None
  3241. OpBranch %16
  3242. %16 = OpLabel
  3243. %20 = OpSLessThan %19 %67 %18
  3244. OpBranchConditional %20 %13 %36
  3245. %13 = OpLabel
  3246. %28 = OpAccessChain %7 %25 %67
  3247. %29 = OpLoad %6 %28
  3248. %31 = OpIMul %6 %29 %30
  3249. %32 = OpAccessChain %7 %25 %67
  3250. OpStore %32 %31
  3251. OpBranch %15
  3252. %15 = OpLabel
  3253. %35 = OpIAdd %6 %67 %34
  3254. OpStore %8 %35
  3255. OpBranch %12
  3256. %36 = OpLabel
  3257. %68 = OpPhi %6 %9 %16 %51 %39
  3258. OpLoopMerge %52 %39 None
  3259. OpBranch %40
  3260. %40 = OpLabel
  3261. %42 = OpSLessThan %19 %68 %18
  3262. OpBranchConditional %42 %37 %52
  3263. %37 = OpLabel
  3264. %46 = OpAccessChain %7 %25 %68
  3265. %47 = OpLoad %6 %46
  3266. %48 = OpIAdd %6 %47 %30
  3267. %49 = OpAccessChain %7 %43 %68
  3268. OpStore %49 %48
  3269. OpBranch %39
  3270. %39 = OpLabel
  3271. %51 = OpIAdd %6 %68 %34
  3272. OpStore %10 %51
  3273. OpBranch %36
  3274. %52 = OpLabel
  3275. %70 = OpPhi %6 %9 %40 %66 %55
  3276. OpLoopMerge %54 %55 None
  3277. OpBranch %56
  3278. %56 = OpLabel
  3279. %58 = OpSLessThan %19 %70 %18
  3280. OpBranchConditional %58 %53 %54
  3281. %53 = OpLabel
  3282. %61 = OpAccessChain %7 %25 %70
  3283. %62 = OpLoad %6 %61
  3284. %63 = OpIMul %6 %62 %30
  3285. %64 = OpAccessChain %7 %25 %70
  3286. OpStore %64 %63
  3287. OpBranch %55
  3288. %55 = OpLabel
  3289. %66 = OpIAdd %6 %70 %34
  3290. OpStore %11 %66
  3291. OpBranch %52
  3292. %54 = OpLabel
  3293. OpReturn
  3294. OpFunctionEnd
  3295. )";
  3296. std::unique_ptr<IRContext> context =
  3297. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  3298. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3299. Module* module = context->module();
  3300. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  3301. << text << std::endl;
  3302. Function& f = *module->begin();
  3303. {
  3304. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  3305. EXPECT_EQ(ld.NumLoops(), 3u);
  3306. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  3307. {
  3308. LoopFusion fusion(context.get(), loops[0], loops[1]);
  3309. EXPECT_FALSE(fusion.AreCompatible());
  3310. }
  3311. ld.CreatePreHeaderBlocksIfMissing();
  3312. {
  3313. LoopFusion fusion(context.get(), loops[0], loops[1]);
  3314. EXPECT_TRUE(fusion.AreCompatible());
  3315. EXPECT_TRUE(fusion.IsLegal());
  3316. fusion.Fuse();
  3317. }
  3318. }
  3319. {
  3320. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  3321. EXPECT_EQ(ld.NumLoops(), 2u);
  3322. std::string checks = R"(
  3323. CHECK: [[PHI_0:%\w+]] = OpPhi
  3324. CHECK-NEXT: OpLoopMerge
  3325. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]]
  3326. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  3327. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]]
  3328. CHECK-NEXT: OpStore [[STORE_0]]
  3329. CHECK-NOT: OpPhi
  3330. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]]
  3331. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  3332. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]]
  3333. CHECK-NEXT: OpStore [[STORE_1]]
  3334. CHECK: [[PHI_1:%\w+]] = OpPhi
  3335. CHECK-NEXT: OpLoopMerge
  3336. CHECK: [[LOAD_2:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_1]]
  3337. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_2]]
  3338. CHECK: [[STORE_2:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_1]]
  3339. CHECK-NEXT: OpStore [[STORE_2]]
  3340. )";
  3341. Match(checks, context.get());
  3342. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  3343. LoopFusion fusion(context.get(), loops[0], loops[1]);
  3344. EXPECT_TRUE(fusion.AreCompatible());
  3345. EXPECT_TRUE(fusion.IsLegal());
  3346. fusion.Fuse();
  3347. }
  3348. {
  3349. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  3350. EXPECT_EQ(ld.NumLoops(), 1u);
  3351. std::string checks = R"(
  3352. CHECK: [[PHI:%\w+]] = OpPhi
  3353. CHECK-NEXT: OpLoopMerge
  3354. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  3355. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
  3356. CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  3357. CHECK-NEXT: OpStore [[STORE_0]]
  3358. CHECK-NOT: OpPhi
  3359. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  3360. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
  3361. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  3362. CHECK-NEXT: OpStore [[STORE_1]]
  3363. CHECK-NOT: OpPhi
  3364. CHECK: [[LOAD_2:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  3365. CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_2]]
  3366. CHECK: [[STORE_2:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  3367. CHECK-NEXT: OpStore [[STORE_2]]
  3368. )";
  3369. Match(checks, context.get());
  3370. }
  3371. }
  3372. /*
  3373. Generated from the following GLSL + --eliminate-local-multi-store
  3374. #version 440 core
  3375. void main() {
  3376. int[10] a;
  3377. int[10] b;
  3378. int sum_0 = 0;
  3379. int sum_1 = 0;
  3380. // No loop-carried dependences, legal
  3381. for (int i = 0; i < 10; i++) {
  3382. sum_0 += a[i];
  3383. }
  3384. for (int j = 0; j < 10; j++) {
  3385. sum_1 += b[j];
  3386. }
  3387. int total = sum_0 + sum_1;
  3388. }
  3389. */
  3390. TEST_F(FusionLegalTest, IndependentReductions) {
  3391. std::string text = R"(
  3392. OpCapability Shader
  3393. %1 = OpExtInstImport "GLSL.std.450"
  3394. OpMemoryModel Logical GLSL450
  3395. OpEntryPoint Fragment %4 "main"
  3396. OpExecutionMode %4 OriginUpperLeft
  3397. OpSource GLSL 440
  3398. OpName %4 "main"
  3399. OpName %8 "sum_0"
  3400. OpName %10 "sum_1"
  3401. OpName %11 "i"
  3402. OpName %25 "a"
  3403. OpName %34 "j"
  3404. OpName %42 "b"
  3405. OpName %50 "total"
  3406. %2 = OpTypeVoid
  3407. %3 = OpTypeFunction %2
  3408. %6 = OpTypeInt 32 1
  3409. %7 = OpTypePointer Function %6
  3410. %9 = OpConstant %6 0
  3411. %18 = OpConstant %6 10
  3412. %19 = OpTypeBool
  3413. %21 = OpTypeInt 32 0
  3414. %22 = OpConstant %21 10
  3415. %23 = OpTypeArray %6 %22
  3416. %24 = OpTypePointer Function %23
  3417. %32 = OpConstant %6 1
  3418. %4 = OpFunction %2 None %3
  3419. %5 = OpLabel
  3420. %8 = OpVariable %7 Function
  3421. %10 = OpVariable %7 Function
  3422. %11 = OpVariable %7 Function
  3423. %25 = OpVariable %24 Function
  3424. %34 = OpVariable %7 Function
  3425. %42 = OpVariable %24 Function
  3426. %50 = OpVariable %7 Function
  3427. OpStore %8 %9
  3428. OpStore %10 %9
  3429. OpStore %11 %9
  3430. OpBranch %12
  3431. %12 = OpLabel
  3432. %57 = OpPhi %6 %9 %5 %30 %15
  3433. %54 = OpPhi %6 %9 %5 %33 %15
  3434. OpLoopMerge %14 %15 None
  3435. OpBranch %16
  3436. %16 = OpLabel
  3437. %20 = OpSLessThan %19 %54 %18
  3438. OpBranchConditional %20 %13 %14
  3439. %13 = OpLabel
  3440. %27 = OpAccessChain %7 %25 %54
  3441. %28 = OpLoad %6 %27
  3442. %30 = OpIAdd %6 %57 %28
  3443. OpStore %8 %30
  3444. OpBranch %15
  3445. %15 = OpLabel
  3446. %33 = OpIAdd %6 %54 %32
  3447. OpStore %11 %33
  3448. OpBranch %12
  3449. %14 = OpLabel
  3450. OpStore %34 %9
  3451. OpBranch %35
  3452. %35 = OpLabel
  3453. %58 = OpPhi %6 %9 %14 %47 %38
  3454. %55 = OpPhi %6 %9 %14 %49 %38
  3455. OpLoopMerge %37 %38 None
  3456. OpBranch %39
  3457. %39 = OpLabel
  3458. %41 = OpSLessThan %19 %55 %18
  3459. OpBranchConditional %41 %36 %37
  3460. %36 = OpLabel
  3461. %44 = OpAccessChain %7 %42 %55
  3462. %45 = OpLoad %6 %44
  3463. %47 = OpIAdd %6 %58 %45
  3464. OpStore %10 %47
  3465. OpBranch %38
  3466. %38 = OpLabel
  3467. %49 = OpIAdd %6 %55 %32
  3468. OpStore %34 %49
  3469. OpBranch %35
  3470. %37 = OpLabel
  3471. %53 = OpIAdd %6 %57 %58
  3472. OpStore %50 %53
  3473. OpReturn
  3474. OpFunctionEnd
  3475. )";
  3476. std::unique_ptr<IRContext> context =
  3477. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  3478. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3479. Module* module = context->module();
  3480. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  3481. << text << std::endl;
  3482. Function& f = *module->begin();
  3483. {
  3484. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  3485. EXPECT_EQ(ld.NumLoops(), 2u);
  3486. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  3487. LoopFusion fusion(context.get(), loops[0], loops[1]);
  3488. EXPECT_TRUE(fusion.AreCompatible());
  3489. EXPECT_TRUE(fusion.IsLegal());
  3490. fusion.Fuse();
  3491. }
  3492. {
  3493. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  3494. EXPECT_EQ(ld.NumLoops(), 1u);
  3495. std::string checks = R"(
  3496. CHECK: [[SUM_0:%\w+]] = OpPhi
  3497. CHECK-NEXT: [[SUM_1:%\w+]] = OpPhi
  3498. CHECK-NEXT: [[PHI:%\w+]] = OpPhi
  3499. CHECK-NEXT: OpLoopMerge
  3500. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  3501. CHECK-NEXT: [[LOAD_RES_0:%\w+]] = OpLoad {{%\w+}} [[LOAD_0]]
  3502. CHECK-NEXT: [[ADD_RES_0:%\w+]] = OpIAdd {{%\w+}} [[SUM_0]] [[LOAD_RES_0]]
  3503. CHECK-NEXT: OpStore {{%\w+}} [[ADD_RES_0]]
  3504. CHECK-NOT: OpPhi
  3505. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  3506. CHECK-NEXT: [[LOAD_RES_1:%\w+]] = OpLoad {{%\w+}} [[LOAD_1]]
  3507. CHECK-NEXT: [[ADD_RES_1:%\w+]] = OpIAdd {{%\w+}} [[SUM_1]] [[LOAD_RES_1]]
  3508. CHECK-NEXT: OpStore {{%\w+}} [[ADD_RES_1]]
  3509. )";
  3510. Match(checks, context.get());
  3511. }
  3512. }
  3513. /*
  3514. Generated from the following GLSL + --eliminate-local-multi-store
  3515. #version 440 core
  3516. void main() {
  3517. int[10] a;
  3518. int[10] b;
  3519. int sum_0 = 0;
  3520. int sum_1 = 0;
  3521. // No loop-carried dependences, legal
  3522. for (int i = 0; i < 10; i++) {
  3523. sum_0 += a[i];
  3524. }
  3525. for (int j = 0; j < 10; j++) {
  3526. sum_1 += b[j];
  3527. }
  3528. int total = sum_0 + sum_1;
  3529. }
  3530. */
  3531. TEST_F(FusionLegalTest, IndependentReductionsOneLCSSA) {
  3532. std::string text = R"(
  3533. OpCapability Shader
  3534. %1 = OpExtInstImport "GLSL.std.450"
  3535. OpMemoryModel Logical GLSL450
  3536. OpEntryPoint Fragment %4 "main"
  3537. OpExecutionMode %4 OriginUpperLeft
  3538. OpSource GLSL 440
  3539. OpName %4 "main"
  3540. OpName %8 "sum_0"
  3541. OpName %10 "sum_1"
  3542. OpName %11 "i"
  3543. OpName %25 "a"
  3544. OpName %34 "j"
  3545. OpName %42 "b"
  3546. OpName %50 "total"
  3547. %2 = OpTypeVoid
  3548. %3 = OpTypeFunction %2
  3549. %6 = OpTypeInt 32 1
  3550. %7 = OpTypePointer Function %6
  3551. %9 = OpConstant %6 0
  3552. %18 = OpConstant %6 10
  3553. %19 = OpTypeBool
  3554. %21 = OpTypeInt 32 0
  3555. %22 = OpConstant %21 10
  3556. %23 = OpTypeArray %6 %22
  3557. %24 = OpTypePointer Function %23
  3558. %32 = OpConstant %6 1
  3559. %4 = OpFunction %2 None %3
  3560. %5 = OpLabel
  3561. %8 = OpVariable %7 Function
  3562. %10 = OpVariable %7 Function
  3563. %11 = OpVariable %7 Function
  3564. %25 = OpVariable %24 Function
  3565. %34 = OpVariable %7 Function
  3566. %42 = OpVariable %24 Function
  3567. %50 = OpVariable %7 Function
  3568. OpStore %8 %9
  3569. OpStore %10 %9
  3570. OpStore %11 %9
  3571. OpBranch %12
  3572. %12 = OpLabel
  3573. %57 = OpPhi %6 %9 %5 %30 %15
  3574. %54 = OpPhi %6 %9 %5 %33 %15
  3575. OpLoopMerge %14 %15 None
  3576. OpBranch %16
  3577. %16 = OpLabel
  3578. %20 = OpSLessThan %19 %54 %18
  3579. OpBranchConditional %20 %13 %14
  3580. %13 = OpLabel
  3581. %27 = OpAccessChain %7 %25 %54
  3582. %28 = OpLoad %6 %27
  3583. %30 = OpIAdd %6 %57 %28
  3584. OpStore %8 %30
  3585. OpBranch %15
  3586. %15 = OpLabel
  3587. %33 = OpIAdd %6 %54 %32
  3588. OpStore %11 %33
  3589. OpBranch %12
  3590. %14 = OpLabel
  3591. OpStore %34 %9
  3592. OpBranch %35
  3593. %35 = OpLabel
  3594. %58 = OpPhi %6 %9 %14 %47 %38
  3595. %55 = OpPhi %6 %9 %14 %49 %38
  3596. OpLoopMerge %37 %38 None
  3597. OpBranch %39
  3598. %39 = OpLabel
  3599. %41 = OpSLessThan %19 %55 %18
  3600. OpBranchConditional %41 %36 %37
  3601. %36 = OpLabel
  3602. %44 = OpAccessChain %7 %42 %55
  3603. %45 = OpLoad %6 %44
  3604. %47 = OpIAdd %6 %58 %45
  3605. OpStore %10 %47
  3606. OpBranch %38
  3607. %38 = OpLabel
  3608. %49 = OpIAdd %6 %55 %32
  3609. OpStore %34 %49
  3610. OpBranch %35
  3611. %37 = OpLabel
  3612. %53 = OpIAdd %6 %57 %58
  3613. OpStore %50 %53
  3614. OpReturn
  3615. OpFunctionEnd
  3616. )";
  3617. std::unique_ptr<IRContext> context =
  3618. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  3619. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3620. Module* module = context->module();
  3621. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  3622. << text << std::endl;
  3623. Function& f = *module->begin();
  3624. {
  3625. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  3626. EXPECT_EQ(ld.NumLoops(), 2u);
  3627. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  3628. LoopUtils utils_0(context.get(), loops[0]);
  3629. utils_0.MakeLoopClosedSSA();
  3630. LoopFusion fusion(context.get(), loops[0], loops[1]);
  3631. EXPECT_TRUE(fusion.AreCompatible());
  3632. EXPECT_TRUE(fusion.IsLegal());
  3633. fusion.Fuse();
  3634. }
  3635. {
  3636. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  3637. EXPECT_EQ(ld.NumLoops(), 1u);
  3638. std::string checks = R"(
  3639. CHECK: [[SUM_0:%\w+]] = OpPhi
  3640. CHECK-NEXT: [[SUM_1:%\w+]] = OpPhi
  3641. CHECK-NEXT: [[PHI:%\w+]] = OpPhi
  3642. CHECK-NEXT: OpLoopMerge
  3643. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  3644. CHECK-NEXT: [[LOAD_RES_0:%\w+]] = OpLoad {{%\w+}} [[LOAD_0]]
  3645. CHECK-NEXT: [[ADD_RES_0:%\w+]] = OpIAdd {{%\w+}} [[SUM_0]] [[LOAD_RES_0]]
  3646. CHECK-NEXT: OpStore {{%\w+}} [[ADD_RES_0]]
  3647. CHECK-NOT: OpPhi
  3648. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  3649. CHECK-NEXT: [[LOAD_RES_1:%\w+]] = OpLoad {{%\w+}} [[LOAD_1]]
  3650. CHECK-NEXT: [[ADD_RES_1:%\w+]] = OpIAdd {{%\w+}} [[SUM_1]] [[LOAD_RES_1]]
  3651. CHECK-NEXT: OpStore {{%\w+}} [[ADD_RES_1]]
  3652. )";
  3653. Match(checks, context.get());
  3654. }
  3655. }
  3656. /*
  3657. Generated from the following GLSL + --eliminate-local-multi-store
  3658. #version 440 core
  3659. void main() {
  3660. int[10] a;
  3661. int[10] b;
  3662. int sum_0 = 0;
  3663. int sum_1 = 0;
  3664. // No loop-carried dependences, legal
  3665. for (int i = 0; i < 10; i++) {
  3666. sum_0 += a[i];
  3667. }
  3668. for (int j = 0; j < 10; j++) {
  3669. sum_1 += b[j];
  3670. }
  3671. int total = sum_0 + sum_1;
  3672. }
  3673. */
  3674. TEST_F(FusionLegalTest, IndependentReductionsBothLCSSA) {
  3675. std::string text = R"(
  3676. OpCapability Shader
  3677. %1 = OpExtInstImport "GLSL.std.450"
  3678. OpMemoryModel Logical GLSL450
  3679. OpEntryPoint Fragment %4 "main"
  3680. OpExecutionMode %4 OriginUpperLeft
  3681. OpSource GLSL 440
  3682. OpName %4 "main"
  3683. OpName %8 "sum_0"
  3684. OpName %10 "sum_1"
  3685. OpName %11 "i"
  3686. OpName %25 "a"
  3687. OpName %34 "j"
  3688. OpName %42 "b"
  3689. OpName %50 "total"
  3690. %2 = OpTypeVoid
  3691. %3 = OpTypeFunction %2
  3692. %6 = OpTypeInt 32 1
  3693. %7 = OpTypePointer Function %6
  3694. %9 = OpConstant %6 0
  3695. %18 = OpConstant %6 10
  3696. %19 = OpTypeBool
  3697. %21 = OpTypeInt 32 0
  3698. %22 = OpConstant %21 10
  3699. %23 = OpTypeArray %6 %22
  3700. %24 = OpTypePointer Function %23
  3701. %32 = OpConstant %6 1
  3702. %4 = OpFunction %2 None %3
  3703. %5 = OpLabel
  3704. %8 = OpVariable %7 Function
  3705. %10 = OpVariable %7 Function
  3706. %11 = OpVariable %7 Function
  3707. %25 = OpVariable %24 Function
  3708. %34 = OpVariable %7 Function
  3709. %42 = OpVariable %24 Function
  3710. %50 = OpVariable %7 Function
  3711. OpStore %8 %9
  3712. OpStore %10 %9
  3713. OpStore %11 %9
  3714. OpBranch %12
  3715. %12 = OpLabel
  3716. %57 = OpPhi %6 %9 %5 %30 %15
  3717. %54 = OpPhi %6 %9 %5 %33 %15
  3718. OpLoopMerge %14 %15 None
  3719. OpBranch %16
  3720. %16 = OpLabel
  3721. %20 = OpSLessThan %19 %54 %18
  3722. OpBranchConditional %20 %13 %14
  3723. %13 = OpLabel
  3724. %27 = OpAccessChain %7 %25 %54
  3725. %28 = OpLoad %6 %27
  3726. %30 = OpIAdd %6 %57 %28
  3727. OpStore %8 %30
  3728. OpBranch %15
  3729. %15 = OpLabel
  3730. %33 = OpIAdd %6 %54 %32
  3731. OpStore %11 %33
  3732. OpBranch %12
  3733. %14 = OpLabel
  3734. OpStore %34 %9
  3735. OpBranch %35
  3736. %35 = OpLabel
  3737. %58 = OpPhi %6 %9 %14 %47 %38
  3738. %55 = OpPhi %6 %9 %14 %49 %38
  3739. OpLoopMerge %37 %38 None
  3740. OpBranch %39
  3741. %39 = OpLabel
  3742. %41 = OpSLessThan %19 %55 %18
  3743. OpBranchConditional %41 %36 %37
  3744. %36 = OpLabel
  3745. %44 = OpAccessChain %7 %42 %55
  3746. %45 = OpLoad %6 %44
  3747. %47 = OpIAdd %6 %58 %45
  3748. OpStore %10 %47
  3749. OpBranch %38
  3750. %38 = OpLabel
  3751. %49 = OpIAdd %6 %55 %32
  3752. OpStore %34 %49
  3753. OpBranch %35
  3754. %37 = OpLabel
  3755. %53 = OpIAdd %6 %57 %58
  3756. OpStore %50 %53
  3757. OpReturn
  3758. OpFunctionEnd
  3759. )";
  3760. std::unique_ptr<IRContext> context =
  3761. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  3762. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3763. Module* module = context->module();
  3764. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  3765. << text << std::endl;
  3766. Function& f = *module->begin();
  3767. {
  3768. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  3769. EXPECT_EQ(ld.NumLoops(), 2u);
  3770. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  3771. LoopUtils utils_0(context.get(), loops[0]);
  3772. utils_0.MakeLoopClosedSSA();
  3773. LoopUtils utils_1(context.get(), loops[1]);
  3774. utils_1.MakeLoopClosedSSA();
  3775. LoopFusion fusion(context.get(), loops[0], loops[1]);
  3776. EXPECT_TRUE(fusion.AreCompatible());
  3777. EXPECT_TRUE(fusion.IsLegal());
  3778. fusion.Fuse();
  3779. }
  3780. {
  3781. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  3782. EXPECT_EQ(ld.NumLoops(), 1u);
  3783. std::string checks = R"(
  3784. CHECK: [[SUM_0:%\w+]] = OpPhi
  3785. CHECK-NEXT: [[SUM_1:%\w+]] = OpPhi
  3786. CHECK-NEXT: [[PHI:%\w+]] = OpPhi
  3787. CHECK-NEXT: OpLoopMerge
  3788. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  3789. CHECK-NEXT: [[LOAD_RES_0:%\w+]] = OpLoad {{%\w+}} [[LOAD_0]]
  3790. CHECK-NEXT: [[ADD_RES_0:%\w+]] = OpIAdd {{%\w+}} [[SUM_0]] [[LOAD_RES_0]]
  3791. CHECK-NEXT: OpStore {{%\w+}} [[ADD_RES_0]]
  3792. CHECK-NOT: OpPhi
  3793. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  3794. CHECK-NEXT: [[LOAD_RES_1:%\w+]] = OpLoad {{%\w+}} [[LOAD_1]]
  3795. CHECK-NEXT: [[ADD_RES_1:%\w+]] = OpIAdd {{%\w+}} [[SUM_1]] [[LOAD_RES_1]]
  3796. CHECK-NEXT: OpStore {{%\w+}} [[ADD_RES_1]]
  3797. )";
  3798. Match(checks, context.get());
  3799. }
  3800. }
  3801. /*
  3802. Generated from the following GLSL + --eliminate-local-multi-store
  3803. #version 440 core
  3804. void main() {
  3805. int[10] a;
  3806. int[10] b;
  3807. int sum_0 = 0;
  3808. // No loop-carried dependences, legal
  3809. for (int i = 0; i < 10; i++) {
  3810. sum_0 += a[i];
  3811. }
  3812. for (int j = 0; j < 10; j++) {
  3813. a[j] = b[j];
  3814. }
  3815. }
  3816. */
  3817. TEST_F(FusionLegalTest, LoadStoreReductionAndNonLoopCarriedDependence) {
  3818. std::string text = R"(
  3819. OpCapability Shader
  3820. %1 = OpExtInstImport "GLSL.std.450"
  3821. OpMemoryModel Logical GLSL450
  3822. OpEntryPoint Fragment %4 "main"
  3823. OpExecutionMode %4 OriginUpperLeft
  3824. OpSource GLSL 440
  3825. OpName %4 "main"
  3826. OpName %8 "sum_0"
  3827. OpName %10 "i"
  3828. OpName %24 "a"
  3829. OpName %33 "j"
  3830. OpName %42 "b"
  3831. %2 = OpTypeVoid
  3832. %3 = OpTypeFunction %2
  3833. %6 = OpTypeInt 32 1
  3834. %7 = OpTypePointer Function %6
  3835. %9 = OpConstant %6 0
  3836. %17 = OpConstant %6 10
  3837. %18 = OpTypeBool
  3838. %20 = OpTypeInt 32 0
  3839. %21 = OpConstant %20 10
  3840. %22 = OpTypeArray %6 %21
  3841. %23 = OpTypePointer Function %22
  3842. %31 = OpConstant %6 1
  3843. %4 = OpFunction %2 None %3
  3844. %5 = OpLabel
  3845. %8 = OpVariable %7 Function
  3846. %10 = OpVariable %7 Function
  3847. %24 = OpVariable %23 Function
  3848. %33 = OpVariable %7 Function
  3849. %42 = OpVariable %23 Function
  3850. OpStore %8 %9
  3851. OpStore %10 %9
  3852. OpBranch %11
  3853. %11 = OpLabel
  3854. %51 = OpPhi %6 %9 %5 %29 %14
  3855. %49 = OpPhi %6 %9 %5 %32 %14
  3856. OpLoopMerge %13 %14 None
  3857. OpBranch %15
  3858. %15 = OpLabel
  3859. %19 = OpSLessThan %18 %49 %17
  3860. OpBranchConditional %19 %12 %13
  3861. %12 = OpLabel
  3862. %26 = OpAccessChain %7 %24 %49
  3863. %27 = OpLoad %6 %26
  3864. %29 = OpIAdd %6 %51 %27
  3865. OpStore %8 %29
  3866. OpBranch %14
  3867. %14 = OpLabel
  3868. %32 = OpIAdd %6 %49 %31
  3869. OpStore %10 %32
  3870. OpBranch %11
  3871. %13 = OpLabel
  3872. OpStore %33 %9
  3873. OpBranch %34
  3874. %34 = OpLabel
  3875. %50 = OpPhi %6 %9 %13 %48 %37
  3876. OpLoopMerge %36 %37 None
  3877. OpBranch %38
  3878. %38 = OpLabel
  3879. %40 = OpSLessThan %18 %50 %17
  3880. OpBranchConditional %40 %35 %36
  3881. %35 = OpLabel
  3882. %44 = OpAccessChain %7 %42 %50
  3883. %45 = OpLoad %6 %44
  3884. %46 = OpAccessChain %7 %24 %50
  3885. OpStore %46 %45
  3886. OpBranch %37
  3887. %37 = OpLabel
  3888. %48 = OpIAdd %6 %50 %31
  3889. OpStore %33 %48
  3890. OpBranch %34
  3891. %36 = OpLabel
  3892. OpReturn
  3893. OpFunctionEnd
  3894. )";
  3895. std::unique_ptr<IRContext> context =
  3896. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  3897. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3898. Module* module = context->module();
  3899. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  3900. << text << std::endl;
  3901. Function& f = *module->begin();
  3902. {
  3903. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  3904. EXPECT_EQ(ld.NumLoops(), 2u);
  3905. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  3906. LoopFusion fusion(context.get(), loops[0], loops[1]);
  3907. EXPECT_TRUE(fusion.AreCompatible());
  3908. // TODO: Loop descriptor doesn't return induction variables but all OpPhi
  3909. // in the header and LoopDependenceAnalysis falls over.
  3910. // EXPECT_TRUE(fusion.IsLegal());
  3911. // fusion.Fuse();
  3912. }
  3913. {
  3914. // LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  3915. // EXPECT_EQ(ld.NumLoops(), 1u);
  3916. // std::string checks = R"(
  3917. // CHECK: [[SUM_0:%\w+]] = OpPhi
  3918. // CHECK-NEXT: [[PHI:%\w+]] = OpPhi
  3919. // CHECK-NEXT: OpLoopMerge
  3920. // CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  3921. // CHECK-NEXT: [[LOAD_RES_0:%\w+]] = OpLoad {{%\w+}} [[LOAD_0]]
  3922. // CHECK-NEXT: [[ADD_RES_0:%\w+]] = OpIAdd {{%\w+}} [[SUM_0]] [[LOAD_RES_0]]
  3923. // CHECK-NEXT: OpStore {{%\w+}} [[ADD_RES_0]]
  3924. // CHECK-NOT: OpPhi
  3925. // CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  3926. // CHECK-NEXT: [[LOAD_RES_1:%\w+]] = OpLoad {{%\w+}} [[LOAD_1]]
  3927. // CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  3928. // CHECK-NEXT: OpStore [[STORE_1]] [[LOAD_RES_1]]
  3929. // )";
  3930. // Match(checks, context.get());
  3931. }
  3932. }
  3933. /*
  3934. Generated from the following GLSL + --eliminate-local-multi-store
  3935. #version 440 core
  3936. int x;
  3937. void main() {
  3938. int[10] a;
  3939. int[10] b;
  3940. // Legal.
  3941. for (int i = 0; i < 10; i++) {
  3942. x += a[i];
  3943. }
  3944. for (int j = 0; j < 10; j++) {
  3945. b[j] = b[j]+1;
  3946. }
  3947. }
  3948. */
  3949. TEST_F(FusionLegalTest, ReductionAndNonLoopCarriedDependence) {
  3950. std::string text = R"(
  3951. OpCapability Shader
  3952. %1 = OpExtInstImport "GLSL.std.450"
  3953. OpMemoryModel Logical GLSL450
  3954. OpEntryPoint Fragment %4 "main"
  3955. OpExecutionMode %4 OriginUpperLeft
  3956. OpSource GLSL 440
  3957. OpName %4 "main"
  3958. OpName %8 "i"
  3959. OpName %20 "x"
  3960. OpName %25 "a"
  3961. OpName %34 "j"
  3962. OpName %42 "b"
  3963. %2 = OpTypeVoid
  3964. %3 = OpTypeFunction %2
  3965. %6 = OpTypeInt 32 1
  3966. %7 = OpTypePointer Function %6
  3967. %9 = OpConstant %6 0
  3968. %16 = OpConstant %6 10
  3969. %17 = OpTypeBool
  3970. %19 = OpTypePointer Private %6
  3971. %20 = OpVariable %19 Private
  3972. %21 = OpTypeInt 32 0
  3973. %22 = OpConstant %21 10
  3974. %23 = OpTypeArray %6 %22
  3975. %24 = OpTypePointer Function %23
  3976. %32 = OpConstant %6 1
  3977. %4 = OpFunction %2 None %3
  3978. %5 = OpLabel
  3979. %8 = OpVariable %7 Function
  3980. %25 = OpVariable %24 Function
  3981. %34 = OpVariable %7 Function
  3982. %42 = OpVariable %24 Function
  3983. OpStore %8 %9
  3984. OpBranch %10
  3985. %10 = OpLabel
  3986. %51 = OpPhi %6 %9 %5 %33 %13
  3987. OpLoopMerge %12 %13 None
  3988. OpBranch %14
  3989. %14 = OpLabel
  3990. %18 = OpSLessThan %17 %51 %16
  3991. OpBranchConditional %18 %11 %12
  3992. %11 = OpLabel
  3993. %27 = OpAccessChain %7 %25 %51
  3994. %28 = OpLoad %6 %27
  3995. %29 = OpLoad %6 %20
  3996. %30 = OpIAdd %6 %29 %28
  3997. OpStore %20 %30
  3998. OpBranch %13
  3999. %13 = OpLabel
  4000. %33 = OpIAdd %6 %51 %32
  4001. OpStore %8 %33
  4002. OpBranch %10
  4003. %12 = OpLabel
  4004. OpStore %34 %9
  4005. OpBranch %35
  4006. %35 = OpLabel
  4007. %52 = OpPhi %6 %9 %12 %50 %38
  4008. OpLoopMerge %37 %38 None
  4009. OpBranch %39
  4010. %39 = OpLabel
  4011. %41 = OpSLessThan %17 %52 %16
  4012. OpBranchConditional %41 %36 %37
  4013. %36 = OpLabel
  4014. %45 = OpAccessChain %7 %42 %52
  4015. %46 = OpLoad %6 %45
  4016. %47 = OpIAdd %6 %46 %32
  4017. %48 = OpAccessChain %7 %42 %52
  4018. OpStore %48 %47
  4019. OpBranch %38
  4020. %38 = OpLabel
  4021. %50 = OpIAdd %6 %52 %32
  4022. OpStore %34 %50
  4023. OpBranch %35
  4024. %37 = OpLabel
  4025. OpReturn
  4026. OpFunctionEnd
  4027. )";
  4028. std::unique_ptr<IRContext> context =
  4029. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  4030. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  4031. Module* module = context->module();
  4032. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  4033. << text << std::endl;
  4034. Function& f = *module->begin();
  4035. {
  4036. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  4037. EXPECT_EQ(ld.NumLoops(), 2u);
  4038. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  4039. LoopFusion fusion(context.get(), loops[0], loops[1]);
  4040. EXPECT_TRUE(fusion.AreCompatible());
  4041. EXPECT_TRUE(fusion.IsLegal());
  4042. fusion.Fuse();
  4043. }
  4044. {
  4045. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  4046. EXPECT_EQ(ld.NumLoops(), 1u);
  4047. std::string checks = R"(
  4048. CHECK: OpName [[X:%\w+]] "x"
  4049. CHECK: [[PHI:%\w+]] = OpPhi
  4050. CHECK-NEXT: OpLoopMerge
  4051. CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  4052. CHECK-NEXT: [[LOAD_RES_0:%\w+]] = OpLoad {{%\w+}} [[LOAD_0]]
  4053. CHECK-NEXT: [[X_LOAD:%\w+]] = OpLoad {{%\w+}} [[X]]
  4054. CHECK-NEXT: [[ADD_RES_0:%\w+]] = OpIAdd {{%\w+}} [[X_LOAD]] [[LOAD_RES_0]]
  4055. CHECK-NEXT: OpStore [[X]] [[ADD_RES_0]]
  4056. CHECK-NOT: OpPhi
  4057. CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  4058. CHECK-NEXT: {{%\w+}} = OpLoad {{%\w+}} [[LOAD_1]]
  4059. CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
  4060. CHECK-NEXT: OpStore [[STORE_1]]
  4061. )";
  4062. Match(checks, context.get());
  4063. }
  4064. }
  4065. /*
  4066. Generated from the following GLSL + --eliminate-local-multi-store
  4067. #version 440 core
  4068. struct TestStruct {
  4069. int[10] a;
  4070. int b;
  4071. };
  4072. void main() {
  4073. TestStruct test_0;
  4074. TestStruct test_1;
  4075. TestStruct test_2;
  4076. test_1.b = 2;
  4077. for (int i = 0; i < 10; i++) {
  4078. test_0.a[i] = i;
  4079. }
  4080. for (int j = 0; j < 10; j++) {
  4081. test_2 = test_1;
  4082. }
  4083. }
  4084. */
  4085. TEST_F(FusionLegalTest, ArrayInStruct) {
  4086. std::string text = R"(
  4087. OpCapability Shader
  4088. %1 = OpExtInstImport "GLSL.std.450"
  4089. OpMemoryModel Logical GLSL450
  4090. OpEntryPoint Fragment %4 "main"
  4091. OpExecutionMode %4 OriginUpperLeft
  4092. OpSource GLSL 440
  4093. OpName %4 "main"
  4094. OpName %10 "TestStruct"
  4095. OpMemberName %10 0 "a"
  4096. OpMemberName %10 1 "b"
  4097. OpName %12 "test_1"
  4098. OpName %17 "i"
  4099. OpName %28 "test_0"
  4100. OpName %34 "j"
  4101. OpName %42 "test_2"
  4102. %2 = OpTypeVoid
  4103. %3 = OpTypeFunction %2
  4104. %6 = OpTypeInt 32 1
  4105. %7 = OpTypeInt 32 0
  4106. %8 = OpConstant %7 10
  4107. %9 = OpTypeArray %6 %8
  4108. %10 = OpTypeStruct %9 %6
  4109. %11 = OpTypePointer Function %10
  4110. %13 = OpConstant %6 1
  4111. %14 = OpConstant %6 2
  4112. %15 = OpTypePointer Function %6
  4113. %18 = OpConstant %6 0
  4114. %25 = OpConstant %6 10
  4115. %26 = OpTypeBool
  4116. %4 = OpFunction %2 None %3
  4117. %5 = OpLabel
  4118. %12 = OpVariable %11 Function
  4119. %17 = OpVariable %15 Function
  4120. %28 = OpVariable %11 Function
  4121. %34 = OpVariable %15 Function
  4122. %42 = OpVariable %11 Function
  4123. %16 = OpAccessChain %15 %12 %13
  4124. OpStore %16 %14
  4125. OpStore %17 %18
  4126. OpBranch %19
  4127. %19 = OpLabel
  4128. %46 = OpPhi %6 %18 %5 %33 %22
  4129. OpLoopMerge %21 %22 None
  4130. OpBranch %23
  4131. %23 = OpLabel
  4132. %27 = OpSLessThan %26 %46 %25
  4133. OpBranchConditional %27 %20 %21
  4134. %20 = OpLabel
  4135. %31 = OpAccessChain %15 %28 %18 %46
  4136. OpStore %31 %46
  4137. OpBranch %22
  4138. %22 = OpLabel
  4139. %33 = OpIAdd %6 %46 %13
  4140. OpStore %17 %33
  4141. OpBranch %19
  4142. %21 = OpLabel
  4143. OpStore %34 %18
  4144. OpBranch %35
  4145. %35 = OpLabel
  4146. %47 = OpPhi %6 %18 %21 %45 %38
  4147. OpLoopMerge %37 %38 None
  4148. OpBranch %39
  4149. %39 = OpLabel
  4150. %41 = OpSLessThan %26 %47 %25
  4151. OpBranchConditional %41 %36 %37
  4152. %36 = OpLabel
  4153. %43 = OpLoad %10 %12
  4154. OpStore %42 %43
  4155. OpBranch %38
  4156. %38 = OpLabel
  4157. %45 = OpIAdd %6 %47 %13
  4158. OpStore %34 %45
  4159. OpBranch %35
  4160. %37 = OpLabel
  4161. OpReturn
  4162. OpFunctionEnd
  4163. )";
  4164. std::unique_ptr<IRContext> context =
  4165. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  4166. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  4167. Module* module = context->module();
  4168. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  4169. << text << std::endl;
  4170. Function& f = *module->begin();
  4171. {
  4172. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  4173. EXPECT_EQ(ld.NumLoops(), 2u);
  4174. auto loops = ld.GetLoopsInBinaryLayoutOrder();
  4175. LoopFusion fusion(context.get(), loops[0], loops[1]);
  4176. EXPECT_TRUE(fusion.AreCompatible());
  4177. EXPECT_TRUE(fusion.IsLegal());
  4178. fusion.Fuse();
  4179. }
  4180. {
  4181. LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
  4182. EXPECT_EQ(ld.NumLoops(), 1u);
  4183. // clang-format off
  4184. std::string checks = R"(
  4185. CHECK: OpName [[TEST_1:%\w+]] "test_1"
  4186. CHECK: OpName [[TEST_0:%\w+]] "test_0"
  4187. CHECK: OpName [[TEST_2:%\w+]] "test_2"
  4188. CHECK: [[PHI:%\w+]] = OpPhi
  4189. CHECK-NEXT: OpLoopMerge
  4190. CHECK: [[TEST_0_STORE:%\w+]] = OpAccessChain {{%\w+}} [[TEST_0]] {{%\w+}} {{%\w+}}
  4191. CHECK-NEXT: OpStore [[TEST_0_STORE]] [[PHI]]
  4192. CHECK-NOT: OpPhi
  4193. CHECK: [[TEST_1_LOAD:%\w+]] = OpLoad {{%\w+}} [[TEST_1]]
  4194. CHECK: OpStore [[TEST_2]] [[TEST_1_LOAD]]
  4195. )";
  4196. // clang-format on
  4197. Match(checks, context.get());
  4198. }
  4199. }
  4200. } // namespace
  4201. } // namespace opt
  4202. } // namespace spvtools