propagator_test.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <map>
  15. #include <memory>
  16. #include <string>
  17. #include <vector>
  18. #include "gmock/gmock.h"
  19. #include "gtest/gtest.h"
  20. #include "source/opt/build_module.h"
  21. #include "source/opt/cfg.h"
  22. #include "source/opt/ir_context.h"
  23. #include "source/opt/pass.h"
  24. #include "source/opt/propagator.h"
  25. namespace spvtools {
  26. namespace opt {
  27. namespace {
  28. using ::testing::UnorderedElementsAre;
  29. class PropagatorTest : public testing::Test {
  30. protected:
  31. virtual void TearDown() {
  32. ctx_.reset(nullptr);
  33. values_.clear();
  34. values_vec_.clear();
  35. }
  36. void Assemble(const std::string& input) {
  37. ctx_ = BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, input);
  38. ASSERT_NE(nullptr, ctx_) << "Assembling failed for shader:\n"
  39. << input << "\n";
  40. }
  41. bool Propagate(const SSAPropagator::VisitFunction& visit_fn) {
  42. SSAPropagator propagator(ctx_.get(), visit_fn);
  43. bool retval = false;
  44. for (auto& fn : *ctx_->module()) {
  45. retval |= propagator.Run(&fn);
  46. }
  47. return retval;
  48. }
  49. const std::vector<uint32_t>& GetValues() {
  50. values_vec_.clear();
  51. for (const auto& it : values_) {
  52. values_vec_.push_back(it.second);
  53. }
  54. return values_vec_;
  55. }
  56. std::unique_ptr<IRContext> ctx_;
  57. std::map<uint32_t, uint32_t> values_;
  58. std::vector<uint32_t> values_vec_;
  59. };
  60. TEST_F(PropagatorTest, LocalPropagate) {
  61. const std::string spv_asm = R"(
  62. OpCapability Shader
  63. %1 = OpExtInstImport "GLSL.std.450"
  64. OpMemoryModel Logical GLSL450
  65. OpEntryPoint Fragment %main "main" %outparm
  66. OpExecutionMode %main OriginUpperLeft
  67. OpSource GLSL 450
  68. OpName %main "main"
  69. OpName %x "x"
  70. OpName %y "y"
  71. OpName %z "z"
  72. OpName %outparm "outparm"
  73. OpDecorate %outparm Location 0
  74. %void = OpTypeVoid
  75. %3 = OpTypeFunction %void
  76. %int = OpTypeInt 32 1
  77. %_ptr_Function_int = OpTypePointer Function %int
  78. %int_4 = OpConstant %int 4
  79. %int_3 = OpConstant %int 3
  80. %int_1 = OpConstant %int 1
  81. %_ptr_Output_int = OpTypePointer Output %int
  82. %outparm = OpVariable %_ptr_Output_int Output
  83. %main = OpFunction %void None %3
  84. %5 = OpLabel
  85. %x = OpVariable %_ptr_Function_int Function
  86. %y = OpVariable %_ptr_Function_int Function
  87. %z = OpVariable %_ptr_Function_int Function
  88. OpStore %x %int_4
  89. OpStore %y %int_3
  90. OpStore %z %int_1
  91. %20 = OpLoad %int %z
  92. OpStore %outparm %20
  93. OpReturn
  94. OpFunctionEnd
  95. )";
  96. Assemble(spv_asm);
  97. const auto visit_fn = [this](Instruction* instr, BasicBlock** dest_bb) {
  98. *dest_bb = nullptr;
  99. if (instr->opcode() == SpvOpStore) {
  100. uint32_t lhs_id = instr->GetSingleWordOperand(0);
  101. uint32_t rhs_id = instr->GetSingleWordOperand(1);
  102. Instruction* rhs_def = ctx_->get_def_use_mgr()->GetDef(rhs_id);
  103. if (rhs_def->opcode() == SpvOpConstant) {
  104. uint32_t val = rhs_def->GetSingleWordOperand(2);
  105. values_[lhs_id] = val;
  106. return SSAPropagator::kInteresting;
  107. }
  108. }
  109. return SSAPropagator::kVarying;
  110. };
  111. EXPECT_TRUE(Propagate(visit_fn));
  112. EXPECT_THAT(GetValues(), UnorderedElementsAre(4, 3, 1));
  113. }
  114. TEST_F(PropagatorTest, PropagateThroughPhis) {
  115. const std::string spv_asm = R"(
  116. OpCapability Shader
  117. %1 = OpExtInstImport "GLSL.std.450"
  118. OpMemoryModel Logical GLSL450
  119. OpEntryPoint Fragment %main "main" %x %outparm
  120. OpExecutionMode %main OriginUpperLeft
  121. OpSource GLSL 450
  122. OpName %main "main"
  123. OpName %x "x"
  124. OpName %outparm "outparm"
  125. OpDecorate %x Flat
  126. OpDecorate %x Location 0
  127. OpDecorate %outparm Location 0
  128. %void = OpTypeVoid
  129. %3 = OpTypeFunction %void
  130. %int = OpTypeInt 32 1
  131. %bool = OpTypeBool
  132. %_ptr_Function_int = OpTypePointer Function %int
  133. %int_4 = OpConstant %int 4
  134. %int_3 = OpConstant %int 3
  135. %int_1 = OpConstant %int 1
  136. %_ptr_Input_int = OpTypePointer Input %int
  137. %x = OpVariable %_ptr_Input_int Input
  138. %_ptr_Output_int = OpTypePointer Output %int
  139. %outparm = OpVariable %_ptr_Output_int Output
  140. %main = OpFunction %void None %3
  141. %4 = OpLabel
  142. %5 = OpLoad %int %x
  143. %6 = OpSGreaterThan %bool %5 %int_3
  144. OpSelectionMerge %25 None
  145. OpBranchConditional %6 %22 %23
  146. %22 = OpLabel
  147. %7 = OpLoad %int %int_4
  148. OpBranch %25
  149. %23 = OpLabel
  150. %8 = OpLoad %int %int_4
  151. OpBranch %25
  152. %25 = OpLabel
  153. %35 = OpPhi %int %7 %22 %8 %23
  154. OpStore %outparm %35
  155. OpReturn
  156. OpFunctionEnd
  157. )";
  158. Assemble(spv_asm);
  159. Instruction* phi_instr = nullptr;
  160. const auto visit_fn = [this, &phi_instr](Instruction* instr,
  161. BasicBlock** dest_bb) {
  162. *dest_bb = nullptr;
  163. if (instr->opcode() == SpvOpLoad) {
  164. uint32_t rhs_id = instr->GetSingleWordOperand(2);
  165. Instruction* rhs_def = ctx_->get_def_use_mgr()->GetDef(rhs_id);
  166. if (rhs_def->opcode() == SpvOpConstant) {
  167. uint32_t val = rhs_def->GetSingleWordOperand(2);
  168. values_[instr->result_id()] = val;
  169. return SSAPropagator::kInteresting;
  170. }
  171. } else if (instr->opcode() == SpvOpPhi) {
  172. phi_instr = instr;
  173. SSAPropagator::PropStatus retval;
  174. for (uint32_t i = 2; i < instr->NumOperands(); i += 2) {
  175. uint32_t phi_arg_id = instr->GetSingleWordOperand(i);
  176. auto it = values_.find(phi_arg_id);
  177. if (it != values_.end()) {
  178. EXPECT_EQ(it->second, 4u);
  179. retval = SSAPropagator::kInteresting;
  180. values_[instr->result_id()] = it->second;
  181. } else {
  182. retval = SSAPropagator::kNotInteresting;
  183. break;
  184. }
  185. }
  186. return retval;
  187. }
  188. return SSAPropagator::kVarying;
  189. };
  190. EXPECT_TRUE(Propagate(visit_fn));
  191. // The propagator should've concluded that the Phi instruction has a constant
  192. // value of 4.
  193. EXPECT_NE(phi_instr, nullptr);
  194. EXPECT_EQ(values_[phi_instr->result_id()], 4u);
  195. EXPECT_THAT(GetValues(), UnorderedElementsAre(4u, 4u, 4u));
  196. }
  197. } // namespace
  198. } // namespace opt
  199. } // namespace spvtools