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/ffi/function.h>
27 #include <tvm/ffi/reflection/registry.h>
28 #include <tvm/node/node.h>
29 #include <tvm/runtime/object.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:
49  String name;
50 
51  static void RegisterReflection() {
52  namespace refl = tvm::ffi::reflection;
53  refl::ObjectDef<SourceNameNode>().def_ro("name", &SourceNameNode::name);
54  }
55 
56  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
57  static constexpr const char* _type_key = "ir.SourceName";
59 };
60 
65 class SourceName : public ObjectRef {
66  public:
73  TVM_DLL static SourceName Get(const String& name);
74 
76 };
77 
81 class Span;
85 class SpanNode : public Object {
86  public:
90  int line;
92  int column;
94  int end_line;
97 
98  static void RegisterReflection() {
99  namespace refl = tvm::ffi::reflection;
100  refl::ObjectDef<SpanNode>()
101  .def_ro("source_name", &SpanNode::source_name)
102  .def_ro("line", &SpanNode::line)
103  .def_ro("column", &SpanNode::column)
104  .def_ro("end_line", &SpanNode::end_line)
105  .def_ro("end_column", &SpanNode::end_column);
106  }
107 
108  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
109  static constexpr const char* _type_key = "ir.Span";
111 };
112 
113 class Span : public ObjectRef {
114  public:
115  TVM_DLL Span(SourceName source_name, int line, int end_line, int column, int end_column);
116 
118  TVM_DLL Span Merge(const Span& other) const;
119 
121 };
122 
126 class SequentialSpanNode : public SpanNode {
127  public:
129  Array<Span> spans;
130 
131  static void RegisterReflection() {
132  namespace refl = tvm::ffi::reflection;
133  refl::ObjectDef<SequentialSpanNode>().def_ro("spans", &SequentialSpanNode::spans);
134  }
135 
136  static constexpr const char* _type_key = "ir.SequentialSpan";
138 };
139 
144 class SequentialSpan : public Span {
145  public:
146  TVM_DLL SequentialSpan(Array<Span> spans);
147 
148  TVM_DLL SequentialSpan(std::initializer_list<Span> init);
149 
151 };
152 
158 class Source;
159 
160 class SourceNode : public Object {
161  public:
164 
166  String source;
167 
169  std::vector<std::pair<int, int>> line_map;
170 
171  static void RegisterReflection() {
172  namespace refl = tvm::ffi::reflection;
173  refl::ObjectDef<SourceNode>()
174  .def_ro("source_name", &SourceNode::source_name)
175  .def_ro("source", &SourceNode::source);
176  }
177 
178  static constexpr const char* _type_key = "ir.Source";
180 };
181 
182 class Source : public ObjectRef {
183  public:
184  TVM_DLL Source(SourceName src_name, std::string source);
185  TVM_DLL tvm::String GetLine(int line);
186 
188 };
189 
193 class SourceMap;
197 class SourceMapObj : public Object {
198  public:
200  Map<SourceName, Source> source_map;
201 
202  static void RegisterReflection() {
203  namespace refl = tvm::ffi::reflection;
204  refl::ObjectDef<SourceMapObj>().def_ro("source_map", &SourceMapObj::source_map);
205  }
206 
207  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
208  static constexpr const char* _type_key = "ir.SourceMap";
210 };
211 
212 class SourceMap : public ObjectRef {
213  public:
214  explicit SourceMap(Map<SourceName, Source> source_map);
215 
216  explicit SourceMap(std::initializer_list<std::pair<SourceName, Source>> source_map)
217  : SourceMap(Map<SourceName, Source>(source_map)) {}
218 
220 
221  void Add(const Source& source);
222 
224  ICHECK(get() != nullptr);
225  return static_cast<SourceMapObj*>(get_mutable());
226  }
227 
229 };
230 
231 } // namespace tvm
232 
233 #endif // TVM_IR_SOURCE_MAP_H_
Store a list of spans for an expr generated from mulitple source exprs.
Definition: source_map.h:126
Array< Span > spans
The original source list of spans to construct a sequential span.
Definition: source_map.h:129
static constexpr const char * _type_key
Definition: source_map.h:136
static void RegisterReflection()
Definition: source_map.h:131
TVM_DECLARE_FINAL_OBJECT_INFO(SequentialSpanNode, SpanNode)
Reference class of SequentialSpanNode.
Definition: source_map.h:144
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:197
TVM_DECLARE_FINAL_OBJECT_INFO(SourceMapObj, Object)
static constexpr const char * _type_key
Definition: source_map.h:208
Map< SourceName, Source > source_map
The source mapping.
Definition: source_map.h:200
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: source_map.h:207
static void RegisterReflection()
Definition: source_map.h:202
Definition: source_map.h:212
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(SourceMap, ObjectRef, SourceMapObj)
void Add(const Source &source)
SourceMap()
Definition: source_map.h:219
SourceMapObj * operator->()
Definition: source_map.h:223
SourceMap(Map< SourceName, Source > source_map)
SourceMap(std::initializer_list< std::pair< SourceName, Source >> source_map)
Definition: source_map.h:216
The name of a source fragment.
Definition: source_map.h:46
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: source_map.h:56
static void RegisterReflection()
Definition: source_map.h:51
static constexpr const char * _type_key
Definition: source_map.h:57
TVM_DECLARE_FINAL_OBJECT_INFO(SourceNameNode, Object)
String name
The source name.
Definition: source_map.h:49
The source name of a file span.
Definition: source_map.h:65
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:160
std::vector< std::pair< int, int > > line_map
A mapping of line breaks into the raw source.
Definition: source_map.h:169
TVM_DECLARE_FINAL_OBJECT_INFO(SourceNode, Object)
String source
The raw source.
Definition: source_map.h:166
static constexpr const char * _type_key
Definition: source_map.h:178
static void RegisterReflection()
Definition: source_map.h:171
SourceName source_name
The source name.
Definition: source_map.h:163
Definition: source_map.h:182
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:85
TVM_DECLARE_BASE_OBJECT_INFO(SpanNode, Object)
static constexpr const char * _type_key
Definition: source_map.h:109
int column
The column offset.
Definition: source_map.h:92
int line
The line number.
Definition: source_map.h:90
int end_line
The end line number.
Definition: source_map.h:94
static void RegisterReflection()
Definition: source_map.h:98
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: source_map.h:108
SourceName source_name
The source name.
Definition: source_map.h:88
int end_column
The end column number.
Definition: source_map.h:96
Definition: source_map.h:113
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.
Definition: repr_printer.h:91
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
Definitions and helper macros for IR/AST nodes.
A managed object in the TVM runtime.