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/ops/AddOp.h"
19#include "mir/ops/ConstantOp.h"
20#include "mir/ops/InputOp.h"
21#include "mir/ops/OutputOp.h"
22
23#include <gtest/gtest.h>
24
25namespace
26{
27using namespace nnc;
28using namespace mir;
29
30TEST(DeadCodeEliminationTest, RemovesSingleNodes)
31{
33 graph.create<ops::ConstantOp>(TensorVariant{DataType::FLOAT32, {}});
34 graph.create<ops::ConstantOp>(TensorVariant{DataType::FLOAT32, {}});
35
37 pass.run(&graph);
38 ASSERT_EQ(graph.getNodes().size(), 0);
39}
40
41TEST(DeadCodeEliminationTest, RemovesChainedNodes)
42{
44 auto c1 = graph.create<ops::ConstantOp>(TensorVariant{DataType::FLOAT32, {}})->getOutput(0);
45 auto c2 = graph.create<ops::ConstantOp>(TensorVariant{DataType::FLOAT32, {}})->getOutput(0);
46 auto sum = graph.create<ops::AddOp>(c1, c2)->getOutput(0);
47 graph.create<ops::AddOp>(sum, sum);
48
50 pass.run(&graph);
51 ASSERT_EQ(graph.getNodes().size(), 0);
52}
53
54TEST(DeadCodeEliminationTest, PreservesInputNode)
55{
57 graph.create<ops::InputOp>(TensorType{DataType::FLOAT32, {}});
58
60 pass.run(&graph);
61 ASSERT_EQ(graph.getNodes().size(), 1);
62}
63
64TEST(DeadCodeEliminationTest, PreservesOutputNode)
65{
67 auto c = graph.create<ops::ConstantOp>(TensorVariant{DataType::FLOAT32, {}})->getOutput(0);
68 graph.create<ops::OutputOp>(c);
69
71 pass.run(&graph);
72 ASSERT_EQ(graph.getNodes().size(), 2);
73}
74
75TEST(DeadCodeEliminationTest, PreservesUsedNodes)
76{
78 auto c1 = graph.create<ops::ConstantOp>(TensorVariant{DataType::FLOAT32, {}})->getOutput(0);
79 auto c2 = graph.create<ops::ConstantOp>(TensorVariant{DataType::FLOAT32, {}})->getOutput(0);
80 graph.create<ops::AddOp>(c1, c2);
81 graph.create<ops::OutputOp>(c1);
82 graph.create<ops::OutputOp>(c2);
83
85 pass.run(&graph);
86 ASSERT_EQ(graph.getNodes().size(), 4);
87}
88
89} // unnamed namespace
This pass removes operations without uses. Importers currently only generate sConstantOps without use...
PassData run(PassData data) override
run compiler pass
TEST(Shape, Base)
Definition Index.cpp:24