| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828 |
- // Copyright (c) 2018 Google LLC.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- #include <memory>
- #include <string>
- #include <vector>
- #include "gmock/gmock.h"
- #include "source/opt/loop_unroller.h"
- #include "source/opt/loop_utils.h"
- #include "source/opt/pass.h"
- #include "test/opt/assembly_builder.h"
- #include "test/opt/function_utils.h"
- #include "test/opt/pass_fixture.h"
- #include "test/opt/pass_utils.h"
- namespace spvtools {
- namespace opt {
- namespace {
- using ::testing::UnorderedElementsAre;
- using PassClassTest = PassTest<::testing::Test>;
- /*
- Generated from the following GLSL
- #version 330 core
- layout(location = 0) out vec4 c;
- void main() {
- float x[4];
- for (int i = 0; i < 4; ++i) {
- x[i] = 1.0f;
- }
- }
- */
- TEST_F(PassClassTest, SimpleFullyUnrollTest) {
- // With LocalMultiStoreElimPass
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 330
- OpName %2 "main"
- OpName %5 "x"
- OpName %3 "c"
- OpDecorate %3 Location 0
- %6 = OpTypeVoid
- %7 = OpTypeFunction %6
- %8 = OpTypeInt 32 1
- %9 = OpTypePointer Function %8
- %10 = OpConstant %8 0
- %11 = OpConstant %8 4
- %12 = OpTypeBool
- %13 = OpTypeFloat 32
- %14 = OpTypeInt 32 0
- %15 = OpConstant %14 4
- %16 = OpTypeArray %13 %15
- %17 = OpTypePointer Function %16
- %18 = OpConstant %13 1
- %19 = OpTypePointer Function %13
- %20 = OpConstant %8 1
- %21 = OpTypeVector %13 4
- %22 = OpTypePointer Output %21
- %3 = OpVariable %22 Output
- %2 = OpFunction %6 None %7
- %23 = OpLabel
- %5 = OpVariable %17 Function
- OpBranch %24
- %24 = OpLabel
- %35 = OpPhi %8 %10 %23 %34 %26
- OpLoopMerge %25 %26 Unroll
- OpBranch %27
- %27 = OpLabel
- %29 = OpSLessThan %12 %35 %11
- OpBranchConditional %29 %30 %25
- %30 = OpLabel
- %32 = OpAccessChain %19 %5 %35
- OpStore %32 %18
- OpBranch %26
- %26 = OpLabel
- %34 = OpIAdd %8 %35 %20
- OpBranch %24
- %25 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const std::string output = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 330
- OpName %2 "main"
- OpName %4 "x"
- OpName %3 "c"
- OpDecorate %3 Location 0
- %5 = OpTypeVoid
- %6 = OpTypeFunction %5
- %7 = OpTypeInt 32 1
- %8 = OpTypePointer Function %7
- %9 = OpConstant %7 0
- %10 = OpConstant %7 4
- %11 = OpTypeBool
- %12 = OpTypeFloat 32
- %13 = OpTypeInt 32 0
- %14 = OpConstant %13 4
- %15 = OpTypeArray %12 %14
- %16 = OpTypePointer Function %15
- %17 = OpConstant %12 1
- %18 = OpTypePointer Function %12
- %19 = OpConstant %7 1
- %20 = OpTypeVector %12 4
- %21 = OpTypePointer Output %20
- %3 = OpVariable %21 Output
- %2 = OpFunction %5 None %6
- %22 = OpLabel
- %4 = OpVariable %16 Function
- OpBranch %23
- %23 = OpLabel
- OpBranch %28
- %28 = OpLabel
- %29 = OpSLessThan %11 %9 %10
- OpBranch %30
- %30 = OpLabel
- %31 = OpAccessChain %18 %4 %9
- OpStore %31 %17
- OpBranch %26
- %26 = OpLabel
- %25 = OpIAdd %7 %9 %19
- OpBranch %32
- %32 = OpLabel
- OpBranch %34
- %34 = OpLabel
- %35 = OpSLessThan %11 %25 %10
- OpBranch %36
- %36 = OpLabel
- %37 = OpAccessChain %18 %4 %25
- OpStore %37 %17
- OpBranch %38
- %38 = OpLabel
- %39 = OpIAdd %7 %25 %19
- OpBranch %40
- %40 = OpLabel
- OpBranch %42
- %42 = OpLabel
- %43 = OpSLessThan %11 %39 %10
- OpBranch %44
- %44 = OpLabel
- %45 = OpAccessChain %18 %4 %39
- OpStore %45 %17
- OpBranch %46
- %46 = OpLabel
- %47 = OpIAdd %7 %39 %19
- OpBranch %48
- %48 = OpLabel
- OpBranch %50
- %50 = OpLabel
- %51 = OpSLessThan %11 %47 %10
- OpBranch %52
- %52 = OpLabel
- %53 = OpAccessChain %18 %4 %47
- OpStore %53 %17
- OpBranch %54
- %54 = OpLabel
- %55 = OpIAdd %7 %47 %19
- OpBranch %27
- %27 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<LoopUnroller>(text, output, false);
- }
- /*
- Generated from the following GLSL
- #version 330 core
- layout(location = 0) out vec4 c;
- void main() {
- float x[4];
- for (int i = 0; i < 4; ++i) {
- x[i] = 1.0f;
- }
- }
- */
- TEST_F(PassClassTest, SimpleFullyUnrollWithDebugInstructions) {
- // We must preserve the debug information including OpenCL.DebugInfo.100
- // instructions and OpLine instructions. Only the first block has
- // DebugDeclare and DebugValue used for the declaration (i.e., DebugValue
- // with Deref). Other blocks unrolled from the loop must not contain them.
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- %ext = OpExtInstImport "OpenCL.DebugInfo.100"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 330
- %file_name = OpString "test"
- %float_name = OpString "float"
- %main_name = OpString "main"
- %f_name = OpString "f"
- %i_name = OpString "i"
- OpName %2 "main"
- OpName %5 "x"
- OpName %3 "c"
- OpDecorate %3 Location 0
- %6 = OpTypeVoid
- %7 = OpTypeFunction %6
- %8 = OpTypeInt 32 1
- %9 = OpTypePointer Function %8
- %10 = OpConstant %8 0
- %11 = OpConstant %8 4
- %12 = OpTypeBool
- %13 = OpTypeFloat 32
- %14 = OpTypeInt 32 0
- %uint_32 = OpConstant %14 32
- %15 = OpConstant %14 4
- %16 = OpTypeArray %13 %15
- %17 = OpTypePointer Function %16
- %18 = OpConstant %13 1
- %19 = OpTypePointer Function %13
- %20 = OpConstant %8 1
- %21 = OpTypeVector %13 4
- %22 = OpTypePointer Output %21
- %3 = OpVariable %22 Output
- %null_expr = OpExtInst %6 %ext DebugExpression
- %deref = OpExtInst %6 %ext DebugOperation Deref
- %deref_expr = OpExtInst %6 %ext DebugExpression %deref
- %src = OpExtInst %6 %ext DebugSource %file_name
- %cu = OpExtInst %6 %ext DebugCompilationUnit 1 4 %src HLSL
- %dbg_tf = OpExtInst %6 %ext DebugTypeBasic %float_name %uint_32 Float
- %dbg_v4f = OpExtInst %6 %ext DebugTypeVector %dbg_tf 4
- %main_ty = OpExtInst %6 %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
- %dbg_main = OpExtInst %6 %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %2
- %bb = OpExtInst %6 %ext DebugLexicalBlock %src 0 0 %dbg_main
- %dbg_f = OpExtInst %6 %ext DebugLocalVariable %f_name %dbg_v4f %src 0 0 %dbg_main FlagIsLocal
- %dbg_i = OpExtInst %6 %ext DebugLocalVariable %i_name %dbg_v4f %src 1 0 %bb FlagIsLocal
- ; CHECK: [[f:%\w+]] = OpString "f"
- ; CHECK: [[i:%\w+]] = OpString "i"
- ; CHECK: [[int_0:%\w+]] = OpConstant {{%\w+}} 0
- ; CHECK: [[null_expr:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugExpression
- ; CHECK: [[deref:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugOperation Deref
- ; CHECK: [[deref_expr:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugExpression [[deref]]
- ; CHECK: [[dbg_fn:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugFunction
- ; CHECK: [[dbg_bb:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugLexicalBlock
- ; CHECK: [[dbg_f:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugLocalVariable [[f]] {{%\w+}} {{%\w+}} 0 0 [[dbg_fn]]
- ; CHECK: [[dbg_i:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugLocalVariable [[i]] {{%\w+}} {{%\w+}} 1 0 [[dbg_bb]]
- %2 = OpFunction %6 None %7
- %23 = OpLabel
- ; The first block has DebugDeclare and DebugValue with Deref
- ;
- ; CHECK: OpLabel
- ; CHECK: DebugScope [[dbg_fn]]
- ; CHECK: [[x:%\w+]] = OpVariable {{%\w+}} Function
- ; CHECK: OpLine {{%\w+}} 0 0
- ; CHECK: OpBranch
- ; CHECK: OpLabel
- ; CHECK: DebugScope [[dbg_fn]]
- ; CHECK: DebugValue [[dbg_f]] [[int_0]] [[null_expr]]
- ; CHECK: OpBranch
- ; CHECK: DebugScope [[dbg_fn]]
- ; CHECK: OpLine {{%\w+}} 1 1
- ; CHECK: OpSLessThan
- ; CHECK: OpLine {{%\w+}} 2 0
- ; CHECK: OpBranch
- ; CHECK: OpLabel
- ; CHECK: DebugScope [[dbg_bb]]
- ; CHECK: DebugDeclare [[dbg_f]] [[x]] [[null_expr]]
- ; CHECK: DebugValue [[dbg_i]] [[x]] [[deref_expr]]
- ; CHECK: OpLine {{%\w+}} 3 0
- ;
- ; CHECK: OpLine {{%\w+}} 6 0
- ; CHECK: [[add:%\w+]] = OpIAdd
- ; CHECK: DebugValue [[dbg_f]] [[add]] [[null_expr]]
- ; CHECK: OpLine {{%\w+}} 7 0
- ; Other blocks do not have DebugDeclare and DebugValue with Deref
- ;
- ; CHECK: DebugScope [[dbg_fn]]
- ; CHECK: OpLine {{%\w+}} 1 1
- ; CHECK: OpSLessThan
- ; CHECK: OpLine {{%\w+}} 2 0
- ; CHECK: OpBranch
- ; CHECK: OpLabel
- ;
- ; CHECK: DebugScope [[dbg_bb]]
- ; CHECK-NOT: DebugDeclare [[dbg_f]] [[x]] [[null_expr]]
- ; CHECK-NOT: DebugValue [[dbg_i]] [[x]] [[deref_expr]]
- ; CHECK: OpLine {{%\w+}} 3 0
- ;
- ; CHECK: OpLine {{%\w+}} 6 0
- ; CHECK: [[add:%\w+]] = OpIAdd
- ; CHECK: DebugValue [[dbg_f]] [[add]] [[null_expr]]
- ; CHECK: OpLine {{%\w+}} 7 0
- ;
- ; CHECK-NOT: DebugDeclare [[dbg_f]] [[x]] [[null_expr]]
- ; CHECK-NOT: DebugValue [[dbg_i]] [[x]] [[deref_expr]]
- ; CHECK: DebugScope [[dbg_fn]]
- ; CHECK: OpLine {{%\w+}} 8 0
- ; CHECK: OpReturn
- %s0 = OpExtInst %6 %ext DebugScope %dbg_main
- %5 = OpVariable %17 Function
- OpLine %file_name 0 0
- OpBranch %24
- %24 = OpLabel
- %s1 = OpExtInst %6 %ext DebugScope %dbg_main
- %35 = OpPhi %8 %10 %23 %34 %26
- %value0 = OpExtInst %6 %ext DebugValue %dbg_f %35 %null_expr
- OpLine %file_name 1 0
- OpLoopMerge %25 %26 Unroll
- OpBranch %27
- %27 = OpLabel
- %s2 = OpExtInst %6 %ext DebugScope %dbg_main
- OpLine %file_name 1 1
- %29 = OpSLessThan %12 %35 %11
- OpLine %file_name 2 0
- OpBranchConditional %29 %30 %25
- %30 = OpLabel
- %s3 = OpExtInst %6 %ext DebugScope %bb
- %decl0 = OpExtInst %6 %ext DebugDeclare %dbg_f %5 %null_expr
- %decl1 = OpExtInst %6 %ext DebugValue %dbg_i %5 %deref_expr
- OpLine %file_name 3 0
- %32 = OpAccessChain %19 %5 %35
- OpLine %file_name 4 0
- OpStore %32 %18
- OpLine %file_name 5 0
- OpBranch %26
- %26 = OpLabel
- %s4 = OpExtInst %6 %ext DebugScope %dbg_main
- OpLine %file_name 6 0
- %34 = OpIAdd %8 %35 %20
- %value1 = OpExtInst %6 %ext DebugValue %dbg_f %34 %null_expr
- OpLine %file_name 7 0
- OpBranch %24
- %25 = OpLabel
- %s5 = OpExtInst %6 %ext DebugScope %dbg_main
- OpLine %file_name 8 0
- OpReturn
- OpFunctionEnd)";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndMatch<LoopUnroller>(text, true);
- }
- TEST_F(PassClassTest, SimpleFullyUnrollWithShaderDebugInstructions) {
- // We must preserve the debug information including
- // NonSemantic.Shader.DebugInfo.100 instructions and DebugLine instructions.
- const std::string text = R"(
- OpCapability Shader
- OpExtension "SPV_KHR_non_semantic_info"
- %1 = OpExtInstImport "GLSL.std.450"
- %ext = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 330
- %file_name = OpString "test"
- %float_name = OpString "float"
- %main_name = OpString "main"
- %f_name = OpString "f"
- %i_name = OpString "i"
- OpName %2 "main"
- OpName %5 "x"
- OpName %3 "c"
- OpDecorate %3 Location 0
- %6 = OpTypeVoid
- %7 = OpTypeFunction %6
- %8 = OpTypeInt 32 1
- %9 = OpTypePointer Function %8
- %10 = OpConstant %8 0
- %11 = OpConstant %8 4
- %12 = OpTypeBool
- %13 = OpTypeFloat 32
- %14 = OpTypeInt 32 0
- %uint_0 = OpConstant %14 0
- %uint_1 = OpConstant %14 1
- %uint_2 = OpConstant %14 2
- %uint_3 = OpConstant %14 3
- %uint_4 = OpConstant %14 4
- %uint_5 = OpConstant %14 5
- %uint_6 = OpConstant %14 6
- %uint_7 = OpConstant %14 7
- %uint_8 = OpConstant %14 8
- %uint_10 = OpConstant %14 10
- %uint_32 = OpConstant %14 32
- %15 = OpConstant %14 4
- %16 = OpTypeArray %13 %15
- %17 = OpTypePointer Function %16
- %18 = OpConstant %13 1
- %19 = OpTypePointer Function %13
- %20 = OpConstant %8 1
- %21 = OpTypeVector %13 4
- %22 = OpTypePointer Output %21
- %3 = OpVariable %22 Output
- %null_expr = OpExtInst %6 %ext DebugExpression
- %deref = OpExtInst %6 %ext DebugOperation %uint_0
- %deref_expr = OpExtInst %6 %ext DebugExpression %deref
- %src = OpExtInst %6 %ext DebugSource %file_name
- %cu = OpExtInst %6 %ext DebugCompilationUnit %uint_1 %uint_4 %src %uint_5
- %dbg_tf = OpExtInst %6 %ext DebugTypeBasic %float_name %uint_32 %uint_3 %uint_0
- %dbg_v4f = OpExtInst %6 %ext DebugTypeVector %dbg_tf %uint_4
- %main_ty = OpExtInst %6 %ext DebugTypeFunction %uint_3 %dbg_v4f %dbg_v4f
- %dbg_main = OpExtInst %6 %ext DebugFunction %main_name %main_ty %src %uint_0 %uint_0 %cu %main_name %uint_3 %uint_10
- %bb = OpExtInst %6 %ext DebugLexicalBlock %src %uint_0 %uint_0 %dbg_main
- %dbg_f = OpExtInst %6 %ext DebugLocalVariable %f_name %dbg_v4f %src %uint_0 %uint_0 %dbg_main %uint_4
- %dbg_i = OpExtInst %6 %ext DebugLocalVariable %i_name %dbg_v4f %src %uint_1 %uint_0 %bb %uint_4
- ; CHECK: [[f:%\w+]] = OpString "f"
- ; CHECK: [[i:%\w+]] = OpString "i"
- ; CHECK: [[int_0:%\w+]] = OpConstant {{%\w+}} 0
- ; CHECK: [[null_expr:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugExpression
- ; CHECK: [[deref:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugOperation %uint_0
- ; CHECK: [[deref_expr:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugExpression [[deref]]
- ; CHECK: [[dbg_fn:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugFunction
- ; CHECK: [[dbg_bb:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugLexicalBlock
- ; CHECK: [[dbg_f:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugLocalVariable [[f]] {{%\w+}} {{%\w+}} %uint_0 %uint_0 [[dbg_fn]]
- ; CHECK: [[dbg_i:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugLocalVariable [[i]] {{%\w+}} {{%\w+}} %uint_1 %uint_0 [[dbg_bb]]
- %2 = OpFunction %6 None %7
- %23 = OpLabel
- ; The first block has DebugDeclare and DebugValue with Deref
- ;
- ; CHECK: OpLabel
- ; CHECK: %x = OpVariable %_ptr_Function__arr_float_uint_4_0 Function
- ; CHECK: OpBranch
- ; CHECK: OpLabel
- ; CHECK: DebugScope [[dbg_fn]]
- ; CHECK: DebugValue [[dbg_f]] [[int_0]] [[null_expr]]
- ; CHECK: OpBranch
- ; CHECK: DebugScope [[dbg_fn]]
- ; CHECK: DebugLine {{%\w+}} %uint_1 %uint_1 %uint_1 %uint_1
- ; CHECK: OpSLessThan
- ; CHECK: DebugLine {{%\w+}} %uint_2 %uint_2 %uint_0 %uint_0
- ; CHECK: OpBranch
- ; CHECK: OpLabel
- ; CHECK: DebugScope [[dbg_bb]]
- ; CHECK: DebugDeclare [[dbg_f]] %x [[null_expr]]
- ; CHECK: DebugValue [[dbg_i]] %x [[deref_expr]]
- ; CHECK: DebugLine {{%\w+}} %uint_3 %uint_3 %uint_0 %uint_0
- ;
- ; CHECK: DebugLine {{%\w+}} %uint_6 %uint_6 %uint_0 %uint_0
- ; CHECK: [[add:%\w+]] = OpIAdd
- ; CHECK: DebugValue [[dbg_f]] [[add]] [[null_expr]]
- ; CHECK: DebugLine {{%\w+}} %uint_7 %uint_7 %uint_0 %uint_0
- ; Other blocks do not have DebugDeclare and DebugValue with Deref
- ;
- ; CHECK: DebugScope [[dbg_fn]]
- ; CHECK: DebugLine {{%\w+}} %uint_1 %uint_1 %uint_1 %uint_1
- ; CHECK: OpSLessThan
- ; CHECK: DebugLine {{%\w+}} %uint_2 %uint_2 %uint_0 %uint_0
- ; CHECK: OpBranch
- ; CHECK: OpLabel
- ;
- ; CHECK: DebugScope [[dbg_bb]]
- ; CHECK-NOT: DebugDeclare [[dbg_f]] %x [[null_expr]]
- ; CHECK-NOT: DebugValue [[dbg_i]] %x [[deref_expr]]
- ; CHECK: DebugLine {{%\w+}} %uint_3 %uint_3 %uint_0 %uint_0
- ;
- ; CHECK: DebugLine {{%\w+}} %uint_6 %uint_6 %uint_0 %uint_0
- ; CHECK: [[add:%\w+]] = OpIAdd
- ; CHECK: DebugValue [[dbg_f]] [[add]] [[null_expr]]
- ; CHECK: DebugLine {{%\w+}} %uint_7 %uint_7 %uint_0 %uint_0
- ;
- ; CHECK-NOT: DebugDeclare [[dbg_f]] %x [[null_expr]]
- ; CHECK-NOT: DebugValue [[dbg_i]] %x [[deref_expr]]
- ; CHECK: DebugScope [[dbg_fn]]
- ; CHECK: DebugLine {{%\w+}} %uint_8 %uint_8 %uint_0 %uint_0
- ; CHECK: OpReturn
- %5 = OpVariable %17 Function
- OpBranch %24
- %24 = OpLabel
- %35 = OpPhi %8 %10 %23 %34 %26
- %s1 = OpExtInst %6 %ext DebugScope %dbg_main
- %d10 = OpExtInst %6 %ext DebugLine %file_name %uint_1 %uint_1 %uint_0 %uint_0
- %value0 = OpExtInst %6 %ext DebugValue %dbg_f %35 %null_expr
- OpLoopMerge %25 %26 Unroll
- OpBranch %27
- %27 = OpLabel
- %s2 = OpExtInst %6 %ext DebugScope %dbg_main
- %d1 = OpExtInst %6 %ext DebugLine %file_name %uint_1 %uint_1 %uint_1 %uint_1
- %29 = OpSLessThan %12 %35 %11
- %d2 = OpExtInst %6 %ext DebugLine %file_name %uint_2 %uint_2 %uint_0 %uint_0
- OpBranchConditional %29 %30 %25
- %30 = OpLabel
- %s3 = OpExtInst %6 %ext DebugScope %bb
- %decl0 = OpExtInst %6 %ext DebugDeclare %dbg_f %5 %null_expr
- %decl1 = OpExtInst %6 %ext DebugValue %dbg_i %5 %deref_expr
- %d3 = OpExtInst %6 %ext DebugLine %file_name %uint_3 %uint_3 %uint_0 %uint_0
- %32 = OpAccessChain %19 %5 %35
- %d4 = OpExtInst %6 %ext DebugLine %file_name %uint_4 %uint_4 %uint_0 %uint_0
- OpStore %32 %18
- %d5 = OpExtInst %6 %ext DebugLine %file_name %uint_5 %uint_5 %uint_0 %uint_0
- OpBranch %26
- %26 = OpLabel
- %s4 = OpExtInst %6 %ext DebugScope %dbg_main
- %d6 = OpExtInst %6 %ext DebugLine %file_name %uint_6 %uint_6 %uint_0 %uint_0
- %34 = OpIAdd %8 %35 %20
- %value1 = OpExtInst %6 %ext DebugValue %dbg_f %34 %null_expr
- %d7 = OpExtInst %6 %ext DebugLine %file_name %uint_7 %uint_7 %uint_0 %uint_0
- OpBranch %24
- %25 = OpLabel
- %s5 = OpExtInst %6 %ext DebugScope %dbg_main
- %d8 = OpExtInst %6 %ext DebugLine %file_name %uint_8 %uint_8 %uint_0 %uint_0
- OpReturn
- OpFunctionEnd)";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
- SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
- SinglePassRunAndMatch<LoopUnroller>(text, true);
- }
- template <int factor>
- class PartialUnrollerTestPass : public Pass {
- public:
- PartialUnrollerTestPass() : Pass() {}
- const char* name() const override { return "Loop unroller"; }
- Status Process() override {
- for (Function& f : *context()->module()) {
- LoopDescriptor& loop_descriptor = *context()->GetLoopDescriptor(&f);
- for (auto& loop : loop_descriptor) {
- LoopUtils loop_utils{context(), &loop};
- loop_utils.PartiallyUnroll(factor);
- }
- }
- return Pass::Status::SuccessWithChange;
- }
- };
- /*
- Generated from the following GLSL
- #version 330 core
- layout(location = 0) out vec4 c;
- void main() {
- float x[10];
- for (int i = 0; i < 10; ++i) {
- x[i] = 1.0f;
- }
- }
- */
- TEST_F(PassClassTest, SimplePartialUnroll) {
- // With LocalMultiStoreElimPass
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 330
- OpName %2 "main"
- OpName %5 "x"
- OpName %3 "c"
- OpDecorate %3 Location 0
- %6 = OpTypeVoid
- %7 = OpTypeFunction %6
- %8 = OpTypeInt 32 1
- %9 = OpTypePointer Function %8
- %10 = OpConstant %8 0
- %11 = OpConstant %8 10
- %12 = OpTypeBool
- %13 = OpTypeFloat 32
- %14 = OpTypeInt 32 0
- %15 = OpConstant %14 10
- %16 = OpTypeArray %13 %15
- %17 = OpTypePointer Function %16
- %18 = OpConstant %13 1
- %19 = OpTypePointer Function %13
- %20 = OpConstant %8 1
- %21 = OpTypeVector %13 4
- %22 = OpTypePointer Output %21
- %3 = OpVariable %22 Output
- %2 = OpFunction %6 None %7
- %23 = OpLabel
- %5 = OpVariable %17 Function
- OpBranch %24
- %24 = OpLabel
- %35 = OpPhi %8 %10 %23 %34 %26
- OpLoopMerge %25 %26 Unroll
- OpBranch %27
- %27 = OpLabel
- %29 = OpSLessThan %12 %35 %11
- OpBranchConditional %29 %30 %25
- %30 = OpLabel
- %32 = OpAccessChain %19 %5 %35
- OpStore %32 %18
- OpBranch %26
- %26 = OpLabel
- %34 = OpIAdd %8 %35 %20
- OpBranch %24
- %25 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const std::string output = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 330
- OpName %2 "main"
- OpName %4 "x"
- OpName %3 "c"
- OpDecorate %3 Location 0
- %5 = OpTypeVoid
- %6 = OpTypeFunction %5
- %7 = OpTypeInt 32 1
- %8 = OpTypePointer Function %7
- %9 = OpConstant %7 0
- %10 = OpConstant %7 10
- %11 = OpTypeBool
- %12 = OpTypeFloat 32
- %13 = OpTypeInt 32 0
- %14 = OpConstant %13 10
- %15 = OpTypeArray %12 %14
- %16 = OpTypePointer Function %15
- %17 = OpConstant %12 1
- %18 = OpTypePointer Function %12
- %19 = OpConstant %7 1
- %20 = OpTypeVector %12 4
- %21 = OpTypePointer Output %20
- %3 = OpVariable %21 Output
- %2 = OpFunction %5 None %6
- %22 = OpLabel
- %4 = OpVariable %16 Function
- OpBranch %23
- %23 = OpLabel
- %24 = OpPhi %7 %9 %22 %39 %38
- OpLoopMerge %27 %38 DontUnroll
- OpBranch %28
- %28 = OpLabel
- %29 = OpSLessThan %11 %24 %10
- OpBranchConditional %29 %30 %27
- %30 = OpLabel
- %31 = OpAccessChain %18 %4 %24
- OpStore %31 %17
- OpBranch %26
- %26 = OpLabel
- %25 = OpIAdd %7 %24 %19
- OpBranch %32
- %32 = OpLabel
- OpBranch %34
- %34 = OpLabel
- %35 = OpSLessThan %11 %25 %10
- OpBranch %36
- %36 = OpLabel
- %37 = OpAccessChain %18 %4 %25
- OpStore %37 %17
- OpBranch %38
- %38 = OpLabel
- %39 = OpIAdd %7 %25 %19
- OpBranch %23
- %27 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<PartialUnrollerTestPass<2>>(text, output, false);
- }
- /*
- Generated from the following GLSL
- #version 330 core
- layout(location = 0) out vec4 c;
- void main() {
- float x[10];
- for (int i = 0; i < 10; ++i) {
- x[i] = 1.0f;
- }
- }
- */
- TEST_F(PassClassTest, SimpleUnevenPartialUnroll) {
- // With LocalMultiStoreElimPass
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 330
- OpName %2 "main"
- OpName %5 "x"
- OpName %3 "c"
- OpDecorate %3 Location 0
- %6 = OpTypeVoid
- %7 = OpTypeFunction %6
- %8 = OpTypeInt 32 1
- %9 = OpTypePointer Function %8
- %10 = OpConstant %8 0
- %11 = OpConstant %8 10
- %12 = OpTypeBool
- %13 = OpTypeFloat 32
- %14 = OpTypeInt 32 0
- %15 = OpConstant %14 10
- %16 = OpTypeArray %13 %15
- %17 = OpTypePointer Function %16
- %18 = OpConstant %13 1
- %19 = OpTypePointer Function %13
- %20 = OpConstant %8 1
- %21 = OpTypeVector %13 4
- %22 = OpTypePointer Output %21
- %3 = OpVariable %22 Output
- %2 = OpFunction %6 None %7
- %23 = OpLabel
- %5 = OpVariable %17 Function
- OpBranch %24
- %24 = OpLabel
- %35 = OpPhi %8 %10 %23 %34 %26
- OpLoopMerge %25 %26 Unroll
- OpBranch %27
- %27 = OpLabel
- %29 = OpSLessThan %12 %35 %11
- OpBranchConditional %29 %30 %25
- %30 = OpLabel
- %32 = OpAccessChain %19 %5 %35
- OpStore %32 %18
- OpBranch %26
- %26 = OpLabel
- %34 = OpIAdd %8 %35 %20
- OpBranch %24
- %25 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const std::string output = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 330
- OpName %2 "main"
- OpName %4 "x"
- OpName %3 "c"
- OpDecorate %3 Location 0
- %5 = OpTypeVoid
- %6 = OpTypeFunction %5
- %7 = OpTypeInt 32 1
- %8 = OpTypePointer Function %7
- %9 = OpConstant %7 0
- %10 = OpConstant %7 10
- %11 = OpTypeBool
- %12 = OpTypeFloat 32
- %13 = OpTypeInt 32 0
- %14 = OpConstant %13 10
- %15 = OpTypeArray %12 %14
- %16 = OpTypePointer Function %15
- %17 = OpConstant %12 1
- %18 = OpTypePointer Function %12
- %19 = OpConstant %7 1
- %20 = OpTypeVector %12 4
- %21 = OpTypePointer Output %20
- %3 = OpVariable %21 Output
- %58 = OpConstant %13 1
- %2 = OpFunction %5 None %6
- %22 = OpLabel
- %4 = OpVariable %16 Function
- OpBranch %23
- %23 = OpLabel
- %24 = OpPhi %7 %9 %22 %25 %26
- OpLoopMerge %32 %26 Unroll
- OpBranch %28
- %28 = OpLabel
- %29 = OpSLessThan %11 %24 %58
- OpBranchConditional %29 %30 %32
- %30 = OpLabel
- %31 = OpAccessChain %18 %4 %24
- OpStore %31 %17
- OpBranch %26
- %26 = OpLabel
- %25 = OpIAdd %7 %24 %19
- OpBranch %23
- %32 = OpLabel
- OpBranch %33
- %33 = OpLabel
- %34 = OpPhi %7 %24 %32 %57 %56
- OpLoopMerge %41 %56 DontUnroll
- OpBranch %35
- %35 = OpLabel
- %36 = OpSLessThan %11 %34 %10
- OpBranchConditional %36 %37 %41
- %37 = OpLabel
- %38 = OpAccessChain %18 %4 %34
- OpStore %38 %17
- OpBranch %39
- %39 = OpLabel
- %40 = OpIAdd %7 %34 %19
- OpBranch %42
- %42 = OpLabel
- OpBranch %44
- %44 = OpLabel
- %45 = OpSLessThan %11 %40 %10
- OpBranch %46
- %46 = OpLabel
- %47 = OpAccessChain %18 %4 %40
- OpStore %47 %17
- OpBranch %48
- %48 = OpLabel
- %49 = OpIAdd %7 %40 %19
- OpBranch %50
- %50 = OpLabel
- OpBranch %52
- %52 = OpLabel
- %53 = OpSLessThan %11 %49 %10
- OpBranch %54
- %54 = OpLabel
- %55 = OpAccessChain %18 %4 %49
- OpStore %55 %17
- OpBranch %56
- %56 = OpLabel
- %57 = OpIAdd %7 %49 %19
- OpBranch %33
- %41 = OpLabel
- OpReturn
- %27 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- // By unrolling by a factor that doesn't divide evenly into the number of loop
- // iterations we perform an additional transform when partially unrolling to
- // account for the remainder.
- SinglePassRunAndCheck<PartialUnrollerTestPass<3>>(text, output, false);
- }
- /* Generated from
- #version 410 core
- layout(location=0) flat in int upper_bound;
- void main() {
- float x[10];
- for (int i = 2; i < 8; i+=2) {
- x[i] = i;
- }
- }
- */
- TEST_F(PassClassTest, SimpleLoopIterationsCheck) {
- // With LocalMultiStoreElimPass
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 410
- OpName %2 "main"
- OpName %5 "x"
- OpName %3 "upper_bound"
- OpDecorate %3 Flat
- OpDecorate %3 Location 0
- %6 = OpTypeVoid
- %7 = OpTypeFunction %6
- %8 = OpTypeInt 32 1
- %9 = OpTypePointer Function %8
- %10 = OpConstant %8 2
- %11 = OpConstant %8 8
- %12 = OpTypeBool
- %13 = OpTypeFloat 32
- %14 = OpTypeInt 32 0
- %15 = OpConstant %14 10
- %16 = OpTypeArray %13 %15
- %17 = OpTypePointer Function %16
- %18 = OpTypePointer Function %13
- %19 = OpTypePointer Input %8
- %3 = OpVariable %19 Input
- %2 = OpFunction %6 None %7
- %20 = OpLabel
- %5 = OpVariable %17 Function
- OpBranch %21
- %21 = OpLabel
- %34 = OpPhi %8 %10 %20 %33 %23
- OpLoopMerge %22 %23 Unroll
- OpBranch %24
- %24 = OpLabel
- %26 = OpSLessThan %12 %34 %11
- OpBranchConditional %26 %27 %22
- %27 = OpLabel
- %30 = OpConvertSToF %13 %34
- %31 = OpAccessChain %18 %5 %34
- OpStore %31 %30
- OpBranch %23
- %23 = OpLabel
- %33 = OpIAdd %8 %34 %10
- OpBranch %21
- %22 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << text << std::endl;
- Function* f = spvtest::GetFunction(module, 2);
- LoopDescriptor& loop_descriptor = *context->GetLoopDescriptor(f);
- EXPECT_EQ(loop_descriptor.NumLoops(), 1u);
- Loop& loop = loop_descriptor.GetLoopByIndex(0);
- EXPECT_TRUE(loop.HasUnrollLoopControl());
- BasicBlock* condition = loop.FindConditionBlock();
- EXPECT_EQ(condition->id(), 24u);
- Instruction* induction = loop.FindConditionVariable(condition);
- EXPECT_EQ(induction->result_id(), 34u);
- LoopUtils loop_utils{context.get(), &loop};
- EXPECT_TRUE(loop_utils.CanPerformUnroll());
- size_t iterations = 0;
- EXPECT_TRUE(loop.FindNumberOfIterations(induction, &*condition->ctail(),
- &iterations));
- EXPECT_EQ(iterations, 3u);
- }
- /* Generated from
- #version 410 core
- void main() {
- float x[10];
- for (int i = -1; i < 6; i+=3) {
- x[i] = i;
- }
- }
- */
- TEST_F(PassClassTest, SimpleLoopIterationsCheckSignedInit) {
- // With LocalMultiStoreElimPass
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main" %3
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 410
- OpName %2 "main"
- OpName %5 "x"
- OpName %3 "upper_bound"
- OpDecorate %3 Flat
- OpDecorate %3 Location 0
- %6 = OpTypeVoid
- %7 = OpTypeFunction %6
- %8 = OpTypeInt 32 1
- %9 = OpTypePointer Function %8
- %10 = OpConstant %8 -1
- %11 = OpConstant %8 6
- %12 = OpTypeBool
- %13 = OpTypeFloat 32
- %14 = OpTypeInt 32 0
- %15 = OpConstant %14 10
- %16 = OpTypeArray %13 %15
- %17 = OpTypePointer Function %16
- %18 = OpTypePointer Function %13
- %19 = OpConstant %8 3
- %20 = OpTypePointer Input %8
- %3 = OpVariable %20 Input
- %2 = OpFunction %6 None %7
- %21 = OpLabel
- %5 = OpVariable %17 Function
- OpBranch %22
- %22 = OpLabel
- %35 = OpPhi %8 %10 %21 %34 %24
- OpLoopMerge %23 %24 None
- OpBranch %25
- %25 = OpLabel
- %27 = OpSLessThan %12 %35 %11
- OpBranchConditional %27 %28 %23
- %28 = OpLabel
- %31 = OpConvertSToF %13 %35
- %32 = OpAccessChain %18 %5 %35
- OpStore %32 %31
- OpBranch %24
- %24 = OpLabel
- %34 = OpIAdd %8 %35 %19
- OpBranch %22
- %23 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << text << std::endl;
- Function* f = spvtest::GetFunction(module, 2);
- LoopDescriptor& loop_descriptor = *context->GetLoopDescriptor(f);
- EXPECT_EQ(loop_descriptor.NumLoops(), 1u);
- Loop& loop = loop_descriptor.GetLoopByIndex(0);
- EXPECT_FALSE(loop.HasUnrollLoopControl());
- BasicBlock* condition = loop.FindConditionBlock();
- EXPECT_EQ(condition->id(), 25u);
- Instruction* induction = loop.FindConditionVariable(condition);
- EXPECT_EQ(induction->result_id(), 35u);
- LoopUtils loop_utils{context.get(), &loop};
- EXPECT_TRUE(loop_utils.CanPerformUnroll());
- size_t iterations = 0;
- EXPECT_TRUE(loop.FindNumberOfIterations(induction, &*condition->ctail(),
- &iterations));
- EXPECT_EQ(iterations, 3u);
- }
- /*
- Generated from the following GLSL
- #version 410 core
- void main() {
- float out_array[6];
- for (uint i = 0; i < 2; i++) {
- for (int x = 0; x < 3; ++x) {
- out_array[x + i*3] = i;
- }
- }
- }
- */
- TEST_F(PassClassTest, UnrollNestedLoops) {
- // With LocalMultiStoreElimPass
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 410
- OpName %4 "main"
- OpName %35 "out_array"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 0
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 2
- %17 = OpTypeBool
- %19 = OpTypeInt 32 1
- %20 = OpTypePointer Function %19
- %22 = OpConstant %19 0
- %29 = OpConstant %19 3
- %31 = OpTypeFloat 32
- %32 = OpConstant %6 6
- %33 = OpTypeArray %31 %32
- %34 = OpTypePointer Function %33
- %39 = OpConstant %6 3
- %44 = OpTypePointer Function %31
- %47 = OpConstant %19 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %35 = OpVariable %34 Function
- OpBranch %10
- %10 = OpLabel
- %51 = OpPhi %6 %9 %5 %50 %13
- OpLoopMerge %12 %13 Unroll
- OpBranch %14
- %14 = OpLabel
- %18 = OpULessThan %17 %51 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- OpBranch %23
- %23 = OpLabel
- %54 = OpPhi %19 %22 %11 %48 %26
- OpLoopMerge %25 %26 Unroll
- OpBranch %27
- %27 = OpLabel
- %30 = OpSLessThan %17 %54 %29
- OpBranchConditional %30 %24 %25
- %24 = OpLabel
- %37 = OpBitcast %6 %54
- %40 = OpIMul %6 %51 %39
- %41 = OpIAdd %6 %37 %40
- %43 = OpConvertUToF %31 %51
- %45 = OpAccessChain %44 %35 %41
- OpStore %45 %43
- OpBranch %26
- %26 = OpLabel
- %48 = OpIAdd %19 %54 %47
- OpBranch %23
- %25 = OpLabel
- OpBranch %13
- %13 = OpLabel
- %50 = OpIAdd %6 %51 %47
- OpBranch %10
- %12 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const std::string output = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 410
- OpName %2 "main"
- OpName %3 "out_array"
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 0
- %7 = OpTypePointer Function %6
- %8 = OpConstant %6 0
- %9 = OpConstant %6 2
- %10 = OpTypeBool
- %11 = OpTypeInt 32 1
- %12 = OpTypePointer Function %11
- %13 = OpConstant %11 0
- %14 = OpConstant %11 3
- %15 = OpTypeFloat 32
- %16 = OpConstant %6 6
- %17 = OpTypeArray %15 %16
- %18 = OpTypePointer Function %17
- %19 = OpConstant %6 3
- %20 = OpTypePointer Function %15
- %21 = OpConstant %11 1
- %2 = OpFunction %4 None %5
- %22 = OpLabel
- %3 = OpVariable %18 Function
- OpBranch %23
- %23 = OpLabel
- OpBranch %28
- %28 = OpLabel
- %29 = OpULessThan %10 %8 %9
- OpBranch %30
- %30 = OpLabel
- OpBranch %31
- %31 = OpLabel
- OpBranch %36
- %36 = OpLabel
- %37 = OpSLessThan %10 %13 %14
- OpBranch %38
- %38 = OpLabel
- %39 = OpBitcast %6 %13
- %40 = OpIMul %6 %8 %19
- %41 = OpIAdd %6 %39 %40
- %42 = OpConvertUToF %15 %8
- %43 = OpAccessChain %20 %3 %41
- OpStore %43 %42
- OpBranch %34
- %34 = OpLabel
- %33 = OpIAdd %11 %13 %21
- OpBranch %44
- %44 = OpLabel
- OpBranch %46
- %46 = OpLabel
- %47 = OpSLessThan %10 %33 %14
- OpBranch %48
- %48 = OpLabel
- %49 = OpBitcast %6 %33
- %50 = OpIMul %6 %8 %19
- %51 = OpIAdd %6 %49 %50
- %52 = OpConvertUToF %15 %8
- %53 = OpAccessChain %20 %3 %51
- OpStore %53 %52
- OpBranch %54
- %54 = OpLabel
- %55 = OpIAdd %11 %33 %21
- OpBranch %56
- %56 = OpLabel
- OpBranch %58
- %58 = OpLabel
- %59 = OpSLessThan %10 %55 %14
- OpBranch %60
- %60 = OpLabel
- %61 = OpBitcast %6 %55
- %62 = OpIMul %6 %8 %19
- %63 = OpIAdd %6 %61 %62
- %64 = OpConvertUToF %15 %8
- %65 = OpAccessChain %20 %3 %63
- OpStore %65 %64
- OpBranch %66
- %66 = OpLabel
- %67 = OpIAdd %11 %55 %21
- OpBranch %35
- %35 = OpLabel
- OpBranch %26
- %26 = OpLabel
- %25 = OpIAdd %6 %8 %21
- OpBranch %68
- %68 = OpLabel
- OpBranch %70
- %70 = OpLabel
- %71 = OpULessThan %10 %25 %9
- OpBranch %72
- %72 = OpLabel
- OpBranch %73
- %73 = OpLabel
- OpBranch %74
- %74 = OpLabel
- %75 = OpSLessThan %10 %13 %14
- OpBranch %76
- %76 = OpLabel
- %77 = OpBitcast %6 %13
- %78 = OpIMul %6 %25 %19
- %79 = OpIAdd %6 %77 %78
- %80 = OpConvertUToF %15 %25
- %81 = OpAccessChain %20 %3 %79
- OpStore %81 %80
- OpBranch %82
- %82 = OpLabel
- %83 = OpIAdd %11 %13 %21
- OpBranch %84
- %84 = OpLabel
- OpBranch %85
- %85 = OpLabel
- %86 = OpSLessThan %10 %83 %14
- OpBranch %87
- %87 = OpLabel
- %88 = OpBitcast %6 %83
- %89 = OpIMul %6 %25 %19
- %90 = OpIAdd %6 %88 %89
- %91 = OpConvertUToF %15 %25
- %92 = OpAccessChain %20 %3 %90
- OpStore %92 %91
- OpBranch %93
- %93 = OpLabel
- %94 = OpIAdd %11 %83 %21
- OpBranch %95
- %95 = OpLabel
- OpBranch %96
- %96 = OpLabel
- %97 = OpSLessThan %10 %94 %14
- OpBranch %98
- %98 = OpLabel
- %99 = OpBitcast %6 %94
- %100 = OpIMul %6 %25 %19
- %101 = OpIAdd %6 %99 %100
- %102 = OpConvertUToF %15 %25
- %103 = OpAccessChain %20 %3 %101
- OpStore %103 %102
- OpBranch %104
- %104 = OpLabel
- %105 = OpIAdd %11 %94 %21
- OpBranch %106
- %106 = OpLabel
- OpBranch %107
- %107 = OpLabel
- %108 = OpIAdd %6 %25 %21
- OpBranch %27
- %27 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<LoopUnroller>(text, output, false);
- }
- /*
- Generated from the following GLSL
- #version 410 core
- void main() {
- float out_array[2];
- for (int i = -3; i < -1; i++) {
- out_array[3 + i] = i;
- }
- }
- */
- TEST_F(PassClassTest, NegativeConditionAndInit) {
- // With LocalMultiStoreElimPass
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 410
- OpName %4 "main"
- OpName %23 "out_array"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 -3
- %16 = OpConstant %6 -1
- %17 = OpTypeBool
- %19 = OpTypeInt 32 0
- %20 = OpConstant %19 2
- %21 = OpTypeArray %6 %20
- %22 = OpTypePointer Function %21
- %25 = OpConstant %6 3
- %30 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %23 = OpVariable %22 Function
- OpBranch %10
- %10 = OpLabel
- %32 = OpPhi %6 %9 %5 %31 %13
- OpLoopMerge %12 %13 Unroll
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %32 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %26 = OpIAdd %6 %32 %25
- %28 = OpAccessChain %7 %23 %26
- OpStore %28 %32
- OpBranch %13
- %13 = OpLabel
- %31 = OpIAdd %6 %32 %30
- OpBranch %10
- %12 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const std::string expected = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 410
- OpName %2 "main"
- OpName %3 "out_array"
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %8 = OpConstant %6 -3
- %9 = OpConstant %6 -1
- %10 = OpTypeBool
- %11 = OpTypeInt 32 0
- %12 = OpConstant %11 2
- %13 = OpTypeArray %6 %12
- %14 = OpTypePointer Function %13
- %15 = OpConstant %6 3
- %16 = OpConstant %6 1
- %2 = OpFunction %4 None %5
- %17 = OpLabel
- %3 = OpVariable %14 Function
- OpBranch %18
- %18 = OpLabel
- OpBranch %23
- %23 = OpLabel
- %24 = OpSLessThan %10 %8 %9
- OpBranch %25
- %25 = OpLabel
- %26 = OpIAdd %6 %8 %15
- %27 = OpAccessChain %7 %3 %26
- OpStore %27 %8
- OpBranch %21
- %21 = OpLabel
- %20 = OpIAdd %6 %8 %16
- OpBranch %28
- %28 = OpLabel
- OpBranch %30
- %30 = OpLabel
- %31 = OpSLessThan %10 %20 %9
- OpBranch %32
- %32 = OpLabel
- %33 = OpIAdd %6 %20 %15
- %34 = OpAccessChain %7 %3 %33
- OpStore %34 %20
- OpBranch %35
- %35 = OpLabel
- %36 = OpIAdd %6 %20 %16
- OpBranch %22
- %22 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- // SinglePassRunAndCheck<LoopUnroller>(text, expected, false);
- Function* f = spvtest::GetFunction(module, 4);
- LoopDescriptor& loop_descriptor = *context->GetLoopDescriptor(f);
- EXPECT_EQ(loop_descriptor.NumLoops(), 1u);
- Loop& loop = loop_descriptor.GetLoopByIndex(0);
- EXPECT_TRUE(loop.HasUnrollLoopControl());
- BasicBlock* condition = loop.FindConditionBlock();
- EXPECT_EQ(condition->id(), 14u);
- Instruction* induction = loop.FindConditionVariable(condition);
- EXPECT_EQ(induction->result_id(), 32u);
- LoopUtils loop_utils{context.get(), &loop};
- EXPECT_TRUE(loop_utils.CanPerformUnroll());
- size_t iterations = 0;
- EXPECT_TRUE(loop.FindNumberOfIterations(induction, &*condition->ctail(),
- &iterations));
- EXPECT_EQ(iterations, 2u);
- SinglePassRunAndCheck<LoopUnroller>(text, expected, false);
- }
- /*
- Generated from the following GLSL
- #version 410 core
- void main() {
- float out_array[9];
- for (int i = -10; i < -1; i++) {
- out_array[i] = i;
- }
- }
- */
- TEST_F(PassClassTest, NegativeConditionAndInitResidualUnroll) {
- // With LocalMultiStoreElimPass
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 410
- OpName %4 "main"
- OpName %23 "out_array"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 -10
- %16 = OpConstant %6 -1
- %17 = OpTypeBool
- %19 = OpTypeInt 32 0
- %20 = OpConstant %19 9
- %21 = OpTypeArray %6 %20
- %22 = OpTypePointer Function %21
- %25 = OpConstant %6 10
- %30 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %23 = OpVariable %22 Function
- OpBranch %10
- %10 = OpLabel
- %32 = OpPhi %6 %9 %5 %31 %13
- OpLoopMerge %12 %13 Unroll
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %32 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %26 = OpIAdd %6 %32 %25
- %28 = OpAccessChain %7 %23 %26
- OpStore %28 %32
- OpBranch %13
- %13 = OpLabel
- %31 = OpIAdd %6 %32 %30
- OpBranch %10
- %12 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const std::string expected = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 410
- OpName %2 "main"
- OpName %3 "out_array"
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %8 = OpConstant %6 -10
- %9 = OpConstant %6 -1
- %10 = OpTypeBool
- %11 = OpTypeInt 32 0
- %12 = OpConstant %11 9
- %13 = OpTypeArray %6 %12
- %14 = OpTypePointer Function %13
- %15 = OpConstant %6 10
- %16 = OpConstant %6 1
- %48 = OpConstant %6 -9
- %2 = OpFunction %4 None %5
- %17 = OpLabel
- %3 = OpVariable %14 Function
- OpBranch %18
- %18 = OpLabel
- %19 = OpPhi %6 %8 %17 %20 %21
- OpLoopMerge %28 %21 Unroll
- OpBranch %23
- %23 = OpLabel
- %24 = OpSLessThan %10 %19 %48
- OpBranchConditional %24 %25 %28
- %25 = OpLabel
- %26 = OpIAdd %6 %19 %15
- %27 = OpAccessChain %7 %3 %26
- OpStore %27 %19
- OpBranch %21
- %21 = OpLabel
- %20 = OpIAdd %6 %19 %16
- OpBranch %18
- %28 = OpLabel
- OpBranch %29
- %29 = OpLabel
- %30 = OpPhi %6 %19 %28 %47 %46
- OpLoopMerge %38 %46 DontUnroll
- OpBranch %31
- %31 = OpLabel
- %32 = OpSLessThan %10 %30 %9
- OpBranchConditional %32 %33 %38
- %33 = OpLabel
- %34 = OpIAdd %6 %30 %15
- %35 = OpAccessChain %7 %3 %34
- OpStore %35 %30
- OpBranch %36
- %36 = OpLabel
- %37 = OpIAdd %6 %30 %16
- OpBranch %39
- %39 = OpLabel
- OpBranch %41
- %41 = OpLabel
- %42 = OpSLessThan %10 %37 %9
- OpBranch %43
- %43 = OpLabel
- %44 = OpIAdd %6 %37 %15
- %45 = OpAccessChain %7 %3 %44
- OpStore %45 %37
- OpBranch %46
- %46 = OpLabel
- %47 = OpIAdd %6 %37 %16
- OpBranch %29
- %38 = OpLabel
- OpReturn
- %22 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- Function* f = spvtest::GetFunction(module, 4);
- LoopDescriptor& loop_descriptor = *context->GetLoopDescriptor(f);
- EXPECT_EQ(loop_descriptor.NumLoops(), 1u);
- Loop& loop = loop_descriptor.GetLoopByIndex(0);
- EXPECT_TRUE(loop.HasUnrollLoopControl());
- BasicBlock* condition = loop.FindConditionBlock();
- EXPECT_EQ(condition->id(), 14u);
- Instruction* induction = loop.FindConditionVariable(condition);
- EXPECT_EQ(induction->result_id(), 32u);
- LoopUtils loop_utils{context.get(), &loop};
- EXPECT_TRUE(loop_utils.CanPerformUnroll());
- size_t iterations = 0;
- EXPECT_TRUE(loop.FindNumberOfIterations(induction, &*condition->ctail(),
- &iterations));
- EXPECT_EQ(iterations, 9u);
- SinglePassRunAndCheck<PartialUnrollerTestPass<2>>(text, expected, false);
- }
- /*
- Generated from the following GLSL
- #version 410 core
- void main() {
- float out_array[10];
- for (uint i = 0; i < 2; i++) {
- for (int x = 0; x < 5; ++x) {
- out_array[x + i*5] = i;
- }
- }
- }
- */
- TEST_F(PassClassTest, UnrollNestedLoopsValidateDescriptor) {
- // With LocalMultiStoreElimPass
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 410
- OpName %4 "main"
- OpName %35 "out_array"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 0
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 2
- %17 = OpTypeBool
- %19 = OpTypeInt 32 1
- %20 = OpTypePointer Function %19
- %22 = OpConstant %19 0
- %29 = OpConstant %19 5
- %31 = OpTypeFloat 32
- %32 = OpConstant %6 10
- %33 = OpTypeArray %31 %32
- %34 = OpTypePointer Function %33
- %39 = OpConstant %6 5
- %44 = OpTypePointer Function %31
- %47 = OpConstant %19 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %35 = OpVariable %34 Function
- OpBranch %10
- %10 = OpLabel
- %51 = OpPhi %6 %9 %5 %50 %13
- OpLoopMerge %12 %13 Unroll
- OpBranch %14
- %14 = OpLabel
- %18 = OpULessThan %17 %51 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- OpBranch %23
- %23 = OpLabel
- %54 = OpPhi %19 %22 %11 %48 %26
- OpLoopMerge %25 %26 Unroll
- OpBranch %27
- %27 = OpLabel
- %30 = OpSLessThan %17 %54 %29
- OpBranchConditional %30 %24 %25
- %24 = OpLabel
- %37 = OpBitcast %6 %54
- %40 = OpIMul %6 %51 %39
- %41 = OpIAdd %6 %37 %40
- %43 = OpConvertUToF %31 %51
- %45 = OpAccessChain %44 %35 %41
- OpStore %45 %43
- OpBranch %26
- %26 = OpLabel
- %48 = OpIAdd %19 %54 %47
- OpBranch %23
- %25 = OpLabel
- OpBranch %13
- %13 = OpLabel
- %50 = OpIAdd %6 %51 %47
- OpBranch %10
- %12 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- { // Test fully unroll
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << text << std::endl;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- Function* f = spvtest::GetFunction(module, 4);
- LoopDescriptor& loop_descriptor = *context->GetLoopDescriptor(f);
- EXPECT_EQ(loop_descriptor.NumLoops(), 2u);
- Loop& outer_loop = loop_descriptor.GetLoopByIndex(1);
- EXPECT_TRUE(outer_loop.HasUnrollLoopControl());
- Loop& inner_loop = loop_descriptor.GetLoopByIndex(0);
- EXPECT_TRUE(inner_loop.HasUnrollLoopControl());
- EXPECT_EQ(outer_loop.GetBlocks().size(), 9u);
- EXPECT_EQ(inner_loop.GetBlocks().size(), 4u);
- EXPECT_EQ(outer_loop.NumImmediateChildren(), 1u);
- EXPECT_EQ(inner_loop.NumImmediateChildren(), 0u);
- {
- LoopUtils loop_utils{context.get(), &inner_loop};
- loop_utils.FullyUnroll();
- loop_utils.Finalize();
- }
- EXPECT_EQ(loop_descriptor.NumLoops(), 1u);
- EXPECT_EQ(outer_loop.GetBlocks().size(), 25u);
- EXPECT_EQ(outer_loop.NumImmediateChildren(), 0u);
- {
- LoopUtils loop_utils{context.get(), &outer_loop};
- loop_utils.FullyUnroll();
- loop_utils.Finalize();
- }
- EXPECT_EQ(loop_descriptor.NumLoops(), 0u);
- }
- { // Test partially unroll
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << text << std::endl;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- Function* f = spvtest::GetFunction(module, 4);
- LoopDescriptor& loop_descriptor = *context->GetLoopDescriptor(f);
- EXPECT_EQ(loop_descriptor.NumLoops(), 2u);
- Loop& outer_loop = loop_descriptor.GetLoopByIndex(1);
- EXPECT_TRUE(outer_loop.HasUnrollLoopControl());
- Loop& inner_loop = loop_descriptor.GetLoopByIndex(0);
- EXPECT_TRUE(inner_loop.HasUnrollLoopControl());
- EXPECT_EQ(outer_loop.GetBlocks().size(), 9u);
- EXPECT_EQ(inner_loop.GetBlocks().size(), 4u);
- EXPECT_EQ(outer_loop.NumImmediateChildren(), 1u);
- EXPECT_EQ(inner_loop.NumImmediateChildren(), 0u);
- LoopUtils loop_utils{context.get(), &inner_loop};
- loop_utils.PartiallyUnroll(2);
- loop_utils.Finalize();
- // The number of loops should actually grow.
- EXPECT_EQ(loop_descriptor.NumLoops(), 3u);
- EXPECT_EQ(outer_loop.GetBlocks().size(), 18u);
- EXPECT_EQ(outer_loop.NumImmediateChildren(), 2u);
- }
- }
- /*
- Generated from the following GLSL
- #version 410 core
- void main() {
- float out_array[3];
- for (int i = 3; i > 0; --i) {
- out_array[i] = i;
- }
- }
- */
- TEST_F(PassClassTest, FullyUnrollNegativeStepLoopTest) {
- // With LocalMultiStoreElimPass
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 410
- OpName %4 "main"
- OpName %24 "out_array"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 3
- %16 = OpConstant %6 0
- %17 = OpTypeBool
- %19 = OpTypeFloat 32
- %20 = OpTypeInt 32 0
- %21 = OpConstant %20 3
- %22 = OpTypeArray %19 %21
- %23 = OpTypePointer Function %22
- %28 = OpTypePointer Function %19
- %31 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %24 = OpVariable %23 Function
- OpBranch %10
- %10 = OpLabel
- %33 = OpPhi %6 %9 %5 %32 %13
- OpLoopMerge %12 %13 Unroll
- OpBranch %14
- %14 = OpLabel
- %18 = OpSGreaterThan %17 %33 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %27 = OpConvertSToF %19 %33
- %29 = OpAccessChain %28 %24 %33
- OpStore %29 %27
- OpBranch %13
- %13 = OpLabel
- %32 = OpISub %6 %33 %31
- OpBranch %10
- %12 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const std::string output = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 410
- OpName %2 "main"
- OpName %3 "out_array"
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %8 = OpConstant %6 3
- %9 = OpConstant %6 0
- %10 = OpTypeBool
- %11 = OpTypeFloat 32
- %12 = OpTypeInt 32 0
- %13 = OpConstant %12 3
- %14 = OpTypeArray %11 %13
- %15 = OpTypePointer Function %14
- %16 = OpTypePointer Function %11
- %17 = OpConstant %6 1
- %2 = OpFunction %4 None %5
- %18 = OpLabel
- %3 = OpVariable %15 Function
- OpBranch %19
- %19 = OpLabel
- OpBranch %24
- %24 = OpLabel
- %25 = OpSGreaterThan %10 %8 %9
- OpBranch %26
- %26 = OpLabel
- %27 = OpConvertSToF %11 %8
- %28 = OpAccessChain %16 %3 %8
- OpStore %28 %27
- OpBranch %22
- %22 = OpLabel
- %21 = OpISub %6 %8 %17
- OpBranch %29
- %29 = OpLabel
- OpBranch %31
- %31 = OpLabel
- %32 = OpSGreaterThan %10 %21 %9
- OpBranch %33
- %33 = OpLabel
- %34 = OpConvertSToF %11 %21
- %35 = OpAccessChain %16 %3 %21
- OpStore %35 %34
- OpBranch %36
- %36 = OpLabel
- %37 = OpISub %6 %21 %17
- OpBranch %38
- %38 = OpLabel
- OpBranch %40
- %40 = OpLabel
- %41 = OpSGreaterThan %10 %37 %9
- OpBranch %42
- %42 = OpLabel
- %43 = OpConvertSToF %11 %37
- %44 = OpAccessChain %16 %3 %37
- OpStore %44 %43
- OpBranch %45
- %45 = OpLabel
- %46 = OpISub %6 %37 %17
- OpBranch %23
- %23 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<LoopUnroller>(text, output, false);
- }
- /*
- Generated from the following GLSL
- #version 410 core
- void main() {
- float out_array[3];
- for (int i = 9; i > 0; i-=3) {
- out_array[i] = i;
- }
- }
- */
- TEST_F(PassClassTest, FullyUnrollNegativeNonOneStepLoop) {
- // With LocalMultiStoreElimPass
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 410
- OpName %4 "main"
- OpName %24 "out_array"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 9
- %16 = OpConstant %6 0
- %17 = OpTypeBool
- %19 = OpTypeFloat 32
- %20 = OpTypeInt 32 0
- %21 = OpConstant %20 3
- %22 = OpTypeArray %19 %21
- %23 = OpTypePointer Function %22
- %28 = OpTypePointer Function %19
- %30 = OpConstant %6 3
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %24 = OpVariable %23 Function
- OpBranch %10
- %10 = OpLabel
- %33 = OpPhi %6 %9 %5 %32 %13
- OpLoopMerge %12 %13 Unroll
- OpBranch %14
- %14 = OpLabel
- %18 = OpSGreaterThan %17 %33 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %27 = OpConvertSToF %19 %33
- %29 = OpAccessChain %28 %24 %33
- OpStore %29 %27
- OpBranch %13
- %13 = OpLabel
- %32 = OpISub %6 %33 %30
- OpBranch %10
- %12 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const std::string output = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 410
- OpName %2 "main"
- OpName %3 "out_array"
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %8 = OpConstant %6 9
- %9 = OpConstant %6 0
- %10 = OpTypeBool
- %11 = OpTypeFloat 32
- %12 = OpTypeInt 32 0
- %13 = OpConstant %12 3
- %14 = OpTypeArray %11 %13
- %15 = OpTypePointer Function %14
- %16 = OpTypePointer Function %11
- %17 = OpConstant %6 3
- %2 = OpFunction %4 None %5
- %18 = OpLabel
- %3 = OpVariable %15 Function
- OpBranch %19
- %19 = OpLabel
- OpBranch %24
- %24 = OpLabel
- %25 = OpSGreaterThan %10 %8 %9
- OpBranch %26
- %26 = OpLabel
- %27 = OpConvertSToF %11 %8
- %28 = OpAccessChain %16 %3 %8
- OpStore %28 %27
- OpBranch %22
- %22 = OpLabel
- %21 = OpISub %6 %8 %17
- OpBranch %29
- %29 = OpLabel
- OpBranch %31
- %31 = OpLabel
- %32 = OpSGreaterThan %10 %21 %9
- OpBranch %33
- %33 = OpLabel
- %34 = OpConvertSToF %11 %21
- %35 = OpAccessChain %16 %3 %21
- OpStore %35 %34
- OpBranch %36
- %36 = OpLabel
- %37 = OpISub %6 %21 %17
- OpBranch %38
- %38 = OpLabel
- OpBranch %40
- %40 = OpLabel
- %41 = OpSGreaterThan %10 %37 %9
- OpBranch %42
- %42 = OpLabel
- %43 = OpConvertSToF %11 %37
- %44 = OpAccessChain %16 %3 %37
- OpStore %44 %43
- OpBranch %45
- %45 = OpLabel
- %46 = OpISub %6 %37 %17
- OpBranch %23
- %23 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<LoopUnroller>(text, output, false);
- }
- /*
- Generated from the following GLSL
- #version 410 core
- void main() {
- float out_array[3];
- for (int i = 0; i < 7; i+=3) {
- out_array[i] = i;
- }
- }
- */
- TEST_F(PassClassTest, FullyUnrollNonDivisibleStepLoop) {
- // With LocalMultiStoreElimPass
- const std::string text = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 410
- OpName %4 "main"
- OpName %24 "out_array"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %16 = OpConstant %6 7
- %17 = OpTypeBool
- %19 = OpTypeFloat 32
- %20 = OpTypeInt 32 0
- %21 = OpConstant %20 3
- %22 = OpTypeArray %19 %21
- %23 = OpTypePointer Function %22
- %28 = OpTypePointer Function %19
- %30 = OpConstant %6 3
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %24 = OpVariable %23 Function
- OpBranch %10
- %10 = OpLabel
- %33 = OpPhi %6 %9 %5 %32 %13
- OpLoopMerge %12 %13 Unroll
- OpBranch %14
- %14 = OpLabel
- %18 = OpSLessThan %17 %33 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %27 = OpConvertSToF %19 %33
- %29 = OpAccessChain %28 %24 %33
- OpStore %29 %27
- OpBranch %13
- %13 = OpLabel
- %32 = OpIAdd %6 %33 %30
- OpBranch %10
- %12 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const std::string output = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 410
- OpName %2 "main"
- OpName %3 "out_array"
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %8 = OpConstant %6 0
- %9 = OpConstant %6 7
- %10 = OpTypeBool
- %11 = OpTypeFloat 32
- %12 = OpTypeInt 32 0
- %13 = OpConstant %12 3
- %14 = OpTypeArray %11 %13
- %15 = OpTypePointer Function %14
- %16 = OpTypePointer Function %11
- %17 = OpConstant %6 3
- %2 = OpFunction %4 None %5
- %18 = OpLabel
- %3 = OpVariable %15 Function
- OpBranch %19
- %19 = OpLabel
- OpBranch %24
- %24 = OpLabel
- %25 = OpSLessThan %10 %8 %9
- OpBranch %26
- %26 = OpLabel
- %27 = OpConvertSToF %11 %8
- %28 = OpAccessChain %16 %3 %8
- OpStore %28 %27
- OpBranch %22
- %22 = OpLabel
- %21 = OpIAdd %6 %8 %17
- OpBranch %29
- %29 = OpLabel
- OpBranch %31
- %31 = OpLabel
- %32 = OpSLessThan %10 %21 %9
- OpBranch %33
- %33 = OpLabel
- %34 = OpConvertSToF %11 %21
- %35 = OpAccessChain %16 %3 %21
- OpStore %35 %34
- OpBranch %36
- %36 = OpLabel
- %37 = OpIAdd %6 %21 %17
- OpBranch %38
- %38 = OpLabel
- OpBranch %40
- %40 = OpLabel
- %41 = OpSLessThan %10 %37 %9
- OpBranch %42
- %42 = OpLabel
- %43 = OpConvertSToF %11 %37
- %44 = OpAccessChain %16 %3 %37
- OpStore %44 %43
- OpBranch %45
- %45 = OpLabel
- %46 = OpIAdd %6 %37 %17
- OpBranch %23
- %23 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<LoopUnroller>(text, output, false);
- }
- /*
- Generated from the following GLSL
- #version 410 core
- void main() {
- float out_array[4];
- for (int i = 11; i > 0; i-=3) {
- out_array[i] = i;
- }
- }
- */
- TEST_F(PassClassTest, FullyUnrollNegativeNonDivisibleStepLoop) {
- // With LocalMultiStoreElimPass
- const std::string text = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 410
- OpName %4 "main"
- OpName %24 "out_array"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 11
- %16 = OpConstant %6 0
- %17 = OpTypeBool
- %19 = OpTypeFloat 32
- %20 = OpTypeInt 32 0
- %21 = OpConstant %20 4
- %22 = OpTypeArray %19 %21
- %23 = OpTypePointer Function %22
- %28 = OpTypePointer Function %19
- %30 = OpConstant %6 3
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %24 = OpVariable %23 Function
- OpBranch %10
- %10 = OpLabel
- %33 = OpPhi %6 %9 %5 %32 %13
- OpLoopMerge %12 %13 Unroll
- OpBranch %14
- %14 = OpLabel
- %18 = OpSGreaterThan %17 %33 %16
- OpBranchConditional %18 %11 %12
- %11 = OpLabel
- %27 = OpConvertSToF %19 %33
- %29 = OpAccessChain %28 %24 %33
- OpStore %29 %27
- OpBranch %13
- %13 = OpLabel
- %32 = OpISub %6 %33 %30
- OpBranch %10
- %12 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const std::string output = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 410
- OpName %2 "main"
- OpName %3 "out_array"
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %8 = OpConstant %6 11
- %9 = OpConstant %6 0
- %10 = OpTypeBool
- %11 = OpTypeFloat 32
- %12 = OpTypeInt 32 0
- %13 = OpConstant %12 4
- %14 = OpTypeArray %11 %13
- %15 = OpTypePointer Function %14
- %16 = OpTypePointer Function %11
- %17 = OpConstant %6 3
- %2 = OpFunction %4 None %5
- %18 = OpLabel
- %3 = OpVariable %15 Function
- OpBranch %19
- %19 = OpLabel
- OpBranch %24
- %24 = OpLabel
- %25 = OpSGreaterThan %10 %8 %9
- OpBranch %26
- %26 = OpLabel
- %27 = OpConvertSToF %11 %8
- %28 = OpAccessChain %16 %3 %8
- OpStore %28 %27
- OpBranch %22
- %22 = OpLabel
- %21 = OpISub %6 %8 %17
- OpBranch %29
- %29 = OpLabel
- OpBranch %31
- %31 = OpLabel
- %32 = OpSGreaterThan %10 %21 %9
- OpBranch %33
- %33 = OpLabel
- %34 = OpConvertSToF %11 %21
- %35 = OpAccessChain %16 %3 %21
- OpStore %35 %34
- OpBranch %36
- %36 = OpLabel
- %37 = OpISub %6 %21 %17
- OpBranch %38
- %38 = OpLabel
- OpBranch %40
- %40 = OpLabel
- %41 = OpSGreaterThan %10 %37 %9
- OpBranch %42
- %42 = OpLabel
- %43 = OpConvertSToF %11 %37
- %44 = OpAccessChain %16 %3 %37
- OpStore %44 %43
- OpBranch %45
- %45 = OpLabel
- %46 = OpISub %6 %37 %17
- OpBranch %47
- %47 = OpLabel
- OpBranch %49
- %49 = OpLabel
- %50 = OpSGreaterThan %10 %46 %9
- OpBranch %51
- %51 = OpLabel
- %52 = OpConvertSToF %11 %46
- %53 = OpAccessChain %16 %3 %46
- OpStore %53 %52
- OpBranch %54
- %54 = OpLabel
- %55 = OpISub %6 %46 %17
- OpBranch %23
- %23 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<LoopUnroller>(text, output, false);
- }
- // With LocalMultiStoreElimPass
- static const std::string multiple_phi_shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 410
- OpName %4 "main"
- OpName %8 "foo("
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypeFunction %6
- %10 = OpTypePointer Function %6
- %12 = OpConstant %6 0
- %14 = OpConstant %6 3
- %22 = OpConstant %6 6
- %23 = OpTypeBool
- %31 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- %40 = OpFunctionCall %6 %8
- OpReturn
- OpFunctionEnd
- %8 = OpFunction %6 None %7
- %9 = OpLabel
- OpBranch %16
- %16 = OpLabel
- %41 = OpPhi %6 %12 %9 %34 %19
- %42 = OpPhi %6 %14 %9 %29 %19
- %43 = OpPhi %6 %12 %9 %32 %19
- OpLoopMerge %18 %19 Unroll
- OpBranch %20
- %20 = OpLabel
- %24 = OpSLessThan %23 %43 %22
- OpBranchConditional %24 %17 %18
- %17 = OpLabel
- %27 = OpIMul %6 %43 %41
- %29 = OpIAdd %6 %42 %27
- OpBranch %19
- %19 = OpLabel
- %32 = OpIAdd %6 %43 %31
- %34 = OpISub %6 %41 %31
- OpBranch %16
- %18 = OpLabel
- %37 = OpIAdd %6 %42 %41
- OpReturnValue %37
- OpFunctionEnd
- )";
- TEST_F(PassClassTest, PartiallyUnrollResidualMultipleInductionVariables) {
- const std::string output = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 410
- OpName %2 "main"
- OpName %3 "foo("
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 1
- %7 = OpTypeFunction %6
- %8 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %10 = OpConstant %6 3
- %11 = OpConstant %6 6
- %12 = OpTypeBool
- %13 = OpConstant %6 1
- %82 = OpTypeInt 32 0
- %83 = OpConstant %82 2
- %2 = OpFunction %4 None %5
- %14 = OpLabel
- %15 = OpFunctionCall %6 %3
- OpReturn
- OpFunctionEnd
- %3 = OpFunction %6 None %7
- %16 = OpLabel
- OpBranch %17
- %17 = OpLabel
- %18 = OpPhi %6 %9 %16 %19 %20
- %21 = OpPhi %6 %10 %16 %22 %20
- %23 = OpPhi %6 %9 %16 %24 %20
- OpLoopMerge %31 %20 Unroll
- OpBranch %26
- %26 = OpLabel
- %27 = OpSLessThan %12 %23 %83
- OpBranchConditional %27 %28 %31
- %28 = OpLabel
- %29 = OpIMul %6 %23 %18
- %22 = OpIAdd %6 %21 %29
- OpBranch %20
- %20 = OpLabel
- %24 = OpIAdd %6 %23 %13
- %19 = OpISub %6 %18 %13
- OpBranch %17
- %31 = OpLabel
- OpBranch %32
- %32 = OpLabel
- %33 = OpPhi %6 %18 %31 %81 %79
- %34 = OpPhi %6 %21 %31 %78 %79
- %35 = OpPhi %6 %23 %31 %80 %79
- OpLoopMerge %44 %79 DontUnroll
- OpBranch %36
- %36 = OpLabel
- %37 = OpSLessThan %12 %35 %11
- OpBranchConditional %37 %38 %44
- %38 = OpLabel
- %39 = OpIMul %6 %35 %33
- %40 = OpIAdd %6 %34 %39
- OpBranch %41
- %41 = OpLabel
- %42 = OpIAdd %6 %35 %13
- %43 = OpISub %6 %33 %13
- OpBranch %46
- %46 = OpLabel
- OpBranch %50
- %50 = OpLabel
- %51 = OpSLessThan %12 %42 %11
- OpBranch %52
- %52 = OpLabel
- %53 = OpIMul %6 %42 %43
- %54 = OpIAdd %6 %40 %53
- OpBranch %55
- %55 = OpLabel
- %56 = OpIAdd %6 %42 %13
- %57 = OpISub %6 %43 %13
- OpBranch %58
- %58 = OpLabel
- OpBranch %62
- %62 = OpLabel
- %63 = OpSLessThan %12 %56 %11
- OpBranch %64
- %64 = OpLabel
- %65 = OpIMul %6 %56 %57
- %66 = OpIAdd %6 %54 %65
- OpBranch %67
- %67 = OpLabel
- %68 = OpIAdd %6 %56 %13
- %69 = OpISub %6 %57 %13
- OpBranch %70
- %70 = OpLabel
- OpBranch %74
- %74 = OpLabel
- %75 = OpSLessThan %12 %68 %11
- OpBranch %76
- %76 = OpLabel
- %77 = OpIMul %6 %68 %69
- %78 = OpIAdd %6 %66 %77
- OpBranch %79
- %79 = OpLabel
- %80 = OpIAdd %6 %68 %13
- %81 = OpISub %6 %69 %13
- OpBranch %32
- %44 = OpLabel
- %45 = OpIAdd %6 %34 %33
- OpReturnValue %45
- %25 = OpLabel
- %30 = OpIAdd %6 %34 %33
- OpReturnValue %30
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, multiple_phi_shader,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << multiple_phi_shader << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<PartialUnrollerTestPass<4>>(multiple_phi_shader, output,
- false);
- }
- TEST_F(PassClassTest, PartiallyUnrollMultipleInductionVariables) {
- const std::string output = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 410
- OpName %2 "main"
- OpName %3 "foo("
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 1
- %7 = OpTypeFunction %6
- %8 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %10 = OpConstant %6 3
- %11 = OpConstant %6 6
- %12 = OpTypeBool
- %13 = OpConstant %6 1
- %2 = OpFunction %4 None %5
- %14 = OpLabel
- %15 = OpFunctionCall %6 %3
- OpReturn
- OpFunctionEnd
- %3 = OpFunction %6 None %7
- %16 = OpLabel
- OpBranch %17
- %17 = OpLabel
- %18 = OpPhi %6 %9 %16 %42 %40
- %21 = OpPhi %6 %10 %16 %39 %40
- %23 = OpPhi %6 %9 %16 %41 %40
- OpLoopMerge %25 %40 DontUnroll
- OpBranch %26
- %26 = OpLabel
- %27 = OpSLessThan %12 %23 %11
- OpBranchConditional %27 %28 %25
- %28 = OpLabel
- %29 = OpIMul %6 %23 %18
- %22 = OpIAdd %6 %21 %29
- OpBranch %20
- %20 = OpLabel
- %24 = OpIAdd %6 %23 %13
- %19 = OpISub %6 %18 %13
- OpBranch %31
- %31 = OpLabel
- OpBranch %35
- %35 = OpLabel
- %36 = OpSLessThan %12 %24 %11
- OpBranch %37
- %37 = OpLabel
- %38 = OpIMul %6 %24 %19
- %39 = OpIAdd %6 %22 %38
- OpBranch %40
- %40 = OpLabel
- %41 = OpIAdd %6 %24 %13
- %42 = OpISub %6 %19 %13
- OpBranch %17
- %25 = OpLabel
- %30 = OpIAdd %6 %21 %18
- OpReturnValue %30
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, multiple_phi_shader,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << multiple_phi_shader << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<PartialUnrollerTestPass<2>>(multiple_phi_shader, output,
- false);
- }
- TEST_F(PassClassTest, FullyUnrollMultipleInductionVariables) {
- const std::string output = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 410
- OpName %2 "main"
- OpName %3 "foo("
- %4 = OpTypeVoid
- %5 = OpTypeFunction %4
- %6 = OpTypeInt 32 1
- %7 = OpTypeFunction %6
- %8 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %10 = OpConstant %6 3
- %11 = OpConstant %6 6
- %12 = OpTypeBool
- %13 = OpConstant %6 1
- %2 = OpFunction %4 None %5
- %14 = OpLabel
- %15 = OpFunctionCall %6 %3
- OpReturn
- OpFunctionEnd
- %3 = OpFunction %6 None %7
- %16 = OpLabel
- OpBranch %17
- %17 = OpLabel
- OpBranch %26
- %26 = OpLabel
- %27 = OpSLessThan %12 %9 %11
- OpBranch %28
- %28 = OpLabel
- %29 = OpIMul %6 %9 %9
- %22 = OpIAdd %6 %10 %29
- OpBranch %20
- %20 = OpLabel
- %24 = OpIAdd %6 %9 %13
- %19 = OpISub %6 %9 %13
- OpBranch %31
- %31 = OpLabel
- OpBranch %35
- %35 = OpLabel
- %36 = OpSLessThan %12 %24 %11
- OpBranch %37
- %37 = OpLabel
- %38 = OpIMul %6 %24 %19
- %39 = OpIAdd %6 %22 %38
- OpBranch %40
- %40 = OpLabel
- %41 = OpIAdd %6 %24 %13
- %42 = OpISub %6 %19 %13
- OpBranch %43
- %43 = OpLabel
- OpBranch %47
- %47 = OpLabel
- %48 = OpSLessThan %12 %41 %11
- OpBranch %49
- %49 = OpLabel
- %50 = OpIMul %6 %41 %42
- %51 = OpIAdd %6 %39 %50
- OpBranch %52
- %52 = OpLabel
- %53 = OpIAdd %6 %41 %13
- %54 = OpISub %6 %42 %13
- OpBranch %55
- %55 = OpLabel
- OpBranch %59
- %59 = OpLabel
- %60 = OpSLessThan %12 %53 %11
- OpBranch %61
- %61 = OpLabel
- %62 = OpIMul %6 %53 %54
- %63 = OpIAdd %6 %51 %62
- OpBranch %64
- %64 = OpLabel
- %65 = OpIAdd %6 %53 %13
- %66 = OpISub %6 %54 %13
- OpBranch %67
- %67 = OpLabel
- OpBranch %71
- %71 = OpLabel
- %72 = OpSLessThan %12 %65 %11
- OpBranch %73
- %73 = OpLabel
- %74 = OpIMul %6 %65 %66
- %75 = OpIAdd %6 %63 %74
- OpBranch %76
- %76 = OpLabel
- %77 = OpIAdd %6 %65 %13
- %78 = OpISub %6 %66 %13
- OpBranch %79
- %79 = OpLabel
- OpBranch %83
- %83 = OpLabel
- %84 = OpSLessThan %12 %77 %11
- OpBranch %85
- %85 = OpLabel
- %86 = OpIMul %6 %77 %78
- %87 = OpIAdd %6 %75 %86
- OpBranch %88
- %88 = OpLabel
- %89 = OpIAdd %6 %77 %13
- %90 = OpISub %6 %78 %13
- OpBranch %25
- %25 = OpLabel
- %30 = OpIAdd %6 %87 %90
- OpReturnValue %30
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, multiple_phi_shader,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << multiple_phi_shader << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<LoopUnroller>(multiple_phi_shader, output, false);
- }
- /*
- Generated from the following GLSL
- #version 440 core
- void main()
- {
- int j = 0;
- for (int i = 0; i <= 2; ++i)
- ++j;
- for (int i = 1; i >= 0; --i)
- ++j;
- }
- */
- TEST_F(PassClassTest, FullyUnrollEqualToOperations) {
- // With LocalMultiStoreElimPass
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource GLSL 440
- OpName %4 "main"
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpTypePointer Function %6
- %9 = OpConstant %6 0
- %17 = OpConstant %6 2
- %18 = OpTypeBool
- %21 = OpConstant %6 1
- %4 = OpFunction %2 None %3
- %5 = OpLabel
- OpBranch %11
- %11 = OpLabel
- %37 = OpPhi %6 %9 %5 %22 %14
- %38 = OpPhi %6 %9 %5 %24 %14
- OpLoopMerge %13 %14 Unroll
- OpBranch %15
- %15 = OpLabel
- %19 = OpSLessThanEqual %18 %38 %17
- OpBranchConditional %19 %12 %13
- %12 = OpLabel
- %22 = OpIAdd %6 %37 %21
- OpBranch %14
- %14 = OpLabel
- %24 = OpIAdd %6 %38 %21
- OpBranch %11
- %13 = OpLabel
- OpBranch %26
- %26 = OpLabel
- %39 = OpPhi %6 %37 %13 %34 %29
- %40 = OpPhi %6 %21 %13 %36 %29
- OpLoopMerge %28 %29 Unroll
- OpBranch %30
- %30 = OpLabel
- %32 = OpSGreaterThanEqual %18 %40 %9
- OpBranchConditional %32 %27 %28
- %27 = OpLabel
- %34 = OpIAdd %6 %39 %21
- OpBranch %29
- %29 = OpLabel
- %36 = OpISub %6 %40 %21
- OpBranch %26
- %28 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const std::string output = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 440
- OpName %2 "main"
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeInt 32 1
- %6 = OpTypePointer Function %5
- %7 = OpConstant %5 0
- %8 = OpConstant %5 2
- %9 = OpTypeBool
- %10 = OpConstant %5 1
- %2 = OpFunction %3 None %4
- %11 = OpLabel
- OpBranch %12
- %12 = OpLabel
- OpBranch %19
- %19 = OpLabel
- %20 = OpSLessThanEqual %9 %7 %8
- OpBranch %21
- %21 = OpLabel
- %14 = OpIAdd %5 %7 %10
- OpBranch %15
- %15 = OpLabel
- %17 = OpIAdd %5 %7 %10
- OpBranch %41
- %41 = OpLabel
- OpBranch %44
- %44 = OpLabel
- %45 = OpSLessThanEqual %9 %17 %8
- OpBranch %46
- %46 = OpLabel
- %47 = OpIAdd %5 %14 %10
- OpBranch %48
- %48 = OpLabel
- %49 = OpIAdd %5 %17 %10
- OpBranch %50
- %50 = OpLabel
- OpBranch %53
- %53 = OpLabel
- %54 = OpSLessThanEqual %9 %49 %8
- OpBranch %55
- %55 = OpLabel
- %56 = OpIAdd %5 %47 %10
- OpBranch %57
- %57 = OpLabel
- %58 = OpIAdd %5 %49 %10
- OpBranch %18
- %18 = OpLabel
- OpBranch %22
- %22 = OpLabel
- OpBranch %29
- %29 = OpLabel
- %30 = OpSGreaterThanEqual %9 %10 %7
- OpBranch %31
- %31 = OpLabel
- %24 = OpIAdd %5 %56 %10
- OpBranch %25
- %25 = OpLabel
- %27 = OpISub %5 %10 %10
- OpBranch %32
- %32 = OpLabel
- OpBranch %35
- %35 = OpLabel
- %36 = OpSGreaterThanEqual %9 %27 %7
- OpBranch %37
- %37 = OpLabel
- %38 = OpIAdd %5 %24 %10
- OpBranch %39
- %39 = OpLabel
- %40 = OpISub %5 %27 %10
- OpBranch %28
- %28 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<LoopUnroller>(text, output, false);
- }
- // With LocalMultiStoreElimPass
- const std::string condition_in_header = R"(
- OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %main "main" %o
- OpExecutionMode %main OriginUpperLeft
- OpSource GLSL 430
- OpDecorate %o Location 0
- %void = OpTypeVoid
- %6 = OpTypeFunction %void
- %int = OpTypeInt 32 1
- %int_n2 = OpConstant %int -2
- %int_2 = OpConstant %int 2
- %bool = OpTypeBool
- %float = OpTypeFloat 32
- %_ptr_Output_float = OpTypePointer Output %float
- %o = OpVariable %_ptr_Output_float Output
- %float_1 = OpConstant %float 1
- %main = OpFunction %void None %6
- %15 = OpLabel
- OpBranch %16
- %16 = OpLabel
- %27 = OpPhi %int %int_n2 %15 %26 %18
- %21 = OpSLessThanEqual %bool %27 %int_2
- OpLoopMerge %17 %18 Unroll
- OpBranchConditional %21 %22 %17
- %22 = OpLabel
- %23 = OpLoad %float %o
- %24 = OpFAdd %float %23 %float_1
- OpStore %o %24
- OpBranch %18
- %18 = OpLabel
- %26 = OpIAdd %int %27 %int_2
- OpBranch %16
- %17 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- TEST_F(PassClassTest, FullyUnrollConditionIsInHeaderBlock) {
- const std::string output = R"(OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %1 "main" %2
- OpExecutionMode %1 OriginUpperLeft
- OpSource GLSL 430
- OpDecorate %2 Location 0
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeInt 32 1
- %6 = OpConstant %5 -2
- %7 = OpConstant %5 2
- %8 = OpTypeBool
- %9 = OpTypeFloat 32
- %10 = OpTypePointer Output %9
- %2 = OpVariable %10 Output
- %11 = OpConstant %9 1
- %1 = OpFunction %3 None %4
- %12 = OpLabel
- OpBranch %13
- %13 = OpLabel
- %17 = OpSLessThanEqual %8 %6 %7
- OpBranch %19
- %19 = OpLabel
- %20 = OpLoad %9 %2
- %21 = OpFAdd %9 %20 %11
- OpStore %2 %21
- OpBranch %16
- %16 = OpLabel
- %15 = OpIAdd %5 %6 %7
- OpBranch %22
- %22 = OpLabel
- %24 = OpSLessThanEqual %8 %15 %7
- OpBranch %25
- %25 = OpLabel
- %26 = OpLoad %9 %2
- %27 = OpFAdd %9 %26 %11
- OpStore %2 %27
- OpBranch %28
- %28 = OpLabel
- %29 = OpIAdd %5 %15 %7
- OpBranch %30
- %30 = OpLabel
- %32 = OpSLessThanEqual %8 %29 %7
- OpBranch %33
- %33 = OpLabel
- %34 = OpLoad %9 %2
- %35 = OpFAdd %9 %34 %11
- OpStore %2 %35
- OpBranch %36
- %36 = OpLabel
- %37 = OpIAdd %5 %29 %7
- OpBranch %18
- %18 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, condition_in_header,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << condition_in_header << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<LoopUnroller>(condition_in_header, output, false);
- }
- TEST_F(PassClassTest, PartiallyUnrollResidualConditionIsInHeaderBlock) {
- const std::string output = R"(OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %1 "main" %2
- OpExecutionMode %1 OriginUpperLeft
- OpSource GLSL 430
- OpDecorate %2 Location 0
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeInt 32 1
- %6 = OpConstant %5 -2
- %7 = OpConstant %5 2
- %8 = OpTypeBool
- %9 = OpTypeFloat 32
- %10 = OpTypePointer Output %9
- %2 = OpVariable %10 Output
- %11 = OpConstant %9 1
- %40 = OpTypeInt 32 0
- %41 = OpConstant %40 1
- %1 = OpFunction %3 None %4
- %12 = OpLabel
- OpBranch %13
- %13 = OpLabel
- %14 = OpPhi %5 %6 %12 %15 %16
- %17 = OpSLessThanEqual %8 %14 %41
- OpLoopMerge %22 %16 Unroll
- OpBranchConditional %17 %19 %22
- %19 = OpLabel
- %20 = OpLoad %9 %2
- %21 = OpFAdd %9 %20 %11
- OpStore %2 %21
- OpBranch %16
- %16 = OpLabel
- %15 = OpIAdd %5 %14 %7
- OpBranch %13
- %22 = OpLabel
- OpBranch %23
- %23 = OpLabel
- %24 = OpPhi %5 %14 %22 %39 %38
- %25 = OpSLessThanEqual %8 %24 %7
- OpLoopMerge %31 %38 DontUnroll
- OpBranchConditional %25 %26 %31
- %26 = OpLabel
- %27 = OpLoad %9 %2
- %28 = OpFAdd %9 %27 %11
- OpStore %2 %28
- OpBranch %29
- %29 = OpLabel
- %30 = OpIAdd %5 %24 %7
- OpBranch %32
- %32 = OpLabel
- %34 = OpSLessThanEqual %8 %30 %7
- OpBranch %35
- %35 = OpLabel
- %36 = OpLoad %9 %2
- %37 = OpFAdd %9 %36 %11
- OpStore %2 %37
- OpBranch %38
- %38 = OpLabel
- %39 = OpIAdd %5 %30 %7
- OpBranch %23
- %31 = OpLabel
- OpReturn
- %18 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, condition_in_header,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << condition_in_header << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<PartialUnrollerTestPass<2>>(condition_in_header, output,
- false);
- }
- /*
- Generated from following GLSL with latch block artificially inserted to be
- separate from continue.
- #version 430
- void main(void) {
- float x[10];
- for (int i = 0; i < 10; ++i) {
- x[i] = i;
- }
- }
- */
- TEST_F(PassClassTest, PartiallyUnrollLatchNotContinue) {
- const std::string text = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 430
- OpName %2 "main"
- OpName %3 "i"
- OpName %4 "x"
- %5 = OpTypeVoid
- %6 = OpTypeFunction %5
- %7 = OpTypeInt 32 1
- %8 = OpTypePointer Function %7
- %9 = OpConstant %7 0
- %10 = OpConstant %7 10
- %11 = OpTypeBool
- %12 = OpTypeFloat 32
- %13 = OpTypeInt 32 0
- %14 = OpConstant %13 10
- %15 = OpTypeArray %12 %14
- %16 = OpTypePointer Function %15
- %17 = OpTypePointer Function %12
- %18 = OpConstant %7 1
- %2 = OpFunction %5 None %6
- %19 = OpLabel
- %3 = OpVariable %8 Function
- %4 = OpVariable %16 Function
- OpStore %3 %9
- OpBranch %20
- %20 = OpLabel
- %21 = OpPhi %7 %9 %19 %22 %30
- OpLoopMerge %24 %23 Unroll
- OpBranch %25
- %25 = OpLabel
- %26 = OpSLessThan %11 %21 %10
- OpBranchConditional %26 %27 %24
- %27 = OpLabel
- %28 = OpConvertSToF %12 %21
- %29 = OpAccessChain %17 %4 %21
- OpStore %29 %28
- OpBranch %23
- %23 = OpLabel
- %22 = OpIAdd %7 %21 %18
- OpStore %3 %22
- OpBranch %30
- %30 = OpLabel
- OpBranch %20
- %24 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const std::string expected = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource GLSL 430
- OpName %2 "main"
- OpName %3 "i"
- OpName %4 "x"
- %5 = OpTypeVoid
- %6 = OpTypeFunction %5
- %7 = OpTypeInt 32 1
- %8 = OpTypePointer Function %7
- %9 = OpConstant %7 0
- %10 = OpConstant %7 10
- %11 = OpTypeBool
- %12 = OpTypeFloat 32
- %13 = OpTypeInt 32 0
- %14 = OpConstant %13 10
- %15 = OpTypeArray %12 %14
- %16 = OpTypePointer Function %15
- %17 = OpTypePointer Function %12
- %18 = OpConstant %7 1
- %63 = OpConstant %13 1
- %2 = OpFunction %5 None %6
- %19 = OpLabel
- %3 = OpVariable %8 Function
- %4 = OpVariable %16 Function
- OpStore %3 %9
- OpBranch %20
- %20 = OpLabel
- %21 = OpPhi %7 %9 %19 %22 %23
- OpLoopMerge %31 %25 Unroll
- OpBranch %26
- %26 = OpLabel
- %27 = OpSLessThan %11 %21 %63
- OpBranchConditional %27 %28 %31
- %28 = OpLabel
- %29 = OpConvertSToF %12 %21
- %30 = OpAccessChain %17 %4 %21
- OpStore %30 %29
- OpBranch %25
- %25 = OpLabel
- %22 = OpIAdd %7 %21 %18
- OpStore %3 %22
- OpBranch %23
- %23 = OpLabel
- OpBranch %20
- %31 = OpLabel
- OpBranch %32
- %32 = OpLabel
- %33 = OpPhi %7 %21 %31 %61 %62
- OpLoopMerge %42 %60 DontUnroll
- OpBranch %34
- %34 = OpLabel
- %35 = OpSLessThan %11 %33 %10
- OpBranchConditional %35 %36 %42
- %36 = OpLabel
- %37 = OpConvertSToF %12 %33
- %38 = OpAccessChain %17 %4 %33
- OpStore %38 %37
- OpBranch %39
- %39 = OpLabel
- %40 = OpIAdd %7 %33 %18
- OpStore %3 %40
- OpBranch %41
- %41 = OpLabel
- OpBranch %43
- %43 = OpLabel
- OpBranch %45
- %45 = OpLabel
- %46 = OpSLessThan %11 %40 %10
- OpBranch %47
- %47 = OpLabel
- %48 = OpConvertSToF %12 %40
- %49 = OpAccessChain %17 %4 %40
- OpStore %49 %48
- OpBranch %50
- %50 = OpLabel
- %51 = OpIAdd %7 %40 %18
- OpStore %3 %51
- OpBranch %52
- %52 = OpLabel
- OpBranch %53
- %53 = OpLabel
- OpBranch %55
- %55 = OpLabel
- %56 = OpSLessThan %11 %51 %10
- OpBranch %57
- %57 = OpLabel
- %58 = OpConvertSToF %12 %51
- %59 = OpAccessChain %17 %4 %51
- OpStore %59 %58
- OpBranch %60
- %60 = OpLabel
- %61 = OpIAdd %7 %51 %18
- OpStore %3 %61
- OpBranch %62
- %62 = OpLabel
- OpBranch %32
- %42 = OpLabel
- OpReturn
- %24 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<PartialUnrollerTestPass<3>>(text, expected, true);
- // Make sure the latch block information is preserved and propagated correctly
- // by the pass.
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- PartialUnrollerTestPass<3> unroller;
- unroller.SetContextForTesting(context.get());
- unroller.Process();
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << text << std::endl;
- const Function* f = spvtest::GetFunction(module, 2);
- LoopDescriptor ld{context.get(), f};
- EXPECT_EQ(ld.NumLoops(), 2u);
- Loop& loop_1 = ld.GetLoopByIndex(0u);
- EXPECT_NE(loop_1.GetLatchBlock(), loop_1.GetContinueBlock());
- Loop& loop_2 = ld.GetLoopByIndex(1u);
- EXPECT_NE(loop_2.GetLatchBlock(), loop_2.GetContinueBlock());
- }
- // Test that a loop with a self-referencing OpPhi instruction is handled
- // correctly.
- TEST_F(PassClassTest, OpPhiSelfReference) {
- const std::string text = R"(
- ; Find the two adds from the unrolled loop
- ; CHECK: OpIAdd
- ; CHECK: OpIAdd
- ; CHECK: OpIAdd %uint %uint_0 %uint_1
- ; CHECK-NEXT: OpReturn
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint GLCompute %2 "main"
- OpExecutionMode %2 LocalSize 8 8 1
- OpSource HLSL 600
- %uint = OpTypeInt 32 0
- %void = OpTypeVoid
- %5 = OpTypeFunction %void
- %uint_0 = OpConstant %uint 0
- %uint_1 = OpConstant %uint 1
- %bool = OpTypeBool
- %true = OpConstantTrue %bool
- %2 = OpFunction %void None %5
- %10 = OpLabel
- OpBranch %19
- %19 = OpLabel
- %20 = OpPhi %uint %uint_0 %10 %20 %21
- %22 = OpPhi %uint %uint_0 %10 %23 %21
- %24 = OpULessThanEqual %bool %22 %uint_1
- OpLoopMerge %25 %21 Unroll
- OpBranchConditional %24 %21 %25
- %21 = OpLabel
- %23 = OpIAdd %uint %22 %uint_1
- OpBranch %19
- %25 = OpLabel
- %14 = OpIAdd %uint %20 %uint_1
- OpReturn
- OpFunctionEnd
- )";
- const bool kFullyUnroll = true;
- const uint32_t kUnrollFactor = 0;
- SinglePassRunAndMatch<opt::LoopUnroller>(text, true, kFullyUnroll,
- kUnrollFactor);
- }
- // Test that a loop containing an unreachable merge block can still be unrolled
- // correctly.
- TEST_F(PassClassTest, UnreachableMerge) {
- const std::string text = R"(
- ; Identify the first iteration of the unrolled loop, and make sure it contains
- ; the unreachable merge block.
- ; The first SelectionMerge corresponds to the original loop merge.
- ; The second is the branch in the loop.
- ; CHECK: OpSelectionMerge {{%\w+}} None
- ; CHECK: OpSelectionMerge [[unrch1:%\w+]] None
- ; CHECK: [[unrch1]] = OpLabel
- ; CHECK-NEXT: OpUnreachable
- ; Identify the second iteration of the unrolled loop, and make sure it contains
- ; the unreachable merge block.
- ; The first SelectionMerge corresponds to the original loop merge
- ; The second is the branch in the loop.
- ; CHECK: OpSelectionMerge {{%\w+}} None
- ; CHECK: OpSelectionMerge [[unrch2:%\w+]] None
- ; CHECK: [[unrch2]] = OpLabel
- ; CHECK-NEXT: OpUnreachable
- OpCapability Shader
- OpMemoryModel Logical GLSL450
- OpEntryPoint GLCompute %main "main"
- OpExecutionMode %main LocalSize 64 1 1
- OpSource HLSL 600
- OpName %main "main"
- %uint = OpTypeInt 32 0
- %uint_0 = OpConstant %uint 0
- %uint_2 = OpConstant %uint 2
- %uint_1 = OpConstant %uint 1
- %bool = OpTypeBool
- %void = OpTypeVoid
- %18 = OpTypeFunction %void
- %main = OpFunction %void None %18
- %23 = OpLabel
- OpBranch %24
- %24 = OpLabel
- %28 = OpPhi %uint %uint_0 %23 %29 %27
- %30 = OpULessThan %bool %28 %uint_2
- OpLoopMerge %31 %27 Unroll
- OpBranchConditional %30 %32 %31
- %32 = OpLabel
- OpSelectionMerge %33 None
- OpSwitch %uint_0 %34
- %34 = OpLabel
- %35 = OpUndef %bool
- OpSelectionMerge %36 None
- OpBranchConditional %35 %37 %38
- %38 = OpLabel
- OpBranch %33
- %37 = OpLabel
- OpBranch %33
- %36 = OpLabel
- OpUnreachable
- %33 = OpLabel
- OpBranch %27
- %27 = OpLabel
- %29 = OpIAdd %uint %28 %uint_1
- OpBranch %24
- %31 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const bool kFullyUnroll = true;
- const uint32_t kUnrollFactor = 0;
- SinglePassRunAndMatch<opt::LoopUnroller>(text, true, kFullyUnroll,
- kUnrollFactor);
- }
- TEST_F(PassClassTest, InitValueIsConstantNull) {
- const std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpConstantNull %6
- %13 = OpConstant %6 1
- %21 = OpConstant %6 1
- %10 = OpTypeBool
- %17 = OpTypePointer Function %6
- %4 = OpFunction %2 None %3
- %11 = OpLabel
- OpBranch %5
- %5 = OpLabel
- %23 = OpPhi %6 %7 %11 %20 %15
- OpLoopMerge %8 %15 Unroll
- OpBranch %14
- %14 = OpLabel
- %9 = OpSLessThan %10 %23 %13
- OpBranchConditional %9 %15 %8
- %15 = OpLabel
- %20 = OpIAdd %6 %23 %21
- OpBranch %5
- %8 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const std::string output = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 320
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeInt 32 1
- %6 = OpConstantNull %5
- %7 = OpConstant %5 1
- %8 = OpConstant %5 1
- %9 = OpTypeBool
- %10 = OpTypePointer Function %5
- %2 = OpFunction %3 None %4
- %11 = OpLabel
- OpBranch %12
- %12 = OpLabel
- OpBranch %17
- %17 = OpLabel
- %18 = OpSLessThan %9 %6 %7
- OpBranch %15
- %15 = OpLabel
- %14 = OpIAdd %5 %6 %8
- OpBranch %16
- %16 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- auto context = BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, shader,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << shader << std::endl;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<LoopUnroller>(shader, output, false);
- }
- TEST_F(PassClassTest, ConditionValueIsConstantNull) {
- const std::string shader = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %4 "main"
- OpExecutionMode %4 OriginUpperLeft
- OpSource ESSL 320
- %2 = OpTypeVoid
- %3 = OpTypeFunction %2
- %6 = OpTypeInt 32 1
- %7 = OpConstantNull %6
- %13 = OpConstant %6 1
- %21 = OpConstant %6 1
- %10 = OpTypeBool
- %17 = OpTypePointer Function %6
- %4 = OpFunction %2 None %3
- %11 = OpLabel
- OpBranch %5
- %5 = OpLabel
- %23 = OpPhi %6 %13 %11 %20 %15
- OpLoopMerge %8 %15 Unroll
- OpBranch %14
- %14 = OpLabel
- %9 = OpSGreaterThan %10 %23 %7
- OpBranchConditional %9 %15 %8
- %15 = OpLabel
- %20 = OpISub %6 %23 %21
- OpBranch %5
- %8 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- const std::string output = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- OpSource ESSL 320
- %3 = OpTypeVoid
- %4 = OpTypeFunction %3
- %5 = OpTypeInt 32 1
- %6 = OpConstantNull %5
- %7 = OpConstant %5 1
- %8 = OpConstant %5 1
- %9 = OpTypeBool
- %10 = OpTypePointer Function %5
- %2 = OpFunction %3 None %4
- %11 = OpLabel
- OpBranch %12
- %12 = OpLabel
- OpBranch %17
- %17 = OpLabel
- %18 = OpSGreaterThan %9 %7 %6
- OpBranch %15
- %15 = OpLabel
- %14 = OpISub %5 %7 %8
- OpBranch %16
- %16 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- auto context = BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, shader,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
- << shader << std::endl;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndCheck<LoopUnroller>(shader, output, false);
- }
- TEST_F(PassClassTest, UnrollWithPhiReferencesPhi) {
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %main "main" %color
- OpExecutionMode %main OriginUpperLeft
- OpSource HLSL 600
- OpName %main "main"
- OpName %color "color"
- OpDecorate %color Location 0
- %uint = OpTypeInt 32 0
- %float = OpTypeFloat 32
- %float_0 = OpConstant %float 0
- %float_1 = OpConstant %float 1
- %uint_1 = OpConstant %uint 1
- %uint_3 = OpConstant %uint 3
- %void = OpTypeVoid
- %11 = OpTypeFunction %void
- %bool = OpTypeBool
- %v4float = OpTypeVector %float 4
- %_ptr_Output_v4float = OpTypePointer Output %v4float
- %color = OpVariable %_ptr_Output_v4float Output
- %main = OpFunction %void None %11
- %15 = OpLabel
- OpBranch %16
- %16 = OpLabel
- %17 = OpPhi %float %float_0 %15 %18 %19
- %18 = OpPhi %float %float_1 %15 %20 %19
- %21 = OpPhi %uint %uint_1 %15 %22 %19
- %23 = OpULessThanEqual %bool %21 %uint_3
- OpLoopMerge %24 %19 Unroll
- OpBranchConditional %23 %25 %24
- %25 = OpLabel
- ; First loop iteration
- ; CHECK: [[next_phi1_0:%\w+]] = OpFSub %float %float_1 %float_0
- ; Second loop iteration
- ; CHECK: [[next_phi1_1:%\w+]] = OpFSub %float [[next_phi1_0]] %float_1
- ; Third loop iteration
- ; CHECK: OpFSub %float [[next_phi1_1]] [[next_phi1_0]]
- %20 = OpFSub %float %18 %17
- OpBranch %19
- %19 = OpLabel
- %22 = OpIAdd %uint %21 %uint_1
- OpBranch %16
- %24 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
- SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
- SinglePassRunAndMatch<LoopUnroller>(text, true);
- }
- TEST_F(PassClassTest, UnrollWithDoublePhiReferencesPhi) {
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %main "main" %color
- OpExecutionMode %main OriginUpperLeft
- OpSource HLSL 600
- OpName %main "main"
- OpName %color "color"
- OpDecorate %color Location 0
- %uint = OpTypeInt 32 0
- %float = OpTypeFloat 32
- %float_0 = OpConstant %float 0
- %float_1 = OpConstant %float 1
- %uint_1 = OpConstant %uint 1
- %uint_3 = OpConstant %uint 3
- %void = OpTypeVoid
- %11 = OpTypeFunction %void
- %bool = OpTypeBool
- %v4float = OpTypeVector %float 4
- %_ptr_Output_v4float = OpTypePointer Output %v4float
- %color = OpVariable %_ptr_Output_v4float Output
- %main = OpFunction %void None %11
- %15 = OpLabel
- OpBranch %16
- %16 = OpLabel
- %17 = OpPhi %float %float_1 %15 %18 %19
- %18 = OpPhi %float %float_0 %15 %20 %19
- %20 = OpPhi %float %float_1 %15 %21 %19
- %22 = OpPhi %uint %uint_1 %15 %23 %19
- %24 = OpULessThanEqual %bool %22 %uint_3
- OpLoopMerge %25 %19 Unroll
- OpBranchConditional %24 %26 %25
- %26 = OpLabel
- ; First loop iteration
- ; CHECK: [[next_phi1_0:%\w+]] = OpFSub %float %float_1 %float_0
- ; CHECK: OpFMul %float %float_1
- ; Second loop iteration
- ; CHECK: [[next_phi1_1:%\w+]] = OpFSub %float [[next_phi1_0]] %float_1
- ; CHECK: OpFMul %float %float_0
- ; Third loop iteration
- ; CHECK: OpFSub %float [[next_phi1_1]] [[next_phi1_0]]
- ; CHECK: OpFMul %float %float_1
- %21 = OpFSub %float %20 %18
- %27 = OpFMul %float %17 %21
- OpBranch %19
- %19 = OpLabel
- %23 = OpIAdd %uint %22 %uint_1
- OpBranch %16
- %25 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
- SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
- SinglePassRunAndMatch<LoopUnroller>(text, true);
- }
- TEST_F(PassClassTest, PartialUnrollWithPhiReferencesPhi) {
- // With LocalMultiStoreElimPass
- const std::string text = R"(
- OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %main "main" %color
- OpExecutionMode %main OriginUpperLeft
- OpSource HLSL 600
- OpName %main "main"
- OpName %color "color"
- OpDecorate %color Location 0
- %uint = OpTypeInt 32 0
- %float = OpTypeFloat 32
- %float_0 = OpConstant %float 0
- %float_1 = OpConstant %float 1
- %uint_1 = OpConstant %uint 1
- %uint_3 = OpConstant %uint 3
- %void = OpTypeVoid
- %11 = OpTypeFunction %void
- %bool = OpTypeBool
- %v4float = OpTypeVector %float 4
- %_ptr_Output_v4float = OpTypePointer Output %v4float
- %color = OpVariable %_ptr_Output_v4float Output
- %main = OpFunction %void None %11
- %15 = OpLabel
- OpBranch %16
- %16 = OpLabel
- %17 = OpPhi %float %float_0 %15 %18 %19
- %18 = OpPhi %float %float_1 %15 %20 %19
- %21 = OpPhi %uint %uint_1 %15 %22 %19
- %23 = OpULessThanEqual %bool %21 %uint_3
- OpLoopMerge %24 %19 Unroll
- OpBranchConditional %23 %25 %24
- %25 = OpLabel
- ; CHECK: [[phi0_0:%\w+]] = OpPhi {{%\w+}} {{%\w+}} {{%\w+}} [[phi1_0:%\w+]]
- ; CHECK: [[phi1_0]] = OpPhi {{%\w+}} {{%\w+}} {{%\w+}} [[sub:%\w+]]
- ; CHECK: [[sub]] = OpFSub {{%\w+}} [[phi1_0]] [[phi0_0]]
- ; CHECK: [[phi0_1:%\w+]] = OpPhi {{%\w+}} [[phi0_0]]
- ; CHECK: [[phi1_1:%\w+]] = OpPhi {{%\w+}} [[phi1_0]]
- ; CHECK: [[sub:%\w+]] = OpFSub {{%\w+}} [[phi1_1]] [[phi0_1]]
- ; CHECK: OpFSub {{%\w+}} [[sub]] [[phi1_1]]
- %20 = OpFSub %float %18 %17
- OpBranch %19
- %19 = OpLabel
- %22 = OpIAdd %uint %21 %uint_1
- OpBranch %16
- %24 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- std::unique_ptr<IRContext> context =
- BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
- SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
- Module* module = context->module();
- EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
- << text << std::endl;
- LoopUnroller loop_unroller;
- SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
- SinglePassRunAndMatch<PartialUnrollerTestPass<2>>(text, true);
- }
- TEST_F(PassClassTest, DontUnrollInfiteLoop) {
- // This is an infinite loop that because the step is 0. We want to make sure
- // the unroller does not try to unroll it.
- const std::string text = R"(OpCapability Shader
- %1 = OpExtInstImport "GLSL.std.450"
- OpMemoryModel Logical GLSL450
- OpEntryPoint Fragment %2 "main"
- OpExecutionMode %2 OriginUpperLeft
- %void = OpTypeVoid
- %4 = OpTypeFunction %void
- %int = OpTypeInt 32 1
- %int_0 = OpConstant %int 0
- %int_50 = OpConstant %int 50
- %bool = OpTypeBool
- %int_0_0 = OpConstant %int 0
- %2 = OpFunction %void None %4
- %10 = OpLabel
- OpBranch %11
- %11 = OpLabel
- %12 = OpPhi %int %int_0 %10 %13 %14
- %15 = OpSLessThan %bool %12 %int_50
- OpLoopMerge %16 %14 Unroll
- OpBranchConditional %15 %14 %16
- %14 = OpLabel
- %13 = OpIAdd %int %12 %int_0_0
- OpBranch %11
- %16 = OpLabel
- OpReturn
- OpFunctionEnd
- )";
- SinglePassRunAndCheck<LoopUnroller>(text, text, false);
- }
- } // namespace
- } // namespace opt
- } // namespace spvtools
|