workaround1209_test.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 <string>
  15. #include <unordered_set>
  16. #include "test/opt/pass_fixture.h"
  17. #include "test/opt/pass_utils.h"
  18. namespace spvtools {
  19. namespace opt {
  20. namespace {
  21. using Workaround1209Test = PassTest<::testing::Test>;
  22. TEST_F(Workaround1209Test, RemoveOpUnreachableInLoop) {
  23. const std::string text = R"(
  24. OpCapability Shader
  25. %1 = OpExtInstImport "GLSL.std.450"
  26. OpMemoryModel Logical GLSL450
  27. OpEntryPoint Vertex %main "main" %texcoord %gl_VertexIndex %_
  28. OpSource GLSL 400
  29. OpSourceExtension "GL_ARB_separate_shader_objects"
  30. OpSourceExtension "GL_ARB_shading_language_420pack"
  31. OpName %main "main"
  32. OpName %texcoord "texcoord"
  33. OpName %buf "buf"
  34. OpMemberName %buf 0 "MVP"
  35. OpMemberName %buf 1 "position"
  36. OpMemberName %buf 2 "attr"
  37. OpName %ubuf "ubuf"
  38. OpName %gl_VertexIndex "gl_VertexIndex"
  39. OpName %gl_PerVertex "gl_PerVertex"
  40. OpMemberName %gl_PerVertex 0 "gl_Position"
  41. OpName %_ ""
  42. OpDecorate %texcoord Location 0
  43. OpDecorate %_arr_v4float_uint_72 ArrayStride 16
  44. OpDecorate %_arr_v4float_uint_72_0 ArrayStride 16
  45. OpMemberDecorate %buf 0 ColMajor
  46. OpMemberDecorate %buf 0 Offset 0
  47. OpMemberDecorate %buf 0 MatrixStride 16
  48. OpMemberDecorate %buf 1 Offset 64
  49. OpMemberDecorate %buf 2 Offset 1216
  50. OpDecorate %buf Block
  51. OpDecorate %ubuf DescriptorSet 0
  52. OpDecorate %ubuf Binding 0
  53. OpDecorate %gl_VertexIndex BuiltIn VertexIndex
  54. OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
  55. OpDecorate %gl_PerVertex Block
  56. %void = OpTypeVoid
  57. %12 = OpTypeFunction %void
  58. %float = OpTypeFloat 32
  59. %v4float = OpTypeVector %float 4
  60. %_ptr_Output_v4float = OpTypePointer Output %v4float
  61. %texcoord = OpVariable %_ptr_Output_v4float Output
  62. %mat4v4float = OpTypeMatrix %v4float 4
  63. %uint = OpTypeInt 32 0
  64. %uint_72 = OpConstant %uint 72
  65. %_arr_v4float_uint_72 = OpTypeArray %v4float %uint_72
  66. %_arr_v4float_uint_72_0 = OpTypeArray %v4float %uint_72
  67. %buf = OpTypeStruct %mat4v4float %_arr_v4float_uint_72 %_arr_v4float_uint_72_0
  68. %_ptr_Uniform_buf = OpTypePointer Uniform %buf
  69. %ubuf = OpVariable %_ptr_Uniform_buf Uniform
  70. %int = OpTypeInt 32 1
  71. %int_2 = OpConstant %int 2
  72. %_ptr_Input_int = OpTypePointer Input %int
  73. %gl_VertexIndex = OpVariable %_ptr_Input_int Input
  74. %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
  75. %gl_PerVertex = OpTypeStruct %v4float
  76. %_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
  77. %_ = OpVariable %_ptr_Output_gl_PerVertex Output
  78. %int_0 = OpConstant %int 0
  79. %int_1 = OpConstant %int 1
  80. %float_1 = OpConstant %float 1
  81. %28 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  82. %main = OpFunction %void None %12
  83. %29 = OpLabel
  84. OpBranch %30
  85. %30 = OpLabel
  86. ; CHECK: OpLoopMerge [[merge:%[a-zA-Z_\d]+]]
  87. OpLoopMerge %31 %32 None
  88. OpBranch %33
  89. %33 = OpLabel
  90. ; CHECK: OpSelectionMerge [[sel_merge:%[a-zA-Z_\d]+]]
  91. OpSelectionMerge %34 None
  92. OpSwitch %int_1 %35
  93. %35 = OpLabel
  94. %36 = OpLoad %int %gl_VertexIndex
  95. %37 = OpAccessChain %_ptr_Uniform_v4float %ubuf %int_2 %36
  96. %38 = OpLoad %v4float %37
  97. OpStore %texcoord %38
  98. %39 = OpAccessChain %_ptr_Output_v4float %_ %int_0
  99. OpStore %39 %28
  100. OpBranch %31
  101. ; CHECK: [[sel_merge]] = OpLabel
  102. %34 = OpLabel
  103. ; CHECK-NEXT: OpBranch [[merge]]
  104. OpUnreachable
  105. %32 = OpLabel
  106. OpBranch %30
  107. %31 = OpLabel
  108. OpReturn
  109. OpFunctionEnd)";
  110. SinglePassRunAndMatch<Workaround1209>(text, false);
  111. }
  112. TEST_F(Workaround1209Test, RemoveOpUnreachableInNestedLoop) {
  113. const std::string text = R"(
  114. OpCapability Shader
  115. %1 = OpExtInstImport "GLSL.std.450"
  116. OpMemoryModel Logical GLSL450
  117. OpEntryPoint Vertex %2 "main" %3 %4 %5
  118. OpSource GLSL 400
  119. OpSourceExtension "GL_ARB_separate_shader_objects"
  120. OpSourceExtension "GL_ARB_shading_language_420pack"
  121. OpName %2 "main"
  122. OpName %3 "texcoord"
  123. OpName %6 "buf"
  124. OpMemberName %6 0 "MVP"
  125. OpMemberName %6 1 "position"
  126. OpMemberName %6 2 "attr"
  127. OpName %7 "ubuf"
  128. OpName %4 "gl_VertexIndex"
  129. OpName %8 "gl_PerVertex"
  130. OpMemberName %8 0 "gl_Position"
  131. OpName %5 ""
  132. OpDecorate %3 Location 0
  133. OpDecorate %9 ArrayStride 16
  134. OpDecorate %10 ArrayStride 16
  135. OpMemberDecorate %6 0 ColMajor
  136. OpMemberDecorate %6 0 Offset 0
  137. OpMemberDecorate %6 0 MatrixStride 16
  138. OpMemberDecorate %6 1 Offset 64
  139. OpMemberDecorate %6 2 Offset 1216
  140. OpDecorate %6 Block
  141. OpDecorate %7 DescriptorSet 0
  142. OpDecorate %7 Binding 0
  143. OpDecorate %4 BuiltIn VertexIndex
  144. OpMemberDecorate %8 0 BuiltIn Position
  145. OpDecorate %8 Block
  146. %11 = OpTypeVoid
  147. %12 = OpTypeFunction %11
  148. %13 = OpTypeFloat 32
  149. %14 = OpTypeVector %13 4
  150. %15 = OpTypePointer Output %14
  151. %3 = OpVariable %15 Output
  152. %16 = OpTypeMatrix %14 4
  153. %17 = OpTypeInt 32 0
  154. %18 = OpConstant %17 72
  155. %9 = OpTypeArray %14 %18
  156. %10 = OpTypeArray %14 %18
  157. %6 = OpTypeStruct %16 %9 %10
  158. %19 = OpTypePointer Uniform %6
  159. %7 = OpVariable %19 Uniform
  160. %20 = OpTypeInt 32 1
  161. %21 = OpConstant %20 2
  162. %22 = OpTypePointer Input %20
  163. %4 = OpVariable %22 Input
  164. %23 = OpTypePointer Uniform %14
  165. %8 = OpTypeStruct %14
  166. %24 = OpTypePointer Output %8
  167. %5 = OpVariable %24 Output
  168. %25 = OpConstant %20 0
  169. %26 = OpConstant %20 1
  170. %27 = OpConstant %13 1
  171. %28 = OpConstantComposite %14 %27 %27 %27 %27
  172. %2 = OpFunction %11 None %12
  173. %29 = OpLabel
  174. OpBranch %31
  175. %31 = OpLabel
  176. ; CHECK: OpLoopMerge
  177. OpLoopMerge %32 %33 None
  178. OpBranch %30
  179. %30 = OpLabel
  180. ; CHECK: OpLoopMerge [[merge:%[a-zA-Z_\d]+]]
  181. OpLoopMerge %34 %35 None
  182. OpBranch %36
  183. %36 = OpLabel
  184. ; CHECK: OpSelectionMerge [[sel_merge:%[a-zA-Z_\d]+]]
  185. OpSelectionMerge %37 None
  186. OpSwitch %26 %38
  187. %38 = OpLabel
  188. %39 = OpLoad %20 %4
  189. %40 = OpAccessChain %23 %7 %21 %39
  190. %41 = OpLoad %14 %40
  191. OpStore %3 %41
  192. %42 = OpAccessChain %15 %5 %25
  193. OpStore %42 %28
  194. OpBranch %34
  195. ; CHECK: [[sel_merge]] = OpLabel
  196. %37 = OpLabel
  197. ; CHECK-NEXT: OpBranch [[merge]]
  198. OpUnreachable
  199. %35 = OpLabel
  200. OpBranch %30
  201. %34 = OpLabel
  202. OpBranch %32
  203. %33 = OpLabel
  204. OpBranch %31
  205. %32 = OpLabel
  206. OpReturn
  207. OpFunctionEnd)";
  208. SinglePassRunAndMatch<Workaround1209>(text, false);
  209. }
  210. TEST_F(Workaround1209Test, RemoveOpUnreachableInAdjacentLoops) {
  211. const std::string text = R"(
  212. OpCapability Shader
  213. %1 = OpExtInstImport "GLSL.std.450"
  214. OpMemoryModel Logical GLSL450
  215. OpEntryPoint Vertex %2 "main" %3 %4 %5
  216. OpSource GLSL 400
  217. OpSourceExtension "GL_ARB_separate_shader_objects"
  218. OpSourceExtension "GL_ARB_shading_language_420pack"
  219. OpName %2 "main"
  220. OpName %3 "texcoord"
  221. OpName %6 "buf"
  222. OpMemberName %6 0 "MVP"
  223. OpMemberName %6 1 "position"
  224. OpMemberName %6 2 "attr"
  225. OpName %7 "ubuf"
  226. OpName %4 "gl_VertexIndex"
  227. OpName %8 "gl_PerVertex"
  228. OpMemberName %8 0 "gl_Position"
  229. OpName %5 ""
  230. OpDecorate %3 Location 0
  231. OpDecorate %9 ArrayStride 16
  232. OpDecorate %10 ArrayStride 16
  233. OpMemberDecorate %6 0 ColMajor
  234. OpMemberDecorate %6 0 Offset 0
  235. OpMemberDecorate %6 0 MatrixStride 16
  236. OpMemberDecorate %6 1 Offset 64
  237. OpMemberDecorate %6 2 Offset 1216
  238. OpDecorate %6 Block
  239. OpDecorate %7 DescriptorSet 0
  240. OpDecorate %7 Binding 0
  241. OpDecorate %4 BuiltIn VertexIndex
  242. OpMemberDecorate %8 0 BuiltIn Position
  243. OpDecorate %8 Block
  244. %11 = OpTypeVoid
  245. %12 = OpTypeFunction %11
  246. %13 = OpTypeFloat 32
  247. %14 = OpTypeVector %13 4
  248. %15 = OpTypePointer Output %14
  249. %3 = OpVariable %15 Output
  250. %16 = OpTypeMatrix %14 4
  251. %17 = OpTypeInt 32 0
  252. %18 = OpConstant %17 72
  253. %9 = OpTypeArray %14 %18
  254. %10 = OpTypeArray %14 %18
  255. %6 = OpTypeStruct %16 %9 %10
  256. %19 = OpTypePointer Uniform %6
  257. %7 = OpVariable %19 Uniform
  258. %20 = OpTypeInt 32 1
  259. %21 = OpConstant %20 2
  260. %22 = OpTypePointer Input %20
  261. %4 = OpVariable %22 Input
  262. %23 = OpTypePointer Uniform %14
  263. %8 = OpTypeStruct %14
  264. %24 = OpTypePointer Output %8
  265. %5 = OpVariable %24 Output
  266. %25 = OpConstant %20 0
  267. %26 = OpConstant %20 1
  268. %27 = OpConstant %13 1
  269. %28 = OpConstantComposite %14 %27 %27 %27 %27
  270. %2 = OpFunction %11 None %12
  271. %29 = OpLabel
  272. OpBranch %30
  273. %30 = OpLabel
  274. ; CHECK: OpLoopMerge [[merge1:%[a-zA-Z_\d]+]]
  275. OpLoopMerge %31 %32 None
  276. OpBranch %33
  277. %33 = OpLabel
  278. ; CHECK: OpSelectionMerge [[sel_merge1:%[a-zA-Z_\d]+]]
  279. OpSelectionMerge %34 None
  280. OpSwitch %26 %35
  281. %35 = OpLabel
  282. %36 = OpLoad %20 %4
  283. %37 = OpAccessChain %23 %7 %21 %36
  284. %38 = OpLoad %14 %37
  285. OpStore %3 %38
  286. %39 = OpAccessChain %15 %5 %25
  287. OpStore %39 %28
  288. OpBranch %31
  289. ; CHECK: [[sel_merge1]] = OpLabel
  290. %34 = OpLabel
  291. ; CHECK-NEXT: OpBranch [[merge1]]
  292. OpUnreachable
  293. %32 = OpLabel
  294. OpBranch %30
  295. %31 = OpLabel
  296. ; CHECK: OpLoopMerge [[merge2:%[a-zA-Z_\d]+]]
  297. OpLoopMerge %40 %41 None
  298. OpBranch %42
  299. %42 = OpLabel
  300. ; CHECK: OpSelectionMerge [[sel_merge2:%[a-zA-Z_\d]+]]
  301. OpSelectionMerge %43 None
  302. OpSwitch %26 %44
  303. %44 = OpLabel
  304. %45 = OpLoad %20 %4
  305. %46 = OpAccessChain %23 %7 %21 %45
  306. %47 = OpLoad %14 %46
  307. OpStore %3 %47
  308. %48 = OpAccessChain %15 %5 %25
  309. OpStore %48 %28
  310. OpBranch %40
  311. ; CHECK: [[sel_merge2]] = OpLabel
  312. %43 = OpLabel
  313. ; CHECK-NEXT: OpBranch [[merge2]]
  314. OpUnreachable
  315. %41 = OpLabel
  316. OpBranch %31
  317. %40 = OpLabel
  318. OpReturn
  319. OpFunctionEnd)";
  320. SinglePassRunAndMatch<Workaround1209>(text, false);
  321. }
  322. TEST_F(Workaround1209Test, LeaveUnreachableNotInLoop) {
  323. const std::string text = R"(
  324. OpCapability Shader
  325. %1 = OpExtInstImport "GLSL.std.450"
  326. OpMemoryModel Logical GLSL450
  327. OpEntryPoint Vertex %main "main" %texcoord %gl_VertexIndex %_
  328. OpSource GLSL 400
  329. OpSourceExtension "GL_ARB_separate_shader_objects"
  330. OpSourceExtension "GL_ARB_shading_language_420pack"
  331. OpName %main "main"
  332. OpName %texcoord "texcoord"
  333. OpName %buf "buf"
  334. OpMemberName %buf 0 "MVP"
  335. OpMemberName %buf 1 "position"
  336. OpMemberName %buf 2 "attr"
  337. OpName %ubuf "ubuf"
  338. OpName %gl_VertexIndex "gl_VertexIndex"
  339. OpName %gl_PerVertex "gl_PerVertex"
  340. OpMemberName %gl_PerVertex 0 "gl_Position"
  341. OpName %_ ""
  342. OpDecorate %texcoord Location 0
  343. OpDecorate %_arr_v4float_uint_72 ArrayStride 16
  344. OpDecorate %_arr_v4float_uint_72_0 ArrayStride 16
  345. OpMemberDecorate %buf 0 ColMajor
  346. OpMemberDecorate %buf 0 Offset 0
  347. OpMemberDecorate %buf 0 MatrixStride 16
  348. OpMemberDecorate %buf 1 Offset 64
  349. OpMemberDecorate %buf 2 Offset 1216
  350. OpDecorate %buf Block
  351. OpDecorate %ubuf DescriptorSet 0
  352. OpDecorate %ubuf Binding 0
  353. OpDecorate %gl_VertexIndex BuiltIn VertexIndex
  354. OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
  355. OpDecorate %gl_PerVertex Block
  356. %void = OpTypeVoid
  357. %12 = OpTypeFunction %void
  358. %float = OpTypeFloat 32
  359. %v4float = OpTypeVector %float 4
  360. %_ptr_Output_v4float = OpTypePointer Output %v4float
  361. %texcoord = OpVariable %_ptr_Output_v4float Output
  362. %mat4v4float = OpTypeMatrix %v4float 4
  363. %uint = OpTypeInt 32 0
  364. %uint_72 = OpConstant %uint 72
  365. %_arr_v4float_uint_72 = OpTypeArray %v4float %uint_72
  366. %_arr_v4float_uint_72_0 = OpTypeArray %v4float %uint_72
  367. %buf = OpTypeStruct %mat4v4float %_arr_v4float_uint_72 %_arr_v4float_uint_72_0
  368. %_ptr_Uniform_buf = OpTypePointer Uniform %buf
  369. %ubuf = OpVariable %_ptr_Uniform_buf Uniform
  370. %int = OpTypeInt 32 1
  371. %int_2 = OpConstant %int 2
  372. %_ptr_Input_int = OpTypePointer Input %int
  373. %gl_VertexIndex = OpVariable %_ptr_Input_int Input
  374. %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
  375. %gl_PerVertex = OpTypeStruct %v4float
  376. %_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
  377. %_ = OpVariable %_ptr_Output_gl_PerVertex Output
  378. %int_0 = OpConstant %int 0
  379. %int_1 = OpConstant %int 1
  380. %float_1 = OpConstant %float 1
  381. %28 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
  382. %main = OpFunction %void None %12
  383. %29 = OpLabel
  384. OpBranch %30
  385. %30 = OpLabel
  386. OpSelectionMerge %34 None
  387. OpSwitch %int_1 %35
  388. %35 = OpLabel
  389. %36 = OpLoad %int %gl_VertexIndex
  390. %37 = OpAccessChain %_ptr_Uniform_v4float %ubuf %int_2 %36
  391. %38 = OpLoad %v4float %37
  392. OpStore %texcoord %38
  393. %39 = OpAccessChain %_ptr_Output_v4float %_ %int_0
  394. OpStore %39 %28
  395. OpReturn
  396. %34 = OpLabel
  397. ; CHECK: OpUnreachable
  398. OpUnreachable
  399. OpFunctionEnd)";
  400. SinglePassRunAndMatch<Workaround1209>(text, false);
  401. }
  402. } // namespace
  403. } // namespace opt
  404. } // namespace spvtools