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 */
19#ifndef TVM_SCRIPT_IR_BUILDER_BASE_H_
20#define TVM_SCRIPT_IR_BUILDER_BASE_H_
21
22#include <tvm/ffi/reflection/registry.h>
23#include <tvm/ir/expr.h>
24#include <tvm/ir/function.h>
26
27#include <vector>
28
29namespace tvm {
30namespace script {
31namespace ir_builder {
32
34
43class IRBuilderFrameObj : public ffi::Object {
44 public:
48 std::vector<ffi::TypedFunction<void()>> callbacks;
50 mutable Location loc = UnknownLoc();
51
52 static void RegisterReflection() {
53 namespace refl = tvm::ffi::reflection;
54 refl::ObjectDef<IRBuilderFrameObj>().def_ro("loc", &IRBuilderFrameObj::loc);
55 // `callbacks` is not registered as it's not visited.
56 }
57
58 static constexpr const bool _type_mutable = true;
59 TVM_FFI_DECLARE_OBJECT_INFO("script.ir_builder.IRBuilderFrame", IRBuilderFrameObj, ffi::Object);
60
61 public:
63 virtual ~IRBuilderFrameObj() = default;
68 virtual void EnterWithScope();
73 virtual void ExitWithScope();
78 void AddCallback(ffi::TypedFunction<void()> callback);
79};
80
85class IRBuilderFrame : public ffi::ObjectRef {
86 public:
88
89 protected:
91 IRBuilderFrame() = default;
92 explicit IRBuilderFrame(ffi::ObjectPtr<IRBuilderFrameObj> data) : ffi::ObjectRef(data) {}
93
94 public:
99 inline void EnterWithScope() {
100 TVM_FFI_ICHECK(data_ != nullptr);
101 static_cast<IRBuilderFrameObj*>(data_.get())->EnterWithScope();
102 }
107 inline void ExitWithScope() {
108 TVM_FFI_ICHECK(data_ != nullptr);
109 static_cast<IRBuilderFrameObj*>(data_.get())->ExitWithScope();
110 data_.reset();
111 }
112};
113
115
136class IRBuilderObj : public ffi::Object {
137 public:
139 ffi::Array<IRBuilderFrame> frames;
141 ffi::Optional<ffi::ObjectRef> result;
143 std::vector<Location> locs;
144
145 static void RegisterReflection() {
146 namespace refl = tvm::ffi::reflection;
147 refl::ObjectDef<IRBuilderObj>()
148 .def_ro("frames", &IRBuilderObj::frames)
149 .def_ro("result", &IRBuilderObj::result);
150 }
151
152 static constexpr const bool _type_mutable = true;
153 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.IRBuilder", IRBuilderObj, ffi::Object);
154
155 public:
161 template <typename TFrame>
162 inline ffi::Optional<TFrame> FindFrame() const;
169 template <typename TFrame>
170 inline ffi::Optional<TFrame> GetLastFrame() const;
176 template <typename TObjectRef>
177 inline TObjectRef Get() const;
179 void PushLoc(Location loc);
181 void PopLoc();
185 ffi::ObjectRef SetCurrentLoc(ffi::ObjectRef obj) const;
187 ffi::ObjectRef SetLoc(ffi::ObjectRef obj, Location loc) const;
188};
189
194class IRBuilder : public ffi::ObjectRef {
195 public:
199
200 public:
235 static bool IsInScope();
242 template <class TObjectRef>
243 inline static TObjectRef Name(ffi::String name, TObjectRef obj);
244};
245
247
248namespace details {
249
252 public:
253 using FType = ObjectFunctor<Location*(const ffi::ObjectRef&)>;
254 static FType& vtable();
255};
256
257class Namer {
258 public:
259 using FType = ObjectFunctor<void(const ffi::ObjectRef&, ffi::String)>;
260 static FType& vtable();
261 static void Name(ffi::ObjectRef node, ffi::String name);
262};
263
264} // namespace details
265
266template <class TObjectRef>
267inline TObjectRef IRBuilder::Name(ffi::String name, TObjectRef obj) {
268 details::Namer::Name(obj, name);
269 return obj.template as_or_throw<TObjectRef>();
270}
271
272template <typename TFrame>
273inline ffi::Optional<TFrame> IRBuilderObj::FindFrame() const {
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);
278 }
279 }
280 return std::nullopt;
281}
282
283template <typename TFrame>
284inline ffi::Optional<TFrame> IRBuilderObj::GetLastFrame() const {
285 using TFrameNode = typename TFrame::ContainerType;
286 if (!frames.empty() && frames.back()->IsInstance<TFrameNode>()) {
287 return frames.back().template as_or_throw<TFrame>();
288 }
289 return std::nullopt;
290}
291
292template <typename TObjectRef>
293inline TObjectRef IRBuilderObj::Get() const {
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);
300}
301
302} // namespace ir_builder
303} // namespace script
304} // namespace tvm
305
306#endif // TVM_SCRIPT_IR_BUILDER_BASE_H_
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)
Base expr nodes in TVM.
Function nodes.
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.