ONE - On-device Neural Engine
Loading...
Searching...
No Matches
DeadCodeElimination.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
18#include "mir/Graph.h"
19
20#include <algorithm>
21
22using namespace mir;
23
25{
26 auto graph = static_cast<Graph *>(data);
27 assert(graph);
28
29 std::vector<Operation *> sorted_nodes = getSortedNodes(graph);
30
31 auto remove_if_unused = [graph](Operation *op) {
32 if (op->getType() == Operation::Type::input || op->getType() == Operation::Type::output)
33 return;
34
35 bool has_no_uses =
36 std::all_of(op->getOutputs().cbegin(), op->getOutputs().cend(),
37 [](const Operation::Output &output) { return output.getUses().empty(); });
38
39 if (has_no_uses)
40 {
41 graph->removeNode(op);
42 }
43 };
44
45 std::for_each(sorted_nodes.rbegin(), sorted_nodes.rend(), remove_if_unused);
46
47 return graph;
48}
PassData run(PassData data) override
run compiler pass
class that encapsulate value returned and taken by pass
Definition PassData.h:30
std::vector< Operation * > getSortedNodes(Graph *graph)
Returns nodes of the graph sorted topologically.
Definition Graph.cpp:42