tvm
var_table.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  */
19 #ifndef TVM_SCRIPT_PRINTER_VAR_TABLE_H_
20 #define TVM_SCRIPT_PRINTER_VAR_TABLE_H_
21 
22 #include <tvm/node/node.h>
23 #include <tvm/node/object_path.h>
24 #include <tvm/script/printer/doc.h>
27 
28 #include <unordered_map>
29 #include <unordered_set>
30 
31 namespace tvm {
32 namespace script {
33 namespace printer {
34 
44 class VarTableNode : public Object {
45  public:
47 
60  IdDoc Define(const ObjectRef& obj, const String& name_hint, const ObjectPath& object_path,
61  const Frame& frame);
62 
73  IdDoc Define(const ObjectRef& obj, const TracedObject<String>& name_hint, const Frame& frame) {
74  return Define(obj, name_hint.Get(), name_hint.GetPath(), frame);
75  }
76 
77  using DocFactory = std::function<ExprDoc()>;
78 
95  void DefineByDoc(const ObjectRef& obj, DocFactory doc_factory, const Frame& frame);
96 
104  Optional<ExprDoc> GetVarDoc(const ObjectRef& obj, const ObjectPath& object_path) const;
105 
112  template <typename TObjectRef>
114  return GetVarDoc(obj.Get(), obj.GetPath());
115  }
116 
123  bool IsVarDefined(const ObjectRef& obj) const;
124 
125  static constexpr const char* _type_key = "script.printer.VarTable";
127 
128  private:
129  void RemoveVar(const ObjectRef& obj);
130 
131  struct VariableInfo {
132  DocFactory doc_factory;
133  Optional<String> name;
134  };
135  std::unordered_map<ObjectRef, VariableInfo, ObjectPtrHash, ObjectPtrEqual> obj2info;
136  std::unordered_set<String> defined_names;
137 };
138 
142 class VarTable : public ObjectRef {
143  public:
147  VarTable();
149 };
150 
151 } // namespace printer
152 } // namespace script
153 } // namespace tvm
154 
155 #endif // TVM_SCRIPT_PRINTER_VAR_TABLE_H_
void VisitAttrs(AttrVisitor *)
Definition: var_table.h:46
Traced wrapper for regular (non-container) TVM objects.
Definition: traced_object.h:39
#define TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:758
Reference type of IdDocNode.
Definition: doc.h:367
Definitions and helper macros for IR/AST nodes.
IdDoc Define(const ObjectRef &obj, const String &name_hint, const ObjectPath &object_path, const Frame &frame)
Define variable by name.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
const RefT & Get() const
Access the wrapped object.
Definition: traced_object.h:115
base class of all object containers.
Definition: object.h:167
TVM_DECLARE_FINAL_OBJECT_INFO(VarTableNode, Object)
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
Reference type of FrameNode.
Definition: frame.h:76
Reference to string objects.
Definition: string.h:97
const ObjectPath & GetPath() const
Get the path of the wrapped object.
Definition: traced_object.h:157
Base class of all object reference.
Definition: object.h:511
Variable Table manages mapping from variable object to ExprDoc during the process of printing TVMScri...
Definition: var_table.h:44
Optional< ExprDoc > GetVarDoc(const ObjectRef &obj, const ObjectPath &object_path) const
Get the doc for variable.
void DefineByDoc(const ObjectRef &obj, DocFactory doc_factory, const Frame &frame)
Define variable by doc factory.
static constexpr const char * _type_key
Definition: var_table.h:125
std::function< ExprDoc()> DocFactory
Definition: var_table.h:77
Definition: object_path.h:122
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
IdDoc Define(const ObjectRef &obj, const TracedObject< String > &name_hint, const Frame &frame)
Define variable by name.
Definition: var_table.h:73
Reference type of VarTableNode.
Definition: var_table.h:142
bool IsVarDefined(const ObjectRef &obj) const
Check if a variable exists in the table.
Optional< ExprDoc > GetVarDoc(const TracedObject< TObjectRef > obj) const
Get the doc for variable.
Definition: var_table.h:113