19 #ifndef TVM_SCRIPT_IR_BUILDER_BASE_H_ 20 #define TVM_SCRIPT_IR_BUILDER_BASE_H_ 30 namespace ir_builder {
67 std::vector<runtime::TypedPackedFunc<void()>>
callbacks;
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;
241 static bool IsInScope();
248 template <
class TObjectRef>
249 inline static TObjectRef Name(
String name, TObjectRef obj);
259 static FType& vtable();
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);
304 #endif // TVM_SCRIPT_IR_BUILDER_BASE_H_ Managed reference to an IRBuilderFrameNode.
Definition: base.h:100
A dynamically dispatched functor on the type of the first argument.
Definition: functor.h:64
virtual void EnterWithScope()
The method called when entering RAII scope.
#define TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:758
Definitions and helper macros for IR/AST nodes.
void EnterWithScope()
Redirected to IRBuilderFrameNode::EnterWithScope.
Definition: base.h:115
static TObjectRef Name(String name, TObjectRef obj)
Give a string name to the obj
Definition: base.h:266
std::vector< runtime::TypedPackedFunc< void()> > callbacks
A list of callbacks used when exiting the frame.
Definition: base.h:67
static constexpr const char * _type_key
Definition: base.h:73
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
base class of all object containers.
Definition: object.h:167
void AddCallback(runtime::TypedPackedFunc< void()> callback)
Add a callback method invoked when exiting the RAII scope.
runtime::Array< IRBuilderFrame > frames
A stack of context frames in the IRBuilder.
Definition: base.h:159
void VisitAttrs(tvm::AttrVisitor *v)
Definition: base.h:163
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
Optional< TFrame > GetLastFrame() const
Get the frame on top of the stack this->frames if its type is TFrame.
Definition: base.h:283
Managed reference to an IRBuilderNode.
Definition: base.h:200
Optional< ObjectRef > result
The outcome of IR construction.
Definition: base.h:161
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
void VisitAttrs(tvm::AttrVisitor *v)
Definition: base.h:69
TObjectRef Get() const
Get the IR being constructed.
Definition: base.h:292
Reference to string objects.
Definition: string.h:98
Please refer to TypedPackedFunc<R(Args..)>.
Definition: packed_func.h:60
void ExitWithScope()
Redirected to IRBuilderFrameNode::ExitWithScope.
Definition: base.h:123
Base class of all object reference.
Definition: object.h:511
#define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:671
static void Name(ObjectRef node, String name)
virtual void ExitWithScope()
The method called when exiting RAII scope.
TVM_DECLARE_BASE_OBJECT_INFO(IRBuilderFrameNode, runtime::Object)
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
constexpr runtime::NullOptType NullOpt
Definition: optional.h:160
A dialect-agnostic IRBuilder that constructs any IR of TVM. An idiomatic use of this class is to put ...
Definition: base.h:156
Optional< TFrame > FindFrame() const
Find a frame of the given type in the stack this->frames from top to bottom.
Definition: base.h:272
virtual ~IRBuilderFrameNode()=default
Default destructor.