-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathir.cpp
More file actions
1068 lines (929 loc) · 26.9 KB
/
Copy pathir.cpp
File metadata and controls
1068 lines (929 loc) · 26.9 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 <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <iostream>
#include <string.h>
#include "types.h"
#include "util/util.h"
#include "macros.h"
#include "ir_printer.h"
#include "util/compare.h"
#include "util/arrays.h"
using namespace std;
namespace simit {
namespace ir {
// class IRNode
std::ostream &operator<<(std::ostream &os, const IRNode &node) {
IRPrinter printer(os);
printer.print(node);
return os;
}
// class Expr
Expr::Expr(const Var &var) : Expr(VarExpr::make(var)) {}
Expr::Expr(int val) : IRHandle(Literal::make(val)) {}
Expr::Expr(double val) : IRHandle(Literal::make(val)) {}
Expr::Expr(double_complex val) : IRHandle(Literal::make(val)) {}
Expr Expr::operator()(const std::vector<IndexVar> &indexVars) const {
return IndexedTensor::make(*this, indexVars);
}
std::ostream &operator<<(std::ostream &os, const Expr &expr) {
IRPrinter printer(os);
printer.print(expr);
return os;
}
Expr operator-(Expr a) {
return Neg::make(a);
}
Expr operator+(Expr a, Expr b) {
return Add::make(a, b);
}
Expr operator-(Expr a, Expr b) {
return Sub::make(a, b);
}
Expr operator*(Expr a, Expr b) {
return Mul::make(a, b);
}
Expr operator/(Expr a, Expr b) {
return Div::make(a, b);
}
Expr operator%(Expr a, Expr b) {
return Rem::make(a, b);
}
// class Stmt
std::ostream &operator<<(std::ostream &os, const Stmt &Stmt) {
IRPrinter printer(os);
printer.skipTopExprParenthesis();
printer.print(Stmt);
return os;
}
// class ForDomain
std::ostream &operator<<(std::ostream &os, const ForDomain &d) {
switch (d.kind) {
case ForDomain::IndexSet:
os << d.indexSet;
break;
case ForDomain::Endpoints:
os << d.set << ".endpoints[" << d.var << "]";
break;
case ForDomain::Edges:
os << d.set << ".edges[" << d.var << "]";
break;
case ForDomain::Grid:
os << "grid[";
for (const Var& v : d.gridVars) {
os << v << ",";
}
os << "]";
break;
case ForDomain::NeighborsOf:
os << d.set << ".neighborsOf[" << d.var << "]";
break;
case ForDomain::Neighbors:
os << d.set << ".neighbors[" << d.var << "]";
break;
case ForDomain::Diagonal:
os << d.set << ".diagonal[" << d.var << "]";
break;
}
return os;
}
// Type compute functions
Type getFieldType(Expr elementOrSet, std::string fieldName) {
iassert(elementOrSet.type().isElement() || elementOrSet.type().isSet());
Type fieldType;
if (elementOrSet.type().isElement()) {
const ElementType *elemType = elementOrSet.type().toElement();
fieldType = elemType->field(fieldName).type;
}
else if (elementOrSet.type().isSet()) {
const SetType *setType = elementOrSet.type().toSet();
const ElementType *elemType = setType->elementType.toElement();
const TensorType *elemFieldType= elemType->field(fieldName).type.toTensor();
const ScalarType componentType = elemFieldType->getComponentType();
// The type of a set field is
// `tensor[set](tensor[elementFieldDimensions](elemFieldComponentType))[']`
// If the element field is a row vector, then the set field is also a row
// vector. Otherwise, the set field is a column vector.
vector<IndexDomain> dimensions;
if (elemFieldType->order() == 0) {
dimensions.push_back(IndexDomain(IndexSet(elementOrSet)));
}
else {
unsigned order = elemFieldType->order();
dimensions = vector<IndexDomain>(order);
dimensions[0] = IndexDomain(IndexSet(elementOrSet));
vector<IndexDomain> elemFieldDimensions = elemFieldType->getDimensions();
for (size_t i=0; i < order; ++i) {
dimensions[i] = dimensions[i] * elemFieldDimensions[i];
}
}
const bool isColumnVector = (elemFieldType->getDimensions().size() == 0 ||
elemFieldType->isColumnVector);
fieldType = TensorType::make(componentType, dimensions, isColumnVector);
}
return fieldType;
}
Type getBlockType(Expr tensor) {
iassert(tensor.type().isTensor());
return tensor.type().toTensor()->getBlockType();
}
Type getIndexExprType(std::vector<IndexVar> lhsIndexVars, Expr expr,
bool isColumnVector) {
iassert(isScalar(expr.type()));
std::vector<IndexDomain> dimensions;
for (auto &indexVar : lhsIndexVars) {
dimensions.push_back(indexVar.getDomain());
}
const auto componentType = expr.type().toTensor()->getComponentType();
return TensorType::make(componentType, dimensions, isColumnVector);
}
// enum CompoundOperator
std::ostream &operator<<(std::ostream &os, const CompoundOperator &cop) {
switch (cop) {
case CompoundOperator::None: {
break;
}
case CompoundOperator::Add: {
os << "+";
break;
}
case CompoundOperator::Sub: {
os << "-";
break;
}
}
return os;
}
// struct Literal
void Literal::cast(Type type) {
iassert(type.isTensor());
iassert(type.toTensor()->getComponentType() ==
this->type.toTensor()->getComponentType());
iassert(type.toTensor()->size() == this->type.toTensor()->size());
this->type = type;
}
int Literal::getIntVal(int index) const {
iassert(type.toTensor()->getComponentType().isInt())
<< "getIntVal only valid for literals with int components";
return ((int*)data)[index];
}
double Literal::getFloatVal(int index) const {
if (ScalarType::singleFloat()) {
return ((float*)data)[index];
}
else {
return ((double*)data)[index];
}
}
double_complex Literal::getComplexVal(int index) const {
if (ScalarType::singleFloat()) {
return ((float_complex*)data)[index];
}
else {
return ((double_complex*)data)[index];
}
}
bool Literal::isAllZeros() const {
for (unsigned i = 0; i < size; ++i) {
if (((uint8_t*)data)[i] != 0) {
return false;
}
}
return true;
}
Expr Literal::make(Type type) {
return Literal::make(type, nullptr, 0);
}
Expr Literal::make(int val) {
return make(Int, &val, sizeof(int));
}
Expr Literal::make(double val) {
// Choose appropriate precision
if (ScalarType::singleFloat()) {
float floatVal = (float) val;
return make(Float, &floatVal, sizeof(float));
}
else {
return make(Float, &val, sizeof(double));
}
}
Expr Literal::make(bool val) {
return make(Boolean, &val, sizeof(bool));
}
Expr Literal::make(std::string val) {
Literal *node = new Literal;
node->type = String;
node->size = sizeof(char) * (val.length() + 1);
node->data = malloc(node->size);
val.copy((char *)node->data, val.length());
((char *)(node->data))[node->size - 1] = '\0';
return node;
}
Expr Literal::make(double_complex val) {
// Choose appropriate precision
if (ScalarType::singleFloat()) {
float_complex floatVal;
floatVal.real = (float) val.real;
floatVal.imag = (float) val.imag;
return make(Complex, &floatVal, sizeof(float_complex));
}
else {
return make(Complex, &val, sizeof(double_complex));
}
}
Expr Literal::make(Type type, void* values, size_t bufSize) {
iassert(type.isTensor()) << "only tensor literals are supported for now";
const TensorType *ttype = type.toTensor();
size_t size = 0;
size_t sizeInBytes = 0;
switch (type.kind()) {
case Type::Tensor: {
size = ttype->size();
sizeInBytes = size * ttype->getComponentType().bytes();
break;
}
case Type::Set:
case Type::Element:
case Type::UnnamedTuple:
case Type::NamedTuple:
case Type::Array:
case Type::Opaque:
iassert(false) << "only tensor and scalar literals currently supported";
break;
case Type::Undefined:
ierror << "attempting to create literal of undefined type";
break;
}
Literal *node = new Literal;
node->type = type;
node->size = sizeInBytes;
node->data = malloc(node->size);
if (values != nullptr) {
iassert(node->size <= bufSize)
<< "bufSize too small for desired type: " << type
<< ", needed " << sizeInBytes << ", got " << bufSize;
memcpy(node->data, values, node->size);
}
else {
// Zero array
switch (ttype->getComponentType().kind) {
case ir::ScalarType::Boolean:
util::zero<bool>(node->data, size);
break;
case ir::ScalarType::Int:
util::zero<int>(node->data, size);
break;
case ir::ScalarType::Float:
if (ir::ScalarType::singleFloat()) {
iassert(ir::ScalarType::floatBytes == sizeof(float));
util::zero<float>(node->data, size);
}
else {
iassert(ir::ScalarType::floatBytes == sizeof(double));
util::zero<double>(node->data, size);
}
break;
case ir::ScalarType::Complex:
if (ir::ScalarType::singleFloat()) {
iassert(ir::ScalarType::floatBytes == sizeof(float));
util::zero<float>(node->data, size);
}
else {
iassert(ir::ScalarType::floatBytes == sizeof(double));
util::zero<double>(node->data, size);
}
break;
case ir::ScalarType::String:
unreachable;
}
}
return node;
}
Expr Literal::make(Type type, std::vector<double> values) {
iassert(isScalar(type) || type.toTensor()->getComponentType().isFloat() &&
type.toTensor()->size() == values.size() ||
type.toTensor()->getComponentType().isComplex() &&
2 * type.toTensor()->size() == values.size());
iassert(type.toTensor()->getComponentType().isFloat() ||
type.toTensor()->getComponentType().isComplex())
<< "Float array constructor must use float or complex component type";
if (ScalarType::singleFloat()) {
// Convert double vector to float vector
std::vector<float> floatValues;
for (double val : values) {
floatValues.push_back(val);
}
return Literal::make(type, floatValues.data(),
util::getVectorSize(floatValues));
}
else {
return Literal::make(type, values.data(),
util::getVectorSize(values));
}
}
Expr Literal::make(Type type, std::vector<double_complex> values) {
iassert(isScalar(type) ||
type.toTensor()->getComponentType().isComplex() &&
2 * type.toTensor()->size() == values.size());
iassert(type.toTensor()->getComponentType().isComplex())
<< "Complex array constructor must use complex component type";
if (ScalarType::singleFloat()) {
// Convert double vector to float vector
std::vector<float_complex> floatValues;
for (double_complex val : values) {
floatValues.push_back(float_complex(val.real, val.imag));
}
return Literal::make(type, floatValues.data(),
util::getVectorSize(floatValues));
}
else {
return Literal::make(type, values.data(),
util::getVectorSize(values));
}
}
Literal::~Literal() {
free(data);
}
inline size_t getTensorByteSize(const TensorType *tensorType) {
return tensorType->size() * tensorType->getComponentType().bytes();
}
bool operator==(const Literal& l, const Literal& r) {
iassert(l.type.isTensor() && r.type.isTensor());
if (l.type != r.type) {
return false;
}
iassert(getTensorByteSize(l.type.toTensor()) ==
getTensorByteSize(r.type.toTensor()));
size_t size = l.type.toTensor()->size();
switch (l.type.toTensor()->getComponentType().kind) {
case ir::ScalarType::Int: {
return util::compare<int>(l.data, r.data, size);
}
case ir::ScalarType::Float: {
if (ir::ScalarType::singleFloat()) {
return util::compare<float>(l.data, r.data, size);
}
else {
return util::compare<double>(l.data, r.data, size);
}
}
case ir::ScalarType::Boolean: {
return util::compare<bool>(l.data, r.data, size);
}
case ir::ScalarType::Complex: {
if (ir::ScalarType::singleFloat()) {
return util::compare<float_complex>(l.data, r.data, size);
}
else {
return util::compare<double_complex>(l.data, r.data, size);
}
}
case ir::ScalarType::String: {
return (std::strcmp((const char *)l.data, (const char *)r.data) == 0);
}
default: {
not_supported_yet;
return false;
}
}
return true;
}
bool operator!=(const Literal& l, const Literal& r) {
return !(l == r);
}
// struct VarExpr
Expr VarExpr::make(Var var) {
VarExpr *node = new VarExpr;
node->type = var.getType();
node->var = var;
return node;
}
// struct Load
Expr Load::make(Expr buffer, Expr index) {
iassert(isScalar(index.type()));
Load *node = new Load;
// TODO: Temporary handle loading from TensorType (should only support arrays)
ScalarType loadType = (buffer.type().isTensor())
? buffer.type().toTensor()->getComponentType()
: buffer.type().toArray()->elementType;
node->type = TensorType::make(loadType);
node->buffer = buffer;
node->index = index;
return node;
}
// struct FieldRead
Expr FieldRead::make(Expr elementOrSet, std::string fieldName) {
iassert(elementOrSet.type().isElement() || elementOrSet.type().isSet());
FieldRead *node = new FieldRead;
node->type = getFieldType(elementOrSet, fieldName);
node->elementOrSet = elementOrSet;
node->fieldName = fieldName;
return node;
}
// struct Length
Expr Length::make(IndexSet indexSet) {
Length *node = new Length;
node->type = TensorType::make(ScalarType(ScalarType::Int));
node->indexSet = indexSet;
return node;
}
// struct IndexRead
Expr IndexRead::make(Expr edgeSet, Kind kind) {
iassert(edgeSet.type().isSet());
IndexRead *node = new IndexRead;
node->type = TensorType::make(ScalarType(ScalarType::Int),
{IndexDomain(IndexSet(edgeSet))});
node->edgeSet = edgeSet;
node->kind = kind;
return node;
}
Expr IndexRead::make(Expr edgeSet, Kind kind, int index) {
iassert(edgeSet.type().isGridSet());
iassert(kind == GridDim);
IndexRead *node = new IndexRead;
node->type = TensorType::make(ScalarType(ScalarType::Int));
node->edgeSet = edgeSet;
node->kind = kind;
node->index = index;
return node;
}
// struct Neg
Expr Neg::make(Expr a) {
iassert_scalar(a);
Neg *node = new Neg;
node->type = a.type();
node->a = a;
return node;
}
// struct Add
Expr Add::make(Expr a, Expr b) {
iassert_scalar(a);
iassert_types_equal(a,b);
Add *node = new Add;
node->type = a.type();
node->a = a;
node->b = b;
return node;
}
// struct Sub
Expr Sub::make(Expr a, Expr b) {
iassert_scalar(a);
iassert_types_equal(a,b);
Sub *node = new Sub;
node->type = a.type();
node->a = a;
node->b = b;
return node;
}
// struct Mul
Expr Mul::make(Expr a, Expr b) {
iassert_scalar(a);
iassert_types_equal(a,b);
Mul *node = new Mul;
node->type = a.type();
node->a = a;
node->b = b;
return node;
}
// struct Div
Expr Div::make(Expr a, Expr b) {
iassert_scalar(a);
iassert_types_equal(a,b);
Div *node = new Div;
node->type = a.type();
node->a = a;
node->b = b;
return node;
}
// struct Rem
Expr Rem::make(Expr a, Expr b) {
iassert_int_scalar(a);
iassert_types_equal(a,b);
Rem *node = new Rem;
node->type = a.type();
node->a = a;
node->b = b;
return node;
}
// struct Not
Expr Not::make(Expr a) {
iassert_boolean_scalar(a);
Not *node = new Not;
node->type = TensorType::make(ScalarType::Boolean);
node->a = a;
return node;
}
// struct Eq
Expr Eq::make(Expr a, Expr b) {
iassert_types_equal(a,b);
Eq *node = new Eq;
node->type = TensorType::make(ScalarType::Boolean);
node->a = a;
node->b = b;
return node;
}
// struct Ne
Expr Ne::make(Expr a, Expr b) {
iassert_types_equal(a,b);
Ne *node = new Ne;
node->type = TensorType::make(ScalarType::Boolean);
node->a = a;
node->b = b;
return node;
}
// struct Gt
Expr Gt::make(Expr a, Expr b) {
iassert_types_equal(a,b);
Gt *node = new Gt;
node->type = TensorType::make(ScalarType::Boolean);
node->a = a;
node->b = b;
return node;
}
// struct Lt
Expr Lt::make(Expr a, Expr b) {
iassert_types_equal(a,b);
Lt *node = new Lt;
node->type = TensorType::make(ScalarType::Boolean);
node->a = a;
node->b = b;
return node;
}
// struct Ge
Expr Ge::make(Expr a, Expr b) {
iassert_types_equal(a,b);
Ge *node = new Ge;
node->type = TensorType::make(ScalarType::Boolean);
node->a = a;
node->b = b;
return node;
}
// struct Le
Expr Le::make(Expr a, Expr b) {
iassert_types_equal(a,b);
Le *node = new Le;
node->type = TensorType::make(ScalarType::Boolean);
node->a = a;
node->b = b;
return node;
}
// struct And
Expr And::make(Expr a, Expr b) {
iassert_boolean_scalar(a);
iassert_boolean_scalar(b);
And *node = new And;
node->type = TensorType::make(ScalarType::Boolean);
node->a = a;
node->b = b;
return node;
}
// struct Or
Expr Or::make(Expr a, Expr b) {
iassert_boolean_scalar(a);
iassert_boolean_scalar(b);
Or *node = new Or;
node->type = TensorType::make(ScalarType::Boolean);
node->a = a;
node->b = b;
return node;
}
// struct Xor
Expr Xor::make(Expr a, Expr b) {
iassert_boolean_scalar(a);
iassert_boolean_scalar(b);
Xor *node = new Xor;
node->type = TensorType::make(ScalarType::Boolean);
node->a = a;
node->b = b;
return node;
}
// struct VarDecl
Stmt VarDecl::make(Var var) {
VarDecl *node = new VarDecl;
node->var = var;
return node;
}
// struct AssignStmt
Stmt AssignStmt::make(Var var, Expr value) {
return make(var, value, CompoundOperator::None);
}
Stmt AssignStmt::make(Var var, Expr value, CompoundOperator cop) {
AssignStmt *node = new AssignStmt;
node->var = var;
node->value = value;
node->cop = cop;
return node;
}
// struct Store
Stmt Store::make(Expr buf, Expr index, Expr value, CompoundOperator cop) {
iassert(isScalar(value.type()));
// TODO: Change to only allow stores to arrays, not tensors
// iassert(buffer.type().isArray()) << "Can only store to arrays";
// iassert(value.type()==TensorType::make(buff().toArray()->elementType))
// << "Stored value type " << util::quote(value.type())
// << " does not match the element type of array "
// << util::quote(buff().toArray()->elementType);
iassert(buf.type().isArray() || buf.type().isTensor())
<< "Can only store to arrays and tensors";
iassert(!buf.type().isTensor() ||
TensorType::make(buf.type().toTensor()->getComponentType())==value.type())
<< "Stored value type " << util::quote(value.type())
<< " does not match the component type of tensor "
<< util::quote(buf.type().toTensor()->getBlockType()) ;
Store *node = new Store;
node->buffer = buf;
node->index = index;
node->value = value;
node->cop = cop;
return node;
}
// struct FieldWrite
Stmt FieldWrite::make(Expr elementOrSet, std::string fieldName, Expr value,
CompoundOperator cop) {
FieldWrite *node = new FieldWrite;
node->elementOrSet = elementOrSet;
node->fieldName = fieldName;
node->value = value;
node->cop = cop;
return node;
}
// struct CallStmt
Stmt CallStmt::make(std::vector<Var> results,
Func callee, std::vector<Expr> actuals) {
CallStmt *node = new CallStmt;
node->results = results;
node->callee = callee;
node->actuals = actuals;
return node;
}
// struct Scope
Stmt Scope::make(Stmt scopedStmt) {
iassert(scopedStmt.defined());
Scope *node = new Scope;
node->scopedStmt = scopedStmt;
return node;
}
// struct IfThenElse
Stmt IfThenElse::make(Expr condition, Stmt thenBody) {
IfThenElse *node = new IfThenElse;
node->condition = condition;
node->thenBody = Scope::make(thenBody);
node->elseBody = Stmt();
return node;
}
Stmt IfThenElse::make(Expr condition, Stmt thenBody, Stmt elseBody) {
IfThenElse *node = new IfThenElse;
node->condition = condition;
node->thenBody = Scope::make(thenBody);
node->elseBody = Scope::make(elseBody);
return node;
}
// struct ForRange
Stmt ForRange::make(Var var, Expr start, Expr end, Stmt body) {
iassert(var.defined());
iassert(body.defined());
iassert(start.defined());
iassert(end.defined());
ForRange *node = new ForRange;
node->var = var;
node->start = start;
node->end = end;
node->body = Scope::make(body);
return Scope::make(node); // Put loop variable in a scope
}
// struct For
Stmt For::make(Var var, ForDomain domain, Stmt body) {
For *node = new For;
node->var = var;
node->domain = domain;
node->body = Scope::make(body);
return Scope::make(node); // Put loop variable in a scope
}
// struct While
Stmt While::make(Expr condition, Stmt body) {
While *node = new While;
node->condition = condition;
node->body = Scope::make(body);
return node;
}
// struct Kernel
Stmt Kernel::make(Var var, IndexDomain domain, Stmt body) {
Kernel *node = new Kernel;
node->var = var;
node->domain = domain;
node->body = body;
return node;
}
// struct Block
Stmt Block::make(Stmt first, Stmt rest) {
iassert(first.defined() || rest.defined()) << "Empty block";
// Handle case where first is undefined, to ease codegen in loops
if (!first.defined()) {
std::swap(first,rest);
}
Block *node = new Block;
if (isa<Block>(first)) {
const Block* block = to<Block>(first);
node->stmts = block->stmts;
} else {
node->stmts = {first};
}
if (rest.defined()) {
if (isa<Block>(rest)) {
const Block* block = to<Block>(rest);
node->stmts.insert(node->stmts.end(), block->stmts.begin(), block->stmts.end());
} else {
node->stmts.push_back(rest);
}
}
return node;
}
Stmt Block::make(std::vector<Stmt> stmts) {
iassert(stmts.size() > 0) << "Empty block";
Block *node = new Block;
node->stmts = stmts;
return node;
}
// struct Print
Stmt Print::make(Expr expr, std::string format) {
Print *node = new Print;
node->expr = expr;
node->format = format;
return node;
}
Stmt Print::make(std::string str) {
return Print::make(Literal::make(str));
}
// struct Comment
Stmt Comment::make(std::string comment, Stmt commentedStmt,
bool footerSpace, bool headerSpace){
Comment *node = new Comment;
node->comment = comment;
node->commentedStmt = commentedStmt;
node->footerSpace = footerSpace;
node->headerSpace = headerSpace;
return node;
}
// struct Pass
Stmt Pass::make() {
Pass *node = new Pass;
return node;
}
// struct UnnamedTupleRead
Expr UnnamedTupleRead::make(Expr tuple, Expr index) {
iassert(tuple.type().isUnnamedTuple());
UnnamedTupleRead *node = new UnnamedTupleRead;
node->type = tuple.type().toUnnamedTuple()->elementType;
node->tuple = tuple;
node->index = index;
return node;
}
// struct NamedTupleRead
Expr NamedTupleRead::make(Expr tuple, std::string elementName) {
iassert(tuple.type().isNamedTuple());
NamedTupleRead *node = new NamedTupleRead;
node->type = tuple.type().toNamedTuple()->element(elementName).type;
node->tuple = tuple;
node->elementName = elementName;
return node;
}
// struct SetRead
Expr SetRead::make(Expr set, std::vector<Expr> indices) {
#ifdef SIMIT_ASSERTS
iassert(set.type().isSet());
for (const Expr &index : indices) {
iassert(isScalar(index.type()));
}
#endif
if (set.type().isUnstructuredSet() &&
set.type().toUnstructuredSet()->getCardinality() == 0) {
// TODO: Can't check dimensions of a free-standing unstructured
// set, we probably need to track this globally at some point.
// For now, this is checked during map lowering.
}
else if (set.type().isGridSet()) {
// Indices should index both the source offset and sink offset
// giving 2*dims indices.
iassert(indices.size() == set.type().toGridSet()->dimensions*2);
}
else {
not_supported_yet;
}
SetRead *node = new SetRead;
node->type = set.type().toSet()->elementType;
node->set = set;
node->indices = indices;
return node;
}
// struct TensorRead
Expr TensorRead::make(Expr tensor, std::vector<Expr> indices) {
iassert(tensor.type().isTensor());
#ifdef SIMIT_ASSERTS
for (auto &index : indices) {
iassert(isScalar(index.type()) || index.type().isElement());
}
#endif
iassert(indices.size() == 1 ||
indices.size() == tensor.type().toTensor()->order());
TensorRead *node = new TensorRead;
node->type = getBlockType(tensor);
node->tensor = tensor;
node->indices = indices;
return node;
}
// struct TensorWrite
Stmt TensorWrite::make(Expr tensor, std::vector<Expr> indices, Expr value,
CompoundOperator cop) {
TensorWrite *node = new TensorWrite;
node->tensor = tensor;
node->indices = indices;
node->value = value;
node->cop = cop;
return node;
}
// struct IndexedTensor
Expr IndexedTensor::make(Expr tensor, std::vector<IndexVar> indexVars) {
#ifdef SIMIT_ASSERTS
iassert(tensor.type().isTensor()) << "Only tensors can be indexed.";
iassert(indexVars.size() == tensor.type().toTensor()->order());
std::vector<IndexDomain> dimensions =
tensor.type().toTensor()->getDimensions();
for (size_t i=0; i < indexVars.size(); ++i) {
iassert(indexVars[i].getDomain() == dimensions[i])
<< "IndexVar domain does not match tensor dimension "
<< "for var " << indexVars[i] << ": "
<< indexVars[i].getDomain() << " != " << dimensions[i];
}
#endif
IndexedTensor *node = new IndexedTensor;
node->type = TensorType::make(tensor.type().toTensor()->getComponentType());
node->tensor = tensor;
node->indexVars = indexVars;
return node;
}
// struct IndexExpr
std::vector<IndexVar> IndexExpr::domain() const {
class DomainGatherer : private IRVisitor {
public:
vector<IndexVar> getDomain(const IndexExpr &indexExpr) {