dataflow.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Copyright (c) 2021 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 "source/opt/dataflow.h"
  15. #include <map>
  16. #include <set>
  17. #include "gtest/gtest.h"
  18. #include "opt/function_utils.h"
  19. #include "source/opt/build_module.h"
  20. namespace spvtools {
  21. namespace opt {
  22. namespace {
  23. using DataFlowTest = ::testing::Test;
  24. // Simple analyses for testing:
  25. // Stores the result IDs of visited instructions in visit order.
  26. struct VisitOrder : public ForwardDataFlowAnalysis {
  27. std::vector<uint32_t> visited_result_ids;
  28. VisitOrder(IRContext& context, LabelPosition label_position)
  29. : ForwardDataFlowAnalysis(context, label_position) {}
  30. VisitResult Visit(Instruction* inst) override {
  31. if (inst->HasResultId()) {
  32. visited_result_ids.push_back(inst->result_id());
  33. }
  34. return DataFlowAnalysis::VisitResult::kResultFixed;
  35. }
  36. };
  37. // For each block, stores the set of blocks it can be preceded by.
  38. // For example, with the following CFG:
  39. // V-----------.
  40. // -> 11 -> 12 -> 13 -> 15
  41. // \-> 14 ---^
  42. //
  43. // The answer is:
  44. // 11: 11, 12, 13
  45. // 12: 11, 12, 13
  46. // 13: 11, 12, 13
  47. // 14: 11, 12, 13
  48. // 15: 11, 12, 13, 14
  49. struct BackwardReachability : public ForwardDataFlowAnalysis {
  50. std::map<uint32_t, std::set<uint32_t>> reachable_from;
  51. BackwardReachability(IRContext& context)
  52. : ForwardDataFlowAnalysis(
  53. context, ForwardDataFlowAnalysis::LabelPosition::kLabelsOnly) {}
  54. VisitResult Visit(Instruction* inst) override {
  55. // Conditional branches can be enqueued from labels, so skip them.
  56. if (inst->opcode() != spv::Op::OpLabel)
  57. return DataFlowAnalysis::VisitResult::kResultFixed;
  58. uint32_t id = inst->result_id();
  59. VisitResult ret = DataFlowAnalysis::VisitResult::kResultFixed;
  60. std::set<uint32_t>& precedents = reachable_from[id];
  61. for (uint32_t pred : context().cfg()->preds(id)) {
  62. bool pred_inserted = precedents.insert(pred).second;
  63. if (pred_inserted) {
  64. ret = DataFlowAnalysis::VisitResult::kResultChanged;
  65. }
  66. for (uint32_t block : reachable_from[pred]) {
  67. bool inserted = precedents.insert(block).second;
  68. if (inserted) {
  69. ret = DataFlowAnalysis::VisitResult::kResultChanged;
  70. }
  71. }
  72. }
  73. return ret;
  74. }
  75. void InitializeWorklist(Function* function,
  76. bool is_first_iteration) override {
  77. // Since successor function is exact, only need one pass.
  78. if (is_first_iteration) {
  79. ForwardDataFlowAnalysis::InitializeWorklist(function, true);
  80. }
  81. }
  82. };
  83. TEST_F(DataFlowTest, ReversePostOrder) {
  84. // Note: labels and IDs are intentionally out of order.
  85. //
  86. // CFG: (order of branches is from bottom to top)
  87. // V-----------.
  88. // -> 50 -> 40 -> 20 -> 60 -> 70
  89. // \-> 30 ---^
  90. // DFS tree with RPO numbering:
  91. // -> 50[0] -> 40[1] -> 20[2] 60[4] -> 70[5]
  92. // \-> 30[3] ---^
  93. const std::string text = R"(
  94. OpCapability Shader
  95. %1 = OpExtInstImport "GLSL.std.450"
  96. OpMemoryModel Logical GLSL450
  97. OpEntryPoint Fragment %2 "main"
  98. OpExecutionMode %2 OriginUpperLeft
  99. OpSource GLSL 430
  100. %3 = OpTypeVoid
  101. %4 = OpTypeFunction %3
  102. %6 = OpTypeBool
  103. %5 = OpConstantTrue %6
  104. %2 = OpFunction %3 None %4
  105. %50 = OpLabel
  106. %51 = OpUndef %6
  107. %52 = OpUndef %6
  108. OpBranch %40
  109. %70 = OpLabel
  110. %69 = OpUndef %6
  111. OpReturn
  112. %60 = OpLabel
  113. %61 = OpUndef %6
  114. OpBranchConditional %5 %70 %40
  115. %30 = OpLabel
  116. %29 = OpUndef %6
  117. OpBranch %60
  118. %20 = OpLabel
  119. %21 = OpUndef %6
  120. OpBranch %60
  121. %40 = OpLabel
  122. %39 = OpUndef %6
  123. OpBranchConditional %5 %30 %20
  124. OpFunctionEnd
  125. )";
  126. std::unique_ptr<IRContext> context =
  127. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
  128. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  129. ASSERT_NE(context, nullptr);
  130. Function* function = spvtest::GetFunction(context->module(), 2);
  131. std::map<ForwardDataFlowAnalysis::LabelPosition, std::vector<uint32_t>>
  132. expected_order;
  133. expected_order[ForwardDataFlowAnalysis::LabelPosition::kLabelsOnly] = {
  134. 50, 40, 20, 30, 60, 70,
  135. };
  136. expected_order[ForwardDataFlowAnalysis::LabelPosition::kLabelsAtBeginning] = {
  137. 50, 51, 52, 40, 39, 20, 21, 30, 29, 60, 61, 70, 69,
  138. };
  139. expected_order[ForwardDataFlowAnalysis::LabelPosition::kLabelsAtEnd] = {
  140. 51, 52, 50, 39, 40, 21, 20, 29, 30, 61, 60, 69, 70,
  141. };
  142. expected_order[ForwardDataFlowAnalysis::LabelPosition::kNoLabels] = {
  143. 51, 52, 39, 21, 29, 61, 69,
  144. };
  145. for (const auto& test_case : expected_order) {
  146. VisitOrder analysis(*context, test_case.first);
  147. analysis.Run(function);
  148. EXPECT_EQ(test_case.second, analysis.visited_result_ids);
  149. }
  150. }
  151. TEST_F(DataFlowTest, BackwardReachability) {
  152. // CFG:
  153. // V-----------.
  154. // -> 11 -> 12 -> 13 -> 15
  155. // \-> 14 ---^
  156. const std::string text = R"(
  157. OpCapability Shader
  158. %1 = OpExtInstImport "GLSL.std.450"
  159. OpMemoryModel Logical GLSL450
  160. OpEntryPoint Fragment %2 "main"
  161. OpExecutionMode %2 OriginUpperLeft
  162. OpSource GLSL 430
  163. %3 = OpTypeVoid
  164. %4 = OpTypeFunction %3
  165. %6 = OpTypeBool
  166. %5 = OpConstantTrue %6
  167. %2 = OpFunction %3 None %4
  168. %11 = OpLabel
  169. OpBranch %12
  170. %12 = OpLabel
  171. OpBranchConditional %5 %14 %13
  172. %13 = OpLabel
  173. OpBranchConditional %5 %15 %11
  174. %14 = OpLabel
  175. OpBranch %15
  176. %15 = OpLabel
  177. OpReturn
  178. OpFunctionEnd
  179. )";
  180. std::unique_ptr<IRContext> context =
  181. BuildModule(SPV_ENV_UNIVERSAL_1_2, nullptr, text,
  182. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
  183. ASSERT_NE(context, nullptr);
  184. Function* function = spvtest::GetFunction(context->module(), 2);
  185. BackwardReachability analysis(*context);
  186. analysis.Run(function);
  187. std::map<uint32_t, std::set<uint32_t>> expected_result;
  188. expected_result[11] = {11, 12, 13};
  189. expected_result[12] = {11, 12, 13};
  190. expected_result[13] = {11, 12, 13};
  191. expected_result[14] = {11, 12, 13};
  192. expected_result[15] = {11, 12, 13, 14};
  193. EXPECT_EQ(expected_result, analysis.reachable_from);
  194. }
  195. } // namespace
  196. } // namespace opt
  197. } // namespace spvtools