inline_test.cpp 160 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573
  1. // Copyright (c) 2017-2022 Valve Corporation
  2. // Copyright (c) 2017-2022 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 %15 None
  1491. OpBranch %14
  1492. %14 = OpLabel
  1493. OpBranch %15
  1494. %15 = OpLabel
  1495. OpBranchConditional %true %10 %12
  1496. %12 = OpLabel
  1497. OpReturn
  1498. OpFunctionEnd
  1499. )";
  1500. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + nonEntryFuncs + before,
  1501. predefs + nonEntryFuncs + after,
  1502. false, true);
  1503. }
  1504. TEST_F(InlineTest, MultiBlockLoopHeaderCallsMultiBlockCallee) {
  1505. // Like SingleBlockLoopCallsMultiBlockCallee but the loop has several
  1506. // blocks, but the function call still occurs in the loop header.
  1507. // Example from https://github.com/KhronosGroup/SPIRV-Tools/issues/800
  1508. const std::string predefs =
  1509. R"(OpCapability Shader
  1510. OpMemoryModel Logical GLSL450
  1511. OpEntryPoint GLCompute %1 "main"
  1512. OpSource OpenCL_C 120
  1513. %bool = OpTypeBool
  1514. %true = OpConstantTrue %bool
  1515. %int = OpTypeInt 32 1
  1516. %int_1 = OpConstant %int 1
  1517. %int_2 = OpConstant %int 2
  1518. %int_3 = OpConstant %int 3
  1519. %int_4 = OpConstant %int 4
  1520. %int_5 = OpConstant %int 5
  1521. %void = OpTypeVoid
  1522. %11 = OpTypeFunction %void
  1523. )";
  1524. const std::string nonEntryFuncs =
  1525. R"(%12 = OpFunction %void None %11
  1526. %13 = OpLabel
  1527. %14 = OpCopyObject %int %int_1
  1528. OpBranch %15
  1529. %15 = OpLabel
  1530. %16 = OpCopyObject %int %int_2
  1531. OpReturn
  1532. OpFunctionEnd
  1533. )";
  1534. const std::string before =
  1535. R"(%1 = OpFunction %void None %11
  1536. %17 = OpLabel
  1537. OpBranch %18
  1538. %18 = OpLabel
  1539. %19 = OpCopyObject %int %int_3
  1540. %20 = OpFunctionCall %void %12
  1541. %21 = OpCopyObject %int %int_4
  1542. OpLoopMerge %22 %23 None
  1543. OpBranchConditional %true %23 %22
  1544. %23 = OpLabel
  1545. %24 = OpCopyObject %int %int_5
  1546. OpBranchConditional %true %18 %22
  1547. %22 = OpLabel
  1548. OpReturn
  1549. OpFunctionEnd
  1550. )";
  1551. const std::string after =
  1552. R"(%1 = OpFunction %void None %11
  1553. %17 = OpLabel
  1554. OpBranch %18
  1555. %18 = OpLabel
  1556. %19 = OpCopyObject %int %int_3
  1557. %26 = OpCopyObject %int %int_1
  1558. OpLoopMerge %22 %23 None
  1559. OpBranch %27
  1560. %27 = OpLabel
  1561. %28 = OpCopyObject %int %int_2
  1562. %21 = OpCopyObject %int %int_4
  1563. OpBranchConditional %true %23 %22
  1564. %23 = OpLabel
  1565. %24 = OpCopyObject %int %int_5
  1566. OpBranchConditional %true %18 %22
  1567. %22 = OpLabel
  1568. OpReturn
  1569. OpFunctionEnd
  1570. )";
  1571. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + nonEntryFuncs + before,
  1572. predefs + nonEntryFuncs + after,
  1573. false, true);
  1574. }
  1575. TEST_F(InlineTest, SingleBlockLoopCallsMultiBlockCalleeHavingSelectionMerge) {
  1576. // This is similar to SingleBlockLoopCallsMultiBlockCallee except
  1577. // that calleee block also has a merge instruction in its first block.
  1578. // That merge instruction must be an OpSelectionMerge (because the entry
  1579. // block of a function can't be the header of a loop since the entry
  1580. // block can't be the target of a branch).
  1581. //
  1582. // In this case the OpLoopMerge can't be placed in the same block as
  1583. // the OpSelectionMerge, so inlining must create a new block to contain
  1584. // the callee contents.
  1585. //
  1586. // Additionally, we have two extra OpCopyObject instructions to prove that
  1587. // the OpLoopMerge is moved to the right location.
  1588. //
  1589. // Also ensure that OpPhis within the cloned callee code are valid.
  1590. // We need to test that the predecessor blocks are remapped correctly so that
  1591. // dominance rules are satisfied
  1592. const std::string predefs =
  1593. R"(OpCapability Shader
  1594. OpMemoryModel Logical GLSL450
  1595. OpEntryPoint GLCompute %1 "main"
  1596. OpSource OpenCL_C 120
  1597. %bool = OpTypeBool
  1598. %true = OpConstantTrue %bool
  1599. %false = OpConstantFalse %bool
  1600. %void = OpTypeVoid
  1601. %6 = OpTypeFunction %void
  1602. )";
  1603. // This callee has multiple blocks, and an OpPhi in the last block
  1604. // that references a value from the first block. This tests that
  1605. // cloned block IDs are remapped appropriately. The OpPhi dominance
  1606. // requires that the remapped %9 must be in a block that dominates
  1607. // the remapped %8.
  1608. const std::string nonEntryFuncs =
  1609. R"(%7 = OpFunction %void None %6
  1610. %8 = OpLabel
  1611. %9 = OpCopyObject %bool %true
  1612. OpSelectionMerge %10 None
  1613. OpBranchConditional %true %10 %10
  1614. %10 = OpLabel
  1615. %11 = OpPhi %bool %9 %8
  1616. OpReturn
  1617. OpFunctionEnd
  1618. )";
  1619. const std::string before =
  1620. R"(%1 = OpFunction %void None %6
  1621. %12 = OpLabel
  1622. OpBranch %13
  1623. %13 = OpLabel
  1624. %14 = OpCopyObject %bool %false
  1625. %15 = OpFunctionCall %void %7
  1626. OpLoopMerge %16 %13 None
  1627. OpBranchConditional %true %13 %16
  1628. %16 = OpLabel
  1629. OpReturn
  1630. OpFunctionEnd
  1631. )";
  1632. // Note the remapped Phi uses %17 as the parent instead
  1633. // of %13, demonstrating that the parent block has been remapped
  1634. // correctly.
  1635. const std::string after =
  1636. R"(%1 = OpFunction %void None %6
  1637. %12 = OpLabel
  1638. OpBranch %13
  1639. %13 = OpLabel
  1640. %14 = OpCopyObject %bool %false
  1641. OpLoopMerge %16 %22 None
  1642. OpBranch %17
  1643. %17 = OpLabel
  1644. %19 = OpCopyObject %bool %true
  1645. OpSelectionMerge %20 None
  1646. OpBranchConditional %true %20 %20
  1647. %20 = OpLabel
  1648. %21 = OpPhi %bool %19 %17
  1649. OpBranch %22
  1650. %22 = OpLabel
  1651. OpBranchConditional %true %13 %16
  1652. %16 = OpLabel
  1653. OpReturn
  1654. OpFunctionEnd
  1655. )";
  1656. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + nonEntryFuncs + before,
  1657. predefs + nonEntryFuncs + after,
  1658. false, true);
  1659. }
  1660. TEST_F(InlineTest,
  1661. MultiBlockLoopHeaderCallsFromToMultiBlockCalleeHavingSelectionMerge) {
  1662. // This is similar to SingleBlockLoopCallsMultiBlockCalleeHavingSelectionMerge
  1663. // but the call is in the header block of a multi block loop.
  1664. const std::string predefs =
  1665. R"(OpCapability Shader
  1666. OpMemoryModel Logical GLSL450
  1667. OpEntryPoint GLCompute %1 "main"
  1668. OpSource OpenCL_C 120
  1669. %bool = OpTypeBool
  1670. %true = OpConstantTrue %bool
  1671. %int = OpTypeInt 32 1
  1672. %int_1 = OpConstant %int 1
  1673. %int_2 = OpConstant %int 2
  1674. %int_3 = OpConstant %int 3
  1675. %int_4 = OpConstant %int 4
  1676. %int_5 = OpConstant %int 5
  1677. %void = OpTypeVoid
  1678. %11 = OpTypeFunction %void
  1679. )";
  1680. const std::string nonEntryFuncs =
  1681. R"(%12 = OpFunction %void None %11
  1682. %13 = OpLabel
  1683. %14 = OpCopyObject %int %int_1
  1684. OpSelectionMerge %15 None
  1685. OpBranchConditional %true %15 %15
  1686. %15 = OpLabel
  1687. %16 = OpCopyObject %int %int_2
  1688. OpReturn
  1689. OpFunctionEnd
  1690. )";
  1691. const std::string before =
  1692. R"(%1 = OpFunction %void None %11
  1693. %17 = OpLabel
  1694. OpBranch %18
  1695. %18 = OpLabel
  1696. %19 = OpCopyObject %int %int_3
  1697. %20 = OpFunctionCall %void %12
  1698. %21 = OpCopyObject %int %int_4
  1699. OpLoopMerge %22 %23 None
  1700. OpBranchConditional %true %23 %22
  1701. %23 = OpLabel
  1702. %24 = OpCopyObject %int %int_5
  1703. OpBranchConditional %true %18 %22
  1704. %22 = OpLabel
  1705. OpReturn
  1706. OpFunctionEnd
  1707. )";
  1708. const std::string after =
  1709. R"(%1 = OpFunction %void None %11
  1710. %17 = OpLabel
  1711. OpBranch %18
  1712. %18 = OpLabel
  1713. %19 = OpCopyObject %int %int_3
  1714. OpLoopMerge %22 %23 None
  1715. OpBranch %25
  1716. %25 = OpLabel
  1717. %27 = OpCopyObject %int %int_1
  1718. OpSelectionMerge %28 None
  1719. OpBranchConditional %true %28 %28
  1720. %28 = OpLabel
  1721. %29 = OpCopyObject %int %int_2
  1722. %21 = OpCopyObject %int %int_4
  1723. OpBranchConditional %true %23 %22
  1724. %23 = OpLabel
  1725. %24 = OpCopyObject %int %int_5
  1726. OpBranchConditional %true %18 %22
  1727. %22 = OpLabel
  1728. OpReturn
  1729. OpFunctionEnd
  1730. )";
  1731. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + nonEntryFuncs + before,
  1732. predefs + nonEntryFuncs + after,
  1733. false, true);
  1734. }
  1735. TEST_F(InlineTest, NonInlinableCalleeWithSingleReturn) {
  1736. // The case from https://github.com/KhronosGroup/SPIRV-Tools/issues/2018
  1737. //
  1738. // The callee has a single return, but cannot be inlined because the
  1739. // return is inside a loop.
  1740. const std::string predefs =
  1741. R"(OpCapability Shader
  1742. %1 = OpExtInstImport "GLSL.std.450"
  1743. OpMemoryModel Logical GLSL450
  1744. OpEntryPoint Fragment %main "main" %_GLF_color
  1745. OpExecutionMode %main OriginUpperLeft
  1746. OpSource ESSL 310
  1747. OpName %main "main"
  1748. OpName %f_ "f("
  1749. OpName %i "i"
  1750. OpName %_GLF_color "_GLF_color"
  1751. OpDecorate %_GLF_color Location 0
  1752. %void = OpTypeVoid
  1753. %7 = OpTypeFunction %void
  1754. %float = OpTypeFloat 32
  1755. %9 = OpTypeFunction %float
  1756. %float_1 = OpConstant %float 1
  1757. %bool = OpTypeBool
  1758. %false = OpConstantFalse %bool
  1759. %int = OpTypeInt 32 1
  1760. %_ptr_Function_int = OpTypePointer Function %int
  1761. %int_0 = OpConstant %int 0
  1762. %int_1 = OpConstant %int 1
  1763. %v4float = OpTypeVector %float 4
  1764. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1765. %_GLF_color = OpVariable %_ptr_Output_v4float Output
  1766. %float_0 = OpConstant %float 0
  1767. %20 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
  1768. %21 = OpConstantComposite %v4float %float_0 %float_1 %float_0 %float_1
  1769. )";
  1770. const std::string caller =
  1771. R"(%main = OpFunction %void None %7
  1772. %22 = OpLabel
  1773. %i = OpVariable %_ptr_Function_int Function
  1774. OpStore %i %int_0
  1775. OpBranch %23
  1776. %23 = OpLabel
  1777. OpLoopMerge %24 %25 None
  1778. OpBranch %26
  1779. %26 = OpLabel
  1780. %27 = OpLoad %int %i
  1781. %28 = OpSLessThan %bool %27 %int_1
  1782. OpBranchConditional %28 %29 %24
  1783. %29 = OpLabel
  1784. OpStore %_GLF_color %20
  1785. %30 = OpFunctionCall %float %f_
  1786. OpBranch %25
  1787. %25 = OpLabel
  1788. %31 = OpLoad %int %i
  1789. %32 = OpIAdd %int %31 %int_1
  1790. OpStore %i %32
  1791. OpBranch %23
  1792. %24 = OpLabel
  1793. OpStore %_GLF_color %21
  1794. OpReturn
  1795. OpFunctionEnd
  1796. )";
  1797. const std::string callee =
  1798. R"(%f_ = OpFunction %float None %9
  1799. %33 = OpLabel
  1800. OpBranch %34
  1801. %34 = OpLabel
  1802. OpLoopMerge %35 %36 None
  1803. OpBranch %37
  1804. %37 = OpLabel
  1805. OpReturnValue %float_1
  1806. %36 = OpLabel
  1807. OpBranch %34
  1808. %35 = OpLabel
  1809. OpUnreachable
  1810. OpFunctionEnd
  1811. )";
  1812. SinglePassRunAndCheck<InlineExhaustivePass>(
  1813. predefs + caller + callee, predefs + caller + callee, false, true);
  1814. }
  1815. TEST_F(InlineTest, Decorated1) {
  1816. // Same test as Simple with the difference
  1817. // that OpFAdd in the outlined function is
  1818. // decorated with RelaxedPrecision
  1819. // Expected result is an equal decoration
  1820. // of the corresponding inlined instruction
  1821. //
  1822. // #version 140
  1823. //
  1824. // in vec4 BaseColor;
  1825. //
  1826. // float foo(vec4 bar)
  1827. // {
  1828. // return bar.x + bar.y;
  1829. // }
  1830. //
  1831. // void main()
  1832. // {
  1833. // vec4 color = vec4(foo(BaseColor));
  1834. // gl_FragColor = color;
  1835. // }
  1836. const std::string predefs =
  1837. R"(OpCapability Shader
  1838. %1 = OpExtInstImport "GLSL.std.450"
  1839. OpMemoryModel Logical GLSL450
  1840. OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
  1841. OpExecutionMode %main OriginUpperLeft
  1842. OpSource GLSL 140
  1843. OpName %main "main"
  1844. OpName %foo_vf4_ "foo(vf4;"
  1845. OpName %bar "bar"
  1846. OpName %color "color"
  1847. OpName %BaseColor "BaseColor"
  1848. OpName %param "param"
  1849. OpName %gl_FragColor "gl_FragColor"
  1850. OpDecorate %9 RelaxedPrecision
  1851. )";
  1852. const std::string before =
  1853. R"(%void = OpTypeVoid
  1854. %11 = OpTypeFunction %void
  1855. %float = OpTypeFloat 32
  1856. %v4float = OpTypeVector %float 4
  1857. %_ptr_Function_v4float = OpTypePointer Function %v4float
  1858. %15 = OpTypeFunction %float %_ptr_Function_v4float
  1859. %uint = OpTypeInt 32 0
  1860. %uint_0 = OpConstant %uint 0
  1861. %_ptr_Function_float = OpTypePointer Function %float
  1862. %uint_1 = OpConstant %uint 1
  1863. %_ptr_Input_v4float = OpTypePointer Input %v4float
  1864. %BaseColor = OpVariable %_ptr_Input_v4float Input
  1865. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1866. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  1867. %main = OpFunction %void None %11
  1868. %22 = OpLabel
  1869. %color = OpVariable %_ptr_Function_v4float Function
  1870. %param = OpVariable %_ptr_Function_v4float Function
  1871. %23 = OpLoad %v4float %BaseColor
  1872. OpStore %param %23
  1873. %24 = OpFunctionCall %float %foo_vf4_ %param
  1874. %25 = OpCompositeConstruct %v4float %24 %24 %24 %24
  1875. OpStore %color %25
  1876. %26 = OpLoad %v4float %color
  1877. OpStore %gl_FragColor %26
  1878. OpReturn
  1879. OpFunctionEnd
  1880. )";
  1881. const std::string after =
  1882. R"(OpDecorate %38 RelaxedPrecision
  1883. %void = OpTypeVoid
  1884. %11 = OpTypeFunction %void
  1885. %float = OpTypeFloat 32
  1886. %v4float = OpTypeVector %float 4
  1887. %_ptr_Function_v4float = OpTypePointer Function %v4float
  1888. %15 = OpTypeFunction %float %_ptr_Function_v4float
  1889. %uint = OpTypeInt 32 0
  1890. %uint_0 = OpConstant %uint 0
  1891. %_ptr_Function_float = OpTypePointer Function %float
  1892. %uint_1 = OpConstant %uint 1
  1893. %_ptr_Input_v4float = OpTypePointer Input %v4float
  1894. %BaseColor = OpVariable %_ptr_Input_v4float Input
  1895. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1896. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  1897. %main = OpFunction %void None %11
  1898. %22 = OpLabel
  1899. %32 = OpVariable %_ptr_Function_float Function
  1900. %color = OpVariable %_ptr_Function_v4float Function
  1901. %param = OpVariable %_ptr_Function_v4float Function
  1902. %23 = OpLoad %v4float %BaseColor
  1903. OpStore %param %23
  1904. %34 = OpAccessChain %_ptr_Function_float %param %uint_0
  1905. %35 = OpLoad %float %34
  1906. %36 = OpAccessChain %_ptr_Function_float %param %uint_1
  1907. %37 = OpLoad %float %36
  1908. %38 = OpFAdd %float %35 %37
  1909. OpStore %32 %38
  1910. %24 = OpLoad %float %32
  1911. %25 = OpCompositeConstruct %v4float %24 %24 %24 %24
  1912. OpStore %color %25
  1913. %26 = OpLoad %v4float %color
  1914. OpStore %gl_FragColor %26
  1915. OpReturn
  1916. OpFunctionEnd
  1917. )";
  1918. const std::string nonEntryFuncs =
  1919. R"(%foo_vf4_ = OpFunction %float None %15
  1920. %bar = OpFunctionParameter %_ptr_Function_v4float
  1921. %27 = OpLabel
  1922. %28 = OpAccessChain %_ptr_Function_float %bar %uint_0
  1923. %29 = OpLoad %float %28
  1924. %30 = OpAccessChain %_ptr_Function_float %bar %uint_1
  1925. %31 = OpLoad %float %30
  1926. %9 = OpFAdd %float %29 %31
  1927. OpReturnValue %9
  1928. OpFunctionEnd
  1929. )";
  1930. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + before + nonEntryFuncs,
  1931. predefs + after + nonEntryFuncs,
  1932. false, true);
  1933. }
  1934. TEST_F(InlineTest, Decorated2) {
  1935. // Same test as Simple with the difference
  1936. // that the Result <id> of the outlined OpFunction
  1937. // is decorated with RelaxedPrecision
  1938. // Expected result is an equal decoration
  1939. // of the created return variable
  1940. //
  1941. // #version 140
  1942. //
  1943. // in vec4 BaseColor;
  1944. //
  1945. // float foo(vec4 bar)
  1946. // {
  1947. // return bar.x + bar.y;
  1948. // }
  1949. //
  1950. // void main()
  1951. // {
  1952. // vec4 color = vec4(foo(BaseColor));
  1953. // gl_FragColor = color;
  1954. // }
  1955. const std::string predefs =
  1956. R"(OpCapability Shader
  1957. %1 = OpExtInstImport "GLSL.std.450"
  1958. OpMemoryModel Logical GLSL450
  1959. OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
  1960. OpExecutionMode %main OriginUpperLeft
  1961. OpSource GLSL 140
  1962. OpName %main "main"
  1963. OpName %foo_vf4_ "foo(vf4;"
  1964. OpName %bar "bar"
  1965. OpName %color "color"
  1966. OpName %BaseColor "BaseColor"
  1967. OpName %param "param"
  1968. OpName %gl_FragColor "gl_FragColor"
  1969. OpDecorate %foo_vf4_ RelaxedPrecision
  1970. )";
  1971. const std::string before =
  1972. R"(%void = OpTypeVoid
  1973. %10 = OpTypeFunction %void
  1974. %float = OpTypeFloat 32
  1975. %v4float = OpTypeVector %float 4
  1976. %_ptr_Function_v4float = OpTypePointer Function %v4float
  1977. %14 = OpTypeFunction %float %_ptr_Function_v4float
  1978. %uint = OpTypeInt 32 0
  1979. %uint_0 = OpConstant %uint 0
  1980. %_ptr_Function_float = OpTypePointer Function %float
  1981. %uint_1 = OpConstant %uint 1
  1982. %_ptr_Input_v4float = OpTypePointer Input %v4float
  1983. %BaseColor = OpVariable %_ptr_Input_v4float Input
  1984. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1985. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  1986. %main = OpFunction %void None %10
  1987. %21 = OpLabel
  1988. %color = OpVariable %_ptr_Function_v4float Function
  1989. %param = OpVariable %_ptr_Function_v4float Function
  1990. %22 = OpLoad %v4float %BaseColor
  1991. OpStore %param %22
  1992. %23 = OpFunctionCall %float %foo_vf4_ %param
  1993. %24 = OpCompositeConstruct %v4float %23 %23 %23 %23
  1994. OpStore %color %24
  1995. %25 = OpLoad %v4float %color
  1996. OpStore %gl_FragColor %25
  1997. OpReturn
  1998. OpFunctionEnd
  1999. )";
  2000. const std::string after =
  2001. R"(OpDecorate %32 RelaxedPrecision
  2002. %void = OpTypeVoid
  2003. %10 = OpTypeFunction %void
  2004. %float = OpTypeFloat 32
  2005. %v4float = OpTypeVector %float 4
  2006. %_ptr_Function_v4float = OpTypePointer Function %v4float
  2007. %14 = OpTypeFunction %float %_ptr_Function_v4float
  2008. %uint = OpTypeInt 32 0
  2009. %uint_0 = OpConstant %uint 0
  2010. %_ptr_Function_float = OpTypePointer Function %float
  2011. %uint_1 = OpConstant %uint 1
  2012. %_ptr_Input_v4float = OpTypePointer Input %v4float
  2013. %BaseColor = OpVariable %_ptr_Input_v4float Input
  2014. %_ptr_Output_v4float = OpTypePointer Output %v4float
  2015. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  2016. %main = OpFunction %void None %10
  2017. %21 = OpLabel
  2018. %32 = OpVariable %_ptr_Function_float Function
  2019. %color = OpVariable %_ptr_Function_v4float Function
  2020. %param = OpVariable %_ptr_Function_v4float Function
  2021. %22 = OpLoad %v4float %BaseColor
  2022. OpStore %param %22
  2023. %34 = OpAccessChain %_ptr_Function_float %param %uint_0
  2024. %35 = OpLoad %float %34
  2025. %36 = OpAccessChain %_ptr_Function_float %param %uint_1
  2026. %37 = OpLoad %float %36
  2027. %38 = OpFAdd %float %35 %37
  2028. OpStore %32 %38
  2029. %23 = OpLoad %float %32
  2030. %24 = OpCompositeConstruct %v4float %23 %23 %23 %23
  2031. OpStore %color %24
  2032. %25 = OpLoad %v4float %color
  2033. OpStore %gl_FragColor %25
  2034. OpReturn
  2035. OpFunctionEnd
  2036. )";
  2037. const std::string nonEntryFuncs =
  2038. R"(%foo_vf4_ = OpFunction %float None %14
  2039. %bar = OpFunctionParameter %_ptr_Function_v4float
  2040. %26 = OpLabel
  2041. %27 = OpAccessChain %_ptr_Function_float %bar %uint_0
  2042. %28 = OpLoad %float %27
  2043. %29 = OpAccessChain %_ptr_Function_float %bar %uint_1
  2044. %30 = OpLoad %float %29
  2045. %31 = OpFAdd %float %28 %30
  2046. OpReturnValue %31
  2047. OpFunctionEnd
  2048. )";
  2049. SinglePassRunAndCheck<InlineExhaustivePass>(predefs + before + nonEntryFuncs,
  2050. predefs + after + nonEntryFuncs,
  2051. false, true);
  2052. }
  2053. TEST_F(InlineTest, DeleteName) {
  2054. // Test that the name of the result id of the call is deleted.
  2055. const std::string before =
  2056. R"(
  2057. OpCapability Shader
  2058. OpMemoryModel Logical GLSL450
  2059. OpEntryPoint Vertex %main "main"
  2060. OpName %main "main"
  2061. OpName %main_entry "main_entry"
  2062. OpName %foo_result "foo_result"
  2063. OpName %void_fn "void_fn"
  2064. OpName %foo "foo"
  2065. OpName %foo_entry "foo_entry"
  2066. %void = OpTypeVoid
  2067. %void_fn = OpTypeFunction %void
  2068. %foo = OpFunction %void None %void_fn
  2069. %foo_entry = OpLabel
  2070. OpReturn
  2071. OpFunctionEnd
  2072. %main = OpFunction %void None %void_fn
  2073. %main_entry = OpLabel
  2074. %foo_result = OpFunctionCall %void %foo
  2075. OpReturn
  2076. OpFunctionEnd
  2077. )";
  2078. const std::string after =
  2079. R"(OpCapability Shader
  2080. OpMemoryModel Logical GLSL450
  2081. OpEntryPoint Vertex %main "main"
  2082. OpName %main "main"
  2083. OpName %main_entry "main_entry"
  2084. OpName %void_fn "void_fn"
  2085. OpName %foo "foo"
  2086. OpName %foo_entry "foo_entry"
  2087. %void = OpTypeVoid
  2088. %void_fn = OpTypeFunction %void
  2089. %foo = OpFunction %void None %void_fn
  2090. %foo_entry = OpLabel
  2091. OpReturn
  2092. OpFunctionEnd
  2093. %main = OpFunction %void None %void_fn
  2094. %main_entry = OpLabel
  2095. OpReturn
  2096. OpFunctionEnd
  2097. )";
  2098. SinglePassRunAndCheck<InlineExhaustivePass>(before, after, false, true);
  2099. }
  2100. TEST_F(InlineTest, SetParent) {
  2101. // Test that after inlining all basic blocks have the correct parent.
  2102. const std::string text =
  2103. R"(
  2104. OpCapability Shader
  2105. OpMemoryModel Logical GLSL450
  2106. OpEntryPoint Vertex %main "main"
  2107. OpName %main "main"
  2108. OpName %main_entry "main_entry"
  2109. OpName %foo_result "foo_result"
  2110. OpName %void_fn "void_fn"
  2111. OpName %foo "foo"
  2112. OpName %foo_entry "foo_entry"
  2113. %void = OpTypeVoid
  2114. %void_fn = OpTypeFunction %void
  2115. %foo = OpFunction %void None %void_fn
  2116. %foo_entry = OpLabel
  2117. OpReturn
  2118. OpFunctionEnd
  2119. %main = OpFunction %void None %void_fn
  2120. %main_entry = OpLabel
  2121. %foo_result = OpFunctionCall %void %foo
  2122. OpReturn
  2123. OpFunctionEnd
  2124. )";
  2125. std::unique_ptr<IRContext> context =
  2126. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text);
  2127. InlineExhaustivePass pass;
  2128. pass.Run(context.get());
  2129. for (Function& func : *context->module()) {
  2130. for (BasicBlock& bb : func) {
  2131. EXPECT_TRUE(bb.GetParent() == &func);
  2132. }
  2133. }
  2134. }
  2135. TEST_F(InlineTest, OpVariableWithInit) {
  2136. // Check that there is a store that corresponds to the initializer. This
  2137. // test makes sure that is a store to the variable in the loop and before any
  2138. // load.
  2139. const std::string text = R"(
  2140. ; CHECK: OpFunction
  2141. ; CHECK-NOT: OpFunctionEnd
  2142. ; CHECK: [[var:%\w+]] = OpVariable %_ptr_Function_float Function %float_0
  2143. ; CHECK: OpLoopMerge [[outer_merge:%\w+]]
  2144. ; CHECK-NOT: OpLoad %float [[var]]
  2145. ; CHECK: OpStore [[var]] %float_0
  2146. ; CHECK: OpFunctionEnd
  2147. OpCapability Shader
  2148. %1 = OpExtInstImport "GLSL.std.450"
  2149. OpMemoryModel Logical GLSL450
  2150. OpEntryPoint Fragment %main "main" %o
  2151. OpExecutionMode %main OriginUpperLeft
  2152. OpSource GLSL 450
  2153. OpDecorate %o Location 0
  2154. %void = OpTypeVoid
  2155. %3 = OpTypeFunction %void
  2156. %float = OpTypeFloat 32
  2157. %7 = OpTypeFunction %float
  2158. %_ptr_Function_float = OpTypePointer Function %float
  2159. %float_0 = OpConstant %float 0
  2160. %bool = OpTypeBool
  2161. %float_1 = OpConstant %float 1
  2162. %_ptr_Output_float = OpTypePointer Output %float
  2163. %o = OpVariable %_ptr_Output_float Output
  2164. %int = OpTypeInt 32 1
  2165. %_ptr_Function_int = OpTypePointer Function %int
  2166. %_ptr_Input_int = OpTypePointer Input %int
  2167. %int_0 = OpConstant %int 0
  2168. %int_1 = OpConstant %int 1
  2169. %int_2 = OpConstant %int 2
  2170. %main = OpFunction %void None %3
  2171. %5 = OpLabel
  2172. OpStore %o %float_0
  2173. OpBranch %34
  2174. %34 = OpLabel
  2175. %39 = OpPhi %int %int_0 %5 %47 %37
  2176. OpLoopMerge %36 %37 None
  2177. OpBranch %38
  2178. %38 = OpLabel
  2179. %41 = OpSLessThan %bool %39 %int_2
  2180. OpBranchConditional %41 %35 %36
  2181. %35 = OpLabel
  2182. %42 = OpFunctionCall %float %foo_
  2183. %43 = OpLoad %float %o
  2184. %44 = OpFAdd %float %43 %42
  2185. OpStore %o %44
  2186. OpBranch %37
  2187. %37 = OpLabel
  2188. %47 = OpIAdd %int %39 %int_1
  2189. OpBranch %34
  2190. %36 = OpLabel
  2191. OpReturn
  2192. OpFunctionEnd
  2193. %foo_ = OpFunction %float None %7
  2194. %9 = OpLabel
  2195. %n = OpVariable %_ptr_Function_float Function %float_0
  2196. %13 = OpLoad %float %n
  2197. %15 = OpFOrdEqual %bool %13 %float_0
  2198. OpSelectionMerge %17 None
  2199. OpBranchConditional %15 %16 %17
  2200. %16 = OpLabel
  2201. %19 = OpLoad %float %n
  2202. %20 = OpFAdd %float %19 %float_1
  2203. OpStore %n %20
  2204. OpBranch %17
  2205. %17 = OpLabel
  2206. %21 = OpLoad %float %n
  2207. OpReturnValue %21
  2208. OpFunctionEnd
  2209. )";
  2210. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  2211. }
  2212. TEST_F(InlineTest, DontInlineDirectlyRecursiveFunc) {
  2213. // Test that the name of the result id of the call is deleted.
  2214. const std::string test =
  2215. R"(OpCapability Shader
  2216. OpMemoryModel Logical GLSL450
  2217. OpEntryPoint Fragment %1 "main"
  2218. OpExecutionMode %1 OriginUpperLeft
  2219. %void = OpTypeVoid
  2220. %4 = OpTypeFunction %void
  2221. %float = OpTypeFloat 32
  2222. %_struct_6 = OpTypeStruct %float %float
  2223. %15 = OpConstantNull %_struct_6
  2224. %7 = OpTypeFunction %_struct_6
  2225. %1 = OpFunction %void Pure|Const %4
  2226. %8 = OpLabel
  2227. %2 = OpFunctionCall %_struct_6 %9
  2228. OpKill
  2229. OpFunctionEnd
  2230. %9 = OpFunction %_struct_6 None %7
  2231. %10 = OpLabel
  2232. %11 = OpFunctionCall %_struct_6 %9
  2233. OpReturnValue %15
  2234. OpFunctionEnd
  2235. )";
  2236. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2237. SinglePassRunAndCheck<InlineExhaustivePass>(test, test, false, true);
  2238. }
  2239. TEST_F(InlineTest, DontInlineInDirectlyRecursiveFunc) {
  2240. // Test that the name of the result id of the call is deleted.
  2241. const std::string test =
  2242. R"(OpCapability Shader
  2243. OpMemoryModel Logical GLSL450
  2244. OpEntryPoint Fragment %1 "main"
  2245. OpExecutionMode %1 OriginUpperLeft
  2246. %void = OpTypeVoid
  2247. %4 = OpTypeFunction %void
  2248. %float = OpTypeFloat 32
  2249. %_struct_6 = OpTypeStruct %float %float
  2250. %15 = OpConstantNull %_struct_6
  2251. %7 = OpTypeFunction %_struct_6
  2252. %1 = OpFunction %void Pure|Const %4
  2253. %8 = OpLabel
  2254. %2 = OpFunctionCall %_struct_6 %9
  2255. OpKill
  2256. OpFunctionEnd
  2257. %9 = OpFunction %_struct_6 None %7
  2258. %10 = OpLabel
  2259. %11 = OpFunctionCall %_struct_6 %12
  2260. OpReturnValue %15
  2261. OpFunctionEnd
  2262. %12 = OpFunction %_struct_6 None %7
  2263. %13 = OpLabel
  2264. %14 = OpFunctionCall %_struct_6 %9
  2265. OpReturnValue %15
  2266. OpFunctionEnd
  2267. )";
  2268. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2269. SinglePassRunAndCheck<InlineExhaustivePass>(test, test, false, true);
  2270. }
  2271. TEST_F(InlineTest, DontInlineFuncWithOpKillInContinue) {
  2272. const std::string test =
  2273. R"(OpCapability Shader
  2274. %1 = OpExtInstImport "GLSL.std.450"
  2275. OpMemoryModel Logical GLSL450
  2276. OpEntryPoint Fragment %main "main"
  2277. OpExecutionMode %main OriginUpperLeft
  2278. OpSource GLSL 330
  2279. OpName %main "main"
  2280. OpName %kill_ "kill("
  2281. %void = OpTypeVoid
  2282. %3 = OpTypeFunction %void
  2283. %bool = OpTypeBool
  2284. %true = OpConstantTrue %bool
  2285. %main = OpFunction %void None %3
  2286. %5 = OpLabel
  2287. OpBranch %9
  2288. %9 = OpLabel
  2289. OpLoopMerge %11 %12 None
  2290. OpBranch %13
  2291. %13 = OpLabel
  2292. OpBranchConditional %true %10 %11
  2293. %10 = OpLabel
  2294. OpBranch %12
  2295. %12 = OpLabel
  2296. %16 = OpFunctionCall %void %kill_
  2297. OpBranch %9
  2298. %11 = OpLabel
  2299. OpReturn
  2300. OpFunctionEnd
  2301. %kill_ = OpFunction %void None %3
  2302. %7 = OpLabel
  2303. OpKill
  2304. OpFunctionEnd
  2305. )";
  2306. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2307. SinglePassRunAndCheck<InlineExhaustivePass>(test, test, false, true);
  2308. }
  2309. TEST_F(InlineTest, DontInlineFuncWithDontInline) {
  2310. // Check that the function with DontInline flag is not inlined.
  2311. const std::string text = R"(
  2312. ; CHECK: %foo = OpFunction %int DontInline
  2313. ; CHECK: OpReturnValue %int_0
  2314. OpCapability Shader
  2315. OpMemoryModel Logical GLSL450
  2316. OpEntryPoint Fragment %main "main"
  2317. OpExecutionMode %main OriginUpperLeft
  2318. OpSource HLSL 600
  2319. OpName %main "main"
  2320. OpName %foo "foo"
  2321. %int = OpTypeInt 32 1
  2322. %int_0 = OpConstant %int 0
  2323. %void = OpTypeVoid
  2324. %6 = OpTypeFunction %void
  2325. %7 = OpTypeFunction %int
  2326. %main = OpFunction %void None %6
  2327. %8 = OpLabel
  2328. %9 = OpFunctionCall %int %foo
  2329. OpReturn
  2330. OpFunctionEnd
  2331. %foo = OpFunction %int DontInline %7
  2332. %10 = OpLabel
  2333. OpReturnValue %int_0
  2334. OpFunctionEnd
  2335. )";
  2336. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  2337. }
  2338. TEST_F(InlineTest, InlineFuncWithOpKillNotInContinue) {
  2339. const std::string before =
  2340. R"(OpCapability Shader
  2341. %1 = OpExtInstImport "GLSL.std.450"
  2342. OpMemoryModel Logical GLSL450
  2343. OpEntryPoint Fragment %main "main"
  2344. OpExecutionMode %main OriginUpperLeft
  2345. OpSource GLSL 330
  2346. OpName %main "main"
  2347. OpName %kill_ "kill("
  2348. %void = OpTypeVoid
  2349. %3 = OpTypeFunction %void
  2350. %bool = OpTypeBool
  2351. %true = OpConstantTrue %bool
  2352. %main = OpFunction %void None %3
  2353. %5 = OpLabel
  2354. %16 = OpFunctionCall %void %kill_
  2355. OpReturn
  2356. OpFunctionEnd
  2357. %kill_ = OpFunction %void None %3
  2358. %7 = OpLabel
  2359. OpKill
  2360. OpFunctionEnd
  2361. )";
  2362. const std::string after =
  2363. R"(OpCapability Shader
  2364. %1 = OpExtInstImport "GLSL.std.450"
  2365. OpMemoryModel Logical GLSL450
  2366. OpEntryPoint Fragment %main "main"
  2367. OpExecutionMode %main OriginUpperLeft
  2368. OpSource GLSL 330
  2369. OpName %main "main"
  2370. OpName %kill_ "kill("
  2371. %void = OpTypeVoid
  2372. %3 = OpTypeFunction %void
  2373. %bool = OpTypeBool
  2374. %true = OpConstantTrue %bool
  2375. %main = OpFunction %void None %3
  2376. %5 = OpLabel
  2377. OpKill
  2378. %18 = OpLabel
  2379. OpReturn
  2380. OpFunctionEnd
  2381. %kill_ = OpFunction %void None %3
  2382. %7 = OpLabel
  2383. OpKill
  2384. OpFunctionEnd
  2385. )";
  2386. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2387. SinglePassRunAndCheck<InlineExhaustivePass>(before, after, false, true);
  2388. }
  2389. TEST_F(InlineTest, DontInlineFuncWithOpTerminateInvocationInContinue) {
  2390. const std::string test =
  2391. R"(OpCapability Shader
  2392. OpExtension "SPV_KHR_terminate_invocation"
  2393. %1 = OpExtInstImport "GLSL.std.450"
  2394. OpMemoryModel Logical GLSL450
  2395. OpEntryPoint Fragment %main "main"
  2396. OpExecutionMode %main OriginUpperLeft
  2397. OpSource GLSL 330
  2398. OpName %main "main"
  2399. OpName %kill_ "kill("
  2400. %void = OpTypeVoid
  2401. %3 = OpTypeFunction %void
  2402. %bool = OpTypeBool
  2403. %true = OpConstantTrue %bool
  2404. %main = OpFunction %void None %3
  2405. %5 = OpLabel
  2406. OpBranch %9
  2407. %9 = OpLabel
  2408. OpLoopMerge %11 %12 None
  2409. OpBranch %13
  2410. %13 = OpLabel
  2411. OpBranchConditional %true %10 %11
  2412. %10 = OpLabel
  2413. OpBranch %12
  2414. %12 = OpLabel
  2415. %16 = OpFunctionCall %void %kill_
  2416. OpBranch %9
  2417. %11 = OpLabel
  2418. OpReturn
  2419. OpFunctionEnd
  2420. %kill_ = OpFunction %void None %3
  2421. %7 = OpLabel
  2422. OpTerminateInvocation
  2423. OpFunctionEnd
  2424. )";
  2425. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2426. SinglePassRunAndCheck<InlineExhaustivePass>(test, test, false, true);
  2427. }
  2428. TEST_F(InlineTest, InlineFuncWithOpTerminateInvocationNotInContinue) {
  2429. const std::string before =
  2430. R"(OpCapability Shader
  2431. OpExtension "SPV_KHR_terminate_invocation"
  2432. %1 = OpExtInstImport "GLSL.std.450"
  2433. OpMemoryModel Logical GLSL450
  2434. OpEntryPoint Fragment %main "main"
  2435. OpExecutionMode %main OriginUpperLeft
  2436. OpSource GLSL 330
  2437. OpName %main "main"
  2438. OpName %kill_ "kill("
  2439. %void = OpTypeVoid
  2440. %3 = OpTypeFunction %void
  2441. %bool = OpTypeBool
  2442. %true = OpConstantTrue %bool
  2443. %main = OpFunction %void None %3
  2444. %5 = OpLabel
  2445. %16 = OpFunctionCall %void %kill_
  2446. OpReturn
  2447. OpFunctionEnd
  2448. %kill_ = OpFunction %void None %3
  2449. %7 = OpLabel
  2450. OpTerminateInvocation
  2451. OpFunctionEnd
  2452. )";
  2453. const std::string after =
  2454. R"(OpCapability Shader
  2455. OpExtension "SPV_KHR_terminate_invocation"
  2456. %1 = OpExtInstImport "GLSL.std.450"
  2457. OpMemoryModel Logical GLSL450
  2458. OpEntryPoint Fragment %main "main"
  2459. OpExecutionMode %main OriginUpperLeft
  2460. OpSource GLSL 330
  2461. OpName %main "main"
  2462. OpName %kill_ "kill("
  2463. %void = OpTypeVoid
  2464. %3 = OpTypeFunction %void
  2465. %bool = OpTypeBool
  2466. %true = OpConstantTrue %bool
  2467. %main = OpFunction %void None %3
  2468. %5 = OpLabel
  2469. OpTerminateInvocation
  2470. %18 = OpLabel
  2471. OpReturn
  2472. OpFunctionEnd
  2473. %kill_ = OpFunction %void None %3
  2474. %7 = OpLabel
  2475. OpTerminateInvocation
  2476. OpFunctionEnd
  2477. )";
  2478. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2479. SinglePassRunAndCheck<InlineExhaustivePass>(before, after, false, true);
  2480. }
  2481. TEST_F(InlineTest, InlineForLinkage) {
  2482. const std::string before =
  2483. R"(OpCapability SampledBuffer
  2484. OpCapability ImageBuffer
  2485. OpCapability Shader
  2486. OpCapability Linkage
  2487. OpMemoryModel Logical GLSL450
  2488. OpSource HLSL 630
  2489. OpName %type_buffer_image "type.buffer.image"
  2490. OpName %output "output"
  2491. OpName %main "main"
  2492. OpName %color "color"
  2493. OpName %bb_entry "bb.entry"
  2494. OpName %param_var_color "param.var.color"
  2495. OpName %fn "fn"
  2496. OpName %color_0 "color"
  2497. OpName %bb_entry_0 "bb.entry"
  2498. OpName %v "v"
  2499. OpDecorate %main LinkageAttributes "main" Export
  2500. OpDecorate %output DescriptorSet 0
  2501. OpDecorate %output Binding 1
  2502. %float = OpTypeFloat 32
  2503. %float_0_200000003 = OpConstant %float 0.200000003
  2504. %v4float = OpTypeVector %float 4
  2505. %6 = OpConstantComposite %v4float %float_0_200000003 %float_0_200000003 %float_0_200000003 %float_0_200000003
  2506. %int = OpTypeInt 32 1
  2507. %int_0 = OpConstant %int 0
  2508. %type_buffer_image = OpTypeImage %float Buffer 2 0 0 2 Rgba32f
  2509. %_ptr_UniformConstant_type_buffer_image = OpTypePointer UniformConstant %type_buffer_image
  2510. %_ptr_Function_v4float = OpTypePointer Function %v4float
  2511. %11 = OpTypeFunction %float %_ptr_Function_v4float
  2512. %_ptr_Function_float = OpTypePointer Function %float
  2513. %output = OpVariable %_ptr_UniformConstant_type_buffer_image UniformConstant
  2514. %main = OpFunction %float None %11
  2515. %color = OpFunctionParameter %_ptr_Function_v4float
  2516. %bb_entry = OpLabel
  2517. %param_var_color = OpVariable %_ptr_Function_v4float Function
  2518. %16 = OpLoad %v4float %color
  2519. OpStore %param_var_color %16
  2520. %17 = OpFunctionCall %float %fn %param_var_color
  2521. OpReturnValue %17
  2522. OpFunctionEnd
  2523. %fn = OpFunction %float None %11
  2524. %color_0 = OpFunctionParameter %_ptr_Function_v4float
  2525. %bb_entry_0 = OpLabel
  2526. %v = OpVariable %_ptr_Function_v4float Function
  2527. %22 = OpLoad %v4float %color_0
  2528. OpStore %v %22
  2529. %23 = OpLoad %v4float %v
  2530. %24 = OpFMul %v4float %23 %6
  2531. OpStore %v %24
  2532. %26 = OpAccessChain %_ptr_Function_float %v %int_0
  2533. %27 = OpLoad %float %26
  2534. OpReturnValue %27
  2535. OpFunctionEnd
  2536. )";
  2537. const std::string after =
  2538. R"(OpCapability SampledBuffer
  2539. OpCapability ImageBuffer
  2540. OpCapability Shader
  2541. OpCapability Linkage
  2542. OpMemoryModel Logical GLSL450
  2543. OpSource HLSL 630
  2544. OpName %type_buffer_image "type.buffer.image"
  2545. OpName %output "output"
  2546. OpName %main "main"
  2547. OpName %color "color"
  2548. OpName %bb_entry "bb.entry"
  2549. OpName %param_var_color "param.var.color"
  2550. OpName %fn "fn"
  2551. OpName %color_0 "color"
  2552. OpName %bb_entry_0 "bb.entry"
  2553. OpName %v "v"
  2554. OpDecorate %main LinkageAttributes "main" Export
  2555. OpDecorate %output DescriptorSet 0
  2556. OpDecorate %output Binding 1
  2557. %float = OpTypeFloat 32
  2558. %float_0_200000003 = OpConstant %float 0.200000003
  2559. %v4float = OpTypeVector %float 4
  2560. %6 = OpConstantComposite %v4float %float_0_200000003 %float_0_200000003 %float_0_200000003 %float_0_200000003
  2561. %int = OpTypeInt 32 1
  2562. %int_0 = OpConstant %int 0
  2563. %type_buffer_image = OpTypeImage %float Buffer 2 0 0 2 Rgba32f
  2564. %_ptr_UniformConstant_type_buffer_image = OpTypePointer UniformConstant %type_buffer_image
  2565. %_ptr_Function_v4float = OpTypePointer Function %v4float
  2566. %11 = OpTypeFunction %float %_ptr_Function_v4float
  2567. %_ptr_Function_float = OpTypePointer Function %float
  2568. %output = OpVariable %_ptr_UniformConstant_type_buffer_image UniformConstant
  2569. %main = OpFunction %float None %11
  2570. %color = OpFunctionParameter %_ptr_Function_v4float
  2571. %bb_entry = OpLabel
  2572. %28 = OpVariable %_ptr_Function_v4float Function
  2573. %29 = OpVariable %_ptr_Function_float Function
  2574. %param_var_color = OpVariable %_ptr_Function_v4float Function
  2575. %16 = OpLoad %v4float %color
  2576. OpStore %param_var_color %16
  2577. %31 = OpLoad %v4float %param_var_color
  2578. OpStore %28 %31
  2579. %32 = OpLoad %v4float %28
  2580. %33 = OpFMul %v4float %32 %6
  2581. OpStore %28 %33
  2582. %34 = OpAccessChain %_ptr_Function_float %28 %int_0
  2583. %35 = OpLoad %float %34
  2584. OpStore %29 %35
  2585. %17 = OpLoad %float %29
  2586. OpReturnValue %17
  2587. OpFunctionEnd
  2588. %fn = OpFunction %float None %11
  2589. %color_0 = OpFunctionParameter %_ptr_Function_v4float
  2590. %bb_entry_0 = OpLabel
  2591. %v = OpVariable %_ptr_Function_v4float Function
  2592. %22 = OpLoad %v4float %color_0
  2593. OpStore %v %22
  2594. %23 = OpLoad %v4float %v
  2595. %24 = OpFMul %v4float %23 %6
  2596. OpStore %v %24
  2597. %26 = OpAccessChain %_ptr_Function_float %v %int_0
  2598. %27 = OpLoad %float %26
  2599. OpReturnValue %27
  2600. OpFunctionEnd
  2601. )";
  2602. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  2603. SinglePassRunAndCheck<InlineExhaustivePass>(before, after, false, true);
  2604. }
  2605. TEST_F(InlineTest, InlineFuncWithOpTerminateRayNotInContinue) {
  2606. const std::string text =
  2607. R"(
  2608. OpCapability RayTracingKHR
  2609. OpExtension "SPV_KHR_ray_tracing"
  2610. OpMemoryModel Logical GLSL450
  2611. OpEntryPoint AnyHitKHR %MyAHitMain2 "MyAHitMain2" %a
  2612. OpSource HLSL 630
  2613. OpName %a "a"
  2614. OpName %MyAHitMain2 "MyAHitMain2"
  2615. OpName %param_var_a "param.var.a"
  2616. OpName %src_MyAHitMain2 "src.MyAHitMain2"
  2617. OpName %a_0 "a"
  2618. OpName %bb_entry "bb.entry"
  2619. %int = OpTypeInt 32 1
  2620. %_ptr_IncomingRayPayloadKHR_int = OpTypePointer IncomingRayPayloadKHR %int
  2621. %void = OpTypeVoid
  2622. %6 = OpTypeFunction %void
  2623. %_ptr_Function_int = OpTypePointer Function %int
  2624. %14 = OpTypeFunction %void %_ptr_Function_int
  2625. %a = OpVariable %_ptr_IncomingRayPayloadKHR_int IncomingRayPayloadKHR
  2626. %MyAHitMain2 = OpFunction %void None %6
  2627. %7 = OpLabel
  2628. %param_var_a = OpVariable %_ptr_Function_int Function
  2629. %10 = OpLoad %int %a
  2630. OpStore %param_var_a %10
  2631. %11 = OpFunctionCall %void %src_MyAHitMain2 %param_var_a
  2632. %13 = OpLoad %int %param_var_a
  2633. OpStore %a %13
  2634. OpReturn
  2635. OpFunctionEnd
  2636. %src_MyAHitMain2 = OpFunction %void None %14
  2637. %a_0 = OpFunctionParameter %_ptr_Function_int
  2638. %bb_entry = OpLabel
  2639. %17 = OpLoad %int %a_0
  2640. OpStore %a %17
  2641. OpTerminateRayKHR
  2642. OpFunctionEnd
  2643. ; CHECK: %MyAHitMain2 = OpFunction %void None
  2644. ; CHECK-NEXT: OpLabel
  2645. ; CHECK-NEXT: %param_var_a = OpVariable %_ptr_Function_int Function
  2646. ; CHECK-NEXT: OpLoad %int %a
  2647. ; CHECK-NEXT: OpStore %param_var_a {{%\d+}}
  2648. ; CHECK-NEXT: OpLoad %int %param_var_a
  2649. ; CHECK-NEXT: OpStore %a {{%\d+}}
  2650. ; CHECK-NEXT: OpTerminateRayKHR
  2651. ; CHECK-NEXT: OpLabel
  2652. ; CHECK-NEXT: OpLoad %int %param_var_a
  2653. ; CHECK-NEXT: OpStore %a %16
  2654. ; CHECK-NEXT: OpReturn
  2655. ; CHECK-NEXT: OpFunctionEnd
  2656. )";
  2657. SinglePassRunAndMatch<InlineExhaustivePass>(text, false);
  2658. }
  2659. TEST_F(InlineTest, EarlyReturnFunctionInlined) {
  2660. // #version 140
  2661. //
  2662. // in vec4 BaseColor;
  2663. //
  2664. // float foo(vec4 bar)
  2665. // {
  2666. // if (bar.x < 0.0)
  2667. // return 0.0;
  2668. // return bar.x;
  2669. // }
  2670. //
  2671. // void main()
  2672. // {
  2673. // vec4 color = vec4(foo(BaseColor));
  2674. // gl_FragColor = color;
  2675. // }
  2676. const std::string predefs =
  2677. R"(OpCapability Shader
  2678. %1 = OpExtInstImport "GLSL.std.450"
  2679. OpMemoryModel Logical GLSL450
  2680. OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
  2681. OpExecutionMode %main OriginUpperLeft
  2682. OpSource GLSL 140
  2683. OpName %main "main"
  2684. OpName %foo_vf4_ "foo(vf4;"
  2685. OpName %bar "bar"
  2686. OpName %color "color"
  2687. OpName %BaseColor "BaseColor"
  2688. OpName %param "param"
  2689. OpName %gl_FragColor "gl_FragColor"
  2690. %void = OpTypeVoid
  2691. %10 = OpTypeFunction %void
  2692. %float = OpTypeFloat 32
  2693. %v4float = OpTypeVector %float 4
  2694. %_ptr_Function_v4float = OpTypePointer Function %v4float
  2695. %14 = OpTypeFunction %float %_ptr_Function_v4float
  2696. %uint = OpTypeInt 32 0
  2697. %uint_0 = OpConstant %uint 0
  2698. %_ptr_Function_float = OpTypePointer Function %float
  2699. %float_0 = OpConstant %float 0
  2700. %bool = OpTypeBool
  2701. %_ptr_Input_v4float = OpTypePointer Input %v4float
  2702. %BaseColor = OpVariable %_ptr_Input_v4float Input
  2703. %_ptr_Output_v4float = OpTypePointer Output %v4float
  2704. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  2705. )";
  2706. const std::string foo =
  2707. R"(%foo_vf4_ = OpFunction %float None %14
  2708. %bar = OpFunctionParameter %_ptr_Function_v4float
  2709. %27 = OpLabel
  2710. %28 = OpAccessChain %_ptr_Function_float %bar %uint_0
  2711. %29 = OpLoad %float %28
  2712. %30 = OpFOrdLessThan %bool %29 %float_0
  2713. OpSelectionMerge %31 None
  2714. OpBranchConditional %30 %32 %31
  2715. %32 = OpLabel
  2716. OpReturnValue %float_0
  2717. %31 = OpLabel
  2718. %33 = OpAccessChain %_ptr_Function_float %bar %uint_0
  2719. %34 = OpLoad %float %33
  2720. OpReturnValue %34
  2721. OpFunctionEnd
  2722. )";
  2723. const std::string fooMergeReturn =
  2724. R"(%foo_vf4_ = OpFunction %float None %14
  2725. %bar = OpFunctionParameter %_ptr_Function_v4float
  2726. %27 = OpLabel
  2727. %41 = OpVariable %_ptr_Function_bool Function %false
  2728. %36 = OpVariable %_ptr_Function_float Function
  2729. OpSelectionMerge %35 None
  2730. OpSwitch %uint_0 %38
  2731. %38 = OpLabel
  2732. %28 = OpAccessChain %_ptr_Function_float %bar %uint_0
  2733. %29 = OpLoad %float %28
  2734. %30 = OpFOrdLessThan %bool %29 %float_0
  2735. OpSelectionMerge %31 None
  2736. OpBranchConditional %30 %32 %31
  2737. %32 = OpLabel
  2738. OpStore %41 %true
  2739. OpStore %36 %float_0
  2740. OpBranch %35
  2741. %31 = OpLabel
  2742. %33 = OpAccessChain %_ptr_Function_float %bar %uint_0
  2743. %34 = OpLoad %float %33
  2744. OpStore %41 %true
  2745. OpStore %36 %34
  2746. OpBranch %35
  2747. %35 = OpLabel
  2748. %37 = OpLoad %float %36
  2749. OpReturnValue %37
  2750. OpFunctionEnd
  2751. )";
  2752. const std::string before =
  2753. R"(%main = OpFunction %void None %10
  2754. %22 = OpLabel
  2755. %color = OpVariable %_ptr_Function_v4float Function
  2756. %param = OpVariable %_ptr_Function_v4float Function
  2757. %23 = OpLoad %v4float %BaseColor
  2758. OpStore %param %23
  2759. %24 = OpFunctionCall %float %foo_vf4_ %param
  2760. %25 = OpCompositeConstruct %v4float %24 %24 %24 %24
  2761. OpStore %color %25
  2762. %26 = OpLoad %v4float %color
  2763. OpStore %gl_FragColor %26
  2764. OpReturn
  2765. OpFunctionEnd
  2766. )";
  2767. const std::string after =
  2768. R"(%false = OpConstantFalse %bool
  2769. %_ptr_Function_bool = OpTypePointer Function %bool
  2770. %true = OpConstantTrue %bool
  2771. %main = OpFunction %void None %10
  2772. %22 = OpLabel
  2773. %43 = OpVariable %_ptr_Function_bool Function %false
  2774. %44 = OpVariable %_ptr_Function_float Function
  2775. %45 = OpVariable %_ptr_Function_float Function
  2776. %color = OpVariable %_ptr_Function_v4float Function
  2777. %param = OpVariable %_ptr_Function_v4float Function
  2778. %23 = OpLoad %v4float %BaseColor
  2779. OpStore %param %23
  2780. OpStore %43 %false
  2781. OpSelectionMerge %55 None
  2782. OpSwitch %uint_0 %47
  2783. %47 = OpLabel
  2784. %48 = OpAccessChain %_ptr_Function_float %param %uint_0
  2785. %49 = OpLoad %float %48
  2786. %50 = OpFOrdLessThan %bool %49 %float_0
  2787. OpSelectionMerge %52 None
  2788. OpBranchConditional %50 %51 %52
  2789. %51 = OpLabel
  2790. OpStore %43 %true
  2791. OpStore %44 %float_0
  2792. OpBranch %55
  2793. %52 = OpLabel
  2794. %53 = OpAccessChain %_ptr_Function_float %param %uint_0
  2795. %54 = OpLoad %float %53
  2796. OpStore %43 %true
  2797. OpStore %44 %54
  2798. OpBranch %55
  2799. %55 = OpLabel
  2800. %56 = OpLoad %float %44
  2801. OpStore %45 %56
  2802. %24 = OpLoad %float %45
  2803. %25 = OpCompositeConstruct %v4float %24 %24 %24 %24
  2804. OpStore %color %25
  2805. %26 = OpLoad %v4float %color
  2806. OpStore %gl_FragColor %26
  2807. OpReturn
  2808. OpFunctionEnd
  2809. )";
  2810. // The early return case must be handled by merge-return first.
  2811. AddPass<MergeReturnPass>();
  2812. AddPass<InlineExhaustivePass>();
  2813. RunAndCheck(predefs + before + foo, predefs + after + fooMergeReturn);
  2814. }
  2815. TEST_F(InlineTest, EarlyReturnNotAppearingLastInFunctionInlined) {
  2816. // Example from https://github.com/KhronosGroup/SPIRV-Tools/issues/755
  2817. //
  2818. // Original example is derived from:
  2819. //
  2820. // #version 450
  2821. //
  2822. // float foo() {
  2823. // if (true) {
  2824. // }
  2825. // }
  2826. //
  2827. // void main() { foo(); }
  2828. //
  2829. // But the order of basic blocks in foo is changed so that the return
  2830. // block is listed second-last. There is only one return in the callee
  2831. // but it does not appear last.
  2832. const std::string predefs =
  2833. R"(OpCapability Shader
  2834. OpMemoryModel Logical GLSL450
  2835. OpEntryPoint Vertex %main "main"
  2836. OpSource GLSL 450
  2837. OpName %main "main"
  2838. OpName %foo_ "foo("
  2839. %void = OpTypeVoid
  2840. %4 = OpTypeFunction %void
  2841. %bool = OpTypeBool
  2842. %true = OpConstantTrue %bool
  2843. )";
  2844. const std::string foo =
  2845. R"(%foo_ = OpFunction %void None %4
  2846. %7 = OpLabel
  2847. OpSelectionMerge %8 None
  2848. OpBranchConditional %true %9 %8
  2849. %8 = OpLabel
  2850. OpReturn
  2851. %9 = OpLabel
  2852. OpBranch %8
  2853. OpFunctionEnd
  2854. )";
  2855. const std::string fooMergeReturn =
  2856. R"(%uint = OpTypeInt 32 0
  2857. %uint_0 = OpConstant %uint 0
  2858. %false = OpConstantFalse %bool
  2859. %_ptr_Function_bool = OpTypePointer Function %bool
  2860. %foo_ = OpFunction %void None %4
  2861. %7 = OpLabel
  2862. %18 = OpVariable %_ptr_Function_bool Function %false
  2863. OpSelectionMerge %12 None
  2864. OpSwitch %uint_0 %13
  2865. %13 = OpLabel
  2866. OpSelectionMerge %8 None
  2867. OpBranchConditional %true %9 %8
  2868. %8 = OpLabel
  2869. OpStore %18 %true
  2870. OpBranch %12
  2871. %9 = OpLabel
  2872. OpBranch %8
  2873. %12 = OpLabel
  2874. OpReturn
  2875. OpFunctionEnd
  2876. )";
  2877. const std::string before =
  2878. R"(%main = OpFunction %void None %4
  2879. %10 = OpLabel
  2880. %11 = OpFunctionCall %void %foo_
  2881. OpReturn
  2882. OpFunctionEnd
  2883. )";
  2884. const std::string after =
  2885. R"(%main = OpFunction %void None %4
  2886. %10 = OpLabel
  2887. %19 = OpVariable %_ptr_Function_bool Function %false
  2888. OpStore %19 %false
  2889. OpSelectionMerge %24 None
  2890. OpSwitch %uint_0 %21
  2891. %21 = OpLabel
  2892. OpSelectionMerge %22 None
  2893. OpBranchConditional %true %23 %22
  2894. %22 = OpLabel
  2895. OpStore %19 %true
  2896. OpBranch %24
  2897. %23 = OpLabel
  2898. OpBranch %22
  2899. %24 = OpLabel
  2900. OpReturn
  2901. OpFunctionEnd
  2902. )";
  2903. // The early return case must be handled by merge-return first.
  2904. AddPass<MergeReturnPass>();
  2905. AddPass<InlineExhaustivePass>();
  2906. RunAndCheck(predefs + foo + before, predefs + fooMergeReturn + after);
  2907. }
  2908. TEST_F(InlineTest, CalleeWithSingleReturnNeedsSingleTripLoopWrapper) {
  2909. // The case from https://github.com/KhronosGroup/SPIRV-Tools/issues/2018
  2910. //
  2911. // The callee has a single return, but needs single-trip loop wrapper
  2912. // to be inlined because the return is in a selection structure.
  2913. const std::string predefs =
  2914. R"(OpCapability Shader
  2915. %1 = OpExtInstImport "GLSL.std.450"
  2916. OpMemoryModel Logical GLSL450
  2917. OpEntryPoint Fragment %main "main" %_GLF_color
  2918. OpExecutionMode %main OriginUpperLeft
  2919. OpSource ESSL 310
  2920. OpName %main "main"
  2921. OpName %f_ "f("
  2922. OpName %i "i"
  2923. OpName %_GLF_color "_GLF_color"
  2924. OpDecorate %_GLF_color Location 0
  2925. %void = OpTypeVoid
  2926. %7 = OpTypeFunction %void
  2927. %float = OpTypeFloat 32
  2928. %9 = OpTypeFunction %float
  2929. %float_1 = OpConstant %float 1
  2930. %bool = OpTypeBool
  2931. %false = OpConstantFalse %bool
  2932. %true = OpConstantTrue %bool
  2933. %int = OpTypeInt 32 1
  2934. %_ptr_Function_int = OpTypePointer Function %int
  2935. %int_0 = OpConstant %int 0
  2936. %int_1 = OpConstant %int 1
  2937. %v4float = OpTypeVector %float 4
  2938. %_ptr_Output_v4float = OpTypePointer Output %v4float
  2939. %_GLF_color = OpVariable %_ptr_Output_v4float Output
  2940. %float_0 = OpConstant %float 0
  2941. %21 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
  2942. %22 = OpConstantComposite %v4float %float_0 %float_1 %float_0 %float_1
  2943. )";
  2944. const std::string new_predefs =
  2945. R"(%_ptr_Function_float = OpTypePointer Function %float
  2946. %uint = OpTypeInt 32 0
  2947. %uint_0 = OpConstant %uint 0
  2948. %_ptr_Function_bool = OpTypePointer Function %bool
  2949. )";
  2950. const std::string main_before =
  2951. R"(%main = OpFunction %void None %7
  2952. %23 = OpLabel
  2953. %i = OpVariable %_ptr_Function_int Function
  2954. OpStore %i %int_0
  2955. OpBranch %24
  2956. %24 = OpLabel
  2957. OpLoopMerge %25 %26 None
  2958. OpBranch %27
  2959. %27 = OpLabel
  2960. %28 = OpLoad %int %i
  2961. %29 = OpSLessThan %bool %28 %int_1
  2962. OpBranchConditional %29 %30 %25
  2963. %30 = OpLabel
  2964. OpStore %_GLF_color %21
  2965. %31 = OpFunctionCall %float %f_
  2966. OpBranch %26
  2967. %26 = OpLabel
  2968. %32 = OpLoad %int %i
  2969. %33 = OpIAdd %int %32 %int_1
  2970. OpStore %i %33
  2971. OpBranch %24
  2972. %25 = OpLabel
  2973. OpStore %_GLF_color %22
  2974. OpReturn
  2975. OpFunctionEnd
  2976. )";
  2977. const std::string main_after =
  2978. R"(%main = OpFunction %void None %7
  2979. %23 = OpLabel
  2980. %46 = OpVariable %_ptr_Function_bool Function %false
  2981. %47 = OpVariable %_ptr_Function_float Function
  2982. %48 = OpVariable %_ptr_Function_float Function
  2983. %i = OpVariable %_ptr_Function_int Function
  2984. OpStore %i %int_0
  2985. OpBranch %24
  2986. %24 = OpLabel
  2987. OpLoopMerge %25 %26 None
  2988. OpBranch %27
  2989. %27 = OpLabel
  2990. %28 = OpLoad %int %i
  2991. %29 = OpSLessThan %bool %28 %int_1
  2992. OpBranchConditional %29 %30 %25
  2993. %30 = OpLabel
  2994. OpStore %_GLF_color %21
  2995. OpStore %46 %false
  2996. OpSelectionMerge %53 None
  2997. OpSwitch %uint_0 %50
  2998. %50 = OpLabel
  2999. OpSelectionMerge %52 None
  3000. OpBranchConditional %true %51 %52
  3001. %51 = OpLabel
  3002. OpStore %46 %true
  3003. OpStore %47 %float_1
  3004. OpBranch %53
  3005. %52 = OpLabel
  3006. OpStore %46 %true
  3007. OpStore %47 %float_1
  3008. OpBranch %53
  3009. %53 = OpLabel
  3010. %54 = OpLoad %float %47
  3011. OpStore %48 %54
  3012. %31 = OpLoad %float %48
  3013. OpBranch %26
  3014. %26 = OpLabel
  3015. %32 = OpLoad %int %i
  3016. %33 = OpIAdd %int %32 %int_1
  3017. OpStore %i %33
  3018. OpBranch %24
  3019. %25 = OpLabel
  3020. OpStore %_GLF_color %22
  3021. OpReturn
  3022. OpFunctionEnd
  3023. )";
  3024. const std::string callee =
  3025. R"(%f_ = OpFunction %float None %9
  3026. %34 = OpLabel
  3027. OpSelectionMerge %35 None
  3028. OpBranchConditional %true %36 %35
  3029. %36 = OpLabel
  3030. OpReturnValue %float_1
  3031. %35 = OpLabel
  3032. OpReturnValue %float_1
  3033. OpFunctionEnd
  3034. )";
  3035. const std::string calleeMergeReturn =
  3036. R"(%f_ = OpFunction %float None %9
  3037. %34 = OpLabel
  3038. %45 = OpVariable %_ptr_Function_bool Function %false
  3039. %39 = OpVariable %_ptr_Function_float Function
  3040. OpSelectionMerge %37 None
  3041. OpSwitch %uint_0 %41
  3042. %41 = OpLabel
  3043. OpSelectionMerge %35 None
  3044. OpBranchConditional %true %36 %35
  3045. %36 = OpLabel
  3046. OpStore %45 %true
  3047. OpStore %39 %float_1
  3048. OpBranch %37
  3049. %35 = OpLabel
  3050. OpStore %45 %true
  3051. OpStore %39 %float_1
  3052. OpBranch %37
  3053. %37 = OpLabel
  3054. %40 = OpLoad %float %39
  3055. OpReturnValue %40
  3056. OpFunctionEnd
  3057. )";
  3058. // The early return case must be handled by merge-return first.
  3059. AddPass<MergeReturnPass>();
  3060. AddPass<InlineExhaustivePass>();
  3061. RunAndCheck(predefs + main_before + callee,
  3062. predefs + new_predefs + main_after + calleeMergeReturn);
  3063. }
  3064. TEST_F(InlineTest, ForwardReferencesInPhiInlined) {
  3065. // The basic structure of the test case is like this:
  3066. //
  3067. // int foo() {
  3068. // int result = 1;
  3069. // if (true) {
  3070. // result = 1;
  3071. // }
  3072. // return result;
  3073. // }
  3074. //
  3075. // void main() {
  3076. // int x = foo();
  3077. // }
  3078. //
  3079. // but with modifications: Using Phi instead of load/store, and the
  3080. // return block in foo appears before the "then" block.
  3081. const std::string predefs =
  3082. R"(OpCapability Shader
  3083. %1 = OpExtInstImport "GLSL.std.450"
  3084. OpMemoryModel Logical GLSL450
  3085. OpEntryPoint Vertex %main "main"
  3086. OpSource GLSL 450
  3087. OpName %main "main"
  3088. OpName %foo_ "foo("
  3089. OpName %x "x"
  3090. %void = OpTypeVoid
  3091. %6 = OpTypeFunction %void
  3092. %int = OpTypeInt 32 1
  3093. %8 = OpTypeFunction %int
  3094. %bool = OpTypeBool
  3095. %true = OpConstantTrue %bool
  3096. %int_0 = OpConstant %int 0
  3097. %_ptr_Function_int = OpTypePointer Function %int
  3098. )";
  3099. const std::string callee =
  3100. R"(%foo_ = OpFunction %int None %8
  3101. %13 = OpLabel
  3102. %14 = OpCopyObject %int %int_0
  3103. OpSelectionMerge %15 None
  3104. OpBranchConditional %true %16 %15
  3105. %15 = OpLabel
  3106. %17 = OpPhi %int %14 %13 %18 %16
  3107. OpReturnValue %17
  3108. %16 = OpLabel
  3109. %18 = OpCopyObject %int %int_0
  3110. OpBranch %15
  3111. OpFunctionEnd
  3112. )";
  3113. const std::string calleeMergeReturn =
  3114. R"(%uint = OpTypeInt 32 0
  3115. %uint_0 = OpConstant %uint 0
  3116. %false = OpConstantFalse %bool
  3117. %_ptr_Function_bool = OpTypePointer Function %bool
  3118. %foo_ = OpFunction %int None %8
  3119. %13 = OpLabel
  3120. %29 = OpVariable %_ptr_Function_bool Function %false
  3121. %22 = OpVariable %_ptr_Function_int Function
  3122. OpSelectionMerge %21 None
  3123. OpSwitch %uint_0 %24
  3124. %24 = OpLabel
  3125. %14 = OpCopyObject %int %int_0
  3126. OpSelectionMerge %15 None
  3127. OpBranchConditional %true %16 %15
  3128. %15 = OpLabel
  3129. %17 = OpPhi %int %14 %24 %18 %16
  3130. OpStore %29 %true
  3131. OpStore %22 %17
  3132. OpBranch %21
  3133. %16 = OpLabel
  3134. %18 = OpCopyObject %int %int_0
  3135. OpBranch %15
  3136. %21 = OpLabel
  3137. %23 = OpLoad %int %22
  3138. OpReturnValue %23
  3139. OpFunctionEnd
  3140. )";
  3141. const std::string before =
  3142. R"(%main = OpFunction %void None %6
  3143. %19 = OpLabel
  3144. %x = OpVariable %_ptr_Function_int Function
  3145. %20 = OpFunctionCall %int %foo_
  3146. OpStore %x %20
  3147. OpReturn
  3148. OpFunctionEnd
  3149. )";
  3150. const std::string after =
  3151. R"(%main = OpFunction %void None %6
  3152. %19 = OpLabel
  3153. %30 = OpVariable %_ptr_Function_bool Function %false
  3154. %31 = OpVariable %_ptr_Function_int Function
  3155. %32 = OpVariable %_ptr_Function_int Function
  3156. %x = OpVariable %_ptr_Function_int Function
  3157. OpStore %30 %false
  3158. OpSelectionMerge %40 None
  3159. OpSwitch %uint_0 %34
  3160. %34 = OpLabel
  3161. %35 = OpCopyObject %int %int_0
  3162. OpSelectionMerge %36 None
  3163. OpBranchConditional %true %38 %36
  3164. %36 = OpLabel
  3165. %37 = OpPhi %int %35 %34 %39 %38
  3166. OpStore %30 %true
  3167. OpStore %31 %37
  3168. OpBranch %40
  3169. %38 = OpLabel
  3170. %39 = OpCopyObject %int %int_0
  3171. OpBranch %36
  3172. %40 = OpLabel
  3173. %41 = OpLoad %int %31
  3174. OpStore %32 %41
  3175. %20 = OpLoad %int %32
  3176. OpStore %x %20
  3177. OpReturn
  3178. OpFunctionEnd
  3179. )";
  3180. AddPass<MergeReturnPass>();
  3181. AddPass<InlineExhaustivePass>();
  3182. RunAndCheck(predefs + callee + before, predefs + calleeMergeReturn + after);
  3183. }
  3184. TEST_F(InlineTest, DebugSimple) {
  3185. // Check that it correctly generates DebugInlinedAt and maps it to DebugScope
  3186. // for the inlined function foo().
  3187. const std::string text = R"(
  3188. ; CHECK: [[main_name:%\d+]] = OpString "main"
  3189. ; CHECK: [[foo_name:%\d+]] = OpString "foo"
  3190. ; CHECK: [[dbg_main:%\d+]] = OpExtInst %void {{%\d+}} DebugFunction [[main_name]] {{%\d+}} {{%\d+}} 4 1 {{%\d+}} [[main_name]] FlagIsProtected|FlagIsPrivate 4 [[main:%\d+]]
  3191. ; CHECK: [[dbg_foo:%\d+]] = OpExtInst %void {{%\d+}} DebugFunction [[foo_name]] {{%\d+}} {{%\d+}} 1 1 {{%\d+}} [[foo_name]] FlagIsProtected|FlagIsPrivate 1 [[foo:%\d+]]
  3192. ; CHECK: [[foo_bb:%\d+]] = OpExtInst %void {{%\d+}} DebugLexicalBlock {{%\d+}} 1 14 [[dbg_foo]]
  3193. ; CHECK: [[inlined_at:%\d+]] = OpExtInst %void {{%\d+}} DebugInlinedAt 4 [[dbg_main]]
  3194. ; CHECK: [[main]] = OpFunction %void None
  3195. ; CHECK: {{%\d+}} = OpExtInst %void {{%\d+}} DebugScope [[foo_bb]] [[inlined_at]]
  3196. ; CHECK: [[foo]] = OpFunction %v4float None
  3197. OpCapability Shader
  3198. %1 = OpExtInstImport "OpenCL.DebugInfo.100"
  3199. OpMemoryModel Logical GLSL450
  3200. OpEntryPoint Fragment %main "main" %3 %4
  3201. OpExecutionMode %main OriginUpperLeft
  3202. %5 = OpString "ps.hlsl"
  3203. OpSource HLSL 600 %5
  3204. %6 = OpString "float"
  3205. %main_name = OpString "main"
  3206. %foo_name = OpString "foo"
  3207. OpDecorate %3 Location 0
  3208. OpDecorate %4 Location 0
  3209. %uint = OpTypeInt 32 0
  3210. %uint_32 = OpConstant %uint 32
  3211. %float = OpTypeFloat 32
  3212. %float_1 = OpConstant %float 1
  3213. %v4float = OpTypeVector %float 4
  3214. %14 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  3215. %_ptr_Input_v4float = OpTypePointer Input %v4float
  3216. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3217. %void = OpTypeVoid
  3218. %18 = OpTypeFunction %void
  3219. %19 = OpTypeFunction %v4float
  3220. %3 = OpVariable %_ptr_Input_v4float Input
  3221. %4 = OpVariable %_ptr_Output_v4float Output
  3222. %20 = OpExtInst %void %1 DebugSource %5
  3223. %21 = OpExtInst %void %1 DebugCompilationUnit 1 4 %20 HLSL
  3224. %22 = OpExtInst %void %1 DebugTypeBasic %6 %uint_32 Float
  3225. %23 = OpExtInst %void %1 DebugTypeVector %22 4
  3226. %24 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %23 %23
  3227. %25 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %23
  3228. %dbg_main = OpExtInst %void %1 DebugFunction %main_name %24 %20 4 1 %21 %main_name FlagIsProtected|FlagIsPrivate 4 %main
  3229. %dbg_foo = OpExtInst %void %1 DebugFunction %foo_name %25 %20 1 1 %21 %foo_name FlagIsProtected|FlagIsPrivate 1 %foo
  3230. %29 = OpExtInst %void %1 DebugLexicalBlock %20 1 14 %dbg_foo
  3231. %main = OpFunction %void None %18
  3232. %30 = OpLabel
  3233. %31 = OpExtInst %void %1 DebugScope %dbg_main
  3234. %32 = OpFunctionCall %v4float %foo
  3235. %33 = OpLoad %v4float %3
  3236. %34 = OpFAdd %v4float %32 %33
  3237. OpStore %4 %34
  3238. OpReturn
  3239. OpFunctionEnd
  3240. %foo = OpFunction %v4float None %19
  3241. %35 = OpExtInst %void %1 DebugScope %dbg_foo
  3242. %36 = OpLabel
  3243. %37 = OpExtInst %void %1 DebugScope %29
  3244. OpReturnValue %14
  3245. OpFunctionEnd
  3246. )";
  3247. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  3248. }
  3249. TEST_F(InlineTest, ShaderDebugSimple) {
  3250. // Same as DebugSimple but for NonSemantic.Shader.DebugInfo.100.
  3251. const std::string text = R"(
  3252. ; CHECK: [[main_name:%\d+]] = OpString "main"
  3253. ; CHECK: [[foo_name:%\d+]] = OpString "foo"
  3254. ; CHECK: [[dbg_main:%\d+]] = OpExtInst %void {{%\d+}} DebugFunction [[main_name]] {{%\d+}} {{%\d+}} %uint_4 %uint_1 {{%\d+}} [[main_name]] %uint_3 %uint_4
  3255. ; CHECK: [[dbg_foo:%\d+]] = OpExtInst %void {{%\d+}} DebugFunction [[foo_name]] {{%\d+}} {{%\d+}} %uint_1 %uint_1 {{%\d+}} [[foo_name]] %uint_3 %uint_1
  3256. ; CHECK: [[foo_bb:%\d+]] = OpExtInst %void {{%\d+}} DebugLexicalBlock {{%\d+}} %uint_1 %uint_14 [[dbg_foo]]
  3257. ; CHECK: [[inlined_at:%\d+]] = OpExtInst %void {{%\d+}} DebugInlinedAt %uint_4 [[dbg_main]]
  3258. ; CHECK: [[main:%\d+]] = OpFunction %void None
  3259. ; CHECK: {{%\d+}} = OpExtInst %void {{%\d+}} DebugScope [[foo_bb]] [[inlined_at]]
  3260. ; CHECK: [[foo:%\d+]] = OpFunction %v4float None
  3261. OpCapability Shader
  3262. OpExtension "SPV_KHR_non_semantic_info"
  3263. %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
  3264. OpMemoryModel Logical GLSL450
  3265. OpEntryPoint Fragment %main "main" %3 %4
  3266. OpExecutionMode %main OriginUpperLeft
  3267. %5 = OpString "ps.hlsl"
  3268. OpSource HLSL 600 %5
  3269. %6 = OpString "float"
  3270. %main_name = OpString "main"
  3271. %foo_name = OpString "foo"
  3272. OpDecorate %3 Location 0
  3273. OpDecorate %4 Location 0
  3274. %uint = OpTypeInt 32 0
  3275. %uint_0 = OpConstant %uint 0
  3276. %uint_1 = OpConstant %uint 1
  3277. %uint_2 = OpConstant %uint 2
  3278. %uint_3 = OpConstant %uint 3
  3279. %uint_4 = OpConstant %uint 4
  3280. %uint_5 = OpConstant %uint 5
  3281. %uint_14 = OpConstant %uint 14
  3282. %uint_32 = OpConstant %uint 32
  3283. %float = OpTypeFloat 32
  3284. %float_1 = OpConstant %float 1
  3285. %v4float = OpTypeVector %float 4
  3286. %14 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  3287. %_ptr_Input_v4float = OpTypePointer Input %v4float
  3288. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3289. %void = OpTypeVoid
  3290. %18 = OpTypeFunction %void
  3291. %19 = OpTypeFunction %v4float
  3292. %3 = OpVariable %_ptr_Input_v4float Input
  3293. %4 = OpVariable %_ptr_Output_v4float Output
  3294. %20 = OpExtInst %void %1 DebugSource %5
  3295. %21 = OpExtInst %void %1 DebugCompilationUnit %uint_1 %uint_4 %20 %uint_5
  3296. %22 = OpExtInst %void %1 DebugTypeBasic %6 %uint_32 %uint_3 %uint_0
  3297. %23 = OpExtInst %void %1 DebugTypeVector %22 %uint_4
  3298. %24 = OpExtInst %void %1 DebugTypeFunction %uint_3 %23 %23
  3299. %25 = OpExtInst %void %1 DebugTypeFunction %uint_3 %23
  3300. %dbg_main = OpExtInst %void %1 DebugFunction %main_name %24 %20 %uint_4 %uint_1 %21 %main_name %uint_3 %uint_4
  3301. %dbg_foo = OpExtInst %void %1 DebugFunction %foo_name %25 %20 %uint_1 %uint_1 %21 %foo_name %uint_3 %uint_1
  3302. %29 = OpExtInst %void %1 DebugLexicalBlock %20 %uint_1 %uint_14 %dbg_foo
  3303. %main = OpFunction %void None %18
  3304. %30 = OpLabel
  3305. %dbg_main_def = OpExtInst %void %1 DebugFunctionDefinition %dbg_main %main
  3306. %31 = OpExtInst %void %1 DebugScope %dbg_main
  3307. %32 = OpFunctionCall %v4float %foo
  3308. %33 = OpLoad %v4float %3
  3309. %34 = OpFAdd %v4float %32 %33
  3310. OpStore %4 %34
  3311. OpReturn
  3312. OpFunctionEnd
  3313. %foo = OpFunction %v4float None %19
  3314. %36 = OpLabel
  3315. %dbg_foo_def = OpExtInst %void %1 DebugFunctionDefinition %dbg_foo %foo
  3316. %35 = OpExtInst %void %1 DebugScope %dbg_foo
  3317. %37 = OpExtInst %void %1 DebugScope %29
  3318. OpReturnValue %14
  3319. OpFunctionEnd
  3320. )";
  3321. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  3322. }
  3323. TEST_F(InlineTest, DebugNested) {
  3324. // When function main() calls function zoo() and function zoo() calls
  3325. // function bar() and function bar() calls function foo(), check that
  3326. // the inline pass correctly generates DebugInlinedAt instructions
  3327. // for the nested function calls.
  3328. const std::string text = R"(
  3329. ; CHECK: [[v4f1:%\d+]] = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  3330. ; CHECK: [[v4f2:%\d+]] = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
  3331. ; CHECK: [[v4f3:%\d+]] = OpConstantComposite %v4float %float_3 %float_3 %float_3 %float_3
  3332. ; CHECK: [[color:%\d+]] = OpVariable %_ptr_Input_v4float Input
  3333. ; CHECK: [[dbg_main:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction {{%\d+}} {{%\d+}} {{%\d+}} 10 1 {{%\d+}} {{%\d+}} FlagIsProtected|FlagIsPrivate 10 [[main:%\d+]]
  3334. ; CHECK: [[dbg_foo:%\d+]] = OpExtInst %void [[ext]] DebugFunction {{%\d+}} {{%\d+}} {{%\d+}} 1 1 {{%\d+}} {{%\d+}} FlagIsProtected|FlagIsPrivate 1 [[foo:%\d+]]
  3335. ; CHECK: [[dbg_bar:%\d+]] = OpExtInst %void [[ext]] DebugFunction {{%\d+}} {{%\d+}} {{%\d+}} 4 1 {{%\d+}} {{%\d+}} FlagIsProtected|FlagIsPrivate 4 [[bar:%\d+]]
  3336. ; CHECK: [[dbg_zoo:%\d+]] = OpExtInst %void [[ext]] DebugFunction {{%\d+}} {{%\d+}} {{%\d+}} 7 1 {{%\d+}} {{%\d+}} FlagIsProtected|FlagIsPrivate 7 [[zoo:%\d+]]
  3337. ; CHECK: [[inlined_to_main:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 600 [[dbg_main]]
  3338. ; CHECK: [[inlined_to_zoo:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 700 [[dbg_zoo]] [[inlined_to_main]]
  3339. ; CHECK: [[inlined_to_bar:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 300 [[dbg_bar]] [[inlined_to_zoo]]
  3340. ; CHECK: [[main]] = OpFunction %void None
  3341. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_foo]] [[inlined_to_bar]]
  3342. ; CHECK-NEXT: OpLine {{%\d+}} 100 0
  3343. ; CHECK-NEXT: OpStore {{%\d+}} [[v4f1]]
  3344. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_bar]] [[inlined_to_zoo]]
  3345. ; CHECK-NEXT: OpLine {{%\d+}} 300 0
  3346. ; CHECK-NEXT: [[foo_ret:%\d+]] = OpLoad %v4float
  3347. ; CHECK-NEXT: OpLine {{%\d+}} 400 0
  3348. ; CHECK-NEXT: {{%\d+}} = OpFAdd %v4float [[foo_ret]] [[v4f2]]
  3349. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_zoo]] [[inlined_to_main]]
  3350. ; CHECK-NEXT: OpLine {{%\d+}} 700 0
  3351. ; CHECK-NEXT: [[bar_ret:%\d+]] = OpLoad %v4float
  3352. ; CHECK-NEXT: {{%\d+}} = OpFAdd %v4float [[bar_ret]] [[v4f3]]
  3353. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_main]]
  3354. ; CHECK-NEXT: OpLine {{%\d+}} 600 0
  3355. ; CHECK-NEXT: [[zoo_ret:%\d+]] = OpLoad %v4float
  3356. ; CHECK-NEXT: [[color_val:%\d+]] = OpLoad %v4float [[color]]
  3357. ; CHECK-NEXT: {{%\d+}} = OpFAdd %v4float [[zoo_ret]] [[color_val]]
  3358. ; CHECK: [[foo]] = OpFunction %v4float None
  3359. ; CHECK: [[bar]] = OpFunction %v4float None
  3360. ; CHECK: [[zoo]] = OpFunction %v4float None
  3361. OpCapability Shader
  3362. %1 = OpExtInstImport "OpenCL.DebugInfo.100"
  3363. OpMemoryModel Logical GLSL450
  3364. OpEntryPoint Fragment %main "main" %3 %4
  3365. OpExecutionMode %main OriginUpperLeft
  3366. %5 = OpString "ps.hlsl"
  3367. OpSource HLSL 600 %5
  3368. %6 = OpString "float"
  3369. %7 = OpString "main"
  3370. %8 = OpString "foo"
  3371. %9 = OpString "bar"
  3372. %10 = OpString "zoo"
  3373. OpDecorate %3 Location 0
  3374. OpDecorate %4 Location 0
  3375. %uint = OpTypeInt 32 0
  3376. %uint_32 = OpConstant %uint 32
  3377. %float = OpTypeFloat 32
  3378. %float_1 = OpConstant %float 1
  3379. %float_2 = OpConstant %float 2
  3380. %float_3 = OpConstant %float 3
  3381. %v4float = OpTypeVector %float 4
  3382. %18 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  3383. %19 = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
  3384. %20 = OpConstantComposite %v4float %float_3 %float_3 %float_3 %float_3
  3385. %_ptr_Input_v4float = OpTypePointer Input %v4float
  3386. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3387. %void = OpTypeVoid
  3388. %24 = OpTypeFunction %void
  3389. %25 = OpTypeFunction %v4float
  3390. %3 = OpVariable %_ptr_Input_v4float Input
  3391. %4 = OpVariable %_ptr_Output_v4float Output
  3392. %26 = OpExtInst %void %1 DebugSource %5
  3393. %27 = OpExtInst %void %1 DebugCompilationUnit 1 4 %26 HLSL
  3394. %28 = OpExtInst %void %1 DebugTypeBasic %6 %uint_32 Float
  3395. %29 = OpExtInst %void %1 DebugTypeVector %28 4
  3396. %30 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %29 %29
  3397. %31 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %29
  3398. %32 = OpExtInst %void %1 DebugFunction %7 %30 %26 10 1 %27 %7 FlagIsProtected|FlagIsPrivate 10 %main
  3399. %33 = OpExtInst %void %1 DebugFunction %8 %31 %26 1 1 %27 %8 FlagIsProtected|FlagIsPrivate 1 %foo
  3400. %35 = OpExtInst %void %1 DebugFunction %9 %31 %26 4 1 %27 %9 FlagIsProtected|FlagIsPrivate 4 %bar
  3401. %37 = OpExtInst %void %1 DebugFunction %10 %31 %26 7 1 %27 %10 FlagIsProtected|FlagIsPrivate 7 %zoo
  3402. %main = OpFunction %void None %24
  3403. %39 = OpLabel
  3404. %40 = OpExtInst %void %1 DebugScope %32
  3405. OpLine %5 600 0
  3406. %41 = OpFunctionCall %v4float %zoo
  3407. %42 = OpLoad %v4float %3
  3408. %43 = OpFAdd %v4float %41 %42
  3409. OpStore %4 %43
  3410. OpReturn
  3411. OpFunctionEnd
  3412. %foo = OpFunction %v4float None %25
  3413. %44 = OpExtInst %void %1 DebugScope %33
  3414. %45 = OpLabel
  3415. OpLine %5 100 0
  3416. OpReturnValue %18
  3417. OpFunctionEnd
  3418. OpLine %5 200 0
  3419. %bar = OpFunction %v4float None %25
  3420. %46 = OpExtInst %void %1 DebugScope %35
  3421. %47 = OpLabel
  3422. OpLine %5 300 0
  3423. %48 = OpFunctionCall %v4float %foo
  3424. OpLine %5 400 0
  3425. %49 = OpFAdd %v4float %48 %19
  3426. OpLine %5 500 0
  3427. OpReturnValue %49
  3428. OpFunctionEnd
  3429. %zoo = OpFunction %v4float None %25
  3430. %50 = OpExtInst %void %1 DebugScope %37
  3431. %51 = OpLabel
  3432. OpLine %5 700 0
  3433. %52 = OpFunctionCall %v4float %bar
  3434. %53 = OpFAdd %v4float %52 %20
  3435. OpReturnValue %53
  3436. OpFunctionEnd
  3437. )";
  3438. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  3439. }
  3440. TEST_F(InlineTest, DebugSimpleHLSLPixelShader) {
  3441. const std::string text = R"(
  3442. ; CHECK: [[dbg_main:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction {{%\d+}} {{%\d+}} {{%\d+}} 1 1 {{%\d+}} {{%\d+}} FlagIsProtected|FlagIsPrivate 1 %src_main
  3443. ; CHECK: [[lex_blk:%\d+]] = OpExtInst %void [[ext]] DebugLexicalBlock {{%\d+}} 1 47 [[dbg_main]]
  3444. ; CHECK: %main = OpFunction %void None
  3445. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_main]]
  3446. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugDeclare {{%\d+}} %param_var_color
  3447. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[lex_blk]]
  3448. ; CHECK: OpLine {{%\d+}} 2 10
  3449. ; CHECK: {{%\d+}} = OpLoad %v4float %param_var_color
  3450. ; CHECK: OpLine {{%\d+}} 2 3
  3451. ; CHECK: OpFunctionEnd
  3452. ; CHECK: %src_main = OpFunction %v4float None
  3453. OpCapability Shader
  3454. %1 = OpExtInstImport "OpenCL.DebugInfo.100"
  3455. OpMemoryModel Logical GLSL450
  3456. OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_TARGET
  3457. OpExecutionMode %main OriginUpperLeft
  3458. %5 = OpString "ps.hlsl"
  3459. OpSource HLSL 600 %5
  3460. %14 = OpString "#line 1 \"ps.hlsl\"
  3461. float4 main(float4 color : COLOR) : SV_TARGET {
  3462. return color;
  3463. }
  3464. "
  3465. %17 = OpString "float"
  3466. %21 = OpString "src.main"
  3467. %24 = OpString "color"
  3468. OpName %in_var_COLOR "in.var.COLOR"
  3469. OpName %out_var_SV_TARGET "out.var.SV_TARGET"
  3470. OpName %main "main"
  3471. OpName %param_var_color "param.var.color"
  3472. OpName %src_main "src.main"
  3473. OpName %color "color"
  3474. OpName %bb_entry "bb.entry"
  3475. OpDecorate %in_var_COLOR Location 0
  3476. OpDecorate %out_var_SV_TARGET Location 0
  3477. %uint = OpTypeInt 32 0
  3478. %uint_32 = OpConstant %uint 32
  3479. %float = OpTypeFloat 32
  3480. %v4float = OpTypeVector %float 4
  3481. %_ptr_Input_v4float = OpTypePointer Input %v4float
  3482. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3483. %void = OpTypeVoid
  3484. %27 = OpTypeFunction %void
  3485. %_ptr_Function_v4float = OpTypePointer Function %v4float
  3486. %33 = OpTypeFunction %v4float %_ptr_Function_v4float
  3487. %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
  3488. %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
  3489. %13 = OpExtInst %void %1 DebugExpression
  3490. %15 = OpExtInst %void %1 DebugSource %5 %14
  3491. %16 = OpExtInst %void %1 DebugCompilationUnit 1 4 %15 HLSL
  3492. %18 = OpExtInst %void %1 DebugTypeBasic %17 %uint_32 Float
  3493. %19 = OpExtInst %void %1 DebugTypeVector %18 4
  3494. %20 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %19 %19
  3495. %22 = OpExtInst %void %1 DebugFunction %21 %20 %15 1 1 %16 %21 FlagIsProtected|FlagIsPrivate 1 %src_main
  3496. %25 = OpExtInst %void %1 DebugLocalVariable %24 %19 %15 1 20 %22 FlagIsLocal 0
  3497. %26 = OpExtInst %void %1 DebugLexicalBlock %15 1 47 %22
  3498. %main = OpFunction %void None %27
  3499. %28 = OpLabel
  3500. %param_var_color = OpVariable %_ptr_Function_v4float Function
  3501. %31 = OpLoad %v4float %in_var_COLOR
  3502. OpStore %param_var_color %31
  3503. %32 = OpFunctionCall %v4float %src_main %param_var_color
  3504. OpStore %out_var_SV_TARGET %32
  3505. OpReturn
  3506. OpFunctionEnd
  3507. OpLine %5 1 1
  3508. %src_main = OpFunction %v4float None %33
  3509. %34 = OpExtInst %void %1 DebugScope %22
  3510. %color = OpFunctionParameter %_ptr_Function_v4float
  3511. %36 = OpExtInst %void %1 DebugDeclare %25 %color %13
  3512. %bb_entry = OpLabel
  3513. %38 = OpExtInst %void %1 DebugScope %26
  3514. OpLine %5 2 10
  3515. %39 = OpLoad %v4float %color
  3516. OpLine %5 2 3
  3517. OpReturnValue %39
  3518. OpFunctionEnd
  3519. )";
  3520. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  3521. }
  3522. TEST_F(InlineTest, ShaderDebugSimpleHLSLPixelShader) {
  3523. // Same as DebugSimpleHLSLPixelShader but for
  3524. // NonSemantic.Shader.DebugInfo.100.
  3525. const std::string text = R"(
  3526. ; CHECK: [[dbg_main:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction {{%\d+}} {{%\d+}} {{%\d+}} %uint_1 %uint_1 {{%\d+}} {{%\d+}} %uint_3 %uint_1
  3527. ; CHECK: [[lex_blk:%\d+]] = OpExtInst %void [[ext]] DebugLexicalBlock {{%\d+}} %uint_1 %uint_47 [[dbg_main]]
  3528. ; CHECK: %main = OpFunction %void None
  3529. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_main]]
  3530. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugDeclare {{%\d+}} %param_var_color
  3531. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[lex_blk]]
  3532. ; CHECK: {{%\d+}} = OpExtInst %void %1 DebugLine {{%\d+}} %uint_2 %uint_2 %uint_10 %uint_10
  3533. ; CHECK: {{%\d+}} = OpLoad %v4float %param_var_color
  3534. ; CHECK: {{%\d+}} = OpExtInst %void %1 DebugLine {{%\d+}} %uint_2 %uint_2 %uint_3 %uint_3
  3535. ; CHECK: OpFunctionEnd
  3536. ; CHECK: %src_main = OpFunction %v4float None
  3537. OpCapability Shader
  3538. OpExtension "SPV_KHR_non_semantic_info"
  3539. %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
  3540. OpMemoryModel Logical GLSL450
  3541. OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_TARGET
  3542. OpExecutionMode %main OriginUpperLeft
  3543. %5 = OpString "ps.hlsl"
  3544. OpSource HLSL 600 %5
  3545. %14 = OpString "#line 1 \"ps.hlsl\"
  3546. float4 main(float4 color : COLOR) : SV_TARGET {
  3547. return color;
  3548. }
  3549. "
  3550. %17 = OpString "float"
  3551. %21 = OpString "src.main"
  3552. %24 = OpString "color"
  3553. OpName %in_var_COLOR "in.var.COLOR"
  3554. OpName %out_var_SV_TARGET "out.var.SV_TARGET"
  3555. OpName %main "main"
  3556. OpName %param_var_color "param.var.color"
  3557. OpName %src_main "src.main"
  3558. OpName %color "color"
  3559. OpName %bb_entry "bb.entry"
  3560. OpDecorate %in_var_COLOR Location 0
  3561. OpDecorate %out_var_SV_TARGET Location 0
  3562. %uint = OpTypeInt 32 0
  3563. %uint_0 = OpConstant %uint 0
  3564. %uint_1 = OpConstant %uint 1
  3565. %uint_2 = OpConstant %uint 2
  3566. %uint_3 = OpConstant %uint 3
  3567. %uint_4 = OpConstant %uint 4
  3568. %uint_5 = OpConstant %uint 5
  3569. %uint_10 = OpConstant %uint 10
  3570. %uint_20 = OpConstant %uint 20
  3571. %uint_32 = OpConstant %uint 32
  3572. %uint_47 = OpConstant %uint 47
  3573. %float = OpTypeFloat 32
  3574. %v4float = OpTypeVector %float 4
  3575. %_ptr_Input_v4float = OpTypePointer Input %v4float
  3576. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3577. %void = OpTypeVoid
  3578. %27 = OpTypeFunction %void
  3579. %_ptr_Function_v4float = OpTypePointer Function %v4float
  3580. %33 = OpTypeFunction %v4float %_ptr_Function_v4float
  3581. %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
  3582. %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
  3583. %13 = OpExtInst %void %1 DebugExpression
  3584. %15 = OpExtInst %void %1 DebugSource %5 %14
  3585. %16 = OpExtInst %void %1 DebugCompilationUnit %uint_1 %uint_4 %15 %uint_5
  3586. %18 = OpExtInst %void %1 DebugTypeBasic %17 %uint_32 %uint_3 %uint_0
  3587. %19 = OpExtInst %void %1 DebugTypeVector %18 %uint_4
  3588. %20 = OpExtInst %void %1 DebugTypeFunction %uint_3 %19 %19
  3589. %22 = OpExtInst %void %1 DebugFunction %21 %20 %15 %uint_1 %uint_1 %16 %21 %uint_3 %uint_1
  3590. %25 = OpExtInst %void %1 DebugLocalVariable %24 %19 %15 %uint_1 %uint_20 %22 %uint_4 %uint_0
  3591. %26 = OpExtInst %void %1 DebugLexicalBlock %15 %uint_1 %uint_47 %22
  3592. %main = OpFunction %void None %27
  3593. %28 = OpLabel
  3594. %param_var_color = OpVariable %_ptr_Function_v4float Function
  3595. %31 = OpLoad %v4float %in_var_COLOR
  3596. OpStore %param_var_color %31
  3597. %32 = OpFunctionCall %v4float %src_main %param_var_color
  3598. OpStore %out_var_SV_TARGET %32
  3599. OpReturn
  3600. OpFunctionEnd
  3601. %src_main = OpFunction %v4float None %33
  3602. %color = OpFunctionParameter %_ptr_Function_v4float
  3603. %bb_entry = OpLabel
  3604. %140 = OpExtInst %void %1 DebugFunctionDefinition %22 %src_main
  3605. %141 = OpExtInst %void %1 DebugLine %15 %uint_1 %uint_1 %uint_1 %uint_1
  3606. %34 = OpExtInst %void %1 DebugScope %22
  3607. %36 = OpExtInst %void %1 DebugDeclare %25 %color %13
  3608. %38 = OpExtInst %void %1 DebugScope %26
  3609. %142 = OpExtInst %void %1 DebugLine %15 %uint_2 %uint_2 %uint_10 %uint_10
  3610. %39 = OpLoad %v4float %color
  3611. %143 = OpExtInst %void %1 DebugLine %15 %uint_2 %uint_2 %uint_3 %uint_3
  3612. OpReturnValue %39
  3613. OpFunctionEnd
  3614. )";
  3615. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  3616. }
  3617. TEST_F(InlineTest, DebugDeclareForCalleeFunctionParam) {
  3618. // Check that InlinePass correctly generates DebugDeclare instructions
  3619. // for callee function's parameters and maps them to corresponding
  3620. // local variables of caller function.
  3621. const std::string text = R"(
  3622. ; CHECK: [[add:%\d+]] = OpString "add"
  3623. ; CHECK: [[a:%\d+]] = OpString "a"
  3624. ; CHECK: [[b:%\d+]] = OpString "b"
  3625. ; CHECK: [[dbg_add:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction [[add]]
  3626. ; CHECK: [[dbg_a:%\d+]] = OpExtInst %void [[ext]] DebugLocalVariable [[a]]
  3627. ; CHECK: [[dbg_b:%\d+]] = OpExtInst %void [[ext]] DebugLocalVariable [[b]]
  3628. ; CHECK: [[inlinedat:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 5
  3629. ; CHECK: OpStore [[param_a:%\d+]]
  3630. ; CHECK: OpStore [[param_b:%\d+]]
  3631. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_add]] [[inlinedat]]
  3632. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugDeclare [[dbg_a]] [[param_a]]
  3633. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugDeclare [[dbg_b]] [[param_b]]
  3634. OpCapability Shader
  3635. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  3636. OpMemoryModel Logical GLSL450
  3637. OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_TARGET
  3638. OpExecutionMode %main OriginUpperLeft
  3639. %file_name = OpString "ps.hlsl"
  3640. OpSource HLSL 600 %file_name
  3641. %float_name = OpString "float"
  3642. %main_name = OpString "main"
  3643. %add_name = OpString "add"
  3644. %a_name = OpString "a"
  3645. %b_name = OpString "b"
  3646. OpDecorate %in_var_COLOR Location 0
  3647. OpDecorate %out_var_SV_TARGET Location 0
  3648. %uint = OpTypeInt 32 0
  3649. %uint_32 = OpConstant %uint 32
  3650. %float = OpTypeFloat 32
  3651. %float_1 = OpConstant %float 1
  3652. %float_2 = OpConstant %float 2
  3653. %v4float = OpTypeVector %float 4
  3654. %v4f1 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  3655. %v4f2 = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
  3656. %_ptr_Input_v4float = OpTypePointer Input %v4float
  3657. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3658. %_ptr_Function_v4float = OpTypePointer Function %v4float
  3659. %add_fn_type = OpTypeFunction %v4float %_ptr_Function_v4float %_ptr_Function_v4float
  3660. %void = OpTypeVoid
  3661. %void_fn_type = OpTypeFunction %void
  3662. %v4f_fn_type = OpTypeFunction %v4float
  3663. %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
  3664. %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
  3665. %null_expr = OpExtInst %void %ext DebugExpression
  3666. %src = OpExtInst %void %ext DebugSource %file_name
  3667. %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
  3668. %dbg_f = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
  3669. %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_f 4
  3670. %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
  3671. %add_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f %dbg_v4f
  3672. %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 5 1 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
  3673. %dbg_add = OpExtInst %void %ext DebugFunction %add_name %add_ty %src 1 1 %cu %add_name FlagIsProtected|FlagIsPrivate 1 %add
  3674. %dbg_a = OpExtInst %void %ext DebugLocalVariable %a_name %dbg_v4f %src 1 13 %dbg_add FlagIsLocal 0
  3675. %dbg_b = OpExtInst %void %ext DebugLocalVariable %b_name %dbg_v4f %src 1 20 %dbg_add FlagIsLocal 1
  3676. %add_lb = OpExtInst %void %ext DebugLexicalBlock %src 1 23 %dbg_add
  3677. %main = OpFunction %void None %void_fn_type
  3678. %main_bb = OpLabel
  3679. %param_a = OpVariable %_ptr_Function_v4float Function
  3680. %param_b = OpVariable %_ptr_Function_v4float Function
  3681. %scope0 = OpExtInst %void %ext DebugScope %dbg_main
  3682. OpStore %param_a %v4f1
  3683. OpStore %param_b %v4f2
  3684. %result = OpFunctionCall %v4float %add %param_a %param_b
  3685. OpStore %out_var_SV_TARGET %result
  3686. OpReturn
  3687. OpFunctionEnd
  3688. %add = OpFunction %v4float None %add_fn_type
  3689. %scope1 = OpExtInst %void %ext DebugScope %dbg_add
  3690. %a = OpFunctionParameter %_ptr_Function_v4float
  3691. %b = OpFunctionParameter %_ptr_Function_v4float
  3692. %decl0 = OpExtInst %void %ext DebugDeclare %dbg_a %a %null_expr
  3693. %decl1 = OpExtInst %void %ext DebugDeclare %dbg_b %b %null_expr
  3694. %add_bb = OpLabel
  3695. %scope2 = OpExtInst %void %ext DebugScope %add_lb
  3696. %a_val = OpLoad %v4float %a
  3697. %b_val = OpLoad %v4float %b
  3698. %res = OpFAdd %v4float %a_val %b_val
  3699. OpReturnValue %res
  3700. OpFunctionEnd
  3701. )";
  3702. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  3703. }
  3704. TEST_F(InlineTest, DebugDeclareForCalleeLocalVar) {
  3705. // Check that InlinePass correctly generates DebugDeclare instructions
  3706. // for callee function's local variables and maps them to corresponding
  3707. // local variables of caller function.
  3708. const std::string text = R"(
  3709. ; CHECK: [[add:%\d+]] = OpString "add"
  3710. ; CHECK: [[foo:%\d+]] = OpString "foo"
  3711. ; CHECK: [[dbg_add:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction [[add]]
  3712. ; CHECK: [[dbg_foo:%\d+]] = OpExtInst %void [[ext]] DebugLocalVariable [[foo]] {{%\d+}} {{%\d+}} 2 2 [[dbg_add]]
  3713. ; CHECK: [[inlinedat:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 5
  3714. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_add]] [[inlinedat]]
  3715. ; CHECK: [[new_foo:%\d+]] = OpVariable %_ptr_Function_v4float Function
  3716. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_add]] [[inlinedat]]
  3717. ; CHECK: [[a_val:%\d+]] = OpLoad %v4float
  3718. ; CHECK: [[b_val:%\d+]] = OpLoad %v4float
  3719. ; CHECK: [[res:%\d+]] = OpFAdd %v4float [[a_val]] [[b_val]]
  3720. ; CHECK: OpStore [[new_foo]] [[res]]
  3721. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugDeclare [[dbg_foo]] [[new_foo]]
  3722. OpCapability Shader
  3723. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  3724. OpMemoryModel Logical GLSL450
  3725. OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_TARGET
  3726. OpExecutionMode %main OriginUpperLeft
  3727. %file_name = OpString "ps.hlsl"
  3728. OpSource HLSL 600 %file_name
  3729. %float_name = OpString "float"
  3730. %main_name = OpString "main"
  3731. %add_name = OpString "add"
  3732. %foo_name = OpString "foo"
  3733. OpDecorate %in_var_COLOR Location 0
  3734. OpDecorate %out_var_SV_TARGET Location 0
  3735. %uint = OpTypeInt 32 0
  3736. %uint_32 = OpConstant %uint 32
  3737. %float = OpTypeFloat 32
  3738. %float_1 = OpConstant %float 1
  3739. %float_2 = OpConstant %float 2
  3740. %v4float = OpTypeVector %float 4
  3741. %v4f1 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  3742. %v4f2 = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
  3743. %_ptr_Input_v4float = OpTypePointer Input %v4float
  3744. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3745. %_ptr_Function_v4float = OpTypePointer Function %v4float
  3746. %add_fn_type = OpTypeFunction %v4float %_ptr_Function_v4float %_ptr_Function_v4float
  3747. %void = OpTypeVoid
  3748. %void_fn_type = OpTypeFunction %void
  3749. %v4f_fn_type = OpTypeFunction %v4float
  3750. %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
  3751. %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
  3752. %null_expr = OpExtInst %void %ext DebugExpression
  3753. %src = OpExtInst %void %ext DebugSource %file_name
  3754. %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
  3755. %dbg_f = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
  3756. %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_f 4
  3757. %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
  3758. %add_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f %dbg_v4f
  3759. %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 5 1 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
  3760. %dbg_add = OpExtInst %void %ext DebugFunction %add_name %add_ty %src 1 1 %cu %add_name FlagIsProtected|FlagIsPrivate 1 %add
  3761. %dbg_foo = OpExtInst %void %ext DebugLocalVariable %foo_name %dbg_v4f %src 2 2 %dbg_add FlagIsLocal
  3762. %main = OpFunction %void None %void_fn_type
  3763. %main_bb = OpLabel
  3764. %param_a = OpVariable %_ptr_Function_v4float Function
  3765. %param_b = OpVariable %_ptr_Function_v4float Function
  3766. %scope0 = OpExtInst %void %ext DebugScope %dbg_main
  3767. OpStore %param_a %v4f1
  3768. OpStore %param_b %v4f2
  3769. %result = OpFunctionCall %v4float %add %param_a %param_b
  3770. OpStore %out_var_SV_TARGET %result
  3771. OpReturn
  3772. OpFunctionEnd
  3773. %add = OpFunction %v4float None %add_fn_type
  3774. %scope1 = OpExtInst %void %ext DebugScope %dbg_add
  3775. %a = OpFunctionParameter %_ptr_Function_v4float
  3776. %b = OpFunctionParameter %_ptr_Function_v4float
  3777. %add_bb = OpLabel
  3778. %foo = OpVariable %_ptr_Function_v4float Function
  3779. %a_val = OpLoad %v4float %a
  3780. %b_val = OpLoad %v4float %b
  3781. %res = OpFAdd %v4float %a_val %b_val
  3782. OpStore %foo %res
  3783. %decl = OpExtInst %void %ext DebugDeclare %dbg_foo %foo %null_expr
  3784. %foo_val = OpLoad %v4float %foo
  3785. OpReturnValue %foo_val
  3786. OpFunctionEnd
  3787. )";
  3788. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  3789. }
  3790. TEST_F(InlineTest, DebugDeclareMultiple) {
  3791. // Check that InlinePass correctly generates DebugDeclare instructions
  3792. // for callee function's parameters and maps them to corresponding
  3793. // local variables of caller function.
  3794. const std::string text = R"(
  3795. ; CHECK: [[add:%\d+]] = OpString "add"
  3796. ; CHECK: [[a:%\d+]] = OpString "a"
  3797. ; CHECK: [[b:%\d+]] = OpString "b"
  3798. ; CHECK: [[dbg_add:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction [[add]]
  3799. ; CHECK: [[dbg_a:%\d+]] = OpExtInst %void [[ext]] DebugLocalVariable [[a]]
  3800. ; CHECK: [[dbg_b:%\d+]] = OpExtInst %void [[ext]] DebugLocalVariable [[b]]
  3801. ; CHECK: OpFunction
  3802. ; CHECK-NOT: OpFunctionEnd
  3803. ; CHECK: OpStore [[param_a:%\d+]]
  3804. ; CHECK: OpStore [[param_b:%\d+]]
  3805. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_add]]
  3806. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugDeclare [[dbg_a]] [[param_a]]
  3807. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugDeclare [[dbg_b]] [[param_b]]
  3808. ; CHECK: [[a_val:%\d+]] = OpLoad %v4float [[param_a]]
  3809. ; CHECK: OpStore [[foo:%\d+]] [[a_val]]
  3810. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugValue [[dbg_a]] [[foo]]
  3811. OpCapability Shader
  3812. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  3813. OpMemoryModel Logical GLSL450
  3814. OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_TARGET
  3815. OpExecutionMode %main OriginUpperLeft
  3816. %file_name = OpString "ps.hlsl"
  3817. OpSource HLSL 600 %file_name
  3818. %float_name = OpString "float"
  3819. %main_name = OpString "main"
  3820. %add_name = OpString "add"
  3821. %a_name = OpString "a"
  3822. %b_name = OpString "b"
  3823. OpDecorate %in_var_COLOR Location 0
  3824. OpDecorate %out_var_SV_TARGET Location 0
  3825. %uint = OpTypeInt 32 0
  3826. %uint_32 = OpConstant %uint 32
  3827. %float = OpTypeFloat 32
  3828. %float_1 = OpConstant %float 1
  3829. %float_2 = OpConstant %float 2
  3830. %v4float = OpTypeVector %float 4
  3831. %v4f1 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  3832. %v4f2 = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
  3833. %_ptr_Input_v4float = OpTypePointer Input %v4float
  3834. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3835. %_ptr_Function_v4float = OpTypePointer Function %v4float
  3836. %add_fn_type = OpTypeFunction %v4float %_ptr_Function_v4float %_ptr_Function_v4float
  3837. %void = OpTypeVoid
  3838. %void_fn_type = OpTypeFunction %void
  3839. %v4f_fn_type = OpTypeFunction %v4float
  3840. %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
  3841. %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
  3842. %null_expr = OpExtInst %void %ext DebugExpression
  3843. %src = OpExtInst %void %ext DebugSource %file_name
  3844. %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
  3845. %dbg_f = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
  3846. %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_f 4
  3847. %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
  3848. %add_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f %dbg_v4f
  3849. %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 5 1 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
  3850. %dbg_add = OpExtInst %void %ext DebugFunction %add_name %add_ty %src 1 1 %cu %add_name FlagIsProtected|FlagIsPrivate 1 %add
  3851. %dbg_a = OpExtInst %void %ext DebugLocalVariable %a_name %dbg_v4f %src 1 13 %dbg_add FlagIsLocal 0
  3852. %dbg_b = OpExtInst %void %ext DebugLocalVariable %b_name %dbg_v4f %src 1 20 %dbg_add FlagIsLocal 1
  3853. %main = OpFunction %void None %void_fn_type
  3854. %main_bb = OpLabel
  3855. %param_a = OpVariable %_ptr_Function_v4float Function
  3856. %param_b = OpVariable %_ptr_Function_v4float Function
  3857. %scope0 = OpExtInst %void %ext DebugScope %dbg_main
  3858. OpStore %param_a %v4f1
  3859. OpStore %param_b %v4f2
  3860. %result = OpFunctionCall %v4float %add %param_a %param_b
  3861. OpStore %out_var_SV_TARGET %result
  3862. OpReturn
  3863. OpFunctionEnd
  3864. %add = OpFunction %v4float None %add_fn_type
  3865. %scope1 = OpExtInst %void %ext DebugScope %dbg_add
  3866. %a = OpFunctionParameter %_ptr_Function_v4float
  3867. %b = OpFunctionParameter %_ptr_Function_v4float
  3868. %decl0 = OpExtInst %void %ext DebugDeclare %dbg_a %a %null_expr
  3869. %add_bb = OpLabel
  3870. %decl1 = OpExtInst %void %ext DebugDeclare %dbg_b %b %null_expr
  3871. %foo = OpVariable %_ptr_Function_v4float Function
  3872. %a_val = OpLoad %v4float %a
  3873. OpStore %foo %a_val
  3874. %dbg_val = OpExtInst %void %ext DebugValue %dbg_a %foo %null_expr
  3875. %b_val = OpLoad %v4float %b
  3876. %res = OpFAdd %v4float %a_val %b_val
  3877. OpReturnValue %res
  3878. OpFunctionEnd
  3879. )";
  3880. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  3881. }
  3882. TEST_F(InlineTest, DebugValueForFunctionCallReturn) {
  3883. // Check that InlinePass correctly generates DebugValue instruction
  3884. // for function call's return value and maps it to a corresponding
  3885. // value in the caller function.
  3886. const std::string text = R"(
  3887. ; CHECK: [[main:%\d+]] = OpString "main"
  3888. ; CHECK: [[add:%\d+]] = OpString "add"
  3889. ; CHECK: [[result:%\d+]] = OpString "result"
  3890. ; CHECK: [[dbg_main:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction [[main]]
  3891. ; CHECK: [[dbg_add:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction [[add]]
  3892. ; CHECK: [[dbg_result:%\d+]] = OpExtInst %void [[ext]] DebugLocalVariable [[result]] {{%\d+}} {{%\d+}} 6 2 [[dbg_main]]
  3893. ; CHECK: [[inlinedat:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 5
  3894. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_add]] [[inlinedat]]
  3895. ; CHECK: [[a_val:%\d+]] = OpLoad %v4float
  3896. ; CHECK: [[b_val:%\d+]] = OpLoad %v4float
  3897. ; CHECK: [[res:%\d+]] = OpFAdd %v4float [[a_val]] [[b_val]]
  3898. ; CHECK: OpStore [[new_result:%\d+]] [[res]]
  3899. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_main]]
  3900. ; CHECK: [[result_val:%\d+]] = OpLoad %v4float [[new_result]]
  3901. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugValue [[dbg_result]] [[result_val]]
  3902. OpCapability Shader
  3903. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  3904. OpMemoryModel Logical GLSL450
  3905. OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_TARGET
  3906. OpExecutionMode %main OriginUpperLeft
  3907. %file_name = OpString "ps.hlsl"
  3908. OpSource HLSL 600 %file_name
  3909. %float_name = OpString "float"
  3910. %main_name = OpString "main"
  3911. %add_name = OpString "add"
  3912. %result_name = OpString "result"
  3913. OpDecorate %in_var_COLOR Location 0
  3914. OpDecorate %out_var_SV_TARGET Location 0
  3915. %uint = OpTypeInt 32 0
  3916. %uint_32 = OpConstant %uint 32
  3917. %float = OpTypeFloat 32
  3918. %float_1 = OpConstant %float 1
  3919. %float_2 = OpConstant %float 2
  3920. %v4float = OpTypeVector %float 4
  3921. %v4f1 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  3922. %v4f2 = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
  3923. %_ptr_Input_v4float = OpTypePointer Input %v4float
  3924. %_ptr_Output_v4float = OpTypePointer Output %v4float
  3925. %_ptr_Function_v4float = OpTypePointer Function %v4float
  3926. %add_fn_type = OpTypeFunction %v4float %_ptr_Function_v4float %_ptr_Function_v4float
  3927. %void = OpTypeVoid
  3928. %void_fn_type = OpTypeFunction %void
  3929. %v4f_fn_type = OpTypeFunction %v4float
  3930. %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
  3931. %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
  3932. %null_expr = OpExtInst %void %ext DebugExpression
  3933. %src = OpExtInst %void %ext DebugSource %file_name
  3934. %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
  3935. %dbg_f = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
  3936. %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_f 4
  3937. %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
  3938. %add_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f %dbg_v4f
  3939. %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 5 1 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
  3940. %dbg_add = OpExtInst %void %ext DebugFunction %add_name %add_ty %src 1 1 %cu %add_name FlagIsProtected|FlagIsPrivate 1 %add
  3941. %dbg_result = OpExtInst %void %ext DebugLocalVariable %result_name %dbg_v4f %src 6 2 %dbg_main FlagIsLocal
  3942. %main = OpFunction %void None %void_fn_type
  3943. %main_bb = OpLabel
  3944. %param_a = OpVariable %_ptr_Function_v4float Function
  3945. %param_b = OpVariable %_ptr_Function_v4float Function
  3946. %scope0 = OpExtInst %void %ext DebugScope %dbg_main
  3947. OpStore %param_a %v4f1
  3948. OpStore %param_b %v4f2
  3949. %result = OpFunctionCall %v4float %add %param_a %param_b
  3950. %value = OpExtInst %void %ext DebugValue %dbg_result %result %null_expr
  3951. OpStore %out_var_SV_TARGET %result
  3952. OpReturn
  3953. OpFunctionEnd
  3954. %add = OpFunction %v4float None %add_fn_type
  3955. %scope1 = OpExtInst %void %ext DebugScope %dbg_add
  3956. %a = OpFunctionParameter %_ptr_Function_v4float
  3957. %b = OpFunctionParameter %_ptr_Function_v4float
  3958. %add_bb = OpLabel
  3959. %a_val = OpLoad %v4float %a
  3960. %b_val = OpLoad %v4float %b
  3961. %res = OpFAdd %v4float %a_val %b_val
  3962. OpReturnValue %res
  3963. OpFunctionEnd
  3964. )";
  3965. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  3966. }
  3967. TEST_F(InlineTest, NestedWithAnExistingDebugInlinedAt) {
  3968. // When a DebugScope instruction in a callee function already has a
  3969. // DebugInlinedAt information, we have to create a recursive
  3970. // DebugInlinedAt chain. See inlined_to_zoo and inlined_to_bar in
  3971. // the following code.
  3972. const std::string text = R"(
  3973. ; CHECK: [[main:%\d+]] = OpString "main"
  3974. ; CHECK: [[foo:%\d+]] = OpString "foo"
  3975. ; CHECK: [[bar:%\d+]] = OpString "bar"
  3976. ; CHECK: [[zoo:%\d+]] = OpString "zoo"
  3977. ; CHECK: [[v4f1:%\d+]] = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  3978. ; CHECK: [[v4f2:%\d+]] = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
  3979. ; CHECK: [[v4f3:%\d+]] = OpConstantComposite %v4float %float_3 %float_3 %float_3 %float_3
  3980. ; CHECK: [[dbg_main:%\d+]] = OpExtInst %void [[ext:%\d+]] DebugFunction [[main]]
  3981. ; CHECK: [[dbg_foo:%\d+]] = OpExtInst %void [[ext]] DebugFunction [[foo]]
  3982. ; CHECK: [[dbg_bar:%\d+]] = OpExtInst %void [[ext]] DebugFunction [[bar]]
  3983. ; CHECK: [[dbg_zoo:%\d+]] = OpExtInst %void [[ext]] DebugFunction [[zoo]]
  3984. ; CHECK: [[inlined_to_main:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 10 [[dbg_main]]
  3985. ; CHECK: [[inlined_to_zoo:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 7 [[dbg_zoo]] [[inlined_to_main]]
  3986. ; CHECK: [[inlined_to_main:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 10 [[dbg_main]]
  3987. ; CHECK: [[inlined_to_bar:%\d+]] = OpExtInst %void [[ext]] DebugInlinedAt 4 [[dbg_bar]] [[inlined_to_zoo]]
  3988. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_foo]] [[inlined_to_bar]]
  3989. ; CHECK: OpStore [[foo_ret:%\d+]] [[v4f1]]
  3990. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_bar]] [[inlined_to_zoo]]
  3991. ; CHECK: [[foo_ret_val:%\d+]] = OpLoad %v4float [[foo_ret]]
  3992. ; CHECK: [[bar_ret:%\d+]] = OpFAdd %v4float [[foo_ret_val]] [[v4f2]]
  3993. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_zoo]] [[inlined_to_main]]
  3994. ; CHECK: [[zoo_result:%\d+]] = OpFAdd %v4float [[bar_ret]] [[v4f3]]
  3995. ; CHECK: OpStore [[zoo_ret:%\d+]] [[zoo_result]]
  3996. ; CHECK: {{%\d+}} = OpExtInst %void [[ext]] DebugScope [[dbg_main]]
  3997. ; CHECK: [[zoo_ret_val:%\d+]] = OpLoad %v4float [[zoo_ret]]
  3998. ; CHECK: {{%\d+}} = OpFAdd %v4float [[zoo_ret_val]] {{%\d+}}
  3999. OpCapability Shader
  4000. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  4001. OpMemoryModel Logical GLSL450
  4002. OpEntryPoint Fragment %main "main" %in_var_COLOR %out_var_SV_TARGET
  4003. OpExecutionMode %main OriginUpperLeft
  4004. %file_name = OpString "ps.hlsl"
  4005. OpSource HLSL 600 %file_name
  4006. %float_name = OpString "float"
  4007. %main_name = OpString "main"
  4008. %foo_name = OpString "foo"
  4009. %bar_name = OpString "bar"
  4010. %zoo_name = OpString "zoo"
  4011. OpDecorate %in_var_COLOR Location 0
  4012. OpDecorate %out_var_SV_TARGET Location 0
  4013. %uint = OpTypeInt 32 0
  4014. %uint_32 = OpConstant %uint 32
  4015. %float = OpTypeFloat 32
  4016. %float_1 = OpConstant %float 1
  4017. %float_2 = OpConstant %float 2
  4018. %float_3 = OpConstant %float 3
  4019. %v4float = OpTypeVector %float 4
  4020. %v4f1 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  4021. %v4f2 = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
  4022. %v4f3 = OpConstantComposite %v4float %float_3 %float_3 %float_3 %float_3
  4023. %_ptr_Input_v4float = OpTypePointer Input %v4float
  4024. %_ptr_Output_v4float = OpTypePointer Output %v4float
  4025. %void = OpTypeVoid
  4026. %void_fn_type = OpTypeFunction %void
  4027. %v4f_fn_type = OpTypeFunction %v4float
  4028. %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
  4029. %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
  4030. %src = OpExtInst %void %ext DebugSource %file_name
  4031. %cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
  4032. %dbg_f = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
  4033. %dbg_v4f = OpExtInst %void %ext DebugTypeVector %dbg_f 4
  4034. %main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f %dbg_v4f
  4035. %foo_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_v4f
  4036. %dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 10 1 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
  4037. %dbg_foo = OpExtInst %void %ext DebugFunction %foo_name %foo_ty %src 1 1 %cu %foo_name FlagIsProtected|FlagIsPrivate 1 %foo
  4038. %dbg_bar = OpExtInst %void %ext DebugFunction %bar_name %foo_ty %src 4 1 %cu %bar_name FlagIsProtected|FlagIsPrivate 4 %bar
  4039. %dbg_zoo = OpExtInst %void %ext DebugFunction %zoo_name %foo_ty %src 7 1 %cu %zoo_name FlagIsProtected|FlagIsPrivate 7 %zoo
  4040. %inlined_to_zoo = OpExtInst %void %ext DebugInlinedAt 7 %dbg_zoo
  4041. %main = OpFunction %void None %void_fn_type
  4042. %main_bb = OpLabel
  4043. %scope0 = OpExtInst %void %ext DebugScope %dbg_main
  4044. %zoo_val = OpFunctionCall %v4float %zoo
  4045. %color = OpLoad %v4float %in_var_COLOR
  4046. %result = OpFAdd %v4float %zoo_val %color
  4047. OpStore %out_var_SV_TARGET %result
  4048. OpReturn
  4049. OpFunctionEnd
  4050. %foo = OpFunction %v4float None %v4f_fn_type
  4051. %scope1 = OpExtInst %void %ext DebugScope %dbg_foo
  4052. %foo_bb = OpLabel
  4053. OpReturnValue %v4f1
  4054. OpFunctionEnd
  4055. %zoo = OpFunction %v4float None %v4f_fn_type
  4056. %scope3 = OpExtInst %void %ext DebugScope %dbg_zoo
  4057. %zoo_bb = OpLabel
  4058. %scope2 = OpExtInst %void %ext DebugScope %dbg_bar %inlined_to_zoo
  4059. %foo_val = OpFunctionCall %v4float %foo
  4060. %bar_val = OpFAdd %v4float %foo_val %v4f2
  4061. %scope4 = OpExtInst %void %ext DebugScope %dbg_zoo
  4062. %zoo_ret = OpFAdd %v4float %bar_val %v4f3
  4063. OpReturnValue %zoo_ret
  4064. OpFunctionEnd
  4065. %bar = OpFunction %v4float None %v4f_fn_type
  4066. %scope5 = OpExtInst %void %ext DebugScope %dbg_bar
  4067. %bar_bb = OpLabel
  4068. %foo_val0 = OpFunctionCall %v4float %foo
  4069. %bar_ret = OpFAdd %v4float %foo_val0 %v4f2
  4070. OpReturnValue %bar_ret
  4071. OpFunctionEnd
  4072. )";
  4073. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  4074. }
  4075. TEST_F(InlineTest, CreateConstantForInlinedAt) {
  4076. // This shader causes CreateDebugInlinedAt to generate a constant.
  4077. // Using the Constant manager would attempt to build the invalidated
  4078. // DefUse manager during inlining which could cause an assert because
  4079. // the function is in an inconsistent state. This test verifies that
  4080. // CreateDebugInlinedAt detects that the DefUse manager is disabled
  4081. // and creates a duplicate constant safely without the Constant manager.
  4082. //
  4083. // int function1() {
  4084. // return 1;
  4085. // }
  4086. //
  4087. // void main() {
  4088. // function1();
  4089. // }
  4090. const std::string text = R"(OpCapability Shader
  4091. ; CHECK: %uint_7 = OpConstant %uint 7
  4092. ; CHECK: %uint_7_0 = OpConstant %uint 7
  4093. ; CHECK: OpExtInst %void %1 DebugInlinedAt %uint_7_0
  4094. OpExtension "SPV_KHR_non_semantic_info"
  4095. %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
  4096. OpMemoryModel Logical GLSL450
  4097. OpEntryPoint Fragment %main "main"
  4098. OpExecutionMode %main OriginUpperLeft
  4099. %3 = OpString "parent3.hlsl"
  4100. %8 = OpString "int"
  4101. %19 = OpString "function1"
  4102. %20 = OpString ""
  4103. %26 = OpString "main"
  4104. OpName %main "main"
  4105. OpName %src_main "src.main"
  4106. OpName %bb_entry "bb.entry"
  4107. OpName %function1 "function1"
  4108. OpName %bb_entry_0 "bb.entry"
  4109. %int = OpTypeInt 32 1
  4110. %int_1 = OpConstant %int 1
  4111. %uint = OpTypeInt 32 0
  4112. %uint_32 = OpConstant %uint 32
  4113. %void = OpTypeVoid
  4114. %uint_4 = OpConstant %uint 4
  4115. %uint_0 = OpConstant %uint 0
  4116. %uint_3 = OpConstant %uint 3
  4117. %uint_1 = OpConstant %uint 1
  4118. %uint_5 = OpConstant %uint 5
  4119. %uint_2 = OpConstant %uint 2
  4120. %uint_17 = OpConstant %uint 17
  4121. %uint_6 = OpConstant %uint 6
  4122. %uint_13 = OpConstant %uint 13
  4123. %uint_7 = OpConstant %uint 7
  4124. %31 = OpTypeFunction %void
  4125. %42 = OpTypeFunction %int
  4126. %10 = OpExtInst %void %1 DebugTypeBasic %8 %uint_32 %uint_4 %uint_0
  4127. %13 = OpExtInst %void %1 DebugTypeFunction %uint_3 %10
  4128. %15 = OpExtInst %void %1 DebugSource %3
  4129. %16 = OpExtInst %void %1 DebugCompilationUnit %uint_1 %uint_4 %15 %uint_5
  4130. %21 = OpExtInst %void %1 DebugFunction %19 %13 %15 %uint_2 %uint_1 %16 %20 %uint_3 %uint_2
  4131. %23 = OpExtInst %void %1 DebugLexicalBlock %15 %uint_2 %uint_17 %21
  4132. %25 = OpExtInst %void %1 DebugTypeFunction %uint_3 %void
  4133. %27 = OpExtInst %void %1 DebugFunction %26 %25 %15 %uint_6 %uint_1 %16 %20 %uint_3 %uint_6
  4134. %29 = OpExtInst %void %1 DebugLexicalBlock %15 %uint_6 %uint_13 %27
  4135. %main = OpFunction %void None %31
  4136. %32 = OpLabel
  4137. %33 = OpFunctionCall %void %src_main
  4138. OpLine %3 8 1
  4139. OpReturn
  4140. OpFunctionEnd
  4141. OpLine %3 6 1
  4142. %src_main = OpFunction %void None %31
  4143. OpNoLine
  4144. %bb_entry = OpLabel
  4145. %47 = OpExtInst %void %1 DebugScope %27
  4146. %37 = OpExtInst %void %1 DebugFunctionDefinition %27 %src_main
  4147. %48 = OpExtInst %void %1 DebugScope %29
  4148. OpLine %3 7 3
  4149. %39 = OpFunctionCall %int %function1
  4150. %49 = OpExtInst %void %1 DebugScope %27
  4151. OpLine %3 8 1
  4152. OpReturn
  4153. %50 = OpExtInst %void %1 DebugNoScope
  4154. OpFunctionEnd
  4155. OpLine %3 2 1
  4156. %function1 = OpFunction %int None %42
  4157. OpNoLine
  4158. %bb_entry_0 = OpLabel
  4159. %51 = OpExtInst %void %1 DebugScope %21
  4160. %45 = OpExtInst %void %1 DebugFunctionDefinition %21 %function1
  4161. %52 = OpExtInst %void %1 DebugScope %23
  4162. OpLine %3 3 3
  4163. OpReturnValue %int_1
  4164. %53 = OpExtInst %void %1 DebugNoScope
  4165. OpFunctionEnd
  4166. )";
  4167. SetTargetEnv(SPV_ENV_VULKAN_1_2);
  4168. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  4169. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  4170. }
  4171. TEST_F(InlineTest, CreateDebugInlinedAtFromDebugLine) {
  4172. const std::string text = R"(OpCapability Shader
  4173. ; CHECK: OpExtInst %void %1 DebugInlinedAt %uint_6
  4174. OpExtension "SPV_KHR_non_semantic_info"
  4175. %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
  4176. OpMemoryModel Logical GLSL450
  4177. OpEntryPoint Fragment %main "main"
  4178. OpExecutionMode %main OriginUpperLeft
  4179. %3 = OpString "debuginlinedat.frag"
  4180. %8 = OpString "int"
  4181. %15 = OpString "int function1() {
  4182. return 1;
  4183. }
  4184. void main() {
  4185. function1();
  4186. }
  4187. "
  4188. %20 = OpString "function1"
  4189. %21 = OpString ""
  4190. %26 = OpString "main"
  4191. OpName %main "main"
  4192. OpName %src_main "src.main"
  4193. OpName %bb_entry "bb.entry"
  4194. OpName %function1 "function1"
  4195. OpName %bb_entry_0 "bb.entry"
  4196. %int = OpTypeInt 32 1
  4197. %int_1 = OpConstant %int 1
  4198. %uint = OpTypeInt 32 0
  4199. %uint_32 = OpConstant %uint 32
  4200. %void = OpTypeVoid
  4201. %uint_4 = OpConstant %uint 4
  4202. %uint_0 = OpConstant %uint 0
  4203. %uint_3 = OpConstant %uint 3
  4204. %uint_1 = OpConstant %uint 1
  4205. %uint_5 = OpConstant %uint 5
  4206. %uint_17 = OpConstant %uint 17
  4207. %uint_13 = OpConstant %uint 13
  4208. %30 = OpTypeFunction %void
  4209. %uint_7 = OpConstant %uint 7
  4210. %uint_6 = OpConstant %uint 6
  4211. %uint_2 = OpConstant %uint 2
  4212. %uint_12 = OpConstant %uint 12
  4213. %48 = OpTypeFunction %int
  4214. %uint_9 = OpConstant %uint 9
  4215. %10 = OpExtInst %void %1 DebugTypeBasic %8 %uint_32 %uint_4 %uint_0
  4216. %13 = OpExtInst %void %1 DebugTypeFunction %uint_3 %10
  4217. %16 = OpExtInst %void %1 DebugSource %3 %15
  4218. %17 = OpExtInst %void %1 DebugCompilationUnit %uint_1 %uint_4 %16 %uint_5
  4219. %22 = OpExtInst %void %1 DebugFunction %20 %13 %16 %uint_1 %uint_1 %17 %21 %uint_3 %uint_1
  4220. %23 = OpExtInst %void %1 DebugLexicalBlock %16 %uint_1 %uint_17 %22
  4221. %25 = OpExtInst %void %1 DebugTypeFunction %uint_3 %void
  4222. %27 = OpExtInst %void %1 DebugFunction %26 %25 %16 %uint_5 %uint_1 %17 %21 %uint_3 %uint_5
  4223. %28 = OpExtInst %void %1 DebugLexicalBlock %16 %uint_5 %uint_13 %27
  4224. %main = OpFunction %void None %30
  4225. %31 = OpLabel
  4226. %32 = OpFunctionCall %void %src_main
  4227. %34 = OpExtInst %void %1 DebugLine %16 %uint_7 %uint_7 %uint_1 %uint_1
  4228. OpReturn
  4229. OpFunctionEnd
  4230. %src_main = OpFunction %void None %30
  4231. %bb_entry = OpLabel
  4232. %37 = OpExtInst %void %1 DebugScope %27
  4233. %38 = OpExtInst %void %1 DebugFunctionDefinition %27 %src_main
  4234. %39 = OpExtInst %void %1 DebugScope %28
  4235. %40 = OpExtInst %void %1 DebugLine %16 %uint_6 %uint_6 %uint_2 %uint_12
  4236. %44 = OpFunctionCall %int %function1
  4237. %46 = OpExtInst %void %1 DebugScope %27
  4238. %47 = OpExtInst %void %1 DebugLine %16 %uint_7 %uint_7 %uint_1 %uint_1
  4239. OpReturn
  4240. OpFunctionEnd
  4241. %function1 = OpFunction %int None %48
  4242. %bb_entry_0 = OpLabel
  4243. %50 = OpExtInst %void %1 DebugScope %22
  4244. %51 = OpExtInst %void %1 DebugFunctionDefinition %22 %function1
  4245. %52 = OpExtInst %void %1 DebugScope %23
  4246. %53 = OpExtInst %void %1 DebugLine %16 %uint_2 %uint_2 %uint_2 %uint_9
  4247. OpReturnValue %int_1
  4248. OpFunctionEnd
  4249. )";
  4250. SetTargetEnv(SPV_ENV_VULKAN_1_2);
  4251. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  4252. }
  4253. TEST_F(InlineTest, DecorateReturnVariableWithAliasedPointer) {
  4254. const std::string text = R"(OpCapability Int64
  4255. OpCapability VariablePointers
  4256. OpCapability PhysicalStorageBufferAddresses
  4257. OpCapability Shader
  4258. OpExtension "SPV_KHR_storage_buffer_storage_class"
  4259. OpExtension "SPV_KHR_variable_pointers"
  4260. OpExtension "SPV_KHR_physical_storage_buffer"
  4261. OpMemoryModel PhysicalStorageBuffer64 GLSL450
  4262. OpEntryPoint GLCompute %1 "main"
  4263. OpExecutionMode %1 LocalSize 8 8 1
  4264. OpMemberDecorate %_struct_5 0 Offset 0
  4265. OpMemberDecorate %_struct_5 1 Offset 4
  4266. OpDecorate %6 Aliased
  4267. ; CHECK: OpDecorate %22 AliasedPointer
  4268. %void = OpTypeVoid
  4269. %8 = OpTypeFunction %void
  4270. %int = OpTypeInt 32 1
  4271. %int_0 = OpConstant %int 0
  4272. OpTypeForwardPointer %_ptr_PhysicalStorageBuffer__struct_5 PhysicalStorageBuffer
  4273. %_struct_3 = OpTypeStruct %int %_ptr_PhysicalStorageBuffer__struct_5
  4274. %_ptr_PhysicalStorageBuffer_int = OpTypePointer PhysicalStorageBuffer %int
  4275. %_struct_5 = OpTypeStruct %int %int
  4276. %11 = OpTypeFunction %_ptr_PhysicalStorageBuffer_int %_ptr_PhysicalStorageBuffer__struct_5
  4277. %_ptr_PhysicalStorageBuffer__struct_5 = OpTypePointer PhysicalStorageBuffer %_struct_5
  4278. %_ptr_Function__struct_3 = OpTypePointer Function %_struct_3
  4279. %1 = OpFunction %void None %8
  4280. %13 = OpLabel
  4281. %14 = OpVariable %_ptr_Function__struct_3 Function
  4282. %15 = OpLoad %_struct_3 %14
  4283. %16 = OpCompositeExtract %_ptr_PhysicalStorageBuffer__struct_5 %15 1
  4284. %17 = OpFunctionCall %_ptr_PhysicalStorageBuffer_int %18 %16
  4285. OpReturn
  4286. OpFunctionEnd
  4287. %18 = OpFunction %_ptr_PhysicalStorageBuffer_int None %11
  4288. %6 = OpFunctionParameter %_ptr_PhysicalStorageBuffer__struct_5
  4289. %19 = OpLabel
  4290. %20 = OpAccessChain %_ptr_PhysicalStorageBuffer_int %6 %int_0
  4291. OpReturnValue %20
  4292. OpFunctionEnd)";
  4293. SetTargetEnv(SPV_ENV_VULKAN_1_2);
  4294. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  4295. }
  4296. TEST_F(InlineTest, DebugDeclareWithAccessChain) {
  4297. const std::string text = R"(
  4298. ; CHECK: [[EmptyStruct:%[\w]+]] = OpTypeStruct %float
  4299. ; CHECK-DAG: [[Struct:%[\w]+]] = OpTypeStruct [[EmptyStruct]]
  4300. ; CHECK-DAG: [[PtrType:%[\w]+]] = OpTypePointer Function [[Struct]]
  4301. ; CHECK-DAG: [[EmptyPtrType:%[\w]+]] = OpTypePointer Function [[EmptyStruct]]
  4302. OpCapability Shader
  4303. OpExtension "SPV_KHR_non_semantic_info"
  4304. OpExtension "SPV_KHR_relaxed_extended_instruction"
  4305. %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
  4306. OpMemoryModel Logical GLSL450
  4307. OpEntryPoint GLCompute %2 "computeMain"
  4308. OpExecutionMode %2 LocalSize 1 1 1
  4309. %3 = OpString "s.hlsl"
  4310. %4 = OpString "float"
  4311. %5 = OpString "source"
  4312. %6 = OpString "a"
  4313. %7 = OpString "SomeStruct"
  4314. %8 = OpString "SomeStruct.getA"
  4315. %9 = OpString ""
  4316. %10 = OpString "this"
  4317. %int = OpTypeInt 32 1
  4318. %int_0 = OpConstant %int 0
  4319. %uint = OpTypeInt 32 0
  4320. %uint_0 = OpConstant %uint 0
  4321. %uint_32 = OpConstant %uint 32
  4322. %float = OpTypeFloat 32
  4323. %void = OpTypeVoid
  4324. %uint_3 = OpConstant %uint 3
  4325. %uint_1 = OpConstant %uint 1
  4326. %uint_4 = OpConstant %uint 4
  4327. %uint_5 = OpConstant %uint 5
  4328. %uint_11 = OpConstant %uint 11
  4329. %uint_8 = OpConstant %uint 8
  4330. %uint_288 = OpConstant %uint 288
  4331. %25 = OpTypeFunction %void
  4332. %_struct_26 = OpTypeStruct %float
  4333. %_struct_27 = OpTypeStruct %_struct_26
  4334. %_ptr_Function__struct_27 = OpTypePointer Function %_struct_27
  4335. %_ptr_Function__struct_26 = OpTypePointer Function %_struct_26
  4336. %_ptr_Function_float = OpTypePointer Function %float
  4337. %30 = OpTypeFunction %float %_ptr_Function__struct_26 %_ptr_Function_float
  4338. %31 = OpUndef %float
  4339. %32 = OpExtInst %void %1 DebugTypeBasic %4 %uint_32 %uint_3 %uint_0
  4340. %33 = OpExtInst %void %1 DebugSource %3 %5
  4341. %34 = OpExtInst %void %1 DebugCompilationUnit %uint_1 %uint_4 %33 %uint_5
  4342. %35 = OpExtInst %void %1 DebugTypeMember %6 %32 %33 %uint_3 %uint_11 %uint_0 %uint_32 %uint_3
  4343. %36 = OpExtInstWithForwardRefsKHR %void %1 DebugTypeComposite %7 %uint_1 %33 %uint_1 %uint_8 %34 %7 %uint_32 %uint_3 %35 %37
  4344. %38 = OpExtInst %void %1 DebugTypeFunction %uint_3 %32 %36
  4345. %37 = OpExtInst %void %1 DebugFunction %8 %38 %33 %uint_4 %uint_5 %36 %9 %uint_3 %uint_4
  4346. %39 = OpExtInst %void %1 DebugLocalVariable %10 %36 %33 %uint_4 %uint_5 %37 %uint_288 %uint_1
  4347. %52 = OpExtInst %void %1 DebugLocalVariable %10 %32 %33 %uint_4 %uint_5 %37 %uint_288 %uint_1
  4348. %40 = OpExtInst %void %1 DebugExpression
  4349. ; CHECK: OpFunction %void None
  4350. ; CHECK: [[Var:%[\w]+]] = OpVariable [[PtrType]] Function
  4351. ; CHECK: OpExtInst %void {{%[\w+]+}} DebugDeclare {{%[\w+]+}} [[Var]] {{%[\w+]+}} %int_0
  4352. ; CHECK: OpExtInst %void {{%[\w+]+}} DebugDeclare {{%[\w+]+}} [[Var]] {{%[\w+]+}} %int_0 %int_0
  4353. %2 = OpFunction %void None %25
  4354. %41 = OpLabel
  4355. %42 = OpVariable %_ptr_Function__struct_27 Function
  4356. %43 = OpAccessChain %_ptr_Function__struct_26 %42 %int_0
  4357. %49 = OpAccessChain %_ptr_Function_float %43 %int_0
  4358. %44 = OpFunctionCall %float %45 %43 %49
  4359. OpReturn
  4360. OpFunctionEnd
  4361. ; CHECK: OpFunction %float None
  4362. %45 = OpFunction %float None %30
  4363. %46 = OpFunctionParameter %_ptr_Function__struct_26
  4364. %50 = OpFunctionParameter %_ptr_Function_float
  4365. %47 = OpLabel
  4366. %48 = OpExtInst %void %1 DebugDeclare %39 %46 %40
  4367. %51 = OpExtInst %void %1 DebugDeclare %52 %50 %40
  4368. OpReturnValue %31
  4369. OpFunctionEnd
  4370. )";
  4371. SetTargetEnv(SPV_ENV_VULKAN_1_2);
  4372. SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
  4373. }
  4374. // TODO(greg-lunarg): Add tests to verify handling of these cases:
  4375. //
  4376. // Empty modules
  4377. // Modules without function definitions
  4378. // Modules in which all functions do not call other functions
  4379. // Caller and callee both accessing the same global variable
  4380. // Functions with OpLine & OpNoLine
  4381. // Others?
  4382. // TODO(dneto): Test suggestions from code review
  4383. // https://github.com/KhronosGroup/SPIRV-Tools/pull/534
  4384. //
  4385. // Callee function returns a value generated outside the callee,
  4386. // e.g. a constant value. This might exercise some logic not yet
  4387. // exercised by the current tests: the false branch in the "if"
  4388. // inside the spv::Op::OpReturnValue case in InlinePass::GenInlineCode?
  4389. // SampledImage before function call, but callee is only single block.
  4390. // Then the SampledImage instruction is not cloned. Documents existing
  4391. // behaviour.
  4392. // SampledImage after function call. It is not cloned or changed.
  4393. } // namespace
  4394. } // namespace opt
  4395. } // namespace spvtools