Imported Upstream version 1.7.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
27 namespace onert
28 {
29 namespace compiler
30 {
31
32 class TensorBuilders
33 {
34 public:
35   TensorBuilders() = default;
36
37   TensorBuilders(const onert::backend::BackendContexts &backend_contexts, bool include_controlflow)
38   {
39     for (const auto &e : backend_contexts)
40     {
41       if (e.first->config()->id() == backend::controlflow::Config::ID)
42       {
43         _cf_tensor_builder = std::dynamic_pointer_cast<backend::controlflow::TensorBuilder>(
44             e.second->tensor_builder);
45         if (include_controlflow)
46           _tensor_builders.insert(e.second->tensor_builder);
47       }
48       else
49       {
50         _tensor_builders.insert(e.second->tensor_builder);
51       }
52     }
53   }
54
55   std::unordered_set<std::shared_ptr<onert::backend::ITensorBuilder>>::const_iterator begin() const
56   {
57     return _tensor_builders.cbegin();
58   }
59   std::unordered_set<std::shared_ptr<onert::backend::ITensorBuilder>>::const_iterator end() const
60   {
61     return _tensor_builders.cend();
62   }
63
64   std::shared_ptr<backend::controlflow::TensorBuilder> getControlflowTensorBuilder() const
65   {
66     return _cf_tensor_builder;
67   }
68
69 private:
70   std::unordered_set<std::shared_ptr<backend::ITensorBuilder>> _tensor_builders;
71   std::shared_ptr<backend::controlflow::TensorBuilder> _cf_tensor_builder;
72 };
73
74 } // namespace compiler
75 } // namespace onert
76
77 #endif // __ONERT_COMPILER_TENSOR_BUILDERS_H__