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 span_check.cc |
21 | * \brief A utility for checking and reporting malformed span information. |
22 | */ |
23 | #include "./span_check.h" |
24 | |
25 | #include <tvm/relay/transform.h> |
26 | |
27 | namespace tvm { |
28 | namespace parser { |
29 | |
30 | using tvm::relay::transform::CreateFunctionPass; |
31 | using tvm::transform::PassContext; |
32 | |
33 | void SpanChecker::VisitExpr(const Expr& e) { |
34 | this->expression = e; |
35 | VisitSpan(e->span); |
36 | span_stack.push_back(e->span); |
37 | ExprVisitor::VisitExpr(e); |
38 | this->expression = e; |
39 | span_stack.pop_back(); |
40 | } |
41 | |
42 | // TODO(@jroesch, @junru): we need to deal with unique spans for global/var. |
43 | void SpanChecker::VisitExpr_(const VarNode* op) {} |
44 | void SpanChecker::VisitExpr_(const GlobalVarNode* op) {} |
45 | void SpanChecker::VisitExpr_(const ConstantNode* op) {} |
46 | |
47 | void SpanChecker::VisitExpr_(const TupleNode* op) { ExprVisitor::VisitExpr_(op); } |
48 | |
49 | void SpanChecker::VisitExpr_(const FunctionNode* op) { ExprVisitor::VisitExpr_(op); } |
50 | |
51 | void SpanChecker::VisitExpr_(const CallNode* op) { ExprVisitor::VisitExpr_(op); } |
52 | |
53 | void SpanChecker::VisitExpr_(const LetNode* op) { ExprVisitor::VisitExpr_(op); } |
54 | |
55 | void SpanChecker::VisitExpr_(const IfNode* op) { ExprVisitor::VisitExpr_(op); } |
56 | |
57 | void SpanChecker::VisitExpr_(const OpNode* op) {} |
58 | |
59 | void SpanChecker::VisitExpr_(const TupleGetItemNode* op) { ExprVisitor::VisitExpr_(op); } |
60 | |
61 | void SpanChecker::VisitExpr_(const RefCreateNode* op) { ExprVisitor::VisitExpr_(op); } |
62 | |
63 | void SpanChecker::VisitExpr_(const RefReadNode* op) { ExprVisitor::VisitExpr_(op); } |
64 | |
65 | void SpanChecker::VisitExpr_(const RefWriteNode* op) { ExprVisitor::VisitExpr_(op); } |
66 | |
67 | void SpanChecker::VisitExpr_(const ConstructorNode* op) {} // ExprVisitor::VisitExpr_(op); } |
68 | |
69 | void SpanChecker::VisitExpr_(const MatchNode* op) { ExprVisitor::VisitExpr_(op); } |
70 | |
71 | void SpanChecker::VisitSpan(const Span& sp) { |
72 | if (!sp.defined()) { |
73 | Span span; |
74 | for (auto spans = this->span_stack.rbegin(); spans != this->span_stack.rend(); spans++) { |
75 | span = this->span_stack.back(); |
76 | if (span.defined()) { |
77 | diag_ctx.Emit(Diagnostic::Warning(span) << "found null-span, i-nodes deep from this span." ); |
78 | return; |
79 | } |
80 | } |
81 | auto warning = Diagnostic::Warning(span); |
82 | warning << "\tAll spans are null\n" ; |
83 | warning << "\t" << this->expression; |
84 | diag_ctx.Emit(warning); |
85 | } |
86 | } |
87 | |
88 | void SpanChecker::VisitType(const Type& t) {} |
89 | void SpanChecker::VisitClause(const Clause& c) {} |
90 | void SpanChecker::VisitPattern(const Pattern& c) {} |
91 | |
92 | Pass SpanCheck() { |
93 | return CreateFunctionPass( |
94 | [](const Function& func, const IRModule& mod, const PassContext& ctx) { |
95 | ICHECK(ctx->diag_ctx) << "Diagnostic context must be set." ; |
96 | SpanChecker checker(ctx->diag_ctx.value()); |
97 | checker.VisitExpr(func); |
98 | ctx->diag_ctx.value().Render(); |
99 | return func; |
100 | }, |
101 | 0, "SpanCheck" , {}); |
102 | } |
103 | |
104 | TVM_REGISTER_GLOBAL("parser.SpanCheck" ).set_body_typed([]() { return SpanCheck(); }); |
105 | |
106 | } // namespace parser |
107 | } // namespace tvm |
108 | |