tvm
Loading...
Searching...
No Matches
stmt.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// Acknowledgement: Many low-level stmts originate from Halide.
24#ifndef TVM_TIRX_STMT_H_
25#define TVM_TIRX_STMT_H_
26
27#include <tvm/ffi/reflection/registry.h>
28#include <tvm/ir/prim/expr.h>
29#include <tvm/tirx/buffer.h>
31#include <tvm/tirx/exec_scope.h>
32#include <tvm/tirx/layout.h>
33
34#include <optional>
35#include <string>
36#include <type_traits>
37#include <utility>
38
39namespace tvm {
40namespace tirx {
41
43class StmtNode : public ffi::Object {
44 public:
49 mutable Span span;
50
51 StmtNode() = default;
52 explicit StmtNode(Span span) : span(span) {}
53
54 static void RegisterReflection() {
55 namespace refl = tvm::ffi::reflection;
56 refl::ObjectDef<StmtNode>().def_ro("span", &StmtNode::span,
57 refl::AttachFieldFlag::SEqHashIgnore());
58 }
59
61
62 static constexpr const uint32_t _type_child_slots = 15;
63 TVM_FFI_DECLARE_OBJECT_INFO("tirx.Stmt", StmtNode, ffi::Object);
64};
65
67class Stmt : public ffi::ObjectRef {
68 public:
70};
71
79class BindNode : public StmtNode {
80 public:
85
86 static void RegisterReflection() {
87 namespace refl = tvm::ffi::reflection;
88 refl::ObjectDef<BindNode>()
89 // TODO(tqchen): use SEqHashDefNonRecursive after the next pypi tvm-ffi release
90 .def_ro("var", &BindNode::var, refl::AttachFieldFlag::SEqHashDefRecursive())
91 .def_ro("value", &BindNode::value);
92 }
94};
95
107
118class AttrStmtNode : public StmtNode {
119 public:
121 ffi::Any node;
123 ffi::String attr_key;
128
129 static void RegisterReflection() {
130 namespace refl = tvm::ffi::reflection;
131 refl::ObjectDef<AttrStmtNode>()
132 .def_ro("node", &AttrStmtNode::node)
133 .def_ro("attr_key", &AttrStmtNode::attr_key)
134 .def_ro("value", &AttrStmtNode::value)
135 .def_ro("body", &AttrStmtNode::body);
136 }
138};
139
144class AttrStmt : public Stmt {
145 public:
146 TVM_DLL AttrStmt(ffi::Any node, ffi::String attr_key, PrimExpr value, Stmt body,
147 Span span = Span());
148
151};
152
162class AssertStmtNode : public StmtNode {
163 public:
169 ffi::Array<prim::StringImm> message_parts;
170
171 static void RegisterReflection() {
172 namespace refl = tvm::ffi::reflection;
173 refl::ObjectDef<AssertStmtNode>()
174 .def_ro("condition", &AssertStmtNode::condition)
175 .def_ro("error_kind", &AssertStmtNode::error_kind)
176 .def_ro("message_parts", &AssertStmtNode::message_parts);
177 }
179};
180
185class AssertStmt : public Stmt {
186 public:
188 ffi::Array<prim::StringImm> message_parts, Span span = Span());
189
192};
193
204class BufferStoreNode : public StmtNode {
205 public:
211 ffi::Array<PrimExpr> indices;
212
213 static void RegisterReflection() {
214 namespace refl = tvm::ffi::reflection;
215 refl::ObjectDef<BufferStoreNode>()
216 .def_ro("buffer", &BufferStoreNode::buffer, refl::AttachFieldFlag::SEqHashDefRecursive())
217 .def_ro("value", &BufferStoreNode::value)
218 .def_ro("indices", &BufferStoreNode::indices);
219 }
221};
222
235
237class DeclBufferNode : public StmtNode {
238 public:
243
244 static void RegisterReflection() {
245 namespace refl = tvm::ffi::reflection;
246 refl::ObjectDef<DeclBufferNode>()
247 .def_ro("buffer", &DeclBufferNode::buffer, refl::AttachFieldFlag::SEqHashDefRecursive())
248 .def_ro("data", &DeclBufferNode::data);
249 }
251};
252
260
262class AllocBufferNode : public StmtNode {
263 public:
272 ffi::Map<ffi::String, ffi::Any> annotations;
273
274 static void RegisterReflection() {
275 namespace refl = tvm::ffi::reflection;
276 refl::ObjectDef<AllocBufferNode>()
277 // TODO(tqchen): use SEqHashDefNonRecursive after the next pypi tvm-ffi release
278 .def_ro("buffer", &AllocBufferNode::buffer, refl::AttachFieldFlag::SEqHashDefRecursive())
279 .def_ro("annotations", &AllocBufferNode::annotations);
280 }
282};
283
285class AllocBuffer : public Stmt {
286 public:
288 BufferVar buffer,
289 ffi::Map<ffi::String, ffi::Any> annotations = ffi::Map<ffi::String, ffi::Any>(),
290 Span span = Span());
295 std::optional<int64_t> ConstantAllocationSize() const {
296 int64_t result = 1;
297 for (const PrimExpr& extent : (*this)->buffer->shape) {
298 if (const auto* int_size = extent.as<IntImmNode>()) {
299 result *= int_size->value;
300 } else {
301 return std::nullopt;
302 }
303 }
304 return result;
305 }
306
309};
310
315class SeqStmtNode : public StmtNode {
316 public:
318 ffi::Array<Stmt> seq;
319
321 size_t size() const { return seq.size(); }
325 Stmt operator[](size_t index) const { return seq[index]; }
326
327 static void RegisterReflection() {
328 namespace refl = tvm::ffi::reflection;
329 refl::ObjectDef<SeqStmtNode>().def_ro("seq", &SeqStmtNode::seq);
330 }
332};
333
340class EvaluateNode : public StmtNode {
341 public:
344
345 static void RegisterReflection() {
346 namespace refl = tvm::ffi::reflection;
347 refl::ObjectDef<EvaluateNode>().def_ro("value", &EvaluateNode::value);
348 }
350};
351
356class Evaluate : public Stmt {
357 public:
358 TVM_DLL explicit Evaluate(Expr value, Span span = Span());
359
360 explicit Evaluate(int value, Span span = Span()) : Evaluate(PrimExpr(value), span) {}
361
364};
365
367class SeqStmt : public Stmt {
368 public:
374 TVM_DLL explicit SeqStmt(ffi::Array<Stmt> seq, Span span = Span());
375
377 size_t size() const { return operator->()->size(); }
381 Stmt operator[](size_t index) const { return (*(operator->()))[index]; }
402 template <typename... Args>
403 static Stmt Flatten(Args&&... seq_args) {
404 ffi::Array<Stmt> seq;
405
406 ffi::details::for_each(Flattener(&seq), std::forward<Args>(seq_args)...);
407
408 if (seq.empty()) {
409 return Evaluate(0);
410 } else if (seq.size() == 1) {
411 return seq[0];
412 }
413
414 // If the argument is a single SeqStmt argument with no
415 // flattening or unwrapping required, then we may
416 // return the SeqStmt as-is.
417 if constexpr (sizeof...(seq_args) == 1) {
418 if (auto opt = Flattener::AsSeqStmt(std::forward<Args>(seq_args)...)) {
419 SeqStmt original = opt.value();
420 bool all_same = [&]() {
421 if (original->seq.size() != seq.size()) {
422 return false;
423 }
424 for (size_t i = 0; i < seq.size(); i++) {
425 if (!original->seq[i].same_as(seq[i])) {
426 return false;
427 }
428 }
429 return true;
430 }();
431 if (all_same) {
432 return original;
433 }
434 }
435 }
436
437 return SeqStmt(seq);
438 }
440 class Flattener {
441 public:
442 explicit Flattener(ffi::Array<Stmt>* seq) : seq_(seq) {}
443
444 template <typename T>
445 static ffi::Optional<SeqStmt> AsSeqStmt(const T& t) {
446 if constexpr (std::is_same_v<T, SeqStmt>) {
447 return t;
448 }
449 if constexpr (!std::is_base_of_v<T, SeqStmt>) {
450 return std::nullopt;
451 }
452 if constexpr (std::is_base_of_v<Stmt, T>) {
453 if (const SeqStmtNode* ptr = t.template as<SeqStmtNode>()) {
454 return ffi::GetRef<SeqStmt>(ptr);
455 } else {
456 return std::nullopt;
457 }
458 }
459 return std::nullopt;
460 }
461
462 template <typename T>
463 void operator()(size_t i, const T& stmt_or_seq) const {
464 if constexpr (std::is_base_of_v<ObjectRef, T>) {
465 // Early bail-out, applicable to any ObjectRef
466 if (!stmt_or_seq.defined()) {
467 return;
468 }
469 }
470
471 if constexpr (std::is_same_v<T, SeqStmt>) {
472 // Static type-checking for a SeqStmt that could be flattened.
473 (*this)(0, stmt_or_seq->seq);
474 return;
475 }
476
477 if constexpr (std::is_base_of_v<T, SeqStmt>) {
478 // Dynamic type-checking for a SeqStmt that could be
479 // flattened.
480 if (auto* op = stmt_or_seq.template as<SeqStmtNode>()) {
481 operator()(0, op->seq);
482 return;
483 }
484 }
485
486 if constexpr (std::is_base_of_v<T, Evaluate>) {
487 // Evaluate(0) is used to represent a no-op, and may be
488 // generated by previous calls to SeqStmt::Flatten(). These
489 // should be removed to ensure that Flatten(a+b) is equivalent
490 // to Flatten(Flatten(a), Flatten(b)).
491 if (auto* op = stmt_or_seq.template as<EvaluateNode>()) {
492 if (auto* as_int = op->value.template as<IntImmNode>(); as_int && as_int->value == 0) {
493 return;
494 }
495 }
496 }
497
498 if constexpr (std::is_base_of_v<Stmt, T>) {
499 // Any other Stmt type just gets appended.
500 seq_->push_back(stmt_or_seq);
501 } else {
502 // Anything else is treated as an iterable of Stmt.
503 for (auto v : stmt_or_seq) {
504 this->operator()(0, v);
505 }
506 }
507 }
508
509 private:
510 ffi::Array<Stmt>* seq_;
511 };
512
515};
516
520class IfThenElseNode : public StmtNode {
521 public:
527 ffi::Optional<Stmt> else_case;
528
529 static void RegisterReflection() {
530 namespace refl = tvm::ffi::reflection;
531 refl::ObjectDef<IfThenElseNode>()
532 .def_ro("condition", &IfThenElseNode::condition)
533 .def_ro("then_case", &IfThenElseNode::then_case)
534 .def_ro("else_case", &IfThenElseNode::else_case);
535 }
537};
538
543class IfThenElse : public Stmt {
544 public:
545 TVM_DLL IfThenElse(PrimExpr condition, Stmt then_case,
546 ffi::Optional<Stmt> else_case = std::nullopt, Span span = Span());
547
550};
551
559enum class ForKind : int {
561 kSerial = 0,
563 kParallel = 1,
568 kVectorized = 2,
570 kUnrolled = 3,
578};
579
590class ForNode : public StmtNode {
591 public:
606 ffi::Optional<IterVar> thread_binding;
615 ffi::Map<ffi::String, ffi::Any> annotations;
619 ffi::Optional<PrimExpr> step;
620
621 static void RegisterReflection() {
622 namespace refl = tvm::ffi::reflection;
623 refl::ObjectDef<ForNode>()
624 .def_ro("loop_var", &ForNode::loop_var, refl::AttachFieldFlag::SEqHashDefRecursive())
625 .def_ro("min", &ForNode::min)
626 .def_ro("extent", &ForNode::extent)
627 .def_ro("kind", &ForNode::kind)
628 .def_ro("body", &ForNode::body)
629 .def_ro("thread_binding", &ForNode::thread_binding)
630 .def_ro("annotations", &ForNode::annotations)
631 .def_ro("step", &ForNode::step);
632 }
633
635 bool HasTrivialStep() const;
636
638};
639
644class For : public Stmt {
645 public:
646 TVM_DLL For(PrimVar loop_var, PrimExpr min, PrimExpr extent, ForKind kind, Stmt body,
647 ffi::Optional<IterVar> thread_binding = std::nullopt,
648 ffi::Map<ffi::String, ffi::Any> annotations = {},
649 ffi::Optional<PrimExpr> step = std::nullopt, Span span = Span());
650
653};
654
665class WhileNode : public StmtNode {
666 public:
671
672 static void RegisterReflection() {
673 namespace refl = tvm::ffi::reflection;
674 refl::ObjectDef<WhileNode>()
675 .def_ro("condition", &WhileNode::condition)
676 .def_ro("body", &WhileNode::body);
677 }
679};
680
692
696class ReturnNode : public StmtNode {
697 public:
700
701 static void RegisterReflection() {
702 namespace refl = tvm::ffi::reflection;
703 refl::ObjectDef<ReturnNode>().def_ro("value", &ReturnNode::value);
704 }
705
707};
708
720
724class BreakNode : public StmtNode {
725 public:
726 static void RegisterReflection() {
727 namespace refl = tvm::ffi::reflection;
728 refl::ObjectDef<BreakNode>();
729 }
730
732};
733
745
749class ContinueNode : public StmtNode {
750 public:
751 static void RegisterReflection() {
752 namespace refl = tvm::ffi::reflection;
753 refl::ObjectDef<ContinueNode>();
754 }
755
757};
758
770
780class MatchBufferRegionNode : public ffi::Object {
781 public:
786
787 static void RegisterReflection() {
788 namespace refl = tvm::ffi::reflection;
789 refl::ObjectDef<MatchBufferRegionNode>()
790 .def_ro("buffer", &MatchBufferRegionNode::buffer,
791 refl::AttachFieldFlag::SEqHashDefRecursive())
792 .def_ro("source", &MatchBufferRegionNode::source);
793 }
794
796 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.MatchBufferRegion", MatchBufferRegionNode, ffi::Object);
797};
798
811
833class SBlockNode : public StmtNode {
834 public:
836 ffi::Array<IterVar> iter_vars;
838 ffi::Array<BufferRegion> reads;
840 ffi::Array<BufferRegion> writes;
842 ffi::String name_hint;
844 ffi::Array<BufferVar> alloc_buffers;
846 ffi::Array<MatchBufferRegion> match_buffers;
848 ffi::Map<ffi::String, ffi::Any> annotations;
856 ffi::Optional<Stmt> init;
859
860 static void RegisterReflection() {
861 namespace refl = tvm::ffi::reflection;
862 refl::ObjectDef<SBlockNode>()
863 .def_ro("iter_vars", &SBlockNode::iter_vars, refl::AttachFieldFlag::SEqHashDefRecursive())
864 .def_ro("reads", &SBlockNode::reads)
865 .def_ro("writes", &SBlockNode::writes)
866 .def_ro("name_hint", &SBlockNode::name_hint, refl::AttachFieldFlag::SEqHashIgnore())
867 .def_ro("alloc_buffers", &SBlockNode::alloc_buffers,
868 refl::AttachFieldFlag::SEqHashDefRecursive())
869 .def_ro("match_buffers", &SBlockNode::match_buffers)
870 .def_ro("annotations", &SBlockNode::annotations)
871 .def_ro("init", &SBlockNode::init)
872 .def_ro("body", &SBlockNode::body);
873 }
875};
876
881class SBlock : public Stmt {
882 public:
883 TVM_DLL explicit SBlock(
884 ffi::Array<IterVar> iter_vars, ffi::Array<BufferRegion> reads,
885 ffi::Array<BufferRegion> writes, ffi::String name_hint, Stmt body,
886 ffi::Optional<Stmt> init = std::nullopt,
887 ffi::Array<BufferVar> alloc_buffers = ffi::Array<BufferVar>(),
888 ffi::Array<MatchBufferRegion> match_buffers = ffi::Array<MatchBufferRegion>(),
889 ffi::Map<ffi::String, ffi::Any> annotations = ffi::Map<ffi::String, ffi::Any>(),
890 Span span = Span());
891
892 TVM_DLL explicit SBlock(ffi::String name_hint, Stmt body,
893 ffi::Array<BufferVar> alloc_buffers = ffi::Array<BufferVar>(),
894 Span span = Span());
895
898};
899
904 public:
906 ffi::Array<PrimExpr> iter_values;
914
915 static void RegisterReflection() {
916 namespace refl = tvm::ffi::reflection;
917 refl::ObjectDef<SBlockRealizeNode>()
918 .def_ro("iter_values", &SBlockRealizeNode::iter_values)
919 .def_ro("predicate", &SBlockRealizeNode::predicate)
920 .def_ro("block", &SBlockRealizeNode::block);
921 }
923};
924
929class SBlockRealize : public Stmt {
930 public:
931 TVM_DLL explicit SBlockRealize(ffi::Array<PrimExpr> iter_values, PrimExpr predicate, SBlock block,
932 Span span = Span());
933
936};
937
947 public:
950
951 static void RegisterReflection() {
952 namespace refl = tvm::ffi::reflection;
953 refl::ObjectDef<ScopeIdDefStmtNode>().def_ro("def", &ScopeIdDefStmtNode::def);
954 }
956};
957
966
968namespace attr {
973constexpr const char* compute_scope = "compute_scope";
975constexpr const char* device_id = "device_id";
977constexpr const char* device_scope = "device_scope";
979constexpr const char* device_type = "device_type";
985constexpr const char* extern_scope = "extern_scope";
987constexpr const char* pragma_auto_unroll_max_step = "pragma_auto_unroll_max_step";
989constexpr const char* pragma_import_c = "pragma_import_c";
991constexpr const char* pragma_import_llvm = "pragma_import_llvm";
993constexpr const char* pragma_unroll_explicit = "pragma_unroll_explicit";
995constexpr const char* storage_alignment = "storage_alignment";
997constexpr const char* thread_extent = "thread_extent";
999constexpr const char* kVolatile = "tirx.volatile";
1001constexpr const char* buffer_data_alignment = "buffer_data_alignment";
1003constexpr const char* buffer_allocated_addr = "buffer_allocated_addr";
1004constexpr const char* tensorized_nki_instruction = "tensorized_nki_instruction";
1005
1009constexpr const char* kPersistentKernel = "tirx.persistent_kernel";
1010
1017constexpr const char* kDeviceEntry = "tirx.device_entry";
1018
1024inline bool IsPragmaKey(const std::string& attr_key) {
1025 return attr_key.compare(0, 7, "pragma_") == 0;
1026}
1027
1028} // namespace attr
1036
1037// overload printing of for type.
1038TVM_DLL std::ostream& operator<<(std::ostream& os, ForKind kind);
1039
1040// inline implementations
1041inline const char* ForKind2String(ForKind t) {
1042 switch (t) {
1043 case ForKind::kSerial:
1044 return "serial";
1045 case ForKind::kParallel:
1046 return "parallel";
1048 return "vectorized";
1049 case ForKind::kUnrolled:
1050 return "unroll";
1052 return "thread_binding";
1053 }
1054 TVM_FFI_THROW(InternalError) << "Unknown ForKind" << t;
1056}
1057
1058} // namespace tirx
1059} // namespace tvm
1060#endif // TVM_TIR_STMT_H_
Symbolic n-dimensional array, to represent a memory buffer.
Managed reference to ExprNode.
Definition base_expr.h:335
Constant integer literals in the program.
Definition expr.h:487
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:401
Definition base_expr.h:137
Definition source_map.h:111
Managed reference to VarNode.
Definition expr.h:372
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Managed reference to StringImmNode.
Definition expr.h:68
Allocate a buffer and declare it in scope.
Definition stmt.h:262
BufferVar buffer
The buffer being allocated and declared.
Definition stmt.h:265
ffi::Map< ffi::String, ffi::Any > annotations
Additional annotations about the allocation.
Definition stmt.h:272
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.AllocBuffer", AllocBufferNode, StmtNode)
static void RegisterReflection()
Definition stmt.h:274
Managed reference to AllocBufferNode.
Definition stmt.h:285
AllocBuffer(BufferVar buffer, ffi::Map< ffi::String, ffi::Any > annotations=ffi::Map< ffi::String, ffi::Any >(), Span span=Span())
std::optional< int64_t > ConstantAllocationSize() const
If the buffer's shape is constant, return the total number of elements.
Definition stmt.h:295
TVM_DEFINE_OBJECT_REF_COW_METHOD(AllocBufferNode)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(AllocBuffer, Stmt, AllocBufferNode)
Assert condition, if an error occurs, return the error message.
Definition stmt.h:162
ffi::Array< prim::StringImm > message_parts
Error message fragments, concatenated at runtime when assertion fails.
Definition stmt.h:169
PrimExpr condition
Condition to be checked.
Definition stmt.h:165
prim::StringImm error_kind
The error kind, e.g. "RuntimeError", "TypeError", "ValueError".
Definition stmt.h:167
static void RegisterReflection()
Definition stmt.h:171
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.AssertStmt", AssertStmtNode, StmtNode)
Managed reference to AssertStmtNode.
Definition stmt.h:185
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(AssertStmt, Stmt, AssertStmtNode)
AssertStmt(PrimExpr condition, prim::StringImm error_kind, ffi::Array< prim::StringImm > message_parts, Span span=Span())
TVM_DEFINE_OBJECT_REF_COW_METHOD(AssertStmtNode)
Define certain auxiliary attribute for the body to be a symbolic value. This provide auxiliary inform...
Definition stmt.h:118
static void RegisterReflection()
Definition stmt.h:129
PrimExpr value
The attribute value, value is well defined at current scope.
Definition stmt.h:125
ffi::Any node
this is attribute about certain node
Definition stmt.h:121
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.AttrStmt", AttrStmtNode, StmtNode)
ffi::String attr_key
the type key of the attribute
Definition stmt.h:123
Stmt body
The body statement to be executed.
Definition stmt.h:127
Managed reference to AttrStmtNode.
Definition stmt.h:144
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(AttrStmt, Stmt, AttrStmtNode)
AttrStmt(ffi::Any node, ffi::String attr_key, PrimExpr value, Stmt body, Span span=Span())
TVM_DEFINE_OBJECT_REF_COW_METHOD(AttrStmtNode)
Bind a variable to a value in the enclosing scope.
Definition stmt.h:79
Expr value
The value to bind to the variable.
Definition stmt.h:84
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.Bind", BindNode, StmtNode)
static void RegisterReflection()
Definition stmt.h:86
Var var
The variable being bound.
Definition stmt.h:82
Managed reference to BindNode.
Definition stmt.h:100
Bind(Var var, Expr value, Span span=Span())
TVM_DEFINE_OBJECT_REF_COW_METHOD(BindNode)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Bind, Stmt, BindNode)
A Break in control flow.
Definition stmt.h:724
static void RegisterReflection()
Definition stmt.h:726
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.Break", BreakNode, StmtNode)
Managed reference to BreakNode.
Definition stmt.h:738
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Break, Stmt, BreakNode)
Break(Span span)
TVM_DEFINE_OBJECT_REF_COW_METHOD(BreakNode)
Managed reference to BufferRegionNode.
Definition buffer_region.h:66
Store value to the high dimension buffer.
Definition stmt.h:204
ffi::Array< PrimExpr > indices
The indices location to be stored.
Definition stmt.h:211
static void RegisterReflection()
Definition stmt.h:213
PrimExpr value
The value to be stored.
Definition stmt.h:209
BufferVar buffer
The buffer variable.
Definition stmt.h:207
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.BufferStore", BufferStoreNode, StmtNode)
Managed reference to BufferStoreNode.
Definition stmt.h:227
TVM_DEFINE_OBJECT_REF_COW_METHOD(BufferStoreNode)
BufferStore(BufferVar buffer, PrimExpr value, ffi::Array< PrimExpr > indices, Span span=Span())
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(BufferStore, Stmt, BufferStoreNode)
Checked zero-state view over an ordinary VarNode with BufferType.
Definition buffer.h:179
A Continue in control flow.
Definition stmt.h:749
static void RegisterReflection()
Definition stmt.h:751
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.Continue", ContinueNode, StmtNode)
Managed reference to ContinueNode.
Definition stmt.h:763
TVM_DEFINE_OBJECT_REF_COW_METHOD(ContinueNode)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Continue, Stmt, ContinueNode)
Declare a buffer that can be used in the body.
Definition stmt.h:237
BufferVar buffer
The buffer being declared.
Definition stmt.h:240
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.DeclBuffer", DeclBufferNode, StmtNode)
Expr data
Physical pointer expression backing the declaration.
Definition stmt.h:242
static void RegisterReflection()
Definition stmt.h:244
Managed reference to DeclBufferNode.
Definition stmt.h:254
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(DeclBuffer, Stmt, DeclBufferNode)
DeclBuffer(BufferVar buffer, Expr data, Span span=Span())
TVM_DEFINE_OBJECT_REF_COW_METHOD(DeclBufferNode)
Evaluates an expression. This is mostly used for putting a Call node into Stmt.
Definition stmt.h:340
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.Evaluate", EvaluateNode, StmtNode)
Expr value
The expression to be evaluated.
Definition stmt.h:343
static void RegisterReflection()
Definition stmt.h:345
Managed reference to EvaluateNode.
Definition stmt.h:356
Evaluate(Expr value, Span span=Span())
Evaluate(int value, Span span=Span())
Definition stmt.h:360
TVM_DEFINE_OBJECT_REF_COW_METHOD(EvaluateNode)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Evaluate, Stmt, EvaluateNode)
A for loop, with possible type annotations.
Definition stmt.h:590
PrimExpr min
The minimum value of iteration.
Definition stmt.h:595
ffi::Optional< IterVar > thread_binding
Only valid when kind == ForKind::kThreadBinding The context thread that this loop variable bounds to.
Definition stmt.h:606
ffi::Optional< PrimExpr > step
The loop step. It is one if not specified.
Definition stmt.h:619
bool HasTrivialStep() const
Check it is a loop without nontrivial loop step.
Stmt body
The body of the for loop.
Definition stmt.h:601
PrimExpr extent
The extent of the iteration.
Definition stmt.h:597
ForKind kind
The kind of the for loop.
Definition stmt.h:599
ffi::Map< ffi::String, ffi::Any > annotations
Additional annotations about the loop.
Definition stmt.h:615
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.For", ForNode, StmtNode)
PrimVar loop_var
The loop variable.
Definition stmt.h:593
static void RegisterReflection()
Definition stmt.h:621
Managed reference to ForNode.
Definition stmt.h:644
For(PrimVar loop_var, PrimExpr min, PrimExpr extent, ForKind kind, Stmt body, ffi::Optional< IterVar > thread_binding=std::nullopt, ffi::Map< ffi::String, ffi::Any > annotations={}, ffi::Optional< PrimExpr > step=std::nullopt, Span span=Span())
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(For, Stmt, ForNode)
TVM_DEFINE_OBJECT_REF_COW_METHOD(ForNode)
IfThenElse statement.
Definition stmt.h:520
static void RegisterReflection()
Definition stmt.h:529
ffi::Optional< Stmt > else_case
The branch to be executed when condition is false, can be null.
Definition stmt.h:527
PrimExpr condition
The condition.
Definition stmt.h:523
Stmt then_case
The branch to be executed when condition is true.
Definition stmt.h:525
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.IfThenElse", IfThenElseNode, StmtNode)
Managed reference to IfThenElseNode.
Definition stmt.h:543
TVM_DEFINE_OBJECT_REF_COW_METHOD(IfThenElseNode)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(IfThenElse, Stmt, IfThenElseNode)
IfThenElse(PrimExpr condition, Stmt then_case, ffi::Optional< Stmt > else_case=std::nullopt, Span span=Span())
Match introduces a constraint that the source buffer region can be remapped to the data layout specif...
Definition stmt.h:780
BufferRegion source
The source buffer region.
Definition stmt.h:785
static void RegisterReflection()
Definition stmt.h:787
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition stmt.h:795
BufferVar buffer
The target buffer.
Definition stmt.h:783
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.MatchBufferRegion", MatchBufferRegionNode, ffi::Object)
Managed reference to MatchBufferRegionNode.
Definition stmt.h:803
TVM_DEFINE_OBJECT_REF_COW_METHOD(MatchBufferRegionNode)
MatchBufferRegion(BufferVar buffer, BufferRegion source)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(MatchBufferRegion, ffi::ObjectRef, MatchBufferRegionNode)
Checked scalar view over a VarNode.
Definition var.h:46
A return from the current function.
Definition stmt.h:696
static void RegisterReflection()
Definition stmt.h:701
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.Return", ReturnNode, StmtNode)
Expr value
The value to return.
Definition stmt.h:699
Managed reference to ReturnNode.
Definition stmt.h:713
TVM_DEFINE_OBJECT_REF_COW_METHOD(ReturnNode)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Return, Stmt, ReturnNode)
Return(Expr value, Span span=Span())
A block is a basic schedule unit in TIR.
Definition stmt.h:833
Stmt body
The body of the block.
Definition stmt.h:858
ffi::Array< BufferRegion > reads
The read buffer regions of the block.
Definition stmt.h:838
static void RegisterReflection()
Definition stmt.h:860
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.SBlock", SBlockNode, StmtNode)
ffi::Array< MatchBufferRegion > match_buffers
The match buffer regions.
Definition stmt.h:846
ffi::String name_hint
The name_hint of the block.
Definition stmt.h:842
ffi::Optional< Stmt > init
The init statement is executed during the first iteration of reduction loops in a reduction block....
Definition stmt.h:856
ffi::Array< BufferVar > alloc_buffers
The buffer allocated in the block.
Definition stmt.h:844
ffi::Array< IterVar > iter_vars
The variables of the block.
Definition stmt.h:836
ffi::Array< BufferRegion > writes
The write buffer regions of the block.
Definition stmt.h:840
ffi::Map< ffi::String, ffi::Any > annotations
The annotation of the block.
Definition stmt.h:848
A block realization node represents execution of the block at the binding values.
Definition stmt.h:903
static void RegisterReflection()
Definition stmt.h:915
PrimExpr predicate
The predicate of the block realization, the block will only be executed when the predicate is true.
Definition stmt.h:911
ffi::Array< PrimExpr > iter_values
The corresponding values of the iter vars.
Definition stmt.h:906
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.SBlockRealize", SBlockRealizeNode, StmtNode)
SBlock block
The block to be realized.
Definition stmt.h:913
Managed reference to BlockRealizeNode.
Definition stmt.h:929
TVM_DEFINE_OBJECT_REF_COW_METHOD(SBlockRealizeNode)
SBlockRealize(ffi::Array< PrimExpr > iter_values, PrimExpr predicate, SBlock block, Span span=Span())
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(SBlockRealize, Stmt, SBlockRealizeNode)
Managed reference to SBlockNode.
Definition stmt.h:881
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(SBlock, Stmt, SBlockNode)
SBlock(ffi::String name_hint, Stmt body, ffi::Array< BufferVar > alloc_buffers=ffi::Array< BufferVar >(), Span span=Span())
TVM_DEFINE_OBJECT_REF_COW_METHOD(SBlockNode)
SBlock(ffi::Array< IterVar > iter_vars, ffi::Array< BufferRegion > reads, ffi::Array< BufferRegion > writes, ffi::String name_hint, Stmt body, ffi::Optional< Stmt > init=std::nullopt, ffi::Array< BufferVar > alloc_buffers=ffi::Array< BufferVar >(), ffi::Array< MatchBufferRegion > match_buffers=ffi::Array< MatchBufferRegion >(), ffi::Map< ffi::String, ffi::Any > annotations=ffi::Map< ffi::String, ffi::Any >(), Span span=Span())
Standalone statement that declares a scope-id binding (e.g. cta_id, warp_id, lane_id)....
Definition stmt.h:946
ScopeIdDef def
The scope-id definition (Vars + extents + binding).
Definition stmt.h:949
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.ScopeIdDefStmt", ScopeIdDefStmtNode, StmtNode)
static void RegisterReflection()
Definition stmt.h:951
Managed reference to ScopeIdDefStmtNode.
Definition stmt.h:959
ScopeIdDefStmt(ScopeIdDef def, Span span=Span())
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(ScopeIdDefStmt, Stmt, ScopeIdDefStmtNode)
TVM_DEFINE_OBJECT_REF_COW_METHOD(ScopeIdDefStmtNode)
Definition exec_scope.h:137
The container of seq statement. Represent a sequence of statements.
Definition stmt.h:315
size_t size() const
Definition stmt.h:321
Stmt operator[](size_t index) const
Get the index-th element in the sequence.
Definition stmt.h:325
ffi::Array< Stmt > seq
internal sequence content.
Definition stmt.h:318
static void RegisterReflection()
Definition stmt.h:327
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.SeqStmt", SeqStmtNode, StmtNode)
Helper class to flatten sequence of arguments into Array.
Definition stmt.h:440
static ffi::Optional< SeqStmt > AsSeqStmt(const T &t)
Definition stmt.h:445
void operator()(size_t i, const T &stmt_or_seq) const
Definition stmt.h:463
Flattener(ffi::Array< Stmt > *seq)
Definition stmt.h:442
Sequence statement.
Definition stmt.h:367
TVM_DEFINE_OBJECT_REF_COW_METHOD(SeqStmtNode)
Stmt operator[](size_t index) const
Get the index-th element in the sequence.
Definition stmt.h:381
static Stmt Flatten(Args &&... seq_args)
Construct a sequence statement by flattening all the arrays and sequences in the arguments recursivel...
Definition stmt.h:403
size_t size() const
Definition stmt.h:377
SeqStmt(ffi::Array< Stmt > seq, Span span=Span())
Construct SeqStmt.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(SeqStmt, Stmt, SeqStmtNode)
Base node of all statements.
Definition stmt.h:43
static constexpr const uint32_t _type_child_slots
Definition stmt.h:62
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition stmt.h:60
Span span
Span that points to the original source code. Reserved debug information.
Definition stmt.h:49
StmtNode(Span span)
Definition stmt.h:52
TVM_FFI_DECLARE_OBJECT_INFO("tirx.Stmt", StmtNode, ffi::Object)
static void RegisterReflection()
Definition stmt.h:54
Container of all statements.
Definition stmt.h:67
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Stmt, ffi::ObjectRef, StmtNode)
A While loop.
Definition stmt.h:665
static void RegisterReflection()
Definition stmt.h:672
PrimExpr condition
The termination condition.
Definition stmt.h:668
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("tirx.While", WhileNode, StmtNode)
Stmt body
The body of the while loop.
Definition stmt.h:670
Managed reference to WhileNode.
Definition stmt.h:685
TVM_DEFINE_OBJECT_REF_COW_METHOD(WhileNode)
While(PrimExpr condition, Stmt body, Span span=Span())
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(While, Stmt, WhileNode)
TIR expressions.
Definition of layout.
constexpr const char * thread_extent
Mark launching extent of thread, used by device API.
Definition stmt.h:997
bool IsPragmaKey(const std::string &attr_key)
Check if attr_key is a pragma key extension.
Definition stmt.h:1024
constexpr const char * pragma_unroll_explicit
Pragma: unroll explicit.
Definition stmt.h:993
constexpr const char * storage_alignment
Mark storage alignment requirement of buffers.
Definition stmt.h:995
constexpr const char * buffer_data_alignment
Mark buffer initial addr alignment in bytes.
Definition stmt.h:1001
constexpr const char * kPersistentKernel
Mark the kernel as persistent.
Definition stmt.h:1009
constexpr const char * device_id
The allocation device for global malloc in host.
Definition stmt.h:975
constexpr const char * pragma_auto_unroll_max_step
Pragma: auto-unroll, max_step.
Definition stmt.h:987
constexpr const char * tensorized_nki_instruction
Definition stmt.h:1004
constexpr const char * pragma_import_llvm
Import llvm source or file into the final code gen module.
Definition stmt.h:991
constexpr const char * pragma_import_c
Import C source or file into the final code gen module.
Definition stmt.h:989
constexpr const char * compute_scope
Mark the scope as when computation start to happen. This can hint some code generator to create a new...
Definition stmt.h:973
constexpr const char * buffer_allocated_addr
Mark buffer allocated addr in bytes.
Definition stmt.h:1003
constexpr const char * device_type
The device type.
Definition stmt.h:979
constexpr const char * kVolatile
Annotation key on AllocBuffer marking the allocation as volatile.
Definition stmt.h:999
constexpr const char * device_scope
Mark that it is in the device scope.
Definition stmt.h:977
constexpr const char * extern_scope
Mark the scope as generated by extern primitive. Such scope can contain arbitrary ir program and we n...
Definition stmt.h:985
constexpr const char * kDeviceEntry
Mark the device-region entry within a PrimFunc body. The AttrStmt so-keyed has a body that is the dev...
Definition stmt.h:1017
@ kUnrolled
The execution is unrolled.
Definition var.h:128
@ kVectorized
The loop is vectorized.
Definition var.h:132
const char * ForKind2String(ForKind t)
Definition stmt.h:1041
std::ostream & operator<<(std::ostream &os, CallEffectKind side_effect)
Definition op_attr_types.h:140
PrimExpr TypeAnnotation(PrimType dtype, Span span=Span())
Create a type annotation expression.
const Op & min()
ForKind
The kind of the loop.
Definition stmt.h:559
@ kThreadBinding
The loop variable is bound to a thread in an environment. In the final stage of lowering,...
@ kUnrolled
The loop body must be unrolled.
@ kParallel
Parallel execution on CPU.
@ kVectorized
Vector SIMD loop. The loop body will be vectorized.
@ kSerial
default semantics – serial execution.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40