graphics_robust_access_test.cpp 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562
  1. // Copyright (c) 2019 Google LLC
  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 <array>
  15. #include <sstream>
  16. #include <string>
  17. #include <vector>
  18. #include "gmock/gmock.h"
  19. #include "pass_fixture.h"
  20. #include "pass_utils.h"
  21. #include "source/opt/graphics_robust_access_pass.h"
  22. namespace {
  23. using namespace spvtools;
  24. using opt::GraphicsRobustAccessPass;
  25. using GraphicsRobustAccessTest = opt::PassTest<::testing::Test>;
  26. // Test incompatible module, determined at module-level.
  27. TEST_F(GraphicsRobustAccessTest, FailNotShader) {
  28. const std::string text = R"(
  29. ; CHECK: Can only process Shader modules
  30. OpCapability Kernel
  31. )";
  32. SinglePassRunAndFail<GraphicsRobustAccessPass>(text);
  33. }
  34. TEST_F(GraphicsRobustAccessTest, FailCantProcessVariablePointers) {
  35. const std::string text = R"(
  36. ; CHECK: Can't process modules with VariablePointers capability
  37. OpCapability VariablePointers
  38. )";
  39. SinglePassRunAndFail<GraphicsRobustAccessPass>(text);
  40. }
  41. TEST_F(GraphicsRobustAccessTest, FailCantProcessVariablePointersStorageBuffer) {
  42. const std::string text = R"(
  43. ; CHECK: Can't process modules with VariablePointersStorageBuffer capability
  44. OpCapability VariablePointersStorageBuffer
  45. )";
  46. SinglePassRunAndFail<GraphicsRobustAccessPass>(text);
  47. }
  48. TEST_F(GraphicsRobustAccessTest, FailCantProcessRuntimeDescriptorArrayEXT) {
  49. const std::string text = R"(
  50. ; CHECK: Can't process modules with RuntimeDescriptorArrayEXT capability
  51. OpCapability RuntimeDescriptorArrayEXT
  52. )";
  53. SinglePassRunAndFail<GraphicsRobustAccessPass>(text);
  54. }
  55. TEST_F(GraphicsRobustAccessTest, FailCantProcessPhysical32AddressingModel) {
  56. const std::string text = R"(
  57. ; CHECK: Addressing model must be Logical. Found OpMemoryModel Physical32 OpenCL
  58. OpCapability Shader
  59. OpMemoryModel Physical32 OpenCL
  60. )";
  61. SinglePassRunAndFail<GraphicsRobustAccessPass>(text);
  62. }
  63. TEST_F(GraphicsRobustAccessTest, FailCantProcessPhysical64AddressingModel) {
  64. const std::string text = R"(
  65. ; CHECK: Addressing model must be Logical. Found OpMemoryModel Physical64 OpenCL
  66. OpCapability Shader
  67. OpMemoryModel Physical64 OpenCL
  68. )";
  69. SinglePassRunAndFail<GraphicsRobustAccessPass>(text);
  70. }
  71. TEST_F(GraphicsRobustAccessTest,
  72. FailCantProcessPhysicalStorageBuffer64EXTAddressingModel) {
  73. const std::string text = R"(
  74. ; CHECK: Addressing model must be Logical. Found OpMemoryModel PhysicalStorageBuffer64 GLSL450
  75. OpCapability Shader
  76. OpMemoryModel PhysicalStorageBuffer64EXT GLSL450
  77. )";
  78. SinglePassRunAndFail<GraphicsRobustAccessPass>(text);
  79. }
  80. // Test access chains
  81. // Returns the names of access chain instructions handled by the pass.
  82. // For the purposes of this pass, regular and in-bounds access chains are the
  83. // same.)
  84. std::vector<const char*> AccessChains() {
  85. return {"OpAccessChain", "OpInBoundsAccessChain"};
  86. }
  87. std::string ShaderPreamble() {
  88. return R"(
  89. OpCapability Shader
  90. OpMemoryModel Logical Simple
  91. OpEntryPoint GLCompute %main "main"
  92. )";
  93. }
  94. std::string ShaderPreamble(const std::vector<std::string>& names) {
  95. std::ostringstream os;
  96. os << ShaderPreamble();
  97. for (auto& name : names) {
  98. os << " OpName %" << name << " \"" << name << "\"\n";
  99. }
  100. return os.str();
  101. }
  102. std::string ShaderPreambleAC() {
  103. return ShaderPreamble({"ac", "ptr_ty", "var"});
  104. }
  105. std::string ShaderPreambleAC(const std::vector<std::string>& names) {
  106. auto names2 = names;
  107. names2.push_back("ac");
  108. names2.push_back("ptr_ty");
  109. names2.push_back("var");
  110. return ShaderPreamble(names2);
  111. }
  112. std::string DecoSSBO() {
  113. return R"(
  114. OpDecorate %ssbo_s BufferBlock
  115. OpMemberDecorate %ssbo_s 0 Offset 0
  116. OpMemberDecorate %ssbo_s 1 Offset 4
  117. OpMemberDecorate %ssbo_s 2 Offset 16
  118. OpDecorate %var DescriptorSet 0
  119. OpDecorate %var Binding 0
  120. )";
  121. }
  122. std::string TypesVoid() {
  123. return R"(
  124. %void = OpTypeVoid
  125. %void_fn = OpTypeFunction %void
  126. )";
  127. }
  128. std::string TypesInt() {
  129. return R"(
  130. %uint = OpTypeInt 32 0
  131. %int = OpTypeInt 32 1
  132. )";
  133. }
  134. std::string TypesFloat() {
  135. return R"(
  136. %float = OpTypeFloat 32
  137. )";
  138. }
  139. std::string TypesShort() {
  140. return R"(
  141. %ushort = OpTypeInt 16 0
  142. %short = OpTypeInt 16 1
  143. )";
  144. }
  145. std::string TypesLong() {
  146. return R"(
  147. %ulong = OpTypeInt 64 0
  148. %long = OpTypeInt 64 1
  149. )";
  150. }
  151. std::string MainPrefix() {
  152. return R"(
  153. %main = OpFunction %void None %void_fn
  154. %entry = OpLabel
  155. )";
  156. }
  157. std::string MainSuffix() {
  158. return R"(
  159. OpReturn
  160. OpFunctionEnd
  161. )";
  162. }
  163. std::string ACCheck(const std::string& access_chain_inst,
  164. const std::string& original,
  165. const std::string& transformed) {
  166. return "\n ; CHECK: %ac = " + access_chain_inst + " %ptr_ty %var" +
  167. (transformed.empty() ? "" : " ") + transformed +
  168. "\n ; CHECK-NOT: " + access_chain_inst +
  169. "\n ; CHECK-NEXT: OpReturn"
  170. "\n %ac = " +
  171. access_chain_inst + " %ptr_ty %var " + (original.empty() ? "" : " ") +
  172. original + "\n";
  173. }
  174. std::string ACCheckFail(const std::string& access_chain_inst,
  175. const std::string& original,
  176. const std::string& transformed) {
  177. return "\n ; CHECK: %ac = " + access_chain_inst + " %ptr_ty %var" +
  178. (transformed.empty() ? "" : " ") + transformed +
  179. "\n ; CHECK-NOT: " + access_chain_inst +
  180. "\n ; CHECK-NOT: OpReturn"
  181. "\n %ac = " +
  182. access_chain_inst + " %ptr_ty %var " + (original.empty() ? "" : " ") +
  183. original + "\n";
  184. }
  185. // Access chain into:
  186. // Vector
  187. // Vector sizes 2, 3, 4
  188. // Matrix
  189. // Matrix columns 2, 4
  190. // Component is vector 2, 4
  191. // Array
  192. // Struct
  193. // TODO(dneto): RuntimeArray
  194. TEST_F(GraphicsRobustAccessTest, ACVectorLeastInboundConstantUntouched) {
  195. for (auto* ac : AccessChains()) {
  196. std::ostringstream shaders;
  197. shaders << ShaderPreambleAC() << TypesVoid() << TypesInt() << R"(
  198. %uvec2 = OpTypeVector %uint 2
  199. %var_ty = OpTypePointer Function %uvec2
  200. %ptr_ty = OpTypePointer Function %uint
  201. %uint_0 = OpConstant %uint 0
  202. )"
  203. << MainPrefix() << R"(
  204. %var = OpVariable %var_ty Function)" << ACCheck(ac, "%uint_0", "%uint_0")
  205. << MainSuffix();
  206. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  207. }
  208. }
  209. TEST_F(GraphicsRobustAccessTest, ACVectorMostInboundConstantUntouched) {
  210. for (auto* ac : AccessChains()) {
  211. std::ostringstream shaders;
  212. shaders << ShaderPreambleAC() << TypesVoid() << TypesInt() << R"(
  213. %v4uint = OpTypeVector %uint 4
  214. %var_ty = OpTypePointer Function %v4uint
  215. %ptr_ty = OpTypePointer Function %uint
  216. %uint_3 = OpConstant %uint 3
  217. )"
  218. << MainPrefix() << R"(
  219. %var = OpVariable %var_ty Function)" << ACCheck(ac, "%uint_3", "%uint_3")
  220. << MainSuffix();
  221. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  222. }
  223. }
  224. TEST_F(GraphicsRobustAccessTest, ACVectorExcessConstantClamped) {
  225. for (auto* ac : AccessChains()) {
  226. std::ostringstream shaders;
  227. shaders << ShaderPreambleAC() << TypesVoid() << TypesInt() << R"(
  228. %v4uint = OpTypeVector %uint 4
  229. %var_ty = OpTypePointer Function %v4uint
  230. %ptr_ty = OpTypePointer Function %uint
  231. %uint_4 = OpConstant %uint 4
  232. )"
  233. << MainPrefix() << R"(
  234. %var = OpVariable %var_ty Function)" << ACCheck(ac, "%uint_4", "%int_3")
  235. << MainSuffix();
  236. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  237. }
  238. }
  239. TEST_F(GraphicsRobustAccessTest, ACVectorNegativeConstantClamped) {
  240. for (auto* ac : AccessChains()) {
  241. std::ostringstream shaders;
  242. shaders << ShaderPreambleAC() << TypesVoid() << TypesInt() << R"(
  243. %v4uint = OpTypeVector %uint 4
  244. %var_ty = OpTypePointer Function %v4uint
  245. %ptr_ty = OpTypePointer Function %uint
  246. %int_n1 = OpConstant %int -1
  247. )"
  248. << MainPrefix() << R"(
  249. ; CHECK: %int_0 = OpConstant %int 0
  250. %var = OpVariable %var_ty Function)" << ACCheck(ac, "%int_n1", "%int_0")
  251. << MainSuffix();
  252. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  253. }
  254. }
  255. // Like the previous test, but ensures the pass knows how to modify an index
  256. // which does not come first in the access chain.
  257. TEST_F(GraphicsRobustAccessTest, ACVectorInArrayNegativeConstantClamped) {
  258. for (auto* ac : AccessChains()) {
  259. std::ostringstream shaders;
  260. shaders << ShaderPreambleAC() << TypesVoid() << TypesInt() << R"(
  261. %v4uint = OpTypeVector %uint 4
  262. %uint_1 = OpConstant %uint 1
  263. %uint_2 = OpConstant %uint 2
  264. %arr = OpTypeArray %v4uint %uint_2
  265. %var_ty = OpTypePointer Function %arr
  266. %ptr_ty = OpTypePointer Function %uint
  267. %int_n1 = OpConstant %int -1
  268. )"
  269. << MainPrefix() << R"(
  270. ; CHECK: %int_0 = OpConstant %int 0
  271. %var = OpVariable %var_ty Function)"
  272. << ACCheck(ac, "%uint_1 %int_n1", "%uint_1 %int_0") << MainSuffix();
  273. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  274. }
  275. }
  276. TEST_F(GraphicsRobustAccessTest, ACVectorGeneralClamped) {
  277. for (auto* ac : AccessChains()) {
  278. std::ostringstream shaders;
  279. shaders << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt() << R"(
  280. %v4uint = OpTypeVector %uint 4
  281. %var_ty = OpTypePointer Function %v4uint
  282. %ptr_ty = OpTypePointer Function %uint
  283. %i = OpUndef %int)"
  284. << MainPrefix() << R"(
  285. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  286. ; CHECK-DAG: %int_0 = OpConstant %int 0
  287. ; CHECK-DAG: %int_3 = OpConstant %int 3
  288. ; CHECK: OpLabel
  289. ; CHECK: %[[clamp:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %i %int_0 %int_3
  290. %var = OpVariable %var_ty Function)" << ACCheck(ac, "%i", "%[[clamp]]")
  291. << MainSuffix();
  292. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  293. }
  294. }
  295. TEST_F(GraphicsRobustAccessTest, ACVectorGeneralShortClamped) {
  296. // Show that signed 16 bit integers are clamped as well.
  297. for (auto* ac : AccessChains()) {
  298. std::ostringstream shaders;
  299. shaders << "OpCapability Int16\n"
  300. << ShaderPreambleAC({"i"}) << TypesVoid() << TypesShort() <<
  301. R"(
  302. %v4short = OpTypeVector %short 4
  303. %var_ty = OpTypePointer Function %v4short
  304. %ptr_ty = OpTypePointer Function %short
  305. %i = OpUndef %short)"
  306. << MainPrefix() << R"(
  307. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  308. ; CHECK-NOT: = OpTypeInt 32
  309. ; CHECK-DAG: %short_0 = OpConstant %short 0
  310. ; CHECK-DAG: %short_3 = OpConstant %short 3
  311. ; CHECK-NOT: = OpTypeInt 32
  312. ; CHECK: OpLabel
  313. ; CHECK: %[[clamp:\w+]] = OpExtInst %short %[[GLSLSTD450]] SClamp %i %short_0 %short_3
  314. %var = OpVariable %var_ty Function)" << ACCheck(ac, "%i", "%[[clamp]]")
  315. << MainSuffix();
  316. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  317. }
  318. }
  319. TEST_F(GraphicsRobustAccessTest, ACVectorGeneralUShortClamped) {
  320. // Show that unsigned 16 bit integers are clamped as well.
  321. for (auto* ac : AccessChains()) {
  322. std::ostringstream shaders;
  323. shaders << "OpCapability Int16\n"
  324. << ShaderPreambleAC({"i"}) << TypesVoid() << TypesShort() <<
  325. R"(
  326. %v4ushort = OpTypeVector %ushort 4
  327. %var_ty = OpTypePointer Function %v4ushort
  328. %ptr_ty = OpTypePointer Function %ushort
  329. %i = OpUndef %ushort)"
  330. << MainPrefix() << R"(
  331. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  332. ; CHECK-NOT: = OpTypeInt 32
  333. ; CHECK-DAG: %short_0 = OpConstant %short 0
  334. ; CHECK-DAG: %short_3 = OpConstant %short 3
  335. ; CHECK-NOT: = OpTypeInt 32
  336. ; CHECK: OpLabel
  337. ; CHECK: %[[clamp:\w+]] = OpExtInst %ushort %[[GLSLSTD450]] SClamp %i %short_0 %short_3
  338. %var = OpVariable %var_ty Function)" << ACCheck(ac, "%i", "%[[clamp]]")
  339. << MainSuffix();
  340. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  341. }
  342. }
  343. TEST_F(GraphicsRobustAccessTest, ACVectorGeneralLongClamped) {
  344. // Show that signed 64 bit integers are clamped as well.
  345. for (auto* ac : AccessChains()) {
  346. std::ostringstream shaders;
  347. shaders << "OpCapability Int64\n"
  348. << ShaderPreambleAC({"i"}) << TypesVoid() << TypesLong() <<
  349. R"(
  350. %v4long = OpTypeVector %long 4
  351. %var_ty = OpTypePointer Function %v4long
  352. %ptr_ty = OpTypePointer Function %long
  353. %i = OpUndef %long)"
  354. << MainPrefix() << R"(
  355. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  356. ; CHECK-NOT: = OpTypeInt 32
  357. ; CHECK-DAG: %long_0 = OpConstant %long 0
  358. ; CHECK-DAG: %long_3 = OpConstant %long 3
  359. ; CHECK-NOT: = OpTypeInt 32
  360. ; CHECK: OpLabel
  361. ; CHECK: %[[clamp:\w+]] = OpExtInst %long %[[GLSLSTD450]] SClamp %i %long_0 %long_3
  362. %var = OpVariable %var_ty Function)" << ACCheck(ac, "%i", "%[[clamp]]")
  363. << MainSuffix();
  364. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  365. }
  366. }
  367. TEST_F(GraphicsRobustAccessTest, ACVectorGeneralULongClamped) {
  368. // Show that unsigned 64 bit integers are clamped as well.
  369. for (auto* ac : AccessChains()) {
  370. std::ostringstream shaders;
  371. shaders << "OpCapability Int64\n"
  372. << ShaderPreambleAC({"i"}) << TypesVoid() << TypesLong() <<
  373. R"(
  374. %v4ulong = OpTypeVector %ulong 4
  375. %var_ty = OpTypePointer Function %v4ulong
  376. %ptr_ty = OpTypePointer Function %ulong
  377. %i = OpUndef %ulong)"
  378. << MainPrefix() << R"(
  379. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  380. ; CHECK-NOT: = OpTypeInt 32
  381. ; CHECK-DAG: %long_0 = OpConstant %long 0
  382. ; CHECK-DAG: %long_3 = OpConstant %long 3
  383. ; CHECK-NOT: = OpTypeInt 32
  384. ; CHECK: OpLabel
  385. ; CHECK: %[[clamp:\w+]] = OpExtInst %ulong %[[GLSLSTD450]] SClamp %i %long_0 %long_3
  386. %var = OpVariable %var_ty Function)" << ACCheck(ac, "%i", "%[[clamp]]")
  387. << MainSuffix();
  388. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  389. }
  390. }
  391. TEST_F(GraphicsRobustAccessTest, ACMatrixLeastInboundConstantUntouched) {
  392. for (auto* ac : AccessChains()) {
  393. std::ostringstream shaders;
  394. shaders << ShaderPreambleAC() << TypesVoid() << TypesInt()
  395. << TypesFloat() << R"(
  396. %v2float = OpTypeVector %float 2
  397. %mat4v2float = OpTypeMatrix %v2float 4
  398. %var_ty = OpTypePointer Function %mat4v2float
  399. %ptr_ty = OpTypePointer Function %float
  400. %uint_0 = OpConstant %uint 0
  401. %uint_1 = OpConstant %uint 1
  402. )" << MainPrefix() << R"(
  403. %var = OpVariable %var_ty Function)"
  404. << ACCheck(ac, "%uint_0 %uint_1", "%uint_0 %uint_1")
  405. << MainSuffix();
  406. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  407. }
  408. }
  409. TEST_F(GraphicsRobustAccessTest, ACMatrixMostInboundConstantUntouched) {
  410. for (auto* ac : AccessChains()) {
  411. std::ostringstream shaders;
  412. shaders << ShaderPreambleAC() << TypesVoid() << TypesInt()
  413. << TypesFloat() << R"(
  414. %v2float = OpTypeVector %float 2
  415. %mat4v2float = OpTypeMatrix %v2float 4
  416. %var_ty = OpTypePointer Function %mat4v2float
  417. %ptr_ty = OpTypePointer Function %float
  418. %uint_1 = OpConstant %uint 1
  419. %uint_3 = OpConstant %uint 3
  420. )" << MainPrefix() << R"(
  421. %var = OpVariable %var_ty Function)"
  422. << ACCheck(ac, "%uint_3 %uint_1", "%uint_3 %uint_1")
  423. << MainSuffix();
  424. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  425. }
  426. }
  427. TEST_F(GraphicsRobustAccessTest, ACMatrixExcessConstantClamped) {
  428. for (auto* ac : AccessChains()) {
  429. std::ostringstream shaders;
  430. shaders << ShaderPreambleAC() << TypesVoid() << TypesInt()
  431. << TypesFloat() << R"(
  432. %v2float = OpTypeVector %float 2
  433. %mat4v2float = OpTypeMatrix %v2float 4
  434. %var_ty = OpTypePointer Function %mat4v2float
  435. %ptr_ty = OpTypePointer Function %float
  436. %uint_1 = OpConstant %uint 1
  437. %uint_4 = OpConstant %uint 4
  438. )" << MainPrefix() << R"(
  439. ; CHECK: %int_3 = OpConstant %int 3
  440. %var = OpVariable %var_ty Function)"
  441. << ACCheck(ac, "%uint_4 %uint_1", "%int_3 %uint_1") << MainSuffix();
  442. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  443. }
  444. }
  445. TEST_F(GraphicsRobustAccessTest, ACMatrixNegativeConstantClamped) {
  446. for (auto* ac : AccessChains()) {
  447. std::ostringstream shaders;
  448. shaders << ShaderPreambleAC() << TypesVoid() << TypesInt()
  449. << TypesFloat() << R"(
  450. %v2float = OpTypeVector %float 2
  451. %mat4v2float = OpTypeMatrix %v2float 4
  452. %var_ty = OpTypePointer Function %mat4v2float
  453. %ptr_ty = OpTypePointer Function %float
  454. %uint_1 = OpConstant %uint 1
  455. %int_n1 = OpConstant %int -1
  456. )" << MainPrefix() << R"(
  457. ; CHECK: %int_0 = OpConstant %int 0
  458. %var = OpVariable %var_ty Function)"
  459. << ACCheck(ac, "%int_n1 %uint_1", "%int_0 %uint_1") << MainSuffix();
  460. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  461. }
  462. }
  463. TEST_F(GraphicsRobustAccessTest, ACMatrixGeneralClamped) {
  464. for (auto* ac : AccessChains()) {
  465. std::ostringstream shaders;
  466. shaders << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt()
  467. << TypesFloat() << R"(
  468. %v2float = OpTypeVector %float 2
  469. %mat4v2float = OpTypeMatrix %v2float 4
  470. %var_ty = OpTypePointer Function %mat4v2float
  471. %ptr_ty = OpTypePointer Function %float
  472. %uint_1 = OpConstant %uint 1
  473. %i = OpUndef %int
  474. )" << MainPrefix() << R"(
  475. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  476. ; CHECK-DAG: %int_0 = OpConstant %int 0
  477. ; CHECK-DAG: %int_3 = OpConstant %int 3
  478. ; CHECK: OpLabel
  479. ; CHECK: %[[clamp:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %i %int_0 %int_3
  480. %var = OpVariable %var_ty Function)"
  481. << ACCheck(ac, "%i %uint_1", "%[[clamp]] %uint_1") << MainSuffix();
  482. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  483. }
  484. }
  485. TEST_F(GraphicsRobustAccessTest, ACArrayLeastInboundConstantUntouched) {
  486. for (auto* ac : AccessChains()) {
  487. std::ostringstream shaders;
  488. shaders << ShaderPreambleAC() << TypesVoid() << TypesInt()
  489. << TypesFloat() << R"(
  490. %uint_200 = OpConstant %uint 200
  491. %arr = OpTypeArray %float %uint_200
  492. %var_ty = OpTypePointer Function %arr
  493. %ptr_ty = OpTypePointer Function %float
  494. %int_0 = OpConstant %int 0
  495. )" << MainPrefix() << R"(
  496. %var = OpVariable %var_ty Function)"
  497. << ACCheck(ac, "%int_0", "%int_0") << MainSuffix();
  498. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  499. }
  500. }
  501. TEST_F(GraphicsRobustAccessTest, ACArrayMostInboundConstantUntouched) {
  502. for (auto* ac : AccessChains()) {
  503. std::ostringstream shaders;
  504. shaders << ShaderPreambleAC() << TypesVoid() << TypesInt()
  505. << TypesFloat() << R"(
  506. %uint_200 = OpConstant %uint 200
  507. %arr = OpTypeArray %float %uint_200
  508. %var_ty = OpTypePointer Function %arr
  509. %ptr_ty = OpTypePointer Function %float
  510. %int_199 = OpConstant %int 199
  511. )" << MainPrefix() << R"(
  512. %var = OpVariable %var_ty Function)"
  513. << ACCheck(ac, "%int_199", "%int_199") << MainSuffix();
  514. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  515. }
  516. }
  517. TEST_F(GraphicsRobustAccessTest, ACArrayGeneralClamped) {
  518. for (auto* ac : AccessChains()) {
  519. std::ostringstream shaders;
  520. shaders << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt()
  521. << TypesFloat() << R"(
  522. %uint_200 = OpConstant %uint 200
  523. %arr = OpTypeArray %float %uint_200
  524. %var_ty = OpTypePointer Function %arr
  525. %ptr_ty = OpTypePointer Function %float
  526. %i = OpUndef %int
  527. )" << MainPrefix() << R"(
  528. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  529. ; CHECK-DAG: %int_0 = OpConstant %int 0
  530. ; CHECK-DAG: %int_199 = OpConstant %int 199
  531. ; CHECK: OpLabel
  532. ; CHECK: %[[clamp:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %i %int_0 %int_199
  533. %var = OpVariable %var_ty Function)"
  534. << ACCheck(ac, "%i", "%[[clamp]]") << MainSuffix();
  535. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  536. }
  537. }
  538. TEST_F(GraphicsRobustAccessTest, ACArrayGeneralShortIndexUIntBoundsClamped) {
  539. // Index is signed short, array bounds overflows the index type.
  540. for (auto* ac : AccessChains()) {
  541. std::ostringstream shaders;
  542. shaders << "OpCapability Int16\n"
  543. << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt()
  544. << TypesShort() << TypesFloat() << R"(
  545. %uint_70000 = OpConstant %uint 70000 ; overflows 16bits
  546. %arr = OpTypeArray %float %uint_70000
  547. %var_ty = OpTypePointer Function %arr
  548. %ptr_ty = OpTypePointer Function %float
  549. %i = OpUndef %short
  550. )" << MainPrefix() << R"(
  551. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  552. ; CHECK-DAG: %int_0 = OpConstant %int 0
  553. ; CHECK-DAG: %int_69999 = OpConstant %int 69999
  554. ; CHECK: OpLabel
  555. ; CHECK: %[[i_ext:\w+]] = OpSConvert %uint %i
  556. ; CHECK: %[[clamp:\w+]] = OpExtInst %uint %[[GLSLSTD450]] SClamp %[[i_ext]] %int_0 %int_69999
  557. %var = OpVariable %var_ty Function)"
  558. << ACCheck(ac, "%i", "%[[clamp]]") << MainSuffix();
  559. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  560. }
  561. }
  562. TEST_F(GraphicsRobustAccessTest, ACArrayGeneralUShortIndexIntBoundsClamped) {
  563. // Index is unsigned short, array bounds overflows the index type.
  564. for (auto* ac : AccessChains()) {
  565. std::ostringstream shaders;
  566. shaders << "OpCapability Int16\n"
  567. << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt()
  568. << TypesShort() << TypesFloat() << R"(
  569. %int_70000 = OpConstant %int 70000 ; overflows 16bits
  570. %arr = OpTypeArray %float %int_70000
  571. %var_ty = OpTypePointer Function %arr
  572. %ptr_ty = OpTypePointer Function %float
  573. %i = OpUndef %ushort
  574. )" << MainPrefix() << R"(
  575. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  576. ; CHECK-DAG: %int_0 = OpConstant %int 0
  577. ; CHECK-DAG: %int_69999 = OpConstant %int 69999
  578. ; CHECK: OpLabel
  579. ; CHECK: %[[i_ext:\w+]] = OpUConvert %uint %i
  580. ; CHECK: %[[clamp:\w+]] = OpExtInst %uint %[[GLSLSTD450]] SClamp %[[i_ext]] %int_0 %int_69999
  581. %var = OpVariable %var_ty Function)"
  582. << ACCheck(ac, "%i", "%[[clamp]]") << MainSuffix();
  583. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  584. }
  585. }
  586. TEST_F(GraphicsRobustAccessTest, ACArrayGeneralUIntIndexShortBoundsClamped) {
  587. // Signed int index i is wider than the array bounds type.
  588. for (auto* ac : AccessChains()) {
  589. std::ostringstream shaders;
  590. shaders << "OpCapability Int16\n"
  591. << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt()
  592. << TypesShort() << TypesFloat() << R"(
  593. %short_200 = OpConstant %short 200
  594. %arr = OpTypeArray %float %short_200
  595. %var_ty = OpTypePointer Function %arr
  596. %ptr_ty = OpTypePointer Function %float
  597. %i = OpUndef %uint
  598. )" << MainPrefix() << R"(
  599. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  600. ; CHECK-DAG: %int_0 = OpConstant %int 0
  601. ; CHECK-DAG: %int_199 = OpConstant %int 199
  602. ; CHECK: OpLabel
  603. ; CHECK: %[[clamp:\w+]] = OpExtInst %uint %[[GLSLSTD450]] SClamp %i %int_0 %int_199
  604. %var = OpVariable %var_ty Function)"
  605. << ACCheck(ac, "%i", "%[[clamp]]") << MainSuffix();
  606. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  607. }
  608. }
  609. TEST_F(GraphicsRobustAccessTest, ACArrayGeneralIntIndexUShortBoundsClamped) {
  610. // Unsigned int index i is wider than the array bounds type.
  611. for (auto* ac : AccessChains()) {
  612. std::ostringstream shaders;
  613. shaders << "OpCapability Int16\n"
  614. << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt()
  615. << TypesShort() << TypesFloat() << R"(
  616. %ushort_200 = OpConstant %ushort 200
  617. %arr = OpTypeArray %float %ushort_200
  618. %var_ty = OpTypePointer Function %arr
  619. %ptr_ty = OpTypePointer Function %float
  620. %i = OpUndef %int
  621. )" << MainPrefix() << R"(
  622. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  623. ; CHECK-DAG: %int_0 = OpConstant %int 0
  624. ; CHECK-DAG: %int_199 = OpConstant %int 199
  625. ; CHECK: OpLabel
  626. ; CHECK: %[[clamp:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %i %int_0 %int_199
  627. %var = OpVariable %var_ty Function)"
  628. << ACCheck(ac, "%i", "%[[clamp]]") << MainSuffix();
  629. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  630. }
  631. }
  632. TEST_F(GraphicsRobustAccessTest, ACArrayGeneralLongIndexUIntBoundsClamped) {
  633. // Signed long index i is wider than the array bounds type.
  634. for (auto* ac : AccessChains()) {
  635. std::ostringstream shaders;
  636. shaders << "OpCapability Int64\n"
  637. << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt()
  638. << TypesLong() << TypesFloat() << R"(
  639. %uint_200 = OpConstant %uint 200
  640. %arr = OpTypeArray %float %uint_200
  641. %var_ty = OpTypePointer Function %arr
  642. %ptr_ty = OpTypePointer Function %float
  643. %i = OpUndef %long
  644. )" << MainPrefix() << R"(
  645. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  646. ; CHECK-DAG: %long_0 = OpConstant %long 0
  647. ; CHECK-DAG: %long_199 = OpConstant %long 199
  648. ; CHECK: OpLabel
  649. ; CHECK: %[[clamp:\w+]] = OpExtInst %long %[[GLSLSTD450]] SClamp %i %long_0 %long_199
  650. %var = OpVariable %var_ty Function)"
  651. << ACCheck(ac, "%i", "%[[clamp]]") << MainSuffix();
  652. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  653. }
  654. }
  655. TEST_F(GraphicsRobustAccessTest, ACArrayGeneralULongIndexIntBoundsClamped) {
  656. // Unsigned long index i is wider than the array bounds type.
  657. for (auto* ac : AccessChains()) {
  658. std::ostringstream shaders;
  659. shaders << "OpCapability Int64\n"
  660. << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt()
  661. << TypesLong() << TypesFloat() << R"(
  662. %int_200 = OpConstant %int 200
  663. %arr = OpTypeArray %float %int_200
  664. %var_ty = OpTypePointer Function %arr
  665. %ptr_ty = OpTypePointer Function %float
  666. %i = OpUndef %ulong
  667. )" << MainPrefix() << R"(
  668. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  669. ; CHECK-DAG: %long_0 = OpConstant %long 0
  670. ; CHECK-DAG: %long_199 = OpConstant %long 199
  671. ; CHECK: OpLabel
  672. ; CHECK: %[[clamp:\w+]] = OpExtInst %ulong %[[GLSLSTD450]] SClamp %i %long_0 %long_199
  673. %var = OpVariable %var_ty Function)"
  674. << ACCheck(ac, "%i", "%[[clamp]]") << MainSuffix();
  675. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  676. }
  677. }
  678. TEST_F(GraphicsRobustAccessTest,
  679. ACArrayGeneralShortIndeArrayBiggerThanShortMaxClipsToShortIntMax) {
  680. for (auto* ac : AccessChains()) {
  681. std::ostringstream shaders;
  682. shaders << "OpCapability Int16\n"
  683. << ShaderPreambleAC({"i"}) << TypesVoid() << TypesShort()
  684. << TypesInt() << TypesFloat() << R"(
  685. %uint_50000 = OpConstant %uint 50000
  686. %arr = OpTypeArray %float %uint_50000
  687. %var_ty = OpTypePointer Function %arr
  688. %ptr_ty = OpTypePointer Function %float
  689. %i = OpUndef %ushort
  690. )" << MainPrefix() << R"(
  691. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  692. ; CHECK-DAG: %short_0 = OpConstant %short 0
  693. ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %short 32767
  694. ; CHECK: OpLabel
  695. ; CHECK: %[[clamp:\w+]] = OpExtInst %ushort %[[GLSLSTD450]] SClamp %i %short_0 %[[intmax]]
  696. %var = OpVariable %var_ty Function)"
  697. << ACCheck(ac, "%i", "%[[clamp]]") << MainSuffix();
  698. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  699. }
  700. }
  701. TEST_F(GraphicsRobustAccessTest,
  702. ACArrayGeneralIntIndexArrayBiggerThanIntMaxClipsToSignedIntMax) {
  703. for (auto* ac : AccessChains()) {
  704. std::ostringstream shaders;
  705. shaders << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt()
  706. << TypesFloat() << R"(
  707. %uint_3000000000 = OpConstant %uint 3000000000
  708. %arr = OpTypeArray %float %uint_3000000000
  709. %var_ty = OpTypePointer Function %arr
  710. %ptr_ty = OpTypePointer Function %float
  711. %i = OpUndef %uint
  712. )" << MainPrefix() << R"(
  713. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  714. ; CHECK-DAG: %int_0 = OpConstant %int 0
  715. ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %int 2147483647
  716. ; CHECK: OpLabel
  717. ; CHECK: %[[clamp:\w+]] = OpExtInst %uint %[[GLSLSTD450]] SClamp %i %int_0 %[[intmax]]
  718. %var = OpVariable %var_ty Function)"
  719. << ACCheck(ac, "%i", "%[[clamp]]") << MainSuffix();
  720. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  721. }
  722. }
  723. TEST_F(GraphicsRobustAccessTest,
  724. ACArrayGeneralLongIndexArrayBiggerThanLongMaxClipsToSignedLongMax) {
  725. for (auto* ac : AccessChains()) {
  726. std::ostringstream shaders;
  727. shaders << "OpCapability Int64\n"
  728. << ShaderPreambleAC({"i"}) << TypesVoid() << TypesInt()
  729. << TypesLong()
  730. << TypesFloat()
  731. // 2^63 == 9,223,372,036,854,775,807
  732. << R"(
  733. %ulong_9223372036854775999 = OpConstant %ulong 9223372036854775999
  734. %arr = OpTypeArray %float %ulong_9223372036854775999
  735. %var_ty = OpTypePointer Function %arr
  736. %ptr_ty = OpTypePointer Function %float
  737. %i = OpUndef %ulong
  738. )"
  739. << MainPrefix() << R"(
  740. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  741. ; CHECK-DAG: %long_0 = OpConstant %long 0
  742. ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %long 9223372036854775807
  743. ; CHECK: OpLabel
  744. ; CHECK: %[[clamp:\w+]] = OpExtInst %ulong %[[GLSLSTD450]] SClamp %i %long_0 %[[intmax]]
  745. %var = OpVariable %var_ty Function)" << ACCheck(ac, "%i", "%[[clamp]]")
  746. << MainSuffix();
  747. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  748. }
  749. }
  750. TEST_F(GraphicsRobustAccessTest, ACArraySpecIdSizedAlwaysClamped) {
  751. for (auto* ac : AccessChains()) {
  752. std::ostringstream shaders;
  753. shaders << ShaderPreambleAC({"spec200"}) << R"(
  754. OpDecorate %spec200 SpecId 0 )" << TypesVoid() << TypesInt()
  755. << TypesFloat() << R"(
  756. %spec200 = OpSpecConstant %int 200
  757. %arr = OpTypeArray %float %spec200
  758. %var_ty = OpTypePointer Function %arr
  759. %ptr_ty = OpTypePointer Function %float
  760. %uint_5 = OpConstant %uint 5
  761. )" << MainPrefix() << R"(
  762. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  763. ; CHECK-DAG: %uint_0 = OpConstant %uint 0
  764. ; CHECK-DAG: %uint_1 = OpConstant %uint 1
  765. ; CHECK-DAG: %[[uint_intmax:\w+]] = OpConstant %uint 2147483647
  766. ; CHECK: OpLabel
  767. ; CHECK: %[[max:\w+]] = OpISub %uint %spec200 %uint_1
  768. ; CHECK: %[[smin:\w+]] = OpExtInst %uint %[[GLSLSTD450]] UMin %[[max]] %[[uint_intmax]]
  769. ; CHECK: %[[clamp:\w+]] = OpExtInst %uint %[[GLSLSTD450]] SClamp %uint_5 %uint_0 %[[smin]]
  770. %var = OpVariable %var_ty Function)"
  771. << ACCheck(ac, "%uint_5", "%[[clamp]]") << MainSuffix();
  772. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  773. }
  774. }
  775. TEST_F(GraphicsRobustAccessTest, ACStructLeastUntouched) {
  776. for (auto* ac : AccessChains()) {
  777. std::ostringstream shaders;
  778. shaders << ShaderPreambleAC() << TypesVoid() << TypesInt()
  779. << TypesFloat() << R"(
  780. %struct = OpTypeStruct %float %float %float
  781. %var_ty = OpTypePointer Function %struct
  782. %ptr_ty = OpTypePointer Function %float
  783. %int_0 = OpConstant %int 0
  784. )" << MainPrefix() << R"(
  785. %var = OpVariable %var_ty Function)"
  786. << ACCheck(ac, "%int_0", "%int_0") << MainSuffix();
  787. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  788. }
  789. }
  790. TEST_F(GraphicsRobustAccessTest, ACStructMostUntouched) {
  791. for (auto* ac : AccessChains()) {
  792. std::ostringstream shaders;
  793. shaders << ShaderPreambleAC() << TypesVoid() << TypesInt()
  794. << TypesFloat() << R"(
  795. %struct = OpTypeStruct %float %float %float
  796. %var_ty = OpTypePointer Function %struct
  797. %ptr_ty = OpTypePointer Function %float
  798. %int_2 = OpConstant %int 2
  799. )" << MainPrefix() << R"(
  800. %var = OpVariable %var_ty Function)"
  801. << ACCheck(ac, "%int_2", "%int_2") << MainSuffix();
  802. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  803. }
  804. }
  805. TEST_F(GraphicsRobustAccessTest, ACStructSpecConstantFail) {
  806. for (auto* ac : AccessChains()) {
  807. std::ostringstream shaders;
  808. shaders << ShaderPreambleAC({"struct", "spec200"})
  809. << "OpDecorate %spec200 SpecId 0\n"
  810. <<
  811. TypesVoid() << TypesInt() << TypesFloat() << R"(
  812. %spec200 = OpSpecConstant %int 200
  813. %struct = OpTypeStruct %float %float %float
  814. %var_ty = OpTypePointer Function %struct
  815. %ptr_ty = OpTypePointer Function %float
  816. )" << MainPrefix() << R"(
  817. %var = OpVariable %var_ty Function
  818. ; CHECK: Member index into struct is not a constant integer
  819. ; CHECK-SAME: %spec200 = OpSpecConstant %int 200
  820. )"
  821. << ACCheckFail(ac, "%spec200", "%spec200") << MainSuffix();
  822. SinglePassRunAndFail<GraphicsRobustAccessPass>(shaders.str());
  823. }
  824. }
  825. TEST_F(GraphicsRobustAccessTest, ACStructFloatConstantFail) {
  826. for (auto* ac : AccessChains()) {
  827. std::ostringstream shaders;
  828. shaders << ShaderPreambleAC({"struct"}) <<
  829. TypesVoid() << TypesInt() << TypesFloat() << R"(
  830. %float_2 = OpConstant %float 2
  831. %struct = OpTypeStruct %float %float %float
  832. %var_ty = OpTypePointer Function %struct
  833. %ptr_ty = OpTypePointer Function %float
  834. )" << MainPrefix() << R"(
  835. %var = OpVariable %var_ty Function
  836. ; CHECK: Member index into struct is not a constant integer
  837. ; CHECK-SAME: %float_2 = OpConstant %float 2
  838. )"
  839. << ACCheckFail(ac, "%float_2", "%float_2") << MainSuffix();
  840. SinglePassRunAndFail<GraphicsRobustAccessPass>(shaders.str());
  841. }
  842. }
  843. TEST_F(GraphicsRobustAccessTest, ACStructNonConstantFail) {
  844. for (auto* ac : AccessChains()) {
  845. std::ostringstream shaders;
  846. shaders << ShaderPreambleAC({"struct", "i"}) <<
  847. TypesVoid() << TypesInt() << TypesFloat() << R"(
  848. %float_2 = OpConstant %float 2
  849. %struct = OpTypeStruct %float %float %float
  850. %var_ty = OpTypePointer Function %struct
  851. %ptr_ty = OpTypePointer Function %float
  852. %i = OpUndef %int
  853. )" << MainPrefix() << R"(
  854. %var = OpVariable %var_ty Function
  855. ; CHECK: Member index into struct is not a constant integer
  856. ; CHECK-SAME: %i = OpUndef %int
  857. )"
  858. << ACCheckFail(ac, "%i", "%i") << MainSuffix();
  859. SinglePassRunAndFail<GraphicsRobustAccessPass>(shaders.str());
  860. }
  861. }
  862. TEST_F(GraphicsRobustAccessTest, ACStructExcessFail) {
  863. for (auto* ac : AccessChains()) {
  864. std::ostringstream shaders;
  865. shaders << ShaderPreambleAC({"struct", "i"}) << TypesVoid() << TypesInt()
  866. << TypesFloat() << R"(
  867. %struct = OpTypeStruct %float %float %float
  868. %var_ty = OpTypePointer Function %struct
  869. %ptr_ty = OpTypePointer Function %float
  870. %i = OpConstant %int 4
  871. )" << MainPrefix() << R"(
  872. %var = OpVariable %var_ty Function
  873. ; CHECK: Member index 4 is out of bounds for struct type:
  874. ; CHECK-SAME: %struct = OpTypeStruct %float %float %float
  875. )"
  876. << ACCheckFail(ac, "%i", "%i") << MainSuffix();
  877. SinglePassRunAndFail<GraphicsRobustAccessPass>(shaders.str());
  878. }
  879. }
  880. TEST_F(GraphicsRobustAccessTest, ACStructNegativeFail) {
  881. for (auto* ac : AccessChains()) {
  882. std::ostringstream shaders;
  883. shaders << ShaderPreambleAC({"struct", "i"}) << TypesVoid() << TypesInt()
  884. << TypesFloat() << R"(
  885. %struct = OpTypeStruct %float %float %float
  886. %var_ty = OpTypePointer Function %struct
  887. %ptr_ty = OpTypePointer Function %float
  888. %i = OpConstant %int -1
  889. )" << MainPrefix() << R"(
  890. %var = OpVariable %var_ty Function
  891. ; CHECK: Member index -1 is out of bounds for struct type:
  892. ; CHECK-SAME: %struct = OpTypeStruct %float %float %float
  893. )"
  894. << ACCheckFail(ac, "%i", "%i") << MainSuffix();
  895. SinglePassRunAndFail<GraphicsRobustAccessPass>(shaders.str());
  896. }
  897. }
  898. TEST_F(GraphicsRobustAccessTest, ACRTArrayLeastInboundClamped) {
  899. for (auto* ac : AccessChains()) {
  900. std::ostringstream shaders;
  901. shaders << ShaderPreambleAC() << "OpDecorate %rtarr ArrayStride 4 "
  902. << DecoSSBO() << TypesVoid() << TypesInt() << TypesFloat() << R"(
  903. %rtarr = OpTypeRuntimeArray %float
  904. %ssbo_s = OpTypeStruct %uint %uint %rtarr
  905. %var_ty = OpTypePointer Uniform %ssbo_s
  906. %ptr_ty = OpTypePointer Uniform %float
  907. %var = OpVariable %var_ty Uniform
  908. %int_0 = OpConstant %int 0
  909. %int_2 = OpConstant %int 2
  910. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  911. ; CHECK-DAG: %int_1 = OpConstant %int 1
  912. ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %int 2147483647
  913. ; CHECK: OpLabel
  914. ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %var 2
  915. ; CHECK: %[[max:\w+]] = OpISub %int %[[arrlen]] %int_1
  916. ; CHECK: %[[smin:\w+]] = OpExtInst %int %[[GLSLSTD450]] UMin %[[max]] %[[intmax]]
  917. ; CHECK: %[[clamp:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %int_0 %int_0 %[[smin]]
  918. )"
  919. << MainPrefix() << ACCheck(ac, "%int_2 %int_0", "%int_2 %[[clamp]]")
  920. << MainSuffix();
  921. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  922. }
  923. }
  924. TEST_F(GraphicsRobustAccessTest, ACRTArrayGeneralShortIndexClamped) {
  925. for (auto* ac : AccessChains()) {
  926. std::ostringstream shaders;
  927. shaders << "OpCapability Int16\n"
  928. << ShaderPreambleAC({"i"}) << "OpDecorate %rtarr ArrayStride 4 "
  929. << DecoSSBO() << TypesVoid() << TypesShort() << TypesFloat() << R"(
  930. %rtarr = OpTypeRuntimeArray %float
  931. %ssbo_s = OpTypeStruct %short %short %rtarr
  932. %var_ty = OpTypePointer Uniform %ssbo_s
  933. %ptr_ty = OpTypePointer Uniform %float
  934. %var = OpVariable %var_ty Uniform
  935. %short_2 = OpConstant %short 2
  936. %i = OpUndef %short
  937. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  938. ; CHECK: %uint = OpTypeInt 32 0
  939. ; CHECK-DAG: %uint_1 = OpConstant %uint 1
  940. ; CHECK-DAG: %uint_0 = OpConstant %uint 0
  941. ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %uint 2147483647
  942. ; CHECK: OpLabel
  943. ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %var 2
  944. ; CHECK-DAG: %[[max:\w+]] = OpISub %uint %[[arrlen]] %uint_1
  945. ; CHECK-DAG: %[[i_ext:\w+]] = OpSConvert %uint %i
  946. ; CHECK: %[[smin:\w+]] = OpExtInst %uint %[[GLSLSTD450]] UMin %[[max]] %[[intmax]]
  947. ; CHECK: %[[clamp:\w+]] = OpExtInst %uint %[[GLSLSTD450]] SClamp %[[i_ext]] %uint_0 %[[smin]]
  948. )"
  949. << MainPrefix() << ACCheck(ac, "%short_2 %i", "%short_2 %[[clamp]]")
  950. << MainSuffix();
  951. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  952. }
  953. }
  954. TEST_F(GraphicsRobustAccessTest, ACRTArrayGeneralUShortIndexClamped) {
  955. for (auto* ac : AccessChains()) {
  956. std::ostringstream shaders;
  957. shaders << "OpCapability Int16\n"
  958. << ShaderPreambleAC({"i"}) << "OpDecorate %rtarr ArrayStride 4 "
  959. << DecoSSBO() << TypesVoid() << TypesShort() << TypesFloat() << R"(
  960. %rtarr = OpTypeRuntimeArray %float
  961. %ssbo_s = OpTypeStruct %short %short %rtarr
  962. %var_ty = OpTypePointer Uniform %ssbo_s
  963. %ptr_ty = OpTypePointer Uniform %float
  964. %var = OpVariable %var_ty Uniform
  965. %short_2 = OpConstant %short 2
  966. %i = OpUndef %ushort
  967. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  968. ; CHECK: %uint = OpTypeInt 32 0
  969. ; CHECK-DAG: %uint_1 = OpConstant %uint 1
  970. ; CHECK-DAG: %uint_0 = OpConstant %uint 0
  971. ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %uint 2147483647
  972. ; CHECK: OpLabel
  973. ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %var 2
  974. ; CHECK-DAG: %[[max:\w+]] = OpISub %uint %[[arrlen]] %uint_1
  975. ; CHECK-DAG: %[[i_ext:\w+]] = OpSConvert %uint %i
  976. ; CHECK: %[[smin:\w+]] = OpExtInst %uint %[[GLSLSTD450]] UMin %[[max]] %[[intmax]]
  977. ; CHECK: %[[clamp:\w+]] = OpExtInst %uint %[[GLSLSTD450]] SClamp %[[i_ext]] %uint_0 %[[smin]]
  978. )"
  979. << MainPrefix() << ACCheck(ac, "%short_2 %i", "%short_2 %[[clamp]]")
  980. << MainSuffix();
  981. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  982. }
  983. }
  984. TEST_F(GraphicsRobustAccessTest, ACRTArrayGeneralIntIndexClamped) {
  985. for (auto* ac : AccessChains()) {
  986. std::ostringstream shaders;
  987. shaders << ShaderPreambleAC({"i"}) << "OpDecorate %rtarr ArrayStride 4 "
  988. << DecoSSBO() << TypesVoid() << TypesInt() << TypesFloat() << R"(
  989. %rtarr = OpTypeRuntimeArray %float
  990. %ssbo_s = OpTypeStruct %int %int %rtarr
  991. %var_ty = OpTypePointer Uniform %ssbo_s
  992. %ptr_ty = OpTypePointer Uniform %float
  993. %var = OpVariable %var_ty Uniform
  994. %int_2 = OpConstant %int 2
  995. %i = OpUndef %int
  996. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  997. ; CHECK-DAG: %int_1 = OpConstant %int 1
  998. ; CHECK-DAG: %int_0 = OpConstant %int 0
  999. ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %int 2147483647
  1000. ; CHECK: OpLabel
  1001. ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %var 2
  1002. ; CHECK: %[[max:\w+]] = OpISub %int %[[arrlen]] %int_1
  1003. ; CHECK: %[[smin:\w+]] = OpExtInst %int %[[GLSLSTD450]] UMin %[[max]] %[[intmax]]
  1004. ; CHECK: %[[clamp:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %i %int_0 %[[smin]]
  1005. )"
  1006. << MainPrefix() << ACCheck(ac, "%int_2 %i", "%int_2 %[[clamp]]")
  1007. << MainSuffix();
  1008. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  1009. }
  1010. }
  1011. TEST_F(GraphicsRobustAccessTest, ACRTArrayGeneralUIntIndexClamped) {
  1012. for (auto* ac : AccessChains()) {
  1013. std::ostringstream shaders;
  1014. shaders << ShaderPreambleAC({"i"}) << "OpDecorate %rtarr ArrayStride 4 "
  1015. << DecoSSBO() << TypesVoid() << TypesInt() << TypesFloat() << R"(
  1016. %rtarr = OpTypeRuntimeArray %float
  1017. %ssbo_s = OpTypeStruct %int %int %rtarr
  1018. %var_ty = OpTypePointer Uniform %ssbo_s
  1019. %ptr_ty = OpTypePointer Uniform %float
  1020. %var = OpVariable %var_ty Uniform
  1021. %int_2 = OpConstant %int 2
  1022. %i = OpUndef %uint
  1023. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  1024. ; CHECK-DAG: %uint_1 = OpConstant %uint 1
  1025. ; CHECK-DAG: %uint_0 = OpConstant %uint 0
  1026. ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %uint 2147483647
  1027. ; CHECK: OpLabel
  1028. ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %var 2
  1029. ; CHECK: %[[max:\w+]] = OpISub %uint %[[arrlen]] %uint_1
  1030. ; CHECK: %[[smin:\w+]] = OpExtInst %uint %[[GLSLSTD450]] UMin %[[max]] %[[intmax]]
  1031. ; CHECK: %[[clamp:\w+]] = OpExtInst %uint %[[GLSLSTD450]] SClamp %i %uint_0 %[[smin]]
  1032. )"
  1033. << MainPrefix() << ACCheck(ac, "%int_2 %i", "%int_2 %[[clamp]]")
  1034. << MainSuffix();
  1035. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  1036. }
  1037. }
  1038. TEST_F(GraphicsRobustAccessTest, ACRTArrayGeneralLongIndexClamped) {
  1039. for (auto* ac : AccessChains()) {
  1040. std::ostringstream shaders;
  1041. shaders << "OpCapability Int64" << ShaderPreambleAC({"i"})
  1042. << "OpDecorate %rtarr ArrayStride 4 " << DecoSSBO() << TypesVoid()
  1043. << TypesInt() << TypesLong() << TypesFloat() << R"(
  1044. %rtarr = OpTypeRuntimeArray %float
  1045. %ssbo_s = OpTypeStruct %int %int %rtarr
  1046. %var_ty = OpTypePointer Uniform %ssbo_s
  1047. %ptr_ty = OpTypePointer Uniform %float
  1048. %var = OpVariable %var_ty Uniform
  1049. %int_2 = OpConstant %int 2
  1050. %i = OpUndef %long
  1051. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  1052. ; CHECK-DAG: %long_0 = OpConstant %long 0
  1053. ; CHECK-DAG: %long_1 = OpConstant %long 1
  1054. ; CHECK-DAG: %[[longmax:\w+]] = OpConstant %long 9223372036854775807
  1055. ; CHECK: OpLabel
  1056. ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %var 2
  1057. ; CHECK: %[[arrlen_ext:\w+]] = OpUConvert %ulong %[[arrlen]]
  1058. ; CHECK: %[[max:\w+]] = OpISub %long %[[arrlen_ext]] %long_1
  1059. ; CHECK: %[[smin:\w+]] = OpExtInst %long %[[GLSLSTD450]] UMin %[[max]] %[[longmax]]
  1060. ; CHECK: %[[clamp:\w+]] = OpExtInst %long %[[GLSLSTD450]] SClamp %i %long_0 %[[smin]]
  1061. )" << MainPrefix()
  1062. << ACCheck(ac, "%int_2 %i", "%int_2 %[[clamp]]") << MainSuffix();
  1063. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  1064. }
  1065. }
  1066. TEST_F(GraphicsRobustAccessTest, ACRTArrayGeneralULongIndexClamped) {
  1067. for (auto* ac : AccessChains()) {
  1068. std::ostringstream shaders;
  1069. shaders << "OpCapability Int64" << ShaderPreambleAC({"i"})
  1070. << "OpDecorate %rtarr ArrayStride 4 " << DecoSSBO() << TypesVoid()
  1071. << TypesInt() << TypesLong() << TypesFloat() << R"(
  1072. %rtarr = OpTypeRuntimeArray %float
  1073. %ssbo_s = OpTypeStruct %int %int %rtarr
  1074. %var_ty = OpTypePointer Uniform %ssbo_s
  1075. %ptr_ty = OpTypePointer Uniform %float
  1076. %var = OpVariable %var_ty Uniform
  1077. %int_2 = OpConstant %int 2
  1078. %i = OpUndef %ulong
  1079. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  1080. ; CHECK-DAG: %ulong_0 = OpConstant %ulong 0
  1081. ; CHECK-DAG: %ulong_1 = OpConstant %ulong 1
  1082. ; CHECK-DAG: %[[longmax:\w+]] = OpConstant %ulong 9223372036854775807
  1083. ; CHECK: OpLabel
  1084. ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %var 2
  1085. ; CHECK: %[[arrlen_ext:\w+]] = OpUConvert %ulong %[[arrlen]]
  1086. ; CHECK: %[[max:\w+]] = OpISub %ulong %[[arrlen_ext]] %ulong_1
  1087. ; CHECK: %[[smin:\w+]] = OpExtInst %ulong %[[GLSLSTD450]] UMin %[[max]] %[[longmax]]
  1088. ; CHECK: %[[clamp:\w+]] = OpExtInst %ulong %[[GLSLSTD450]] SClamp %i %ulong_0 %[[smin]]
  1089. )" << MainPrefix()
  1090. << ACCheck(ac, "%int_2 %i", "%int_2 %[[clamp]]") << MainSuffix();
  1091. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  1092. }
  1093. }
  1094. TEST_F(GraphicsRobustAccessTest, ACRTArrayStructVectorElem) {
  1095. // The point of this test is that the access chain can have indices past the
  1096. // index into the runtime array. For good measure, the index into the final
  1097. // struct is out of bounds. We have to clamp that index too.
  1098. for (auto* ac : AccessChains()) {
  1099. std::ostringstream shaders;
  1100. shaders << ShaderPreambleAC({"i", "j"})
  1101. << "OpDecorate %rtarr ArrayStride 32\n"
  1102. << DecoSSBO() << "OpMemberDecorate %rtelem 0 Offset 0\n"
  1103. << "OpMemberDecorate %rtelem 1 Offset 16\n"
  1104. << TypesVoid() << TypesInt() << TypesFloat() << R"(
  1105. %v4float = OpTypeVector %float 4
  1106. %rtelem = OpTypeStruct %v4float %v4float
  1107. %rtarr = OpTypeRuntimeArray %rtelem
  1108. %ssbo_s = OpTypeStruct %int %int %rtarr
  1109. %var_ty = OpTypePointer Uniform %ssbo_s
  1110. %ptr_ty = OpTypePointer Uniform %float
  1111. %var = OpVariable %var_ty Uniform
  1112. %int_1 = OpConstant %int 1
  1113. %int_2 = OpConstant %int 2
  1114. %i = OpUndef %int
  1115. %j = OpUndef %int
  1116. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  1117. ; CHECK-DAG: %int_0 = OpConstant %int 0
  1118. ; CHECK-DAG: %int_3 = OpConstant %int 3
  1119. ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %int 2147483647
  1120. ; CHECK: OpLabel
  1121. ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %var 2
  1122. ; CHECK: %[[max:\w+]] = OpISub %int %[[arrlen]] %int_1
  1123. ; CHECK: %[[smin:\w+]] = OpExtInst %int %[[GLSLSTD450]] UMin %[[max]] %[[intmax]]
  1124. ; CHECK: %[[clamp_i:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %i %int_0 %[[smin]]
  1125. ; CHECK: %[[clamp_j:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %j %int_0 %int_3
  1126. )" << MainPrefix()
  1127. << ACCheck(ac, "%int_2 %i %int_1 %j",
  1128. "%int_2 %[[clamp_i]] %int_1 %[[clamp_j]]")
  1129. << MainSuffix();
  1130. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  1131. }
  1132. }
  1133. TEST_F(GraphicsRobustAccessTest, ACArrayRTArrayStructVectorElem) {
  1134. // Now add an additional level of arrays around the Block-decorated struct.
  1135. for (auto* ac : AccessChains()) {
  1136. std::ostringstream shaders;
  1137. shaders << ShaderPreambleAC({"i", "ssbo_s"})
  1138. << "OpDecorate %rtarr ArrayStride 32\n"
  1139. << DecoSSBO() << "OpMemberDecorate %rtelem 0 Offset 0\n"
  1140. << "OpMemberDecorate %rtelem 1 Offset 16\n"
  1141. << TypesVoid() << TypesInt() << TypesFloat() << R"(
  1142. %v4float = OpTypeVector %float 4
  1143. %rtelem = OpTypeStruct %v4float %v4float
  1144. %rtarr = OpTypeRuntimeArray %rtelem
  1145. %ssbo_s = OpTypeStruct %int %int %rtarr
  1146. %arr_size = OpConstant %int 10
  1147. %arr_ssbo = OpTypeArray %ssbo_s %arr_size
  1148. %var_ty = OpTypePointer Uniform %arr_ssbo
  1149. %ptr_ty = OpTypePointer Uniform %float
  1150. %var = OpVariable %var_ty Uniform
  1151. %int_1 = OpConstant %int 1
  1152. %int_2 = OpConstant %int 2
  1153. %int_17 = OpConstant %int 17
  1154. %i = OpUndef %int
  1155. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  1156. ; CHECK-DAG: %[[ssbo_p:\w+]] = OpTypePointer Uniform %ssbo_s
  1157. ; CHECK-DAG: %int_0 = OpConstant %int 0
  1158. ; CHECK-DAG: %int_9 = OpConstant %int 9
  1159. ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %int 2147483647
  1160. ; CHECK: OpLabel
  1161. ; This access chain is manufatured only so we can compute the array length.
  1162. ; Note that the %int_9 is already clamped
  1163. ; CHECK: %[[ssbo_base:\w+]] = )" << ac
  1164. << R"( %[[ssbo_p]] %var %int_9
  1165. ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %[[ssbo_base]] 2
  1166. ; CHECK: %[[max:\w+]] = OpISub %int %[[arrlen]] %int_1
  1167. ; CHECK: %[[smin:\w+]] = OpExtInst %int %[[GLSLSTD450]] UMin %[[max]] %[[intmax]]
  1168. ; CHECK: %[[clamp_i:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %i %int_0 %[[smin]]
  1169. )" << MainPrefix()
  1170. << ACCheck(ac, "%int_17 %int_2 %i %int_1 %int_2",
  1171. "%int_9 %int_2 %[[clamp_i]] %int_1 %int_2")
  1172. << MainSuffix();
  1173. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  1174. }
  1175. }
  1176. TEST_F(GraphicsRobustAccessTest, ACSplitACArrayRTArrayStructVectorElem) {
  1177. // Split the address calculation across two access chains. Force
  1178. // the transform to walk up the access chains to find the base variable.
  1179. for (auto* ac : AccessChains()) {
  1180. std::ostringstream shaders;
  1181. shaders << ShaderPreambleAC({"i", "j", "k", "ssbo_s", "ssbo_pty",
  1182. "rtarr_pty", "ac_ssbo", "ac_rtarr"})
  1183. << "OpDecorate %rtarr ArrayStride 32\n"
  1184. << DecoSSBO() << "OpMemberDecorate %rtelem 0 Offset 0\n"
  1185. << "OpMemberDecorate %rtelem 1 Offset 16\n"
  1186. << TypesVoid() << TypesInt() << TypesFloat() << R"(
  1187. %v4float = OpTypeVector %float 4
  1188. %rtelem = OpTypeStruct %v4float %v4float
  1189. %rtarr = OpTypeRuntimeArray %rtelem
  1190. %ssbo_s = OpTypeStruct %int %int %rtarr
  1191. %arr_size = OpConstant %int 10
  1192. %arr_ssbo = OpTypeArray %ssbo_s %arr_size
  1193. %var_ty = OpTypePointer Uniform %arr_ssbo
  1194. %ssbo_pty = OpTypePointer Uniform %ssbo_s
  1195. %rtarr_pty = OpTypePointer Uniform %rtarr
  1196. %ptr_ty = OpTypePointer Uniform %float
  1197. %var = OpVariable %var_ty Uniform
  1198. %int_1 = OpConstant %int 1
  1199. %int_2 = OpConstant %int 2
  1200. %i = OpUndef %int
  1201. %j = OpUndef %int
  1202. %k = OpUndef %int
  1203. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  1204. ; CHECK-DAG: %int_0 = OpConstant %int 0
  1205. ; CHECK-DAG: %int_9 = OpConstant %int 9
  1206. ; CHECK-DAG: %int_3 = OpConstant %int 3
  1207. ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %int 2147483647
  1208. ; CHECK: OpLabel
  1209. ; CHECK: %[[clamp_i:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %i %int_0 %int_9
  1210. ; CHECK: %ac_ssbo = )" << ac
  1211. << R"( %ssbo_pty %var %[[clamp_i]]
  1212. ; CHECK: %ac_rtarr = )"
  1213. << ac << R"( %rtarr_pty %ac_ssbo %int_2
  1214. ; This is the interesting bit. This array length is needed for an OpAccessChain
  1215. ; computing %ac, but the algorithm had to track back through %ac_rtarr's
  1216. ; definition to find the base pointer %ac_ssbo.
  1217. ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %ac_ssbo 2
  1218. ; CHECK: %[[max:\w+]] = OpISub %int %[[arrlen]] %int_1
  1219. ; CHECK: %[[smin:\w+]] = OpExtInst %int %[[GLSLSTD450]] UMin %[[max]] %[[intmax]]
  1220. ; CHECK: %[[clamp_j:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %j %int_0 %[[smin]]
  1221. ; CHECK: %[[clamp_k:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %k %int_0 %int_3
  1222. ; CHECK: %ac = )" << ac
  1223. << R"( %ptr_ty %ac_rtarr %[[clamp_j]] %int_1 %[[clamp_k]]
  1224. ; CHECK-NOT: AccessChain
  1225. )" << MainPrefix()
  1226. << "%ac_ssbo = " << ac << " %ssbo_pty %var %i\n"
  1227. << "%ac_rtarr = " << ac << " %rtarr_pty %ac_ssbo %int_2\n"
  1228. << "%ac = " << ac << " %ptr_ty %ac_rtarr %j %int_1 %k\n"
  1229. << MainSuffix();
  1230. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  1231. }
  1232. }
  1233. TEST_F(GraphicsRobustAccessTest,
  1234. ACSplitACArrayRTArrayStructVectorElemAcrossBasicBlocks) {
  1235. // Split the address calculation across two access chains. Force
  1236. // the transform to walk up the access chains to find the base variable.
  1237. // This time, put the different access chains in different basic blocks.
  1238. // This is an integrity check to ensure that we keep the instruction-to-block
  1239. // mapping consistent.
  1240. for (auto* ac : AccessChains()) {
  1241. std::ostringstream shaders;
  1242. shaders << ShaderPreambleAC({"i", "j", "k", "bb1", "bb2", "ssbo_s",
  1243. "ssbo_pty", "rtarr_pty", "ac_ssbo",
  1244. "ac_rtarr"})
  1245. << "OpDecorate %rtarr ArrayStride 32\n"
  1246. << DecoSSBO() << "OpMemberDecorate %rtelem 0 Offset 0\n"
  1247. << "OpMemberDecorate %rtelem 1 Offset 16\n"
  1248. << TypesVoid() << TypesInt() << TypesFloat() << R"(
  1249. %v4float = OpTypeVector %float 4
  1250. %rtelem = OpTypeStruct %v4float %v4float
  1251. %rtarr = OpTypeRuntimeArray %rtelem
  1252. %ssbo_s = OpTypeStruct %int %int %rtarr
  1253. %arr_size = OpConstant %int 10
  1254. %arr_ssbo = OpTypeArray %ssbo_s %arr_size
  1255. %var_ty = OpTypePointer Uniform %arr_ssbo
  1256. %ssbo_pty = OpTypePointer Uniform %ssbo_s
  1257. %rtarr_pty = OpTypePointer Uniform %rtarr
  1258. %ptr_ty = OpTypePointer Uniform %float
  1259. %var = OpVariable %var_ty Uniform
  1260. %int_1 = OpConstant %int 1
  1261. %int_2 = OpConstant %int 2
  1262. %i = OpUndef %int
  1263. %j = OpUndef %int
  1264. %k = OpUndef %int
  1265. ; CHECK: %[[GLSLSTD450:\w+]] = OpExtInstImport "GLSL.std.450"
  1266. ; CHECK-DAG: %int_0 = OpConstant %int 0
  1267. ; CHECK-DAG: %int_9 = OpConstant %int 9
  1268. ; CHECK-DAG: %int_3 = OpConstant %int 3
  1269. ; CHECK-DAG: %[[intmax:\w+]] = OpConstant %int 2147483647
  1270. ; CHECK: OpLabel
  1271. ; CHECK: %[[clamp_i:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %i %int_0 %int_9
  1272. ; CHECK: %ac_ssbo = )" << ac
  1273. << R"( %ssbo_pty %var %[[clamp_i]]
  1274. ; CHECK: %bb1 = OpLabel
  1275. ; CHECK: %ac_rtarr = )"
  1276. << ac << R"( %rtarr_pty %ac_ssbo %int_2
  1277. ; CHECK: %bb2 = OpLabel
  1278. ; This is the interesting bit. This array length is needed for an OpAccessChain
  1279. ; computing %ac, but the algorithm had to track back through %ac_rtarr's
  1280. ; definition to find the base pointer %ac_ssbo.
  1281. ; CHECK: %[[arrlen:\w+]] = OpArrayLength %uint %ac_ssbo 2
  1282. ; CHECK: %[[max:\w+]] = OpISub %int %[[arrlen]] %int_1
  1283. ; CHECK: %[[smin:\w+]] = OpExtInst %int %[[GLSLSTD450]] UMin %[[max]] %[[intmax]]
  1284. ; CHECK: %[[clamp_j:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %j %int_0 %[[smin]]
  1285. ; CHECK: %[[clamp_k:\w+]] = OpExtInst %int %[[GLSLSTD450]] SClamp %k %int_0 %int_3
  1286. ; CHECK: %ac = )" << ac
  1287. << R"( %ptr_ty %ac_rtarr %[[clamp_j]] %int_1 %[[clamp_k]]
  1288. ; CHECK-NOT: AccessChain
  1289. )" << MainPrefix()
  1290. << "%ac_ssbo = " << ac << " %ssbo_pty %var %i\n"
  1291. << "OpBranch %bb1\n%bb1 = OpLabel\n"
  1292. << "%ac_rtarr = " << ac << " %rtarr_pty %ac_ssbo %int_2\n"
  1293. << "OpBranch %bb2\n%bb2 = OpLabel\n"
  1294. << "%ac = " << ac << " %ptr_ty %ac_rtarr %j %int_1 %k\n"
  1295. << MainSuffix();
  1296. SinglePassRunAndMatch<GraphicsRobustAccessPass>(shaders.str(), true);
  1297. }
  1298. }
  1299. TEST_F(GraphicsRobustAccessTest, bug3813) {
  1300. // This shader comes from Dawn's
  1301. // TextureViewSamplingTest.TextureCubeMapOnWholeTexture, converted from GLSL
  1302. // by glslang.
  1303. // The pass was inserting a signed 32-bit int type, but not correctly marking
  1304. // the shader as changed.
  1305. std::string shader = R"(
  1306. ; SPIR-V
  1307. ; Version: 1.0
  1308. ; Generator: Google Shaderc over Glslang; 10
  1309. ; Bound: 46
  1310. ; Schema: 0
  1311. OpCapability Shader
  1312. %1 = OpExtInstImport "GLSL.std.450"
  1313. OpMemoryModel Logical GLSL450
  1314. OpEntryPoint Fragment %4 "main" %12 %29
  1315. OpExecutionMode %4 OriginUpperLeft
  1316. OpSource GLSL 450
  1317. OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
  1318. OpSourceExtension "GL_GOOGLE_include_directive"
  1319. OpName %4 "main"
  1320. OpName %8 "sc"
  1321. OpName %12 "texCoord"
  1322. OpName %21 "tc"
  1323. OpName %29 "fragColor"
  1324. OpName %32 "texture0"
  1325. OpName %36 "sampler0"
  1326. OpDecorate %12 Location 0
  1327. OpDecorate %29 Location 0
  1328. OpDecorate %32 DescriptorSet 0
  1329. OpDecorate %32 Binding 1
  1330. OpDecorate %36 DescriptorSet 0
  1331. OpDecorate %36 Binding 0
  1332. %2 = OpTypeVoid
  1333. %3 = OpTypeFunction %2
  1334. %6 = OpTypeFloat 32
  1335. %7 = OpTypePointer Function %6
  1336. %9 = OpConstant %6 2
  1337. %10 = OpTypeVector %6 2
  1338. %11 = OpTypePointer Input %10
  1339. %12 = OpVariable %11 Input
  1340. %13 = OpTypeInt 32 0
  1341. %14 = OpConstant %13 0
  1342. %15 = OpTypePointer Input %6
  1343. %19 = OpConstant %6 1
  1344. %22 = OpConstant %13 1
  1345. %27 = OpTypeVector %6 4
  1346. %28 = OpTypePointer Output %27
  1347. %29 = OpVariable %28 Output
  1348. %30 = OpTypeImage %6 Cube 0 0 0 1 Unknown
  1349. %31 = OpTypePointer UniformConstant %30
  1350. %32 = OpVariable %31 UniformConstant
  1351. %34 = OpTypeSampler
  1352. %35 = OpTypePointer UniformConstant %34
  1353. %36 = OpVariable %35 UniformConstant
  1354. %38 = OpTypeSampledImage %30
  1355. %43 = OpTypeVector %6 3
  1356. %4 = OpFunction %2 None %3
  1357. %5 = OpLabel
  1358. %8 = OpVariable %7 Function
  1359. %21 = OpVariable %7 Function
  1360. %16 = OpAccessChain %15 %12 %14
  1361. %17 = OpLoad %6 %16
  1362. %18 = OpFMul %6 %9 %17
  1363. %20 = OpFSub %6 %18 %19
  1364. OpStore %8 %20
  1365. %23 = OpAccessChain %15 %12 %22
  1366. %24 = OpLoad %6 %23
  1367. %25 = OpFMul %6 %9 %24
  1368. %26 = OpFSub %6 %25 %19
  1369. OpStore %21 %26
  1370. %33 = OpLoad %30 %32
  1371. %37 = OpLoad %34 %36
  1372. %39 = OpSampledImage %38 %33 %37
  1373. %40 = OpLoad %6 %21
  1374. %41 = OpLoad %6 %8
  1375. %42 = OpFNegate %6 %41
  1376. %44 = OpCompositeConstruct %43 %19 %40 %42
  1377. %45 = OpImageSampleImplicitLod %27 %39 %44
  1378. OpStore %29 %45
  1379. OpReturn
  1380. OpFunctionEnd
  1381. )";
  1382. std::string expected = R"(OpCapability Shader
  1383. %1 = OpExtInstImport "GLSL.std.450"
  1384. OpMemoryModel Logical GLSL450
  1385. OpEntryPoint Fragment %main "main" %texCoord %fragColor
  1386. OpExecutionMode %main OriginUpperLeft
  1387. OpSource GLSL 450
  1388. OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
  1389. OpSourceExtension "GL_GOOGLE_include_directive"
  1390. OpName %main "main"
  1391. OpName %sc "sc"
  1392. OpName %texCoord "texCoord"
  1393. OpName %tc "tc"
  1394. OpName %fragColor "fragColor"
  1395. OpName %texture0 "texture0"
  1396. OpName %sampler0 "sampler0"
  1397. OpDecorate %texCoord Location 0
  1398. OpDecorate %fragColor Location 0
  1399. OpDecorate %texture0 DescriptorSet 0
  1400. OpDecorate %texture0 Binding 1
  1401. OpDecorate %sampler0 DescriptorSet 0
  1402. OpDecorate %sampler0 Binding 0
  1403. %void = OpTypeVoid
  1404. %10 = OpTypeFunction %void
  1405. %float = OpTypeFloat 32
  1406. %_ptr_Function_float = OpTypePointer Function %float
  1407. %float_2 = OpConstant %float 2
  1408. %v2float = OpTypeVector %float 2
  1409. %_ptr_Input_v2float = OpTypePointer Input %v2float
  1410. %texCoord = OpVariable %_ptr_Input_v2float Input
  1411. %uint = OpTypeInt 32 0
  1412. %uint_0 = OpConstant %uint 0
  1413. %_ptr_Input_float = OpTypePointer Input %float
  1414. %float_1 = OpConstant %float 1
  1415. %uint_1 = OpConstant %uint 1
  1416. %v4float = OpTypeVector %float 4
  1417. %_ptr_Output_v4float = OpTypePointer Output %v4float
  1418. %fragColor = OpVariable %_ptr_Output_v4float Output
  1419. %23 = OpTypeImage %float Cube 0 0 0 1 Unknown
  1420. %_ptr_UniformConstant_23 = OpTypePointer UniformConstant %23
  1421. %texture0 = OpVariable %_ptr_UniformConstant_23 UniformConstant
  1422. %25 = OpTypeSampler
  1423. %_ptr_UniformConstant_25 = OpTypePointer UniformConstant %25
  1424. %sampler0 = OpVariable %_ptr_UniformConstant_25 UniformConstant
  1425. %27 = OpTypeSampledImage %23
  1426. %v3float = OpTypeVector %float 3
  1427. %int = OpTypeInt 32 1
  1428. %main = OpFunction %void None %10
  1429. %29 = OpLabel
  1430. %sc = OpVariable %_ptr_Function_float Function
  1431. %tc = OpVariable %_ptr_Function_float Function
  1432. %30 = OpAccessChain %_ptr_Input_float %texCoord %uint_0
  1433. %31 = OpLoad %float %30
  1434. %32 = OpFMul %float %float_2 %31
  1435. %33 = OpFSub %float %32 %float_1
  1436. OpStore %sc %33
  1437. %34 = OpAccessChain %_ptr_Input_float %texCoord %uint_1
  1438. %35 = OpLoad %float %34
  1439. %36 = OpFMul %float %float_2 %35
  1440. %37 = OpFSub %float %36 %float_1
  1441. OpStore %tc %37
  1442. %38 = OpLoad %23 %texture0
  1443. %39 = OpLoad %25 %sampler0
  1444. %40 = OpSampledImage %27 %38 %39
  1445. %41 = OpLoad %float %tc
  1446. %42 = OpLoad %float %sc
  1447. %43 = OpFNegate %float %42
  1448. %44 = OpCompositeConstruct %v3float %float_1 %41 %43
  1449. %45 = OpImageSampleImplicitLod %v4float %40 %44
  1450. OpStore %fragColor %45
  1451. OpReturn
  1452. OpFunctionEnd
  1453. )";
  1454. SinglePassRunAndCheck<GraphicsRobustAccessPass>(shader, expected, false,
  1455. true);
  1456. }
  1457. // TODO(dneto): Test access chain index wider than 64 bits?
  1458. // TODO(dneto): Test struct access chain index wider than 64 bits?
  1459. // TODO(dneto): OpImageTexelPointer
  1460. // - all Dim types: 1D 2D Cube 3D Rect Buffer
  1461. // - all Dim types that can be arrayed: 1D 2D 3D
  1462. // - sample index: set to 0 if not multisampled
  1463. // - Dim (2D, Cube Rect} with multisampling
  1464. // -1 0 max excess
  1465. // TODO(dneto): Test OpImageTexelPointer with coordinate component index other
  1466. // than 32 bits.
  1467. } // namespace