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>
25#include <tvm/ir/node_functor.h>
26
27#include <vector>
28
29namespace tvm {
30namespace script {
31namespace ir_builder {
32
34
65class IRBuilderFrameNode : public ffi::Object {
66 public:
68 std::vector<ffi::TypedFunction<void()>> callbacks;
69
70 static void RegisterReflection() {
71 namespace refl = tvm::ffi::reflection;
72 refl::ObjectDef<IRBuilderFrameNode>();
73 // `callbacks` is not registered as it's not visited.
74 }
75
76 static constexpr const bool _type_mutable = true;
77 TVM_FFI_DECLARE_OBJECT_INFO("script.ir_builder.IRBuilderFrame", IRBuilderFrameNode, ffi::Object);
78
79 public:
81 virtual ~IRBuilderFrameNode() = default;
86 virtual void EnterWithScope();
91 virtual void ExitWithScope();
96 void AddCallback(ffi::TypedFunction<void()> callback);
97};
98
103class IRBuilderFrame : public ffi::ObjectRef {
104 public:
106
107 protected:
109 IRBuilderFrame() = default;
110 explicit IRBuilderFrame(ffi::ObjectPtr<IRBuilderFrameNode> data) : ffi::ObjectRef(data) {}
111
112 public:
117 inline void EnterWithScope() {
118 TVM_FFI_ICHECK(data_ != nullptr);
119 static_cast<IRBuilderFrameNode*>(data_.get())->EnterWithScope();
120 }
125 inline void ExitWithScope() {
126 TVM_FFI_ICHECK(data_ != nullptr);
127 static_cast<IRBuilderFrameNode*>(data_.get())->ExitWithScope();
128 data_.reset();
129 }
130};
131
133
158class IRBuilderNode : public ffi::Object {
159 public:
161 ffi::Array<IRBuilderFrame> frames;
163 ffi::Optional<ffi::ObjectRef> result;
165 std::vector<Span> source_spans;
166
167 static void RegisterReflection() {
168 namespace refl = tvm::ffi::reflection;
169 refl::ObjectDef<IRBuilderNode>()
170 .def_ro("frames", &IRBuilderNode::frames)
171 .def_ro("result", &IRBuilderNode::result);
172 }
173
174 static constexpr const bool _type_mutable = true;
175 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.IRBuilder", IRBuilderNode, ffi::Object);
176
177 public:
183 template <typename TFrame>
184 inline ffi::Optional<TFrame> FindFrame() const;
191 template <typename TFrame>
192 inline ffi::Optional<TFrame> GetLastFrame() const;
198 template <typename TObjectRef>
199 inline TObjectRef Get() const;
207 ffi::ObjectRef SetCurrentSourceSpan(ffi::ObjectRef obj) const;
208};
209
214class IRBuilder : public ffi::ObjectRef {
215 public:
219
220 public:
255 static bool IsInScope();
262 template <class TObjectRef>
263 inline static TObjectRef Name(ffi::String name, TObjectRef obj);
264};
265
267
268namespace details {
269
270class Namer {
271 public:
272 using FType = NodeFunctor<void(const ffi::ObjectRef&, ffi::String)>;
273 static FType& vtable();
274 static void Name(ffi::ObjectRef node, ffi::String name);
275};
276
277} // namespace details
278
279template <class TObjectRef>
280inline TObjectRef IRBuilder::Name(ffi::String name, TObjectRef obj) {
282 return obj.template as_or_throw<TObjectRef>();
283}
284
285template <typename TFrame>
286inline ffi::Optional<TFrame> IRBuilderNode::FindFrame() const {
287 using TFrameNode = typename TFrame::ContainerType;
288 for (auto it = frames.rbegin(); it != frames.rend(); ++it) {
289 if (const TFrameNode* p = (*it).template as<TFrameNode>()) {
290 return ffi::GetRef<TFrame>(p);
291 }
292 }
293 return std::nullopt;
294}
295
296template <typename TFrame>
297inline ffi::Optional<TFrame> IRBuilderNode::GetLastFrame() const {
298 using TFrameNode = typename TFrame::ContainerType;
299 if (!frames.empty() && frames.back()->IsInstance<TFrameNode>()) {
300 return frames.back().as_or_throw<TFrame>();
301 }
302 return std::nullopt;
303}
304
305template <typename TObjectRef>
307 using TObject = typename TObjectRef::ContainerType;
308 TVM_FFI_CHECK(result.has_value(), IndexError) << "No result exists in IRBuilder yet";
309 const auto* n = result.as<TObject>();
310 TVM_FFI_CHECK(n != nullptr, TypeError)
311 << "IRBuilder result is not of type: " << TObject::_type_key;
312 return ffi::GetRef<TObjectRef>(n);
313}
314
315} // namespace ir_builder
316} // namespace script
317} // namespace tvm
318
319#endif // TVM_SCRIPT_IR_BUILDER_BASE_H_
A dynamically dispatched functor on the type of the first argument.
Definition node_functor.h:62
Definition source_map.h:111
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
TVM_FFI_DECLARE_OBJECT_INFO("script.ir_builder.IRBuilderFrame", IRBuilderFrameNode, ffi::Object)
static constexpr const bool _type_mutable
Definition base.h:76
std::vector< ffi::TypedFunction< void()> > callbacks
A list of callbacks used when exiting the frame.
Definition base.h:68
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:117
IRBuilderFrame(ffi::ObjectPtr< IRBuilderFrameNode > data)
Definition base.h:110
IRBuilderFrame()=default
Disallow direct construction of this object.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IRBuilderFrame, ffi::ObjectRef, IRBuilderFrameNode)
void ExitWithScope()
Redirected to IRBuilderFrameNode::ExitWithScope.
Definition base.h:125
A dialect-agnostic IRBuilder that constructs any IR of TVM. An idiomatic use of this class is to put ...
Definition base.h:158
void PushSourceSpan(Span span)
Push a frontend source span for IR constructed in the nested scope.
ffi::Optional< TFrame > FindFrame() const
Find a frame of the given type in the stack this->frames from top to bottom.
Definition base.h:286
ffi::Optional< TFrame > GetLastFrame() const
Get the frame on top of the stack this->frames if its type is TFrame.
Definition base.h:297
void PopSourceSpan()
Pop the innermost frontend source span.
std::vector< Span > source_spans
Active frontend source spans, from outermost to innermost.
Definition base.h:165
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.IRBuilder", IRBuilderNode, ffi::Object)
TObjectRef Get() const
Get the IR being constructed.
Definition base.h:306
ffi::Optional< ffi::ObjectRef > result
The outcome of IR construction.
Definition base.h:163
Span GetCurrentSourceSpan() const
Return the normalized active source span, including expansion history.
static constexpr const bool _type_mutable
Definition base.h:174
ffi::ObjectRef SetCurrentSourceSpan(ffi::ObjectRef obj) const
Attach the active source span to an expression that has no span yet.
ffi::Array< IRBuilderFrame > frames
A stack of context frames in the IRBuilder.
Definition base.h:161
static void RegisterReflection()
Definition base.h:167
Managed reference to an IRBuilderNode.
Definition base.h:214
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IRBuilder, ffi::ObjectRef, IRBuilderNode)
static TObjectRef Name(ffi::String name, TObjectRef obj)
Give a string name to the obj
Definition base.h:280
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.
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 analyzer.h:40
Defines the Functor data structures.