| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624 |
- // 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 "source/reduce/structured_loop_to_selection_reduction_opportunity_finder.h"
- #include "source/opt/build_module.h"
- #include "source/reduce/reduction_opportunity.h"
- #include "test/reduce/reduce_test_util.h"
- namespace spvtools {
- namespace reduce {
- namespace {
- TEST(StructuredLoopToSelectionReductionPassTest, LoopyShader1) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 100
- %17 = OpTypeBool
- %20 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %15 = OpLoad %6 %8
- %18 = OpSLessThan %17 %15 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- OpBranch %13
- %13 = OpLabel
- %19 = OpLoad %6 %8
- %21 = OpIAdd %6 %19 %20
- OpStore %8 %21
- OpBranch %10
- %12 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(1, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string after_op_0 = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 100
- %17 = OpTypeBool
- %20 = OpConstant %6 1
- %22 = OpConstantTrue %17
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- OpSelectionMerge %12 None
- OpBranchConditional %22 %14 %12
- %14 = OpLabel
- %15 = OpLoad %6 %8
- %18 = OpSLessThan %17 %15 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- OpBranch %12
- %13 = OpLabel
- %19 = OpLoad %6 %8
- %21 = OpIAdd %6 %19 %20
- OpStore %8 %21
- OpBranch %10
- %12 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, after_op_0, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest, LoopyShader2) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 100
- %17 = OpTypeBool
- %28 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %19 = OpVariable %7 Function
- %32 = OpVariable %7 Function
- %40 = OpVariable %7 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %15 = OpLoad %6 %8
- %18 = OpSLessThan %17 %15 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- OpStore %19 %9
- OpBranch %20
- %20 = OpLabel
- OpLoopMerge %22 %23 None
- OpBranch %24
- %24 = OpLabel
- %25 = OpLoad %6 %19
- %26 = OpSLessThan %17 %25 %16
- OpBranchConditional %26 %21 %22
- %21 = OpLabel
- OpBranch %23
- %23 = OpLabel
- %27 = OpLoad %6 %19
- %29 = OpIAdd %6 %27 %28
- OpStore %19 %29
- OpBranch %20
- %22 = OpLabel
- OpBranch %13
- %13 = OpLabel
- %30 = OpLoad %6 %8
- %31 = OpIAdd %6 %30 %28
- OpStore %8 %31
- OpBranch %10
- %12 = OpLabel
- OpStore %32 %9
- OpBranch %33
- %33 = OpLabel
- OpLoopMerge %35 %36 None
- OpBranch %37
- %37 = OpLabel
- %38 = OpLoad %6 %32
- %39 = OpSLessThan %17 %38 %16
- OpBranchConditional %39 %34 %35
- %34 = OpLabel
- OpStore %40 %9
- OpBranch %41
- %41 = OpLabel
- OpLoopMerge %43 %44 None
- OpBranch %45
- %45 = OpLabel
- %46 = OpLoad %6 %40
- %47 = OpSLessThan %17 %46 %16
- OpBranchConditional %47 %42 %43
- %42 = OpLabel
- OpBranch %44
- %44 = OpLabel
- %48 = OpLoad %6 %40
- %49 = OpIAdd %6 %48 %28
- OpStore %40 %49
- OpBranch %41
- %43 = OpLabel
- OpBranch %36
- %36 = OpLabel
- %50 = OpLoad %6 %32
- %51 = OpIAdd %6 %50 %28
- OpStore %32 %51
- OpBranch %33
- %35 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(4, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string after_op_0 = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 100
- %17 = OpTypeBool
- %28 = OpConstant %6 1
- %52 = OpConstantTrue %17
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %19 = OpVariable %7 Function
- %32 = OpVariable %7 Function
- %40 = OpVariable %7 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- OpSelectionMerge %12 None
- OpBranchConditional %52 %14 %12
- %14 = OpLabel
- %15 = OpLoad %6 %8
- %18 = OpSLessThan %17 %15 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- OpStore %19 %9
- OpBranch %20
- %20 = OpLabel
- OpLoopMerge %22 %23 None
- OpBranch %24
- %24 = OpLabel
- %25 = OpLoad %6 %19
- %26 = OpSLessThan %17 %25 %16
- OpBranchConditional %26 %21 %22
- %21 = OpLabel
- OpBranch %23
- %23 = OpLabel
- %27 = OpLoad %6 %19
- %29 = OpIAdd %6 %27 %28
- OpStore %19 %29
- OpBranch %20
- %22 = OpLabel
- OpBranch %12
- %13 = OpLabel
- %30 = OpLoad %6 %8
- %31 = OpIAdd %6 %30 %28
- OpStore %8 %31
- OpBranch %10
- %12 = OpLabel
- OpStore %32 %9
- OpBranch %33
- %33 = OpLabel
- OpLoopMerge %35 %36 None
- OpBranch %37
- %37 = OpLabel
- %38 = OpLoad %6 %32
- %39 = OpSLessThan %17 %38 %16
- OpBranchConditional %39 %34 %35
- %34 = OpLabel
- OpStore %40 %9
- OpBranch %41
- %41 = OpLabel
- OpLoopMerge %43 %44 None
- OpBranch %45
- %45 = OpLabel
- %46 = OpLoad %6 %40
- %47 = OpSLessThan %17 %46 %16
- OpBranchConditional %47 %42 %43
- %42 = OpLabel
- OpBranch %44
- %44 = OpLabel
- %48 = OpLoad %6 %40
- %49 = OpIAdd %6 %48 %28
- OpStore %40 %49
- OpBranch %41
- %43 = OpLabel
- OpBranch %36
- %36 = OpLabel
- %50 = OpLoad %6 %32
- %51 = OpIAdd %6 %50 %28
- OpStore %32 %51
- OpBranch %33
- %35 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, after_op_0, context.get());
- ASSERT_TRUE(ops[1]->PreconditionHolds());
- ops[1]->TryToApply();
- CheckValid(env, context.get());
- std::string after_op_1 = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 100
- %17 = OpTypeBool
- %28 = OpConstant %6 1
- %52 = OpConstantTrue %17
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %19 = OpVariable %7 Function
- %32 = OpVariable %7 Function
- %40 = OpVariable %7 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- OpSelectionMerge %12 None
- OpBranchConditional %52 %14 %12
- %14 = OpLabel
- %15 = OpLoad %6 %8
- %18 = OpSLessThan %17 %15 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- OpStore %19 %9
- OpBranch %20
- %20 = OpLabel
- OpSelectionMerge %22 None
- OpBranchConditional %52 %24 %22
- %24 = OpLabel
- %25 = OpLoad %6 %19
- %26 = OpSLessThan %17 %25 %16
- OpBranchConditional %26 %21 %22
- %21 = OpLabel
- OpBranch %22
- %23 = OpLabel
- %27 = OpLoad %6 %19
- %29 = OpIAdd %6 %27 %28
- OpStore %19 %29
- OpBranch %20
- %22 = OpLabel
- OpBranch %12
- %13 = OpLabel
- %30 = OpLoad %6 %8
- %31 = OpIAdd %6 %30 %28
- OpStore %8 %31
- OpBranch %10
- %12 = OpLabel
- OpStore %32 %9
- OpBranch %33
- %33 = OpLabel
- OpLoopMerge %35 %36 None
- OpBranch %37
- %37 = OpLabel
- %38 = OpLoad %6 %32
- %39 = OpSLessThan %17 %38 %16
- OpBranchConditional %39 %34 %35
- %34 = OpLabel
- OpStore %40 %9
- OpBranch %41
- %41 = OpLabel
- OpLoopMerge %43 %44 None
- OpBranch %45
- %45 = OpLabel
- %46 = OpLoad %6 %40
- %47 = OpSLessThan %17 %46 %16
- OpBranchConditional %47 %42 %43
- %42 = OpLabel
- OpBranch %44
- %44 = OpLabel
- %48 = OpLoad %6 %40
- %49 = OpIAdd %6 %48 %28
- OpStore %40 %49
- OpBranch %41
- %43 = OpLabel
- OpBranch %36
- %36 = OpLabel
- %50 = OpLoad %6 %32
- %51 = OpIAdd %6 %50 %28
- OpStore %32 %51
- OpBranch %33
- %35 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, after_op_1, context.get());
- ASSERT_TRUE(ops[2]->PreconditionHolds());
- ops[2]->TryToApply();
- CheckValid(env, context.get());
- std::string after_op_2 = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 100
- %17 = OpTypeBool
- %28 = OpConstant %6 1
- %52 = OpConstantTrue %17
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %19 = OpVariable %7 Function
- %32 = OpVariable %7 Function
- %40 = OpVariable %7 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- OpSelectionMerge %12 None
- OpBranchConditional %52 %14 %12
- %14 = OpLabel
- %15 = OpLoad %6 %8
- %18 = OpSLessThan %17 %15 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- OpStore %19 %9
- OpBranch %20
- %20 = OpLabel
- OpSelectionMerge %22 None
- OpBranchConditional %52 %24 %22
- %24 = OpLabel
- %25 = OpLoad %6 %19
- %26 = OpSLessThan %17 %25 %16
- OpBranchConditional %26 %21 %22
- %21 = OpLabel
- OpBranch %22
- %23 = OpLabel
- %27 = OpLoad %6 %19
- %29 = OpIAdd %6 %27 %28
- OpStore %19 %29
- OpBranch %20
- %22 = OpLabel
- OpBranch %12
- %13 = OpLabel
- %30 = OpLoad %6 %8
- %31 = OpIAdd %6 %30 %28
- OpStore %8 %31
- OpBranch %10
- %12 = OpLabel
- OpStore %32 %9
- OpBranch %33
- %33 = OpLabel
- OpSelectionMerge %35 None
- OpBranchConditional %52 %37 %35
- %37 = OpLabel
- %38 = OpLoad %6 %32
- %39 = OpSLessThan %17 %38 %16
- OpBranchConditional %39 %34 %35
- %34 = OpLabel
- OpStore %40 %9
- OpBranch %41
- %41 = OpLabel
- OpLoopMerge %43 %44 None
- OpBranch %45
- %45 = OpLabel
- %46 = OpLoad %6 %40
- %47 = OpSLessThan %17 %46 %16
- OpBranchConditional %47 %42 %43
- %42 = OpLabel
- OpBranch %44
- %44 = OpLabel
- %48 = OpLoad %6 %40
- %49 = OpIAdd %6 %48 %28
- OpStore %40 %49
- OpBranch %41
- %43 = OpLabel
- OpBranch %35
- %36 = OpLabel
- %50 = OpLoad %6 %32
- %51 = OpIAdd %6 %50 %28
- OpStore %32 %51
- OpBranch %33
- %35 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, after_op_2, context.get());
- ASSERT_TRUE(ops[3]->PreconditionHolds());
- ops[3]->TryToApply();
- CheckValid(env, context.get());
- std::string after_op_3 = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 100
- %17 = OpTypeBool
- %28 = OpConstant %6 1
- %52 = OpConstantTrue %17
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- %19 = OpVariable %7 Function
- %32 = OpVariable %7 Function
- %40 = OpVariable %7 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- OpSelectionMerge %12 None
- OpBranchConditional %52 %14 %12
- %14 = OpLabel
- %15 = OpLoad %6 %8
- %18 = OpSLessThan %17 %15 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- OpStore %19 %9
- OpBranch %20
- %20 = OpLabel
- OpSelectionMerge %22 None
- OpBranchConditional %52 %24 %22
- %24 = OpLabel
- %25 = OpLoad %6 %19
- %26 = OpSLessThan %17 %25 %16
- OpBranchConditional %26 %21 %22
- %21 = OpLabel
- OpBranch %22
- %23 = OpLabel
- %27 = OpLoad %6 %19
- %29 = OpIAdd %6 %27 %28
- OpStore %19 %29
- OpBranch %20
- %22 = OpLabel
- OpBranch %12
- %13 = OpLabel
- %30 = OpLoad %6 %8
- %31 = OpIAdd %6 %30 %28
- OpStore %8 %31
- OpBranch %10
- %12 = OpLabel
- OpStore %32 %9
- OpBranch %33
- %33 = OpLabel
- OpSelectionMerge %35 None
- OpBranchConditional %52 %37 %35
- %37 = OpLabel
- %38 = OpLoad %6 %32
- %39 = OpSLessThan %17 %38 %16
- OpBranchConditional %39 %34 %35
- %34 = OpLabel
- OpStore %40 %9
- OpBranch %41
- %41 = OpLabel
- OpSelectionMerge %43 None
- OpBranchConditional %52 %45 %43
- %45 = OpLabel
- %46 = OpLoad %6 %40
- %47 = OpSLessThan %17 %46 %16
- OpBranchConditional %47 %42 %43
- %42 = OpLabel
- OpBranch %43
- %44 = OpLabel
- %48 = OpLoad %6 %40
- %49 = OpIAdd %6 %48 %28
- OpStore %40 %49
- OpBranch %41
- %43 = OpLabel
- OpBranch %35
- %36 = OpLabel
- %50 = OpLoad %6 %32
- %51 = OpIAdd %6 %50 %28
- OpStore %32 %51
- OpBranch %33
- %35 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, after_op_3, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest, LoopyShader3) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 10
- %16 = OpConstant %6 0
- %17 = OpTypeBool
- %20 = OpConstant %6 1
- %23 = OpConstant %6 3
- %40 = OpConstant %6 5
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %8 = OpVariable %7 Function
- OpStore %8 %9
- OpBranch %10
- %10 = OpLabel
- OpLoopMerge %12 %13 None
- OpBranch %14
- %14 = OpLabel
- %15 = OpLoad %6 %8
- %18 = OpSGreaterThan %17 %15 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %19 = OpLoad %6 %8
- %21 = OpISub %6 %19 %20
- OpStore %8 %21
- %22 = OpLoad %6 %8
- %24 = OpSLessThan %17 %22 %23
- OpSelectionMerge %26 None
- OpBranchConditional %24 %25 %26
- %25 = OpLabel
- OpBranch %13
- %26 = OpLabel
- OpBranch %28
- %28 = OpLabel
- OpLoopMerge %30 %31 None
- OpBranch %29
- %29 = OpLabel
- %32 = OpLoad %6 %8
- %33 = OpISub %6 %32 %20
- OpStore %8 %33
- %34 = OpLoad %6 %8
- %35 = OpIEqual %17 %34 %20
- OpSelectionMerge %37 None
- OpBranchConditional %35 %36 %37
- %36 = OpLabel
- OpReturn ; This return spoils everything: it means the merge does not post-dominate the header.
- %37 = OpLabel
- OpBranch %31
- %31 = OpLabel
- %39 = OpLoad %6 %8
- %41 = OpSGreaterThan %17 %39 %40
- OpBranchConditional %41 %28 %30
- %30 = OpLabel
- OpBranch %13
- %13 = OpLabel
- OpBranch %10
- %12 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(0, ops.size());
- }
- TEST(StructuredLoopToSelectionReductionPassTest, LoopyShader4) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %8 = OpTypeFunction %6 %7
- %13 = OpConstant %6 0
- %22 = OpTypeBool
- %25 = OpConstant %6 1
- %39 = OpConstant %6 100
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %45 = OpVariable %7 Function
- %46 = OpVariable %7 Function
- %47 = OpVariable %7 Function
- %32 = OpVariable %7 Function
- %42 = OpVariable %7 Function
- OpStore %32 %13
- OpBranch %33
- %33 = OpLabel
- OpLoopMerge %35 %36 None
- OpBranch %37
- %37 = OpLabel
- %38 = OpLoad %6 %32
- %40 = OpSLessThan %22 %38 %39
- OpBranchConditional %40 %34 %35
- %34 = OpLabel
- OpBranch %36
- %36 = OpLabel
- %41 = OpLoad %6 %32
- OpStore %42 %25
- OpStore %45 %13
- OpStore %46 %13
- OpBranch %48
- %48 = OpLabel
- OpLoopMerge %49 %50 None
- OpBranch %51
- %51 = OpLabel
- %52 = OpLoad %6 %46
- %53 = OpLoad %6 %42
- %54 = OpSLessThan %22 %52 %53
- OpBranchConditional %54 %55 %49
- %55 = OpLabel
- %56 = OpLoad %6 %45
- %57 = OpIAdd %6 %56 %25
- OpStore %45 %57
- OpBranch %50
- %50 = OpLabel
- %58 = OpLoad %6 %46
- %59 = OpIAdd %6 %58 %25
- OpStore %46 %59
- OpBranch %48
- %49 = OpLabel
- %60 = OpLoad %6 %45
- OpStore %47 %60
- %43 = OpLoad %6 %47
- %44 = OpIAdd %6 %41 %43
- OpStore %32 %44
- OpBranch %33
- %35 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- // Initially there are two opportunities.
- ASSERT_EQ(2, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string after_op_0 = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %8 = OpTypeFunction %6 %7
- %13 = OpConstant %6 0
- %22 = OpTypeBool
- %25 = OpConstant %6 1
- %39 = OpConstant %6 100
- %61 = OpConstantTrue %22
- %62 = OpUndef %6
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %45 = OpVariable %7 Function
- %46 = OpVariable %7 Function
- %47 = OpVariable %7 Function
- %32 = OpVariable %7 Function
- %42 = OpVariable %7 Function
- OpStore %32 %13
- OpBranch %33
- %33 = OpLabel
- OpSelectionMerge %35 None
- OpBranchConditional %61 %37 %35
- %37 = OpLabel
- %38 = OpLoad %6 %32
- %40 = OpSLessThan %22 %38 %39
- OpBranchConditional %40 %34 %35
- %34 = OpLabel
- OpBranch %35
- %36 = OpLabel
- %41 = OpLoad %6 %32
- OpStore %42 %25
- OpStore %45 %13
- OpStore %46 %13
- OpBranch %48
- %48 = OpLabel
- OpLoopMerge %49 %50 None
- OpBranch %51
- %51 = OpLabel
- %52 = OpLoad %6 %46
- %53 = OpLoad %6 %42
- %54 = OpSLessThan %22 %52 %53
- OpBranchConditional %54 %55 %49
- %55 = OpLabel
- %56 = OpLoad %6 %45
- %57 = OpIAdd %6 %56 %25
- OpStore %45 %57
- OpBranch %50
- %50 = OpLabel
- %58 = OpLoad %6 %46
- %59 = OpIAdd %6 %58 %25
- OpStore %46 %59
- OpBranch %48
- %49 = OpLabel
- %60 = OpLoad %6 %45
- OpStore %47 %60
- %43 = OpLoad %6 %47
- %44 = OpIAdd %6 %62 %43
- OpStore %32 %44
- OpBranch %33
- %35 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, after_op_0, context.get());
- // Applying the first opportunity has killed the second opportunity, because
- // there was a loop embedded in the continue target of the loop we have just
- // eliminated; the continue-embedded loop is now unreachable.
- ASSERT_FALSE(ops[1]->PreconditionHolds());
- }
- TEST(StructuredLoopToSelectionReductionPassTest, ConditionalBreak1) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- OpName %4 "main"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %10 = OpTypeBool
- %11 = OpConstantFalse %10
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpBranch %6
- %6 = OpLabel
- OpLoopMerge %8 %9 None
- OpBranch %7
- %7 = OpLabel
- OpSelectionMerge %13 None
- OpBranchConditional %11 %12 %13
- %12 = OpLabel
- OpBranch %8
- %13 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpBranchConditional %11 %6 %8
- %8 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(1, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string after_op_0 = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- OpName %4 "main"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %10 = OpTypeBool
- %11 = OpConstantFalse %10
- %14 = OpConstantTrue %10
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpBranch %6
- %6 = OpLabel
- OpSelectionMerge %8 None
- OpBranchConditional %14 %7 %8
- %7 = OpLabel
- OpSelectionMerge %13 None
- OpBranchConditional %11 %12 %13
- %12 = OpLabel
- OpBranch %13
- %13 = OpLabel
- OpBranch %8
- %9 = OpLabel
- OpBranchConditional %11 %6 %8
- %8 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, after_op_0, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest, ConditionalBreak2) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- OpName %4 "main"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %10 = OpTypeBool
- %11 = OpConstantFalse %10
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpBranch %6
- %6 = OpLabel
- OpLoopMerge %8 %9 None
- OpBranch %7
- %7 = OpLabel
- OpSelectionMerge %13 None
- OpBranchConditional %11 %8 %13
- %13 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpBranchConditional %11 %6 %8
- %8 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(1, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string after_op_0 = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- OpName %4 "main"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %10 = OpTypeBool
- %11 = OpConstantFalse %10
- %14 = OpConstantTrue %10
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpBranch %6
- %6 = OpLabel
- OpSelectionMerge %8 None
- OpBranchConditional %14 %7 %8
- %7 = OpLabel
- OpSelectionMerge %13 None
- OpBranchConditional %11 %13 %13
- %13 = OpLabel
- OpBranch %8
- %9 = OpLabel
- OpBranchConditional %11 %6 %8
- %8 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, after_op_0, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest, UnconditionalBreak) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- OpName %4 "main"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpBranch %6
- %6 = OpLabel
- OpLoopMerge %8 %9 None
- OpBranch %7
- %7 = OpLabel
- OpBranch %8
- %9 = OpLabel
- OpBranch %6
- %8 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(1, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string after_op_0 = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- OpName %4 "main"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %10 = OpTypeBool
- %11 = OpConstantTrue %10
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpBranch %6
- %6 = OpLabel
- OpSelectionMerge %8 None
- OpBranchConditional %11 %7 %8
- %7 = OpLabel
- OpBranch %8
- %9 = OpLabel
- OpBranch %6
- %8 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, after_op_0, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest, Complex) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- OpMemberDecorate %4 0 Offset 0
- OpMemberDecorate %4 1 Offset 4
- OpMemberDecorate %4 2 Offset 8
- OpMemberDecorate %4 3 Offset 12
- OpDecorate %4 Block
- OpDecorate %5 DescriptorSet 0
- OpDecorate %5 Binding 0
- OpDecorate %3 Location 0
- %6 = OpTypeVoid
- %7 = OpTypeFunction %6
- %8 = OpTypeBool
- %9 = OpTypePointer Function %8
- %10 = OpTypeInt 32 1
- %4 = OpTypeStruct %10 %10 %10 %10
- %11 = OpTypePointer Uniform %4
- %5 = OpVariable %11 Uniform
- %12 = OpConstant %10 0
- %13 = OpTypePointer Uniform %10
- %14 = OpTypeInt 32 0
- %15 = OpConstant %14 0
- %16 = OpConstant %10 1
- %17 = OpConstant %10 2
- %18 = OpConstant %10 3
- %19 = OpTypePointer Function %10
- %20 = OpConstantFalse %8
- %21 = OpTypeFloat 32
- %22 = OpTypeVector %21 4
- %23 = OpTypePointer Output %22
- %3 = OpVariable %23 Output
- %2 = OpFunction %6 None %7
- %24 = OpLabel
- %25 = OpVariable %9 Function
- %26 = OpVariable %9 Function
- %27 = OpVariable %9 Function
- %28 = OpVariable %9 Function
- %29 = OpVariable %9 Function
- %30 = OpVariable %19 Function
- %31 = OpAccessChain %13 %5 %12
- %32 = OpLoad %10 %31
- %33 = OpINotEqual %8 %32 %15
- OpStore %25 %33
- %34 = OpAccessChain %13 %5 %16
- %35 = OpLoad %10 %34
- %36 = OpINotEqual %8 %35 %15
- OpStore %26 %36
- %37 = OpAccessChain %13 %5 %17
- %38 = OpLoad %10 %37
- %39 = OpINotEqual %8 %38 %15
- OpStore %27 %39
- %40 = OpAccessChain %13 %5 %18
- %41 = OpLoad %10 %40
- %42 = OpINotEqual %8 %41 %15
- OpStore %28 %42
- %43 = OpLoad %8 %25
- OpStore %29 %43
- OpStore %30 %12
- OpBranch %44
- %44 = OpLabel
- OpLoopMerge %45 %46 None
- OpBranch %47
- %47 = OpLabel
- %48 = OpLoad %8 %29
- OpBranchConditional %48 %49 %45
- %49 = OpLabel
- %50 = OpLoad %8 %25
- OpSelectionMerge %51 None
- OpBranchConditional %50 %52 %51
- %52 = OpLabel
- %53 = OpLoad %8 %26
- OpStore %29 %53
- %54 = OpLoad %10 %30
- %55 = OpIAdd %10 %54 %16
- OpStore %30 %55
- OpBranch %51
- %51 = OpLabel
- %56 = OpLoad %8 %26
- OpSelectionMerge %57 None
- OpBranchConditional %56 %58 %57
- %58 = OpLabel
- %59 = OpLoad %10 %30
- %60 = OpIAdd %10 %59 %16
- OpStore %30 %60
- %61 = OpLoad %8 %29
- %62 = OpLoad %8 %25
- %63 = OpLogicalOr %8 %61 %62
- OpStore %29 %63
- %64 = OpLoad %8 %27
- OpSelectionMerge %65 None
- OpBranchConditional %64 %66 %65
- %66 = OpLabel
- %67 = OpLoad %10 %30
- %68 = OpIAdd %10 %67 %17
- OpStore %30 %68
- %69 = OpLoad %8 %29
- %70 = OpLogicalNot %8 %69
- OpStore %29 %70
- OpBranch %46
- %65 = OpLabel
- %71 = OpLoad %8 %29
- %72 = OpLogicalOr %8 %71 %20
- OpStore %29 %72
- OpBranch %46
- %57 = OpLabel
- OpBranch %73
- %73 = OpLabel
- OpLoopMerge %74 %75 None
- OpBranch %76
- %76 = OpLabel
- %77 = OpLoad %8 %28
- OpSelectionMerge %78 None
- OpBranchConditional %77 %79 %80
- %79 = OpLabel
- %81 = OpLoad %10 %30
- OpSelectionMerge %82 None
- OpSwitch %81 %83 1 %84 2 %85
- %83 = OpLabel
- OpBranch %82
- %84 = OpLabel
- %86 = OpLoad %8 %29
- %87 = OpSelect %10 %86 %16 %17
- %88 = OpLoad %10 %30
- %89 = OpIAdd %10 %88 %87
- OpStore %30 %89
- OpBranch %82
- %85 = OpLabel
- OpBranch %75
- %82 = OpLabel
- %90 = OpLoad %8 %27
- OpSelectionMerge %91 None
- OpBranchConditional %90 %92 %91
- %92 = OpLabel
- OpBranch %75
- %91 = OpLabel
- OpBranch %78
- %80 = OpLabel
- OpBranch %74
- %78 = OpLabel
- OpBranch %75
- %75 = OpLabel
- %93 = OpLoad %8 %29
- OpBranchConditional %93 %73 %74
- %74 = OpLabel
- OpBranch %46
- %46 = OpLabel
- OpBranch %44
- %45 = OpLabel
- %94 = OpLoad %10 %30
- %95 = OpConvertSToF %21 %94
- %96 = OpCompositeConstruct %22 %95 %95 %95 %95
- OpStore %3 %96
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(2, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string after_op_0 = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- OpMemberDecorate %4 0 Offset 0
- OpMemberDecorate %4 1 Offset 4
- OpMemberDecorate %4 2 Offset 8
- OpMemberDecorate %4 3 Offset 12
- OpDecorate %4 Block
- OpDecorate %5 DescriptorSet 0
- OpDecorate %5 Binding 0
- OpDecorate %3 Location 0
- %6 = OpTypeVoid
- %7 = OpTypeFunction %6
- %8 = OpTypeBool
- %9 = OpTypePointer Function %8
- %10 = OpTypeInt 32 1
- %4 = OpTypeStruct %10 %10 %10 %10
- %11 = OpTypePointer Uniform %4
- %5 = OpVariable %11 Uniform
- %12 = OpConstant %10 0
- %13 = OpTypePointer Uniform %10
- %14 = OpTypeInt 32 0
- %15 = OpConstant %14 0
- %16 = OpConstant %10 1
- %17 = OpConstant %10 2
- %18 = OpConstant %10 3
- %19 = OpTypePointer Function %10
- %20 = OpConstantFalse %8
- %21 = OpTypeFloat 32
- %22 = OpTypeVector %21 4
- %23 = OpTypePointer Output %22
- %3 = OpVariable %23 Output
- %97 = OpConstantTrue %8
- %2 = OpFunction %6 None %7
- %24 = OpLabel
- %25 = OpVariable %9 Function
- %26 = OpVariable %9 Function
- %27 = OpVariable %9 Function
- %28 = OpVariable %9 Function
- %29 = OpVariable %9 Function
- %30 = OpVariable %19 Function
- %31 = OpAccessChain %13 %5 %12
- %32 = OpLoad %10 %31
- %33 = OpINotEqual %8 %32 %15
- OpStore %25 %33
- %34 = OpAccessChain %13 %5 %16
- %35 = OpLoad %10 %34
- %36 = OpINotEqual %8 %35 %15
- OpStore %26 %36
- %37 = OpAccessChain %13 %5 %17
- %38 = OpLoad %10 %37
- %39 = OpINotEqual %8 %38 %15
- OpStore %27 %39
- %40 = OpAccessChain %13 %5 %18
- %41 = OpLoad %10 %40
- %42 = OpINotEqual %8 %41 %15
- OpStore %28 %42
- %43 = OpLoad %8 %25
- OpStore %29 %43
- OpStore %30 %12
- OpBranch %44
- %44 = OpLabel
- OpSelectionMerge %45 None ; Was OpLoopMerge %45 %46 None
- OpBranchConditional %97 %47 %45 ; Was OpBranch %47
- %47 = OpLabel
- %48 = OpLoad %8 %29
- OpBranchConditional %48 %49 %45
- %49 = OpLabel
- %50 = OpLoad %8 %25
- OpSelectionMerge %51 None
- OpBranchConditional %50 %52 %51
- %52 = OpLabel
- %53 = OpLoad %8 %26
- OpStore %29 %53
- %54 = OpLoad %10 %30
- %55 = OpIAdd %10 %54 %16
- OpStore %30 %55
- OpBranch %51
- %51 = OpLabel
- %56 = OpLoad %8 %26
- OpSelectionMerge %57 None
- OpBranchConditional %56 %58 %57
- %58 = OpLabel
- %59 = OpLoad %10 %30
- %60 = OpIAdd %10 %59 %16
- OpStore %30 %60
- %61 = OpLoad %8 %29
- %62 = OpLoad %8 %25
- %63 = OpLogicalOr %8 %61 %62
- OpStore %29 %63
- %64 = OpLoad %8 %27
- OpSelectionMerge %65 None
- OpBranchConditional %64 %66 %65
- %66 = OpLabel
- %67 = OpLoad %10 %30
- %68 = OpIAdd %10 %67 %17
- OpStore %30 %68
- %69 = OpLoad %8 %29
- %70 = OpLogicalNot %8 %69
- OpStore %29 %70
- OpBranch %65 ; Was OpBranch %46
- %65 = OpLabel
- %71 = OpLoad %8 %29
- %72 = OpLogicalOr %8 %71 %20
- OpStore %29 %72
- OpBranch %57 ; Was OpBranch %46
- %57 = OpLabel
- OpBranch %73
- %73 = OpLabel
- OpLoopMerge %74 %75 None
- OpBranch %76
- %76 = OpLabel
- %77 = OpLoad %8 %28
- OpSelectionMerge %78 None
- OpBranchConditional %77 %79 %80
- %79 = OpLabel
- %81 = OpLoad %10 %30
- OpSelectionMerge %82 None
- OpSwitch %81 %83 1 %84 2 %85
- %83 = OpLabel
- OpBranch %82
- %84 = OpLabel
- %86 = OpLoad %8 %29
- %87 = OpSelect %10 %86 %16 %17
- %88 = OpLoad %10 %30
- %89 = OpIAdd %10 %88 %87
- OpStore %30 %89
- OpBranch %82
- %85 = OpLabel
- OpBranch %75
- %82 = OpLabel
- %90 = OpLoad %8 %27
- OpSelectionMerge %91 None
- OpBranchConditional %90 %92 %91
- %92 = OpLabel
- OpBranch %75
- %91 = OpLabel
- OpBranch %78
- %80 = OpLabel
- OpBranch %74
- %78 = OpLabel
- OpBranch %75
- %75 = OpLabel
- %93 = OpLoad %8 %29
- OpBranchConditional %93 %73 %74
- %74 = OpLabel
- OpBranch %45 ; Was OpBranch %46
- %46 = OpLabel
- OpBranch %44
- %45 = OpLabel
- %94 = OpLoad %10 %30
- %95 = OpConvertSToF %21 %94
- %96 = OpCompositeConstruct %22 %95 %95 %95 %95
- OpStore %3 %96
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, after_op_0, context.get());
- ASSERT_TRUE(ops[1]->PreconditionHolds());
- ops[1]->TryToApply();
- CheckValid(env, context.get());
- std::string after_op_1 = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- OpMemberDecorate %4 0 Offset 0
- OpMemberDecorate %4 1 Offset 4
- OpMemberDecorate %4 2 Offset 8
- OpMemberDecorate %4 3 Offset 12
- OpDecorate %4 Block
- OpDecorate %5 DescriptorSet 0
- OpDecorate %5 Binding 0
- OpDecorate %3 Location 0
- %6 = OpTypeVoid
- %7 = OpTypeFunction %6
- %8 = OpTypeBool
- %9 = OpTypePointer Function %8
- %10 = OpTypeInt 32 1
- %4 = OpTypeStruct %10 %10 %10 %10
- %11 = OpTypePointer Uniform %4
- %5 = OpVariable %11 Uniform
- %12 = OpConstant %10 0
- %13 = OpTypePointer Uniform %10
- %14 = OpTypeInt 32 0
- %15 = OpConstant %14 0
- %16 = OpConstant %10 1
- %17 = OpConstant %10 2
- %18 = OpConstant %10 3
- %19 = OpTypePointer Function %10
- %20 = OpConstantFalse %8
- %21 = OpTypeFloat 32
- %22 = OpTypeVector %21 4
- %23 = OpTypePointer Output %22
- %3 = OpVariable %23 Output
- %97 = OpConstantTrue %8
- %2 = OpFunction %6 None %7
- %24 = OpLabel
- %25 = OpVariable %9 Function
- %26 = OpVariable %9 Function
- %27 = OpVariable %9 Function
- %28 = OpVariable %9 Function
- %29 = OpVariable %9 Function
- %30 = OpVariable %19 Function
- %31 = OpAccessChain %13 %5 %12
- %32 = OpLoad %10 %31
- %33 = OpINotEqual %8 %32 %15
- OpStore %25 %33
- %34 = OpAccessChain %13 %5 %16
- %35 = OpLoad %10 %34
- %36 = OpINotEqual %8 %35 %15
- OpStore %26 %36
- %37 = OpAccessChain %13 %5 %17
- %38 = OpLoad %10 %37
- %39 = OpINotEqual %8 %38 %15
- OpStore %27 %39
- %40 = OpAccessChain %13 %5 %18
- %41 = OpLoad %10 %40
- %42 = OpINotEqual %8 %41 %15
- OpStore %28 %42
- %43 = OpLoad %8 %25
- OpStore %29 %43
- OpStore %30 %12
- OpBranch %44
- %44 = OpLabel
- OpSelectionMerge %45 None ; Was OpLoopMerge %45 %46 None
- OpBranchConditional %97 %47 %45 ; Was OpBranch %47
- %47 = OpLabel
- %48 = OpLoad %8 %29
- OpBranchConditional %48 %49 %45
- %49 = OpLabel
- %50 = OpLoad %8 %25
- OpSelectionMerge %51 None
- OpBranchConditional %50 %52 %51
- %52 = OpLabel
- %53 = OpLoad %8 %26
- OpStore %29 %53
- %54 = OpLoad %10 %30
- %55 = OpIAdd %10 %54 %16
- OpStore %30 %55
- OpBranch %51
- %51 = OpLabel
- %56 = OpLoad %8 %26
- OpSelectionMerge %57 None
- OpBranchConditional %56 %58 %57
- %58 = OpLabel
- %59 = OpLoad %10 %30
- %60 = OpIAdd %10 %59 %16
- OpStore %30 %60
- %61 = OpLoad %8 %29
- %62 = OpLoad %8 %25
- %63 = OpLogicalOr %8 %61 %62
- OpStore %29 %63
- %64 = OpLoad %8 %27
- OpSelectionMerge %65 None
- OpBranchConditional %64 %66 %65
- %66 = OpLabel
- %67 = OpLoad %10 %30
- %68 = OpIAdd %10 %67 %17
- OpStore %30 %68
- %69 = OpLoad %8 %29
- %70 = OpLogicalNot %8 %69
- OpStore %29 %70
- OpBranch %65 ; Was OpBranch %46
- %65 = OpLabel
- %71 = OpLoad %8 %29
- %72 = OpLogicalOr %8 %71 %20
- OpStore %29 %72
- OpBranch %57 ; Was OpBranch %46
- %57 = OpLabel
- OpBranch %73
- %73 = OpLabel
- OpSelectionMerge %74 None ; Was OpLoopMerge %74 %75 None
- OpBranchConditional %97 %76 %74 ; Was OpBranch %76
- %76 = OpLabel
- %77 = OpLoad %8 %28
- OpSelectionMerge %78 None
- OpBranchConditional %77 %79 %80
- %79 = OpLabel
- %81 = OpLoad %10 %30
- OpSelectionMerge %82 None
- OpSwitch %81 %83 1 %84 2 %85
- %83 = OpLabel
- OpBranch %82
- %84 = OpLabel
- %86 = OpLoad %8 %29
- %87 = OpSelect %10 %86 %16 %17
- %88 = OpLoad %10 %30
- %89 = OpIAdd %10 %88 %87
- OpStore %30 %89
- OpBranch %82
- %85 = OpLabel
- OpBranch %82
- %82 = OpLabel
- %90 = OpLoad %8 %27
- OpSelectionMerge %91 None
- OpBranchConditional %90 %92 %91
- %92 = OpLabel
- OpBranch %91
- %91 = OpLabel
- OpBranch %78
- %80 = OpLabel
- OpBranch %78 ; Was OpBranch %74
- %78 = OpLabel
- OpBranch %74
- %75 = OpLabel
- %93 = OpLoad %8 %29
- OpBranchConditional %93 %73 %74
- %74 = OpLabel
- OpBranch %45 ; Was OpBranch %46
- %46 = OpLabel
- OpBranch %44
- %45 = OpLabel
- %94 = OpLoad %10 %30
- %95 = OpConvertSToF %21 %94
- %96 = OpCompositeConstruct %22 %95 %95 %95 %95
- OpStore %3 %96
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, after_op_1, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest, ComplexOptimized) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- OpMemberDecorate %4 0 Offset 0
- OpMemberDecorate %4 1 Offset 4
- OpMemberDecorate %4 2 Offset 8
- OpMemberDecorate %4 3 Offset 12
- OpDecorate %4 Block
- OpDecorate %5 DescriptorSet 0
- OpDecorate %5 Binding 0
- OpDecorate %3 Location 0
- %6 = OpTypeVoid
- %7 = OpTypeFunction %6
- %8 = OpTypeBool
- %10 = OpTypeInt 32 1
- %4 = OpTypeStruct %10 %10 %10 %10
- %11 = OpTypePointer Uniform %4
- %5 = OpVariable %11 Uniform
- %12 = OpConstant %10 0
- %13 = OpTypePointer Uniform %10
- %14 = OpTypeInt 32 0
- %15 = OpConstant %14 0
- %16 = OpConstant %10 1
- %17 = OpConstant %10 2
- %18 = OpConstant %10 3
- %20 = OpConstantFalse %8
- %21 = OpTypeFloat 32
- %22 = OpTypeVector %21 4
- %23 = OpTypePointer Output %22
- %3 = OpVariable %23 Output
- %2 = OpFunction %6 None %7
- %24 = OpLabel
- %31 = OpAccessChain %13 %5 %12
- %32 = OpLoad %10 %31
- %33 = OpINotEqual %8 %32 %15
- %34 = OpAccessChain %13 %5 %16
- %35 = OpLoad %10 %34
- %36 = OpINotEqual %8 %35 %15
- %37 = OpAccessChain %13 %5 %17
- %38 = OpLoad %10 %37
- %39 = OpINotEqual %8 %38 %15
- %40 = OpAccessChain %13 %5 %18
- %41 = OpLoad %10 %40
- %42 = OpINotEqual %8 %41 %15
- OpBranch %44
- %44 = OpLabel
- %98 = OpPhi %10 %12 %24 %107 %46
- %97 = OpPhi %8 %33 %24 %105 %46
- OpLoopMerge %45 %46 None
- OpBranchConditional %97 %49 %45
- %49 = OpLabel
- OpSelectionMerge %51 None
- OpBranchConditional %33 %52 %51
- %52 = OpLabel
- %55 = OpIAdd %10 %98 %16
- OpBranch %51
- %51 = OpLabel
- %100 = OpPhi %10 %98 %49 %55 %52
- %113 = OpSelect %8 %33 %36 %97
- OpSelectionMerge %57 None
- OpBranchConditional %36 %58 %57
- %58 = OpLabel
- %60 = OpIAdd %10 %100 %16
- %63 = OpLogicalOr %8 %113 %33
- OpSelectionMerge %65 None
- OpBranchConditional %39 %66 %65
- %66 = OpLabel
- %68 = OpIAdd %10 %100 %18
- %70 = OpLogicalNot %8 %63
- OpBranch %46
- %65 = OpLabel
- %72 = OpLogicalOr %8 %63 %20
- OpBranch %46
- %57 = OpLabel
- OpBranch %73
- %73 = OpLabel
- %99 = OpPhi %10 %100 %57 %109 %75
- OpLoopMerge %74 %75 None
- OpBranch %76
- %76 = OpLabel
- OpSelectionMerge %78 None
- OpBranchConditional %42 %79 %80
- %79 = OpLabel
- OpSelectionMerge %82 None
- OpSwitch %99 %83 1 %84 2 %85
- %83 = OpLabel
- OpBranch %82
- %84 = OpLabel
- %87 = OpSelect %10 %113 %16 %17
- %89 = OpIAdd %10 %99 %87
- OpBranch %82
- %85 = OpLabel
- OpBranch %75
- %82 = OpLabel
- %110 = OpPhi %10 %99 %83 %89 %84
- OpSelectionMerge %91 None
- OpBranchConditional %39 %92 %91
- %92 = OpLabel
- OpBranch %75
- %91 = OpLabel
- OpBranch %78
- %80 = OpLabel
- OpBranch %74
- %78 = OpLabel
- OpBranch %75
- %75 = OpLabel
- %109 = OpPhi %10 %99 %85 %110 %92 %110 %78
- OpBranchConditional %113 %73 %74
- %74 = OpLabel
- %108 = OpPhi %10 %99 %80 %109 %75
- OpBranch %46
- %46 = OpLabel
- %107 = OpPhi %10 %68 %66 %60 %65 %108 %74
- %105 = OpPhi %8 %70 %66 %72 %65 %113 %74
- OpBranch %44
- %45 = OpLabel
- %95 = OpConvertSToF %21 %98
- %96 = OpCompositeConstruct %22 %95 %95 %95 %95
- OpStore %3 %96
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(2, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string after_op_0 = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- OpMemberDecorate %4 0 Offset 0
- OpMemberDecorate %4 1 Offset 4
- OpMemberDecorate %4 2 Offset 8
- OpMemberDecorate %4 3 Offset 12
- OpDecorate %4 Block
- OpDecorate %5 DescriptorSet 0
- OpDecorate %5 Binding 0
- OpDecorate %3 Location 0
- %6 = OpTypeVoid
- %7 = OpTypeFunction %6
- %8 = OpTypeBool
- %10 = OpTypeInt 32 1
- %4 = OpTypeStruct %10 %10 %10 %10
- %11 = OpTypePointer Uniform %4
- %5 = OpVariable %11 Uniform
- %12 = OpConstant %10 0
- %13 = OpTypePointer Uniform %10
- %14 = OpTypeInt 32 0
- %15 = OpConstant %14 0
- %16 = OpConstant %10 1
- %17 = OpConstant %10 2
- %18 = OpConstant %10 3
- %20 = OpConstantFalse %8
- %21 = OpTypeFloat 32
- %22 = OpTypeVector %21 4
- %23 = OpTypePointer Output %22
- %3 = OpVariable %23 Output
- %114 = OpUndef %10
- %115 = OpUndef %8
- %2 = OpFunction %6 None %7
- %24 = OpLabel
- %31 = OpAccessChain %13 %5 %12
- %32 = OpLoad %10 %31
- %33 = OpINotEqual %8 %32 %15
- %34 = OpAccessChain %13 %5 %16
- %35 = OpLoad %10 %34
- %36 = OpINotEqual %8 %35 %15
- %37 = OpAccessChain %13 %5 %17
- %38 = OpLoad %10 %37
- %39 = OpINotEqual %8 %38 %15
- %40 = OpAccessChain %13 %5 %18
- %41 = OpLoad %10 %40
- %42 = OpINotEqual %8 %41 %15
- OpBranch %44
- %44 = OpLabel
- %98 = OpPhi %10 %12 %24 %114 %46
- %97 = OpPhi %8 %33 %24 %115 %46
- OpSelectionMerge %45 None ; Was OpLoopMerge %45 %46 None
- OpBranchConditional %97 %49 %45
- %49 = OpLabel
- OpSelectionMerge %51 None
- OpBranchConditional %33 %52 %51
- %52 = OpLabel
- %55 = OpIAdd %10 %98 %16
- OpBranch %51
- %51 = OpLabel
- %100 = OpPhi %10 %98 %49 %55 %52
- %113 = OpSelect %8 %33 %36 %97
- OpSelectionMerge %57 None
- OpBranchConditional %36 %58 %57
- %58 = OpLabel
- %60 = OpIAdd %10 %100 %16
- %63 = OpLogicalOr %8 %113 %33
- OpSelectionMerge %65 None
- OpBranchConditional %39 %66 %65
- %66 = OpLabel
- %68 = OpIAdd %10 %100 %18
- %70 = OpLogicalNot %8 %63
- OpBranch %65 ; Was OpBranch %46
- %65 = OpLabel
- %72 = OpLogicalOr %8 %63 %20
- OpBranch %57 ; Was OpBranch %46
- %57 = OpLabel
- OpBranch %73
- %73 = OpLabel
- %99 = OpPhi %10 %100 %57 %109 %75
- OpLoopMerge %74 %75 None
- OpBranch %76
- %76 = OpLabel
- OpSelectionMerge %78 None
- OpBranchConditional %42 %79 %80
- %79 = OpLabel
- OpSelectionMerge %82 None
- OpSwitch %99 %83 1 %84 2 %85
- %83 = OpLabel
- OpBranch %82
- %84 = OpLabel
- %87 = OpSelect %10 %113 %16 %17
- %89 = OpIAdd %10 %99 %87
- OpBranch %82
- %85 = OpLabel
- OpBranch %75
- %82 = OpLabel
- %110 = OpPhi %10 %99 %83 %89 %84
- OpSelectionMerge %91 None
- OpBranchConditional %39 %92 %91
- %92 = OpLabel
- OpBranch %75
- %91 = OpLabel
- OpBranch %78
- %80 = OpLabel
- OpBranch %74
- %78 = OpLabel
- OpBranch %75
- %75 = OpLabel
- %109 = OpPhi %10 %99 %85 %110 %92 %110 %78
- OpBranchConditional %113 %73 %74
- %74 = OpLabel
- %108 = OpPhi %10 %99 %80 %109 %75
- OpBranch %45 ; Was OpBranch %46
- %46 = OpLabel
- %107 = OpPhi %10 ; Was OpPhi %10 %68 %66 %60 %65 %108 %74
- %105 = OpPhi %8 ; Was OpPhi %8 %70 %66 %72 %65 %113 %74
- OpBranch %44
- %45 = OpLabel
- %95 = OpConvertSToF %21 %98
- %96 = OpCompositeConstruct %22 %95 %95 %95 %95
- OpStore %3 %96
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, after_op_0, context.get());
- ASSERT_TRUE(ops[1]->PreconditionHolds());
- ops[1]->TryToApply();
- CheckValid(env, context.get());
- std::string after_op_1 = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- OpMemberDecorate %4 0 Offset 0
- OpMemberDecorate %4 1 Offset 4
- OpMemberDecorate %4 2 Offset 8
- OpMemberDecorate %4 3 Offset 12
- OpDecorate %4 Block
- OpDecorate %5 DescriptorSet 0
- OpDecorate %5 Binding 0
- OpDecorate %3 Location 0
- %6 = OpTypeVoid
- %7 = OpTypeFunction %6
- %8 = OpTypeBool
- %10 = OpTypeInt 32 1
- %4 = OpTypeStruct %10 %10 %10 %10
- %11 = OpTypePointer Uniform %4
- %5 = OpVariable %11 Uniform
- %12 = OpConstant %10 0
- %13 = OpTypePointer Uniform %10
- %14 = OpTypeInt 32 0
- %15 = OpConstant %14 0
- %16 = OpConstant %10 1
- %17 = OpConstant %10 2
- %18 = OpConstant %10 3
- %20 = OpConstantFalse %8
- %21 = OpTypeFloat 32
- %22 = OpTypeVector %21 4
- %23 = OpTypePointer Output %22
- %3 = OpVariable %23 Output
- %114 = OpUndef %10
- %115 = OpUndef %8
- %116 = OpConstantTrue %8
- %2 = OpFunction %6 None %7
- %24 = OpLabel
- %31 = OpAccessChain %13 %5 %12
- %32 = OpLoad %10 %31
- %33 = OpINotEqual %8 %32 %15
- %34 = OpAccessChain %13 %5 %16
- %35 = OpLoad %10 %34
- %36 = OpINotEqual %8 %35 %15
- %37 = OpAccessChain %13 %5 %17
- %38 = OpLoad %10 %37
- %39 = OpINotEqual %8 %38 %15
- %40 = OpAccessChain %13 %5 %18
- %41 = OpLoad %10 %40
- %42 = OpINotEqual %8 %41 %15
- OpBranch %44
- %44 = OpLabel
- %98 = OpPhi %10 %12 %24 %114 %46
- %97 = OpPhi %8 %33 %24 %115 %46
- OpSelectionMerge %45 None ; Was OpLoopMerge %45 %46 None
- OpBranchConditional %97 %49 %45
- %49 = OpLabel
- OpSelectionMerge %51 None
- OpBranchConditional %33 %52 %51
- %52 = OpLabel
- %55 = OpIAdd %10 %98 %16
- OpBranch %51
- %51 = OpLabel
- %100 = OpPhi %10 %98 %49 %55 %52
- %113 = OpSelect %8 %33 %36 %97
- OpSelectionMerge %57 None
- OpBranchConditional %36 %58 %57
- %58 = OpLabel
- %60 = OpIAdd %10 %100 %16
- %63 = OpLogicalOr %8 %113 %33
- OpSelectionMerge %65 None
- OpBranchConditional %39 %66 %65
- %66 = OpLabel
- %68 = OpIAdd %10 %100 %18
- %70 = OpLogicalNot %8 %63
- OpBranch %65 ; Was OpBranch %46
- %65 = OpLabel
- %72 = OpLogicalOr %8 %63 %20
- OpBranch %57 ; Was OpBranch %46
- %57 = OpLabel
- OpBranch %73
- %73 = OpLabel
- %99 = OpPhi %10 %100 %57 %114 %75
- OpSelectionMerge %74 None ; Was OpLoopMerge %74 %75 None
- OpBranchConditional %116 %76 %74
- %76 = OpLabel
- OpSelectionMerge %78 None
- OpBranchConditional %42 %79 %80
- %79 = OpLabel
- OpSelectionMerge %82 None
- OpSwitch %99 %83 1 %84 2 %85
- %83 = OpLabel
- OpBranch %82
- %84 = OpLabel
- %87 = OpSelect %10 %113 %16 %17
- %89 = OpIAdd %10 %99 %87
- OpBranch %82
- %85 = OpLabel
- OpBranch %82 ; Was OpBranch %75
- %82 = OpLabel
- %110 = OpPhi %10 %99 %83 %89 %84 %114 %85 ; Was OpPhi %10 %99 %83 %89 %84
- OpSelectionMerge %91 None
- OpBranchConditional %39 %92 %91
- %92 = OpLabel
- OpBranch %91 ; OpBranch %75
- %91 = OpLabel
- OpBranch %78
- %80 = OpLabel
- OpBranch %78 ; Was OpBranch %74
- %78 = OpLabel
- OpBranch %74 ; Was OpBranch %75
- %75 = OpLabel
- %109 = OpPhi %10 ; Was OpPhi %10 %99 %85 %110 %92 %110 %78
- OpBranchConditional %115 %73 %74
- %74 = OpLabel
- %108 = OpPhi %10 %114 %75 %114 %78 %114 %73 ; Was OpPhi %10 %99 %80 %109 %75
- OpBranch %45 ; Was OpBranch %46
- %46 = OpLabel
- %107 = OpPhi %10 ; Was OpPhi %10 %68 %66 %60 %65 %108 %74
- %105 = OpPhi %8 ; Was OpPhi %8 %70 %66 %72 %65 %113 %74
- OpBranch %44
- %45 = OpLabel
- %95 = OpConvertSToF %21 %98
- %96 = OpCompositeConstruct %22 %95 %95 %95 %95
- OpStore %3 %96
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, after_op_1, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest, DominanceIssue) {
- // Exposes a scenario where redirecting edges results in uses of ids being
- // non-dominated. We replace such uses with OpUndef to account for this.
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %5 = OpTypeInt 32 1
- %7 = OpTypePointer Function %5
- %6 = OpTypeBool
- %8 = OpConstantTrue %6
- %9 = OpConstant %5 10
- %10 = OpConstant %5 20
- %11 = OpConstant %5 30
- %4 = OpFunction %2 None %3
- %12 = OpLabel
- OpBranch %13
- %13 = OpLabel
- OpLoopMerge %14 %15 None
- OpBranch %16
- %16 = OpLabel
- OpSelectionMerge %17 None
- OpBranchConditional %8 %18 %19
- %18 = OpLabel
- OpBranch %14
- %19 = OpLabel
- %20 = OpIAdd %5 %9 %10
- OpBranch %17
- %17 = OpLabel
- %21 = OpIAdd %5 %20 %11
- OpBranchConditional %8 %14 %15
- %15 = OpLabel
- OpBranch %13
- %14 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(1, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string expected = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %5 = OpTypeInt 32 1
- %7 = OpTypePointer Function %5
- %6 = OpTypeBool
- %8 = OpConstantTrue %6
- %9 = OpConstant %5 10
- %10 = OpConstant %5 20
- %11 = OpConstant %5 30
- %22 = OpUndef %5
- %4 = OpFunction %2 None %3
- %12 = OpLabel
- OpBranch %13
- %13 = OpLabel
- OpSelectionMerge %14 None
- OpBranchConditional %8 %16 %14
- %16 = OpLabel
- OpSelectionMerge %17 None
- OpBranchConditional %8 %18 %19
- %18 = OpLabel
- OpBranch %17
- %19 = OpLabel
- %20 = OpIAdd %5 %9 %10
- OpBranch %17
- %17 = OpLabel
- %21 = OpIAdd %5 %22 %11
- OpBranchConditional %8 %14 %14
- %15 = OpLabel
- OpBranch %13
- %14 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, expected, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest, AccessChainIssue) {
- // Exposes a scenario where redirecting edges results in a use of an id
- // generated by an access chain being non-dominated.
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main" %56
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- OpMemberDecorate %28 0 Offset 0
- OpDecorate %28 Block
- OpDecorate %30 DescriptorSet 0
- OpDecorate %30 Binding 0
- OpDecorate %56 Location 0
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %7 = OpTypeVector %6 2
- %8 = OpTypePointer Function %7
- %60 = OpTypePointer Private %7
- %10 = OpConstant %6 0
- %11 = OpConstantComposite %7 %10 %10
- %12 = OpTypePointer Function %6
- %59 = OpTypePointer Private %6
- %14 = OpTypeInt 32 1
- %15 = OpTypePointer Function %14
- %17 = OpConstant %14 0
- %24 = OpConstant %14 100
- %25 = OpTypeBool
- %28 = OpTypeStruct %6
- %29 = OpTypePointer Uniform %28
- %30 = OpVariable %29 Uniform
- %31 = OpTypePointer Uniform %6
- %39 = OpTypeInt 32 0
- %40 = OpConstant %39 1
- %45 = OpConstant %39 0
- %52 = OpConstant %14 1
- %54 = OpTypeVector %6 4
- %55 = OpTypePointer Output %54
- %56 = OpVariable %55 Output
- %9 = OpVariable %60 Private
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %13 = OpVariable %12 Function
- %16 = OpVariable %15 Function
- %38 = OpVariable %12 Function
- OpStore %9 %11
- OpStore %13 %10
- OpStore %16 %17
- OpBranch %18
- %18 = OpLabel
- OpLoopMerge %20 %21 None
- OpBranch %22
- %22 = OpLabel
- %23 = OpLoad %14 %16
- %26 = OpSLessThan %25 %23 %24
- OpBranchConditional %26 %19 %20
- %19 = OpLabel
- %27 = OpLoad %14 %16
- %32 = OpAccessChain %31 %30 %17
- %33 = OpLoad %6 %32
- %34 = OpConvertFToS %14 %33
- %35 = OpSLessThan %25 %27 %34
- OpSelectionMerge %37 None
- OpBranchConditional %35 %36 %44
- %36 = OpLabel
- %41 = OpAccessChain %59 %9 %40
- %42 = OpLoad %6 %41
- OpStore %38 %42
- OpBranch %20
- %44 = OpLabel
- %46 = OpAccessChain %59 %9 %45
- OpBranch %37
- %37 = OpLabel
- %47 = OpLoad %6 %46
- OpStore %38 %47
- %48 = OpLoad %6 %38
- %49 = OpLoad %6 %13
- %50 = OpFAdd %6 %49 %48
- OpStore %13 %50
- OpBranch %21
- %21 = OpLabel
- %51 = OpLoad %14 %16
- %53 = OpIAdd %14 %51 %52
- OpStore %16 %53
- OpBranch %18
- %20 = OpLabel
- %57 = OpLoad %6 %13
- %58 = OpCompositeConstruct %54 %57 %57 %57 %57
- OpStore %56 %58
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(1, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string expected = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main" %56
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- OpMemberDecorate %28 0 Offset 0
- OpDecorate %28 Block
- OpDecorate %30 DescriptorSet 0
- OpDecorate %30 Binding 0
- OpDecorate %56 Location 0
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %7 = OpTypeVector %6 2
- %8 = OpTypePointer Function %7
- %60 = OpTypePointer Private %7
- %10 = OpConstant %6 0
- %11 = OpConstantComposite %7 %10 %10
- %12 = OpTypePointer Function %6
- %59 = OpTypePointer Private %6
- %14 = OpTypeInt 32 1
- %15 = OpTypePointer Function %14
- %17 = OpConstant %14 0
- %24 = OpConstant %14 100
- %25 = OpTypeBool
- %28 = OpTypeStruct %6
- %29 = OpTypePointer Uniform %28
- %30 = OpVariable %29 Uniform
- %31 = OpTypePointer Uniform %6
- %39 = OpTypeInt 32 0
- %40 = OpConstant %39 1
- %45 = OpConstant %39 0
- %52 = OpConstant %14 1
- %54 = OpTypeVector %6 4
- %55 = OpTypePointer Output %54
- %56 = OpVariable %55 Output
- %9 = OpVariable %60 Private
- %61 = OpConstantTrue %25
- %62 = OpVariable %59 Private
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %13 = OpVariable %12 Function
- %16 = OpVariable %15 Function
- %38 = OpVariable %12 Function
- OpStore %9 %11
- OpStore %13 %10
- OpStore %16 %17
- OpBranch %18
- %18 = OpLabel
- OpSelectionMerge %20 None
- OpBranchConditional %61 %22 %20
- %22 = OpLabel
- %23 = OpLoad %14 %16
- %26 = OpSLessThan %25 %23 %24
- OpBranchConditional %26 %19 %20
- %19 = OpLabel
- %27 = OpLoad %14 %16
- %32 = OpAccessChain %31 %30 %17
- %33 = OpLoad %6 %32
- %34 = OpConvertFToS %14 %33
- %35 = OpSLessThan %25 %27 %34
- OpSelectionMerge %37 None
- OpBranchConditional %35 %36 %44
- %36 = OpLabel
- %41 = OpAccessChain %59 %9 %40
- %42 = OpLoad %6 %41
- OpStore %38 %42
- OpBranch %37
- %44 = OpLabel
- %46 = OpAccessChain %59 %9 %45
- OpBranch %37
- %37 = OpLabel
- %47 = OpLoad %6 %62
- OpStore %38 %47
- %48 = OpLoad %6 %38
- %49 = OpLoad %6 %13
- %50 = OpFAdd %6 %49 %48
- OpStore %13 %50
- OpBranch %20
- %21 = OpLabel
- %51 = OpLoad %14 %16
- %53 = OpIAdd %14 %51 %52
- OpStore %16 %53
- OpBranch %18
- %20 = OpLabel
- %57 = OpLoad %6 %13
- %58 = OpCompositeConstruct %54 %57 %57 %57 %57
- OpStore %56 %58
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, expected, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest, DominanceAndPhiIssue) {
- // Exposes an interesting scenario where a use in a phi stops being dominated
- // by the block with which it is associated in the phi.
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %17 = OpTypeBool
- %18 = OpConstantTrue %17
- %19 = OpConstantFalse %17
- %20 = OpTypeInt 32 1
- %21 = OpConstant %20 5
- %22 = OpConstant %20 6
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpBranch %6
- %6 = OpLabel
- OpLoopMerge %16 %15 None
- OpBranch %7
- %7 = OpLabel
- OpSelectionMerge %13 None
- OpBranchConditional %18 %8 %9
- %8 = OpLabel
- OpSelectionMerge %12 None
- OpBranchConditional %18 %10 %11
- %9 = OpLabel
- OpBranch %16
- %10 = OpLabel
- OpBranch %16
- %11 = OpLabel
- %23 = OpIAdd %20 %21 %22
- OpBranch %12
- %12 = OpLabel
- OpBranch %13
- %13 = OpLabel
- OpBranch %14
- %14 = OpLabel
- %24 = OpPhi %20 %23 %13
- OpBranchConditional %19 %15 %16
- %15 = OpLabel
- OpBranch %6
- %16 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(1, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string expected = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %17 = OpTypeBool
- %18 = OpConstantTrue %17
- %19 = OpConstantFalse %17
- %20 = OpTypeInt 32 1
- %21 = OpConstant %20 5
- %22 = OpConstant %20 6
- %25 = OpUndef %20
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpBranch %6
- %6 = OpLabel
- OpSelectionMerge %16 None
- OpBranchConditional %18 %7 %16
- %7 = OpLabel
- OpSelectionMerge %13 None
- OpBranchConditional %18 %8 %9
- %8 = OpLabel
- OpSelectionMerge %12 None
- OpBranchConditional %18 %10 %11
- %9 = OpLabel
- OpBranch %13
- %10 = OpLabel
- OpBranch %12
- %11 = OpLabel
- %23 = OpIAdd %20 %21 %22
- OpBranch %12
- %12 = OpLabel
- OpBranch %13
- %13 = OpLabel
- OpBranch %14
- %14 = OpLabel
- %24 = OpPhi %20 %25 %13
- OpBranchConditional %19 %16 %16
- %15 = OpLabel
- OpBranch %6
- %16 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, expected, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest, OpLineBeforeOpPhi) {
- // Test to ensure the pass knows OpLine and OpPhi instructions can be
- // interleaved.
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpString "somefile"
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 1
- %7 = OpConstant %6 10
- %8 = OpConstant %6 20
- %9 = OpConstant %6 30
- %10 = OpTypeBool
- %11 = OpConstantTrue %10
- %2 = OpFunction %4 None %5
- %12 = OpLabel
- OpBranch %13
- %13 = OpLabel
- OpLoopMerge %14 %15 None
- OpBranch %16
- %16 = OpLabel
- OpSelectionMerge %17 None
- OpBranchConditional %11 %18 %19
- %18 = OpLabel
- %20 = OpIAdd %6 %7 %8
- %21 = OpIAdd %6 %7 %9
- OpBranch %17
- %19 = OpLabel
- OpBranch %14
- %17 = OpLabel
- %22 = OpPhi %6 %20 %18
- OpLine %3 0 0
- %23 = OpPhi %6 %21 %18
- OpBranch %15
- %15 = OpLabel
- OpBranch %13
- %14 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(1, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string expected = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpString "somefile"
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 1
- %7 = OpConstant %6 10
- %8 = OpConstant %6 20
- %9 = OpConstant %6 30
- %10 = OpTypeBool
- %11 = OpConstantTrue %10
- %24 = OpUndef %6
- %2 = OpFunction %4 None %5
- %12 = OpLabel
- OpBranch %13
- %13 = OpLabel
- OpSelectionMerge %14 None
- OpBranchConditional %11 %16 %14
- %16 = OpLabel
- OpSelectionMerge %17 None
- OpBranchConditional %11 %18 %19
- %18 = OpLabel
- %20 = OpIAdd %6 %7 %8
- %21 = OpIAdd %6 %7 %9
- OpBranch %17
- %19 = OpLabel
- OpBranch %17
- %17 = OpLabel
- %22 = OpPhi %6 %20 %18 %24 %19
- OpLine %3 0 0
- %23 = OpPhi %6 %21 %18 %24 %19
- OpBranch %14
- %15 = OpLabel
- OpBranch %13
- %14 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, expected, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest,
- SelectionMergeIsContinueTarget) {
- // Example where a loop's continue target is also the target of a selection.
- // In this scenario we cautiously do not apply the transformation.
- std::string shader = R"(
- OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Vertex %1 "main"
- %2 = OpTypeVoid
- %3 = OpTypeBool
- %4 = OpTypeFunction %2
- %1 = OpFunction %2 None %4
- %5 = OpLabel
- %6 = OpUndef %3
- OpBranch %7
- %7 = OpLabel
- %8 = OpPhi %3 %6 %5 %9 %10
- OpLoopMerge %11 %10 None
- OpBranch %12
- %12 = OpLabel
- %13 = OpUndef %3
- OpSelectionMerge %10 None
- OpBranchConditional %13 %14 %10
- %14 = OpLabel
- OpBranch %10
- %10 = OpLabel
- %9 = OpUndef %3
- OpBranchConditional %9 %7 %11
- %11 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- // There should be no opportunities.
- ASSERT_EQ(0, ops.size());
- }
- TEST(StructuredLoopToSelectionReductionPassTest,
- SwitchSelectionMergeIsContinueTarget) {
- // Another example where a loop's continue target is also the target of a
- // selection; this time a selection associated with an OpSwitch. We
- // cautiously do not apply the transformation.
- std::string shader = R"(
- OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Vertex %1 "main"
- %2 = OpTypeVoid
- %3 = OpTypeBool
- %5 = OpTypeInt 32 1
- %4 = OpTypeFunction %2
- %6 = OpConstant %5 2
- %7 = OpConstantTrue %3
- %1 = OpFunction %2 None %4
- %8 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpLoopMerge %14 %15 None
- OpBranchConditional %7 %10 %14
- %10 = OpLabel
- OpSelectionMerge %15 None
- OpSwitch %6 %12 1 %11 2 %11 3 %15
- %11 = OpLabel
- OpBranch %12
- %12 = OpLabel
- OpBranch %15
- %15 = OpLabel
- OpBranch %9
- %14 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- // There should be no opportunities.
- ASSERT_EQ(0, ops.size());
- }
- TEST(StructuredLoopToSelectionReductionPassTest, ContinueTargetIsSwitchTarget) {
- std::string shader = R"(
- OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Vertex %1 "main"
- %2 = OpTypeVoid
- %3 = OpTypeBool
- %5 = OpTypeInt 32 1
- %4 = OpTypeFunction %2
- %6 = OpConstant %5 2
- %7 = OpConstantTrue %3
- %1 = OpFunction %2 None %4
- %8 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpLoopMerge %14 %12 None
- OpBranchConditional %7 %10 %14
- %10 = OpLabel
- OpSelectionMerge %15 None
- OpSwitch %6 %12 1 %11 2 %11 3 %15
- %11 = OpLabel
- OpBranch %12
- %12 = OpLabel
- OpBranch %9
- %15 = OpLabel
- OpBranch %14
- %14 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(1, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string expected = R"(
- OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Vertex %1 "main"
- %2 = OpTypeVoid
- %3 = OpTypeBool
- %5 = OpTypeInt 32 1
- %4 = OpTypeFunction %2
- %6 = OpConstant %5 2
- %7 = OpConstantTrue %3
- %1 = OpFunction %2 None %4
- %8 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpSelectionMerge %14 None
- OpBranchConditional %7 %10 %14
- %10 = OpLabel
- OpSelectionMerge %15 None
- OpSwitch %6 %15 1 %11 2 %11 3 %15
- %11 = OpLabel
- OpBranch %15
- %12 = OpLabel
- OpBranch %9
- %15 = OpLabel
- OpBranch %14
- %14 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, expected, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest,
- MultipleSwitchTargetsAreContinueTarget) {
- std::string shader = R"(
- OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Vertex %1 "main"
- %2 = OpTypeVoid
- %3 = OpTypeBool
- %5 = OpTypeInt 32 1
- %4 = OpTypeFunction %2
- %6 = OpConstant %5 2
- %7 = OpConstantTrue %3
- %1 = OpFunction %2 None %4
- %8 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpLoopMerge %14 %12 None
- OpBranchConditional %7 %10 %14
- %10 = OpLabel
- OpSelectionMerge %15 None
- OpSwitch %6 %11 1 %12 2 %12 3 %15
- %11 = OpLabel
- OpBranch %12
- %12 = OpLabel
- OpBranch %9
- %15 = OpLabel
- OpBranch %14
- %14 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(1, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string expected = R"(
- OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Vertex %1 "main"
- %2 = OpTypeVoid
- %3 = OpTypeBool
- %5 = OpTypeInt 32 1
- %4 = OpTypeFunction %2
- %6 = OpConstant %5 2
- %7 = OpConstantTrue %3
- %1 = OpFunction %2 None %4
- %8 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpSelectionMerge %14 None
- OpBranchConditional %7 %10 %14
- %10 = OpLabel
- OpSelectionMerge %15 None
- OpSwitch %6 %11 1 %15 2 %15 3 %15
- %11 = OpLabel
- OpBranch %15
- %12 = OpLabel
- OpBranch %9
- %15 = OpLabel
- OpBranch %14
- %14 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, expected, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest, LoopBranchesStraightToMerge) {
- std::string shader = R"(
- OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Vertex %1 "main"
- %2 = OpTypeVoid
- %4 = OpTypeFunction %2
- %1 = OpFunction %2 None %4
- %8 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpLoopMerge %14 %12 None
- OpBranch %14
- %12 = OpLabel
- OpBranch %9
- %14 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(1, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string expected = R"(
- OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Vertex %1 "main"
- %2 = OpTypeVoid
- %4 = OpTypeFunction %2
- %15 = OpTypeBool
- %16 = OpConstantTrue %15
- %1 = OpFunction %2 None %4
- %8 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpSelectionMerge %14 None
- OpBranchConditional %16 %14 %14
- %12 = OpLabel
- OpBranch %9
- %14 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, expected, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest,
- LoopConditionallyJumpsToMergeOrContinue) {
- std::string shader = R"(
- OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Vertex %1 "main"
- %2 = OpTypeVoid
- %3 = OpTypeBool
- %4 = OpTypeFunction %2
- %7 = OpConstantTrue %3
- %1 = OpFunction %2 None %4
- %8 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpLoopMerge %14 %12 None
- OpBranchConditional %7 %14 %12
- %12 = OpLabel
- OpBranch %9
- %14 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(1, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string expected = R"(
- OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Vertex %1 "main"
- %2 = OpTypeVoid
- %3 = OpTypeBool
- %4 = OpTypeFunction %2
- %7 = OpConstantTrue %3
- %1 = OpFunction %2 None %4
- %8 = OpLabel
- OpBranch %9
- %9 = OpLabel
- OpSelectionMerge %14 None
- OpBranchConditional %7 %14 %14
- %12 = OpLabel
- OpBranch %9
- %14 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, expected, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest, MultipleAccessChains) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypeStruct %6
- %8 = OpTypeStruct %7
- %9 = OpTypePointer Function %8
- %11 = OpConstant %6 3
- %12 = OpConstantComposite %7 %11
- %13 = OpConstantComposite %8 %12
- %14 = OpTypePointer Function %7
- %16 = OpConstant %6 0
- %19 = OpTypePointer Function %6
- %15 = OpTypeBool
- %18 = OpConstantTrue %15
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %10 = OpVariable %9 Function
- %20 = OpVariable %19 Function
- OpStore %10 %13
- OpBranch %23
- %23 = OpLabel
- OpLoopMerge %25 %26 None
- OpBranch %27
- %27 = OpLabel
- OpSelectionMerge %28 None
- OpBranchConditional %18 %29 %25
- %29 = OpLabel
- %17 = OpAccessChain %14 %10 %16
- OpBranch %28
- %28 = OpLabel
- %21 = OpAccessChain %19 %17 %16
- %22 = OpLoad %6 %21
- %24 = OpAccessChain %19 %10 %16 %16
- OpStore %24 %22
- OpBranch %25
- %26 = OpLabel
- OpBranch %23
- %25 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(1, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string expected = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypeStruct %6
- %8 = OpTypeStruct %7
- %9 = OpTypePointer Function %8
- %11 = OpConstant %6 3
- %12 = OpConstantComposite %7 %11
- %13 = OpConstantComposite %8 %12
- %14 = OpTypePointer Function %7
- %16 = OpConstant %6 0
- %19 = OpTypePointer Function %6
- %15 = OpTypeBool
- %18 = OpConstantTrue %15
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %10 = OpVariable %9 Function
- %20 = OpVariable %19 Function
- %30 = OpVariable %14 Function
- OpStore %10 %13
- OpBranch %23
- %23 = OpLabel
- OpSelectionMerge %25 None
- OpBranchConditional %18 %27 %25
- %27 = OpLabel
- OpSelectionMerge %28 None
- OpBranchConditional %18 %29 %28
- %29 = OpLabel
- %17 = OpAccessChain %14 %10 %16
- OpBranch %28
- %28 = OpLabel
- %21 = OpAccessChain %19 %30 %16
- %22 = OpLoad %6 %21
- %24 = OpAccessChain %19 %10 %16 %16
- OpStore %24 %22
- OpBranch %25
- %26 = OpLabel
- OpBranch %23
- %25 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, expected, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest,
- UnreachableInnerLoopContinueBranchingToOuterLoopMerge) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeBool
- %6 = OpConstantTrue %5
- %2 = OpFunction %3 None %4
- %7 = OpLabel
- OpBranch %8
- %8 = OpLabel
- OpLoopMerge %9 %10 None
- OpBranch %11
- %11 = OpLabel
- OpLoopMerge %12 %13 None
- OpBranch %12
- %13 = OpLabel
- OpBranch %11
- %12 = OpLabel
- OpBranch %10
- %10 = OpLabel
- OpBranchConditional %6 %9 %8
- %9 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(2, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string after_op_0 = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeBool
- %6 = OpConstantTrue %5
- %2 = OpFunction %3 None %4
- %7 = OpLabel
- OpBranch %8
- %8 = OpLabel
- OpSelectionMerge %9 None
- OpBranchConditional %6 %11 %9
- %11 = OpLabel
- OpLoopMerge %12 %13 None
- OpBranch %12
- %13 = OpLabel
- OpBranch %11
- %12 = OpLabel
- OpBranch %9
- %10 = OpLabel
- OpBranchConditional %6 %9 %8
- %9 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, after_op_0, context.get());
- ASSERT_TRUE(ops[1]->PreconditionHolds());
- ops[1]->TryToApply();
- CheckValid(env, context.get());
- std::string after_op_1 = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeBool
- %6 = OpConstantTrue %5
- %2 = OpFunction %3 None %4
- %7 = OpLabel
- OpBranch %8
- %8 = OpLabel
- OpSelectionMerge %9 None
- OpBranchConditional %6 %11 %9
- %11 = OpLabel
- OpSelectionMerge %12 None
- OpBranchConditional %6 %12 %12
- %13 = OpLabel
- OpBranch %11
- %12 = OpLabel
- OpBranch %9
- %10 = OpLabel
- OpBranchConditional %6 %9 %8
- %9 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, after_op_1, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest,
- UnreachableInnerLoopContinueBranchingToOuterLoopMerge2) {
- // In this test, the unreachable continue is composed of multiple blocks.
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeBool
- %6 = OpConstantTrue %5
- %2 = OpFunction %3 None %4
- %7 = OpLabel
- OpBranch %8
- %8 = OpLabel
- OpLoopMerge %9 %10 None
- OpBranch %11
- %11 = OpLabel
- OpLoopMerge %12 %13 None
- OpBranch %12
- %13 = OpLabel
- OpBranch %14
- %14 = OpLabel
- OpBranch %11
- %12 = OpLabel
- OpBranch %10
- %10 = OpLabel
- OpBranchConditional %6 %9 %8
- %9 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(2, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string after_op_0 = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeBool
- %6 = OpConstantTrue %5
- %2 = OpFunction %3 None %4
- %7 = OpLabel
- OpBranch %8
- %8 = OpLabel
- OpSelectionMerge %9 None
- OpBranchConditional %6 %11 %9
- %11 = OpLabel
- OpLoopMerge %12 %13 None
- OpBranch %12
- %13 = OpLabel
- OpBranch %14
- %14 = OpLabel
- OpBranch %11
- %12 = OpLabel
- OpBranch %9
- %10 = OpLabel
- OpBranchConditional %6 %9 %8
- %9 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, after_op_0, context.get());
- ASSERT_TRUE(ops[1]->PreconditionHolds());
- ops[1]->TryToApply();
- CheckValid(env, context.get());
- std::string after_op_1 = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeBool
- %6 = OpConstantTrue %5
- %2 = OpFunction %3 None %4
- %7 = OpLabel
- OpBranch %8
- %8 = OpLabel
- OpSelectionMerge %9 None
- OpBranchConditional %6 %11 %9
- %11 = OpLabel
- OpSelectionMerge %12 None
- OpBranchConditional %6 %12 %12
- %13 = OpLabel
- OpBranch %14
- %14 = OpLabel
- OpBranch %11
- %12 = OpLabel
- OpBranch %9
- %10 = OpLabel
- OpBranchConditional %6 %9 %8
- %9 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, after_op_1, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest,
- InnerLoopHeaderBranchesToOuterLoopMerge) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeBool
- %6 = OpConstantTrue %5
- %2 = OpFunction %3 None %4
- %7 = OpLabel
- OpBranch %8
- %8 = OpLabel
- OpLoopMerge %9 %10 None
- OpBranch %11
- %11 = OpLabel
- OpLoopMerge %12 %13 None
- OpBranchConditional %6 %9 %13
- %13 = OpLabel
- OpBranchConditional %6 %11 %12
- %12 = OpLabel
- OpBranch %10
- %10 = OpLabel
- OpBranchConditional %6 %9 %8
- %9 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- // We cannot transform the inner loop due to its header jumping straight to
- // the outer loop merge (the inner loop's merge does not post-dominate its
- // header).
- ASSERT_EQ(1, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string after_op_0 = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeBool
- %6 = OpConstantTrue %5
- %2 = OpFunction %3 None %4
- %7 = OpLabel
- OpBranch %8
- %8 = OpLabel
- OpSelectionMerge %9 None
- OpBranchConditional %6 %11 %9
- %11 = OpLabel
- OpLoopMerge %12 %13 None
- OpBranchConditional %6 %12 %13
- %13 = OpLabel
- OpBranchConditional %6 %11 %12
- %12 = OpLabel
- OpBranch %9
- %10 = OpLabel
- OpBranchConditional %6 %9 %8
- %9 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, after_op_0, context.get());
- // Now look again for more opportunities.
- ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- // What was the inner loop should now be transformable, as the jump to the
- // outer loop's merge has been redirected.
- ASSERT_EQ(1, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string after_another_op_0 = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeBool
- %6 = OpConstantTrue %5
- %2 = OpFunction %3 None %4
- %7 = OpLabel
- OpBranch %8
- %8 = OpLabel
- OpSelectionMerge %9 None
- OpBranchConditional %6 %11 %9
- %11 = OpLabel
- OpSelectionMerge %12 None
- OpBranchConditional %6 %12 %12
- %13 = OpLabel
- OpBranchConditional %6 %11 %12
- %12 = OpLabel
- OpBranch %9
- %10 = OpLabel
- OpBranchConditional %6 %9 %8
- %9 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, after_another_op_0, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest, LongAccessChains) {
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 310
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeInt 32 1
- %6 = OpTypeInt 32 0
- %7 = OpConstant %6 5
- %8 = OpTypeArray %5 %7
- %9 = OpTypeStruct %8
- %10 = OpTypeStruct %9 %9
- %11 = OpConstant %6 2
- %12 = OpTypeArray %10 %11
- %13 = OpTypeStruct %12
- %14 = OpTypePointer Function %13
- %15 = OpConstant %5 0
- %16 = OpConstant %5 1
- %17 = OpConstant %5 2
- %18 = OpConstant %5 3
- %19 = OpConstant %5 4
- %20 = OpConstantComposite %8 %15 %16 %17 %18 %19
- %21 = OpConstantComposite %9 %20
- %22 = OpConstant %5 5
- %23 = OpConstant %5 6
- %24 = OpConstant %5 7
- %25 = OpConstant %5 8
- %26 = OpConstant %5 9
- %27 = OpConstantComposite %8 %22 %23 %24 %25 %26
- %28 = OpConstantComposite %9 %27
- %29 = OpConstantComposite %10 %21 %28
- %30 = OpConstant %5 10
- %31 = OpConstant %5 11
- %32 = OpConstant %5 12
- %33 = OpConstant %5 13
- %34 = OpConstant %5 14
- %35 = OpConstantComposite %8 %30 %31 %32 %33 %34
- %36 = OpConstantComposite %9 %35
- %37 = OpConstant %5 15
- %38 = OpConstant %5 16
- %39 = OpConstant %5 17
- %40 = OpConstant %5 18
- %41 = OpConstant %5 19
- %42 = OpConstantComposite %8 %37 %38 %39 %40 %41
- %43 = OpConstantComposite %9 %42
- %44 = OpConstantComposite %10 %36 %43
- %45 = OpConstantComposite %12 %29 %44
- %46 = OpConstantComposite %13 %45
- %47 = OpTypePointer Function %12
- %48 = OpTypePointer Function %10
- %49 = OpTypePointer Function %9
- %50 = OpTypePointer Function %8
- %51 = OpTypePointer Function %5
- %52 = OpTypeBool
- %53 = OpConstantTrue %52
- %2 = OpFunction %3 None %4
- %54 = OpLabel
- %55 = OpVariable %14 Function
- OpStore %55 %46
- OpBranch %56
- %56 = OpLabel
- OpLoopMerge %57 %58 None
- OpBranchConditional %53 %57 %59
- %59 = OpLabel
- OpSelectionMerge %60 None
- OpBranchConditional %53 %61 %57
- %61 = OpLabel
- %62 = OpAccessChain %47 %55 %15
- OpBranch %63
- %63 = OpLabel
- OpSelectionMerge %64 None
- OpBranchConditional %53 %65 %57
- %65 = OpLabel
- %66 = OpAccessChain %48 %62 %16
- OpBranch %67
- %67 = OpLabel
- OpSelectionMerge %68 None
- OpBranchConditional %53 %69 %57
- %69 = OpLabel
- %70 = OpAccessChain %49 %66 %16
- OpBranch %71
- %71 = OpLabel
- OpSelectionMerge %72 None
- OpBranchConditional %53 %73 %57
- %73 = OpLabel
- %74 = OpAccessChain %50 %70 %15
- OpBranch %75
- %75 = OpLabel
- OpSelectionMerge %76 None
- OpBranchConditional %53 %77 %57
- %77 = OpLabel
- %78 = OpAccessChain %51 %74 %17
- OpBranch %79
- %79 = OpLabel
- OpSelectionMerge %80 None
- OpBranchConditional %53 %81 %57
- %81 = OpLabel
- %82 = OpLoad %5 %78
- OpBranch %80
- %80 = OpLabel
- OpBranch %76
- %76 = OpLabel
- OpBranch %72
- %72 = OpLabel
- OpBranch %68
- %68 = OpLabel
- OpBranch %64
- %64 = OpLabel
- OpBranch %60
- %60 = OpLabel
- OpBranch %58
- %58 = OpLabel
- OpBranch %56
- %57 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(1, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- // TODO(2183): When we have a more general solution for handling access
- // chains, write an expected result for this test.
- // std::string expected = R"(
- // Expected text for transformed shader
- //)";
- // CheckEqual(env, expected, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest, LoopyShaderWithOpDecorate) {
- // A shader containing a function that contains a loop and some definitions
- // that are "used" in OpDecorate instructions (outside the function). These
- // "uses" were causing segfaults because we try to calculate their dominance
- // information, which doesn't make sense.
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main" %9
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- OpName %4 "main"
- OpName %9 "_GLF_color"
- OpName %14 "buf0"
- OpMemberName %14 0 "a"
- OpName %16 ""
- OpDecorate %9 RelaxedPrecision
- OpDecorate %9 Location 0
- OpMemberDecorate %14 0 RelaxedPrecision
- OpMemberDecorate %14 0 Offset 0
- OpDecorate %14 Block
- OpDecorate %16 DescriptorSet 0
- OpDecorate %16 Binding 0
- OpDecorate %21 RelaxedPrecision
- OpDecorate %35 RelaxedPrecision
- OpDecorate %36 RelaxedPrecision
- OpDecorate %39 RelaxedPrecision
- OpDecorate %40 RelaxedPrecision
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %7 = OpTypeVector %6 4
- %8 = OpTypePointer Output %7
- %9 = OpVariable %8 Output
- %10 = OpConstant %6 1
- %11 = OpConstantComposite %7 %10 %10 %10 %10
- %14 = OpTypeStruct %6
- %15 = OpTypePointer Uniform %14
- %16 = OpVariable %15 Uniform
- %17 = OpTypeInt 32 1
- %18 = OpConstant %17 0
- %19 = OpTypePointer Uniform %6
- %28 = OpConstant %6 2
- %29 = OpTypeBool
- %31 = OpTypeInt 32 0
- %32 = OpConstant %31 0
- %33 = OpTypePointer Output %6
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpStore %9 %11
- %20 = OpAccessChain %19 %16 %18
- %21 = OpLoad %6 %20
- OpBranch %22
- %22 = OpLabel
- %40 = OpPhi %6 %21 %5 %39 %23
- %30 = OpFOrdLessThan %29 %40 %28
- OpLoopMerge %24 %23 None
- OpBranchConditional %30 %23 %24
- %23 = OpLabel
- %34 = OpAccessChain %33 %9 %32
- %35 = OpLoad %6 %34
- %36 = OpFAdd %6 %35 %10
- OpStore %34 %36
- %39 = OpFAdd %6 %40 %10
- OpBranch %22
- %24 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(1, ops.size());
- ASSERT_TRUE(ops[0]->PreconditionHolds());
- ops[0]->TryToApply();
- CheckValid(env, context.get());
- std::string after_op_0 = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main" %9
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- OpName %4 "main"
- OpName %9 "_GLF_color"
- OpName %14 "buf0"
- OpMemberName %14 0 "a"
- OpName %16 ""
- OpDecorate %9 RelaxedPrecision
- OpDecorate %9 Location 0
- OpMemberDecorate %14 0 RelaxedPrecision
- OpMemberDecorate %14 0 Offset 0
- OpDecorate %14 Block
- OpDecorate %16 DescriptorSet 0
- OpDecorate %16 Binding 0
- OpDecorate %21 RelaxedPrecision
- OpDecorate %35 RelaxedPrecision
- OpDecorate %36 RelaxedPrecision
- OpDecorate %39 RelaxedPrecision
- OpDecorate %40 RelaxedPrecision
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeFloat 32
- %7 = OpTypeVector %6 4
- %8 = OpTypePointer Output %7
- %9 = OpVariable %8 Output
- %10 = OpConstant %6 1
- %11 = OpConstantComposite %7 %10 %10 %10 %10
- %14 = OpTypeStruct %6
- %15 = OpTypePointer Uniform %14
- %16 = OpVariable %15 Uniform
- %17 = OpTypeInt 32 1
- %18 = OpConstant %17 0
- %19 = OpTypePointer Uniform %6
- %28 = OpConstant %6 2
- %29 = OpTypeBool
- %31 = OpTypeInt 32 0
- %32 = OpConstant %31 0
- %33 = OpTypePointer Output %6
- %41 = OpUndef %6 ; Added
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpStore %9 %11
- %20 = OpAccessChain %19 %16 %18
- %21 = OpLoad %6 %20
- OpBranch %22
- %22 = OpLabel
- %40 = OpPhi %6 %21 %5 %41 %23 ; Changed
- %30 = OpFOrdLessThan %29 %40 %28
- OpSelectionMerge %24 None ; Changed
- OpBranchConditional %30 %24 %24
- %23 = OpLabel
- %34 = OpAccessChain %33 %9 %32
- %35 = OpLoad %6 %34
- %36 = OpFAdd %6 %35 %10
- OpStore %34 %36
- %39 = OpFAdd %6 %41 %10 ; Changed
- OpBranch %22
- %24 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- CheckEqual(env, after_op_0, context.get());
- }
- TEST(StructuredLoopToSelectionReductionPassTest,
- LoopWithCombinedHeaderAndContinue) {
- // A shader containing a loop where the header is also the continue target.
- // For now, we don't simplify such loops.
- std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 310
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeBool
- %30 = OpConstantFalse %6
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpBranch %10
- %10 = OpLabel ; loop header and continue target
- OpLoopMerge %12 %10 None
- OpBranchConditional %30 %10 %12
- %12 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const auto env = SPV_ENV_UNIVERSAL_1_3;
- const auto context = BuildModule(env, nullptr, shader, kReduceAssembleOption);
- const auto ops = StructuredLoopToSelectionReductionOpportunityFinder()
- .GetAvailableOpportunities(context.get(), 0);
- ASSERT_EQ(0, ops.size());
- }
- } // namespace
- } // namespace reduce
- } // namespace spvtools
|