tvm
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_TIR_FRAME_H_
20 #define TVM_SCRIPT_IR_BUILDER_TIR_FRAME_H_
21 
24 #include <tvm/tir/stmt.h>
25 
26 namespace tvm {
27 namespace script {
28 namespace ir_builder {
29 namespace tir {
30 
37  public:
40 
43  v->Visit("stmts", &stmts);
44  }
45 
46  static constexpr const char* _type_key = "script.ir_builder.tir.TIRFrame";
48 };
49 
55 class TIRFrame : public IRBuilderFrame {
56  public:
58 
59  protected:
60  TIRFrame() = default;
61 };
62 
69  public:
75  bool is_private;
86 
89  v->Visit("name", &name);
90  v->Visit("args", &args);
91  v->Visit("is_private", &is_private);
92  v->Visit("ret_type", &ret_type);
93  v->Visit("buffer_map", &buffer_map);
94  v->Visit("attrs", &attrs);
95  v->Visit("env_threads", &env_threads);
96  v->Visit("root_alloc_buffers", &root_alloc_buffers);
97  }
98 
99  static constexpr const char* _type_key = "script.ir_builder.tir.PrimFuncFrame";
101 
102  public:
107  void ExitWithScope() final;
108 };
109 
115 class PrimFuncFrame : public TIRFrame {
116  public:
118 };
119 
125 class BlockFrameNode : public TIRFrameNode {
126  public:
152 
155  v->Visit("name", &name);
156  v->Visit("iter_vars", &iter_vars);
157  v->Visit("reads", &reads);
158  v->Visit("writes", &writes);
159  v->Visit("init", &init);
160  v->Visit("alloc_buffers", &alloc_buffers);
161  v->Visit("match_buffers", &match_buffers);
162  v->Visit("annotations", &annotations);
163  v->Visit("iter_values", &iter_values);
164  v->Visit("predicate", &predicate);
165  v->Visit("no_realize", &no_realize);
166  }
167 
168  static constexpr const char* _type_key = "script.ir_builder.tir.BlockFrame";
170 
171  public:
176  void ExitWithScope() final;
177 };
178 
185 class BlockFrame : public TIRFrame {
186  public:
188 };
189 
196  public:
198 
199  static constexpr const char* _type_key = "script.ir_builder.tir.BlockInitFrame";
201 
202  public:
207  void EnterWithScope() final;
212  void ExitWithScope() final;
213 };
214 
220 class BlockInitFrame : public TIRFrame {
221  public:
223 };
224 
230 class ForFrameNode : public TIRFrameNode {
231  public:
240  Array<tvm::tir::Var> loop_vars, Array<Range> loop_extents, tvm::tir::Stmt loop_body)>;
247 
250  v->Visit("vars", &vars);
251  v->Visit("doms", &doms);
252  // `f_make_for_loop` is not visited.
253  }
254 
255  static constexpr const char* _type_key = "script.ir_builder.tir.ForFrame";
257 
258  public:
263  void ExitWithScope() final;
264 };
265 
271 class ForFrame : public TIRFrame {
272  public:
274 };
275 
283  public:
288 
291  v->Visit("condition", &condition);
292  v->Visit("message", &message);
293  }
294 
295  static constexpr const char* _type_key = "script.ir_builder.tir.AssertFrame";
297 
298  public:
303  void ExitWithScope() final;
304 };
305 
311 class AssertFrame : public TIRFrame {
312  public:
314 };
315 
321 class LetFrameNode : public TIRFrameNode {
322  public:
327 
330  v->Visit("var", &var);
331  v->Visit("value", &value);
332  }
333 
334  static constexpr const char* _type_key = "script.ir_builder.tir.LetFrame";
336 
337  public:
342  void ExitWithScope() final;
343 };
344 
350 class LetFrame : public TIRFrame {
351  public:
353 };
354 
360  public:
367 
370  v->Visit("extent", &extent);
371  v->Visit("attr_key", &attr_key);
372  v->Visit("iter_var", &iter_var);
373  }
374 
375  static constexpr const char* _type_key = "script.ir_builder.tir.LaunchThreadFrame";
377 
378  public:
383  void ExitWithScope() final;
384 };
385 
391 class LaunchThreadFrame : public TIRFrame {
392  public:
395 };
396 
403  public:
410 
413  v->Visit("buffer_slice", &buffer_slice);
414  v->Visit("storage_scope", &storage_scope);
415  v->Visit("condition", &condition);
416  }
417 
418  static constexpr const char* _type_key = "script.ir_builder.tir.RealizeFrame";
420 
421  public:
426  void ExitWithScope() final;
427 };
428 
434 class RealizeFrame : public TIRFrame {
435  public:
437 };
438 
445  public:
458 
461  v->Visit("extents", &extents);
462  v->Visit("dtype", &dtype);
463  v->Visit("storage_scope", &storage_scope);
464  v->Visit("condition", &condition);
465  v->Visit("annotations", &annotations);
466  v->Visit("buffer_var", &buffer_var);
467  }
468 
469  static constexpr const char* _type_key = "script.ir_builder.tir.AllocateFrame";
471 
472  public:
477  void ExitWithScope() final;
478 };
479 
485 class AllocateFrame : public TIRFrame {
486  public:
488 };
489 
496  public:
507 
510  v->Visit("dtype", &dtype);
511  v->Visit("extents", &extents);
512  v->Visit("data", &data);
513  v->Visit("buffer_var", &buffer_var);
514  v->Visit("annotations", &annotations);
515  }
516 
517  static constexpr const char* _type_key = "script.ir_builder.tir.AllocateConstFrame";
519 
520  public:
525  void ExitWithScope() final;
526 };
527 
533 class AllocateConstFrame : public TIRFrame {
534  public:
537 };
543 class AttrFrameNode : public TIRFrameNode {
544  public:
551 
554  v->Visit("node", &node);
555  v->Visit("attr_key", &attr_key);
556  v->Visit("value", &value);
557  }
558 
559  static constexpr const char* _type_key = "script.ir_builder.tir.AttrFrame";
561 
562  public:
567  void ExitWithScope() final;
568 };
569 
575 class AttrFrame : public TIRFrame {
576  public:
578 };
579 
585 class WhileFrameNode : public TIRFrameNode {
586  public:
589 
592  v->Visit("condition", &condition);
593  }
594 
595  static constexpr const char* _type_key = "script.ir_builder.tir.WhileFrame";
597 
598  public:
603  void ExitWithScope() final;
604 };
605 
611 class WhileFrame : public TIRFrame {
612  public:
614 };
615 
621 class IfFrameNode : public TIRFrameNode {
622  public:
629 
632  v->Visit("condition", &condition);
633  v->Visit("then_stmts", &then_stmts);
634  v->Visit("else_stmts", &else_stmts);
635  }
636 
637  static constexpr const char* _type_key = "script.ir_builder.tir.IfFrame";
639 
640  public:
645  void ExitWithScope() final;
646 };
647 
653 class IfFrame : public TIRFrame {
654  public:
656 };
657 
663 class ThenFrameNode : public TIRFrameNode {
664  public:
665  static constexpr const char* _type_key = "script.ir_builder.tir.ThenFrame";
667 
668  public:
673  void EnterWithScope() final;
678  void ExitWithScope() final;
679 };
680 
686 class ThenFrame : public TIRFrame {
687  public:
689 };
690 
696 class ElseFrameNode : public TIRFrameNode {
697  public:
698  static constexpr const char* _type_key = "script.ir_builder.tir.ElseFrame";
700 
701  public:
706  void EnterWithScope() final;
711  void ExitWithScope() final;
712 };
713 
719 class ElseFrame : public TIRFrame {
720  public:
722 };
723 
725  public:
729  bool allocated;
730 
733  v->Visit("buffer", &buffer);
734  v->Visit("allocated", &allocated);
735  }
736 
737  static constexpr const char* _type_key = "script.ir_builder.tir.DeclBufferFrame";
739 
740  public:
741  void ExitWithScope() final;
742 };
743 
744 class DeclBufferFrame : public TIRFrame {
745  public:
747 };
748 
749 } // namespace tir
750 } // namespace ir_builder
751 } // namespace script
752 } // namespace tvm
753 
754 #endif // TVM_SCRIPT_IR_BUILDER_TIR_FRAME_H_
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
Reference to PrimExprNode.
Definition: expr.h:115
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Runtime primitive data type.
Definition: data_type.h:43
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics,...
Definition: map.h:1271
Managed NDArray. The array is backed by reference counted blocks.
Definition: ndarray.h:51
Base class of all object reference.
Definition: object.h:519
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Reference to string objects.
Definition: string.h:98
void VisitAttrs(tvm::AttrVisitor *v)
Definition: base.h:69
Managed reference to an IRBuilderFrameNode.
Definition: base.h:100
A frame represents the allocate constant.
Definition: frame.h:495
void VisitAttrs(tvm::AttrVisitor *v)
Definition: frame.h:508
tvm::tir::Var buffer_var
The buffer var.
Definition: frame.h:504
Map< String, ObjectRef > annotations
Additional annotations about the allocation.
Definition: frame.h:506
Array< PrimExpr > extents
The extents of the allocate.
Definition: frame.h:500
TVM_DECLARE_FINAL_OBJECT_INFO(AllocateConstFrameNode, TIRFrameNode)
tvm::runtime::NDArray data
The data associated with the constant.
Definition: frame.h:502
void ExitWithScope() final
The method called when exiting RAII scope.
DataType dtype
The data type of the buffer.
Definition: frame.h:498
Managed reference to AllocateConstFrameNode.
Definition: frame.h:533
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(AllocateConstFrame, TIRFrame, AllocateConstFrameNode)
A frame represents the allocate.
Definition: frame.h:444
void VisitAttrs(tvm::AttrVisitor *v)
Definition: frame.h:459
tvm::tir::Var buffer_var
The buffer var.
Definition: frame.h:457
TVM_DECLARE_FINAL_OBJECT_INFO(AllocateFrameNode, TIRFrameNode)
Map< String, ObjectRef > annotations
Additional annotation hints.
Definition: frame.h:455
DataType dtype
The data type of the buffer.
Definition: frame.h:449
PrimExpr condition
The condition.
Definition: frame.h:453
void ExitWithScope() final
The method called when exiting RAII scope.
String storage_scope
The storage scope.
Definition: frame.h:451
Array< PrimExpr > extents
The extents of the allocate.
Definition: frame.h:447
Managed reference to AllocateFrameNode.
Definition: frame.h:485
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(AllocateFrame, TIRFrame, AllocateFrameNode)
A frame that represents the assert statement. Proceeds if the condition is true, otherwise aborts wit...
Definition: frame.h:282
void VisitAttrs(tvm::AttrVisitor *v)
Definition: frame.h:289
TVM_DECLARE_FINAL_OBJECT_INFO(AssertFrameNode, TIRFrameNode)
PrimExpr message
The output error message when the assertion failed.
Definition: frame.h:287
PrimExpr condition
The PrimExpr to test.
Definition: frame.h:285
void ExitWithScope() final
The method called when exiting RAII scope.
Managed reference to AssertFrameNode.
Definition: frame.h:311
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(AssertFrame, TIRFrame, AssertFrameNode)
A frame that represents attribute node.
Definition: frame.h:543
TVM_DECLARE_FINAL_OBJECT_INFO(AttrFrameNode, TIRFrameNode)
String attr_key
Attribute type key.
Definition: frame.h:548
void ExitWithScope() final
The method called when exiting RAII scope.
ObjectRef node
The node to annotate the attribute.
Definition: frame.h:546
void VisitAttrs(tvm::AttrVisitor *v)
Definition: frame.h:552
PrimExpr value
The value of the attribute.
Definition: frame.h:550
Managed reference to AttrFrameNode.
Definition: frame.h:575
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(AttrFrame, TIRFrame, AttrFrameNode)
A frame that represents the block.
Definition: frame.h:125
Optional< tvm::tir::Stmt > init
The init statement of the bolck.
Definition: frame.h:136
String name
The name of the block.
Definition: frame.h:128
void ExitWithScope() final
The method called when exiting RAII scope.
void VisitAttrs(tvm::AttrVisitor *v)
Definition: frame.h:153
Array< tvm::tir::IterVar > iter_vars
The variables of the block.
Definition: frame.h:130
Optional< Array< tvm::tir::BufferRegion > > writes
The write buffer regions of the block.
Definition: frame.h:134
Array< tvm::tir::Buffer > alloc_buffers
The buffer allocated in the block.
Definition: frame.h:138
Optional< Map< String, ObjectRef > > annotations
The annotation of the block.
Definition: frame.h:142
Optional< PrimExpr > predicate
The predicate of the block realization, the block will only be executed when the predicate is true.
Definition: frame.h:149
Array< tvm::tir::MatchBufferRegion > match_buffers
The match buffer regions.
Definition: frame.h:140
Array< PrimExpr > iter_values
The corresponding values of the iter vars.
Definition: frame.h:144
Optional< Array< tvm::tir::BufferRegion > > reads
The read buffer regions of the block.
Definition: frame.h:132
TVM_DECLARE_FINAL_OBJECT_INFO(BlockFrameNode, TIRFrameNode)
bool no_realize
The flag whether to construct BlockRealize or Block.
Definition: frame.h:151
Managed reference to BlockFrameNode.
Definition: frame.h:185
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(BlockFrame, TIRFrame, BlockFrameNode)
A frame that represents the block initialization statment.
Definition: frame.h:195
TVM_DECLARE_FINAL_OBJECT_INFO(BlockInitFrameNode, TIRFrameNode)
void EnterWithScope() final
The method called when entering RAII scope.
void VisitAttrs(tvm::AttrVisitor *v)
Definition: frame.h:197
Managed reference to BlockInitFrameNode.
Definition: frame.h:220
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(BlockInitFrame, TIRFrame, BlockInitFrameNode)
void ExitWithScope() final
The method called when exiting RAII scope.
void VisitAttrs(tvm::AttrVisitor *v)
Definition: frame.h:731
tvm::tir::Buffer buffer
The declared buffer.
Definition: frame.h:727
TVM_DECLARE_FINAL_OBJECT_INFO(DeclBufferFrameNode, TIRFrameNode)
bool allocated
The buffer allocated or not.
Definition: frame.h:729
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(DeclBufferFrame, TIRFrame, DeclBufferFrameNode)
A frame that represents else.
Definition: frame.h:696
void EnterWithScope() final
The method called when entering RAII scope.
TVM_DECLARE_FINAL_OBJECT_INFO(ElseFrameNode, TIRFrameNode)
Managed reference to ElseFrameNode.
Definition: frame.h:719
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(ElseFrame, TIRFrame, ElseFrameNode)
A frame that represents the for loop.
Definition: frame.h:230
void VisitAttrs(tvm::AttrVisitor *v)
Definition: frame.h:248
FMakeForLoop f_make_for_loop
The for loop generating function.
Definition: frame.h:246
Array< Range > doms
The domains of iteration.
Definition: frame.h:244
Array< tvm::tir::Var > vars
The loop variable.
Definition: frame.h:242
void ExitWithScope() final
The method called when exiting RAII scope.
TVM_DECLARE_FINAL_OBJECT_INFO(ForFrameNode, TIRFrameNode)
Managed reference to ForFrameNode.
Definition: frame.h:271
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(ForFrame, TIRFrame, ForFrameNode)
A frame that represents if statement.
Definition: frame.h:621
TVM_DECLARE_FINAL_OBJECT_INFO(IfFrameNode, TIRFrameNode)
Optional< Array< tvm::tir::Stmt > > else_stmts
The stetements in the false branch.
Definition: frame.h:628
void ExitWithScope() final
The method called when exiting RAII scope.
Optional< Array< tvm::tir::Stmt > > then_stmts
The statements in the true branch.
Definition: frame.h:626
PrimExpr condition
The condition of the if statement.
Definition: frame.h:624
void VisitAttrs(tvm::AttrVisitor *v)
Definition: frame.h:630
Managed reference to IfFrameNode.
Definition: frame.h:653
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(IfFrame, TIRFrame, IfFrameNode)
The LaunchThreadFrameNode.
Definition: frame.h:359
void VisitAttrs(tvm::AttrVisitor *v)
Definition: frame.h:368
void ExitWithScope() final
The method called when exiting RAII scope.
tvm::tir::IterVar iter_var
The iteration variable.
Definition: frame.h:366
TVM_DECLARE_FINAL_OBJECT_INFO(LaunchThreadFrameNode, TIRFrameNode)
String attr_key
The attribute key, could be either virtual_thread or thread_extent.
Definition: frame.h:364
PrimExpr extent
The extent of environment thread.
Definition: frame.h:362
Managed reference to LaunchThreadFrameNode.
Definition: frame.h:391
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(LaunchThreadFrame, TIRFrame, LaunchThreadFrameNode)
A frame represents the let binding expression, which binds a var.
Definition: frame.h:321
tvm::tir::Var var
The variable we bind to.
Definition: frame.h:324
PrimExpr value
The value we bind var to.
Definition: frame.h:326
TVM_DECLARE_FINAL_OBJECT_INFO(LetFrameNode, TIRFrameNode)
void ExitWithScope() final
The method called when exiting RAII scope.
void VisitAttrs(tvm::AttrVisitor *v)
Definition: frame.h:328
Managed reference to LetFrameNode.
Definition: frame.h:350
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(LetFrame, TIRFrame, LetFrameNode)
A frame that represents the PrimFunc containing TIR statements.
Definition: frame.h:68
void VisitAttrs(tvm::AttrVisitor *v)
Definition: frame.h:87
void ExitWithScope() final
The method called when exiting RAII scope.
Map< tvm::tir::Var, tvm::tir::Buffer > buffer_map
Maps some parameters to specific Buffer data structures.
Definition: frame.h:79
Map< tvm::tir::Var, tvm::tir::IterVar > env_threads
The variable map bound to thread env.
Definition: frame.h:83
Array< tvm::tir::Buffer > root_alloc_buffers
The buffer allocated in root block.
Definition: frame.h:85
Optional< String > name
The name of the block.
Definition: frame.h:71
static constexpr const char * _type_key
Definition: frame.h:99
TVM_DECLARE_FINAL_OBJECT_INFO(PrimFuncFrameNode, TIRFrameNode)
Array< tvm::tir::Var > args
Function parameters.
Definition: frame.h:73
Optional< Type > ret_type
The return type of the function.
Definition: frame.h:77
bool is_private
Whether the PrimFunc is annotated as private.
Definition: frame.h:75
Map< String, ObjectRef > attrs
Additional attributes storing the meta-data.
Definition: frame.h:81
Managed reference to PrimFuncFrameNode.
Definition: frame.h:115
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(PrimFuncFrame, TIRFrame, PrimFuncFrameNode)
A frame that represents realization.
Definition: frame.h:402
TVM_DECLARE_FINAL_OBJECT_INFO(RealizeFrameNode, TIRFrameNode)
void ExitWithScope() final
The method called when exiting RAII scope.
String storage_scope
The storage scope associated with this realization.
Definition: frame.h:407
PrimExpr condition
The condition expression.
Definition: frame.h:409
void VisitAttrs(tvm::AttrVisitor *v)
Definition: frame.h:411
tvm::tir::BufferRegion buffer_slice
The region of buffer access.
Definition: frame.h:405
Managed reference to RealizeFrameNode.
Definition: frame.h:434
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(RealizeFrame, TIRFrame, RealizeFrameNode)
A base frame that represents the TIR fame with body of statements.
Definition: frame.h:36
Array< tvm::tir::Stmt > stmts
The Stmt within in this frame.
Definition: frame.h:39
TVM_DECLARE_BASE_OBJECT_INFO(TIRFrameNode, IRBuilderFrameNode)
void VisitAttrs(tvm::AttrVisitor *v)
Definition: frame.h:41
static constexpr const char * _type_key
Definition: frame.h:46
Managed reference to TIRFrameNode.
Definition: frame.h:55
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(TIRFrame, IRBuilderFrame, TIRFrameNode)
A frame that represents then.
Definition: frame.h:663
TVM_DECLARE_FINAL_OBJECT_INFO(ThenFrameNode, TIRFrameNode)
void EnterWithScope() final
The method called when entering RAII scope.
Managed reference to ThenFrameNode.
Definition: frame.h:686
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(ThenFrame, TIRFrame, ThenFrameNode)
A frame that represents while loop.
Definition: frame.h:585
TVM_DECLARE_FINAL_OBJECT_INFO(WhileFrameNode, TIRFrameNode)
PrimExpr condition
The termination condition of while.
Definition: frame.h:588
void VisitAttrs(tvm::AttrVisitor *v)
Definition: frame.h:590
void ExitWithScope() final
The method called when exiting RAII scope.
Managed reference to WhileFrameNode.
Definition: frame.h:611
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(WhileFrame, TIRFrame, WhileFrameNode)
Managed reference to BufferRegionNode.
Definition: stmt.h:1166
Buffer is a symbolic n-darray structure. It is a composition of primitive symbolic types,...
Definition: buffer.h:174
Iteration Variable, represents an iteration over an integer interval.
Definition: var.h:315
Container of all statements.
Definition: stmt.h:59
a named variable in TIR
Definition: var.h:89
Var var(std::string name_hint, DataType t=DataType::Int(32))
Construct a new Var expression.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
TIR statements.