hoist_from_independent_loops.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/licm_pass.h"
  17. #include "test/opt/pass_fixture.h"
  18. namespace spvtools {
  19. namespace opt {
  20. namespace {
  21. using ::testing::UnorderedElementsAre;
  22. using PassClassTest = PassTest<::testing::Test>;
  23. /*
  24. Tests that the LICM pass will analyse multiple independent loops in a function
  25. Generated from the following GLSL fragment shader
  26. --eliminate-local-multi-store has also been run on the spv binary
  27. #version 440 core
  28. void main(){
  29. int a = 1;
  30. int b = 2;
  31. int hoist = 0;
  32. for (int i = 0; i < 10; i++) {
  33. // invariant
  34. hoist = a + b;
  35. }
  36. for (int i = 0; i < 10; i++) {
  37. // invariant
  38. hoist = a + b;
  39. }
  40. int c = 1;
  41. int d = 2;
  42. int hoist2 = 0;
  43. for (int i = 0; i < 10; i++) {
  44. // invariant
  45. hoist2 = c + d;
  46. }
  47. }
  48. */
  49. TEST_F(PassClassTest, HoistFromIndependentLoops) {
  50. const std::string before_hoist = R"(OpCapability Shader
  51. %1 = OpExtInstImport "GLSL.std.450"
  52. OpMemoryModel Logical GLSL450
  53. OpEntryPoint Fragment %main "main"
  54. OpExecutionMode %main OriginUpperLeft
  55. OpSource GLSL 440
  56. OpName %main "main"
  57. %void = OpTypeVoid
  58. %4 = OpTypeFunction %void
  59. %int = OpTypeInt 32 1
  60. %_ptr_Function_int = OpTypePointer Function %int
  61. %int_1 = OpConstant %int 1
  62. %int_2 = OpConstant %int 2
  63. %int_0 = OpConstant %int 0
  64. %int_10 = OpConstant %int 10
  65. %bool = OpTypeBool
  66. %main = OpFunction %void None %4
  67. %12 = OpLabel
  68. OpBranch %13
  69. %13 = OpLabel
  70. %14 = OpPhi %int %int_0 %12 %15 %16
  71. %17 = OpPhi %int %int_0 %12 %18 %16
  72. OpLoopMerge %19 %16 None
  73. OpBranch %20
  74. %20 = OpLabel
  75. %21 = OpSLessThan %bool %17 %int_10
  76. OpBranchConditional %21 %22 %19
  77. %22 = OpLabel
  78. %15 = OpIAdd %int %int_1 %int_2
  79. OpBranch %16
  80. %16 = OpLabel
  81. %18 = OpIAdd %int %17 %int_1
  82. OpBranch %13
  83. %19 = OpLabel
  84. OpBranch %23
  85. %23 = OpLabel
  86. %24 = OpPhi %int %14 %19 %25 %26
  87. %27 = OpPhi %int %int_0 %19 %28 %26
  88. OpLoopMerge %29 %26 None
  89. OpBranch %30
  90. %30 = OpLabel
  91. %31 = OpSLessThan %bool %27 %int_10
  92. OpBranchConditional %31 %32 %29
  93. %32 = OpLabel
  94. %25 = OpIAdd %int %int_1 %int_2
  95. OpBranch %26
  96. %26 = OpLabel
  97. %28 = OpIAdd %int %27 %int_1
  98. OpBranch %23
  99. %29 = OpLabel
  100. OpBranch %33
  101. %33 = OpLabel
  102. %34 = OpPhi %int %int_0 %29 %35 %36
  103. %37 = OpPhi %int %int_0 %29 %38 %36
  104. OpLoopMerge %39 %36 None
  105. OpBranch %40
  106. %40 = OpLabel
  107. %41 = OpSLessThan %bool %37 %int_10
  108. OpBranchConditional %41 %42 %39
  109. %42 = OpLabel
  110. %35 = OpIAdd %int %int_1 %int_2
  111. OpBranch %36
  112. %36 = OpLabel
  113. %38 = OpIAdd %int %37 %int_1
  114. OpBranch %33
  115. %39 = OpLabel
  116. OpReturn
  117. OpFunctionEnd
  118. )";
  119. const std::string after_hoist = R"(OpCapability Shader
  120. %1 = OpExtInstImport "GLSL.std.450"
  121. OpMemoryModel Logical GLSL450
  122. OpEntryPoint Fragment %main "main"
  123. OpExecutionMode %main OriginUpperLeft
  124. OpSource GLSL 440
  125. OpName %main "main"
  126. %void = OpTypeVoid
  127. %4 = OpTypeFunction %void
  128. %int = OpTypeInt 32 1
  129. %_ptr_Function_int = OpTypePointer Function %int
  130. %int_1 = OpConstant %int 1
  131. %int_2 = OpConstant %int 2
  132. %int_0 = OpConstant %int 0
  133. %int_10 = OpConstant %int 10
  134. %bool = OpTypeBool
  135. %main = OpFunction %void None %4
  136. %12 = OpLabel
  137. %15 = OpIAdd %int %int_1 %int_2
  138. OpBranch %13
  139. %13 = OpLabel
  140. %14 = OpPhi %int %int_0 %12 %15 %16
  141. %17 = OpPhi %int %int_0 %12 %18 %16
  142. OpLoopMerge %19 %16 None
  143. OpBranch %20
  144. %20 = OpLabel
  145. %21 = OpSLessThan %bool %17 %int_10
  146. OpBranchConditional %21 %22 %19
  147. %22 = OpLabel
  148. OpBranch %16
  149. %16 = OpLabel
  150. %18 = OpIAdd %int %17 %int_1
  151. OpBranch %13
  152. %19 = OpLabel
  153. %25 = OpIAdd %int %int_1 %int_2
  154. OpBranch %23
  155. %23 = OpLabel
  156. %24 = OpPhi %int %14 %19 %25 %26
  157. %27 = OpPhi %int %int_0 %19 %28 %26
  158. OpLoopMerge %29 %26 None
  159. OpBranch %30
  160. %30 = OpLabel
  161. %31 = OpSLessThan %bool %27 %int_10
  162. OpBranchConditional %31 %32 %29
  163. %32 = OpLabel
  164. OpBranch %26
  165. %26 = OpLabel
  166. %28 = OpIAdd %int %27 %int_1
  167. OpBranch %23
  168. %29 = OpLabel
  169. %35 = OpIAdd %int %int_1 %int_2
  170. OpBranch %33
  171. %33 = OpLabel
  172. %34 = OpPhi %int %int_0 %29 %35 %36
  173. %37 = OpPhi %int %int_0 %29 %38 %36
  174. OpLoopMerge %39 %36 None
  175. OpBranch %40
  176. %40 = OpLabel
  177. %41 = OpSLessThan %bool %37 %int_10
  178. OpBranchConditional %41 %42 %39
  179. %42 = OpLabel
  180. OpBranch %36
  181. %36 = OpLabel
  182. %38 = OpIAdd %int %37 %int_1
  183. OpBranch %33
  184. %39 = OpLabel
  185. OpReturn
  186. OpFunctionEnd
  187. )";
  188. SinglePassRunAndCheck<LICMPass>(before_hoist, after_hoist, true);
  189. }
  190. } // namespace
  191. } // namespace opt
  192. } // namespace spvtools