-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
stmt.h
1703 lines (1496 loc) · 54.6 KB
/
stmt.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*!
* \file tvm/tir/stmt.h
* \brief TIR statements.
*/
// Acknowledgement: Many low-level stmts originate from Halide.
#ifndef TVM_TIR_STMT_H_
#define TVM_TIR_STMT_H_
#include <tvm/tir/expr.h>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
namespace tvm {
namespace tir {
/*! \brief Base node of all statements. */
class StmtNode : public Object {
public:
/*!
* \brief Span that points to the original source code.
* Reserved debug information.
*/
mutable Span span;
StmtNode() = default;
explicit StmtNode(Span span) : span(span) {}
TVM_OBJECT_ENABLE_SCRIPT_PRINTER();
static constexpr const char* _type_key = "tir.Stmt";
static constexpr const bool _type_has_method_sequal_reduce = true;
static constexpr const bool _type_has_method_shash_reduce = true;
static constexpr const uint32_t _type_child_slots = 15;
TVM_DECLARE_BASE_OBJECT_INFO(StmtNode, Object);
};
/*! \brief Container of all statements */
class Stmt : public ObjectRef {
public:
TVM_DEFINE_OBJECT_REF_METHODS(Stmt, ObjectRef, StmtNode);
};
/*!
* \brief Let binding, bind var to value, then run body.
*/
class LetStmtNode : public StmtNode {
public:
/*! \brief The variable. */
Var var;
/*! \brief The value to be bound. */
PrimExpr value;
/*! \brief The body block. */
Stmt body;
void VisitAttrs(AttrVisitor* v) {
v->Visit("var", &var);
v->Visit("value", &value);
v->Visit("body", &body);
v->Visit("span", &span);
}
bool SEqualReduce(const LetStmtNode* other, SEqualReducer equal) const {
return equal.DefEqual(var, other->var) && equal(value, other->value) &&
equal(body, other->body);
}
void SHashReduce(SHashReducer hash_reduce) const {
hash_reduce.DefHash(var);
hash_reduce(value);
hash_reduce(body);
}
static constexpr const char* _type_key = "tir.LetStmt";
TVM_DECLARE_FINAL_OBJECT_INFO(LetStmtNode, StmtNode);
};
/*!
* \brief Managed reference to LetStmtNode.
* \sa LetStmtNode
*/
class LetStmt : public Stmt {
public:
TVM_DLL LetStmt(Var var, PrimExpr value, Stmt body, Span span = Span());
TVM_DEFINE_OBJECT_REF_METHODS(LetStmt, Stmt, LetStmtNode);
TVM_DEFINE_OBJECT_REF_COW_METHOD(LetStmtNode);
};
/*!
* \brief Define certain auxiliary attribute for the body to be a symbolic value.
* This provide auxiliary information for IR passes that transforms body.
*
* In terms of effect, this is equivalent to Block(Evaluate(value), body).
*
* Examples of possible usage:
* - Bound of function, variables.
* - Hint which block corresponds to a parallel region.
*/
class AttrStmtNode : public StmtNode {
public:
/*! \brief this is attribute about certain node */
ObjectRef node;
/*! \brief the type key of the attribute */
String attr_key;
/*! \brief The attribute value, value is well defined at current scope. */
PrimExpr value;
/*! \brief The body statement to be executed */
Stmt body;
void VisitAttrs(AttrVisitor* v) {
v->Visit("node", &node);
v->Visit("attr_key", &attr_key);
v->Visit("value", &value);
v->Visit("body", &body);
v->Visit("span", &span);
}
bool SEqualReduce(const AttrStmtNode* other, SEqualReducer equal) const {
return equal(node, other->node) && equal(attr_key, other->attr_key) &&
equal(value, other->value) && equal(body, other->body);
}
void SHashReduce(SHashReducer hash_reduce) const {
hash_reduce(node);
hash_reduce(attr_key);
hash_reduce(value);
hash_reduce(body);
}
static constexpr const char* _type_key = "tir.AttrStmt";
TVM_DECLARE_FINAL_OBJECT_INFO(AttrStmtNode, StmtNode);
};
/*!
* \brief Managed reference to AttrStmtNode.
* \sa AttrStmtNode
*/
class AttrStmt : public Stmt {
public:
TVM_DLL AttrStmt(ObjectRef node, String attr_key, PrimExpr value, Stmt body, Span span = Span());
TVM_DEFINE_OBJECT_REF_METHODS(AttrStmt, Stmt, AttrStmtNode);
TVM_DEFINE_OBJECT_REF_COW_METHOD(AttrStmtNode);
};
/*!
* \brief Assert condition, if an error occurs, return the error message.
*/
class AssertStmtNode : public StmtNode {
public:
/*! \brief Condition to be checked. */
PrimExpr condition;
/*! \brief Error message when assertion failed. */
PrimExpr message;
/*!
* \brief Body which this assertion holds true.
* Will be executed after the assertion.
*/
Stmt body;
void VisitAttrs(AttrVisitor* v) {
v->Visit("condition", &condition);
v->Visit("message", &message);
v->Visit("body", &body);
v->Visit("span", &span);
}
bool SEqualReduce(const AssertStmtNode* other, SEqualReducer equal) const {
return equal(condition, other->condition) && equal(message, other->message) &&
equal(body, other->body);
}
void SHashReduce(SHashReducer hash_reduce) const {
hash_reduce(condition);
hash_reduce(message);
hash_reduce(body);
}
static constexpr const char* _type_key = "tir.AssertStmt";
TVM_DECLARE_FINAL_OBJECT_INFO(AssertStmtNode, StmtNode);
};
/*!
* \brief Managed reference to AssertStmtNode.
* \sa AssertStmtNode
*/
class AssertStmt : public Stmt {
public:
TVM_DLL AssertStmt(PrimExpr condition, PrimExpr message, Stmt body, Span span = Span());
TVM_DEFINE_OBJECT_REF_METHODS(AssertStmt, Stmt, AssertStmtNode);
TVM_DEFINE_OBJECT_REF_COW_METHOD(AssertStmtNode);
};
/*!
* \brief Store value to the high dimension buffer.
*
* \code
*
* buffer[i, j] = value;
*
* \endcode
* \sa BufferLoad
*/
class BufferStoreNode : public StmtNode {
public:
/*! \brief The buffer variable. */
Buffer buffer;
/*! \brief The value to be stored. */
PrimExpr value;
/*! \brief The indices location to be stored. */
Array<PrimExpr> indices;
void VisitAttrs(AttrVisitor* v) {
v->Visit("buffer", &buffer);
v->Visit("value", &value);
v->Visit("indices", &indices);
v->Visit("span", &span);
}
bool SEqualReduce(const BufferStoreNode* other, SEqualReducer equal) const {
return equal(buffer, other->buffer) && equal(value, other->value) &&
equal(indices, other->indices);
}
void SHashReduce(SHashReducer hash_reduce) const {
hash_reduce(buffer);
hash_reduce(value);
hash_reduce(indices);
}
static constexpr const char* _type_key = "tir.BufferStore";
TVM_DECLARE_FINAL_OBJECT_INFO(BufferStoreNode, StmtNode);
};
/*!
* \brief Managed reference to BufferStoreNode.
* \sa BufferStoreNode
*/
class BufferStore : public Stmt {
public:
TVM_DLL explicit BufferStore(Buffer buffer, PrimExpr value, Array<PrimExpr> indices,
Span span = Span());
TVM_DEFINE_OBJECT_REF_METHODS(BufferStore, Stmt, BufferStoreNode);
TVM_DEFINE_OBJECT_REF_COW_METHOD(BufferStoreNode);
};
/*!
* \brief Annotate the region where the buffer need to
* be read and write in the body.
* We only need to allocate the space for the corresponding region.
*
* \note There should be at most one BufferRealize for each buffer.
* BufferRealize is not necessary for external buffers,
* since they are assumed to be fully allocated.
*
* \sa BufferLoad, BufferStore
*/
class BufferRealizeNode : public StmtNode {
public:
/*! \brief The buffer variable. */
Buffer buffer;
/*! \brief Bounds to be realized */
Array<Range> bounds;
/*! \brief Only realize if condition holds. */
PrimExpr condition;
/*! \brief The body of realization. */
Stmt body;
void VisitAttrs(AttrVisitor* v) {
v->Visit("buffer", &buffer);
v->Visit("bounds", &bounds);
v->Visit("condition", &condition);
v->Visit("body", &body);
v->Visit("span", &span);
}
bool SEqualReduce(const BufferRealizeNode* other, SEqualReducer equal) const {
return equal(buffer, other->buffer) && equal(bounds, other->bounds) &&
equal(condition, other->condition) && equal(body, other->body);
}
void SHashReduce(SHashReducer hash_reduce) const {
hash_reduce(buffer);
hash_reduce(bounds);
hash_reduce(condition);
hash_reduce(body);
}
BufferRealizeNode() = default;
BufferRealizeNode(Buffer buffer, Array<Range> bounds, PrimExpr condition, Stmt body,
Span span = Span())
: StmtNode(span), buffer(buffer), bounds(bounds), condition(condition), body(body) {}
static constexpr const char* _type_key = "tir.BufferRealize";
TVM_DECLARE_FINAL_OBJECT_INFO(BufferRealizeNode, StmtNode);
};
/*!
* \brief Managed reference to BufferRealizeNode.
* \sa BufferRealizeNode
*/
class BufferRealize : public Stmt {
public:
TVM_DLL explicit BufferRealize(Buffer buffer, Array<Range> bounds, PrimExpr condition, Stmt body,
Span span = Span());
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(BufferRealize, Stmt, BufferRealizeNode);
TVM_DEFINE_OBJECT_REF_COW_METHOD(BufferRealizeNode);
};
/*!
* \brief Store value into mult-dimensional array that will be read by the consumer
* of the producer.
*
* \note This node only appears in high-level DSLs that are built on top of the TIR.
* It should not appear in a valid TIR PrimFunc. A high-level DSL needs to lower
* this node before TIR transformations.
*
* \sa DataProducer
*/
class ProducerStoreNode : public StmtNode {
public:
/*! \brief The producer to store the results into. */
DataProducer producer;
/*! \brief The value to be stored. */
PrimExpr value;
/*! \brief The index arguments of the function. */
Array<PrimExpr> indices;
void VisitAttrs(AttrVisitor* v) {
v->Visit("producer", &producer);
v->Visit("value", &value);
v->Visit("indices", &indices);
v->Visit("span", &span);
}
bool SEqualReduce(const ProducerStoreNode* other, SEqualReducer equal) const {
return equal(producer, other->producer) && equal(value, other->value) &&
equal(indices, other->indices);
}
void SHashReduce(SHashReducer hash_reduce) const {
hash_reduce(producer);
hash_reduce(value);
hash_reduce(indices);
}
static constexpr const char* _type_key = "tir.ProducerStore";
TVM_DECLARE_FINAL_OBJECT_INFO(ProducerStoreNode, StmtNode);
};
/*!
* \brief Managed reference to ProducerStoreNode.
* \sa ProducerStoreNode
*/
class ProducerStore : public Stmt {
public:
TVM_DLL ProducerStore(DataProducer producer, PrimExpr value, Array<PrimExpr> indices,
Span span = Span());
TVM_DEFINE_OBJECT_REF_METHODS(ProducerStore, Stmt, ProducerStoreNode);
TVM_DEFINE_OBJECT_REF_COW_METHOD(ProducerStoreNode);
};
/*!
* \brief Annotate the bounds where the data produced by the producer
* need to be written and read in body.
* We will need to allocate space for the corresponding regions.
*
* \note This node only appears in high-level DSLs that are built on top of the TIR.
* It should not appear in a valid TIR PrimFunc. A high-level DSL needs to lower
* this node before TIR transformations.
*
* \sa DataProducer
*/
class ProducerRealizeNode : public StmtNode {
public:
/*! \brief The producer that produces the data. */
DataProducer producer;
/*! \brief Bounds to be realized. */
Region bounds;
/*! \brief Only realize if condition holds. */
PrimExpr condition;
/*! \brief The body of realization. */
Stmt body;
/*! \brief The storage scope associated with this realization. */
String storage_scope;
void VisitAttrs(AttrVisitor* v) {
v->Visit("producer", &producer);
v->Visit("bounds", &bounds);
v->Visit("condition", &condition);
v->Visit("body", &body);
v->Visit("storage_scope", &storage_scope);
v->Visit("span", &span);
}
bool SEqualReduce(const ProducerRealizeNode* other, SEqualReducer equal) const {
return equal(producer, other->producer) && equal(bounds, other->bounds) &&
equal(condition, other->condition) && equal(body, other->body) &&
equal(storage_scope, other->storage_scope);
}
void SHashReduce(SHashReducer hash_reduce) const {
hash_reduce(producer);
hash_reduce(bounds);
hash_reduce(condition);
hash_reduce(body);
hash_reduce(storage_scope);
}
static constexpr const char* _type_key = "tir.ProducerRealize";
TVM_DECLARE_FINAL_OBJECT_INFO(ProducerRealizeNode, StmtNode);
};
/*!
* \brief Managed reference to ProducerRealizeNode.
* \sa ProducerRealizeNode
*/
class ProducerRealize : public Stmt {
public:
TVM_DLL ProducerRealize(DataProducer producer, Region bounds, PrimExpr condition, Stmt body,
String storage_scope = "", Span span = Span());
TVM_DEFINE_OBJECT_REF_METHODS(ProducerRealize, Stmt, ProducerRealizeNode);
TVM_DEFINE_OBJECT_REF_COW_METHOD(ProducerRealizeNode);
};
/*!
* \brief Allocate a buffer that can be used in body.
*/
class AllocateNode : public StmtNode {
public:
/*! \brief The buffer variable. */
Var buffer_var;
/*! \brief The type of the buffer. */
DataType dtype;
/*! \brief The extents of the buffer. */
Array<PrimExpr> extents;
/*! \brief Only allocate buffer when condition is satisfied. */
PrimExpr condition;
/*! \brief The body to be executed. */
Stmt body;
/*!
* \brief Additional annotations about the allocation.
*
* These annotations can be used as auxiliary hint
* to future transformations.
*/
Map<String, ObjectRef> annotations;
void VisitAttrs(AttrVisitor* v) {
v->Visit("buffer_var", &buffer_var);
v->Visit("dtype", &dtype);
v->Visit("extents", &extents);
v->Visit("condition", &condition);
v->Visit("body", &body);
v->Visit("annotations", &annotations);
v->Visit("span", &span);
}
bool SEqualReduce(const AllocateNode* other, SEqualReducer equal) const {
return equal.DefEqual(buffer_var, other->buffer_var) && equal(dtype, other->dtype) &&
equal(extents, other->extents) && equal(condition, other->condition) &&
equal(body, other->body) && equal(annotations, other->annotations);
}
void SHashReduce(SHashReducer hash_reduce) const {
hash_reduce.DefHash(buffer_var);
hash_reduce(dtype);
hash_reduce(extents);
hash_reduce(condition);
hash_reduce(body);
hash_reduce(annotations);
}
/*!
* \brief If the buffer size is constant, return the size.
* Otherwise return 0.
* \return The result.
*/
int64_t ConstantAllocationSize() const { return ConstantAllocationSize(extents); }
/*!
* \brief If the buffer size is constant, return the size.
* Otherwise return 0.
* \param extents The extents of the buffer.
* \return The result.
*/
TVM_DLL static int64_t ConstantAllocationSize(const Array<PrimExpr>& extents);
static constexpr const char* _type_key = "tir.Allocate";
static constexpr const bool _type_has_method_sequal_reduce = true;
static constexpr const bool _type_has_method_shash_reduce = true;
TVM_DECLARE_FINAL_OBJECT_INFO(AllocateNode, StmtNode);
};
/*!
* \brief Managed reference to AllocateNode.
* \sa AllocateNode
*/
class Allocate : public Stmt {
public:
TVM_DLL Allocate(Var buffer_var, DataType dtype, Array<PrimExpr> extents, PrimExpr condition,
Stmt body, Map<String, ObjectRef> annotations = Map<String, ObjectRef>(),
Span span = Span());
TVM_DEFINE_OBJECT_REF_METHODS(Allocate, Stmt, AllocateNode);
TVM_DEFINE_OBJECT_REF_COW_METHOD(AllocateNode);
};
/*!
* \brief Allocate a buffer that can be used in body.
*/
class AllocateConstNode : public StmtNode {
public:
/*! \brief The buffer variable. */
Var buffer_var;
/*! \brief The optional data associated to the constant.
*/
Optional<runtime::NDArray> data;
/*!
* \brief If the PrimFunc containing the Stmt is added to IRModule, this is an optional index
* to indicate the index within "constants" attribute, that is a Array<NDArray> of IRModule.
*/
Optional<Integer> irmod_storage_idx;
/*! \brief The type of the buffer. */
DataType dtype;
/*! \brief The extents of the buffer. */
Array<PrimExpr> extents;
/*! \brief The body to be executed. */
Stmt body;
/*!
* \brief Additional annotations about the allocation.
*
* These annotations can be used as auxiliary hint
* to future transformations.
*/
Map<String, ObjectRef> annotations;
void VisitAttrs(AttrVisitor* v) {
v->Visit("buffer_var", &buffer_var);
v->Visit("data", &data);
v->Visit("irmod_storage_idx", &irmod_storage_idx);
v->Visit("dtype", &dtype);
v->Visit("extents", &extents);
v->Visit("body", &body);
v->Visit("annotations", &annotations);
v->Visit("span", &span);
}
bool SEqualReduce(const AllocateConstNode* other, SEqualReducer equal) const {
return equal.DefEqual(buffer_var, other->buffer_var) && equal(dtype, other->dtype) &&
equal(extents, other->extents) && equal(data, other->data) && equal(body, other->body) &&
equal(annotations, other->annotations);
}
void SHashReduce(SHashReducer hash_reduce) const {
hash_reduce.DefHash(buffer_var);
hash_reduce(dtype);
hash_reduce(extents);
hash_reduce(body);
hash_reduce(annotations);
hash_reduce(data);
}
/*!
* \brief If the buffer size is constant, return the size.
* Otherwise return 0.
* \return The result.
*/
int64_t ConstantAllocationSize() const { return ConstantAllocationSize(extents); }
/*!
* \brief If the buffer size is constant, return the size.
* Otherwise return 0.
* \param extents The extents of the buffer.
* \return The result.
*/
TVM_DLL static int64_t ConstantAllocationSize(const Array<PrimExpr>& extents);
static constexpr const char* _type_key = "tir.AllocateConst";
static constexpr const bool _type_has_method_sequal_reduce = true;
static constexpr const bool _type_has_method_shash_reduce = true;
TVM_DECLARE_FINAL_OBJECT_INFO(AllocateConstNode, StmtNode);
};
/*!
* \brief Managed reference to AllocateConstNode.
* \sa AllocateConstNode
*/
class AllocateConst : public Stmt {
public:
/* The constructor to create a IRNode with constant data
* depending on the type of ObjectRef, it will either
* create AllocateConstNode with irmod_storage_idx or data
*/
TVM_DLL AllocateConst(Var buffer_var, DataType dtype, Array<PrimExpr> extents,
ObjectRef data_or_idx, Stmt body,
Map<String, ObjectRef> annotations = Map<String, ObjectRef>(),
Span span = Span());
TVM_DEFINE_OBJECT_REF_METHODS(AllocateConst, Stmt, AllocateConstNode);
TVM_DEFINE_OBJECT_REF_COW_METHOD(AllocateConstNode);
};
/*! \brief Declare a buffer that can be used in the body */
class DeclBufferNode : public StmtNode {
public:
/*! \brief The buffer being declared */
Buffer buffer;
/*! \brief The body to be executed */
Stmt body;
void VisitAttrs(AttrVisitor* v) {
v->Visit("buffer", &buffer);
v->Visit("body", &body);
v->Visit("span", &span);
}
bool SEqualReduce(const DeclBufferNode* other, SEqualReducer equal) const {
return equal(buffer, other->buffer) && equal(body, other->body);
}
void SHashReduce(SHashReducer hash_reduce) const {
hash_reduce(buffer);
hash_reduce(body);
}
static constexpr const char* _type_key = "tir.DeclBuffer";
TVM_DECLARE_FINAL_OBJECT_INFO(DeclBufferNode, StmtNode);
};
/*! \brief Managed reference to DeclBufferNode */
class DeclBuffer : public Stmt {
public:
TVM_DLL DeclBuffer(Buffer buffer, Stmt body, Span span = Span());
TVM_DEFINE_OBJECT_REF_METHODS(DeclBuffer, Stmt, DeclBufferNode);
TVM_DEFINE_OBJECT_REF_COW_METHOD(DeclBufferNode);
};
/*!
* \brief The container of seq statement.
* Represent a sequence of statements.
*/
class SeqStmtNode : public StmtNode {
public:
/*! \brief internal sequence content. */
Array<Stmt> seq;
/*! \return get the size of the sequence */
size_t size() const { return seq.size(); }
/*!
* \brief Get the index-th element in the sequence.
*/
Stmt operator[](size_t index) const { return seq[index]; }
void VisitAttrs(AttrVisitor* v) {
v->Visit("seq", &seq);
v->Visit("span", &span);
}
bool SEqualReduce(const SeqStmtNode* other, SEqualReducer equal) const {
return equal(seq, other->seq);
}
void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(seq); }
static constexpr const char* _type_key = "tir.SeqStmt";
TVM_DECLARE_FINAL_OBJECT_INFO(SeqStmtNode, StmtNode);
};
/*!
* \brief Evaluates an expression.
* This is mostly used for putting a Call node into Stmt.
*
* If value do not have side-effect, this node can be safely removed.
*/
class EvaluateNode : public StmtNode {
public:
/*! \brief The expression to be evaluated. */
PrimExpr value;
void VisitAttrs(AttrVisitor* v) {
v->Visit("value", &value);
v->Visit("span", &span);
}
bool SEqualReduce(const EvaluateNode* other, SEqualReducer equal) const {
return equal(value, other->value);
}
void SHashReduce(SHashReducer hash_reduce) const { hash_reduce(value); }
static constexpr const char* _type_key = "tir.Evaluate";
TVM_DECLARE_FINAL_OBJECT_INFO(EvaluateNode, StmtNode);
};
/*!
* \brief Managed reference to EvaluateNode.
* \sa EvaluateNode
*/
class Evaluate : public Stmt {
public:
TVM_DLL explicit Evaluate(PrimExpr value, Span span = Span());
explicit Evaluate(int value, Span span = Span()) : Evaluate(PrimExpr(value), span) {}
TVM_DEFINE_OBJECT_REF_METHODS(Evaluate, Stmt, EvaluateNode);
TVM_DEFINE_OBJECT_REF_COW_METHOD(EvaluateNode);
};
/*! \brief Sequence statement. */
class SeqStmt : public Stmt {
public:
/*!
* \brief Construct SeqStmt.
* \param seq The sequence.
* \param span The location of this object in the source code.
*/
TVM_DLL explicit SeqStmt(Array<Stmt> seq, Span span = Span());
/*! \return get the size of the sequence */
size_t size() const { return operator->()->size(); }
/*!
* \brief Get the index-th element in the sequence.
*/
Stmt operator[](size_t index) const { return (*(operator->()))[index]; }
/*!
* \brief Construct a sequence statement by flattening
* all the arrays and sequences in the arguments
* recursively.
*
* - When an argument is nullptr, it will be ignored.
* - When an argument is an array or a SeqStmt, it will be flattened recursively.
* - A normal Stmt will be appended to the end of the sequence.
*
* \note This function can directly return an element
* if it is the only element in the sequence.
*
* \note If the only argument to this function is a SeqStmt, and if
* no flattening of the SeqStmt is required, then the SeqStmt
* will be returned as-is.
*
* \param seq_args The list of arguments to be flattened.
* \tparam Args arguments
* \return The constructed statement
*/
template <typename... Args>
static Stmt Flatten(Args&&... seq_args) {
Array<Stmt> seq;
runtime::detail::for_each(Flattener(&seq), std::forward<Args>(seq_args)...);
if (seq.empty()) {
return Evaluate(0);
} else if (seq.size() == 1) {
return seq[0];
}
// If the argument is a single SeqStmt argument with no
// flattening or unwrapping required, then we may
// return the SeqStmt as-is.
if constexpr (sizeof...(seq_args) == 1) {
if (auto opt = Flattener::AsSeqStmt(std::forward<Args>(seq_args)...)) {
SeqStmt original = opt.value();
bool all_same = [&]() {
if (original->seq.size() != seq.size()) {
return false;
}
for (size_t i = 0; i < seq.size(); i++) {
if (!original->seq[i].same_as(seq[i])) {
return false;
}
}
return true;
}();
if (all_same) {
return original;
}
}
}
return SeqStmt(seq);
}
/*! \brief Helper class to flatten sequence of arguments into Array. */
class Flattener {
public:
explicit Flattener(Array<Stmt>* seq) : seq_(seq) {}
template <typename T>
static Optional<SeqStmt> AsSeqStmt(const T& t) {
if constexpr (std::is_same_v<T, SeqStmt>) {
return t;
} else if constexpr (!std::is_base_of_v<T, SeqStmt>) {
return NullOpt;
} else if (auto* ptr = t.template as<SeqStmtNode>()) {
return GetRef<SeqStmt>(ptr);
} else {
return NullOpt;
}
}
template <typename T>
void operator()(size_t i, const T& stmt_or_seq) const {
if constexpr (std::is_base_of_v<ObjectRef, T>) {
// Early bail-out, applicable to any ObjectRef
if (!stmt_or_seq.defined()) {
return;
}
}
if constexpr (std::is_same_v<T, SeqStmt>) {
// Static type-checking for a SeqStmt that could be flattened.
(*this)(0, stmt_or_seq->seq);
return;
}
if constexpr (std::is_base_of_v<T, SeqStmt>) {
// Dynamic type-checking for a SeqStmt that could be
// flattened.
if (auto* op = stmt_or_seq.template as<SeqStmtNode>()) {
operator()(0, op->seq);
return;
}
}
if constexpr (std::is_base_of_v<T, Evaluate>) {
// Evaluate(0) is used to represent a no-op, and may be
// generated by previous calls to SeqStmt::Flatten(). These
// should be removed to ensure that Flatten(a+b) is equivalent
// to Flatten(Flatten(a), Flatten(b)).
if (auto* op = stmt_or_seq.template as<EvaluateNode>()) {
if (auto* as_int = op->value.template as<IntImmNode>(); as_int && as_int->value == 0) {
return;
}
}
}
if constexpr (std::is_base_of_v<Stmt, T>) {
// Any other Stmt type just gets appended.
seq_->push_back(stmt_or_seq);
} else {
// Anything else is treated as an iterable of Stmt.
for (auto v : stmt_or_seq) {
this->operator()(0, v);
}
}
}
private:
Array<Stmt>* seq_;
};
TVM_DEFINE_OBJECT_REF_METHODS(SeqStmt, Stmt, SeqStmtNode);
TVM_DEFINE_OBJECT_REF_COW_METHOD(SeqStmtNode);
};
/*!
* \brief IfThenElse statement.
*/
class IfThenElseNode : public StmtNode {
public:
/*! \brief The condition. */
PrimExpr condition;
/*! \brief The branch to be executed when condition is true. */
Stmt then_case;
/*! \brief The branch to be executed when condition is false, can be null. */
Optional<Stmt> else_case;
void VisitAttrs(AttrVisitor* v) {
v->Visit("condition", &condition);
v->Visit("then_case", &then_case);
v->Visit("else_case", &else_case);
v->Visit("span", &span);
}
bool SEqualReduce(const IfThenElseNode* other, SEqualReducer equal) const {
return equal(condition, other->condition) && equal(then_case, other->then_case) &&
equal(else_case, other->else_case);
}
void SHashReduce(SHashReducer hash_reduce) const {
hash_reduce(condition);
hash_reduce(then_case);
hash_reduce(else_case);
}
static constexpr const char* _type_key = "tir.IfThenElse";
TVM_DECLARE_FINAL_OBJECT_INFO(IfThenElseNode, StmtNode);
};
/*!
* \brief Managed reference to IfThenElseNode.
* \sa IfThenElseNode
*/
class IfThenElse : public Stmt {
public:
TVM_DLL IfThenElse(PrimExpr condition, Stmt then_case, Optional<Stmt> else_case = NullOpt,
Span span = Span());
TVM_DEFINE_OBJECT_REF_METHODS(IfThenElse, Stmt, IfThenElseNode);
TVM_DEFINE_OBJECT_REF_COW_METHOD(IfThenElseNode);
};
/*!
* \brief The kind of the loop.
*
* ForKind can change the control flow semantics
* of the loop. So the kind field needs to be considered
* in all TIR passes.
*/
enum class ForKind : int {
/*! \brief default semantics -- serial execution. */
kSerial = 0,
/*! \brief Parallel execution on CPU. */
kParallel = 1,
/*!
* \brief Vector SIMD loop.
* The loop body will be vectorized.
*/
kVectorized = 2,
/*! \brief The loop body must be unrolled. */
kUnrolled = 3,
/*!
* \brief The loop variable is bound to a thread in
* an environment. In the final stage of lowering,
* the loop is simply removed and the loop variable is
* mapped to the corresponding context thread.
*/
kThreadBinding = 4
};
/*!
* \brief A for loop, with possible type annotations.
*
* \code
*
* for (loop_var = min; loop_var < min + extent; ++loop_var) {
* // body
* }
* \endcode
*/
class ForNode : public StmtNode {
public:
/*! \brief The loop variable. */
Var loop_var;
/*! \brief The minimum value of iteration. */
PrimExpr min;
/*! \brief The extent of the iteration. */
PrimExpr extent;
/*! \brief The kind of the for loop. */
ForKind kind;
/*! \brief The body of the for loop. */
Stmt body;
/*!
* \brief Only valid when kind == ForKind::kThreadBinding
* The context thread that this loop variable bounds to.
*/
Optional<IterVar> thread_binding;
/*!
* \brief Additional annotations about the loop.
*
* These annotations can be used as auxiliary hint
* to future transformations. An annotation should
* not change the control flow semantics of the loop
* and can be ignored in most passes.
*/
Map<String, ObjectRef> annotations;
void VisitAttrs(AttrVisitor* v) {
v->Visit("loop_var", &loop_var);
v->Visit("min", &min);
v->Visit("extent", &extent);
v->Visit("kind", &kind);
v->Visit("body", &body);
v->Visit("thread_binding", &thread_binding);
v->Visit("annotations", &annotations);
v->Visit("span", &span);
}