simplification_test.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // Copyright (c) 2018 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 <string>
  15. #include "gmock/gmock.h"
  16. #include "source/opt/simplification_pass.h"
  17. #include "test/opt/pass_fixture.h"
  18. namespace spvtools {
  19. namespace opt {
  20. namespace {
  21. using SimplificationTest = PassTest<::testing::Test>;
  22. TEST_F(SimplificationTest, StraightLineTest) {
  23. // Testing that folding rules are combined in simple straight line code.
  24. const std::string text = R"(OpCapability Shader
  25. %1 = OpExtInstImport "GLSL.std.450"
  26. OpMemoryModel Logical GLSL450
  27. OpEntryPoint Fragment %main "main" %i %o
  28. OpExecutionMode %main OriginUpperLeft
  29. OpSource GLSL 430
  30. OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
  31. OpSourceExtension "GL_GOOGLE_include_directive"
  32. OpName %main "main"
  33. OpName %i "i"
  34. OpName %o "o"
  35. OpDecorate %i Flat
  36. OpDecorate %i Location 0
  37. OpDecorate %o Location 0
  38. %void = OpTypeVoid
  39. %8 = OpTypeFunction %void
  40. %int = OpTypeInt 32 1
  41. %v4int = OpTypeVector %int 4
  42. %int_0 = OpConstant %int 0
  43. %13 = OpConstantComposite %v4int %int_0 %int_0 %int_0 %int_0
  44. %int_1 = OpConstant %int 1
  45. %_ptr_Input_v4int = OpTypePointer Input %v4int
  46. %i = OpVariable %_ptr_Input_v4int Input
  47. %_ptr_Output_int = OpTypePointer Output %int
  48. %o = OpVariable %_ptr_Output_int Output
  49. %main = OpFunction %void None %8
  50. %21 = OpLabel
  51. %31 = OpCompositeInsert %v4int %int_1 %13 0
  52. ; CHECK: [[load:%[a-zA-Z_\d]+]] = OpLoad
  53. %23 = OpLoad %v4int %i
  54. %33 = OpCompositeInsert %v4int %int_0 %23 0
  55. %35 = OpCompositeExtract %int %31 0
  56. ; CHECK: [[extract:%[a-zA-Z_\d]+]] = OpCompositeExtract %int [[load]] 1
  57. %37 = OpCompositeExtract %int %33 1
  58. ; CHECK: [[add:%[a-zA-Z_\d]+]] = OpIAdd %int %int_1 [[extract]]
  59. %29 = OpIAdd %int %35 %37
  60. OpStore %o %29
  61. OpReturn
  62. OpFunctionEnd
  63. )";
  64. SinglePassRunAndMatch<SimplificationPass>(text, false);
  65. }
  66. TEST_F(SimplificationTest, NewInstructionTest) {
  67. // Testing that new instructions are simplified. Specifically,
  68. // that the new add instruction generated by FactorAddMul is
  69. // further simplified by MergeGenericAddSub.
  70. const std::string text = R"(OpCapability Shader
  71. %1 = OpExtInstImport "GLSL.std.450"
  72. OpMemoryModel Logical GLSL450
  73. OpEntryPoint Fragment %main "main"
  74. OpExecutionMode %main OriginUpperLeft
  75. OpSource GLSL 430
  76. OpName %main "main"
  77. %void = OpTypeVoid
  78. %4 = OpTypeFunction %void
  79. %int = OpTypeInt 32 1
  80. %_ptr_int = OpTypePointer Function %int
  81. ; CHECK: [[mul:%[a-zA-Z_\d]+]] = OpIMul %int %13 %11
  82. %main = OpFunction %void None %4
  83. %7 = OpLabel
  84. %8 = OpVariable %_ptr_int Function
  85. %9 = OpVariable %_ptr_int Function
  86. %10 = OpVariable %_ptr_int Function
  87. %11 = OpLoad %int %8
  88. %12 = OpLoad %int %9
  89. %13 = OpLoad %int %10
  90. %14 = OpISub %int %11 %12
  91. %15 = OpIMul %int %13 %11
  92. %16 = OpIMul %int %13 %12
  93. %17 = OpIAdd %int %14 %15
  94. OpReturn
  95. OpFunctionEnd
  96. )";
  97. SinglePassRunAndMatch<SimplificationPass>(text, false);
  98. }
  99. TEST_F(SimplificationTest, AcrossBasicBlocks) {
  100. // Testing that folding rules are combined across basic blocks.
  101. const std::string text = R"(OpCapability Shader
  102. %1 = OpExtInstImport "GLSL.std.450"
  103. OpMemoryModel Logical GLSL450
  104. OpEntryPoint Fragment %main "main" %i %o
  105. OpExecutionMode %main OriginUpperLeft
  106. OpSource GLSL 430
  107. OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
  108. OpSourceExtension "GL_GOOGLE_include_directive"
  109. OpName %main "main"
  110. OpName %i "i"
  111. OpName %o "o"
  112. OpDecorate %i Flat
  113. OpDecorate %i Location 0
  114. OpDecorate %o Location 0
  115. %void = OpTypeVoid
  116. %8 = OpTypeFunction %void
  117. %int = OpTypeInt 32 1
  118. %v4int = OpTypeVector %int 4
  119. %int_0 = OpConstant %int 0
  120. %_ptr_Input_v4int = OpTypePointer Input %v4int
  121. %i = OpVariable %_ptr_Input_v4int Input
  122. %uint = OpTypeInt 32 0
  123. %uint_0 = OpConstant %uint 0
  124. %_ptr_Input_int = OpTypePointer Input %int
  125. %int_10 = OpConstant %int 10
  126. %bool = OpTypeBool
  127. %int_1 = OpConstant %int 1
  128. %_ptr_Output_int = OpTypePointer Output %int
  129. %o = OpVariable %_ptr_Output_int Output
  130. %main = OpFunction %void None %8
  131. %24 = OpLabel
  132. ; CHECK: [[load:%[a-zA-Z_\d]+]] = OpLoad %v4int %i
  133. %25 = OpLoad %v4int %i
  134. %41 = OpCompositeInsert %v4int %int_0 %25 0
  135. %27 = OpAccessChain %_ptr_Input_int %i %uint_0
  136. %28 = OpLoad %int %27
  137. %29 = OpSGreaterThan %bool %28 %int_10
  138. OpSelectionMerge %30 None
  139. OpBranchConditional %29 %31 %32
  140. %31 = OpLabel
  141. %43 = OpCopyObject %v4int %25
  142. OpBranch %30
  143. %32 = OpLabel
  144. %45 = OpCopyObject %v4int %25
  145. OpBranch %30
  146. %30 = OpLabel
  147. %50 = OpPhi %v4int %43 %31 %45 %32
  148. ; CHECK: [[extract1:%[a-zA-Z_\d]+]] = OpCompositeExtract %int [[load]] 0
  149. %47 = OpCompositeExtract %int %50 0
  150. ; CHECK: [[extract2:%[a-zA-Z_\d]+]] = OpCompositeExtract %int [[load]] 1
  151. %49 = OpCompositeExtract %int %41 1
  152. ; CHECK: [[add:%[a-zA-Z_\d]+]] = OpIAdd %int [[extract1]] [[extract2]]
  153. %39 = OpIAdd %int %47 %49
  154. OpStore %o %39
  155. OpReturn
  156. OpFunctionEnd
  157. )";
  158. SinglePassRunAndMatch<SimplificationPass>(text, false);
  159. }
  160. TEST_F(SimplificationTest, ThroughLoops) {
  161. // Testing that folding rules are applied multiple times to instructions
  162. // to be able to propagate across loop iterations.
  163. const std::string text = R"(
  164. OpCapability Shader
  165. %1 = OpExtInstImport "GLSL.std.450"
  166. OpMemoryModel Logical GLSL450
  167. OpEntryPoint Fragment %main "main" %o %i
  168. OpExecutionMode %main OriginUpperLeft
  169. OpSource GLSL 430
  170. OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
  171. OpSourceExtension "GL_GOOGLE_include_directive"
  172. OpName %main "main"
  173. OpName %o "o"
  174. OpName %i "i"
  175. OpDecorate %o Location 0
  176. OpDecorate %i Flat
  177. OpDecorate %i Location 0
  178. %void = OpTypeVoid
  179. %8 = OpTypeFunction %void
  180. %int = OpTypeInt 32 1
  181. %v4int = OpTypeVector %int 4
  182. %int_0 = OpConstant %int 0
  183. ; CHECK: [[constant:%[a-zA-Z_\d]+]] = OpConstantComposite %v4int %int_0 %int_0 %int_0 %int_0
  184. %13 = OpConstantComposite %v4int %int_0 %int_0 %int_0 %int_0
  185. %bool = OpTypeBool
  186. %_ptr_Output_int = OpTypePointer Output %int
  187. %o = OpVariable %_ptr_Output_int Output
  188. %_ptr_Input_v4int = OpTypePointer Input %v4int
  189. %i = OpVariable %_ptr_Input_v4int Input
  190. %68 = OpUndef %v4int
  191. %main = OpFunction %void None %8
  192. %23 = OpLabel
  193. ; CHECK: [[load:%[a-zA-Z_\d]+]] = OpLoad %v4int %i
  194. %load = OpLoad %v4int %i
  195. OpBranch %24
  196. %24 = OpLabel
  197. %67 = OpPhi %v4int %load %23 %64 %26
  198. ; CHECK: OpLoopMerge [[merge_lab:%[a-zA-Z_\d]+]]
  199. OpLoopMerge %25 %26 None
  200. OpBranch %27
  201. %27 = OpLabel
  202. %48 = OpCompositeExtract %int %67 0
  203. %30 = OpIEqual %bool %48 %int_0
  204. OpBranchConditional %30 %31 %25
  205. %31 = OpLabel
  206. %50 = OpCompositeExtract %int %67 0
  207. %54 = OpCompositeExtract %int %67 1
  208. %58 = OpCompositeExtract %int %67 2
  209. %62 = OpCompositeExtract %int %67 3
  210. %64 = OpCompositeConstruct %v4int %50 %54 %58 %62
  211. OpBranch %26
  212. %26 = OpLabel
  213. OpBranch %24
  214. %25 = OpLabel
  215. ; CHECK: [[merge_lab]] = OpLabel
  216. ; CHECK: [[extract:%[a-zA-Z_\d]+]] = OpCompositeExtract %int [[load]] 0
  217. %66 = OpCompositeExtract %int %67 0
  218. ; CHECK-NEXT: OpStore %o [[extract]]
  219. OpStore %o %66
  220. OpReturn
  221. OpFunctionEnd
  222. )";
  223. SinglePassRunAndMatch<SimplificationPass>(text, false);
  224. }
  225. TEST_F(SimplificationTest, CopyObjectWithDecorations1) {
  226. // Don't simplify OpCopyObject if the result id has a decoration that the
  227. // operand does not.
  228. const std::string text = R"(OpCapability Shader
  229. OpCapability ShaderNonUniform
  230. %1 = OpExtInstImport "GLSL.std.450"
  231. OpMemoryModel Logical GLSL450
  232. OpEntryPoint Fragment %2 "main"
  233. OpExecutionMode %2 OriginUpperLeft
  234. OpSource GLSL 430
  235. OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
  236. OpSourceExtension "GL_GOOGLE_include_directive"
  237. OpDecorate %3 NonUniform
  238. %void = OpTypeVoid
  239. %5 = OpTypeFunction %void
  240. %int = OpTypeInt 32 1
  241. %2 = OpFunction %void None %5
  242. %7 = OpLabel
  243. %8 = OpUndef %int
  244. %3 = OpCopyObject %int %8
  245. %9 = OpIAdd %int %3 %3
  246. OpReturn
  247. OpFunctionEnd
  248. )";
  249. SinglePassRunAndCheck<SimplificationPass>(text, text, false);
  250. }
  251. TEST_F(SimplificationTest, CopyObjectWithDecorations2) {
  252. // Simplify OpCopyObject if the result id is a subset of the decorations of
  253. // the operand.
  254. const std::string before = R"(OpCapability Shader
  255. OpCapability ShaderNonUniform
  256. %1 = OpExtInstImport "GLSL.std.450"
  257. OpMemoryModel Logical GLSL450
  258. OpEntryPoint Fragment %2 "main"
  259. OpExecutionMode %2 OriginUpperLeft
  260. OpSource GLSL 430
  261. OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
  262. OpSourceExtension "GL_GOOGLE_include_directive"
  263. OpDecorate %3 NonUniform
  264. %void = OpTypeVoid
  265. %5 = OpTypeFunction %void
  266. %int = OpTypeInt 32 1
  267. %2 = OpFunction %void None %5
  268. %7 = OpLabel
  269. %3 = OpUndef %int
  270. %8 = OpCopyObject %int %3
  271. %9 = OpIAdd %int %8 %8
  272. OpReturn
  273. OpFunctionEnd
  274. )";
  275. const std::string after = R"(OpCapability Shader
  276. OpCapability ShaderNonUniform
  277. %1 = OpExtInstImport "GLSL.std.450"
  278. OpMemoryModel Logical GLSL450
  279. OpEntryPoint Fragment %2 "main"
  280. OpExecutionMode %2 OriginUpperLeft
  281. OpSource GLSL 430
  282. OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
  283. OpSourceExtension "GL_GOOGLE_include_directive"
  284. OpDecorate %3 NonUniform
  285. %void = OpTypeVoid
  286. %5 = OpTypeFunction %void
  287. %int = OpTypeInt 32 1
  288. %2 = OpFunction %void None %5
  289. %7 = OpLabel
  290. %3 = OpUndef %int
  291. %9 = OpIAdd %int %3 %3
  292. OpReturn
  293. OpFunctionEnd
  294. )";
  295. SinglePassRunAndCheck<SimplificationPass>(before, after, false);
  296. }
  297. TEST_F(SimplificationTest, DontMoveDecorations) {
  298. const std::string spirv = R"(
  299. ; CHECK-NOT: RelaxedPrecision
  300. ; CHECK: [[sub:%\w+]] = OpFSub
  301. ; CHECK: OpStore {{.*}} [[sub]]
  302. OpCapability Shader
  303. OpMemoryModel Logical GLSL450
  304. OpEntryPoint GLCompute %main "main"
  305. OpExecutionMode %main LocalSize 1 1 1
  306. OpDecorate %add RelaxedPrecision
  307. OpDecorate %block Block
  308. OpMemberDecorate %block 0 Offset 0
  309. OpMemberDecorate %block 1 Offset 4
  310. OpDecorate %in DescriptorSet 0
  311. OpDecorate %in Binding 0
  312. OpDecorate %out DescriptorSet 0
  313. OpDecorate %out Binding 1
  314. %void = OpTypeVoid
  315. %float = OpTypeFloat 32
  316. %void_fn = OpTypeFunction %void
  317. %block = OpTypeStruct %float %float
  318. %ptr_ssbo_block = OpTypePointer StorageBuffer %block
  319. %in = OpVariable %ptr_ssbo_block StorageBuffer
  320. %out = OpVariable %ptr_ssbo_block StorageBuffer
  321. %ptr_ssbo_float = OpTypePointer StorageBuffer %float
  322. %int = OpTypeInt 32 0
  323. %int_0 = OpConstant %int 0
  324. %int_1 = OpConstant %int 1
  325. %float_0 = OpConstant %float 0
  326. %main = OpFunction %void None %void_fn
  327. %entry = OpLabel
  328. %in_gep_0 = OpAccessChain %ptr_ssbo_float %in %int_0
  329. %in_gep_1 = OpAccessChain %ptr_ssbo_float %in %int_1
  330. %load_0 = OpLoad %float %in_gep_0
  331. %load_1 = OpLoad %float %in_gep_1
  332. %sub = OpFSub %float %load_0 %load_1
  333. %add = OpFAdd %float %float_0 %sub
  334. %out_gep_0 = OpAccessChain %ptr_ssbo_float %out %int_0
  335. OpStore %out_gep_0 %add
  336. OpReturn
  337. OpFunctionEnd
  338. )";
  339. SinglePassRunAndMatch<SimplificationPass>(spirv, true);
  340. }
  341. TEST_F(SimplificationTest, FunctionDeclaration) {
  342. // Make sure the pass works with a function declaration that is called.
  343. const std::string text = R"(OpCapability Addresses
  344. OpCapability Linkage
  345. OpCapability Kernel
  346. OpCapability Int8
  347. %1 = OpExtInstImport "OpenCL.std"
  348. OpMemoryModel Physical64 OpenCL
  349. OpEntryPoint Kernel %2 "_Z23julia__1166_kernel_77094Bool"
  350. OpExecutionMode %2 ContractionOff
  351. OpSource Unknown 0
  352. OpDecorate %3 LinkageAttributes "julia_error_7712" Import
  353. %void = OpTypeVoid
  354. %5 = OpTypeFunction %void
  355. %3 = OpFunction %void None %5
  356. OpFunctionEnd
  357. %2 = OpFunction %void None %5
  358. %6 = OpLabel
  359. %7 = OpFunctionCall %void %3
  360. OpReturn
  361. OpFunctionEnd
  362. )";
  363. SinglePassRunAndCheck<SimplificationPass>(text, text, false);
  364. }
  365. } // namespace
  366. } // namespace opt
  367. } // namespace spvtools