unroll_simple.cpp 88 KB

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