| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #ifndef PNNX_PASS_LEVEL2_H |
| 16 | #define PNNX_PASS_LEVEL2_H |
| 17 | |
| 18 | #include "ir.h" |
| 19 | |
| 20 | namespace pnnx { |
| 21 | |
| 22 | class GraphRewriterPass |
| 23 | { |
| 24 | public: |
| 25 | virtual ~GraphRewriterPass(); |
| 26 | |
| 27 | virtual const char* match_pattern_graph() const = 0; |
| 28 | |
| 29 | virtual const char* replace_pattern_graph() const; |
| 30 | |
| 31 | virtual const char* type_str() const; |
| 32 | |
| 33 | virtual const char* name_str() const; |
| 34 | |
| 35 | virtual bool match(const std::map<std::string, Parameter>& captured_params) const; |
| 36 | |
| 37 | virtual bool match(const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const; |
| 38 | |
| 39 | virtual bool match(const std::map<std::string, const Operator*>& matched_operators, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const; |
| 40 | |
| 41 | virtual void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const; |
| 42 | |
| 43 | virtual void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const; |
| 44 | |
| 45 | virtual void write(const std::map<std::string, Operator*>& ops, const std::map<std::string, Parameter>& captured_params) const; |
| 46 | |
| 47 | virtual void write(const std::map<std::string, Operator*>& ops, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const; |
| 48 | }; |
| 49 | |
| 50 | class GraphRewriterPassRegister |
| 51 | { |
| 52 | public: |
| 53 | GraphRewriterPassRegister(const GraphRewriterPass* pass, int priority); |
| 54 | ~GraphRewriterPassRegister(); |
| 55 | const GraphRewriterPass* pass; |
| 56 | }; |
| 57 | |
| 58 | #define REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(CLASS, PRIORITY) \ |
| 59 | static GraphRewriterPassRegister g_global_pnnx_graphrewriterpass_##CLASS##_register(new CLASS, PRIORITY); |
| 60 | |
| 61 | void pnnx_graph_rewrite(Graph& graph, const GraphRewriterPass* pass, int& opindex); |
| 62 | |
| 63 | void pass_level2(Graph& g); |
| 64 | |
| 65 | } // namespace pnnx |
| 66 | |
| 67 | #endif // PNNX_PASS_LEVEL2_H |