inline_test.cpp 154 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448
  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 %5 %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 %5 %uint_2 %uint_2 %uint_10 %uint_10
  3610. %39 = OpLoad %v4float %color
  3611. %143 = OpExtInst %void %1 DebugLine %5 %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. // TODO(greg-lunarg): Add tests to verify handling of these cases:
  4254. //
  4255. // Empty modules
  4256. // Modules without function definitions
  4257. // Modules in which all functions do not call other functions
  4258. // Caller and callee both accessing the same global variable
  4259. // Functions with OpLine & OpNoLine
  4260. // Others?
  4261. // TODO(dneto): Test suggestions from code review
  4262. // https://github.com/KhronosGroup/SPIRV-Tools/pull/534
  4263. //
  4264. // Callee function returns a value generated outside the callee,
  4265. // e.g. a constant value. This might exercise some logic not yet
  4266. // exercised by the current tests: the false branch in the "if"
  4267. // inside the spv::Op::OpReturnValue case in InlinePass::GenInlineCode?
  4268. // SampledImage before function call, but callee is only single block.
  4269. // Then the SampledImage instruction is not cloned. Documents existing
  4270. // behaviour.
  4271. // SampledImage after function call. It is not cloned or changed.
  4272. } // namespace
  4273. } // namespace opt
  4274. } // namespace spvtools