23#ifndef TVM_SCRIPT_IR_BUILDER_BASE_H_
24#define TVM_SCRIPT_IR_BUILDER_BASE_H_
26#include <tvm/ffi/reflection/registry.h>
58 namespace refl = tvm::ffi::reflection;
59 refl::ObjectDef<IRBuilderFrameObj>().def_ro(
"loc", &IRBuilderFrameObj::loc);
63 static constexpr const bool _type_mutable =
true;
97 explicit IRBuilderFrame(ffi::ObjectPtr<IRBuilderFrameObj> data) : ffi::ObjectRef(data) {}
105 TVM_FFI_ICHECK(data_ !=
nullptr);
113 TVM_FFI_ICHECK(data_ !=
nullptr);
151 namespace refl = tvm::ffi::reflection;
152 refl::ObjectDef<IRBuilderObj>()
166 template <
typename TFrame>
167 inline ffi::Optional<TFrame>
FindFrame()
const;
174 template <
typename TFrame>
181 template <
typename TObjectRef>
182 inline TObjectRef
Get()
const;
188 locs.push_back(std::move(loc));
189 return static_cast<int64_t
>(
locs.size()) - 1;
197 TVM_FFI_CHECK(index >= 0 &&
static_cast<size_t>(index) <
locs.size(), ValueError)
198 <<
"Invalid source location scope";
199 locs[index] = std::move(loc);
203 TVM_FFI_CHECK(!
locs.empty(), ValueError)
204 <<
"ValueError: No source location exists in the builder scope";
220 IRBuilder() { data_ = ffi::make_object<IRBuilderObj>(); }
265 template <
class TObjectRef>
266 inline static TObjectRef
Name(ffi::String name, TObjectRef obj);
289template <
class TObjectRef>
292 return obj.template as_or_throw<TObjectRef>();
295template <
typename TFrame>
297 using TFrameNode =
typename TFrame::ContainerType;
298 for (
auto it =
frames.rbegin(); it !=
frames.rend(); ++it) {
299 if (
const TFrameNode* p = (*it).template as<TFrameNode>()) {
300 return ffi::GetRef<TFrame>(p);
306template <
typename TFrame>
308 using TFrameNode =
typename TFrame::ContainerType;
309 if (!
frames.empty() &&
frames.back()->IsInstance<TFrameNode>()) {
310 return frames.back().template as_or_throw<TFrame>();
315template <
typename TObjectRef>
317 using TObject =
typename TObjectRef::ContainerType;
318 TVM_FFI_CHECK(
result.has_value(), IndexError) <<
"No result exists in IRBuilder yet";
319 const auto* n =
result.as<TObject>();
320 TVM_FFI_CHECK(n !=
nullptr, TypeError)
321 <<
"IRBuilder result is not of type: " << TObject::_type_key;
322 return ffi::GetRef<TObjectRef>(n);
#define TVM_TIRX_DLL
Definition base.h:55
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:48
virtual void EnterWithScope()
The method called when entering RAII scope.
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 void RegisterReflection()
Definition base.h:57
std::vector< ffi::TypedFunction< void()> > callbacks
A list of callbacks used when exiting the frame.
Definition base.h:53
virtual ~IRBuilderFrameObj()=default
Default destructor.
IRBuilderFrameObj()
Capture the source-call context when this frame is constructed.
Managed reference to an IRBuilderFrameObj.
Definition base.h:90
void EnterWithScope()
Redirected to IRBuilderFrameObj::EnterWithScope.
Definition base.h:104
IRBuilderFrame()=default
Disallow direct construction of this object.
IRBuilderFrame(ffi::ObjectPtr< IRBuilderFrameObj > data)
Definition base.h:97
void ExitWithScope()
Redirected to IRBuilderFrameObj::ExitWithScope.
Definition base.h:112
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:141
ffi::Array< IRBuilderFrame > frames
A stack of context frames in the IRBuilder.
Definition base.h:144
ffi::Optional< ffi::ObjectRef > result
The outcome of IR construction.
Definition base.h:146
TObjectRef Get() const
Get the IR being constructed.
Definition base.h:316
void PopLoc()
Pop the innermost frontend source location.
Definition base.h:202
void UpdateLoc(int64_t index, Location loc)
Update a source location without changing its nested scopes.
Definition base.h:196
ffi::Optional< TFrame > FindFrame() const
Find a frame of the given type in the stack this->frames from top to bottom.
Definition base.h:296
int64_t PushLoc(Location loc)
Push a frontend source location for IR constructed in the nested scope.
Definition base.h:187
static constexpr const bool _type_mutable
Definition base.h:157
ffi::Optional< TFrame > GetLastFrame() const
Get the top frame if its type is TFrame.
Definition base.h:307
std::vector< Location > locs
Active frontend source locations, from outermost to innermost.
Definition base.h:148
static void RegisterReflection()
Definition base.h:150
TVM_TIRX_DLL ffi::ObjectRef AttachLoc(ffi::ObjectRef obj, Location loc=UnknownLoc()) const
Compose active and explicit source context onto the same supported node or frame.
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.IRBuilder", IRBuilderObj, ffi::Object)
TVM_TIRX_DLL Location ResolveLoc(Location location=UnknownLoc()) const
Normalize the full active source stack with an optional explicit location.
Managed reference to an IRBuilderObj.
Definition base.h:217
TVM_TIRX_DLL void EnterWithScope()
Puts the current IRBuilder into a thread-local scope, which can be retrieved using IRBuilder::Current...
static TObjectRef Name(ffi::String name, TObjectRef obj)
Give a string name to the obj
Definition base.h:290
static TVM_TIRX_DLL IRBuilder Current()
Get the current IRBuilder in the current thread-local scope.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IRBuilder, ffi::ObjectRef, IRBuilderObj)
static TVM_TIRX_DLL bool IsInScope()
See if the current thread-local scope has an IRBuilder.
TVM_TIRX_DLL void ExitWithScope()
Exit the RAII scope.
IRBuilder()
Creates an IRBuilder.
Definition base.h:220
Language variant extensible access to an object's mutable source location.
Definition base.h:274
ObjectFunctor< Location *(const ffi::ObjectRef &)> FType
Definition base.h:276
static TVM_TIRX_DLL FType & vtable()
ObjectFunctor< void(const ffi::ObjectRef &, ffi::String)> FType
Definition base.h:282
static TVM_TIRX_DLL FType & vtable()
static TVM_TIRX_DLL void Name(ffi::ObjectRef node, ffi::String name)
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
Native object dispatch, visiting and mutation with structural fallback.