tvm
source_map.h
Go to the documentation of this file.
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  */
23 #ifndef TVM_IR_SOURCE_MAP_H_
24 #define TVM_IR_SOURCE_MAP_H_
25 
26 #include <tvm/node/node.h>
27 #include <tvm/runtime/object.h>
29 #include <tvm/runtime/registry.h>
30 
31 #include <fstream>
32 #include <string>
33 #include <utility>
34 #include <vector>
35 
36 namespace tvm {
37 
42 class SourceName;
46 class SourceNameNode : public Object {
47  public:
50  // override attr visitor
51  void VisitAttrs(AttrVisitor* v) { v->Visit("name", &name); }
52 
53  static constexpr bool _type_has_method_sequal_reduce = true;
54 
55  bool SEqualReduce(const SourceNameNode* other, SEqualReducer equal) const {
56  return equal(name, other->name);
57  }
58 
59  static constexpr const char* _type_key = "SourceName";
61 };
62 
67 class SourceName : public ObjectRef {
68  public:
75  TVM_DLL static SourceName Get(const String& name);
76 
78 };
79 
83 class Span;
87 class SpanNode : public Object {
88  public:
92  int line;
94  int column;
96  int end_line;
99 
100  // override attr visitor
102  v->Visit("source_name", &source_name);
103  v->Visit("line", &line);
104  v->Visit("column", &column);
105  v->Visit("end_line", &end_line);
106  v->Visit("end_column", &end_column);
107  }
108  static constexpr bool _type_has_method_sequal_reduce = true;
109 
110  bool SEqualReduce(const SpanNode* other, SEqualReducer equal) const {
111  return equal(source_name, other->source_name) && equal(line, other->line) &&
112  equal(column, other->column) && equal(end_line, other->end_line) &&
113  equal(end_column, other->end_column);
114  }
115 
116  static constexpr const char* _type_key = "Span";
118 };
119 
120 class Span : public ObjectRef {
121  public:
122  TVM_DLL Span(SourceName source_name, int line, int end_line, int column, int end_column);
123 
125  TVM_DLL Span Merge(const Span& other) const;
126 
128 };
129 
133 class SequentialSpanNode : public SpanNode {
134  public:
137 
138  // override attr visitor
141  v->Visit("spans", &spans);
142  }
143 
144  static constexpr const char* _type_key = "SequentialSpan";
146 
148  if (spans.size() != other->spans.size()) {
149  return false;
150  }
151 
152  for (size_t i = 0, e = spans.size(); i != e; ++i) {
153  if (!StructuralEqual()(spans[i], other->spans[i])) {
154  return false;
155  }
156  }
157  return true;
158  }
159 };
160 
165 class SequentialSpan : public Span {
166  public:
167  TVM_DLL SequentialSpan(Array<Span> spans);
168 
169  TVM_DLL SequentialSpan(std::initializer_list<Span> init);
170 
172 };
173 
179 class Source;
180 
181 class SourceNode : public Object {
182  public:
185 
188 
190  std::vector<std::pair<int, int>> line_map;
191 
192  // override attr visitor
194  v->Visit("source_name", &source_name);
195  v->Visit("source", &source);
196  }
197 
198  static constexpr const char* _type_key = "Source";
200 };
201 
202 class Source : public ObjectRef {
203  public:
204  TVM_DLL Source(SourceName src_name, std::string source);
205  TVM_DLL tvm::String GetLine(int line);
206 
208 };
209 
213 class SourceMap;
217 class SourceMapNode : public Object {
218  public:
221 
222  // override attr visitor
223  void VisitAttrs(AttrVisitor* v) { v->Visit("source_map", &source_map); }
224 
225  bool SEqualReduce(const SourceMapNode* other, SEqualReducer equal) const {
226  return equal(source_map, other->source_map);
227  }
228 
229  static constexpr const char* _type_key = "SourceMap";
231 };
232 
233 class SourceMap : public ObjectRef {
234  public:
235  explicit SourceMap(Map<SourceName, Source> source_map);
236 
237  explicit SourceMap(std::initializer_list<std::pair<SourceName, Source>> source_map)
238  : SourceMap(Map<SourceName, Source>(source_map)) {}
239 
241 
242  void Add(const Source& source);
243 
245  ICHECK(get() != nullptr);
246  return static_cast<SourceMapNode*>(get_mutable());
247  }
248 
250 };
251 
252 } // namespace tvm
253 
254 #endif // TVM_IR_SOURCE_MAP_H_
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:126
Store a list of spans for an expr generated from mulitple source exprs.
Definition: source_map.h:133
void VisitAttrs(AttrVisitor *v)
Definition: source_map.h:139
bool SEqualReduce(const SequentialSpanNode *other, SEqualReducer equal) const
Definition: source_map.h:147
Array< Span > spans
The original source list of spans to construct a sequential span.
Definition: source_map.h:136
static constexpr const char * _type_key
Definition: source_map.h:144
TVM_DECLARE_FINAL_OBJECT_INFO(SequentialSpanNode, SpanNode)
Reference class of SequentialSpanNode.
Definition: source_map.h:165
SequentialSpan(Array< Span > spans)
TVM_DEFINE_OBJECT_REF_METHODS(SequentialSpan, Span, SequentialSpanNode)
SequentialSpan(std::initializer_list< Span > init)
Stores locations in frontend source that generated a node.
Definition: source_map.h:217
TVM_DECLARE_FINAL_OBJECT_INFO(SourceMapNode, Object)
bool SEqualReduce(const SourceMapNode *other, SEqualReducer equal) const
Definition: source_map.h:225
static constexpr const char * _type_key
Definition: source_map.h:229
Map< SourceName, Source > source_map
The source mapping.
Definition: source_map.h:220
void VisitAttrs(AttrVisitor *v)
Definition: source_map.h:223
Definition: source_map.h:233
void Add(const Source &source)
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(SourceMap, ObjectRef, SourceMapNode)
SourceMap()
Definition: source_map.h:240
SourceMap(Map< SourceName, Source > source_map)
SourceMapNode * operator->()
Definition: source_map.h:244
SourceMap(std::initializer_list< std::pair< SourceName, Source >> source_map)
Definition: source_map.h:237
The name of a source fragment.
Definition: source_map.h:46
static constexpr bool _type_has_method_sequal_reduce
Definition: source_map.h:53
static constexpr const char * _type_key
Definition: source_map.h:59
TVM_DECLARE_FINAL_OBJECT_INFO(SourceNameNode, Object)
void VisitAttrs(AttrVisitor *v)
Definition: source_map.h:51
String name
The source name.
Definition: source_map.h:49
bool SEqualReduce(const SourceNameNode *other, SEqualReducer equal) const
Definition: source_map.h:55
The source name of a file span.
Definition: source_map.h:67
static SourceName Get(const String &name)
Get an SourceName for a given operator name. Will raise an error if the source name has not been regi...
TVM_DEFINE_OBJECT_REF_METHODS(SourceName, ObjectRef, SourceNameNode)
Definition: source_map.h:181
std::vector< std::pair< int, int > > line_map
A mapping of line breaks into the raw source.
Definition: source_map.h:190
void VisitAttrs(AttrVisitor *v)
Definition: source_map.h:193
TVM_DECLARE_FINAL_OBJECT_INFO(SourceNode, Object)
String source
The raw source.
Definition: source_map.h:187
static constexpr const char * _type_key
Definition: source_map.h:198
SourceName source_name
The source name.
Definition: source_map.h:184
Definition: source_map.h:202
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(Source, ObjectRef, SourceNode)
Source(SourceName src_name, std::string source)
tvm::String GetLine(int line)
Stores locations in frontend source that generated a node.
Definition: source_map.h:87
static constexpr bool _type_has_method_sequal_reduce
Definition: source_map.h:108
TVM_DECLARE_BASE_OBJECT_INFO(SpanNode, Object)
static constexpr const char * _type_key
Definition: source_map.h:116
int column
The column offset.
Definition: source_map.h:94
int line
The line number.
Definition: source_map.h:92
int end_line
The end line number.
Definition: source_map.h:96
SourceName source_name
The source name.
Definition: source_map.h:90
void VisitAttrs(AttrVisitor *v)
Definition: source_map.h:101
int end_column
The end column number.
Definition: source_map.h:98
bool SEqualReduce(const SpanNode *other, SEqualReducer equal) const
Definition: source_map.h:110
Definition: source_map.h:120
TVM_DEFINE_OBJECT_REF_METHODS(Span, ObjectRef, SpanNode)
Span(SourceName source_name, int line, int end_line, int column, int end_column)
Span Merge(const Span &other) const
Merge two spans into one which captures the combined regions.
Content-aware structural equality comparator for objects.
Definition: structural_equal.h:103
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics,...
Definition: map.h:1271
Base class of all object reference.
Definition: object.h:519
const Object * get() const
Definition: object.h:554
Object * get_mutable() const
Definition: object.h:607
base class of all object containers.
Definition: object.h:171
Reference to string objects.
Definition: string.h:98
tvm::SourceName SourceName
Definition: base.h:64
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
Definitions and helper macros for IR/AST nodes.
A managed object in the TVM runtime.
Type-erased function used across TVM API.
This file defines the TVM global function registry.