A stack frame of the IRBuilder used to keep track of the current scope. Furthermore, the information stored in each stack frame can be useful for context-dependent IR construction.The T::MatchBuffer
below adds an element in PrimFuncNode::buffer_map
:
#ifndef TVM_SCRIPT_IR_BUILDER_BASE_H_
#define TVM_SCRIPT_IR_BUILDER_BASE_H_
#include <vector>
namespace script {
namespace ir_builder {
class IRBuilderFrameNode : public runtime::Object {
public:
std::vector<runtime::TypedPackedFunc<void()>>
callbacks;
}
static constexpr
const char*
_type_key =
"script.ir_builder.IRBuilderFrame";
public:
void AddCallback(runtime::TypedPackedFunc<
void()> callback);
};
class IRBuilderFrame : public runtime::ObjectRef {
public:
protected:
public:
ICHECK(
data_ !=
nullptr);
}
ICHECK(
data_ !=
nullptr);
}
};
class IRBuilderNode : public runtime::Object {
public:
runtime::Array<IRBuilderFrame>
frames;
}
static constexpr
const char*
_type_key =
"script.ir_builder.IRBuilder";
public:
template <typename TFrame>
template <typename TFrame>
template <typename TObjectRef>
inline TObjectRef
Get()
const;
};
class IRBuilder : public runtime::ObjectRef {
public:
public:
template <class TObjectRef>
inline static TObjectRef
Name(String name, TObjectRef obj);
};
namespace details {
class Namer {
public:
using FType = NodeFunctor<void(
const ObjectRef&, String)>;
static void Name(ObjectRef node, String name);
};
}
template <class TObjectRef>
return Downcast<TObjectRef>(obj);
}
template <typename TFrame>
using TFrameNode = typename TFrame::ContainerType;
for (
auto it =
frames.rbegin(); it !=
frames.rend(); ++it) {
if (const TFrameNode* p = (*it).template as<TFrameNode>()) {
return GetRef<TFrame>(p);
}
}
}
template <typename TFrame>
using TFrameNode = typename TFrame::ContainerType;
if (!
frames.empty() &&
frames.back()->IsInstance<TFrameNode>()) {
return Downcast<TFrame>(
frames.back());
}
}
template <typename TObjectRef>
using TObject = typename TObjectRef::ContainerType;
CHECK(
result.defined()) <<
"IndexError: No result exists in IRBuilder yet";
const auto* n =
result.as<TObject>();
CHECK(n != nullptr) << "TypeError: IRBuilder result is not of type: " << TObject::_type_key;
return GetRef<TObjectRef>(n);
}
}
}
}
#endif
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
ObjectRef()=default
default constructor
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:605
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.
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.
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
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)
NodeFunctor< void(const ObjectRef &, String)> FType
Definition: base.h:258
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.