tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 
135 class Source;
136 
137 class SourceNode : public Object {
138  public:
141 
144 
146  std::vector<std::pair<int, int>> line_map;
147 
148  // override attr visitor
150  v->Visit("source_name", &source_name);
151  v->Visit("source", &source);
152  }
153 
154  static constexpr const char* _type_key = "Source";
156 };
157 
158 class Source : public ObjectRef {
159  public:
160  TVM_DLL Source(SourceName src_name, std::string source);
161  TVM_DLL tvm::String GetLine(int line);
162 
164 };
165 
169 class SourceMap;
173 class SourceMapNode : public Object {
174  public:
177 
178  // override attr visitor
179  void VisitAttrs(AttrVisitor* v) { v->Visit("source_map", &source_map); }
180 
181  bool SEqualReduce(const SourceMapNode* other, SEqualReducer equal) const {
182  return equal(source_map, other->source_map);
183  }
184 
185  static constexpr const char* _type_key = "SourceMap";
187 };
188 
189 class SourceMap : public ObjectRef {
190  public:
191  explicit SourceMap(Map<SourceName, Source> source_map);
192 
193  explicit SourceMap(std::initializer_list<std::pair<SourceName, Source>> source_map)
194  : SourceMap(Map<SourceName, Source>(source_map)) {}
195 
197 
198  void Add(const Source& source);
199 
201  ICHECK(get() != nullptr);
202  return static_cast<SourceMapNode*>(get_mutable());
203  }
204 
206 };
207 
208 } // namespace tvm
209 
210 #endif // TVM_IR_SOURCE_MAP_H_
tvm::Span Span
Definition: base.h:65
Stores locations in frontend source that generated a node.
Definition: source_map.h:173
Definitions and helper macros for IR/AST nodes.
bool SEqualReduce(const SpanNode *other, SEqualReducer equal) const
Definition: source_map.h:110
The name of a source fragment.
Definition: source_map.h:46
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:124
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
static constexpr bool _type_has_method_sequal_reduce
Definition: source_map.h:53
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
tvm::SourceName SourceName
Definition: base.h:64
Definition: source_map.h:158
SourceName source_name
The source name.
Definition: source_map.h:90
TVM_DECLARE_FINAL_OBJECT_INFO(SourceNameNode, Object)
SourceMap(std::initializer_list< std::pair< SourceName, Source >> source_map)
Definition: source_map.h:193
base class of all object containers.
Definition: object.h:167
String source
The raw source.
Definition: source_map.h:143
int column
The column offset.
Definition: source_map.h:94
int line
The line number.
Definition: source_map.h:92
Definition: source_map.h:189
void VisitAttrs(AttrVisitor *v)
Definition: source_map.h:149
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
Definition: source_map.h:137
Definition: source_map.h:120
String name
The source name.
Definition: source_map.h:49
The source name of a file span.
Definition: source_map.h:67
void VisitAttrs(AttrVisitor *v)
Definition: source_map.h:101
int end_column
The end column number.
Definition: source_map.h:98
static constexpr const char * _type_key
Definition: source_map.h:59
Reference to string objects.
Definition: string.h:98
#define TVM_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:713
bool SEqualReduce(const SourceNameNode *other, SEqualReducer equal) const
Definition: source_map.h:55
int end_line
The end line number.
Definition: source_map.h:96
SourceMap()
Definition: source_map.h:196
bool SEqualReduce(const SourceMapNode *other, SEqualReducer equal) const
Definition: source_map.h:181
Base class of all object reference.
Definition: object.h:511
Stores locations in frontend source that generated a node.
Definition: source_map.h:87
A managed object in the TVM runtime.
Map< K, V > Merge(Map< K, V > lhs, const Map< K, V > &rhs)
Merge two Maps.
Definition: map.h:1471
void VisitAttrs(AttrVisitor *v)
Definition: source_map.h:51
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics, which means map is mutable but copy will happen when array is referenced in more than two places.
Definition: map.h:1271
SourceName source_name
The source name.
Definition: source_map.h:140
void VisitAttrs(AttrVisitor *v)
Definition: source_map.h:179
std::vector< std::pair< int, int > > line_map
A mapping of line breaks into the raw source.
Definition: source_map.h:146
#define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:728
SourceMapNode * operator->()
Definition: source_map.h:200
Type-erased function used across TVM API.
This file defines the TVM global function registry.
Map< SourceName, Source > source_map
The source mapping.
Definition: source_map.h:176