tvm
Loading...
Searching...
No Matches
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/container/array.h>
27#include <tvm/ffi/function.h>
28#include <tvm/ffi/reflection/registry.h>
29#include <tvm/runtime/base.h>
30
31#include <fstream>
32#include <string>
33#include <utility>
34#include <vector>
35
36namespace tvm {
37
42class SourceName;
46class SourceNameNode : public ffi::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
57 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.SourceName", SourceNameNode, ffi::Object);
58};
59
64class SourceName : public ffi::ObjectRef {
65 public:
72 TVM_DLL static SourceName Get(const ffi::String& name);
73
75};
76
80class Span;
84class SpanNode : public ffi::Object {
85 public:
89 int line;
91 int column;
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
108 TVM_FFI_DECLARE_OBJECT_INFO("ir.Span", SpanNode, ffi::Object);
109};
110
111class Span : public ffi::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
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
140class 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
154class Source;
155
156class SourceNode : public ffi::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
176class Source : public ffi::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
187class SourceMap;
191class SourceMapObj : public ffi::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
202 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.SourceMap", SourceMapObj, ffi::Object);
203};
204
205class SourceMap : public ffi::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
213
214 void Add(const Source& source);
215
217 TVM_FFI_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
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
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.SourceMap", SourceMapObj, ffi::Object)
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)
SourceMapObj * operator->()
Definition source_map.h:216
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(SourceMap, ffi::ObjectRef, SourceMapObj)
SourceMap()
Definition source_map.h:212
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, ffi::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, ffi::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
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.Source", SourceNode, ffi::Object)
static void RegisterReflection()
Definition source_map.h:167
SourceName source_name
The source name.
Definition source_map.h:159
Definition source_map.h:176
tvm::ffi::String GetLine(int line)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Source, ffi::ObjectRef, SourceNode)
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, ffi::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, ffi::ObjectRef, SpanNode)
Span Merge(const Span &other) const
Merge two spans into one which captures the combined regions.
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40