inline_test.cpp 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138
  1. // Copyright (c) 2017 Valve Corporation
  2. // Copyright (c) 2017 LunarG Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include <memory>
  16. #include <string>
  17. #include <vector>
  18. #include "test/opt/pass_fixture.h"
  19. #include "test/opt/pass_utils.h"
  20. namespace spvtools {
  21. namespace opt {
  22. namespace {
  23. using InlineTest = PassTest<::testing::Test>;
  24. TEST_F(InlineTest, Simple) {
  25. // #version 140
  26. //
  27. // in vec4 BaseColor;
  28. //
  29. // float foo(vec4 bar)
  30. // {
  31. // return bar.x + bar.y;
  32. // }
  33. //
  34. // void main()
  35. // {
  36. // vec4 color = vec4(foo(BaseColor));
  37. // gl_FragColor = color;
  38. // }
  39. const std::vector<const char*> predefs = {
  40. // clang-format off
  41. "OpCapability Shader",
  42. "%1 = OpExtInstImport \"GLSL.std.450\"",
  43. "OpMemoryModel Logical GLSL450",
  44. "OpEntryPoint Fragment %main \"main\" %BaseColor %gl_FragColor",
  45. "OpExecutionMode %main OriginUpperLeft",
  46. "OpSource GLSL 140",
  47. "OpName %main \"main\"",
  48. "OpName %foo_vf4_ \"foo(vf4;\"",
  49. "OpName %bar \"bar\"",
  50. "OpName %color \"color\"",
  51. "OpName %BaseColor \"BaseColor\"",
  52. "OpName %param \"param\"",
  53. "OpName %gl_FragColor \"gl_FragColor\"",
  54. "%void = OpTypeVoid",
  55. "%10 = OpTypeFunction %void",
  56. "%float = OpTypeFloat 32",
  57. "%v4float = OpTypeVector %float 4",
  58. "%_ptr_Function_v4float = OpTypePointer Function %v4float",
  59. "%14 = OpTypeFunction %float %_ptr_Function_v4float",
  60. "%uint = OpTypeInt 32 0",
  61. "%uint_0 = OpConstant %uint 0",
  62. "%_ptr_Function_float = OpTypePointer Function %float",
  63. "%uint_1 = OpConstant %uint 1",
  64. "%_ptr_Input_v4float = OpTypePointer Input %v4float",
  65. "%BaseColor = OpVariable %_ptr_Input_v4float Input",
  66. "%_ptr_Output_v4float = OpTypePointer Output %v4float",
  67. "%gl_FragColor = OpVariable %_ptr_Output_v4float Output",
  68. // clang-format on
  69. };
  70. const std::vector<const char*> nonEntryFuncs = {
  71. // clang-format off
  72. "%foo_vf4_ = OpFunction %float None %14",
  73. "%bar = OpFunctionParameter %_ptr_Function_v4float",
  74. "%26 = OpLabel",
  75. "%27 = OpAccessChain %_ptr_Function_float %bar %uint_0",
  76. "%28 = OpLoad %float %27",
  77. "%29 = OpAccessChain %_ptr_Function_float %bar %uint_1",
  78. "%30 = OpLoad %float %29",
  79. "%31 = OpFAdd %float %28 %30",
  80. "OpReturnValue %31",
  81. "OpFunctionEnd",
  82. // clang-format on
  83. };
  84. const std::vector<const char*> before = {
  85. // clang-format off
  86. "%main = OpFunction %void None %10",
  87. "%21 = OpLabel",
  88. "%color = OpVariable %_ptr_Function_v4float Function",
  89. "%param = OpVariable %_ptr_Function_v4float Function",
  90. "%22 = OpLoad %v4float %BaseColor",
  91. "OpStore %param %22",
  92. "%23 = OpFunctionCall %float %foo_vf4_ %param",
  93. "%24 = OpCompositeConstruct %v4float %23 %23 %23 %23",
  94. "OpStore %color %24",
  95. "%25 = OpLoad %v4float %color",
  96. "OpStore %gl_FragColor %25",
  97. "OpReturn",
  98. "OpFunctionEnd",
  99. // clang-format on
  100. };
  101. const std::vector<const char*> after = {
  102. // clang-format off
  103. "%main = OpFunction %void None %10",
  104. "%21 = OpLabel",
  105. "%32 = OpVariable %_ptr_Function_float Function",
  106. "%color = OpVariable %_ptr_Function_v4float Function",
  107. "%param = OpVariable %_ptr_Function_v4float Function",
  108. "%22 = OpLoad %v4float %BaseColor",
  109. "OpStore %param %22",
  110. "%33 = OpAccessChain %_ptr_Function_float %param %uint_0",
  111. "%34 = OpLoad %float %33",
  112. "%35 = OpAccessChain %_ptr_Function_float %param %uint_1",
  113. "%36 = OpLoad %float %35",
  114. "%37 = OpFAdd %float %34 %36",
  115. "OpStore %32 %37",
  116. "%23 = OpLoad %float %32",
  117. "%24 = OpCompositeConstruct %v4float %23 %23 %23 %23",
  118. "OpStore %color %24",
  119. "%25 = OpLoad %v4float %color",
  120. "OpStore %gl_FragColor %25",
  121. "OpReturn",
  122. "OpFunctionEnd",
  123. // clang-format on
  124. };
  125. SinglePassRunAndCheck<InlineExhaustivePass>(
  126. JoinAllInsts(Concat(Concat(predefs, before), nonEntryFuncs)),
  127. JoinAllInsts(Concat(Concat(predefs, after), nonEntryFuncs)),
  128. /* skip_nop = */ false, /* do_validate = */ true);
  129. }
  130. TEST_F(InlineTest, Nested) {
  131. // #version 140
  132. //
  133. // in vec4 BaseColor;
  134. //
  135. // float foo2(float f, float f2)
  136. // {
  137. // return f * f2;
  138. // }
  139. //
  140. // float foo(vec4 bar)
  141. // {
  142. // return foo2(bar.x + bar.y, bar.z);
  143. // }
  144. //
  145. // void main()
  146. // {
  147. // vec4 color = vec4(foo(BaseColor));
  148. // gl_FragColor = color;
  149. // }
  150. const std::vector<const char*> predefs = {
  151. // clang-format off
  152. "OpCapability Shader",
  153. "%1 = OpExtInstImport \"GLSL.std.450\"",
  154. "OpMemoryModel Logical GLSL450",
  155. "OpEntryPoint Fragment %main \"main\" %BaseColor %gl_FragColor",
  156. "OpExecutionMode %main OriginUpperLeft",
  157. "OpSource GLSL 140",
  158. "OpName %main \"main\"",
  159. "OpName %foo2_f1_f1_ \"foo2(f1;f1;\"",
  160. "OpName %f \"f\"",
  161. "OpName %f2 \"f2\"",
  162. "OpName %foo_vf4_ \"foo(vf4;\"",
  163. "OpName %bar \"bar\"",
  164. "OpName %param \"param\"",
  165. "OpName %param_0 \"param\"",
  166. "OpName %color \"color\"",
  167. "OpName %BaseColor \"BaseColor\"",
  168. "OpName %param_1 \"param\"",
  169. "OpName %gl_FragColor \"gl_FragColor\"",
  170. "%void = OpTypeVoid",
  171. "%15 = OpTypeFunction %void",
  172. "%float = OpTypeFloat 32",
  173. "%_ptr_Function_float = OpTypePointer Function %float",
  174. "%18 = OpTypeFunction %float %_ptr_Function_float %_ptr_Function_float",
  175. "%v4float = OpTypeVector %float 4",
  176. "%_ptr_Function_v4float = OpTypePointer Function %v4float",
  177. "%21 = OpTypeFunction %float %_ptr_Function_v4float",
  178. "%uint = OpTypeInt 32 0",
  179. "%uint_0 = OpConstant %uint 0",
  180. "%uint_1 = OpConstant %uint 1",
  181. "%uint_2 = OpConstant %uint 2",
  182. "%_ptr_Input_v4float = OpTypePointer Input %v4float",
  183. "%BaseColor = OpVariable %_ptr_Input_v4float Input",
  184. "%_ptr_Output_v4float = OpTypePointer Output %v4float",
  185. "%gl_FragColor = OpVariable %_ptr_Output_v4float Output",
  186. // clang-format on
  187. };
  188. const std::vector<const char*> nonEntryFuncs = {
  189. // clang-format off
  190. "%foo2_f1_f1_ = OpFunction %float None %18",
  191. "%f = OpFunctionParameter %_ptr_Function_float",
  192. "%f2 = OpFunctionParameter %_ptr_Function_float",
  193. "%33 = OpLabel",
  194. "%34 = OpLoad %float %f",
  195. "%35 = OpLoad %float %f2",
  196. "%36 = OpFMul %float %34 %35",
  197. "OpReturnValue %36",
  198. "OpFunctionEnd",
  199. "%foo_vf4_ = OpFunction %float None %21",
  200. "%bar = OpFunctionParameter %_ptr_Function_v4float",
  201. "%37 = OpLabel",
  202. "%param = OpVariable %_ptr_Function_float Function",
  203. "%param_0 = OpVariable %_ptr_Function_float Function",
  204. "%38 = OpAccessChain %_ptr_Function_float %bar %uint_0",
  205. "%39 = OpLoad %float %38",
  206. "%40 = OpAccessChain %_ptr_Function_float %bar %uint_1",
  207. "%41 = OpLoad %float %40",
  208. "%42 = OpFAdd %float %39 %41",
  209. "OpStore %param %42",
  210. "%43 = OpAccessChain %_ptr_Function_float %bar %uint_2",
  211. "%44 = OpLoad %float %43",
  212. "OpStore %param_0 %44",
  213. "%45 = OpFunctionCall %float %foo2_f1_f1_ %param %param_0",
  214. "OpReturnValue %45",
  215. "OpFunctionEnd",
  216. // clang-format on
  217. };
  218. const std::vector<const char*> before = {
  219. // clang-format off
  220. "%main = OpFunction %void None %15",
  221. "%28 = OpLabel",
  222. "%color = OpVariable %_ptr_Function_v4float Function",
  223. "%param_1 = OpVariable %_ptr_Function_v4float Function",
  224. "%29 = OpLoad %v4float %BaseColor",
  225. "OpStore %param_1 %29",
  226. "%30 = OpFunctionCall %float %foo_vf4_ %param_1",
  227. "%31 = OpCompositeConstruct %v4float %30 %30 %30 %30",
  228. "OpStore %color %31",
  229. "%32 = OpLoad %v4float %color",
  230. "OpStore %gl_FragColor %32",
  231. "OpReturn",
  232. "OpFunctionEnd",
  233. // clang-format on
  234. };
  235. const std::vector<const char*> after = {
  236. // clang-format off
  237. "%main = OpFunction %void None %15",
  238. "%28 = OpLabel",
  239. "%57 = OpVariable %_ptr_Function_float Function",
  240. "%46 = OpVariable %_ptr_Function_float Function",
  241. "%47 = OpVariable %_ptr_Function_float Function",
  242. "%48 = OpVariable %_ptr_Function_float Function",
  243. "%color = OpVariable %_ptr_Function_v4float Function",
  244. "%param_1 = OpVariable %_ptr_Function_v4float Function",
  245. "%29 = OpLoad %v4float %BaseColor",
  246. "OpStore %param_1 %29",
  247. "%49 = OpAccessChain %_ptr_Function_float %param_1 %uint_0",
  248. "%50 = OpLoad %float %49",
  249. "%51 = OpAccessChain %_ptr_Function_float %param_1 %uint_1",
  250. "%52 = OpLoad %float %51",
  251. "%53 = OpFAdd %float %50 %52",
  252. "OpStore %46 %53",
  253. "%54 = OpAccessChain %_ptr_Function_float %param_1 %uint_2",
  254. "%55 = OpLoad %float %54",
  255. "OpStore %47 %55",
  256. "%58 = OpLoad %float %46",
  257. "%59 = OpLoad %float %47",
  258. "%60 = OpFMul %float %58 %59",
  259. "OpStore %57 %60",
  260. "%56 = OpLoad %float %57",
  261. "OpStore %48 %56",
  262. "%30 = OpLoad %float %48",
  263. "%31 = OpCompositeConstruct %v4float %30 %30 %30 %30",
  264. "OpStore %color %31",
  265. "%32 = OpLoad %v4float %color",
  266. "OpStore %gl_FragColor %32",
  267. "OpReturn",
  268. "OpFunctionEnd",
  269. // clang-format on
  270. };
  271. SinglePassRunAndCheck<InlineExhaustivePass>(
  272. JoinAllInsts(Concat(Concat(predefs, before), nonEntryFuncs)),
  273. JoinAllInsts(Concat(Concat(predefs, after), nonEntryFuncs)),
  274. /* skip_nop = */ false, /* do_validate = */ true);
  275. }
  276. TEST_F(InlineTest, InOutParameter) {
  277. // #version 400
  278. //
  279. // in vec4 Basecolor;
  280. //
  281. // void foo(inout vec4 bar)
  282. // {
  283. // bar.z = bar.x + bar.y;
  284. // }
  285. //
  286. // void main()
  287. // {
  288. // vec4 b = Basecolor;
  289. // foo(b);
  290. // vec4 color = vec4(b.z);
  291. // gl_FragColor = color;
  292. // }
  293. const std::vector<const char*> predefs = {
  294. // clang-format off
  295. "OpCapability Shader",
  296. "%1 = OpExtInstImport \"GLSL.std.450\"",
  297. "OpMemoryModel Logical GLSL450",
  298. "OpEntryPoint Fragment %main \"main\" %Basecolor %gl_FragColor",
  299. "OpExecutionMode %main OriginUpperLeft",
  300. "OpSource GLSL 400",
  301. "OpName %main \"main\"",
  302. "OpName %foo_vf4_ \"foo(vf4;\"",
  303. "OpName %bar \"bar\"",
  304. "OpName %b \"b\"",
  305. "OpName %Basecolor \"Basecolor\"",
  306. "OpName %param \"param\"",
  307. "OpName %color \"color\"",
  308. "OpName %gl_FragColor \"gl_FragColor\"",
  309. "%void = OpTypeVoid",
  310. "%11 = OpTypeFunction %void",
  311. "%float = OpTypeFloat 32",
  312. "%v4float = OpTypeVector %float 4",
  313. "%_ptr_Function_v4float = OpTypePointer Function %v4float",
  314. "%15 = OpTypeFunction %void %_ptr_Function_v4float",
  315. "%uint = OpTypeInt 32 0",
  316. "%uint_0 = OpConstant %uint 0",
  317. "%_ptr_Function_float = OpTypePointer Function %float",
  318. "%uint_1 = OpConstant %uint 1",
  319. "%uint_2 = OpConstant %uint 2",
  320. "%_ptr_Input_v4float = OpTypePointer Input %v4float",
  321. "%Basecolor = OpVariable %_ptr_Input_v4float Input",
  322. "%_ptr_Output_v4float = OpTypePointer Output %v4float",
  323. "%gl_FragColor = OpVariable %_ptr_Output_v4float Output",
  324. // clang-format on
  325. };
  326. const std::vector<const char*> nonEntryFuncs = {
  327. // clang-format off
  328. "%foo_vf4_ = OpFunction %void None %15",
  329. "%bar = OpFunctionParameter %_ptr_Function_v4float",
  330. "%32 = OpLabel",
  331. "%33 = OpAccessChain %_ptr_Function_float %bar %uint_0",
  332. "%34 = OpLoad %float %33",
  333. "%35 = OpAccessChain %_ptr_Function_float %bar %uint_1",
  334. "%36 = OpLoad %float %35",
  335. "%37 = OpFAdd %float %34 %36",
  336. "%38 = OpAccessChain %_ptr_Function_float %bar %uint_2",
  337. "OpStore %38 %37",
  338. "OpReturn",
  339. "OpFunctionEnd",
  340. // clang-format on
  341. };
  342. const std::vector<const char*> before = {
  343. // clang-format off
  344. "%main = OpFunction %void None %11",
  345. "%23 = OpLabel",
  346. "%b = OpVariable %_ptr_Function_v4float Function",
  347. "%param = OpVariable %_ptr_Function_v4float Function",
  348. "%color = OpVariable %_ptr_Function_v4float Function",
  349. "%24 = OpLoad %v4float %Basecolor",
  350. "OpStore %b %24",
  351. "%25 = OpLoad %v4float %b",
  352. "OpStore %param %25",
  353. "%26 = OpFunctionCall %void %foo_vf4_ %param",
  354. "%27 = OpLoad %v4float %param",
  355. "OpStore %b %27",
  356. "%28 = OpAccessChain %_ptr_Function_float %b %uint_2",
  357. "%29 = OpLoad %float %28",
  358. "%30 = OpCompositeConstruct %v4float %29 %29 %29 %29",
  359. "OpStore %color %30",
  360. "%31 = OpLoad %v4float %color",
  361. "OpStore %gl_FragColor %31",
  362. "OpReturn",
  363. "OpFunctionEnd",
  364. // clang-format on
  365. };
  366. const std::vector<const char*> after = {
  367. // clang-format off
  368. "%main = OpFunction %void None %11",
  369. "%23 = OpLabel",
  370. "%b = OpVariable %_ptr_Function_v4float Function",
  371. "%param = OpVariable %_ptr_Function_v4float Function",
  372. "%color = OpVariable %_ptr_Function_v4float Function",
  373. "%24 = OpLoad %v4float %Basecolor",
  374. "OpStore %b %24",
  375. "%25 = OpLoad %v4float %b",
  376. "OpStore %param %25",
  377. "%39 = OpAccessChain %_ptr_Function_float %param %uint_0",
  378. "%40 = OpLoad %float %39",
  379. "%41 = OpAccessChain %_ptr_Function_float %param %uint_1",
  380. "%42 = OpLoad %float %41",
  381. "%43 = OpFAdd %float %40 %42",
  382. "%44 = OpAccessChain %_ptr_Function_float %param %uint_2",
  383. "OpStore %44 %43",
  384. "%27 = OpLoad %v4float %param",
  385. "OpStore %b %27",
  386. "%28 = OpAccessChain %_ptr_Function_float %b %uint_2",
  387. "%29 = OpLoad %float %28",
  388. "%30 = OpCompositeConstruct %v4float %29 %29 %29 %29",
  389. "OpStore %color %30",
  390. "%31 = OpLoad %v4float %color",
  391. "OpStore %gl_FragColor %31",
  392. "OpReturn",
  393. "OpFunctionEnd",
  394. // clang-format on
  395. };
  396. SinglePassRunAndCheck<InlineExhaustivePass>(
  397. JoinAllInsts(Concat(Concat(predefs, before), nonEntryFuncs)),
  398. JoinAllInsts(Concat(Concat(predefs, after), nonEntryFuncs)),
  399. /* skip_nop = */ false, /* do_validate = */ true);
  400. }
  401. TEST_F(InlineTest, BranchInCallee) {
  402. // #version 140
  403. //
  404. // in vec4 BaseColor;
  405. //
  406. // float foo(vec4 bar)
  407. // {
  408. // float r = bar.x;
  409. // if (r < 0.0)
  410. // r = -r;
  411. // return r;
  412. // }
  413. //
  414. // void main()
  415. // {
  416. // vec4 color = vec4(foo(BaseColor));
  417. //
  418. // gl_FragColor = color;
  419. // }
  420. const std::vector<const char*> predefs = {
  421. // clang-format off
  422. "OpCapability Shader",
  423. "%1 = OpExtInstImport \"GLSL.std.450\"",
  424. "OpMemoryModel Logical GLSL450",
  425. "OpEntryPoint Fragment %main \"main\" %BaseColor %gl_FragColor",
  426. "OpExecutionMode %main OriginUpperLeft",
  427. "OpSource GLSL 140",
  428. "OpName %main \"main\"",
  429. "OpName %foo_vf4_ \"foo(vf4;\"",
  430. "OpName %bar \"bar\"",
  431. "OpName %r \"r\"",
  432. "OpName %color \"color\"",
  433. "OpName %BaseColor \"BaseColor\"",
  434. "OpName %param \"param\"",
  435. "OpName %gl_FragColor \"gl_FragColor\"",
  436. "%void = OpTypeVoid",
  437. "%11 = OpTypeFunction %void",
  438. "%float = OpTypeFloat 32",
  439. "%v4float = OpTypeVector %float 4",
  440. "%_ptr_Function_v4float = OpTypePointer Function %v4float",
  441. "%15 = OpTypeFunction %float %_ptr_Function_v4float",
  442. "%_ptr_Function_float = OpTypePointer Function %float",
  443. "%uint = OpTypeInt 32 0",
  444. "%uint_0 = OpConstant %uint 0",
  445. "%float_0 = OpConstant %float 0",
  446. "%bool = OpTypeBool",
  447. "%_ptr_Input_v4float = OpTypePointer Input %v4float",
  448. "%BaseColor = OpVariable %_ptr_Input_v4float Input",
  449. "%_ptr_Output_v4float = OpTypePointer Output %v4float",
  450. "%gl_FragColor = OpVariable %_ptr_Output_v4float Output",
  451. // clang-format on
  452. };
  453. const std::vector<const char*> nonEntryFuncs = {
  454. // clang-format off
  455. "%foo_vf4_ = OpFunction %float None %15",
  456. "%bar = OpFunctionParameter %_ptr_Function_v4float",
  457. "%28 = OpLabel",
  458. "%r = OpVariable %_ptr_Function_float Function",
  459. "%29 = OpAccessChain %_ptr_Function_float %bar %uint_0",
  460. "%30 = OpLoad %float %29",
  461. "OpStore %r %30",
  462. "%31 = OpLoad %float %r",
  463. "%32 = OpFOrdLessThan %bool %31 %float_0",
  464. "OpSelectionMerge %33 None",
  465. "OpBranchConditional %32 %34 %33",
  466. "%34 = OpLabel",
  467. "%35 = OpLoad %float %r",
  468. "%36 = OpFNegate %float %35",
  469. "OpStore %r %36",
  470. "OpBranch %33",
  471. "%33 = OpLabel",
  472. "%37 = OpLoad %float %r",
  473. "OpReturnValue %37",
  474. "OpFunctionEnd",
  475. // clang-format on
  476. };
  477. const std::vector<const char*> before = {
  478. // clang-format off
  479. "%main = OpFunction %void None %11",
  480. "%23 = OpLabel",
  481. "%color = OpVariable %_ptr_Function_v4float Function",
  482. "%param = OpVariable %_ptr_Function_v4float Function",
  483. "%24 = OpLoad %v4float %BaseColor",
  484. "OpStore %param %24",
  485. "%25 = OpFunctionCall %float %foo_vf4_ %param",
  486. "%26 = OpCompositeConstruct %v4float %25 %25 %25 %25",
  487. "OpStore %color %26",
  488. "%27 = OpLoad %v4float %color",
  489. "OpStore %gl_FragColor %27",
  490. "OpReturn",
  491. "OpFunctionEnd",
  492. // clang-format on
  493. };
  494. const std::vector<const char*> after = {
  495. // clang-format off
  496. "%main = OpFunction %void None %11",
  497. "%23 = OpLabel",
  498. "%38 = OpVariable %_ptr_Function_float Function",
  499. "%39 = OpVariable %_ptr_Function_float Function",
  500. "%color = OpVariable %_ptr_Function_v4float Function",
  501. "%param = OpVariable %_ptr_Function_v4float Function",
  502. "%24 = OpLoad %v4float %BaseColor",
  503. "OpStore %param %24",
  504. "%40 = OpAccessChain %_ptr_Function_float %param %uint_0",
  505. "%41 = OpLoad %float %40",
  506. "OpStore %38 %41",
  507. "%42 = OpLoad %float %38",
  508. "%43 = OpFOrdLessThan %bool %42 %float_0",
  509. "OpSelectionMerge %44 None",
  510. "OpBranchConditional %43 %45 %44",
  511. "%45 = OpLabel",
  512. "%46 = OpLoad %float %38",
  513. "%47 = OpFNegate %float %46",
  514. "OpStore %38 %47",
  515. "OpBranch %44",
  516. "%44 = OpLabel",
  517. "%48 = OpLoad %float %38",
  518. "OpStore %39 %48",
  519. "%25 = OpLoad %float %39",
  520. "%26 = OpCompositeConstruct %v4float %25 %25 %25 %25",
  521. "OpStore %color %26",
  522. "%27 = OpLoad %v4float %color",
  523. "OpStore %gl_FragColor %27",
  524. "OpReturn",
  525. "OpFunctionEnd",
  526. // clang-format on
  527. };
  528. SinglePassRunAndCheck<InlineExhaustivePass>(
  529. JoinAllInsts(Concat(Concat(predefs, before), nonEntryFuncs)),
  530. JoinAllInsts(Concat(Concat(predefs, after), nonEntryFuncs)),
  531. /* skip_nop = */ false, /* do_validate = */ true);
  532. }
  533. TEST_F(InlineTest, PhiAfterCall) {
  534. // #version 140
  535. //
  536. // in vec4 BaseColor;
  537. //
  538. // float foo(float bar)
  539. // {
  540. // float r = bar;
  541. // if (r < 0.0)
  542. // r = -r;
  543. // return r;
  544. // }
  545. //
  546. // void main()
  547. // {
  548. // vec4 color = BaseColor;
  549. // if (foo(color.x) > 2.0 && foo(color.y) > 2.0)
  550. // color = vec4(0.0);
  551. // gl_FragColor = color;
  552. // }
  553. const std::vector<const char*> predefs = {
  554. // clang-format off
  555. "OpCapability Shader",
  556. "%1 = OpExtInstImport \"GLSL.std.450\"",
  557. "OpMemoryModel Logical GLSL450",
  558. "OpEntryPoint Fragment %main \"main\" %BaseColor %gl_FragColor",
  559. "OpExecutionMode %main OriginUpperLeft",
  560. "OpSource GLSL 140",
  561. "OpName %main \"main\"",
  562. "OpName %foo_f1_ \"foo(f1;\"",
  563. "OpName %bar \"bar\"",
  564. "OpName %r \"r\"",
  565. "OpName %color \"color\"",
  566. "OpName %BaseColor \"BaseColor\"",
  567. "OpName %param \"param\"",
  568. "OpName %param_0 \"param\"",
  569. "OpName %gl_FragColor \"gl_FragColor\"",
  570. "%void = OpTypeVoid",
  571. "%12 = OpTypeFunction %void",
  572. "%float = OpTypeFloat 32",
  573. "%_ptr_Function_float = OpTypePointer Function %float",
  574. "%15 = OpTypeFunction %float %_ptr_Function_float",
  575. "%float_0 = OpConstant %float 0",
  576. "%bool = OpTypeBool",
  577. "%v4float = OpTypeVector %float 4",
  578. "%_ptr_Function_v4float = OpTypePointer Function %v4float",
  579. "%_ptr_Input_v4float = OpTypePointer Input %v4float",
  580. "%BaseColor = OpVariable %_ptr_Input_v4float Input",
  581. "%uint = OpTypeInt 32 0",
  582. "%uint_0 = OpConstant %uint 0",
  583. "%float_2 = OpConstant %float 2",
  584. "%uint_1 = OpConstant %uint 1",
  585. "%25 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0",
  586. "%_ptr_Output_v4float = OpTypePointer Output %v4float",
  587. "%gl_FragColor = OpVariable %_ptr_Output_v4float Output",
  588. // clang-format on
  589. };
  590. const std::vector<const char*> nonEntryFuncs = {
  591. // clang-format off
  592. "%foo_f1_ = OpFunction %float None %15",
  593. "%bar = OpFunctionParameter %_ptr_Function_float",
  594. "%43 = OpLabel",
  595. "%r = OpVariable %_ptr_Function_float Function",
  596. "%44 = OpLoad %float %bar",
  597. "OpStore %r %44",
  598. "%45 = OpLoad %float %r",
  599. "%46 = OpFOrdLessThan %bool %45 %float_0",
  600. "OpSelectionMerge %47 None",
  601. "OpBranchConditional %46 %48 %47",
  602. "%48 = OpLabel",
  603. "%49 = OpLoad %float %r",
  604. "%50 = OpFNegate %float %49",
  605. "OpStore %r %50",
  606. "OpBranch %47",
  607. "%47 = OpLabel",
  608. "%51 = OpLoad %float %r",
  609. "OpReturnValue %51",
  610. "OpFunctionEnd",
  611. // clang-format on
  612. };
  613. const std::vector<const char*> before = {
  614. // clang-format off
  615. "%main = OpFunction %void None %12",
  616. "%27 = OpLabel",
  617. "%color = OpVariable %_ptr_Function_v4float Function",
  618. "%param = OpVariable %_ptr_Function_float Function",
  619. "%param_0 = OpVariable %_ptr_Function_float Function",
  620. "%28 = OpLoad %v4float %BaseColor",
  621. "OpStore %color %28",
  622. "%29 = OpAccessChain %_ptr_Function_float %color %uint_0",
  623. "%30 = OpLoad %float %29",
  624. "OpStore %param %30",
  625. "%31 = OpFunctionCall %float %foo_f1_ %param",
  626. "%32 = OpFOrdGreaterThan %bool %31 %float_2",
  627. "OpSelectionMerge %33 None",
  628. "OpBranchConditional %32 %34 %33",
  629. "%34 = OpLabel",
  630. "%35 = OpAccessChain %_ptr_Function_float %color %uint_1",
  631. "%36 = OpLoad %float %35",
  632. "OpStore %param_0 %36",
  633. "%37 = OpFunctionCall %float %foo_f1_ %param_0",
  634. "%38 = OpFOrdGreaterThan %bool %37 %float_2",
  635. "OpBranch %33",
  636. "%33 = OpLabel",
  637. "%39 = OpPhi %bool %32 %27 %38 %34",
  638. "OpSelectionMerge %40 None",
  639. "OpBranchConditional %39 %41 %40",
  640. "%41 = OpLabel",
  641. "OpStore %color %25",
  642. "OpBranch %40",
  643. "%40 = OpLabel",
  644. "%42 = OpLoad %v4float %color",
  645. "OpStore %gl_FragColor %42",
  646. "OpReturn",
  647. "OpFunctionEnd",
  648. // clang-format on
  649. };
  650. const std::vector<const char*> after = {
  651. // clang-format off
  652. "%main = OpFunction %void None %12",
  653. "%27 = OpLabel",
  654. "%62 = OpVariable %_ptr_Function_float Function",
  655. "%63 = OpVariable %_ptr_Function_float Function",
  656. "%52 = OpVariable %_ptr_Function_float Function",
  657. "%53 = OpVariable %_ptr_Function_float Function",
  658. "%color = OpVariable %_ptr_Function_v4float Function",
  659. "%param = OpVariable %_ptr_Function_float Function",
  660. "%param_0 = OpVariable %_ptr_Function_float Function",
  661. "%28 = OpLoad %v4float %BaseColor",
  662. "OpStore %color %28",
  663. "%29 = OpAccessChain %_ptr_Function_float %color %uint_0",
  664. "%30 = OpLoad %float %29",
  665. "OpStore %param %30",
  666. "%54 = OpLoad %float %param",
  667. "OpStore %52 %54",
  668. "%55 = OpLoad %float %52",
  669. "%56 = OpFOrdLessThan %bool %55 %float_0",
  670. "OpSelectionMerge %57 None",
  671. "OpBranchConditional %56 %58 %57",
  672. "%58 = OpLabel",
  673. "%59 = OpLoad %float %52",
  674. "%60 = OpFNegate %float %59",
  675. "OpStore %52 %60",
  676. "OpBranch %57",
  677. "%57 = OpLabel",
  678. "%61 = OpLoad %float %52",
  679. "OpStore %53 %61",
  680. "%31 = OpLoad %float %53",
  681. "%32 = OpFOrdGreaterThan %bool %31 %float_2",
  682. "OpSelectionMerge %33 None",
  683. "OpBranchConditional %32 %34 %33",
  684. "%34 = OpLabel",
  685. "%35 = OpAccessChain %_ptr_Function_float %color %uint_1",
  686. "%36 = OpLoad %float %35",
  687. "OpStore %param_0 %36",
  688. "%64 = OpLoad %float %param_0",
  689. "OpStore %62 %64",
  690. "%65 = OpLoad %float %62",
  691. "%66 = OpFOrdLessThan %bool %65 %float_0",
  692. "OpSelectionMerge %67 None",
  693. "OpBranchConditional %66 %68 %67",
  694. "%68 = OpLabel",
  695. "%69 = OpLoad %float %62",
  696. "%70 = OpFNegate %float %69",
  697. "OpStore %62 %70",
  698. "OpBranch %67",
  699. "%67 = OpLabel",
  700. "%71 = OpLoad %float %62",
  701. "OpStore %63 %71",
  702. "%37 = OpLoad %float %63",
  703. "%38 = OpFOrdGreaterThan %bool %37 %float_2",
  704. "OpBranch %33",
  705. "%33 = OpLabel",
  706. "%39 = OpPhi %bool %32 %57 %38 %67",
  707. "OpSelectionMerge %40 None",
  708. "OpBranchConditional %39 %41 %40",
  709. "%41 = OpLabel",
  710. "OpStore %color %25",
  711. "OpBranch %40",
  712. "%40 = OpLabel",
  713. "%42 = OpLoad %v4float %color",
  714. "OpStore %gl_FragColor %42",
  715. "OpReturn",
  716. "OpFunctionEnd",
  717. // clang-format on
  718. };
  719. SinglePassRunAndCheck<InlineExhaustivePass>(
  720. JoinAllInsts(Concat(Concat(predefs, before), nonEntryFuncs)),
  721. JoinAllInsts(Concat(Concat(predefs, after), nonEntryFuncs)),
  722. /* skip_nop = */ false, /* do_validate = */ true);
  723. }
  724. TEST_F(InlineTest, OpSampledImageOutOfBlock) {
  725. // #version 450
  726. //
  727. // uniform texture2D t2D;
  728. // uniform sampler samp;
  729. // out vec4 FragColor;
  730. // in vec4 BaseColor;
  731. //
  732. // float foo(vec4 bar)
  733. // {
  734. // float r = bar.x;
  735. // if (r < 0.0)
  736. // r = -r;
  737. // return r;
  738. // }
  739. //
  740. // void main()
  741. // {
  742. // vec4 color1 = texture(sampler2D(t2D, samp), vec2(1.0));
  743. // vec4 color2 = vec4(foo(BaseColor));
  744. // vec4 color3 = texture(sampler2D(t2D, samp), vec2(0.5));
  745. // FragColor = (color1 + color2 + color3)/3;
  746. // }
  747. //
  748. // Note: the before SPIR-V will need to be edited to create a use of
  749. // the OpSampledImage across the function call.
  750. const std::vector<const char*> predefs = {
  751. // clang-format off
  752. "OpCapability Shader",
  753. "%1 = OpExtInstImport \"GLSL.std.450\"",
  754. "OpMemoryModel Logical GLSL450",
  755. "OpEntryPoint Fragment %main \"main\" %BaseColor %FragColor",
  756. "OpExecutionMode %main OriginUpperLeft",
  757. "OpSource GLSL 450",
  758. "OpName %main \"main\"",
  759. "OpName %foo_vf4_ \"foo(vf4;\"",
  760. "OpName %bar \"bar\"",
  761. "OpName %r \"r\"",
  762. "OpName %color1 \"color1\"",
  763. "OpName %t2D \"t2D\"",
  764. "OpName %samp \"samp\"",
  765. "OpName %color2 \"color2\"",
  766. "OpName %BaseColor \"BaseColor\"",
  767. "OpName %param \"param\"",
  768. "OpName %color3 \"color3\"",
  769. "OpName %FragColor \"FragColor\"",
  770. "OpDecorate %t2D DescriptorSet 0",
  771. "OpDecorate %samp DescriptorSet 0",
  772. "%void = OpTypeVoid",
  773. "%15 = OpTypeFunction %void",
  774. "%float = OpTypeFloat 32",
  775. "%v4float = OpTypeVector %float 4",
  776. "%_ptr_Function_v4float = OpTypePointer Function %v4float",
  777. "%19 = OpTypeFunction %float %_ptr_Function_v4float",
  778. "%_ptr_Function_float = OpTypePointer Function %float",
  779. "%uint = OpTypeInt 32 0",
  780. "%uint_0 = OpConstant %uint 0",
  781. "%float_0 = OpConstant %float 0",
  782. "%bool = OpTypeBool",
  783. "%25 = OpTypeImage %float 2D 0 0 0 1 Unknown",
  784. "%_ptr_UniformConstant_25 = OpTypePointer UniformConstant %25",
  785. "%t2D = OpVariable %_ptr_UniformConstant_25 UniformConstant",
  786. "%27 = OpTypeSampler",
  787. "%_ptr_UniformConstant_27 = OpTypePointer UniformConstant %27",
  788. "%samp = OpVariable %_ptr_UniformConstant_27 UniformConstant",
  789. "%29 = OpTypeSampledImage %25",
  790. "%v2float = OpTypeVector %float 2",
  791. "%float_1 = OpConstant %float 1",
  792. "%32 = OpConstantComposite %v2float %float_1 %float_1",
  793. "%_ptr_Input_v4float = OpTypePointer Input %v4float",
  794. "%BaseColor = OpVariable %_ptr_Input_v4float Input",
  795. "%float_0_5 = OpConstant %float 0.5",
  796. "%35 = OpConstantComposite %v2float %float_0_5 %float_0_5",
  797. "%_ptr_Output_v4float = OpTypePointer Output %v4float",
  798. "%FragColor = OpVariable %_ptr_Output_v4float Output",
  799. "%float_3 = OpConstant %float 3",
  800. // clang-format on
  801. };
  802. const std::vector<const char*> nonEntryFuncs = {
  803. // clang-format off
  804. "%foo_vf4_ = OpFunction %float None %19",
  805. "%bar = OpFunctionParameter %_ptr_Function_v4float",
  806. "%56 = OpLabel",
  807. "%r = OpVariable %_ptr_Function_float Function",
  808. "%57 = OpAccessChain %_ptr_Function_float %bar %uint_0",
  809. "%58 = OpLoad %float %57",
  810. "OpStore %r %58",
  811. "%59 = OpLoad %float %r",
  812. "%60 = OpFOrdLessThan %bool %59 %float_0",
  813. "OpSelectionMerge %61 None",
  814. "OpBranchConditional %60 %62 %61",
  815. "%62 = OpLabel",
  816. "%63 = OpLoad %float %r",
  817. "%64 = OpFNegate %float %63",
  818. "OpStore %r %64",
  819. "OpBranch %61",
  820. "%61 = OpLabel",
  821. "%65 = OpLoad %float %r",
  822. "OpReturnValue %65",
  823. "OpFunctionEnd",
  824. // clang-format on
  825. };
  826. const std::vector<const char*> before = {
  827. // clang-format off
  828. "%main = OpFunction %void None %15",
  829. "%38 = OpLabel",
  830. "%color1 = OpVariable %_ptr_Function_v4float Function",
  831. "%color2 = OpVariable %_ptr_Function_v4float Function",
  832. "%param = OpVariable %_ptr_Function_v4float Function",
  833. "%color3 = OpVariable %_ptr_Function_v4float Function",
  834. "%39 = OpLoad %25 %t2D",
  835. "%40 = OpLoad %27 %samp",
  836. "%41 = OpSampledImage %29 %39 %40",
  837. "%42 = OpImageSampleImplicitLod %v4float %41 %32",
  838. "OpStore %color1 %42",
  839. "%43 = OpLoad %v4float %BaseColor",
  840. "OpStore %param %43",
  841. "%44 = OpFunctionCall %float %foo_vf4_ %param",
  842. "%45 = OpCompositeConstruct %v4float %44 %44 %44 %44",
  843. "OpStore %color2 %45",
  844. "%46 = OpLoad %25 %t2D",
  845. "%47 = OpLoad %27 %samp",
  846. "%48 = OpImageSampleImplicitLod %v4float %41 %35",
  847. "OpStore %color3 %48",
  848. "%49 = OpLoad %v4float %color1",
  849. "%50 = OpLoad %v4float %color2",
  850. "%51 = OpFAdd %v4float %49 %50",
  851. "%52 = OpLoad %v4float %color3",
  852. "%53 = OpFAdd %v4float %51 %52",
  853. "%54 = OpCompositeConstruct %v4float %float_3 %float_3 %float_3 %float_3",
  854. "%55 = OpFDiv %v4float %53 %54",
  855. "OpStore %FragColor %55",
  856. "OpReturn",
  857. "OpFunctionEnd",
  858. // clang-format on
  859. };
  860. const std::vector<const char*> after = {
  861. // clang-format off
  862. "%main = OpFunction %void None %15",
  863. "%38 = OpLabel",
  864. "%66 = OpVariable %_ptr_Function_float Function",
  865. "%67 = OpVariable %_ptr_Function_float Function",
  866. "%color1 = OpVariable %_ptr_Function_v4float Function",
  867. "%color2 = OpVariable %_ptr_Function_v4float Function",
  868. "%param = OpVariable %_ptr_Function_v4float Function",
  869. "%color3 = OpVariable %_ptr_Function_v4float Function",
  870. "%39 = OpLoad %25 %t2D",
  871. "%40 = OpLoad %27 %samp",
  872. "%41 = OpSampledImage %29 %39 %40",
  873. "%42 = OpImageSampleImplicitLod %v4float %41 %32",
  874. "OpStore %color1 %42",
  875. "%43 = OpLoad %v4float %BaseColor",
  876. "OpStore %param %43",
  877. "%68 = OpAccessChain %_ptr_Function_float %param %uint_0",
  878. "%69 = OpLoad %float %68",
  879. "OpStore %66 %69",
  880. "%70 = OpLoad %float %66",
  881. "%71 = OpFOrdLessThan %bool %70 %float_0",
  882. "OpSelectionMerge %72 None",
  883. "OpBranchConditional %71 %73 %72",
  884. "%73 = OpLabel",
  885. "%74 = OpLoad %float %66",
  886. "%75 = OpFNegate %float %74",
  887. "OpStore %66 %75",
  888. "OpBranch %72",
  889. "%72 = OpLabel",
  890. "%76 = OpLoad %float %66",
  891. "OpStore %67 %76",
  892. "%44 = OpLoad %float %67",
  893. "%45 = OpCompositeConstruct %v4float %44 %44 %44 %44",
  894. "OpStore %color2 %45",
  895. "%46 = OpLoad %25 %t2D",
  896. "%47 = OpLoad %27 %samp",
  897. "%77 = OpSampledImage %29 %39 %40",
  898. "%48 = OpImageSampleImplicitLod %v4float %77 %35",
  899. "OpStore %color3 %48",
  900. "%49 = OpLoad %v4float %color1",
  901. "%50 = OpLoad %v4float %color2",
  902. "%51 = OpFAdd %v4float %49 %50",
  903. "%52 = OpLoad %v4float %color3",
  904. "%53 = OpFAdd %v4float %51 %52",
  905. "%54 = OpCompositeConstruct %v4float %float_3 %float_3 %float_3 %float_3",
  906. "%55 = OpFDiv %v4float %53 %54",
  907. "OpStore %FragColor %55",
  908. "OpReturn",
  909. "OpFunctionEnd",
  910. // clang-format on
  911. };
  912. SinglePassRunAndCheck<InlineExhaustivePass>(
  913. JoinAllInsts(Concat(Concat(predefs, before), nonEntryFuncs)),
  914. JoinAllInsts(Concat(Concat(predefs, after), nonEntryFuncs)),
  915. /* skip_nop = */ false, /* do_validate = */ true);
  916. }
  917. TEST_F(InlineTest, OpImageOutOfBlock) {
  918. // #version 450
  919. //
  920. // uniform texture2D t2D;
  921. // uniform sampler samp;
  922. // uniform sampler samp2;
  923. //
  924. // out vec4 FragColor;
  925. //
  926. // in vec4 BaseColor;
  927. //
  928. // float foo(vec4 bar)
  929. // {
  930. // float r = bar.x;
  931. // if (r < 0.0)
  932. // r = -r;
  933. // return r;
  934. // }
  935. //
  936. // void main()
  937. // {
  938. // vec4 color1 = texture(sampler2D(t2D, samp), vec2(1.0));
  939. // vec4 color2 = vec4(foo(BaseColor));
  940. // vec4 color3 = texture(sampler2D(t2D, samp2), vec2(0.5));
  941. // FragColor = (color1 + color2 + color3)/3;
  942. // }
  943. // Note: the before SPIR-V will need to be edited to create an OpImage
  944. // from the first OpSampledImage, place it before the call and use it
  945. // in the second OpSampledImage following the call.
  946. const std::vector<const char*> predefs = {
  947. // clang-format off
  948. "OpCapability Shader",
  949. "%1 = OpExtInstImport \"GLSL.std.450\"",
  950. "OpMemoryModel Logical GLSL450",
  951. "OpEntryPoint Fragment %main \"main\" %BaseColor %FragColor",
  952. "OpExecutionMode %main OriginUpperLeft",
  953. "OpSource GLSL 450",
  954. "OpName %main \"main\"",
  955. "OpName %foo_vf4_ \"foo(vf4;\"",
  956. "OpName %bar \"bar\"",
  957. "OpName %r \"r\"",
  958. "OpName %color1 \"color1\"",
  959. "OpName %t2D \"t2D\"",
  960. "OpName %samp \"samp\"",
  961. "OpName %color2 \"color2\"",
  962. "OpName %BaseColor \"BaseColor\"",
  963. "OpName %param \"param\"",
  964. "OpName %color3 \"color3\"",
  965. "OpName %samp2 \"samp2\"",
  966. "OpName %FragColor \"FragColor\"",
  967. "OpDecorate %t2D DescriptorSet 0",
  968. "OpDecorate %samp DescriptorSet 0",
  969. "OpDecorate %samp2 DescriptorSet 0",
  970. "%void = OpTypeVoid",
  971. "%16 = OpTypeFunction %void",
  972. "%float = OpTypeFloat 32",
  973. "%v4float = OpTypeVector %float 4",
  974. "%_ptr_Function_v4float = OpTypePointer Function %v4float",
  975. "%20 = OpTypeFunction %float %_ptr_Function_v4float",
  976. "%_ptr_Function_float = OpTypePointer Function %float",
  977. "%uint = OpTypeInt 32 0",
  978. "%uint_0 = OpConstant %uint 0",
  979. "%float_0 = OpConstant %float 0",
  980. "%bool = OpTypeBool",
  981. "%26 = OpTypeImage %float 2D 0 0 0 1 Unknown",
  982. "%_ptr_UniformConstant_26 = OpTypePointer UniformConstant %26",
  983. "%t2D = OpVariable %_ptr_UniformConstant_26 UniformConstant",
  984. "%28 = OpTypeSampler",
  985. "%_ptr_UniformConstant_28 = OpTypePointer UniformConstant %28",
  986. "%samp = OpVariable %_ptr_UniformConstant_28 UniformConstant",
  987. "%30 = OpTypeSampledImage %26",
  988. "%v2float = OpTypeVector %float 2",
  989. "%float_1 = OpConstant %float 1",
  990. "%33 = OpConstantComposite %v2float %float_1 %float_1",
  991. "%_ptr_Input_v4float = OpTypePointer Input %v4float",
  992. "%BaseColor = OpVariable %_ptr_Input_v4float Input",
  993. "%samp2 = OpVariable %_ptr_UniformConstant_28 UniformConstant",
  994. "%float_0_5 = OpConstant %float 0.5",
  995. "%36 = OpConstantComposite %v2float %float_0_5 %float_0_5",
  996. "%_ptr_Output_v4float = OpTypePointer Output %v4float",
  997. "%FragColor = OpVariable %_ptr_Output_v4float Output",
  998. "%float_3 = OpConstant %float 3",
  999. // clang-format on
  1000. };
  1001. const std::vector<const char*> nonEntryFuncs = {
  1002. // clang-format off
  1003. "%foo_vf4_ = OpFunction %float None %20",
  1004. "%bar = OpFunctionParameter %_ptr_Function_v4float",
  1005. "%58 = OpLabel",
  1006. "%r = OpVariable %_ptr_Function_float Function",
  1007. "%59 = OpAccessChain %_ptr_Function_float %bar %uint_0",
  1008. "%60 = OpLoad %float %59",
  1009. "OpStore %r %60",
  1010. "%61 = OpLoad %float %r",
  1011. "%62 = OpFOrdLessThan %bool %61 %float_0",
  1012. "OpSelectionMerge %63 None",
  1013. "OpBranchConditional %62 %64 %63",
  1014. "%64 = OpLabel",
  1015. "%65 = OpLoad %float %r",
  1016. "%66 = OpFNegate %float %65",
  1017. "OpStore %r %66",
  1018. "OpBranch %63",
  1019. "%63 = OpLabel",
  1020. "%67 = OpLoad %float %r",
  1021. "OpReturnValue %67",
  1022. "OpFunctionEnd",
  1023. // clang-format on
  1024. };
  1025. const std::vector<const char*> before = {
  1026. // clang-format off
  1027. "%main = OpFunction %void None %16",
  1028. "%39 = OpLabel",
  1029. "%color1 = OpVariable %_ptr_Function_v4float Function",
  1030. "%color2 = OpVariable %_ptr_Function_v4float Function",
  1031. "%param = OpVariable %_ptr_Function_v4float Function",
  1032. "%color3 = OpVariable %_ptr_Function_v4float Function",
  1033. "%40 = OpLoad %26 %t2D",
  1034. "%41 = OpLoad %28 %samp",
  1035. "%42 = OpSampledImage %30 %40 %41",
  1036. "%43 = OpImageSampleImplicitLod %v4float %42 %33",
  1037. "%44 = OpImage %26 %42",
  1038. "%45 = OpLoad %28 %samp2",
  1039. "OpStore %color1 %43",
  1040. "%46 = OpLoad %v4float %BaseColor",
  1041. "OpStore %param %46",
  1042. "%47 = OpFunctionCall %float %foo_vf4_ %param",
  1043. "%48 = OpCompositeConstruct %v4float %47 %47 %47 %47",
  1044. "OpStore %color2 %48",
  1045. "%49 = OpSampledImage %30 %44 %45",
  1046. "%50 = OpImageSampleImplicitLod %v4float %49 %36",
  1047. "OpStore %color3 %50",
  1048. "%51 = OpLoad %v4float %color1",
  1049. "%52 = OpLoad %v4float %color2",
  1050. "%53 = OpFAdd %v4float %51 %52",
  1051. "%54 = OpLoad %v4float %color3",
  1052. "%55 = OpFAdd %v4float %53 %54",
  1053. "%56 = OpCompositeConstruct %v4float %float_3 %float_3 %float_3 %float_3",
  1054. "%57 = OpFDiv %v4float %55 %56",
  1055. "OpStore %FragColor %57",
  1056. "OpReturn",
  1057. "OpFunctionEnd",
  1058. // clang-format on
  1059. };
  1060. const std::vector<const char*> after = {
  1061. // clang-format off
  1062. "%main = OpFunction %void None %16",
  1063. "%39 = OpLabel",
  1064. "%68 = OpVariable %_ptr_Function_float Function",
  1065. "%69 = OpVariable %_ptr_Function_float Function",
  1066. "%color1 = OpVariable %_ptr_Function_v4float Function",
  1067. "%color2 = OpVariable %_ptr_Function_v4float Function",
  1068. "%param = OpVariable %_ptr_Function_v4float Function",
  1069. "%color3 = OpVariable %_ptr_Function_v4float Function",
  1070. "%40 = OpLoad %26 %t2D",
  1071. "%41 = OpLoad %28 %samp",
  1072. "%42 = OpSampledImage %30 %40 %41",
  1073. "%43 = OpImageSampleImplicitLod %v4float %42 %33",
  1074. "%44 = OpImage %26 %42",
  1075. "%45 = OpLoad %28 %samp2",
  1076. "OpStore %color1 %43",
  1077. "%46 = OpLoad %v4float %BaseColor",
  1078. "OpStore %param %46",
  1079. "%70 = OpAccessChain %_ptr_Function_float %param %uint_0",
  1080. "%71 = OpLoad %float %70",
  1081. "OpStore %68 %71",
  1082. "%72 = OpLoad %float %68",
  1083. "%73 = OpFOrdLessThan %bool %72 %float_0",
  1084. "OpSelectionMerge %74 None",
  1085. "OpBranchConditional %73 %75 %74",
  1086. "%75 = OpLabel",
  1087. "%76 = OpLoad %float %68",
  1088. "%77 = OpFNegate %float %76",
  1089. "OpStore %68 %77",
  1090. "OpBranch %74",
  1091. "%74 = OpLabel",
  1092. "%78 = OpLoad %float %68",
  1093. "OpStore %69 %78",
  1094. "%47 = OpLoad %float %69",
  1095. "%48 = OpCompositeConstruct %v4float %47 %47 %47 %47",
  1096. "OpStore %color2 %48",
  1097. "%79 = OpSampledImage %30 %40 %41",
  1098. "%80 = OpImage %26 %79",
  1099. "%49 = OpSampledImage %30 %80 %45",
  1100. "%50 = OpImageSampleImplicitLod %v4float %49 %36",
  1101. "OpStore %color3 %50",
  1102. "%51 = OpLoad %v4float %color1",
  1103. "%52 = OpLoad %v4float %color2",
  1104. "%53 = OpFAdd %v4float %51 %52",
  1105. "%54 = OpLoad %v4float %color3",
  1106. "%55 = OpFAdd %v4float %53 %54",
  1107. "%56 = OpCompositeConstruct %v4float %float_3 %float_3 %float_3 %float_3",
  1108. "%57 = OpFDiv %v4float %55 %56",
  1109. "OpStore %FragColor %57",
  1110. "OpReturn",
  1111. "OpFunctionEnd",
  1112. // clang-format on
  1113. };
  1114. SinglePassRunAndCheck<InlineExhaustivePass>(
  1115. JoinAllInsts(Concat(Concat(predefs, before), nonEntryFuncs)),
  1116. JoinAllInsts(Concat(Concat(predefs, after), nonEntryFuncs)),
  1117. /* skip_nop = */ false, /* do_validate = */ true);
  1118. }
  1119. TEST_F(InlineTest, OpImageAndOpSampledImageOutOfBlock) {
  1120. // #version 450
  1121. //
  1122. // uniform texture2D t2D;
  1123. // uniform sampler samp;
  1124. // uniform sampler samp2;
  1125. //
  1126. // out vec4 FragColor;
  1127. //
  1128. // in vec4 BaseColor;
  1129. //
  1130. // float foo(vec4 bar)
  1131. // {
  1132. // float r = bar.x;
  1133. // if (r < 0.0)
  1134. // r = -r;
  1135. // return r;
  1136. // }
  1137. //
  1138. // void main()
  1139. // {
  1140. // vec4 color1 = texture(sampler2D(t2D, samp), vec2(1.0));
  1141. // vec4 color2 = vec4(foo(BaseColor));
  1142. // vec4 color3 = texture(sampler2D(t2D, samp2), vec2(0.5));
  1143. // FragColor = (color1 + color2 + color3)/3;
  1144. // }
  1145. // Note: the before SPIR-V will need to be edited to create an OpImage
  1146. // and subsequent OpSampledImage that is used across the function call.
  1147. const std::vector<const char*> predefs = {
  1148. // clang-format off
  1149. "OpCapability Shader",
  1150. "%1 = OpExtInstImport \"GLSL.std.450\"",
  1151. "OpMemoryModel Logical GLSL450",
  1152. "OpEntryPoint Fragment %main \"main\" %BaseColor %FragColor",
  1153. "OpExecutionMode %main OriginUpperLeft",
  1154. "OpSource GLSL 450",
  1155. "OpName %main \"main\"",
  1156. "OpName %foo_vf4_ \"foo(vf4;\"",
  1157. "OpName %bar \"bar\"",
  1158. "OpName %r \"r\"",
  1159. "OpName %color1 \"color1\"",
  1160. "OpName %t2D \"t2D\"",
  1161. "OpName %samp \"samp\"",
  1162. "OpName %color2 \"color2\"",
  1163. "OpName %BaseColor \"BaseColor\"",
  1164. "OpName %param \"param\"",
  1165. "OpName %color3 \"color3\"",
  1166. "OpName %samp2 \"samp2\"",
  1167. "OpName %FragColor \"FragColor\"",
  1168. "OpDecorate %t2D DescriptorSet 0",
  1169. "OpDecorate %samp DescriptorSet 0",
  1170. "OpDecorate %samp2 DescriptorSet 0",
  1171. "%void = OpTypeVoid",
  1172. "%16 = OpTypeFunction %void",
  1173. "%float = OpTypeFloat 32",
  1174. "%v4float = OpTypeVector %float 4",
  1175. "%_ptr_Function_v4float = OpTypePointer Function %v4float",
  1176. "%20 = OpTypeFunction %float %_ptr_Function_v4float",
  1177. "%_ptr_Function_float = OpTypePointer Function %float",
  1178. "%uint = OpTypeInt 32 0",
  1179. "%uint_0 = OpConstant %uint 0",
  1180. "%float_0 = OpConstant %float 0",
  1181. "%bool = OpTypeBool",
  1182. "%26 = OpTypeImage %float 2D 0 0 0 1 Unknown",
  1183. "%_ptr_UniformConstant_26 = OpTypePointer UniformConstant %26",
  1184. "%t2D = OpVariable %_ptr_UniformConstant_26 UniformConstant",
  1185. "%28 = OpTypeSampler",
  1186. "%_ptr_UniformConstant_28 = OpTypePointer UniformConstant %28",
  1187. "%samp = OpVariable %_ptr_UniformConstant_28 UniformConstant",
  1188. "%30 = OpTypeSampledImage %26",
  1189. "%v2float = OpTypeVector %float 2",
  1190. "%float_1 = OpConstant %float 1",
  1191. "%33 = OpConstantComposite %v2float %float_1 %float_1",
  1192. "%_ptr_Input_v4float = OpTypePointer Input %v4float",
  1193. "%BaseColor = OpVariable %_ptr_Input_v4float Input",
  1194. "%samp2 = OpVariable %_ptr_UniformConstant_28 UniformConstant",
  1195. "%float_0_5 = OpConstant %float 0.5",
  1196. "%36 = OpConstantComposite %v2float %float_0_5 %float_0_5",
  1197. "%_ptr_Output_v4float = OpTypePointer Output %v4float",
  1198. "%FragColor = OpVariable %_ptr_Output_v4float Output",
  1199. "%float_3 = OpConstant %float 3",
  1200. // clang-format on
  1201. };
  1202. const std::vector<const char*> nonEntryFuncs = {
  1203. // clang-format off
  1204. "%foo_vf4_ = OpFunction %float None %20",
  1205. "%bar = OpFunctionParameter %_ptr_Function_v4float",
  1206. "%58 = OpLabel",
  1207. "%r = OpVariable %_ptr_Function_float Function",
  1208. "%59 = OpAccessChain %_ptr_Function_float %bar %uint_0",
  1209. "%60 = OpLoad %float %59",
  1210. "OpStore %r %60",
  1211. "%61 = OpLoad %float %r",
  1212. "%62 = OpFOrdLessThan %bool %61 %float_0",
  1213. "OpSelectionMerge %63 None",
  1214. "OpBranchConditional %62 %64 %63",
  1215. "%64 = OpLabel",
  1216. "%65 = OpLoad %float %r",
  1217. "%66 = OpFNegate %float %65",
  1218. "OpStore %r %66",
  1219. "OpBranch %63",
  1220. "%63 = OpLabel",
  1221. "%67 = OpLoad %float %r",
  1222. "OpReturnValue %67",
  1223. "OpFunctionEnd",
  1224. // clang-format on
  1225. };
  1226. const std::vector<const char*> before = {
  1227. // clang-format off
  1228. "%main = OpFunction %void None %16",
  1229. "%39 = OpLabel",
  1230. "%color1 = OpVariable %_ptr_Function_v4float Function",
  1231. "%color2 = OpVariable %_ptr_Function_v4float Function",
  1232. "%param = OpVariable %_ptr_Function_v4float Function",
  1233. "%color3 = OpVariable %_ptr_Function_v4float Function",
  1234. "%40 = OpLoad %26 %t2D",
  1235. "%41 = OpLoad %28 %samp",
  1236. "%42 = OpSampledImage %30 %40 %41",
  1237. "%43 = OpImageSampleImplicitLod %v4float %42 %33",
  1238. "%44 = OpImage %26 %42",
  1239. "%45 = OpLoad %28 %samp2",
  1240. "%46 = OpSampledImage %30 %44 %45",
  1241. "OpStore %color1 %43",
  1242. "%47 = OpLoad %v4float %BaseColor",
  1243. "OpStore %param %47",
  1244. "%48 = OpFunctionCall %float %foo_vf4_ %param",
  1245. "%49 = OpCompositeConstruct %v4float %48 %48 %48 %48",
  1246. "OpStore %color2 %49",
  1247. "%50 = OpImageSampleImplicitLod %v4float %46 %36",
  1248. "OpStore %color3 %50",
  1249. "%51 = OpLoad %v4float %color1",
  1250. "%52 = OpLoad %v4float %color2",
  1251. "%53 = OpFAdd %v4float %51 %52",
  1252. "%54 = OpLoad %v4float %color3",
  1253. "%55 = OpFAdd %v4float %53 %54",
  1254. "%56 = OpCompositeConstruct %v4float %float_3 %float_3 %float_3 %float_3",
  1255. "%57 = OpFDiv %v4float %55 %56",
  1256. "OpStore %FragColor %57",
  1257. "OpReturn",
  1258. "OpFunctionEnd",
  1259. // clang-format on
  1260. };
  1261. const std::vector<const char*> after = {
  1262. // clang-format off
  1263. "%main = OpFunction %void None %16",
  1264. "%39 = OpLabel",
  1265. "%68 = OpVariable %_ptr_Function_float Function",
  1266. "%69 = OpVariable %_ptr_Function_float Function",
  1267. "%color1 = OpVariable %_ptr_Function_v4float Function",
  1268. "%color2 = OpVariable %_ptr_Function_v4float Function",
  1269. "%param = OpVariable %_ptr_Function_v4float Function",
  1270. "%color3 = OpVariable %_ptr_Function_v4float Function",
  1271. "%40 = OpLoad %26 %t2D",
  1272. "%41 = OpLoad %28 %samp",
  1273. "%42 = OpSampledImage %30 %40 %41",
  1274. "%43 = OpImageSampleImplicitLod %v4float %42 %33",
  1275. "%44 = OpImage %26 %42",
  1276. "%45 = OpLoad %28 %samp2",
  1277. "%46 = OpSampledImage %30 %44 %45",
  1278. "OpStore %color1 %43",
  1279. "%47 = OpLoad %v4float %BaseColor",
  1280. "OpStore %param %47",
  1281. "%70 = OpAccessChain %_ptr_Function_float %param %uint_0",
  1282. "%71 = OpLoad %float %70",
  1283. "OpStore %68 %71",
  1284. "%72 = OpLoad %float %68",
  1285. "%73 = OpFOrdLessThan %bool %72 %float_0",
  1286. "OpSelectionMerge %74 None",
  1287. "OpBranchConditional %73 %75 %74",
  1288. "%75 = OpLabel",
  1289. "%76 = OpLoad %float %68",
  1290. "%77 = OpFNegate %float %76",
  1291. "OpStore %68 %77",
  1292. "OpBranch %74",
  1293. "%74 = OpLabel",
  1294. "%78 = OpLoad %float %68",
  1295. "OpStore %69 %78",
  1296. "%48 = OpLoad %float %69",
  1297. "%49 = OpCompositeConstruct %v4float %48 %48 %48 %48",
  1298. "OpStore %color2 %49",
  1299. "%79 = OpSampledImage %30 %40 %41",
  1300. "%80 = OpImage %26 %79",
  1301. "%81 = OpSampledImage %30 %80 %45",
  1302. "%50 = OpImageSampleImplicitLod %v4float %81 %36",
  1303. "OpStore %color3 %50",
  1304. "%51 = OpLoad %v4float %color1",
  1305. "%52 = OpLoad %v4float %color2",
  1306. "%53 = OpFAdd %v4float %51 %52",
  1307. "%54 = OpLoad %v4float %color3",
  1308. "%55 = OpFAdd %v4float %53 %54",
  1309. "%56 = OpCompositeConstruct %v4float %float_3 %float_3 %float_3 %float_3",
  1310. "%57 = OpFDiv %v4float %55 %56",
  1311. "OpStore %FragColor %57",
  1312. "OpReturn",
  1313. "OpFunctionEnd",
  1314. // clang-format on
  1315. };
  1316. SinglePassRunAndCheck<InlineExhaustivePass>(
  1317. JoinAllInsts(Concat(Concat(predefs, before), nonEntryFuncs)),
  1318. JoinAllInsts(Concat(Concat(predefs, after), nonEntryFuncs)),
  1319. /* skip_nop = */ false, /* do_validate = */ true);
  1320. }
  1321. TEST_F(InlineTest, EarlyReturnFunctionInlined) {
  1322. // #version 140
  1323. //
  1324. // in vec4 BaseColor;
  1325. //
  1326. // float foo(vec4 bar)
  1327. // {
  1328. // if (bar.x < 0.0)
  1329. // return 0.0;
  1330. // return bar.x;
  1331. // }
  1332. //
  1333. // void main()
  1334. // {
  1335. // vec4 color = vec4(foo(BaseColor));
  1336. // gl_FragColor = color;
  1337. // }
  1338. const std::string predefs =
  1339. R"(OpCapability Shader
  1340. %1 = OpExtInstImport "GLSL.std.450"
  1341. OpMemoryModel Logical GLSL450
  1342. OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
  1343. OpExecutionMode %main OriginUpperLeft
  1344. OpSource GLSL 140
  1345. OpName %main "main"
  1346. OpName %foo_vf4_ "foo(vf4;"
  1347. OpName %bar "bar"
  1348. OpName %color "color"
  1349. OpName %BaseColor "BaseColor"
  1350. OpName %param "param"
  1351. OpName %gl_FragColor "gl_FragColor"
  1352. %void = OpTypeVoid
  1353. %10 = OpTypeFunction %void
  1354. %float = OpTypeFloat 32
  1355. %v4float = OpTypeVector %float 4
  1356. %_ptr_Function_v4float = OpTypePointer Function %v4float
  1357. %14 = OpTypeFunction %float %_ptr_Function_v4float
  1358. %uint = OpTypeInt 32 0
  1359. %uint_0 = OpConstant %uint 0
  1360. %_ptr_Function_float = OpTypePointer Function %float
  1361. %float_0 = OpConstant %float 0
  1362. %bool = OpTypeBool
  1363. %_ptr_Input_v4float = OpTypePointer Input %v4float
  1364. %BaseColor = OpVariable %_ptr_Input_v4float Input
  1365. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1366. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  1367. )";
  1368. const std::string nonEntryFuncs =
  1369. R"(%foo_vf4_ = OpFunction %float None %14
  1370. %bar = OpFunctionParameter %_ptr_Function_v4float
  1371. %27 = OpLabel
  1372. %28 = OpAccessChain %_ptr_Function_float %bar %uint_0
  1373. %29 = OpLoad %float %28
  1374. %30 = OpFOrdLessThan %bool %29 %float_0
  1375. OpSelectionMerge %31 None
  1376. OpBranchConditional %30 %32 %31
  1377. %32 = OpLabel
  1378. OpReturnValue %float_0
  1379. %31 = OpLabel
  1380. %33 = OpAccessChain %_ptr_Function_float %bar %uint_0
  1381. %34 = OpLoad %float %33
  1382. OpReturnValue %34
  1383. OpFunctionEnd
  1384. )";
  1385. const std::string before =
  1386. R"(%main = OpFunction %void None %10
  1387. %22 = OpLabel
  1388. %color = OpVariable %_ptr_Function_v4float Function
  1389. %param = OpVariable %_ptr_Function_v4float Function
  1390. %23 = OpLoad %v4float %BaseColor
  1391. OpStore %param %23
  1392. %24 = OpFunctionCall %float %foo_vf4_ %param
  1393. %25 = OpCompositeConstruct %v4float %24 %24 %24 %24
  1394. OpStore %color %25
  1395. %26 = OpLoad %v4float %color
  1396. OpStore %gl_FragColor %26
  1397. OpReturn
  1398. OpFunctionEnd
  1399. )";
  1400. const std::string after =
  1401. R"(%false = OpConstantFalse %bool
  1402. %main = OpFunction %void None %10
  1403. %22 = OpLabel
  1404. %35 = OpVariable %_ptr_Function_float Function
  1405. %color = OpVariable %_ptr_Function_v4float Function
  1406. %param = OpVariable %_ptr_Function_v4float Function
  1407. %23 = OpLoad %v4float %BaseColor
  1408. OpStore %param %23
  1409. OpBranch %36
  1410. %36 = OpLabel
  1411. OpLoopMerge %37 %38 None
  1412. OpBranch %39
  1413. %39 = OpLabel
  1414. %40 = OpAccessChain %_ptr_Function_float %param %uint_0
  1415. %41 = OpLoad %float %40
  1416. %42 = OpFOrdLessThan %bool %41 %float_0
  1417. OpSelectionMerge %43 None
  1418. OpBranchConditional %42 %44 %43
  1419. %44 = OpLabel
  1420. OpStore %35 %float_0
  1421. OpBranch %37
  1422. %43 = OpLabel
  1423. %45 = OpAccessChain %_ptr_Function_float %param %uint_0
  1424. %46 = OpLoad %float %45
  1425. OpStore %35 %46
  1426. OpBranch %37
  1427. %38 = OpLabel
  1428. OpBranchConditional %false %36 %37
  1429. %37 = OpLabel
  1430. %24 = OpLoad %float %35
  1431. %25 = OpCompositeConstruct %v4float %24 %24 %24 %24
  1432. OpStore %color %25
  1433. %26 = OpLoad %v4float %color
  1434. OpStore %gl_FragColor %26
  1435. OpReturn
  1436. OpFunctionEnd
  1437. )";
  1438. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + before + nonEntryFuncs,
  1439. predefs + after + nonEntryFuncs,
  1440. false, true);
  1441. }
  1442. TEST_F(InlineTest, EarlyReturnNotAppearingLastInFunctionInlined) {
  1443. // Example from https://github.com/KhronosGroup/SPIRV-Tools/issues/755
  1444. //
  1445. // Original example is derived from:
  1446. //
  1447. // #version 450
  1448. //
  1449. // float foo() {
  1450. // if (true) {
  1451. // }
  1452. // }
  1453. //
  1454. // void main() { foo(); }
  1455. //
  1456. // But the order of basic blocks in foo is changed so that the return
  1457. // block is listed second-last. There is only one return in the callee
  1458. // but it does not appear last.
  1459. const std::string predefs =
  1460. R"(OpCapability Shader
  1461. OpMemoryModel Logical GLSL450
  1462. OpEntryPoint Vertex %main "main"
  1463. OpSource GLSL 450
  1464. OpName %main "main"
  1465. OpName %foo_ "foo("
  1466. %void = OpTypeVoid
  1467. %4 = OpTypeFunction %void
  1468. %bool = OpTypeBool
  1469. %true = OpConstantTrue %bool
  1470. )";
  1471. const std::string nonEntryFuncs =
  1472. R"(%foo_ = OpFunction %void None %4
  1473. %7 = OpLabel
  1474. OpSelectionMerge %8 None
  1475. OpBranchConditional %true %9 %8
  1476. %8 = OpLabel
  1477. OpReturn
  1478. %9 = OpLabel
  1479. OpBranch %8
  1480. OpFunctionEnd
  1481. )";
  1482. const std::string before =
  1483. R"(%main = OpFunction %void None %4
  1484. %10 = OpLabel
  1485. %11 = OpFunctionCall %void %foo_
  1486. OpReturn
  1487. OpFunctionEnd
  1488. )";
  1489. const std::string after =
  1490. R"(%main = OpFunction %void None %4
  1491. %10 = OpLabel
  1492. OpSelectionMerge %12 None
  1493. OpBranchConditional %true %13 %12
  1494. %12 = OpLabel
  1495. OpBranch %14
  1496. %13 = OpLabel
  1497. OpBranch %12
  1498. %14 = OpLabel
  1499. OpReturn
  1500. OpFunctionEnd
  1501. )";
  1502. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + nonEntryFuncs + before,
  1503. predefs + nonEntryFuncs + after,
  1504. false, true);
  1505. }
  1506. TEST_F(InlineTest, ForwardReferencesInPhiInlined) {
  1507. // The basic structure of the test case is like this:
  1508. //
  1509. // int foo() {
  1510. // int result = 1;
  1511. // if (true) {
  1512. // result = 1;
  1513. // }
  1514. // return result;
  1515. // }
  1516. //
  1517. // void main() {
  1518. // int x = foo();
  1519. // }
  1520. //
  1521. // but with modifications: Using Phi instead of load/store, and the
  1522. // return block in foo appears before the "then" block.
  1523. const std::string predefs =
  1524. R"(OpCapability Shader
  1525. %1 = OpExtInstImport "GLSL.std.450"
  1526. OpMemoryModel Logical GLSL450
  1527. OpEntryPoint Vertex %main "main"
  1528. OpSource GLSL 450
  1529. OpName %main "main"
  1530. OpName %foo_ "foo("
  1531. OpName %x "x"
  1532. %void = OpTypeVoid
  1533. %6 = OpTypeFunction %void
  1534. %int = OpTypeInt 32 1
  1535. %8 = OpTypeFunction %int
  1536. %bool = OpTypeBool
  1537. %true = OpConstantTrue %bool
  1538. %int_0 = OpConstant %int 0
  1539. %_ptr_Function_int = OpTypePointer Function %int
  1540. )";
  1541. const std::string nonEntryFuncs =
  1542. R"(%foo_ = OpFunction %int None %8
  1543. %13 = OpLabel
  1544. %14 = OpCopyObject %int %int_0
  1545. OpSelectionMerge %15 None
  1546. OpBranchConditional %true %16 %15
  1547. %15 = OpLabel
  1548. %17 = OpPhi %int %14 %13 %18 %16
  1549. OpReturnValue %17
  1550. %16 = OpLabel
  1551. %18 = OpCopyObject %int %int_0
  1552. OpBranch %15
  1553. OpFunctionEnd
  1554. )";
  1555. const std::string before =
  1556. R"(%main = OpFunction %void None %6
  1557. %19 = OpLabel
  1558. %x = OpVariable %_ptr_Function_int Function
  1559. %20 = OpFunctionCall %int %foo_
  1560. OpStore %x %20
  1561. OpReturn
  1562. OpFunctionEnd
  1563. )";
  1564. const std::string after =
  1565. R"(%main = OpFunction %void None %6
  1566. %19 = OpLabel
  1567. %21 = OpVariable %_ptr_Function_int Function
  1568. %x = OpVariable %_ptr_Function_int Function
  1569. %22 = OpCopyObject %int %int_0
  1570. OpSelectionMerge %23 None
  1571. OpBranchConditional %true %24 %23
  1572. %23 = OpLabel
  1573. %26 = OpPhi %int %22 %19 %25 %24
  1574. OpStore %21 %26
  1575. OpBranch %27
  1576. %24 = OpLabel
  1577. %25 = OpCopyObject %int %int_0
  1578. OpBranch %23
  1579. %27 = OpLabel
  1580. %20 = OpLoad %int %21
  1581. OpStore %x %20
  1582. OpReturn
  1583. OpFunctionEnd
  1584. )";
  1585. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + nonEntryFuncs + before,
  1586. predefs + nonEntryFuncs + after,
  1587. false, true);
  1588. }
  1589. TEST_F(InlineTest, EarlyReturnInLoopIsNotInlined) {
  1590. // #version 140
  1591. //
  1592. // in vec4 BaseColor;
  1593. //
  1594. // float foo(vec4 bar)
  1595. // {
  1596. // while (true) {
  1597. // if (bar.x < 0.0)
  1598. // return 0.0;
  1599. // return bar.x;
  1600. // }
  1601. // }
  1602. //
  1603. // void main()
  1604. // {
  1605. // vec4 color = vec4(foo(BaseColor));
  1606. // gl_FragColor = color;
  1607. // }
  1608. const std::string assembly =
  1609. R"(OpCapability Shader
  1610. %1 = OpExtInstImport "GLSL.std.450"
  1611. OpMemoryModel Logical GLSL450
  1612. OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
  1613. OpExecutionMode %main OriginUpperLeft
  1614. OpSource GLSL 140
  1615. OpName %main "main"
  1616. OpName %foo_vf4_ "foo(vf4;"
  1617. OpName %bar "bar"
  1618. OpName %color "color"
  1619. OpName %BaseColor "BaseColor"
  1620. OpName %param "param"
  1621. OpName %gl_FragColor "gl_FragColor"
  1622. %void = OpTypeVoid
  1623. %10 = OpTypeFunction %void
  1624. %float = OpTypeFloat 32
  1625. %v4float = OpTypeVector %float 4
  1626. %_ptr_Function_v4float = OpTypePointer Function %v4float
  1627. %14 = OpTypeFunction %float %_ptr_Function_v4float
  1628. %bool = OpTypeBool
  1629. %true = OpConstantTrue %bool
  1630. %uint = OpTypeInt 32 0
  1631. %uint_0 = OpConstant %uint 0
  1632. %_ptr_Function_float = OpTypePointer Function %float
  1633. %float_0 = OpConstant %float 0
  1634. %_ptr_Input_v4float = OpTypePointer Input %v4float
  1635. %BaseColor = OpVariable %_ptr_Input_v4float Input
  1636. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1637. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  1638. %main = OpFunction %void None %10
  1639. %23 = OpLabel
  1640. %color = OpVariable %_ptr_Function_v4float Function
  1641. %param = OpVariable %_ptr_Function_v4float Function
  1642. %24 = OpLoad %v4float %BaseColor
  1643. OpStore %param %24
  1644. %25 = OpFunctionCall %float %foo_vf4_ %param
  1645. %26 = OpCompositeConstruct %v4float %25 %25 %25 %25
  1646. OpStore %color %26
  1647. %27 = OpLoad %v4float %color
  1648. OpStore %gl_FragColor %27
  1649. OpReturn
  1650. OpFunctionEnd
  1651. %foo_vf4_ = OpFunction %float None %14
  1652. %bar = OpFunctionParameter %_ptr_Function_v4float
  1653. %28 = OpLabel
  1654. OpBranch %29
  1655. %29 = OpLabel
  1656. OpLoopMerge %30 %31 None
  1657. OpBranch %32
  1658. %32 = OpLabel
  1659. OpBranchConditional %true %33 %30
  1660. %33 = OpLabel
  1661. %34 = OpAccessChain %_ptr_Function_float %bar %uint_0
  1662. %35 = OpLoad %float %34
  1663. %36 = OpFOrdLessThan %bool %35 %float_0
  1664. OpSelectionMerge %37 None
  1665. OpBranchConditional %36 %38 %37
  1666. %38 = OpLabel
  1667. OpReturnValue %float_0
  1668. %37 = OpLabel
  1669. %39 = OpAccessChain %_ptr_Function_float %bar %uint_0
  1670. %40 = OpLoad %float %39
  1671. OpReturnValue %40
  1672. %31 = OpLabel
  1673. OpBranch %29
  1674. %30 = OpLabel
  1675. %41 = OpUndef %float
  1676. OpReturnValue %41
  1677. OpFunctionEnd
  1678. )";
  1679. SinglePassRunAndCheck<InlineExhaustivePass>(assembly, assembly, false, true);
  1680. }
  1681. TEST_F(InlineTest, ExternalFunctionIsNotInlined) {
  1682. // In particular, don't crash.
  1683. // See report https://github.com/KhronosGroup/SPIRV-Tools/issues/605
  1684. const std::string assembly =
  1685. R"(OpCapability Addresses
  1686. OpCapability Kernel
  1687. OpCapability Linkage
  1688. OpMemoryModel Physical32 OpenCL
  1689. OpEntryPoint Kernel %1 "entry_pt"
  1690. OpDecorate %2 LinkageAttributes "external" Import
  1691. %void = OpTypeVoid
  1692. %4 = OpTypeFunction %void
  1693. %2 = OpFunction %void None %4
  1694. OpFunctionEnd
  1695. %1 = OpFunction %void None %4
  1696. %5 = OpLabel
  1697. %6 = OpFunctionCall %void %2
  1698. OpReturn
  1699. OpFunctionEnd
  1700. )";
  1701. SinglePassRunAndCheck<InlineExhaustivePass>(assembly, assembly, false, true);
  1702. }
  1703. TEST_F(InlineTest, SingleBlockLoopCallsMultiBlockCallee) {
  1704. // Example from https://github.com/KhronosGroup/SPIRV-Tools/issues/787
  1705. //
  1706. // CFG structure is:
  1707. // foo:
  1708. // fooentry -> fooexit
  1709. //
  1710. // main:
  1711. // entry -> loop
  1712. // loop -> loop, merge
  1713. // loop calls foo()
  1714. // merge
  1715. //
  1716. // Since the callee has multiple blocks, it will split the calling block
  1717. // into at least two, resulting in a new "back-half" block that contains
  1718. // the instructions after the inlined function call. If the calling block
  1719. // has an OpLoopMerge that points back to the calling block itself, then
  1720. // the OpLoopMerge can't remain in the back-half block, but must be
  1721. // moved to the end of the original calling block, and it continue target
  1722. // operand updated to point to the back-half block.
  1723. const std::string predefs =
  1724. R"(OpCapability Shader
  1725. OpMemoryModel Logical GLSL450
  1726. OpEntryPoint GLCompute %1 "main"
  1727. OpSource OpenCL_C 120
  1728. %bool = OpTypeBool
  1729. %true = OpConstantTrue %bool
  1730. %void = OpTypeVoid
  1731. )";
  1732. const std::string nonEntryFuncs =
  1733. R"(%5 = OpTypeFunction %void
  1734. %6 = OpFunction %void None %5
  1735. %7 = OpLabel
  1736. OpBranch %8
  1737. %8 = OpLabel
  1738. OpReturn
  1739. OpFunctionEnd
  1740. )";
  1741. const std::string before =
  1742. R"(%1 = OpFunction %void None %5
  1743. %9 = OpLabel
  1744. OpBranch %10
  1745. %10 = OpLabel
  1746. %11 = OpFunctionCall %void %6
  1747. OpLoopMerge %12 %10 None
  1748. OpBranchConditional %true %10 %12
  1749. %12 = OpLabel
  1750. OpReturn
  1751. OpFunctionEnd
  1752. )";
  1753. const std::string after =
  1754. R"(%1 = OpFunction %void None %5
  1755. %9 = OpLabel
  1756. OpBranch %10
  1757. %10 = OpLabel
  1758. OpLoopMerge %12 %13 None
  1759. OpBranch %13
  1760. %13 = OpLabel
  1761. OpBranchConditional %true %10 %12
  1762. %12 = OpLabel
  1763. OpReturn
  1764. OpFunctionEnd
  1765. )";
  1766. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + nonEntryFuncs + before,
  1767. predefs + nonEntryFuncs + after,
  1768. false, true);
  1769. }
  1770. TEST_F(InlineTest, MultiBlockLoopHeaderCallsMultiBlockCallee) {
  1771. // Like SingleBlockLoopCallsMultiBlockCallee but the loop has several
  1772. // blocks, but the function call still occurs in the loop header.
  1773. // Example from https://github.com/KhronosGroup/SPIRV-Tools/issues/800
  1774. const std::string predefs =
  1775. R"(OpCapability Shader
  1776. OpMemoryModel Logical GLSL450
  1777. OpEntryPoint GLCompute %1 "main"
  1778. OpSource OpenCL_C 120
  1779. %bool = OpTypeBool
  1780. %true = OpConstantTrue %bool
  1781. %int = OpTypeInt 32 1
  1782. %int_1 = OpConstant %int 1
  1783. %int_2 = OpConstant %int 2
  1784. %int_3 = OpConstant %int 3
  1785. %int_4 = OpConstant %int 4
  1786. %int_5 = OpConstant %int 5
  1787. %void = OpTypeVoid
  1788. %11 = OpTypeFunction %void
  1789. )";
  1790. const std::string nonEntryFuncs =
  1791. R"(%12 = OpFunction %void None %11
  1792. %13 = OpLabel
  1793. %14 = OpCopyObject %int %int_1
  1794. OpBranch %15
  1795. %15 = OpLabel
  1796. %16 = OpCopyObject %int %int_2
  1797. OpReturn
  1798. OpFunctionEnd
  1799. )";
  1800. const std::string before =
  1801. R"(%1 = OpFunction %void None %11
  1802. %17 = OpLabel
  1803. OpBranch %18
  1804. %18 = OpLabel
  1805. %19 = OpCopyObject %int %int_3
  1806. %20 = OpFunctionCall %void %12
  1807. %21 = OpCopyObject %int %int_4
  1808. OpLoopMerge %22 %23 None
  1809. OpBranchConditional %true %23 %22
  1810. %23 = OpLabel
  1811. %24 = OpCopyObject %int %int_5
  1812. OpBranchConditional %true %18 %22
  1813. %22 = OpLabel
  1814. OpReturn
  1815. OpFunctionEnd
  1816. )";
  1817. const std::string after =
  1818. R"(%1 = OpFunction %void None %11
  1819. %17 = OpLabel
  1820. OpBranch %18
  1821. %18 = OpLabel
  1822. %19 = OpCopyObject %int %int_3
  1823. %25 = OpCopyObject %int %int_1
  1824. OpLoopMerge %22 %23 None
  1825. OpBranch %26
  1826. %26 = OpLabel
  1827. %27 = OpCopyObject %int %int_2
  1828. %21 = OpCopyObject %int %int_4
  1829. OpBranchConditional %true %23 %22
  1830. %23 = OpLabel
  1831. %24 = OpCopyObject %int %int_5
  1832. OpBranchConditional %true %18 %22
  1833. %22 = OpLabel
  1834. OpReturn
  1835. OpFunctionEnd
  1836. )";
  1837. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + nonEntryFuncs + before,
  1838. predefs + nonEntryFuncs + after,
  1839. false, true);
  1840. }
  1841. TEST_F(InlineTest, SingleBlockLoopCallsMultiBlockCalleeHavingSelectionMerge) {
  1842. // This is similar to SingleBlockLoopCallsMultiBlockCallee except
  1843. // that calleee block also has a merge instruction in its first block.
  1844. // That merge instruction must be an OpSelectionMerge (because the entry
  1845. // block of a function can't be the header of a loop since the entry
  1846. // block can't be the target of a branch).
  1847. //
  1848. // In this case the OpLoopMerge can't be placed in the same block as
  1849. // the OpSelectionMerge, so inlining must create a new block to contain
  1850. // the callee contents.
  1851. //
  1852. // Additionally, we have two dummy OpCopyObject instructions to prove that
  1853. // the OpLoopMerge is moved to the right location.
  1854. //
  1855. // Also ensure that OpPhis within the cloned callee code are valid.
  1856. // We need to test that the predecessor blocks are remapped correctly so that
  1857. // dominance rules are satisfied
  1858. const std::string predefs =
  1859. R"(OpCapability Shader
  1860. OpMemoryModel Logical GLSL450
  1861. OpEntryPoint GLCompute %1 "main"
  1862. OpSource OpenCL_C 120
  1863. %bool = OpTypeBool
  1864. %true = OpConstantTrue %bool
  1865. %false = OpConstantFalse %bool
  1866. %void = OpTypeVoid
  1867. %6 = OpTypeFunction %void
  1868. )";
  1869. // This callee has multiple blocks, and an OpPhi in the last block
  1870. // that references a value from the first block. This tests that
  1871. // cloned block IDs are remapped appropriately. The OpPhi dominance
  1872. // requires that the remapped %9 must be in a block that dominates
  1873. // the remapped %8.
  1874. const std::string nonEntryFuncs =
  1875. R"(%7 = OpFunction %void None %6
  1876. %8 = OpLabel
  1877. %9 = OpCopyObject %bool %true
  1878. OpSelectionMerge %10 None
  1879. OpBranchConditional %true %10 %10
  1880. %10 = OpLabel
  1881. %11 = OpPhi %bool %9 %8
  1882. OpReturn
  1883. OpFunctionEnd
  1884. )";
  1885. const std::string before =
  1886. R"(%1 = OpFunction %void None %6
  1887. %12 = OpLabel
  1888. OpBranch %13
  1889. %13 = OpLabel
  1890. %14 = OpCopyObject %bool %false
  1891. %15 = OpFunctionCall %void %7
  1892. OpLoopMerge %16 %13 None
  1893. OpBranchConditional %true %13 %16
  1894. %16 = OpLabel
  1895. OpReturn
  1896. OpFunctionEnd
  1897. )";
  1898. // Note the remapped Phi uses %17 as the parent instead
  1899. // of %13, demonstrating that the parent block has been remapped
  1900. // correctly.
  1901. const std::string after =
  1902. R"(%1 = OpFunction %void None %6
  1903. %12 = OpLabel
  1904. OpBranch %13
  1905. %13 = OpLabel
  1906. %14 = OpCopyObject %bool %false
  1907. OpLoopMerge %16 %19 None
  1908. OpBranch %17
  1909. %17 = OpLabel
  1910. %18 = OpCopyObject %bool %true
  1911. OpSelectionMerge %19 None
  1912. OpBranchConditional %true %19 %19
  1913. %19 = OpLabel
  1914. %20 = OpPhi %bool %18 %17
  1915. OpBranchConditional %true %13 %16
  1916. %16 = OpLabel
  1917. OpReturn
  1918. OpFunctionEnd
  1919. )";
  1920. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + nonEntryFuncs + before,
  1921. predefs + nonEntryFuncs + after,
  1922. false, true);
  1923. }
  1924. TEST_F(InlineTest,
  1925. MultiBlockLoopHeaderCallsFromToMultiBlockCalleeHavingSelectionMerge) {
  1926. // This is similar to SingleBlockLoopCallsMultiBlockCalleeHavingSelectionMerge
  1927. // but the call is in the header block of a multi block loop.
  1928. const std::string predefs =
  1929. R"(OpCapability Shader
  1930. OpMemoryModel Logical GLSL450
  1931. OpEntryPoint GLCompute %1 "main"
  1932. OpSource OpenCL_C 120
  1933. %bool = OpTypeBool
  1934. %true = OpConstantTrue %bool
  1935. %int = OpTypeInt 32 1
  1936. %int_1 = OpConstant %int 1
  1937. %int_2 = OpConstant %int 2
  1938. %int_3 = OpConstant %int 3
  1939. %int_4 = OpConstant %int 4
  1940. %int_5 = OpConstant %int 5
  1941. %void = OpTypeVoid
  1942. %11 = OpTypeFunction %void
  1943. )";
  1944. const std::string nonEntryFuncs =
  1945. R"(%12 = OpFunction %void None %11
  1946. %13 = OpLabel
  1947. %14 = OpCopyObject %int %int_1
  1948. OpSelectionMerge %15 None
  1949. OpBranchConditional %true %15 %15
  1950. %15 = OpLabel
  1951. %16 = OpCopyObject %int %int_2
  1952. OpReturn
  1953. OpFunctionEnd
  1954. )";
  1955. const std::string before =
  1956. R"(%1 = OpFunction %void None %11
  1957. %17 = OpLabel
  1958. OpBranch %18
  1959. %18 = OpLabel
  1960. %19 = OpCopyObject %int %int_3
  1961. %20 = OpFunctionCall %void %12
  1962. %21 = OpCopyObject %int %int_4
  1963. OpLoopMerge %22 %23 None
  1964. OpBranchConditional %true %23 %22
  1965. %23 = OpLabel
  1966. %24 = OpCopyObject %int %int_5
  1967. OpBranchConditional %true %18 %22
  1968. %22 = OpLabel
  1969. OpReturn
  1970. OpFunctionEnd
  1971. )";
  1972. const std::string after =
  1973. R"(%1 = OpFunction %void None %11
  1974. %17 = OpLabel
  1975. OpBranch %18
  1976. %18 = OpLabel
  1977. %19 = OpCopyObject %int %int_3
  1978. OpLoopMerge %22 %23 None
  1979. OpBranch %25
  1980. %25 = OpLabel
  1981. %26 = OpCopyObject %int %int_1
  1982. OpSelectionMerge %27 None
  1983. OpBranchConditional %true %27 %27
  1984. %27 = OpLabel
  1985. %28 = OpCopyObject %int %int_2
  1986. %21 = OpCopyObject %int %int_4
  1987. OpBranchConditional %true %23 %22
  1988. %23 = OpLabel
  1989. %24 = OpCopyObject %int %int_5
  1990. OpBranchConditional %true %18 %22
  1991. %22 = OpLabel
  1992. OpReturn
  1993. OpFunctionEnd
  1994. )";
  1995. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + nonEntryFuncs + before,
  1996. predefs + nonEntryFuncs + after,
  1997. false, true);
  1998. }
  1999. TEST_F(
  2000. InlineTest,
  2001. SingleBlockLoopCallsMultiBlockCalleeHavingSelectionMergeAndMultiReturns) {
  2002. // This is similar to SingleBlockLoopCallsMultiBlockCalleeHavingSelectionMerge
  2003. // except that in addition to starting with a selection header, the
  2004. // callee also has multi returns.
  2005. //
  2006. // So now we have to accommodate:
  2007. // - The caller's OpLoopMerge (which must move to the first block)
  2008. // - The single-trip loop to wrap the multi returns, and
  2009. // - The callee's selection merge in its first block.
  2010. // Each of these must go into their own blocks.
  2011. const std::string predefs =
  2012. R"(OpCapability Shader
  2013. OpMemoryModel Logical GLSL450
  2014. OpEntryPoint GLCompute %1 "main"
  2015. OpSource OpenCL_C 120
  2016. %bool = OpTypeBool
  2017. %int = OpTypeInt 32 1
  2018. %true = OpConstantTrue %bool
  2019. %false = OpConstantFalse %bool
  2020. %int_0 = OpConstant %int 0
  2021. %int_1 = OpConstant %int 1
  2022. %int_2 = OpConstant %int 2
  2023. %int_3 = OpConstant %int 3
  2024. %int_4 = OpConstant %int 4
  2025. %void = OpTypeVoid
  2026. %12 = OpTypeFunction %void
  2027. )";
  2028. const std::string nonEntryFuncs =
  2029. R"(%13 = OpFunction %void None %12
  2030. %14 = OpLabel
  2031. %15 = OpCopyObject %int %int_0
  2032. OpReturn
  2033. %16 = OpLabel
  2034. %17 = OpCopyObject %int %int_1
  2035. OpReturn
  2036. OpFunctionEnd
  2037. )";
  2038. const std::string before =
  2039. R"(%1 = OpFunction %void None %12
  2040. %18 = OpLabel
  2041. OpBranch %19
  2042. %19 = OpLabel
  2043. %20 = OpCopyObject %int %int_2
  2044. %21 = OpFunctionCall %void %13
  2045. %22 = OpCopyObject %int %int_3
  2046. OpLoopMerge %23 %19 None
  2047. OpBranchConditional %true %19 %23
  2048. %23 = OpLabel
  2049. %24 = OpCopyObject %int %int_4
  2050. OpReturn
  2051. OpFunctionEnd
  2052. )";
  2053. const std::string after =
  2054. R"(%1 = OpFunction %void None %12
  2055. %18 = OpLabel
  2056. OpBranch %19
  2057. %19 = OpLabel
  2058. %20 = OpCopyObject %int %int_2
  2059. %25 = OpCopyObject %int %int_0
  2060. OpLoopMerge %23 %26 None
  2061. OpBranch %26
  2062. %27 = OpLabel
  2063. %28 = OpCopyObject %int %int_1
  2064. OpBranch %26
  2065. %26 = OpLabel
  2066. %22 = OpCopyObject %int %int_3
  2067. OpBranchConditional %true %19 %23
  2068. %23 = OpLabel
  2069. %24 = OpCopyObject %int %int_4
  2070. OpReturn
  2071. OpFunctionEnd
  2072. )";
  2073. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + nonEntryFuncs + before,
  2074. predefs + nonEntryFuncs + after,
  2075. false, true);
  2076. }
  2077. TEST_F(InlineTest, CalleeWithMultiReturnAndPhiRequiresEntryBlockRemapping) {
  2078. // The case from https://github.com/KhronosGroup/SPIRV-Tools/issues/790
  2079. //
  2080. // The callee has multiple returns, and so must be wrapped with a single-trip
  2081. // loop. That code must remap the callee entry block ID to the introduced
  2082. // loop body's ID. Otherwise you can get a dominance error in a cloned OpPhi.
  2083. const std::string predefs =
  2084. R"(OpCapability Shader
  2085. OpMemoryModel Logical GLSL450
  2086. OpEntryPoint GLCompute %1 "main"
  2087. OpSource OpenCL_C 120
  2088. %int = OpTypeInt 32 1
  2089. %int_0 = OpConstant %int 0
  2090. %int_1 = OpConstant %int 1
  2091. %int_2 = OpConstant %int 2
  2092. %int_3 = OpConstant %int 3
  2093. %int_4 = OpConstant %int 4
  2094. %void = OpTypeVoid
  2095. %9 = OpTypeFunction %void
  2096. %bool = OpTypeBool
  2097. %false = OpConstantFalse %bool
  2098. )";
  2099. // This callee has multiple returns, and a Phi in the second block referencing
  2100. // a value generated in the entry block.
  2101. const std::string nonEntryFuncs =
  2102. R"(%12 = OpFunction %void None %9
  2103. %13 = OpLabel
  2104. %14 = OpCopyObject %int %int_0
  2105. OpBranch %15
  2106. %15 = OpLabel
  2107. %16 = OpPhi %int %14 %13
  2108. %17 = OpCopyObject %int %int_1
  2109. OpReturn
  2110. %18 = OpLabel
  2111. %19 = OpCopyObject %int %int_2
  2112. OpReturn
  2113. OpFunctionEnd
  2114. )";
  2115. const std::string before =
  2116. R"(%1 = OpFunction %void None %9
  2117. %20 = OpLabel
  2118. %21 = OpCopyObject %int %int_3
  2119. %22 = OpFunctionCall %void %12
  2120. %23 = OpCopyObject %int %int_4
  2121. OpReturn
  2122. OpFunctionEnd
  2123. )";
  2124. const std::string after =
  2125. R"(%1 = OpFunction %void None %9
  2126. %20 = OpLabel
  2127. %21 = OpCopyObject %int %int_3
  2128. %24 = OpCopyObject %int %int_0
  2129. OpBranch %25
  2130. %25 = OpLabel
  2131. %26 = OpPhi %int %24 %20
  2132. %27 = OpCopyObject %int %int_1
  2133. OpBranch %28
  2134. %29 = OpLabel
  2135. %30 = OpCopyObject %int %int_2
  2136. OpBranch %28
  2137. %28 = OpLabel
  2138. %23 = OpCopyObject %int %int_4
  2139. OpReturn
  2140. OpFunctionEnd
  2141. )";
  2142. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + nonEntryFuncs + before,
  2143. predefs + nonEntryFuncs + after,
  2144. false, true);
  2145. }
  2146. TEST_F(InlineTest, NonInlinableCalleeWithSingleReturn) {
  2147. // The case from https://github.com/KhronosGroup/SPIRV-Tools/issues/2018
  2148. //
  2149. // The callee has a single return, but cannot be inlined because the
  2150. // return is inside a loop.
  2151. const std::string predefs =
  2152. R"(OpCapability Shader
  2153. %1 = OpExtInstImport "GLSL.std.450"
  2154. OpMemoryModel Logical GLSL450
  2155. OpEntryPoint Fragment %main "main" %_GLF_color
  2156. OpExecutionMode %main OriginUpperLeft
  2157. OpSource ESSL 310
  2158. OpName %main "main"
  2159. OpName %f_ "f("
  2160. OpName %i "i"
  2161. OpName %_GLF_color "_GLF_color"
  2162. OpDecorate %_GLF_color Location 0
  2163. %void = OpTypeVoid
  2164. %7 = OpTypeFunction %void
  2165. %float = OpTypeFloat 32
  2166. %9 = OpTypeFunction %float
  2167. %float_1 = OpConstant %float 1
  2168. %bool = OpTypeBool
  2169. %false = OpConstantFalse %bool
  2170. %int = OpTypeInt 32 1
  2171. %_ptr_Function_int = OpTypePointer Function %int
  2172. %int_0 = OpConstant %int 0
  2173. %int_1 = OpConstant %int 1
  2174. %v4float = OpTypeVector %float 4
  2175. %_ptr_Output_v4float = OpTypePointer Output %v4float
  2176. %_GLF_color = OpVariable %_ptr_Output_v4float Output
  2177. %float_0 = OpConstant %float 0
  2178. %20 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
  2179. %21 = OpConstantComposite %v4float %float_0 %float_1 %float_0 %float_1
  2180. )";
  2181. const std::string caller =
  2182. R"(%main = OpFunction %void None %7
  2183. %22 = OpLabel
  2184. %i = OpVariable %_ptr_Function_int Function
  2185. OpStore %i %int_0
  2186. OpBranch %23
  2187. %23 = OpLabel
  2188. OpLoopMerge %24 %25 None
  2189. OpBranch %26
  2190. %26 = OpLabel
  2191. %27 = OpLoad %int %i
  2192. %28 = OpSLessThan %bool %27 %int_1
  2193. OpBranchConditional %28 %29 %24
  2194. %29 = OpLabel
  2195. OpStore %_GLF_color %20
  2196. %30 = OpFunctionCall %float %f_
  2197. OpBranch %25
  2198. %25 = OpLabel
  2199. %31 = OpLoad %int %i
  2200. %32 = OpIAdd %int %31 %int_1
  2201. OpStore %i %32
  2202. OpBranch %23
  2203. %24 = OpLabel
  2204. OpStore %_GLF_color %21
  2205. OpReturn
  2206. OpFunctionEnd
  2207. )";
  2208. const std::string callee =
  2209. R"(%f_ = OpFunction %float None %9
  2210. %33 = OpLabel
  2211. OpBranch %34
  2212. %34 = OpLabel
  2213. OpLoopMerge %35 %36 None
  2214. OpBranch %37
  2215. %37 = OpLabel
  2216. OpReturnValue %float_1
  2217. %36 = OpLabel
  2218. OpBranch %34
  2219. %35 = OpLabel
  2220. OpUnreachable
  2221. OpFunctionEnd
  2222. )";
  2223. SinglePassRunAndCheck<InlineExhaustivePass>(
  2224. predefs + caller + callee, predefs + caller + callee, false, true);
  2225. }
  2226. TEST_F(InlineTest, CalleeWithSingleReturnNeedsSingleTripLoopWrapper) {
  2227. // The case from https://github.com/KhronosGroup/SPIRV-Tools/issues/2018
  2228. //
  2229. // The callee has a single return, but needs single-trip loop wrapper
  2230. // to be inlined because the return is in a selection structure.
  2231. const std::string predefs =
  2232. R"(OpCapability Shader
  2233. %1 = OpExtInstImport "GLSL.std.450"
  2234. OpMemoryModel Logical GLSL450
  2235. OpEntryPoint Fragment %main "main" %_GLF_color
  2236. OpExecutionMode %main OriginUpperLeft
  2237. OpSource ESSL 310
  2238. OpName %main "main"
  2239. OpName %f_ "f("
  2240. OpName %i "i"
  2241. OpName %_GLF_color "_GLF_color"
  2242. OpDecorate %_GLF_color Location 0
  2243. %void = OpTypeVoid
  2244. %7 = OpTypeFunction %void
  2245. %float = OpTypeFloat 32
  2246. %9 = OpTypeFunction %float
  2247. %float_1 = OpConstant %float 1
  2248. %bool = OpTypeBool
  2249. %false = OpConstantFalse %bool
  2250. %true = OpConstantTrue %bool
  2251. %int = OpTypeInt 32 1
  2252. %_ptr_Function_int = OpTypePointer Function %int
  2253. %int_0 = OpConstant %int 0
  2254. %int_1 = OpConstant %int 1
  2255. %v4float = OpTypeVector %float 4
  2256. %_ptr_Output_v4float = OpTypePointer Output %v4float
  2257. %_GLF_color = OpVariable %_ptr_Output_v4float Output
  2258. %float_0 = OpConstant %float 0
  2259. %21 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
  2260. %22 = OpConstantComposite %v4float %float_0 %float_1 %float_0 %float_1
  2261. )";
  2262. const std::string new_predefs =
  2263. R"(%_ptr_Function_float = OpTypePointer Function %float
  2264. )";
  2265. const std::string main_before =
  2266. R"(%main = OpFunction %void None %7
  2267. %23 = OpLabel
  2268. %i = OpVariable %_ptr_Function_int Function
  2269. OpStore %i %int_0
  2270. OpBranch %24
  2271. %24 = OpLabel
  2272. OpLoopMerge %25 %26 None
  2273. OpBranch %27
  2274. %27 = OpLabel
  2275. %28 = OpLoad %int %i
  2276. %29 = OpSLessThan %bool %28 %int_1
  2277. OpBranchConditional %29 %30 %25
  2278. %30 = OpLabel
  2279. OpStore %_GLF_color %21
  2280. %31 = OpFunctionCall %float %f_
  2281. OpBranch %26
  2282. %26 = OpLabel
  2283. %32 = OpLoad %int %i
  2284. %33 = OpIAdd %int %32 %int_1
  2285. OpStore %i %33
  2286. OpBranch %24
  2287. %25 = OpLabel
  2288. OpStore %_GLF_color %22
  2289. OpReturn
  2290. OpFunctionEnd
  2291. )";
  2292. const std::string main_after =
  2293. R"(%main = OpFunction %void None %7
  2294. %23 = OpLabel
  2295. %38 = OpVariable %_ptr_Function_float Function
  2296. %i = OpVariable %_ptr_Function_int Function
  2297. OpStore %i %int_0
  2298. OpBranch %24
  2299. %24 = OpLabel
  2300. OpLoopMerge %25 %26 None
  2301. OpBranch %27
  2302. %27 = OpLabel
  2303. %28 = OpLoad %int %i
  2304. %29 = OpSLessThan %bool %28 %int_1
  2305. OpBranchConditional %29 %30 %25
  2306. %30 = OpLabel
  2307. OpStore %_GLF_color %21
  2308. OpBranch %39
  2309. %39 = OpLabel
  2310. OpLoopMerge %40 %41 None
  2311. OpBranch %42
  2312. %42 = OpLabel
  2313. OpSelectionMerge %43 None
  2314. OpBranchConditional %true %44 %43
  2315. %44 = OpLabel
  2316. OpStore %38 %float_1
  2317. OpBranch %40
  2318. %43 = OpLabel
  2319. OpStore %38 %float_1
  2320. OpBranch %40
  2321. %41 = OpLabel
  2322. OpBranchConditional %false %39 %40
  2323. %40 = OpLabel
  2324. %31 = OpLoad %float %38
  2325. OpBranch %26
  2326. %26 = OpLabel
  2327. %32 = OpLoad %int %i
  2328. %33 = OpIAdd %int %32 %int_1
  2329. OpStore %i %33
  2330. OpBranch %24
  2331. %25 = OpLabel
  2332. OpStore %_GLF_color %22
  2333. OpReturn
  2334. OpFunctionEnd
  2335. )";
  2336. const std::string callee =
  2337. R"(%f_ = OpFunction %float None %9
  2338. %34 = OpLabel
  2339. OpSelectionMerge %35 None
  2340. OpBranchConditional %true %36 %35
  2341. %36 = OpLabel
  2342. OpReturnValue %float_1
  2343. %35 = OpLabel
  2344. OpReturnValue %float_1
  2345. OpFunctionEnd
  2346. )";
  2347. SinglePassRunAndCheck<InlineExhaustivePass>(
  2348. predefs + main_before + callee,
  2349. predefs + new_predefs + main_after + callee, false, true);
  2350. }
  2351. TEST_F(InlineTest, Decorated1) {
  2352. // Same test as Simple with the difference
  2353. // that OpFAdd in the outlined function is
  2354. // decorated with RelaxedPrecision
  2355. // Expected result is an equal decoration
  2356. // of the corresponding inlined instruction
  2357. //
  2358. // #version 140
  2359. //
  2360. // in vec4 BaseColor;
  2361. //
  2362. // float foo(vec4 bar)
  2363. // {
  2364. // return bar.x + bar.y;
  2365. // }
  2366. //
  2367. // void main()
  2368. // {
  2369. // vec4 color = vec4(foo(BaseColor));
  2370. // gl_FragColor = color;
  2371. // }
  2372. const std::string predefs =
  2373. R"(OpCapability Shader
  2374. %1 = OpExtInstImport "GLSL.std.450"
  2375. OpMemoryModel Logical GLSL450
  2376. OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
  2377. OpExecutionMode %main OriginUpperLeft
  2378. OpSource GLSL 140
  2379. OpName %main "main"
  2380. OpName %foo_vf4_ "foo(vf4;"
  2381. OpName %bar "bar"
  2382. OpName %color "color"
  2383. OpName %BaseColor "BaseColor"
  2384. OpName %param "param"
  2385. OpName %gl_FragColor "gl_FragColor"
  2386. OpDecorate %9 RelaxedPrecision
  2387. )";
  2388. const std::string before =
  2389. R"(%void = OpTypeVoid
  2390. %11 = OpTypeFunction %void
  2391. %float = OpTypeFloat 32
  2392. %v4float = OpTypeVector %float 4
  2393. %_ptr_Function_v4float = OpTypePointer Function %v4float
  2394. %15 = OpTypeFunction %float %_ptr_Function_v4float
  2395. %uint = OpTypeInt 32 0
  2396. %uint_0 = OpConstant %uint 0
  2397. %_ptr_Function_float = OpTypePointer Function %float
  2398. %uint_1 = OpConstant %uint 1
  2399. %_ptr_Input_v4float = OpTypePointer Input %v4float
  2400. %BaseColor = OpVariable %_ptr_Input_v4float Input
  2401. %_ptr_Output_v4float = OpTypePointer Output %v4float
  2402. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  2403. %main = OpFunction %void None %11
  2404. %22 = OpLabel
  2405. %color = OpVariable %_ptr_Function_v4float Function
  2406. %param = OpVariable %_ptr_Function_v4float Function
  2407. %23 = OpLoad %v4float %BaseColor
  2408. OpStore %param %23
  2409. %24 = OpFunctionCall %float %foo_vf4_ %param
  2410. %25 = OpCompositeConstruct %v4float %24 %24 %24 %24
  2411. OpStore %color %25
  2412. %26 = OpLoad %v4float %color
  2413. OpStore %gl_FragColor %26
  2414. OpReturn
  2415. OpFunctionEnd
  2416. )";
  2417. const std::string after =
  2418. R"(OpDecorate %37 RelaxedPrecision
  2419. %void = OpTypeVoid
  2420. %11 = OpTypeFunction %void
  2421. %float = OpTypeFloat 32
  2422. %v4float = OpTypeVector %float 4
  2423. %_ptr_Function_v4float = OpTypePointer Function %v4float
  2424. %15 = OpTypeFunction %float %_ptr_Function_v4float
  2425. %uint = OpTypeInt 32 0
  2426. %uint_0 = OpConstant %uint 0
  2427. %_ptr_Function_float = OpTypePointer Function %float
  2428. %uint_1 = OpConstant %uint 1
  2429. %_ptr_Input_v4float = OpTypePointer Input %v4float
  2430. %BaseColor = OpVariable %_ptr_Input_v4float Input
  2431. %_ptr_Output_v4float = OpTypePointer Output %v4float
  2432. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  2433. %main = OpFunction %void None %11
  2434. %22 = OpLabel
  2435. %32 = OpVariable %_ptr_Function_float Function
  2436. %color = OpVariable %_ptr_Function_v4float Function
  2437. %param = OpVariable %_ptr_Function_v4float Function
  2438. %23 = OpLoad %v4float %BaseColor
  2439. OpStore %param %23
  2440. %33 = OpAccessChain %_ptr_Function_float %param %uint_0
  2441. %34 = OpLoad %float %33
  2442. %35 = OpAccessChain %_ptr_Function_float %param %uint_1
  2443. %36 = OpLoad %float %35
  2444. %37 = OpFAdd %float %34 %36
  2445. OpStore %32 %37
  2446. %24 = OpLoad %float %32
  2447. %25 = OpCompositeConstruct %v4float %24 %24 %24 %24
  2448. OpStore %color %25
  2449. %26 = OpLoad %v4float %color
  2450. OpStore %gl_FragColor %26
  2451. OpReturn
  2452. OpFunctionEnd
  2453. )";
  2454. const std::string nonEntryFuncs =
  2455. R"(%foo_vf4_ = OpFunction %float None %15
  2456. %bar = OpFunctionParameter %_ptr_Function_v4float
  2457. %27 = OpLabel
  2458. %28 = OpAccessChain %_ptr_Function_float %bar %uint_0
  2459. %29 = OpLoad %float %28
  2460. %30 = OpAccessChain %_ptr_Function_float %bar %uint_1
  2461. %31 = OpLoad %float %30
  2462. %9 = OpFAdd %float %29 %31
  2463. OpReturnValue %9
  2464. OpFunctionEnd
  2465. )";
  2466. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + before + nonEntryFuncs,
  2467. predefs + after + nonEntryFuncs,
  2468. false, true);
  2469. }
  2470. TEST_F(InlineTest, Decorated2) {
  2471. // Same test as Simple with the difference
  2472. // that the Result <id> of the outlined OpFunction
  2473. // is decorated with RelaxedPrecision
  2474. // Expected result is an equal decoration
  2475. // of the created return variable
  2476. //
  2477. // #version 140
  2478. //
  2479. // in vec4 BaseColor;
  2480. //
  2481. // float foo(vec4 bar)
  2482. // {
  2483. // return bar.x + bar.y;
  2484. // }
  2485. //
  2486. // void main()
  2487. // {
  2488. // vec4 color = vec4(foo(BaseColor));
  2489. // gl_FragColor = color;
  2490. // }
  2491. const std::string predefs =
  2492. R"(OpCapability Shader
  2493. %1 = OpExtInstImport "GLSL.std.450"
  2494. OpMemoryModel Logical GLSL450
  2495. OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
  2496. OpExecutionMode %main OriginUpperLeft
  2497. OpSource GLSL 140
  2498. OpName %main "main"
  2499. OpName %foo_vf4_ "foo(vf4;"
  2500. OpName %bar "bar"
  2501. OpName %color "color"
  2502. OpName %BaseColor "BaseColor"
  2503. OpName %param "param"
  2504. OpName %gl_FragColor "gl_FragColor"
  2505. OpDecorate %foo_vf4_ RelaxedPrecision
  2506. )";
  2507. const std::string before =
  2508. R"(%void = OpTypeVoid
  2509. %10 = OpTypeFunction %void
  2510. %float = OpTypeFloat 32
  2511. %v4float = OpTypeVector %float 4
  2512. %_ptr_Function_v4float = OpTypePointer Function %v4float
  2513. %14 = OpTypeFunction %float %_ptr_Function_v4float
  2514. %uint = OpTypeInt 32 0
  2515. %uint_0 = OpConstant %uint 0
  2516. %_ptr_Function_float = OpTypePointer Function %float
  2517. %uint_1 = OpConstant %uint 1
  2518. %_ptr_Input_v4float = OpTypePointer Input %v4float
  2519. %BaseColor = OpVariable %_ptr_Input_v4float Input
  2520. %_ptr_Output_v4float = OpTypePointer Output %v4float
  2521. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  2522. %main = OpFunction %void None %10
  2523. %21 = OpLabel
  2524. %color = OpVariable %_ptr_Function_v4float Function
  2525. %param = OpVariable %_ptr_Function_v4float Function
  2526. %22 = OpLoad %v4float %BaseColor
  2527. OpStore %param %22
  2528. %23 = OpFunctionCall %float %foo_vf4_ %param
  2529. %24 = OpCompositeConstruct %v4float %23 %23 %23 %23
  2530. OpStore %color %24
  2531. %25 = OpLoad %v4float %color
  2532. OpStore %gl_FragColor %25
  2533. OpReturn
  2534. OpFunctionEnd
  2535. )";
  2536. const std::string after =
  2537. R"(OpDecorate %32 RelaxedPrecision
  2538. %void = OpTypeVoid
  2539. %10 = OpTypeFunction %void
  2540. %float = OpTypeFloat 32
  2541. %v4float = OpTypeVector %float 4
  2542. %_ptr_Function_v4float = OpTypePointer Function %v4float
  2543. %14 = OpTypeFunction %float %_ptr_Function_v4float
  2544. %uint = OpTypeInt 32 0
  2545. %uint_0 = OpConstant %uint 0
  2546. %_ptr_Function_float = OpTypePointer Function %float
  2547. %uint_1 = OpConstant %uint 1
  2548. %_ptr_Input_v4float = OpTypePointer Input %v4float
  2549. %BaseColor = OpVariable %_ptr_Input_v4float Input
  2550. %_ptr_Output_v4float = OpTypePointer Output %v4float
  2551. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  2552. %main = OpFunction %void None %10
  2553. %21 = OpLabel
  2554. %32 = OpVariable %_ptr_Function_float Function
  2555. %color = OpVariable %_ptr_Function_v4float Function
  2556. %param = OpVariable %_ptr_Function_v4float Function
  2557. %22 = OpLoad %v4float %BaseColor
  2558. OpStore %param %22
  2559. %33 = OpAccessChain %_ptr_Function_float %param %uint_0
  2560. %34 = OpLoad %float %33
  2561. %35 = OpAccessChain %_ptr_Function_float %param %uint_1
  2562. %36 = OpLoad %float %35
  2563. %37 = OpFAdd %float %34 %36
  2564. OpStore %32 %37
  2565. %23 = OpLoad %float %32
  2566. %24 = OpCompositeConstruct %v4float %23 %23 %23 %23
  2567. OpStore %color %24
  2568. %25 = OpLoad %v4float %color
  2569. OpStore %gl_FragColor %25
  2570. OpReturn
  2571. OpFunctionEnd
  2572. )";
  2573. const std::string nonEntryFuncs =
  2574. R"(%foo_vf4_ = OpFunction %float None %14
  2575. %bar = OpFunctionParameter %_ptr_Function_v4float
  2576. %26 = OpLabel
  2577. %27 = OpAccessChain %_ptr_Function_float %bar %uint_0
  2578. %28 = OpLoad %float %27
  2579. %29 = OpAccessChain %_ptr_Function_float %bar %uint_1
  2580. %30 = OpLoad %float %29
  2581. %31 = OpFAdd %float %28 %30
  2582. OpReturnValue %31
  2583. OpFunctionEnd
  2584. )";
  2585. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + before + nonEntryFuncs,
  2586. predefs + after + nonEntryFuncs,
  2587. false, true);
  2588. }
  2589. TEST_F(InlineTest, DeleteName) {
  2590. // Test that the name of the result id of the call is deleted.
  2591. const std::string before =
  2592. R"(
  2593. OpCapability Shader
  2594. OpMemoryModel Logical GLSL450
  2595. OpEntryPoint Vertex %main "main"
  2596. OpName %main "main"
  2597. OpName %main_entry "main_entry"
  2598. OpName %foo_result "foo_result"
  2599. OpName %void_fn "void_fn"
  2600. OpName %foo "foo"
  2601. OpName %foo_entry "foo_entry"
  2602. %void = OpTypeVoid
  2603. %void_fn = OpTypeFunction %void
  2604. %foo = OpFunction %void None %void_fn
  2605. %foo_entry = OpLabel
  2606. OpReturn
  2607. OpFunctionEnd
  2608. %main = OpFunction %void None %void_fn
  2609. %main_entry = OpLabel
  2610. %foo_result = OpFunctionCall %void %foo
  2611. OpReturn
  2612. OpFunctionEnd
  2613. )";
  2614. const std::string after =
  2615. R"(OpCapability Shader
  2616. OpMemoryModel Logical GLSL450
  2617. OpEntryPoint Vertex %main "main"
  2618. OpName %main "main"
  2619. OpName %main_entry "main_entry"
  2620. OpName %void_fn "void_fn"
  2621. OpName %foo "foo"
  2622. OpName %foo_entry "foo_entry"
  2623. %void = OpTypeVoid
  2624. %void_fn = OpTypeFunction %void
  2625. %foo = OpFunction %void None %void_fn
  2626. %foo_entry = OpLabel
  2627. OpReturn
  2628. OpFunctionEnd
  2629. %main = OpFunction %void None %void_fn
  2630. %main_entry = OpLabel
  2631. OpReturn
  2632. OpFunctionEnd
  2633. )";
  2634. SinglePassRunAndCheck<InlineExhaustivePass>(before, after, false, true);
  2635. }
  2636. TEST_F(InlineTest, SetParent) {
  2637. // Test that after inlining all basic blocks have the correct parent.
  2638. const std::string text =
  2639. R"(
  2640. OpCapability Shader
  2641. OpMemoryModel Logical GLSL450
  2642. OpEntryPoint Vertex %main "main"
  2643. OpName %main "main"
  2644. OpName %main_entry "main_entry"
  2645. OpName %foo_result "foo_result"
  2646. OpName %void_fn "void_fn"
  2647. OpName %foo "foo"
  2648. OpName %foo_entry "foo_entry"
  2649. %void = OpTypeVoid
  2650. %void_fn = OpTypeFunction %void
  2651. %foo = OpFunction %void None %void_fn
  2652. %foo_entry = OpLabel
  2653. OpReturn
  2654. OpFunctionEnd
  2655. %main = OpFunction %void None %void_fn
  2656. %main_entry = OpLabel
  2657. %foo_result = OpFunctionCall %void %foo
  2658. OpReturn
  2659. OpFunctionEnd
  2660. )";
  2661. std::unique_ptr<IRContext> context =
  2662. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  2663. InlineExhaustivePass pass;
  2664. pass.Run(context.get());
  2665. for (Function& func : *context->module()) {
  2666. for (BasicBlock& bb : func) {
  2667. EXPECT_TRUE(bb.GetParent() == &func);
  2668. }
  2669. }
  2670. }
  2671. TEST_F(InlineTest, OpKill) {
  2672. const std::string text = R"(
  2673. ; CHECK: OpFunction
  2674. ; CHECK-NEXT: OpLabel
  2675. ; CHECK-NEXT: OpKill
  2676. ; CHECK-NEXT: OpLabel
  2677. ; CHECK-NEXT: OpReturn
  2678. ; CHECK-NEXT: OpFunctionEnd
  2679. OpCapability Shader
  2680. OpMemoryModel Logical GLSL450
  2681. OpEntryPoint Fragment %main "main"
  2682. OpExecutionMode %main OriginUpperLeft
  2683. %void = OpTypeVoid
  2684. %voidfuncty = OpTypeFunction %void
  2685. %main = OpFunction %void None %voidfuncty
  2686. %1 = OpLabel
  2687. %2 = OpFunctionCall %void %func
  2688. OpReturn
  2689. OpFunctionEnd
  2690. %func = OpFunction %void None %voidfuncty
  2691. %3 = OpLabel
  2692. OpKill
  2693. OpFunctionEnd
  2694. )";
  2695. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  2696. }
  2697. TEST_F(InlineTest, OpKillWithTrailingInstructions) {
  2698. const std::string text = R"(
  2699. ; CHECK: OpFunction
  2700. ; CHECK-NEXT: OpLabel
  2701. ; CHECK-NEXT: [[var:%\w+]] = OpVariable
  2702. ; CHECK-NEXT: OpKill
  2703. ; CHECK-NEXT: OpLabel
  2704. ; CHECK-NEXT: OpStore [[var]]
  2705. ; CHECK-NEXT: OpReturn
  2706. ; CHECK-NEXT: OpFunctionEnd
  2707. OpCapability Shader
  2708. OpMemoryModel Logical GLSL450
  2709. OpEntryPoint Fragment %main "main"
  2710. OpExecutionMode %main OriginUpperLeft
  2711. %void = OpTypeVoid
  2712. %bool = OpTypeBool
  2713. %true = OpConstantTrue %bool
  2714. %bool_func_ptr = OpTypePointer Function %bool
  2715. %voidfuncty = OpTypeFunction %void
  2716. %main = OpFunction %void None %voidfuncty
  2717. %1 = OpLabel
  2718. %2 = OpVariable %bool_func_ptr Function
  2719. %3 = OpFunctionCall %void %func
  2720. OpStore %2 %true
  2721. OpReturn
  2722. OpFunctionEnd
  2723. %func = OpFunction %void None %voidfuncty
  2724. %4 = OpLabel
  2725. OpKill
  2726. OpFunctionEnd
  2727. )";
  2728. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  2729. }
  2730. TEST_F(InlineTest, OpKillInIf) {
  2731. const std::string text = R"(
  2732. ; CHECK: OpFunction
  2733. ; CHECK: OpLabel
  2734. ; CHECK: [[var:%\w+]] = OpVariable
  2735. ; CHECK-NEXT: [[ld:%\w+]] = OpLoad {{%\w+}} [[var]]
  2736. ; CHECK-NEXT: OpBranch [[label:%\w+]]
  2737. ; CHECK-NEXT: [[label]] = OpLabel
  2738. ; CHECK-NEXT: OpLoopMerge [[loop_merge:%\w+]] [[continue:%\w+]] None
  2739. ; CHECK-NEXT: OpBranch [[label:%\w+]]
  2740. ; CHECK-NEXT: [[label]] = OpLabel
  2741. ; CHECK-NEXT: OpSelectionMerge [[sel_merge:%\w+]] None
  2742. ; CHECK-NEXT: OpBranchConditional {{%\w+}} [[kill_label:%\w+]] [[label:%\w+]]
  2743. ; CHECK-NEXT: [[kill_label]] = OpLabel
  2744. ; CHECK-NEXT: OpKill
  2745. ; CHECK-NEXT: [[label]] = OpLabel
  2746. ; CHECK-NEXT: OpBranch [[loop_merge]]
  2747. ; CHECK-NEXT: [[sel_merge]] = OpLabel
  2748. ; CHECK-NEXT: OpBranch [[loop_merge]]
  2749. ; CHECK-NEXT: [[continue]] = OpLabel
  2750. ; CHECK-NEXT: OpBranchConditional
  2751. ; CHECK-NEXT: [[loop_merge]] = OpLabel
  2752. ; CHECK-NEXT: OpStore [[var]] [[ld]]
  2753. ; CHECK-NEXT: OpReturn
  2754. ; CHECK-NEXT: OpFunctionEnd
  2755. OpCapability Shader
  2756. OpMemoryModel Logical GLSL450
  2757. OpEntryPoint Fragment %main "main"
  2758. OpExecutionMode %main OriginUpperLeft
  2759. %void = OpTypeVoid
  2760. %bool = OpTypeBool
  2761. %true = OpConstantTrue %bool
  2762. %bool_func_ptr = OpTypePointer Function %bool
  2763. %voidfuncty = OpTypeFunction %void
  2764. %main = OpFunction %void None %voidfuncty
  2765. %1 = OpLabel
  2766. %2 = OpVariable %bool_func_ptr Function
  2767. %3 = OpLoad %bool %2
  2768. %4 = OpFunctionCall %void %func
  2769. OpStore %2 %3
  2770. OpReturn
  2771. OpFunctionEnd
  2772. %func = OpFunction %void None %voidfuncty
  2773. %5 = OpLabel
  2774. OpSelectionMerge %6 None
  2775. OpBranchConditional %true %7 %8
  2776. %7 = OpLabel
  2777. OpKill
  2778. %8 = OpLabel
  2779. OpReturn
  2780. %6 = OpLabel
  2781. OpReturn
  2782. OpFunctionEnd
  2783. )";
  2784. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  2785. }
  2786. TEST_F(InlineTest, OpKillInLoop) {
  2787. const std::string text = R"(
  2788. ; CHECK: OpFunction
  2789. ; CHECK: OpLabel
  2790. ; CHECK: [[var:%\w+]] = OpVariable
  2791. ; CHECK-NEXT: [[ld:%\w+]] = OpLoad {{%\w+}} [[var]]
  2792. ; CHECK-NEXT: OpBranch [[loop:%\w+]]
  2793. ; CHECK-NEXT: [[loop]] = OpLabel
  2794. ; CHECK-NEXT: OpLoopMerge [[loop_merge:%\w+]] [[continue:%\w+]] None
  2795. ; CHECK-NEXT: OpBranch [[label:%\w+]]
  2796. ; CHECK-NEXT: [[label]] = OpLabel
  2797. ; CHECK-NEXT: OpKill
  2798. ; CHECK-NEXT: [[loop_merge]] = OpLabel
  2799. ; CHECK-NEXT: OpBranch [[label:%\w+]]
  2800. ; CHECK-NEXT: [[continue]] = OpLabel
  2801. ; CHECK-NEXT: OpBranch [[loop]]
  2802. ; CHECK-NEXT: [[label]] = OpLabel
  2803. ; CHECK-NEXT: OpStore [[var]] [[ld]]
  2804. ; CHECK-NEXT: OpReturn
  2805. ; CHECK-NEXT: OpFunctionEnd
  2806. OpCapability Shader
  2807. OpMemoryModel Logical GLSL450
  2808. OpEntryPoint Fragment %main "main"
  2809. OpExecutionMode %main OriginUpperLeft
  2810. %void = OpTypeVoid
  2811. %bool = OpTypeBool
  2812. %true = OpConstantTrue %bool
  2813. %voidfuncty = OpTypeFunction %void
  2814. %bool_func_ptr = OpTypePointer Function %bool
  2815. %main = OpFunction %void None %voidfuncty
  2816. %1 = OpLabel
  2817. %2 = OpVariable %bool_func_ptr Function
  2818. %3 = OpLoad %bool %2
  2819. %4 = OpFunctionCall %void %func
  2820. OpStore %2 %3
  2821. OpReturn
  2822. OpFunctionEnd
  2823. %func = OpFunction %void None %voidfuncty
  2824. %5 = OpLabel
  2825. OpBranch %10
  2826. %10 = OpLabel
  2827. OpLoopMerge %6 %7 None
  2828. OpBranch %8
  2829. %8 = OpLabel
  2830. OpKill
  2831. %6 = OpLabel
  2832. OpReturn
  2833. %7 = OpLabel
  2834. OpBranch %10
  2835. OpFunctionEnd
  2836. )";
  2837. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  2838. }
  2839. TEST_F(InlineTest, OpVariableWithInit) {
  2840. // Check that there is a store that corresponds to the initializer. This
  2841. // test makes sure that is a store to the variable in the loop and before any
  2842. // load.
  2843. const std::string text = R"(
  2844. ; CHECK: OpFunction
  2845. ; CHECK-NOT: OpFunctionEnd
  2846. ; CHECK: [[var:%\w+]] = OpVariable %_ptr_Function_float Function %float_0
  2847. ; CHECK: OpLoopMerge [[outer_merge:%\w+]]
  2848. ; CHECK-NOT: OpLoad %float [[var]]
  2849. ; CHECK: OpStore [[var]] %float_0
  2850. ; CHECK: OpFunctionEnd
  2851. OpCapability Shader
  2852. %1 = OpExtInstImport "GLSL.std.450"
  2853. OpMemoryModel Logical GLSL450
  2854. OpEntryPoint Fragment %main "main" %o
  2855. OpExecutionMode %main OriginUpperLeft
  2856. OpSource GLSL 450
  2857. OpDecorate %o Location 0
  2858. %void = OpTypeVoid
  2859. %3 = OpTypeFunction %void
  2860. %float = OpTypeFloat 32
  2861. %7 = OpTypeFunction %float
  2862. %_ptr_Function_float = OpTypePointer Function %float
  2863. %float_0 = OpConstant %float 0
  2864. %bool = OpTypeBool
  2865. %float_1 = OpConstant %float 1
  2866. %_ptr_Output_float = OpTypePointer Output %float
  2867. %o = OpVariable %_ptr_Output_float Output
  2868. %int = OpTypeInt 32 1
  2869. %_ptr_Function_int = OpTypePointer Function %int
  2870. %_ptr_Input_int = OpTypePointer Input %int
  2871. %int_0 = OpConstant %int 0
  2872. %int_1 = OpConstant %int 1
  2873. %int_2 = OpConstant %int 2
  2874. %main = OpFunction %void None %3
  2875. %5 = OpLabel
  2876. OpStore %o %float_0
  2877. OpBranch %34
  2878. %34 = OpLabel
  2879. %39 = OpPhi %int %int_0 %5 %47 %37
  2880. OpLoopMerge %36 %37 None
  2881. OpBranch %38
  2882. %38 = OpLabel
  2883. %41 = OpSLessThan %bool %39 %int_2
  2884. OpBranchConditional %41 %35 %36
  2885. %35 = OpLabel
  2886. %42 = OpFunctionCall %float %foo_
  2887. %43 = OpLoad %float %o
  2888. %44 = OpFAdd %float %43 %42
  2889. OpStore %o %44
  2890. OpBranch %37
  2891. %37 = OpLabel
  2892. %47 = OpIAdd %int %39 %int_1
  2893. OpBranch %34
  2894. %36 = OpLabel
  2895. OpReturn
  2896. OpFunctionEnd
  2897. %foo_ = OpFunction %float None %7
  2898. %9 = OpLabel
  2899. %n = OpVariable %_ptr_Function_float Function %float_0
  2900. %13 = OpLoad %float %n
  2901. %15 = OpFOrdEqual %bool %13 %float_0
  2902. OpSelectionMerge %17 None
  2903. OpBranchConditional %15 %16 %17
  2904. %16 = OpLabel
  2905. %19 = OpLoad %float %n
  2906. %20 = OpFAdd %float %19 %float_1
  2907. OpStore %n %20
  2908. OpBranch %17
  2909. %17 = OpLabel
  2910. %21 = OpLoad %float %n
  2911. OpReturnValue %21
  2912. OpFunctionEnd
  2913. )";
  2914. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  2915. }
  2916. TEST_F(InlineTest, DontInlineDirectlyRecursiveFunc) {
  2917. // Test that the name of the result id of the call is deleted.
  2918. const std::string test =
  2919. R"(OpCapability Shader
  2920. OpMemoryModel Logical GLSL450
  2921. OpEntryPoint Fragment %1 "main"
  2922. OpExecutionMode %1 OriginUpperLeft
  2923. OpDecorate %2 DescriptorSet 439418829
  2924. %void = OpTypeVoid
  2925. %4 = OpTypeFunction %void
  2926. %float = OpTypeFloat 32
  2927. %_struct_6 = OpTypeStruct %float %float
  2928. %15 = OpConstantNull %_struct_6
  2929. %7 = OpTypeFunction %_struct_6
  2930. %1 = OpFunction %void Pure|Const %4
  2931. %8 = OpLabel
  2932. %2 = OpFunctionCall %_struct_6 %9
  2933. OpKill
  2934. OpFunctionEnd
  2935. %9 = OpFunction %_struct_6 None %7
  2936. %10 = OpLabel
  2937. %11 = OpFunctionCall %_struct_6 %9
  2938. OpReturnValue %15
  2939. OpFunctionEnd
  2940. )";
  2941. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2942. SinglePassRunAndCheck<InlineExhaustivePass>(test, test, false, true);
  2943. }
  2944. TEST_F(InlineTest, DontInlineInDirectlyRecursiveFunc) {
  2945. // Test that the name of the result id of the call is deleted.
  2946. const std::string test =
  2947. R"(OpCapability Shader
  2948. OpMemoryModel Logical GLSL450
  2949. OpEntryPoint Fragment %1 "main"
  2950. OpExecutionMode %1 OriginUpperLeft
  2951. OpDecorate %2 DescriptorSet 439418829
  2952. %void = OpTypeVoid
  2953. %4 = OpTypeFunction %void
  2954. %float = OpTypeFloat 32
  2955. %_struct_6 = OpTypeStruct %float %float
  2956. %15 = OpConstantNull %_struct_6
  2957. %7 = OpTypeFunction %_struct_6
  2958. %1 = OpFunction %void Pure|Const %4
  2959. %8 = OpLabel
  2960. %2 = OpFunctionCall %_struct_6 %9
  2961. OpKill
  2962. OpFunctionEnd
  2963. %9 = OpFunction %_struct_6 None %7
  2964. %10 = OpLabel
  2965. %11 = OpFunctionCall %_struct_6 %12
  2966. OpReturnValue %15
  2967. OpFunctionEnd
  2968. %12 = OpFunction %_struct_6 None %7
  2969. %13 = OpLabel
  2970. %14 = OpFunctionCall %_struct_6 %9
  2971. OpReturnValue %15
  2972. OpFunctionEnd
  2973. )";
  2974. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2975. SinglePassRunAndCheck<InlineExhaustivePass>(test, test, false, true);
  2976. }
  2977. // TODO(greg-lunarg): Add tests to verify handling of these cases:
  2978. //
  2979. // Empty modules
  2980. // Modules without function definitions
  2981. // Modules in which all functions do not call other functions
  2982. // Caller and callee both accessing the same global variable
  2983. // Functions with OpLine & OpNoLine
  2984. // Others?
  2985. // TODO(dneto): Test suggestions from code review
  2986. // https://github.com/KhronosGroup/SPIRV-Tools/pull/534
  2987. //
  2988. // Callee function returns a value generated outside the callee,
  2989. // e.g. a constant value. This might exercise some logic not yet
  2990. // exercised by the current tests: the false branch in the "if"
  2991. // inside the SpvOpReturnValue case in InlinePass::GenInlineCode?
  2992. // SampledImage before function call, but callee is only single block.
  2993. // Then the SampledImage instruction is not cloned. Documents existing
  2994. // behaviour.
  2995. // SampledImage after function call. It is not cloned or changed.
  2996. } // namespace
  2997. } // namespace opt
  2998. } // namespace spvtools