scalar_replacement_test.cpp 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315
  1. // Copyright (c) 2017 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/opt/scalar_replacement_pass.h"
  15. #include <string>
  16. #include "gmock/gmock.h"
  17. #include "test/opt/assembly_builder.h"
  18. #include "test/opt/pass_fixture.h"
  19. #include "test/opt/pass_utils.h"
  20. namespace spvtools {
  21. namespace opt {
  22. namespace {
  23. using ScalarReplacementPassName = ::testing::Test;
  24. TEST_F(ScalarReplacementPassName, Default) {
  25. auto srp = ScalarReplacementPass();
  26. EXPECT_STREQ(srp.name(), "scalar-replacement=100");
  27. }
  28. TEST_F(ScalarReplacementPassName, Large) {
  29. auto srp = ScalarReplacementPass(0xffffffffu);
  30. EXPECT_STREQ(srp.name(), "scalar-replacement=4294967295");
  31. }
  32. using ScalarReplacementTest = PassTest<::testing::Test>;
  33. TEST_F(ScalarReplacementTest, SimpleStruct) {
  34. const std::string text = R"(
  35. ;
  36. ; CHECK: [[struct:%\w+]] = OpTypeStruct [[elem:%\w+]]
  37. ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Function [[struct]]
  38. ; CHECK: [[elem_ptr:%\w+]] = OpTypePointer Function [[elem]]
  39. ; CHECK: OpConstantNull [[struct]]
  40. ; CHECK: [[null:%\w+]] = OpConstantNull [[elem]]
  41. ; CHECK-NOT: OpVariable [[struct_ptr]]
  42. ; CHECK: [[one:%\w+]] = OpVariable [[elem_ptr]] Function [[null]]
  43. ; CHECK-NEXT: [[two:%\w+]] = OpVariable [[elem_ptr]] Function [[null]]
  44. ; CHECK-NOT: OpVariable [[elem_ptr]] Function [[null]]
  45. ; CHECK-NOT: OpVariable [[struct_ptr]]
  46. ; CHECK-NOT: OpInBoundsAccessChain
  47. ; CHECK: [[l1:%\w+]] = OpLoad [[elem]] [[two]]
  48. ; CHECK-NOT: OpAccessChain
  49. ; CHECK: [[l2:%\w+]] = OpLoad [[elem]] [[one]]
  50. ; CHECK: OpIAdd [[elem]] [[l1]] [[l2]]
  51. ;
  52. OpCapability Shader
  53. OpCapability Linkage
  54. OpMemoryModel Logical GLSL450
  55. OpName %6 "simple_struct"
  56. %1 = OpTypeVoid
  57. %2 = OpTypeInt 32 0
  58. %3 = OpTypeStruct %2 %2 %2 %2
  59. %4 = OpTypePointer Function %3
  60. %5 = OpTypePointer Function %2
  61. %6 = OpTypeFunction %2
  62. %7 = OpConstantNull %3
  63. %8 = OpConstant %2 0
  64. %9 = OpConstant %2 1
  65. %10 = OpConstant %2 2
  66. %11 = OpConstant %2 3
  67. %12 = OpFunction %2 None %6
  68. %13 = OpLabel
  69. %14 = OpVariable %4 Function %7
  70. %15 = OpInBoundsAccessChain %5 %14 %8
  71. %16 = OpLoad %2 %15
  72. %17 = OpAccessChain %5 %14 %10
  73. %18 = OpLoad %2 %17
  74. %19 = OpIAdd %2 %16 %18
  75. OpReturnValue %19
  76. OpFunctionEnd
  77. )";
  78. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  79. }
  80. TEST_F(ScalarReplacementTest, StructInitialization) {
  81. const std::string text = R"(
  82. ;
  83. ; CHECK: [[elem:%\w+]] = OpTypeInt 32 0
  84. ; CHECK: [[struct:%\w+]] = OpTypeStruct [[elem]] [[elem]] [[elem]] [[elem]]
  85. ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Function [[struct]]
  86. ; CHECK: [[elem_ptr:%\w+]] = OpTypePointer Function [[elem]]
  87. ; CHECK: [[zero:%\w+]] = OpConstant [[elem]] 0
  88. ; CHECK: [[undef:%\w+]] = OpUndef [[elem]]
  89. ; CHECK: [[two:%\w+]] = OpConstant [[elem]] 2
  90. ; CHECK: [[null:%\w+]] = OpConstantNull [[elem]]
  91. ; CHECK-NOT: OpVariable [[struct_ptr]]
  92. ; CHECK: OpVariable [[elem_ptr]] Function [[null]]
  93. ; CHECK-NEXT: OpVariable [[elem_ptr]] Function [[two]]
  94. ; CHECK-NOT: OpVariable [[elem_ptr]] Function [[undef]]
  95. ; CHECK-NEXT: OpVariable [[elem_ptr]] Function
  96. ; CHECK-NEXT: OpVariable [[elem_ptr]] Function [[zero]]
  97. ; CHECK-NOT: OpVariable [[elem_ptr]] Function [[undef]]
  98. ;
  99. OpCapability Shader
  100. OpCapability Linkage
  101. OpMemoryModel Logical GLSL450
  102. OpName %6 "struct_init"
  103. %1 = OpTypeVoid
  104. %2 = OpTypeInt 32 0
  105. %3 = OpTypeStruct %2 %2 %2 %2
  106. %4 = OpTypePointer Function %3
  107. %20 = OpTypePointer Function %2
  108. %6 = OpTypeFunction %1
  109. %7 = OpConstant %2 0
  110. %8 = OpUndef %2
  111. %9 = OpConstant %2 2
  112. %30 = OpConstant %2 1
  113. %31 = OpConstant %2 3
  114. %10 = OpConstantNull %2
  115. %11 = OpConstantComposite %3 %7 %8 %9 %10
  116. %12 = OpFunction %1 None %6
  117. %13 = OpLabel
  118. %14 = OpVariable %4 Function %11
  119. %15 = OpAccessChain %20 %14 %7
  120. OpStore %15 %10
  121. %16 = OpAccessChain %20 %14 %9
  122. OpStore %16 %10
  123. %17 = OpAccessChain %20 %14 %30
  124. OpStore %17 %10
  125. %18 = OpAccessChain %20 %14 %31
  126. OpStore %18 %10
  127. OpReturn
  128. OpFunctionEnd
  129. )";
  130. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  131. }
  132. TEST_F(ScalarReplacementTest, SpecConstantInitialization) {
  133. const std::string text = R"(
  134. ;
  135. ; CHECK: [[int:%\w+]] = OpTypeInt 32 0
  136. ; CHECK: [[struct:%\w+]] = OpTypeStruct [[int]] [[int]]
  137. ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Function [[struct]]
  138. ; CHECK: [[int_ptr:%\w+]] = OpTypePointer Function [[int]]
  139. ; CHECK: [[spec_comp:%\w+]] = OpSpecConstantComposite [[struct]]
  140. ; CHECK: [[ex0:%\w+]] = OpSpecConstantOp [[int]] CompositeExtract [[spec_comp]] 0
  141. ; CHECK: [[ex1:%\w+]] = OpSpecConstantOp [[int]] CompositeExtract [[spec_comp]] 1
  142. ; CHECK-NOT: OpVariable [[struct]]
  143. ; CHECK: OpVariable [[int_ptr]] Function [[ex1]]
  144. ; CHECK-NEXT: OpVariable [[int_ptr]] Function [[ex0]]
  145. ; CHECK-NOT: OpVariable [[struct]]
  146. ;
  147. OpCapability Shader
  148. OpCapability Linkage
  149. OpMemoryModel Logical GLSL450
  150. OpName %6 "spec_const"
  151. %1 = OpTypeVoid
  152. %2 = OpTypeInt 32 0
  153. %3 = OpTypeStruct %2 %2
  154. %4 = OpTypePointer Function %3
  155. %20 = OpTypePointer Function %2
  156. %5 = OpTypeFunction %1
  157. %6 = OpConstant %2 0
  158. %30 = OpConstant %2 1
  159. %7 = OpSpecConstant %2 0
  160. %8 = OpSpecConstantOp %2 IAdd %7 %7
  161. %9 = OpSpecConstantComposite %3 %7 %8
  162. %10 = OpFunction %1 None %5
  163. %11 = OpLabel
  164. %12 = OpVariable %4 Function %9
  165. %13 = OpAccessChain %20 %12 %6
  166. %14 = OpLoad %2 %13
  167. %15 = OpAccessChain %20 %12 %30
  168. %16 = OpLoad %2 %15
  169. OpReturn
  170. OpFunctionEnd
  171. )";
  172. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  173. }
  174. // TODO(alanbaker): Re-enable when vector and matrix scalarization is supported.
  175. // TEST_F(ScalarReplacementTest, VectorInitialization) {
  176. // const std::string text = R"(
  177. // ;
  178. // ; CHECK: [[elem:%\w+]] = OpTypeInt 32 0
  179. // ; CHECK: [[vector:%\w+]] = OpTypeVector [[elem]] 4
  180. // ; CHECK: [[vector_ptr:%\w+]] = OpTypePointer Function [[vector]]
  181. // ; CHECK: [[elem_ptr:%\w+]] = OpTypePointer Function [[elem]]
  182. // ; CHECK: [[zero:%\w+]] = OpConstant [[elem]] 0
  183. // ; CHECK: [[undef:%\w+]] = OpUndef [[elem]]
  184. // ; CHECK: [[two:%\w+]] = OpConstant [[elem]] 2
  185. // ; CHECK: [[null:%\w+]] = OpConstantNull [[elem]]
  186. // ; CHECK-NOT: OpVariable [[vector_ptr]]
  187. // ; CHECK: OpVariable [[elem_ptr]] Function [[zero]]
  188. // ; CHECK-NOT: OpVariable [[elem_ptr]] Function [[undef]]
  189. // ; CHECK-NEXT: OpVariable [[elem_ptr]] Function
  190. // ; CHECK-NEXT: OpVariable [[elem_ptr]] Function [[two]]
  191. // ; CHECK-NEXT: OpVariable [[elem_ptr]] Function [[null]]
  192. // ; CHECK-NOT: OpVariable [[elem_ptr]] Function [[undef]]
  193. // ;
  194. // OpCapability Shader
  195. // OpCapability Linkage
  196. // OpMemoryModel Logical GLSL450
  197. // OpName %6 "vector_init"
  198. // %1 = OpTypeVoid
  199. // %2 = OpTypeInt 32 0
  200. // %3 = OpTypeVector %2 4
  201. // %4 = OpTypePointer Function %3
  202. // %20 = OpTypePointer Function %2
  203. // %6 = OpTypeFunction %1
  204. // %7 = OpConstant %2 0
  205. // %8 = OpUndef %2
  206. // %9 = OpConstant %2 2
  207. // %30 = OpConstant %2 1
  208. // %31 = OpConstant %2 3
  209. // %10 = OpConstantNull %2
  210. // %11 = OpConstantComposite %3 %10 %9 %8 %7
  211. // %12 = OpFunction %1 None %6
  212. // %13 = OpLabel
  213. // %14 = OpVariable %4 Function %11
  214. // %15 = OpAccessChain %20 %14 %7
  215. // OpStore %15 %10
  216. // %16 = OpAccessChain %20 %14 %9
  217. // OpStore %16 %10
  218. // %17 = OpAccessChain %20 %14 %30
  219. // OpStore %17 %10
  220. // %18 = OpAccessChain %20 %14 %31
  221. // OpStore %18 %10
  222. // OpReturn
  223. // OpFunctionEnd
  224. // )";
  225. //
  226. // SinglePassRunAndMatch<opt::ScalarReplacementPass>(text, true);
  227. // }
  228. //
  229. // TEST_F(ScalarReplacementTest, MatrixInitialization) {
  230. // const std::string text = R"(
  231. // ;
  232. // ; CHECK: [[float:%\w+]] = OpTypeFloat 32
  233. // ; CHECK: [[vector:%\w+]] = OpTypeVector [[float]] 2
  234. // ; CHECK: [[matrix:%\w+]] = OpTypeMatrix [[vector]] 2
  235. // ; CHECK: [[matrix_ptr:%\w+]] = OpTypePointer Function [[matrix]]
  236. // ; CHECK: [[float_ptr:%\w+]] = OpTypePointer Function [[float]]
  237. // ; CHECK: [[vec_ptr:%\w+]] = OpTypePointer Function [[vector]]
  238. // ; CHECK: [[zerof:%\w+]] = OpConstant [[float]] 0
  239. // ; CHECK: [[onef:%\w+]] = OpConstant [[float]] 1
  240. // ; CHECK: [[one_zero:%\w+]] = OpConstantComposite [[vector]] [[onef]]
  241. // [[zerof]] ; CHECK: [[zero_one:%\w+]] = OpConstantComposite [[vector]]
  242. // [[zerof]] [[onef]] ; CHECK: [[const_mat:%\w+]] = OpConstantComposite
  243. // [[matrix]] [[one_zero]]
  244. // [[zero_one]] ; CHECK-NOT: OpVariable [[matrix]] ; CHECK-NOT: OpVariable
  245. // [[vector]] Function [[one_zero]] ; CHECK: [[f1:%\w+]] = OpVariable
  246. // [[float_ptr]] Function [[zerof]] ; CHECK-NEXT: [[f2:%\w+]] = OpVariable
  247. // [[float_ptr]] Function [[onef]] ; CHECK-NEXT: [[vec_var:%\w+]] = OpVariable
  248. // [[vec_ptr]] Function [[zero_one]] ; CHECK-NOT: OpVariable [[matrix]] ;
  249. // CHECK-NOT: OpVariable [[vector]] Function [[one_zero]]
  250. // ;
  251. // OpCapability Shader
  252. // OpCapability Linkage
  253. // OpMemoryModel Logical GLSL450
  254. // OpName %7 "matrix_init"
  255. // %1 = OpTypeVoid
  256. // %2 = OpTypeFloat 32
  257. // %3 = OpTypeVector %2 2
  258. // %4 = OpTypeMatrix %3 2
  259. // %5 = OpTypePointer Function %4
  260. // %6 = OpTypePointer Function %2
  261. // %30 = OpTypePointer Function %3
  262. // %10 = OpTypeInt 32 0
  263. // %7 = OpTypeFunction %1 %10
  264. // %8 = OpConstant %2 0.0
  265. // %9 = OpConstant %2 1.0
  266. // %11 = OpConstant %10 0
  267. // %12 = OpConstant %10 1
  268. // %13 = OpConstantComposite %3 %9 %8
  269. // %14 = OpConstantComposite %3 %8 %9
  270. // %15 = OpConstantComposite %4 %13 %14
  271. // %16 = OpFunction %1 None %7
  272. // %31 = OpFunctionParameter %10
  273. // %17 = OpLabel
  274. // %18 = OpVariable %5 Function %15
  275. // %19 = OpAccessChain %6 %18 %11 %12
  276. // OpStore %19 %8
  277. // %20 = OpAccessChain %6 %18 %11 %11
  278. // OpStore %20 %8
  279. // %21 = OpAccessChain %30 %18 %12
  280. // OpStore %21 %14
  281. // OpReturn
  282. // OpFunctionEnd
  283. // )";
  284. //
  285. // SinglePassRunAndMatch<opt::ScalarReplacementPass>(text, true);
  286. // }
  287. TEST_F(ScalarReplacementTest, ElideAccessChain) {
  288. const std::string text = R"(
  289. ;
  290. ; CHECK: [[var:%\w+]] = OpVariable
  291. ; CHECK-NOT: OpAccessChain
  292. ; CHECK: OpStore [[var]]
  293. ;
  294. OpCapability Shader
  295. OpCapability Linkage
  296. OpMemoryModel Logical GLSL450
  297. OpName %6 "elide_access_chain"
  298. %1 = OpTypeVoid
  299. %2 = OpTypeInt 32 0
  300. %3 = OpTypeStruct %2 %2 %2 %2
  301. %4 = OpTypePointer Function %3
  302. %20 = OpTypePointer Function %2
  303. %6 = OpTypeFunction %1
  304. %7 = OpConstant %2 0
  305. %8 = OpUndef %2
  306. %9 = OpConstant %2 2
  307. %10 = OpConstantNull %2
  308. %11 = OpConstantComposite %3 %7 %8 %9 %10
  309. %12 = OpFunction %1 None %6
  310. %13 = OpLabel
  311. %14 = OpVariable %4 Function %11
  312. %15 = OpAccessChain %20 %14 %7
  313. OpStore %15 %10
  314. OpReturn
  315. OpFunctionEnd
  316. )";
  317. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  318. }
  319. TEST_F(ScalarReplacementTest, ElideMultipleAccessChains) {
  320. const std::string text = R"(
  321. ;
  322. ; CHECK: [[var:%\w+]] = OpVariable
  323. ; CHECK-NOT: OpInBoundsAccessChain
  324. ; CHECK OpStore [[var]]
  325. ;
  326. OpCapability Shader
  327. OpCapability Linkage
  328. OpMemoryModel Logical GLSL450
  329. OpName %6 "elide_two_access_chains"
  330. %1 = OpTypeVoid
  331. %2 = OpTypeFloat 32
  332. %3 = OpTypeStruct %2 %2
  333. %4 = OpTypeStruct %3 %3
  334. %5 = OpTypePointer Function %4
  335. %6 = OpTypePointer Function %2
  336. %7 = OpTypeFunction %1
  337. %8 = OpConstant %2 0.0
  338. %9 = OpConstant %2 1.0
  339. %10 = OpTypeInt 32 0
  340. %11 = OpConstant %10 0
  341. %12 = OpConstant %10 1
  342. %13 = OpConstantComposite %3 %9 %8
  343. %14 = OpConstantComposite %3 %8 %9
  344. %15 = OpConstantComposite %4 %13 %14
  345. %16 = OpFunction %1 None %7
  346. %17 = OpLabel
  347. %18 = OpVariable %5 Function %15
  348. %19 = OpInBoundsAccessChain %6 %18 %11 %12
  349. OpStore %19 %8
  350. OpReturn
  351. OpFunctionEnd
  352. )";
  353. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  354. }
  355. TEST_F(ScalarReplacementTest, ReplaceAccessChain) {
  356. const std::string text = R"(
  357. ;
  358. ; CHECK: [[param:%\w+]] = OpFunctionParameter
  359. ; CHECK: [[var:%\w+]] = OpVariable
  360. ; CHECK: [[access:%\w+]] = OpAccessChain {{%\w+}} [[var]] [[param]]
  361. ; CHECK: OpStore [[access]]
  362. ;
  363. OpCapability Shader
  364. OpCapability Linkage
  365. OpMemoryModel Logical GLSL450
  366. OpName %7 "replace_access_chain"
  367. %1 = OpTypeVoid
  368. %2 = OpTypeFloat 32
  369. %10 = OpTypeInt 32 0
  370. %uint_2 = OpConstant %10 2
  371. %3 = OpTypeArray %2 %uint_2
  372. %4 = OpTypeStruct %3 %3
  373. %5 = OpTypePointer Function %4
  374. %20 = OpTypePointer Function %3
  375. %6 = OpTypePointer Function %2
  376. %7 = OpTypeFunction %1 %10
  377. %8 = OpConstant %2 0.0
  378. %9 = OpConstant %2 1.0
  379. %11 = OpConstant %10 0
  380. %12 = OpConstant %10 1
  381. %13 = OpConstantComposite %3 %9 %8
  382. %14 = OpConstantComposite %3 %8 %9
  383. %15 = OpConstantComposite %4 %13 %14
  384. %16 = OpFunction %1 None %7
  385. %32 = OpFunctionParameter %10
  386. %17 = OpLabel
  387. %18 = OpVariable %5 Function %15
  388. %19 = OpAccessChain %6 %18 %11 %32
  389. OpStore %19 %8
  390. OpReturn
  391. OpFunctionEnd
  392. )";
  393. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  394. }
  395. TEST_F(ScalarReplacementTest, ArrayInitialization) {
  396. const std::string text = R"(
  397. ;
  398. ; CHECK: [[float:%\w+]] = OpTypeFloat 32
  399. ; CHECK: [[array:%\w+]] = OpTypeArray
  400. ; CHECK: [[array_ptr:%\w+]] = OpTypePointer Function [[array]]
  401. ; CHECK: [[float_ptr:%\w+]] = OpTypePointer Function [[float]]
  402. ; CHECK: [[float0:%\w+]] = OpConstant [[float]] 0
  403. ; CHECK: [[float1:%\w+]] = OpConstant [[float]] 1
  404. ; CHECK: [[float2:%\w+]] = OpConstant [[float]] 2
  405. ; CHECK-NOT: OpVariable [[array_ptr]]
  406. ; CHECK: [[var0:%\w+]] = OpVariable [[float_ptr]] Function [[float0]]
  407. ; CHECK-NEXT: [[var1:%\w+]] = OpVariable [[float_ptr]] Function [[float1]]
  408. ; CHECK-NEXT: [[var2:%\w+]] = OpVariable [[float_ptr]] Function [[float2]]
  409. ; CHECK-NOT: OpVariable [[array_ptr]]
  410. ;
  411. OpCapability Shader
  412. OpCapability Linkage
  413. OpMemoryModel Logical GLSL450
  414. OpName %func "array_init"
  415. %void = OpTypeVoid
  416. %uint = OpTypeInt 32 0
  417. %float = OpTypeFloat 32
  418. %uint_0 = OpConstant %uint 0
  419. %uint_1 = OpConstant %uint 1
  420. %uint_2 = OpConstant %uint 2
  421. %uint_3 = OpConstant %uint 3
  422. %float_array = OpTypeArray %float %uint_3
  423. %array_ptr = OpTypePointer Function %float_array
  424. %float_ptr = OpTypePointer Function %float
  425. %float_0 = OpConstant %float 0
  426. %float_1 = OpConstant %float 1
  427. %float_2 = OpConstant %float 2
  428. %const_array = OpConstantComposite %float_array %float_2 %float_1 %float_0
  429. %func = OpTypeFunction %void
  430. %1 = OpFunction %void None %func
  431. %2 = OpLabel
  432. %3 = OpVariable %array_ptr Function %const_array
  433. %4 = OpInBoundsAccessChain %float_ptr %3 %uint_0
  434. OpStore %4 %float_0
  435. %5 = OpInBoundsAccessChain %float_ptr %3 %uint_1
  436. OpStore %5 %float_0
  437. %6 = OpInBoundsAccessChain %float_ptr %3 %uint_2
  438. OpStore %6 %float_0
  439. OpReturn
  440. OpFunctionEnd
  441. )";
  442. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  443. }
  444. TEST_F(ScalarReplacementTest, NonUniformCompositeInitialization) {
  445. const std::string text = R"(
  446. ;
  447. ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
  448. ; CHECK: [[long:%\w+]] = OpTypeInt 64 1
  449. ; CHECK: [[dvector:%\w+]] = OpTypeVector
  450. ; CHECK: [[vector:%\w+]] = OpTypeVector
  451. ; CHECK: [[array:%\w+]] = OpTypeArray
  452. ; CHECK: [[matrix:%\w+]] = OpTypeMatrix
  453. ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[vector]]
  454. ; CHECK: [[struct2:%\w+]] = OpTypeStruct [[struct1]] [[matrix]] [[array]] [[uint]]
  455. ; CHECK: [[struct1_ptr:%\w+]] = OpTypePointer Function [[struct1]]
  456. ; CHECK: [[matrix_ptr:%\w+]] = OpTypePointer Function [[matrix]]
  457. ; CHECK: [[array_ptr:%\w+]] = OpTypePointer Function [[array]]
  458. ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
  459. ; CHECK: [[struct2_ptr:%\w+]] = OpTypePointer Function [[struct2]]
  460. ; CHECK: [[const_array:%\w+]] = OpConstantComposite [[array]]
  461. ; CHECK: [[const_matrix:%\w+]] = OpConstantNull [[matrix]]
  462. ; CHECK: [[const_struct1:%\w+]] = OpConstantComposite [[struct1]]
  463. ; CHECK: OpUndef [[uint]]
  464. ; CHECK: OpUndef [[vector]]
  465. ; CHECK: OpUndef [[long]]
  466. ; CHECK: OpFunction
  467. ; CHECK-NOT: OpVariable [[struct2_ptr]] Function
  468. ; CHECK: OpVariable [[uint_ptr]] Function
  469. ; CHECK-NEXT: OpVariable [[matrix_ptr]] Function [[const_matrix]]
  470. ; CHECK-NOT: OpVariable [[struct1_ptr]] Function [[const_struct1]]
  471. ; CHECK-NOT: OpVariable [[struct2_ptr]] Function
  472. ;
  473. OpCapability Shader
  474. OpCapability Linkage
  475. OpCapability Int64
  476. OpCapability Float64
  477. OpMemoryModel Logical GLSL450
  478. OpName %func "non_uniform_composite_init"
  479. %void = OpTypeVoid
  480. %uint = OpTypeInt 32 0
  481. %int64 = OpTypeInt 64 1
  482. %float = OpTypeFloat 32
  483. %double = OpTypeFloat 64
  484. %double2 = OpTypeVector %double 2
  485. %float4 = OpTypeVector %float 4
  486. %int64_0 = OpConstant %int64 0
  487. %int64_1 = OpConstant %int64 1
  488. %int64_2 = OpConstant %int64 2
  489. %int64_3 = OpConstant %int64 3
  490. %int64_array3 = OpTypeArray %int64 %int64_3
  491. %matrix_double2 = OpTypeMatrix %double2 2
  492. %struct1 = OpTypeStruct %uint %float4
  493. %struct2 = OpTypeStruct %struct1 %matrix_double2 %int64_array3 %uint
  494. %struct1_ptr = OpTypePointer Function %struct1
  495. %matrix_double2_ptr = OpTypePointer Function %matrix_double2
  496. %int64_array_ptr = OpTypePointer Function %int64_array3
  497. %uint_ptr = OpTypePointer Function %uint
  498. %struct2_ptr = OpTypePointer Function %struct2
  499. %const_uint = OpConstant %uint 0
  500. %const_int64_array = OpConstantComposite %int64_array3 %int64_0 %int64_1 %int64_2
  501. %const_double2 = OpConstantNull %double2
  502. %const_matrix_double2 = OpConstantNull %matrix_double2
  503. %undef_float4 = OpUndef %float4
  504. %const_struct1 = OpConstantComposite %struct1 %const_uint %undef_float4
  505. %const_struct2 = OpConstantComposite %struct2 %const_struct1 %const_matrix_double2 %const_int64_array %const_uint
  506. %func = OpTypeFunction %void
  507. %1 = OpFunction %void None %func
  508. %2 = OpLabel
  509. %var = OpVariable %struct2_ptr Function %const_struct2
  510. %3 = OpAccessChain %struct1_ptr %var %int64_0
  511. OpStore %3 %const_struct1
  512. %4 = OpAccessChain %matrix_double2_ptr %var %int64_1
  513. OpStore %4 %const_matrix_double2
  514. %5 = OpAccessChain %int64_array_ptr %var %int64_2
  515. OpStore %5 %const_int64_array
  516. %6 = OpAccessChain %uint_ptr %var %int64_3
  517. OpStore %6 %const_uint
  518. OpReturn
  519. OpFunctionEnd
  520. )";
  521. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  522. }
  523. TEST_F(ScalarReplacementTest, ElideUncombinedAccessChains) {
  524. const std::string text = R"(
  525. ;
  526. ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
  527. ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
  528. ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
  529. ; CHECK: [[var:%\w+]] = OpVariable [[uint_ptr]] Function
  530. ; CHECK-NOT: OpAccessChain
  531. ; CHECK: OpStore [[var]] [[const]]
  532. ;
  533. OpCapability Shader
  534. OpCapability Linkage
  535. OpMemoryModel Logical GLSL450
  536. OpName %func "elide_uncombined_access_chains"
  537. %void = OpTypeVoid
  538. %uint = OpTypeInt 32 0
  539. %struct1 = OpTypeStruct %uint
  540. %struct2 = OpTypeStruct %struct1
  541. %uint_ptr = OpTypePointer Function %uint
  542. %struct1_ptr = OpTypePointer Function %struct1
  543. %struct2_ptr = OpTypePointer Function %struct2
  544. %uint_0 = OpConstant %uint 0
  545. %func = OpTypeFunction %void
  546. %1 = OpFunction %void None %func
  547. %2 = OpLabel
  548. %var = OpVariable %struct2_ptr Function
  549. %3 = OpAccessChain %struct1_ptr %var %uint_0
  550. %4 = OpAccessChain %uint_ptr %3 %uint_0
  551. OpStore %4 %uint_0
  552. OpReturn
  553. OpFunctionEnd
  554. )";
  555. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  556. }
  557. TEST_F(ScalarReplacementTest, ElideSingleUncombinedAccessChains) {
  558. const std::string text = R"(
  559. ;
  560. ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
  561. ; CHECK: [[array:%\w+]] = OpTypeArray [[uint]]
  562. ; CHECK: [[array_ptr:%\w+]] = OpTypePointer Function [[array]]
  563. ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
  564. ; CHECK: [[param:%\w+]] = OpFunctionParameter [[uint]]
  565. ; CHECK: [[var:%\w+]] = OpVariable [[array_ptr]] Function
  566. ; CHECK: [[access:%\w+]] = OpAccessChain {{.*}} [[var]] [[param]]
  567. ; CHECK: OpStore [[access]] [[const]]
  568. ;
  569. OpCapability Shader
  570. OpCapability Linkage
  571. OpMemoryModel Logical GLSL450
  572. OpName %func "elide_single_uncombined_access_chains"
  573. %void = OpTypeVoid
  574. %uint = OpTypeInt 32 0
  575. %uint_1 = OpConstant %uint 1
  576. %array = OpTypeArray %uint %uint_1
  577. %struct2 = OpTypeStruct %array
  578. %uint_ptr = OpTypePointer Function %uint
  579. %array_ptr = OpTypePointer Function %array
  580. %struct2_ptr = OpTypePointer Function %struct2
  581. %uint_0 = OpConstant %uint 0
  582. %func = OpTypeFunction %void %uint
  583. %1 = OpFunction %void None %func
  584. %param = OpFunctionParameter %uint
  585. %2 = OpLabel
  586. %var = OpVariable %struct2_ptr Function
  587. %3 = OpAccessChain %array_ptr %var %uint_0
  588. %4 = OpAccessChain %uint_ptr %3 %param
  589. OpStore %4 %uint_0
  590. OpReturn
  591. OpFunctionEnd
  592. )";
  593. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  594. }
  595. TEST_F(ScalarReplacementTest, ReplaceWholeLoad) {
  596. const std::string text = R"(
  597. ;
  598. ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
  599. ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[uint]]
  600. ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
  601. ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
  602. ; CHECK: [[var1:%\w+]] = OpVariable [[uint_ptr]] Function
  603. ; CHECK: [[var0:%\w+]] = OpVariable [[uint_ptr]] Function
  604. ; CHECK: [[l1:%\w+]] = OpLoad [[uint]] [[var1]]
  605. ; CHECK: [[l0:%\w+]] = OpLoad [[uint]] [[var0]]
  606. ; CHECK: OpCompositeConstruct [[struct1]] [[l0]] [[l1]]
  607. ;
  608. OpCapability Shader
  609. OpCapability Linkage
  610. OpMemoryModel Logical GLSL450
  611. OpName %func "replace_whole_load"
  612. %void = OpTypeVoid
  613. %uint = OpTypeInt 32 0
  614. %struct1 = OpTypeStruct %uint %uint
  615. %uint_ptr = OpTypePointer Function %uint
  616. %struct1_ptr = OpTypePointer Function %struct1
  617. %uint_0 = OpConstant %uint 0
  618. %uint_1 = OpConstant %uint 1
  619. %func = OpTypeFunction %void
  620. %1 = OpFunction %void None %func
  621. %2 = OpLabel
  622. %var = OpVariable %struct1_ptr Function
  623. %load = OpLoad %struct1 %var
  624. %3 = OpAccessChain %uint_ptr %var %uint_0
  625. OpStore %3 %uint_0
  626. %4 = OpAccessChain %uint_ptr %var %uint_1
  627. OpStore %4 %uint_0
  628. OpReturn
  629. OpFunctionEnd
  630. )";
  631. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  632. }
  633. TEST_F(ScalarReplacementTest, ReplaceWholeLoadCopyMemoryAccess) {
  634. const std::string text = R"(
  635. ;
  636. ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
  637. ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[uint]]
  638. ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
  639. ; CHECK: [[undef:%\w+]] = OpUndef [[uint]]
  640. ; CHECK: [[var0:%\w+]] = OpVariable [[uint_ptr]] Function
  641. ; CHECK: [[l0:%\w+]] = OpLoad [[uint]] [[var0]] Nontemporal
  642. ; CHECK: OpCompositeConstruct [[struct1]] [[l0]] [[undef]]
  643. ;
  644. OpCapability Shader
  645. OpCapability Linkage
  646. OpMemoryModel Logical GLSL450
  647. OpName %func "replace_whole_load_copy_memory_access"
  648. %void = OpTypeVoid
  649. %uint = OpTypeInt 32 0
  650. %struct1 = OpTypeStruct %uint %uint
  651. %uint_ptr = OpTypePointer Function %uint
  652. %struct1_ptr = OpTypePointer Function %struct1
  653. %uint_0 = OpConstant %uint 0
  654. %func = OpTypeFunction %void
  655. %1 = OpFunction %void None %func
  656. %2 = OpLabel
  657. %var = OpVariable %struct1_ptr Function
  658. %load = OpLoad %struct1 %var Nontemporal
  659. %3 = OpAccessChain %uint_ptr %var %uint_0
  660. OpStore %3 %uint_0
  661. OpReturn
  662. OpFunctionEnd
  663. )";
  664. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  665. }
  666. TEST_F(ScalarReplacementTest, ReplaceWholeStore) {
  667. const std::string text = R"(
  668. ;
  669. ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
  670. ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[uint]]
  671. ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
  672. ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
  673. ; CHECK: [[const_struct:%\w+]] = OpConstantComposite [[struct1]] [[const]] [[const]]
  674. ; CHECK: [[var0:%\w+]] = OpVariable [[uint_ptr]] Function
  675. ; CHECK: [[ex0:%\w+]] = OpCompositeExtract [[uint]] [[const_struct]] 0
  676. ; CHECK: OpStore [[var0]] [[ex0]]
  677. ;
  678. OpCapability Shader
  679. OpCapability Linkage
  680. OpMemoryModel Logical GLSL450
  681. OpName %func "replace_whole_store"
  682. %void = OpTypeVoid
  683. %uint = OpTypeInt 32 0
  684. %struct1 = OpTypeStruct %uint %uint
  685. %uint_ptr = OpTypePointer Function %uint
  686. %struct1_ptr = OpTypePointer Function %struct1
  687. %uint_0 = OpConstant %uint 0
  688. %const_struct = OpConstantComposite %struct1 %uint_0 %uint_0
  689. %func = OpTypeFunction %void
  690. %1 = OpFunction %void None %func
  691. %2 = OpLabel
  692. %var = OpVariable %struct1_ptr Function
  693. OpStore %var %const_struct
  694. %3 = OpAccessChain %uint_ptr %var %uint_0
  695. %4 = OpLoad %uint %3
  696. OpReturn
  697. OpFunctionEnd
  698. )";
  699. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  700. }
  701. TEST_F(ScalarReplacementTest, ReplaceWholeStoreCopyMemoryAccess) {
  702. const std::string text = R"(
  703. ;
  704. ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
  705. ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[uint]]
  706. ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
  707. ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
  708. ; CHECK: [[const_struct:%\w+]] = OpConstantComposite [[struct1]] [[const]] [[const]]
  709. ; CHECK: [[var0:%\w+]] = OpVariable [[uint_ptr]] Function
  710. ; CHECK-NOT: OpVariable
  711. ; CHECK: [[ex0:%\w+]] = OpCompositeExtract [[uint]] [[const_struct]] 0
  712. ; CHECK: OpStore [[var0]] [[ex0]] Aligned 4
  713. ;
  714. OpCapability Shader
  715. OpCapability Linkage
  716. OpMemoryModel Logical GLSL450
  717. OpName %func "replace_whole_store_copy_memory_access"
  718. %void = OpTypeVoid
  719. %uint = OpTypeInt 32 0
  720. %struct1 = OpTypeStruct %uint %uint
  721. %uint_ptr = OpTypePointer Function %uint
  722. %struct1_ptr = OpTypePointer Function %struct1
  723. %uint_0 = OpConstant %uint 0
  724. %const_struct = OpConstantComposite %struct1 %uint_0 %uint_0
  725. %func = OpTypeFunction %void
  726. %1 = OpFunction %void None %func
  727. %2 = OpLabel
  728. %var = OpVariable %struct1_ptr Function
  729. OpStore %var %const_struct Aligned 4
  730. %3 = OpAccessChain %uint_ptr %var %uint_0
  731. %4 = OpLoad %uint %3
  732. OpReturn
  733. OpFunctionEnd
  734. )";
  735. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  736. }
  737. TEST_F(ScalarReplacementTest, DontTouchVolatileLoad) {
  738. const std::string text = R"(
  739. ;
  740. ; CHECK: [[struct:%\w+]] = OpTypeStruct
  741. ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Function [[struct]]
  742. ; CHECK: OpLabel
  743. ; CHECK-NEXT: OpVariable [[struct_ptr]]
  744. ; CHECK-NOT: OpVariable
  745. ;
  746. OpCapability Shader
  747. OpCapability Linkage
  748. OpMemoryModel Logical GLSL450
  749. OpName %func "dont_touch_volatile_load"
  750. %void = OpTypeVoid
  751. %uint = OpTypeInt 32 0
  752. %struct1 = OpTypeStruct %uint
  753. %uint_ptr = OpTypePointer Function %uint
  754. %struct1_ptr = OpTypePointer Function %struct1
  755. %uint_0 = OpConstant %uint 0
  756. %func = OpTypeFunction %void
  757. %1 = OpFunction %void None %func
  758. %2 = OpLabel
  759. %var = OpVariable %struct1_ptr Function
  760. %3 = OpAccessChain %uint_ptr %var %uint_0
  761. %4 = OpLoad %uint %3 Volatile
  762. OpReturn
  763. OpFunctionEnd
  764. )";
  765. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  766. }
  767. TEST_F(ScalarReplacementTest, DontTouchVolatileStore) {
  768. const std::string text = R"(
  769. ;
  770. ; CHECK: [[struct:%\w+]] = OpTypeStruct
  771. ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Function [[struct]]
  772. ; CHECK: OpLabel
  773. ; CHECK-NEXT: OpVariable [[struct_ptr]]
  774. ; CHECK-NOT: OpVariable
  775. ;
  776. OpCapability Shader
  777. OpCapability Linkage
  778. OpMemoryModel Logical GLSL450
  779. OpName %func "dont_touch_volatile_store"
  780. %void = OpTypeVoid
  781. %uint = OpTypeInt 32 0
  782. %struct1 = OpTypeStruct %uint
  783. %uint_ptr = OpTypePointer Function %uint
  784. %struct1_ptr = OpTypePointer Function %struct1
  785. %uint_0 = OpConstant %uint 0
  786. %func = OpTypeFunction %void
  787. %1 = OpFunction %void None %func
  788. %2 = OpLabel
  789. %var = OpVariable %struct1_ptr Function
  790. %3 = OpAccessChain %uint_ptr %var %uint_0
  791. OpStore %3 %uint_0 Volatile
  792. OpReturn
  793. OpFunctionEnd
  794. )";
  795. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  796. }
  797. TEST_F(ScalarReplacementTest, DontTouchSpecNonFunctionVariable) {
  798. const std::string text = R"(
  799. ;
  800. ; CHECK: [[struct:%\w+]] = OpTypeStruct
  801. ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Uniform [[struct]]
  802. ; CHECK: OpConstant
  803. ; CHECK-NEXT: OpVariable [[struct_ptr]]
  804. ; CHECK-NOT: OpVariable
  805. ;
  806. OpCapability Shader
  807. OpCapability Linkage
  808. OpMemoryModel Logical GLSL450
  809. OpName %func "dont_touch_spec_constant_access_chain"
  810. %void = OpTypeVoid
  811. %uint = OpTypeInt 32 0
  812. %struct1 = OpTypeStruct %uint
  813. %uint_ptr = OpTypePointer Uniform %uint
  814. %struct1_ptr = OpTypePointer Uniform %struct1
  815. %uint_0 = OpConstant %uint 0
  816. %var = OpVariable %struct1_ptr Uniform
  817. %func = OpTypeFunction %void
  818. %1 = OpFunction %void None %func
  819. %2 = OpLabel
  820. %3 = OpAccessChain %uint_ptr %var %uint_0
  821. OpStore %3 %uint_0 Volatile
  822. OpReturn
  823. OpFunctionEnd
  824. )";
  825. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  826. }
  827. TEST_F(ScalarReplacementTest, DontTouchSpecConstantAccessChain) {
  828. const std::string text = R"(
  829. ;
  830. ; CHECK: [[array:%\w+]] = OpTypeArray
  831. ; CHECK: [[array_ptr:%\w+]] = OpTypePointer Function [[array]]
  832. ; CHECK: OpLabel
  833. ; CHECK-NEXT: OpVariable [[array_ptr]]
  834. ; CHECK-NOT: OpVariable
  835. ;
  836. OpCapability Shader
  837. OpCapability Linkage
  838. OpMemoryModel Logical GLSL450
  839. OpName %func "dont_touch_spec_constant_access_chain"
  840. %void = OpTypeVoid
  841. %uint = OpTypeInt 32 0
  842. %uint_1 = OpConstant %uint 1
  843. %array = OpTypeArray %uint %uint_1
  844. %uint_ptr = OpTypePointer Function %uint
  845. %array_ptr = OpTypePointer Function %array
  846. %uint_0 = OpConstant %uint 0
  847. %spec_const = OpSpecConstant %uint 0
  848. %func = OpTypeFunction %void
  849. %1 = OpFunction %void None %func
  850. %2 = OpLabel
  851. %var = OpVariable %array_ptr Function
  852. %3 = OpAccessChain %uint_ptr %var %spec_const
  853. OpStore %3 %uint_0 Volatile
  854. OpReturn
  855. OpFunctionEnd
  856. )";
  857. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  858. }
  859. TEST_F(ScalarReplacementTest, NoPartialAccesses) {
  860. const std::string text = R"(
  861. ;
  862. ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
  863. ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
  864. ; CHECK: OpLabel
  865. ; CHECK-NOT: OpVariable
  866. ;
  867. OpCapability Shader
  868. OpCapability Linkage
  869. OpMemoryModel Logical GLSL450
  870. OpName %func "no_partial_accesses"
  871. %void = OpTypeVoid
  872. %uint = OpTypeInt 32 0
  873. %struct1 = OpTypeStruct %uint
  874. %uint_ptr = OpTypePointer Function %uint
  875. %struct1_ptr = OpTypePointer Function %struct1
  876. %const = OpConstantNull %struct1
  877. %func = OpTypeFunction %void
  878. %1 = OpFunction %void None %func
  879. %2 = OpLabel
  880. %var = OpVariable %struct1_ptr Function
  881. OpStore %var %const
  882. OpReturn
  883. OpFunctionEnd
  884. )";
  885. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  886. }
  887. TEST_F(ScalarReplacementTest, DontTouchPtrAccessChain) {
  888. const std::string text = R"(
  889. ;
  890. ; CHECK: [[struct:%\w+]] = OpTypeStruct
  891. ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Function [[struct]]
  892. ; CHECK: OpLabel
  893. ; CHECK-NEXT: OpVariable [[struct_ptr]]
  894. ; CHECK-NOT: OpVariable
  895. ;
  896. OpCapability Shader
  897. OpCapability Linkage
  898. OpMemoryModel Logical GLSL450
  899. OpName %func "dont_touch_ptr_access_chain"
  900. %void = OpTypeVoid
  901. %uint = OpTypeInt 32 0
  902. %struct1 = OpTypeStruct %uint
  903. %uint_ptr = OpTypePointer Function %uint
  904. %struct1_ptr = OpTypePointer Function %struct1
  905. %uint_0 = OpConstant %uint 0
  906. %func = OpTypeFunction %void
  907. %1 = OpFunction %void None %func
  908. %2 = OpLabel
  909. %var = OpVariable %struct1_ptr Function
  910. %3 = OpPtrAccessChain %uint_ptr %var %uint_0 %uint_0
  911. OpStore %3 %uint_0
  912. %4 = OpAccessChain %uint_ptr %var %uint_0
  913. OpStore %4 %uint_0
  914. OpReturn
  915. OpFunctionEnd
  916. )";
  917. SinglePassRunAndMatch<ScalarReplacementPass>(text, false);
  918. }
  919. TEST_F(ScalarReplacementTest, DontTouchInBoundsPtrAccessChain) {
  920. const std::string text = R"(
  921. ;
  922. ; CHECK: [[struct:%\w+]] = OpTypeStruct
  923. ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Function [[struct]]
  924. ; CHECK: OpLabel
  925. ; CHECK-NEXT: OpVariable [[struct_ptr]]
  926. ; CHECK-NOT: OpVariable
  927. ;
  928. OpCapability Shader
  929. OpCapability Linkage
  930. OpMemoryModel Logical GLSL450
  931. OpName %func "dont_touch_in_bounds_ptr_access_chain"
  932. %void = OpTypeVoid
  933. %uint = OpTypeInt 32 0
  934. %struct1 = OpTypeStruct %uint
  935. %uint_ptr = OpTypePointer Function %uint
  936. %struct1_ptr = OpTypePointer Function %struct1
  937. %uint_0 = OpConstant %uint 0
  938. %func = OpTypeFunction %void
  939. %1 = OpFunction %void None %func
  940. %2 = OpLabel
  941. %var = OpVariable %struct1_ptr Function
  942. %3 = OpInBoundsPtrAccessChain %uint_ptr %var %uint_0 %uint_0
  943. OpStore %3 %uint_0
  944. %4 = OpInBoundsAccessChain %uint_ptr %var %uint_0
  945. OpStore %4 %uint_0
  946. OpReturn
  947. OpFunctionEnd
  948. )";
  949. SinglePassRunAndMatch<ScalarReplacementPass>(text, false);
  950. }
  951. TEST_F(ScalarReplacementTest, DonTouchAliasedDecoration) {
  952. const std::string text = R"(
  953. ;
  954. ; CHECK: [[struct:%\w+]] = OpTypeStruct
  955. ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Function [[struct]]
  956. ; CHECK: OpLabel
  957. ; CHECK-NEXT: OpVariable [[struct_ptr]]
  958. ; CHECK-NOT: OpVariable
  959. ;
  960. OpCapability Shader
  961. OpCapability Linkage
  962. OpMemoryModel Logical GLSL450
  963. OpName %func "aliased"
  964. OpDecorate %var Aliased
  965. %void = OpTypeVoid
  966. %uint = OpTypeInt 32 0
  967. %struct1 = OpTypeStruct %uint
  968. %uint_ptr = OpTypePointer Function %uint
  969. %struct1_ptr = OpTypePointer Function %struct1
  970. %uint_0 = OpConstant %uint 0
  971. %func = OpTypeFunction %void
  972. %1 = OpFunction %void None %func
  973. %2 = OpLabel
  974. %var = OpVariable %struct1_ptr Function
  975. %3 = OpAccessChain %uint_ptr %var %uint_0
  976. %4 = OpLoad %uint %3
  977. OpReturn
  978. OpFunctionEnd
  979. )";
  980. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  981. }
  982. TEST_F(ScalarReplacementTest, CopyRestrictDecoration) {
  983. const std::string text = R"(
  984. ;
  985. ; CHECK: OpName
  986. ; CHECK-NEXT: OpDecorate [[var0:%\w+]] Restrict
  987. ; CHECK-NEXT: OpDecorate [[var1:%\w+]] Restrict
  988. ; CHECK: [[int:%\w+]] = OpTypeInt
  989. ; CHECK: [[struct:%\w+]] = OpTypeStruct
  990. ; CHECK: [[int_ptr:%\w+]] = OpTypePointer Function [[int]]
  991. ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Function [[struct]]
  992. ; CHECK: OpLabel
  993. ; CHECK-NEXT: [[var1]] = OpVariable [[int_ptr]]
  994. ; CHECK-NEXT: [[var0]] = OpVariable [[int_ptr]]
  995. ; CHECK-NOT: OpVariable [[struct_ptr]]
  996. ;
  997. OpCapability Shader
  998. OpCapability Linkage
  999. OpMemoryModel Logical GLSL450
  1000. OpName %func "restrict"
  1001. OpDecorate %var Restrict
  1002. %void = OpTypeVoid
  1003. %uint = OpTypeInt 32 0
  1004. %struct1 = OpTypeStruct %uint %uint
  1005. %uint_ptr = OpTypePointer Function %uint
  1006. %struct1_ptr = OpTypePointer Function %struct1
  1007. %uint_0 = OpConstant %uint 0
  1008. %uint_1 = OpConstant %uint 1
  1009. %func = OpTypeFunction %void
  1010. %1 = OpFunction %void None %func
  1011. %2 = OpLabel
  1012. %var = OpVariable %struct1_ptr Function
  1013. %3 = OpAccessChain %uint_ptr %var %uint_0
  1014. %4 = OpLoad %uint %3
  1015. %5 = OpAccessChain %uint_ptr %var %uint_1
  1016. %6 = OpLoad %uint %5
  1017. OpReturn
  1018. OpFunctionEnd
  1019. )";
  1020. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  1021. }
  1022. TEST_F(ScalarReplacementTest, DontClobberDecoratesOnSubtypes) {
  1023. const std::string text = R"(
  1024. ;
  1025. ; CHECK: OpDecorate [[array:%\w+]] ArrayStride 1
  1026. ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
  1027. ; CHECK: [[array]] = OpTypeArray [[uint]]
  1028. ; CHECK: [[array_ptr:%\w+]] = OpTypePointer Function [[array]]
  1029. ; CHECK: OpLabel
  1030. ; CHECK-NEXT: OpVariable [[array_ptr]] Function
  1031. ; CHECK-NOT: OpVariable
  1032. ;
  1033. OpCapability Shader
  1034. OpCapability Linkage
  1035. OpMemoryModel Logical GLSL450
  1036. OpName %func "array_stride"
  1037. OpDecorate %array ArrayStride 1
  1038. %void = OpTypeVoid
  1039. %uint = OpTypeInt 32 0
  1040. %uint_1 = OpConstant %uint 1
  1041. %array = OpTypeArray %uint %uint_1
  1042. %struct1 = OpTypeStruct %array
  1043. %uint_ptr = OpTypePointer Function %uint
  1044. %struct1_ptr = OpTypePointer Function %struct1
  1045. %uint_0 = OpConstant %uint 0
  1046. %func = OpTypeFunction %void %uint
  1047. %1 = OpFunction %void None %func
  1048. %param = OpFunctionParameter %uint
  1049. %2 = OpLabel
  1050. %var = OpVariable %struct1_ptr Function
  1051. %3 = OpAccessChain %uint_ptr %var %uint_0 %param
  1052. %4 = OpLoad %uint %3
  1053. OpReturn
  1054. OpFunctionEnd
  1055. )";
  1056. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  1057. }
  1058. TEST_F(ScalarReplacementTest, DontCopyMemberDecorate) {
  1059. const std::string text = R"(
  1060. ;
  1061. ; CHECK-NOT: OpDecorate
  1062. ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
  1063. ; CHECK: [[struct:%\w+]] = OpTypeStruct [[uint]]
  1064. ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
  1065. ; CHECK: [[struct_ptr:%\w+]] = OpTypePointer Function [[struct]]
  1066. ; CHECK: OpLabel
  1067. ; CHECK-NEXT: OpVariable [[uint_ptr]] Function
  1068. ; CHECK-NOT: OpVariable
  1069. ;
  1070. OpCapability Shader
  1071. OpCapability Linkage
  1072. OpMemoryModel Logical GLSL450
  1073. OpName %func "member_decorate"
  1074. OpMemberDecorate %struct1 0 Offset 1
  1075. %void = OpTypeVoid
  1076. %uint = OpTypeInt 32 0
  1077. %uint_1 = OpConstant %uint 1
  1078. %struct1 = OpTypeStruct %uint
  1079. %uint_ptr = OpTypePointer Function %uint
  1080. %struct1_ptr = OpTypePointer Function %struct1
  1081. %uint_0 = OpConstant %uint 0
  1082. %func = OpTypeFunction %void %uint
  1083. %1 = OpFunction %void None %func
  1084. %2 = OpLabel
  1085. %var = OpVariable %struct1_ptr Function
  1086. %3 = OpAccessChain %uint_ptr %var %uint_0
  1087. %4 = OpLoad %uint %3
  1088. OpReturn
  1089. OpFunctionEnd
  1090. )";
  1091. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  1092. }
  1093. TEST_F(ScalarReplacementTest, NoPartialAccesses2) {
  1094. const std::string text = R"(
  1095. ;
  1096. ; CHECK: [[float:%\w+]] = OpTypeFloat 32
  1097. ; CHECK: [[float_ptr:%\w+]] = OpTypePointer Function [[float]]
  1098. ; CHECK: OpVariable [[float_ptr]] Function
  1099. ; CHECK: OpVariable [[float_ptr]] Function
  1100. ; CHECK: OpVariable [[float_ptr]] Function
  1101. ; CHECK: OpVariable [[float_ptr]] Function
  1102. ; CHECK: OpVariable [[float_ptr]] Function
  1103. ; CHECK: OpVariable [[float_ptr]] Function
  1104. ; CHECK: OpVariable [[float_ptr]] Function
  1105. ; CHECK-NOT: OpVariable
  1106. ;
  1107. OpCapability Shader
  1108. %1 = OpExtInstImport "GLSL.std.450"
  1109. OpMemoryModel Logical GLSL450
  1110. OpEntryPoint Fragment %main "main" %fo
  1111. OpExecutionMode %main OriginUpperLeft
  1112. OpSource GLSL 430
  1113. OpName %main "main"
  1114. OpName %S "S"
  1115. OpMemberName %S 0 "x"
  1116. OpMemberName %S 1 "y"
  1117. OpName %ts1 "ts1"
  1118. OpName %S_0 "S"
  1119. OpMemberName %S_0 0 "x"
  1120. OpMemberName %S_0 1 "y"
  1121. OpName %U_t "U_t"
  1122. OpMemberName %U_t 0 "g_s1"
  1123. OpMemberName %U_t 1 "g_s2"
  1124. OpMemberName %U_t 2 "g_s3"
  1125. OpName %_ ""
  1126. OpName %ts2 "ts2"
  1127. OpName %_Globals_ "_Globals_"
  1128. OpMemberName %_Globals_ 0 "g_b"
  1129. OpName %__0 ""
  1130. OpName %ts3 "ts3"
  1131. OpName %ts4 "ts4"
  1132. OpName %fo "fo"
  1133. OpMemberDecorate %S_0 0 Offset 0
  1134. OpMemberDecorate %S_0 1 Offset 4
  1135. OpMemberDecorate %U_t 0 Offset 0
  1136. OpMemberDecorate %U_t 1 Offset 8
  1137. OpMemberDecorate %U_t 2 Offset 16
  1138. OpDecorate %U_t BufferBlock
  1139. OpDecorate %_ DescriptorSet 0
  1140. OpMemberDecorate %_Globals_ 0 Offset 0
  1141. OpDecorate %_Globals_ Block
  1142. OpDecorate %__0 DescriptorSet 0
  1143. OpDecorate %__0 Binding 0
  1144. OpDecorate %fo Location 0
  1145. %void = OpTypeVoid
  1146. %15 = OpTypeFunction %void
  1147. %float = OpTypeFloat 32
  1148. %S = OpTypeStruct %float %float
  1149. %_ptr_Function_S = OpTypePointer Function %S
  1150. %S_0 = OpTypeStruct %float %float
  1151. %U_t = OpTypeStruct %S_0 %S_0 %S_0
  1152. %_ptr_Uniform_U_t = OpTypePointer Uniform %U_t
  1153. %_ = OpVariable %_ptr_Uniform_U_t Uniform
  1154. %int = OpTypeInt 32 1
  1155. %int_0 = OpConstant %int 0
  1156. %_ptr_Uniform_S_0 = OpTypePointer Uniform %S_0
  1157. %_ptr_Function_float = OpTypePointer Function %float
  1158. %int_1 = OpConstant %int 1
  1159. %uint = OpTypeInt 32 0
  1160. %_Globals_ = OpTypeStruct %uint
  1161. %_ptr_Uniform__Globals_ = OpTypePointer Uniform %_Globals_
  1162. %__0 = OpVariable %_ptr_Uniform__Globals_ Uniform
  1163. %_ptr_Uniform_uint = OpTypePointer Uniform %uint
  1164. %bool = OpTypeBool
  1165. %uint_0 = OpConstant %uint 0
  1166. %_ptr_Output_float = OpTypePointer Output %float
  1167. %fo = OpVariable %_ptr_Output_float Output
  1168. %main = OpFunction %void None %15
  1169. %30 = OpLabel
  1170. %ts1 = OpVariable %_ptr_Function_S Function
  1171. %ts2 = OpVariable %_ptr_Function_S Function
  1172. %ts3 = OpVariable %_ptr_Function_S Function
  1173. %ts4 = OpVariable %_ptr_Function_S Function
  1174. %31 = OpAccessChain %_ptr_Uniform_S_0 %_ %int_0
  1175. %32 = OpLoad %S_0 %31
  1176. %33 = OpCompositeExtract %float %32 0
  1177. %34 = OpAccessChain %_ptr_Function_float %ts1 %int_0
  1178. OpStore %34 %33
  1179. %35 = OpCompositeExtract %float %32 1
  1180. %36 = OpAccessChain %_ptr_Function_float %ts1 %int_1
  1181. OpStore %36 %35
  1182. %37 = OpAccessChain %_ptr_Uniform_S_0 %_ %int_1
  1183. %38 = OpLoad %S_0 %37
  1184. %39 = OpCompositeExtract %float %38 0
  1185. %40 = OpAccessChain %_ptr_Function_float %ts2 %int_0
  1186. OpStore %40 %39
  1187. %41 = OpCompositeExtract %float %38 1
  1188. %42 = OpAccessChain %_ptr_Function_float %ts2 %int_1
  1189. OpStore %42 %41
  1190. %43 = OpAccessChain %_ptr_Uniform_uint %__0 %int_0
  1191. %44 = OpLoad %uint %43
  1192. %45 = OpINotEqual %bool %44 %uint_0
  1193. OpSelectionMerge %46 None
  1194. OpBranchConditional %45 %47 %48
  1195. %47 = OpLabel
  1196. %49 = OpLoad %S %ts1
  1197. OpStore %ts3 %49
  1198. OpBranch %46
  1199. %48 = OpLabel
  1200. %50 = OpLoad %S %ts2
  1201. OpStore %ts3 %50
  1202. OpBranch %46
  1203. %46 = OpLabel
  1204. %51 = OpLoad %S %ts3
  1205. OpStore %ts4 %51
  1206. %52 = OpAccessChain %_ptr_Function_float %ts4 %int_1
  1207. %53 = OpLoad %float %52
  1208. OpStore %fo %53
  1209. OpReturn
  1210. OpFunctionEnd
  1211. )";
  1212. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  1213. }
  1214. TEST_F(ScalarReplacementTest, ReplaceWholeLoadAndStore) {
  1215. const std::string text = R"(
  1216. ;
  1217. ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
  1218. ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[uint]]
  1219. ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
  1220. ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
  1221. ; CHECK: [[undef:%\w+]] = OpUndef [[uint]]
  1222. ; CHECK: [[var0:%\w+]] = OpVariable [[uint_ptr]] Function
  1223. ; CHECK: [[var1:%\w+]] = OpVariable [[uint_ptr]] Function
  1224. ; CHECK-NOT: OpVariable
  1225. ; CHECK: [[l0:%\w+]] = OpLoad [[uint]] [[var0]]
  1226. ; CHECK: [[c0:%\w+]] = OpCompositeConstruct [[struct1]] [[l0]] [[undef]]
  1227. ; CHECK: [[e0:%\w+]] = OpCompositeExtract [[uint]] [[c0]] 0
  1228. ; CHECK: OpStore [[var1]] [[e0]]
  1229. ; CHECK: [[l1:%\w+]] = OpLoad [[uint]] [[var1]]
  1230. ; CHECK: [[c1:%\w+]] = OpCompositeConstruct [[struct1]] [[l1]] [[undef]]
  1231. ; CHECK: [[e1:%\w+]] = OpCompositeExtract [[uint]] [[c1]] 0
  1232. ;
  1233. OpCapability Shader
  1234. OpCapability Linkage
  1235. OpMemoryModel Logical GLSL450
  1236. OpName %func "replace_whole_load"
  1237. %void = OpTypeVoid
  1238. %uint = OpTypeInt 32 0
  1239. %struct1 = OpTypeStruct %uint %uint
  1240. %uint_ptr = OpTypePointer Function %uint
  1241. %struct1_ptr = OpTypePointer Function %struct1
  1242. %uint_0 = OpConstant %uint 0
  1243. %uint_1 = OpConstant %uint 1
  1244. %func = OpTypeFunction %void
  1245. %1 = OpFunction %void None %func
  1246. %2 = OpLabel
  1247. %var2 = OpVariable %struct1_ptr Function
  1248. %var1 = OpVariable %struct1_ptr Function
  1249. %load1 = OpLoad %struct1 %var1
  1250. OpStore %var2 %load1
  1251. %load2 = OpLoad %struct1 %var2
  1252. %3 = OpCompositeExtract %uint %load2 0
  1253. OpReturn
  1254. OpFunctionEnd
  1255. )";
  1256. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  1257. }
  1258. TEST_F(ScalarReplacementTest, ReplaceWholeLoadAndStore2) {
  1259. // TODO: We can improve this case by ensuring that |var2| is processed first.
  1260. const std::string text = R"(
  1261. ;
  1262. ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
  1263. ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[uint]]
  1264. ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
  1265. ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
  1266. ; CHECK: [[undef:%\w+]] = OpUndef [[uint]]
  1267. ; CHECK: [[var1:%\w+]] = OpVariable [[uint_ptr]] Function
  1268. ; CHECK: [[var0a:%\w+]] = OpVariable [[uint_ptr]] Function
  1269. ; CHECK: [[var0b:%\w+]] = OpVariable [[uint_ptr]] Function
  1270. ; CHECK-NOT: OpVariable
  1271. ; CHECK: [[l0a:%\w+]] = OpLoad [[uint]] [[var0a]]
  1272. ; CHECK: [[l0b:%\w+]] = OpLoad [[uint]] [[var0b]]
  1273. ; CHECK: [[c0:%\w+]] = OpCompositeConstruct [[struct1]] [[l0b]] [[l0a]]
  1274. ; CHECK: [[e0:%\w+]] = OpCompositeExtract [[uint]] [[c0]] 0
  1275. ; CHECK: OpStore [[var1]] [[e0]]
  1276. ; CHECK: [[l1:%\w+]] = OpLoad [[uint]] [[var1]]
  1277. ; CHECK: [[c1:%\w+]] = OpCompositeConstruct [[struct1]] [[l1]] [[undef]]
  1278. ; CHECK: [[e1:%\w+]] = OpCompositeExtract [[uint]] [[c1]] 0
  1279. ;
  1280. OpCapability Shader
  1281. OpCapability Linkage
  1282. OpMemoryModel Logical GLSL450
  1283. OpName %func "replace_whole_load"
  1284. %void = OpTypeVoid
  1285. %uint = OpTypeInt 32 0
  1286. %struct1 = OpTypeStruct %uint %uint
  1287. %uint_ptr = OpTypePointer Function %uint
  1288. %struct1_ptr = OpTypePointer Function %struct1
  1289. %uint_0 = OpConstant %uint 0
  1290. %uint_1 = OpConstant %uint 1
  1291. %func = OpTypeFunction %void
  1292. %1 = OpFunction %void None %func
  1293. %2 = OpLabel
  1294. %var1 = OpVariable %struct1_ptr Function
  1295. %var2 = OpVariable %struct1_ptr Function
  1296. %load1 = OpLoad %struct1 %var1
  1297. OpStore %var2 %load1
  1298. %load2 = OpLoad %struct1 %var2
  1299. %3 = OpCompositeExtract %uint %load2 0
  1300. OpReturn
  1301. OpFunctionEnd
  1302. )";
  1303. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  1304. }
  1305. TEST_F(ScalarReplacementTest, CreateAmbiguousNullConstant1) {
  1306. const std::string text = R"(
  1307. ;
  1308. ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
  1309. ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[struct_member:%\w+]]
  1310. ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
  1311. ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
  1312. ; CHECK: [[undef:%\w+]] = OpUndef [[struct_member]]
  1313. ; CHECK: [[var0a:%\w+]] = OpVariable [[uint_ptr]] Function
  1314. ; CHECK: [[var1:%\w+]] = OpVariable [[uint_ptr]] Function
  1315. ; CHECK: [[var0b:%\w+]] = OpVariable [[uint_ptr]] Function
  1316. ; CHECK-NOT: OpVariable
  1317. ; CHECK: OpStore [[var1]]
  1318. ; CHECK: [[l1:%\w+]] = OpLoad [[uint]] [[var1]]
  1319. ; CHECK: [[c1:%\w+]] = OpCompositeConstruct [[struct1]] [[l1]] [[undef]]
  1320. ; CHECK: [[e1:%\w+]] = OpCompositeExtract [[uint]] [[c1]] 0
  1321. ;
  1322. OpCapability Shader
  1323. OpCapability Linkage
  1324. OpMemoryModel Logical GLSL450
  1325. OpName %func "replace_whole_load"
  1326. %void = OpTypeVoid
  1327. %uint = OpTypeInt 32 0
  1328. %struct2 = OpTypeStruct %uint
  1329. %struct3 = OpTypeStruct %uint
  1330. %struct1 = OpTypeStruct %uint %struct2
  1331. %uint_ptr = OpTypePointer Function %uint
  1332. %struct1_ptr = OpTypePointer Function %struct1
  1333. %uint_0 = OpConstant %uint 0
  1334. %uint_1 = OpConstant %uint 1
  1335. %func = OpTypeFunction %void
  1336. %1 = OpFunction %void None %func
  1337. %2 = OpLabel
  1338. %var1 = OpVariable %struct1_ptr Function
  1339. %var2 = OpVariable %struct1_ptr Function
  1340. %load1 = OpLoad %struct1 %var1
  1341. OpStore %var2 %load1
  1342. %load2 = OpLoad %struct1 %var2
  1343. %3 = OpCompositeExtract %uint %load2 0
  1344. OpReturn
  1345. OpFunctionEnd
  1346. )";
  1347. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  1348. }
  1349. TEST_F(ScalarReplacementTest, SpecConstantArray) {
  1350. const std::string text = R"(
  1351. ; CHECK: [[int:%\w+]] = OpTypeInt
  1352. ; CHECK: [[spec_const:%\w+]] = OpSpecConstant [[int]] 4
  1353. ; CHECK: [[spec_op:%\w+]] = OpSpecConstantOp [[int]] IAdd [[spec_const]] [[spec_const]]
  1354. ; CHECK: [[array1:%\w+]] = OpTypeArray [[int]] [[spec_const]]
  1355. ; CHECK: [[array2:%\w+]] = OpTypeArray [[int]] [[spec_op]]
  1356. ; CHECK: [[ptr_array1:%\w+]] = OpTypePointer Function [[array1]]
  1357. ; CHECK: [[ptr_array2:%\w+]] = OpTypePointer Function [[array2]]
  1358. ; CHECK: OpLabel
  1359. ; CHECK-NEXT: OpVariable [[ptr_array1]] Function
  1360. ; CHECK-NEXT: OpVariable [[ptr_array2]] Function
  1361. ; CHECK-NOT: OpVariable
  1362. OpCapability Shader
  1363. OpCapability Linkage
  1364. OpMemoryModel Logical GLSL450
  1365. %void = OpTypeVoid
  1366. %void_fn = OpTypeFunction %void
  1367. %int = OpTypeInt 32 0
  1368. %spec_const = OpSpecConstant %int 4
  1369. %spec_op = OpSpecConstantOp %int IAdd %spec_const %spec_const
  1370. %array_1 = OpTypeArray %int %spec_const
  1371. %array_2 = OpTypeArray %int %spec_op
  1372. %ptr_array_1_Function = OpTypePointer Function %array_1
  1373. %ptr_array_2_Function = OpTypePointer Function %array_2
  1374. %func = OpFunction %void None %void_fn
  1375. %1 = OpLabel
  1376. %var_1 = OpVariable %ptr_array_1_Function Function
  1377. %var_2 = OpVariable %ptr_array_2_Function Function
  1378. OpReturn
  1379. OpFunctionEnd
  1380. )";
  1381. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  1382. }
  1383. TEST_F(ScalarReplacementTest, CreateAmbiguousNullConstant2) {
  1384. const std::string text = R"(
  1385. ;
  1386. ; CHECK: [[uint:%\w+]] = OpTypeInt 32 0
  1387. ; CHECK: [[struct1:%\w+]] = OpTypeStruct [[uint]] [[struct_member:%\w+]]
  1388. ; CHECK: [[uint_ptr:%\w+]] = OpTypePointer Function [[uint]]
  1389. ; CHECK: [[const:%\w+]] = OpConstant [[uint]] 0
  1390. ; CHECK: [[undef:%\w+]] = OpUndef [[struct_member]]
  1391. ; CHECK: [[var0a:%\w+]] = OpVariable [[uint_ptr]] Function
  1392. ; CHECK: [[var1:%\w+]] = OpVariable [[uint_ptr]] Function
  1393. ; CHECK: [[var0b:%\w+]] = OpVariable [[uint_ptr]] Function
  1394. ; CHECK: OpStore [[var1]]
  1395. ; CHECK: [[l1:%\w+]] = OpLoad [[uint]] [[var1]]
  1396. ; CHECK: [[c1:%\w+]] = OpCompositeConstruct [[struct1]] [[l1]] [[undef]]
  1397. ; CHECK: [[e1:%\w+]] = OpCompositeExtract [[uint]] [[c1]] 0
  1398. ;
  1399. OpCapability Shader
  1400. OpCapability Linkage
  1401. OpMemoryModel Logical GLSL450
  1402. OpName %func "replace_whole_load"
  1403. %void = OpTypeVoid
  1404. %uint = OpTypeInt 32 0
  1405. %struct3 = OpTypeStruct %uint
  1406. %struct2 = OpTypeStruct %uint
  1407. %struct1 = OpTypeStruct %uint %struct2
  1408. %uint_ptr = OpTypePointer Function %uint
  1409. %struct1_ptr = OpTypePointer Function %struct1
  1410. %uint_0 = OpConstant %uint 0
  1411. %uint_1 = OpConstant %uint 1
  1412. %func = OpTypeFunction %void
  1413. %1 = OpFunction %void None %func
  1414. %2 = OpLabel
  1415. %var1 = OpVariable %struct1_ptr Function
  1416. %var2 = OpVariable %struct1_ptr Function
  1417. %load1 = OpLoad %struct1 %var1
  1418. OpStore %var2 %load1
  1419. %load2 = OpLoad %struct1 %var2
  1420. %3 = OpCompositeExtract %uint %load2 0
  1421. OpReturn
  1422. OpFunctionEnd
  1423. )";
  1424. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  1425. }
  1426. // Test that a struct of size 4 is not replaced when there is a limit of 2.
  1427. TEST_F(ScalarReplacementTest, TestLimit) {
  1428. const std::string text = R"(
  1429. OpCapability Shader
  1430. OpCapability Linkage
  1431. OpMemoryModel Logical GLSL450
  1432. OpName %6 "simple_struct"
  1433. %1 = OpTypeVoid
  1434. %2 = OpTypeInt 32 0
  1435. %3 = OpTypeStruct %2 %2 %2 %2
  1436. %4 = OpTypePointer Function %3
  1437. %5 = OpTypePointer Function %2
  1438. %6 = OpTypeFunction %2
  1439. %7 = OpConstantNull %3
  1440. %8 = OpConstant %2 0
  1441. %9 = OpConstant %2 1
  1442. %10 = OpConstant %2 2
  1443. %11 = OpConstant %2 3
  1444. %12 = OpFunction %2 None %6
  1445. %13 = OpLabel
  1446. %14 = OpVariable %4 Function %7
  1447. %15 = OpInBoundsAccessChain %5 %14 %8
  1448. %16 = OpLoad %2 %15
  1449. %17 = OpAccessChain %5 %14 %10
  1450. %18 = OpLoad %2 %17
  1451. %19 = OpIAdd %2 %16 %18
  1452. OpReturnValue %19
  1453. OpFunctionEnd
  1454. )";
  1455. auto result =
  1456. SinglePassRunAndDisassemble<ScalarReplacementPass>(text, true, false, 2);
  1457. EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
  1458. }
  1459. // Test that a struct of size 4 is replaced when there is a limit of 0 (no
  1460. // limit). This is the same spir-v as a test above, so we do not check that it
  1461. // is correctly transformed. We leave that to the test above.
  1462. TEST_F(ScalarReplacementTest, TestUnimited) {
  1463. const std::string text = R"(
  1464. OpCapability Shader
  1465. OpCapability Linkage
  1466. OpMemoryModel Logical GLSL450
  1467. OpName %6 "simple_struct"
  1468. %1 = OpTypeVoid
  1469. %2 = OpTypeInt 32 0
  1470. %3 = OpTypeStruct %2 %2 %2 %2
  1471. %4 = OpTypePointer Function %3
  1472. %5 = OpTypePointer Function %2
  1473. %6 = OpTypeFunction %2
  1474. %7 = OpConstantNull %3
  1475. %8 = OpConstant %2 0
  1476. %9 = OpConstant %2 1
  1477. %10 = OpConstant %2 2
  1478. %11 = OpConstant %2 3
  1479. %12 = OpFunction %2 None %6
  1480. %13 = OpLabel
  1481. %14 = OpVariable %4 Function %7
  1482. %15 = OpInBoundsAccessChain %5 %14 %8
  1483. %16 = OpLoad %2 %15
  1484. %17 = OpAccessChain %5 %14 %10
  1485. %18 = OpLoad %2 %17
  1486. %19 = OpIAdd %2 %16 %18
  1487. OpReturnValue %19
  1488. OpFunctionEnd
  1489. )";
  1490. auto result =
  1491. SinglePassRunAndDisassemble<ScalarReplacementPass>(text, true, false, 0);
  1492. EXPECT_EQ(Pass::Status::SuccessWithChange, std::get<1>(result));
  1493. }
  1494. TEST_F(ScalarReplacementTest, AmbigousPointer) {
  1495. const std::string text = R"(
  1496. ; CHECK: [[s1:%\w+]] = OpTypeStruct %uint
  1497. ; CHECK: [[s2:%\w+]] = OpTypeStruct %uint
  1498. ; CHECK: [[s3:%\w+]] = OpTypeStruct [[s2]]
  1499. ; CHECK: [[s3_const:%\w+]] = OpConstantComposite [[s3]]
  1500. ; CHECK: [[s2_ptr:%\w+]] = OpTypePointer Function [[s2]]
  1501. ; CHECK: OpCompositeExtract [[s2]] [[s3_const]]
  1502. OpCapability Shader
  1503. %1 = OpExtInstImport "GLSL.std.450"
  1504. OpMemoryModel Logical GLSL450
  1505. OpEntryPoint Fragment %2 "main"
  1506. OpExecutionMode %2 OriginUpperLeft
  1507. OpSource ESSL 310
  1508. %void = OpTypeVoid
  1509. %5 = OpTypeFunction %void
  1510. %uint = OpTypeInt 32 0
  1511. %_struct_7 = OpTypeStruct %uint
  1512. %_struct_8 = OpTypeStruct %uint
  1513. %_struct_9 = OpTypeStruct %_struct_8
  1514. %uint_1 = OpConstant %uint 1
  1515. %11 = OpConstantComposite %_struct_8 %uint_1
  1516. %12 = OpConstantComposite %_struct_9 %11
  1517. %_ptr_Function__struct_9 = OpTypePointer Function %_struct_9
  1518. %_ptr_Function__struct_7 = OpTypePointer Function %_struct_7
  1519. %2 = OpFunction %void None %5
  1520. %15 = OpLabel
  1521. %var = OpVariable %_ptr_Function__struct_9 Function
  1522. OpStore %var %12
  1523. %ld = OpLoad %_struct_9 %var
  1524. %ex = OpCompositeExtract %_struct_8 %ld 0
  1525. OpReturn
  1526. OpFunctionEnd
  1527. )";
  1528. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  1529. }
  1530. // Test that scalar replacement does not crash when there is an OpAccessChain
  1531. // with no index. If we choose to handle this case in the future, then the
  1532. // result can change.
  1533. TEST_F(ScalarReplacementTest, TestAccessChainWithNoIndexes) {
  1534. const std::string text = R"(
  1535. OpCapability Shader
  1536. OpMemoryModel Logical GLSL450
  1537. OpEntryPoint Fragment %1 "main"
  1538. OpExecutionMode %1 OriginLowerLeft
  1539. %void = OpTypeVoid
  1540. %3 = OpTypeFunction %void
  1541. %float = OpTypeFloat 32
  1542. %_struct_5 = OpTypeStruct %float
  1543. %_ptr_Function__struct_5 = OpTypePointer Function %_struct_5
  1544. %1 = OpFunction %void None %3
  1545. %7 = OpLabel
  1546. %8 = OpVariable %_ptr_Function__struct_5 Function
  1547. %9 = OpAccessChain %_ptr_Function__struct_5 %8
  1548. OpReturn
  1549. OpFunctionEnd
  1550. )";
  1551. auto result =
  1552. SinglePassRunAndDisassemble<ScalarReplacementPass>(text, true, false);
  1553. EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
  1554. }
  1555. // Test that id overflow is handled gracefully.
  1556. TEST_F(ScalarReplacementTest, IdBoundOverflow1) {
  1557. const std::string text = R"(
  1558. OpCapability ImageQuery
  1559. OpMemoryModel Logical GLSL450
  1560. OpEntryPoint Fragment %4 "main"
  1561. OpExecutionMode %4 OriginUpperLeft
  1562. OpDecorate %4194302 DescriptorSet 1073495039
  1563. %2 = OpTypeVoid
  1564. %3 = OpTypeFunction %2
  1565. %6 = OpTypeFloat 32
  1566. %7 = OpTypeStruct %6 %6
  1567. %557056 = OpTypeStruct %7
  1568. %9 = OpTypePointer Function %7
  1569. %18 = OpTypeFunction %7 %9
  1570. %4 = OpFunction %2 Pure|Const %3
  1571. %1836763 = OpLabel
  1572. %4194302 = OpVariable %9 Function
  1573. %10 = OpVariable %9 Function
  1574. OpKill
  1575. %4194301 = OpLabel
  1576. %524296 = OpLoad %7 %4194302
  1577. OpKill
  1578. OpFunctionEnd
  1579. )";
  1580. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1581. std::vector<Message> messages = {
  1582. {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."},
  1583. {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."}};
  1584. SetMessageConsumer(GetTestMessageConsumer(messages));
  1585. auto result = SinglePassRunToBinary<ScalarReplacementPass>(text, true, false);
  1586. EXPECT_EQ(Pass::Status::Failure, std::get<1>(result));
  1587. }
  1588. // Test that id overflow is handled gracefully.
  1589. TEST_F(ScalarReplacementTest, IdBoundOverflow2) {
  1590. const std::string text = R"(
  1591. OpCapability Shader
  1592. OpMemoryModel Logical GLSL450
  1593. OpEntryPoint Fragment %4 "main" %17
  1594. OpExecutionMode %4 OriginUpperLeft
  1595. %2 = OpTypeVoid
  1596. %3 = OpTypeFunction %2
  1597. %6 = OpTypeFloat 32
  1598. %7 = OpTypeVector %6 4
  1599. %8 = OpTypeStruct %7
  1600. %9 = OpTypePointer Function %8
  1601. %16 = OpTypePointer Output %7
  1602. %21 = OpTypeInt 32 1
  1603. %22 = OpConstant %21 0
  1604. %23 = OpTypePointer Function %7
  1605. %17 = OpVariable %16 Output
  1606. %4 = OpFunction %2 None %3
  1607. %5 = OpLabel
  1608. %4194300 = OpVariable %23 Function
  1609. %10 = OpVariable %9 Function
  1610. %4194301 = OpAccessChain %23 %10 %22
  1611. %4194302 = OpLoad %7 %4194301
  1612. OpStore %4194300 %4194302
  1613. %15 = OpLoad %7 %4194300
  1614. OpStore %17 %15
  1615. OpReturn
  1616. OpFunctionEnd
  1617. )";
  1618. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1619. std::vector<Message> messages = {
  1620. {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."}};
  1621. SetMessageConsumer(GetTestMessageConsumer(messages));
  1622. auto result = SinglePassRunToBinary<ScalarReplacementPass>(text, true, false);
  1623. EXPECT_EQ(Pass::Status::Failure, std::get<1>(result));
  1624. }
  1625. // Test that id overflow is handled gracefully.
  1626. TEST_F(ScalarReplacementTest, IdBoundOverflow3) {
  1627. const std::string text = R"(
  1628. OpCapability InterpolationFunction
  1629. OpExtension "z"
  1630. OpMemoryModel Logical GLSL450
  1631. OpEntryPoint Fragment %4 "main"
  1632. OpExecutionMode %4 OriginUpperLeft
  1633. %2 = OpTypeVoid
  1634. %3 = OpTypeFunction %2
  1635. %6 = OpTypeFloat 32
  1636. %7 = OpTypeStruct %6 %6
  1637. %9 = OpTypePointer Function %7
  1638. %18 = OpTypeFunction %7 %9
  1639. %21 = OpTypeInt 32 0
  1640. %22 = OpConstant %21 4293000676
  1641. %4194302 = OpConstantNull %6
  1642. %4 = OpFunction %2 Inline|Pure %3
  1643. %786464 = OpLabel
  1644. %4194298 = OpVariable %9 Function
  1645. %10 = OpVariable %9 Function
  1646. %4194299 = OpUDiv %21 %22 %22
  1647. %4194300 = OpLoad %7 %10
  1648. %50959 = OpLoad %7 %4194298
  1649. OpKill
  1650. OpFunctionEnd
  1651. %1 = OpFunction %7 None %18
  1652. %19 = OpFunctionParameter %9
  1653. %147667 = OpLabel
  1654. %2044391 = OpUDiv %21 %22 %22
  1655. %25 = OpLoad %7 %19
  1656. OpReturnValue %25
  1657. OpFunctionEnd
  1658. %4194295 = OpFunction %2 None %3
  1659. %4194296 = OpLabel
  1660. OpKill
  1661. OpFunctionEnd
  1662. )";
  1663. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1664. std::vector<Message> messages = {
  1665. {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."},
  1666. {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."},
  1667. {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."},
  1668. {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."},
  1669. {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."},
  1670. {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."},
  1671. {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."},
  1672. {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."},
  1673. {SPV_MSG_ERROR, "", 0, 0, "ID overflow. Try running compact-ids."}};
  1674. SetMessageConsumer(GetTestMessageConsumer(messages));
  1675. auto result = SinglePassRunToBinary<ScalarReplacementPass>(text, true, false);
  1676. EXPECT_EQ(Pass::Status::Failure, std::get<1>(result));
  1677. }
  1678. // Test that replacements for OpAccessChain do not go out of bounds.
  1679. // https://github.com/KhronosGroup/SPIRV-Tools/issues/2609.
  1680. TEST_F(ScalarReplacementTest, OutOfBoundOpAccessChain) {
  1681. const std::string text = R"(
  1682. OpCapability Shader
  1683. %1 = OpExtInstImport "GLSL.std.450"
  1684. OpMemoryModel Logical GLSL450
  1685. OpEntryPoint Fragment %main "main" %_GLF_color
  1686. OpExecutionMode %main OriginUpperLeft
  1687. OpSource ESSL 310
  1688. OpName %main "main"
  1689. OpName %a "a"
  1690. OpName %_GLF_color "_GLF_color"
  1691. OpDecorate %_GLF_color Location 0
  1692. %void = OpTypeVoid
  1693. %3 = OpTypeFunction %void
  1694. %int = OpTypeInt 32 1
  1695. %_ptr_Function_int = OpTypePointer Function %int
  1696. %int_1 = OpConstant %int 1
  1697. %float = OpTypeFloat 32
  1698. %uint = OpTypeInt 32 0
  1699. %uint_1 = OpConstant %uint 1
  1700. %_arr_float_uint_1 = OpTypeArray %float %uint_1
  1701. %_ptr_Function__arr_float_uint_1 = OpTypePointer Function %_arr_float_uint_1
  1702. %_ptr_Function_float = OpTypePointer Function %float
  1703. %_ptr_Output_float = OpTypePointer Output %float
  1704. %_GLF_color = OpVariable %_ptr_Output_float Output
  1705. %main = OpFunction %void None %3
  1706. %5 = OpLabel
  1707. %a = OpVariable %_ptr_Function__arr_float_uint_1 Function
  1708. %21 = OpAccessChain %_ptr_Function_float %a %int_1
  1709. %22 = OpLoad %float %21
  1710. OpStore %_GLF_color %22
  1711. OpReturn
  1712. OpFunctionEnd
  1713. )";
  1714. SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  1715. auto result =
  1716. SinglePassRunAndDisassemble<ScalarReplacementPass>(text, true, false);
  1717. EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
  1718. }
  1719. TEST_F(ScalarReplacementTest, CharIndex) {
  1720. const std::string text = R"(
  1721. ; CHECK: [[int:%\w+]] = OpTypeInt 32 0
  1722. ; CHECK: [[ptr:%\w+]] = OpTypePointer Function [[int]]
  1723. ; CHECK: OpVariable [[ptr]] Function
  1724. OpCapability Shader
  1725. OpCapability Int8
  1726. OpMemoryModel Logical GLSL450
  1727. OpEntryPoint GLCompute %main "main"
  1728. OpExecutionMode %main LocalSize 1 1 1
  1729. %void = OpTypeVoid
  1730. %int = OpTypeInt 32 0
  1731. %int_1024 = OpConstant %int 1024
  1732. %char = OpTypeInt 8 0
  1733. %char_1 = OpConstant %char 1
  1734. %array = OpTypeArray %int %int_1024
  1735. %ptr_func_array = OpTypePointer Function %array
  1736. %ptr_func_int = OpTypePointer Function %int
  1737. %void_fn = OpTypeFunction %void
  1738. %main = OpFunction %void None %void_fn
  1739. %entry = OpLabel
  1740. %var = OpVariable %ptr_func_array Function
  1741. %gep = OpAccessChain %ptr_func_int %var %char_1
  1742. OpStore %gep %int_1024
  1743. OpReturn
  1744. OpFunctionEnd
  1745. )";
  1746. SinglePassRunAndMatch<ScalarReplacementPass>(text, true, 0);
  1747. }
  1748. TEST_F(ScalarReplacementTest, OutOfBoundsOpAccessChainNegative) {
  1749. const std::string text = R"(
  1750. OpCapability Shader
  1751. OpCapability Int8
  1752. OpMemoryModel Logical GLSL450
  1753. OpEntryPoint GLCompute %main "main"
  1754. OpExecutionMode %main LocalSize 1 1 1
  1755. %void = OpTypeVoid
  1756. %int = OpTypeInt 32 0
  1757. %int_1024 = OpConstant %int 1024
  1758. %char = OpTypeInt 8 1
  1759. %char_n1 = OpConstant %char -1
  1760. %array = OpTypeArray %int %int_1024
  1761. %ptr_func_array = OpTypePointer Function %array
  1762. %ptr_func_int = OpTypePointer Function %int
  1763. %void_fn = OpTypeFunction %void
  1764. %main = OpFunction %void None %void_fn
  1765. %entry = OpLabel
  1766. %var = OpVariable %ptr_func_array Function
  1767. %gep = OpAccessChain %ptr_func_int %var %char_n1
  1768. OpStore %gep %int_1024
  1769. OpReturn
  1770. OpFunctionEnd
  1771. )";
  1772. auto result =
  1773. SinglePassRunAndDisassemble<ScalarReplacementPass>(text, true, true, 0);
  1774. EXPECT_EQ(Pass::Status::SuccessWithoutChange, std::get<1>(result));
  1775. }
  1776. TEST_F(ScalarReplacementTest, RelaxedPrecisionMemberDecoration) {
  1777. const std::string text = R"(
  1778. ; CHECK: OpDecorate {{%\w+}} RelaxedPrecision
  1779. ; CHECK: OpDecorate [[new_var:%\w+]] RelaxedPrecision
  1780. ; CHECK: [[new_var]] = OpVariable %_ptr_Function_v3float Function
  1781. ; CHECK: OpLoad %v3float [[new_var]]
  1782. OpCapability Shader
  1783. OpMemoryModel Logical GLSL450
  1784. OpEntryPoint Vertex %1 "Draw2DTexCol_VS" %2 %3
  1785. OpSource HLSL 600
  1786. OpDecorate %2 Location 0
  1787. OpDecorate %3 Location 1
  1788. OpDecorate %3 RelaxedPrecision
  1789. OpMemberDecorate %_struct_4 1 RelaxedPrecision
  1790. %float = OpTypeFloat 32
  1791. %int = OpTypeInt 32 1
  1792. %int_1 = OpConstant %int 1
  1793. %v3float = OpTypeVector %float 3
  1794. %_ptr_Input_v3float = OpTypePointer Input %v3float
  1795. %void = OpTypeVoid
  1796. %11 = OpTypeFunction %void
  1797. %_struct_4 = OpTypeStruct %v3float %v3float
  1798. %_ptr_Function__struct_4 = OpTypePointer Function %_struct_4
  1799. %_ptr_Function_v3float = OpTypePointer Function %v3float
  1800. %2 = OpVariable %_ptr_Input_v3float Input
  1801. %3 = OpVariable %_ptr_Input_v3float Input
  1802. %1 = OpFunction %void None %11
  1803. %14 = OpLabel
  1804. %15 = OpVariable %_ptr_Function__struct_4 Function
  1805. %16 = OpLoad %v3float %2
  1806. %17 = OpLoad %v3float %3
  1807. %18 = OpCompositeConstruct %_struct_4 %16 %17
  1808. OpStore %15 %18
  1809. %19 = OpAccessChain %_ptr_Function_v3float %15 %int_1
  1810. %20 = OpLoad %v3float %19
  1811. OpReturn
  1812. OpFunctionEnd
  1813. )";
  1814. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  1815. }
  1816. TEST_F(ScalarReplacementTest, DebugDeclare) {
  1817. const std::string text = R"(
  1818. OpCapability Shader
  1819. OpCapability Linkage
  1820. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  1821. OpMemoryModel Logical GLSL450
  1822. %test = OpString "test"
  1823. OpName %6 "simple_struct"
  1824. %1 = OpTypeVoid
  1825. %2 = OpTypeInt 32 0
  1826. %uint_32 = OpConstant %2 32
  1827. %3 = OpTypeStruct %2 %2 %2 %2
  1828. %4 = OpTypePointer Function %3
  1829. %5 = OpTypePointer Function %2
  1830. %6 = OpTypeFunction %2
  1831. %7 = OpConstantNull %3
  1832. %8 = OpConstant %2 0
  1833. %9 = OpConstant %2 1
  1834. %10 = OpConstant %2 2
  1835. %11 = OpConstant %2 3
  1836. %null_expr = OpExtInst %1 %ext DebugExpression
  1837. %src = OpExtInst %1 %ext DebugSource %test
  1838. %cu = OpExtInst %1 %ext DebugCompilationUnit 1 4 %src HLSL
  1839. %dbg_tf = OpExtInst %1 %ext DebugTypeBasic %test %uint_32 Float
  1840. %main_ty = OpExtInst %1 %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %1
  1841. %dbg_main = OpExtInst %1 %ext DebugFunction %test %main_ty %src 0 0 %cu %test FlagIsProtected|FlagIsPrivate 0 %12
  1842. %dbg_foo = OpExtInst %1 %ext DebugLocalVariable %test %dbg_tf %src 0 0 %dbg_main FlagIsLocal
  1843. %12 = OpFunction %2 None %6
  1844. %13 = OpLabel
  1845. %scope = OpExtInst %1 %ext DebugScope %dbg_main
  1846. %14 = OpVariable %4 Function %7
  1847. ; CHECK: [[deref:%\w+]] = OpExtInst %void [[ext:%\w+]] DebugOperation Deref
  1848. ; CHECK: [[dbg_local_var:%\w+]] = OpExtInst %void [[ext]] DebugLocalVariable
  1849. ; CHECK: [[deref_expr:%\w+]] = OpExtInst %void [[ext]] DebugExpression [[deref]]
  1850. ; CHECK: [[repl3:%\w+]] = OpVariable %_ptr_Function_uint Function
  1851. ; CHECK: [[repl2:%\w+]] = OpVariable %_ptr_Function_uint Function
  1852. ; CHECK: [[repl1:%\w+]] = OpVariable %_ptr_Function_uint Function
  1853. ; CHECK: [[repl0:%\w+]] = OpVariable %_ptr_Function_uint Function
  1854. ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl3]] [[deref_expr]] %int_3
  1855. ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl2]] [[deref_expr]] %int_2
  1856. ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl1]] [[deref_expr]] %int_1
  1857. ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl0]] [[deref_expr]] %int_0
  1858. ; CHECK-NOT: DebugDeclare
  1859. %decl = OpExtInst %1 %ext DebugDeclare %dbg_foo %14 %null_expr
  1860. %15 = OpInBoundsAccessChain %5 %14 %8
  1861. %16 = OpLoad %2 %15
  1862. %17 = OpAccessChain %5 %14 %10
  1863. %18 = OpLoad %2 %17
  1864. %19 = OpIAdd %2 %16 %18
  1865. OpReturnValue %19
  1866. OpFunctionEnd
  1867. )";
  1868. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  1869. }
  1870. TEST_F(ScalarReplacementTest, DebugValue) {
  1871. const std::string text = R"(
  1872. OpCapability Shader
  1873. OpCapability Linkage
  1874. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  1875. OpMemoryModel Logical GLSL450
  1876. %test = OpString "test"
  1877. OpName %6 "simple_struct"
  1878. %1 = OpTypeVoid
  1879. %2 = OpTypeInt 32 0
  1880. %uint_32 = OpConstant %2 32
  1881. %3 = OpTypeStruct %2 %2 %2 %2
  1882. %4 = OpTypePointer Function %3
  1883. %5 = OpTypePointer Function %2
  1884. %6 = OpTypeFunction %2
  1885. %7 = OpConstantNull %3
  1886. %8 = OpConstant %2 0
  1887. %9 = OpConstant %2 1
  1888. %10 = OpConstant %2 2
  1889. %11 = OpConstant %2 3
  1890. %deref = OpExtInst %1 %ext DebugOperation Deref
  1891. %deref_expr = OpExtInst %1 %ext DebugExpression %deref
  1892. %null_expr = OpExtInst %1 %ext DebugExpression
  1893. %src = OpExtInst %1 %ext DebugSource %test
  1894. %cu = OpExtInst %1 %ext DebugCompilationUnit 1 4 %src HLSL
  1895. %dbg_tf = OpExtInst %1 %ext DebugTypeBasic %test %uint_32 Float
  1896. %main_ty = OpExtInst %1 %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %1
  1897. %dbg_main = OpExtInst %1 %ext DebugFunction %test %main_ty %src 0 0 %cu %test FlagIsProtected|FlagIsPrivate 0 %12
  1898. %dbg_foo = OpExtInst %1 %ext DebugLocalVariable %test %dbg_tf %src 0 0 %dbg_main FlagIsLocal
  1899. %12 = OpFunction %2 None %6
  1900. %13 = OpLabel
  1901. %scope = OpExtInst %1 %ext DebugScope %dbg_main
  1902. %14 = OpVariable %4 Function %7
  1903. ; CHECK: [[deref:%\w+]] = OpExtInst %void [[ext:%\w+]] DebugOperation Deref
  1904. ; CHECK: [[deref_expr:%\w+]] = OpExtInst %void [[ext]] DebugExpression [[deref]]
  1905. ; CHECK: [[dbg_local_var:%\w+]] = OpExtInst %void [[ext]] DebugLocalVariable
  1906. ; CHECK: [[repl3:%\w+]] = OpVariable %_ptr_Function_uint Function
  1907. ; CHECK: [[repl2:%\w+]] = OpVariable %_ptr_Function_uint Function
  1908. ; CHECK: [[repl1:%\w+]] = OpVariable %_ptr_Function_uint Function
  1909. ; CHECK: [[repl0:%\w+]] = OpVariable %_ptr_Function_uint Function
  1910. ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl0]] [[deref_expr]] %int_0
  1911. ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl1]] [[deref_expr]] %int_1
  1912. ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl2]] [[deref_expr]] %int_2
  1913. ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl3]] [[deref_expr]] %int_3
  1914. %value = OpExtInst %1 %ext DebugValue %dbg_foo %14 %deref_expr
  1915. %15 = OpInBoundsAccessChain %5 %14 %8
  1916. %16 = OpLoad %2 %15
  1917. %17 = OpAccessChain %5 %14 %10
  1918. %18 = OpLoad %2 %17
  1919. %19 = OpIAdd %2 %16 %18
  1920. OpReturnValue %19
  1921. OpFunctionEnd
  1922. )";
  1923. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  1924. }
  1925. TEST_F(ScalarReplacementTest, DebugDeclareRecursive) {
  1926. const std::string text = R"(
  1927. OpCapability Shader
  1928. OpCapability Linkage
  1929. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  1930. OpMemoryModel Logical GLSL450
  1931. %test = OpString "test"
  1932. OpName %6 "simple_struct"
  1933. %1 = OpTypeVoid
  1934. %2 = OpTypeInt 32 0
  1935. %uint_32 = OpConstant %2 32
  1936. %float = OpTypeFloat 32
  1937. %float_1 = OpConstant %float 1
  1938. %member = OpTypeStruct %2 %float
  1939. %3 = OpTypeStruct %2 %member %float
  1940. %4 = OpTypePointer Function %3
  1941. %5 = OpTypePointer Function %2
  1942. %ptr_float_Function = OpTypePointer Function %float
  1943. %6 = OpTypeFunction %2
  1944. %cmember = OpConstantComposite %member %uint_32 %float_1
  1945. %7 = OpConstantComposite %3 %uint_32 %cmember %float_1
  1946. %8 = OpConstant %2 0
  1947. %9 = OpConstant %2 1
  1948. %10 = OpConstant %2 2
  1949. %null_expr = OpExtInst %1 %ext DebugExpression
  1950. %src = OpExtInst %1 %ext DebugSource %test
  1951. %cu = OpExtInst %1 %ext DebugCompilationUnit 1 4 %src HLSL
  1952. %dbg_tf = OpExtInst %1 %ext DebugTypeBasic %test %uint_32 Float
  1953. %main_ty = OpExtInst %1 %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %1
  1954. %dbg_main = OpExtInst %1 %ext DebugFunction %test %main_ty %src 0 0 %cu %test FlagIsProtected|FlagIsPrivate 0 %12
  1955. %dbg_foo = OpExtInst %1 %ext DebugLocalVariable %test %dbg_tf %src 0 0 %dbg_main FlagIsLocal
  1956. %12 = OpFunction %2 None %6
  1957. %13 = OpLabel
  1958. %scope = OpExtInst %1 %ext DebugScope %dbg_main
  1959. %14 = OpVariable %4 Function %7
  1960. ; CHECK: [[deref:%\w+]] = OpExtInst %void [[ext:%\w+]] DebugOperation Deref
  1961. ; CHECK: [[dbg_local_var:%\w+]] = OpExtInst %void [[ext]] DebugLocalVariable
  1962. ; CHECK: [[deref_expr:%\w+]] = OpExtInst %void [[ext]] DebugExpression [[deref]]
  1963. ; CHECK: [[repl2:%\w+]] = OpVariable %_ptr_Function_float Function %float_1
  1964. ; CHECK: [[repl1:%\w+]] = OpVariable %_ptr_Function_uint Function %uint_32
  1965. ; CHECK: [[repl3:%\w+]] = OpVariable %_ptr_Function_float Function %float_1
  1966. ; CHECK: [[repl0:%\w+]] = OpVariable %_ptr_Function_uint Function %uint_32
  1967. ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl3]] [[deref_expr]] %int_2
  1968. ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl1]] [[deref_expr]] %int_1 %int_0
  1969. ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl2]] [[deref_expr]] %int_1 %int_1
  1970. ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl0]] [[deref_expr]] %int_0
  1971. ; CHECK-NOT: DebugDeclare
  1972. %decl = OpExtInst %1 %ext DebugDeclare %dbg_foo %14 %null_expr
  1973. %15 = OpInBoundsAccessChain %5 %14 %8
  1974. %16 = OpLoad %2 %15
  1975. %17 = OpAccessChain %ptr_float_Function %14 %10
  1976. %18 = OpLoad %float %17
  1977. %value = OpConvertFToU %2 %18
  1978. %19 = OpIAdd %2 %16 %value
  1979. OpReturnValue %19
  1980. OpFunctionEnd
  1981. )";
  1982. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  1983. }
  1984. TEST_F(ScalarReplacementTest, DebugValueWithIndex) {
  1985. const std::string text = R"(
  1986. OpCapability Shader
  1987. OpCapability Linkage
  1988. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  1989. OpMemoryModel Logical GLSL450
  1990. %test = OpString "test"
  1991. OpName %6 "simple_struct"
  1992. %1 = OpTypeVoid
  1993. %2 = OpTypeInt 32 0
  1994. %uint_32 = OpConstant %2 32
  1995. %3 = OpTypeStruct %2 %2 %2 %2
  1996. %4 = OpTypePointer Function %3
  1997. %5 = OpTypePointer Function %2
  1998. %6 = OpTypeFunction %2
  1999. %7 = OpConstantNull %3
  2000. %8 = OpConstant %2 0
  2001. %9 = OpConstant %2 1
  2002. %10 = OpConstant %2 2
  2003. %11 = OpConstant %2 3
  2004. %deref = OpExtInst %1 %ext DebugOperation Deref
  2005. %deref_expr = OpExtInst %1 %ext DebugExpression %deref
  2006. %null_expr = OpExtInst %1 %ext DebugExpression
  2007. %src = OpExtInst %1 %ext DebugSource %test
  2008. %cu = OpExtInst %1 %ext DebugCompilationUnit 1 4 %src HLSL
  2009. %dbg_tf = OpExtInst %1 %ext DebugTypeBasic %test %uint_32 Float
  2010. %main_ty = OpExtInst %1 %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %1
  2011. %dbg_main = OpExtInst %1 %ext DebugFunction %test %main_ty %src 0 0 %cu %test FlagIsProtected|FlagIsPrivate 0 %12
  2012. %dbg_foo = OpExtInst %1 %ext DebugLocalVariable %test %dbg_tf %src 0 0 %dbg_main FlagIsLocal
  2013. %12 = OpFunction %2 None %6
  2014. %13 = OpLabel
  2015. %scope = OpExtInst %1 %ext DebugScope %dbg_main
  2016. %14 = OpVariable %4 Function %7
  2017. ; CHECK: [[deref:%\w+]] = OpExtInst %void [[ext:%\w+]] DebugOperation Deref
  2018. ; CHECK: [[deref_expr:%\w+]] = OpExtInst %void [[ext]] DebugExpression [[deref]]
  2019. ; CHECK: [[dbg_local_var:%\w+]] = OpExtInst %void [[ext]] DebugLocalVariable
  2020. ; CHECK: [[repl3:%\w+]] = OpVariable %_ptr_Function_uint Function
  2021. ; CHECK: [[repl2:%\w+]] = OpVariable %_ptr_Function_uint Function
  2022. ; CHECK: [[repl1:%\w+]] = OpVariable %_ptr_Function_uint Function
  2023. ; CHECK: [[repl0:%\w+]] = OpVariable %_ptr_Function_uint Function
  2024. ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl0]] [[deref_expr]] %uint_0 %uint_1 %uint_2 %int_0
  2025. ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl1]] [[deref_expr]] %uint_0 %uint_1 %uint_2 %int_1
  2026. ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl2]] [[deref_expr]] %uint_0 %uint_1 %uint_2 %int_2
  2027. ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl3]] [[deref_expr]] %uint_0 %uint_1 %uint_2 %int_3
  2028. %value = OpExtInst %1 %ext DebugValue %dbg_foo %14 %deref_expr %8 %9 %10
  2029. %15 = OpInBoundsAccessChain %5 %14 %8
  2030. %16 = OpLoad %2 %15
  2031. %17 = OpAccessChain %5 %14 %10
  2032. %18 = OpLoad %2 %17
  2033. %19 = OpIAdd %2 %16 %18
  2034. OpReturnValue %19
  2035. OpFunctionEnd
  2036. )";
  2037. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  2038. }
  2039. TEST_F(ScalarReplacementTest, DebugDeclareForVariableInOtherBB) {
  2040. const std::string text = R"(
  2041. OpCapability Shader
  2042. OpCapability Linkage
  2043. %ext = OpExtInstImport "OpenCL.DebugInfo.100"
  2044. OpMemoryModel Logical GLSL450
  2045. %test = OpString "test"
  2046. OpName %6 "simple_struct"
  2047. %1 = OpTypeVoid
  2048. %2 = OpTypeInt 32 0
  2049. %uint_32 = OpConstant %2 32
  2050. %3 = OpTypeStruct %2 %2 %2 %2
  2051. %4 = OpTypePointer Function %3
  2052. %5 = OpTypePointer Function %2
  2053. %6 = OpTypeFunction %2
  2054. %7 = OpConstantNull %3
  2055. %8 = OpConstant %2 0
  2056. %9 = OpConstant %2 1
  2057. %10 = OpConstant %2 2
  2058. %11 = OpConstant %2 3
  2059. %deref = OpExtInst %1 %ext DebugOperation Deref
  2060. %deref_expr = OpExtInst %1 %ext DebugExpression %deref
  2061. %null_expr = OpExtInst %1 %ext DebugExpression
  2062. %src = OpExtInst %1 %ext DebugSource %test
  2063. %cu = OpExtInst %1 %ext DebugCompilationUnit 1 4 %src HLSL
  2064. %dbg_tf = OpExtInst %1 %ext DebugTypeBasic %test %uint_32 Float
  2065. %main_ty = OpExtInst %1 %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %1
  2066. %dbg_main = OpExtInst %1 %ext DebugFunction %test %main_ty %src 0 0 %cu %test FlagIsProtected|FlagIsPrivate 0 %12
  2067. %dbg_foo = OpExtInst %1 %ext DebugLocalVariable %test %dbg_tf %src 0 0 %dbg_main FlagIsLocal
  2068. %12 = OpFunction %2 None %6
  2069. %13 = OpLabel
  2070. %scope = OpExtInst %1 %ext DebugScope %dbg_main
  2071. %14 = OpVariable %4 Function %7
  2072. ; CHECK: [[dbg_local_var:%\w+]] = OpExtInst %void [[ext:%\w+]] DebugLocalVariable
  2073. ; CHECK: [[repl3:%\w+]] = OpVariable %_ptr_Function_uint Function
  2074. ; CHECK: [[repl2:%\w+]] = OpVariable %_ptr_Function_uint Function
  2075. ; CHECK: [[repl1:%\w+]] = OpVariable %_ptr_Function_uint Function
  2076. ; CHECK: [[repl0:%\w+]] = OpVariable %_ptr_Function_uint Function
  2077. ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl3]] [[deref_expr:%\w+]] %int_3
  2078. ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl2]] [[deref_expr]] %int_2
  2079. ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl1]] [[deref_expr]] %int_1
  2080. ; CHECK: OpExtInst %void [[ext]] DebugValue [[dbg_local_var]] [[repl0]] [[deref_expr]] %int_0
  2081. OpBranch %20
  2082. %20 = OpLabel
  2083. %value = OpExtInst %1 %ext DebugDeclare %dbg_foo %14 %null_expr
  2084. %15 = OpInBoundsAccessChain %5 %14 %8
  2085. %16 = OpLoad %2 %15
  2086. %17 = OpAccessChain %5 %14 %10
  2087. %18 = OpLoad %2 %17
  2088. %19 = OpIAdd %2 %16 %18
  2089. OpReturnValue %19
  2090. OpFunctionEnd
  2091. )";
  2092. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  2093. }
  2094. TEST_F(ScalarReplacementTest, ImageTexelPointer) {
  2095. // Test whether the scalar replacement correctly checks the
  2096. // OpImageTexelPointer user of an aggregate with an image type.
  2097. const std::string text = R"(
  2098. ;
  2099. ; CHECK: [[imgTy:%\w+]] = OpTypeImage %uint Buffer 2 0 0 2 R32ui
  2100. ; CHECK: [[ptrImgTy:%\w+]] = OpTypePointer Function [[imgTy]]
  2101. ; CHECK: [[img:%\w+]] = OpVariable [[ptrImgTy]] Function
  2102. ; CHECK: [[imgTexelPtr:%\w+]] = OpImageTexelPointer {{%\w+}} [[img]] %uint_0 %uint_0
  2103. ; CHECK: OpAtomicIAdd %uint [[imgTexelPtr]] %uint_1 %uint_0 %uint_1
  2104. ;
  2105. OpCapability Shader
  2106. OpCapability SampledBuffer
  2107. OpCapability ImageBuffer
  2108. OpMemoryModel Logical GLSL450
  2109. OpEntryPoint GLCompute %1 "main"
  2110. OpExecutionMode %1 LocalSize 64 1 1
  2111. %void = OpTypeVoid
  2112. %uint = OpTypeInt 32 0
  2113. %uint_0 = OpConstant %uint 0
  2114. %uint_1 = OpConstant %uint 1
  2115. %_ptr_Image_uint = OpTypePointer Image %uint
  2116. %type_buffer_image = OpTypeImage %uint Buffer 2 0 0 2 R32ui
  2117. %_ptr_Function_type_buffer_image = OpTypePointer Function %type_buffer_image
  2118. %image_struct = OpTypeStruct %type_buffer_image %type_buffer_image
  2119. %_ptr_Function_image_struct = OpTypePointer Function %image_struct
  2120. %func = OpTypeFunction %void
  2121. %1 = OpFunction %void None %func
  2122. %2 = OpLabel
  2123. %3 = OpVariable %_ptr_Function_image_struct Function
  2124. %4 = OpAccessChain %_ptr_Function_type_buffer_image %3 %uint_1
  2125. %5 = OpImageTexelPointer %_ptr_Image_uint %4 %uint_0 %uint_0
  2126. %6 = OpAtomicIAdd %uint %5 %uint_1 %uint_0 %uint_1
  2127. OpReturn
  2128. OpFunctionEnd
  2129. )";
  2130. SinglePassRunAndMatch<ScalarReplacementPass>(text, false);
  2131. }
  2132. TEST_F(ScalarReplacementTest, FunctionDeclaration) {
  2133. // Make sure the pass works with a function declaration that is called.
  2134. const std::string text = R"(OpCapability Addresses
  2135. OpCapability Linkage
  2136. OpCapability Kernel
  2137. OpCapability Int8
  2138. %1 = OpExtInstImport "OpenCL.std"
  2139. OpMemoryModel Physical64 OpenCL
  2140. OpEntryPoint Kernel %2 "_Z23julia__1166_kernel_77094Bool"
  2141. OpExecutionMode %2 ContractionOff
  2142. OpSource Unknown 0
  2143. OpDecorate %3 LinkageAttributes "julia_error_7712" Import
  2144. %void = OpTypeVoid
  2145. %5 = OpTypeFunction %void
  2146. %3 = OpFunction %void None %5
  2147. OpFunctionEnd
  2148. %2 = OpFunction %void None %5
  2149. %6 = OpLabel
  2150. %7 = OpFunctionCall %void %3
  2151. OpReturn
  2152. OpFunctionEnd
  2153. )";
  2154. SinglePassRunAndCheck<ScalarReplacementPass>(text, text, false);
  2155. }
  2156. TEST_F(ScalarReplacementTest, UndefImageMember) {
  2157. // Test that scalar replacement creates an undef for a type that cannot have
  2158. // and OpConstantNull.
  2159. const std::string text = R"(
  2160. ; CHECK: [[image_type:%\w+]] = OpTypeSampledImage {{%\w+}}
  2161. ; CHECK: [[struct_type:%\w+]] = OpTypeStruct [[image_type]]
  2162. ; CHECK: [[undef:%\w+]] = OpUndef [[image_type]]
  2163. ; CHECK: {{%\w+}} = OpCompositeConstruct [[struct_type]] [[undef]]
  2164. OpCapability Shader
  2165. %1 = OpExtInstImport "GLSL.std.450"
  2166. OpMemoryModel Logical GLSL450
  2167. OpEntryPoint Fragment %2 "main"
  2168. OpExecutionMode %2 OriginUpperLeft
  2169. %void = OpTypeVoid
  2170. %4 = OpTypeFunction %void
  2171. %float = OpTypeFloat 32
  2172. %6 = OpTypeImage %float 2D 0 0 0 1 Unknown
  2173. %7 = OpTypeSampledImage %6
  2174. %_struct_8 = OpTypeStruct %7
  2175. %9 = OpTypeFunction %_struct_8
  2176. %10 = OpUndef %_struct_8
  2177. %_ptr_Function__struct_8 = OpTypePointer Function %_struct_8
  2178. %2 = OpFunction %void None %4
  2179. %11 = OpLabel
  2180. %16 = OpVariable %_ptr_Function__struct_8 Function
  2181. OpStore %16 %10
  2182. %12 = OpLoad %_struct_8 %16
  2183. OpReturn
  2184. OpFunctionEnd
  2185. )";
  2186. SinglePassRunAndMatch<ScalarReplacementPass>(text, true);
  2187. }
  2188. } // namespace
  2189. } // namespace opt
  2190. } // namespace spvtools