Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / runtime / onert / core / include / compiler / 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 compiler
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 ir::Graph &graph, const compiler::CompilerOptions &options);
40
41   ir::Graph &graph() { return _graph; }
42   const ir::Graph &graph() const { return _graph; }
43   const ir::LowerInfoMap *getLowerInfo() const { return &_lower_info_map; }
44   const ir::operation::LowerInfo *getLowerInfo(const ir::OpSequenceIndex &op_seq_index) const;
45   void setLowerInfo(const ir::OpSequenceIndex &op_seq_index,
46                     std::unique_ptr<ir::operation::LowerInfo> &&lower_info);
47   void removeLowerInfo(const ir::OpSequenceIndex &op_seq_index);
48   const ir::operand::LowerInfo *getLowerInfo(const ir::OperandIndex &index) const;
49   ir::operand::LowerInfo *getLowerInfo(const ir::OperandIndex &index);
50   void setLowerInfo(const ir::OperandIndex &index,
51                     std::unique_ptr<ir::operand::LowerInfo> &&lower_info);
52   void removeLowerInfo(const ir::OperandIndex &index);
53   ir::OpSequences &op_seqs() { return _op_seqs; }
54   const ir::OpSequences &op_seqs() const { return _op_seqs; }
55   void iterateTopolOpSeqs(
56       const std::function<void(const ir::OpSequenceIndex &, const ir::OpSequence &)> &fn) const;
57   void
58   iterateTopolOpSeqs(const std::function<void(const ir::OpSequenceIndex &, ir::OpSequence &)> &fn);
59   const backend::BackendContexts &backend_contexts() { return _backend_contexts; }
60   const backend::BackendContexts &backend_contexts() const { return _backend_contexts; }
61   std::shared_ptr<ir::OperationIndexMap<int64_t>> indexed_ranks() { return _indexed_ranks; }
62
63 private:
64   void
65   makeOpSequences(ir::OperandIndexMap<std::unique_ptr<ir::operand::LowerInfo>> &operands_lower_info,
66                   const compiler::CompilerOptions &options,
67                   const compiler::BackendResolver &backend_resolver);
68
69   void manipulateLowerInfo(
70       ir::OperandIndexMap<std::unique_ptr<ir::operand::LowerInfo>> &operands_lower_info);
71   void dumpLowerInfo();
72   bool mergeable(const ir::OpSequenceIndex &op_seq_index, const ir::OperationIndex &node_index,
73                  ir::Layout layout, const compiler::BackendResolver &backend_resolver);
74   ir::OpSequenceIndex appendFreshSingleOpSequence(const ir::OperationIndex &node_index,
75                                                   const ir::Operation &node);
76
77 private:
78   ir::Graph _graph;
79   backend::BackendContexts _backend_contexts;
80   std::shared_ptr<ir::OperationIndexMap<int64_t>> _indexed_ranks;
81   ir::LowerInfoMap _lower_info_map;
82   // Pass(for Perm) can accept only graph so that Graph has OpSequences as a member
83   ir::OpSequences _op_seqs;
84 };
85
86 } // namespace compiler
87 } // namespace onert
88
89 #endif // __ONERT_IR_LOWERED_GRAPH_H__