tvm
Loading...
Searching...
No Matches
location.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_LOCATION_H_
24#define TVM_IR_LOCATION_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
56 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
57 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.SourceName", SourceNameNode, ffi::Object);
58};
59
64class SourceName : public ffi::ObjectRef {
65 public:
72 TVM_TIRX_DLL static SourceName Get(const ffi::String& name);
73
74 explicit SourceName(ffi::UnsafeInit tag, ffi::ObjectPtr<SourceNameNode> n) : ffi::ObjectRef(tag) {
75 data_ = std::move(n);
76 }
77
79};
80
82class LocationNode : public ffi::Object {
83 public:
84 static void RegisterReflection() { ffi::reflection::ObjectDef<LocationNode>(); }
85
86 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
87 TVM_FFI_DECLARE_OBJECT_INFO("ir.Location", LocationNode, ffi::Object);
88};
89
91class Location : public ffi::ObjectRef {
92 public:
94};
95
98 public:
99 static void RegisterReflection() { ffi::reflection::ObjectDef<UnknownLocNode>(); }
100
102};
103
111
114 public:
116 explicit SourceLocNode(ffi::UnsafeInit tag) : source_name(tag) {}
127
128 static void RegisterReflection() {
129 namespace refl = tvm::ffi::reflection;
130 refl::ObjectDef<SourceLocNode>()
131 .def_ro("source_name", &SourceLocNode::source_name)
132 .def_ro("start_line", &SourceLocNode::start_line)
133 .def_ro("start_column", &SourceLocNode::start_column)
134 .def_ro("end_line", &SourceLocNode::end_line)
135 .def_ro("end_column", &SourceLocNode::end_column);
136 }
137
139};
140
142class SourceLoc : public Location {
143 public:
144 TVM_TIRX_DLL SourceLoc(SourceName source_name, int start_line, int start_column, int end_line,
145 int end_column);
146
149
151};
152
155 public:
158
159 static void RegisterReflection() {
160 namespace refl = tvm::ffi::reflection;
161 refl::ObjectDef<CallSiteLocNode>()
162 .def_ro("callee", &CallSiteLocNode::callee)
163 .def_ro("caller", &CallSiteLocNode::caller);
164 }
165
167};
168
176
182class Source;
183
184class SourceObj : public ffi::Object {
185 public:
187 explicit SourceObj(ffi::UnsafeInit tag) : source_name(tag) {}
190
192 ffi::String source;
193
195 std::vector<std::pair<int, int>> line_map;
196
197 static void RegisterReflection() {
198 namespace refl = tvm::ffi::reflection;
199 refl::ObjectDef<SourceObj>()
200 .def_ro("source_name", &SourceObj::source_name)
201 .def_ro("source", &SourceObj::source);
202 }
204};
205
206class Source : public ffi::ObjectRef {
207 public:
208 TVM_TIRX_DLL Source(SourceName src_name, std::string source);
209 TVM_TIRX_DLL tvm::ffi::String GetLine(int line);
210
212};
213
217class SourceMap;
221class SourceMapObj : public ffi::Object {
222 public:
224 ffi::Map<SourceName, Source> source_map;
225
226 static void RegisterReflection() {
227 namespace refl = tvm::ffi::reflection;
228 refl::ObjectDef<SourceMapObj>().def_ro("source_map", &SourceMapObj::source_map);
229 }
230
231 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
232 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.SourceMap", SourceMapObj, ffi::Object);
233};
234
235class SourceMap : public ffi::ObjectRef {
236 public:
237 explicit SourceMap(ffi::Map<SourceName, Source> source_map);
238
239 explicit SourceMap(std::initializer_list<std::pair<SourceName, Source>> source_map)
240 : SourceMap(ffi::Map<SourceName, Source>(source_map)) {}
241
243
244 void Add(const Source& source);
245
247 TVM_FFI_ICHECK(get() != nullptr);
248 return static_cast<SourceMapObj*>(get_mutable());
249 }
250
252};
253
254} // namespace tvm
255
256#endif // TVM_IR_LOCATION_H_
The location of a callee together with its caller's provenance.
Definition location.h:154
Location callee
Definition location.h:156
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.CallSiteLoc", CallSiteLocNode, LocationNode)
static void RegisterReflection()
Definition location.h:159
Location caller
Definition location.h:157
Explicit inline callee/caller provenance.
Definition location.h:170
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(CallSiteLoc, Location, CallSiteLocNode)
TVM_TIRX_DLL CallSiteLoc(Location callee, Location caller)
Base class for immutable source-location metadata.
Definition location.h:82
TVM_FFI_DECLARE_OBJECT_INFO("ir.Location", LocationNode, ffi::Object)
static void RegisterReflection()
Definition location.h:84
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition location.h:86
Non-nullable source-location metadata.
Definition location.h:91
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Location, ffi::ObjectRef, LocationNode)
A range in one source, retaining frontend coordinate conventions.
Definition location.h:113
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.SourceLoc", SourceLocNode, LocationNode)
SourceLocNode(SourceName source_name)
Definition location.h:115
int start_line
The starting line number.
Definition location.h:120
int start_column
The starting column offset.
Definition location.h:122
static void RegisterReflection()
Definition location.h:128
SourceName source_name
The source name.
Definition location.h:118
SourceLocNode(ffi::UnsafeInit tag)
Definition location.h:116
int end_column
The ending column offset.
Definition location.h:126
int end_line
The ending line number.
Definition location.h:124
A concrete source range.
Definition location.h:142
TVM_TIRX_DLL SourceLoc Merge(const SourceLoc &other) const
Merge two ranges in the same source.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(SourceLoc, Location, SourceLocNode)
TVM_TIRX_DLL SourceLoc(SourceName source_name, int start_line, int start_column, int end_line, int end_column)
Stores locations in frontend source that generated a node.
Definition location.h:221
ffi::Map< SourceName, Source > source_map
The source mapping.
Definition location.h:224
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition location.h:231
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.SourceMap", SourceMapObj, ffi::Object)
static void RegisterReflection()
Definition location.h:226
Definition location.h:235
SourceMap(ffi::Map< SourceName, Source > source_map)
void Add(const Source &source)
SourceMapObj * operator->()
Definition location.h:246
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(SourceMap, ffi::ObjectRef, SourceMapObj)
SourceMap()
Definition location.h:242
SourceMap(std::initializer_list< std::pair< SourceName, Source > > source_map)
Definition location.h:239
The name of a source fragment.
Definition location.h:46
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition location.h:56
static void RegisterReflection()
Definition location.h:51
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.SourceName", SourceNameNode, ffi::Object)
ffi::String name
The source name.
Definition location.h:49
The source name of a file loc.
Definition location.h:64
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(SourceName, ffi::ObjectRef, SourceNameNode)
SourceName(ffi::UnsafeInit tag, ffi::ObjectPtr< SourceNameNode > n)
Definition location.h:74
static TVM_TIRX_DLL 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...
Definition location.h:184
static void RegisterReflection()
Definition location.h:197
SourceObj(SourceName source_name)
Definition location.h:186
SourceObj(ffi::UnsafeInit tag)
Definition location.h:187
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.Source", SourceObj, ffi::Object)
SourceName source_name
The source name.
Definition location.h:189
ffi::String source
The raw source.
Definition location.h:192
std::vector< std::pair< int, int > > line_map
A mapping of line breaks into the raw source.
Definition location.h:195
Definition location.h:206
TVM_TIRX_DLL tvm::ffi::String GetLine(int line)
TVM_TIRX_DLL Source(SourceName src_name, std::string source)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Source, ffi::ObjectRef, SourceObj)
No source-location information is available.
Definition location.h:97
static void RegisterReflection()
Definition location.h:99
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.UnknownLoc", UnknownLocNode, LocationNode)
Reference to the shared immutable unknown location.
Definition location.h:105
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(UnknownLoc, Location, UnknownLocNode)
TVM_TIRX_DLL UnknownLoc()
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
#define TVM_TIRX_DLL
Definition base.h:83