19 #ifndef TVM_SCRIPT_IR_BUILDER_BASE_H_
20 #define TVM_SCRIPT_IR_BUILDER_BASE_H_
30 namespace ir_builder {
73 static constexpr
const char*
_type_key =
"script.ir_builder.IRBuilderFrame";
116 ICHECK(
data_ !=
nullptr);
124 ICHECK(
data_ !=
nullptr);
164 v->Visit(
"frames", &
frames);
165 v->Visit(
"result", &
result);
168 static constexpr
const char*
_type_key =
"script.ir_builder.IRBuilder";
177 template <
typename TFrame>
185 template <
typename TFrame>
192 template <
typename TObjectRef>
193 inline TObjectRef
Get()
const;
248 template <
class TObjectRef>
249 inline static TObjectRef
Name(
String name, TObjectRef obj);
265 template <
class TObjectRef>
268 return Downcast<TObjectRef>(obj);
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);
282 template <
typename TFrame>
284 using TFrameNode =
typename TFrame::ContainerType;
285 if (!
frames.empty() &&
frames.back()->IsInstance<TFrameNode>()) {
286 return Downcast<TFrame>(
frames.back());
291 template <
typename TObjectRef>
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);
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)
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.