Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / runtime / onert / core / include / backend / cpu_common / KernelGeneratorBase.h
1 /*
2  * Copyright (c) 2018 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_BACKEND_CPU_COMMON_KERNEL_GENERATOR_BASE_H__
18 #define __ONERT_BACKEND_CPU_COMMON_KERNEL_GENERATOR_BASE_H__
19
20 #include <assert.h>
21 #include <memory>
22 #include <functional>
23
24 #include "ir/OperationVisitor.h"
25 #include "ir/OpSequence.h"
26 #include <memory>
27 #include "exec/FunctionSequence.h"
28 #include "backend/ITensorRegistry.h"
29
30 namespace onert
31 {
32 namespace backend
33 {
34 namespace cpu_common
35 {
36
37 class KernelGeneratorBase : public ir::OperationVisitor
38 {
39 public:
40   virtual ~KernelGeneratorBase() = default;
41
42   std::unique_ptr<exec::IFunction> releaseFunction()
43   {
44     assert(_return_fn);
45     return std::move(_return_fn);
46   }
47
48   std::unique_ptr<exec::FunctionSequence> generate(const ir::OpSequence &op_seq)
49   {
50     op_seq.accept(*this);
51     return std::move(_return_fn_seq);
52   }
53
54 protected:
55   using OperationVisitor::visit;
56
57   void visit(const ir::OpSequence &) override
58   {
59     throw std::runtime_error("KernelGenerator: NYI for operation 'OpSequence'");
60   }
61
62 #define OP(InternalName)                                                                \
63   void visit(const ir::operation::InternalName &) override                              \
64   {                                                                                     \
65     throw std::runtime_error("KernelGenerator: NYI for operation '" #InternalName "'"); \
66   }
67 #include "ir/Operations.lst"
68 #undef OP
69
70 protected:
71   std::unique_ptr<exec::IFunction> _return_fn;
72   std::unique_ptr<exec::FunctionSequence> _return_fn_seq; // TODO Extract this out
73 };
74
75 } // namespace cpu_common
76 } // namespace backend
77 } // namespace onert
78
79 #endif // __ONERT_BACKEND_CPU_COMMON_KERNEL_GENERATOR_BASE_H__