fusion_legal.cpp 134 KB

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