1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one |
3 | * or more contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. The ASF licenses this file |
6 | * to you under the Apache License, Version 2.0 (the |
7 | * "License"); you may not use this file except in compliance |
8 | * with the License. You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, |
13 | * software distributed under the License is distributed on an |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | * KIND, either express or implied. See the License for the |
16 | * specific language governing permissions and limitations |
17 | * under the License. |
18 | */ |
19 | /*! |
20 | * \file tvm/node/repr_printer.h |
21 | * \brief Printer class to print repr string of each AST/IR nodes. |
22 | */ |
23 | #ifndef TVM_NODE_REPR_PRINTER_H_ |
24 | #define TVM_NODE_REPR_PRINTER_H_ |
25 | |
26 | #include <tvm/node/functor.h> |
27 | #include <tvm/node/script_printer.h> |
28 | |
29 | #include <iostream> |
30 | #include <string> |
31 | |
32 | namespace tvm { |
33 | /*! \brief A printer class to print the AST/IR nodes. */ |
34 | class ReprPrinter { |
35 | public: |
36 | /*! \brief The output stream */ |
37 | std::ostream& stream; |
38 | /*! \brief The indentation level. */ |
39 | int indent{0}; |
40 | |
41 | explicit ReprPrinter(std::ostream& stream) // NOLINT(*) |
42 | : stream(stream) {} |
43 | |
44 | /*! \brief The node to be printed. */ |
45 | TVM_DLL void Print(const ObjectRef& node); |
46 | /*! \brief Print indent to the stream */ |
47 | TVM_DLL void PrintIndent(); |
48 | // Allow registration to be printer. |
49 | using FType = NodeFunctor<void(const ObjectRef&, ReprPrinter*)>; |
50 | TVM_DLL static FType& vtable(); |
51 | }; |
52 | |
53 | /*! \brief Legacy behavior of ReprPrinter. */ |
54 | class ReprLegacyPrinter { |
55 | public: |
56 | /*! \brief The indentation level. */ |
57 | int indent{0}; |
58 | |
59 | explicit ReprLegacyPrinter(std::ostream& stream) // NOLINT(*) |
60 | : stream(stream) {} |
61 | |
62 | /*! \brief The node to be printed. */ |
63 | TVM_DLL void Print(const ObjectRef& node); |
64 | /*! \brief Print indent to the stream */ |
65 | TVM_DLL void PrintIndent(); |
66 | /*! \brief Could the LegacyPrinter dispatch the node */ |
67 | TVM_DLL static bool CanDispatch(const ObjectRef& node); |
68 | /*! \brief Return the ostream it maintains */ |
69 | TVM_DLL std::ostream& Stream() const; |
70 | // Allow registration to be printer. |
71 | using FType = NodeFunctor<void(const ObjectRef&, ReprLegacyPrinter*)>; |
72 | TVM_DLL static FType& vtable(); |
73 | |
74 | private: |
75 | /*! \brief The output stream */ |
76 | std::ostream& stream; |
77 | }; |
78 | |
79 | /*! |
80 | * \brief Dump the node to stderr, used for debug purposes. |
81 | * \param node The input node |
82 | */ |
83 | TVM_DLL void Dump(const runtime::ObjectRef& node); |
84 | |
85 | /*! |
86 | * \brief Dump the node to stderr, used for debug purposes. |
87 | * \param node The input node |
88 | */ |
89 | TVM_DLL void Dump(const runtime::Object* node); |
90 | |
91 | } // namespace tvm |
92 | |
93 | namespace tvm { |
94 | namespace runtime { |
95 | // default print function for all objects |
96 | // provide in the runtime namespace as this is where objectref originally comes from. |
97 | inline std::ostream& operator<<(std::ostream& os, const ObjectRef& n) { // NOLINT(*) |
98 | ReprPrinter(os).Print(n); |
99 | return os; |
100 | } |
101 | |
102 | inline std::string AsLegacyRepr(const ObjectRef& n) { |
103 | std::ostringstream os; |
104 | ReprLegacyPrinter(os).Print(n); |
105 | return os.str(); |
106 | } |
107 | } // namespace runtime |
108 | using runtime::AsLegacyRepr; |
109 | } // namespace tvm |
110 | #endif // TVM_NODE_REPR_PRINTER_H_ |
111 | |