Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / runtime / onert / core / include / ir / LoweredGraph.h
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 #ifndef __ONERT_IR_LOWERED_GRAPH_H__
18 #define __ONERT_IR_LOWERED_GRAPH_H__
19
20 #include "ir/Graph.h"
21 #include "ir/LowerInfoMap.h"
22 #include "ir/OpSequences.h"
23 #include "compiler/BackendResolver.h"
24 #include "compiler/Compiler.h"
25
26 namespace onert
27 {
28 namespace ir
29 {
30
31 /**
32  * @brief Class that contains lowering information on graph.
33  *        In addition, after lowering, operands in graph will be set to "dynamic"
34  *        if the shape of output of an operation cannot be decided at compilation time.
35  */
36 class LoweredGraph
37 {
38 public:
39   LoweredGraph(const Graph &graph, const compiler::CompilerOptions &options);
40
41   Graph &graph() { return _graph; }
42   const Graph &graph() const { return _graph; }
43   const LowerInfoMap *getLowerInfo() const { return &_lower_info_map; }
44   const operation::LowerInfo *getLowerInfo(const OpSequenceIndex &op_seq_index) const;
45   void setLowerInfo(const OpSequenceIndex &op_seq_index,
46                     std::unique_ptr<operation::LowerInfo> &&lower_info);
47   void removeLowerInfo(const OpSequenceIndex &op_seq_index);
48   const operand::LowerInfo *getLowerInfo(const OperandIndex &index) const;
49   operand::LowerInfo *getLowerInfo(const OperandIndex &index);
50   void setLowerInfo(const OperandIndex &index, std::unique_ptr<operand::LowerInfo> &&lower_info);
51   void removeLowerInfo(const OperandIndex &index);
52   OpSequences &op_seqs() { return _op_seqs; }
53   const OpSequences &op_seqs() const { return _op_seqs; }
54   void iterateTopolOpSeqs(
55       const std::function<void(const OpSequenceIndex &, const OpSequence &)> &fn) const;
56   void iterateTopolOpSeqs(const std::function<void(const OpSequenceIndex &, OpSequence &)> &fn);
57   const backend::BackendContexts &backend_contexts() { return _backend_contexts; }
58   const backend::BackendContexts &backend_contexts() const { return _backend_contexts; }
59   std::shared_ptr<ir::OperationIndexMap<int64_t>> indexed_ranks() { return _indexed_ranks; }
60
61 private:
62   void makeOpSequences(OperandIndexMap<std::unique_ptr<operand::LowerInfo>> &operands_lower_info,
63                        const compiler::CompilerOptions &options,
64                        const compiler::BackendResolver &backend_resolver);
65
66   void
67   manipulateLowerInfo(OperandIndexMap<std::unique_ptr<operand::LowerInfo>> &operands_lower_info,
68                       bool is_primary);
69   void dumpLowerInfo();
70   bool mergeable(const OpSequenceIndex &op_seq_index, const OperationIndex &node_index,
71                  Layout layout, const compiler::BackendResolver &backend_resolver);
72   OpSequenceIndex appendFreshSingleOpSequence(const OperationIndex &node_index,
73                                               const Operation &node);
74
75 private:
76   Graph _graph;
77   backend::BackendContexts _backend_contexts;
78   std::shared_ptr<ir::OperationIndexMap<int64_t>> _indexed_ranks;
79   LowerInfoMap _lower_info_map;
80   // Pass(for Perm) can accept only graph so that Graph has OpSequences as a member
81   OpSequences _op_seqs;
82 };
83
84 } // namespace ir
85 } // namespace onert
86
87 #endif // __ONERT_IR_LOWERED_GRAPH_H__