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  ffi::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;
58 };
59 
64 class SourceName : public ObjectRef {
65  public:
72  TVM_DLL static SourceName Get(const ffi::String& name);
73 
75 };
76 
80 class Span;
84 class SpanNode : public Object {
85  public:
89  int line;
91  int column;
93  int end_line;
96 
97  static void RegisterReflection() {
98  namespace refl = tvm::ffi::reflection;
99  refl::ObjectDef<SpanNode>()
100  .def_ro("source_name", &SpanNode::source_name)
101  .def_ro("line", &SpanNode::line)
102  .def_ro("column", &SpanNode::column)
103  .def_ro("end_line", &SpanNode::end_line)
104  .def_ro("end_column", &SpanNode::end_column);
105  }
106 
107  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
109 };
110 
111 class Span : public ObjectRef {
112  public:
113  TVM_DLL Span(SourceName source_name, int line, int end_line, int column, int end_column);
114 
116  TVM_DLL Span Merge(const Span& other) const;
117 
119 };
120 
124 class SequentialSpanNode : public SpanNode {
125  public:
127  ffi::Array<Span> spans;
128 
129  static void RegisterReflection() {
130  namespace refl = tvm::ffi::reflection;
131  refl::ObjectDef<SequentialSpanNode>().def_ro("spans", &SequentialSpanNode::spans);
132  }
134 };
135 
140 class SequentialSpan : public Span {
141  public:
142  TVM_DLL SequentialSpan(ffi::Array<Span> spans);
143 
144  TVM_DLL SequentialSpan(std::initializer_list<Span> init);
145 
147 };
148 
154 class Source;
155 
156 class SourceNode : public Object {
157  public:
160 
162  ffi::String source;
163 
165  std::vector<std::pair<int, int>> line_map;
166 
167  static void RegisterReflection() {
168  namespace refl = tvm::ffi::reflection;
169  refl::ObjectDef<SourceNode>()
170  .def_ro("source_name", &SourceNode::source_name)
171  .def_ro("source", &SourceNode::source);
172  }
174 };
175 
176 class Source : public ObjectRef {
177  public:
178  TVM_DLL Source(SourceName src_name, std::string source);
179  TVM_DLL tvm::ffi::String GetLine(int line);
180 
182 };
183 
187 class SourceMap;
191 class SourceMapObj : public Object {
192  public:
194  ffi::Map<SourceName, Source> source_map;
195 
196  static void RegisterReflection() {
197  namespace refl = tvm::ffi::reflection;
198  refl::ObjectDef<SourceMapObj>().def_ro("source_map", &SourceMapObj::source_map);
199  }
200 
201  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
203 };
204 
205 class SourceMap : public ObjectRef {
206  public:
207  explicit SourceMap(ffi::Map<SourceName, Source> source_map);
208 
209  explicit SourceMap(std::initializer_list<std::pair<SourceName, Source>> source_map)
210  : SourceMap(ffi::Map<SourceName, Source>(source_map)) {}
211 
212  SourceMap() : SourceMap(ffi::Map<SourceName, Source>()) {}
213 
214  void Add(const Source& source);
215 
217  ICHECK(get() != nullptr);
218  return static_cast<SourceMapObj*>(get_mutable());
219  }
220 
222 };
223 
224 } // namespace tvm
225 
226 #endif // TVM_IR_SOURCE_MAP_H_
Store a list of spans for an expr generated from mulitple source exprs.
Definition: source_map.h:124
ffi::Array< Span > spans
The original source list of spans to construct a sequential span.
Definition: source_map.h:127
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.SequentialSpan", SequentialSpanNode, SpanNode)
static void RegisterReflection()
Definition: source_map.h:129
Reference class of SequentialSpanNode.
Definition: source_map.h:140
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(SequentialSpan, Span, SequentialSpanNode)
SequentialSpan(ffi::Array< Span > spans)
SequentialSpan(std::initializer_list< Span > init)
Stores locations in frontend source that generated a node.
Definition: source_map.h:191
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.SourceMap", SourceMapObj, Object)
ffi::Map< SourceName, Source > source_map
The source mapping.
Definition: source_map.h:194
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: source_map.h:201
static void RegisterReflection()
Definition: source_map.h:196
Definition: source_map.h:205
SourceMap(ffi::Map< SourceName, Source > source_map)
void Add(const Source &source)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(SourceMap, ObjectRef, SourceMapObj)
SourceMap()
Definition: source_map.h:212
SourceMapObj * operator->()
Definition: source_map.h:216
SourceMap(std::initializer_list< std::pair< SourceName, Source >> source_map)
Definition: source_map.h:209
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
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.SourceName", SourceNameNode, Object)
ffi::String name
The source name.
Definition: source_map.h:49
The source name of a file span.
Definition: source_map.h:64
static SourceName Get(const ffi::String &name)
Get an SourceName for a given operator name. Will raise an error if the source name has not been regi...
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(SourceName, ObjectRef, SourceNameNode)
Definition: source_map.h:156
ffi::String source
The raw source.
Definition: source_map.h:162
std::vector< std::pair< int, int > > line_map
A mapping of line breaks into the raw source.
Definition: source_map.h:165
static void RegisterReflection()
Definition: source_map.h:167
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.Source", SourceNode, Object)
SourceName source_name
The source name.
Definition: source_map.h:159
Definition: source_map.h:176
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Source, ObjectRef, SourceNode)
tvm::ffi::String GetLine(int line)
Source(SourceName src_name, std::string source)
Stores locations in frontend source that generated a node.
Definition: source_map.h:84
int column
The column offset.
Definition: source_map.h:91
int line
The line number.
Definition: source_map.h:89
int end_line
The end line number.
Definition: source_map.h:93
static void RegisterReflection()
Definition: source_map.h:97
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: source_map.h:107
TVM_FFI_DECLARE_OBJECT_INFO("ir.Span", SpanNode, Object)
SourceName source_name
The source name.
Definition: source_map.h:87
int end_column
The end column number.
Definition: source_map.h:95
Definition: source_map.h:111
Span(SourceName source_name, int line, int end_line, int column, int end_column)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Span, ObjectRef, SpanNode)
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.