hoist_simple_case.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. A simple test for the LICM pass
  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. }
  37. */
  38. TEST_F(PassClassTest, SimpleHoist) {
  39. const std::string before_hoist = R"(OpCapability Shader
  40. %1 = OpExtInstImport "GLSL.std.450"
  41. OpMemoryModel Logical GLSL450
  42. OpEntryPoint Fragment %main "main"
  43. OpExecutionMode %main OriginUpperLeft
  44. OpSource GLSL 440
  45. OpName %main "main"
  46. %void = OpTypeVoid
  47. %4 = OpTypeFunction %void
  48. %int = OpTypeInt 32 1
  49. %_ptr_Function_int = OpTypePointer Function %int
  50. %int_1 = OpConstant %int 1
  51. %int_2 = OpConstant %int 2
  52. %int_0 = OpConstant %int 0
  53. %int_10 = OpConstant %int 10
  54. %bool = OpTypeBool
  55. %main = OpFunction %void None %4
  56. %12 = OpLabel
  57. OpBranch %13
  58. %13 = OpLabel
  59. %14 = OpPhi %int %int_0 %12 %15 %16
  60. %17 = OpPhi %int %int_0 %12 %18 %16
  61. OpLoopMerge %19 %16 None
  62. OpBranch %20
  63. %20 = OpLabel
  64. %21 = OpSLessThan %bool %17 %int_10
  65. OpBranchConditional %21 %22 %19
  66. %22 = OpLabel
  67. %15 = OpIAdd %int %int_1 %int_2
  68. OpBranch %16
  69. %16 = OpLabel
  70. %18 = OpIAdd %int %17 %int_1
  71. OpBranch %13
  72. %19 = OpLabel
  73. OpReturn
  74. OpFunctionEnd
  75. )";
  76. const std::string after_hoist = R"(OpCapability Shader
  77. %1 = OpExtInstImport "GLSL.std.450"
  78. OpMemoryModel Logical GLSL450
  79. OpEntryPoint Fragment %main "main"
  80. OpExecutionMode %main OriginUpperLeft
  81. OpSource GLSL 440
  82. OpName %main "main"
  83. %void = OpTypeVoid
  84. %4 = OpTypeFunction %void
  85. %int = OpTypeInt 32 1
  86. %_ptr_Function_int = OpTypePointer Function %int
  87. %int_1 = OpConstant %int 1
  88. %int_2 = OpConstant %int 2
  89. %int_0 = OpConstant %int 0
  90. %int_10 = OpConstant %int 10
  91. %bool = OpTypeBool
  92. %main = OpFunction %void None %4
  93. %12 = OpLabel
  94. %15 = OpIAdd %int %int_1 %int_2
  95. OpBranch %13
  96. %13 = OpLabel
  97. %14 = OpPhi %int %int_0 %12 %15 %16
  98. %17 = OpPhi %int %int_0 %12 %18 %16
  99. OpLoopMerge %19 %16 None
  100. OpBranch %20
  101. %20 = OpLabel
  102. %21 = OpSLessThan %bool %17 %int_10
  103. OpBranchConditional %21 %22 %19
  104. %22 = OpLabel
  105. OpBranch %16
  106. %16 = OpLabel
  107. %18 = OpIAdd %int %17 %int_1
  108. OpBranch %13
  109. %19 = OpLabel
  110. OpReturn
  111. OpFunctionEnd
  112. )";
  113. SinglePassRunAndCheck<LICMPass>(before_hoist, after_hoist, true);
  114. }
  115. } // namespace
  116. } // namespace opt
  117. } // namespace spvtools