2
0

inline_test.cpp 136 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963
  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. "%34 = OpAccessChain %_ptr_Function_float %param %uint_0",
  111. "%35 = OpLoad %float %34",
  112. "%36 = OpAccessChain %_ptr_Function_float %param %uint_1",
  113. "%37 = OpLoad %float %36",
  114. "%38 = OpFAdd %float %35 %37",
  115. "OpStore %32 %38",
  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. "%58 = 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. "%50 = OpAccessChain %_ptr_Function_float %param_1 %uint_0",
  248. "%51 = OpLoad %float %50",
  249. "%52 = OpAccessChain %_ptr_Function_float %param_1 %uint_1",
  250. "%53 = OpLoad %float %52",
  251. "%54 = OpFAdd %float %51 %53",
  252. "OpStore %46 %54",
  253. "%55 = OpAccessChain %_ptr_Function_float %param_1 %uint_2",
  254. "%56 = OpLoad %float %55",
  255. "OpStore %47 %56",
  256. "%60 = OpLoad %float %46",
  257. "%61 = OpLoad %float %47",
  258. "%62 = OpFMul %float %60 %61",
  259. "OpStore %58 %62",
  260. "%57 = OpLoad %float %58",
  261. "OpStore %48 %57",
  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. "%40 = OpAccessChain %_ptr_Function_float %param %uint_0",
  378. "%41 = OpLoad %float %40",
  379. "%42 = OpAccessChain %_ptr_Function_float %param %uint_1",
  380. "%43 = OpLoad %float %42",
  381. "%44 = OpFAdd %float %41 %43",
  382. "%45 = OpAccessChain %_ptr_Function_float %param %uint_2",
  383. "OpStore %45 %44",
  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. "%41 = OpAccessChain %_ptr_Function_float %param %uint_0",
  505. "%42 = OpLoad %float %41",
  506. "OpStore %38 %42",
  507. "%43 = OpLoad %float %38",
  508. "%44 = OpFOrdLessThan %bool %43 %float_0",
  509. "OpSelectionMerge %48 None",
  510. "OpBranchConditional %44 %45 %48",
  511. "%45 = OpLabel",
  512. "%46 = OpLoad %float %38",
  513. "%47 = OpFNegate %float %46",
  514. "OpStore %38 %47",
  515. "OpBranch %48",
  516. "%48 = OpLabel",
  517. "%49 = OpLoad %float %38",
  518. "OpStore %39 %49",
  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. "%63 = OpVariable %_ptr_Function_float Function",
  655. "%64 = 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. "%55 = OpLoad %float %param",
  667. "OpStore %52 %55",
  668. "%56 = OpLoad %float %52",
  669. "%57 = OpFOrdLessThan %bool %56 %float_0",
  670. "OpSelectionMerge %61 None",
  671. "OpBranchConditional %57 %58 %61",
  672. "%58 = OpLabel",
  673. "%59 = OpLoad %float %52",
  674. "%60 = OpFNegate %float %59",
  675. "OpStore %52 %60",
  676. "OpBranch %61",
  677. "%61 = OpLabel",
  678. "%62 = OpLoad %float %52",
  679. "OpStore %53 %62",
  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. "%66 = OpLoad %float %param_0",
  689. "OpStore %63 %66",
  690. "%67 = OpLoad %float %63",
  691. "%68 = OpFOrdLessThan %bool %67 %float_0",
  692. "OpSelectionMerge %72 None",
  693. "OpBranchConditional %68 %69 %72",
  694. "%69 = OpLabel",
  695. "%70 = OpLoad %float %63",
  696. "%71 = OpFNegate %float %70",
  697. "OpStore %63 %71",
  698. "OpBranch %72",
  699. "%72 = OpLabel",
  700. "%73 = OpLoad %float %63",
  701. "OpStore %64 %73",
  702. "%37 = OpLoad %float %64",
  703. "%38 = OpFOrdGreaterThan %bool %37 %float_2",
  704. "OpBranch %33",
  705. "%33 = OpLabel",
  706. "%39 = OpPhi %bool %32 %61 %38 %72",
  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. "%69 = OpAccessChain %_ptr_Function_float %param %uint_0",
  878. "%70 = OpLoad %float %69",
  879. "OpStore %66 %70",
  880. "%71 = OpLoad %float %66",
  881. "%72 = OpFOrdLessThan %bool %71 %float_0",
  882. "OpSelectionMerge %76 None",
  883. "OpBranchConditional %72 %73 %76",
  884. "%73 = OpLabel",
  885. "%74 = OpLoad %float %66",
  886. "%75 = OpFNegate %float %74",
  887. "OpStore %66 %75",
  888. "OpBranch %76",
  889. "%76 = OpLabel",
  890. "%77 = OpLoad %float %66",
  891. "OpStore %67 %77",
  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. "%78 = OpSampledImage %29 %39 %40",
  898. "%48 = OpImageSampleImplicitLod %v4float %78 %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. "%71 = OpAccessChain %_ptr_Function_float %param %uint_0",
  1080. "%72 = OpLoad %float %71",
  1081. "OpStore %68 %72",
  1082. "%73 = OpLoad %float %68",
  1083. "%74 = OpFOrdLessThan %bool %73 %float_0",
  1084. "OpSelectionMerge %78 None",
  1085. "OpBranchConditional %74 %75 %78",
  1086. "%75 = OpLabel",
  1087. "%76 = OpLoad %float %68",
  1088. "%77 = OpFNegate %float %76",
  1089. "OpStore %68 %77",
  1090. "OpBranch %78",
  1091. "%78 = OpLabel",
  1092. "%79 = OpLoad %float %68",
  1093. "OpStore %69 %79",
  1094. "%47 = OpLoad %float %69",
  1095. "%48 = OpCompositeConstruct %v4float %47 %47 %47 %47",
  1096. "OpStore %color2 %48",
  1097. "%80 = OpSampledImage %30 %40 %41",
  1098. "%81 = OpImage %26 %80",
  1099. "%49 = OpSampledImage %30 %81 %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. "%71 = OpAccessChain %_ptr_Function_float %param %uint_0",
  1282. "%72 = OpLoad %float %71",
  1283. "OpStore %68 %72",
  1284. "%73 = OpLoad %float %68",
  1285. "%74 = OpFOrdLessThan %bool %73 %float_0",
  1286. "OpSelectionMerge %78 None",
  1287. "OpBranchConditional %74 %75 %78",
  1288. "%75 = OpLabel",
  1289. "%76 = OpLoad %float %68",
  1290. "%77 = OpFNegate %float %76",
  1291. "OpStore %68 %77",
  1292. "OpBranch %78",
  1293. "%78 = OpLabel",
  1294. "%79 = OpLoad %float %68",
  1295. "OpStore %69 %79",
  1296. "%48 = OpLoad %float %69",
  1297. "%49 = OpCompositeConstruct %v4float %48 %48 %48 %48",
  1298. "OpStore %color2 %49",
  1299. "%80 = OpSampledImage %30 %40 %41",
  1300. "%81 = OpImage %26 %80",
  1301. "%82 = OpSampledImage %30 %81 %45",
  1302. "%50 = OpImageSampleImplicitLod %v4float %82 %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, EarlyReturnInLoopIsNotInlined) {
  1322. // #version 140
  1323. //
  1324. // in vec4 BaseColor;
  1325. //
  1326. // float foo(vec4 bar)
  1327. // {
  1328. // while (true) {
  1329. // if (bar.x < 0.0)
  1330. // return 0.0;
  1331. // return bar.x;
  1332. // }
  1333. // }
  1334. //
  1335. // void main()
  1336. // {
  1337. // vec4 color = vec4(foo(BaseColor));
  1338. // gl_FragColor = color;
  1339. // }
  1340. const std::string assembly =
  1341. R"(OpCapability Shader
  1342. %1 = OpExtInstImport "GLSL.std.450"
  1343. OpMemoryModel Logical GLSL450
  1344. OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
  1345. OpExecutionMode %main OriginUpperLeft
  1346. OpSource GLSL 140
  1347. OpName %main "main"
  1348. OpName %foo_vf4_ "foo(vf4;"
  1349. OpName %bar "bar"
  1350. OpName %color "color"
  1351. OpName %BaseColor "BaseColor"
  1352. OpName %param "param"
  1353. OpName %gl_FragColor "gl_FragColor"
  1354. %void = OpTypeVoid
  1355. %10 = OpTypeFunction %void
  1356. %float = OpTypeFloat 32
  1357. %v4float = OpTypeVector %float 4
  1358. %_ptr_Function_v4float = OpTypePointer Function %v4float
  1359. %14 = OpTypeFunction %float %_ptr_Function_v4float
  1360. %bool = OpTypeBool
  1361. %true = OpConstantTrue %bool
  1362. %uint = OpTypeInt 32 0
  1363. %uint_0 = OpConstant %uint 0
  1364. %_ptr_Function_float = OpTypePointer Function %float
  1365. %float_0 = OpConstant %float 0
  1366. %_ptr_Input_v4float = OpTypePointer Input %v4float
  1367. %BaseColor = OpVariable %_ptr_Input_v4float Input
  1368. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1369. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  1370. %main = OpFunction %void None %10
  1371. %23 = OpLabel
  1372. %color = OpVariable %_ptr_Function_v4float Function
  1373. %param = OpVariable %_ptr_Function_v4float Function
  1374. %24 = OpLoad %v4float %BaseColor
  1375. OpStore %param %24
  1376. %25 = OpFunctionCall %float %foo_vf4_ %param
  1377. %26 = OpCompositeConstruct %v4float %25 %25 %25 %25
  1378. OpStore %color %26
  1379. %27 = OpLoad %v4float %color
  1380. OpStore %gl_FragColor %27
  1381. OpReturn
  1382. OpFunctionEnd
  1383. %foo_vf4_ = OpFunction %float None %14
  1384. %bar = OpFunctionParameter %_ptr_Function_v4float
  1385. %28 = OpLabel
  1386. OpBranch %29
  1387. %29 = OpLabel
  1388. OpLoopMerge %30 %31 None
  1389. OpBranch %32
  1390. %32 = OpLabel
  1391. OpBranchConditional %true %33 %30
  1392. %33 = OpLabel
  1393. %34 = OpAccessChain %_ptr_Function_float %bar %uint_0
  1394. %35 = OpLoad %float %34
  1395. %36 = OpFOrdLessThan %bool %35 %float_0
  1396. OpSelectionMerge %37 None
  1397. OpBranchConditional %36 %38 %37
  1398. %38 = OpLabel
  1399. OpReturnValue %float_0
  1400. %37 = OpLabel
  1401. %39 = OpAccessChain %_ptr_Function_float %bar %uint_0
  1402. %40 = OpLoad %float %39
  1403. OpReturnValue %40
  1404. %31 = OpLabel
  1405. OpBranch %29
  1406. %30 = OpLabel
  1407. %41 = OpUndef %float
  1408. OpReturnValue %41
  1409. OpFunctionEnd
  1410. )";
  1411. SinglePassRunAndCheck<InlineExhaustivePass>(assembly, assembly, false, true);
  1412. }
  1413. TEST_F(InlineTest, ExternalFunctionIsNotInlined) {
  1414. // In particular, don't crash.
  1415. // See report https://github.com/KhronosGroup/SPIRV-Tools/issues/605
  1416. const std::string assembly =
  1417. R"(OpCapability Addresses
  1418. OpCapability Kernel
  1419. OpCapability Linkage
  1420. OpMemoryModel Physical32 OpenCL
  1421. OpEntryPoint Kernel %1 "entry_pt"
  1422. OpDecorate %2 LinkageAttributes "external" Import
  1423. %void = OpTypeVoid
  1424. %4 = OpTypeFunction %void
  1425. %2 = OpFunction %void None %4
  1426. OpFunctionEnd
  1427. %1 = OpFunction %void None %4
  1428. %5 = OpLabel
  1429. %6 = OpFunctionCall %void %2
  1430. OpReturn
  1431. OpFunctionEnd
  1432. )";
  1433. SinglePassRunAndCheck<InlineExhaustivePass>(assembly, assembly, false, true);
  1434. }
  1435. TEST_F(InlineTest, SingleBlockLoopCallsMultiBlockCallee) {
  1436. // Example from https://github.com/KhronosGroup/SPIRV-Tools/issues/787
  1437. //
  1438. // CFG structure is:
  1439. // foo:
  1440. // fooentry -> fooexit
  1441. //
  1442. // main:
  1443. // entry -> loop
  1444. // loop -> loop, merge
  1445. // loop calls foo()
  1446. // merge
  1447. //
  1448. // Since the callee has multiple blocks, it will split the calling block
  1449. // into at least two, resulting in a new "back-half" block that contains
  1450. // the instructions after the inlined function call. If the calling block
  1451. // has an OpLoopMerge that points back to the calling block itself, then
  1452. // the OpLoopMerge can't remain in the back-half block, but must be
  1453. // moved to the end of the original calling block, and it continue target
  1454. // operand updated to point to the back-half block.
  1455. const std::string predefs =
  1456. R"(OpCapability Shader
  1457. OpMemoryModel Logical GLSL450
  1458. OpEntryPoint GLCompute %1 "main"
  1459. OpSource OpenCL_C 120
  1460. %bool = OpTypeBool
  1461. %true = OpConstantTrue %bool
  1462. %void = OpTypeVoid
  1463. %5 = OpTypeFunction %void
  1464. )";
  1465. const std::string nonEntryFuncs =
  1466. R"(%6 = OpFunction %void None %5
  1467. %7 = OpLabel
  1468. OpBranch %8
  1469. %8 = OpLabel
  1470. OpReturn
  1471. OpFunctionEnd
  1472. )";
  1473. const std::string before =
  1474. R"(%1 = OpFunction %void None %5
  1475. %9 = OpLabel
  1476. OpBranch %10
  1477. %10 = OpLabel
  1478. %11 = OpFunctionCall %void %6
  1479. OpLoopMerge %12 %10 None
  1480. OpBranchConditional %true %10 %12
  1481. %12 = OpLabel
  1482. OpReturn
  1483. OpFunctionEnd
  1484. )";
  1485. const std::string after =
  1486. R"(%1 = OpFunction %void None %5
  1487. %9 = OpLabel
  1488. OpBranch %10
  1489. %10 = OpLabel
  1490. OpLoopMerge %12 %10 None
  1491. OpBranch %14
  1492. %14 = OpLabel
  1493. OpBranchConditional %true %10 %12
  1494. %12 = OpLabel
  1495. OpReturn
  1496. OpFunctionEnd
  1497. )";
  1498. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + nonEntryFuncs + before,
  1499. predefs + nonEntryFuncs + after,
  1500. false, true);
  1501. }
  1502. TEST_F(InlineTest, MultiBlockLoopHeaderCallsMultiBlockCallee) {
  1503. // Like SingleBlockLoopCallsMultiBlockCallee but the loop has several
  1504. // blocks, but the function call still occurs in the loop header.
  1505. // Example from https://github.com/KhronosGroup/SPIRV-Tools/issues/800
  1506. const std::string predefs =
  1507. R"(OpCapability Shader
  1508. OpMemoryModel Logical GLSL450
  1509. OpEntryPoint GLCompute %1 "main"
  1510. OpSource OpenCL_C 120
  1511. %bool = OpTypeBool
  1512. %true = OpConstantTrue %bool
  1513. %int = OpTypeInt 32 1
  1514. %int_1 = OpConstant %int 1
  1515. %int_2 = OpConstant %int 2
  1516. %int_3 = OpConstant %int 3
  1517. %int_4 = OpConstant %int 4
  1518. %int_5 = OpConstant %int 5
  1519. %void = OpTypeVoid
  1520. %11 = OpTypeFunction %void
  1521. )";
  1522. const std::string nonEntryFuncs =
  1523. R"(%12 = OpFunction %void None %11
  1524. %13 = OpLabel
  1525. %14 = OpCopyObject %int %int_1
  1526. OpBranch %15
  1527. %15 = OpLabel
  1528. %16 = OpCopyObject %int %int_2
  1529. OpReturn
  1530. OpFunctionEnd
  1531. )";
  1532. const std::string before =
  1533. R"(%1 = OpFunction %void None %11
  1534. %17 = OpLabel
  1535. OpBranch %18
  1536. %18 = OpLabel
  1537. %19 = OpCopyObject %int %int_3
  1538. %20 = OpFunctionCall %void %12
  1539. %21 = OpCopyObject %int %int_4
  1540. OpLoopMerge %22 %23 None
  1541. OpBranchConditional %true %23 %22
  1542. %23 = OpLabel
  1543. %24 = OpCopyObject %int %int_5
  1544. OpBranchConditional %true %18 %22
  1545. %22 = OpLabel
  1546. OpReturn
  1547. OpFunctionEnd
  1548. )";
  1549. const std::string after =
  1550. R"(%1 = OpFunction %void None %11
  1551. %17 = OpLabel
  1552. OpBranch %18
  1553. %18 = OpLabel
  1554. %19 = OpCopyObject %int %int_3
  1555. %26 = OpCopyObject %int %int_1
  1556. OpLoopMerge %22 %23 None
  1557. OpBranch %27
  1558. %27 = OpLabel
  1559. %28 = OpCopyObject %int %int_2
  1560. %21 = OpCopyObject %int %int_4
  1561. OpBranchConditional %true %23 %22
  1562. %23 = OpLabel
  1563. %24 = OpCopyObject %int %int_5
  1564. OpBranchConditional %true %18 %22
  1565. %22 = OpLabel
  1566. OpReturn
  1567. OpFunctionEnd
  1568. )";
  1569. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + nonEntryFuncs + before,
  1570. predefs + nonEntryFuncs + after,
  1571. false, true);
  1572. }
  1573. TEST_F(InlineTest, SingleBlockLoopCallsMultiBlockCalleeHavingSelectionMerge) {
  1574. // This is similar to SingleBlockLoopCallsMultiBlockCallee except
  1575. // that calleee block also has a merge instruction in its first block.
  1576. // That merge instruction must be an OpSelectionMerge (because the entry
  1577. // block of a function can't be the header of a loop since the entry
  1578. // block can't be the target of a branch).
  1579. //
  1580. // In this case the OpLoopMerge can't be placed in the same block as
  1581. // the OpSelectionMerge, so inlining must create a new block to contain
  1582. // the callee contents.
  1583. //
  1584. // Additionally, we have two extra OpCopyObject instructions to prove that
  1585. // the OpLoopMerge is moved to the right location.
  1586. //
  1587. // Also ensure that OpPhis within the cloned callee code are valid.
  1588. // We need to test that the predecessor blocks are remapped correctly so that
  1589. // dominance rules are satisfied
  1590. const std::string predefs =
  1591. R"(OpCapability Shader
  1592. OpMemoryModel Logical GLSL450
  1593. OpEntryPoint GLCompute %1 "main"
  1594. OpSource OpenCL_C 120
  1595. %bool = OpTypeBool
  1596. %true = OpConstantTrue %bool
  1597. %false = OpConstantFalse %bool
  1598. %void = OpTypeVoid
  1599. %6 = OpTypeFunction %void
  1600. )";
  1601. // This callee has multiple blocks, and an OpPhi in the last block
  1602. // that references a value from the first block. This tests that
  1603. // cloned block IDs are remapped appropriately. The OpPhi dominance
  1604. // requires that the remapped %9 must be in a block that dominates
  1605. // the remapped %8.
  1606. const std::string nonEntryFuncs =
  1607. R"(%7 = OpFunction %void None %6
  1608. %8 = OpLabel
  1609. %9 = OpCopyObject %bool %true
  1610. OpSelectionMerge %10 None
  1611. OpBranchConditional %true %10 %10
  1612. %10 = OpLabel
  1613. %11 = OpPhi %bool %9 %8
  1614. OpReturn
  1615. OpFunctionEnd
  1616. )";
  1617. const std::string before =
  1618. R"(%1 = OpFunction %void None %6
  1619. %12 = OpLabel
  1620. OpBranch %13
  1621. %13 = OpLabel
  1622. %14 = OpCopyObject %bool %false
  1623. %15 = OpFunctionCall %void %7
  1624. OpLoopMerge %16 %13 None
  1625. OpBranchConditional %true %13 %16
  1626. %16 = OpLabel
  1627. OpReturn
  1628. OpFunctionEnd
  1629. )";
  1630. // Note the remapped Phi uses %17 as the parent instead
  1631. // of %13, demonstrating that the parent block has been remapped
  1632. // correctly.
  1633. const std::string after =
  1634. R"(%1 = OpFunction %void None %6
  1635. %12 = OpLabel
  1636. OpBranch %13
  1637. %13 = OpLabel
  1638. %14 = OpCopyObject %bool %false
  1639. OpLoopMerge %16 %13 None
  1640. OpBranch %17
  1641. %17 = OpLabel
  1642. %19 = OpCopyObject %bool %true
  1643. OpSelectionMerge %20 None
  1644. OpBranchConditional %true %20 %20
  1645. %20 = OpLabel
  1646. %21 = OpPhi %bool %19 %17
  1647. OpBranchConditional %true %13 %16
  1648. %16 = OpLabel
  1649. OpReturn
  1650. OpFunctionEnd
  1651. )";
  1652. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + nonEntryFuncs + before,
  1653. predefs + nonEntryFuncs + after,
  1654. false, true);
  1655. }
  1656. TEST_F(InlineTest,
  1657. MultiBlockLoopHeaderCallsFromToMultiBlockCalleeHavingSelectionMerge) {
  1658. // This is similar to SingleBlockLoopCallsMultiBlockCalleeHavingSelectionMerge
  1659. // but the call is in the header block of a multi block loop.
  1660. const std::string predefs =
  1661. R"(OpCapability Shader
  1662. OpMemoryModel Logical GLSL450
  1663. OpEntryPoint GLCompute %1 "main"
  1664. OpSource OpenCL_C 120
  1665. %bool = OpTypeBool
  1666. %true = OpConstantTrue %bool
  1667. %int = OpTypeInt 32 1
  1668. %int_1 = OpConstant %int 1
  1669. %int_2 = OpConstant %int 2
  1670. %int_3 = OpConstant %int 3
  1671. %int_4 = OpConstant %int 4
  1672. %int_5 = OpConstant %int 5
  1673. %void = OpTypeVoid
  1674. %11 = OpTypeFunction %void
  1675. )";
  1676. const std::string nonEntryFuncs =
  1677. R"(%12 = OpFunction %void None %11
  1678. %13 = OpLabel
  1679. %14 = OpCopyObject %int %int_1
  1680. OpSelectionMerge %15 None
  1681. OpBranchConditional %true %15 %15
  1682. %15 = OpLabel
  1683. %16 = OpCopyObject %int %int_2
  1684. OpReturn
  1685. OpFunctionEnd
  1686. )";
  1687. const std::string before =
  1688. R"(%1 = OpFunction %void None %11
  1689. %17 = OpLabel
  1690. OpBranch %18
  1691. %18 = OpLabel
  1692. %19 = OpCopyObject %int %int_3
  1693. %20 = OpFunctionCall %void %12
  1694. %21 = OpCopyObject %int %int_4
  1695. OpLoopMerge %22 %23 None
  1696. OpBranchConditional %true %23 %22
  1697. %23 = OpLabel
  1698. %24 = OpCopyObject %int %int_5
  1699. OpBranchConditional %true %18 %22
  1700. %22 = OpLabel
  1701. OpReturn
  1702. OpFunctionEnd
  1703. )";
  1704. const std::string after =
  1705. R"(%1 = OpFunction %void None %11
  1706. %17 = OpLabel
  1707. OpBranch %18
  1708. %18 = OpLabel
  1709. %19 = OpCopyObject %int %int_3
  1710. OpLoopMerge %22 %23 None
  1711. OpBranch %25
  1712. %25 = OpLabel
  1713. %27 = OpCopyObject %int %int_1
  1714. OpSelectionMerge %28 None
  1715. OpBranchConditional %true %28 %28
  1716. %28 = OpLabel
  1717. %29 = OpCopyObject %int %int_2
  1718. %21 = OpCopyObject %int %int_4
  1719. OpBranchConditional %true %23 %22
  1720. %23 = OpLabel
  1721. %24 = OpCopyObject %int %int_5
  1722. OpBranchConditional %true %18 %22
  1723. %22 = OpLabel
  1724. OpReturn
  1725. OpFunctionEnd
  1726. )";
  1727. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + nonEntryFuncs + before,
  1728. predefs + nonEntryFuncs + after,
  1729. false, true);
  1730. }
  1731. TEST_F(InlineTest, NonInlinableCalleeWithSingleReturn) {
  1732. // The case from https://github.com/KhronosGroup/SPIRV-Tools/issues/2018
  1733. //
  1734. // The callee has a single return, but cannot be inlined because the
  1735. // return is inside a loop.
  1736. const std::string predefs =
  1737. R"(OpCapability Shader
  1738. %1 = OpExtInstImport "GLSL.std.450"
  1739. OpMemoryModel Logical GLSL450
  1740. OpEntryPoint Fragment %main "main" %_GLF_color
  1741. OpExecutionMode %main OriginUpperLeft
  1742. OpSource ESSL 310
  1743. OpName %main "main"
  1744. OpName %f_ "f("
  1745. OpName %i "i"
  1746. OpName %_GLF_color "_GLF_color"
  1747. OpDecorate %_GLF_color Location 0
  1748. %void = OpTypeVoid
  1749. %7 = OpTypeFunction %void
  1750. %float = OpTypeFloat 32
  1751. %9 = OpTypeFunction %float
  1752. %float_1 = OpConstant %float 1
  1753. %bool = OpTypeBool
  1754. %false = OpConstantFalse %bool
  1755. %int = OpTypeInt 32 1
  1756. %_ptr_Function_int = OpTypePointer Function %int
  1757. %int_0 = OpConstant %int 0
  1758. %int_1 = OpConstant %int 1
  1759. %v4float = OpTypeVector %float 4
  1760. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1761. %_GLF_color = OpVariable %_ptr_Output_v4float Output
  1762. %float_0 = OpConstant %float 0
  1763. %20 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
  1764. %21 = OpConstantComposite %v4float %float_0 %float_1 %float_0 %float_1
  1765. )";
  1766. const std::string caller =
  1767. R"(%main = OpFunction %void None %7
  1768. %22 = OpLabel
  1769. %i = OpVariable %_ptr_Function_int Function
  1770. OpStore %i %int_0
  1771. OpBranch %23
  1772. %23 = OpLabel
  1773. OpLoopMerge %24 %25 None
  1774. OpBranch %26
  1775. %26 = OpLabel
  1776. %27 = OpLoad %int %i
  1777. %28 = OpSLessThan %bool %27 %int_1
  1778. OpBranchConditional %28 %29 %24
  1779. %29 = OpLabel
  1780. OpStore %_GLF_color %20
  1781. %30 = OpFunctionCall %float %f_
  1782. OpBranch %25
  1783. %25 = OpLabel
  1784. %31 = OpLoad %int %i
  1785. %32 = OpIAdd %int %31 %int_1
  1786. OpStore %i %32
  1787. OpBranch %23
  1788. %24 = OpLabel
  1789. OpStore %_GLF_color %21
  1790. OpReturn
  1791. OpFunctionEnd
  1792. )";
  1793. const std::string callee =
  1794. R"(%f_ = OpFunction %float None %9
  1795. %33 = OpLabel
  1796. OpBranch %34
  1797. %34 = OpLabel
  1798. OpLoopMerge %35 %36 None
  1799. OpBranch %37
  1800. %37 = OpLabel
  1801. OpReturnValue %float_1
  1802. %36 = OpLabel
  1803. OpBranch %34
  1804. %35 = OpLabel
  1805. OpUnreachable
  1806. OpFunctionEnd
  1807. )";
  1808. SinglePassRunAndCheck<InlineExhaustivePass>(
  1809. predefs + caller + callee, predefs + caller + callee, false, true);
  1810. }
  1811. TEST_F(InlineTest, Decorated1) {
  1812. // Same test as Simple with the difference
  1813. // that OpFAdd in the outlined function is
  1814. // decorated with RelaxedPrecision
  1815. // Expected result is an equal decoration
  1816. // of the corresponding inlined instruction
  1817. //
  1818. // #version 140
  1819. //
  1820. // in vec4 BaseColor;
  1821. //
  1822. // float foo(vec4 bar)
  1823. // {
  1824. // return bar.x + bar.y;
  1825. // }
  1826. //
  1827. // void main()
  1828. // {
  1829. // vec4 color = vec4(foo(BaseColor));
  1830. // gl_FragColor = color;
  1831. // }
  1832. const std::string predefs =
  1833. R"(OpCapability Shader
  1834. %1 = OpExtInstImport "GLSL.std.450"
  1835. OpMemoryModel Logical GLSL450
  1836. OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
  1837. OpExecutionMode %main OriginUpperLeft
  1838. OpSource GLSL 140
  1839. OpName %main "main"
  1840. OpName %foo_vf4_ "foo(vf4;"
  1841. OpName %bar "bar"
  1842. OpName %color "color"
  1843. OpName %BaseColor "BaseColor"
  1844. OpName %param "param"
  1845. OpName %gl_FragColor "gl_FragColor"
  1846. OpDecorate %9 RelaxedPrecision
  1847. )";
  1848. const std::string before =
  1849. R"(%void = OpTypeVoid
  1850. %11 = OpTypeFunction %void
  1851. %float = OpTypeFloat 32
  1852. %v4float = OpTypeVector %float 4
  1853. %_ptr_Function_v4float = OpTypePointer Function %v4float
  1854. %15 = OpTypeFunction %float %_ptr_Function_v4float
  1855. %uint = OpTypeInt 32 0
  1856. %uint_0 = OpConstant %uint 0
  1857. %_ptr_Function_float = OpTypePointer Function %float
  1858. %uint_1 = OpConstant %uint 1
  1859. %_ptr_Input_v4float = OpTypePointer Input %v4float
  1860. %BaseColor = OpVariable %_ptr_Input_v4float Input
  1861. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1862. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  1863. %main = OpFunction %void None %11
  1864. %22 = OpLabel
  1865. %color = OpVariable %_ptr_Function_v4float Function
  1866. %param = OpVariable %_ptr_Function_v4float Function
  1867. %23 = OpLoad %v4float %BaseColor
  1868. OpStore %param %23
  1869. %24 = OpFunctionCall %float %foo_vf4_ %param
  1870. %25 = OpCompositeConstruct %v4float %24 %24 %24 %24
  1871. OpStore %color %25
  1872. %26 = OpLoad %v4float %color
  1873. OpStore %gl_FragColor %26
  1874. OpReturn
  1875. OpFunctionEnd
  1876. )";
  1877. const std::string after =
  1878. R"(OpDecorate %38 RelaxedPrecision
  1879. %void = OpTypeVoid
  1880. %11 = OpTypeFunction %void
  1881. %float = OpTypeFloat 32
  1882. %v4float = OpTypeVector %float 4
  1883. %_ptr_Function_v4float = OpTypePointer Function %v4float
  1884. %15 = OpTypeFunction %float %_ptr_Function_v4float
  1885. %uint = OpTypeInt 32 0
  1886. %uint_0 = OpConstant %uint 0
  1887. %_ptr_Function_float = OpTypePointer Function %float
  1888. %uint_1 = OpConstant %uint 1
  1889. %_ptr_Input_v4float = OpTypePointer Input %v4float
  1890. %BaseColor = OpVariable %_ptr_Input_v4float Input
  1891. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1892. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  1893. %main = OpFunction %void None %11
  1894. %22 = OpLabel
  1895. %32 = OpVariable %_ptr_Function_float Function
  1896. %color = OpVariable %_ptr_Function_v4float Function
  1897. %param = OpVariable %_ptr_Function_v4float Function
  1898. %23 = OpLoad %v4float %BaseColor
  1899. OpStore %param %23
  1900. %34 = OpAccessChain %_ptr_Function_float %param %uint_0
  1901. %35 = OpLoad %float %34
  1902. %36 = OpAccessChain %_ptr_Function_float %param %uint_1
  1903. %37 = OpLoad %float %36
  1904. %38 = OpFAdd %float %35 %37
  1905. OpStore %32 %38
  1906. %24 = OpLoad %float %32
  1907. %25 = OpCompositeConstruct %v4float %24 %24 %24 %24
  1908. OpStore %color %25
  1909. %26 = OpLoad %v4float %color
  1910. OpStore %gl_FragColor %26
  1911. OpReturn
  1912. OpFunctionEnd
  1913. )";
  1914. const std::string nonEntryFuncs =
  1915. R"(%foo_vf4_ = OpFunction %float None %15
  1916. %bar = OpFunctionParameter %_ptr_Function_v4float
  1917. %27 = OpLabel
  1918. %28 = OpAccessChain %_ptr_Function_float %bar %uint_0
  1919. %29 = OpLoad %float %28
  1920. %30 = OpAccessChain %_ptr_Function_float %bar %uint_1
  1921. %31 = OpLoad %float %30
  1922. %9 = OpFAdd %float %29 %31
  1923. OpReturnValue %9
  1924. OpFunctionEnd
  1925. )";
  1926. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + before + nonEntryFuncs,
  1927. predefs + after + nonEntryFuncs,
  1928. false, true);
  1929. }
  1930. TEST_F(InlineTest, Decorated2) {
  1931. // Same test as Simple with the difference
  1932. // that the Result <id> of the outlined OpFunction
  1933. // is decorated with RelaxedPrecision
  1934. // Expected result is an equal decoration
  1935. // of the created return variable
  1936. //
  1937. // #version 140
  1938. //
  1939. // in vec4 BaseColor;
  1940. //
  1941. // float foo(vec4 bar)
  1942. // {
  1943. // return bar.x + bar.y;
  1944. // }
  1945. //
  1946. // void main()
  1947. // {
  1948. // vec4 color = vec4(foo(BaseColor));
  1949. // gl_FragColor = color;
  1950. // }
  1951. const std::string predefs =
  1952. R"(OpCapability Shader
  1953. %1 = OpExtInstImport "GLSL.std.450"
  1954. OpMemoryModel Logical GLSL450
  1955. OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
  1956. OpExecutionMode %main OriginUpperLeft
  1957. OpSource GLSL 140
  1958. OpName %main "main"
  1959. OpName %foo_vf4_ "foo(vf4;"
  1960. OpName %bar "bar"
  1961. OpName %color "color"
  1962. OpName %BaseColor "BaseColor"
  1963. OpName %param "param"
  1964. OpName %gl_FragColor "gl_FragColor"
  1965. OpDecorate %foo_vf4_ RelaxedPrecision
  1966. )";
  1967. const std::string before =
  1968. R"(%void = OpTypeVoid
  1969. %10 = OpTypeFunction %void
  1970. %float = OpTypeFloat 32
  1971. %v4float = OpTypeVector %float 4
  1972. %_ptr_Function_v4float = OpTypePointer Function %v4float
  1973. %14 = OpTypeFunction %float %_ptr_Function_v4float
  1974. %uint = OpTypeInt 32 0
  1975. %uint_0 = OpConstant %uint 0
  1976. %_ptr_Function_float = OpTypePointer Function %float
  1977. %uint_1 = OpConstant %uint 1
  1978. %_ptr_Input_v4float = OpTypePointer Input %v4float
  1979. %BaseColor = OpVariable %_ptr_Input_v4float Input
  1980. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1981. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  1982. %main = OpFunction %void None %10
  1983. %21 = OpLabel
  1984. %color = OpVariable %_ptr_Function_v4float Function
  1985. %param = OpVariable %_ptr_Function_v4float Function
  1986. %22 = OpLoad %v4float %BaseColor
  1987. OpStore %param %22
  1988. %23 = OpFunctionCall %float %foo_vf4_ %param
  1989. %24 = OpCompositeConstruct %v4float %23 %23 %23 %23
  1990. OpStore %color %24
  1991. %25 = OpLoad %v4float %color
  1992. OpStore %gl_FragColor %25
  1993. OpReturn
  1994. OpFunctionEnd
  1995. )";
  1996. const std::string after =
  1997. R"(OpDecorate %32 RelaxedPrecision
  1998. %void = OpTypeVoid
  1999. %10 = OpTypeFunction %void
  2000. %float = OpTypeFloat 32
  2001. %v4float = OpTypeVector %float 4
  2002. %_ptr_Function_v4float = OpTypePointer Function %v4float
  2003. %14 = OpTypeFunction %float %_ptr_Function_v4float
  2004. %uint = OpTypeInt 32 0
  2005. %uint_0 = OpConstant %uint 0
  2006. %_ptr_Function_float = OpTypePointer Function %float
  2007. %uint_1 = OpConstant %uint 1
  2008. %_ptr_Input_v4float = OpTypePointer Input %v4float
  2009. %BaseColor = OpVariable %_ptr_Input_v4float Input
  2010. %_ptr_Output_v4float = OpTypePointer Output %v4float
  2011. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  2012. %main = OpFunction %void None %10
  2013. %21 = OpLabel
  2014. %32 = OpVariable %_ptr_Function_float Function
  2015. %color = OpVariable %_ptr_Function_v4float Function
  2016. %param = OpVariable %_ptr_Function_v4float Function
  2017. %22 = OpLoad %v4float %BaseColor
  2018. OpStore %param %22
  2019. %34 = OpAccessChain %_ptr_Function_float %param %uint_0
  2020. %35 = OpLoad %float %34
  2021. %36 = OpAccessChain %_ptr_Function_float %param %uint_1
  2022. %37 = OpLoad %float %36
  2023. %38 = OpFAdd %float %35 %37
  2024. OpStore %32 %38
  2025. %23 = OpLoad %float %32
  2026. %24 = OpCompositeConstruct %v4float %23 %23 %23 %23
  2027. OpStore %color %24
  2028. %25 = OpLoad %v4float %color
  2029. OpStore %gl_FragColor %25
  2030. OpReturn
  2031. OpFunctionEnd
  2032. )";
  2033. const std::string nonEntryFuncs =
  2034. R"(%foo_vf4_ = OpFunction %float None %14
  2035. %bar = OpFunctionParameter %_ptr_Function_v4float
  2036. %26 = OpLabel
  2037. %27 = OpAccessChain %_ptr_Function_float %bar %uint_0
  2038. %28 = OpLoad %float %27
  2039. %29 = OpAccessChain %_ptr_Function_float %bar %uint_1
  2040. %30 = OpLoad %float %29
  2041. %31 = OpFAdd %float %28 %30
  2042. OpReturnValue %31
  2043. OpFunctionEnd
  2044. )";
  2045. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + before + nonEntryFuncs,
  2046. predefs + after + nonEntryFuncs,
  2047. false, true);
  2048. }
  2049. TEST_F(InlineTest, DeleteName) {
  2050. // Test that the name of the result id of the call is deleted.
  2051. const std::string before =
  2052. R"(
  2053. OpCapability Shader
  2054. OpMemoryModel Logical GLSL450
  2055. OpEntryPoint Vertex %main "main"
  2056. OpName %main "main"
  2057. OpName %main_entry "main_entry"
  2058. OpName %foo_result "foo_result"
  2059. OpName %void_fn "void_fn"
  2060. OpName %foo "foo"
  2061. OpName %foo_entry "foo_entry"
  2062. %void = OpTypeVoid
  2063. %void_fn = OpTypeFunction %void
  2064. %foo = OpFunction %void None %void_fn
  2065. %foo_entry = OpLabel
  2066. OpReturn
  2067. OpFunctionEnd
  2068. %main = OpFunction %void None %void_fn
  2069. %main_entry = OpLabel
  2070. %foo_result = OpFunctionCall %void %foo
  2071. OpReturn
  2072. OpFunctionEnd
  2073. )";
  2074. const std::string after =
  2075. R"(OpCapability Shader
  2076. OpMemoryModel Logical GLSL450
  2077. OpEntryPoint Vertex %main "main"
  2078. OpName %main "main"
  2079. OpName %main_entry "main_entry"
  2080. OpName %void_fn "void_fn"
  2081. OpName %foo "foo"
  2082. OpName %foo_entry "foo_entry"
  2083. %void = OpTypeVoid
  2084. %void_fn = OpTypeFunction %void
  2085. %foo = OpFunction %void None %void_fn
  2086. %foo_entry = OpLabel
  2087. OpReturn
  2088. OpFunctionEnd
  2089. %main = OpFunction %void None %void_fn
  2090. %main_entry = OpLabel
  2091. OpReturn
  2092. OpFunctionEnd
  2093. )";
  2094. SinglePassRunAndCheck<InlineExhaustivePass>(before, after, false, true);
  2095. }
  2096. TEST_F(InlineTest, SetParent) {
  2097. // Test that after inlining all basic blocks have the correct parent.
  2098. const std::string text =
  2099. R"(
  2100. OpCapability Shader
  2101. OpMemoryModel Logical GLSL450
  2102. OpEntryPoint Vertex %main "main"
  2103. OpName %main "main"
  2104. OpName %main_entry "main_entry"
  2105. OpName %foo_result "foo_result"
  2106. OpName %void_fn "void_fn"
  2107. OpName %foo "foo"
  2108. OpName %foo_entry "foo_entry"
  2109. %void = OpTypeVoid
  2110. %void_fn = OpTypeFunction %void
  2111. %foo = OpFunction %void None %void_fn
  2112. %foo_entry = OpLabel
  2113. OpReturn
  2114. OpFunctionEnd
  2115. %main = OpFunction %void None %void_fn
  2116. %main_entry = OpLabel
  2117. %foo_result = OpFunctionCall %void %foo
  2118. OpReturn
  2119. OpFunctionEnd
  2120. )";
  2121. std::unique_ptr<IRContext> context =
  2122. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  2123. InlineExhaustivePass pass;
  2124. pass.Run(context.get());
  2125. for (Function& func : *context->module()) {
  2126. for (BasicBlock& bb : func) {
  2127. EXPECT_TRUE(bb.GetParent() == &func);
  2128. }
  2129. }
  2130. }
  2131. TEST_F(InlineTest, OpVariableWithInit) {
  2132. // Check that there is a store that corresponds to the initializer. This
  2133. // test makes sure that is a store to the variable in the loop and before any
  2134. // load.
  2135. const std::string text = R"(
  2136. ; CHECK: OpFunction
  2137. ; CHECK-NOT: OpFunctionEnd
  2138. ; CHECK: [[var:%\w+]] = OpVariable %_ptr_Function_float Function %float_0
  2139. ; CHECK: OpLoopMerge [[outer_merge:%\w+]]
  2140. ; CHECK-NOT: OpLoad %float [[var]]
  2141. ; CHECK: OpStore [[var]] %float_0
  2142. ; CHECK: OpFunctionEnd
  2143. OpCapability Shader
  2144. %1 = OpExtInstImport "GLSL.std.450"
  2145. OpMemoryModel Logical GLSL450
  2146. OpEntryPoint Fragment %main "main" %o
  2147. OpExecutionMode %main OriginUpperLeft
  2148. OpSource GLSL 450
  2149. OpDecorate %o Location 0
  2150. %void = OpTypeVoid
  2151. %3 = OpTypeFunction %void
  2152. %float = OpTypeFloat 32
  2153. %7 = OpTypeFunction %float
  2154. %_ptr_Function_float = OpTypePointer Function %float
  2155. %float_0 = OpConstant %float 0
  2156. %bool = OpTypeBool
  2157. %float_1 = OpConstant %float 1
  2158. %_ptr_Output_float = OpTypePointer Output %float
  2159. %o = OpVariable %_ptr_Output_float Output
  2160. %int = OpTypeInt 32 1
  2161. %_ptr_Function_int = OpTypePointer Function %int
  2162. %_ptr_Input_int = OpTypePointer Input %int
  2163. %int_0 = OpConstant %int 0
  2164. %int_1 = OpConstant %int 1
  2165. %int_2 = OpConstant %int 2
  2166. %main = OpFunction %void None %3
  2167. %5 = OpLabel
  2168. OpStore %o %float_0
  2169. OpBranch %34
  2170. %34 = OpLabel
  2171. %39 = OpPhi %int %int_0 %5 %47 %37
  2172. OpLoopMerge %36 %37 None
  2173. OpBranch %38
  2174. %38 = OpLabel
  2175. %41 = OpSLessThan %bool %39 %int_2
  2176. OpBranchConditional %41 %35 %36
  2177. %35 = OpLabel
  2178. %42 = OpFunctionCall %float %foo_
  2179. %43 = OpLoad %float %o
  2180. %44 = OpFAdd %float %43 %42
  2181. OpStore %o %44
  2182. OpBranch %37
  2183. %37 = OpLabel
  2184. %47 = OpIAdd %int %39 %int_1
  2185. OpBranch %34
  2186. %36 = OpLabel
  2187. OpReturn
  2188. OpFunctionEnd
  2189. %foo_ = OpFunction %float None %7
  2190. %9 = OpLabel
  2191. %n = OpVariable %_ptr_Function_float Function %float_0
  2192. %13 = OpLoad %float %n
  2193. %15 = OpFOrdEqual %bool %13 %float_0
  2194. OpSelectionMerge %17 None
  2195. OpBranchConditional %15 %16 %17
  2196. %16 = OpLabel
  2197. %19 = OpLoad %float %n
  2198. %20 = OpFAdd %float %19 %float_1
  2199. OpStore %n %20
  2200. OpBranch %17
  2201. %17 = OpLabel
  2202. %21 = OpLoad %float %n
  2203. OpReturnValue %21
  2204. OpFunctionEnd
  2205. )";
  2206. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  2207. }
  2208. TEST_F(InlineTest, DontInlineDirectlyRecursiveFunc) {
  2209. // Test that the name of the result id of the call is deleted.
  2210. const std::string test =
  2211. R"(OpCapability Shader
  2212. OpMemoryModel Logical GLSL450
  2213. OpEntryPoint Fragment %1 "main"
  2214. OpExecutionMode %1 OriginUpperLeft
  2215. OpDecorate %2 DescriptorSet 439418829
  2216. %void = OpTypeVoid
  2217. %4 = OpTypeFunction %void
  2218. %float = OpTypeFloat 32
  2219. %_struct_6 = OpTypeStruct %float %float
  2220. %15 = OpConstantNull %_struct_6
  2221. %7 = OpTypeFunction %_struct_6
  2222. %1 = OpFunction %void Pure|Const %4
  2223. %8 = OpLabel
  2224. %2 = OpFunctionCall %_struct_6 %9
  2225. OpKill
  2226. OpFunctionEnd
  2227. %9 = OpFunction %_struct_6 None %7
  2228. %10 = OpLabel
  2229. %11 = OpFunctionCall %_struct_6 %9
  2230. OpReturnValue %15
  2231. OpFunctionEnd
  2232. )";
  2233. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2234. SinglePassRunAndCheck<InlineExhaustivePass>(test, test, false, true);
  2235. }
  2236. TEST_F(InlineTest, DontInlineInDirectlyRecursiveFunc) {
  2237. // Test that the name of the result id of the call is deleted.
  2238. const std::string test =
  2239. R"(OpCapability Shader
  2240. OpMemoryModel Logical GLSL450
  2241. OpEntryPoint Fragment %1 "main"
  2242. OpExecutionMode %1 OriginUpperLeft
  2243. OpDecorate %2 DescriptorSet 439418829
  2244. %void = OpTypeVoid
  2245. %4 = OpTypeFunction %void
  2246. %float = OpTypeFloat 32
  2247. %_struct_6 = OpTypeStruct %float %float
  2248. %15 = OpConstantNull %_struct_6
  2249. %7 = OpTypeFunction %_struct_6
  2250. %1 = OpFunction %void Pure|Const %4
  2251. %8 = OpLabel
  2252. %2 = OpFunctionCall %_struct_6 %9
  2253. OpKill
  2254. OpFunctionEnd
  2255. %9 = OpFunction %_struct_6 None %7
  2256. %10 = OpLabel
  2257. %11 = OpFunctionCall %_struct_6 %12
  2258. OpReturnValue %15
  2259. OpFunctionEnd
  2260. %12 = OpFunction %_struct_6 None %7
  2261. %13 = OpLabel
  2262. %14 = OpFunctionCall %_struct_6 %9
  2263. OpReturnValue %15
  2264. OpFunctionEnd
  2265. )";
  2266. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2267. SinglePassRunAndCheck<InlineExhaustivePass>(test, test, false, true);
  2268. }
  2269. TEST_F(InlineTest, DontInlineFuncWithOpKillInContinue) {
  2270. const std::string test =
  2271. R"(OpCapability Shader
  2272. %1 = OpExtInstImport "GLSL.std.450"
  2273. OpMemoryModel Logical GLSL450
  2274. OpEntryPoint Fragment %main "main"
  2275. OpExecutionMode %main OriginUpperLeft
  2276. OpSource GLSL 330
  2277. OpName %main "main"
  2278. OpName %kill_ "kill("
  2279. %void = OpTypeVoid
  2280. %3 = OpTypeFunction %void
  2281. %bool = OpTypeBool
  2282. %true = OpConstantTrue %bool
  2283. %main = OpFunction %void None %3
  2284. %5 = OpLabel
  2285. OpBranch %9
  2286. %9 = OpLabel
  2287. OpLoopMerge %11 %12 None
  2288. OpBranch %13
  2289. %13 = OpLabel
  2290. OpBranchConditional %true %10 %11
  2291. %10 = OpLabel
  2292. OpBranch %12
  2293. %12 = OpLabel
  2294. %16 = OpFunctionCall %void %kill_
  2295. OpBranch %9
  2296. %11 = OpLabel
  2297. OpReturn
  2298. OpFunctionEnd
  2299. %kill_ = OpFunction %void None %3
  2300. %7 = OpLabel
  2301. OpKill
  2302. OpFunctionEnd
  2303. )";
  2304. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2305. SinglePassRunAndCheck<InlineExhaustivePass>(test, test, false, true);
  2306. }
  2307. TEST_F(InlineTest, DontInlineFuncWithDontInline) {
  2308. // Check that the function with DontInline flag is not inlined.
  2309. const std::string text = R"(
  2310. ; CHECK: %foo = OpFunction %int DontInline
  2311. ; CHECK: OpReturnValue %int_0
  2312. OpCapability Shader
  2313. OpMemoryModel Logical GLSL450
  2314. OpEntryPoint Fragment %main "main"
  2315. OpExecutionMode %main OriginUpperLeft
  2316. OpSource HLSL 600
  2317. OpName %main "main"
  2318. OpName %foo "foo"
  2319. %int = OpTypeInt 32 1
  2320. %int_0 = OpConstant %int 0
  2321. %void = OpTypeVoid
  2322. %6 = OpTypeFunction %void
  2323. %7 = OpTypeFunction %int
  2324. %main = OpFunction %void None %6
  2325. %8 = OpLabel
  2326. %9 = OpFunctionCall %int %foo
  2327. OpReturn
  2328. OpFunctionEnd
  2329. %foo = OpFunction %int DontInline %7
  2330. %10 = OpLabel
  2331. OpReturnValue %int_0
  2332. OpFunctionEnd
  2333. )";
  2334. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  2335. }
  2336. TEST_F(InlineTest, InlineFuncWithOpKillNotInContinue) {
  2337. const std::string before =
  2338. R"(OpCapability Shader
  2339. %1 = OpExtInstImport "GLSL.std.450"
  2340. OpMemoryModel Logical GLSL450
  2341. OpEntryPoint Fragment %main "main"
  2342. OpExecutionMode %main OriginUpperLeft
  2343. OpSource GLSL 330
  2344. OpName %main "main"
  2345. OpName %kill_ "kill("
  2346. %void = OpTypeVoid
  2347. %3 = OpTypeFunction %void
  2348. %bool = OpTypeBool
  2349. %true = OpConstantTrue %bool
  2350. %main = OpFunction %void None %3
  2351. %5 = OpLabel
  2352. %16 = OpFunctionCall %void %kill_
  2353. OpReturn
  2354. OpFunctionEnd
  2355. %kill_ = OpFunction %void None %3
  2356. %7 = OpLabel
  2357. OpKill
  2358. OpFunctionEnd
  2359. )";
  2360. const std::string after =
  2361. R"(OpCapability Shader
  2362. %1 = OpExtInstImport "GLSL.std.450"
  2363. OpMemoryModel Logical GLSL450
  2364. OpEntryPoint Fragment %main "main"
  2365. OpExecutionMode %main OriginUpperLeft
  2366. OpSource GLSL 330
  2367. OpName %main "main"
  2368. OpName %kill_ "kill("
  2369. %void = OpTypeVoid
  2370. %3 = OpTypeFunction %void
  2371. %bool = OpTypeBool
  2372. %true = OpConstantTrue %bool
  2373. %main = OpFunction %void None %3
  2374. %5 = OpLabel
  2375. OpKill
  2376. %18 = OpLabel
  2377. OpReturn
  2378. OpFunctionEnd
  2379. %kill_ = OpFunction %void None %3
  2380. %7 = OpLabel
  2381. OpKill
  2382. OpFunctionEnd
  2383. )";
  2384. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2385. SinglePassRunAndCheck<InlineExhaustivePass>(before, after, false, true);
  2386. }
  2387. TEST_F(InlineTest, DontInlineFuncWithOpTerminateInvocationInContinue) {
  2388. const std::string test =
  2389. R"(OpCapability Shader
  2390. OpExtension "SPV_KHR_terminate_invocation"
  2391. %1 = OpExtInstImport "GLSL.std.450"
  2392. OpMemoryModel Logical GLSL450
  2393. OpEntryPoint Fragment %main "main"
  2394. OpExecutionMode %main OriginUpperLeft
  2395. OpSource GLSL 330
  2396. OpName %main "main"
  2397. OpName %kill_ "kill("
  2398. %void = OpTypeVoid
  2399. %3 = OpTypeFunction %void
  2400. %bool = OpTypeBool
  2401. %true = OpConstantTrue %bool
  2402. %main = OpFunction %void None %3
  2403. %5 = OpLabel
  2404. OpBranch %9
  2405. %9 = OpLabel
  2406. OpLoopMerge %11 %12 None
  2407. OpBranch %13
  2408. %13 = OpLabel
  2409. OpBranchConditional %true %10 %11
  2410. %10 = OpLabel
  2411. OpBranch %12
  2412. %12 = OpLabel
  2413. %16 = OpFunctionCall %void %kill_
  2414. OpBranch %9
  2415. %11 = OpLabel
  2416. OpReturn
  2417. OpFunctionEnd
  2418. %kill_ = OpFunction %void None %3
  2419. %7 = OpLabel
  2420. OpTerminateInvocation
  2421. OpFunctionEnd
  2422. )";
  2423. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2424. SinglePassRunAndCheck<InlineExhaustivePass>(test, test, false, true);
  2425. }
  2426. TEST_F(InlineTest, InlineFuncWithOpTerminateInvocationNotInContinue) {
  2427. const std::string before =
  2428. R"(OpCapability Shader
  2429. OpExtension "SPV_KHR_terminate_invocation"
  2430. %1 = OpExtInstImport "GLSL.std.450"
  2431. OpMemoryModel Logical GLSL450
  2432. OpEntryPoint Fragment %main "main"
  2433. OpExecutionMode %main OriginUpperLeft
  2434. OpSource GLSL 330
  2435. OpName %main "main"
  2436. OpName %kill_ "kill("
  2437. %void = OpTypeVoid
  2438. %3 = OpTypeFunction %void
  2439. %bool = OpTypeBool
  2440. %true = OpConstantTrue %bool
  2441. %main = OpFunction %void None %3
  2442. %5 = OpLabel
  2443. %16 = OpFunctionCall %void %kill_
  2444. OpReturn
  2445. OpFunctionEnd
  2446. %kill_ = OpFunction %void None %3
  2447. %7 = OpLabel
  2448. OpTerminateInvocation
  2449. OpFunctionEnd
  2450. )";
  2451. const std::string after =
  2452. R"(OpCapability Shader
  2453. OpExtension "SPV_KHR_terminate_invocation"
  2454. %1 = OpExtInstImport "GLSL.std.450"
  2455. OpMemoryModel Logical GLSL450
  2456. OpEntryPoint Fragment %main "main"
  2457. OpExecutionMode %main OriginUpperLeft
  2458. OpSource GLSL 330
  2459. OpName %main "main"
  2460. OpName %kill_ "kill("
  2461. %void = OpTypeVoid
  2462. %3 = OpTypeFunction %void
  2463. %bool = OpTypeBool
  2464. %true = OpConstantTrue %bool
  2465. %main = OpFunction %void None %3
  2466. %5 = OpLabel
  2467. OpTerminateInvocation
  2468. %18 = OpLabel
  2469. OpReturn
  2470. OpFunctionEnd
  2471. %kill_ = OpFunction %void None %3
  2472. %7 = OpLabel
  2473. OpTerminateInvocation
  2474. OpFunctionEnd
  2475. )";
  2476. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2477. SinglePassRunAndCheck<InlineExhaustivePass>(before, after, false, true);
  2478. }
  2479. TEST_F(InlineTest, InlineFuncWithOpTerminateRayNotInContinue) {
  2480. const std::string text =
  2481. R"(
  2482. OpCapability RayTracingKHR
  2483. OpExtension "SPV_KHR_ray_tracing"
  2484. OpMemoryModel Logical GLSL450
  2485. OpEntryPoint AnyHitKHR %MyAHitMain2 "MyAHitMain2" %a
  2486. OpSource HLSL 630
  2487. OpName %a "a"
  2488. OpName %MyAHitMain2 "MyAHitMain2"
  2489. OpName %param_var_a "param.var.a"
  2490. OpName %src_MyAHitMain2 "src.MyAHitMain2"
  2491. OpName %a_0 "a"
  2492. OpName %bb_entry "bb.entry"
  2493. %int = OpTypeInt 32 1
  2494. %_ptr_IncomingRayPayloadKHR_int = OpTypePointer IncomingRayPayloadKHR %int
  2495. %void = OpTypeVoid
  2496. %6 = OpTypeFunction %void
  2497. %_ptr_Function_int = OpTypePointer Function %int
  2498. %14 = OpTypeFunction %void %_ptr_Function_int
  2499. %a = OpVariable %_ptr_IncomingRayPayloadKHR_int IncomingRayPayloadKHR
  2500. %MyAHitMain2 = OpFunction %void None %6
  2501. %7 = OpLabel
  2502. %param_var_a = OpVariable %_ptr_Function_int Function
  2503. %10 = OpLoad %int %a
  2504. OpStore %param_var_a %10
  2505. %11 = OpFunctionCall %void %src_MyAHitMain2 %param_var_a
  2506. %13 = OpLoad %int %param_var_a
  2507. OpStore %a %13
  2508. OpReturn
  2509. OpFunctionEnd
  2510. %src_MyAHitMain2 = OpFunction %void None %14
  2511. %a_0 = OpFunctionParameter %_ptr_Function_int
  2512. %bb_entry = OpLabel
  2513. %17 = OpLoad %int %a_0
  2514. OpStore %a %17
  2515. OpTerminateRayKHR
  2516. OpFunctionEnd
  2517. ; CHECK: %MyAHitMain2 = OpFunction %void None
  2518. ; CHECK-NEXT: OpLabel
  2519. ; CHECK-NEXT: %param_var_a = OpVariable %_ptr_Function_int Function
  2520. ; CHECK-NEXT: OpLoad %int %a
  2521. ; CHECK-NEXT: OpStore %param_var_a {{%\d+}}
  2522. ; CHECK-NEXT: OpLoad %int %param_var_a
  2523. ; CHECK-NEXT: OpStore %a {{%\d+}}
  2524. ; CHECK-NEXT: OpTerminateRayKHR
  2525. ; CHECK-NEXT: OpLabel
  2526. ; CHECK-NEXT: OpLoad %int %param_var_a
  2527. ; CHECK-NEXT: OpStore %a %16
  2528. ; CHECK-NEXT: OpReturn
  2529. ; CHECK-NEXT: OpFunctionEnd
  2530. )";
  2531. SinglePassRunAndMatch<InlineExhaustivePass>(text, false);
  2532. }
  2533. TEST_F(InlineTest, EarlyReturnFunctionInlined) {
  2534. // #version 140
  2535. //
  2536. // in vec4 BaseColor;
  2537. //
  2538. // float foo(vec4 bar)
  2539. // {
  2540. // if (bar.x < 0.0)
  2541. // return 0.0;
  2542. // return bar.x;
  2543. // }
  2544. //
  2545. // void main()
  2546. // {
  2547. // vec4 color = vec4(foo(BaseColor));
  2548. // gl_FragColor = color;
  2549. // }
  2550. const std::string predefs =
  2551. R"(OpCapability Shader
  2552. %1 = OpExtInstImport "GLSL.std.450"
  2553. OpMemoryModel Logical GLSL450
  2554. OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
  2555. OpExecutionMode %main OriginUpperLeft
  2556. OpSource GLSL 140
  2557. OpName %main "main"
  2558. OpName %foo_vf4_ "foo(vf4;"
  2559. OpName %bar "bar"
  2560. OpName %color "color"
  2561. OpName %BaseColor "BaseColor"
  2562. OpName %param "param"
  2563. OpName %gl_FragColor "gl_FragColor"
  2564. %void = OpTypeVoid
  2565. %10 = OpTypeFunction %void
  2566. %float = OpTypeFloat 32
  2567. %v4float = OpTypeVector %float 4
  2568. %_ptr_Function_v4float = OpTypePointer Function %v4float
  2569. %14 = OpTypeFunction %float %_ptr_Function_v4float
  2570. %uint = OpTypeInt 32 0
  2571. %uint_0 = OpConstant %uint 0
  2572. %_ptr_Function_float = OpTypePointer Function %float
  2573. %float_0 = OpConstant %float 0
  2574. %bool = OpTypeBool
  2575. %_ptr_Input_v4float = OpTypePointer Input %v4float
  2576. %BaseColor = OpVariable %_ptr_Input_v4float Input
  2577. %_ptr_Output_v4float = OpTypePointer Output %v4float
  2578. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  2579. )";
  2580. const std::string foo =
  2581. R"(%foo_vf4_ = OpFunction %float None %14
  2582. %bar = OpFunctionParameter %_ptr_Function_v4float
  2583. %27 = OpLabel
  2584. %28 = OpAccessChain %_ptr_Function_float %bar %uint_0
  2585. %29 = OpLoad %float %28
  2586. %30 = OpFOrdLessThan %bool %29 %float_0
  2587. OpSelectionMerge %31 None
  2588. OpBranchConditional %30 %32 %31
  2589. %32 = OpLabel
  2590. OpReturnValue %float_0
  2591. %31 = OpLabel
  2592. %33 = OpAccessChain %_ptr_Function_float %bar %uint_0
  2593. %34 = OpLoad %float %33
  2594. OpReturnValue %34
  2595. OpFunctionEnd
  2596. )";
  2597. const std::string fooMergeReturn =
  2598. R"(%foo_vf4_ = OpFunction %float None %14
  2599. %bar = OpFunctionParameter %_ptr_Function_v4float
  2600. %27 = OpLabel
  2601. %41 = OpVariable %_ptr_Function_bool Function %false
  2602. %36 = OpVariable %_ptr_Function_float Function
  2603. OpSelectionMerge %35 None
  2604. OpSwitch %uint_0 %38
  2605. %38 = OpLabel
  2606. %28 = OpAccessChain %_ptr_Function_float %bar %uint_0
  2607. %29 = OpLoad %float %28
  2608. %30 = OpFOrdLessThan %bool %29 %float_0
  2609. OpSelectionMerge %31 None
  2610. OpBranchConditional %30 %32 %31
  2611. %32 = OpLabel
  2612. OpStore %41 %true
  2613. OpStore %36 %float_0
  2614. OpBranch %35
  2615. %31 = OpLabel
  2616. %33 = OpAccessChain %_ptr_Function_float %bar %uint_0
  2617. %34 = OpLoad %float %33
  2618. OpStore %41 %true
  2619. OpStore %36 %34
  2620. OpBranch %35
  2621. %35 = OpLabel
  2622. %37 = OpLoad %float %36
  2623. OpReturnValue %37
  2624. OpFunctionEnd
  2625. )";
  2626. const std::string before =
  2627. R"(%main = OpFunction %void None %10
  2628. %22 = OpLabel
  2629. %color = OpVariable %_ptr_Function_v4float Function
  2630. %param = OpVariable %_ptr_Function_v4float Function
  2631. %23 = OpLoad %v4float %BaseColor
  2632. OpStore %param %23
  2633. %24 = OpFunctionCall %float %foo_vf4_ %param
  2634. %25 = OpCompositeConstruct %v4float %24 %24 %24 %24
  2635. OpStore %color %25
  2636. %26 = OpLoad %v4float %color
  2637. OpStore %gl_FragColor %26
  2638. OpReturn
  2639. OpFunctionEnd
  2640. )";
  2641. const std::string after =
  2642. R"(%false = OpConstantFalse %bool
  2643. %_ptr_Function_bool = OpTypePointer Function %bool
  2644. %true = OpConstantTrue %bool
  2645. %main = OpFunction %void None %10
  2646. %22 = OpLabel
  2647. %43 = OpVariable %_ptr_Function_bool Function %false
  2648. %44 = OpVariable %_ptr_Function_float Function
  2649. %45 = OpVariable %_ptr_Function_float Function
  2650. %color = OpVariable %_ptr_Function_v4float Function
  2651. %param = OpVariable %_ptr_Function_v4float Function
  2652. %23 = OpLoad %v4float %BaseColor
  2653. OpStore %param %23
  2654. OpStore %43 %false
  2655. OpSelectionMerge %55 None
  2656. OpSwitch %uint_0 %47
  2657. %47 = OpLabel
  2658. %48 = OpAccessChain %_ptr_Function_float %param %uint_0
  2659. %49 = OpLoad %float %48
  2660. %50 = OpFOrdLessThan %bool %49 %float_0
  2661. OpSelectionMerge %52 None
  2662. OpBranchConditional %50 %51 %52
  2663. %51 = OpLabel
  2664. OpStore %43 %true
  2665. OpStore %44 %float_0
  2666. OpBranch %55
  2667. %52 = OpLabel
  2668. %53 = OpAccessChain %_ptr_Function_float %param %uint_0
  2669. %54 = OpLoad %float %53
  2670. OpStore %43 %true
  2671. OpStore %44 %54
  2672. OpBranch %55
  2673. %55 = OpLabel
  2674. %56 = OpLoad %float %44
  2675. OpStore %45 %56
  2676. %24 = OpLoad %float %45
  2677. %25 = OpCompositeConstruct %v4float %24 %24 %24 %24
  2678. OpStore %color %25
  2679. %26 = OpLoad %v4float %color
  2680. OpStore %gl_FragColor %26
  2681. OpReturn
  2682. OpFunctionEnd
  2683. )";
  2684. // The early return case must be handled by merge-return first.
  2685. AddPass<MergeReturnPass>();
  2686. AddPass<InlineExhaustivePass>();
  2687. RunAndCheck(predefs + before + foo, predefs + after + fooMergeReturn);
  2688. }
  2689. TEST_F(InlineTest, EarlyReturnNotAppearingLastInFunctionInlined) {
  2690. // Example from https://github.com/KhronosGroup/SPIRV-Tools/issues/755
  2691. //
  2692. // Original example is derived from:
  2693. //
  2694. // #version 450
  2695. //
  2696. // float foo() {
  2697. // if (true) {
  2698. // }
  2699. // }
  2700. //
  2701. // void main() { foo(); }
  2702. //
  2703. // But the order of basic blocks in foo is changed so that the return
  2704. // block is listed second-last. There is only one return in the callee
  2705. // but it does not appear last.
  2706. const std::string predefs =
  2707. R"(OpCapability Shader
  2708. OpMemoryModel Logical GLSL450
  2709. OpEntryPoint Vertex %main "main"
  2710. OpSource GLSL 450
  2711. OpName %main "main"
  2712. OpName %foo_ "foo("
  2713. %void = OpTypeVoid
  2714. %4 = OpTypeFunction %void
  2715. %bool = OpTypeBool
  2716. %true = OpConstantTrue %bool
  2717. )";
  2718. const std::string foo =
  2719. R"(%foo_ = OpFunction %void None %4
  2720. %7 = OpLabel
  2721. OpSelectionMerge %8 None
  2722. OpBranchConditional %true %9 %8
  2723. %8 = OpLabel
  2724. OpReturn
  2725. %9 = OpLabel
  2726. OpBranch %8
  2727. OpFunctionEnd
  2728. )";
  2729. const std::string fooMergeReturn =
  2730. R"(%uint = OpTypeInt 32 0
  2731. %uint_0 = OpConstant %uint 0
  2732. %false = OpConstantFalse %bool
  2733. %_ptr_Function_bool = OpTypePointer Function %bool
  2734. %foo_ = OpFunction %void None %4
  2735. %7 = OpLabel
  2736. %18 = OpVariable %_ptr_Function_bool Function %false
  2737. OpSelectionMerge %12 None
  2738. OpSwitch %uint_0 %13
  2739. %13 = OpLabel
  2740. OpSelectionMerge %8 None
  2741. OpBranchConditional %true %9 %8
  2742. %8 = OpLabel
  2743. OpStore %18 %true
  2744. OpBranch %12
  2745. %9 = OpLabel
  2746. OpBranch %8
  2747. %12 = OpLabel
  2748. OpReturn
  2749. OpFunctionEnd
  2750. )";
  2751. const std::string before =
  2752. R"(%main = OpFunction %void None %4
  2753. %10 = OpLabel
  2754. %11 = OpFunctionCall %void %foo_
  2755. OpReturn
  2756. OpFunctionEnd
  2757. )";
  2758. const std::string after =
  2759. R"(%main = OpFunction %void None %4
  2760. %10 = OpLabel
  2761. %19 = OpVariable %_ptr_Function_bool Function %false
  2762. OpStore %19 %false
  2763. OpSelectionMerge %24 None
  2764. OpSwitch %uint_0 %21
  2765. %21 = OpLabel
  2766. OpSelectionMerge %22 None
  2767. OpBranchConditional %true %23 %22
  2768. %22 = OpLabel
  2769. OpStore %19 %true
  2770. OpBranch %24
  2771. %23 = OpLabel
  2772. OpBranch %22
  2773. %24 = OpLabel
  2774. OpReturn
  2775. OpFunctionEnd
  2776. )";
  2777. // The early return case must be handled by merge-return first.
  2778. AddPass<MergeReturnPass>();
  2779. AddPass<InlineExhaustivePass>();
  2780. RunAndCheck(predefs + foo + before, predefs + fooMergeReturn + after);
  2781. }
  2782. TEST_F(InlineTest, CalleeWithSingleReturnNeedsSingleTripLoopWrapper) {
  2783. // The case from https://github.com/KhronosGroup/SPIRV-Tools/issues/2018
  2784. //
  2785. // The callee has a single return, but needs single-trip loop wrapper
  2786. // to be inlined because the return is in a selection structure.
  2787. const std::string predefs =
  2788. R"(OpCapability Shader
  2789. %1 = OpExtInstImport "GLSL.std.450"
  2790. OpMemoryModel Logical GLSL450
  2791. OpEntryPoint Fragment %main "main" %_GLF_color
  2792. OpExecutionMode %main OriginUpperLeft
  2793. OpSource ESSL 310
  2794. OpName %main "main"
  2795. OpName %f_ "f("
  2796. OpName %i "i"
  2797. OpName %_GLF_color "_GLF_color"
  2798. OpDecorate %_GLF_color Location 0
  2799. %void = OpTypeVoid
  2800. %7 = OpTypeFunction %void
  2801. %float = OpTypeFloat 32
  2802. %9 = OpTypeFunction %float
  2803. %float_1 = OpConstant %float 1
  2804. %bool = OpTypeBool
  2805. %false = OpConstantFalse %bool
  2806. %true = OpConstantTrue %bool
  2807. %int = OpTypeInt 32 1
  2808. %_ptr_Function_int = OpTypePointer Function %int
  2809. %int_0 = OpConstant %int 0
  2810. %int_1 = OpConstant %int 1
  2811. %v4float = OpTypeVector %float 4
  2812. %_ptr_Output_v4float = OpTypePointer Output %v4float
  2813. %_GLF_color = OpVariable %_ptr_Output_v4float Output
  2814. %float_0 = OpConstant %float 0
  2815. %21 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
  2816. %22 = OpConstantComposite %v4float %float_0 %float_1 %float_0 %float_1
  2817. )";
  2818. const std::string new_predefs =
  2819. R"(%_ptr_Function_float = OpTypePointer Function %float
  2820. %uint = OpTypeInt 32 0
  2821. %uint_0 = OpConstant %uint 0
  2822. %_ptr_Function_bool = OpTypePointer Function %bool
  2823. )";
  2824. const std::string main_before =
  2825. R"(%main = OpFunction %void None %7
  2826. %23 = OpLabel
  2827. %i = OpVariable %_ptr_Function_int Function
  2828. OpStore %i %int_0
  2829. OpBranch %24
  2830. %24 = OpLabel
  2831. OpLoopMerge %25 %26 None
  2832. OpBranch %27
  2833. %27 = OpLabel
  2834. %28 = OpLoad %int %i
  2835. %29 = OpSLessThan %bool %28 %int_1
  2836. OpBranchConditional %29 %30 %25
  2837. %30 = OpLabel
  2838. OpStore %_GLF_color %21
  2839. %31 = OpFunctionCall %float %f_
  2840. OpBranch %26
  2841. %26 = OpLabel
  2842. %32 = OpLoad %int %i
  2843. %33 = OpIAdd %int %32 %int_1
  2844. OpStore %i %33
  2845. OpBranch %24
  2846. %25 = OpLabel
  2847. OpStore %_GLF_color %22
  2848. OpReturn
  2849. OpFunctionEnd
  2850. )";
  2851. const std::string main_after =
  2852. R"(%main = OpFunction %void None %7
  2853. %23 = OpLabel
  2854. %46 = OpVariable %_ptr_Function_bool Function %false
  2855. %47 = OpVariable %_ptr_Function_float Function
  2856. %48 = OpVariable %_ptr_Function_float Function
  2857. %i = OpVariable %_ptr_Function_int Function
  2858. OpStore %i %int_0
  2859. OpBranch %24
  2860. %24 = OpLabel
  2861. OpLoopMerge %25 %26 None
  2862. OpBranch %27
  2863. %27 = OpLabel
  2864. %28 = OpLoad %int %i
  2865. %29 = OpSLessThan %bool %28 %int_1
  2866. OpBranchConditional %29 %30 %25
  2867. %30 = OpLabel
  2868. OpStore %_GLF_color %21
  2869. OpStore %46 %false
  2870. OpSelectionMerge %53 None
  2871. OpSwitch %uint_0 %50
  2872. %50 = OpLabel
  2873. OpSelectionMerge %52 None
  2874. OpBranchConditional %true %51 %52
  2875. %51 = OpLabel
  2876. OpStore %46 %true
  2877. OpStore %47 %float_1
  2878. OpBranch %53
  2879. %52 = OpLabel
  2880. OpStore %46 %true
  2881. OpStore %47 %float_1
  2882. OpBranch %53
  2883. %53 = OpLabel
  2884. %54 = OpLoad %float %47
  2885. OpStore %48 %54
  2886. %31 = OpLoad %float %48
  2887. OpBranch %26
  2888. %26 = OpLabel
  2889. %32 = OpLoad %int %i
  2890. %33 = OpIAdd %int %32 %int_1
  2891. OpStore %i %33
  2892. OpBranch %24
  2893. %25 = OpLabel
  2894. OpStore %_GLF_color %22
  2895. OpReturn
  2896. OpFunctionEnd
  2897. )";
  2898. const std::string callee =
  2899. R"(%f_ = OpFunction %float None %9
  2900. %34 = OpLabel
  2901. OpSelectionMerge %35 None
  2902. OpBranchConditional %true %36 %35
  2903. %36 = OpLabel
  2904. OpReturnValue %float_1
  2905. %35 = OpLabel
  2906. OpReturnValue %float_1
  2907. OpFunctionEnd
  2908. )";
  2909. const std::string calleeMergeReturn =
  2910. R"(%f_ = OpFunction %float None %9
  2911. %34 = OpLabel
  2912. %45 = OpVariable %_ptr_Function_bool Function %false
  2913. %39 = OpVariable %_ptr_Function_float Function
  2914. OpSelectionMerge %37 None
  2915. OpSwitch %uint_0 %41
  2916. %41 = OpLabel
  2917. OpSelectionMerge %35 None
  2918. OpBranchConditional %true %36 %35
  2919. %36 = OpLabel
  2920. OpStore %45 %true
  2921. OpStore %39 %float_1
  2922. OpBranch %37
  2923. %35 = OpLabel
  2924. OpStore %45 %true
  2925. OpStore %39 %float_1
  2926. OpBranch %37
  2927. %37 = OpLabel
  2928. %40 = OpLoad %float %39
  2929. OpReturnValue %40
  2930. OpFunctionEnd
  2931. )";
  2932. // The early return case must be handled by merge-return first.
  2933. AddPass<MergeReturnPass>();
  2934. AddPass<InlineExhaustivePass>();
  2935. RunAndCheck(predefs + main_before + callee,
  2936. predefs + new_predefs + main_after + calleeMergeReturn);
  2937. }
  2938. TEST_F(InlineTest, ForwardReferencesInPhiInlined) {
  2939. // The basic structure of the test case is like this:
  2940. //
  2941. // int foo() {
  2942. // int result = 1;
  2943. // if (true) {
  2944. // result = 1;
  2945. // }
  2946. // return result;
  2947. // }
  2948. //
  2949. // void main() {
  2950. // int x = foo();
  2951. // }
  2952. //
  2953. // but with modifications: Using Phi instead of load/store, and the
  2954. // return block in foo appears before the "then" block.
  2955. const std::string predefs =
  2956. R"(OpCapability Shader
  2957. %1 = OpExtInstImport "GLSL.std.450"
  2958. OpMemoryModel Logical GLSL450
  2959. OpEntryPoint Vertex %main "main"
  2960. OpSource GLSL 450
  2961. OpName %main "main"
  2962. OpName %foo_ "foo("
  2963. OpName %x "x"
  2964. %void = OpTypeVoid
  2965. %6 = OpTypeFunction %void
  2966. %int = OpTypeInt 32 1
  2967. %8 = OpTypeFunction %int
  2968. %bool = OpTypeBool
  2969. %true = OpConstantTrue %bool
  2970. %int_0 = OpConstant %int 0
  2971. %_ptr_Function_int = OpTypePointer Function %int
  2972. )";
  2973. const std::string callee =
  2974. R"(%foo_ = OpFunction %int None %8
  2975. %13 = OpLabel
  2976. %14 = OpCopyObject %int %int_0
  2977. OpSelectionMerge %15 None
  2978. OpBranchConditional %true %16 %15
  2979. %15 = OpLabel
  2980. %17 = OpPhi %int %14 %13 %18 %16
  2981. OpReturnValue %17
  2982. %16 = OpLabel
  2983. %18 = OpCopyObject %int %int_0
  2984. OpBranch %15
  2985. OpFunctionEnd
  2986. )";
  2987. const std::string calleeMergeReturn =
  2988. R"(%uint = OpTypeInt 32 0
  2989. %uint_0 = OpConstant %uint 0
  2990. %false = OpConstantFalse %bool
  2991. %_ptr_Function_bool = OpTypePointer Function %bool
  2992. %foo_ = OpFunction %int None %8
  2993. %13 = OpLabel
  2994. %29 = OpVariable %_ptr_Function_bool Function %false
  2995. %22 = OpVariable %_ptr_Function_int Function
  2996. OpSelectionMerge %21 None
  2997. OpSwitch %uint_0 %24
  2998. %24 = OpLabel
  2999. %14 = OpCopyObject %int %int_0
  3000. OpSelectionMerge %15 None
  3001. OpBranchConditional %true %16 %15
  3002. %15 = OpLabel
  3003. %17 = OpPhi %int %14 %24 %18 %16
  3004. OpStore %29 %true
  3005. OpStore %22 %17
  3006. OpBranch %21
  3007. %16 = OpLabel
  3008. %18 = OpCopyObject %int %int_0
  3009. OpBranch %15
  3010. %21 = OpLabel
  3011. %23 = OpLoad %int %22
  3012. OpReturnValue %23
  3013. OpFunctionEnd
  3014. )";
  3015. const std::string before =
  3016. R"(%main = OpFunction %void None %6
  3017. %19 = OpLabel
  3018. %x = OpVariable %_ptr_Function_int Function
  3019. %20 = OpFunctionCall %int %foo_
  3020. OpStore %x %20
  3021. OpReturn
  3022. OpFunctionEnd
  3023. )";
  3024. const std::string after =
  3025. R"(%main = OpFunction %void None %6
  3026. %19 = OpLabel
  3027. %30 = OpVariable %_ptr_Function_bool Function %false
  3028. %31 = OpVariable %_ptr_Function_int Function
  3029. %32 = OpVariable %_ptr_Function_int Function
  3030. %x = OpVariable %_ptr_Function_int Function
  3031. OpStore %30 %false
  3032. OpSelectionMerge %40 None
  3033. OpSwitch %uint_0 %34
  3034. %34 = OpLabel
  3035. %35 = OpCopyObject %int %int_0
  3036. OpSelectionMerge %36 None
  3037. OpBranchConditional %true %38 %36
  3038. %36 = OpLabel
  3039. %37 = OpPhi %int %35 %34 %39 %38
  3040. OpStore %30 %true
  3041. OpStore %31 %37
  3042. OpBranch %40
  3043. %38 = OpLabel
  3044. %39 = OpCopyObject %int %int_0
  3045. OpBranch %36
  3046. %40 = OpLabel
  3047. %41 = OpLoad %int %31
  3048. OpStore %32 %41
  3049. %20 = OpLoad %int %32
  3050. OpStore %x %20
  3051. OpReturn
  3052. OpFunctionEnd
  3053. )";
  3054. AddPass<MergeReturnPass>();
  3055. AddPass<InlineExhaustivePass>();
  3056. RunAndCheck(predefs + callee + before, predefs + calleeMergeReturn + after);
  3057. }
  3058. TEST_F(InlineTest, DebugSimple) {
  3059. // Check that it correctly generates DebugInlinedAt and maps it to DebugScope
  3060. // for the inlined function foo().
  3061. const std::string text = R"(
  3062. ; CHECK: [[main_name:%\d+]] = OpString "main"
  3063. ; CHECK: [[foo_name:%\d+]] = OpString "foo"
  3064. ; CHECK: [[dbg_main:%\d+]] = OpExtInst %void {{%\d+}} DebugFunction [[main_name]] {{%\d+}} {{%\d+}} 4 1 {{%\d+}} [[main_name]] FlagIsProtected|FlagIsPrivate 4 [[main:%\d+]]
  3065. ; CHECK: [[dbg_foo:%\d+]] = OpExtInst %void {{%\d+}} DebugFunction [[foo_name]] {{%\d+}} {{%\d+}} 1 1 {{%\d+}} [[foo_name]] FlagIsProtected|FlagIsPrivate 1 [[foo:%\d+]]
  3066. ; CHECK: [[foo_bb:%\d+]] = OpExtInst %void {{%\d+}} DebugLexicalBlock {{%\d+}} 1 14 [[dbg_foo]]
  3067. ; CHECK: [[inlined_at:%\d+]] = OpExtInst %void {{%\d+}} DebugInlinedAt 4 [[dbg_main]]
  3068. ; CHECK: [[main]] = OpFunction %void None
  3069. ; CHECK: {{%\d+}} = OpExtInst %void {{%\d+}} DebugScope [[foo_bb]] [[inlined_at]]
  3070. ; CHECK: [[foo]] = OpFunction %v4float None
  3071. OpCapability Shader
  3072. %1 = OpExtInstImport "OpenCL.DebugInfo.100"
  3073. OpMemoryModel Logical GLSL450
  3074. OpEntryPoint Fragment %main "main" %3 %4
  3075. OpExecutionMode %main OriginUpperLeft
  3076. %5 = OpString "ps.hlsl"
  3077. OpSource HLSL 600 %5
  3078. %6 = OpString "float"
  3079. %main_name = OpString "main"
  3080. %foo_name = OpString "foo"
  3081. OpDecorate %3 Location 0
  3082. OpDecorate %4 Location 0
  3083. %uint = OpTypeInt 32 0
  3084. %uint_32 = OpConstant %uint 32
  3085. %float = OpTypeFloat 32
  3086. %float_1 = OpConstant %float 1
  3087. %v4float = OpTypeVector %float 4
  3088. %14 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  3089. %_ptr_Input_v4float = OpTypePointer Input %v4float
  3090. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3091. %void = OpTypeVoid
  3092. %18 = OpTypeFunction %void
  3093. %19 = OpTypeFunction %v4float
  3094. %3 = OpVariable %_ptr_Input_v4float Input
  3095. %4 = OpVariable %_ptr_Output_v4float Output
  3096. %20 = OpExtInst %void %1 DebugSource %5
  3097. %21 = OpExtInst %void %1 DebugCompilationUnit 1 4 %20 HLSL
  3098. %22 = OpExtInst %void %1 DebugTypeBasic %6 %uint_32 Float
  3099. %23 = OpExtInst %void %1 DebugTypeVector %22 4
  3100. %24 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %23 %23
  3101. %25 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %23
  3102. %dbg_main = OpExtInst %void %1 DebugFunction %main_name %24 %20 4 1 %21 %main_name FlagIsProtected|FlagIsPrivate 4 %main
  3103. %dbg_foo = OpExtInst %void %1 DebugFunction %foo_name %25 %20 1 1 %21 %foo_name FlagIsProtected|FlagIsPrivate 1 %foo
  3104. %29 = OpExtInst %void %1 DebugLexicalBlock %20 1 14 %dbg_foo
  3105. %main = OpFunction %void None %18
  3106. %30 = OpLabel
  3107. %31 = OpExtInst %void %1 DebugScope %dbg_main
  3108. %32 = OpFunctionCall %v4float %foo
  3109. %33 = OpLoad %v4float %3
  3110. %34 = OpFAdd %v4float %32 %33
  3111. OpStore %4 %34
  3112. OpReturn
  3113. OpFunctionEnd
  3114. %foo = OpFunction %v4float None %19
  3115. %35 = OpExtInst %void %1 DebugScope %dbg_foo
  3116. %36 = OpLabel
  3117. %37 = OpExtInst %void %1 DebugScope %29
  3118. OpReturnValue %14
  3119. OpFunctionEnd
  3120. )";
  3121. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  3122. }
  3123. TEST_F(InlineTest, DebugNested) {
  3124. // When function main() calls function zoo() and function zoo() calls
  3125. // function bar() and function bar() calls function foo(), check that
  3126. // the inline pass correctly generates DebugInlinedAt instructions
  3127. // for the nested function calls.
  3128. const std::string text = R"(
  3129. ; CHECK: [[v4f1:%\d+]] = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  3130. ; CHECK: [[v4f2:%\d+]] = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
  3131. ; CHECK: [[v4f3:%\d+]] = OpConstantComposite %v4float %float_3 %float_3 %float_3 %float_3
  3132. ; CHECK: [[color:%\d+]] = OpVariable %_ptr_Input_v4float Input
  3133. ; CHECK: [[dbg_main:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction {{%\d+}} {{%\d+}} {{%\d+}} 10 1 {{%\d+}} {{%\d+}} FlagIsProtected|FlagIsPrivate 10 [[main:%\d+]]
  3134. ; CHECK: [[dbg_foo:%\d+]] = OpExtInst %void [[ext]] DebugFunction {{%\d+}} {{%\d+}} {{%\d+}} 1 1 {{%\d+}} {{%\d+}} FlagIsProtected|FlagIsPrivate 1 [[foo:%\d+]]
  3135. ; CHECK: [[dbg_bar:%\d+]] = OpExtInst %void [[ext]] DebugFunction {{%\d+}} {{%\d+}} {{%\d+}} 4 1 {{%\d+}} {{%\d+}} FlagIsProtected|FlagIsPrivate 4 [[bar:%\d+]]
  3136. ; CHECK: [[dbg_zoo:%\d+]] = OpExtInst %void [[ext]] DebugFunction {{%\d+}} {{%\d+}} {{%\d+}} 7 1 {{%\d+}} {{%\d+}} FlagIsProtected|FlagIsPrivate 7 [[zoo:%\d+]]
  3137. ; CHECK: [[inlined_to_main:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 600 [[dbg_main]]
  3138. ; CHECK: [[inlined_to_zoo:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 700 [[dbg_zoo]] [[inlined_to_main]]
  3139. ; CHECK: [[inlined_to_bar:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 300 [[dbg_bar]] [[inlined_to_zoo]]
  3140. ; CHECK: [[main]] = OpFunction %void None
  3141. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_foo]] [[inlined_to_bar]]
  3142. ; CHECK-NEXT: OpLine {{%\d+}} 100 0
  3143. ; CHECK-NEXT: OpStore {{%\d+}} [[v4f1]]
  3144. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_bar]] [[inlined_to_zoo]]
  3145. ; CHECK-NEXT: OpLine {{%\d+}} 300 0
  3146. ; CHECK-NEXT: [[foo_ret:%\d+]] = OpLoad %v4float
  3147. ; CHECK-NEXT: OpLine {{%\d+}} 400 0
  3148. ; CHECK-NEXT: {{%\d+}} = OpFAdd %v4float [[foo_ret]] [[v4f2]]
  3149. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_zoo]] [[inlined_to_main]]
  3150. ; CHECK-NEXT: OpLine {{%\d+}} 700 0
  3151. ; CHECK-NEXT: [[bar_ret:%\d+]] = OpLoad %v4float
  3152. ; CHECK-NEXT: {{%\d+}} = OpFAdd %v4float [[bar_ret]] [[v4f3]]
  3153. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_main]]
  3154. ; CHECK-NEXT: OpLine {{%\d+}} 600 0
  3155. ; CHECK-NEXT: [[zoo_ret:%\d+]] = OpLoad %v4float
  3156. ; CHECK-NEXT: [[color_val:%\d+]] = OpLoad %v4float [[color]]
  3157. ; CHECK-NEXT: {{%\d+}} = OpFAdd %v4float [[zoo_ret]] [[color_val]]
  3158. ; CHECK: [[foo]] = OpFunction %v4float None
  3159. ; CHECK: [[bar]] = OpFunction %v4float None
  3160. ; CHECK: [[zoo]] = OpFunction %v4float None
  3161. OpCapability Shader
  3162. %1 = OpExtInstImport "OpenCL.DebugInfo.100"
  3163. OpMemoryModel Logical GLSL450
  3164. OpEntryPoint Fragment %main "main" %3 %4
  3165. OpExecutionMode %main OriginUpperLeft
  3166. %5 = OpString "ps.hlsl"
  3167. OpSource HLSL 600 %5
  3168. %6 = OpString "float"
  3169. %7 = OpString "main"
  3170. %8 = OpString "foo"
  3171. %9 = OpString "bar"
  3172. %10 = OpString "zoo"
  3173. OpDecorate %3 Location 0
  3174. OpDecorate %4 Location 0
  3175. %uint = OpTypeInt 32 0
  3176. %uint_32 = OpConstant %uint 32
  3177. %float = OpTypeFloat 32
  3178. %float_1 = OpConstant %float 1
  3179. %float_2 = OpConstant %float 2
  3180. %float_3 = OpConstant %float 3
  3181. %v4float = OpTypeVector %float 4
  3182. %18 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  3183. %19 = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
  3184. %20 = OpConstantComposite %v4float %float_3 %float_3 %float_3 %float_3
  3185. %_ptr_Input_v4float = OpTypePointer Input %v4float
  3186. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3187. %void = OpTypeVoid
  3188. %24 = OpTypeFunction %void
  3189. %25 = OpTypeFunction %v4float
  3190. %3 = OpVariable %_ptr_Input_v4float Input
  3191. %4 = OpVariable %_ptr_Output_v4float Output
  3192. %26 = OpExtInst %void %1 DebugSource %5
  3193. %27 = OpExtInst %void %1 DebugCompilationUnit 1 4 %26 HLSL
  3194. %28 = OpExtInst %void %1 DebugTypeBasic %6 %uint_32 Float
  3195. %29 = OpExtInst %void %1 DebugTypeVector %28 4
  3196. %30 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %29 %29
  3197. %31 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %29
  3198. %32 = OpExtInst %void %1 DebugFunction %7 %30 %26 10 1 %27 %7 FlagIsProtected|FlagIsPrivate 10 %main
  3199. %33 = OpExtInst %void %1 DebugFunction %8 %31 %26 1 1 %27 %8 FlagIsProtected|FlagIsPrivate 1 %foo
  3200. %35 = OpExtInst %void %1 DebugFunction %9 %31 %26 4 1 %27 %9 FlagIsProtected|FlagIsPrivate 4 %bar
  3201. %37 = OpExtInst %void %1 DebugFunction %10 %31 %26 7 1 %27 %10 FlagIsProtected|FlagIsPrivate 7 %zoo
  3202. %main = OpFunction %void None %24
  3203. %39 = OpLabel
  3204. %40 = OpExtInst %void %1 DebugScope %32
  3205. OpLine %5 600 0
  3206. %41 = OpFunctionCall %v4float %zoo
  3207. %42 = OpLoad %v4float %3
  3208. %43 = OpFAdd %v4float %41 %42
  3209. OpStore %4 %43
  3210. OpReturn
  3211. OpFunctionEnd
  3212. %foo = OpFunction %v4float None %25
  3213. %44 = OpExtInst %void %1 DebugScope %33
  3214. %45 = OpLabel
  3215. OpLine %5 100 0
  3216. OpReturnValue %18
  3217. OpFunctionEnd
  3218. OpLine %5 200 0
  3219. %bar = OpFunction %v4float None %25
  3220. %46 = OpExtInst %void %1 DebugScope %35
  3221. %47 = OpLabel
  3222. OpLine %5 300 0
  3223. %48 = OpFunctionCall %v4float %foo
  3224. OpLine %5 400 0
  3225. %49 = OpFAdd %v4float %48 %19
  3226. OpLine %5 500 0
  3227. OpReturnValue %49
  3228. OpFunctionEnd
  3229. %zoo = OpFunction %v4float None %25
  3230. %50 = OpExtInst %void %1 DebugScope %37
  3231. %51 = OpLabel
  3232. OpLine %5 700 0
  3233. %52 = OpFunctionCall %v4float %bar
  3234. %53 = OpFAdd %v4float %52 %20
  3235. OpReturnValue %53
  3236. OpFunctionEnd
  3237. )";
  3238. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  3239. }
  3240. TEST_F(InlineTest, DebugSimpleHLSLPixelShader) {
  3241. const std::string text = R"(
  3242. ; CHECK: [[dbg_main:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction {{%\d+}} {{%\d+}} {{%\d+}} 1 1 {{%\d+}} {{%\d+}} FlagIsProtected|FlagIsPrivate 1 %src_main
  3243. ; CHECK: [[lex_blk:%\d+]] = OpExtInst %void [[ext]] DebugLexicalBlock {{%\d+}} 1 47 [[dbg_main]]
  3244. ; CHECK: %main = OpFunction %void None
  3245. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_main]]
  3246. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugDeclare {{%\d+}} %param_var_color
  3247. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[lex_blk]]
  3248. ; CHECK: OpLine {{%\d+}} 2 10
  3249. ; CHECK: {{%\d+}} = OpLoad %v4float %param_var_color
  3250. ; CHECK: OpLine {{%\d+}} 2 3
  3251. ; CHECK: OpFunctionEnd
  3252. ; CHECK: %src_main = OpFunction %v4float None
  3253. OpCapability Shader
  3254. %1 = OpExtInstImport "OpenCL.DebugInfo.100"
  3255. OpMemoryModel Logical GLSL450
  3256. OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_TARGET
  3257. OpExecutionMode %main OriginUpperLeft
  3258. %5 = OpString "ps.hlsl"
  3259. OpSource HLSL 600 %5
  3260. %14 = OpString "#line 1 \"ps.hlsl\"
  3261. float4 main(float4 color : COLOR) : SV_TARGET {
  3262. return color;
  3263. }
  3264. "
  3265. %17 = OpString "float"
  3266. %21 = OpString "src.main"
  3267. %24 = OpString "color"
  3268. OpName %in_var_COLOR "in.var.COLOR"
  3269. OpName %out_var_SV_TARGET "out.var.SV_TARGET"
  3270. OpName %main "main"
  3271. OpName %param_var_color "param.var.color"
  3272. OpName %src_main "src.main"
  3273. OpName %color "color"
  3274. OpName %bb_entry "bb.entry"
  3275. OpDecorate %in_var_COLOR Location 0
  3276. OpDecorate %out_var_SV_TARGET Location 0
  3277. %uint = OpTypeInt 32 0
  3278. %uint_32 = OpConstant %uint 32
  3279. %float = OpTypeFloat 32
  3280. %v4float = OpTypeVector %float 4
  3281. %_ptr_Input_v4float = OpTypePointer Input %v4float
  3282. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3283. %void = OpTypeVoid
  3284. %27 = OpTypeFunction %void
  3285. %_ptr_Function_v4float = OpTypePointer Function %v4float
  3286. %33 = OpTypeFunction %v4float %_ptr_Function_v4float
  3287. %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
  3288. %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
  3289. %13 = OpExtInst %void %1 DebugExpression
  3290. %15 = OpExtInst %void %1 DebugSource %5 %14
  3291. %16 = OpExtInst %void %1 DebugCompilationUnit 1 4 %15 HLSL
  3292. %18 = OpExtInst %void %1 DebugTypeBasic %17 %uint_32 Float
  3293. %19 = OpExtInst %void %1 DebugTypeVector %18 4
  3294. %20 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %19 %19
  3295. %22 = OpExtInst %void %1 DebugFunction %21 %20 %15 1 1 %16 %21 FlagIsProtected|FlagIsPrivate 1 %src_main
  3296. %25 = OpExtInst %void %1 DebugLocalVariable %24 %19 %15 1 20 %22 FlagIsLocal 0
  3297. %26 = OpExtInst %void %1 DebugLexicalBlock %15 1 47 %22
  3298. %main = OpFunction %void None %27
  3299. %28 = OpLabel
  3300. %param_var_color = OpVariable %_ptr_Function_v4float Function
  3301. %31 = OpLoad %v4float %in_var_COLOR
  3302. OpStore %param_var_color %31
  3303. %32 = OpFunctionCall %v4float %src_main %param_var_color
  3304. OpStore %out_var_SV_TARGET %32
  3305. OpReturn
  3306. OpFunctionEnd
  3307. OpLine %5 1 1
  3308. %src_main = OpFunction %v4float None %33
  3309. %34 = OpExtInst %void %1 DebugScope %22
  3310. %color = OpFunctionParameter %_ptr_Function_v4float
  3311. %36 = OpExtInst %void %1 DebugDeclare %25 %color %13
  3312. %bb_entry = OpLabel
  3313. %38 = OpExtInst %void %1 DebugScope %26
  3314. OpLine %5 2 10
  3315. %39 = OpLoad %v4float %color
  3316. OpLine %5 2 3
  3317. OpReturnValue %39
  3318. OpFunctionEnd
  3319. )";
  3320. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  3321. }
  3322. TEST_F(InlineTest, DebugDeclareForCalleeFunctionParam) {
  3323. // Check that InlinePass correctly generates DebugDeclare instructions
  3324. // for callee function's parameters and maps them to corresponding
  3325. // local variables of caller function.
  3326. const std::string text = R"(
  3327. ; CHECK: [[add:%\d+]] = OpString "add"
  3328. ; CHECK: [[a:%\d+]] = OpString "a"
  3329. ; CHECK: [[b:%\d+]] = OpString "b"
  3330. ; CHECK: [[dbg_add:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction [[add]]
  3331. ; CHECK: [[dbg_a:%\d+]] = OpExtInst %void [[ext]] DebugLocalVariable [[a]]
  3332. ; CHECK: [[dbg_b:%\d+]] = OpExtInst %void [[ext]] DebugLocalVariable [[b]]
  3333. ; CHECK: [[inlinedat:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 5
  3334. ; CHECK: OpStore [[param_a:%\d+]]
  3335. ; CHECK: OpStore [[param_b:%\d+]]
  3336. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_add]] [[inlinedat]]
  3337. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugDeclare [[dbg_a]] [[param_a]]
  3338. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugDeclare [[dbg_b]] [[param_b]]
  3339. OpCapability Shader
  3340. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  3341. OpMemoryModel Logical GLSL450
  3342. OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_TARGET
  3343. OpExecutionMode %main OriginUpperLeft
  3344. %file_name = OpString "ps.hlsl"
  3345. OpSource HLSL 600 %file_name
  3346. %float_name = OpString "float"
  3347. %main_name = OpString "main"
  3348. %add_name = OpString "add"
  3349. %a_name = OpString "a"
  3350. %b_name = OpString "b"
  3351. OpDecorate %in_var_COLOR Location 0
  3352. OpDecorate %out_var_SV_TARGET Location 0
  3353. %uint = OpTypeInt 32 0
  3354. %uint_32 = OpConstant %uint 32
  3355. %float = OpTypeFloat 32
  3356. %float_1 = OpConstant %float 1
  3357. %float_2 = OpConstant %float 2
  3358. %v4float = OpTypeVector %float 4
  3359. %v4f1 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  3360. %v4f2 = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
  3361. %_ptr_Input_v4float = OpTypePointer Input %v4float
  3362. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3363. %_ptr_Function_v4float = OpTypePointer Function %v4float
  3364. %add_fn_type = OpTypeFunction %v4float %_ptr_Function_v4float %_ptr_Function_v4float
  3365. %void = OpTypeVoid
  3366. %void_fn_type = OpTypeFunction %void
  3367. %v4f_fn_type = OpTypeFunction %v4float
  3368. %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
  3369. %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
  3370. %null_expr = OpExtInst %void %ext DebugExpression
  3371. %src = OpExtInst %void %ext DebugSource %file_name
  3372. %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
  3373. %dbg_f = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
  3374. %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_f 4
  3375. %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
  3376. %add_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f %dbg_v4f
  3377. %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 5 1 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
  3378. %dbg_add = OpExtInst %void %ext DebugFunction %add_name %add_ty %src 1 1 %cu %add_name FlagIsProtected|FlagIsPrivate 1 %add
  3379. %dbg_a = OpExtInst %void %ext DebugLocalVariable %a_name %dbg_v4f %src 1 13 %dbg_add FlagIsLocal 0
  3380. %dbg_b = OpExtInst %void %ext DebugLocalVariable %b_name %dbg_v4f %src 1 20 %dbg_add FlagIsLocal 1
  3381. %add_lb = OpExtInst %void %ext DebugLexicalBlock %src 1 23 %dbg_add
  3382. %main = OpFunction %void None %void_fn_type
  3383. %main_bb = OpLabel
  3384. %param_a = OpVariable %_ptr_Function_v4float Function
  3385. %param_b = OpVariable %_ptr_Function_v4float Function
  3386. %scope0 = OpExtInst %void %ext DebugScope %dbg_main
  3387. OpStore %param_a %v4f1
  3388. OpStore %param_b %v4f2
  3389. %result = OpFunctionCall %v4float %add %param_a %param_b
  3390. OpStore %out_var_SV_TARGET %result
  3391. OpReturn
  3392. OpFunctionEnd
  3393. %add = OpFunction %v4float None %add_fn_type
  3394. %scope1 = OpExtInst %void %ext DebugScope %dbg_add
  3395. %a = OpFunctionParameter %_ptr_Function_v4float
  3396. %b = OpFunctionParameter %_ptr_Function_v4float
  3397. %decl0 = OpExtInst %void %ext DebugDeclare %dbg_a %a %null_expr
  3398. %decl1 = OpExtInst %void %ext DebugDeclare %dbg_b %b %null_expr
  3399. %add_bb = OpLabel
  3400. %scope2 = OpExtInst %void %ext DebugScope %add_lb
  3401. %a_val = OpLoad %v4float %a
  3402. %b_val = OpLoad %v4float %b
  3403. %res = OpFAdd %v4float %a_val %b_val
  3404. OpReturnValue %res
  3405. OpFunctionEnd
  3406. )";
  3407. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  3408. }
  3409. TEST_F(InlineTest, DebugDeclareForCalleeLocalVar) {
  3410. // Check that InlinePass correctly generates DebugDeclare instructions
  3411. // for callee function's local variables and maps them to corresponding
  3412. // local variables of caller function.
  3413. const std::string text = R"(
  3414. ; CHECK: [[add:%\d+]] = OpString "add"
  3415. ; CHECK: [[foo:%\d+]] = OpString "foo"
  3416. ; CHECK: [[dbg_add:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction [[add]]
  3417. ; CHECK: [[dbg_foo:%\d+]] = OpExtInst %void [[ext]] DebugLocalVariable [[foo]] {{%\d+}} {{%\d+}} 2 2 [[dbg_add]]
  3418. ; CHECK: [[inlinedat:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 5
  3419. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_add]] [[inlinedat]]
  3420. ; CHECK: [[new_foo:%\d+]] = OpVariable %_ptr_Function_v4float Function
  3421. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_add]] [[inlinedat]]
  3422. ; CHECK: [[a_val:%\d+]] = OpLoad %v4float
  3423. ; CHECK: [[b_val:%\d+]] = OpLoad %v4float
  3424. ; CHECK: [[res:%\d+]] = OpFAdd %v4float [[a_val]] [[b_val]]
  3425. ; CHECK: OpStore [[new_foo]] [[res]]
  3426. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugDeclare [[dbg_foo]] [[new_foo]]
  3427. OpCapability Shader
  3428. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  3429. OpMemoryModel Logical GLSL450
  3430. OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_TARGET
  3431. OpExecutionMode %main OriginUpperLeft
  3432. %file_name = OpString "ps.hlsl"
  3433. OpSource HLSL 600 %file_name
  3434. %float_name = OpString "float"
  3435. %main_name = OpString "main"
  3436. %add_name = OpString "add"
  3437. %foo_name = OpString "foo"
  3438. OpDecorate %in_var_COLOR Location 0
  3439. OpDecorate %out_var_SV_TARGET Location 0
  3440. %uint = OpTypeInt 32 0
  3441. %uint_32 = OpConstant %uint 32
  3442. %float = OpTypeFloat 32
  3443. %float_1 = OpConstant %float 1
  3444. %float_2 = OpConstant %float 2
  3445. %v4float = OpTypeVector %float 4
  3446. %v4f1 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  3447. %v4f2 = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
  3448. %_ptr_Input_v4float = OpTypePointer Input %v4float
  3449. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3450. %_ptr_Function_v4float = OpTypePointer Function %v4float
  3451. %add_fn_type = OpTypeFunction %v4float %_ptr_Function_v4float %_ptr_Function_v4float
  3452. %void = OpTypeVoid
  3453. %void_fn_type = OpTypeFunction %void
  3454. %v4f_fn_type = OpTypeFunction %v4float
  3455. %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
  3456. %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
  3457. %null_expr = OpExtInst %void %ext DebugExpression
  3458. %src = OpExtInst %void %ext DebugSource %file_name
  3459. %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
  3460. %dbg_f = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
  3461. %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_f 4
  3462. %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
  3463. %add_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f %dbg_v4f
  3464. %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 5 1 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
  3465. %dbg_add = OpExtInst %void %ext DebugFunction %add_name %add_ty %src 1 1 %cu %add_name FlagIsProtected|FlagIsPrivate 1 %add
  3466. %dbg_foo = OpExtInst %void %ext DebugLocalVariable %foo_name %dbg_v4f %src 2 2 %dbg_add FlagIsLocal
  3467. %main = OpFunction %void None %void_fn_type
  3468. %main_bb = OpLabel
  3469. %param_a = OpVariable %_ptr_Function_v4float Function
  3470. %param_b = OpVariable %_ptr_Function_v4float Function
  3471. %scope0 = OpExtInst %void %ext DebugScope %dbg_main
  3472. OpStore %param_a %v4f1
  3473. OpStore %param_b %v4f2
  3474. %result = OpFunctionCall %v4float %add %param_a %param_b
  3475. OpStore %out_var_SV_TARGET %result
  3476. OpReturn
  3477. OpFunctionEnd
  3478. %add = OpFunction %v4float None %add_fn_type
  3479. %scope1 = OpExtInst %void %ext DebugScope %dbg_add
  3480. %a = OpFunctionParameter %_ptr_Function_v4float
  3481. %b = OpFunctionParameter %_ptr_Function_v4float
  3482. %add_bb = OpLabel
  3483. %foo = OpVariable %_ptr_Function_v4float Function
  3484. %a_val = OpLoad %v4float %a
  3485. %b_val = OpLoad %v4float %b
  3486. %res = OpFAdd %v4float %a_val %b_val
  3487. OpStore %foo %res
  3488. %decl = OpExtInst %void %ext DebugDeclare %dbg_foo %foo %null_expr
  3489. %foo_val = OpLoad %v4float %foo
  3490. OpReturnValue %foo_val
  3491. OpFunctionEnd
  3492. )";
  3493. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  3494. }
  3495. TEST_F(InlineTest, DebugDeclareMultiple) {
  3496. // Check that InlinePass correctly generates DebugDeclare instructions
  3497. // for callee function's parameters and maps them to corresponding
  3498. // local variables of caller function.
  3499. const std::string text = R"(
  3500. ; CHECK: [[add:%\d+]] = OpString "add"
  3501. ; CHECK: [[a:%\d+]] = OpString "a"
  3502. ; CHECK: [[b:%\d+]] = OpString "b"
  3503. ; CHECK: [[dbg_add:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction [[add]]
  3504. ; CHECK: [[dbg_a:%\d+]] = OpExtInst %void [[ext]] DebugLocalVariable [[a]]
  3505. ; CHECK: [[dbg_b:%\d+]] = OpExtInst %void [[ext]] DebugLocalVariable [[b]]
  3506. ; CHECK: OpFunction
  3507. ; CHECK-NOT: OpFunctionEnd
  3508. ; CHECK: OpStore [[param_a:%\d+]]
  3509. ; CHECK: OpStore [[param_b:%\d+]]
  3510. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_add]]
  3511. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugDeclare [[dbg_a]] [[param_a]]
  3512. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugDeclare [[dbg_b]] [[param_b]]
  3513. ; CHECK: [[a_val:%\d+]] = OpLoad %v4float [[param_a]]
  3514. ; CHECK: OpStore [[foo:%\d+]] [[a_val]]
  3515. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugValue [[dbg_a]] [[foo]]
  3516. OpCapability Shader
  3517. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  3518. OpMemoryModel Logical GLSL450
  3519. OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_TARGET
  3520. OpExecutionMode %main OriginUpperLeft
  3521. %file_name = OpString "ps.hlsl"
  3522. OpSource HLSL 600 %file_name
  3523. %float_name = OpString "float"
  3524. %main_name = OpString "main"
  3525. %add_name = OpString "add"
  3526. %a_name = OpString "a"
  3527. %b_name = OpString "b"
  3528. OpDecorate %in_var_COLOR Location 0
  3529. OpDecorate %out_var_SV_TARGET Location 0
  3530. %uint = OpTypeInt 32 0
  3531. %uint_32 = OpConstant %uint 32
  3532. %float = OpTypeFloat 32
  3533. %float_1 = OpConstant %float 1
  3534. %float_2 = OpConstant %float 2
  3535. %v4float = OpTypeVector %float 4
  3536. %v4f1 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  3537. %v4f2 = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
  3538. %_ptr_Input_v4float = OpTypePointer Input %v4float
  3539. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3540. %_ptr_Function_v4float = OpTypePointer Function %v4float
  3541. %add_fn_type = OpTypeFunction %v4float %_ptr_Function_v4float %_ptr_Function_v4float
  3542. %void = OpTypeVoid
  3543. %void_fn_type = OpTypeFunction %void
  3544. %v4f_fn_type = OpTypeFunction %v4float
  3545. %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
  3546. %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
  3547. %null_expr = OpExtInst %void %ext DebugExpression
  3548. %src = OpExtInst %void %ext DebugSource %file_name
  3549. %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
  3550. %dbg_f = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
  3551. %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_f 4
  3552. %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
  3553. %add_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f %dbg_v4f
  3554. %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 5 1 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
  3555. %dbg_add = OpExtInst %void %ext DebugFunction %add_name %add_ty %src 1 1 %cu %add_name FlagIsProtected|FlagIsPrivate 1 %add
  3556. %dbg_a = OpExtInst %void %ext DebugLocalVariable %a_name %dbg_v4f %src 1 13 %dbg_add FlagIsLocal 0
  3557. %dbg_b = OpExtInst %void %ext DebugLocalVariable %b_name %dbg_v4f %src 1 20 %dbg_add FlagIsLocal 1
  3558. %main = OpFunction %void None %void_fn_type
  3559. %main_bb = OpLabel
  3560. %param_a = OpVariable %_ptr_Function_v4float Function
  3561. %param_b = OpVariable %_ptr_Function_v4float Function
  3562. %scope0 = OpExtInst %void %ext DebugScope %dbg_main
  3563. OpStore %param_a %v4f1
  3564. OpStore %param_b %v4f2
  3565. %result = OpFunctionCall %v4float %add %param_a %param_b
  3566. OpStore %out_var_SV_TARGET %result
  3567. OpReturn
  3568. OpFunctionEnd
  3569. %add = OpFunction %v4float None %add_fn_type
  3570. %scope1 = OpExtInst %void %ext DebugScope %dbg_add
  3571. %a = OpFunctionParameter %_ptr_Function_v4float
  3572. %b = OpFunctionParameter %_ptr_Function_v4float
  3573. %decl0 = OpExtInst %void %ext DebugDeclare %dbg_a %a %null_expr
  3574. %add_bb = OpLabel
  3575. %decl1 = OpExtInst %void %ext DebugDeclare %dbg_b %b %null_expr
  3576. %foo = OpVariable %_ptr_Function_v4float Function
  3577. %a_val = OpLoad %v4float %a
  3578. OpStore %foo %a_val
  3579. %dbg_val = OpExtInst %void %ext DebugValue %dbg_a %foo %null_expr
  3580. %b_val = OpLoad %v4float %b
  3581. %res = OpFAdd %v4float %a_val %b_val
  3582. OpReturnValue %res
  3583. OpFunctionEnd
  3584. )";
  3585. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  3586. }
  3587. TEST_F(InlineTest, DebugValueForFunctionCallReturn) {
  3588. // Check that InlinePass correctly generates DebugValue instruction
  3589. // for function call's return value and maps it to a corresponding
  3590. // value in the caller function.
  3591. const std::string text = R"(
  3592. ; CHECK: [[main:%\d+]] = OpString "main"
  3593. ; CHECK: [[add:%\d+]] = OpString "add"
  3594. ; CHECK: [[result:%\d+]] = OpString "result"
  3595. ; CHECK: [[dbg_main:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction [[main]]
  3596. ; CHECK: [[dbg_add:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction [[add]]
  3597. ; CHECK: [[dbg_result:%\d+]] = OpExtInst %void [[ext]] DebugLocalVariable [[result]] {{%\d+}} {{%\d+}} 6 2 [[dbg_main]]
  3598. ; CHECK: [[inlinedat:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 5
  3599. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_add]] [[inlinedat]]
  3600. ; CHECK: [[a_val:%\d+]] = OpLoad %v4float
  3601. ; CHECK: [[b_val:%\d+]] = OpLoad %v4float
  3602. ; CHECK: [[res:%\d+]] = OpFAdd %v4float [[a_val]] [[b_val]]
  3603. ; CHECK: OpStore [[new_result:%\d+]] [[res]]
  3604. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_main]]
  3605. ; CHECK: [[result_val:%\d+]] = OpLoad %v4float [[new_result]]
  3606. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugValue [[dbg_result]] [[result_val]]
  3607. OpCapability Shader
  3608. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  3609. OpMemoryModel Logical GLSL450
  3610. OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_TARGET
  3611. OpExecutionMode %main OriginUpperLeft
  3612. %file_name = OpString "ps.hlsl"
  3613. OpSource HLSL 600 %file_name
  3614. %float_name = OpString "float"
  3615. %main_name = OpString "main"
  3616. %add_name = OpString "add"
  3617. %result_name = OpString "result"
  3618. OpDecorate %in_var_COLOR Location 0
  3619. OpDecorate %out_var_SV_TARGET Location 0
  3620. %uint = OpTypeInt 32 0
  3621. %uint_32 = OpConstant %uint 32
  3622. %float = OpTypeFloat 32
  3623. %float_1 = OpConstant %float 1
  3624. %float_2 = OpConstant %float 2
  3625. %v4float = OpTypeVector %float 4
  3626. %v4f1 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  3627. %v4f2 = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
  3628. %_ptr_Input_v4float = OpTypePointer Input %v4float
  3629. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3630. %_ptr_Function_v4float = OpTypePointer Function %v4float
  3631. %add_fn_type = OpTypeFunction %v4float %_ptr_Function_v4float %_ptr_Function_v4float
  3632. %void = OpTypeVoid
  3633. %void_fn_type = OpTypeFunction %void
  3634. %v4f_fn_type = OpTypeFunction %v4float
  3635. %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
  3636. %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
  3637. %null_expr = OpExtInst %void %ext DebugExpression
  3638. %src = OpExtInst %void %ext DebugSource %file_name
  3639. %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
  3640. %dbg_f = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
  3641. %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_f 4
  3642. %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
  3643. %add_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f %dbg_v4f
  3644. %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 5 1 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
  3645. %dbg_add = OpExtInst %void %ext DebugFunction %add_name %add_ty %src 1 1 %cu %add_name FlagIsProtected|FlagIsPrivate 1 %add
  3646. %dbg_result = OpExtInst %void %ext DebugLocalVariable %result_name %dbg_v4f %src 6 2 %dbg_main FlagIsLocal
  3647. %main = OpFunction %void None %void_fn_type
  3648. %main_bb = OpLabel
  3649. %param_a = OpVariable %_ptr_Function_v4float Function
  3650. %param_b = OpVariable %_ptr_Function_v4float Function
  3651. %scope0 = OpExtInst %void %ext DebugScope %dbg_main
  3652. OpStore %param_a %v4f1
  3653. OpStore %param_b %v4f2
  3654. %result = OpFunctionCall %v4float %add %param_a %param_b
  3655. %value = OpExtInst %void %ext DebugValue %dbg_result %result %null_expr
  3656. OpStore %out_var_SV_TARGET %result
  3657. OpReturn
  3658. OpFunctionEnd
  3659. %add = OpFunction %v4float None %add_fn_type
  3660. %scope1 = OpExtInst %void %ext DebugScope %dbg_add
  3661. %a = OpFunctionParameter %_ptr_Function_v4float
  3662. %b = OpFunctionParameter %_ptr_Function_v4float
  3663. %add_bb = OpLabel
  3664. %a_val = OpLoad %v4float %a
  3665. %b_val = OpLoad %v4float %b
  3666. %res = OpFAdd %v4float %a_val %b_val
  3667. OpReturnValue %res
  3668. OpFunctionEnd
  3669. )";
  3670. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  3671. }
  3672. TEST_F(InlineTest, NestedWithAnExistingDebugInlinedAt) {
  3673. // When a DebugScope instruction in a callee function already has a
  3674. // DebugInlinedAt information, we have to create a recursive
  3675. // DebugInlinedAt chain. See inlined_to_zoo and inlined_to_bar in
  3676. // the following code.
  3677. const std::string text = R"(
  3678. ; CHECK: [[main:%\d+]] = OpString "main"
  3679. ; CHECK: [[foo:%\d+]] = OpString "foo"
  3680. ; CHECK: [[bar:%\d+]] = OpString "bar"
  3681. ; CHECK: [[zoo:%\d+]] = OpString "zoo"
  3682. ; CHECK: [[v4f1:%\d+]] = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  3683. ; CHECK: [[v4f2:%\d+]] = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
  3684. ; CHECK: [[v4f3:%\d+]] = OpConstantComposite %v4float %float_3 %float_3 %float_3 %float_3
  3685. ; CHECK: [[dbg_main:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction [[main]]
  3686. ; CHECK: [[dbg_foo:%\d+]] = OpExtInst %void [[ext]] DebugFunction [[foo]]
  3687. ; CHECK: [[dbg_bar:%\d+]] = OpExtInst %void [[ext]] DebugFunction [[bar]]
  3688. ; CHECK: [[dbg_zoo:%\d+]] = OpExtInst %void [[ext]] DebugFunction [[zoo]]
  3689. ; CHECK: [[inlined_to_main:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 10 [[dbg_main]]
  3690. ; CHECK: [[inlined_to_zoo:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 7 [[dbg_zoo]] [[inlined_to_main]]
  3691. ; CHECK: [[inlined_to_main:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 10 [[dbg_main]]
  3692. ; CHECK: [[inlined_to_bar:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 4 [[dbg_bar]] [[inlined_to_zoo]]
  3693. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_foo]] [[inlined_to_bar]]
  3694. ; CHECK: OpStore [[foo_ret:%\d+]] [[v4f1]]
  3695. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_bar]] [[inlined_to_zoo]]
  3696. ; CHECK: [[foo_ret_val:%\d+]] = OpLoad %v4float [[foo_ret]]
  3697. ; CHECK: [[bar_ret:%\d+]] = OpFAdd %v4float [[foo_ret_val]] [[v4f2]]
  3698. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_zoo]] [[inlined_to_main]]
  3699. ; CHECK: [[zoo_result:%\d+]] = OpFAdd %v4float [[bar_ret]] [[v4f3]]
  3700. ; CHECK: OpStore [[zoo_ret:%\d+]] [[zoo_result]]
  3701. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_main]]
  3702. ; CHECK: [[zoo_ret_val:%\d+]] = OpLoad %v4float [[zoo_ret]]
  3703. ; CHECK: {{%\d+}} = OpFAdd %v4float [[zoo_ret_val]] {{%\d+}}
  3704. OpCapability Shader
  3705. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  3706. OpMemoryModel Logical GLSL450
  3707. OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_TARGET
  3708. OpExecutionMode %main OriginUpperLeft
  3709. %file_name = OpString "ps.hlsl"
  3710. OpSource HLSL 600 %file_name
  3711. %float_name = OpString "float"
  3712. %main_name = OpString "main"
  3713. %foo_name = OpString "foo"
  3714. %bar_name = OpString "bar"
  3715. %zoo_name = OpString "zoo"
  3716. OpDecorate %in_var_COLOR Location 0
  3717. OpDecorate %out_var_SV_TARGET Location 0
  3718. %uint = OpTypeInt 32 0
  3719. %uint_32 = OpConstant %uint 32
  3720. %float = OpTypeFloat 32
  3721. %float_1 = OpConstant %float 1
  3722. %float_2 = OpConstant %float 2
  3723. %float_3 = OpConstant %float 3
  3724. %v4float = OpTypeVector %float 4
  3725. %v4f1 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  3726. %v4f2 = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
  3727. %v4f3 = OpConstantComposite %v4float %float_3 %float_3 %float_3 %float_3
  3728. %_ptr_Input_v4float = OpTypePointer Input %v4float
  3729. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3730. %void = OpTypeVoid
  3731. %void_fn_type = OpTypeFunction %void
  3732. %v4f_fn_type = OpTypeFunction %v4float
  3733. %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
  3734. %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
  3735. %src = OpExtInst %void %ext DebugSource %file_name
  3736. %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
  3737. %dbg_f = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
  3738. %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_f 4
  3739. %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
  3740. %foo_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f
  3741. %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 10 1 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
  3742. %dbg_foo = OpExtInst %void %ext DebugFunction %foo_name %foo_ty %src 1 1 %cu %foo_name FlagIsProtected|FlagIsPrivate 1 %foo
  3743. %dbg_bar = OpExtInst %void %ext DebugFunction %bar_name %foo_ty %src 4 1 %cu %bar_name FlagIsProtected|FlagIsPrivate 4 %bar
  3744. %dbg_zoo = OpExtInst %void %ext DebugFunction %zoo_name %foo_ty %src 7 1 %cu %zoo_name FlagIsProtected|FlagIsPrivate 7 %zoo
  3745. %inlined_to_zoo = OpExtInst %void %ext DebugInlinedAt 7 %dbg_zoo
  3746. %main = OpFunction %void None %void_fn_type
  3747. %main_bb = OpLabel
  3748. %scope0 = OpExtInst %void %ext DebugScope %dbg_main
  3749. %zoo_val = OpFunctionCall %v4float %zoo
  3750. %color = OpLoad %v4float %in_var_COLOR
  3751. %result = OpFAdd %v4float %zoo_val %color
  3752. OpStore %out_var_SV_TARGET %result
  3753. OpReturn
  3754. OpFunctionEnd
  3755. %foo = OpFunction %v4float None %v4f_fn_type
  3756. %scope1 = OpExtInst %void %ext DebugScope %dbg_foo
  3757. %foo_bb = OpLabel
  3758. OpReturnValue %v4f1
  3759. OpFunctionEnd
  3760. %zoo = OpFunction %v4float None %v4f_fn_type
  3761. %scope3 = OpExtInst %void %ext DebugScope %dbg_zoo
  3762. %zoo_bb = OpLabel
  3763. %scope2 = OpExtInst %void %ext DebugScope %dbg_bar %inlined_to_zoo
  3764. %foo_val = OpFunctionCall %v4float %foo
  3765. %bar_val = OpFAdd %v4float %foo_val %v4f2
  3766. %scope4 = OpExtInst %void %ext DebugScope %dbg_zoo
  3767. %zoo_ret = OpFAdd %v4float %bar_val %v4f3
  3768. OpReturnValue %zoo_ret
  3769. OpFunctionEnd
  3770. %bar = OpFunction %v4float None %v4f_fn_type
  3771. %scope5 = OpExtInst %void %ext DebugScope %dbg_bar
  3772. %bar_bb = OpLabel
  3773. %foo_val0 = OpFunctionCall %v4float %foo
  3774. %bar_ret = OpFAdd %v4float %foo_val0 %v4f2
  3775. OpReturnValue %bar_ret
  3776. OpFunctionEnd
  3777. )";
  3778. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  3779. }
  3780. // TODO(greg-lunarg): Add tests to verify handling of these cases:
  3781. //
  3782. // Empty modules
  3783. // Modules without function definitions
  3784. // Modules in which all functions do not call other functions
  3785. // Caller and callee both accessing the same global variable
  3786. // Functions with OpLine & OpNoLine
  3787. // Others?
  3788. // TODO(dneto): Test suggestions from code review
  3789. // https://github.com/KhronosGroup/SPIRV-Tools/pull/534
  3790. //
  3791. // Callee function returns a value generated outside the callee,
  3792. // e.g. a constant value. This might exercise some logic not yet
  3793. // exercised by the current tests: the false branch in the "if"
  3794. // inside the SpvOpReturnValue case in InlinePass::GenInlineCode?
  3795. // SampledImage before function call, but callee is only single block.
  3796. // Then the SampledImage instruction is not cloned. Documents existing
  3797. // behaviour.
  3798. // SampledImage after function call. It is not cloned or changed.
  3799. } // namespace
  3800. } // namespace opt
  3801. } // namespace spvtools