2
0

dataflow.cpp 6.9 KB

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