local_single_store_elim_test.cpp 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862
  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 <string>
  16. #include "test/opt/pass_fixture.h"
  17. #include "test/opt/pass_utils.h"
  18. namespace spvtools {
  19. namespace opt {
  20. namespace {
  21. using LocalSingleStoreElimTest = PassTest<::testing::Test>;
  22. TEST_F(LocalSingleStoreElimTest, DoSomethingWithExtensions) {
  23. const std::string capabilities_and_extensions =
  24. R"(OpCapability Shader
  25. OpExtension "SPV_EXT_fragment_shader_interlock"
  26. OpExtension "SPV_NV_compute_shader_derivatives"
  27. OpExtension "SPV_KHR_ray_query"
  28. OpExtension "SPV_NV_shader_subgroup_partitioned"
  29. OpExtension "SPV_KHR_ray_tracing"
  30. OpExtension "SPV_EXT_descriptor_indexing"
  31. )";
  32. const std::string before = capabilities_and_extensions +
  33. R"(%1 = OpExtInstImport "GLSL.std.450"
  34. OpMemoryModel Logical GLSL450
  35. OpEntryPoint Fragment %2 "main"
  36. OpExecutionMode %2 OriginUpperLeft
  37. OpSource GLSL 140
  38. %void = OpTypeVoid
  39. %4 = OpTypeFunction %void
  40. %float = OpTypeFloat 32
  41. %_ptr_Function_float = OpTypePointer Function %float
  42. %float_0 = OpConstant %float 0
  43. %2 = OpFunction %void None %4
  44. %8 = OpLabel
  45. %9 = OpVariable %_ptr_Function_float Function
  46. OpStore %9 %float_0
  47. %10 = OpLoad %float %9
  48. OpReturn
  49. OpFunctionEnd
  50. )";
  51. const std::string after = capabilities_and_extensions +
  52. R"(%1 = OpExtInstImport "GLSL.std.450"
  53. OpMemoryModel Logical GLSL450
  54. OpEntryPoint Fragment %2 "main"
  55. OpExecutionMode %2 OriginUpperLeft
  56. OpSource GLSL 140
  57. %void = OpTypeVoid
  58. %4 = OpTypeFunction %void
  59. %float = OpTypeFloat 32
  60. %_ptr_Function_float = OpTypePointer Function %float
  61. %float_0 = OpConstant %float 0
  62. %2 = OpFunction %void None %4
  63. %8 = OpLabel
  64. %9 = OpVariable %_ptr_Function_float Function
  65. OpStore %9 %float_0
  66. OpReturn
  67. OpFunctionEnd
  68. )";
  69. SinglePassRunAndCheck<LocalSingleStoreElimPass>(before, after, true, true);
  70. }
  71. TEST_F(LocalSingleStoreElimTest, PositiveAndNegative) {
  72. // Single store to v is optimized. Multiple store to
  73. // f is not optimized.
  74. //
  75. // #version 140
  76. //
  77. // in vec4 BaseColor;
  78. // in float fi;
  79. //
  80. // void main()
  81. // {
  82. // vec4 v = BaseColor;
  83. // float f = fi;
  84. // if (f < 0)
  85. // f = 0.0;
  86. // gl_FragColor = v + f;
  87. // }
  88. const std::string predefs =
  89. R"(OpCapability Shader
  90. %1 = OpExtInstImport "GLSL.std.450"
  91. OpMemoryModel Logical GLSL450
  92. OpEntryPoint Fragment %main "main" %BaseColor %fi %gl_FragColor
  93. OpExecutionMode %main OriginUpperLeft
  94. OpSource GLSL 140
  95. OpName %main "main"
  96. OpName %v "v"
  97. OpName %BaseColor "BaseColor"
  98. OpName %f "f"
  99. OpName %fi "fi"
  100. OpName %gl_FragColor "gl_FragColor"
  101. %void = OpTypeVoid
  102. %9 = OpTypeFunction %void
  103. %float = OpTypeFloat 32
  104. %v4float = OpTypeVector %float 4
  105. %_ptr_Function_v4float = OpTypePointer Function %v4float
  106. %_ptr_Input_v4float = OpTypePointer Input %v4float
  107. %BaseColor = OpVariable %_ptr_Input_v4float Input
  108. %_ptr_Function_float = OpTypePointer Function %float
  109. %_ptr_Input_float = OpTypePointer Input %float
  110. %fi = OpVariable %_ptr_Input_float Input
  111. %float_0 = OpConstant %float 0
  112. %bool = OpTypeBool
  113. %_ptr_Output_v4float = OpTypePointer Output %v4float
  114. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  115. )";
  116. const std::string before =
  117. R"(%main = OpFunction %void None %9
  118. %19 = OpLabel
  119. %v = OpVariable %_ptr_Function_v4float Function
  120. %f = OpVariable %_ptr_Function_float Function
  121. %20 = OpLoad %v4float %BaseColor
  122. OpStore %v %20
  123. %21 = OpLoad %float %fi
  124. OpStore %f %21
  125. %22 = OpLoad %float %f
  126. %23 = OpFOrdLessThan %bool %22 %float_0
  127. OpSelectionMerge %24 None
  128. OpBranchConditional %23 %25 %24
  129. %25 = OpLabel
  130. OpStore %f %float_0
  131. OpBranch %24
  132. %24 = OpLabel
  133. %26 = OpLoad %v4float %v
  134. %27 = OpLoad %float %f
  135. %28 = OpCompositeConstruct %v4float %27 %27 %27 %27
  136. %29 = OpFAdd %v4float %26 %28
  137. OpStore %gl_FragColor %29
  138. OpReturn
  139. OpFunctionEnd
  140. )";
  141. const std::string after =
  142. R"(%main = OpFunction %void None %9
  143. %19 = OpLabel
  144. %v = OpVariable %_ptr_Function_v4float Function
  145. %f = OpVariable %_ptr_Function_float Function
  146. %20 = OpLoad %v4float %BaseColor
  147. OpStore %v %20
  148. %21 = OpLoad %float %fi
  149. OpStore %f %21
  150. %22 = OpLoad %float %f
  151. %23 = OpFOrdLessThan %bool %22 %float_0
  152. OpSelectionMerge %24 None
  153. OpBranchConditional %23 %25 %24
  154. %25 = OpLabel
  155. OpStore %f %float_0
  156. OpBranch %24
  157. %24 = OpLabel
  158. %27 = OpLoad %float %f
  159. %28 = OpCompositeConstruct %v4float %27 %27 %27 %27
  160. %29 = OpFAdd %v4float %20 %28
  161. OpStore %gl_FragColor %29
  162. OpReturn
  163. OpFunctionEnd
  164. )";
  165. SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
  166. predefs + after, true, true);
  167. }
  168. TEST_F(LocalSingleStoreElimTest, LSSElimForLinkage) {
  169. const std::string predefs =
  170. R"(OpCapability Shader
  171. OpCapability Linkage
  172. %1 = OpExtInstImport "GLSL.std.450"
  173. OpMemoryModel Logical GLSL450
  174. OpSource HLSL 630
  175. OpName %main "main"
  176. OpName %v "v"
  177. OpName %BaseColor "BaseColor"
  178. OpName %f "f"
  179. OpName %fi "fi"
  180. OpName %gl_FragColor "gl_FragColor"
  181. OpDecorate %main LinkageAttributes "main" Export
  182. %void = OpTypeVoid
  183. %9 = OpTypeFunction %void
  184. %float = OpTypeFloat 32
  185. %v4float = OpTypeVector %float 4
  186. %_ptr_Function_v4float = OpTypePointer Function %v4float
  187. %_ptr_Input_v4float = OpTypePointer Input %v4float
  188. %BaseColor = OpVariable %_ptr_Input_v4float Input
  189. %_ptr_Function_float = OpTypePointer Function %float
  190. %_ptr_Input_float = OpTypePointer Input %float
  191. %fi = OpVariable %_ptr_Input_float Input
  192. %float_0 = OpConstant %float 0
  193. %bool = OpTypeBool
  194. %_ptr_Output_v4float = OpTypePointer Output %v4float
  195. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  196. )";
  197. const std::string before =
  198. R"(%main = OpFunction %void None %9
  199. %19 = OpLabel
  200. %v = OpVariable %_ptr_Function_v4float Function
  201. %f = OpVariable %_ptr_Function_float Function
  202. %20 = OpLoad %v4float %BaseColor
  203. OpStore %v %20
  204. %21 = OpLoad %float %fi
  205. OpStore %f %21
  206. %22 = OpLoad %float %f
  207. %23 = OpFOrdLessThan %bool %22 %float_0
  208. OpSelectionMerge %24 None
  209. OpBranchConditional %23 %25 %24
  210. %25 = OpLabel
  211. OpStore %f %float_0
  212. OpBranch %24
  213. %24 = OpLabel
  214. %26 = OpLoad %v4float %v
  215. %27 = OpLoad %float %f
  216. %28 = OpCompositeConstruct %v4float %27 %27 %27 %27
  217. %29 = OpFAdd %v4float %26 %28
  218. OpStore %gl_FragColor %29
  219. OpReturn
  220. OpFunctionEnd
  221. )";
  222. const std::string after =
  223. R"(%main = OpFunction %void None %9
  224. %19 = OpLabel
  225. %v = OpVariable %_ptr_Function_v4float Function
  226. %f = OpVariable %_ptr_Function_float Function
  227. %20 = OpLoad %v4float %BaseColor
  228. OpStore %v %20
  229. %21 = OpLoad %float %fi
  230. OpStore %f %21
  231. %22 = OpLoad %float %f
  232. %23 = OpFOrdLessThan %bool %22 %float_0
  233. OpSelectionMerge %24 None
  234. OpBranchConditional %23 %25 %24
  235. %25 = OpLabel
  236. OpStore %f %float_0
  237. OpBranch %24
  238. %24 = OpLabel
  239. %27 = OpLoad %float %f
  240. %28 = OpCompositeConstruct %v4float %27 %27 %27 %27
  241. %29 = OpFAdd %v4float %20 %28
  242. OpStore %gl_FragColor %29
  243. OpReturn
  244. OpFunctionEnd
  245. )";
  246. SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
  247. predefs + after, true, true);
  248. }
  249. TEST_F(LocalSingleStoreElimTest, ThreeStores) {
  250. // Three stores to multiple loads of v is not optimized.
  251. const std::string predefs =
  252. R"(OpCapability Shader
  253. %1 = OpExtInstImport "GLSL.std.450"
  254. OpMemoryModel Logical GLSL450
  255. OpEntryPoint Fragment %main "main" %BaseColor %fi %gl_FragColor
  256. OpExecutionMode %main OriginUpperLeft
  257. OpSource GLSL 140
  258. OpName %main "main"
  259. OpName %v "v"
  260. OpName %BaseColor "BaseColor"
  261. OpName %fi "fi"
  262. OpName %r "r"
  263. OpName %gl_FragColor "gl_FragColor"
  264. %void = OpTypeVoid
  265. %9 = OpTypeFunction %void
  266. %float = OpTypeFloat 32
  267. %v4float = OpTypeVector %float 4
  268. %_ptr_Function_v4float = OpTypePointer Function %v4float
  269. %_ptr_Input_v4float = OpTypePointer Input %v4float
  270. %BaseColor = OpVariable %_ptr_Input_v4float Input
  271. %_ptr_Input_float = OpTypePointer Input %float
  272. %fi = OpVariable %_ptr_Input_float Input
  273. %float_0 = OpConstant %float 0
  274. %bool = OpTypeBool
  275. %float_1 = OpConstant %float 1
  276. %_ptr_Output_v4float = OpTypePointer Output %v4float
  277. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  278. )";
  279. const std::string before =
  280. R"(%main = OpFunction %void None %9
  281. %19 = OpLabel
  282. %v = OpVariable %_ptr_Function_v4float Function
  283. %r = OpVariable %_ptr_Function_v4float Function
  284. %20 = OpLoad %v4float %BaseColor
  285. OpStore %v %20
  286. %21 = OpLoad %float %fi
  287. %22 = OpFOrdLessThan %bool %21 %float_0
  288. OpSelectionMerge %23 None
  289. OpBranchConditional %22 %24 %25
  290. %24 = OpLabel
  291. %26 = OpLoad %v4float %v
  292. OpStore %v %26
  293. OpStore %r %26
  294. OpBranch %23
  295. %25 = OpLabel
  296. %27 = OpLoad %v4float %v
  297. %28 = OpCompositeConstruct %v4float %float_1 %float_1 %float_1 %float_1
  298. OpStore %v %28
  299. %29 = OpFSub %v4float %28 %27
  300. OpStore %r %29
  301. OpBranch %23
  302. %23 = OpLabel
  303. %30 = OpLoad %v4float %r
  304. OpStore %gl_FragColor %30
  305. OpReturn
  306. OpFunctionEnd
  307. )";
  308. SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
  309. predefs + before, true, true);
  310. }
  311. TEST_F(LocalSingleStoreElimTest, MultipleLoads) {
  312. // Single store to multiple loads of v is optimized.
  313. //
  314. // #version 140
  315. //
  316. // in vec4 BaseColor;
  317. // in float fi;
  318. //
  319. // void main()
  320. // {
  321. // vec4 v = BaseColor;
  322. // float f = fi;
  323. // if (f < 0)
  324. // f = 0.0;
  325. // gl_FragColor = v + f;
  326. // }
  327. const std::string predefs =
  328. R"(OpCapability Shader
  329. %1 = OpExtInstImport "GLSL.std.450"
  330. OpMemoryModel Logical GLSL450
  331. OpEntryPoint Fragment %main "main" %BaseColor %fi %gl_FragColor
  332. OpExecutionMode %main OriginUpperLeft
  333. OpSource GLSL 140
  334. OpName %main "main"
  335. OpName %v "v"
  336. OpName %BaseColor "BaseColor"
  337. OpName %fi "fi"
  338. OpName %r "r"
  339. OpName %gl_FragColor "gl_FragColor"
  340. %void = OpTypeVoid
  341. %9 = OpTypeFunction %void
  342. %float = OpTypeFloat 32
  343. %v4float = OpTypeVector %float 4
  344. %_ptr_Function_v4float = OpTypePointer Function %v4float
  345. %_ptr_Input_v4float = OpTypePointer Input %v4float
  346. %BaseColor = OpVariable %_ptr_Input_v4float Input
  347. %_ptr_Input_float = OpTypePointer Input %float
  348. %fi = OpVariable %_ptr_Input_float Input
  349. %float_0 = OpConstant %float 0
  350. %bool = OpTypeBool
  351. %float_1 = OpConstant %float 1
  352. %_ptr_Output_v4float = OpTypePointer Output %v4float
  353. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  354. )";
  355. const std::string before =
  356. R"(%main = OpFunction %void None %9
  357. %19 = OpLabel
  358. %v = OpVariable %_ptr_Function_v4float Function
  359. %r = OpVariable %_ptr_Function_v4float Function
  360. %20 = OpLoad %v4float %BaseColor
  361. OpStore %v %20
  362. %21 = OpLoad %float %fi
  363. %22 = OpFOrdLessThan %bool %21 %float_0
  364. OpSelectionMerge %23 None
  365. OpBranchConditional %22 %24 %25
  366. %24 = OpLabel
  367. %26 = OpLoad %v4float %v
  368. OpStore %r %26
  369. OpBranch %23
  370. %25 = OpLabel
  371. %27 = OpLoad %v4float %v
  372. %28 = OpCompositeConstruct %v4float %float_1 %float_1 %float_1 %float_1
  373. %29 = OpFSub %v4float %28 %27
  374. OpStore %r %29
  375. OpBranch %23
  376. %23 = OpLabel
  377. %30 = OpLoad %v4float %r
  378. OpStore %gl_FragColor %30
  379. OpReturn
  380. OpFunctionEnd
  381. )";
  382. const std::string after =
  383. R"(%main = OpFunction %void None %9
  384. %19 = OpLabel
  385. %v = OpVariable %_ptr_Function_v4float Function
  386. %r = OpVariable %_ptr_Function_v4float Function
  387. %20 = OpLoad %v4float %BaseColor
  388. OpStore %v %20
  389. %21 = OpLoad %float %fi
  390. %22 = OpFOrdLessThan %bool %21 %float_0
  391. OpSelectionMerge %23 None
  392. OpBranchConditional %22 %24 %25
  393. %24 = OpLabel
  394. OpStore %r %20
  395. OpBranch %23
  396. %25 = OpLabel
  397. %28 = OpCompositeConstruct %v4float %float_1 %float_1 %float_1 %float_1
  398. %29 = OpFSub %v4float %28 %20
  399. OpStore %r %29
  400. OpBranch %23
  401. %23 = OpLabel
  402. %30 = OpLoad %v4float %r
  403. OpStore %gl_FragColor %30
  404. OpReturn
  405. OpFunctionEnd
  406. )";
  407. SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
  408. predefs + after, true, true);
  409. }
  410. TEST_F(LocalSingleStoreElimTest, NoStoreElimWithInterveningAccessChainLoad) {
  411. // Last load of v is eliminated, but access chain load and store of v isn't
  412. //
  413. // #version 140
  414. //
  415. // in vec4 BaseColor;
  416. //
  417. // void main()
  418. // {
  419. // vec4 v = BaseColor;
  420. // float f = v[3];
  421. // gl_FragColor = v * f;
  422. // }
  423. const std::string predefs =
  424. R"(OpCapability Shader
  425. %1 = OpExtInstImport "GLSL.std.450"
  426. OpMemoryModel Logical GLSL450
  427. OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
  428. OpExecutionMode %main OriginUpperLeft
  429. OpSource GLSL 140
  430. OpName %main "main"
  431. OpName %v "v"
  432. OpName %BaseColor "BaseColor"
  433. OpName %f "f"
  434. OpName %gl_FragColor "gl_FragColor"
  435. %void = OpTypeVoid
  436. %8 = OpTypeFunction %void
  437. %float = OpTypeFloat 32
  438. %v4float = OpTypeVector %float 4
  439. %_ptr_Function_v4float = OpTypePointer Function %v4float
  440. %_ptr_Input_v4float = OpTypePointer Input %v4float
  441. %BaseColor = OpVariable %_ptr_Input_v4float Input
  442. %_ptr_Function_float = OpTypePointer Function %float
  443. %uint = OpTypeInt 32 0
  444. %uint_3 = OpConstant %uint 3
  445. %_ptr_Output_v4float = OpTypePointer Output %v4float
  446. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  447. )";
  448. const std::string before =
  449. R"(%main = OpFunction %void None %8
  450. %17 = OpLabel
  451. %v = OpVariable %_ptr_Function_v4float Function
  452. %f = OpVariable %_ptr_Function_float Function
  453. %18 = OpLoad %v4float %BaseColor
  454. OpStore %v %18
  455. %19 = OpAccessChain %_ptr_Function_float %v %uint_3
  456. %20 = OpLoad %float %19
  457. OpStore %f %20
  458. %21 = OpLoad %v4float %v
  459. %22 = OpLoad %float %f
  460. %23 = OpVectorTimesScalar %v4float %21 %22
  461. OpStore %gl_FragColor %23
  462. OpReturn
  463. OpFunctionEnd
  464. )";
  465. const std::string after =
  466. R"(%main = OpFunction %void None %8
  467. %17 = OpLabel
  468. %v = OpVariable %_ptr_Function_v4float Function
  469. %f = OpVariable %_ptr_Function_float Function
  470. %18 = OpLoad %v4float %BaseColor
  471. OpStore %v %18
  472. %19 = OpAccessChain %_ptr_Function_float %v %uint_3
  473. %20 = OpLoad %float %19
  474. OpStore %f %20
  475. %23 = OpVectorTimesScalar %v4float %18 %20
  476. OpStore %gl_FragColor %23
  477. OpReturn
  478. OpFunctionEnd
  479. )";
  480. SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
  481. predefs + after, true, true);
  482. }
  483. TEST_F(LocalSingleStoreElimTest, NoReplaceOfDominatingPartialStore) {
  484. // Note: SPIR-V hand edited to initialize v to vec4(0.0)
  485. //
  486. // #version 140
  487. //
  488. // in vec4 BaseColor;
  489. //
  490. // void main()
  491. // {
  492. // vec4 v;
  493. // float v[1] = 1.0;
  494. // gl_FragColor = v;
  495. // }
  496. const std::string assembly =
  497. R"(OpCapability Shader
  498. %1 = OpExtInstImport "GLSL.std.450"
  499. OpMemoryModel Logical GLSL450
  500. OpEntryPoint Fragment %main "main" %gl_FragColor %BaseColor
  501. OpExecutionMode %main OriginUpperLeft
  502. OpSource GLSL 140
  503. OpName %main "main"
  504. OpName %v "v"
  505. OpName %gl_FragColor "gl_FragColor"
  506. OpName %BaseColor "BaseColor"
  507. %void = OpTypeVoid
  508. %7 = OpTypeFunction %void
  509. %float = OpTypeFloat 32
  510. %v4float = OpTypeVector %float 4
  511. %_ptr_Function_v4float = OpTypePointer Function %v4float
  512. %float_0 = OpConstant %float 0
  513. %12 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
  514. %float_1 = OpConstant %float 1
  515. %uint = OpTypeInt 32 0
  516. %uint_1 = OpConstant %uint 1
  517. %_ptr_Function_float = OpTypePointer Function %float
  518. %_ptr_Output_v4float = OpTypePointer Output %v4float
  519. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  520. %_ptr_Input_v4float = OpTypePointer Input %v4float
  521. %BaseColor = OpVariable %_ptr_Input_v4float Input
  522. %main = OpFunction %void None %7
  523. %19 = OpLabel
  524. %v = OpVariable %_ptr_Function_v4float Function %12
  525. %20 = OpAccessChain %_ptr_Function_float %v %uint_1
  526. OpStore %20 %float_1
  527. %21 = OpLoad %v4float %v
  528. OpStore %gl_FragColor %21
  529. OpReturn
  530. OpFunctionEnd
  531. )";
  532. SinglePassRunAndCheck<LocalSingleStoreElimPass>(assembly, assembly, true,
  533. true);
  534. }
  535. TEST_F(LocalSingleStoreElimTest, ElimIfCopyObjectInFunction) {
  536. // Note: hand edited to insert OpCopyObject
  537. //
  538. // #version 140
  539. //
  540. // in vec4 BaseColor;
  541. // in float fi;
  542. //
  543. // void main()
  544. // {
  545. // vec4 v = BaseColor;
  546. // float f = fi;
  547. // if (f < 0)
  548. // f = 0.0;
  549. // gl_FragColor = v + f;
  550. // }
  551. const std::string predefs =
  552. R"(OpCapability Shader
  553. %1 = OpExtInstImport "GLSL.std.450"
  554. OpMemoryModel Logical GLSL450
  555. OpEntryPoint Fragment %main "main" %BaseColor %fi %gl_FragColor
  556. OpExecutionMode %main OriginUpperLeft
  557. OpSource GLSL 140
  558. OpName %main "main"
  559. OpName %v "v"
  560. OpName %BaseColor "BaseColor"
  561. OpName %f "f"
  562. OpName %fi "fi"
  563. OpName %gl_FragColor "gl_FragColor"
  564. %void = OpTypeVoid
  565. %9 = OpTypeFunction %void
  566. %float = OpTypeFloat 32
  567. %v4float = OpTypeVector %float 4
  568. %_ptr_Function_v4float = OpTypePointer Function %v4float
  569. %_ptr_Input_v4float = OpTypePointer Input %v4float
  570. %BaseColor = OpVariable %_ptr_Input_v4float Input
  571. %_ptr_Function_float = OpTypePointer Function %float
  572. %_ptr_Input_float = OpTypePointer Input %float
  573. %fi = OpVariable %_ptr_Input_float Input
  574. %float_0 = OpConstant %float 0
  575. %bool = OpTypeBool
  576. %_ptr_Output_v4float = OpTypePointer Output %v4float
  577. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  578. )";
  579. const std::string before =
  580. R"(%main = OpFunction %void None %9
  581. %19 = OpLabel
  582. %v = OpVariable %_ptr_Function_v4float Function
  583. %f = OpVariable %_ptr_Function_float Function
  584. %20 = OpLoad %v4float %BaseColor
  585. OpStore %v %20
  586. %21 = OpLoad %float %fi
  587. OpStore %f %21
  588. %22 = OpLoad %float %f
  589. %23 = OpFOrdLessThan %bool %22 %float_0
  590. OpSelectionMerge %24 None
  591. OpBranchConditional %23 %25 %24
  592. %25 = OpLabel
  593. OpStore %f %float_0
  594. OpBranch %24
  595. %24 = OpLabel
  596. %26 = OpCopyObject %_ptr_Function_v4float %v
  597. %27 = OpLoad %v4float %26
  598. %28 = OpLoad %float %f
  599. %29 = OpCompositeConstruct %v4float %28 %28 %28 %28
  600. %30 = OpFAdd %v4float %27 %29
  601. OpStore %gl_FragColor %30
  602. OpReturn
  603. OpFunctionEnd
  604. )";
  605. const std::string after =
  606. R"(%main = OpFunction %void None %9
  607. %19 = OpLabel
  608. %v = OpVariable %_ptr_Function_v4float Function
  609. %f = OpVariable %_ptr_Function_float Function
  610. %20 = OpLoad %v4float %BaseColor
  611. OpStore %v %20
  612. %21 = OpLoad %float %fi
  613. OpStore %f %21
  614. %22 = OpLoad %float %f
  615. %23 = OpFOrdLessThan %bool %22 %float_0
  616. OpSelectionMerge %24 None
  617. OpBranchConditional %23 %25 %24
  618. %25 = OpLabel
  619. OpStore %f %float_0
  620. OpBranch %24
  621. %24 = OpLabel
  622. %26 = OpCopyObject %_ptr_Function_v4float %v
  623. %28 = OpLoad %float %f
  624. %29 = OpCompositeConstruct %v4float %28 %28 %28 %28
  625. %30 = OpFAdd %v4float %20 %29
  626. OpStore %gl_FragColor %30
  627. OpReturn
  628. OpFunctionEnd
  629. )";
  630. SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
  631. predefs + after, true, true);
  632. }
  633. TEST_F(LocalSingleStoreElimTest, NoOptIfStoreNotDominating) {
  634. // Single store to f not optimized because it does not dominate
  635. // the load.
  636. //
  637. // #version 140
  638. //
  639. // in vec4 BaseColor;
  640. // in float fi;
  641. //
  642. // void main()
  643. // {
  644. // float f;
  645. // if (fi < 0)
  646. // f = 0.5;
  647. // if (fi < 0)
  648. // gl_FragColor = BaseColor * f;
  649. // else
  650. // gl_FragColor = BaseColor;
  651. // }
  652. const std::string assembly =
  653. R"(OpCapability Shader
  654. %1 = OpExtInstImport "GLSL.std.450"
  655. OpMemoryModel Logical GLSL450
  656. OpEntryPoint Fragment %main "main" %fi %gl_FragColor %BaseColor
  657. OpExecutionMode %main OriginUpperLeft
  658. OpSource GLSL 140
  659. OpName %main "main"
  660. OpName %fi "fi"
  661. OpName %f "f"
  662. OpName %gl_FragColor "gl_FragColor"
  663. OpName %BaseColor "BaseColor"
  664. %void = OpTypeVoid
  665. %8 = OpTypeFunction %void
  666. %float = OpTypeFloat 32
  667. %_ptr_Input_float = OpTypePointer Input %float
  668. %fi = OpVariable %_ptr_Input_float Input
  669. %float_0 = OpConstant %float 0
  670. %bool = OpTypeBool
  671. %_ptr_Function_float = OpTypePointer Function %float
  672. %float_0_5 = OpConstant %float 0.5
  673. %v4float = OpTypeVector %float 4
  674. %_ptr_Output_v4float = OpTypePointer Output %v4float
  675. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  676. %_ptr_Input_v4float = OpTypePointer Input %v4float
  677. %BaseColor = OpVariable %_ptr_Input_v4float Input
  678. %main = OpFunction %void None %8
  679. %18 = OpLabel
  680. %f = OpVariable %_ptr_Function_float Function
  681. %19 = OpLoad %float %fi
  682. %20 = OpFOrdLessThan %bool %19 %float_0
  683. OpSelectionMerge %21 None
  684. OpBranchConditional %20 %22 %21
  685. %22 = OpLabel
  686. OpStore %f %float_0_5
  687. OpBranch %21
  688. %21 = OpLabel
  689. %23 = OpLoad %float %fi
  690. %24 = OpFOrdLessThan %bool %23 %float_0
  691. OpSelectionMerge %25 None
  692. OpBranchConditional %24 %26 %27
  693. %26 = OpLabel
  694. %28 = OpLoad %v4float %BaseColor
  695. %29 = OpLoad %float %f
  696. %30 = OpVectorTimesScalar %v4float %28 %29
  697. OpStore %gl_FragColor %30
  698. OpBranch %25
  699. %27 = OpLabel
  700. %31 = OpLoad %v4float %BaseColor
  701. OpStore %gl_FragColor %31
  702. OpBranch %25
  703. %25 = OpLabel
  704. OpReturn
  705. OpFunctionEnd
  706. )";
  707. SinglePassRunAndCheck<LocalSingleStoreElimPass>(assembly, assembly, true,
  708. true);
  709. }
  710. TEST_F(LocalSingleStoreElimTest, OptInitializedVariableLikeStore) {
  711. // Initialized variable f is optimized like it was a store.
  712. // Note: The SPIR-V was edited to turn the store to f to an
  713. // an initialization.
  714. //
  715. // #version 140
  716. //
  717. // void main()
  718. // {
  719. // float f = 0.0;
  720. // gl_FragColor = vec4(f);
  721. // }
  722. const std::string predefs =
  723. R"(OpCapability Shader
  724. %1 = OpExtInstImport "GLSL.std.450"
  725. OpMemoryModel Logical GLSL450
  726. OpEntryPoint Fragment %main "main" %gl_FragColor
  727. OpExecutionMode %main OriginUpperLeft
  728. OpSource GLSL 140
  729. OpName %main "main"
  730. OpName %f "f"
  731. OpName %gl_FragColor "gl_FragColor"
  732. OpDecorate %gl_FragColor Location 0
  733. %void = OpTypeVoid
  734. %6 = OpTypeFunction %void
  735. %float = OpTypeFloat 32
  736. %_ptr_Function_float = OpTypePointer Function %float
  737. %float_0 = OpConstant %float 0
  738. %v4float = OpTypeVector %float 4
  739. %_ptr_Output_v4float = OpTypePointer Output %v4float
  740. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  741. )";
  742. const std::string before =
  743. R"(%main = OpFunction %void None %6
  744. %12 = OpLabel
  745. %f = OpVariable %_ptr_Function_float Function %float_0
  746. %13 = OpLoad %float %f
  747. %14 = OpCompositeConstruct %v4float %13 %13 %13 %13
  748. OpStore %gl_FragColor %14
  749. OpReturn
  750. OpFunctionEnd
  751. )";
  752. const std::string after =
  753. R"(%main = OpFunction %void None %6
  754. %12 = OpLabel
  755. %f = OpVariable %_ptr_Function_float Function %float_0
  756. %14 = OpCompositeConstruct %v4float %float_0 %float_0 %float_0 %float_0
  757. OpStore %gl_FragColor %14
  758. OpReturn
  759. OpFunctionEnd
  760. )";
  761. SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
  762. predefs + after, true, true);
  763. }
  764. TEST_F(LocalSingleStoreElimTest, PointerVariable) {
  765. // Test that checks if a pointer variable is removed.
  766. const std::string before =
  767. R"(OpCapability Shader
  768. OpMemoryModel Logical GLSL450
  769. OpEntryPoint Fragment %1 "main" %2
  770. OpExecutionMode %1 OriginUpperLeft
  771. OpMemberDecorate %_struct_3 0 Offset 0
  772. OpDecorate %_runtimearr__struct_3 ArrayStride 16
  773. OpMemberDecorate %_struct_5 0 Offset 0
  774. OpDecorate %_struct_5 BufferBlock
  775. OpMemberDecorate %_struct_6 0 Offset 0
  776. OpDecorate %_struct_6 BufferBlock
  777. OpDecorate %2 Location 0
  778. OpDecorate %7 DescriptorSet 0
  779. OpDecorate %7 Binding 0
  780. %void = OpTypeVoid
  781. %10 = OpTypeFunction %void
  782. %int = OpTypeInt 32 1
  783. %uint = OpTypeInt 32 0
  784. %float = OpTypeFloat 32
  785. %v4float = OpTypeVector %float 4
  786. %_ptr_Output_v4float = OpTypePointer Output %v4float
  787. %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
  788. %_struct_3 = OpTypeStruct %v4float
  789. %_runtimearr__struct_3 = OpTypeRuntimeArray %_struct_3
  790. %_struct_5 = OpTypeStruct %_runtimearr__struct_3
  791. %_ptr_Uniform__struct_5 = OpTypePointer Uniform %_struct_5
  792. %_struct_6 = OpTypeStruct %int
  793. %_ptr_Uniform__struct_6 = OpTypePointer Uniform %_struct_6
  794. %_ptr_Function__ptr_Uniform__struct_5 = OpTypePointer Function %_ptr_Uniform__struct_5
  795. %_ptr_Function__ptr_Uniform__struct_6 = OpTypePointer Function %_ptr_Uniform__struct_6
  796. %int_0 = OpConstant %int 0
  797. %uint_0 = OpConstant %uint 0
  798. %2 = OpVariable %_ptr_Output_v4float Output
  799. %7 = OpVariable %_ptr_Uniform__struct_5 Uniform
  800. %1 = OpFunction %void None %10
  801. %23 = OpLabel
  802. %24 = OpVariable %_ptr_Function__ptr_Uniform__struct_5 Function
  803. OpStore %24 %7
  804. %26 = OpLoad %_ptr_Uniform__struct_5 %24
  805. %27 = OpAccessChain %_ptr_Uniform_v4float %26 %int_0 %uint_0 %int_0
  806. %28 = OpLoad %v4float %27
  807. %29 = OpCopyObject %v4float %28
  808. OpStore %2 %28
  809. OpReturn
  810. OpFunctionEnd
  811. )";
  812. const std::string after =
  813. R"(OpCapability Shader
  814. OpMemoryModel Logical GLSL450
  815. OpEntryPoint Fragment %1 "main" %2
  816. OpExecutionMode %1 OriginUpperLeft
  817. OpMemberDecorate %_struct_3 0 Offset 0
  818. OpDecorate %_runtimearr__struct_3 ArrayStride 16
  819. OpMemberDecorate %_struct_5 0 Offset 0
  820. OpDecorate %_struct_5 BufferBlock
  821. OpMemberDecorate %_struct_6 0 Offset 0
  822. OpDecorate %_struct_6 BufferBlock
  823. OpDecorate %2 Location 0
  824. OpDecorate %7 DescriptorSet 0
  825. OpDecorate %7 Binding 0
  826. %void = OpTypeVoid
  827. %10 = OpTypeFunction %void
  828. %int = OpTypeInt 32 1
  829. %uint = OpTypeInt 32 0
  830. %float = OpTypeFloat 32
  831. %v4float = OpTypeVector %float 4
  832. %_ptr_Output_v4float = OpTypePointer Output %v4float
  833. %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
  834. %_struct_3 = OpTypeStruct %v4float
  835. %_runtimearr__struct_3 = OpTypeRuntimeArray %_struct_3
  836. %_struct_5 = OpTypeStruct %_runtimearr__struct_3
  837. %_ptr_Uniform__struct_5 = OpTypePointer Uniform %_struct_5
  838. %_struct_6 = OpTypeStruct %int
  839. %_ptr_Uniform__struct_6 = OpTypePointer Uniform %_struct_6
  840. %_ptr_Function__ptr_Uniform__struct_5 = OpTypePointer Function %_ptr_Uniform__struct_5
  841. %_ptr_Function__ptr_Uniform__struct_6 = OpTypePointer Function %_ptr_Uniform__struct_6
  842. %int_0 = OpConstant %int 0
  843. %uint_0 = OpConstant %uint 0
  844. %2 = OpVariable %_ptr_Output_v4float Output
  845. %7 = OpVariable %_ptr_Uniform__struct_5 Uniform
  846. %1 = OpFunction %void None %10
  847. %23 = OpLabel
  848. %24 = OpVariable %_ptr_Function__ptr_Uniform__struct_5 Function
  849. OpStore %24 %7
  850. %27 = OpAccessChain %_ptr_Uniform_v4float %7 %int_0 %uint_0 %int_0
  851. %28 = OpLoad %v4float %27
  852. %29 = OpCopyObject %v4float %28
  853. OpStore %2 %28
  854. OpReturn
  855. OpFunctionEnd
  856. )";
  857. // Relax logical pointers to allow pointer allocations.
  858. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  859. ValidatorOptions()->relax_logical_pointer = true;
  860. SinglePassRunAndCheck<LocalSingleStoreElimPass>(before, after, true, true);
  861. }
  862. // Test that that an unused OpAccessChain between a store and a use does does
  863. // not hinders the replacement of the use. We need to check this because
  864. // local-access-chain-convert does always remove the OpAccessChain instructions
  865. // that become dead.
  866. TEST_F(LocalSingleStoreElimTest,
  867. StoreElimWithUnusedInterveningAccessChainLoad) {
  868. // Last load of v is eliminated, but access chain load and store of v isn't
  869. //
  870. // #version 140
  871. //
  872. // in vec4 BaseColor;
  873. //
  874. // void main()
  875. // {
  876. // vec4 v = BaseColor;
  877. // float f = v[3];
  878. // gl_FragColor = v * f;
  879. // }
  880. const std::string predefs =
  881. R"(OpCapability Shader
  882. %1 = OpExtInstImport "GLSL.std.450"
  883. OpMemoryModel Logical GLSL450
  884. OpEntryPoint Fragment %main "main" %BaseColor %gl_FragColor
  885. OpExecutionMode %main OriginUpperLeft
  886. OpSource GLSL 140
  887. OpName %main "main"
  888. OpName %v "v"
  889. OpName %BaseColor "BaseColor"
  890. OpName %gl_FragColor "gl_FragColor"
  891. %void = OpTypeVoid
  892. %8 = OpTypeFunction %void
  893. %float = OpTypeFloat 32
  894. %v4float = OpTypeVector %float 4
  895. %_ptr_Function_v4float = OpTypePointer Function %v4float
  896. %_ptr_Input_v4float = OpTypePointer Input %v4float
  897. %BaseColor = OpVariable %_ptr_Input_v4float Input
  898. %_ptr_Function_float = OpTypePointer Function %float
  899. %uint = OpTypeInt 32 0
  900. %uint_3 = OpConstant %uint 3
  901. %_ptr_Output_v4float = OpTypePointer Output %v4float
  902. %gl_FragColor = OpVariable %_ptr_Output_v4float Output
  903. )";
  904. const std::string before =
  905. R"(%main = OpFunction %void None %8
  906. %17 = OpLabel
  907. %v = OpVariable %_ptr_Function_v4float Function
  908. %18 = OpLoad %v4float %BaseColor
  909. OpStore %v %18
  910. %19 = OpAccessChain %_ptr_Function_float %v %uint_3
  911. %21 = OpLoad %v4float %v
  912. OpStore %gl_FragColor %21
  913. OpReturn
  914. OpFunctionEnd
  915. )";
  916. const std::string after =
  917. R"(%main = OpFunction %void None %8
  918. %17 = OpLabel
  919. %v = OpVariable %_ptr_Function_v4float Function
  920. %18 = OpLoad %v4float %BaseColor
  921. OpStore %v %18
  922. %19 = OpAccessChain %_ptr_Function_float %v %uint_3
  923. OpStore %gl_FragColor %18
  924. OpReturn
  925. OpFunctionEnd
  926. )";
  927. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  928. SinglePassRunAndCheck<LocalSingleStoreElimPass>(predefs + before,
  929. predefs + after, true, true);
  930. }
  931. TEST_F(LocalSingleStoreElimTest, VariablePointerTest) {
  932. // Check that the load of the first variable is still used and that the load
  933. // of the third variable is propagated. The first load has to remain because
  934. // of the store to the variable pointer.
  935. const std::string text = R"(
  936. ; CHECK: [[v1:%\w+]] = OpVariable
  937. ; CHECK: [[v2:%\w+]] = OpVariable
  938. ; CHECK: [[v3:%\w+]] = OpVariable
  939. ; CHECK: [[ld1:%\w+]] = OpLoad %int [[v1]]
  940. ; CHECK: OpIAdd %int [[ld1]] %int_0
  941. OpCapability Shader
  942. OpCapability VariablePointers
  943. %1 = OpExtInstImport "GLSL.std.450"
  944. OpMemoryModel Logical GLSL450
  945. OpEntryPoint GLCompute %2 "main"
  946. OpExecutionMode %2 LocalSize 1 1 1
  947. OpSource GLSL 450
  948. OpMemberDecorate %_struct_3 0 Offset 0
  949. OpMemberDecorate %_struct_3 1 Offset 4
  950. %void = OpTypeVoid
  951. %5 = OpTypeFunction %void
  952. %int = OpTypeInt 32 1
  953. %bool = OpTypeBool
  954. %_struct_3 = OpTypeStruct %int %int
  955. %_ptr_Function__struct_3 = OpTypePointer Function %_struct_3
  956. %_ptr_Function_int = OpTypePointer Function %int
  957. %true = OpConstantTrue %bool
  958. %int_0 = OpConstant %int 0
  959. %int_1 = OpConstant %int 1
  960. %13 = OpConstantNull %_struct_3
  961. %2 = OpFunction %void None %5
  962. %14 = OpLabel
  963. %15 = OpVariable %_ptr_Function_int Function
  964. %16 = OpVariable %_ptr_Function_int Function
  965. %17 = OpVariable %_ptr_Function_int Function
  966. OpStore %15 %int_1
  967. OpStore %17 %int_0
  968. OpSelectionMerge %18 None
  969. OpBranchConditional %true %19 %20
  970. %19 = OpLabel
  971. OpBranch %18
  972. %20 = OpLabel
  973. OpBranch %18
  974. %18 = OpLabel
  975. %21 = OpPhi %_ptr_Function_int %15 %19 %16 %20
  976. OpStore %21 %int_0
  977. %22 = OpLoad %int %15
  978. %23 = OpLoad %int %17
  979. %24 = OpIAdd %int %22 %23
  980. OpReturn
  981. OpFunctionEnd
  982. )";
  983. SinglePassRunAndMatch<LocalSingleStoreElimPass>(text, false);
  984. }
  985. TEST_F(LocalSingleStoreElimTest, DebugDeclareTest) {
  986. // If OpenCL.DebugInfo.100 enabled, check that store/load is still
  987. // optimized, DebugValue placed after the store and the associated
  988. // DebugDeclare is removed.
  989. const std::string text = R"(
  990. OpCapability Shader
  991. %1 = OpExtInstImport "OpenCL.DebugInfo.100"
  992. OpMemoryModel Logical GLSL450
  993. OpEntryPoint Fragment %MainPs "MainPs" %g_tColor %g_sAniso %in_var_TEXCOORD2 %out_var_SV_Target0
  994. OpExecutionMode %MainPs OriginUpperLeft
  995. %20 = OpString "foo.frag"
  996. %24 = OpString "PS_OUTPUT"
  997. %28 = OpString "float"
  998. %31 = OpString "vColor"
  999. %33 = OpString "PS_INPUT"
  1000. %38 = OpString "vTextureCoords"
  1001. %40 = OpString "@type.2d.image"
  1002. %41 = OpString "type.2d.image"
  1003. %43 = OpString "Texture2D.TemplateParam"
  1004. %47 = OpString "src.MainPs"
  1005. %51 = OpString "tc"
  1006. %53 = OpString "ps_output"
  1007. %56 = OpString "i"
  1008. %58 = OpString "@type.sampler"
  1009. %59 = OpString "type.sampler"
  1010. %61 = OpString "g_sAniso"
  1011. %63 = OpString "g_tColor"
  1012. OpName %type_2d_image "type.2d.image"
  1013. OpName %g_tColor "g_tColor"
  1014. OpName %type_sampler "type.sampler"
  1015. OpName %g_sAniso "g_sAniso"
  1016. OpName %in_var_TEXCOORD2 "in.var.TEXCOORD2"
  1017. OpName %out_var_SV_Target0 "out.var.SV_Target0"
  1018. OpName %MainPs "MainPs"
  1019. OpName %PS_INPUT "PS_INPUT"
  1020. OpMemberName %PS_INPUT 0 "vTextureCoords"
  1021. OpName %param_var_i "param.var.i"
  1022. OpName %PS_OUTPUT "PS_OUTPUT"
  1023. OpMemberName %PS_OUTPUT 0 "vColor"
  1024. OpName %type_sampled_image "type.sampled.image"
  1025. OpDecorate %in_var_TEXCOORD2 Location 0
  1026. OpDecorate %out_var_SV_Target0 Location 0
  1027. OpDecorate %g_tColor DescriptorSet 0
  1028. OpDecorate %g_tColor Binding 0
  1029. OpDecorate %g_sAniso DescriptorSet 0
  1030. OpDecorate %g_sAniso Binding 1
  1031. %int = OpTypeInt 32 1
  1032. %int_0 = OpConstant %int 0
  1033. %uint = OpTypeInt 32 0
  1034. %uint_32 = OpConstant %uint 32
  1035. %float = OpTypeFloat 32
  1036. %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
  1037. %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
  1038. %type_sampler = OpTypeSampler
  1039. %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
  1040. %v2float = OpTypeVector %float 2
  1041. %_ptr_Input_v2float = OpTypePointer Input %v2float
  1042. %v4float = OpTypeVector %float 4
  1043. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1044. %void = OpTypeVoid
  1045. %uint_128 = OpConstant %uint 128
  1046. %uint_0 = OpConstant %uint 0
  1047. %uint_64 = OpConstant %uint 64
  1048. %65 = OpTypeFunction %void
  1049. %PS_INPUT = OpTypeStruct %v2float
  1050. %_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
  1051. %PS_OUTPUT = OpTypeStruct %v4float
  1052. %75 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
  1053. %_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
  1054. %_ptr_Function_v2float = OpTypePointer Function %v2float
  1055. %type_sampled_image = OpTypeSampledImage %type_2d_image
  1056. %_ptr_Function_v4float = OpTypePointer Function %v4float
  1057. %g_tColor = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
  1058. %g_sAniso = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
  1059. %in_var_TEXCOORD2 = OpVariable %_ptr_Input_v2float Input
  1060. %out_var_SV_Target0 = OpVariable %_ptr_Output_v4float Output
  1061. %39 = OpExtInst %void %1 DebugInfoNone
  1062. %55 = OpExtInst %void %1 DebugExpression
  1063. %22 = OpExtInst %void %1 DebugSource %20
  1064. %23 = OpExtInst %void %1 DebugCompilationUnit 1 4 %22 HLSL
  1065. %26 = OpExtInst %void %1 DebugTypeComposite %24 Structure %22 10 1 %23 %24 %uint_128 FlagIsProtected|FlagIsPrivate %27
  1066. %29 = OpExtInst %void %1 DebugTypeBasic %28 %uint_32 Float
  1067. %30 = OpExtInst %void %1 DebugTypeVector %29 4
  1068. %27 = OpExtInst %void %1 DebugTypeMember %31 %30 %22 12 5 %26 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
  1069. %35 = OpExtInst %void %1 DebugTypeComposite %33 Structure %22 5 1 %23 %33 %uint_64 FlagIsProtected|FlagIsPrivate %36
  1070. %37 = OpExtInst %void %1 DebugTypeVector %29 2
  1071. %36 = OpExtInst %void %1 DebugTypeMember %38 %37 %22 7 5 %35 %uint_0 %uint_64 FlagIsProtected|FlagIsPrivate
  1072. %42 = OpExtInst %void %1 DebugTypeComposite %40 Class %22 0 0 %23 %41 %39 FlagIsProtected|FlagIsPrivate
  1073. %44 = OpExtInst %void %1 DebugTypeTemplateParameter %43 %29 %39 %22 0 0
  1074. %45 = OpExtInst %void %1 DebugTypeTemplate %42 %44
  1075. %46 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %26 %35
  1076. %48 = OpExtInst %void %1 DebugFunction %47 %46 %22 15 1 %23 %47 FlagIsProtected|FlagIsPrivate 16 %39
  1077. %50 = OpExtInst %void %1 DebugLexicalBlock %22 16 1 %48
  1078. %52 = OpExtInst %void %1 DebugLocalVariable %51 %37 %22 19 12 %50 FlagIsLocal
  1079. %54 = OpExtInst %void %1 DebugLocalVariable %53 %26 %22 17 15 %50 FlagIsLocal
  1080. %57 = OpExtInst %void %1 DebugLocalVariable %56 %35 %22 15 29 %48 FlagIsLocal 1
  1081. %60 = OpExtInst %void %1 DebugTypeComposite %58 Structure %22 0 0 %23 %59 %39 FlagIsProtected|FlagIsPrivate
  1082. %62 = OpExtInst %void %1 DebugGlobalVariable %61 %60 %22 3 14 %23 %61 %g_sAniso FlagIsDefinition
  1083. %64 = OpExtInst %void %1 DebugGlobalVariable %63 %42 %22 1 11 %23 %63 %g_tColor FlagIsDefinition
  1084. %MainPs = OpFunction %void None %65
  1085. %66 = OpLabel
  1086. %114 = OpExtInst %void %1 DebugScope %50
  1087. %98 = OpVariable %_ptr_Function_PS_OUTPUT Function
  1088. %99 = OpVariable %_ptr_Function_v2float Function
  1089. %115 = OpExtInst %void %1 DebugNoScope
  1090. %100 = OpVariable %_ptr_Function_PS_OUTPUT Function
  1091. %param_var_i = OpVariable %_ptr_Function_PS_INPUT Function
  1092. %70 = OpLoad %v2float %in_var_TEXCOORD2
  1093. %71 = OpCompositeConstruct %PS_INPUT %70
  1094. OpStore %param_var_i %71
  1095. %116 = OpExtInst %void %1 DebugScope %48
  1096. %102 = OpExtInst %void %1 DebugDeclare %57 %param_var_i %55
  1097. %117 = OpExtInst %void %1 DebugScope %50
  1098. %103 = OpExtInst %void %1 DebugDeclare %54 %98 %55
  1099. OpLine %20 19 17
  1100. %104 = OpAccessChain %_ptr_Function_v2float %param_var_i %int_0
  1101. %105 = OpLoad %v2float %104
  1102. OpLine %20 19 12
  1103. OpStore %99 %105
  1104. %106 = OpExtInst %void %1 DebugDeclare %52 %99 %55
  1105. ;CHECK-NOT: %106 = OpExtInst %void %1 DebugDeclare %52 %99 %55
  1106. ;CHECK: %119 = OpExtInst %void %1 DebugValue %52 %105 %55
  1107. OpLine %20 20 26
  1108. %107 = OpLoad %type_2d_image %g_tColor
  1109. OpLine %20 20 46
  1110. %108 = OpLoad %type_sampler %g_sAniso
  1111. OpLine %20 20 57
  1112. %109 = OpLoad %v2float %99
  1113. ;CHECK-NOT: %109 = OpLoad %v2float %99
  1114. OpLine %20 20 26
  1115. %110 = OpSampledImage %type_sampled_image %107 %108
  1116. %111 = OpImageSampleImplicitLod %v4float %110 %109 None
  1117. ;CHECK-NOT: %111 = OpImageSampleImplicitLod %v4float %110 %109 None
  1118. ;CHECK: %111 = OpImageSampleImplicitLod %v4float %110 %105 None
  1119. OpLine %20 20 5
  1120. %112 = OpAccessChain %_ptr_Function_v4float %98 %int_0
  1121. OpStore %112 %111
  1122. OpLine %20 21 12
  1123. %113 = OpLoad %PS_OUTPUT %98
  1124. OpLine %20 21 5
  1125. OpStore %100 %113
  1126. %118 = OpExtInst %void %1 DebugNoScope
  1127. %73 = OpLoad %PS_OUTPUT %100
  1128. %74 = OpCompositeExtract %v4float %73 0
  1129. OpStore %out_var_SV_Target0 %74
  1130. OpReturn
  1131. OpFunctionEnd
  1132. )";
  1133. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1134. SinglePassRunAndMatch<LocalSingleStoreElimPass>(text, false);
  1135. }
  1136. TEST_F(LocalSingleStoreElimTest, DebugValueTest) {
  1137. // If OpenCL.DebugInfo.100 enabled, check that store/load is still
  1138. // optimized, DebugValue placed after the store and the associated
  1139. // DebugValue Deref is removed.
  1140. const std::string text = R"(
  1141. OpCapability Shader
  1142. %1 = OpExtInstImport "OpenCL.DebugInfo.100"
  1143. OpMemoryModel Logical GLSL450
  1144. OpEntryPoint Fragment %MainPs "MainPs" %g_tColor %g_sAniso %in_var_TEXCOORD2 %out_var_SV_Target0
  1145. OpExecutionMode %MainPs OriginUpperLeft
  1146. %7 = OpString "foo.frag"
  1147. %8 = OpString "PS_OUTPUT"
  1148. %9 = OpString "float"
  1149. %10 = OpString "vColor"
  1150. %11 = OpString "PS_INPUT"
  1151. %12 = OpString "vTextureCoords"
  1152. %13 = OpString "@type.2d.image"
  1153. %14 = OpString "type.2d.image"
  1154. %15 = OpString "Texture2D.TemplateParam"
  1155. %16 = OpString "src.MainPs"
  1156. %17 = OpString "tc"
  1157. %18 = OpString "ps_output"
  1158. %19 = OpString "i"
  1159. %20 = OpString "@type.sampler"
  1160. %21 = OpString "type.sampler"
  1161. %22 = OpString "g_sAniso"
  1162. %23 = OpString "g_tColor"
  1163. OpName %type_2d_image "type.2d.image"
  1164. OpName %g_tColor "g_tColor"
  1165. OpName %type_sampler "type.sampler"
  1166. OpName %g_sAniso "g_sAniso"
  1167. OpName %in_var_TEXCOORD2 "in.var.TEXCOORD2"
  1168. OpName %out_var_SV_Target0 "out.var.SV_Target0"
  1169. OpName %MainPs "MainPs"
  1170. OpName %PS_INPUT "PS_INPUT"
  1171. OpMemberName %PS_INPUT 0 "vTextureCoords"
  1172. OpName %param_var_i "param.var.i"
  1173. OpName %PS_OUTPUT "PS_OUTPUT"
  1174. OpMemberName %PS_OUTPUT 0 "vColor"
  1175. OpName %type_sampled_image "type.sampled.image"
  1176. OpDecorate %in_var_TEXCOORD2 Location 0
  1177. OpDecorate %out_var_SV_Target0 Location 0
  1178. OpDecorate %g_tColor DescriptorSet 0
  1179. OpDecorate %g_tColor Binding 0
  1180. OpDecorate %g_sAniso DescriptorSet 0
  1181. OpDecorate %g_sAniso Binding 1
  1182. %int = OpTypeInt 32 1
  1183. %int_0 = OpConstant %int 0
  1184. %uint = OpTypeInt 32 0
  1185. %uint_32 = OpConstant %uint 32
  1186. %float = OpTypeFloat 32
  1187. %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
  1188. %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
  1189. %type_sampler = OpTypeSampler
  1190. %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
  1191. %v2float = OpTypeVector %float 2
  1192. %_ptr_Input_v2float = OpTypePointer Input %v2float
  1193. %v4float = OpTypeVector %float 4
  1194. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1195. %void = OpTypeVoid
  1196. %uint_128 = OpConstant %uint 128
  1197. %uint_0 = OpConstant %uint 0
  1198. %uint_64 = OpConstant %uint 64
  1199. %45 = OpTypeFunction %void
  1200. %PS_INPUT = OpTypeStruct %v2float
  1201. %_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
  1202. %PS_OUTPUT = OpTypeStruct %v4float
  1203. %47 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
  1204. %_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
  1205. %_ptr_Function_v2float = OpTypePointer Function %v2float
  1206. %type_sampled_image = OpTypeSampledImage %type_2d_image
  1207. %_ptr_Function_v4float = OpTypePointer Function %v4float
  1208. %g_tColor = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
  1209. %g_sAniso = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
  1210. %in_var_TEXCOORD2 = OpVariable %_ptr_Input_v2float Input
  1211. %out_var_SV_Target0 = OpVariable %_ptr_Output_v4float Output
  1212. %51 = OpExtInst %void %1 DebugInfoNone
  1213. %52 = OpExtInst %void %1 DebugExpression
  1214. %53 = OpExtInst %void %1 DebugOperation Deref
  1215. %54 = OpExtInst %void %1 DebugExpression %53
  1216. %55 = OpExtInst %void %1 DebugSource %7
  1217. %56 = OpExtInst %void %1 DebugCompilationUnit 1 4 %55 HLSL
  1218. %57 = OpExtInst %void %1 DebugTypeComposite %8 Structure %55 10 1 %56 %8 %uint_128 FlagIsProtected|FlagIsPrivate %58
  1219. %59 = OpExtInst %void %1 DebugTypeBasic %9 %uint_32 Float
  1220. %60 = OpExtInst %void %1 DebugTypeVector %59 4
  1221. %58 = OpExtInst %void %1 DebugTypeMember %10 %60 %55 12 5 %57 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
  1222. %61 = OpExtInst %void %1 DebugTypeComposite %11 Structure %55 5 1 %56 %11 %uint_64 FlagIsProtected|FlagIsPrivate %62
  1223. %63 = OpExtInst %void %1 DebugTypeVector %59 2
  1224. %62 = OpExtInst %void %1 DebugTypeMember %12 %63 %55 7 5 %61 %uint_0 %uint_64 FlagIsProtected|FlagIsPrivate
  1225. %64 = OpExtInst %void %1 DebugTypeComposite %13 Class %55 0 0 %56 %14 %51 FlagIsProtected|FlagIsPrivate
  1226. %65 = OpExtInst %void %1 DebugTypeTemplateParameter %15 %59 %51 %55 0 0
  1227. %66 = OpExtInst %void %1 DebugTypeTemplate %64 %65
  1228. %67 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %57 %61
  1229. %68 = OpExtInst %void %1 DebugFunction %16 %67 %55 15 1 %56 %16 FlagIsProtected|FlagIsPrivate 16 %51
  1230. %69 = OpExtInst %void %1 DebugLexicalBlock %55 16 1 %68
  1231. %70 = OpExtInst %void %1 DebugLocalVariable %17 %63 %55 19 12 %69 FlagIsLocal
  1232. %71 = OpExtInst %void %1 DebugLocalVariable %18 %57 %55 17 15 %69 FlagIsLocal
  1233. %72 = OpExtInst %void %1 DebugLocalVariable %19 %61 %55 15 29 %68 FlagIsLocal 1
  1234. %73 = OpExtInst %void %1 DebugTypeComposite %20 Structure %55 0 0 %56 %21 %51 FlagIsProtected|FlagIsPrivate
  1235. %74 = OpExtInst %void %1 DebugGlobalVariable %22 %73 %55 3 14 %56 %22 %g_sAniso FlagIsDefinition
  1236. %75 = OpExtInst %void %1 DebugGlobalVariable %23 %64 %55 1 11 %56 %23 %g_tColor FlagIsDefinition
  1237. %MainPs = OpFunction %void None %45
  1238. %76 = OpLabel
  1239. %101 = OpExtInst %void %1 DebugScope %69
  1240. %78 = OpVariable %_ptr_Function_PS_OUTPUT Function
  1241. %79 = OpVariable %_ptr_Function_v2float Function
  1242. %102 = OpExtInst %void %1 DebugNoScope
  1243. %81 = OpVariable %_ptr_Function_PS_OUTPUT Function
  1244. %param_var_i = OpVariable %_ptr_Function_PS_INPUT Function
  1245. %82 = OpLoad %v2float %in_var_TEXCOORD2
  1246. %83 = OpCompositeConstruct %PS_INPUT %82
  1247. OpStore %param_var_i %83
  1248. %103 = OpExtInst %void %1 DebugScope %68
  1249. %85 = OpExtInst %void %1 DebugDeclare %72 %param_var_i %52
  1250. %104 = OpExtInst %void %1 DebugScope %69
  1251. %87 = OpExtInst %void %1 DebugDeclare %71 %78 %52
  1252. OpLine %7 19 17
  1253. %88 = OpAccessChain %_ptr_Function_v2float %param_var_i %int_0
  1254. %89 = OpLoad %v2float %88
  1255. OpLine %7 19 12
  1256. OpStore %79 %89
  1257. %90 = OpExtInst %void %1 DebugValue %70 %79 %54
  1258. ;CHECK-NOT: %90 = OpExtInst %void %1 DebugValue %70 %79 %54
  1259. ;CHECK: %106 = OpExtInst %void %1 DebugValue %70 %89 %52
  1260. OpLine %7 20 26
  1261. %91 = OpLoad %type_2d_image %g_tColor
  1262. OpLine %7 20 46
  1263. %92 = OpLoad %type_sampler %g_sAniso
  1264. OpLine %7 20 57
  1265. %93 = OpLoad %v2float %79
  1266. ;CHECK-NOT: %93 = OpLoad %v2float %79
  1267. OpLine %7 20 26
  1268. %94 = OpSampledImage %type_sampled_image %91 %92
  1269. %95 = OpImageSampleImplicitLod %v4float %94 %93 None
  1270. ;CHECK-NOT: %95 = OpImageSampleImplicitLod %v4float %94 %93 None
  1271. ;CHECK: %95 = OpImageSampleImplicitLod %v4float %94 %89 None
  1272. OpLine %7 20 5
  1273. %96 = OpAccessChain %_ptr_Function_v4float %78 %int_0
  1274. OpStore %96 %95
  1275. OpLine %7 21 12
  1276. %97 = OpLoad %PS_OUTPUT %78
  1277. OpLine %7 21 5
  1278. OpStore %81 %97
  1279. %105 = OpExtInst %void %1 DebugNoScope
  1280. %99 = OpLoad %PS_OUTPUT %81
  1281. %100 = OpCompositeExtract %v4float %99 0
  1282. OpStore %out_var_SV_Target0 %100
  1283. OpReturn
  1284. OpFunctionEnd
  1285. )";
  1286. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1287. SinglePassRunAndMatch<LocalSingleStoreElimPass>(text, false);
  1288. }
  1289. TEST_F(LocalSingleStoreElimTest, UseStoreLineInfoForDebugValueLine) {
  1290. // When the store is in the scope of OpenCL.DebugInfo.100 DebugDeclare,
  1291. // the OpLine of the added OpenCL.DebugInfo.100 DebugValue must be the
  1292. // same with the OpLine of the store.
  1293. const std::string text = R"(
  1294. OpCapability Shader
  1295. %1 = OpExtInstImport "OpenCL.DebugInfo.100"
  1296. OpMemoryModel Logical GLSL450
  1297. OpEntryPoint Vertex %main "main" %in_var_POSITION %in_var_COLOR %gl_Position %out_var_COLOR
  1298. %7 = OpString "simple.hlsl"
  1299. %8 = OpString "float"
  1300. %9 = OpString "VS_OUTPUT"
  1301. %10 = OpString "color"
  1302. %11 = OpString "pos"
  1303. %12 = OpString "main"
  1304. %13 = OpString ""
  1305. %14 = OpString "vout"
  1306. OpName %in_var_POSITION "in.var.POSITION"
  1307. OpName %in_var_COLOR "in.var.COLOR"
  1308. OpName %out_var_COLOR "out.var.COLOR"
  1309. OpName %main "main"
  1310. OpName %VS_OUTPUT "VS_OUTPUT"
  1311. OpMemberName %VS_OUTPUT 0 "pos"
  1312. OpMemberName %VS_OUTPUT 1 "color"
  1313. OpDecorate %gl_Position BuiltIn Position
  1314. OpDecorate %in_var_POSITION Location 0
  1315. OpDecorate %in_var_COLOR Location 1
  1316. OpDecorate %out_var_COLOR Location 0
  1317. %int = OpTypeInt 32 1
  1318. %int_0 = OpConstant %int 0
  1319. %int_1 = OpConstant %int 1
  1320. %uint = OpTypeInt 32 0
  1321. %uint_32 = OpConstant %uint 32
  1322. %float = OpTypeFloat 32
  1323. %v4float = OpTypeVector %float 4
  1324. %_ptr_Input_v4float = OpTypePointer Input %v4float
  1325. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1326. %void = OpTypeVoid
  1327. %uint_256 = OpConstant %uint 256
  1328. %uint_128 = OpConstant %uint 128
  1329. %uint_0 = OpConstant %uint 0
  1330. %36 = OpTypeFunction %void
  1331. %_ptr_Function_v4float = OpTypePointer Function %v4float
  1332. %VS_OUTPUT = OpTypeStruct %v4float %v4float
  1333. %_ptr_Function_VS_OUTPUT = OpTypePointer Function %VS_OUTPUT
  1334. %in_var_POSITION = OpVariable %_ptr_Input_v4float Input
  1335. %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
  1336. %gl_Position = OpVariable %_ptr_Output_v4float Output
  1337. %out_var_COLOR = OpVariable %_ptr_Output_v4float Output
  1338. %85 = OpExtInst %void %1 DebugOperation Deref
  1339. %81 = OpExtInst %void %1 DebugInfoNone
  1340. %52 = OpExtInst %void %1 DebugExpression
  1341. %40 = OpExtInst %void %1 DebugTypeBasic %8 %uint_32 Float
  1342. %41 = OpExtInst %void %1 DebugTypeVector %40 4
  1343. %42 = OpExtInst %void %1 DebugSource %7
  1344. %43 = OpExtInst %void %1 DebugCompilationUnit 1 4 %42 HLSL
  1345. %44 = OpExtInst %void %1 DebugTypeComposite %9 Structure %42 1 8 %43 %9 %uint_256 FlagIsProtected|FlagIsPrivate %45 %46
  1346. %46 = OpExtInst %void %1 DebugTypeMember %10 %41 %42 3 10 %44 %uint_128 %uint_128 FlagIsProtected|FlagIsPrivate
  1347. %45 = OpExtInst %void %1 DebugTypeMember %11 %41 %42 2 10 %44 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
  1348. %47 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %44 %41 %41
  1349. %48 = OpExtInst %void %1 DebugFunction %12 %47 %42 6 1 %43 %13 FlagIsProtected|FlagIsPrivate 7 %81
  1350. %49 = OpExtInst %void %1 DebugLexicalBlock %42 7 38 %48
  1351. %50 = OpExtInst %void %1 DebugLocalVariable %14 %44 %42 8 13 %49 FlagIsLocal
  1352. %84 = OpExtInst %void %1 DebugExpression %85
  1353. %main = OpFunction %void None %36
  1354. %54 = OpLabel
  1355. %91 = OpExtInst %void %1 DebugScope %49
  1356. OpLine %7 7 23
  1357. %83 = OpVariable %_ptr_Function_v4float Function
  1358. OpLine %7 8 13
  1359. %87 = OpExtInst %void %1 DebugValue %50 %83 %84 %int_1
  1360. OpLine %7 7 23
  1361. %82 = OpVariable %_ptr_Function_v4float Function
  1362. OpLine %7 8 13
  1363. %86 = OpExtInst %void %1 DebugValue %50 %82 %84 %int_0
  1364. OpNoLine
  1365. %92 = OpExtInst %void %1 DebugNoScope
  1366. %55 = OpLoad %v4float %in_var_POSITION
  1367. %56 = OpLoad %v4float %in_var_COLOR
  1368. ;CHECK: [[pos:%\w+]] = OpLoad %v4float %in_var_POSITION
  1369. ;CHECK: [[color:%\w+]] = OpLoad %v4float %in_var_COLOR
  1370. %94 = OpExtInst %void %1 DebugScope %49
  1371. OpLine %7 9 3
  1372. OpStore %82 %55
  1373. ;CHECK: OpLine [[file:%\w+]] 9 3
  1374. ;CHECK: OpStore {{%\w+}} [[pos]]
  1375. ;CHECK: {{%\w+}} = OpExtInst %void {{%\w+}} DebugValue [[vout:%\w+]] [[pos]] [[empty_expr:%\w+]] %int_0
  1376. ;CHECK: OpLine [[file]] 10 3
  1377. ;CHECK: OpStore {{%\w+}} [[color]]
  1378. ;CHECK: {{%\w+}} = OpExtInst %void {{%\w+}} DebugValue [[vout]] [[color]] [[empty_expr]] %int_1
  1379. OpLine %7 10 3
  1380. OpStore %83 %56
  1381. OpLine %7 11 10
  1382. %90 = OpCompositeConstruct %VS_OUTPUT %55 %56
  1383. OpNoLine
  1384. %95 = OpExtInst %void %1 DebugNoScope
  1385. %58 = OpCompositeExtract %v4float %90 0
  1386. OpStore %gl_Position %58
  1387. %59 = OpCompositeExtract %v4float %90 1
  1388. OpStore %out_var_COLOR %59
  1389. OpReturn
  1390. OpFunctionEnd
  1391. )";
  1392. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1393. SinglePassRunAndMatch<LocalSingleStoreElimPass>(text, false);
  1394. }
  1395. TEST_F(LocalSingleStoreElimTest, AddDebugValueforStoreOutOfDebugDeclareScope) {
  1396. const std::string text = R"(
  1397. OpCapability Shader
  1398. %1 = OpExtInstImport "OpenCL.DebugInfo.100"
  1399. OpMemoryModel Logical GLSL450
  1400. OpEntryPoint Vertex %main "main" %in_var_POSITION %in_var_COLOR %gl_Position %out_var_COLOR
  1401. %7 = OpString "simple.hlsl"
  1402. %8 = OpString "float"
  1403. %9 = OpString "VS_OUTPUT"
  1404. %10 = OpString "color"
  1405. %11 = OpString "pos"
  1406. %12 = OpString "main"
  1407. %13 = OpString ""
  1408. %14 = OpString "vout"
  1409. OpName %in_var_POSITION "in.var.POSITION"
  1410. OpName %in_var_COLOR "in.var.COLOR"
  1411. OpName %out_var_COLOR "out.var.COLOR"
  1412. OpName %main "main"
  1413. OpName %VS_OUTPUT "VS_OUTPUT"
  1414. OpMemberName %VS_OUTPUT 0 "pos"
  1415. OpMemberName %VS_OUTPUT 1 "color"
  1416. OpDecorate %gl_Position BuiltIn Position
  1417. OpDecorate %in_var_POSITION Location 0
  1418. OpDecorate %in_var_COLOR Location 1
  1419. OpDecorate %out_var_COLOR Location 0
  1420. %int = OpTypeInt 32 1
  1421. %int_0 = OpConstant %int 0
  1422. %int_1 = OpConstant %int 1
  1423. %uint = OpTypeInt 32 0
  1424. %uint_32 = OpConstant %uint 32
  1425. %float = OpTypeFloat 32
  1426. %v4float = OpTypeVector %float 4
  1427. %_ptr_Input_v4float = OpTypePointer Input %v4float
  1428. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1429. %void = OpTypeVoid
  1430. %uint_256 = OpConstant %uint 256
  1431. %uint_128 = OpConstant %uint 128
  1432. %uint_0 = OpConstant %uint 0
  1433. %36 = OpTypeFunction %void
  1434. %_ptr_Function_v4float = OpTypePointer Function %v4float
  1435. %VS_OUTPUT = OpTypeStruct %v4float %v4float
  1436. %_ptr_Function_VS_OUTPUT = OpTypePointer Function %VS_OUTPUT
  1437. %in_var_POSITION = OpVariable %_ptr_Input_v4float Input
  1438. %in_var_COLOR = OpVariable %_ptr_Input_v4float Input
  1439. %gl_Position = OpVariable %_ptr_Output_v4float Output
  1440. %out_var_COLOR = OpVariable %_ptr_Output_v4float Output
  1441. %85 = OpExtInst %void %1 DebugOperation Deref
  1442. %81 = OpExtInst %void %1 DebugInfoNone
  1443. %52 = OpExtInst %void %1 DebugExpression
  1444. %40 = OpExtInst %void %1 DebugTypeBasic %8 %uint_32 Float
  1445. %41 = OpExtInst %void %1 DebugTypeVector %40 4
  1446. %42 = OpExtInst %void %1 DebugSource %7
  1447. %43 = OpExtInst %void %1 DebugCompilationUnit 1 4 %42 HLSL
  1448. %44 = OpExtInst %void %1 DebugTypeComposite %9 Structure %42 1 8 %43 %9 %uint_256 FlagIsProtected|FlagIsPrivate %45 %46
  1449. %46 = OpExtInst %void %1 DebugTypeMember %10 %41 %42 3 10 %44 %uint_128 %uint_128 FlagIsProtected|FlagIsPrivate
  1450. %45 = OpExtInst %void %1 DebugTypeMember %11 %41 %42 2 10 %44 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
  1451. %47 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %44 %41 %41
  1452. %48 = OpExtInst %void %1 DebugFunction %12 %47 %42 6 1 %43 %13 FlagIsProtected|FlagIsPrivate 7 %81
  1453. %49 = OpExtInst %void %1 DebugLexicalBlock %42 7 38 %48
  1454. %50 = OpExtInst %void %1 DebugLocalVariable %14 %44 %42 8 13 %49 FlagIsLocal
  1455. %51 = OpExtInst %void %1 DebugLocalVariable %10 %41 %42 7 23 %48 FlagIsLocal 2
  1456. %53 = OpExtInst %void %1 DebugLocalVariable %11 %41 %42 6 23 %48 FlagIsLocal 1
  1457. ;CHECK: [[dbg_color:%\w+]] = OpExtInst %void {{%\w+}} DebugLocalVariable {{%\w+}} {{%\w+}} {{%\w+}} 7 23 {{%\w+}} FlagIsLocal 2
  1458. ;CHECK: [[dbg_pos:%\w+]] = OpExtInst %void {{%\w+}} DebugLocalVariable {{%\w+}} {{%\w+}} {{%\w+}} 6 23 {{%\w+}} FlagIsLocal 1
  1459. %84 = OpExtInst %void %1 DebugExpression %85
  1460. %main = OpFunction %void None %36
  1461. %54 = OpLabel
  1462. %91 = OpExtInst %void %1 DebugScope %49
  1463. OpLine %7 7 23
  1464. %83 = OpVariable %_ptr_Function_v4float Function
  1465. OpLine %7 8 13
  1466. %87 = OpExtInst %void %1 DebugValue %50 %83 %84 %int_1
  1467. OpLine %7 7 23
  1468. %82 = OpVariable %_ptr_Function_v4float Function
  1469. OpLine %7 8 13
  1470. %86 = OpExtInst %void %1 DebugValue %50 %82 %84 %int_0
  1471. OpNoLine
  1472. %92 = OpExtInst %void %1 DebugNoScope
  1473. %param_var_pos = OpVariable %_ptr_Function_v4float Function
  1474. %param_var_color = OpVariable %_ptr_Function_v4float Function
  1475. %55 = OpLoad %v4float %in_var_POSITION
  1476. OpLine %7 6 23
  1477. OpStore %param_var_pos %55
  1478. OpNoLine
  1479. %56 = OpLoad %v4float %in_var_COLOR
  1480. ;CHECK: DebugNoScope
  1481. ;CHECK-NOT: OpLine
  1482. OpLine %7 7 23
  1483. OpStore %param_var_color %56
  1484. OpNoLine
  1485. %93 = OpExtInst %void %1 DebugScope %48
  1486. %73 = OpExtInst %void %1 DebugDeclare %53 %param_var_pos %52
  1487. %74 = OpExtInst %void %1 DebugDeclare %51 %param_var_color %52
  1488. ;CHECK: [[pos:%\w+]] = OpLoad %v4float %in_var_POSITION
  1489. ;CHECK: OpLine [[file:%\w+]] 6 23
  1490. ;CHECK: {{%\w+}} = OpExtInst %void {{%\w+}} DebugValue [[dbg_pos]] [[pos]] [[empty_expr:%\w+]]
  1491. ;CHECK: [[color:%\w+]] = OpLoad %v4float %in_var_COLOR
  1492. ;CHECK: OpLine [[file]] 7 23
  1493. ;CHECK: {{%\w+}} = OpExtInst %void {{%\w+}} DebugValue [[dbg_color]] [[color]] [[empty_expr]]
  1494. ;CHECK: OpLine [[file]] 9 3
  1495. %94 = OpExtInst %void %1 DebugScope %49
  1496. OpLine %7 9 3
  1497. OpStore %82 %55
  1498. OpLine %7 10 3
  1499. OpStore %83 %56
  1500. OpLine %7 11 10
  1501. %90 = OpCompositeConstruct %VS_OUTPUT %55 %56
  1502. OpNoLine
  1503. %95 = OpExtInst %void %1 DebugNoScope
  1504. %58 = OpCompositeExtract %v4float %90 0
  1505. OpStore %gl_Position %58
  1506. %59 = OpCompositeExtract %v4float %90 1
  1507. OpStore %out_var_COLOR %59
  1508. OpReturn
  1509. OpFunctionEnd
  1510. )";
  1511. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1512. SinglePassRunAndMatch<LocalSingleStoreElimPass>(text, false);
  1513. }
  1514. TEST_F(LocalSingleStoreElimTest, DebugValuesForAllLocalsAndParams) {
  1515. // Texture2D g_tColor;
  1516. //
  1517. // SamplerState g_sAniso;
  1518. //
  1519. // struct PS_INPUT
  1520. // {
  1521. // float2 vTextureCoords : TEXCOORD2 ;
  1522. // } ;
  1523. //
  1524. // struct PS_OUTPUT
  1525. // {
  1526. // float4 vColor : SV_Target0 ;
  1527. // } ;
  1528. //
  1529. // void do_sample ( in float2 tc, out float4 c ) {
  1530. // c = g_tColor . Sample ( g_sAniso , tc ) ;
  1531. // }
  1532. //
  1533. // PS_OUTPUT MainPs ( PS_INPUT i )
  1534. // {
  1535. // PS_OUTPUT ps_output ;
  1536. // float4 color;
  1537. //
  1538. // do_sample ( i . vTextureCoords . xy , color ) ;
  1539. // ps_output . vColor = color;
  1540. // return ps_output ;
  1541. // }
  1542. const std::string text = R"(
  1543. OpCapability Shader
  1544. %1 = OpExtInstImport "OpenCL.DebugInfo.100"
  1545. ;CHECK: [[set:%\w+]] = OpExtInstImport "OpenCL.DebugInfo.100"
  1546. OpMemoryModel Logical GLSL450
  1547. OpEntryPoint Fragment %MainPs "MainPs" %in_var_TEXCOORD2 %out_var_SV_Target0 %g_tColor %g_sAniso
  1548. OpExecutionMode %MainPs OriginUpperLeft
  1549. %7 = OpString "foo2.frag"
  1550. %21 = OpString "float"
  1551. %27 = OpString "PS_INPUT"
  1552. %31 = OpString "vTextureCoords"
  1553. %34 = OpString "PS_OUTPUT"
  1554. %38 = OpString "vColor"
  1555. %40 = OpString "do_sample"
  1556. %41 = OpString ""
  1557. %45 = OpString "c"
  1558. %47 = OpString "tc"
  1559. %50 = OpString "MainPs"
  1560. %54 = OpString "color"
  1561. %56 = OpString "ps_output"
  1562. %59 = OpString "i"
  1563. %62 = OpString "@type.sampler"
  1564. %63 = OpString "type.sampler"
  1565. %65 = OpString "g_sAniso"
  1566. %67 = OpString "@type.2d.image"
  1567. %68 = OpString "type.2d.image"
  1568. %70 = OpString "TemplateParam"
  1569. %73 = OpString "g_tColor"
  1570. ;CHECK: [[str_c:%\w+]] = OpString "c"
  1571. ;CHECK: [[str_tc:%\w+]] = OpString "tc"
  1572. ;CHECK: [[str_color:%\w+]] = OpString "color"
  1573. ;CHECK: [[str_ps_output:%\w+]] = OpString "ps_output"
  1574. ;CHECK: [[str_i:%\w+]] = OpString "i"
  1575. OpName %type_2d_image "type.2d.image"
  1576. OpName %g_tColor "g_tColor"
  1577. OpName %type_sampler "type.sampler"
  1578. OpName %g_sAniso "g_sAniso"
  1579. OpName %in_var_TEXCOORD2 "in.var.TEXCOORD2"
  1580. OpName %out_var_SV_Target0 "out.var.SV_Target0"
  1581. OpName %MainPs "MainPs"
  1582. OpName %PS_INPUT "PS_INPUT"
  1583. OpMemberName %PS_INPUT 0 "vTextureCoords"
  1584. OpName %PS_OUTPUT "PS_OUTPUT"
  1585. OpMemberName %PS_OUTPUT 0 "vColor"
  1586. OpName %type_sampled_image "type.sampled.image"
  1587. OpDecorate %in_var_TEXCOORD2 Location 0
  1588. OpDecorate %out_var_SV_Target0 Location 0
  1589. OpDecorate %g_tColor DescriptorSet 0
  1590. OpDecorate %g_tColor Binding 0
  1591. OpDecorate %g_sAniso DescriptorSet 0
  1592. OpDecorate %g_sAniso Binding 1
  1593. %int = OpTypeInt 32 1
  1594. %int_0 = OpConstant %int 0
  1595. %uint = OpTypeInt 32 0
  1596. %uint_32 = OpConstant %uint 32
  1597. %float = OpTypeFloat 32
  1598. %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
  1599. %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
  1600. %type_sampler = OpTypeSampler
  1601. %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
  1602. %v2float = OpTypeVector %float 2
  1603. %_ptr_Input_v2float = OpTypePointer Input %v2float
  1604. %v4float = OpTypeVector %float 4
  1605. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1606. %void = OpTypeVoid
  1607. %uint_64 = OpConstant %uint 64
  1608. %uint_0 = OpConstant %uint 0
  1609. %uint_128 = OpConstant %uint 128
  1610. %75 = OpTypeFunction %void
  1611. %PS_INPUT = OpTypeStruct %v2float
  1612. %_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
  1613. %PS_OUTPUT = OpTypeStruct %v4float
  1614. %85 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
  1615. %_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
  1616. %_ptr_Function_v4float = OpTypePointer Function %v4float
  1617. %_ptr_Function_v2float = OpTypePointer Function %v2float
  1618. %105 = OpTypeFunction %void %_ptr_Function_v2float %_ptr_Function_v4float
  1619. %type_sampled_image = OpTypeSampledImage %type_2d_image
  1620. %g_tColor = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
  1621. %g_sAniso = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
  1622. %in_var_TEXCOORD2 = OpVariable %_ptr_Input_v2float Input
  1623. %out_var_SV_Target0 = OpVariable %_ptr_Output_v4float Output
  1624. %145 = OpExtInst %void %1 DebugOperation Deref
  1625. %61 = OpExtInst %void %1 DebugInfoNone
  1626. %58 = OpExtInst %void %1 DebugExpression
  1627. %23 = OpExtInst %void %1 DebugTypeBasic %21 %uint_32 Float
  1628. %24 = OpExtInst %void %1 DebugTypeVector %23 2
  1629. %25 = OpExtInst %void %1 DebugSource %7
  1630. %26 = OpExtInst %void %1 DebugCompilationUnit 1 4 %25 HLSL
  1631. %29 = OpExtInst %void %1 DebugTypeComposite %27 Structure %25 5 8 %26 %27 %uint_64 FlagIsProtected|FlagIsPrivate %30
  1632. %30 = OpExtInst %void %1 DebugTypeMember %31 %24 %25 7 12 %29 %uint_0 %uint_64 FlagIsProtected|FlagIsPrivate
  1633. %33 = OpExtInst %void %1 DebugTypeVector %23 4
  1634. %36 = OpExtInst %void %1 DebugTypeComposite %34 Structure %25 10 8 %26 %34 %uint_128 FlagIsProtected|FlagIsPrivate %37
  1635. %37 = OpExtInst %void %1 DebugTypeMember %38 %33 %25 12 12 %36 %uint_0 %uint_128 FlagIsProtected|FlagIsPrivate
  1636. %39 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %void %24 %33
  1637. %42 = OpExtInst %void %1 DebugFunction %40 %39 %25 15 1 %26 %41 FlagIsProtected|FlagIsPrivate 15 %61
  1638. %44 = OpExtInst %void %1 DebugLexicalBlock %25 15 47 %42
  1639. %46 = OpExtInst %void %1 DebugLocalVariable %45 %33 %25 15 43 %42 FlagIsLocal 2
  1640. %48 = OpExtInst %void %1 DebugLocalVariable %47 %24 %25 15 28 %42 FlagIsLocal 1
  1641. %49 = OpExtInst %void %1 DebugTypeFunction FlagIsProtected|FlagIsPrivate %36 %29
  1642. %51 = OpExtInst %void %1 DebugFunction %50 %49 %25 19 1 %26 %41 FlagIsProtected|FlagIsPrivate 20 %61
  1643. %53 = OpExtInst %void %1 DebugLexicalBlock %25 20 1 %51
  1644. %55 = OpExtInst %void %1 DebugLocalVariable %54 %33 %25 22 12 %53 FlagIsLocal
  1645. %57 = OpExtInst %void %1 DebugLocalVariable %56 %36 %25 21 15 %53 FlagIsLocal
  1646. %60 = OpExtInst %void %1 DebugLocalVariable %59 %29 %25 19 29 %51 FlagIsLocal 1
  1647. %64 = OpExtInst %void %1 DebugTypeComposite %62 Structure %25 0 0 %26 %63 %61 FlagIsProtected|FlagIsPrivate
  1648. %66 = OpExtInst %void %1 DebugGlobalVariable %65 %64 %25 3 14 %26 %65 %g_sAniso FlagIsDefinition
  1649. %69 = OpExtInst %void %1 DebugTypeComposite %67 Class %25 0 0 %26 %68 %61 FlagIsProtected|FlagIsPrivate
  1650. %71 = OpExtInst %void %1 DebugTypeTemplateParameter %70 %33 %61 %25 0 0
  1651. %72 = OpExtInst %void %1 DebugTypeTemplate %69 %71
  1652. %74 = OpExtInst %void %1 DebugGlobalVariable %73 %72 %25 1 11 %26 %73 %g_tColor FlagIsDefinition
  1653. %142 = OpExtInst %void %1 DebugInlinedAt 24 %53
  1654. %144 = OpExtInst %void %1 DebugExpression %145
  1655. %155 = OpExtInst %void %1 DebugExpression %145
  1656. ;CHECK: [[var_c:%\w+]] = OpExtInst %void [[set]] DebugLocalVariable [[str_c]]
  1657. ;CHECK: [[var_tc:%\w+]] = OpExtInst %void [[set]] DebugLocalVariable [[str_tc]]
  1658. ;CHECK: [[var_color:%\w+]] = OpExtInst %void [[set]] DebugLocalVariable [[str_color]]
  1659. ;CHECK: [[var_ps_output:%\w+]] = OpExtInst %void [[set]] DebugLocalVariable [[str_ps_output]]
  1660. ;CHECK: [[var_i:%\w+]] = OpExtInst %void [[set]] DebugLocalVariable [[str_i]]
  1661. %MainPs = OpFunction %void None %75
  1662. %76 = OpLabel
  1663. %153 = OpVariable %_ptr_Function_v2float Function
  1664. %149 = OpVariable %_ptr_Function_v4float Function
  1665. %157 = OpExtInst %void %1 DebugScope %53
  1666. %143 = OpVariable %_ptr_Function_v4float Function
  1667. %121 = OpVariable %_ptr_Function_v4float Function
  1668. %122 = OpVariable %_ptr_Function_v2float Function
  1669. %158 = OpExtInst %void %1 DebugScope %51
  1670. OpLine %7 19 29
  1671. %156 = OpExtInst %void %1 DebugValue %60 %153 %155 %int_0
  1672. %159 = OpExtInst %void %1 DebugScope %53
  1673. OpLine %7 21 15
  1674. %146 = OpExtInst %void %1 DebugValue %57 %143 %144 %int_0
  1675. OpNoLine
  1676. %160 = OpExtInst %void %1 DebugNoScope
  1677. %80 = OpLoad %v2float %in_var_TEXCOORD2
  1678. %81 = OpCompositeConstruct %PS_INPUT %80
  1679. %154 = OpCompositeExtract %v2float %81 0
  1680. OpStore %153 %154
  1681. %161 = OpExtInst %void %1 DebugScope %53
  1682. OpLine %7 22 12
  1683. %127 = OpExtInst %void %1 DebugDeclare %55 %121 %58
  1684. OpLine %7 24 17
  1685. %129 = OpLoad %v2float %153
  1686. OpStore %122 %129
  1687. %162 = OpExtInst %void %1 DebugScope %42 %142
  1688. OpLine %7 15 28
  1689. %135 = OpExtInst %void %1 DebugDeclare %48 %122 %58
  1690. OpLine %7 15 43
  1691. %136 = OpExtInst %void %1 DebugDeclare %46 %121 %58
  1692. %163 = OpExtInst %void %1 DebugScope %44 %142
  1693. OpLine %7 16 9
  1694. %137 = OpLoad %type_2d_image %g_tColor
  1695. OpLine %7 16 29
  1696. %138 = OpLoad %type_sampler %g_sAniso
  1697. OpLine %7 16 40
  1698. %139 = OpLoad %v2float %122
  1699. OpLine %7 16 9
  1700. %140 = OpSampledImage %type_sampled_image %137 %138
  1701. %141 = OpImageSampleImplicitLod %v4float %140 %139 None
  1702. OpLine %7 16 5
  1703. OpStore %121 %141
  1704. %164 = OpExtInst %void %1 DebugScope %53
  1705. OpLine %7 25 26
  1706. %131 = OpLoad %v4float %121
  1707. OpLine %7 25 5
  1708. OpStore %143 %131
  1709. OpLine %7 26 12
  1710. %147 = OpLoad %v4float %143
  1711. %148 = OpCompositeConstruct %PS_OUTPUT %147
  1712. OpLine %7 26 5
  1713. %150 = OpCompositeExtract %v4float %148 0
  1714. OpStore %149 %150
  1715. OpNoLine
  1716. %165 = OpExtInst %void %1 DebugNoScope
  1717. %151 = OpLoad %v4float %149
  1718. %152 = OpCompositeConstruct %PS_OUTPUT %151
  1719. %84 = OpCompositeExtract %v4float %152 0
  1720. OpStore %out_var_SV_Target0 %84
  1721. OpLine %7 27 1
  1722. OpReturn
  1723. OpFunctionEnd
  1724. ;CHECK: {{%\w+}} = OpExtInst %void [[set]] DebugValue [[var_i]]
  1725. ;CHECK: {{%\w+}} = OpExtInst %void [[set]] DebugValue [[var_tc]]
  1726. ;CHECK: {{%\w+}} = OpExtInst %void [[set]] DebugValue [[var_c]]
  1727. ;CHECK: {{%\w+}} = OpExtInst %void [[set]] DebugValue [[var_color]]
  1728. ;CHECK: {{%\w+}} = OpExtInst %void [[set]] DebugValue [[var_ps_output]]
  1729. )";
  1730. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1731. SinglePassRunAndMatch<LocalSingleStoreElimPass>(text, false);
  1732. }
  1733. TEST_F(LocalSingleStoreElimTest, VkMemoryModelTest) {
  1734. const std::string text =
  1735. R"(
  1736. ; CHECK: OpCapability Shader
  1737. ; CHECK: OpCapability VulkanMemoryModel
  1738. ; CHECK: OpExtension "SPV_KHR_vulkan_memory_model"
  1739. OpCapability Shader
  1740. OpCapability VulkanMemoryModel
  1741. OpExtension "SPV_KHR_vulkan_memory_model"
  1742. %1 = OpExtInstImport "GLSL.std.450"
  1743. OpMemoryModel Logical Vulkan
  1744. OpEntryPoint GLCompute %main "main"
  1745. OpExecutionMode %main LocalSize 1 1 1
  1746. OpSource GLSL 450
  1747. %void = OpTypeVoid
  1748. %3 = OpTypeFunction %void
  1749. %int = OpTypeInt 32 1
  1750. %_ptr_Function_int = OpTypePointer Function %int
  1751. %int_0 = OpConstant %int 0
  1752. %int_1 = OpConstant %int 1
  1753. %bool = OpTypeBool
  1754. %false = OpConstantFalse %bool
  1755. ; CHECK: OpFunction
  1756. ; CHECK-NEXT: OpLabel
  1757. ; CHECK-NEXT: [[a:%\w+]] = OpVariable
  1758. ; CHECK-NEXT: [[b:%\w+]] = OpVariable
  1759. ; CHECK: OpStore [[a]] [[v:%\w+]]
  1760. ; CHECK: OpStore [[b]]
  1761. ; Make sure the load was removed.
  1762. ; CHECK: OpLabel
  1763. ; CHECK-NOT: OpLoad %int [[a]]
  1764. ; CHECK: OpStore [[b]] [[v]]
  1765. %main = OpFunction %void None %3
  1766. %5 = OpLabel
  1767. %a = OpVariable %_ptr_Function_int Function
  1768. %b = OpVariable %_ptr_Function_int Function
  1769. OpStore %a %int_0
  1770. OpStore %b %int_1
  1771. OpSelectionMerge %15 None
  1772. OpBranchConditional %false %14 %15
  1773. %14 = OpLabel
  1774. %16 = OpLoad %int %a
  1775. OpStore %b %16
  1776. OpBranch %15
  1777. %15 = OpLabel
  1778. OpReturn
  1779. OpFunctionEnd
  1780. )";
  1781. SinglePassRunAndMatch<LocalSingleStoreElimPass>(text, false);
  1782. }
  1783. // TODO(greg-lunarg): Add tests to verify handling of these cases:
  1784. //
  1785. // Other types
  1786. // Others?
  1787. } // namespace
  1788. } // namespace opt
  1789. } // namespace spvtools