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 */
23#ifndef TVM_SCRIPT_IR_BUILDER_FRAME_H_
24#define TVM_SCRIPT_IR_BUILDER_FRAME_H_
25
26#include <tvm/ffi/reflection/registry.h>
27#include <tvm/ir/expr.h>
28#include <tvm/ir/function.h>
29#include <tvm/ir/module.h>
30#include <tvm/ir/stmt.h>
32
33#include <vector>
34
35namespace tvm {
36namespace script {
37namespace ir_builder {
38namespace ir {
39
46 public:
48 ffi::Map<ffi::String, GlobalVar> global_var_map;
53 ffi::Map<GlobalVar, BaseFunc> functions;
55 ffi::Map<ffi::String, Any> attrs;
57 ffi::Map<ffi::String, ffi::Array<GlobalInfo>> global_infos;
58
59 static void RegisterReflection() {
60 namespace refl = tvm::ffi::reflection;
61 refl::ObjectDef<IRModuleFrameObj>()
62 .def_ro("global_vars", &IRModuleFrameObj::global_var_map)
63 .def_ro("functions", &IRModuleFrameObj::functions)
64 .def_ro("attrs", &IRModuleFrameObj::attrs)
65 .def_ro("global_infos", &IRModuleFrameObj::global_infos);
66 }
67 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.IRModuleFrame", IRModuleFrameObj,
69
70 public:
72};
73
80 public:
81 explicit IRModuleFrame(ffi::ObjectPtr<IRModuleFrameObj> data) : IRBuilderFrame(data) {
82 TVM_FFI_ICHECK(data != nullptr);
83 }
85};
86
92
95 public:
97 ffi::Array<tvm::Stmt> stmts;
98
99 static void RegisterReflection() {
100 ffi::reflection::ObjectDef<StmtFrameObj>().def_ro("stmts", &StmtFrameObj::stmts);
101 }
103};
104
106class StmtFrame : public IRBuilderFrame {
107 public:
109
110 protected:
111 StmtFrame() = default;
112 explicit StmtFrame(ffi::ObjectPtr<StmtFrameObj> data) : IRBuilderFrame(data) {}
113};
114
125
131class ForFrameObj : public StmtFrameObj {
132 public:
140 using FMakeForLoop = ffi::TypedFunction<tvm::Stmt(
141 ffi::Array<tvm::Var> loop_vars, ffi::Array<Range> loop_extents,
142 ffi::Array<ffi::Optional<PrimExpr>> loop_steps, tvm::SeqStmt loop_body, Location loc)>;
144 ffi::Array<tvm::Var> vars;
146 ffi::Array<Range> doms;
148 ffi::Array<ffi::Optional<PrimExpr>> steps;
151
152 static void RegisterReflection() {
153 namespace refl = tvm::ffi::reflection;
154 refl::ObjectDef<ForFrameObj>()
155 .def_ro("vars", &ForFrameObj::vars)
156 .def_ro("doms", &ForFrameObj::doms);
157 // `f_make_for_loop` is not registered as it's not visited.
158 }
160
161 public:
164 ffi::Optional<ffi::Variant<ffi::String, ffi::Array<ffi::String>>> names);
167};
168
174class ForFrame : public StmtFrame {
175 public:
176 explicit ForFrame(ffi::ObjectPtr<ForFrameObj> data) : StmtFrame(ffi::UnsafeInit{}) {
177 TVM_FFI_ICHECK(data != nullptr);
178 data_ = std::move(data);
179 }
181};
182
190 public:
191 explicit AssertFrameObj(ffi::UnsafeInit tag) : condition(tag), error_kind(tag) {}
192
194 : condition(std::move(condition)), error_kind(std::move(error_kind)) {}
195
201 ffi::Array<tvm::StringImm> message_parts;
202
203 static void RegisterReflection() {
204 namespace refl = tvm::ffi::reflection;
205 refl::ObjectDef<AssertFrameObj>()
206 .def_ro("condition", &AssertFrameObj::condition)
207 .def_ro("error_kind", &AssertFrameObj::error_kind)
208 .def_ro("message_parts", &AssertFrameObj::message_parts);
209 }
211
212 public:
218};
219
225class AssertFrame : public StmtFrame {
226 public:
227 explicit AssertFrame(ffi::ObjectPtr<AssertFrameObj> data) : StmtFrame(ffi::UnsafeInit{}) {
228 TVM_FFI_ICHECK(data != nullptr);
229 data_ = std::move(data);
230 }
232};
233
239 public:
240 explicit RegionFrameObj(ffi::UnsafeInit tag) : op(tag) {}
241 explicit RegionFrameObj(Op op) : op(std::move(op)) {}
242
246 ffi::Array<Expr> args;
248 ffi::Array<Var> body_params;
251
252 static void RegisterReflection() {
253 namespace refl = tvm::ffi::reflection;
254 refl::ObjectDef<RegionFrameObj>()
255 .def_ro("op", &RegionFrameObj::op)
256 .def_ro("args", &RegionFrameObj::args)
257 .def_ro("body_params", &RegionFrameObj::body_params)
258 .def_ro("attrs", &RegionFrameObj::attrs);
259 }
261
264};
265
267class RegionFrame : public StmtFrame {
268 public:
269 explicit RegionFrame(ffi::ObjectPtr<RegionFrameObj> data) : StmtFrame(ffi::UnsafeInit{}) {
270 TVM_FFI_ICHECK(data != nullptr);
271 data_ = std::move(data);
272 }
274};
275
282 public:
283 explicit WhileFrameObj(ffi::UnsafeInit tag) : condition(tag) {}
284
285 explicit WhileFrameObj(PrimExpr condition) : condition(std::move(condition)) {}
286
289
290 static void RegisterReflection() {
291 namespace refl = tvm::ffi::reflection;
292 refl::ObjectDef<WhileFrameObj>().def_ro("condition", &WhileFrameObj::condition);
293 }
295
296 public:
302};
303
309class WhileFrame : public StmtFrame {
310 public:
311 explicit WhileFrame(ffi::ObjectPtr<WhileFrameObj> data) : StmtFrame(ffi::UnsafeInit{}) {
312 TVM_FFI_ICHECK(data != nullptr);
313 data_ = std::move(data);
314 }
316};
317
323class IfFrameObj : public StmtFrameObj {
324 public:
325 explicit IfFrameObj(ffi::UnsafeInit tag) : condition(tag) {}
326
327 explicit IfFrameObj(PrimExpr condition) : condition(std::move(condition)) {}
328
332 ffi::Optional<ffi::Array<tvm::Stmt>> then_stmts;
334 ffi::Optional<ffi::Array<tvm::Stmt>> else_stmts;
335
336 static void RegisterReflection() {
337 namespace refl = tvm::ffi::reflection;
338 refl::ObjectDef<IfFrameObj>()
339 .def_ro("condition", &IfFrameObj::condition)
340 .def_ro("then_stmts", &IfFrameObj::then_stmts)
341 .def_ro("else_stmts", &IfFrameObj::else_stmts);
342 }
344
345 public:
351};
352
358class IfFrame : public StmtFrame {
359 public:
360 explicit IfFrame(ffi::ObjectPtr<IfFrameObj> data) : StmtFrame(data) {
361 TVM_FFI_ICHECK(data != nullptr);
362 }
364};
365
372 public:
373 static void RegisterReflection() {
374 namespace refl = tvm::ffi::reflection;
375 refl::ObjectDef<ThenFrameObj>();
376 }
378
379 public:
389 TVM_TIRX_DLL void ExitWithScope() final;
390};
391
397class ThenFrame : public StmtFrame {
398 public:
399 explicit ThenFrame(ffi::ObjectPtr<ThenFrameObj> data) : StmtFrame(data) {
400 TVM_FFI_ICHECK(data != nullptr);
401 }
403};
404
411 public:
412 static void RegisterReflection() {
413 namespace refl = tvm::ffi::reflection;
414 refl::ObjectDef<ElseFrameObj>();
415 }
417
418 public:
428 TVM_TIRX_DLL void ExitWithScope() final;
429};
430
436class ElseFrame : public StmtFrame {
437 public:
438 explicit ElseFrame(ffi::ObjectPtr<ElseFrameObj> data) : StmtFrame(data) {
439 TVM_FFI_ICHECK(data != nullptr);
440 }
441
443};
444
445} // namespace ir
446} // namespace ir_builder
447} // namespace script
448} // namespace tvm
449
450#endif // TVM_SCRIPT_IR_BUILDER_FRAME_H_
Base expr nodes in TVM.
Function nodes.
Statements shared across IR dialects.
#define TVM_TIRX_DLL
Definition base.h:55
Base IRBuilder and frame definitions.
Managed reference to DictAttrsObj.
Definition attrs.h:108
Non-nullable source-location metadata.
Definition location.h:91
Reference-counted handle to a canonical named operator.
Definition op.h:140
Typed reference/view over any Expr whose ExprObj::ty is PrimType.
Definition expr.h:187
Sequence statement.
Definition stmt.h:91
Container of all statements.
Definition stmt.h:61
Managed reference to a string literal.
Definition expr.h:278
A stack frame of the IRBuilder used to keep track of the current scope. Furthermore,...
Definition base.h:48
Location loc
Source context retained until this frame constructs its result.
Definition base.h:55
Managed reference to an IRBuilderFrameObj.
Definition base.h:90
A frame that represents the assert statement. Proceeds if the condition is true, otherwise aborts wit...
Definition frame.h:189
tvm::StringImm error_kind
The error kind, e.g. "RuntimeError", "TypeError", "ValueError".
Definition frame.h:199
static void RegisterReflection()
Definition frame.h:203
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.AssertFrame", AssertFrameObj, StmtFrameObj)
AssertFrameObj(PrimExpr condition, tvm::StringImm error_kind)
Definition frame.h:193
AssertFrameObj(ffi::UnsafeInit tag)
Definition frame.h:191
ffi::Array< tvm::StringImm > message_parts
Error message fragments, concatenated at runtime when assertion fails.
Definition frame.h:201
PrimExpr condition
The PrimExpr to test.
Definition frame.h:197
TVM_TIRX_DLL void ExitWithScope() final
The method called when exiting RAII scope.
Managed reference to AssertFrameObj.
Definition frame.h:225
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(AssertFrame, StmtFrame, AssertFrameObj)
AssertFrame(ffi::ObjectPtr< AssertFrameObj > data)
Definition frame.h:227
A frame that represents else.
Definition frame.h:410
TVM_TIRX_DLL void EnterWithScope() final
The method called when entering RAII scope.
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.ElseFrame", ElseFrameObj, StmtFrameObj)
static void RegisterReflection()
Definition frame.h:412
Managed reference to ElseFrameObj.
Definition frame.h:436
ElseFrame(ffi::ObjectPtr< ElseFrameObj > data)
Definition frame.h:438
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ElseFrame, StmtFrame, ElseFrameObj)
A frame that represents the for loop.
Definition frame.h:131
static void RegisterReflection()
Definition frame.h:152
ffi::Array< Range > doms
The domains of iteration.
Definition frame.h:146
FMakeForLoop f_make_for_loop
The for loop generating function.
Definition frame.h:150
ffi::Array< ffi::Optional< PrimExpr > > steps
The optional steps of iteration.
Definition frame.h:148
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.ForFrame", ForFrameObj, StmtFrameObj)
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:142
ffi::Array< tvm::Var > vars
The loop variable.
Definition frame.h:144
TVM_TIRX_DLL void SetNames(ffi::Optional< ffi::Variant< ffi::String, ffi::Array< ffi::String > > > names)
Apply source target names before entry, preserving variable identity.
TVM_TIRX_DLL void ExitWithScope() final
Construct the loop nest with this frame's stored source location.
Managed reference to ForFrameObj.
Definition frame.h:174
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ForFrame, StmtFrame, ForFrameObj)
ForFrame(ffi::ObjectPtr< ForFrameObj > data)
Definition frame.h:176
A frame that represents the IRModule frame with functions and global variables.
Definition frame.h:45
ffi::Map< GlobalVar, BaseFunc > functions
A map from GlobalVar to global functions and pending declarations.
Definition frame.h:53
static void RegisterReflection()
Definition frame.h:59
ffi::Map< ffi::String, GlobalVar > global_var_map
A map from string names to global variables that ensures global uniqueness.
Definition frame.h:48
TVM_TIRX_DLL 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:57
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.IRModuleFrame", IRModuleFrameObj, IRBuilderFrameObj)
ffi::Map< ffi::String, Any > attrs
IRModule's attributes.
Definition frame.h:55
Managed reference to IRModuleFrameObj.
Definition frame.h:79
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IRModuleFrame, IRBuilderFrame, IRModuleFrameObj)
IRModuleFrame(ffi::ObjectPtr< IRModuleFrameObj > data)
Definition frame.h:81
A frame that represents if statement.
Definition frame.h:323
IfFrameObj(ffi::UnsafeInit tag)
Definition frame.h:325
PrimExpr condition
The condition of the if statement.
Definition frame.h:330
ffi::Optional< ffi::Array< tvm::Stmt > > then_stmts
The statements in the true branch.
Definition frame.h:332
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.IfFrame", IfFrameObj, StmtFrameObj)
ffi::Optional< ffi::Array< tvm::Stmt > > else_stmts
The stetements in the false branch.
Definition frame.h:334
TVM_TIRX_DLL void ExitWithScope() final
The method called when exiting RAII scope.
IfFrameObj(PrimExpr condition)
Definition frame.h:327
static void RegisterReflection()
Definition frame.h:336
Managed reference to IfFrameObj.
Definition frame.h:358
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IfFrame, StmtFrame, IfFrameObj)
IfFrame(ffi::ObjectPtr< IfFrameObj > data)
Definition frame.h:360
A result-free region with lexical body parameters.
Definition frame.h:238
RegionFrameObj(Op op)
Definition frame.h:241
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.RegionFrame", RegionFrameObj, StmtFrameObj)
ffi::Array< Expr > args
Operands evaluated in the enclosing scope.
Definition frame.h:246
RegionFrameObj(ffi::UnsafeInit tag)
Definition frame.h:240
static void RegisterReflection()
Definition frame.h:252
Op op
The operation represented by this region.
Definition frame.h:244
DictAttrs attrs
Additional operation attributes.
Definition frame.h:250
ffi::Array< Var > body_params
Variables defined at entry to the region body.
Definition frame.h:248
TVM_TIRX_DLL void ExitWithScope() final
Construct the region and add it to the enclosing builder frame.
Managed reference to RegionFrameObj.
Definition frame.h:267
RegionFrame(ffi::ObjectPtr< RegionFrameObj > data)
Definition frame.h:269
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(RegionFrame, StmtFrame, RegionFrameObj)
A shared frame containing a sequence of core IR statements.
Definition frame.h:94
ffi::Array< tvm::Stmt > stmts
The statements within this frame.
Definition frame.h:97
static void RegisterReflection()
Definition frame.h:99
TVM_FFI_DECLARE_OBJECT_INFO("script.ir_builder.StmtFrame", StmtFrameObj, IRBuilderFrameObj)
Managed reference to StmtFrameObj.
Definition frame.h:106
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(StmtFrame, IRBuilderFrame, StmtFrameObj)
StmtFrame(ffi::ObjectPtr< StmtFrameObj > data)
Definition frame.h:112
A frame that represents then.
Definition frame.h:371
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.ThenFrame", ThenFrameObj, StmtFrameObj)
static void RegisterReflection()
Definition frame.h:373
TVM_TIRX_DLL void EnterWithScope() final
The method called when entering RAII scope.
Managed reference to ThenFrameObj.
Definition frame.h:397
ThenFrame(ffi::ObjectPtr< ThenFrameObj > data)
Definition frame.h:399
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ThenFrame, StmtFrame, ThenFrameObj)
A frame that represents while loop.
Definition frame.h:281
WhileFrameObj(ffi::UnsafeInit tag)
Definition frame.h:283
WhileFrameObj(PrimExpr condition)
Definition frame.h:285
TVM_TIRX_DLL void ExitWithScope() final
The method called when exiting RAII scope.
static void RegisterReflection()
Definition frame.h:290
PrimExpr condition
The termination condition of while.
Definition frame.h:288
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.ir_builder.WhileFrame", WhileFrameObj, StmtFrameObj)
Managed reference to WhileFrameObj.
Definition frame.h:309
WhileFrame(ffi::ObjectPtr< WhileFrameObj > data)
Definition frame.h:311
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(WhileFrame, StmtFrame, WhileFrameObj)
IRModule that holds the functions and type definitions.
TVM_TIRX_DLL IRModuleFrame FindModuleFrame()
Find the enclosing IRModule frame, allowing intervening frames.
TVM_TIRX_DLL void AddToParent(tvm::Stmt stmt, Location loc)
Append a statement to the current statement frame or top-level builder result.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41