Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / compiler / TensorBuilders.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_COMPILER_TENSOR_BUILDERS_H__
18 #define __ONERT_COMPILER_TENSOR_BUILDERS_H__
19
20 #include <unordered_set>
21 #include <memory>
22 #include "backend/BackendContext.h"
23 #include "backend/Backend.h"
24 #include "backend/controlflow/Config.h"
25 #include "backend/controlflow/TensorBuilder.h"
26 #include "util/logging.h"
27
28 namespace onert
29 {
30 namespace compiler
31 {
32
33 class TensorBuilders
34 {
35 public:
36   TensorBuilders() = default;
37
38   TensorBuilders(const onert::backend::BackendContexts &backend_contexts, bool include_controlflow)
39   {
40     for (const auto &e : backend_contexts)
41     {
42       if (e.first->config()->id() == backend::controlflow::Config::ID)
43       {
44         _cf_tensor_builder = std::dynamic_pointer_cast<backend::controlflow::TensorBuilder>(
45             e.second->tensor_builder);
46         if (include_controlflow)
47           _tensor_builders.insert(e.second->tensor_builder);
48       }
49       else
50       {
51         _tensor_builders.insert(e.second->tensor_builder);
52       }
53     }
54   }
55
56   std::unordered_set<std::shared_ptr<onert::backend::ITensorBuilder>>::const_iterator begin() const
57   {
58     return _tensor_builders.cbegin();
59   }
60   std::unordered_set<std::shared_ptr<onert::backend::ITensorBuilder>>::const_iterator end() const
61   {
62     return _tensor_builders.cend();
63   }
64
65   std::shared_ptr<backend::controlflow::TensorBuilder> getControlflowTensorBuilder() const
66   {
67     return _cf_tensor_builder;
68   }
69
70   std::shared_ptr<backend::ITensor> getITensor(ir::OperandIndex ind)
71   {
72     for (auto &tensor_builder : _tensor_builders)
73     {
74       auto tensor = tensor_builder->tensorAt(ind);
75       if (tensor)
76         return tensor;
77     }
78     return nullptr;
79   }
80
81 private:
82   std::unordered_set<std::shared_ptr<backend::ITensorBuilder>> _tensor_builders;
83   std::shared_ptr<backend::controlflow::TensorBuilder> _cf_tensor_builder;
84 };
85
86 } // namespace compiler
87 } // namespace onert
88
89 #endif // __ONERT_COMPILER_TENSOR_BUILDERS_H__