Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / luci / lang / src / CircleDialect.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/Nodes/CircleInput.h"
19 #include "luci/IR/Nodes/CircleOutput.h"
20
21 #include <loco/IR/Graph.h>
22 #include <loco/IR/GraphInputIndex.h>
23 #include <loco/IR/GraphOutputIndex.h>
24
25 #include "DeadNodeQueryService.h"
26
27 #include <cassert>
28 #include <memory>
29
30 namespace
31 {
32
33 struct GiiQueryServiceImpl final : public loco::GraphInputIndexQueryService
34 {
35   bool associated(const loco::Node *node) const final
36   {
37     if (auto circleinput = dynamic_cast<const luci::CircleInput *>(node))
38     {
39       return circleinput->indexed();
40     }
41     return false;
42   }
43
44   loco::GraphOutputIndex index(const loco::Node *node) const final
45   {
46     assert(associated(node));
47     auto circleinput = loco::must_cast<const luci::CircleInput *>(node);
48     return circleinput->index();
49   }
50 };
51
52 struct GoiQueryServiceImpl final : public loco::GraphOutputIndexQueryService
53 {
54   bool associated(const loco::Node *node) const final
55   {
56     if (auto circleoutput = dynamic_cast<const luci::CircleOutput *>(node))
57     {
58       return circleoutput->indexed();
59     }
60     return false;
61   }
62
63   loco::GraphOutputIndex index(const loco::Node *node) const final
64   {
65     assert(associated(node));
66     auto circleoutput = loco::must_cast<const luci::CircleOutput *>(node);
67     return circleoutput->index();
68   }
69 };
70
71 } // namespace
72
73 namespace luci
74 {
75
76 CircleDialect::CircleDialect()
77 {
78   service<loco::GraphInputIndexQueryService>(std::make_unique<GiiQueryServiceImpl>());
79   service<loco::GraphOutputIndexQueryService>(std::make_unique<GoiQueryServiceImpl>());
80   service<logo::DeadNodeQueryService>(std::make_unique<DeadNodeQueryServiceImpl>());
81 }
82
83 loco::Dialect *CircleDialect::get(void)
84 {
85   static CircleDialect d;
86   return &d;
87 }
88
89 } // namespace luci