Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci / plan / src / CircleNodeExecutionPlan.cpp
1 /*
2  * Copyright (c) 2021 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/Plan/CircleNodeExecutionPlan.h"
18
19 #include <loco.h>
20
21 #include <stdexcept>
22 #include <utility>
23
24 namespace
25 {
26
27 /**
28  * @brief Set annotation for circle node execution plan
29  * @note  Once CircleExecutionPlanAnnotation is annotated, it should not be changed.
30  *        If CircleExecutionPlanAnnotation is needed to be changed, create
31  *        new CircleExecutionPlanAnnotation.
32  */
33 class CircleExecutionPlanAnnotation final : public loco::NodeAnnotation
34 {
35 public:
36   CircleExecutionPlanAnnotation() = delete;
37
38   explicit CircleExecutionPlanAnnotation(luci::CircleNodeExecutionPlan execution_plan)
39     : _execution_plan{std::move(execution_plan)}
40   {
41     // Do nothing
42   }
43
44 public:
45   const luci::CircleNodeExecutionPlan &execution_plan(void) const { return _execution_plan; }
46   // No setter
47
48 private:
49   luci::CircleNodeExecutionPlan _execution_plan;
50 };
51
52 } // namespace
53
54 namespace luci
55 {
56
57 bool has_execution_plan(const luci::CircleNode *circle_node)
58 {
59   return circle_node->annot<CircleExecutionPlanAnnotation>() != nullptr;
60 }
61
62 void add_execution_plan(luci::CircleNode *circle_node,
63                         const luci::CircleNodeExecutionPlan &execution_plan)
64 {
65   circle_node->annot<CircleExecutionPlanAnnotation>(nullptr);
66   circle_node->annot(std::make_unique<CircleExecutionPlanAnnotation>(execution_plan));
67 }
68
69 luci::CircleNodeExecutionPlan get_execution_plan(const luci::CircleNode *circle_node)
70 {
71   if (!has_execution_plan(circle_node))
72     throw std::runtime_error("Cannot find CircleNodeExecutionPlanAnnotation");
73
74   return circle_node->annot<CircleExecutionPlanAnnotation>()->execution_plan();
75 }
76
77 } // namespace luci