-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathIR.cpp
More file actions
1439 lines (1294 loc) · 44.5 KB
/
IR.cpp
File metadata and controls
1439 lines (1294 loc) · 44.5 KB
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
#include "IR.h"
#include "IRMutator.h"
#include "IROperator.h"
#include "IRPrinter.h"
#include "IRVisitor.h"
#include <numeric>
#include <utility>
namespace Halide {
namespace Internal {
Expr Cast::make(Type t, Expr v) {
internal_assert(v.defined()) << "Cast of undefined\n";
internal_assert(t.lanes() == v.type().lanes()) << "Cast may not change vector widths\n";
Cast *node = new Cast;
node->type = t;
node->value = std::move(v);
return node;
}
Expr Reinterpret::make(Type t, Expr v) {
user_assert(v.defined()) << "reinterpret of undefined Expr\n";
int from_bits = v.type().bits() * v.type().lanes();
int to_bits = t.bits() * t.lanes();
user_assert(from_bits == to_bits)
<< "Reinterpret cast from type " << v.type()
<< " which has " << from_bits
<< " bits, to type " << t
<< " which has " << to_bits << " bits\n";
Reinterpret *node = new Reinterpret;
node->type = t;
node->value = std::move(v);
return node;
}
Expr Add::make(Expr a, Expr b) {
internal_assert(a.defined()) << "Add of undefined\n";
internal_assert(b.defined()) << "Add of undefined\n";
internal_assert(a.type() == b.type()) << "Add of mismatched types\n";
Add *node = new Add;
node->type = a.type();
node->a = std::move(a);
node->b = std::move(b);
return node;
}
Expr Sub::make(Expr a, Expr b) {
internal_assert(a.defined()) << "Sub of undefined\n";
internal_assert(b.defined()) << "Sub of undefined\n";
internal_assert(a.type() == b.type()) << "Sub of mismatched types\n";
Sub *node = new Sub;
node->type = a.type();
node->a = std::move(a);
node->b = std::move(b);
return node;
}
Expr Mul::make(Expr a, Expr b) {
internal_assert(a.defined()) << "Mul of undefined\n";
internal_assert(b.defined()) << "Mul of undefined\n";
internal_assert(a.type() == b.type()) << "Mul of mismatched types\n";
Mul *node = new Mul;
node->type = a.type();
node->a = std::move(a);
node->b = std::move(b);
return node;
}
Expr Div::make(Expr a, Expr b) {
internal_assert(a.defined()) << "Div of undefined\n";
internal_assert(b.defined()) << "Div of undefined\n";
internal_assert(a.type() == b.type()) << "Div of mismatched types\n";
Div *node = new Div;
node->type = a.type();
node->a = std::move(a);
node->b = std::move(b);
return node;
}
Expr Mod::make(Expr a, Expr b) {
internal_assert(a.defined()) << "Mod of undefined\n";
internal_assert(b.defined()) << "Mod of undefined\n";
internal_assert(a.type() == b.type()) << "Mod of mismatched types\n";
Mod *node = new Mod;
node->type = a.type();
node->a = std::move(a);
node->b = std::move(b);
return node;
}
Expr Min::make(Expr a, Expr b) {
internal_assert(a.defined()) << "Min of undefined\n";
internal_assert(b.defined()) << "Min of undefined\n";
internal_assert(a.type() == b.type()) << "Min of mismatched types\n";
Min *node = new Min;
node->type = a.type();
node->a = std::move(a);
node->b = std::move(b);
return node;
}
Expr Max::make(Expr a, Expr b) {
internal_assert(a.defined()) << "Max of undefined\n";
internal_assert(b.defined()) << "Max of undefined\n";
internal_assert(a.type() == b.type()) << "Max of mismatched types\n";
Max *node = new Max;
node->type = a.type();
node->a = std::move(a);
node->b = std::move(b);
return node;
}
Expr EQ::make(Expr a, Expr b) {
internal_assert(a.defined()) << "EQ of undefined\n";
internal_assert(b.defined()) << "EQ of undefined\n";
internal_assert(a.type() == b.type()) << "EQ of mismatched types\n";
EQ *node = new EQ;
node->type = Bool(a.type().lanes());
node->a = std::move(a);
node->b = std::move(b);
return node;
}
Expr NE::make(Expr a, Expr b) {
internal_assert(a.defined()) << "NE of undefined\n";
internal_assert(b.defined()) << "NE of undefined\n";
internal_assert(a.type() == b.type()) << "NE of mismatched types\n";
NE *node = new NE;
node->type = Bool(a.type().lanes());
node->a = std::move(a);
node->b = std::move(b);
return node;
}
Expr LT::make(Expr a, Expr b) {
internal_assert(a.defined()) << "LT of undefined\n";
internal_assert(b.defined()) << "LT of undefined\n";
internal_assert(a.type() == b.type()) << "LT of mismatched types\n";
LT *node = new LT;
node->type = Bool(a.type().lanes());
node->a = std::move(a);
node->b = std::move(b);
return node;
}
Expr LE::make(Expr a, Expr b) {
internal_assert(a.defined()) << "LE of undefined\n";
internal_assert(b.defined()) << "LE of undefined\n";
internal_assert(a.type() == b.type()) << "LE of mismatched types\n";
LE *node = new LE;
node->type = Bool(a.type().lanes());
node->a = std::move(a);
node->b = std::move(b);
return node;
}
Expr GT::make(Expr a, Expr b) {
internal_assert(a.defined()) << "GT of undefined\n";
internal_assert(b.defined()) << "GT of undefined\n";
internal_assert(a.type() == b.type()) << "GT of mismatched types\n";
GT *node = new GT;
node->type = Bool(a.type().lanes());
node->a = std::move(a);
node->b = std::move(b);
return node;
}
Expr GE::make(Expr a, Expr b) {
internal_assert(a.defined()) << "GE of undefined\n";
internal_assert(b.defined()) << "GE of undefined\n";
internal_assert(a.type() == b.type()) << "GE of mismatched types\n";
GE *node = new GE;
node->type = Bool(a.type().lanes());
node->a = std::move(a);
node->b = std::move(b);
return node;
}
Expr And::make(Expr a, Expr b) {
internal_assert(a.defined()) << "And of undefined\n";
internal_assert(b.defined()) << "And of undefined\n";
internal_assert(a.type().is_bool()) << "lhs of And is not a bool\n";
internal_assert(b.type().is_bool()) << "rhs of And is not a bool\n";
internal_assert(a.type() == b.type()) << "And of mismatched types\n";
And *node = new And;
node->type = Bool(a.type().lanes());
node->a = std::move(a);
node->b = std::move(b);
return node;
}
Expr Or::make(Expr a, Expr b) {
internal_assert(a.defined()) << "Or of undefined\n";
internal_assert(b.defined()) << "Or of undefined\n";
internal_assert(a.type().is_bool()) << "lhs of Or is not a bool\n";
internal_assert(b.type().is_bool()) << "rhs of Or is not a bool\n";
internal_assert(a.type() == b.type()) << "Or of mismatched types\n";
Or *node = new Or;
node->type = Bool(a.type().lanes());
node->a = std::move(a);
node->b = std::move(b);
return node;
}
Expr Not::make(Expr a) {
internal_assert(a.defined()) << "Not of undefined\n";
internal_assert(a.type().is_bool()) << "argument of Not is not a bool\n";
Not *node = new Not;
node->type = Bool(a.type().lanes());
node->a = std::move(a);
return node;
}
Expr Select::make(Expr condition, Expr true_value, Expr false_value) {
internal_assert(condition.defined()) << "Select of undefined\n";
internal_assert(true_value.defined()) << "Select of undefined\n";
internal_assert(false_value.defined()) << "Select of undefined\n";
internal_assert(condition.type().is_bool()) << "First argument to Select is not a bool: " << condition.type() << "\n";
internal_assert(false_value.type() == true_value.type()) << "Select of mismatched types\n";
internal_assert(condition.type().lanes() == true_value.type().lanes())
<< "In Select, vector lanes of condition must be equal to vector lanes of arguments\n";
Select *node = new Select;
node->type = true_value.type();
node->condition = std::move(condition);
node->true_value = std::move(true_value);
node->false_value = std::move(false_value);
return node;
}
Expr Load::make(Type type, const std::string &name, Expr index, Buffer<> image, Parameter param, Expr predicate, ModulusRemainder alignment) {
internal_assert(predicate.defined()) << "Load with undefined predicate\n";
internal_assert(index.defined()) << "Load of undefined\n";
internal_assert(type.lanes() == index.type().lanes()) << "Vector lanes of Load must match vector lanes of index\n";
internal_assert(type.lanes() == predicate.type().lanes())
<< "Vector lanes of Load must match vector lanes of predicate\n";
Load *node = new Load;
node->type = type;
node->name = name;
node->predicate = std::move(predicate);
node->index = std::move(index);
node->image = std::move(image);
node->param = std::move(param);
node->alignment = alignment;
return node;
}
Expr Ramp::make(Expr base, Expr stride, int lanes) {
internal_assert(base.defined()) << "Ramp of undefined\n";
internal_assert(stride.defined()) << "Ramp of undefined\n";
internal_assert(lanes > 1) << "Ramp of lanes <= 1\n";
internal_assert(stride.type() == base.type()) << "Ramp of mismatched types\n";
Ramp *node = new Ramp;
node->type = base.type().with_lanes(lanes * base.type().lanes());
node->base = std::move(base);
node->stride = std::move(stride);
node->lanes = lanes;
return node;
}
Expr Broadcast::make(Expr value, int lanes) {
internal_assert(value.defined()) << "Broadcast of undefined\n";
internal_assert(lanes != 1) << "Broadcast of lanes 1\n";
Broadcast *node = new Broadcast;
node->type = value.type().with_lanes(lanes * value.type().lanes());
node->value = std::move(value);
node->lanes = lanes;
return node;
}
Expr Let::make(const std::string &name, Expr value, Expr body) {
internal_assert(value.defined()) << "Let of undefined\n";
internal_assert(body.defined()) << "Let of undefined\n";
Let *node = new Let;
node->type = body.type();
node->name = name;
node->value = std::move(value);
node->body = std::move(body);
return node;
}
Stmt LetStmt::make(const std::string &name, Expr value, Stmt body) {
internal_assert(value.defined()) << "Let of undefined\n";
internal_assert(body.defined()) << "Let of undefined\n";
LetStmt *node = new LetStmt;
node->name = name;
node->value = std::move(value);
node->body = std::move(body);
return node;
}
Stmt AssertStmt::make(Expr condition, Expr message) {
internal_assert(condition.defined()) << "AssertStmt of undefined\n";
internal_assert(message.type() == Int(32)) << "AssertStmt message must be an int:" << message << "\n";
AssertStmt *node = new AssertStmt;
node->condition = std::move(condition);
node->message = std::move(message);
return node;
}
Stmt ProducerConsumer::make(const std::string &name, bool is_producer, Stmt body) {
internal_assert(body.defined()) << "ProducerConsumer of undefined\n";
ProducerConsumer *node = new ProducerConsumer;
node->name = name;
node->is_producer = is_producer;
node->body = std::move(body);
return node;
}
Stmt ProducerConsumer::make_produce(const std::string &name, Stmt body) {
return ProducerConsumer::make(name, true, std::move(body));
}
Stmt ProducerConsumer::make_consume(const std::string &name, Stmt body) {
return ProducerConsumer::make(name, false, std::move(body));
}
Stmt For::make(const std::string &name,
Expr min, Expr max,
ForType for_type, Partition partition_policy,
DeviceAPI device_api,
Stmt body) {
internal_assert(min.defined()) << "For of undefined\n";
internal_assert(max.defined()) << "For of undefined\n";
internal_assert(min.type() == Int(32)) << "For with non-integer min\n";
internal_assert(max.type() == Int(32)) << "For with non-integer max\n";
internal_assert(body.defined()) << "For of undefined\n";
For *node = new For;
node->name = name;
node->min = std::move(min);
node->max = std::move(max);
node->for_type = for_type;
node->partition_policy = partition_policy;
node->device_api = device_api;
node->body = std::move(body);
return node;
}
Stmt Acquire::make(Expr semaphore, Expr count, Stmt body) {
internal_assert(semaphore.defined()) << "Acquire with undefined semaphore\n";
internal_assert(body.defined()) << "Acquire with undefined body\n";
Acquire *node = new Acquire;
node->semaphore = std::move(semaphore);
node->count = std::move(count);
node->body = std::move(body);
return node;
}
Stmt Store::make(const std::string &name, Expr value, Expr index, Parameter param, Expr predicate, ModulusRemainder alignment) {
internal_assert(predicate.defined()) << "Store with undefined predicate\n";
internal_assert(value.defined()) << "Store of undefined\n";
internal_assert(index.defined()) << "Store of undefined\n";
internal_assert(value.type().lanes() == index.type().lanes()) << "Vector lanes of Store must match vector lanes of index\n";
internal_assert(value.type().lanes() == predicate.type().lanes())
<< "Vector lanes of Store must match vector lanes of predicate\n";
Store *node = new Store;
node->name = name;
node->predicate = std::move(predicate);
node->value = std::move(value);
node->index = std::move(index);
node->param = std::move(param);
node->alignment = alignment;
return node;
}
Stmt Provide::make(const std::string &name, const std::vector<Expr> &values, const std::vector<Expr> &args, const Expr &predicate) {
internal_assert(predicate.defined()) << "Provide with undefined predicate\n";
internal_assert(!values.empty()) << "Provide of no values\n";
for (const auto &value : values) {
internal_assert(value.defined()) << "Provide of undefined value\n";
}
for (const auto &arg : args) {
internal_assert(arg.defined()) << "Provide to undefined location\n";
}
Provide *node = new Provide;
node->name = name;
node->values = values;
node->args = args;
node->predicate = predicate;
return node;
}
Stmt Allocate::make(const std::string &name, Type type, MemoryType memory_type,
const std::vector<Expr> &extents,
Expr condition, Stmt body,
Expr new_expr, const std::string &free_function, int padding) {
for (const auto &extent : extents) {
internal_assert(extent.defined()) << "Allocate of undefined extent\n";
internal_assert(extent.type().is_scalar() == 1) << "Allocate of vector extent\n";
}
internal_assert(body.defined()) << "Allocate of undefined\n";
internal_assert(condition.defined()) << "Allocate with undefined condition\n";
internal_assert(condition.type().is_bool()) << "Allocate condition is not boolean\n";
internal_assert(!(new_expr.defined() && padding))
<< "Allocate nodes with custom new expressions may not have padding\n";
Allocate *node = new Allocate;
node->name = name;
node->type = type;
node->memory_type = memory_type;
node->extents = extents;
node->new_expr = std::move(new_expr);
node->free_function = free_function;
node->condition = std::move(condition);
node->padding = padding;
node->body = std::move(body);
return node;
}
int32_t Allocate::constant_allocation_size(const std::vector<Expr> &extents, const std::string &name) {
int64_t result = 1;
for (const auto &extent : extents) {
if (const IntImm *int_size = extent.as<IntImm>()) {
// Check if the individual dimension is > 2^31 - 1. Not
// currently necessary because it's an int32_t, which is
// always smaller than 2^31 - 1. If we ever upgrade the
// type of IntImm but not the maximum allocation size, we
// should re-enable this.
/*
if ((int64_t)int_size->value > (((int64_t)(1)<<31) - 1)) {
user_error
<< "Dimension " << i << " for allocation " << name << " has size " <<
int_size->value << " which is greater than 2^31 - 1.";
}
*/
result *= int_size->value;
if (result > (static_cast<int64_t>(1) << 31) - 1) {
user_error
<< "Total size for allocation " << name
<< " is constant but exceeds 2^31 - 1.\n";
}
} else {
return 0;
}
}
return static_cast<int32_t>(result);
}
int32_t Allocate::constant_allocation_size() const {
return Allocate::constant_allocation_size(extents, name);
}
Stmt Free::make(const std::string &name) {
Free *node = new Free;
node->name = name;
return node;
}
Stmt Realize::make(const std::string &name, const std::vector<Type> &types, MemoryType memory_type, const Region &bounds, Expr condition, Stmt body) {
for (const auto &bound : bounds) {
internal_assert(bound.min.defined()) << "Realize of undefined\n";
internal_assert(bound.extent.defined()) << "Realize of undefined\n";
internal_assert(bound.min.type().is_scalar()) << "Realize of vector size\n";
internal_assert(bound.extent.type().is_scalar()) << "Realize of vector size\n";
}
internal_assert(body.defined()) << "Realize of undefined\n";
internal_assert(!types.empty()) << "Realize has empty type\n";
internal_assert(condition.defined()) << "Realize with undefined condition\n";
internal_assert(condition.type().is_bool()) << "Realize condition is not boolean\n";
Realize *node = new Realize;
node->name = name;
node->types = types;
node->memory_type = memory_type;
node->bounds = bounds;
node->condition = std::move(condition);
node->body = std::move(body);
return node;
}
Stmt Prefetch::make(const std::string &name, const std::vector<Type> &types,
const Region &bounds,
const PrefetchDirective &prefetch,
Expr condition, Stmt body) {
for (const auto &bound : bounds) {
internal_assert(bound.min.defined()) << "Prefetch of undefined\n";
internal_assert(bound.extent.defined()) << "Prefetch of undefined\n";
internal_assert(bound.min.type().is_scalar()) << "Prefetch of vector size\n";
internal_assert(bound.extent.type().is_scalar()) << "Prefetch of vector size\n";
}
internal_assert(!types.empty()) << "Prefetch has empty type\n";
internal_assert(body.defined()) << "Prefetch of undefined\n";
internal_assert(condition.defined()) << "Prefetch with undefined condition\n";
internal_assert(condition.type().is_bool()) << "Prefetch condition is not boolean\n";
user_assert(is_pure(prefetch.offset)) << "The offset to the prefetch directive must be pure.";
Prefetch *node = new Prefetch;
node->name = name;
node->types = types;
node->bounds = bounds;
node->prefetch = prefetch;
node->condition = std::move(condition);
node->body = std::move(body);
return node;
}
Stmt Block::make(Stmt first, Stmt rest) {
internal_assert(first.defined()) << "Block of undefined\n";
internal_assert(rest.defined()) << "Block of undefined\n";
Block *node = new Block;
if (const Block *b = first.as<Block>()) {
// Use a canonical block nesting order
node->first = b->first;
node->rest = Block::make(b->rest, std::move(rest));
} else {
node->first = std::move(first);
node->rest = std::move(rest);
}
return node;
}
Stmt Block::make(const std::vector<Stmt> &stmts) {
if (stmts.empty()) {
return Stmt();
}
Stmt result = stmts.back();
for (size_t i = stmts.size() - 1; i > 0; i--) {
result = Block::make(stmts[i - 1], result);
}
return result;
}
Stmt Fork::make(Stmt first, Stmt rest) {
internal_assert(first.defined()) << "Fork of undefined\n";
internal_assert(rest.defined()) << "Fork of undefined\n";
Fork *node = new Fork;
if (const Fork *b = first.as<Fork>()) {
// Use a canonical fork nesting order
node->first = b->first;
node->rest = Fork::make(b->rest, std::move(rest));
} else {
node->first = std::move(first);
node->rest = std::move(rest);
}
return node;
}
Stmt IfThenElse::make(Expr condition, Stmt then_case, Stmt else_case) {
internal_assert(condition.defined() && then_case.defined()) << "IfThenElse of undefined\n";
// else_case may be null.
internal_assert(condition.type().is_scalar()) << "IfThenElse with vector condition\n";
IfThenElse *node = new IfThenElse;
node->condition = std::move(condition);
node->then_case = std::move(then_case);
node->else_case = std::move(else_case);
return node;
}
Stmt Evaluate::make(Expr v) {
internal_assert(v.defined()) << "Evaluate of undefined\n";
Evaluate *node = new Evaluate;
node->value = std::move(v);
return node;
}
Expr Call::make(const Function &func, const std::vector<Expr> &args, int idx) {
internal_assert(idx >= 0 &&
idx < func.outputs())
<< "Value index out of range in call to halide function\n";
internal_assert(func.has_pure_definition() || func.has_extern_definition())
<< "Call to undefined halide function\n";
return make(func.output_types()[(size_t)idx], func.name(), args, Halide,
func.get_contents(), idx, Buffer<>(), Parameter());
}
namespace {
const char *const intrinsic_op_names[] = {
"abs",
"absd",
"add_image_checks_marker",
"alloca",
"bitwise_and",
"bitwise_not",
"bitwise_or",
"bitwise_xor",
"bool_to_mask",
"bundle",
"call_cached_indirect_function",
"cast_mask",
"concat_bits",
"count_leading_zeros",
"count_trailing_zeros",
"debug_to_file",
"declare_box_touched",
"div_round_to_zero",
"dynamic_shuffle",
"extract_bits",
"extract_mask_element",
"get_user_context",
"gpu_thread_barrier",
"halving_add",
"halving_sub",
"hvx_gather",
"hvx_scatter",
"hvx_scatter_acc",
"hvx_scatter_release",
"if_then_else",
"if_then_else_mask",
"image_load",
"image_store",
"lerp",
"likely",
"likely_if_innermost",
"load_typed_struct_member",
"make_struct",
"memoize_expr",
"mod_round_to_zero",
"mul_shift_right",
"mux",
"popcount",
"prefetch",
"profiling_enable_instance_marker",
"promise_clamped",
"random",
"register_destructor",
"require",
"require_mask",
"return_second",
"rewrite_buffer",
"round",
"rounding_halving_add",
"rounding_mul_shift_right",
"rounding_shift_left",
"rounding_shift_right",
"saturating_add",
"saturating_sub",
"saturating_cast",
"scatter_gather",
"select_mask",
"shift_left",
"shift_right",
"signed_integer_overflow",
"size_of_halide_buffer_t",
"skip_stages_marker",
"sliding_window_marker",
"sorted_avg",
"strict_add",
"strict_div",
"strict_eq",
"strict_fma",
"strict_le",
"strict_lt",
"strict_max",
"strict_min",
"strict_mul",
"strict_sub",
"stringify",
"target_arch_is",
"target_bits",
"target_has_feature",
"target_natural_vector_size",
"target_os_is",
"undef",
"unreachable",
"unsafe_promise_clamped",
"widen_right_add",
"widen_right_mul",
"widen_right_sub",
"widening_add",
"widening_mul",
"widening_shift_left",
"widening_shift_right",
"widening_sub",
"get_runtime_vscale",
};
static_assert(sizeof(intrinsic_op_names) / sizeof(intrinsic_op_names[0]) == Call::IntrinsicOpCount,
"intrinsic_op_names needs attention");
} // namespace
const char *Call::get_intrinsic_name(IntrinsicOp op) {
return intrinsic_op_names[op];
}
Expr Call::make(Type type, Call::IntrinsicOp op, const std::vector<Expr> &args, CallType call_type,
FunctionPtr func, int value_index,
const Buffer<> &image, Parameter param) {
internal_assert(call_type == Call::Intrinsic || call_type == Call::PureIntrinsic);
return Call::make(type, intrinsic_op_names[op], args, call_type, std::move(func), value_index, image, std::move(param));
}
Expr Call::make(Type type, const std::string &name, const std::vector<Expr> &args, CallType call_type,
FunctionPtr func, int value_index,
Buffer<> image, Parameter param) {
if (name == intrinsic_op_names[Call::prefetch] && call_type == Call::Intrinsic) {
internal_assert(args.size() % 2 == 0)
<< "Number of args to a prefetch call should be even: {base, offset, extent0, stride0, extent1, stride1, ...}\n";
}
for (size_t i = 0; i < args.size(); i++) {
internal_assert(args[i].defined()) << "Call of " << name << " with argument " << i << " undefined.\n";
}
if (call_type == Halide) {
for (const auto &arg : args) {
internal_assert(arg.type() == Int(32))
<< "Args to call to halide function must be type Int(32)\n";
}
} else if (call_type == Image) {
internal_assert((param.defined() || image.defined()))
<< "Call node to undefined image\n";
for (const auto &arg : args) {
internal_assert(arg.type() == Int(32))
<< "Args to load from image must be type Int(32)\n";
}
}
Call *node = new Call;
node->type = type;
node->name = name;
node->args = args;
node->call_type = call_type;
node->func = std::move(func);
node->value_index = value_index;
node->image = std::move(image);
node->param = std::move(param);
return node;
}
Expr Variable::make(Type type, const std::string &name, Buffer<> image, Parameter param, ReductionDomain reduction_domain) {
internal_assert(!name.empty());
Variable *node = new Variable;
node->type = type;
node->name = name;
node->image = std::move(image);
node->param = std::move(param);
node->reduction_domain = std::move(reduction_domain);
return node;
}
Expr Shuffle::make(const std::vector<Expr> &vectors,
const std::vector<int> &indices) {
internal_assert(!vectors.empty()) << "Shuffle of zero vectors.\n";
internal_assert(!indices.empty()) << "Shufle with zero indices.\n";
Type element_ty = vectors.front().type().element_of();
int input_lanes = 0;
for (const Expr &i : vectors) {
internal_assert(i.type().element_of() == element_ty) << "Shuffle of vectors of mismatched types.\n";
input_lanes += i.type().lanes();
}
for (int i : indices) {
internal_assert(0 <= i && i < input_lanes) << "Shuffle vector index out of range: " << i << "\n";
}
Shuffle *node = new Shuffle;
node->type = element_ty.with_lanes((int)indices.size());
node->vectors = vectors;
node->indices = indices;
return node;
}
Expr Shuffle::make_interleave(const std::vector<Expr> &vectors) {
internal_assert(!vectors.empty()) << "Interleave of zero vectors.\n";
if (vectors.size() == 1) {
return vectors.front();
}
int lanes = vectors.front().type().lanes();
for (const Expr &i : vectors) {
internal_assert(i.type().lanes() == lanes)
<< "Interleave of vectors with different sizes.\n";
}
std::vector<int> indices;
for (int i = 0; i < lanes; i++) {
for (int j = 0; j < (int)vectors.size(); j++) {
indices.push_back(j * lanes + i);
}
}
return make(vectors, indices);
}
Expr Shuffle::make_concat(const std::vector<Expr> &vectors) {
internal_assert(!vectors.empty()) << "Concat of zero vectors.\n";
if (vectors.size() == 1) {
return vectors.front();
}
std::vector<int> indices;
int lane = 0;
for (const auto &vector : vectors) {
for (int j = 0; j < vector.type().lanes(); j++) {
indices.push_back(lane++);
}
}
return make(vectors, indices);
}
Expr Shuffle::make_broadcast(Expr vector, int factor) {
std::vector<int> indices(factor * vector.type().lanes());
for (int ix = 0; ix < factor; ix++) {
std::iota(indices.begin() + ix * vector.type().lanes(),
indices.begin() + (ix + 1) * vector.type().lanes(), 0);
}
return make({std::move(vector)}, indices);
}
Expr Shuffle::make_slice(Expr vector, int begin, int stride, int size) {
if (begin == 0 && size == vector.type().lanes() && stride == 1) {
return vector;
}
std::vector<int> indices;
indices.reserve(size);
for (int i = 0; i < size; i++) {
indices.push_back(begin + i * stride);
}
return make({std::move(vector)}, indices);
}
Expr Shuffle::make_extract_element(Expr vector, int i) {
return make_slice(std::move(vector), i, 1, 1);
}
bool Shuffle::is_broadcast() const {
int lanes = indices.size();
int factor = broadcast_factor();
if (factor == 0 || factor >= lanes) {
return false;
}
int broadcasted_lanes = lanes / factor;
if (broadcasted_lanes < 2 || broadcasted_lanes >= lanes || lanes % broadcasted_lanes != 0) {
return false;
}
for (int i = 0; i < lanes; i++) {
if (indices[i % broadcasted_lanes] != indices[i]) {
return false;
}
}
return true;
}
int Shuffle::broadcast_factor() const {
int lanes = indices.size();
int broadcasted_lanes = 0;
for (; broadcasted_lanes < lanes; broadcasted_lanes++) {
if (indices[broadcasted_lanes] != broadcasted_lanes) {
break;
}
}
if (broadcasted_lanes > 0) {
return lanes / broadcasted_lanes;
} else {
return 0;
}
}
bool Shuffle::is_interleave() const {
int lanes = vectors.front().type().lanes();
// Don't consider concat of scalars as an interleave.
if (lanes == 1) {
return false;
}
for (const Expr &i : vectors) {
if (i.type().lanes() != lanes) {
return false;
}
}
// Require that we are a complete interleaving.
if (lanes * vectors.size() != indices.size()) {
return false;
}
for (int i = 0; i < (int)vectors.size(); i++) {
for (int j = 0; j < lanes; j++) {
if (indices[j * (int)vectors.size() + i] != i * lanes + j) {
return false;
}
}
}
return true;
}
std::vector<std::pair<int, int>> Shuffle::vector_and_lane_indices() const {
// Construct the mapping for each shuffled element and find the index
// of which vector to use and the index for which of its lanes to use
std::vector<std::pair<int, int>> all_indices;
all_indices.reserve(indices.size());
for (int i = 0; i < (int)vectors.size(); i++) {
for (int j = 0; j < vectors[i].type().lanes(); j++) {
all_indices.emplace_back(i, j);
}
}
std::vector<std::pair<int, int>> result;
result.reserve(indices.size());
for (int i : indices) {
result.push_back(all_indices[i]);
}
return result;
}
Stmt Atomic::make(const std::string &producer_name,
const std::string &mutex_name,
Stmt body) {
Atomic *node = new Atomic;
node->producer_name = producer_name;
node->mutex_name = mutex_name;
internal_assert(body.defined()) << "Atomic must have a body statement.\n";
node->body = std::move(body);
return node;
}
Stmt HoistedStorage::make(const std::string &name,
Stmt body) {
internal_assert(body.defined()) << "HoistedStorage must have a body statement.\n";
HoistedStorage *node = new HoistedStorage;
node->name = name;
node->body = std::move(body);
return node;
}
Expr VectorReduce::make(VectorReduce::Operator op,
Expr vec,
int lanes) {
if (vec.type().is_bool()) {
internal_assert(op == VectorReduce::And || op == VectorReduce::Or)
<< "The only legal operators for VectorReduce on a Bool"
<< "vector are VectorReduce::And and VectorReduce::Or\n";
}
internal_assert(!vec.type().is_handle()) << "VectorReduce of handle type";
// Check the output lanes is a factor of the input lanes. They can
// also both be zero if we're constructing a wildcard expression.
internal_assert((lanes == 0 && vec.type().lanes() == 0) ||
(lanes != 0 && (vec.type().lanes() % lanes == 0)))
<< "Vector reduce output lanes must be a divisor of the number of lanes in the argument "
<< lanes << " " << vec.type().lanes() << "\n";
VectorReduce *node = new VectorReduce;
node->type = vec.type().with_lanes(lanes);
node->op = op;
node->value = std::move(vec);
return node;
}
namespace {
// Helper function to determine if a sequence of indices is a
// contiguous ramp.
bool is_ramp(const std::vector<int> &indices, int stride = 1) {
for (size_t i = 0; i + 1 < indices.size(); i++) {
if (indices[i + 1] != indices[i] + stride) {
return false;
}
}
return true;
}