inline_test.cpp 99 KB

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