tvm
Loading...
Searching...
No Matches
base.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
23#ifndef TVM_SCRIPT_IR_BUILDER_BASE_H_
24#define TVM_SCRIPT_IR_BUILDER_BASE_H_
25
26#include <tvm/ffi/reflection/registry.h>
27#include <tvm/ir/expr.h>
28#include <tvm/ir/function.h>
30
31#include <cstdint>
32#include <vector>
33
34namespace tvm {
35namespace script {
36namespace ir_builder {
37
39
48class TVM_TIRX_DLL IRBuilderFrameObj : public ffi::Object {
49 public:
53 std::vector<ffi::TypedFunction<void()>> callbacks;
55 mutable Location loc = UnknownLoc();
56
57 static void RegisterReflection() {
58 namespace refl = tvm::ffi::reflection;
59 refl::ObjectDef<IRBuilderFrameObj>().def_ro("loc", &IRBuilderFrameObj::loc);
60 // `callbacks` is not registered as it's not visited.
61 }
62
63 static constexpr const bool _type_mutable = true;
64 TVM_FFI_DECLARE_OBJECT_INFO("script.ir_builder.IRBuilderFrame", IRBuilderFrameObj, ffi::Object);
65
66 public:
68 virtual ~IRBuilderFrameObj() = default;
73 virtual void EnterWithScope();
78 virtual void ExitWithScope();
83 void AddCallback(ffi::TypedFunction<void()> callback);
84};
85
90class IRBuilderFrame : public ffi::ObjectRef {
91 public:
93
94 protected:
96 IRBuilderFrame() = default;
97 explicit IRBuilderFrame(ffi::ObjectPtr<IRBuilderFrameObj> data) : ffi::ObjectRef(data) {}
98
99 public:
104 inline void EnterWithScope() {
105 TVM_FFI_ICHECK(data_ != nullptr);
106 static_cast<IRBuilderFrameObj*>(data_.get())->EnterWithScope();
107 }
112 inline void ExitWithScope() {
113 TVM_FFI_ICHECK(data_ != nullptr);
114 static_cast<IRBuilderFrameObj*>(data_.get())->ExitWithScope();
115 data_.reset();
116 }
117};
118
120
141class IRBuilderObj : public ffi::Object {
142 public:
144 ffi::Array<IRBuilderFrame> frames;
146 ffi::Optional<ffi::ObjectRef> result;
148 std::vector<Location> locs;
149
150 static void RegisterReflection() {
151 namespace refl = tvm::ffi::reflection;
152 refl::ObjectDef<IRBuilderObj>()
153 .def_ro("frames", &IRBuilderObj::frames)
154 .def_ro("result", &IRBuilderObj::result);
155 }
156
157 static constexpr const bool _type_mutable = true;
158 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.IRBuilder", IRBuilderObj, ffi::Object);
159
160 public:
166 template <typename TFrame>
167 inline ffi::Optional<TFrame> FindFrame() const;
174 template <typename TFrame>
175 inline ffi::Optional<TFrame> GetLastFrame() const;
181 template <typename TObjectRef>
182 inline TObjectRef Get() const;
187 int64_t PushLoc(Location loc) {
188 locs.push_back(std::move(loc));
189 return static_cast<int64_t>(locs.size()) - 1;
190 }
196 void UpdateLoc(int64_t index, Location loc) {
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);
200 }
202 void PopLoc() {
203 TVM_FFI_CHECK(!locs.empty(), ValueError)
204 << "ValueError: No source location exists in the builder scope";
205 locs.pop_back();
206 }
210 TVM_TIRX_DLL ffi::ObjectRef AttachLoc(ffi::ObjectRef obj, Location loc = UnknownLoc()) const;
211};
212
217class IRBuilder : public ffi::ObjectRef {
218 public:
220 IRBuilder() { data_ = ffi::make_object<IRBuilderObj>(); }
222
223 public:
258 TVM_TIRX_DLL static bool IsInScope();
265 template <class TObjectRef>
266 inline static TObjectRef Name(ffi::String name, TObjectRef obj);
267};
268
270
271namespace detail {
272
275 public:
276 using FType = ObjectFunctor<Location*(const ffi::ObjectRef&)>;
278};
279
280class Namer {
281 public:
282 using FType = ObjectFunctor<void(const ffi::ObjectRef&, ffi::String)>;
284 TVM_TIRX_DLL static void Name(ffi::ObjectRef node, ffi::String name);
285};
286
287} // namespace detail
288
289template <class TObjectRef>
290inline TObjectRef IRBuilder::Name(ffi::String name, TObjectRef obj) {
291 detail::Namer::Name(obj, name);
292 return obj.template as_or_throw<TObjectRef>();
293}
294
295template <typename TFrame>
296inline ffi::Optional<TFrame> IRBuilderObj::FindFrame() const {
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);
301 }
302 }
303 return std::nullopt;
304}
305
306template <typename TFrame>
307inline ffi::Optional<TFrame> IRBuilderObj::GetLastFrame() const {
308 using TFrameNode = typename TFrame::ContainerType;
309 if (!frames.empty() && frames.back()->IsInstance<TFrameNode>()) {
310 return frames.back().template as_or_throw<TFrame>();
311 }
312 return std::nullopt;
313}
314
315template <typename TObjectRef>
316inline TObjectRef IRBuilderObj::Get() const {
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);
323}
324
325} // namespace ir_builder
326} // namespace script
327} // namespace tvm
328
329#endif // TVM_SCRIPT_IR_BUILDER_BASE_H_
Base expr nodes in TVM.
Function nodes.
#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
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.