Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / luci / lang / src / CircleDialect.test.cpp
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
17 #include "luci/IR/CircleDialect.h"
18 #include "luci/IR/CircleNodes.h"
19
20 #include <loco.h>
21 #include <logo/DeadNodeQueryService.h>
22
23 #include <gtest/gtest.h>
24
25 TEST(CircleDialectTest, get_P)
26 {
27   auto d = luci::CircleDialect::get();
28
29   // get() SHOULD return a valid(non-null) pointer
30   ASSERT_NE(d, nullptr);
31   // The return value SHOULD be stable across multiple invocations
32   ASSERT_EQ(luci::CircleDialect::get(), d);
33 }
34
35 TEST(CircleDialectTest, check_if_dead_node_service)
36 {
37   /**
38    * [CircleInput1] [CircleInput2]       [CircleInput3]
39    *        \           /               (dangling input)
40    *         \         /
41    *         [CircleAdd]         [CircleBatchMatMul]
42    *              |                (dangling node)
43    *              |
44    *        [CircleOutput1]      [CircleOutput2]
45    *                            (dangling output)
46    */
47   auto g = loco::make_graph();
48
49   auto graph_input1 = g->inputs()->create();
50   auto circle_input1 = g->nodes()->create<luci::CircleInput>();
51   circle_input1->index(graph_input1->index());
52
53   auto graph_input2 = g->inputs()->create();
54   auto circle_input2 = g->nodes()->create<luci::CircleInput>();
55   circle_input2->index(graph_input2->index());
56
57   // dangling output
58   auto graph_input3 = g->inputs()->create();
59   auto dangling_input = g->nodes()->create<luci::CircleInput>();
60   dangling_input->index(graph_input3->index());
61
62   auto active_node = g->nodes()->create<luci::CircleAdd>();
63   active_node->x(circle_input1);
64   active_node->y(circle_input2);
65
66   auto dangling_node = g->nodes()->create<luci::CircleBatchMatMul>();
67
68   auto graph_output1 = g->outputs()->create();
69   auto circle_output1 = g->nodes()->create<luci::CircleOutput>();
70   circle_output1->index(graph_output1->index());
71   circle_output1->from(active_node);
72
73   // dangling output
74   auto graph_output2 = g->outputs()->create();
75   auto circle_output2 = g->nodes()->create<luci::CircleOutput>();
76   circle_output2->index(graph_output2->index());
77
78   auto service = active_node->dialect()->service<logo::DeadNodeQueryService>();
79
80   ASSERT_TRUE(service->isDeadNode(dangling_node));
81   ASSERT_FALSE(service->isDeadNode(dangling_input));
82   ASSERT_FALSE(service->isDeadNode(active_node));
83   ASSERT_FALSE(service->isDeadNode(circle_input1));
84   ASSERT_FALSE(service->isDeadNode(circle_input2));
85   ASSERT_FALSE(service->isDeadNode(circle_output1));
86   ASSERT_FALSE(service->isDeadNode(circle_output2));
87 }