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_PARSER_SOURCE_MAP_H_
24 #define TVM_PARSER_SOURCE_MAP_H_
25 
26 #include <tvm/ir/span.h>
28 #include <tvm/runtime/registry.h>
29 
30 #include <fstream>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 namespace tvm {
36 namespace parser {
37 
43 class Source;
44 
45 class SourceNode : public Object {
46  public:
49 
52 
54  std::vector<std::pair<int, int>> line_map;
55 
56  // override attr visitor
58  v->Visit("source_name", &source_name);
59  v->Visit("source", &source);
60  }
61 
62  static constexpr const char* _type_key = "Source";
64 };
65 
66 class Source : public ObjectRef {
67  public:
68  TVM_DLL Source(SourceName src_name, std::string source);
69  TVM_DLL tvm::String GetLine(int line);
70 
72 };
73 
77 class SourceMap;
81 class SourceMapNode : public Object {
82  public:
85 
86  // override attr visitor
87  void VisitAttrs(AttrVisitor* v) { v->Visit("source_map", &source_map); }
88 
89  bool SEqualReduce(const SourceMapNode* other, SEqualReducer equal) const {
90  return equal(source_map, other->source_map);
91  }
92 
93  static constexpr const char* _type_key = "SourceMap";
95 };
96 
97 class SourceMap : public ObjectRef {
98  public:
99  TVM_DLL SourceMap(Map<SourceName, Source> source_map);
100 
101  TVM_DLL SourceMap(std::initializer_list<std::pair<SourceName, Source>> source_map)
102  : SourceMap(Map<SourceName, Source>(source_map)) {}
103 
104  TVM_DLL SourceMap() : SourceMap(Map<SourceName, Source>()) {}
105 
106  void Add(const Source& source);
107 
109  ICHECK(get() != nullptr);
110  return static_cast<SourceMapNode*>(get_mutable());
111  }
112 
114 };
115 
116 } // namespace parser
117 } // namespace tvm
118 
119 #endif // TVM_PARSER_SOURCE_MAP_H_
SourceMap()
Definition: source_map.h:104
Stores locations in frontend source that generated a node.
Definition: source_map.h:81
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:102
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
static constexpr const char * _type_key
Definition: source_map.h:62
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
std::vector< std::pair< int, int > > line_map
A mapping of line breaks into the raw source.
Definition: source_map.h:54
base class of all object containers.
Definition: object.h:167
bool SEqualReduce(const SourceMapNode *other, SEqualReducer equal) const
Definition: source_map.h:89
Definition: source_map.h:97
Span information for debugging purposes.
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
The source name of a file span.
Definition: span.h:62
void VisitAttrs(AttrVisitor *v)
Definition: source_map.h:57
SourceMapNode * operator->()
Definition: source_map.h:108
Reference to string objects.
Definition: string.h:124
SourceName source_name
The source name.
Definition: source_map.h:48
Map< SourceName, Source > source_map
The source mapping.
Definition: source_map.h:84
void VisitAttrs(AttrVisitor *v)
Definition: source_map.h:87
SourceMap(std::initializer_list< std::pair< SourceName, Source >> source_map)
Definition: source_map.h:101
Base class of all object reference.
Definition: object.h:511
Definition: source_map.h:66
TVM_DECLARE_FINAL_OBJECT_INFO(SourceNode, Object)
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:1268
String source
The raw source.
Definition: source_map.h:51
Definition: source_map.h:45
#define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:728
Type-erased function used across TVM API.
This file defines the TVM global function registry.