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