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#ifndef TVM_SCRIPT_PRINTER_IR_UTILS_H_
20#define TVM_SCRIPT_PRINTER_IR_UTILS_H_
21
22#include <tvm/ir/expr.h>
23#include <tvm/ir/function.h>
24#include <tvm/ir/op.h>
25#include <tvm/script/printer/ir_docsifier.h>
26#include <tvm/support/with.h>
27
28#include <string>
29#include <utility>
30
31#include "../utils.h"
32
33namespace tvm {
34namespace script {
35namespace printer {
36
37class IRFrameNode : public FrameNode {
38 public:
39 void VisitAttrs(AttrVisitor* v) { FrameNode::VisitAttrs(v); }
40
41 static constexpr const char* _type_key = "script.printer.IRFrame";
42 TVM_DECLARE_FINAL_OBJECT_INFO(IRFrameNode, FrameNode);
43};
44
45class IRFrame : public Frame {
46 public:
47 explicit IRFrame(const IRDocsifier& d) {
48 ObjectPtr<IRFrameNode> n = make_object<IRFrameNode>();
49 n->stmts.clear();
50 n->d = d.get();
51 data_ = std::move(n);
52 }
53
54 TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(IRFrame, Frame, IRFrameNode);
55};
56
57/*! \brief Redirected method for the ReprPrinter */
58inline std::string ReprPrintIR(const ObjectRef& obj, const PrinterConfig& cfg) {
59 IRDocsifier d(cfg);
60 With<IRFrame> f(d);
61 (*f)->AddDispatchToken(d, "ir");
62 return Docsify(obj, d, *f, cfg);
63}
64
65} // namespace printer
66} // namespace script
67} // namespace tvm
68
69#endif // TVM_SCRIPT_PRINTER_IR_UTILS_H_
70