tvm
base.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_IR_BUILDER_BASE_H_
20 #define TVM_SCRIPT_IR_BUILDER_BASE_H_
21 
22 #include <tvm/ir/expr.h>
23 #include <tvm/ir/function.h>
24 #include <tvm/node/node.h>
25 
26 #include <vector>
27 
28 namespace tvm {
29 namespace script {
30 namespace ir_builder {
31 
33 
65  public:
67  std::vector<runtime::TypedPackedFunc<void()>> callbacks;
68 
70  // `callbacks` is not visited.
71  }
72 
73  static constexpr const char* _type_key = "script.ir_builder.IRBuilderFrame";
75 
76  public:
78  virtual ~IRBuilderFrameNode() = default;
83  virtual void EnterWithScope();
88  virtual void ExitWithScope();
93  void AddCallback(runtime::TypedPackedFunc<void()> callback);
94 };
95 
101  public:
103  virtual ~IRBuilderFrame() = default;
105 
106  protected:
108  IRBuilderFrame() = default;
109 
110  public:
115  inline void EnterWithScope() {
116  ICHECK(data_ != nullptr);
117  static_cast<IRBuilderFrameNode*>(data_.get())->EnterWithScope();
118  }
123  inline void ExitWithScope() {
124  ICHECK(data_ != nullptr);
125  static_cast<IRBuilderFrameNode*>(data_.get())->ExitWithScope();
126  data_.reset();
127  }
128 };
129 
131 
157  public:
162 
164  v->Visit("frames", &frames);
165  v->Visit("result", &result);
166  }
167 
168  static constexpr const char* _type_key = "script.ir_builder.IRBuilder";
170 
171  public:
177  template <typename TFrame>
178  inline Optional<TFrame> FindFrame() const;
185  template <typename TFrame>
186  inline Optional<TFrame> GetLastFrame() const;
192  template <typename TObjectRef>
193  inline TObjectRef Get() const;
194 };
195 
201  public:
205 
206  public:
239  static IRBuilder Current();
241  static bool IsInScope();
248  template <class TObjectRef>
249  inline static TObjectRef Name(String name, TObjectRef obj);
250 };
251 
253 
254 namespace details {
255 
256 class Namer {
257  public:
258  using FType = NodeFunctor<void(const ObjectRef&, String)>;
259  static FType& vtable();
260  static void Name(ObjectRef node, String name);
261 };
262 
263 } // namespace details
264 
265 template <class TObjectRef>
266 inline TObjectRef IRBuilder::Name(String name, TObjectRef obj) {
267  details::Namer::Name(obj, name);
268  return Downcast<TObjectRef>(obj);
269 }
270 
271 template <typename TFrame>
273  using TFrameNode = typename TFrame::ContainerType;
274  for (auto it = frames.rbegin(); it != frames.rend(); ++it) {
275  if (const TFrameNode* p = (*it).template as<TFrameNode>()) {
276  return GetRef<TFrame>(p);
277  }
278  }
279  return NullOpt;
280 }
281 
282 template <typename TFrame>
284  using TFrameNode = typename TFrame::ContainerType;
285  if (!frames.empty() && frames.back()->IsInstance<TFrameNode>()) {
286  return Downcast<TFrame>(frames.back());
287  }
288  return NullOpt;
289 }
290 
291 template <typename TObjectRef>
292 inline TObjectRef IRBuilderNode::Get() const {
293  using TObject = typename TObjectRef::ContainerType;
294  CHECK(result.defined()) << "IndexError: No result exists in IRBuilder yet";
295  const auto* n = result.as<TObject>();
296  CHECK(n != nullptr) << "TypeError: IRBuilder result is not of type: " << TObject::_type_key;
297  return GetRef<TObjectRef>(n);
298 }
299 
300 } // namespace ir_builder
301 } // namespace script
302 } // namespace tvm
303 
304 #endif // TVM_SCRIPT_IR_BUILDER_BASE_H_
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
A dynamically dispatched functor on the type of the first argument.
Definition: functor.h:64
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Base class of all object reference.
Definition: object.h:519
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:605
base class of all object containers.
Definition: object.h:171
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Reference to string objects.
Definition: string.h:98
Please refer to TypedPackedFunc<R(Args..)>.
Definition: packed_func.h:63
void AddCallback(runtime::TypedPackedFunc< void()> callback)
Add a callback method invoked when exiting the RAII scope.
void VisitAttrs(tvm::AttrVisitor *v)
Definition: base.h:69
TVM_DECLARE_BASE_OBJECT_INFO(IRBuilderFrameNode, runtime::Object)
static constexpr const char * _type_key
Definition: base.h:73
virtual ~IRBuilderFrameNode()=default
Default destructor.
virtual void ExitWithScope()
The method called when exiting RAII scope.
std::vector< runtime::TypedPackedFunc< void()> > callbacks
A list of callbacks used when exiting the frame.
Definition: base.h:67
virtual void EnterWithScope()
The method called when entering RAII scope.
Managed reference to an IRBuilderFrameNode.
Definition: base.h:100
void EnterWithScope()
Redirected to IRBuilderFrameNode::EnterWithScope.
Definition: base.h:115
IRBuilderFrame()=default
Disallow direct construction of this object.
void ExitWithScope()
Redirected to IRBuilderFrameNode::ExitWithScope.
Definition: base.h:123
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(IRBuilderFrame, ObjectRef, IRBuilderFrameNode)
virtual ~IRBuilderFrame()=default
Default destructor.
A dialect-agnostic IRBuilder that constructs any IR of TVM. An idiomatic use of this class is to put ...
Definition: base.h:156
void VisitAttrs(tvm::AttrVisitor *v)
Definition: base.h:163
TObjectRef Get() const
Get the IR being constructed.
Definition: base.h:292
Optional< TFrame > GetLastFrame() const
Get the frame on top of the stack this->frames if its type is TFrame.
Definition: base.h:283
static constexpr const char * _type_key
Definition: base.h:168
TVM_DECLARE_FINAL_OBJECT_INFO(IRBuilderNode, runtime::Object)
Optional< TFrame > FindFrame() const
Find a frame of the given type in the stack this->frames from top to bottom.
Definition: base.h:272
Optional< ObjectRef > result
The outcome of IR construction.
Definition: base.h:161
runtime::Array< IRBuilderFrame > frames
A stack of context frames in the IRBuilder.
Definition: base.h:159
Managed reference to an IRBuilderNode.
Definition: base.h:200
void ExitWithScope()
Exit the RAII scope.
static IRBuilder Current()
Get the current IRBuilder in the current thread-local scope.
void EnterWithScope()
Puts the current IRBuilder into a thread-local scope, which can be retrieved using IRBuilder::Current...
static bool IsInScope()
See if the current thread-local scope has an IRBuilder.
static TObjectRef Name(String name, TObjectRef obj)
Give a string name to the obj
Definition: base.h:266
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(IRBuilder, ObjectRef, IRBuilderNode)
IRBuilder()
Creates an IRBuilder.
static void Name(ObjectRef node, String name)
Base expr nodes in TVM.
Function nodes.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
constexpr runtime::NullOptType NullOpt
Definition: optional.h:169
Definitions and helper macros for IR/AST nodes.