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_SCRIPT_IR_BUILDER_FRAME_H_
20#define TVM_SCRIPT_IR_BUILDER_FRAME_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/module.h>
26#include <tvm/ir/stmt.h>
28
29#include <vector>
30
31namespace tvm {
32namespace script {
33namespace ir_builder {
34namespace ir {
35
42 public:
44 ffi::Map<ffi::String, GlobalVar> global_var_map;
49 ffi::Map<GlobalVar, BaseFunc> functions;
51 ffi::Map<ffi::String, Any> attrs;
53 ffi::Map<ffi::String, ffi::Array<GlobalInfo>> global_infos;
54
55 static void RegisterReflection() {
56 namespace refl = tvm::ffi::reflection;
57 refl::ObjectDef<IRModuleFrameObj>()
58 .def_ro("global_vars", &IRModuleFrameObj::global_var_map)
59 .def_ro("functions", &IRModuleFrameObj::functions)
60 .def_ro("attrs", &IRModuleFrameObj::attrs)
61 .def_ro("global_infos", &IRModuleFrameObj::global_infos);
62 }
63 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.IRModuleFrame", IRModuleFrameObj,
65
66 public:
67 void ExitWithScope() final;
68};
69
76 public:
77 explicit IRModuleFrame(ffi::ObjectPtr<IRModuleFrameObj> data) : IRBuilderFrame(data) {
78 TVM_FFI_ICHECK(data != nullptr);
79 }
81};
82
85 public:
87 ffi::Array<tvm::Stmt> stmts;
88
89 static void RegisterReflection() {
90 ffi::reflection::ObjectDef<StmtFrameObj>().def_ro("stmts", &StmtFrameObj::stmts);
91 }
93};
94
96class StmtFrame : public IRBuilderFrame {
97 public:
99
100 protected:
101 StmtFrame() = default;
102 explicit StmtFrame(ffi::ObjectPtr<StmtFrameObj> data) : IRBuilderFrame(data) {}
103};
104
110class ForFrameObj : public StmtFrameObj {
111 public:
119 using FMakeForLoop = ffi::TypedFunction<tvm::Stmt(
120 ffi::Array<tvm::Var> loop_vars, ffi::Array<Range> loop_extents,
121 ffi::Array<ffi::Optional<PrimExpr>> loop_steps, tvm::SeqStmt loop_body, Location loc)>;
123 ffi::Array<tvm::Var> vars;
125 ffi::Array<Range> doms;
127 ffi::Array<ffi::Optional<PrimExpr>> steps;
130
131 static void RegisterReflection() {
132 namespace refl = tvm::ffi::reflection;
133 refl::ObjectDef<ForFrameObj>()
134 .def_ro("vars", &ForFrameObj::vars)
135 .def_ro("doms", &ForFrameObj::doms);
136 // `f_make_for_loop` is not registered as it's not visited.
137 }
138 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.tirx.ForFrame", ForFrameObj, StmtFrameObj);
139
140 public:
142 void SetNames(ffi::Optional<ffi::Variant<ffi::String, ffi::Array<ffi::String>>> names);
144 void ExitWithScope() final;
145};
146
152class ForFrame : public StmtFrame {
153 public:
154 explicit ForFrame(ffi::ObjectPtr<ForFrameObj> data) : StmtFrame(ffi::UnsafeInit{}) {
155 TVM_FFI_ICHECK(data != nullptr);
156 data_ = std::move(data);
157 }
159};
160
168 public:
169 explicit AssertFrameObj(ffi::UnsafeInit tag) : condition(tag), error_kind(tag) {}
170
172 : condition(std::move(condition)), error_kind(std::move(error_kind)) {}
173
179 ffi::Array<tvm::StringImm> message_parts;
180
181 static void RegisterReflection() {
182 namespace refl = tvm::ffi::reflection;
183 refl::ObjectDef<AssertFrameObj>()
184 .def_ro("condition", &AssertFrameObj::condition)
185 .def_ro("error_kind", &AssertFrameObj::error_kind)
186 .def_ro("message_parts", &AssertFrameObj::message_parts);
187 }
188 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.tirx.AssertFrame", AssertFrameObj,
190
191 public:
196 void ExitWithScope() final;
197};
198
204class AssertFrame : public StmtFrame {
205 public:
206 explicit AssertFrame(ffi::ObjectPtr<AssertFrameObj> data) : StmtFrame(ffi::UnsafeInit{}) {
207 TVM_FFI_ICHECK(data != nullptr);
208 data_ = std::move(data);
209 }
211};
212
218 public:
219 explicit RegionFrameObj(ffi::UnsafeInit tag) : op(tag) {}
220 explicit RegionFrameObj(Op op) : op(std::move(op)) {}
221
225 ffi::Array<Expr> args;
227 ffi::Array<Var> body_params;
230
231 static void RegisterReflection() {
232 namespace refl = tvm::ffi::reflection;
233 refl::ObjectDef<RegionFrameObj>()
234 .def_ro("op", &RegionFrameObj::op)
235 .def_ro("args", &RegionFrameObj::args)
236 .def_ro("body_params", &RegionFrameObj::body_params)
237 .def_ro("attrs", &RegionFrameObj::attrs);
238 }
239 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.tirx.RegionFrame", RegionFrameObj,
241
243 void ExitWithScope() final;
244};
245
247class RegionFrame : public StmtFrame {
248 public:
249 explicit RegionFrame(ffi::ObjectPtr<RegionFrameObj> data) : StmtFrame(ffi::UnsafeInit{}) {
250 TVM_FFI_ICHECK(data != nullptr);
251 data_ = std::move(data);
252 }
254};
255
262 public:
263 explicit WhileFrameObj(ffi::UnsafeInit tag) : condition(tag) {}
264
265 explicit WhileFrameObj(PrimExpr condition) : condition(std::move(condition)) {}
266
269
270 static void RegisterReflection() {
271 namespace refl = tvm::ffi::reflection;
272 refl::ObjectDef<WhileFrameObj>().def_ro("condition", &WhileFrameObj::condition);
273 }
274 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.tirx.WhileFrame", WhileFrameObj,
276
277 public:
282 void ExitWithScope() final;
283};
284
290class WhileFrame : public StmtFrame {
291 public:
292 explicit WhileFrame(ffi::ObjectPtr<WhileFrameObj> data) : StmtFrame(ffi::UnsafeInit{}) {
293 TVM_FFI_ICHECK(data != nullptr);
294 data_ = std::move(data);
295 }
297};
298
304class IfFrameObj : public StmtFrameObj {
305 public:
306 explicit IfFrameObj(ffi::UnsafeInit tag) : condition(tag) {}
307
308 explicit IfFrameObj(PrimExpr condition) : condition(std::move(condition)) {}
309
313 ffi::Optional<ffi::Array<tvm::Stmt>> then_stmts;
315 ffi::Optional<ffi::Array<tvm::Stmt>> else_stmts;
316
317 static void RegisterReflection() {
318 namespace refl = tvm::ffi::reflection;
319 refl::ObjectDef<IfFrameObj>()
320 .def_ro("condition", &IfFrameObj::condition)
321 .def_ro("then_stmts", &IfFrameObj::then_stmts)
322 .def_ro("else_stmts", &IfFrameObj::else_stmts);
323 }
324 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.tirx.IfFrame", IfFrameObj, StmtFrameObj);
325
326 public:
331 void ExitWithScope() final;
332};
333
339class IfFrame : public StmtFrame {
340 public:
341 explicit IfFrame(ffi::ObjectPtr<IfFrameObj> data) : StmtFrame(data) {
342 TVM_FFI_ICHECK(data != nullptr);
343 }
345};
346
353 public:
354 static void RegisterReflection() {
355 namespace refl = tvm::ffi::reflection;
356 refl::ObjectDef<ThenFrameObj>();
357 }
358 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.tirx.ThenFrame", ThenFrameObj, StmtFrameObj);
359
360 public:
365 void EnterWithScope() final;
370 void ExitWithScope() final;
371};
372
378class ThenFrame : public StmtFrame {
379 public:
380 explicit ThenFrame(ffi::ObjectPtr<ThenFrameObj> data) : StmtFrame(data) {
381 TVM_FFI_ICHECK(data != nullptr);
382 }
384};
385
392 public:
393 static void RegisterReflection() {
394 namespace refl = tvm::ffi::reflection;
395 refl::ObjectDef<ElseFrameObj>();
396 }
397 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.tirx.ElseFrame", ElseFrameObj, StmtFrameObj);
398
399 public:
404 void EnterWithScope() final;
409 void ExitWithScope() final;
410};
411
417class ElseFrame : public StmtFrame {
418 public:
419 explicit ElseFrame(ffi::ObjectPtr<ElseFrameObj> data) : StmtFrame(data) {
420 TVM_FFI_ICHECK(data != nullptr);
421 }
422
424};
425
426} // namespace ir
427} // namespace ir_builder
428} // namespace script
429} // namespace tvm
430
431#endif // TVM_SCRIPT_IR_BUILDER_FRAME_H_
Managed reference to DictAttrsNode.
Definition attrs.h:108
Non-nullable source-location metadata.
Definition location.h:91
Reference-counted handle to a canonical named operator.
Definition op.h:147
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:447
Sequence statement.
Definition stmt.h:92
Container of all statements.
Definition stmt.h:62
Managed reference to a string literal.
Definition expr.h:99
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
A frame that represents the assert statement. Proceeds if the condition is true, otherwise aborts wit...
Definition frame.h:167
tvm::StringImm error_kind
The error kind, e.g. "RuntimeError", "TypeError", "ValueError".
Definition frame.h:177
void ExitWithScope() final
The method called when exiting RAII scope.
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.tirx.AssertFrame", AssertFrameObj, StmtFrameObj)
static void RegisterReflection()
Definition frame.h:181
AssertFrameObj(PrimExpr condition, tvm::StringImm error_kind)
Definition frame.h:171
AssertFrameObj(ffi::UnsafeInit tag)
Definition frame.h:169
ffi::Array< tvm::StringImm > message_parts
Error message fragments, concatenated at runtime when assertion fails.
Definition frame.h:179
PrimExpr condition
The PrimExpr to test.
Definition frame.h:175
Managed reference to AssertFrameObj.
Definition frame.h:204
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(AssertFrame, StmtFrame, AssertFrameObj)
AssertFrame(ffi::ObjectPtr< AssertFrameObj > data)
Definition frame.h:206
A frame that represents else.
Definition frame.h:391
void EnterWithScope() final
The method called when entering RAII scope.
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.tirx.ElseFrame", ElseFrameObj, StmtFrameObj)
static void RegisterReflection()
Definition frame.h:393
Managed reference to ElseFrameObj.
Definition frame.h:417
ElseFrame(ffi::ObjectPtr< ElseFrameObj > data)
Definition frame.h:419
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ElseFrame, StmtFrame, ElseFrameObj)
A frame that represents the for loop.
Definition frame.h:110
static void RegisterReflection()
Definition frame.h:131
ffi::Array< Range > doms
The domains of iteration.
Definition frame.h:125
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.tirx.ForFrame", ForFrameObj, StmtFrameObj)
FMakeForLoop f_make_for_loop
The for loop generating function.
Definition frame.h:129
ffi::Array< ffi::Optional< PrimExpr > > steps
The optional steps of iteration.
Definition frame.h:127
ffi::TypedFunction< tvm::Stmt(ffi::Array< tvm::Var > loop_vars, ffi::Array< Range > loop_extents, ffi::Array< ffi::Optional< PrimExpr > > loop_steps, tvm::SeqStmt loop_body, Location loc)> FMakeForLoop
Functions that generate loop nests.
Definition frame.h:121
void ExitWithScope() final
Construct the loop nest with this frame's stored source location.
ffi::Array< tvm::Var > vars
The loop variable.
Definition frame.h:123
void SetNames(ffi::Optional< ffi::Variant< ffi::String, ffi::Array< ffi::String > > > names)
Apply source target names before entry, preserving variable identity.
Managed reference to ForFrameObj.
Definition frame.h:152
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ForFrame, StmtFrame, ForFrameObj)
ForFrame(ffi::ObjectPtr< ForFrameObj > data)
Definition frame.h:154
A frame that represents the IRModule frame with functions and global variables.
Definition frame.h:41
ffi::Map< GlobalVar, BaseFunc > functions
A map from GlobalVar to all global functions.
Definition frame.h:49
static void RegisterReflection()
Definition frame.h:55
ffi::Map< ffi::String, GlobalVar > global_var_map
A map from string names to global variables that ensures global uniqueness.
Definition frame.h:44
void ExitWithScope() final
The method called when exiting RAII scope.
ffi::Map< ffi::String, ffi::Array< GlobalInfo > > global_infos
IRModule's global_infos.
Definition frame.h:53
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.IRModuleFrame", IRModuleFrameObj, IRBuilderFrameObj)
ffi::Map< ffi::String, Any > attrs
IRModule's attributes.
Definition frame.h:51
Managed reference to IRModuleFrameObj.
Definition frame.h:75
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IRModuleFrame, IRBuilderFrame, IRModuleFrameObj)
IRModuleFrame(ffi::ObjectPtr< IRModuleFrameObj > data)
Definition frame.h:77
A frame that represents if statement.
Definition frame.h:304
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.tirx.IfFrame", IfFrameObj, StmtFrameObj)
IfFrameObj(ffi::UnsafeInit tag)
Definition frame.h:306
PrimExpr condition
The condition of the if statement.
Definition frame.h:311
ffi::Optional< ffi::Array< tvm::Stmt > > then_stmts
The statements in the true branch.
Definition frame.h:313
ffi::Optional< ffi::Array< tvm::Stmt > > else_stmts
The stetements in the false branch.
Definition frame.h:315
void ExitWithScope() final
The method called when exiting RAII scope.
IfFrameObj(PrimExpr condition)
Definition frame.h:308
static void RegisterReflection()
Definition frame.h:317
Managed reference to IfFrameObj.
Definition frame.h:339
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IfFrame, StmtFrame, IfFrameObj)
IfFrame(ffi::ObjectPtr< IfFrameObj > data)
Definition frame.h:341
A result-free region with lexical body parameters.
Definition frame.h:217
void ExitWithScope() final
Construct the region and add it to the enclosing builder frame.
RegionFrameObj(Op op)
Definition frame.h:220
ffi::Array< Expr > args
Operands evaluated in the enclosing scope.
Definition frame.h:225
RegionFrameObj(ffi::UnsafeInit tag)
Definition frame.h:219
static void RegisterReflection()
Definition frame.h:231
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.tirx.RegionFrame", RegionFrameObj, StmtFrameObj)
Op op
The operation represented by this region.
Definition frame.h:223
DictAttrs attrs
Additional operation attributes.
Definition frame.h:229
ffi::Array< Var > body_params
Variables defined at entry to the region body.
Definition frame.h:227
Managed reference to RegionFrameObj.
Definition frame.h:247
RegionFrame(ffi::ObjectPtr< RegionFrameObj > data)
Definition frame.h:249
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(RegionFrame, StmtFrame, RegionFrameObj)
A shared frame containing a sequence of core IR statements.
Definition frame.h:84
ffi::Array< tvm::Stmt > stmts
The statements within this frame.
Definition frame.h:87
static void RegisterReflection()
Definition frame.h:89
TVM_FFI_DECLARE_OBJECT_INFO("script.ir_builder.StmtFrame", StmtFrameObj, IRBuilderFrameObj)
Managed reference to StmtFrameObj.
Definition frame.h:96
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(StmtFrame, IRBuilderFrame, StmtFrameObj)
StmtFrame(ffi::ObjectPtr< StmtFrameObj > data)
Definition frame.h:102
A frame that represents then.
Definition frame.h:352
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.tirx.ThenFrame", ThenFrameObj, StmtFrameObj)
static void RegisterReflection()
Definition frame.h:354
void EnterWithScope() final
The method called when entering RAII scope.
Managed reference to ThenFrameObj.
Definition frame.h:378
ThenFrame(ffi::ObjectPtr< ThenFrameObj > data)
Definition frame.h:380
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ThenFrame, StmtFrame, ThenFrameObj)
A frame that represents while loop.
Definition frame.h:261
WhileFrameObj(ffi::UnsafeInit tag)
Definition frame.h:263
void ExitWithScope() final
The method called when exiting RAII scope.
WhileFrameObj(PrimExpr condition)
Definition frame.h:265
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.tirx.WhileFrame", WhileFrameObj, StmtFrameObj)
static void RegisterReflection()
Definition frame.h:270
PrimExpr condition
The termination condition of while.
Definition frame.h:268
Managed reference to WhileFrameObj.
Definition frame.h:290
WhileFrame(ffi::ObjectPtr< WhileFrameObj > data)
Definition frame.h:292
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(WhileFrame, StmtFrame, WhileFrameObj)
Base expr nodes in TVM.
Function nodes.
Statements shared across IR dialects.
IRModule that holds the functions and type definitions.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23