unroll_simple.cpp 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827
  1. // Copyright (c) 2018 Google LLC.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <memory>
  15. #include <vector>
  16. #include "gmock/gmock.h"
  17. #include "source/opt/loop_unroller.h"
  18. #include "source/opt/loop_utils.h"
  19. #include "source/opt/pass.h"
  20. #include "test/opt/assembly_builder.h"
  21. #include "test/opt/function_utils.h"
  22. #include "test/opt/pass_fixture.h"
  23. #include "test/opt/pass_utils.h"
  24. namespace spvtools {
  25. namespace opt {
  26. namespace {
  27. using ::testing::UnorderedElementsAre;
  28. using PassClassTest = PassTest<::testing::Test>;
  29. /*
  30. Generated from the following GLSL
  31. #version 330 core
  32. layout(location = 0) out vec4 c;
  33. void main() {
  34. float x[4];
  35. for (int i = 0; i < 4; ++i) {
  36. x[i] = 1.0f;
  37. }
  38. }
  39. */
  40. TEST_F(PassClassTest, SimpleFullyUnrollTest) {
  41. // With LocalMultiStoreElimPass
  42. const std::string text = R"(
  43. OpCapability Shader
  44. %1 = OpExtInstImport "GLSL.std.450"
  45. OpMemoryModel Logical GLSL450
  46. OpEntryPoint Fragment %2 "main" %3
  47. OpExecutionMode %2 OriginUpperLeft
  48. OpSource GLSL 330
  49. OpName %2 "main"
  50. OpName %5 "x"
  51. OpName %3 "c"
  52. OpDecorate %3 Location 0
  53. %6 = OpTypeVoid
  54. %7 = OpTypeFunction %6
  55. %8 = OpTypeInt 32 1
  56. %9 = OpTypePointer Function %8
  57. %10 = OpConstant %8 0
  58. %11 = OpConstant %8 4
  59. %12 = OpTypeBool
  60. %13 = OpTypeFloat 32
  61. %14 = OpTypeInt 32 0
  62. %15 = OpConstant %14 4
  63. %16 = OpTypeArray %13 %15
  64. %17 = OpTypePointer Function %16
  65. %18 = OpConstant %13 1
  66. %19 = OpTypePointer Function %13
  67. %20 = OpConstant %8 1
  68. %21 = OpTypeVector %13 4
  69. %22 = OpTypePointer Output %21
  70. %3 = OpVariable %22 Output
  71. %2 = OpFunction %6 None %7
  72. %23 = OpLabel
  73. %5 = OpVariable %17 Function
  74. OpBranch %24
  75. %24 = OpLabel
  76. %35 = OpPhi %8 %10 %23 %34 %26
  77. OpLoopMerge %25 %26 Unroll
  78. OpBranch %27
  79. %27 = OpLabel
  80. %29 = OpSLessThan %12 %35 %11
  81. OpBranchConditional %29 %30 %25
  82. %30 = OpLabel
  83. %32 = OpAccessChain %19 %5 %35
  84. OpStore %32 %18
  85. OpBranch %26
  86. %26 = OpLabel
  87. %34 = OpIAdd %8 %35 %20
  88. OpBranch %24
  89. %25 = OpLabel
  90. OpReturn
  91. OpFunctionEnd
  92. )";
  93. const std::string output = R"(OpCapability Shader
  94. %1 = OpExtInstImport "GLSL.std.450"
  95. OpMemoryModel Logical GLSL450
  96. OpEntryPoint Fragment %2 "main" %3
  97. OpExecutionMode %2 OriginUpperLeft
  98. OpSource GLSL 330
  99. OpName %2 "main"
  100. OpName %4 "x"
  101. OpName %3 "c"
  102. OpDecorate %3 Location 0
  103. %5 = OpTypeVoid
  104. %6 = OpTypeFunction %5
  105. %7 = OpTypeInt 32 1
  106. %8 = OpTypePointer Function %7
  107. %9 = OpConstant %7 0
  108. %10 = OpConstant %7 4
  109. %11 = OpTypeBool
  110. %12 = OpTypeFloat 32
  111. %13 = OpTypeInt 32 0
  112. %14 = OpConstant %13 4
  113. %15 = OpTypeArray %12 %14
  114. %16 = OpTypePointer Function %15
  115. %17 = OpConstant %12 1
  116. %18 = OpTypePointer Function %12
  117. %19 = OpConstant %7 1
  118. %20 = OpTypeVector %12 4
  119. %21 = OpTypePointer Output %20
  120. %3 = OpVariable %21 Output
  121. %2 = OpFunction %5 None %6
  122. %22 = OpLabel
  123. %4 = OpVariable %16 Function
  124. OpBranch %23
  125. %23 = OpLabel
  126. OpBranch %28
  127. %28 = OpLabel
  128. %29 = OpSLessThan %11 %9 %10
  129. OpBranch %30
  130. %30 = OpLabel
  131. %31 = OpAccessChain %18 %4 %9
  132. OpStore %31 %17
  133. OpBranch %26
  134. %26 = OpLabel
  135. %25 = OpIAdd %7 %9 %19
  136. OpBranch %32
  137. %32 = OpLabel
  138. OpBranch %34
  139. %34 = OpLabel
  140. %35 = OpSLessThan %11 %25 %10
  141. OpBranch %36
  142. %36 = OpLabel
  143. %37 = OpAccessChain %18 %4 %25
  144. OpStore %37 %17
  145. OpBranch %38
  146. %38 = OpLabel
  147. %39 = OpIAdd %7 %25 %19
  148. OpBranch %40
  149. %40 = OpLabel
  150. OpBranch %42
  151. %42 = OpLabel
  152. %43 = OpSLessThan %11 %39 %10
  153. OpBranch %44
  154. %44 = OpLabel
  155. %45 = OpAccessChain %18 %4 %39
  156. OpStore %45 %17
  157. OpBranch %46
  158. %46 = OpLabel
  159. %47 = OpIAdd %7 %39 %19
  160. OpBranch %48
  161. %48 = OpLabel
  162. OpBranch %50
  163. %50 = OpLabel
  164. %51 = OpSLessThan %11 %47 %10
  165. OpBranch %52
  166. %52 = OpLabel
  167. %53 = OpAccessChain %18 %4 %47
  168. OpStore %53 %17
  169. OpBranch %54
  170. %54 = OpLabel
  171. %55 = OpIAdd %7 %47 %19
  172. OpBranch %27
  173. %27 = OpLabel
  174. OpReturn
  175. OpFunctionEnd
  176. )";
  177. std::unique_ptr<IRContext> context =
  178. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  179. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  180. Module* module = context->module();
  181. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  182. << text << std::endl;
  183. LoopUnroller loop_unroller;
  184. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  185. SinglePassRunAndCheck<LoopUnroller>(text, output, false);
  186. }
  187. /*
  188. Generated from the following GLSL
  189. #version 330 core
  190. layout(location = 0) out vec4 c;
  191. void main() {
  192. float x[4];
  193. for (int i = 0; i < 4; ++i) {
  194. x[i] = 1.0f;
  195. }
  196. }
  197. */
  198. TEST_F(PassClassTest, SimpleFullyUnrollWithDebugInstructions) {
  199. // We must preserve the debug information including OpenCL.DebugInfo.100
  200. // instructions and OpLine instructions. Only the first block has
  201. // DebugDeclare and DebugValue used for the declaration (i.e., DebugValue
  202. // with Deref). Other blocks unrolled from the loop must not contain them.
  203. const std::string text = R"(
  204. OpCapability Shader
  205. %1 = OpExtInstImport "GLSL.std.450"
  206. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  207. OpMemoryModel Logical GLSL450
  208. OpEntryPoint Fragment %2 "main" %3
  209. OpExecutionMode %2 OriginUpperLeft
  210. OpSource GLSL 330
  211. %file_name = OpString "test"
  212. %float_name = OpString "float"
  213. %main_name = OpString "main"
  214. %f_name = OpString "f"
  215. %i_name = OpString "i"
  216. OpName %2 "main"
  217. OpName %5 "x"
  218. OpName %3 "c"
  219. OpDecorate %3 Location 0
  220. %6 = OpTypeVoid
  221. %7 = OpTypeFunction %6
  222. %8 = OpTypeInt 32 1
  223. %9 = OpTypePointer Function %8
  224. %10 = OpConstant %8 0
  225. %11 = OpConstant %8 4
  226. %12 = OpTypeBool
  227. %13 = OpTypeFloat 32
  228. %14 = OpTypeInt 32 0
  229. %uint_32 = OpConstant %14 32
  230. %15 = OpConstant %14 4
  231. %16 = OpTypeArray %13 %15
  232. %17 = OpTypePointer Function %16
  233. %18 = OpConstant %13 1
  234. %19 = OpTypePointer Function %13
  235. %20 = OpConstant %8 1
  236. %21 = OpTypeVector %13 4
  237. %22 = OpTypePointer Output %21
  238. %3 = OpVariable %22 Output
  239. %null_expr = OpExtInst %6 %ext DebugExpression
  240. %deref = OpExtInst %6 %ext DebugOperation Deref
  241. %deref_expr = OpExtInst %6 %ext DebugExpression %deref
  242. %src = OpExtInst %6 %ext DebugSource %file_name
  243. %cu = OpExtInst %6 %ext DebugCompilationUnit 1 4 %src HLSL
  244. %dbg_tf = OpExtInst %6 %ext DebugTypeBasic %float_name %uint_32 Float
  245. %dbg_v4f = OpExtInst %6 %ext DebugTypeVector %dbg_tf 4
  246. %main_ty = OpExtInst %6 %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
  247. %dbg_main = OpExtInst %6 %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %2
  248. %bb = OpExtInst %6 %ext DebugLexicalBlock %src 0 0 %dbg_main
  249. %dbg_f = OpExtInst %6 %ext DebugLocalVariable %f_name %dbg_v4f %src 0 0 %dbg_main FlagIsLocal
  250. %dbg_i = OpExtInst %6 %ext DebugLocalVariable %i_name %dbg_v4f %src 1 0 %bb FlagIsLocal
  251. ; CHECK: [[f:%\w+]] = OpString "f"
  252. ; CHECK: [[i:%\w+]] = OpString "i"
  253. ; CHECK: [[int_0:%\w+]] = OpConstant {{%\w+}} 0
  254. ; CHECK: [[null_expr:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugExpression
  255. ; CHECK: [[deref:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugOperation Deref
  256. ; CHECK: [[deref_expr:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugExpression [[deref]]
  257. ; CHECK: [[dbg_fn:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugFunction
  258. ; CHECK: [[dbg_bb:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugLexicalBlock
  259. ; CHECK: [[dbg_f:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugLocalVariable [[f]] {{%\w+}} {{%\w+}} 0 0 [[dbg_fn]]
  260. ; CHECK: [[dbg_i:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugLocalVariable [[i]] {{%\w+}} {{%\w+}} 1 0 [[dbg_bb]]
  261. %2 = OpFunction %6 None %7
  262. %23 = OpLabel
  263. ; The first block has DebugDeclare and DebugValue with Deref
  264. ;
  265. ; CHECK: OpLabel
  266. ; CHECK: DebugScope [[dbg_fn]]
  267. ; CHECK: [[x:%\w+]] = OpVariable {{%\w+}} Function
  268. ; CHECK: OpLine {{%\w+}} 0 0
  269. ; CHECK: OpBranch
  270. ; CHECK: OpLabel
  271. ; CHECK: DebugScope [[dbg_fn]]
  272. ; CHECK: DebugValue [[dbg_f]] [[int_0]] [[null_expr]]
  273. ; CHECK: OpBranch
  274. ; CHECK: DebugScope [[dbg_fn]]
  275. ; CHECK: OpLine {{%\w+}} 1 1
  276. ; CHECK: OpSLessThan
  277. ; CHECK: OpLine {{%\w+}} 2 0
  278. ; CHECK: OpBranch
  279. ; CHECK: OpLabel
  280. ; CHECK: DebugScope [[dbg_bb]]
  281. ; CHECK: DebugDeclare [[dbg_f]] [[x]] [[null_expr]]
  282. ; CHECK: DebugValue [[dbg_i]] [[x]] [[deref_expr]]
  283. ; CHECK: OpLine {{%\w+}} 3 0
  284. ;
  285. ; CHECK: OpLine {{%\w+}} 6 0
  286. ; CHECK: [[add:%\w+]] = OpIAdd
  287. ; CHECK: DebugValue [[dbg_f]] [[add]] [[null_expr]]
  288. ; CHECK: OpLine {{%\w+}} 7 0
  289. ; Other blocks do not have DebugDeclare and DebugValue with Deref
  290. ;
  291. ; CHECK: DebugScope [[dbg_fn]]
  292. ; CHECK: OpLine {{%\w+}} 1 1
  293. ; CHECK: OpSLessThan
  294. ; CHECK: OpLine {{%\w+}} 2 0
  295. ; CHECK: OpBranch
  296. ; CHECK: OpLabel
  297. ;
  298. ; CHECK: DebugScope [[dbg_bb]]
  299. ; CHECK-NOT: DebugDeclare [[dbg_f]] [[x]] [[null_expr]]
  300. ; CHECK-NOT: DebugValue [[dbg_i]] [[x]] [[deref_expr]]
  301. ; CHECK: OpLine {{%\w+}} 3 0
  302. ;
  303. ; CHECK: OpLine {{%\w+}} 6 0
  304. ; CHECK: [[add:%\w+]] = OpIAdd
  305. ; CHECK: DebugValue [[dbg_f]] [[add]] [[null_expr]]
  306. ; CHECK: OpLine {{%\w+}} 7 0
  307. ;
  308. ; CHECK-NOT: DebugDeclare [[dbg_f]] [[x]] [[null_expr]]
  309. ; CHECK-NOT: DebugValue [[dbg_i]] [[x]] [[deref_expr]]
  310. ; CHECK: DebugScope [[dbg_fn]]
  311. ; CHECK: OpLine {{%\w+}} 8 0
  312. ; CHECK: OpReturn
  313. %s0 = OpExtInst %6 %ext DebugScope %dbg_main
  314. %5 = OpVariable %17 Function
  315. OpLine %file_name 0 0
  316. OpBranch %24
  317. %24 = OpLabel
  318. %s1 = OpExtInst %6 %ext DebugScope %dbg_main
  319. %35 = OpPhi %8 %10 %23 %34 %26
  320. %value0 = OpExtInst %6 %ext DebugValue %dbg_f %35 %null_expr
  321. OpLine %file_name 1 0
  322. OpLoopMerge %25 %26 Unroll
  323. OpBranch %27
  324. %27 = OpLabel
  325. %s2 = OpExtInst %6 %ext DebugScope %dbg_main
  326. OpLine %file_name 1 1
  327. %29 = OpSLessThan %12 %35 %11
  328. OpLine %file_name 2 0
  329. OpBranchConditional %29 %30 %25
  330. %30 = OpLabel
  331. %s3 = OpExtInst %6 %ext DebugScope %bb
  332. %decl0 = OpExtInst %6 %ext DebugDeclare %dbg_f %5 %null_expr
  333. %decl1 = OpExtInst %6 %ext DebugValue %dbg_i %5 %deref_expr
  334. OpLine %file_name 3 0
  335. %32 = OpAccessChain %19 %5 %35
  336. OpLine %file_name 4 0
  337. OpStore %32 %18
  338. OpLine %file_name 5 0
  339. OpBranch %26
  340. %26 = OpLabel
  341. %s4 = OpExtInst %6 %ext DebugScope %dbg_main
  342. OpLine %file_name 6 0
  343. %34 = OpIAdd %8 %35 %20
  344. %value1 = OpExtInst %6 %ext DebugValue %dbg_f %34 %null_expr
  345. OpLine %file_name 7 0
  346. OpBranch %24
  347. %25 = OpLabel
  348. %s5 = OpExtInst %6 %ext DebugScope %dbg_main
  349. OpLine %file_name 8 0
  350. OpReturn
  351. OpFunctionEnd)";
  352. std::unique_ptr<IRContext> context =
  353. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  354. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  355. Module* module = context->module();
  356. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  357. << text << std::endl;
  358. LoopUnroller loop_unroller;
  359. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  360. SinglePassRunAndMatch<LoopUnroller>(text, true);
  361. }
  362. TEST_F(PassClassTest, SimpleFullyUnrollWithShaderDebugInstructions) {
  363. // We must preserve the debug information including
  364. // NonSemantic.Shader.DebugInfo.100 instructions and DebugLine instructions.
  365. const std::string text = R"(
  366. OpCapability Shader
  367. OpExtension "SPV_KHR_non_semantic_info"
  368. %1 = OpExtInstImport "GLSL.std.450"
  369. %ext = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
  370. OpMemoryModel Logical GLSL450
  371. OpEntryPoint Fragment %2 "main" %3
  372. OpExecutionMode %2 OriginUpperLeft
  373. OpSource GLSL 330
  374. %file_name = OpString "test"
  375. %float_name = OpString "float"
  376. %main_name = OpString "main"
  377. %f_name = OpString "f"
  378. %i_name = OpString "i"
  379. OpName %2 "main"
  380. OpName %5 "x"
  381. OpName %3 "c"
  382. OpDecorate %3 Location 0
  383. %6 = OpTypeVoid
  384. %7 = OpTypeFunction %6
  385. %8 = OpTypeInt 32 1
  386. %9 = OpTypePointer Function %8
  387. %10 = OpConstant %8 0
  388. %11 = OpConstant %8 4
  389. %12 = OpTypeBool
  390. %13 = OpTypeFloat 32
  391. %14 = OpTypeInt 32 0
  392. %uint_0 = OpConstant %14 0
  393. %uint_1 = OpConstant %14 1
  394. %uint_2 = OpConstant %14 2
  395. %uint_3 = OpConstant %14 3
  396. %uint_4 = OpConstant %14 4
  397. %uint_5 = OpConstant %14 5
  398. %uint_6 = OpConstant %14 6
  399. %uint_7 = OpConstant %14 7
  400. %uint_8 = OpConstant %14 8
  401. %uint_10 = OpConstant %14 10
  402. %uint_32 = OpConstant %14 32
  403. %15 = OpConstant %14 4
  404. %16 = OpTypeArray %13 %15
  405. %17 = OpTypePointer Function %16
  406. %18 = OpConstant %13 1
  407. %19 = OpTypePointer Function %13
  408. %20 = OpConstant %8 1
  409. %21 = OpTypeVector %13 4
  410. %22 = OpTypePointer Output %21
  411. %3 = OpVariable %22 Output
  412. %null_expr = OpExtInst %6 %ext DebugExpression
  413. %deref = OpExtInst %6 %ext DebugOperation %uint_0
  414. %deref_expr = OpExtInst %6 %ext DebugExpression %deref
  415. %src = OpExtInst %6 %ext DebugSource %file_name
  416. %cu = OpExtInst %6 %ext DebugCompilationUnit %uint_1 %uint_4 %src %uint_5
  417. %dbg_tf = OpExtInst %6 %ext DebugTypeBasic %float_name %uint_32 %uint_3 %uint_0
  418. %dbg_v4f = OpExtInst %6 %ext DebugTypeVector %dbg_tf %uint_4
  419. %main_ty = OpExtInst %6 %ext DebugTypeFunction %uint_3 %dbg_v4f %dbg_v4f
  420. %dbg_main = OpExtInst %6 %ext DebugFunction %main_name %main_ty %src %uint_0 %uint_0 %cu %main_name %uint_3 %uint_10
  421. %bb = OpExtInst %6 %ext DebugLexicalBlock %src %uint_0 %uint_0 %dbg_main
  422. %dbg_f = OpExtInst %6 %ext DebugLocalVariable %f_name %dbg_v4f %src %uint_0 %uint_0 %dbg_main %uint_4
  423. %dbg_i = OpExtInst %6 %ext DebugLocalVariable %i_name %dbg_v4f %src %uint_1 %uint_0 %bb %uint_4
  424. ; CHECK: [[f:%\w+]] = OpString "f"
  425. ; CHECK: [[i:%\w+]] = OpString "i"
  426. ; CHECK: [[int_0:%\w+]] = OpConstant {{%\w+}} 0
  427. ; CHECK: [[null_expr:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugExpression
  428. ; CHECK: [[deref:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugOperation %uint_0
  429. ; CHECK: [[deref_expr:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugExpression [[deref]]
  430. ; CHECK: [[dbg_fn:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugFunction
  431. ; CHECK: [[dbg_bb:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugLexicalBlock
  432. ; CHECK: [[dbg_f:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugLocalVariable [[f]] {{%\w+}} {{%\w+}} %uint_0 %uint_0 [[dbg_fn]]
  433. ; CHECK: [[dbg_i:%\w+]] = OpExtInst {{%\w+}} {{%\w+}} DebugLocalVariable [[i]] {{%\w+}} {{%\w+}} %uint_1 %uint_0 [[dbg_bb]]
  434. %2 = OpFunction %6 None %7
  435. %23 = OpLabel
  436. ; The first block has DebugDeclare and DebugValue with Deref
  437. ;
  438. ; CHECK: OpLabel
  439. ; CHECK: %x = OpVariable %_ptr_Function__arr_float_uint_4_0 Function
  440. ; CHECK: OpBranch
  441. ; CHECK: OpLabel
  442. ; CHECK: DebugScope [[dbg_fn]]
  443. ; CHECK: DebugValue [[dbg_f]] [[int_0]] [[null_expr]]
  444. ; CHECK: OpBranch
  445. ; CHECK: DebugScope [[dbg_fn]]
  446. ; CHECK: DebugLine {{%\w+}} %uint_1 %uint_1 %uint_1 %uint_1
  447. ; CHECK: OpSLessThan
  448. ; CHECK: DebugLine {{%\w+}} %uint_2 %uint_2 %uint_0 %uint_0
  449. ; CHECK: OpBranch
  450. ; CHECK: OpLabel
  451. ; CHECK: DebugScope [[dbg_bb]]
  452. ; CHECK: DebugDeclare [[dbg_f]] %x [[null_expr]]
  453. ; CHECK: DebugValue [[dbg_i]] %x [[deref_expr]]
  454. ; CHECK: DebugLine {{%\w+}} %uint_3 %uint_3 %uint_0 %uint_0
  455. ;
  456. ; CHECK: DebugLine {{%\w+}} %uint_6 %uint_6 %uint_0 %uint_0
  457. ; CHECK: [[add:%\w+]] = OpIAdd
  458. ; CHECK: DebugValue [[dbg_f]] [[add]] [[null_expr]]
  459. ; CHECK: DebugLine {{%\w+}} %uint_7 %uint_7 %uint_0 %uint_0
  460. ; Other blocks do not have DebugDeclare and DebugValue with Deref
  461. ;
  462. ; CHECK: DebugScope [[dbg_fn]]
  463. ; CHECK: DebugLine {{%\w+}} %uint_1 %uint_1 %uint_1 %uint_1
  464. ; CHECK: OpSLessThan
  465. ; CHECK: DebugLine {{%\w+}} %uint_2 %uint_2 %uint_0 %uint_0
  466. ; CHECK: OpBranch
  467. ; CHECK: OpLabel
  468. ;
  469. ; CHECK: DebugScope [[dbg_bb]]
  470. ; CHECK-NOT: DebugDeclare [[dbg_f]] %x [[null_expr]]
  471. ; CHECK-NOT: DebugValue [[dbg_i]] %x [[deref_expr]]
  472. ; CHECK: DebugLine {{%\w+}} %uint_3 %uint_3 %uint_0 %uint_0
  473. ;
  474. ; CHECK: DebugLine {{%\w+}} %uint_6 %uint_6 %uint_0 %uint_0
  475. ; CHECK: [[add:%\w+]] = OpIAdd
  476. ; CHECK: DebugValue [[dbg_f]] [[add]] [[null_expr]]
  477. ; CHECK: DebugLine {{%\w+}} %uint_7 %uint_7 %uint_0 %uint_0
  478. ;
  479. ; CHECK-NOT: DebugDeclare [[dbg_f]] %x [[null_expr]]
  480. ; CHECK-NOT: DebugValue [[dbg_i]] %x [[deref_expr]]
  481. ; CHECK: DebugScope [[dbg_fn]]
  482. ; CHECK: DebugLine {{%\w+}} %uint_8 %uint_8 %uint_0 %uint_0
  483. ; CHECK: OpReturn
  484. %5 = OpVariable %17 Function
  485. OpBranch %24
  486. %24 = OpLabel
  487. %35 = OpPhi %8 %10 %23 %34 %26
  488. %s1 = OpExtInst %6 %ext DebugScope %dbg_main
  489. %d10 = OpExtInst %6 %ext DebugLine %src %uint_1 %uint_1 %uint_0 %uint_0
  490. %value0 = OpExtInst %6 %ext DebugValue %dbg_f %35 %null_expr
  491. OpLoopMerge %25 %26 Unroll
  492. OpBranch %27
  493. %27 = OpLabel
  494. %s2 = OpExtInst %6 %ext DebugScope %dbg_main
  495. %d1 = OpExtInst %6 %ext DebugLine %src %uint_1 %uint_1 %uint_1 %uint_1
  496. %29 = OpSLessThan %12 %35 %11
  497. %d2 = OpExtInst %6 %ext DebugLine %src %uint_2 %uint_2 %uint_0 %uint_0
  498. OpBranchConditional %29 %30 %25
  499. %30 = OpLabel
  500. %s3 = OpExtInst %6 %ext DebugScope %bb
  501. %decl0 = OpExtInst %6 %ext DebugDeclare %dbg_f %5 %null_expr
  502. %decl1 = OpExtInst %6 %ext DebugValue %dbg_i %5 %deref_expr
  503. %d3 = OpExtInst %6 %ext DebugLine %src %uint_3 %uint_3 %uint_0 %uint_0
  504. %32 = OpAccessChain %19 %5 %35
  505. %d4 = OpExtInst %6 %ext DebugLine %src %uint_4 %uint_4 %uint_0 %uint_0
  506. OpStore %32 %18
  507. %d5 = OpExtInst %6 %ext DebugLine %src %uint_5 %uint_5 %uint_0 %uint_0
  508. OpBranch %26
  509. %26 = OpLabel
  510. %s4 = OpExtInst %6 %ext DebugScope %dbg_main
  511. %d6 = OpExtInst %6 %ext DebugLine %src %uint_6 %uint_6 %uint_0 %uint_0
  512. %34 = OpIAdd %8 %35 %20
  513. %value1 = OpExtInst %6 %ext DebugValue %dbg_f %34 %null_expr
  514. %d7 = OpExtInst %6 %ext DebugLine %src %uint_7 %uint_7 %uint_0 %uint_0
  515. OpBranch %24
  516. %25 = OpLabel
  517. %s5 = OpExtInst %6 %ext DebugScope %dbg_main
  518. %d8 = OpExtInst %6 %ext DebugLine %src %uint_8 %uint_8 %uint_0 %uint_0
  519. OpReturn
  520. OpFunctionEnd)";
  521. std::unique_ptr<IRContext> context =
  522. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  523. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  524. Module* module = context->module();
  525. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  526. << text << std::endl;
  527. LoopUnroller loop_unroller;
  528. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
  529. SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
  530. SinglePassRunAndMatch<LoopUnroller>(text, true);
  531. }
  532. template <int factor>
  533. class PartialUnrollerTestPass : public Pass {
  534. public:
  535. PartialUnrollerTestPass() : Pass() {}
  536. const char* name() const override { return "Loop unroller"; }
  537. Status Process() override {
  538. for (Function& f : *context()->module()) {
  539. LoopDescriptor& loop_descriptor = *context()->GetLoopDescriptor(&f);
  540. for (auto& loop : loop_descriptor) {
  541. LoopUtils loop_utils{context(), &loop};
  542. loop_utils.PartiallyUnroll(factor);
  543. }
  544. }
  545. return Pass::Status::SuccessWithChange;
  546. }
  547. };
  548. /*
  549. Generated from the following GLSL
  550. #version 330 core
  551. layout(location = 0) out vec4 c;
  552. void main() {
  553. float x[10];
  554. for (int i = 0; i < 10; ++i) {
  555. x[i] = 1.0f;
  556. }
  557. }
  558. */
  559. TEST_F(PassClassTest, SimplePartialUnroll) {
  560. // With LocalMultiStoreElimPass
  561. const std::string text = R"(
  562. OpCapability Shader
  563. %1 = OpExtInstImport "GLSL.std.450"
  564. OpMemoryModel Logical GLSL450
  565. OpEntryPoint Fragment %2 "main" %3
  566. OpExecutionMode %2 OriginUpperLeft
  567. OpSource GLSL 330
  568. OpName %2 "main"
  569. OpName %5 "x"
  570. OpName %3 "c"
  571. OpDecorate %3 Location 0
  572. %6 = OpTypeVoid
  573. %7 = OpTypeFunction %6
  574. %8 = OpTypeInt 32 1
  575. %9 = OpTypePointer Function %8
  576. %10 = OpConstant %8 0
  577. %11 = OpConstant %8 10
  578. %12 = OpTypeBool
  579. %13 = OpTypeFloat 32
  580. %14 = OpTypeInt 32 0
  581. %15 = OpConstant %14 10
  582. %16 = OpTypeArray %13 %15
  583. %17 = OpTypePointer Function %16
  584. %18 = OpConstant %13 1
  585. %19 = OpTypePointer Function %13
  586. %20 = OpConstant %8 1
  587. %21 = OpTypeVector %13 4
  588. %22 = OpTypePointer Output %21
  589. %3 = OpVariable %22 Output
  590. %2 = OpFunction %6 None %7
  591. %23 = OpLabel
  592. %5 = OpVariable %17 Function
  593. OpBranch %24
  594. %24 = OpLabel
  595. %35 = OpPhi %8 %10 %23 %34 %26
  596. OpLoopMerge %25 %26 Unroll
  597. OpBranch %27
  598. %27 = OpLabel
  599. %29 = OpSLessThan %12 %35 %11
  600. OpBranchConditional %29 %30 %25
  601. %30 = OpLabel
  602. %32 = OpAccessChain %19 %5 %35
  603. OpStore %32 %18
  604. OpBranch %26
  605. %26 = OpLabel
  606. %34 = OpIAdd %8 %35 %20
  607. OpBranch %24
  608. %25 = OpLabel
  609. OpReturn
  610. OpFunctionEnd
  611. )";
  612. const std::string output = R"(OpCapability Shader
  613. %1 = OpExtInstImport "GLSL.std.450"
  614. OpMemoryModel Logical GLSL450
  615. OpEntryPoint Fragment %2 "main" %3
  616. OpExecutionMode %2 OriginUpperLeft
  617. OpSource GLSL 330
  618. OpName %2 "main"
  619. OpName %4 "x"
  620. OpName %3 "c"
  621. OpDecorate %3 Location 0
  622. %5 = OpTypeVoid
  623. %6 = OpTypeFunction %5
  624. %7 = OpTypeInt 32 1
  625. %8 = OpTypePointer Function %7
  626. %9 = OpConstant %7 0
  627. %10 = OpConstant %7 10
  628. %11 = OpTypeBool
  629. %12 = OpTypeFloat 32
  630. %13 = OpTypeInt 32 0
  631. %14 = OpConstant %13 10
  632. %15 = OpTypeArray %12 %14
  633. %16 = OpTypePointer Function %15
  634. %17 = OpConstant %12 1
  635. %18 = OpTypePointer Function %12
  636. %19 = OpConstant %7 1
  637. %20 = OpTypeVector %12 4
  638. %21 = OpTypePointer Output %20
  639. %3 = OpVariable %21 Output
  640. %2 = OpFunction %5 None %6
  641. %22 = OpLabel
  642. %4 = OpVariable %16 Function
  643. OpBranch %23
  644. %23 = OpLabel
  645. %24 = OpPhi %7 %9 %22 %39 %38
  646. OpLoopMerge %27 %38 DontUnroll
  647. OpBranch %28
  648. %28 = OpLabel
  649. %29 = OpSLessThan %11 %24 %10
  650. OpBranchConditional %29 %30 %27
  651. %30 = OpLabel
  652. %31 = OpAccessChain %18 %4 %24
  653. OpStore %31 %17
  654. OpBranch %26
  655. %26 = OpLabel
  656. %25 = OpIAdd %7 %24 %19
  657. OpBranch %32
  658. %32 = OpLabel
  659. OpBranch %34
  660. %34 = OpLabel
  661. %35 = OpSLessThan %11 %25 %10
  662. OpBranch %36
  663. %36 = OpLabel
  664. %37 = OpAccessChain %18 %4 %25
  665. OpStore %37 %17
  666. OpBranch %38
  667. %38 = OpLabel
  668. %39 = OpIAdd %7 %25 %19
  669. OpBranch %23
  670. %27 = OpLabel
  671. OpReturn
  672. OpFunctionEnd
  673. )";
  674. std::unique_ptr<IRContext> context =
  675. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  676. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  677. Module* module = context->module();
  678. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  679. << text << std::endl;
  680. LoopUnroller loop_unroller;
  681. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  682. SinglePassRunAndCheck<PartialUnrollerTestPass<2>>(text, output, false);
  683. }
  684. /*
  685. Generated from the following GLSL
  686. #version 330 core
  687. layout(location = 0) out vec4 c;
  688. void main() {
  689. float x[10];
  690. for (int i = 0; i < 10; ++i) {
  691. x[i] = 1.0f;
  692. }
  693. }
  694. */
  695. TEST_F(PassClassTest, SimpleUnevenPartialUnroll) {
  696. // With LocalMultiStoreElimPass
  697. const std::string text = R"(
  698. OpCapability Shader
  699. %1 = OpExtInstImport "GLSL.std.450"
  700. OpMemoryModel Logical GLSL450
  701. OpEntryPoint Fragment %2 "main" %3
  702. OpExecutionMode %2 OriginUpperLeft
  703. OpSource GLSL 330
  704. OpName %2 "main"
  705. OpName %5 "x"
  706. OpName %3 "c"
  707. OpDecorate %3 Location 0
  708. %6 = OpTypeVoid
  709. %7 = OpTypeFunction %6
  710. %8 = OpTypeInt 32 1
  711. %9 = OpTypePointer Function %8
  712. %10 = OpConstant %8 0
  713. %11 = OpConstant %8 10
  714. %12 = OpTypeBool
  715. %13 = OpTypeFloat 32
  716. %14 = OpTypeInt 32 0
  717. %15 = OpConstant %14 10
  718. %16 = OpTypeArray %13 %15
  719. %17 = OpTypePointer Function %16
  720. %18 = OpConstant %13 1
  721. %19 = OpTypePointer Function %13
  722. %20 = OpConstant %8 1
  723. %21 = OpTypeVector %13 4
  724. %22 = OpTypePointer Output %21
  725. %3 = OpVariable %22 Output
  726. %2 = OpFunction %6 None %7
  727. %23 = OpLabel
  728. %5 = OpVariable %17 Function
  729. OpBranch %24
  730. %24 = OpLabel
  731. %35 = OpPhi %8 %10 %23 %34 %26
  732. OpLoopMerge %25 %26 Unroll
  733. OpBranch %27
  734. %27 = OpLabel
  735. %29 = OpSLessThan %12 %35 %11
  736. OpBranchConditional %29 %30 %25
  737. %30 = OpLabel
  738. %32 = OpAccessChain %19 %5 %35
  739. OpStore %32 %18
  740. OpBranch %26
  741. %26 = OpLabel
  742. %34 = OpIAdd %8 %35 %20
  743. OpBranch %24
  744. %25 = OpLabel
  745. OpReturn
  746. OpFunctionEnd
  747. )";
  748. const std::string output = R"(OpCapability Shader
  749. %1 = OpExtInstImport "GLSL.std.450"
  750. OpMemoryModel Logical GLSL450
  751. OpEntryPoint Fragment %2 "main" %3
  752. OpExecutionMode %2 OriginUpperLeft
  753. OpSource GLSL 330
  754. OpName %2 "main"
  755. OpName %4 "x"
  756. OpName %3 "c"
  757. OpDecorate %3 Location 0
  758. %5 = OpTypeVoid
  759. %6 = OpTypeFunction %5
  760. %7 = OpTypeInt 32 1
  761. %8 = OpTypePointer Function %7
  762. %9 = OpConstant %7 0
  763. %10 = OpConstant %7 10
  764. %11 = OpTypeBool
  765. %12 = OpTypeFloat 32
  766. %13 = OpTypeInt 32 0
  767. %14 = OpConstant %13 10
  768. %15 = OpTypeArray %12 %14
  769. %16 = OpTypePointer Function %15
  770. %17 = OpConstant %12 1
  771. %18 = OpTypePointer Function %12
  772. %19 = OpConstant %7 1
  773. %20 = OpTypeVector %12 4
  774. %21 = OpTypePointer Output %20
  775. %3 = OpVariable %21 Output
  776. %58 = OpConstant %13 1
  777. %2 = OpFunction %5 None %6
  778. %22 = OpLabel
  779. %4 = OpVariable %16 Function
  780. OpBranch %23
  781. %23 = OpLabel
  782. %24 = OpPhi %7 %9 %22 %25 %26
  783. OpLoopMerge %32 %26 Unroll
  784. OpBranch %28
  785. %28 = OpLabel
  786. %29 = OpSLessThan %11 %24 %58
  787. OpBranchConditional %29 %30 %32
  788. %30 = OpLabel
  789. %31 = OpAccessChain %18 %4 %24
  790. OpStore %31 %17
  791. OpBranch %26
  792. %26 = OpLabel
  793. %25 = OpIAdd %7 %24 %19
  794. OpBranch %23
  795. %32 = OpLabel
  796. OpBranch %33
  797. %33 = OpLabel
  798. %34 = OpPhi %7 %24 %32 %57 %56
  799. OpLoopMerge %41 %56 DontUnroll
  800. OpBranch %35
  801. %35 = OpLabel
  802. %36 = OpSLessThan %11 %34 %10
  803. OpBranchConditional %36 %37 %41
  804. %37 = OpLabel
  805. %38 = OpAccessChain %18 %4 %34
  806. OpStore %38 %17
  807. OpBranch %39
  808. %39 = OpLabel
  809. %40 = OpIAdd %7 %34 %19
  810. OpBranch %42
  811. %42 = OpLabel
  812. OpBranch %44
  813. %44 = OpLabel
  814. %45 = OpSLessThan %11 %40 %10
  815. OpBranch %46
  816. %46 = OpLabel
  817. %47 = OpAccessChain %18 %4 %40
  818. OpStore %47 %17
  819. OpBranch %48
  820. %48 = OpLabel
  821. %49 = OpIAdd %7 %40 %19
  822. OpBranch %50
  823. %50 = OpLabel
  824. OpBranch %52
  825. %52 = OpLabel
  826. %53 = OpSLessThan %11 %49 %10
  827. OpBranch %54
  828. %54 = OpLabel
  829. %55 = OpAccessChain %18 %4 %49
  830. OpStore %55 %17
  831. OpBranch %56
  832. %56 = OpLabel
  833. %57 = OpIAdd %7 %49 %19
  834. OpBranch %33
  835. %41 = OpLabel
  836. OpReturn
  837. %27 = OpLabel
  838. OpReturn
  839. OpFunctionEnd
  840. )";
  841. std::unique_ptr<IRContext> context =
  842. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  843. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  844. Module* module = context->module();
  845. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  846. << text << std::endl;
  847. LoopUnroller loop_unroller;
  848. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  849. // By unrolling by a factor that doesn't divide evenly into the number of loop
  850. // iterations we perform an additional transform when partially unrolling to
  851. // account for the remainder.
  852. SinglePassRunAndCheck<PartialUnrollerTestPass<3>>(text, output, false);
  853. }
  854. /* Generated from
  855. #version 410 core
  856. layout(location=0) flat in int upper_bound;
  857. void main() {
  858. float x[10];
  859. for (int i = 2; i < 8; i+=2) {
  860. x[i] = i;
  861. }
  862. }
  863. */
  864. TEST_F(PassClassTest, SimpleLoopIterationsCheck) {
  865. // With LocalMultiStoreElimPass
  866. const std::string text = R"(
  867. OpCapability Shader
  868. %1 = OpExtInstImport "GLSL.std.450"
  869. OpMemoryModel Logical GLSL450
  870. OpEntryPoint Fragment %2 "main" %3
  871. OpExecutionMode %2 OriginUpperLeft
  872. OpSource GLSL 410
  873. OpName %2 "main"
  874. OpName %5 "x"
  875. OpName %3 "upper_bound"
  876. OpDecorate %3 Flat
  877. OpDecorate %3 Location 0
  878. %6 = OpTypeVoid
  879. %7 = OpTypeFunction %6
  880. %8 = OpTypeInt 32 1
  881. %9 = OpTypePointer Function %8
  882. %10 = OpConstant %8 2
  883. %11 = OpConstant %8 8
  884. %12 = OpTypeBool
  885. %13 = OpTypeFloat 32
  886. %14 = OpTypeInt 32 0
  887. %15 = OpConstant %14 10
  888. %16 = OpTypeArray %13 %15
  889. %17 = OpTypePointer Function %16
  890. %18 = OpTypePointer Function %13
  891. %19 = OpTypePointer Input %8
  892. %3 = OpVariable %19 Input
  893. %2 = OpFunction %6 None %7
  894. %20 = OpLabel
  895. %5 = OpVariable %17 Function
  896. OpBranch %21
  897. %21 = OpLabel
  898. %34 = OpPhi %8 %10 %20 %33 %23
  899. OpLoopMerge %22 %23 Unroll
  900. OpBranch %24
  901. %24 = OpLabel
  902. %26 = OpSLessThan %12 %34 %11
  903. OpBranchConditional %26 %27 %22
  904. %27 = OpLabel
  905. %30 = OpConvertSToF %13 %34
  906. %31 = OpAccessChain %18 %5 %34
  907. OpStore %31 %30
  908. OpBranch %23
  909. %23 = OpLabel
  910. %33 = OpIAdd %8 %34 %10
  911. OpBranch %21
  912. %22 = OpLabel
  913. OpReturn
  914. OpFunctionEnd
  915. )";
  916. std::unique_ptr<IRContext> context =
  917. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  918. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  919. Module* module = context->module();
  920. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  921. << text << std::endl;
  922. Function* f = spvtest::GetFunction(module, 2);
  923. LoopDescriptor& loop_descriptor = *context->GetLoopDescriptor(f);
  924. EXPECT_EQ(loop_descriptor.NumLoops(), 1u);
  925. Loop& loop = loop_descriptor.GetLoopByIndex(0);
  926. EXPECT_TRUE(loop.HasUnrollLoopControl());
  927. BasicBlock* condition = loop.FindConditionBlock();
  928. EXPECT_EQ(condition->id(), 24u);
  929. Instruction* induction = loop.FindConditionVariable(condition);
  930. EXPECT_EQ(induction->result_id(), 34u);
  931. LoopUtils loop_utils{context.get(), &loop};
  932. EXPECT_TRUE(loop_utils.CanPerformUnroll());
  933. size_t iterations = 0;
  934. EXPECT_TRUE(loop.FindNumberOfIterations(induction, &*condition->ctail(),
  935. &iterations));
  936. EXPECT_EQ(iterations, 3u);
  937. }
  938. /* Generated from
  939. #version 410 core
  940. void main() {
  941. float x[10];
  942. for (int i = -1; i < 6; i+=3) {
  943. x[i] = i;
  944. }
  945. }
  946. */
  947. TEST_F(PassClassTest, SimpleLoopIterationsCheckSignedInit) {
  948. // With LocalMultiStoreElimPass
  949. const std::string text = R"(
  950. OpCapability Shader
  951. %1 = OpExtInstImport "GLSL.std.450"
  952. OpMemoryModel Logical GLSL450
  953. OpEntryPoint Fragment %2 "main" %3
  954. OpExecutionMode %2 OriginUpperLeft
  955. OpSource GLSL 410
  956. OpName %2 "main"
  957. OpName %5 "x"
  958. OpName %3 "upper_bound"
  959. OpDecorate %3 Flat
  960. OpDecorate %3 Location 0
  961. %6 = OpTypeVoid
  962. %7 = OpTypeFunction %6
  963. %8 = OpTypeInt 32 1
  964. %9 = OpTypePointer Function %8
  965. %10 = OpConstant %8 -1
  966. %11 = OpConstant %8 6
  967. %12 = OpTypeBool
  968. %13 = OpTypeFloat 32
  969. %14 = OpTypeInt 32 0
  970. %15 = OpConstant %14 10
  971. %16 = OpTypeArray %13 %15
  972. %17 = OpTypePointer Function %16
  973. %18 = OpTypePointer Function %13
  974. %19 = OpConstant %8 3
  975. %20 = OpTypePointer Input %8
  976. %3 = OpVariable %20 Input
  977. %2 = OpFunction %6 None %7
  978. %21 = OpLabel
  979. %5 = OpVariable %17 Function
  980. OpBranch %22
  981. %22 = OpLabel
  982. %35 = OpPhi %8 %10 %21 %34 %24
  983. OpLoopMerge %23 %24 None
  984. OpBranch %25
  985. %25 = OpLabel
  986. %27 = OpSLessThan %12 %35 %11
  987. OpBranchConditional %27 %28 %23
  988. %28 = OpLabel
  989. %31 = OpConvertSToF %13 %35
  990. %32 = OpAccessChain %18 %5 %35
  991. OpStore %32 %31
  992. OpBranch %24
  993. %24 = OpLabel
  994. %34 = OpIAdd %8 %35 %19
  995. OpBranch %22
  996. %23 = OpLabel
  997. OpReturn
  998. OpFunctionEnd
  999. )";
  1000. std::unique_ptr<IRContext> context =
  1001. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1002. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1003. Module* module = context->module();
  1004. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  1005. << text << std::endl;
  1006. Function* f = spvtest::GetFunction(module, 2);
  1007. LoopDescriptor& loop_descriptor = *context->GetLoopDescriptor(f);
  1008. EXPECT_EQ(loop_descriptor.NumLoops(), 1u);
  1009. Loop& loop = loop_descriptor.GetLoopByIndex(0);
  1010. EXPECT_FALSE(loop.HasUnrollLoopControl());
  1011. BasicBlock* condition = loop.FindConditionBlock();
  1012. EXPECT_EQ(condition->id(), 25u);
  1013. Instruction* induction = loop.FindConditionVariable(condition);
  1014. EXPECT_EQ(induction->result_id(), 35u);
  1015. LoopUtils loop_utils{context.get(), &loop};
  1016. EXPECT_TRUE(loop_utils.CanPerformUnroll());
  1017. size_t iterations = 0;
  1018. EXPECT_TRUE(loop.FindNumberOfIterations(induction, &*condition->ctail(),
  1019. &iterations));
  1020. EXPECT_EQ(iterations, 3u);
  1021. }
  1022. /*
  1023. Generated from the following GLSL
  1024. #version 410 core
  1025. void main() {
  1026. float out_array[6];
  1027. for (uint i = 0; i < 2; i++) {
  1028. for (int x = 0; x < 3; ++x) {
  1029. out_array[x + i*3] = i;
  1030. }
  1031. }
  1032. }
  1033. */
  1034. TEST_F(PassClassTest, UnrollNestedLoops) {
  1035. // With LocalMultiStoreElimPass
  1036. const std::string text = R"(
  1037. OpCapability Shader
  1038. %1 = OpExtInstImport "GLSL.std.450"
  1039. OpMemoryModel Logical GLSL450
  1040. OpEntryPoint Fragment %4 "main"
  1041. OpExecutionMode %4 OriginUpperLeft
  1042. OpSource GLSL 410
  1043. OpName %4 "main"
  1044. OpName %35 "out_array"
  1045. %2 = OpTypeVoid
  1046. %3 = OpTypeFunction %2
  1047. %6 = OpTypeInt 32 0
  1048. %7 = OpTypePointer Function %6
  1049. %9 = OpConstant %6 0
  1050. %16 = OpConstant %6 2
  1051. %17 = OpTypeBool
  1052. %19 = OpTypeInt 32 1
  1053. %20 = OpTypePointer Function %19
  1054. %22 = OpConstant %19 0
  1055. %29 = OpConstant %19 3
  1056. %31 = OpTypeFloat 32
  1057. %32 = OpConstant %6 6
  1058. %33 = OpTypeArray %31 %32
  1059. %34 = OpTypePointer Function %33
  1060. %39 = OpConstant %6 3
  1061. %44 = OpTypePointer Function %31
  1062. %47 = OpConstant %19 1
  1063. %4 = OpFunction %2 None %3
  1064. %5 = OpLabel
  1065. %35 = OpVariable %34 Function
  1066. OpBranch %10
  1067. %10 = OpLabel
  1068. %51 = OpPhi %6 %9 %5 %50 %13
  1069. OpLoopMerge %12 %13 Unroll
  1070. OpBranch %14
  1071. %14 = OpLabel
  1072. %18 = OpULessThan %17 %51 %16
  1073. OpBranchConditional %18 %11 %12
  1074. %11 = OpLabel
  1075. OpBranch %23
  1076. %23 = OpLabel
  1077. %54 = OpPhi %19 %22 %11 %48 %26
  1078. OpLoopMerge %25 %26 Unroll
  1079. OpBranch %27
  1080. %27 = OpLabel
  1081. %30 = OpSLessThan %17 %54 %29
  1082. OpBranchConditional %30 %24 %25
  1083. %24 = OpLabel
  1084. %37 = OpBitcast %6 %54
  1085. %40 = OpIMul %6 %51 %39
  1086. %41 = OpIAdd %6 %37 %40
  1087. %43 = OpConvertUToF %31 %51
  1088. %45 = OpAccessChain %44 %35 %41
  1089. OpStore %45 %43
  1090. OpBranch %26
  1091. %26 = OpLabel
  1092. %48 = OpIAdd %19 %54 %47
  1093. OpBranch %23
  1094. %25 = OpLabel
  1095. OpBranch %13
  1096. %13 = OpLabel
  1097. %50 = OpIAdd %6 %51 %47
  1098. OpBranch %10
  1099. %12 = OpLabel
  1100. OpReturn
  1101. OpFunctionEnd
  1102. )";
  1103. const std::string output = R"(OpCapability Shader
  1104. %1 = OpExtInstImport "GLSL.std.450"
  1105. OpMemoryModel Logical GLSL450
  1106. OpEntryPoint Fragment %2 "main"
  1107. OpExecutionMode %2 OriginUpperLeft
  1108. OpSource GLSL 410
  1109. OpName %2 "main"
  1110. OpName %3 "out_array"
  1111. %4 = OpTypeVoid
  1112. %5 = OpTypeFunction %4
  1113. %6 = OpTypeInt 32 0
  1114. %7 = OpTypePointer Function %6
  1115. %8 = OpConstant %6 0
  1116. %9 = OpConstant %6 2
  1117. %10 = OpTypeBool
  1118. %11 = OpTypeInt 32 1
  1119. %12 = OpTypePointer Function %11
  1120. %13 = OpConstant %11 0
  1121. %14 = OpConstant %11 3
  1122. %15 = OpTypeFloat 32
  1123. %16 = OpConstant %6 6
  1124. %17 = OpTypeArray %15 %16
  1125. %18 = OpTypePointer Function %17
  1126. %19 = OpConstant %6 3
  1127. %20 = OpTypePointer Function %15
  1128. %21 = OpConstant %11 1
  1129. %2 = OpFunction %4 None %5
  1130. %22 = OpLabel
  1131. %3 = OpVariable %18 Function
  1132. OpBranch %23
  1133. %23 = OpLabel
  1134. OpBranch %28
  1135. %28 = OpLabel
  1136. %29 = OpULessThan %10 %8 %9
  1137. OpBranch %30
  1138. %30 = OpLabel
  1139. OpBranch %31
  1140. %31 = OpLabel
  1141. OpBranch %36
  1142. %36 = OpLabel
  1143. %37 = OpSLessThan %10 %13 %14
  1144. OpBranch %38
  1145. %38 = OpLabel
  1146. %39 = OpBitcast %6 %13
  1147. %40 = OpIMul %6 %8 %19
  1148. %41 = OpIAdd %6 %39 %40
  1149. %42 = OpConvertUToF %15 %8
  1150. %43 = OpAccessChain %20 %3 %41
  1151. OpStore %43 %42
  1152. OpBranch %34
  1153. %34 = OpLabel
  1154. %33 = OpIAdd %11 %13 %21
  1155. OpBranch %44
  1156. %44 = OpLabel
  1157. OpBranch %46
  1158. %46 = OpLabel
  1159. %47 = OpSLessThan %10 %33 %14
  1160. OpBranch %48
  1161. %48 = OpLabel
  1162. %49 = OpBitcast %6 %33
  1163. %50 = OpIMul %6 %8 %19
  1164. %51 = OpIAdd %6 %49 %50
  1165. %52 = OpConvertUToF %15 %8
  1166. %53 = OpAccessChain %20 %3 %51
  1167. OpStore %53 %52
  1168. OpBranch %54
  1169. %54 = OpLabel
  1170. %55 = OpIAdd %11 %33 %21
  1171. OpBranch %56
  1172. %56 = OpLabel
  1173. OpBranch %58
  1174. %58 = OpLabel
  1175. %59 = OpSLessThan %10 %55 %14
  1176. OpBranch %60
  1177. %60 = OpLabel
  1178. %61 = OpBitcast %6 %55
  1179. %62 = OpIMul %6 %8 %19
  1180. %63 = OpIAdd %6 %61 %62
  1181. %64 = OpConvertUToF %15 %8
  1182. %65 = OpAccessChain %20 %3 %63
  1183. OpStore %65 %64
  1184. OpBranch %66
  1185. %66 = OpLabel
  1186. %67 = OpIAdd %11 %55 %21
  1187. OpBranch %35
  1188. %35 = OpLabel
  1189. OpBranch %26
  1190. %26 = OpLabel
  1191. %25 = OpIAdd %6 %8 %21
  1192. OpBranch %68
  1193. %68 = OpLabel
  1194. OpBranch %70
  1195. %70 = OpLabel
  1196. %71 = OpULessThan %10 %25 %9
  1197. OpBranch %72
  1198. %72 = OpLabel
  1199. OpBranch %73
  1200. %73 = OpLabel
  1201. OpBranch %74
  1202. %74 = OpLabel
  1203. %75 = OpSLessThan %10 %13 %14
  1204. OpBranch %76
  1205. %76 = OpLabel
  1206. %77 = OpBitcast %6 %13
  1207. %78 = OpIMul %6 %25 %19
  1208. %79 = OpIAdd %6 %77 %78
  1209. %80 = OpConvertUToF %15 %25
  1210. %81 = OpAccessChain %20 %3 %79
  1211. OpStore %81 %80
  1212. OpBranch %82
  1213. %82 = OpLabel
  1214. %83 = OpIAdd %11 %13 %21
  1215. OpBranch %84
  1216. %84 = OpLabel
  1217. OpBranch %85
  1218. %85 = OpLabel
  1219. %86 = OpSLessThan %10 %83 %14
  1220. OpBranch %87
  1221. %87 = OpLabel
  1222. %88 = OpBitcast %6 %83
  1223. %89 = OpIMul %6 %25 %19
  1224. %90 = OpIAdd %6 %88 %89
  1225. %91 = OpConvertUToF %15 %25
  1226. %92 = OpAccessChain %20 %3 %90
  1227. OpStore %92 %91
  1228. OpBranch %93
  1229. %93 = OpLabel
  1230. %94 = OpIAdd %11 %83 %21
  1231. OpBranch %95
  1232. %95 = OpLabel
  1233. OpBranch %96
  1234. %96 = OpLabel
  1235. %97 = OpSLessThan %10 %94 %14
  1236. OpBranch %98
  1237. %98 = OpLabel
  1238. %99 = OpBitcast %6 %94
  1239. %100 = OpIMul %6 %25 %19
  1240. %101 = OpIAdd %6 %99 %100
  1241. %102 = OpConvertUToF %15 %25
  1242. %103 = OpAccessChain %20 %3 %101
  1243. OpStore %103 %102
  1244. OpBranch %104
  1245. %104 = OpLabel
  1246. %105 = OpIAdd %11 %94 %21
  1247. OpBranch %106
  1248. %106 = OpLabel
  1249. OpBranch %107
  1250. %107 = OpLabel
  1251. %108 = OpIAdd %6 %25 %21
  1252. OpBranch %27
  1253. %27 = OpLabel
  1254. OpReturn
  1255. OpFunctionEnd
  1256. )";
  1257. std::unique_ptr<IRContext> context =
  1258. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1259. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1260. Module* module = context->module();
  1261. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  1262. << text << std::endl;
  1263. LoopUnroller loop_unroller;
  1264. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  1265. SinglePassRunAndCheck<LoopUnroller>(text, output, false);
  1266. }
  1267. /*
  1268. Generated from the following GLSL
  1269. #version 410 core
  1270. void main() {
  1271. float out_array[2];
  1272. for (int i = -3; i < -1; i++) {
  1273. out_array[3 + i] = i;
  1274. }
  1275. }
  1276. */
  1277. TEST_F(PassClassTest, NegativeConditionAndInit) {
  1278. // With LocalMultiStoreElimPass
  1279. const std::string text = R"(
  1280. OpCapability Shader
  1281. %1 = OpExtInstImport "GLSL.std.450"
  1282. OpMemoryModel Logical GLSL450
  1283. OpEntryPoint Fragment %4 "main"
  1284. OpExecutionMode %4 OriginUpperLeft
  1285. OpSource GLSL 410
  1286. OpName %4 "main"
  1287. OpName %23 "out_array"
  1288. %2 = OpTypeVoid
  1289. %3 = OpTypeFunction %2
  1290. %6 = OpTypeInt 32 1
  1291. %7 = OpTypePointer Function %6
  1292. %9 = OpConstant %6 -3
  1293. %16 = OpConstant %6 -1
  1294. %17 = OpTypeBool
  1295. %19 = OpTypeInt 32 0
  1296. %20 = OpConstant %19 2
  1297. %21 = OpTypeArray %6 %20
  1298. %22 = OpTypePointer Function %21
  1299. %25 = OpConstant %6 3
  1300. %30 = OpConstant %6 1
  1301. %4 = OpFunction %2 None %3
  1302. %5 = OpLabel
  1303. %23 = OpVariable %22 Function
  1304. OpBranch %10
  1305. %10 = OpLabel
  1306. %32 = OpPhi %6 %9 %5 %31 %13
  1307. OpLoopMerge %12 %13 Unroll
  1308. OpBranch %14
  1309. %14 = OpLabel
  1310. %18 = OpSLessThan %17 %32 %16
  1311. OpBranchConditional %18 %11 %12
  1312. %11 = OpLabel
  1313. %26 = OpIAdd %6 %32 %25
  1314. %28 = OpAccessChain %7 %23 %26
  1315. OpStore %28 %32
  1316. OpBranch %13
  1317. %13 = OpLabel
  1318. %31 = OpIAdd %6 %32 %30
  1319. OpBranch %10
  1320. %12 = OpLabel
  1321. OpReturn
  1322. OpFunctionEnd
  1323. )";
  1324. const std::string expected = R"(OpCapability Shader
  1325. %1 = OpExtInstImport "GLSL.std.450"
  1326. OpMemoryModel Logical GLSL450
  1327. OpEntryPoint Fragment %2 "main"
  1328. OpExecutionMode %2 OriginUpperLeft
  1329. OpSource GLSL 410
  1330. OpName %2 "main"
  1331. OpName %3 "out_array"
  1332. %4 = OpTypeVoid
  1333. %5 = OpTypeFunction %4
  1334. %6 = OpTypeInt 32 1
  1335. %7 = OpTypePointer Function %6
  1336. %8 = OpConstant %6 -3
  1337. %9 = OpConstant %6 -1
  1338. %10 = OpTypeBool
  1339. %11 = OpTypeInt 32 0
  1340. %12 = OpConstant %11 2
  1341. %13 = OpTypeArray %6 %12
  1342. %14 = OpTypePointer Function %13
  1343. %15 = OpConstant %6 3
  1344. %16 = OpConstant %6 1
  1345. %2 = OpFunction %4 None %5
  1346. %17 = OpLabel
  1347. %3 = OpVariable %14 Function
  1348. OpBranch %18
  1349. %18 = OpLabel
  1350. OpBranch %23
  1351. %23 = OpLabel
  1352. %24 = OpSLessThan %10 %8 %9
  1353. OpBranch %25
  1354. %25 = OpLabel
  1355. %26 = OpIAdd %6 %8 %15
  1356. %27 = OpAccessChain %7 %3 %26
  1357. OpStore %27 %8
  1358. OpBranch %21
  1359. %21 = OpLabel
  1360. %20 = OpIAdd %6 %8 %16
  1361. OpBranch %28
  1362. %28 = OpLabel
  1363. OpBranch %30
  1364. %30 = OpLabel
  1365. %31 = OpSLessThan %10 %20 %9
  1366. OpBranch %32
  1367. %32 = OpLabel
  1368. %33 = OpIAdd %6 %20 %15
  1369. %34 = OpAccessChain %7 %3 %33
  1370. OpStore %34 %20
  1371. OpBranch %35
  1372. %35 = OpLabel
  1373. %36 = OpIAdd %6 %20 %16
  1374. OpBranch %22
  1375. %22 = OpLabel
  1376. OpReturn
  1377. OpFunctionEnd
  1378. )";
  1379. std::unique_ptr<IRContext> context =
  1380. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1381. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1382. Module* module = context->module();
  1383. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  1384. << text << std::endl;
  1385. LoopUnroller loop_unroller;
  1386. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  1387. // SinglePassRunAndCheck<LoopUnroller>(text, expected, false);
  1388. Function* f = spvtest::GetFunction(module, 4);
  1389. LoopDescriptor& loop_descriptor = *context->GetLoopDescriptor(f);
  1390. EXPECT_EQ(loop_descriptor.NumLoops(), 1u);
  1391. Loop& loop = loop_descriptor.GetLoopByIndex(0);
  1392. EXPECT_TRUE(loop.HasUnrollLoopControl());
  1393. BasicBlock* condition = loop.FindConditionBlock();
  1394. EXPECT_EQ(condition->id(), 14u);
  1395. Instruction* induction = loop.FindConditionVariable(condition);
  1396. EXPECT_EQ(induction->result_id(), 32u);
  1397. LoopUtils loop_utils{context.get(), &loop};
  1398. EXPECT_TRUE(loop_utils.CanPerformUnroll());
  1399. size_t iterations = 0;
  1400. EXPECT_TRUE(loop.FindNumberOfIterations(induction, &*condition->ctail(),
  1401. &iterations));
  1402. EXPECT_EQ(iterations, 2u);
  1403. SinglePassRunAndCheck<LoopUnroller>(text, expected, false);
  1404. }
  1405. /*
  1406. Generated from the following GLSL
  1407. #version 410 core
  1408. void main() {
  1409. float out_array[9];
  1410. for (int i = -10; i < -1; i++) {
  1411. out_array[i] = i;
  1412. }
  1413. }
  1414. */
  1415. TEST_F(PassClassTest, NegativeConditionAndInitResidualUnroll) {
  1416. // With LocalMultiStoreElimPass
  1417. const std::string text = R"(
  1418. OpCapability Shader
  1419. %1 = OpExtInstImport "GLSL.std.450"
  1420. OpMemoryModel Logical GLSL450
  1421. OpEntryPoint Fragment %4 "main"
  1422. OpExecutionMode %4 OriginUpperLeft
  1423. OpSource GLSL 410
  1424. OpName %4 "main"
  1425. OpName %23 "out_array"
  1426. %2 = OpTypeVoid
  1427. %3 = OpTypeFunction %2
  1428. %6 = OpTypeInt 32 1
  1429. %7 = OpTypePointer Function %6
  1430. %9 = OpConstant %6 -10
  1431. %16 = OpConstant %6 -1
  1432. %17 = OpTypeBool
  1433. %19 = OpTypeInt 32 0
  1434. %20 = OpConstant %19 9
  1435. %21 = OpTypeArray %6 %20
  1436. %22 = OpTypePointer Function %21
  1437. %25 = OpConstant %6 10
  1438. %30 = OpConstant %6 1
  1439. %4 = OpFunction %2 None %3
  1440. %5 = OpLabel
  1441. %23 = OpVariable %22 Function
  1442. OpBranch %10
  1443. %10 = OpLabel
  1444. %32 = OpPhi %6 %9 %5 %31 %13
  1445. OpLoopMerge %12 %13 Unroll
  1446. OpBranch %14
  1447. %14 = OpLabel
  1448. %18 = OpSLessThan %17 %32 %16
  1449. OpBranchConditional %18 %11 %12
  1450. %11 = OpLabel
  1451. %26 = OpIAdd %6 %32 %25
  1452. %28 = OpAccessChain %7 %23 %26
  1453. OpStore %28 %32
  1454. OpBranch %13
  1455. %13 = OpLabel
  1456. %31 = OpIAdd %6 %32 %30
  1457. OpBranch %10
  1458. %12 = OpLabel
  1459. OpReturn
  1460. OpFunctionEnd
  1461. )";
  1462. const std::string expected = R"(OpCapability Shader
  1463. %1 = OpExtInstImport "GLSL.std.450"
  1464. OpMemoryModel Logical GLSL450
  1465. OpEntryPoint Fragment %2 "main"
  1466. OpExecutionMode %2 OriginUpperLeft
  1467. OpSource GLSL 410
  1468. OpName %2 "main"
  1469. OpName %3 "out_array"
  1470. %4 = OpTypeVoid
  1471. %5 = OpTypeFunction %4
  1472. %6 = OpTypeInt 32 1
  1473. %7 = OpTypePointer Function %6
  1474. %8 = OpConstant %6 -10
  1475. %9 = OpConstant %6 -1
  1476. %10 = OpTypeBool
  1477. %11 = OpTypeInt 32 0
  1478. %12 = OpConstant %11 9
  1479. %13 = OpTypeArray %6 %12
  1480. %14 = OpTypePointer Function %13
  1481. %15 = OpConstant %6 10
  1482. %16 = OpConstant %6 1
  1483. %48 = OpConstant %6 -9
  1484. %2 = OpFunction %4 None %5
  1485. %17 = OpLabel
  1486. %3 = OpVariable %14 Function
  1487. OpBranch %18
  1488. %18 = OpLabel
  1489. %19 = OpPhi %6 %8 %17 %20 %21
  1490. OpLoopMerge %28 %21 Unroll
  1491. OpBranch %23
  1492. %23 = OpLabel
  1493. %24 = OpSLessThan %10 %19 %48
  1494. OpBranchConditional %24 %25 %28
  1495. %25 = OpLabel
  1496. %26 = OpIAdd %6 %19 %15
  1497. %27 = OpAccessChain %7 %3 %26
  1498. OpStore %27 %19
  1499. OpBranch %21
  1500. %21 = OpLabel
  1501. %20 = OpIAdd %6 %19 %16
  1502. OpBranch %18
  1503. %28 = OpLabel
  1504. OpBranch %29
  1505. %29 = OpLabel
  1506. %30 = OpPhi %6 %19 %28 %47 %46
  1507. OpLoopMerge %38 %46 DontUnroll
  1508. OpBranch %31
  1509. %31 = OpLabel
  1510. %32 = OpSLessThan %10 %30 %9
  1511. OpBranchConditional %32 %33 %38
  1512. %33 = OpLabel
  1513. %34 = OpIAdd %6 %30 %15
  1514. %35 = OpAccessChain %7 %3 %34
  1515. OpStore %35 %30
  1516. OpBranch %36
  1517. %36 = OpLabel
  1518. %37 = OpIAdd %6 %30 %16
  1519. OpBranch %39
  1520. %39 = OpLabel
  1521. OpBranch %41
  1522. %41 = OpLabel
  1523. %42 = OpSLessThan %10 %37 %9
  1524. OpBranch %43
  1525. %43 = OpLabel
  1526. %44 = OpIAdd %6 %37 %15
  1527. %45 = OpAccessChain %7 %3 %44
  1528. OpStore %45 %37
  1529. OpBranch %46
  1530. %46 = OpLabel
  1531. %47 = OpIAdd %6 %37 %16
  1532. OpBranch %29
  1533. %38 = OpLabel
  1534. OpReturn
  1535. %22 = OpLabel
  1536. OpReturn
  1537. OpFunctionEnd
  1538. )";
  1539. std::unique_ptr<IRContext> context =
  1540. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1541. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1542. Module* module = context->module();
  1543. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  1544. << text << std::endl;
  1545. LoopUnroller loop_unroller;
  1546. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  1547. Function* f = spvtest::GetFunction(module, 4);
  1548. LoopDescriptor& loop_descriptor = *context->GetLoopDescriptor(f);
  1549. EXPECT_EQ(loop_descriptor.NumLoops(), 1u);
  1550. Loop& loop = loop_descriptor.GetLoopByIndex(0);
  1551. EXPECT_TRUE(loop.HasUnrollLoopControl());
  1552. BasicBlock* condition = loop.FindConditionBlock();
  1553. EXPECT_EQ(condition->id(), 14u);
  1554. Instruction* induction = loop.FindConditionVariable(condition);
  1555. EXPECT_EQ(induction->result_id(), 32u);
  1556. LoopUtils loop_utils{context.get(), &loop};
  1557. EXPECT_TRUE(loop_utils.CanPerformUnroll());
  1558. size_t iterations = 0;
  1559. EXPECT_TRUE(loop.FindNumberOfIterations(induction, &*condition->ctail(),
  1560. &iterations));
  1561. EXPECT_EQ(iterations, 9u);
  1562. SinglePassRunAndCheck<PartialUnrollerTestPass<2>>(text, expected, false);
  1563. }
  1564. /*
  1565. Generated from the following GLSL
  1566. #version 410 core
  1567. void main() {
  1568. float out_array[10];
  1569. for (uint i = 0; i < 2; i++) {
  1570. for (int x = 0; x < 5; ++x) {
  1571. out_array[x + i*5] = i;
  1572. }
  1573. }
  1574. }
  1575. */
  1576. TEST_F(PassClassTest, UnrollNestedLoopsValidateDescriptor) {
  1577. // With LocalMultiStoreElimPass
  1578. const std::string text = R"(
  1579. OpCapability Shader
  1580. %1 = OpExtInstImport "GLSL.std.450"
  1581. OpMemoryModel Logical GLSL450
  1582. OpEntryPoint Fragment %4 "main"
  1583. OpExecutionMode %4 OriginUpperLeft
  1584. OpSource GLSL 410
  1585. OpName %4 "main"
  1586. OpName %35 "out_array"
  1587. %2 = OpTypeVoid
  1588. %3 = OpTypeFunction %2
  1589. %6 = OpTypeInt 32 0
  1590. %7 = OpTypePointer Function %6
  1591. %9 = OpConstant %6 0
  1592. %16 = OpConstant %6 2
  1593. %17 = OpTypeBool
  1594. %19 = OpTypeInt 32 1
  1595. %20 = OpTypePointer Function %19
  1596. %22 = OpConstant %19 0
  1597. %29 = OpConstant %19 5
  1598. %31 = OpTypeFloat 32
  1599. %32 = OpConstant %6 10
  1600. %33 = OpTypeArray %31 %32
  1601. %34 = OpTypePointer Function %33
  1602. %39 = OpConstant %6 5
  1603. %44 = OpTypePointer Function %31
  1604. %47 = OpConstant %19 1
  1605. %4 = OpFunction %2 None %3
  1606. %5 = OpLabel
  1607. %35 = OpVariable %34 Function
  1608. OpBranch %10
  1609. %10 = OpLabel
  1610. %51 = OpPhi %6 %9 %5 %50 %13
  1611. OpLoopMerge %12 %13 Unroll
  1612. OpBranch %14
  1613. %14 = OpLabel
  1614. %18 = OpULessThan %17 %51 %16
  1615. OpBranchConditional %18 %11 %12
  1616. %11 = OpLabel
  1617. OpBranch %23
  1618. %23 = OpLabel
  1619. %54 = OpPhi %19 %22 %11 %48 %26
  1620. OpLoopMerge %25 %26 Unroll
  1621. OpBranch %27
  1622. %27 = OpLabel
  1623. %30 = OpSLessThan %17 %54 %29
  1624. OpBranchConditional %30 %24 %25
  1625. %24 = OpLabel
  1626. %37 = OpBitcast %6 %54
  1627. %40 = OpIMul %6 %51 %39
  1628. %41 = OpIAdd %6 %37 %40
  1629. %43 = OpConvertUToF %31 %51
  1630. %45 = OpAccessChain %44 %35 %41
  1631. OpStore %45 %43
  1632. OpBranch %26
  1633. %26 = OpLabel
  1634. %48 = OpIAdd %19 %54 %47
  1635. OpBranch %23
  1636. %25 = OpLabel
  1637. OpBranch %13
  1638. %13 = OpLabel
  1639. %50 = OpIAdd %6 %51 %47
  1640. OpBranch %10
  1641. %12 = OpLabel
  1642. OpReturn
  1643. OpFunctionEnd
  1644. )";
  1645. { // Test fully unroll
  1646. std::unique_ptr<IRContext> context =
  1647. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1648. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1649. Module* module = context->module();
  1650. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  1651. << text << std::endl;
  1652. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  1653. Function* f = spvtest::GetFunction(module, 4);
  1654. LoopDescriptor& loop_descriptor = *context->GetLoopDescriptor(f);
  1655. EXPECT_EQ(loop_descriptor.NumLoops(), 2u);
  1656. Loop& outer_loop = loop_descriptor.GetLoopByIndex(1);
  1657. EXPECT_TRUE(outer_loop.HasUnrollLoopControl());
  1658. Loop& inner_loop = loop_descriptor.GetLoopByIndex(0);
  1659. EXPECT_TRUE(inner_loop.HasUnrollLoopControl());
  1660. EXPECT_EQ(outer_loop.GetBlocks().size(), 9u);
  1661. EXPECT_EQ(inner_loop.GetBlocks().size(), 4u);
  1662. EXPECT_EQ(outer_loop.NumImmediateChildren(), 1u);
  1663. EXPECT_EQ(inner_loop.NumImmediateChildren(), 0u);
  1664. {
  1665. LoopUtils loop_utils{context.get(), &inner_loop};
  1666. loop_utils.FullyUnroll();
  1667. loop_utils.Finalize();
  1668. }
  1669. EXPECT_EQ(loop_descriptor.NumLoops(), 1u);
  1670. EXPECT_EQ(outer_loop.GetBlocks().size(), 25u);
  1671. EXPECT_EQ(outer_loop.NumImmediateChildren(), 0u);
  1672. {
  1673. LoopUtils loop_utils{context.get(), &outer_loop};
  1674. loop_utils.FullyUnroll();
  1675. loop_utils.Finalize();
  1676. }
  1677. EXPECT_EQ(loop_descriptor.NumLoops(), 0u);
  1678. }
  1679. { // Test partially unroll
  1680. std::unique_ptr<IRContext> context =
  1681. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1682. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1683. Module* module = context->module();
  1684. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  1685. << text << std::endl;
  1686. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  1687. Function* f = spvtest::GetFunction(module, 4);
  1688. LoopDescriptor& loop_descriptor = *context->GetLoopDescriptor(f);
  1689. EXPECT_EQ(loop_descriptor.NumLoops(), 2u);
  1690. Loop& outer_loop = loop_descriptor.GetLoopByIndex(1);
  1691. EXPECT_TRUE(outer_loop.HasUnrollLoopControl());
  1692. Loop& inner_loop = loop_descriptor.GetLoopByIndex(0);
  1693. EXPECT_TRUE(inner_loop.HasUnrollLoopControl());
  1694. EXPECT_EQ(outer_loop.GetBlocks().size(), 9u);
  1695. EXPECT_EQ(inner_loop.GetBlocks().size(), 4u);
  1696. EXPECT_EQ(outer_loop.NumImmediateChildren(), 1u);
  1697. EXPECT_EQ(inner_loop.NumImmediateChildren(), 0u);
  1698. LoopUtils loop_utils{context.get(), &inner_loop};
  1699. loop_utils.PartiallyUnroll(2);
  1700. loop_utils.Finalize();
  1701. // The number of loops should actually grow.
  1702. EXPECT_EQ(loop_descriptor.NumLoops(), 3u);
  1703. EXPECT_EQ(outer_loop.GetBlocks().size(), 18u);
  1704. EXPECT_EQ(outer_loop.NumImmediateChildren(), 2u);
  1705. }
  1706. }
  1707. /*
  1708. Generated from the following GLSL
  1709. #version 410 core
  1710. void main() {
  1711. float out_array[3];
  1712. for (int i = 3; i > 0; --i) {
  1713. out_array[i] = i;
  1714. }
  1715. }
  1716. */
  1717. TEST_F(PassClassTest, FullyUnrollNegativeStepLoopTest) {
  1718. // With LocalMultiStoreElimPass
  1719. const std::string text = R"(
  1720. OpCapability Shader
  1721. %1 = OpExtInstImport "GLSL.std.450"
  1722. OpMemoryModel Logical GLSL450
  1723. OpEntryPoint Fragment %4 "main"
  1724. OpExecutionMode %4 OriginUpperLeft
  1725. OpSource GLSL 410
  1726. OpName %4 "main"
  1727. OpName %24 "out_array"
  1728. %2 = OpTypeVoid
  1729. %3 = OpTypeFunction %2
  1730. %6 = OpTypeInt 32 1
  1731. %7 = OpTypePointer Function %6
  1732. %9 = OpConstant %6 3
  1733. %16 = OpConstant %6 0
  1734. %17 = OpTypeBool
  1735. %19 = OpTypeFloat 32
  1736. %20 = OpTypeInt 32 0
  1737. %21 = OpConstant %20 3
  1738. %22 = OpTypeArray %19 %21
  1739. %23 = OpTypePointer Function %22
  1740. %28 = OpTypePointer Function %19
  1741. %31 = OpConstant %6 1
  1742. %4 = OpFunction %2 None %3
  1743. %5 = OpLabel
  1744. %24 = OpVariable %23 Function
  1745. OpBranch %10
  1746. %10 = OpLabel
  1747. %33 = OpPhi %6 %9 %5 %32 %13
  1748. OpLoopMerge %12 %13 Unroll
  1749. OpBranch %14
  1750. %14 = OpLabel
  1751. %18 = OpSGreaterThan %17 %33 %16
  1752. OpBranchConditional %18 %11 %12
  1753. %11 = OpLabel
  1754. %27 = OpConvertSToF %19 %33
  1755. %29 = OpAccessChain %28 %24 %33
  1756. OpStore %29 %27
  1757. OpBranch %13
  1758. %13 = OpLabel
  1759. %32 = OpISub %6 %33 %31
  1760. OpBranch %10
  1761. %12 = OpLabel
  1762. OpReturn
  1763. OpFunctionEnd
  1764. )";
  1765. const std::string output = R"(OpCapability Shader
  1766. %1 = OpExtInstImport "GLSL.std.450"
  1767. OpMemoryModel Logical GLSL450
  1768. OpEntryPoint Fragment %2 "main"
  1769. OpExecutionMode %2 OriginUpperLeft
  1770. OpSource GLSL 410
  1771. OpName %2 "main"
  1772. OpName %3 "out_array"
  1773. %4 = OpTypeVoid
  1774. %5 = OpTypeFunction %4
  1775. %6 = OpTypeInt 32 1
  1776. %7 = OpTypePointer Function %6
  1777. %8 = OpConstant %6 3
  1778. %9 = OpConstant %6 0
  1779. %10 = OpTypeBool
  1780. %11 = OpTypeFloat 32
  1781. %12 = OpTypeInt 32 0
  1782. %13 = OpConstant %12 3
  1783. %14 = OpTypeArray %11 %13
  1784. %15 = OpTypePointer Function %14
  1785. %16 = OpTypePointer Function %11
  1786. %17 = OpConstant %6 1
  1787. %2 = OpFunction %4 None %5
  1788. %18 = OpLabel
  1789. %3 = OpVariable %15 Function
  1790. OpBranch %19
  1791. %19 = OpLabel
  1792. OpBranch %24
  1793. %24 = OpLabel
  1794. %25 = OpSGreaterThan %10 %8 %9
  1795. OpBranch %26
  1796. %26 = OpLabel
  1797. %27 = OpConvertSToF %11 %8
  1798. %28 = OpAccessChain %16 %3 %8
  1799. OpStore %28 %27
  1800. OpBranch %22
  1801. %22 = OpLabel
  1802. %21 = OpISub %6 %8 %17
  1803. OpBranch %29
  1804. %29 = OpLabel
  1805. OpBranch %31
  1806. %31 = OpLabel
  1807. %32 = OpSGreaterThan %10 %21 %9
  1808. OpBranch %33
  1809. %33 = OpLabel
  1810. %34 = OpConvertSToF %11 %21
  1811. %35 = OpAccessChain %16 %3 %21
  1812. OpStore %35 %34
  1813. OpBranch %36
  1814. %36 = OpLabel
  1815. %37 = OpISub %6 %21 %17
  1816. OpBranch %38
  1817. %38 = OpLabel
  1818. OpBranch %40
  1819. %40 = OpLabel
  1820. %41 = OpSGreaterThan %10 %37 %9
  1821. OpBranch %42
  1822. %42 = OpLabel
  1823. %43 = OpConvertSToF %11 %37
  1824. %44 = OpAccessChain %16 %3 %37
  1825. OpStore %44 %43
  1826. OpBranch %45
  1827. %45 = OpLabel
  1828. %46 = OpISub %6 %37 %17
  1829. OpBranch %23
  1830. %23 = OpLabel
  1831. OpReturn
  1832. OpFunctionEnd
  1833. )";
  1834. std::unique_ptr<IRContext> context =
  1835. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1836. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1837. Module* module = context->module();
  1838. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  1839. << text << std::endl;
  1840. LoopUnroller loop_unroller;
  1841. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  1842. SinglePassRunAndCheck<LoopUnroller>(text, output, false);
  1843. }
  1844. /*
  1845. Generated from the following GLSL
  1846. #version 410 core
  1847. void main() {
  1848. float out_array[3];
  1849. for (int i = 9; i > 0; i-=3) {
  1850. out_array[i] = i;
  1851. }
  1852. }
  1853. */
  1854. TEST_F(PassClassTest, FullyUnrollNegativeNonOneStepLoop) {
  1855. // With LocalMultiStoreElimPass
  1856. const std::string text = R"(
  1857. OpCapability Shader
  1858. %1 = OpExtInstImport "GLSL.std.450"
  1859. OpMemoryModel Logical GLSL450
  1860. OpEntryPoint Fragment %4 "main"
  1861. OpExecutionMode %4 OriginUpperLeft
  1862. OpSource GLSL 410
  1863. OpName %4 "main"
  1864. OpName %24 "out_array"
  1865. %2 = OpTypeVoid
  1866. %3 = OpTypeFunction %2
  1867. %6 = OpTypeInt 32 1
  1868. %7 = OpTypePointer Function %6
  1869. %9 = OpConstant %6 9
  1870. %16 = OpConstant %6 0
  1871. %17 = OpTypeBool
  1872. %19 = OpTypeFloat 32
  1873. %20 = OpTypeInt 32 0
  1874. %21 = OpConstant %20 3
  1875. %22 = OpTypeArray %19 %21
  1876. %23 = OpTypePointer Function %22
  1877. %28 = OpTypePointer Function %19
  1878. %30 = OpConstant %6 3
  1879. %4 = OpFunction %2 None %3
  1880. %5 = OpLabel
  1881. %24 = OpVariable %23 Function
  1882. OpBranch %10
  1883. %10 = OpLabel
  1884. %33 = OpPhi %6 %9 %5 %32 %13
  1885. OpLoopMerge %12 %13 Unroll
  1886. OpBranch %14
  1887. %14 = OpLabel
  1888. %18 = OpSGreaterThan %17 %33 %16
  1889. OpBranchConditional %18 %11 %12
  1890. %11 = OpLabel
  1891. %27 = OpConvertSToF %19 %33
  1892. %29 = OpAccessChain %28 %24 %33
  1893. OpStore %29 %27
  1894. OpBranch %13
  1895. %13 = OpLabel
  1896. %32 = OpISub %6 %33 %30
  1897. OpBranch %10
  1898. %12 = OpLabel
  1899. OpReturn
  1900. OpFunctionEnd
  1901. )";
  1902. const std::string output = R"(OpCapability Shader
  1903. %1 = OpExtInstImport "GLSL.std.450"
  1904. OpMemoryModel Logical GLSL450
  1905. OpEntryPoint Fragment %2 "main"
  1906. OpExecutionMode %2 OriginUpperLeft
  1907. OpSource GLSL 410
  1908. OpName %2 "main"
  1909. OpName %3 "out_array"
  1910. %4 = OpTypeVoid
  1911. %5 = OpTypeFunction %4
  1912. %6 = OpTypeInt 32 1
  1913. %7 = OpTypePointer Function %6
  1914. %8 = OpConstant %6 9
  1915. %9 = OpConstant %6 0
  1916. %10 = OpTypeBool
  1917. %11 = OpTypeFloat 32
  1918. %12 = OpTypeInt 32 0
  1919. %13 = OpConstant %12 3
  1920. %14 = OpTypeArray %11 %13
  1921. %15 = OpTypePointer Function %14
  1922. %16 = OpTypePointer Function %11
  1923. %17 = OpConstant %6 3
  1924. %2 = OpFunction %4 None %5
  1925. %18 = OpLabel
  1926. %3 = OpVariable %15 Function
  1927. OpBranch %19
  1928. %19 = OpLabel
  1929. OpBranch %24
  1930. %24 = OpLabel
  1931. %25 = OpSGreaterThan %10 %8 %9
  1932. OpBranch %26
  1933. %26 = OpLabel
  1934. %27 = OpConvertSToF %11 %8
  1935. %28 = OpAccessChain %16 %3 %8
  1936. OpStore %28 %27
  1937. OpBranch %22
  1938. %22 = OpLabel
  1939. %21 = OpISub %6 %8 %17
  1940. OpBranch %29
  1941. %29 = OpLabel
  1942. OpBranch %31
  1943. %31 = OpLabel
  1944. %32 = OpSGreaterThan %10 %21 %9
  1945. OpBranch %33
  1946. %33 = OpLabel
  1947. %34 = OpConvertSToF %11 %21
  1948. %35 = OpAccessChain %16 %3 %21
  1949. OpStore %35 %34
  1950. OpBranch %36
  1951. %36 = OpLabel
  1952. %37 = OpISub %6 %21 %17
  1953. OpBranch %38
  1954. %38 = OpLabel
  1955. OpBranch %40
  1956. %40 = OpLabel
  1957. %41 = OpSGreaterThan %10 %37 %9
  1958. OpBranch %42
  1959. %42 = OpLabel
  1960. %43 = OpConvertSToF %11 %37
  1961. %44 = OpAccessChain %16 %3 %37
  1962. OpStore %44 %43
  1963. OpBranch %45
  1964. %45 = OpLabel
  1965. %46 = OpISub %6 %37 %17
  1966. OpBranch %23
  1967. %23 = OpLabel
  1968. OpReturn
  1969. OpFunctionEnd
  1970. )";
  1971. std::unique_ptr<IRContext> context =
  1972. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  1973. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1974. Module* module = context->module();
  1975. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  1976. << text << std::endl;
  1977. LoopUnroller loop_unroller;
  1978. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  1979. SinglePassRunAndCheck<LoopUnroller>(text, output, false);
  1980. }
  1981. /*
  1982. Generated from the following GLSL
  1983. #version 410 core
  1984. void main() {
  1985. float out_array[3];
  1986. for (int i = 0; i < 7; i+=3) {
  1987. out_array[i] = i;
  1988. }
  1989. }
  1990. */
  1991. TEST_F(PassClassTest, FullyUnrollNonDivisibleStepLoop) {
  1992. // With LocalMultiStoreElimPass
  1993. const std::string text = R"(OpCapability Shader
  1994. %1 = OpExtInstImport "GLSL.std.450"
  1995. OpMemoryModel Logical GLSL450
  1996. OpEntryPoint Fragment %4 "main"
  1997. OpExecutionMode %4 OriginUpperLeft
  1998. OpSource GLSL 410
  1999. OpName %4 "main"
  2000. OpName %24 "out_array"
  2001. %2 = OpTypeVoid
  2002. %3 = OpTypeFunction %2
  2003. %6 = OpTypeInt 32 1
  2004. %7 = OpTypePointer Function %6
  2005. %9 = OpConstant %6 0
  2006. %16 = OpConstant %6 7
  2007. %17 = OpTypeBool
  2008. %19 = OpTypeFloat 32
  2009. %20 = OpTypeInt 32 0
  2010. %21 = OpConstant %20 3
  2011. %22 = OpTypeArray %19 %21
  2012. %23 = OpTypePointer Function %22
  2013. %28 = OpTypePointer Function %19
  2014. %30 = OpConstant %6 3
  2015. %4 = OpFunction %2 None %3
  2016. %5 = OpLabel
  2017. %24 = OpVariable %23 Function
  2018. OpBranch %10
  2019. %10 = OpLabel
  2020. %33 = OpPhi %6 %9 %5 %32 %13
  2021. OpLoopMerge %12 %13 Unroll
  2022. OpBranch %14
  2023. %14 = OpLabel
  2024. %18 = OpSLessThan %17 %33 %16
  2025. OpBranchConditional %18 %11 %12
  2026. %11 = OpLabel
  2027. %27 = OpConvertSToF %19 %33
  2028. %29 = OpAccessChain %28 %24 %33
  2029. OpStore %29 %27
  2030. OpBranch %13
  2031. %13 = OpLabel
  2032. %32 = OpIAdd %6 %33 %30
  2033. OpBranch %10
  2034. %12 = OpLabel
  2035. OpReturn
  2036. OpFunctionEnd
  2037. )";
  2038. const std::string output = R"(OpCapability Shader
  2039. %1 = OpExtInstImport "GLSL.std.450"
  2040. OpMemoryModel Logical GLSL450
  2041. OpEntryPoint Fragment %2 "main"
  2042. OpExecutionMode %2 OriginUpperLeft
  2043. OpSource GLSL 410
  2044. OpName %2 "main"
  2045. OpName %3 "out_array"
  2046. %4 = OpTypeVoid
  2047. %5 = OpTypeFunction %4
  2048. %6 = OpTypeInt 32 1
  2049. %7 = OpTypePointer Function %6
  2050. %8 = OpConstant %6 0
  2051. %9 = OpConstant %6 7
  2052. %10 = OpTypeBool
  2053. %11 = OpTypeFloat 32
  2054. %12 = OpTypeInt 32 0
  2055. %13 = OpConstant %12 3
  2056. %14 = OpTypeArray %11 %13
  2057. %15 = OpTypePointer Function %14
  2058. %16 = OpTypePointer Function %11
  2059. %17 = OpConstant %6 3
  2060. %2 = OpFunction %4 None %5
  2061. %18 = OpLabel
  2062. %3 = OpVariable %15 Function
  2063. OpBranch %19
  2064. %19 = OpLabel
  2065. OpBranch %24
  2066. %24 = OpLabel
  2067. %25 = OpSLessThan %10 %8 %9
  2068. OpBranch %26
  2069. %26 = OpLabel
  2070. %27 = OpConvertSToF %11 %8
  2071. %28 = OpAccessChain %16 %3 %8
  2072. OpStore %28 %27
  2073. OpBranch %22
  2074. %22 = OpLabel
  2075. %21 = OpIAdd %6 %8 %17
  2076. OpBranch %29
  2077. %29 = OpLabel
  2078. OpBranch %31
  2079. %31 = OpLabel
  2080. %32 = OpSLessThan %10 %21 %9
  2081. OpBranch %33
  2082. %33 = OpLabel
  2083. %34 = OpConvertSToF %11 %21
  2084. %35 = OpAccessChain %16 %3 %21
  2085. OpStore %35 %34
  2086. OpBranch %36
  2087. %36 = OpLabel
  2088. %37 = OpIAdd %6 %21 %17
  2089. OpBranch %38
  2090. %38 = OpLabel
  2091. OpBranch %40
  2092. %40 = OpLabel
  2093. %41 = OpSLessThan %10 %37 %9
  2094. OpBranch %42
  2095. %42 = OpLabel
  2096. %43 = OpConvertSToF %11 %37
  2097. %44 = OpAccessChain %16 %3 %37
  2098. OpStore %44 %43
  2099. OpBranch %45
  2100. %45 = OpLabel
  2101. %46 = OpIAdd %6 %37 %17
  2102. OpBranch %23
  2103. %23 = OpLabel
  2104. OpReturn
  2105. OpFunctionEnd
  2106. )";
  2107. std::unique_ptr<IRContext> context =
  2108. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  2109. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2110. Module* module = context->module();
  2111. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  2112. << text << std::endl;
  2113. LoopUnroller loop_unroller;
  2114. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  2115. SinglePassRunAndCheck<LoopUnroller>(text, output, false);
  2116. }
  2117. /*
  2118. Generated from the following GLSL
  2119. #version 410 core
  2120. void main() {
  2121. float out_array[4];
  2122. for (int i = 11; i > 0; i-=3) {
  2123. out_array[i] = i;
  2124. }
  2125. }
  2126. */
  2127. TEST_F(PassClassTest, FullyUnrollNegativeNonDivisibleStepLoop) {
  2128. // With LocalMultiStoreElimPass
  2129. const std::string text = R"(OpCapability Shader
  2130. %1 = OpExtInstImport "GLSL.std.450"
  2131. OpMemoryModel Logical GLSL450
  2132. OpEntryPoint Fragment %4 "main"
  2133. OpExecutionMode %4 OriginUpperLeft
  2134. OpSource GLSL 410
  2135. OpName %4 "main"
  2136. OpName %24 "out_array"
  2137. %2 = OpTypeVoid
  2138. %3 = OpTypeFunction %2
  2139. %6 = OpTypeInt 32 1
  2140. %7 = OpTypePointer Function %6
  2141. %9 = OpConstant %6 11
  2142. %16 = OpConstant %6 0
  2143. %17 = OpTypeBool
  2144. %19 = OpTypeFloat 32
  2145. %20 = OpTypeInt 32 0
  2146. %21 = OpConstant %20 4
  2147. %22 = OpTypeArray %19 %21
  2148. %23 = OpTypePointer Function %22
  2149. %28 = OpTypePointer Function %19
  2150. %30 = OpConstant %6 3
  2151. %4 = OpFunction %2 None %3
  2152. %5 = OpLabel
  2153. %24 = OpVariable %23 Function
  2154. OpBranch %10
  2155. %10 = OpLabel
  2156. %33 = OpPhi %6 %9 %5 %32 %13
  2157. OpLoopMerge %12 %13 Unroll
  2158. OpBranch %14
  2159. %14 = OpLabel
  2160. %18 = OpSGreaterThan %17 %33 %16
  2161. OpBranchConditional %18 %11 %12
  2162. %11 = OpLabel
  2163. %27 = OpConvertSToF %19 %33
  2164. %29 = OpAccessChain %28 %24 %33
  2165. OpStore %29 %27
  2166. OpBranch %13
  2167. %13 = OpLabel
  2168. %32 = OpISub %6 %33 %30
  2169. OpBranch %10
  2170. %12 = OpLabel
  2171. OpReturn
  2172. OpFunctionEnd
  2173. )";
  2174. const std::string output = R"(OpCapability Shader
  2175. %1 = OpExtInstImport "GLSL.std.450"
  2176. OpMemoryModel Logical GLSL450
  2177. OpEntryPoint Fragment %2 "main"
  2178. OpExecutionMode %2 OriginUpperLeft
  2179. OpSource GLSL 410
  2180. OpName %2 "main"
  2181. OpName %3 "out_array"
  2182. %4 = OpTypeVoid
  2183. %5 = OpTypeFunction %4
  2184. %6 = OpTypeInt 32 1
  2185. %7 = OpTypePointer Function %6
  2186. %8 = OpConstant %6 11
  2187. %9 = OpConstant %6 0
  2188. %10 = OpTypeBool
  2189. %11 = OpTypeFloat 32
  2190. %12 = OpTypeInt 32 0
  2191. %13 = OpConstant %12 4
  2192. %14 = OpTypeArray %11 %13
  2193. %15 = OpTypePointer Function %14
  2194. %16 = OpTypePointer Function %11
  2195. %17 = OpConstant %6 3
  2196. %2 = OpFunction %4 None %5
  2197. %18 = OpLabel
  2198. %3 = OpVariable %15 Function
  2199. OpBranch %19
  2200. %19 = OpLabel
  2201. OpBranch %24
  2202. %24 = OpLabel
  2203. %25 = OpSGreaterThan %10 %8 %9
  2204. OpBranch %26
  2205. %26 = OpLabel
  2206. %27 = OpConvertSToF %11 %8
  2207. %28 = OpAccessChain %16 %3 %8
  2208. OpStore %28 %27
  2209. OpBranch %22
  2210. %22 = OpLabel
  2211. %21 = OpISub %6 %8 %17
  2212. OpBranch %29
  2213. %29 = OpLabel
  2214. OpBranch %31
  2215. %31 = OpLabel
  2216. %32 = OpSGreaterThan %10 %21 %9
  2217. OpBranch %33
  2218. %33 = OpLabel
  2219. %34 = OpConvertSToF %11 %21
  2220. %35 = OpAccessChain %16 %3 %21
  2221. OpStore %35 %34
  2222. OpBranch %36
  2223. %36 = OpLabel
  2224. %37 = OpISub %6 %21 %17
  2225. OpBranch %38
  2226. %38 = OpLabel
  2227. OpBranch %40
  2228. %40 = OpLabel
  2229. %41 = OpSGreaterThan %10 %37 %9
  2230. OpBranch %42
  2231. %42 = OpLabel
  2232. %43 = OpConvertSToF %11 %37
  2233. %44 = OpAccessChain %16 %3 %37
  2234. OpStore %44 %43
  2235. OpBranch %45
  2236. %45 = OpLabel
  2237. %46 = OpISub %6 %37 %17
  2238. OpBranch %47
  2239. %47 = OpLabel
  2240. OpBranch %49
  2241. %49 = OpLabel
  2242. %50 = OpSGreaterThan %10 %46 %9
  2243. OpBranch %51
  2244. %51 = OpLabel
  2245. %52 = OpConvertSToF %11 %46
  2246. %53 = OpAccessChain %16 %3 %46
  2247. OpStore %53 %52
  2248. OpBranch %54
  2249. %54 = OpLabel
  2250. %55 = OpISub %6 %46 %17
  2251. OpBranch %23
  2252. %23 = OpLabel
  2253. OpReturn
  2254. OpFunctionEnd
  2255. )";
  2256. std::unique_ptr<IRContext> context =
  2257. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  2258. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2259. Module* module = context->module();
  2260. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  2261. << text << std::endl;
  2262. LoopUnroller loop_unroller;
  2263. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  2264. SinglePassRunAndCheck<LoopUnroller>(text, output, false);
  2265. }
  2266. // With LocalMultiStoreElimPass
  2267. static const std::string multiple_phi_shader = R"(
  2268. OpCapability Shader
  2269. %1 = OpExtInstImport "GLSL.std.450"
  2270. OpMemoryModel Logical GLSL450
  2271. OpEntryPoint Fragment %4 "main"
  2272. OpExecutionMode %4 OriginUpperLeft
  2273. OpSource GLSL 410
  2274. OpName %4 "main"
  2275. OpName %8 "foo("
  2276. %2 = OpTypeVoid
  2277. %3 = OpTypeFunction %2
  2278. %6 = OpTypeInt 32 1
  2279. %7 = OpTypeFunction %6
  2280. %10 = OpTypePointer Function %6
  2281. %12 = OpConstant %6 0
  2282. %14 = OpConstant %6 3
  2283. %22 = OpConstant %6 6
  2284. %23 = OpTypeBool
  2285. %31 = OpConstant %6 1
  2286. %4 = OpFunction %2 None %3
  2287. %5 = OpLabel
  2288. %40 = OpFunctionCall %6 %8
  2289. OpReturn
  2290. OpFunctionEnd
  2291. %8 = OpFunction %6 None %7
  2292. %9 = OpLabel
  2293. OpBranch %16
  2294. %16 = OpLabel
  2295. %41 = OpPhi %6 %12 %9 %34 %19
  2296. %42 = OpPhi %6 %14 %9 %29 %19
  2297. %43 = OpPhi %6 %12 %9 %32 %19
  2298. OpLoopMerge %18 %19 Unroll
  2299. OpBranch %20
  2300. %20 = OpLabel
  2301. %24 = OpSLessThan %23 %43 %22
  2302. OpBranchConditional %24 %17 %18
  2303. %17 = OpLabel
  2304. %27 = OpIMul %6 %43 %41
  2305. %29 = OpIAdd %6 %42 %27
  2306. OpBranch %19
  2307. %19 = OpLabel
  2308. %32 = OpIAdd %6 %43 %31
  2309. %34 = OpISub %6 %41 %31
  2310. OpBranch %16
  2311. %18 = OpLabel
  2312. %37 = OpIAdd %6 %42 %41
  2313. OpReturnValue %37
  2314. OpFunctionEnd
  2315. )";
  2316. TEST_F(PassClassTest, PartiallyUnrollResidualMultipleInductionVariables) {
  2317. const std::string output = R"(OpCapability Shader
  2318. %1 = OpExtInstImport "GLSL.std.450"
  2319. OpMemoryModel Logical GLSL450
  2320. OpEntryPoint Fragment %2 "main"
  2321. OpExecutionMode %2 OriginUpperLeft
  2322. OpSource GLSL 410
  2323. OpName %2 "main"
  2324. OpName %3 "foo("
  2325. %4 = OpTypeVoid
  2326. %5 = OpTypeFunction %4
  2327. %6 = OpTypeInt 32 1
  2328. %7 = OpTypeFunction %6
  2329. %8 = OpTypePointer Function %6
  2330. %9 = OpConstant %6 0
  2331. %10 = OpConstant %6 3
  2332. %11 = OpConstant %6 6
  2333. %12 = OpTypeBool
  2334. %13 = OpConstant %6 1
  2335. %82 = OpTypeInt 32 0
  2336. %83 = OpConstant %82 2
  2337. %2 = OpFunction %4 None %5
  2338. %14 = OpLabel
  2339. %15 = OpFunctionCall %6 %3
  2340. OpReturn
  2341. OpFunctionEnd
  2342. %3 = OpFunction %6 None %7
  2343. %16 = OpLabel
  2344. OpBranch %17
  2345. %17 = OpLabel
  2346. %18 = OpPhi %6 %9 %16 %19 %20
  2347. %21 = OpPhi %6 %10 %16 %22 %20
  2348. %23 = OpPhi %6 %9 %16 %24 %20
  2349. OpLoopMerge %31 %20 Unroll
  2350. OpBranch %26
  2351. %26 = OpLabel
  2352. %27 = OpSLessThan %12 %23 %83
  2353. OpBranchConditional %27 %28 %31
  2354. %28 = OpLabel
  2355. %29 = OpIMul %6 %23 %18
  2356. %22 = OpIAdd %6 %21 %29
  2357. OpBranch %20
  2358. %20 = OpLabel
  2359. %24 = OpIAdd %6 %23 %13
  2360. %19 = OpISub %6 %18 %13
  2361. OpBranch %17
  2362. %31 = OpLabel
  2363. OpBranch %32
  2364. %32 = OpLabel
  2365. %33 = OpPhi %6 %18 %31 %81 %79
  2366. %34 = OpPhi %6 %21 %31 %78 %79
  2367. %35 = OpPhi %6 %23 %31 %80 %79
  2368. OpLoopMerge %44 %79 DontUnroll
  2369. OpBranch %36
  2370. %36 = OpLabel
  2371. %37 = OpSLessThan %12 %35 %11
  2372. OpBranchConditional %37 %38 %44
  2373. %38 = OpLabel
  2374. %39 = OpIMul %6 %35 %33
  2375. %40 = OpIAdd %6 %34 %39
  2376. OpBranch %41
  2377. %41 = OpLabel
  2378. %42 = OpIAdd %6 %35 %13
  2379. %43 = OpISub %6 %33 %13
  2380. OpBranch %46
  2381. %46 = OpLabel
  2382. OpBranch %50
  2383. %50 = OpLabel
  2384. %51 = OpSLessThan %12 %42 %11
  2385. OpBranch %52
  2386. %52 = OpLabel
  2387. %53 = OpIMul %6 %42 %43
  2388. %54 = OpIAdd %6 %40 %53
  2389. OpBranch %55
  2390. %55 = OpLabel
  2391. %56 = OpIAdd %6 %42 %13
  2392. %57 = OpISub %6 %43 %13
  2393. OpBranch %58
  2394. %58 = OpLabel
  2395. OpBranch %62
  2396. %62 = OpLabel
  2397. %63 = OpSLessThan %12 %56 %11
  2398. OpBranch %64
  2399. %64 = OpLabel
  2400. %65 = OpIMul %6 %56 %57
  2401. %66 = OpIAdd %6 %54 %65
  2402. OpBranch %67
  2403. %67 = OpLabel
  2404. %68 = OpIAdd %6 %56 %13
  2405. %69 = OpISub %6 %57 %13
  2406. OpBranch %70
  2407. %70 = OpLabel
  2408. OpBranch %74
  2409. %74 = OpLabel
  2410. %75 = OpSLessThan %12 %68 %11
  2411. OpBranch %76
  2412. %76 = OpLabel
  2413. %77 = OpIMul %6 %68 %69
  2414. %78 = OpIAdd %6 %66 %77
  2415. OpBranch %79
  2416. %79 = OpLabel
  2417. %80 = OpIAdd %6 %68 %13
  2418. %81 = OpISub %6 %69 %13
  2419. OpBranch %32
  2420. %44 = OpLabel
  2421. %45 = OpIAdd %6 %34 %33
  2422. OpReturnValue %45
  2423. %25 = OpLabel
  2424. %30 = OpIAdd %6 %34 %33
  2425. OpReturnValue %30
  2426. OpFunctionEnd
  2427. )";
  2428. std::unique_ptr<IRContext> context =
  2429. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, multiple_phi_shader,
  2430. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2431. Module* module = context->module();
  2432. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  2433. << multiple_phi_shader << std::endl;
  2434. LoopUnroller loop_unroller;
  2435. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  2436. SinglePassRunAndCheck<PartialUnrollerTestPass<4>>(multiple_phi_shader, output,
  2437. false);
  2438. }
  2439. TEST_F(PassClassTest, PartiallyUnrollMultipleInductionVariables) {
  2440. const std::string output = R"(OpCapability Shader
  2441. %1 = OpExtInstImport "GLSL.std.450"
  2442. OpMemoryModel Logical GLSL450
  2443. OpEntryPoint Fragment %2 "main"
  2444. OpExecutionMode %2 OriginUpperLeft
  2445. OpSource GLSL 410
  2446. OpName %2 "main"
  2447. OpName %3 "foo("
  2448. %4 = OpTypeVoid
  2449. %5 = OpTypeFunction %4
  2450. %6 = OpTypeInt 32 1
  2451. %7 = OpTypeFunction %6
  2452. %8 = OpTypePointer Function %6
  2453. %9 = OpConstant %6 0
  2454. %10 = OpConstant %6 3
  2455. %11 = OpConstant %6 6
  2456. %12 = OpTypeBool
  2457. %13 = OpConstant %6 1
  2458. %2 = OpFunction %4 None %5
  2459. %14 = OpLabel
  2460. %15 = OpFunctionCall %6 %3
  2461. OpReturn
  2462. OpFunctionEnd
  2463. %3 = OpFunction %6 None %7
  2464. %16 = OpLabel
  2465. OpBranch %17
  2466. %17 = OpLabel
  2467. %18 = OpPhi %6 %9 %16 %42 %40
  2468. %21 = OpPhi %6 %10 %16 %39 %40
  2469. %23 = OpPhi %6 %9 %16 %41 %40
  2470. OpLoopMerge %25 %40 DontUnroll
  2471. OpBranch %26
  2472. %26 = OpLabel
  2473. %27 = OpSLessThan %12 %23 %11
  2474. OpBranchConditional %27 %28 %25
  2475. %28 = OpLabel
  2476. %29 = OpIMul %6 %23 %18
  2477. %22 = OpIAdd %6 %21 %29
  2478. OpBranch %20
  2479. %20 = OpLabel
  2480. %24 = OpIAdd %6 %23 %13
  2481. %19 = OpISub %6 %18 %13
  2482. OpBranch %31
  2483. %31 = OpLabel
  2484. OpBranch %35
  2485. %35 = OpLabel
  2486. %36 = OpSLessThan %12 %24 %11
  2487. OpBranch %37
  2488. %37 = OpLabel
  2489. %38 = OpIMul %6 %24 %19
  2490. %39 = OpIAdd %6 %22 %38
  2491. OpBranch %40
  2492. %40 = OpLabel
  2493. %41 = OpIAdd %6 %24 %13
  2494. %42 = OpISub %6 %19 %13
  2495. OpBranch %17
  2496. %25 = OpLabel
  2497. %30 = OpIAdd %6 %21 %18
  2498. OpReturnValue %30
  2499. OpFunctionEnd
  2500. )";
  2501. std::unique_ptr<IRContext> context =
  2502. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, multiple_phi_shader,
  2503. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2504. Module* module = context->module();
  2505. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  2506. << multiple_phi_shader << std::endl;
  2507. LoopUnroller loop_unroller;
  2508. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  2509. SinglePassRunAndCheck<PartialUnrollerTestPass<2>>(multiple_phi_shader, output,
  2510. false);
  2511. }
  2512. TEST_F(PassClassTest, FullyUnrollMultipleInductionVariables) {
  2513. const std::string output = R"(OpCapability Shader
  2514. %1 = OpExtInstImport "GLSL.std.450"
  2515. OpMemoryModel Logical GLSL450
  2516. OpEntryPoint Fragment %2 "main"
  2517. OpExecutionMode %2 OriginUpperLeft
  2518. OpSource GLSL 410
  2519. OpName %2 "main"
  2520. OpName %3 "foo("
  2521. %4 = OpTypeVoid
  2522. %5 = OpTypeFunction %4
  2523. %6 = OpTypeInt 32 1
  2524. %7 = OpTypeFunction %6
  2525. %8 = OpTypePointer Function %6
  2526. %9 = OpConstant %6 0
  2527. %10 = OpConstant %6 3
  2528. %11 = OpConstant %6 6
  2529. %12 = OpTypeBool
  2530. %13 = OpConstant %6 1
  2531. %2 = OpFunction %4 None %5
  2532. %14 = OpLabel
  2533. %15 = OpFunctionCall %6 %3
  2534. OpReturn
  2535. OpFunctionEnd
  2536. %3 = OpFunction %6 None %7
  2537. %16 = OpLabel
  2538. OpBranch %17
  2539. %17 = OpLabel
  2540. OpBranch %26
  2541. %26 = OpLabel
  2542. %27 = OpSLessThan %12 %9 %11
  2543. OpBranch %28
  2544. %28 = OpLabel
  2545. %29 = OpIMul %6 %9 %9
  2546. %22 = OpIAdd %6 %10 %29
  2547. OpBranch %20
  2548. %20 = OpLabel
  2549. %24 = OpIAdd %6 %9 %13
  2550. %19 = OpISub %6 %9 %13
  2551. OpBranch %31
  2552. %31 = OpLabel
  2553. OpBranch %35
  2554. %35 = OpLabel
  2555. %36 = OpSLessThan %12 %24 %11
  2556. OpBranch %37
  2557. %37 = OpLabel
  2558. %38 = OpIMul %6 %24 %19
  2559. %39 = OpIAdd %6 %22 %38
  2560. OpBranch %40
  2561. %40 = OpLabel
  2562. %41 = OpIAdd %6 %24 %13
  2563. %42 = OpISub %6 %19 %13
  2564. OpBranch %43
  2565. %43 = OpLabel
  2566. OpBranch %47
  2567. %47 = OpLabel
  2568. %48 = OpSLessThan %12 %41 %11
  2569. OpBranch %49
  2570. %49 = OpLabel
  2571. %50 = OpIMul %6 %41 %42
  2572. %51 = OpIAdd %6 %39 %50
  2573. OpBranch %52
  2574. %52 = OpLabel
  2575. %53 = OpIAdd %6 %41 %13
  2576. %54 = OpISub %6 %42 %13
  2577. OpBranch %55
  2578. %55 = OpLabel
  2579. OpBranch %59
  2580. %59 = OpLabel
  2581. %60 = OpSLessThan %12 %53 %11
  2582. OpBranch %61
  2583. %61 = OpLabel
  2584. %62 = OpIMul %6 %53 %54
  2585. %63 = OpIAdd %6 %51 %62
  2586. OpBranch %64
  2587. %64 = OpLabel
  2588. %65 = OpIAdd %6 %53 %13
  2589. %66 = OpISub %6 %54 %13
  2590. OpBranch %67
  2591. %67 = OpLabel
  2592. OpBranch %71
  2593. %71 = OpLabel
  2594. %72 = OpSLessThan %12 %65 %11
  2595. OpBranch %73
  2596. %73 = OpLabel
  2597. %74 = OpIMul %6 %65 %66
  2598. %75 = OpIAdd %6 %63 %74
  2599. OpBranch %76
  2600. %76 = OpLabel
  2601. %77 = OpIAdd %6 %65 %13
  2602. %78 = OpISub %6 %66 %13
  2603. OpBranch %79
  2604. %79 = OpLabel
  2605. OpBranch %83
  2606. %83 = OpLabel
  2607. %84 = OpSLessThan %12 %77 %11
  2608. OpBranch %85
  2609. %85 = OpLabel
  2610. %86 = OpIMul %6 %77 %78
  2611. %87 = OpIAdd %6 %75 %86
  2612. OpBranch %88
  2613. %88 = OpLabel
  2614. %89 = OpIAdd %6 %77 %13
  2615. %90 = OpISub %6 %78 %13
  2616. OpBranch %25
  2617. %25 = OpLabel
  2618. %30 = OpIAdd %6 %87 %90
  2619. OpReturnValue %30
  2620. OpFunctionEnd
  2621. )";
  2622. std::unique_ptr<IRContext> context =
  2623. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, multiple_phi_shader,
  2624. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2625. Module* module = context->module();
  2626. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  2627. << multiple_phi_shader << std::endl;
  2628. LoopUnroller loop_unroller;
  2629. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  2630. SinglePassRunAndCheck<LoopUnroller>(multiple_phi_shader, output, false);
  2631. }
  2632. /*
  2633. Generated from the following GLSL
  2634. #version 440 core
  2635. void main()
  2636. {
  2637. int j = 0;
  2638. for (int i = 0; i <= 2; ++i)
  2639. ++j;
  2640. for (int i = 1; i >= 0; --i)
  2641. ++j;
  2642. }
  2643. */
  2644. TEST_F(PassClassTest, FullyUnrollEqualToOperations) {
  2645. // With LocalMultiStoreElimPass
  2646. const std::string text = R"(
  2647. OpCapability Shader
  2648. %1 = OpExtInstImport "GLSL.std.450"
  2649. OpMemoryModel Logical GLSL450
  2650. OpEntryPoint Fragment %4 "main"
  2651. OpExecutionMode %4 OriginUpperLeft
  2652. OpSource GLSL 440
  2653. OpName %4 "main"
  2654. %2 = OpTypeVoid
  2655. %3 = OpTypeFunction %2
  2656. %6 = OpTypeInt 32 1
  2657. %7 = OpTypePointer Function %6
  2658. %9 = OpConstant %6 0
  2659. %17 = OpConstant %6 2
  2660. %18 = OpTypeBool
  2661. %21 = OpConstant %6 1
  2662. %4 = OpFunction %2 None %3
  2663. %5 = OpLabel
  2664. OpBranch %11
  2665. %11 = OpLabel
  2666. %37 = OpPhi %6 %9 %5 %22 %14
  2667. %38 = OpPhi %6 %9 %5 %24 %14
  2668. OpLoopMerge %13 %14 Unroll
  2669. OpBranch %15
  2670. %15 = OpLabel
  2671. %19 = OpSLessThanEqual %18 %38 %17
  2672. OpBranchConditional %19 %12 %13
  2673. %12 = OpLabel
  2674. %22 = OpIAdd %6 %37 %21
  2675. OpBranch %14
  2676. %14 = OpLabel
  2677. %24 = OpIAdd %6 %38 %21
  2678. OpBranch %11
  2679. %13 = OpLabel
  2680. OpBranch %26
  2681. %26 = OpLabel
  2682. %39 = OpPhi %6 %37 %13 %34 %29
  2683. %40 = OpPhi %6 %21 %13 %36 %29
  2684. OpLoopMerge %28 %29 Unroll
  2685. OpBranch %30
  2686. %30 = OpLabel
  2687. %32 = OpSGreaterThanEqual %18 %40 %9
  2688. OpBranchConditional %32 %27 %28
  2689. %27 = OpLabel
  2690. %34 = OpIAdd %6 %39 %21
  2691. OpBranch %29
  2692. %29 = OpLabel
  2693. %36 = OpISub %6 %40 %21
  2694. OpBranch %26
  2695. %28 = OpLabel
  2696. OpReturn
  2697. OpFunctionEnd
  2698. )";
  2699. const std::string output = R"(OpCapability Shader
  2700. %1 = OpExtInstImport "GLSL.std.450"
  2701. OpMemoryModel Logical GLSL450
  2702. OpEntryPoint Fragment %2 "main"
  2703. OpExecutionMode %2 OriginUpperLeft
  2704. OpSource GLSL 440
  2705. OpName %2 "main"
  2706. %3 = OpTypeVoid
  2707. %4 = OpTypeFunction %3
  2708. %5 = OpTypeInt 32 1
  2709. %6 = OpTypePointer Function %5
  2710. %7 = OpConstant %5 0
  2711. %8 = OpConstant %5 2
  2712. %9 = OpTypeBool
  2713. %10 = OpConstant %5 1
  2714. %2 = OpFunction %3 None %4
  2715. %11 = OpLabel
  2716. OpBranch %12
  2717. %12 = OpLabel
  2718. OpBranch %19
  2719. %19 = OpLabel
  2720. %20 = OpSLessThanEqual %9 %7 %8
  2721. OpBranch %21
  2722. %21 = OpLabel
  2723. %14 = OpIAdd %5 %7 %10
  2724. OpBranch %15
  2725. %15 = OpLabel
  2726. %17 = OpIAdd %5 %7 %10
  2727. OpBranch %41
  2728. %41 = OpLabel
  2729. OpBranch %44
  2730. %44 = OpLabel
  2731. %45 = OpSLessThanEqual %9 %17 %8
  2732. OpBranch %46
  2733. %46 = OpLabel
  2734. %47 = OpIAdd %5 %14 %10
  2735. OpBranch %48
  2736. %48 = OpLabel
  2737. %49 = OpIAdd %5 %17 %10
  2738. OpBranch %50
  2739. %50 = OpLabel
  2740. OpBranch %53
  2741. %53 = OpLabel
  2742. %54 = OpSLessThanEqual %9 %49 %8
  2743. OpBranch %55
  2744. %55 = OpLabel
  2745. %56 = OpIAdd %5 %47 %10
  2746. OpBranch %57
  2747. %57 = OpLabel
  2748. %58 = OpIAdd %5 %49 %10
  2749. OpBranch %18
  2750. %18 = OpLabel
  2751. OpBranch %22
  2752. %22 = OpLabel
  2753. OpBranch %29
  2754. %29 = OpLabel
  2755. %30 = OpSGreaterThanEqual %9 %10 %7
  2756. OpBranch %31
  2757. %31 = OpLabel
  2758. %24 = OpIAdd %5 %56 %10
  2759. OpBranch %25
  2760. %25 = OpLabel
  2761. %27 = OpISub %5 %10 %10
  2762. OpBranch %32
  2763. %32 = OpLabel
  2764. OpBranch %35
  2765. %35 = OpLabel
  2766. %36 = OpSGreaterThanEqual %9 %27 %7
  2767. OpBranch %37
  2768. %37 = OpLabel
  2769. %38 = OpIAdd %5 %24 %10
  2770. OpBranch %39
  2771. %39 = OpLabel
  2772. %40 = OpISub %5 %27 %10
  2773. OpBranch %28
  2774. %28 = OpLabel
  2775. OpReturn
  2776. OpFunctionEnd
  2777. )";
  2778. std::unique_ptr<IRContext> context =
  2779. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  2780. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2781. Module* module = context->module();
  2782. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  2783. << text << std::endl;
  2784. LoopUnroller loop_unroller;
  2785. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  2786. SinglePassRunAndCheck<LoopUnroller>(text, output, false);
  2787. }
  2788. // With LocalMultiStoreElimPass
  2789. const std::string condition_in_header = R"(
  2790. OpCapability Shader
  2791. OpMemoryModel Logical GLSL450
  2792. OpEntryPoint Fragment %main "main" %o
  2793. OpExecutionMode %main OriginUpperLeft
  2794. OpSource GLSL 430
  2795. OpDecorate %o Location 0
  2796. %void = OpTypeVoid
  2797. %6 = OpTypeFunction %void
  2798. %int = OpTypeInt 32 1
  2799. %int_n2 = OpConstant %int -2
  2800. %int_2 = OpConstant %int 2
  2801. %bool = OpTypeBool
  2802. %float = OpTypeFloat 32
  2803. %_ptr_Output_float = OpTypePointer Output %float
  2804. %o = OpVariable %_ptr_Output_float Output
  2805. %float_1 = OpConstant %float 1
  2806. %main = OpFunction %void None %6
  2807. %15 = OpLabel
  2808. OpBranch %16
  2809. %16 = OpLabel
  2810. %27 = OpPhi %int %int_n2 %15 %26 %18
  2811. %21 = OpSLessThanEqual %bool %27 %int_2
  2812. OpLoopMerge %17 %18 Unroll
  2813. OpBranchConditional %21 %22 %17
  2814. %22 = OpLabel
  2815. %23 = OpLoad %float %o
  2816. %24 = OpFAdd %float %23 %float_1
  2817. OpStore %o %24
  2818. OpBranch %18
  2819. %18 = OpLabel
  2820. %26 = OpIAdd %int %27 %int_2
  2821. OpBranch %16
  2822. %17 = OpLabel
  2823. OpReturn
  2824. OpFunctionEnd
  2825. )";
  2826. TEST_F(PassClassTest, FullyUnrollConditionIsInHeaderBlock) {
  2827. const std::string output = R"(OpCapability Shader
  2828. OpMemoryModel Logical GLSL450
  2829. OpEntryPoint Fragment %1 "main" %2
  2830. OpExecutionMode %1 OriginUpperLeft
  2831. OpSource GLSL 430
  2832. OpDecorate %2 Location 0
  2833. %3 = OpTypeVoid
  2834. %4 = OpTypeFunction %3
  2835. %5 = OpTypeInt 32 1
  2836. %6 = OpConstant %5 -2
  2837. %7 = OpConstant %5 2
  2838. %8 = OpTypeBool
  2839. %9 = OpTypeFloat 32
  2840. %10 = OpTypePointer Output %9
  2841. %2 = OpVariable %10 Output
  2842. %11 = OpConstant %9 1
  2843. %1 = OpFunction %3 None %4
  2844. %12 = OpLabel
  2845. OpBranch %13
  2846. %13 = OpLabel
  2847. %17 = OpSLessThanEqual %8 %6 %7
  2848. OpBranch %19
  2849. %19 = OpLabel
  2850. %20 = OpLoad %9 %2
  2851. %21 = OpFAdd %9 %20 %11
  2852. OpStore %2 %21
  2853. OpBranch %16
  2854. %16 = OpLabel
  2855. %15 = OpIAdd %5 %6 %7
  2856. OpBranch %22
  2857. %22 = OpLabel
  2858. %24 = OpSLessThanEqual %8 %15 %7
  2859. OpBranch %25
  2860. %25 = OpLabel
  2861. %26 = OpLoad %9 %2
  2862. %27 = OpFAdd %9 %26 %11
  2863. OpStore %2 %27
  2864. OpBranch %28
  2865. %28 = OpLabel
  2866. %29 = OpIAdd %5 %15 %7
  2867. OpBranch %30
  2868. %30 = OpLabel
  2869. %32 = OpSLessThanEqual %8 %29 %7
  2870. OpBranch %33
  2871. %33 = OpLabel
  2872. %34 = OpLoad %9 %2
  2873. %35 = OpFAdd %9 %34 %11
  2874. OpStore %2 %35
  2875. OpBranch %36
  2876. %36 = OpLabel
  2877. %37 = OpIAdd %5 %29 %7
  2878. OpBranch %18
  2879. %18 = OpLabel
  2880. OpReturn
  2881. OpFunctionEnd
  2882. )";
  2883. std::unique_ptr<IRContext> context =
  2884. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, condition_in_header,
  2885. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2886. Module* module = context->module();
  2887. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  2888. << condition_in_header << std::endl;
  2889. LoopUnroller loop_unroller;
  2890. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  2891. SinglePassRunAndCheck<LoopUnroller>(condition_in_header, output, false);
  2892. }
  2893. TEST_F(PassClassTest, PartiallyUnrollResidualConditionIsInHeaderBlock) {
  2894. const std::string output = R"(OpCapability Shader
  2895. OpMemoryModel Logical GLSL450
  2896. OpEntryPoint Fragment %1 "main" %2
  2897. OpExecutionMode %1 OriginUpperLeft
  2898. OpSource GLSL 430
  2899. OpDecorate %2 Location 0
  2900. %3 = OpTypeVoid
  2901. %4 = OpTypeFunction %3
  2902. %5 = OpTypeInt 32 1
  2903. %6 = OpConstant %5 -2
  2904. %7 = OpConstant %5 2
  2905. %8 = OpTypeBool
  2906. %9 = OpTypeFloat 32
  2907. %10 = OpTypePointer Output %9
  2908. %2 = OpVariable %10 Output
  2909. %11 = OpConstant %9 1
  2910. %40 = OpTypeInt 32 0
  2911. %41 = OpConstant %40 1
  2912. %1 = OpFunction %3 None %4
  2913. %12 = OpLabel
  2914. OpBranch %13
  2915. %13 = OpLabel
  2916. %14 = OpPhi %5 %6 %12 %15 %16
  2917. %17 = OpSLessThanEqual %8 %14 %41
  2918. OpLoopMerge %22 %16 Unroll
  2919. OpBranchConditional %17 %19 %22
  2920. %19 = OpLabel
  2921. %20 = OpLoad %9 %2
  2922. %21 = OpFAdd %9 %20 %11
  2923. OpStore %2 %21
  2924. OpBranch %16
  2925. %16 = OpLabel
  2926. %15 = OpIAdd %5 %14 %7
  2927. OpBranch %13
  2928. %22 = OpLabel
  2929. OpBranch %23
  2930. %23 = OpLabel
  2931. %24 = OpPhi %5 %14 %22 %39 %38
  2932. %25 = OpSLessThanEqual %8 %24 %7
  2933. OpLoopMerge %31 %38 DontUnroll
  2934. OpBranchConditional %25 %26 %31
  2935. %26 = OpLabel
  2936. %27 = OpLoad %9 %2
  2937. %28 = OpFAdd %9 %27 %11
  2938. OpStore %2 %28
  2939. OpBranch %29
  2940. %29 = OpLabel
  2941. %30 = OpIAdd %5 %24 %7
  2942. OpBranch %32
  2943. %32 = OpLabel
  2944. %34 = OpSLessThanEqual %8 %30 %7
  2945. OpBranch %35
  2946. %35 = OpLabel
  2947. %36 = OpLoad %9 %2
  2948. %37 = OpFAdd %9 %36 %11
  2949. OpStore %2 %37
  2950. OpBranch %38
  2951. %38 = OpLabel
  2952. %39 = OpIAdd %5 %30 %7
  2953. OpBranch %23
  2954. %31 = OpLabel
  2955. OpReturn
  2956. %18 = OpLabel
  2957. OpReturn
  2958. OpFunctionEnd
  2959. )";
  2960. std::unique_ptr<IRContext> context =
  2961. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, condition_in_header,
  2962. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2963. Module* module = context->module();
  2964. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  2965. << condition_in_header << std::endl;
  2966. LoopUnroller loop_unroller;
  2967. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  2968. SinglePassRunAndCheck<PartialUnrollerTestPass<2>>(condition_in_header, output,
  2969. false);
  2970. }
  2971. /*
  2972. Generated from following GLSL with latch block artificially inserted to be
  2973. separate from continue.
  2974. #version 430
  2975. void main(void) {
  2976. float x[10];
  2977. for (int i = 0; i < 10; ++i) {
  2978. x[i] = i;
  2979. }
  2980. }
  2981. */
  2982. TEST_F(PassClassTest, PartiallyUnrollLatchNotContinue) {
  2983. const std::string text = R"(OpCapability Shader
  2984. %1 = OpExtInstImport "GLSL.std.450"
  2985. OpMemoryModel Logical GLSL450
  2986. OpEntryPoint Fragment %2 "main"
  2987. OpExecutionMode %2 OriginUpperLeft
  2988. OpSource GLSL 430
  2989. OpName %2 "main"
  2990. OpName %3 "i"
  2991. OpName %4 "x"
  2992. %5 = OpTypeVoid
  2993. %6 = OpTypeFunction %5
  2994. %7 = OpTypeInt 32 1
  2995. %8 = OpTypePointer Function %7
  2996. %9 = OpConstant %7 0
  2997. %10 = OpConstant %7 10
  2998. %11 = OpTypeBool
  2999. %12 = OpTypeFloat 32
  3000. %13 = OpTypeInt 32 0
  3001. %14 = OpConstant %13 10
  3002. %15 = OpTypeArray %12 %14
  3003. %16 = OpTypePointer Function %15
  3004. %17 = OpTypePointer Function %12
  3005. %18 = OpConstant %7 1
  3006. %2 = OpFunction %5 None %6
  3007. %19 = OpLabel
  3008. %3 = OpVariable %8 Function
  3009. %4 = OpVariable %16 Function
  3010. OpStore %3 %9
  3011. OpBranch %20
  3012. %20 = OpLabel
  3013. %21 = OpPhi %7 %9 %19 %22 %30
  3014. OpLoopMerge %24 %23 Unroll
  3015. OpBranch %25
  3016. %25 = OpLabel
  3017. %26 = OpSLessThan %11 %21 %10
  3018. OpBranchConditional %26 %27 %24
  3019. %27 = OpLabel
  3020. %28 = OpConvertSToF %12 %21
  3021. %29 = OpAccessChain %17 %4 %21
  3022. OpStore %29 %28
  3023. OpBranch %23
  3024. %23 = OpLabel
  3025. %22 = OpIAdd %7 %21 %18
  3026. OpStore %3 %22
  3027. OpBranch %30
  3028. %30 = OpLabel
  3029. OpBranch %20
  3030. %24 = OpLabel
  3031. OpReturn
  3032. OpFunctionEnd
  3033. )";
  3034. const std::string expected = R"(OpCapability Shader
  3035. %1 = OpExtInstImport "GLSL.std.450"
  3036. OpMemoryModel Logical GLSL450
  3037. OpEntryPoint Fragment %2 "main"
  3038. OpExecutionMode %2 OriginUpperLeft
  3039. OpSource GLSL 430
  3040. OpName %2 "main"
  3041. OpName %3 "i"
  3042. OpName %4 "x"
  3043. %5 = OpTypeVoid
  3044. %6 = OpTypeFunction %5
  3045. %7 = OpTypeInt 32 1
  3046. %8 = OpTypePointer Function %7
  3047. %9 = OpConstant %7 0
  3048. %10 = OpConstant %7 10
  3049. %11 = OpTypeBool
  3050. %12 = OpTypeFloat 32
  3051. %13 = OpTypeInt 32 0
  3052. %14 = OpConstant %13 10
  3053. %15 = OpTypeArray %12 %14
  3054. %16 = OpTypePointer Function %15
  3055. %17 = OpTypePointer Function %12
  3056. %18 = OpConstant %7 1
  3057. %63 = OpConstant %13 1
  3058. %2 = OpFunction %5 None %6
  3059. %19 = OpLabel
  3060. %3 = OpVariable %8 Function
  3061. %4 = OpVariable %16 Function
  3062. OpStore %3 %9
  3063. OpBranch %20
  3064. %20 = OpLabel
  3065. %21 = OpPhi %7 %9 %19 %22 %23
  3066. OpLoopMerge %31 %25 Unroll
  3067. OpBranch %26
  3068. %26 = OpLabel
  3069. %27 = OpSLessThan %11 %21 %63
  3070. OpBranchConditional %27 %28 %31
  3071. %28 = OpLabel
  3072. %29 = OpConvertSToF %12 %21
  3073. %30 = OpAccessChain %17 %4 %21
  3074. OpStore %30 %29
  3075. OpBranch %25
  3076. %25 = OpLabel
  3077. %22 = OpIAdd %7 %21 %18
  3078. OpStore %3 %22
  3079. OpBranch %23
  3080. %23 = OpLabel
  3081. OpBranch %20
  3082. %31 = OpLabel
  3083. OpBranch %32
  3084. %32 = OpLabel
  3085. %33 = OpPhi %7 %21 %31 %61 %62
  3086. OpLoopMerge %42 %60 DontUnroll
  3087. OpBranch %34
  3088. %34 = OpLabel
  3089. %35 = OpSLessThan %11 %33 %10
  3090. OpBranchConditional %35 %36 %42
  3091. %36 = OpLabel
  3092. %37 = OpConvertSToF %12 %33
  3093. %38 = OpAccessChain %17 %4 %33
  3094. OpStore %38 %37
  3095. OpBranch %39
  3096. %39 = OpLabel
  3097. %40 = OpIAdd %7 %33 %18
  3098. OpStore %3 %40
  3099. OpBranch %41
  3100. %41 = OpLabel
  3101. OpBranch %43
  3102. %43 = OpLabel
  3103. OpBranch %45
  3104. %45 = OpLabel
  3105. %46 = OpSLessThan %11 %40 %10
  3106. OpBranch %47
  3107. %47 = OpLabel
  3108. %48 = OpConvertSToF %12 %40
  3109. %49 = OpAccessChain %17 %4 %40
  3110. OpStore %49 %48
  3111. OpBranch %50
  3112. %50 = OpLabel
  3113. %51 = OpIAdd %7 %40 %18
  3114. OpStore %3 %51
  3115. OpBranch %52
  3116. %52 = OpLabel
  3117. OpBranch %53
  3118. %53 = OpLabel
  3119. OpBranch %55
  3120. %55 = OpLabel
  3121. %56 = OpSLessThan %11 %51 %10
  3122. OpBranch %57
  3123. %57 = OpLabel
  3124. %58 = OpConvertSToF %12 %51
  3125. %59 = OpAccessChain %17 %4 %51
  3126. OpStore %59 %58
  3127. OpBranch %60
  3128. %60 = OpLabel
  3129. %61 = OpIAdd %7 %51 %18
  3130. OpStore %3 %61
  3131. OpBranch %62
  3132. %62 = OpLabel
  3133. OpBranch %32
  3134. %42 = OpLabel
  3135. OpReturn
  3136. %24 = OpLabel
  3137. OpReturn
  3138. OpFunctionEnd
  3139. )";
  3140. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  3141. SinglePassRunAndCheck<PartialUnrollerTestPass<3>>(text, expected, true);
  3142. // Make sure the latch block information is preserved and propagated correctly
  3143. // by the pass.
  3144. std::unique_ptr<IRContext> context =
  3145. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  3146. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3147. PartialUnrollerTestPass<3> unroller;
  3148. unroller.SetContextForTesting(context.get());
  3149. unroller.Process();
  3150. Module* module = context->module();
  3151. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  3152. << text << std::endl;
  3153. const Function* f = spvtest::GetFunction(module, 2);
  3154. LoopDescriptor ld{context.get(), f};
  3155. EXPECT_EQ(ld.NumLoops(), 2u);
  3156. Loop& loop_1 = ld.GetLoopByIndex(0u);
  3157. EXPECT_NE(loop_1.GetLatchBlock(), loop_1.GetContinueBlock());
  3158. Loop& loop_2 = ld.GetLoopByIndex(1u);
  3159. EXPECT_NE(loop_2.GetLatchBlock(), loop_2.GetContinueBlock());
  3160. }
  3161. // Test that a loop with a self-referencing OpPhi instruction is handled
  3162. // correctly.
  3163. TEST_F(PassClassTest, OpPhiSelfReference) {
  3164. const std::string text = R"(
  3165. ; Find the two adds from the unrolled loop
  3166. ; CHECK: OpIAdd
  3167. ; CHECK: OpIAdd
  3168. ; CHECK: OpIAdd %uint %uint_0 %uint_1
  3169. ; CHECK-NEXT: OpReturn
  3170. OpCapability Shader
  3171. %1 = OpExtInstImport "GLSL.std.450"
  3172. OpMemoryModel Logical GLSL450
  3173. OpEntryPoint GLCompute %2 "main"
  3174. OpExecutionMode %2 LocalSize 8 8 1
  3175. OpSource HLSL 600
  3176. %uint = OpTypeInt 32 0
  3177. %void = OpTypeVoid
  3178. %5 = OpTypeFunction %void
  3179. %uint_0 = OpConstant %uint 0
  3180. %uint_1 = OpConstant %uint 1
  3181. %bool = OpTypeBool
  3182. %true = OpConstantTrue %bool
  3183. %2 = OpFunction %void None %5
  3184. %10 = OpLabel
  3185. OpBranch %19
  3186. %19 = OpLabel
  3187. %20 = OpPhi %uint %uint_0 %10 %20 %21
  3188. %22 = OpPhi %uint %uint_0 %10 %23 %21
  3189. %24 = OpULessThanEqual %bool %22 %uint_1
  3190. OpLoopMerge %25 %21 Unroll
  3191. OpBranchConditional %24 %21 %25
  3192. %21 = OpLabel
  3193. %23 = OpIAdd %uint %22 %uint_1
  3194. OpBranch %19
  3195. %25 = OpLabel
  3196. %14 = OpIAdd %uint %20 %uint_1
  3197. OpReturn
  3198. OpFunctionEnd
  3199. )";
  3200. const bool kFullyUnroll = true;
  3201. const uint32_t kUnrollFactor = 0;
  3202. SinglePassRunAndMatch<opt::LoopUnroller>(text, true, kFullyUnroll,
  3203. kUnrollFactor);
  3204. }
  3205. // Test that a loop containing an unreachable merge block can still be unrolled
  3206. // correctly.
  3207. TEST_F(PassClassTest, UnreachableMerge) {
  3208. const std::string text = R"(
  3209. ; Identify the first iteration of the unrolled loop, and make sure it contains
  3210. ; the unreachable merge block.
  3211. ; The first SelectionMerge corresponds to the original loop merge.
  3212. ; The second is the branch in the loop.
  3213. ; CHECK: OpSelectionMerge {{%\w+}} None
  3214. ; CHECK: OpSelectionMerge [[unrch1:%\w+]] None
  3215. ; CHECK: [[unrch1]] = OpLabel
  3216. ; CHECK-NEXT: OpUnreachable
  3217. ; Identify the second iteration of the unrolled loop, and make sure it contains
  3218. ; the unreachable merge block.
  3219. ; The first SelectionMerge corresponds to the original loop merge
  3220. ; The second is the branch in the loop.
  3221. ; CHECK: OpSelectionMerge {{%\w+}} None
  3222. ; CHECK: OpSelectionMerge [[unrch2:%\w+]] None
  3223. ; CHECK: [[unrch2]] = OpLabel
  3224. ; CHECK-NEXT: OpUnreachable
  3225. OpCapability Shader
  3226. OpMemoryModel Logical GLSL450
  3227. OpEntryPoint GLCompute %main "main"
  3228. OpExecutionMode %main LocalSize 64 1 1
  3229. OpSource HLSL 600
  3230. OpName %main "main"
  3231. %uint = OpTypeInt 32 0
  3232. %uint_0 = OpConstant %uint 0
  3233. %uint_2 = OpConstant %uint 2
  3234. %uint_1 = OpConstant %uint 1
  3235. %bool = OpTypeBool
  3236. %void = OpTypeVoid
  3237. %18 = OpTypeFunction %void
  3238. %main = OpFunction %void None %18
  3239. %23 = OpLabel
  3240. OpBranch %24
  3241. %24 = OpLabel
  3242. %28 = OpPhi %uint %uint_0 %23 %29 %27
  3243. %30 = OpULessThan %bool %28 %uint_2
  3244. OpLoopMerge %31 %27 Unroll
  3245. OpBranchConditional %30 %32 %31
  3246. %32 = OpLabel
  3247. OpSelectionMerge %33 None
  3248. OpSwitch %uint_0 %34
  3249. %34 = OpLabel
  3250. %35 = OpUndef %bool
  3251. OpSelectionMerge %36 None
  3252. OpBranchConditional %35 %37 %38
  3253. %38 = OpLabel
  3254. OpBranch %33
  3255. %37 = OpLabel
  3256. OpBranch %33
  3257. %36 = OpLabel
  3258. OpUnreachable
  3259. %33 = OpLabel
  3260. OpBranch %27
  3261. %27 = OpLabel
  3262. %29 = OpIAdd %uint %28 %uint_1
  3263. OpBranch %24
  3264. %31 = OpLabel
  3265. OpReturn
  3266. OpFunctionEnd
  3267. )";
  3268. const bool kFullyUnroll = true;
  3269. const uint32_t kUnrollFactor = 0;
  3270. SinglePassRunAndMatch<opt::LoopUnroller>(text, true, kFullyUnroll,
  3271. kUnrollFactor);
  3272. }
  3273. TEST_F(PassClassTest, InitValueIsConstantNull) {
  3274. const std::string shader = R"(
  3275. OpCapability Shader
  3276. %1 = OpExtInstImport "GLSL.std.450"
  3277. OpMemoryModel Logical GLSL450
  3278. OpEntryPoint Fragment %4 "main"
  3279. OpExecutionMode %4 OriginUpperLeft
  3280. OpSource ESSL 320
  3281. %2 = OpTypeVoid
  3282. %3 = OpTypeFunction %2
  3283. %6 = OpTypeInt 32 1
  3284. %7 = OpConstantNull %6
  3285. %13 = OpConstant %6 1
  3286. %21 = OpConstant %6 1
  3287. %10 = OpTypeBool
  3288. %17 = OpTypePointer Function %6
  3289. %4 = OpFunction %2 None %3
  3290. %11 = OpLabel
  3291. OpBranch %5
  3292. %5 = OpLabel
  3293. %23 = OpPhi %6 %7 %11 %20 %15
  3294. OpLoopMerge %8 %15 Unroll
  3295. OpBranch %14
  3296. %14 = OpLabel
  3297. %9 = OpSLessThan %10 %23 %13
  3298. OpBranchConditional %9 %15 %8
  3299. %15 = OpLabel
  3300. %20 = OpIAdd %6 %23 %21
  3301. OpBranch %5
  3302. %8 = OpLabel
  3303. OpReturn
  3304. OpFunctionEnd
  3305. )";
  3306. const std::string output = R"(OpCapability Shader
  3307. %1 = OpExtInstImport "GLSL.std.450"
  3308. OpMemoryModel Logical GLSL450
  3309. OpEntryPoint Fragment %2 "main"
  3310. OpExecutionMode %2 OriginUpperLeft
  3311. OpSource ESSL 320
  3312. %3 = OpTypeVoid
  3313. %4 = OpTypeFunction %3
  3314. %5 = OpTypeInt 32 1
  3315. %6 = OpConstantNull %5
  3316. %7 = OpConstant %5 1
  3317. %8 = OpConstant %5 1
  3318. %9 = OpTypeBool
  3319. %10 = OpTypePointer Function %5
  3320. %2 = OpFunction %3 None %4
  3321. %11 = OpLabel
  3322. OpBranch %12
  3323. %12 = OpLabel
  3324. OpBranch %17
  3325. %17 = OpLabel
  3326. %18 = OpSLessThan %9 %6 %7
  3327. OpBranch %15
  3328. %15 = OpLabel
  3329. %14 = OpIAdd %5 %6 %8
  3330. OpBranch %16
  3331. %16 = OpLabel
  3332. OpReturn
  3333. OpFunctionEnd
  3334. )";
  3335. auto context = BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, shader,
  3336. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3337. Module* module = context->module();
  3338. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  3339. << shader << std::endl;
  3340. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  3341. SinglePassRunAndCheck<LoopUnroller>(shader, output, false);
  3342. }
  3343. TEST_F(PassClassTest, ConditionValueIsConstantNull) {
  3344. const std::string shader = R"(
  3345. OpCapability Shader
  3346. %1 = OpExtInstImport "GLSL.std.450"
  3347. OpMemoryModel Logical GLSL450
  3348. OpEntryPoint Fragment %4 "main"
  3349. OpExecutionMode %4 OriginUpperLeft
  3350. OpSource ESSL 320
  3351. %2 = OpTypeVoid
  3352. %3 = OpTypeFunction %2
  3353. %6 = OpTypeInt 32 1
  3354. %7 = OpConstantNull %6
  3355. %13 = OpConstant %6 1
  3356. %21 = OpConstant %6 1
  3357. %10 = OpTypeBool
  3358. %17 = OpTypePointer Function %6
  3359. %4 = OpFunction %2 None %3
  3360. %11 = OpLabel
  3361. OpBranch %5
  3362. %5 = OpLabel
  3363. %23 = OpPhi %6 %13 %11 %20 %15
  3364. OpLoopMerge %8 %15 Unroll
  3365. OpBranch %14
  3366. %14 = OpLabel
  3367. %9 = OpSGreaterThan %10 %23 %7
  3368. OpBranchConditional %9 %15 %8
  3369. %15 = OpLabel
  3370. %20 = OpISub %6 %23 %21
  3371. OpBranch %5
  3372. %8 = OpLabel
  3373. OpReturn
  3374. OpFunctionEnd
  3375. )";
  3376. const std::string output = R"(OpCapability Shader
  3377. %1 = OpExtInstImport "GLSL.std.450"
  3378. OpMemoryModel Logical GLSL450
  3379. OpEntryPoint Fragment %2 "main"
  3380. OpExecutionMode %2 OriginUpperLeft
  3381. OpSource ESSL 320
  3382. %3 = OpTypeVoid
  3383. %4 = OpTypeFunction %3
  3384. %5 = OpTypeInt 32 1
  3385. %6 = OpConstantNull %5
  3386. %7 = OpConstant %5 1
  3387. %8 = OpConstant %5 1
  3388. %9 = OpTypeBool
  3389. %10 = OpTypePointer Function %5
  3390. %2 = OpFunction %3 None %4
  3391. %11 = OpLabel
  3392. OpBranch %12
  3393. %12 = OpLabel
  3394. OpBranch %17
  3395. %17 = OpLabel
  3396. %18 = OpSGreaterThan %9 %7 %6
  3397. OpBranch %15
  3398. %15 = OpLabel
  3399. %14 = OpISub %5 %7 %8
  3400. OpBranch %16
  3401. %16 = OpLabel
  3402. OpReturn
  3403. OpFunctionEnd
  3404. )";
  3405. auto context = BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, shader,
  3406. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3407. Module* module = context->module();
  3408. EXPECT_NE(nullptr, module) << "Assembling failed for shader:\n"
  3409. << shader << std::endl;
  3410. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  3411. SinglePassRunAndCheck<LoopUnroller>(shader, output, false);
  3412. }
  3413. TEST_F(PassClassTest, UnrollWithPhiReferencesPhi) {
  3414. const std::string text = R"(
  3415. OpCapability Shader
  3416. %1 = OpExtInstImport "GLSL.std.450"
  3417. OpMemoryModel Logical GLSL450
  3418. OpEntryPoint Fragment %main "main" %color
  3419. OpExecutionMode %main OriginUpperLeft
  3420. OpSource HLSL 600
  3421. OpName %main "main"
  3422. OpName %color "color"
  3423. OpDecorate %color Location 0
  3424. %uint = OpTypeInt 32 0
  3425. %float = OpTypeFloat 32
  3426. %float_0 = OpConstant %float 0
  3427. %float_1 = OpConstant %float 1
  3428. %uint_1 = OpConstant %uint 1
  3429. %uint_3 = OpConstant %uint 3
  3430. %void = OpTypeVoid
  3431. %11 = OpTypeFunction %void
  3432. %bool = OpTypeBool
  3433. %v4float = OpTypeVector %float 4
  3434. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3435. %color = OpVariable %_ptr_Output_v4float Output
  3436. %main = OpFunction %void None %11
  3437. %15 = OpLabel
  3438. OpBranch %16
  3439. %16 = OpLabel
  3440. %17 = OpPhi %float %float_0 %15 %18 %19
  3441. %18 = OpPhi %float %float_1 %15 %20 %19
  3442. %21 = OpPhi %uint %uint_1 %15 %22 %19
  3443. %23 = OpULessThanEqual %bool %21 %uint_3
  3444. OpLoopMerge %24 %19 Unroll
  3445. OpBranchConditional %23 %25 %24
  3446. %25 = OpLabel
  3447. ; First loop iteration
  3448. ; CHECK: [[next_phi1_0:%\w+]] = OpFSub %float %float_1 %float_0
  3449. ; Second loop iteration
  3450. ; CHECK: [[next_phi1_1:%\w+]] = OpFSub %float [[next_phi1_0]] %float_1
  3451. ; Third loop iteration
  3452. ; CHECK: OpFSub %float [[next_phi1_1]] [[next_phi1_0]]
  3453. %20 = OpFSub %float %18 %17
  3454. OpBranch %19
  3455. %19 = OpLabel
  3456. %22 = OpIAdd %uint %21 %uint_1
  3457. OpBranch %16
  3458. %24 = OpLabel
  3459. OpReturn
  3460. OpFunctionEnd
  3461. )";
  3462. std::unique_ptr<IRContext> context =
  3463. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  3464. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3465. Module* module = context->module();
  3466. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  3467. << text << std::endl;
  3468. LoopUnroller loop_unroller;
  3469. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
  3470. SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
  3471. SinglePassRunAndMatch<LoopUnroller>(text, true);
  3472. }
  3473. TEST_F(PassClassTest, UnrollWithDoublePhiReferencesPhi) {
  3474. const std::string text = R"(
  3475. OpCapability Shader
  3476. %1 = OpExtInstImport "GLSL.std.450"
  3477. OpMemoryModel Logical GLSL450
  3478. OpEntryPoint Fragment %main "main" %color
  3479. OpExecutionMode %main OriginUpperLeft
  3480. OpSource HLSL 600
  3481. OpName %main "main"
  3482. OpName %color "color"
  3483. OpDecorate %color Location 0
  3484. %uint = OpTypeInt 32 0
  3485. %float = OpTypeFloat 32
  3486. %float_0 = OpConstant %float 0
  3487. %float_1 = OpConstant %float 1
  3488. %uint_1 = OpConstant %uint 1
  3489. %uint_3 = OpConstant %uint 3
  3490. %void = OpTypeVoid
  3491. %11 = OpTypeFunction %void
  3492. %bool = OpTypeBool
  3493. %v4float = OpTypeVector %float 4
  3494. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3495. %color = OpVariable %_ptr_Output_v4float Output
  3496. %main = OpFunction %void None %11
  3497. %15 = OpLabel
  3498. OpBranch %16
  3499. %16 = OpLabel
  3500. %17 = OpPhi %float %float_1 %15 %18 %19
  3501. %18 = OpPhi %float %float_0 %15 %20 %19
  3502. %20 = OpPhi %float %float_1 %15 %21 %19
  3503. %22 = OpPhi %uint %uint_1 %15 %23 %19
  3504. %24 = OpULessThanEqual %bool %22 %uint_3
  3505. OpLoopMerge %25 %19 Unroll
  3506. OpBranchConditional %24 %26 %25
  3507. %26 = OpLabel
  3508. ; First loop iteration
  3509. ; CHECK: [[next_phi1_0:%\w+]] = OpFSub %float %float_1 %float_0
  3510. ; CHECK: OpFMul %float %float_1
  3511. ; Second loop iteration
  3512. ; CHECK: [[next_phi1_1:%\w+]] = OpFSub %float [[next_phi1_0]] %float_1
  3513. ; CHECK: OpFMul %float %float_0
  3514. ; Third loop iteration
  3515. ; CHECK: OpFSub %float [[next_phi1_1]] [[next_phi1_0]]
  3516. ; CHECK: OpFMul %float %float_1
  3517. %21 = OpFSub %float %20 %18
  3518. %27 = OpFMul %float %17 %21
  3519. OpBranch %19
  3520. %19 = OpLabel
  3521. %23 = OpIAdd %uint %22 %uint_1
  3522. OpBranch %16
  3523. %25 = OpLabel
  3524. OpReturn
  3525. OpFunctionEnd
  3526. )";
  3527. std::unique_ptr<IRContext> context =
  3528. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  3529. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3530. Module* module = context->module();
  3531. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  3532. << text << std::endl;
  3533. LoopUnroller loop_unroller;
  3534. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
  3535. SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
  3536. SinglePassRunAndMatch<LoopUnroller>(text, true);
  3537. }
  3538. TEST_F(PassClassTest, PartialUnrollWithPhiReferencesPhi) {
  3539. // With LocalMultiStoreElimPass
  3540. const std::string text = R"(
  3541. OpCapability Shader
  3542. %1 = OpExtInstImport "GLSL.std.450"
  3543. OpMemoryModel Logical GLSL450
  3544. OpEntryPoint Fragment %main "main" %color
  3545. OpExecutionMode %main OriginUpperLeft
  3546. OpSource HLSL 600
  3547. OpName %main "main"
  3548. OpName %color "color"
  3549. OpDecorate %color Location 0
  3550. %uint = OpTypeInt 32 0
  3551. %float = OpTypeFloat 32
  3552. %float_0 = OpConstant %float 0
  3553. %float_1 = OpConstant %float 1
  3554. %uint_1 = OpConstant %uint 1
  3555. %uint_3 = OpConstant %uint 3
  3556. %void = OpTypeVoid
  3557. %11 = OpTypeFunction %void
  3558. %bool = OpTypeBool
  3559. %v4float = OpTypeVector %float 4
  3560. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3561. %color = OpVariable %_ptr_Output_v4float Output
  3562. %main = OpFunction %void None %11
  3563. %15 = OpLabel
  3564. OpBranch %16
  3565. %16 = OpLabel
  3566. %17 = OpPhi %float %float_0 %15 %18 %19
  3567. %18 = OpPhi %float %float_1 %15 %20 %19
  3568. %21 = OpPhi %uint %uint_1 %15 %22 %19
  3569. %23 = OpULessThanEqual %bool %21 %uint_3
  3570. OpLoopMerge %24 %19 Unroll
  3571. OpBranchConditional %23 %25 %24
  3572. %25 = OpLabel
  3573. ; CHECK: [[phi0_0:%\w+]] = OpPhi {{%\w+}} {{%\w+}} {{%\w+}} [[phi1_0:%\w+]]
  3574. ; CHECK: [[phi1_0]] = OpPhi {{%\w+}} {{%\w+}} {{%\w+}} [[sub:%\w+]]
  3575. ; CHECK: [[sub]] = OpFSub {{%\w+}} [[phi1_0]] [[phi0_0]]
  3576. ; CHECK: [[phi0_1:%\w+]] = OpPhi {{%\w+}} [[phi0_0]]
  3577. ; CHECK: [[phi1_1:%\w+]] = OpPhi {{%\w+}} [[phi1_0]]
  3578. ; CHECK: [[sub:%\w+]] = OpFSub {{%\w+}} [[phi1_1]] [[phi0_1]]
  3579. ; CHECK: OpFSub {{%\w+}} [[sub]] [[phi1_1]]
  3580. %20 = OpFSub %float %18 %17
  3581. OpBranch %19
  3582. %19 = OpLabel
  3583. %22 = OpIAdd %uint %21 %uint_1
  3584. OpBranch %16
  3585. %24 = OpLabel
  3586. OpReturn
  3587. OpFunctionEnd
  3588. )";
  3589. std::unique_ptr<IRContext> context =
  3590. BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, text,
  3591. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  3592. Module* module = context->module();
  3593. EXPECT_NE(nullptr, module) << "Assembling failed for ushader:\n"
  3594. << text << std::endl;
  3595. LoopUnroller loop_unroller;
  3596. SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
  3597. SinglePassRunAndMatch<PartialUnrollerTestPass<2>>(text, true);
  3598. }
  3599. TEST_F(PassClassTest, DontUnrollInfiteLoop) {
  3600. // This is an infinite loop that because the step is 0. We want to make sure
  3601. // the unroller does not try to unroll it.
  3602. const std::string text = R"(OpCapability Shader
  3603. %1 = OpExtInstImport "GLSL.std.450"
  3604. OpMemoryModel Logical GLSL450
  3605. OpEntryPoint Fragment %2 "main"
  3606. OpExecutionMode %2 OriginUpperLeft
  3607. %void = OpTypeVoid
  3608. %4 = OpTypeFunction %void
  3609. %int = OpTypeInt 32 1
  3610. %int_0 = OpConstant %int 0
  3611. %int_50 = OpConstant %int 50
  3612. %bool = OpTypeBool
  3613. %int_0_0 = OpConstant %int 0
  3614. %2 = OpFunction %void None %4
  3615. %10 = OpLabel
  3616. OpBranch %11
  3617. %11 = OpLabel
  3618. %12 = OpPhi %int %int_0 %10 %13 %14
  3619. %15 = OpSLessThan %bool %12 %int_50
  3620. OpLoopMerge %16 %14 Unroll
  3621. OpBranchConditional %15 %14 %16
  3622. %14 = OpLabel
  3623. %13 = OpIAdd %int %12 %int_0_0
  3624. OpBranch %11
  3625. %16 = OpLabel
  3626. OpReturn
  3627. OpFunctionEnd
  3628. )";
  3629. SinglePassRunAndCheck<LoopUnroller>(text, text, false);
  3630. }
  3631. } // namespace
  3632. } // namespace opt
  3633. } // namespace spvtools