tvm
Loading...
Searching...
No Matches
frame.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_RELAX_SCRIPT_IR_BUILDER_FRAME_H_
20#define TVM_RELAX_SCRIPT_IR_BUILDER_FRAME_H_
21
22#include <tvm/ffi/reflection/registry.h>
24#include <tvm/relax/expr.h>
28
29#include <utility>
30
31namespace tvm {
32namespace script {
33namespace ir_builder {
34namespace relax {
35
38 public:
39 static void RegisterReflection() {
40 namespace refl = tvm::ffi::reflection;
41 refl::ObjectDef<RelaxFrameObj>();
42 }
43 TVM_FFI_DECLARE_OBJECT_INFO("script.ir_builder.relax.RelaxFrame", RelaxFrameObj,
45};
46
47class RelaxFrame : public IRBuilderFrame {
48 public:
49 explicit RelaxFrame(ffi::ObjectPtr<RelaxFrameObj> data) : IRBuilderFrame(ffi::UnsafeInit{}) {
50 TVM_FFI_ICHECK(data != nullptr);
51 data_ = std::move(data);
52 }
54
55 protected:
56 RelaxFrame() = default;
57};
58
63 public:
65 ffi::Array<tvm::relax::BindingBlock> binding_blocks;
67 ffi::Optional<tvm::relax::Expr> output;
68
69 static void RegisterReflection() {
70 namespace refl = tvm::ffi::reflection;
71 refl::ObjectDef<SeqExprFrameObj>()
72 .def_ro("binding_blocks", &SeqExprFrameObj::binding_blocks)
73 .def_ro("output", &SeqExprFrameObj::output);
74 }
75 TVM_FFI_DECLARE_OBJECT_INFO("script.ir_builder.relax.SeqExprFrame", SeqExprFrameObj,
77
78 public:
79 void EnterWithScope() override;
80 void ExitWithScope() override;
81};
82
83class SeqExprFrame : public RelaxFrame {
84 public:
85 explicit SeqExprFrame(ffi::ObjectPtr<SeqExprFrameObj> data) : RelaxFrame(data) {
86 TVM_FFI_ICHECK(data != nullptr);
87 }
89};
90
93 public:
94 explicit FunctionFrameObj(tvm::relax::BlockBuilder builder) : block_builder(std::move(builder)) {}
95 explicit FunctionFrameObj(ffi::UnsafeInit) : block_builder(ffi::UnsafeInit{}) {}
101 ffi::Optional<ffi::String> name;
103 ffi::Array<tvm::Var> params;
113 ffi::Optional<tvm::Type> ret_ty;
115 ffi::Optional<bool> is_pure;
117 ffi::Optional<bool> is_private;
119 ffi::Map<ffi::String, Any> attrs;
123 bool declaration = false;
124 bool local = false;
125 ffi::Optional<tvm::Var> local_var;
127 ffi::Optional<tvm::relax::Function> function;
128 ffi::Optional<tvm::GlobalVar> global_var;
129
130 static void RegisterReflection() {
131 namespace refl = tvm::ffi::reflection;
132 refl::ObjectDef<FunctionFrameObj>()
133 .def_ro("name", &FunctionFrameObj::name)
134 .def_ro("params", &FunctionFrameObj::params)
135 .def_ro("ret_ty", &FunctionFrameObj::ret_ty)
136 .def_ro("is_pure", &FunctionFrameObj::is_pure)
137 .def_ro("attrs", &FunctionFrameObj::attrs)
138 .def_ro("function", &FunctionFrameObj::function)
139 .def_ro("global_var", &FunctionFrameObj::global_var)
140 .def_ro("local_var", &FunctionFrameObj::local_var);
141 // `binding_blocks` and `output` are inherited from SeqExprFrameObj.
142 // `block_builder` is not registered as it's not visited.
143 }
144 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.relax.FunctionFrame", FunctionFrameObj,
146
147 public:
148 void EnterWithScope() final;
149 void ExitWithScope() final;
150};
151
153 public:
154 explicit FunctionFrame(ffi::ObjectPtr<FunctionFrameObj> data) : SeqExprFrame(data) {
155 TVM_FFI_ICHECK(data != nullptr);
156 }
158};
159
162 public:
166 ffi::Array<tvm::Var> emitted_vars;
177 ffi::Array<tvm::Var> output_vars;
179 ffi::Map<tvm::Var, Location> binding_locs;
180
181 static void RegisterReflection() {
182 namespace refl = tvm::ffi::reflection;
183 refl::ObjectDef<BindingBlockFrameObj>()
184 .def_ro("is_dataflow", &BindingBlockFrameObj::is_dataflow)
185 .def_ro("emitted_vars", &BindingBlockFrameObj::emitted_vars)
186 .def_ro("output_vars", &BindingBlockFrameObj::output_vars);
187 // `block_ended` is not registered as it's not visited.
188 }
189 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.relax.BindingBlockFrame",
191
192 public:
193 void EnterWithScope() final;
194 void ExitWithScope() final;
195};
196
198 public:
199 explicit BindingBlockFrame(ffi::ObjectPtr<BindingBlockFrameObj> data) : RelaxFrame(data) {
200 TVM_FFI_ICHECK(data != nullptr);
201 }
204};
205
211class IfFrameObj : public RelaxFrameObj {
212 public:
213 explicit IfFrameObj(tvm::relax::Expr condition) : condition(std::move(condition)) {}
214 explicit IfFrameObj(ffi::UnsafeInit) : condition(ffi::UnsafeInit{}) {}
215
219 ffi::Optional<tvm::relax::Expr> then_expr;
221 ffi::Optional<tvm::relax::Expr> else_expr;
223 ffi::Optional<tvm::Var> var;
225 ffi::String var_name;
226
227 static void RegisterReflection() {
228 namespace refl = tvm::ffi::reflection;
229 refl::ObjectDef<IfFrameObj>()
230 .def_ro("condition", &IfFrameObj::condition)
231 .def_ro("then_expr", &IfFrameObj::then_expr)
232 .def_ro("else_expr", &IfFrameObj::else_expr)
233 .def_ro("var", &IfFrameObj::var)
234 .def_ro("var_name", &IfFrameObj::var_name);
235 }
236 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.relax.IfFrame", IfFrameObj, RelaxFrameObj);
237
238 public:
243 void EnterWithScope() final;
248 void ExitWithScope() final;
249};
250
256class IfFrame : public RelaxFrame {
257 public:
258 explicit IfFrame(ffi::ObjectPtr<IfFrameObj> data) : RelaxFrame(data) {
259 TVM_FFI_ICHECK(data != nullptr);
260 }
262};
263
270 public:
271 static void RegisterReflection() {
272 namespace refl = tvm::ffi::reflection;
273 refl::ObjectDef<ThenFrameObj>();
274 }
275 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.relax.ThenFrame", ThenFrameObj,
277
278 public:
283 void EnterWithScope() final;
288 void ExitWithScope() final;
289};
290
296class ThenFrame : public SeqExprFrame {
297 public:
298 explicit ThenFrame(ffi::ObjectPtr<ThenFrameObj> data) : SeqExprFrame(data) {
299 TVM_FFI_ICHECK(data != nullptr);
300 }
302};
303
310 public:
311 static void RegisterReflection() {
312 namespace refl = tvm::ffi::reflection;
313 refl::ObjectDef<ElseFrameObj>();
314 }
315 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.relax.ElseFrame", ElseFrameObj,
317
318 public:
323 void EnterWithScope() final;
328 void ExitWithScope() final;
329};
330
336class ElseFrame : public SeqExprFrame {
337 public:
338 explicit ElseFrame(ffi::ObjectPtr<ElseFrameObj> data) : SeqExprFrame(data) {
339 TVM_FFI_ICHECK(data != nullptr);
340 }
342};
343
344} // namespace relax
345} // namespace ir_builder
346} // namespace script
347} // namespace tvm
348
349#endif // TVM_RELAX_SCRIPT_IR_BUILDER_FRAME_H_
The utility for constructing Relax binding blocks.
Managed reference to ExprNode.
Definition base_expr.h:359
Definition block_builder.h:257
A stack frame of the IRBuilder used to keep track of the current scope. Furthermore,...
Definition base.h:43
Managed reference to an IRBuilderFrameObj.
Definition base.h:85
The ir_builder frame for relax binding blocks.
Definition frame.h:161
ffi::Map< tvm::Var, Location > binding_locs
Statement ranges for explicitly emitted bindings.
Definition frame.h:179
ffi::Array< tvm::Var > output_vars
The output vars of the dataflow block.
Definition frame.h:177
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.relax.BindingBlockFrame", BindingBlockFrameObj, RelaxFrameObj)
void EnterWithScope() final
The method called when entering RAII scope.
ffi::Array< tvm::Var > emitted_vars
The variables emitted in this block.
Definition frame.h:166
static void RegisterReflection()
Definition frame.h:181
bool block_ended
A boolean indicating if the dataflow block is ended of construction. If it is true,...
Definition frame.h:172
bool is_dataflow
The flag that indicates whether the block is a dataflow block.
Definition frame.h:164
BindingBlockFrame(ffi::ObjectPtr< BindingBlockFrameObj > data)
Definition frame.h:199
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(BindingBlockFrame, RelaxFrame, BindingBlockFrameObj)
A frame that represents else.
Definition frame.h:309
static void RegisterReflection()
Definition frame.h:311
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.relax.ElseFrame", ElseFrameObj, SeqExprFrameObj)
void EnterWithScope() final
The method called when entering RAII scope.
Managed reference to ElseFrameObj.
Definition frame.h:336
ElseFrame(ffi::ObjectPtr< ElseFrameObj > data)
Definition frame.h:338
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ElseFrame, SeqExprFrame, ElseFrameObj)
The ir_builder frame for the relax function.
Definition frame.h:92
void EnterWithScope() final
The method called when entering RAII scope.
ffi::Optional< bool > is_pure
Whether the function is annotated as pure.
Definition frame.h:115
ffi::Optional< bool > is_private
Whether the function is annotated as private.
Definition frame.h:117
static void RegisterReflection()
Definition frame.h:130
ffi::Optional< tvm::GlobalVar > global_var
Definition frame.h:128
FunctionFrameObj(ffi::UnsafeInit)
Definition frame.h:95
tvm::relax::BlockBuilder block_builder
The block builder to create Relax function.
Definition frame.h:121
void ExitWithScope() final
The method called when exiting RAII scope.
ffi::Map< ffi::String, Any > attrs
The function attributes.
Definition frame.h:119
ffi::Optional< ffi::String > name
The function name.
Definition frame.h:101
FunctionFrameObj(tvm::relax::BlockBuilder builder)
Definition frame.h:94
ffi::Optional< tvm::Type > ret_ty
The function return type.
Definition frame.h:113
ffi::Optional< tvm::Var > local_var
Definition frame.h:125
ffi::Optional< tvm::relax::Function > function
Finalized function and its stable module reference.
Definition frame.h:127
ffi::Array< tvm::Var > params
The function params.
Definition frame.h:103
bool declaration
Whether this frame constructs only a function signature.
Definition frame.h:123
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.relax.FunctionFrame", FunctionFrameObj, SeqExprFrameObj)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(FunctionFrame, SeqExprFrame, FunctionFrameObj)
FunctionFrame(ffi::ObjectPtr< FunctionFrameObj > data)
Definition frame.h:154
A frame that represents if statement.
Definition frame.h:211
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.relax.IfFrame", IfFrameObj, RelaxFrameObj)
ffi::Optional< tvm::relax::Expr > else_expr
The Bindings in the false branch.
Definition frame.h:221
IfFrameObj(ffi::UnsafeInit)
Definition frame.h:214
IfFrameObj(tvm::relax::Expr condition)
Definition frame.h:213
ffi::String var_name
The binding var name.
Definition frame.h:225
void EnterWithScope() final
The method called when entering RAII scope.
ffi::Optional< tvm::Var > var
The Binding var.
Definition frame.h:223
tvm::relax::Expr condition
The condition of the if statement.
Definition frame.h:217
static void RegisterReflection()
Definition frame.h:227
ffi::Optional< tvm::relax::Expr > then_expr
The Bindings in the true branch.
Definition frame.h:219
Managed reference to IfFrameObj.
Definition frame.h:256
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IfFrame, RelaxFrame, IfFrameObj)
IfFrame(ffi::ObjectPtr< IfFrameObj > data)
Definition frame.h:258
The base ir_builder frame for the relax dialect.
Definition frame.h:37
static void RegisterReflection()
Definition frame.h:39
TVM_FFI_DECLARE_OBJECT_INFO("script.ir_builder.relax.RelaxFrame", RelaxFrameObj, IRBuilderFrameObj)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(RelaxFrame, IRBuilderFrame, RelaxFrameObj)
RelaxFrame(ffi::ObjectPtr< RelaxFrameObj > data)
Definition frame.h:49
The base ir_builder frame for frames with SeqExpr i.e. Functions, IfExpr branches.
Definition frame.h:62
static void RegisterReflection()
Definition frame.h:69
ffi::Array< tvm::relax::BindingBlock > binding_blocks
The binding blocks inside the frame.
Definition frame.h:65
TVM_FFI_DECLARE_OBJECT_INFO("script.ir_builder.relax.SeqExprFrame", SeqExprFrameObj, RelaxFrameObj)
void ExitWithScope() override
The method called when exiting RAII scope.
void EnterWithScope() override
The method called when entering RAII scope.
ffi::Optional< tvm::relax::Expr > output
The frame output expr. std::nullopt when undefined.
Definition frame.h:67
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(SeqExprFrame, RelaxFrame, SeqExprFrameObj)
SeqExprFrame(ffi::ObjectPtr< SeqExprFrameObj > data)
Definition frame.h:85
A frame that represents then.
Definition frame.h:269
static void RegisterReflection()
Definition frame.h:271
void EnterWithScope() final
The method called when entering RAII scope.
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.relax.ThenFrame", ThenFrameObj, SeqExprFrameObj)
Managed reference to ThenFrameObj.
Definition frame.h:296
ThenFrame(ffi::ObjectPtr< ThenFrameObj > data)
Definition frame.h:298
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ThenFrame, SeqExprFrame, ThenFrameObj)
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23