2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include "ir/OperationIndexMap.h"
22 #include "util/logging.h"
35 bool DAGChecker::verify(const Graph &graph) const noexcept
37 auto &operations = graph.operations();
40 OperationIndexMap<bool> visited;
42 [&](const OperationIndex &index, const Operation &) { visited[index] = false; });
43 OperationIndexMap<bool> on_stack = visited; // Copy from visited
45 std::function<void(const OperationIndex &index, const Operation &)> dfs_recursive =
46 [&](const OperationIndex &index, const Operation &node) -> void {
51 visited[index] = true;
52 on_stack[index] = true;
54 for (auto output : node.getOutputs() | Remove::DUPLICATED)
56 const auto &operand = graph.operands().at(output);
57 for (const auto &use : operand.getUses())
59 dfs_recursive(use, graph.operations().at(use));
63 on_stack[index] = false;
66 operations.iterate(dfs_recursive);
72 // EdgeConsistencyVerifier
75 bool EdgeConsistencyChecker::verify(const Graph &graph) const noexcept
77 auto &operations = graph.operations();
79 operations.iterate([&](const OperationIndex &index, const Operation &node) {
80 for (auto operand_index : node.getInputs() | ir::Remove::UNDEFINED)
84 auto &operand = graph.operands().at(operand_index);
85 bool operand_has_use = operand.getUses().contains(index);
88 VERBOSE(EdgeConsistencyChecker) << "[ERROR] EDGE MISMATCH : Missing USE edge - Operand "
89 << operand_index << " to Operation " << index
94 catch (const std::out_of_range &e)
96 VERBOSE(EdgeConsistencyChecker)
97 << "[ERROR] OPEARAND NOT FOUND : Operation " << index << " has Operand "
98 << operand_index << ", but the operand object is not present in the graph" << std::endl;
102 for (auto operand_index : node.getOutputs())
106 auto &operand = graph.operands().at(operand_index);
107 if (operand.getDef() != index)
109 VERBOSE(EdgeConsistencyChecker) << "[ERROR] EDGE MISMATCH : Missing DEF edge - Operand"
110 << operand_index << " to Operation " << index
115 catch (const std::out_of_range &e)
117 VERBOSE(EdgeConsistencyChecker)
118 << "[ERROR] OPEARAND NOT FOUND : Operation " << index << " has Operand "
119 << operand_index << ", but the operand object is not present in the graph" << std::endl;
125 VERBOSE(EdgeConsistencyChecker) << "Total Number of errors : " << errors << std::endl;
130 } // namespace verifier