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 | /*! |
21 | * \file text_printer.h |
22 | * \brief Printer to print out the unified IR text format |
23 | * that can be parsed by a parser. |
24 | */ |
25 | |
26 | #ifndef TVM_RELAY_PRINTER_TIR_TEXT_PRINTER_DEBUG_H_ |
27 | #define TVM_RELAY_PRINTER_TIR_TEXT_PRINTER_DEBUG_H_ |
28 | |
29 | #include <tuple> |
30 | #include <vector> |
31 | |
32 | #include "text_printer.h" |
33 | |
34 | namespace tvm { |
35 | namespace relay { |
36 | |
37 | class TIRTextPrinterDebug : public TIRTextPrinter { |
38 | public: |
39 | explicit TIRTextPrinterDebug(bool show_spans) |
40 | : TIRTextPrinter(false, &meta_), current_line_(1), show_spans_(show_spans) {} |
41 | |
42 | std::vector<std::tuple<const PrimExprNode*, size_t>> GetExprsByLine() const { |
43 | return exprs_by_line_; |
44 | } |
45 | |
46 | std::vector<std::tuple<const StmtNode*, size_t>> GetStmtsByLine() const { return stmts_by_line_; } |
47 | |
48 | private: |
49 | Doc NewLine() override; |
50 | |
51 | Doc VisitStmt(const tvm::tir::Stmt& n) override; |
52 | Doc VisitExpr(const PrimExpr& e) override; |
53 | |
54 | TextMetaDataContext meta_; |
55 | |
56 | // Line that the printer is currently printing |
57 | size_t current_line_; |
58 | |
59 | // Whether to include spans relevant to each line before a newline or not |
60 | bool show_spans_; |
61 | |
62 | // Record of all stmts and exprs and their corresponding line |
63 | std::vector<std::tuple<const StmtNode*, size_t>> stmts_by_line_; |
64 | std::vector<std::tuple<const PrimExprNode*, size_t>> exprs_by_line_; |
65 | }; |
66 | |
67 | } // namespace relay |
68 | } // namespace tvm |
69 | |
70 | #endif // TVM_RELAY_PRINTER_TIR_TEXT_PRINTER_DEBUG_H_ |
71 | |