19 #ifndef TVM_SCRIPT_IR_BUILDER_BASE_H_
20 #define TVM_SCRIPT_IR_BUILDER_BASE_H_
22 #include <tvm/ffi/reflection/registry.h>
31 namespace ir_builder {
72 refl::ObjectDef<IRBuilderFrameNode>();
76 static constexpr
const char*
_type_key =
"script.ir_builder.IRBuilderFrame";
117 ICHECK(data_ !=
nullptr);
125 ICHECK(data_ !=
nullptr);
166 refl::ObjectDef<IRBuilderNode>()
171 static constexpr
const char*
_type_key =
"script.ir_builder.IRBuilder";
180 template <
typename TFrame>
181 inline Optional<TFrame>
FindFrame()
const;
188 template <
typename TFrame>
195 template <
typename TObjectRef>
196 inline TObjectRef
Get()
const;
251 template <
class TObjectRef>
252 inline static TObjectRef
Name(String name, TObjectRef obj);
263 static void Name(ObjectRef node, String name);
268 template <
class TObjectRef>
271 return Downcast<TObjectRef>(obj);
274 template <
typename TFrame>
276 using TFrameNode =
typename TFrame::ContainerType;
277 for (
auto it =
frames.rbegin(); it !=
frames.rend(); ++it) {
278 if (
const TFrameNode* p = (*it).template as<TFrameNode>()) {
279 return GetRef<TFrame>(p);
285 template <
typename TFrame>
287 using TFrameNode =
typename TFrame::ContainerType;
288 if (!
frames.empty() &&
frames.back()->IsInstance<TFrameNode>()) {
289 return Downcast<TFrame>(
frames.back());
294 template <
typename TObjectRef>
296 using TObject =
typename TObjectRef::ContainerType;
297 CHECK(
result.defined()) <<
"IndexError: No result exists in IRBuilder yet";
298 const auto* n =
result.as<TObject>();
299 CHECK(n !=
nullptr) <<
"TypeError: IRBuilder result is not of type: " << TObject::_type_key;
300 return GetRef<TObjectRef>(n);
A dynamically dispatched functor on the type of the first argument.
Definition: functor.h:65
TVM_DECLARE_BASE_OBJECT_INFO(IRBuilderFrameNode, runtime::Object)
std::vector< ffi::TypedFunction< void()> > callbacks
A list of callbacks used when exiting the frame.
Definition: base.h:68
static constexpr const char * _type_key
Definition: base.h:76
virtual ~IRBuilderFrameNode()=default
Default destructor.
virtual void ExitWithScope()
The method called when exiting RAII scope.
virtual void EnterWithScope()
The method called when entering RAII scope.
void AddCallback(ffi::TypedFunction< void()> callback)
Add a callback method invoked when exiting the RAII scope.
static void RegisterReflection()
Definition: base.h:70
Managed reference to an IRBuilderFrameNode.
Definition: base.h:103
void EnterWithScope()
Redirected to IRBuilderFrameNode::EnterWithScope.
Definition: base.h:116
IRBuilderFrame()=default
Disallow direct construction of this object.
void ExitWithScope()
Redirected to IRBuilderFrameNode::ExitWithScope.
Definition: base.h:124
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(IRBuilderFrame, ObjectRef, IRBuilderFrameNode)
A dialect-agnostic IRBuilder that constructs any IR of TVM. An idiomatic use of this class is to put ...
Definition: base.h:157
TObjectRef Get() const
Get the IR being constructed.
Definition: base.h:295
Array< IRBuilderFrame > frames
A stack of context frames in the IRBuilder.
Definition: base.h:160
Optional< TFrame > GetLastFrame() const
Get the frame on top of the stack this->frames if its type is TFrame.
Definition: base.h:286
static constexpr const char * _type_key
Definition: base.h:171
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:275
Optional< ObjectRef > result
The outcome of IR construction.
Definition: base.h:162
static void RegisterReflection()
Definition: base.h:164
Managed reference to an IRBuilderNode.
Definition: base.h:203
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:269
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(IRBuilder, ObjectRef, IRBuilderNode)
IRBuilder()
Creates an IRBuilder.
static void Name(ObjectRef node, String name)
Definition: repr_printer.h:91
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
Definitions and helper macros for IR/AST nodes.