propagator_test.cpp 6.9 KB

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