| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577 |
- // Copyright (c) 2018 Google LLC.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- #include <memory>
- #include <vector>
- #include "effcee/effcee.h"
- #include "gmock/gmock.h"
- #include "source/opt/loop_descriptor.h"
- #include "source/opt/loop_fusion.h"
- #include "test/opt/pass_fixture.h"
- namespace spvtools {
- namespace opt {
- namespace {
- using FusionLegalTest = PassTest<::testing::Test>;
- bool Validate(const std::vector<uint32_t>& bin) {
- spv_target_env target_env = SPV_ENV_UNIVERSAL_1_2;
- spv_context spvContext = spvContextCreate(target_env);
- spv_diagnostic diagnostic = nullptr;
- spv_const_binary_t binary = {bin.data(), bin.size()};
- spv_result_t error = spvValidate(spvContext, &binary, &diagnostic);
- if (error != 0) spvDiagnosticPrint(diagnostic);
- spvDiagnosticDestroy(diagnostic);
- spvContextDestroy(spvContext);
- return error == 0;
- }
- void Match(const std::string& checks, IRContext* context) {
- // Silence unused warnings with !defined(SPIRV_EFFCE)
- (void)checks;
- std::vector<uint32_t> bin;
- context->module()->ToBinary(&bin, true);
- EXPECT_TRUE(Validate(bin));
- std::string assembly;
- SpirvTools tools(SPV_ENV_UNIVERSAL_1_2);
- EXPECT_TRUE(
- tools.Disassemble(bin, &assembly, SPV_BINARY_TO_TEXT_OPTION_NO_HEADER))
- << "Disassembling failed for shader:\n"
- << assembly << std::endl;
- auto match_result = effcee::Match(assembly, checks);
- EXPECT_EQ(effcee::Result::Status::Ok, match_result.status())
- << match_result.message() << "\nChecking result:\n"
- << assembly;
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10] a;
- int[10] b;
- // No dependence, legal
- for (int i = 0; i < 10; i++) {
- a[i] = a[i]*2;
- }
- for (int i = 0; i < 10; i++) {
- b[i] = b[i]+2;
- }
- }
- */
- TEST_F(FusionLegalTest, DifferentArraysInLoops) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %23 "a"
- OpName %34 "i"
- OpName %42 "b"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 10
- %17 = OpTypeBool
- %19 = OpTypeInt 32 0
- %20 = OpConstant %19 10
- %21 = OpTypeArray %6 %20
- %22 = OpTypePointer Function %21
- %28 = OpConstant %6 2
- %32 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %23 = OpVariable %22 Function
- %34 = OpVariable %7 Function
- %42 = OpVariable %22 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- %51 = OpPhi %6 %9 %5 %33 %13
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %51 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %26 = OpAccessChain %7 %23 %51
- %27 = OpLoad %6 %26
- %29 = OpIMul %6 %27 %28
- %30 = OpAccessChain %7 %23 %51
- OpStore %30 %29
- OpBranch %13
- %13 = OpLabel
- %33 = OpIAdd %6 %51 %32
- OpStore %8 %33
- OpBranch %10
- %12 = OpLabel
- OpStore %34 %9
- OpBranch %35
- %35 = OpLabel
- %52 = OpPhi %6 %9 %12 %50 %38
- OpLoopMerge %37 %38 None
- OpBranch %39
- %39 = OpLabel
- %41 = OpSLessThan %17 %52 %16
- OpBranchConditional %41 %36 %37
- %36 = OpLabel
- %45 = OpAccessChain %7 %42 %52
- %46 = OpLoad %6 %45
- %47 = OpIAdd %6 %46 %28
- %48 = OpAccessChain %7 %42 %52
- OpStore %48 %47
- OpBranch %38
- %38 = OpLabel
- %50 = OpIAdd %6 %52 %32
- OpStore %34 %50
- OpBranch %35
- %37 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- std::string checks = R"(
- CHECK: [[PHI:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- auto& ld_final = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld_final.NumLoops(), 1u);
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10] a;
- int[10] b;
- int[10] c;
- // Only loads to the same array, legal
- for (int i = 0; i < 10; i++) {
- b[i] = a[i]*2;
- }
- for (int i = 0; i < 10; i++) {
- c[i] = a[i]+2;
- }
- }
- */
- TEST_F(FusionLegalTest, OnlyLoadsToSameArray) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %23 "b"
- OpName %25 "a"
- OpName %35 "i"
- OpName %43 "c"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 10
- %17 = OpTypeBool
- %19 = OpTypeInt 32 0
- %20 = OpConstant %19 10
- %21 = OpTypeArray %6 %20
- %22 = OpTypePointer Function %21
- %29 = OpConstant %6 2
- %33 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %23 = OpVariable %22 Function
- %25 = OpVariable %22 Function
- %35 = OpVariable %7 Function
- %43 = OpVariable %22 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- %52 = OpPhi %6 %9 %5 %34 %13
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %52 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %27 = OpAccessChain %7 %25 %52
- %28 = OpLoad %6 %27
- %30 = OpIMul %6 %28 %29
- %31 = OpAccessChain %7 %23 %52
- OpStore %31 %30
- OpBranch %13
- %13 = OpLabel
- %34 = OpIAdd %6 %52 %33
- OpStore %8 %34
- OpBranch %10
- %12 = OpLabel
- OpStore %35 %9
- OpBranch %36
- %36 = OpLabel
- %53 = OpPhi %6 %9 %12 %51 %39
- OpLoopMerge %38 %39 None
- OpBranch %40
- %40 = OpLabel
- %42 = OpSLessThan %17 %53 %16
- OpBranchConditional %42 %37 %38
- %37 = OpLabel
- %46 = OpAccessChain %7 %25 %53
- %47 = OpLoad %6 %46
- %48 = OpIAdd %6 %47 %29
- %49 = OpAccessChain %7 %43 %53
- OpStore %49 %48
- OpBranch %39
- %39 = OpLabel
- %51 = OpIAdd %6 %53 %33
- OpStore %35 %51
- OpBranch %36
- %38 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- std::string checks = R"(
- CHECK: [[PHI:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- auto& ld_final = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld_final.NumLoops(), 1u);
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10] a;
- int[10] b;
- // No loop-carried dependences, legal
- for (int i = 0; i < 10; i++) {
- a[i] = a[i]*2;
- }
- for (int i = 0; i < 10; i++) {
- b[i] = a[i]+2;
- }
- }
- */
- TEST_F(FusionLegalTest, NoLoopCarriedDependences) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %23 "a"
- OpName %34 "i"
- OpName %42 "b"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 10
- %17 = OpTypeBool
- %19 = OpTypeInt 32 0
- %20 = OpConstant %19 10
- %21 = OpTypeArray %6 %20
- %22 = OpTypePointer Function %21
- %28 = OpConstant %6 2
- %32 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %23 = OpVariable %22 Function
- %34 = OpVariable %7 Function
- %42 = OpVariable %22 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- %51 = OpPhi %6 %9 %5 %33 %13
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %51 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %26 = OpAccessChain %7 %23 %51
- %27 = OpLoad %6 %26
- %29 = OpIMul %6 %27 %28
- %30 = OpAccessChain %7 %23 %51
- OpStore %30 %29
- OpBranch %13
- %13 = OpLabel
- %33 = OpIAdd %6 %51 %32
- OpStore %8 %33
- OpBranch %10
- %12 = OpLabel
- OpStore %34 %9
- OpBranch %35
- %35 = OpLabel
- %52 = OpPhi %6 %9 %12 %50 %38
- OpLoopMerge %37 %38 None
- OpBranch %39
- %39 = OpLabel
- %41 = OpSLessThan %17 %52 %16
- OpBranchConditional %41 %36 %37
- %36 = OpLabel
- %45 = OpAccessChain %7 %23 %52
- %46 = OpLoad %6 %45
- %47 = OpIAdd %6 %46 %28
- %48 = OpAccessChain %7 %42 %52
- OpStore %48 %47
- OpBranch %38
- %38 = OpLabel
- %50 = OpIAdd %6 %52 %32
- OpStore %34 %50
- OpBranch %35
- %37 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- std::string checks = R"(
- CHECK: [[PHI:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- auto& ld_final = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld_final.NumLoops(), 1u);
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10] a;
- int[10] b;
- int[10] c;
- // Parallelism inhibiting, but legal.
- for (int i = 0; i < 10; i++) {
- a[i] = b[i] + 1;
- }
- for (int i = 0; i < 10; i++) {
- c[i] = a[i] + c[i-1];
- }
- }
- */
- TEST_F(FusionLegalTest, ExistingLoopCarriedDependence) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %23 "a"
- OpName %25 "b"
- OpName %34 "i"
- OpName %42 "c"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 10
- %17 = OpTypeBool
- %19 = OpTypeInt 32 0
- %20 = OpConstant %19 10
- %21 = OpTypeArray %6 %20
- %22 = OpTypePointer Function %21
- %29 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %23 = OpVariable %22 Function
- %25 = OpVariable %22 Function
- %34 = OpVariable %7 Function
- %42 = OpVariable %22 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- %55 = OpPhi %6 %9 %5 %33 %13
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %55 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %27 = OpAccessChain %7 %25 %55
- %28 = OpLoad %6 %27
- %30 = OpIAdd %6 %28 %29
- %31 = OpAccessChain %7 %23 %55
- OpStore %31 %30
- OpBranch %13
- %13 = OpLabel
- %33 = OpIAdd %6 %55 %29
- OpStore %8 %33
- OpBranch %10
- %12 = OpLabel
- OpStore %34 %9
- OpBranch %35
- %35 = OpLabel
- %56 = OpPhi %6 %9 %12 %54 %38
- OpLoopMerge %37 %38 None
- OpBranch %39
- %39 = OpLabel
- %41 = OpSLessThan %17 %56 %16
- OpBranchConditional %41 %36 %37
- %36 = OpLabel
- %45 = OpAccessChain %7 %23 %56
- %46 = OpLoad %6 %45
- %48 = OpISub %6 %56 %29
- %49 = OpAccessChain %7 %42 %48
- %50 = OpLoad %6 %49
- %51 = OpIAdd %6 %46 %50
- %52 = OpAccessChain %7 %42 %56
- OpStore %52 %51
- OpBranch %38
- %38 = OpLabel
- %54 = OpIAdd %6 %56 %29
- OpStore %34 %54
- OpBranch %35
- %37 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- std::string checks = R"(
- CHECK: [[PHI:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[I_1:%\w+]] = OpISub {{%\w+}} [[PHI]] {{%\w+}}
- CHECK-NEXT: [[LOAD_2:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_2]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- auto& ld_final = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld_final.NumLoops(), 1u);
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10] a;
- int[10] b;
- int[10] c;
- // Creates a loop-carried dependence, but negative, so legal
- for (int i = 0; i < 10; i++) {
- a[i+1] = b[i] + 1;
- }
- for (int i = 0; i < 10; i++) {
- c[i] = a[i] + 2;
- }
- }
- */
- TEST_F(FusionLegalTest, NegativeDistanceCreatedRAW) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %23 "a"
- OpName %27 "b"
- OpName %35 "i"
- OpName %43 "c"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 10
- %17 = OpTypeBool
- %19 = OpTypeInt 32 0
- %20 = OpConstant %19 10
- %21 = OpTypeArray %6 %20
- %22 = OpTypePointer Function %21
- %25 = OpConstant %6 1
- %48 = OpConstant %6 2
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %23 = OpVariable %22 Function
- %27 = OpVariable %22 Function
- %35 = OpVariable %7 Function
- %43 = OpVariable %22 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- %53 = OpPhi %6 %9 %5 %34 %13
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %53 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %26 = OpIAdd %6 %53 %25
- %29 = OpAccessChain %7 %27 %53
- %30 = OpLoad %6 %29
- %31 = OpIAdd %6 %30 %25
- %32 = OpAccessChain %7 %23 %26
- OpStore %32 %31
- OpBranch %13
- %13 = OpLabel
- %34 = OpIAdd %6 %53 %25
- OpStore %8 %34
- OpBranch %10
- %12 = OpLabel
- OpStore %35 %9
- OpBranch %36
- %36 = OpLabel
- %54 = OpPhi %6 %9 %12 %52 %39
- OpLoopMerge %38 %39 None
- OpBranch %40
- %40 = OpLabel
- %42 = OpSLessThan %17 %54 %16
- OpBranchConditional %42 %37 %38
- %37 = OpLabel
- %46 = OpAccessChain %7 %23 %54
- %47 = OpLoad %6 %46
- %49 = OpIAdd %6 %47 %48
- %50 = OpAccessChain %7 %43 %54
- OpStore %50 %49
- OpBranch %39
- %39 = OpLabel
- %52 = OpIAdd %6 %54 %25
- OpStore %35 %52
- OpBranch %36
- %38 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- std::string checks = R"(
- CHECK: [[PHI:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[I_1:%\w+]] = OpIAdd {{%\w+}} [[PHI]] {{%\w+}}
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_1]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- }
- {
- auto& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 1u);
- }
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10] a;
- int[10] b;
- int[10] c;
- // Legal
- for (int i = 0; i < 10; i++) {
- a[i+1] = b[i] + 1;
- }
- for (int i = 0; i < 10; i++) {
- c[i] = a[i+1] + 2;
- }
- }
- */
- TEST_F(FusionLegalTest, NoLoopCarriedDependencesAdjustedIndex) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %23 "a"
- OpName %27 "b"
- OpName %35 "i"
- OpName %43 "c"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 10
- %17 = OpTypeBool
- %19 = OpTypeInt 32 0
- %20 = OpConstant %19 10
- %21 = OpTypeArray %6 %20
- %22 = OpTypePointer Function %21
- %25 = OpConstant %6 1
- %49 = OpConstant %6 2
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %23 = OpVariable %22 Function
- %27 = OpVariable %22 Function
- %35 = OpVariable %7 Function
- %43 = OpVariable %22 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- %54 = OpPhi %6 %9 %5 %34 %13
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %54 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %26 = OpIAdd %6 %54 %25
- %29 = OpAccessChain %7 %27 %54
- %30 = OpLoad %6 %29
- %31 = OpIAdd %6 %30 %25
- %32 = OpAccessChain %7 %23 %26
- OpStore %32 %31
- OpBranch %13
- %13 = OpLabel
- %34 = OpIAdd %6 %54 %25
- OpStore %8 %34
- OpBranch %10
- %12 = OpLabel
- OpStore %35 %9
- OpBranch %36
- %36 = OpLabel
- %55 = OpPhi %6 %9 %12 %53 %39
- OpLoopMerge %38 %39 None
- OpBranch %40
- %40 = OpLabel
- %42 = OpSLessThan %17 %55 %16
- OpBranchConditional %42 %37 %38
- %37 = OpLabel
- %46 = OpIAdd %6 %55 %25
- %47 = OpAccessChain %7 %23 %46
- %48 = OpLoad %6 %47
- %50 = OpIAdd %6 %48 %49
- %51 = OpAccessChain %7 %43 %55
- OpStore %51 %50
- OpBranch %39
- %39 = OpLabel
- %53 = OpIAdd %6 %55 %25
- OpStore %35 %53
- OpBranch %36
- %38 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- std::string checks = R"(
- CHECK: [[PHI:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[I_1:%\w+]] = OpIAdd {{%\w+}} [[PHI]] {{%\w+}}
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_1]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK-NOT: OpPhi
- CHECK: [[I_1:%\w+]] = OpIAdd {{%\w+}} [[PHI]] {{%\w+}}
- CHECK-NEXT: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- auto& ld_final = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld_final.NumLoops(), 1u);
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10] a;
- int[10] b;
- int[10] c;
- // Legal, independent locations in |a|, SIV
- for (int i = 0; i < 10; i++) {
- a[2*i+1] = b[i] + 1;
- }
- for (int i = 0; i < 10; i++) {
- c[i] = a[2*i] + 2;
- }
- }
- */
- TEST_F(FusionLegalTest, IndependentSIV) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %23 "a"
- OpName %29 "b"
- OpName %37 "i"
- OpName %45 "c"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 10
- %17 = OpTypeBool
- %19 = OpTypeInt 32 0
- %20 = OpConstant %19 10
- %21 = OpTypeArray %6 %20
- %22 = OpTypePointer Function %21
- %24 = OpConstant %6 2
- %27 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %23 = OpVariable %22 Function
- %29 = OpVariable %22 Function
- %37 = OpVariable %7 Function
- %45 = OpVariable %22 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- %55 = OpPhi %6 %9 %5 %36 %13
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %55 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %26 = OpIMul %6 %24 %55
- %28 = OpIAdd %6 %26 %27
- %31 = OpAccessChain %7 %29 %55
- %32 = OpLoad %6 %31
- %33 = OpIAdd %6 %32 %27
- %34 = OpAccessChain %7 %23 %28
- OpStore %34 %33
- OpBranch %13
- %13 = OpLabel
- %36 = OpIAdd %6 %55 %27
- OpStore %8 %36
- OpBranch %10
- %12 = OpLabel
- OpStore %37 %9
- OpBranch %38
- %38 = OpLabel
- %56 = OpPhi %6 %9 %12 %54 %41
- OpLoopMerge %40 %41 None
- OpBranch %42
- %42 = OpLabel
- %44 = OpSLessThan %17 %56 %16
- OpBranchConditional %44 %39 %40
- %39 = OpLabel
- %48 = OpIMul %6 %24 %56
- %49 = OpAccessChain %7 %23 %48
- %50 = OpLoad %6 %49
- %51 = OpIAdd %6 %50 %24
- %52 = OpAccessChain %7 %45 %56
- OpStore %52 %51
- OpBranch %41
- %41 = OpLabel
- %54 = OpIAdd %6 %56 %27
- OpStore %37 %54
- OpBranch %38
- %40 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- std::string checks = R"(
- CHECK: [[PHI:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[I_2:%\w+]] = OpIMul {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: [[I_2_1:%\w+]] = OpIAdd {{%\w+}} [[I_2]] {{%\w+}}
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_2_1]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK-NOT: OpPhi
- CHECK: [[I_2:%\w+]] = OpIMul {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_2]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- auto& ld_final = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld_final.NumLoops(), 1u);
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10] a;
- int[10] b;
- int[10] c;
- // Legal, independent locations in |a|, ZIV
- for (int i = 0; i < 10; i++) {
- a[1] = b[i] + 1;
- }
- for (int i = 0; i < 10; i++) {
- c[i] = a[9] + 2;
- }
- }
- */
- TEST_F(FusionLegalTest, IndependentZIV) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %23 "a"
- OpName %25 "b"
- OpName %33 "i"
- OpName %41 "c"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 10
- %17 = OpTypeBool
- %19 = OpTypeInt 32 0
- %20 = OpConstant %19 10
- %21 = OpTypeArray %6 %20
- %22 = OpTypePointer Function %21
- %24 = OpConstant %6 1
- %43 = OpConstant %6 9
- %46 = OpConstant %6 2
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %23 = OpVariable %22 Function
- %25 = OpVariable %22 Function
- %33 = OpVariable %7 Function
- %41 = OpVariable %22 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- %51 = OpPhi %6 %9 %5 %32 %13
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %51 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %27 = OpAccessChain %7 %25 %51
- %28 = OpLoad %6 %27
- %29 = OpIAdd %6 %28 %24
- %30 = OpAccessChain %7 %23 %24
- OpStore %30 %29
- OpBranch %13
- %13 = OpLabel
- %32 = OpIAdd %6 %51 %24
- OpStore %8 %32
- OpBranch %10
- %12 = OpLabel
- OpStore %33 %9
- OpBranch %34
- %34 = OpLabel
- %52 = OpPhi %6 %9 %12 %50 %37
- OpLoopMerge %36 %37 None
- OpBranch %38
- %38 = OpLabel
- %40 = OpSLessThan %17 %52 %16
- OpBranchConditional %40 %35 %36
- %35 = OpLabel
- %44 = OpAccessChain %7 %23 %43
- %45 = OpLoad %6 %44
- %47 = OpIAdd %6 %45 %46
- %48 = OpAccessChain %7 %41 %52
- OpStore %48 %47
- OpBranch %37
- %37 = OpLabel
- %50 = OpIAdd %6 %52 %24
- OpStore %33 %50
- OpBranch %34
- %36 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- std::string checks = R"(
- CHECK: [[PHI:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK-NOT: OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK: OpStore
- CHECK-NOT: OpPhi
- CHECK-NOT: OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK: OpLoad
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- auto& ld_final = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld_final.NumLoops(), 1u);
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[20] a;
- int[10] b;
- int[10] c;
- // Legal, non-overlapping sections in |a|
- for (int i = 0; i < 10; i++) {
- a[i] = b[i] + 1;
- }
- for (int i = 0; i < 10; i++) {
- c[i] = a[i+10] + 2;
- }
- }
- */
- TEST_F(FusionLegalTest, NonOverlappingAccesses) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %23 "a"
- OpName %28 "b"
- OpName %37 "i"
- OpName %45 "c"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 10
- %17 = OpTypeBool
- %19 = OpTypeInt 32 0
- %20 = OpConstant %19 20
- %21 = OpTypeArray %6 %20
- %22 = OpTypePointer Function %21
- %25 = OpConstant %19 10
- %26 = OpTypeArray %6 %25
- %27 = OpTypePointer Function %26
- %32 = OpConstant %6 1
- %51 = OpConstant %6 2
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %23 = OpVariable %22 Function
- %28 = OpVariable %27 Function
- %37 = OpVariable %7 Function
- %45 = OpVariable %27 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- %56 = OpPhi %6 %9 %5 %36 %13
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %56 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %30 = OpAccessChain %7 %28 %56
- %31 = OpLoad %6 %30
- %33 = OpIAdd %6 %31 %32
- %34 = OpAccessChain %7 %23 %56
- OpStore %34 %33
- OpBranch %13
- %13 = OpLabel
- %36 = OpIAdd %6 %56 %32
- OpStore %8 %36
- OpBranch %10
- %12 = OpLabel
- OpStore %37 %9
- OpBranch %38
- %38 = OpLabel
- %57 = OpPhi %6 %9 %12 %55 %41
- OpLoopMerge %40 %41 None
- OpBranch %42
- %42 = OpLabel
- %44 = OpSLessThan %17 %57 %16
- OpBranchConditional %44 %39 %40
- %39 = OpLabel
- %48 = OpIAdd %6 %57 %16
- %49 = OpAccessChain %7 %23 %48
- %50 = OpLoad %6 %49
- %52 = OpIAdd %6 %50 %51
- %53 = OpAccessChain %7 %45 %57
- OpStore %53 %52
- OpBranch %41
- %41 = OpLabel
- %55 = OpIAdd %6 %57 %32
- OpStore %37 %55
- OpBranch %38
- %40 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- std::string checks = R"(
- CHECK: [[PHI:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NOT: OpPhi
- CHECK: [[I_10:%\w+]] = OpIAdd {{%\w+}} [[PHI]] {{%\w+}}
- CHECK-NEXT: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_10]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- auto& ld_final = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld_final.NumLoops(), 1u);
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10] a;
- int[10] b;
- int[10] c;
- // Legal, 3 adjacent loops
- for (int i = 0; i < 10; i++) {
- a[i] = b[i] + 1;
- }
- for (int i = 0; i < 10; i++) {
- c[i] = a[i] + 2;
- }
- for (int i = 0; i < 10; i++) {
- b[i] = c[i] + 10;
- }
- }
- */
- TEST_F(FusionLegalTest, AdjacentLoops) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %23 "a"
- OpName %25 "b"
- OpName %34 "i"
- OpName %42 "c"
- OpName %52 "i"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 10
- %17 = OpTypeBool
- %19 = OpTypeInt 32 0
- %20 = OpConstant %19 10
- %21 = OpTypeArray %6 %20
- %22 = OpTypePointer Function %21
- %29 = OpConstant %6 1
- %47 = OpConstant %6 2
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %23 = OpVariable %22 Function
- %25 = OpVariable %22 Function
- %34 = OpVariable %7 Function
- %42 = OpVariable %22 Function
- %52 = OpVariable %7 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- %68 = OpPhi %6 %9 %5 %33 %13
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %68 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %27 = OpAccessChain %7 %25 %68
- %28 = OpLoad %6 %27
- %30 = OpIAdd %6 %28 %29
- %31 = OpAccessChain %7 %23 %68
- OpStore %31 %30
- OpBranch %13
- %13 = OpLabel
- %33 = OpIAdd %6 %68 %29
- OpStore %8 %33
- OpBranch %10
- %12 = OpLabel
- OpStore %34 %9
- OpBranch %35
- %35 = OpLabel
- %69 = OpPhi %6 %9 %12 %51 %38
- OpLoopMerge %37 %38 None
- OpBranch %39
- %39 = OpLabel
- %41 = OpSLessThan %17 %69 %16
- OpBranchConditional %41 %36 %37
- %36 = OpLabel
- %45 = OpAccessChain %7 %23 %69
- %46 = OpLoad %6 %45
- %48 = OpIAdd %6 %46 %47
- %49 = OpAccessChain %7 %42 %69
- OpStore %49 %48
- OpBranch %38
- %38 = OpLabel
- %51 = OpIAdd %6 %69 %29
- OpStore %34 %51
- OpBranch %35
- %37 = OpLabel
- OpStore %52 %9
- OpBranch %53
- %53 = OpLabel
- %70 = OpPhi %6 %9 %37 %67 %56
- OpLoopMerge %55 %56 None
- OpBranch %57
- %57 = OpLabel
- %59 = OpSLessThan %17 %70 %16
- OpBranchConditional %59 %54 %55
- %54 = OpLabel
- %62 = OpAccessChain %7 %42 %70
- %63 = OpLoad %6 %62
- %64 = OpIAdd %6 %63 %16
- %65 = OpAccessChain %7 %25 %70
- OpStore %65 %64
- OpBranch %56
- %56 = OpLabel
- %67 = OpIAdd %6 %70 %29
- OpStore %52 %67
- OpBranch %53
- %55 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 3u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[1], loops[2]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- std::string checks = R"(
- CHECK: [[PHI_0:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK: [[PHI_1:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_1]]
- CHECK-NEXT: OpStore [[STORE_1]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_2:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_2]]
- CHECK: [[STORE_2:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_1]]
- CHECK-NEXT: OpStore [[STORE_2]]
- )";
- Match(checks, context.get());
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- std::string checks_ = R"(
- CHECK: [[PHI:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_1]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_2:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_2]]
- CHECK: [[STORE_2:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_2]]
- )";
- Match(checks_, context.get());
- auto& ld_final = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld_final.NumLoops(), 1u);
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10][10] a;
- int[10][10] b;
- int[10][10] c;
- // Legal inner loop fusion
- for (int i = 0; i < 10; i++) {
- for (int j = 0; j < 10; j++) {
- c[i][j] = a[i][j] + 2;
- }
- for (int j = 0; j < 10; j++) {
- b[i][j] = c[i][j] + 10;
- }
- }
- }
- */
- TEST_F(FusionLegalTest, InnerLoopFusion) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %19 "j"
- OpName %32 "c"
- OpName %35 "a"
- OpName %46 "j"
- OpName %54 "b"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 10
- %17 = OpTypeBool
- %27 = OpTypeInt 32 0
- %28 = OpConstant %27 10
- %29 = OpTypeArray %6 %28
- %30 = OpTypeArray %29 %28
- %31 = OpTypePointer Function %30
- %40 = OpConstant %6 2
- %44 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %19 = OpVariable %7 Function
- %32 = OpVariable %31 Function
- %35 = OpVariable %31 Function
- %46 = OpVariable %7 Function
- %54 = OpVariable %31 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- %67 = OpPhi %6 %9 %5 %66 %13
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %67 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- OpStore %19 %9
- OpBranch %20
- %20 = OpLabel
- %68 = OpPhi %6 %9 %11 %45 %23
- OpLoopMerge %22 %23 None
- OpBranch %24
- %24 = OpLabel
- %26 = OpSLessThan %17 %68 %16
- OpBranchConditional %26 %21 %22
- %21 = OpLabel
- %38 = OpAccessChain %7 %35 %67 %68
- %39 = OpLoad %6 %38
- %41 = OpIAdd %6 %39 %40
- %42 = OpAccessChain %7 %32 %67 %68
- OpStore %42 %41
- OpBranch %23
- %23 = OpLabel
- %45 = OpIAdd %6 %68 %44
- OpStore %19 %45
- OpBranch %20
- %22 = OpLabel
- OpStore %46 %9
- OpBranch %47
- %47 = OpLabel
- %69 = OpPhi %6 %9 %22 %64 %50
- OpLoopMerge %49 %50 None
- OpBranch %51
- %51 = OpLabel
- %53 = OpSLessThan %17 %69 %16
- OpBranchConditional %53 %48 %49
- %48 = OpLabel
- %59 = OpAccessChain %7 %32 %67 %69
- %60 = OpLoad %6 %59
- %61 = OpIAdd %6 %60 %16
- %62 = OpAccessChain %7 %54 %67 %69
- OpStore %62 %61
- OpBranch %50
- %50 = OpLabel
- %64 = OpIAdd %6 %69 %44
- OpStore %46 %64
- OpBranch %47
- %49 = OpLabel
- OpBranch %13
- %13 = OpLabel
- %66 = OpIAdd %6 %67 %44
- OpStore %8 %66
- OpBranch %10
- %12 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 3u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- auto loop_0 = loops[0];
- auto loop_1 = loops[1];
- auto loop_2 = loops[2];
- {
- LoopFusion fusion(context.get(), loop_0, loop_1);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_0, loop_2);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_1, loop_2);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- std::string checks = R"(
- CHECK: [[PHI_0:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[PHI_1:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- auto& ld_final = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld_final.NumLoops(), 2u);
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- // 12
- #version 440 core
- void main() {
- int[10][10] a;
- int[10][10] b;
- int[10][10] c;
- // Legal both
- for (int i = 0; i < 10; i++) {
- for (int j = 0; j < 10; j++) {
- c[i][j] = a[i][j] + 2;
- }
- }
- for (int i = 0; i < 10; i++) {
- for (int j = 0; j < 10; j++) {
- b[i][j] = c[i][j] + 10;
- }
- }
- }
- */
- TEST_F(FusionLegalTest, OuterAndInnerLoop) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %19 "j"
- OpName %32 "c"
- OpName %35 "a"
- OpName %48 "i"
- OpName %56 "j"
- OpName %64 "b"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 10
- %17 = OpTypeBool
- %27 = OpTypeInt 32 0
- %28 = OpConstant %27 10
- %29 = OpTypeArray %6 %28
- %30 = OpTypeArray %29 %28
- %31 = OpTypePointer Function %30
- %40 = OpConstant %6 2
- %44 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %19 = OpVariable %7 Function
- %32 = OpVariable %31 Function
- %35 = OpVariable %31 Function
- %48 = OpVariable %7 Function
- %56 = OpVariable %7 Function
- %64 = OpVariable %31 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- %77 = OpPhi %6 %9 %5 %47 %13
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %77 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- OpStore %19 %9
- OpBranch %20
- %20 = OpLabel
- %81 = OpPhi %6 %9 %11 %45 %23
- OpLoopMerge %22 %23 None
- OpBranch %24
- %24 = OpLabel
- %26 = OpSLessThan %17 %81 %16
- OpBranchConditional %26 %21 %22
- %21 = OpLabel
- %38 = OpAccessChain %7 %35 %77 %81
- %39 = OpLoad %6 %38
- %41 = OpIAdd %6 %39 %40
- %42 = OpAccessChain %7 %32 %77 %81
- OpStore %42 %41
- OpBranch %23
- %23 = OpLabel
- %45 = OpIAdd %6 %81 %44
- OpStore %19 %45
- OpBranch %20
- %22 = OpLabel
- OpBranch %13
- %13 = OpLabel
- %47 = OpIAdd %6 %77 %44
- OpStore %8 %47
- OpBranch %10
- %12 = OpLabel
- OpStore %48 %9
- OpBranch %49
- %49 = OpLabel
- %78 = OpPhi %6 %9 %12 %76 %52
- OpLoopMerge %51 %52 None
- OpBranch %53
- %53 = OpLabel
- %55 = OpSLessThan %17 %78 %16
- OpBranchConditional %55 %50 %51
- %50 = OpLabel
- OpStore %56 %9
- OpBranch %57
- %57 = OpLabel
- %79 = OpPhi %6 %9 %50 %74 %60
- OpLoopMerge %59 %60 None
- OpBranch %61
- %61 = OpLabel
- %63 = OpSLessThan %17 %79 %16
- OpBranchConditional %63 %58 %59
- %58 = OpLabel
- %69 = OpAccessChain %7 %32 %78 %79
- %70 = OpLoad %6 %69
- %71 = OpIAdd %6 %70 %16
- %72 = OpAccessChain %7 %64 %78 %79
- OpStore %72 %71
- OpBranch %60
- %60 = OpLabel
- %74 = OpIAdd %6 %79 %44
- OpStore %56 %74
- OpBranch %57
- %59 = OpLabel
- OpBranch %52
- %52 = OpLabel
- %76 = OpIAdd %6 %78 %44
- OpStore %48 %76
- OpBranch %49
- %51 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 4u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- auto loop_0 = loops[0];
- auto loop_1 = loops[1];
- auto loop_2 = loops[2];
- auto loop_3 = loops[3];
- {
- LoopFusion fusion(context.get(), loop_0, loop_1);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_1, loop_2);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_2, loop_3);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_1, loop_3);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_0, loop_2);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- std::string checks = R"(
- CHECK: [[PHI_0:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[PHI_1:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK: [[PHI_2:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_2]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_2]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- }
- {
- auto& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 3u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- auto loop_0 = loops[0];
- auto loop_1 = loops[1];
- auto loop_2 = loops[2];
- {
- LoopFusion fusion(context.get(), loop_0, loop_1);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_0, loop_2);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_1, loop_2);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- std::string checks = R"(
- CHECK: [[PHI_0:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[PHI_1:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- }
- {
- auto& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- }
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10][10] a;
- int[10][10] b;
- int[10][10] c;
- // Legal both, more complex
- for (int i = 0; i < 10; i++) {
- for (int j = 0; j < 10; j++) {
- if (i % 2 == 0 && j % 2 == 0) {
- c[i][j] = a[i][j] + 2;
- }
- }
- }
- for (int i = 0; i < 10; i++) {
- for (int j = 0; j < 10; j++) {
- b[i][j] = c[i][j] + 10;
- }
- }
- }
- */
- TEST_F(FusionLegalTest, OuterAndInnerLoopMoreComplex) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %19 "j"
- OpName %44 "c"
- OpName %47 "a"
- OpName %59 "i"
- OpName %67 "j"
- OpName %75 "b"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 10
- %17 = OpTypeBool
- %28 = OpConstant %6 2
- %39 = OpTypeInt 32 0
- %40 = OpConstant %39 10
- %41 = OpTypeArray %6 %40
- %42 = OpTypeArray %41 %40
- %43 = OpTypePointer Function %42
- %55 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %19 = OpVariable %7 Function
- %44 = OpVariable %43 Function
- %47 = OpVariable %43 Function
- %59 = OpVariable %7 Function
- %67 = OpVariable %7 Function
- %75 = OpVariable %43 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- %88 = OpPhi %6 %9 %5 %58 %13
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %88 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- OpStore %19 %9
- OpBranch %20
- %20 = OpLabel
- %92 = OpPhi %6 %9 %11 %56 %23
- OpLoopMerge %22 %23 None
- OpBranch %24
- %24 = OpLabel
- %26 = OpSLessThan %17 %92 %16
- OpBranchConditional %26 %21 %22
- %21 = OpLabel
- %29 = OpSMod %6 %88 %28
- %30 = OpIEqual %17 %29 %9
- OpSelectionMerge %32 None
- OpBranchConditional %30 %31 %32
- %31 = OpLabel
- %34 = OpSMod %6 %92 %28
- %35 = OpIEqual %17 %34 %9
- OpBranch %32
- %32 = OpLabel
- %36 = OpPhi %17 %30 %21 %35 %31
- OpSelectionMerge %38 None
- OpBranchConditional %36 %37 %38
- %37 = OpLabel
- %50 = OpAccessChain %7 %47 %88 %92
- %51 = OpLoad %6 %50
- %52 = OpIAdd %6 %51 %28
- %53 = OpAccessChain %7 %44 %88 %92
- OpStore %53 %52
- OpBranch %38
- %38 = OpLabel
- OpBranch %23
- %23 = OpLabel
- %56 = OpIAdd %6 %92 %55
- OpStore %19 %56
- OpBranch %20
- %22 = OpLabel
- OpBranch %13
- %13 = OpLabel
- %58 = OpIAdd %6 %88 %55
- OpStore %8 %58
- OpBranch %10
- %12 = OpLabel
- OpStore %59 %9
- OpBranch %60
- %60 = OpLabel
- %89 = OpPhi %6 %9 %12 %87 %63
- OpLoopMerge %62 %63 None
- OpBranch %64
- %64 = OpLabel
- %66 = OpSLessThan %17 %89 %16
- OpBranchConditional %66 %61 %62
- %61 = OpLabel
- OpStore %67 %9
- OpBranch %68
- %68 = OpLabel
- %90 = OpPhi %6 %9 %61 %85 %71
- OpLoopMerge %70 %71 None
- OpBranch %72
- %72 = OpLabel
- %74 = OpSLessThan %17 %90 %16
- OpBranchConditional %74 %69 %70
- %69 = OpLabel
- %80 = OpAccessChain %7 %44 %89 %90
- %81 = OpLoad %6 %80
- %82 = OpIAdd %6 %81 %16
- %83 = OpAccessChain %7 %75 %89 %90
- OpStore %83 %82
- OpBranch %71
- %71 = OpLabel
- %85 = OpIAdd %6 %90 %55
- OpStore %67 %85
- OpBranch %68
- %70 = OpLabel
- OpBranch %63
- %63 = OpLabel
- %87 = OpIAdd %6 %89 %55
- OpStore %59 %87
- OpBranch %60
- %62 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 4u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- auto loop_0 = loops[0];
- auto loop_1 = loops[1];
- auto loop_2 = loops[2];
- auto loop_3 = loops[3];
- {
- LoopFusion fusion(context.get(), loop_0, loop_1);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_1, loop_2);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_2, loop_3);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_1, loop_3);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_0, loop_2);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- std::string checks = R"(
- CHECK: [[PHI_0:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[PHI_1:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: OpPhi
- CHECK-NEXT: OpSelectionMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK: [[PHI_2:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_2]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_2]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- }
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 3u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- auto loop_0 = loops[0];
- auto loop_1 = loops[1];
- auto loop_2 = loops[2];
- {
- LoopFusion fusion(context.get(), loop_0, loop_1);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_0, loop_2);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_1, loop_2);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- std::string checks = R"(
- CHECK: [[PHI_0:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[PHI_1:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: OpPhi
- CHECK-NEXT: OpSelectionMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- }
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- }
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10][10] a;
- int[10][10] b;
- int[10][10] c;
- // Outer would have been illegal to fuse, but since written
- // like this, inner loop fusion is legal.
- for (int i = 0; i < 10; i++) {
- for (int j = 0; j < 10; j++) {
- c[i][j] = a[i][j] + 2;
- }
- for (int j = 0; j < 10; j++) {
- b[i][j] = c[i+1][j] + 10;
- }
- }
- }
- */
- TEST_F(FusionLegalTest, InnerWithExistingDependenceOnOuter) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %19 "j"
- OpName %32 "c"
- OpName %35 "a"
- OpName %46 "j"
- OpName %54 "b"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 10
- %17 = OpTypeBool
- %27 = OpTypeInt 32 0
- %28 = OpConstant %27 10
- %29 = OpTypeArray %6 %28
- %30 = OpTypeArray %29 %28
- %31 = OpTypePointer Function %30
- %40 = OpConstant %6 2
- %44 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %19 = OpVariable %7 Function
- %32 = OpVariable %31 Function
- %35 = OpVariable %31 Function
- %46 = OpVariable %7 Function
- %54 = OpVariable %31 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- %68 = OpPhi %6 %9 %5 %67 %13
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %68 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- OpStore %19 %9
- OpBranch %20
- %20 = OpLabel
- %69 = OpPhi %6 %9 %11 %45 %23
- OpLoopMerge %22 %23 None
- OpBranch %24
- %24 = OpLabel
- %26 = OpSLessThan %17 %69 %16
- OpBranchConditional %26 %21 %22
- %21 = OpLabel
- %38 = OpAccessChain %7 %35 %68 %69
- %39 = OpLoad %6 %38
- %41 = OpIAdd %6 %39 %40
- %42 = OpAccessChain %7 %32 %68 %69
- OpStore %42 %41
- OpBranch %23
- %23 = OpLabel
- %45 = OpIAdd %6 %69 %44
- OpStore %19 %45
- OpBranch %20
- %22 = OpLabel
- OpStore %46 %9
- OpBranch %47
- %47 = OpLabel
- %70 = OpPhi %6 %9 %22 %65 %50
- OpLoopMerge %49 %50 None
- OpBranch %51
- %51 = OpLabel
- %53 = OpSLessThan %17 %70 %16
- OpBranchConditional %53 %48 %49
- %48 = OpLabel
- %58 = OpIAdd %6 %68 %44
- %60 = OpAccessChain %7 %32 %58 %70
- %61 = OpLoad %6 %60
- %62 = OpIAdd %6 %61 %16
- %63 = OpAccessChain %7 %54 %68 %70
- OpStore %63 %62
- OpBranch %50
- %50 = OpLabel
- %65 = OpIAdd %6 %70 %44
- OpStore %46 %65
- OpBranch %47
- %49 = OpLabel
- OpBranch %13
- %13 = OpLabel
- %67 = OpIAdd %6 %68 %44
- OpStore %8 %67
- OpBranch %10
- %12 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 3u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- auto loop_0 = loops[0];
- auto loop_1 = loops[1];
- auto loop_2 = loops[2];
- {
- LoopFusion fusion(context.get(), loop_0, loop_1);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_0, loop_2);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_1, loop_2);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- }
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- std::string checks = R"(
- CHECK: [[PHI_0:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[PHI_1:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK-NOT: OpPhi
- CHECK: [[I_1:%\w+]] = OpIAdd {{%\w+}} [[PHI_0]] {{%\w+}}
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_1]] [[PHI_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- }
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10] a;
- int[10] b;
- int[10] c;
- // One dimensional arrays. Legal, outer dist 0, inner independent.
- for (int i = 0; i < 10; i++) {
- for (int j = 0; j < 10; j++) {
- c[i] = a[j] + 2;
- }
- }
- for (int i = 0; i < 10; i++) {
- for (int j = 0; j < 10; j++) {
- b[j] = c[i] + 10;
- }
- }
- }
- */
- TEST_F(FusionLegalTest, OuterAndInnerLoopOneDimArrays) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %19 "j"
- OpName %31 "c"
- OpName %33 "a"
- OpName %45 "i"
- OpName %53 "j"
- OpName %61 "b"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 10
- %17 = OpTypeBool
- %27 = OpTypeInt 32 0
- %28 = OpConstant %27 10
- %29 = OpTypeArray %6 %28
- %30 = OpTypePointer Function %29
- %37 = OpConstant %6 2
- %41 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %19 = OpVariable %7 Function
- %31 = OpVariable %30 Function
- %33 = OpVariable %30 Function
- %45 = OpVariable %7 Function
- %53 = OpVariable %7 Function
- %61 = OpVariable %30 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- %72 = OpPhi %6 %9 %5 %44 %13
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %72 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- OpStore %19 %9
- OpBranch %20
- %20 = OpLabel
- %76 = OpPhi %6 %9 %11 %42 %23
- OpLoopMerge %22 %23 None
- OpBranch %24
- %24 = OpLabel
- %26 = OpSLessThan %17 %76 %16
- OpBranchConditional %26 %21 %22
- %21 = OpLabel
- %35 = OpAccessChain %7 %33 %76
- %36 = OpLoad %6 %35
- %38 = OpIAdd %6 %36 %37
- %39 = OpAccessChain %7 %31 %72
- OpStore %39 %38
- OpBranch %23
- %23 = OpLabel
- %42 = OpIAdd %6 %76 %41
- OpStore %19 %42
- OpBranch %20
- %22 = OpLabel
- OpBranch %13
- %13 = OpLabel
- %44 = OpIAdd %6 %72 %41
- OpStore %8 %44
- OpBranch %10
- %12 = OpLabel
- OpStore %45 %9
- OpBranch %46
- %46 = OpLabel
- %73 = OpPhi %6 %9 %12 %71 %49
- OpLoopMerge %48 %49 None
- OpBranch %50
- %50 = OpLabel
- %52 = OpSLessThan %17 %73 %16
- OpBranchConditional %52 %47 %48
- %47 = OpLabel
- OpStore %53 %9
- OpBranch %54
- %54 = OpLabel
- %74 = OpPhi %6 %9 %47 %69 %57
- OpLoopMerge %56 %57 None
- OpBranch %58
- %58 = OpLabel
- %60 = OpSLessThan %17 %74 %16
- OpBranchConditional %60 %55 %56
- %55 = OpLabel
- %64 = OpAccessChain %7 %31 %73
- %65 = OpLoad %6 %64
- %66 = OpIAdd %6 %65 %16
- %67 = OpAccessChain %7 %61 %74
- OpStore %67 %66
- OpBranch %57
- %57 = OpLabel
- %69 = OpIAdd %6 %74 %41
- OpStore %53 %69
- OpBranch %54
- %56 = OpLabel
- OpBranch %49
- %49 = OpLabel
- %71 = OpIAdd %6 %73 %41
- OpStore %45 %71
- OpBranch %46
- %48 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 4u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- auto loop_0 = loops[0];
- auto loop_1 = loops[1];
- auto loop_2 = loops[2];
- auto loop_3 = loops[3];
- {
- LoopFusion fusion(context.get(), loop_0, loop_1);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_1, loop_2);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_2, loop_3);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_0, loop_2);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- std::string checks = R"(
- CHECK: [[PHI_0:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[PHI_1:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK: [[PHI_2:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_2]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- }
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 3u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- auto loop_0 = loops[0];
- auto loop_1 = loops[1];
- auto loop_2 = loops[2];
- {
- LoopFusion fusion(context.get(), loop_0, loop_1);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_0, loop_2);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- {
- LoopFusion fusion(context.get(), loop_1, loop_2);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- std::string checks = R"(
- CHECK: [[PHI_0:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[PHI_1:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_1]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- }
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10] a;
- int[10] b;
- int[10] c;
- // Legal, creates a loop-carried dependence, but has negative distance
- for (int i = 0; i < 10; i++) {
- c[i] = a[i+1] + 1;
- }
- for (int i = 0; i < 10; i++) {
- a[i] = c[i] + 2;
- }
- }
- */
- TEST_F(FusionLegalTest, NegativeDistanceCreatedWAR) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %23 "c"
- OpName %25 "a"
- OpName %35 "i"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 10
- %17 = OpTypeBool
- %19 = OpTypeInt 32 0
- %20 = OpConstant %19 10
- %21 = OpTypeArray %6 %20
- %22 = OpTypePointer Function %21
- %27 = OpConstant %6 1
- %47 = OpConstant %6 2
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %23 = OpVariable %22 Function
- %25 = OpVariable %22 Function
- %35 = OpVariable %7 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- %52 = OpPhi %6 %9 %5 %34 %13
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %52 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %28 = OpIAdd %6 %52 %27
- %29 = OpAccessChain %7 %25 %28
- %30 = OpLoad %6 %29
- %31 = OpIAdd %6 %30 %27
- %32 = OpAccessChain %7 %23 %52
- OpStore %32 %31
- OpBranch %13
- %13 = OpLabel
- %34 = OpIAdd %6 %52 %27
- OpStore %8 %34
- OpBranch %10
- %12 = OpLabel
- OpStore %35 %9
- OpBranch %36
- %36 = OpLabel
- %53 = OpPhi %6 %9 %12 %51 %39
- OpLoopMerge %38 %39 None
- OpBranch %40
- %40 = OpLabel
- %42 = OpSLessThan %17 %53 %16
- OpBranchConditional %42 %37 %38
- %37 = OpLabel
- %45 = OpAccessChain %7 %23 %53
- %46 = OpLoad %6 %45
- %48 = OpIAdd %6 %46 %47
- %49 = OpAccessChain %7 %25 %53
- OpStore %49 %48
- OpBranch %39
- %39 = OpLabel
- %51 = OpIAdd %6 %53 %27
- OpStore %35 %51
- OpBranch %36
- %38 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- std::string checks = R"(
- CHECK: [[PHI:%\w+]] = OpPhi
- CHECK: [[I_1:%\w+]] = OpIAdd {{%\w+}} [[PHI]] {{%\w+}}
- CHECK-NEXT: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- }
- {
- auto& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 1u);
- }
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10] a;
- int[10] b;
- int[10] c;
- // Legal, creates a loop-carried dependence, but has negative distance
- for (int i = 0; i < 10; i++) {
- a[i+1] = b[i] + 1;
- }
- for (int i = 0; i < 10; i++) {
- a[i] = c[i+1] + 2;
- }
- }
- */
- TEST_F(FusionLegalTest, NegativeDistanceCreatedWAW) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %23 "a"
- OpName %27 "b"
- OpName %35 "i"
- OpName %44 "c"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 10
- %17 = OpTypeBool
- %19 = OpTypeInt 32 0
- %20 = OpConstant %19 10
- %21 = OpTypeArray %6 %20
- %22 = OpTypePointer Function %21
- %25 = OpConstant %6 1
- %49 = OpConstant %6 2
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %23 = OpVariable %22 Function
- %27 = OpVariable %22 Function
- %35 = OpVariable %7 Function
- %44 = OpVariable %22 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- %54 = OpPhi %6 %9 %5 %34 %13
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %54 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %26 = OpIAdd %6 %54 %25
- %29 = OpAccessChain %7 %27 %54
- %30 = OpLoad %6 %29
- %31 = OpIAdd %6 %30 %25
- %32 = OpAccessChain %7 %23 %26
- OpStore %32 %31
- OpBranch %13
- %13 = OpLabel
- %34 = OpIAdd %6 %54 %25
- OpStore %8 %34
- OpBranch %10
- %12 = OpLabel
- OpStore %35 %9
- OpBranch %36
- %36 = OpLabel
- %55 = OpPhi %6 %9 %12 %53 %39
- OpLoopMerge %38 %39 None
- OpBranch %40
- %40 = OpLabel
- %42 = OpSLessThan %17 %55 %16
- OpBranchConditional %42 %37 %38
- %37 = OpLabel
- %46 = OpIAdd %6 %55 %25
- %47 = OpAccessChain %7 %44 %46
- %48 = OpLoad %6 %47
- %50 = OpIAdd %6 %48 %49
- %51 = OpAccessChain %7 %23 %55
- OpStore %51 %50
- OpBranch %39
- %39 = OpLabel
- %53 = OpIAdd %6 %55 %25
- OpStore %35 %53
- OpBranch %36
- %38 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 1u);
- std::string checks = R"(
- CHECK: [[PHI:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[I_1:%\w+]] = OpIAdd {{%\w+}} [[PHI]] {{%\w+}}
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_1]]
- CHECK-NEXT: OpStore
- CHECK-NOT: OpPhi
- CHECK: [[I_1:%\w+]] = OpIAdd {{%\w+}} [[PHI]] {{%\w+}}
- CHECK-NEXT: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- }
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10] a;
- int[10] b;
- int[10] c;
- // Legal, no loop-carried dependence
- for (int i = 0; i < 10; i++) {
- a[i] = b[i] + 1;
- }
- for (int i = 0; i < 10; i++) {
- a[i] = c[i+1] + 2;
- }
- }
- */
- TEST_F(FusionLegalTest, NoLoopCarriedDependencesWAW) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %23 "a"
- OpName %25 "b"
- OpName %34 "i"
- OpName %43 "c"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 10
- %17 = OpTypeBool
- %19 = OpTypeInt 32 0
- %20 = OpConstant %19 10
- %21 = OpTypeArray %6 %20
- %22 = OpTypePointer Function %21
- %29 = OpConstant %6 1
- %48 = OpConstant %6 2
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %23 = OpVariable %22 Function
- %25 = OpVariable %22 Function
- %34 = OpVariable %7 Function
- %43 = OpVariable %22 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- %53 = OpPhi %6 %9 %5 %33 %13
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %53 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %27 = OpAccessChain %7 %25 %53
- %28 = OpLoad %6 %27
- %30 = OpIAdd %6 %28 %29
- %31 = OpAccessChain %7 %23 %53
- OpStore %31 %30
- OpBranch %13
- %13 = OpLabel
- %33 = OpIAdd %6 %53 %29
- OpStore %8 %33
- OpBranch %10
- %12 = OpLabel
- OpStore %34 %9
- OpBranch %35
- %35 = OpLabel
- %54 = OpPhi %6 %9 %12 %52 %38
- OpLoopMerge %37 %38 None
- OpBranch %39
- %39 = OpLabel
- %41 = OpSLessThan %17 %54 %16
- OpBranchConditional %41 %36 %37
- %36 = OpLabel
- %45 = OpIAdd %6 %54 %29
- %46 = OpAccessChain %7 %43 %45
- %47 = OpLoad %6 %46
- %49 = OpIAdd %6 %47 %48
- %50 = OpAccessChain %7 %23 %54
- OpStore %50 %49
- OpBranch %38
- %38 = OpLabel
- %52 = OpIAdd %6 %54 %29
- OpStore %34 %52
- OpBranch %35
- %37 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 1u);
- std::string checks = R"(
- CHECK: [[PHI:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK-NOT: OpPhi
- CHECK: [[I_1:%\w+]] = OpIAdd {{%\w+}} [[PHI]] {{%\w+}}
- CHECK-NEXT: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[I_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- }
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10][10] a;
- int[10][10] b;
- int[10][10] c;
- // Legal outer. Continue and break are fine if nested in inner loops
- for (int i = 0; i < 10; i++) {
- for (int j = 0; j < 10; j++) {
- if (j % 2 == 0) {
- c[i][j] = a[i][j] + 2;
- } else {
- continue;
- }
- }
- }
- for (int i = 0; i < 10; i++) {
- for (int j = 0; j < 10; j++) {
- if (j % 2 == 0) {
- b[i][j] = c[i][j] + 10;
- } else {
- break;
- }
- }
- }
- }
- */
- TEST_F(FusionLegalTest, OuterloopWithBreakContinueInInner) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %19 "j"
- OpName %38 "c"
- OpName %41 "a"
- OpName %55 "i"
- OpName %63 "j"
- OpName %76 "b"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 10
- %17 = OpTypeBool
- %28 = OpConstant %6 2
- %33 = OpTypeInt 32 0
- %34 = OpConstant %33 10
- %35 = OpTypeArray %6 %34
- %36 = OpTypeArray %35 %34
- %37 = OpTypePointer Function %36
- %51 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %19 = OpVariable %7 Function
- %38 = OpVariable %37 Function
- %41 = OpVariable %37 Function
- %55 = OpVariable %7 Function
- %63 = OpVariable %7 Function
- %76 = OpVariable %37 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- %91 = OpPhi %6 %9 %5 %54 %13
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %91 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- OpStore %19 %9
- OpBranch %20
- %20 = OpLabel
- %96 = OpPhi %6 %9 %11 %52 %23
- OpLoopMerge %22 %23 None
- OpBranch %24
- %24 = OpLabel
- %26 = OpSLessThan %17 %96 %16
- OpBranchConditional %26 %21 %22
- %21 = OpLabel
- %29 = OpSMod %6 %96 %28
- %30 = OpIEqual %17 %29 %9
- OpSelectionMerge %sel_merge None
- OpBranchConditional %30 %31 %48
- %31 = OpLabel
- %44 = OpAccessChain %7 %41 %91 %96
- %45 = OpLoad %6 %44
- %46 = OpIAdd %6 %45 %28
- %47 = OpAccessChain %7 %38 %91 %96
- OpStore %47 %46
- OpBranch %32
- %48 = OpLabel
- OpBranch %sel_merge
- %32 = OpLabel
- OpBranch %sel_merge
- %sel_merge = OpLabel
- OpBranch %23
- %23 = OpLabel
- %52 = OpIAdd %6 %96 %51
- OpStore %19 %52
- OpBranch %20
- %22 = OpLabel
- OpBranch %13
- %13 = OpLabel
- %54 = OpIAdd %6 %91 %51
- OpStore %8 %54
- OpBranch %10
- %12 = OpLabel
- OpStore %55 %9
- OpBranch %56
- %56 = OpLabel
- %92 = OpPhi %6 %9 %12 %90 %59
- OpLoopMerge %58 %59 None
- OpBranch %60
- %60 = OpLabel
- %62 = OpSLessThan %17 %92 %16
- OpBranchConditional %62 %57 %58
- %57 = OpLabel
- OpStore %63 %9
- OpBranch %64
- %64 = OpLabel
- %93 = OpPhi %6 %9 %57 %88 %67
- OpLoopMerge %66 %67 None
- OpBranch %68
- %68 = OpLabel
- %70 = OpSLessThan %17 %93 %16
- OpBranchConditional %70 %65 %66
- %65 = OpLabel
- %72 = OpSMod %6 %93 %28
- %73 = OpIEqual %17 %72 %9
- OpSelectionMerge %75 None
- OpBranchConditional %73 %74 %66
- %74 = OpLabel
- %81 = OpAccessChain %7 %38 %92 %93
- %82 = OpLoad %6 %81
- %83 = OpIAdd %6 %82 %16
- %84 = OpAccessChain %7 %76 %92 %93
- OpStore %84 %83
- OpBranch %75
- %75 = OpLabel
- OpBranch %67
- %67 = OpLabel
- %88 = OpIAdd %6 %93 %51
- OpStore %63 %88
- OpBranch %64
- %66 = OpLabel
- OpBranch %59
- %59 = OpLabel
- %90 = OpIAdd %6 %92 %51
- OpStore %55 %90
- OpBranch %56
- %58 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 4u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[0], loops[2]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 3u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[1], loops[2]);
- EXPECT_FALSE(fusion.AreCompatible());
- std::string checks = R"(
- CHECK: [[PHI_0:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[PHI_1:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_1]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK: [[PHI_2:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_2]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]] [[PHI_2]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- }
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- // j loop preheader removed manually
- #version 440 core
- void main() {
- int[10] a;
- int[10] b;
- int i = 0;
- int j = 0;
- // No loop-carried dependences, legal
- for (; i < 10; i++) {
- a[i] = a[i]*2;
- }
- for (; j < 10; j++) {
- b[j] = a[j]+2;
- }
- }
- */
- TEST_F(FusionLegalTest, DifferentArraysInLoopsNoPreheader) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %10 "j"
- OpName %24 "a"
- OpName %42 "b"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %17 = OpConstant %6 10
- %18 = OpTypeBool
- %20 = OpTypeInt 32 0
- %21 = OpConstant %20 10
- %22 = OpTypeArray %6 %21
- %23 = OpTypePointer Function %22
- %29 = OpConstant %6 2
- %33 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %10 = OpVariable %7 Function
- %24 = OpVariable %23 Function
- %42 = OpVariable %23 Function
- OpStore %8 %9
- OpStore %10 %9
- OpBranch %11
- %11 = OpLabel
- %51 = OpPhi %6 %9 %5 %34 %14
- OpLoopMerge %35 %14 None
- OpBranch %15
- %15 = OpLabel
- %19 = OpSLessThan %18 %51 %17
- OpBranchConditional %19 %12 %35
- %12 = OpLabel
- %27 = OpAccessChain %7 %24 %51
- %28 = OpLoad %6 %27
- %30 = OpIMul %6 %28 %29
- %31 = OpAccessChain %7 %24 %51
- OpStore %31 %30
- OpBranch %14
- %14 = OpLabel
- %34 = OpIAdd %6 %51 %33
- OpStore %8 %34
- OpBranch %11
- %35 = OpLabel
- %52 = OpPhi %6 %9 %15 %50 %38
- OpLoopMerge %37 %38 None
- OpBranch %39
- %39 = OpLabel
- %41 = OpSLessThan %18 %52 %17
- OpBranchConditional %41 %36 %37
- %36 = OpLabel
- %45 = OpAccessChain %7 %24 %52
- %46 = OpLoad %6 %45
- %47 = OpIAdd %6 %46 %29
- %48 = OpAccessChain %7 %42 %52
- OpStore %48 %47
- OpBranch %38
- %38 = OpLabel
- %50 = OpIAdd %6 %52 %33
- OpStore %10 %50
- OpBranch %35
- %37 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- {
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- ld.CreatePreHeaderBlocksIfMissing();
- {
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- }
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 1u);
- std::string checks = R"(
- CHECK: [[PHI:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- }
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- // j & k loop preheaders removed manually
- #version 440 core
- void main() {
- int[10] a;
- int[10] b;
- int i = 0;
- int j = 0;
- int k = 0;
- // No loop-carried dependences, legal
- for (; i < 10; i++) {
- a[i] = a[i]*2;
- }
- for (; j < 10; j++) {
- b[j] = a[j]+2;
- }
- for (; k < 10; k++) {
- a[k] = a[k]*2;
- }
- }
- */
- TEST_F(FusionLegalTest, AdjacentLoopsNoPreheaders) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %10 "j"
- OpName %11 "k"
- OpName %25 "a"
- OpName %43 "b"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %18 = OpConstant %6 10
- %19 = OpTypeBool
- %21 = OpTypeInt 32 0
- %22 = OpConstant %21 10
- %23 = OpTypeArray %6 %22
- %24 = OpTypePointer Function %23
- %30 = OpConstant %6 2
- %34 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %10 = OpVariable %7 Function
- %11 = OpVariable %7 Function
- %25 = OpVariable %24 Function
- %43 = OpVariable %24 Function
- OpStore %8 %9
- OpStore %10 %9
- OpStore %11 %9
- OpBranch %12
- %12 = OpLabel
- %67 = OpPhi %6 %9 %5 %35 %15
- OpLoopMerge %36 %15 None
- OpBranch %16
- %16 = OpLabel
- %20 = OpSLessThan %19 %67 %18
- OpBranchConditional %20 %13 %36
- %13 = OpLabel
- %28 = OpAccessChain %7 %25 %67
- %29 = OpLoad %6 %28
- %31 = OpIMul %6 %29 %30
- %32 = OpAccessChain %7 %25 %67
- OpStore %32 %31
- OpBranch %15
- %15 = OpLabel
- %35 = OpIAdd %6 %67 %34
- OpStore %8 %35
- OpBranch %12
- %36 = OpLabel
- %68 = OpPhi %6 %9 %16 %51 %39
- OpLoopMerge %52 %39 None
- OpBranch %40
- %40 = OpLabel
- %42 = OpSLessThan %19 %68 %18
- OpBranchConditional %42 %37 %52
- %37 = OpLabel
- %46 = OpAccessChain %7 %25 %68
- %47 = OpLoad %6 %46
- %48 = OpIAdd %6 %47 %30
- %49 = OpAccessChain %7 %43 %68
- OpStore %49 %48
- OpBranch %39
- %39 = OpLabel
- %51 = OpIAdd %6 %68 %34
- OpStore %10 %51
- OpBranch %36
- %52 = OpLabel
- %70 = OpPhi %6 %9 %40 %66 %55
- OpLoopMerge %54 %55 None
- OpBranch %56
- %56 = OpLabel
- %58 = OpSLessThan %19 %70 %18
- OpBranchConditional %58 %53 %54
- %53 = OpLabel
- %61 = OpAccessChain %7 %25 %70
- %62 = OpLoad %6 %61
- %63 = OpIMul %6 %62 %30
- %64 = OpAccessChain %7 %25 %70
- OpStore %64 %63
- OpBranch %55
- %55 = OpLabel
- %66 = OpIAdd %6 %70 %34
- OpStore %11 %66
- OpBranch %52
- %54 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 3u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- {
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_FALSE(fusion.AreCompatible());
- }
- ld.CreatePreHeaderBlocksIfMissing();
- {
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- }
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- std::string checks = R"(
- CHECK: [[PHI_0:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_0]]
- CHECK-NEXT: OpStore [[STORE_1]]
- CHECK: [[PHI_1:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_2:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_1]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_2]]
- CHECK: [[STORE_2:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI_1]]
- CHECK-NEXT: OpStore [[STORE_2]]
- )";
- Match(checks, context.get());
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 1u);
- std::string checks = R"(
- CHECK: [[PHI:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_0]]
- CHECK: [[STORE_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_0]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_1]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_2:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpLoad {{%\w+}} [[LOAD_2]]
- CHECK: [[STORE_2:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_2]]
- )";
- Match(checks, context.get());
- }
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10] a;
- int[10] b;
- int sum_0 = 0;
- int sum_1 = 0;
- // No loop-carried dependences, legal
- for (int i = 0; i < 10; i++) {
- sum_0 += a[i];
- }
- for (int j = 0; j < 10; j++) {
- sum_1 += b[j];
- }
- int total = sum_0 + sum_1;
- }
- */
- TEST_F(FusionLegalTest, IndependentReductions) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "sum_0"
- OpName %10 "sum_1"
- OpName %11 "i"
- OpName %25 "a"
- OpName %34 "j"
- OpName %42 "b"
- OpName %50 "total"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %18 = OpConstant %6 10
- %19 = OpTypeBool
- %21 = OpTypeInt 32 0
- %22 = OpConstant %21 10
- %23 = OpTypeArray %6 %22
- %24 = OpTypePointer Function %23
- %32 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %10 = OpVariable %7 Function
- %11 = OpVariable %7 Function
- %25 = OpVariable %24 Function
- %34 = OpVariable %7 Function
- %42 = OpVariable %24 Function
- %50 = OpVariable %7 Function
- OpStore %8 %9
- OpStore %10 %9
- OpStore %11 %9
- OpBranch %12
- %12 = OpLabel
- %57 = OpPhi %6 %9 %5 %30 %15
- %54 = OpPhi %6 %9 %5 %33 %15
- OpLoopMerge %14 %15 None
- OpBranch %16
- %16 = OpLabel
- %20 = OpSLessThan %19 %54 %18
- OpBranchConditional %20 %13 %14
- %13 = OpLabel
- %27 = OpAccessChain %7 %25 %54
- %28 = OpLoad %6 %27
- %30 = OpIAdd %6 %57 %28
- OpStore %8 %30
- OpBranch %15
- %15 = OpLabel
- %33 = OpIAdd %6 %54 %32
- OpStore %11 %33
- OpBranch %12
- %14 = OpLabel
- OpStore %34 %9
- OpBranch %35
- %35 = OpLabel
- %58 = OpPhi %6 %9 %14 %47 %38
- %55 = OpPhi %6 %9 %14 %49 %38
- OpLoopMerge %37 %38 None
- OpBranch %39
- %39 = OpLabel
- %41 = OpSLessThan %19 %55 %18
- OpBranchConditional %41 %36 %37
- %36 = OpLabel
- %44 = OpAccessChain %7 %42 %55
- %45 = OpLoad %6 %44
- %47 = OpIAdd %6 %58 %45
- OpStore %10 %47
- OpBranch %38
- %38 = OpLabel
- %49 = OpIAdd %6 %55 %32
- OpStore %34 %49
- OpBranch %35
- %37 = OpLabel
- %53 = OpIAdd %6 %57 %58
- OpStore %50 %53
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 1u);
- std::string checks = R"(
- CHECK: [[SUM_0:%\w+]] = OpPhi
- CHECK-NEXT: [[SUM_1:%\w+]] = OpPhi
- CHECK-NEXT: [[PHI:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: [[LOAD_RES_0:%\w+]] = OpLoad {{%\w+}} [[LOAD_0]]
- CHECK-NEXT: [[ADD_RES_0:%\w+]] = OpIAdd {{%\w+}} [[SUM_0]] [[LOAD_RES_0]]
- CHECK-NEXT: OpStore {{%\w+}} [[ADD_RES_0]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: [[LOAD_RES_1:%\w+]] = OpLoad {{%\w+}} [[LOAD_1]]
- CHECK-NEXT: [[ADD_RES_1:%\w+]] = OpIAdd {{%\w+}} [[SUM_1]] [[LOAD_RES_1]]
- CHECK-NEXT: OpStore {{%\w+}} [[ADD_RES_1]]
- )";
- Match(checks, context.get());
- }
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10] a;
- int[10] b;
- int sum_0 = 0;
- int sum_1 = 0;
- // No loop-carried dependences, legal
- for (int i = 0; i < 10; i++) {
- sum_0 += a[i];
- }
- for (int j = 0; j < 10; j++) {
- sum_1 += b[j];
- }
- int total = sum_0 + sum_1;
- }
- */
- TEST_F(FusionLegalTest, IndependentReductionsOneLCSSA) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "sum_0"
- OpName %10 "sum_1"
- OpName %11 "i"
- OpName %25 "a"
- OpName %34 "j"
- OpName %42 "b"
- OpName %50 "total"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %18 = OpConstant %6 10
- %19 = OpTypeBool
- %21 = OpTypeInt 32 0
- %22 = OpConstant %21 10
- %23 = OpTypeArray %6 %22
- %24 = OpTypePointer Function %23
- %32 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %10 = OpVariable %7 Function
- %11 = OpVariable %7 Function
- %25 = OpVariable %24 Function
- %34 = OpVariable %7 Function
- %42 = OpVariable %24 Function
- %50 = OpVariable %7 Function
- OpStore %8 %9
- OpStore %10 %9
- OpStore %11 %9
- OpBranch %12
- %12 = OpLabel
- %57 = OpPhi %6 %9 %5 %30 %15
- %54 = OpPhi %6 %9 %5 %33 %15
- OpLoopMerge %14 %15 None
- OpBranch %16
- %16 = OpLabel
- %20 = OpSLessThan %19 %54 %18
- OpBranchConditional %20 %13 %14
- %13 = OpLabel
- %27 = OpAccessChain %7 %25 %54
- %28 = OpLoad %6 %27
- %30 = OpIAdd %6 %57 %28
- OpStore %8 %30
- OpBranch %15
- %15 = OpLabel
- %33 = OpIAdd %6 %54 %32
- OpStore %11 %33
- OpBranch %12
- %14 = OpLabel
- OpStore %34 %9
- OpBranch %35
- %35 = OpLabel
- %58 = OpPhi %6 %9 %14 %47 %38
- %55 = OpPhi %6 %9 %14 %49 %38
- OpLoopMerge %37 %38 None
- OpBranch %39
- %39 = OpLabel
- %41 = OpSLessThan %19 %55 %18
- OpBranchConditional %41 %36 %37
- %36 = OpLabel
- %44 = OpAccessChain %7 %42 %55
- %45 = OpLoad %6 %44
- %47 = OpIAdd %6 %58 %45
- OpStore %10 %47
- OpBranch %38
- %38 = OpLabel
- %49 = OpIAdd %6 %55 %32
- OpStore %34 %49
- OpBranch %35
- %37 = OpLabel
- %53 = OpIAdd %6 %57 %58
- OpStore %50 %53
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopUtils utils_0(context.get(), loops[0]);
- utils_0.MakeLoopClosedSSA();
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 1u);
- std::string checks = R"(
- CHECK: [[SUM_0:%\w+]] = OpPhi
- CHECK-NEXT: [[SUM_1:%\w+]] = OpPhi
- CHECK-NEXT: [[PHI:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: [[LOAD_RES_0:%\w+]] = OpLoad {{%\w+}} [[LOAD_0]]
- CHECK-NEXT: [[ADD_RES_0:%\w+]] = OpIAdd {{%\w+}} [[SUM_0]] [[LOAD_RES_0]]
- CHECK-NEXT: OpStore {{%\w+}} [[ADD_RES_0]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: [[LOAD_RES_1:%\w+]] = OpLoad {{%\w+}} [[LOAD_1]]
- CHECK-NEXT: [[ADD_RES_1:%\w+]] = OpIAdd {{%\w+}} [[SUM_1]] [[LOAD_RES_1]]
- CHECK-NEXT: OpStore {{%\w+}} [[ADD_RES_1]]
- )";
- Match(checks, context.get());
- }
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10] a;
- int[10] b;
- int sum_0 = 0;
- int sum_1 = 0;
- // No loop-carried dependences, legal
- for (int i = 0; i < 10; i++) {
- sum_0 += a[i];
- }
- for (int j = 0; j < 10; j++) {
- sum_1 += b[j];
- }
- int total = sum_0 + sum_1;
- }
- */
- TEST_F(FusionLegalTest, IndependentReductionsBothLCSSA) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "sum_0"
- OpName %10 "sum_1"
- OpName %11 "i"
- OpName %25 "a"
- OpName %34 "j"
- OpName %42 "b"
- OpName %50 "total"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %18 = OpConstant %6 10
- %19 = OpTypeBool
- %21 = OpTypeInt 32 0
- %22 = OpConstant %21 10
- %23 = OpTypeArray %6 %22
- %24 = OpTypePointer Function %23
- %32 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %10 = OpVariable %7 Function
- %11 = OpVariable %7 Function
- %25 = OpVariable %24 Function
- %34 = OpVariable %7 Function
- %42 = OpVariable %24 Function
- %50 = OpVariable %7 Function
- OpStore %8 %9
- OpStore %10 %9
- OpStore %11 %9
- OpBranch %12
- %12 = OpLabel
- %57 = OpPhi %6 %9 %5 %30 %15
- %54 = OpPhi %6 %9 %5 %33 %15
- OpLoopMerge %14 %15 None
- OpBranch %16
- %16 = OpLabel
- %20 = OpSLessThan %19 %54 %18
- OpBranchConditional %20 %13 %14
- %13 = OpLabel
- %27 = OpAccessChain %7 %25 %54
- %28 = OpLoad %6 %27
- %30 = OpIAdd %6 %57 %28
- OpStore %8 %30
- OpBranch %15
- %15 = OpLabel
- %33 = OpIAdd %6 %54 %32
- OpStore %11 %33
- OpBranch %12
- %14 = OpLabel
- OpStore %34 %9
- OpBranch %35
- %35 = OpLabel
- %58 = OpPhi %6 %9 %14 %47 %38
- %55 = OpPhi %6 %9 %14 %49 %38
- OpLoopMerge %37 %38 None
- OpBranch %39
- %39 = OpLabel
- %41 = OpSLessThan %19 %55 %18
- OpBranchConditional %41 %36 %37
- %36 = OpLabel
- %44 = OpAccessChain %7 %42 %55
- %45 = OpLoad %6 %44
- %47 = OpIAdd %6 %58 %45
- OpStore %10 %47
- OpBranch %38
- %38 = OpLabel
- %49 = OpIAdd %6 %55 %32
- OpStore %34 %49
- OpBranch %35
- %37 = OpLabel
- %53 = OpIAdd %6 %57 %58
- OpStore %50 %53
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopUtils utils_0(context.get(), loops[0]);
- utils_0.MakeLoopClosedSSA();
- LoopUtils utils_1(context.get(), loops[1]);
- utils_1.MakeLoopClosedSSA();
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 1u);
- std::string checks = R"(
- CHECK: [[SUM_0:%\w+]] = OpPhi
- CHECK-NEXT: [[SUM_1:%\w+]] = OpPhi
- CHECK-NEXT: [[PHI:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: [[LOAD_RES_0:%\w+]] = OpLoad {{%\w+}} [[LOAD_0]]
- CHECK-NEXT: [[ADD_RES_0:%\w+]] = OpIAdd {{%\w+}} [[SUM_0]] [[LOAD_RES_0]]
- CHECK-NEXT: OpStore {{%\w+}} [[ADD_RES_0]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: [[LOAD_RES_1:%\w+]] = OpLoad {{%\w+}} [[LOAD_1]]
- CHECK-NEXT: [[ADD_RES_1:%\w+]] = OpIAdd {{%\w+}} [[SUM_1]] [[LOAD_RES_1]]
- CHECK-NEXT: OpStore {{%\w+}} [[ADD_RES_1]]
- )";
- Match(checks, context.get());
- }
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- void main() {
- int[10] a;
- int[10] b;
- int sum_0 = 0;
- // No loop-carried dependences, legal
- for (int i = 0; i < 10; i++) {
- sum_0 += a[i];
- }
- for (int j = 0; j < 10; j++) {
- a[j] = b[j];
- }
- }
- */
- TEST_F(FusionLegalTest, LoadStoreReductionAndNonLoopCarriedDependence) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "sum_0"
- OpName %10 "i"
- OpName %24 "a"
- OpName %33 "j"
- OpName %42 "b"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %17 = OpConstant %6 10
- %18 = OpTypeBool
- %20 = OpTypeInt 32 0
- %21 = OpConstant %20 10
- %22 = OpTypeArray %6 %21
- %23 = OpTypePointer Function %22
- %31 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %10 = OpVariable %7 Function
- %24 = OpVariable %23 Function
- %33 = OpVariable %7 Function
- %42 = OpVariable %23 Function
- OpStore %8 %9
- OpStore %10 %9
- OpBranch %11
- %11 = OpLabel
- %51 = OpPhi %6 %9 %5 %29 %14
- %49 = OpPhi %6 %9 %5 %32 %14
- OpLoopMerge %13 %14 None
- OpBranch %15
- %15 = OpLabel
- %19 = OpSLessThan %18 %49 %17
- OpBranchConditional %19 %12 %13
- %12 = OpLabel
- %26 = OpAccessChain %7 %24 %49
- %27 = OpLoad %6 %26
- %29 = OpIAdd %6 %51 %27
- OpStore %8 %29
- OpBranch %14
- %14 = OpLabel
- %32 = OpIAdd %6 %49 %31
- OpStore %10 %32
- OpBranch %11
- %13 = OpLabel
- OpStore %33 %9
- OpBranch %34
- %34 = OpLabel
- %50 = OpPhi %6 %9 %13 %48 %37
- OpLoopMerge %36 %37 None
- OpBranch %38
- %38 = OpLabel
- %40 = OpSLessThan %18 %50 %17
- OpBranchConditional %40 %35 %36
- %35 = OpLabel
- %44 = OpAccessChain %7 %42 %50
- %45 = OpLoad %6 %44
- %46 = OpAccessChain %7 %24 %50
- OpStore %46 %45
- OpBranch %37
- %37 = OpLabel
- %48 = OpIAdd %6 %50 %31
- OpStore %33 %48
- OpBranch %34
- %36 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- // TODO: Loop descriptor doesn't return induction variables but all OpPhi
- // in the header and LoopDependenceAnalysis falls over.
- // EXPECT_TRUE(fusion.IsLegal());
- // fusion.Fuse();
- }
- {
- // LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- // EXPECT_EQ(ld.NumLoops(), 1u);
- // std::string checks = R"(
- // CHECK: [[SUM_0:%\w+]] = OpPhi
- // CHECK-NEXT: [[PHI:%\w+]] = OpPhi
- // CHECK-NEXT: OpLoopMerge
- // CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- // CHECK-NEXT: [[LOAD_RES_0:%\w+]] = OpLoad {{%\w+}} [[LOAD_0]]
- // CHECK-NEXT: [[ADD_RES_0:%\w+]] = OpIAdd {{%\w+}} [[SUM_0]] [[LOAD_RES_0]]
- // CHECK-NEXT: OpStore {{%\w+}} [[ADD_RES_0]]
- // CHECK-NOT: OpPhi
- // CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- // CHECK-NEXT: [[LOAD_RES_1:%\w+]] = OpLoad {{%\w+}} [[LOAD_1]]
- // CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- // CHECK-NEXT: OpStore [[STORE_1]] [[LOAD_RES_1]]
- // )";
- // Match(checks, context.get());
- }
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- int x;
- void main() {
- int[10] a;
- int[10] b;
- // Legal.
- for (int i = 0; i < 10; i++) {
- x += a[i];
- }
- for (int j = 0; j < 10; j++) {
- b[j] = b[j]+1;
- }
- }
- */
- TEST_F(FusionLegalTest, ReductionAndNonLoopCarriedDependence) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %8 "i"
- OpName %20 "x"
- OpName %25 "a"
- OpName %34 "j"
- OpName %42 "b"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 10
- %17 = OpTypeBool
- %19 = OpTypePointer Private %6
- %20 = OpVariable %19 Private
- %21 = OpTypeInt 32 0
- %22 = OpConstant %21 10
- %23 = OpTypeArray %6 %22
- %24 = OpTypePointer Function %23
- %32 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %25 = OpVariable %24 Function
- %34 = OpVariable %7 Function
- %42 = OpVariable %24 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- %51 = OpPhi %6 %9 %5 %33 %13
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %51 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %27 = OpAccessChain %7 %25 %51
- %28 = OpLoad %6 %27
- %29 = OpLoad %6 %20
- %30 = OpIAdd %6 %29 %28
- OpStore %20 %30
- OpBranch %13
- %13 = OpLabel
- %33 = OpIAdd %6 %51 %32
- OpStore %8 %33
- OpBranch %10
- %12 = OpLabel
- OpStore %34 %9
- OpBranch %35
- %35 = OpLabel
- %52 = OpPhi %6 %9 %12 %50 %38
- OpLoopMerge %37 %38 None
- OpBranch %39
- %39 = OpLabel
- %41 = OpSLessThan %17 %52 %16
- OpBranchConditional %41 %36 %37
- %36 = OpLabel
- %45 = OpAccessChain %7 %42 %52
- %46 = OpLoad %6 %45
- %47 = OpIAdd %6 %46 %32
- %48 = OpAccessChain %7 %42 %52
- OpStore %48 %47
- OpBranch %38
- %38 = OpLabel
- %50 = OpIAdd %6 %52 %32
- OpStore %34 %50
- OpBranch %35
- %37 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 1u);
- std::string checks = R"(
- CHECK: OpName [[X:%\w+]] "x"
- CHECK: [[PHI:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[LOAD_0:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: [[LOAD_RES_0:%\w+]] = OpLoad {{%\w+}} [[LOAD_0]]
- CHECK-NEXT: [[X_LOAD:%\w+]] = OpLoad {{%\w+}} [[X]]
- CHECK-NEXT: [[ADD_RES_0:%\w+]] = OpIAdd {{%\w+}} [[X_LOAD]] [[LOAD_RES_0]]
- CHECK-NEXT: OpStore [[X]] [[ADD_RES_0]]
- CHECK-NOT: OpPhi
- CHECK: [[LOAD_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: {{%\w+}} = OpLoad {{%\w+}} [[LOAD_1]]
- CHECK: [[STORE_1:%\w+]] = OpAccessChain {{%\w+}} {{%\w+}} [[PHI]]
- CHECK-NEXT: OpStore [[STORE_1]]
- )";
- Match(checks, context.get());
- }
- }
- /*
- Generated from the following GLSL + --eliminate-local-multi-store
- #version 440 core
- struct TestStruct {
- int[10] a;
- int b;
- };
- void main() {
- TestStruct test_0;
- TestStruct test_1;
- TestStruct test_2;
- test_1.b = 2;
- for (int i = 0; i < 10; i++) {
- test_0.a[i] = i;
- }
- for (int j = 0; j < 10; j++) {
- test_2 = test_1;
- }
- }
- */
- TEST_F(FusionLegalTest, ArrayInStruct) {
- std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- OpName %10 "TestStruct"
- OpMemberName %10 0 "a"
- OpMemberName %10 1 "b"
- OpName %12 "test_1"
- OpName %17 "i"
- OpName %28 "test_0"
- OpName %34 "j"
- OpName %42 "test_2"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypeInt 32 0
- %8 = OpConstant %7 10
- %9 = OpTypeArray %6 %8
- %10 = OpTypeStruct %9 %6
- %11 = OpTypePointer Function %10
- %13 = OpConstant %6 1
- %14 = OpConstant %6 2
- %15 = OpTypePointer Function %6
- %18 = OpConstant %6 0
- %25 = OpConstant %6 10
- %26 = OpTypeBool
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %12 = OpVariable %11 Function
- %17 = OpVariable %15 Function
- %28 = OpVariable %11 Function
- %34 = OpVariable %15 Function
- %42 = OpVariable %11 Function
- %16 = OpAccessChain %15 %12 %13
- OpStore %16 %14
- OpStore %17 %18
- OpBranch %19
- %19 = OpLabel
- %46 = OpPhi %6 %18 %5 %33 %22
- OpLoopMerge %21 %22 None
- OpBranch %23
- %23 = OpLabel
- %27 = OpSLessThan %26 %46 %25
- OpBranchConditional %27 %20 %21
- %20 = OpLabel
- %31 = OpAccessChain %15 %28 %18 %46
- OpStore %31 %46
- OpBranch %22
- %22 = OpLabel
- %33 = OpIAdd %6 %46 %13
- OpStore %17 %33
- OpBranch %19
- %21 = OpLabel
- OpStore %34 %18
- OpBranch %35
- %35 = OpLabel
- %47 = OpPhi %6 %18 %21 %45 %38
- OpLoopMerge %37 %38 None
- OpBranch %39
- %39 = OpLabel
- %41 = OpSLessThan %26 %47 %25
- OpBranchConditional %41 %36 %37
- %36 = OpLabel
- %43 = OpLoad %10 %12
- OpStore %42 %43
- OpBranch %38
- %38 = OpLabel
- %45 = OpIAdd %6 %47 %13
- OpStore %34 %45
- OpBranch %35
- %37 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- Function& f = *module->begin();
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 2u);
- auto loops = ld.GetLoopsInBinaryLayoutOrder();
- LoopFusion fusion(context.get(), loops[0], loops[1]);
- EXPECT_TRUE(fusion.AreCompatible());
- EXPECT_TRUE(fusion.IsLegal());
- fusion.Fuse();
- }
- {
- LoopDescriptor& ld = *context->GetLoopDescriptor(&f);
- EXPECT_EQ(ld.NumLoops(), 1u);
- // clang-format off
- std::string checks = R"(
- CHECK: OpName [[TEST_1:%\w+]] "test_1"
- CHECK: OpName [[TEST_0:%\w+]] "test_0"
- CHECK: OpName [[TEST_2:%\w+]] "test_2"
- CHECK: [[PHI:%\w+]] = OpPhi
- CHECK-NEXT: OpLoopMerge
- CHECK: [[TEST_0_STORE:%\w+]] = OpAccessChain {{%\w+}} [[TEST_0]] {{%\w+}} {{%\w+}}
- CHECK-NEXT: OpStore [[TEST_0_STORE]] [[PHI]]
- CHECK-NOT: OpPhi
- CHECK: [[TEST_1_LOAD:%\w+]] = OpLoad {{%\w+}} [[TEST_1]]
- CHECK: OpStore [[TEST_2]] [[TEST_1_LOAD]]
- )";
- // clang-format on
- Match(checks, context.get());
- }
- }
- } // namespace
- } // namespace opt
- } // namespace spvtools
|