workaround1209_test.cpp 16 KB

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