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