19#ifndef TVM_SCRIPT_IR_BUILDER_BASE_H_
20#define TVM_SCRIPT_IR_BUILDER_BASE_H_
22#include <tvm/ffi/reflection/registry.h>
53 namespace refl = tvm::ffi::reflection;
92 explicit IRBuilderFrame(ffi::ObjectPtr<IRBuilderFrameObj> data) : ffi::ObjectRef(data) {}
100 TVM_FFI_ICHECK(data_ !=
nullptr);
108 TVM_FFI_ICHECK(data_ !=
nullptr);
146 namespace refl = tvm::ffi::reflection;
147 refl::ObjectDef<IRBuilderObj>()
161 template <
typename TFrame>
162 inline ffi::Optional<TFrame>
FindFrame()
const;
169 template <
typename TFrame>
176 template <
typename TObjectRef>
177 inline TObjectRef
Get()
const;
242 template <
class TObjectRef>
243 inline static TObjectRef
Name(ffi::String name, TObjectRef obj);
261 static void Name(ffi::ObjectRef node, ffi::String name);
266template <
class TObjectRef>
269 return obj.template as_or_throw<TObjectRef>();
272template <
typename TFrame>
274 using TFrameNode =
typename TFrame::ContainerType;
275 for (
auto it =
frames.rbegin(); it !=
frames.rend(); ++it) {
276 if (
const TFrameNode* p = (*it).template as<TFrameNode>()) {
277 return ffi::GetRef<TFrame>(p);
283template <
typename TFrame>
285 using TFrameNode =
typename TFrame::ContainerType;
286 if (!
frames.empty() &&
frames.back()->IsInstance<TFrameNode>()) {
287 return frames.back().template as_or_throw<TFrame>();
292template <
typename TObjectRef>
294 using TObject =
typename TObjectRef::ContainerType;
295 TVM_FFI_CHECK(
result.has_value(), IndexError) <<
"No result exists in IRBuilder yet";
296 const auto* n =
result.as<TObject>();
297 TVM_FFI_CHECK(n !=
nullptr, TypeError)
298 <<
"IRBuilder result is not of type: " << TObject::_type_key;
299 return ffi::GetRef<TObjectRef>(n);
Non-nullable source-location metadata.
Definition location.h:91
A dynamically dispatched functor on the type of the first argument.
Definition object_functor.h:85
Reference to the shared immutable unknown location.
Definition location.h:105
A stack frame of the IRBuilder used to keep track of the current scope. Furthermore,...
Definition base.h:43
virtual void EnterWithScope()
The method called when entering RAII scope.
Location loc
Source context retained until this frame constructs its result.
Definition base.h:50
TVM_FFI_DECLARE_OBJECT_INFO("script.ir_builder.IRBuilderFrame", IRBuilderFrameObj, ffi::Object)
void AddCallback(ffi::TypedFunction< void()> callback)
Add a callback method invoked when exiting the RAII scope.
virtual void ExitWithScope()
The method called when exiting RAII scope.
static constexpr const bool _type_mutable
Definition base.h:58
static void RegisterReflection()
Definition base.h:52
std::vector< ffi::TypedFunction< void()> > callbacks
A list of callbacks used when exiting the frame.
Definition base.h:48
virtual ~IRBuilderFrameObj()=default
Default destructor.
IRBuilderFrameObj()
Capture the source-call context when this frame is constructed.
Managed reference to an IRBuilderFrameObj.
Definition base.h:85
void EnterWithScope()
Redirected to IRBuilderFrameObj::EnterWithScope.
Definition base.h:99
IRBuilderFrame()=default
Disallow direct construction of this object.
IRBuilderFrame(ffi::ObjectPtr< IRBuilderFrameObj > data)
Definition base.h:92
void ExitWithScope()
Redirected to IRBuilderFrameObj::ExitWithScope.
Definition base.h:107
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IRBuilderFrame, ffi::ObjectRef, IRBuilderFrameObj)
A language variant agnostic IRBuilder that constructs any IR of TVM. An idiomatic use of this class i...
Definition base.h:136
ffi::Array< IRBuilderFrame > frames
A stack of context frames in the IRBuilder.
Definition base.h:139
ffi::Optional< ffi::ObjectRef > result
The outcome of IR construction.
Definition base.h:141
TObjectRef Get() const
Get the IR being constructed.
Definition base.h:293
void PopLoc()
Pop the innermost frontend source location.
ffi::ObjectRef SetLoc(ffi::ObjectRef obj, Location loc) const
Attach an explicit location composed with the active source-call context.
ffi::Optional< TFrame > FindFrame() const
Find a frame of the given type in the stack this->frames from top to bottom.
Definition base.h:273
static constexpr const bool _type_mutable
Definition base.h:152
ffi::Optional< TFrame > GetLastFrame() const
Get the top frame if its type is TFrame.
Definition base.h:284
Location GetCurrentLoc(Location location=UnknownLoc()) const
Return the normalized active source loc, including expansion history.
std::vector< Location > locs
Active frontend source locations, from outermost to innermost.
Definition base.h:143
void PushLoc(Location loc)
Push a frontend source location for IR constructed in the nested scope.
ffi::ObjectRef SetCurrentLoc(ffi::ObjectRef obj) const
Compose active source context onto a supported node or construction frame.
static void RegisterReflection()
Definition base.h:145
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.IRBuilder", IRBuilderObj, ffi::Object)
Managed reference to an IRBuilderObj.
Definition base.h:194
static TObjectRef Name(ffi::String name, TObjectRef obj)
Give a string name to the obj
Definition base.h:267
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IRBuilder, ffi::ObjectRef, IRBuilderObj)
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.
IRBuilder()
Creates an IRBuilder.
Language variant extensible access to an object's mutable source location.
Definition base.h:251
static void Name(ffi::ObjectRef node, ffi::String name)
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
Native object dispatch, visiting and mutation with structural fallback.