forked from pssrawat/artemis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncdefn.cpp
9111 lines (8834 loc) · 319 KB
/
funcdefn.cpp
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 "funcdefn.hpp"
using namespace std;
bool stmtNode::is_scalar_stmt (void) {
bool ret = true;
ret &= lhs_node->is_scalar_expr ();
ret &= rhs_node->is_scalar_expr ();
return ret;
}
DATA_TYPE stmtNode::infer_stmt_type (void) {
DATA_TYPE ret = BOOL;
lhs_node->infer_expr_type (ret);
rhs_node->infer_expr_type (ret);
return ret;
}
void stmtNode::retime_statement (string stream_dim, int offset) {
stringstream before, after;
if (DEBUG) {
before << "Before retiming, statement : ";
lhs_node->print_node (before);
before << print_stmt_op (op_type);
rhs_node->print_node (before);
cout << before.str() << endl;
}
lhs_node = lhs_node->retime_node (stream_dim, offset);
rhs_node = rhs_node->retime_node (stream_dim, offset);
if (DEBUG) {
after << "After retiming, statement : ";
lhs_node->print_node (after);
after << print_stmt_op (op_type);
rhs_node->print_node (after);
cout << after.str() << endl;
}
}
void stmtNode::accessed_arrays (set<string> &arrays) {
//lhs_node->accessed_arrays (arrays);
rhs_node->accessed_arrays (arrays);
}
void stmtNode::decompose_statements (vector<stmtNode*> &substmts, bool fold_computations, map<string, set<int>> &array_use_map, vector<tuple<string,string>> &sym_initializations, vector<stmtNode*> &init_stmts) {
exprNode *dist_rhs = rhs_node->distribute_node ();
dist_rhs->remove_redundant_nesting ();
stringstream temp;
dist_rhs->print_node (temp);
if (DEBUG)
cout << "distributed rhs = " << temp.str () << endl;
vector<tuple<exprNode*,STMT_OP,exprNode*>> subexprs;
if (op_type == ST_EQ || op_type == ST_PLUSEQ || op_type == ST_MINUSEQ) {
bool flip = (op_type == ST_MINUSEQ) ? true : false;
dist_rhs->decompose_node (lhs_node, subexprs, flip);
if (DEBUG) {
cout << "decomposed stmt = " << endl;
stringstream decomp;
for (auto s : subexprs) {
get<0>(s)->print_node (decomp);
decomp << print_stmt_op (get<1>(s));
get<2>(s)->print_node (decomp);
decomp << "\n";
}
cout << decomp.str();
}
}
if (subexprs.empty())
subexprs.push_back (make_tuple (lhs_node, op_type, dist_rhs));
// Now create the sub statements
for (auto s : subexprs) {
substmts.push_back (new stmtNode (get<1>(s), get<0>(s), get<2>(s), stmt_domain));
}
if (fold_computations) {
cout << "FOLD COMPUTATIONS IS TRUE\n";
fold_symmetric_computations (substmts, array_use_map, sym_initializations, init_stmts);
}
}
void stmtNode::fold_symmetric_computations (vector<stmtNode*> &substmts, map<string, set<int>> &array_use_map, vector<tuple<string,string>> &sym_initializations, vector<stmtNode*> &init_stmts) {
//Try to find symmetry in the substatements. This is experimental.
//Symmetry can be found only if there are at least two accesses that are unique to this statement
int unique_accesses = 0;
for (auto a : array_use_map) {
if (a.second.size() != 1)
continue;
for (auto b : a.second) {
if (b == stmt_num)
unique_accesses++;
}
}
bool explore_symmetry = (unique_accesses >= 2 && substmts.size() >= 2);
if (substmts.size() == 2) {
exprNode *rhs1 = substmts[0]->get_rhs_expr();
exprNode *rhs2 = substmts[1]->get_rhs_expr();
explore_symmetry &= !(rhs1->is_shiftvec_node() && rhs2->is_shiftvec_node());
}
if (explore_symmetry) {
//Right now, the algorithm is based on string matching
vector<string> stringified_substmt_rhs;
vector<set<string>> substmt_accessed_arrays;
map<string,set<int>> substmt_array_use_map;
int substmt_num = 0;
for (auto s : substmts) {
stringstream rhs_out;
exprNode *rhs = s->get_rhs_expr ();
rhs->print_node (rhs_out);
stringified_substmt_rhs.push_back (rhs_out.str());
//Compute the accesses for each substatement
set<string> arrays;
s->accessed_arrays (arrays);
//Retain only arrays that are unique to this statement
for (set<string>::iterator it=arrays.begin(); it!=arrays.end();) {
if (array_use_map[*it].size() == 1)
it++;
else
it = arrays.erase(it);
}
substmt_accessed_arrays.push_back (arrays);
for (auto a : arrays) {
if (substmt_array_use_map.find (a) != substmt_array_use_map.end())
substmt_array_use_map[a].insert (substmt_num);
else {
set<int> st;
st.insert (substmt_num);
substmt_array_use_map[a] = st;
}
}
substmt_num++;
cout << "stringified substmt = " << rhs_out.str() << "\n";
cout << "Accessed arrays = ";
for (auto a : arrays)
cout << a << " ";
cout << "\n";
}
if (DEBUG) {
cout << "Substmt accessed array map :\n";
for (auto a : substmt_array_use_map) {
cout << a.first << " : (";
for (auto b : a.second) {
cout << b << " ";
}
cout << ")\n";
}
}
//Now create disjoint subsets of the substatements based on the array use
//These are the limitations: Right now, we assume that only two disjoint
//groups can be created, based on two arrays
set<int> substmt_union;
for (auto a : substmt_array_use_map) {
substmt_union.insert (a.second.begin(), a.second.end());
}
if ((substmt_array_use_map.size() == 2) && ((int)substmt_union.size() == substmt_num)) {
//Try to make pairs
set<OP_TYPE> final_op;
set<int> domain, range;
string domain_arr, range_arr;
int index = 0;
for (auto a : substmt_array_use_map) {
if (index == 0) {
domain_arr = trim_string (a.first, "[");
domain = a.second;
}
else if (index == 1) {
range_arr = trim_string (a.first, "[");
range = a.second;
}
index++;
}
bool symmetrical = (domain.size() == range.size());
if (symmetrical) {
set<int> range_mirror;
for (auto d : domain) {
string domain_expr = stringified_substmt_rhs[d];
bool found = false;
for (set<int>::iterator r=range.begin(); r!=range.end(); r++) {
if (range_mirror.find (*r) != range_mirror.end())
continue;
string range_expr = stringified_substmt_rhs[*r];
//Replace all occurrences of range_arr with domain_arr
boost::replace_all (range_expr, range_arr+"[", domain_arr+"[");
if (range_expr.compare(domain_expr) == 0) {
cout << "Found same strings for domain " << d << " and range " << *r << endl;
STMT_OP domain_op = substmts[d]->get_op_type();
STMT_OP range_op = substmts[*r]->get_op_type();
if ((domain_op == ST_EQ && range_op == ST_MINUSEQ) ||
(domain_op == ST_PLUSEQ && range_op == ST_MINUSEQ) ||
(domain_op == ST_MINUSEQ && range_op == ST_PLUSEQ))
final_op.insert (T_MINUS);
else if ((domain_op == ST_EQ && range_op == ST_PLUSEQ) ||
(domain_op == ST_MINUSEQ && range_op == ST_MINUSEQ) ||
(domain_op == ST_PLUSEQ && range_op == ST_PLUSEQ))
final_op.insert (T_PLUS);
else
symmetrical = false;
found = true;
range_mirror.insert (*r);
}
}
symmetrical &= found && (final_op.size() == 1);
if (!symmetrical) break;
}
}
if (symmetrical) {
//Get the dimensionality and subscripts of the domain array
cout << "SYMMETRICAL\n";
bool same_subscripts = true;
vector<string> iters;
DATA_TYPE type;
for (auto d : domain) {
exprNode *rhs = substmts[d]->get_rhs_expr();
same_subscripts &= rhs->same_subscripts_for_array (domain_arr, iters, type);
}
if (same_subscripts) {
//Make changes to the IR: retain only the substatements in domain
vector<stmtNode*> dom_stmts;
for (auto d : domain) {
dom_stmts.push_back (substmts[d]);
}
substmts.clear();
substmts = dom_stmts;
//Replace all the occurrences of domain_arr with a new array
string sym_arr = "sym_" + domain_arr;
for (auto s : substmts) {
exprNode *rhs = s->get_rhs_expr();
rhs->replace_array_name (domain_arr, sym_arr);
}
//Add initialization for the new array
vector<exprNode*> index;
for (auto it : iters) {
if (all_of(it.begin(), it.end(), ::isdigit)) {
int value = atoi(it.c_str());
exprNode *idx = new datatypeNode<int>(value, INT);
index.push_back (idx);
}
else {
exprNode *idx = new idNode (it, type, false);
index.push_back (idx);
}
}
exprNode *lhs = new shiftvecNode (sym_arr, type, false);
dynamic_cast<shiftvecNode*>(lhs)->set_indices (index);
exprNode *rhs1 = new shiftvecNode (domain_arr, type, false);
dynamic_cast<shiftvecNode*>(rhs1)->set_indices (index);
exprNode *rhs2 = new shiftvecNode (range_arr, type, false);
dynamic_cast<shiftvecNode*>(rhs2)->set_indices (index);
exprNode *rhs = new binaryNode(*(final_op.begin()), rhs1, rhs2);
stmtNode *init_stmt = new stmtNode (ST_EQ, lhs, rhs);
//Insert initialization to substmts
//substmts.insert (substmts.begin(), init_stmt);
init_stmts.push_back (init_stmt);
//Mark the new sym array for declaration
sym_initializations.push_back (make_tuple(domain_arr, sym_arr));
}
}
}
}
}
bool stencilDefn::halo_exists (void) {
bool halo_exists = false;
for (auto iter : iterators) {
int halo_lo = (halo_dims.find (iter) != halo_dims.end()) ? get<0>(halo_dims[iter]) : 0;
int halo_hi = (halo_dims.find (iter) != halo_dims.end()) ? get<1>(halo_dims[iter]) : 0;
halo_exists |= (halo_lo != 0 || halo_hi != 0);
}
return halo_exists;
}
void stencilDefn::set_halo_dims (map<string,tuple<int,int>> hd) {
halo_dims = hd;
// Fill in the missing values
for (auto iter : get_iterators ()) {
if (halo_dims.find (iter) == halo_dims.end ())
halo_dims[iter] = make_tuple (0, 0);
// Remove the halo along the streaming dimension
else if (stream && iter.compare (stream_dim) == 0)
halo_dims[iter] == make_tuple (0, 0);
}
//// Make the halo along coalescing dimension 0
//halo_dims[(get_iterators()).back()] = make_tuple (0, 0);
}
void stencilDefn::reset_halo_dims (void) {
for (auto iter : get_iterators ()) {
halo_dims[iter] = make_tuple (0, 0);
}
}
void stencilDefn::set_loop_gen_strategy (bool b) {
//for (auto hd : halo_dims) {
// b &= (get<0>(hd.second) == 0 && get<1>(hd.second) == 0);
//}
generate_if = b;
}
void stencilDefn::set_loop_prefetch_strategy (bool p) {
prefetch = p;
}
void stencilDefn::set_load_style (bool b) {
//bool no_halo = true;
//for (auto hd : halo_dims) {
// no_halo &= (get<0>(hd.second) == 0 && get<1>(hd.second) == 0);
//}
blocked_loads = b;
}
void stencilDefn::set_unroll_decls (symtabTemplate <int> *u) {
unroll_decls = u;
//Fill in the missing values
for (auto iter: get_iterators ()) {
if (!(unroll_decls->symbol_present (iter))) {
unroll_decls->push_symbol (iter, 1);
}
// Remove any unrolling along the streaming dimension
if (stream && iter.compare (stream_dim) == 0) {
int uf = unroll_decls->find_symbol (iter);
stream_uf = uf > 1 ? uf : stream_uf;
unroll_decls->delete_symbol (iter);
unroll_decls->push_symbol (iter, 1);
}
}
}
void stencilDefn::unroll_stmts (void) {
map<string,int> udecls = get_unroll_decls ();
for (auto u : udecls) {
map<string,int> scalar_version_map;
stmtList *new_stmts = new stmtList ();
for (int val=1; val<u.second; val++) {
for (auto stmt : stmt_list->get_stmt_list ()) {
exprNode *lhs = stmt->get_lhs_expr ();
exprNode *rhs = stmt->get_rhs_expr ();
STMT_OP stmt_type = stmt->get_op_type ();
// Generate unrolled statements
exprNode *unroll_lhs = lhs->unroll_expr (u.first, val, scalar_version_map, true);
exprNode *unroll_rhs = rhs->unroll_expr (u.first, val, scalar_version_map, false);
new_stmts->push_stmt (new stmtNode (stmt_type, unroll_lhs, unroll_rhs, stmt->get_stmt_domain()));
}
}
stmt_list->push_stmt (new_stmts->get_stmt_list ());
// Add the new variables to var_decls
for (auto svm : scalar_version_map) {
string var_name = svm.first;
assert (is_var_decl (var_name));
DATA_TYPE type = get_var_type (var_name);
for (int val = 1; val <svm.second; val++) {
string new_name = var_name + "_" + to_string (val);
push_var_decl (new_name, type);
}
}
}
}
void stencilDefn::set_data_type (void) {
vector<stmtNode*> stmts = stmt_list->get_stmt_list ();
map<string, DATA_TYPE> data_types;
for (auto ad : array_decls) {
data_types[ad->get_array_name()] = ad->get_array_type ();
}
for (auto vd : var_decls->get_symbol_map ()) {
data_types[vd.first] = vd.second;
}
for (auto vd : var_init_decls) {
data_types[get<1>(vd)] = get<0>(vd);
}
for (auto stmt : stmts) {
exprNode *lhs = stmt->get_lhs_expr ();
exprNode *rhs = stmt->get_rhs_expr ();
lhs->set_data_type (data_types);
rhs->set_data_type (data_types);
}
}
void stencilDefn::print_stencil_body (string stencil_name, map<int,vector<int>> &clusters, stringstream &out) {
vector<string> args = get_arg_list ();
vector<stmtNode*> stmts = get_stmt_list ();
int cluster_num = 0;
for (map<int,vector<int>>::iterator c=clusters.begin(); c!=clusters.end(); c++,cluster_num++) {
vector<int> cluster_stmts = c->second;
cluster_stmts.push_back (c->first);
// Get the accesess in the statements
set<string> accesses;
for (auto stmt : stmts) {
int stmt_num = stmt->get_stmt_num ();
if (find (cluster_stmts.begin(), cluster_stmts.end(), stmt_num) == cluster_stmts.end())
continue;
stringstream lhs_out, rhs_out;
exprNode *lhs = stmt->get_lhs_expr ();
exprNode *rhs = stmt->get_rhs_expr ();
lhs->collect_accesses (accesses);
rhs->collect_accesses (accesses);
}
out << "\nstencil " << stencil_name;
if (clusters.size() > 0)
out << cluster_num;
out << " (";
bool first = true;
for (vector<string>::iterator a=args.begin(); a!=args.end(); a++) {
if (accesses.find (*a) == accesses.end())
continue;
if (!first)
out << ", ";
out << *a;
first = false;
}
out << ") {\n";
// Print temporary variables
map<string,DATA_TYPE> tvd_map = get_var_decls ();
if (!tvd_map.empty ()) {
for (map<string,DATA_TYPE>::iterator m=tvd_map.begin(); m!=tvd_map.end(); m++) {
if (accesses.find (m->first) == accesses.end())
continue;
out << "\t" << print_data_type (m->second) << m->first << ";\n";
}
}
// Print temporary array declarations
vector<arrayDecl*> tad = get_array_decls ();
if (!tad.empty ()) {
for (vector<arrayDecl*>::iterator a=tad.begin(); a!=tad.end(); a++) {
string name = (*a)->get_array_name ();
if (accesses.find (name) == accesses.end())
continue;
DATA_TYPE t = (*a)->get_array_type ();
out << "\t" << print_data_type (t) << name;
vector<Range*> range = (*a)->get_array_range ();
for (auto r : range) {
exprNode *lo = r->get_lo_range ();
stringstream lo_out;
lo->print_node (lo_out);
exprNode *hi = r->get_hi_range ();
stringstream hi_out;
hi->print_node (hi_out);
out << "[" << lo_out.str() << ":" << hi_out.str() << "]";
}
out << ";\n";
}
}
// Print temporary variables with initializations
for (auto vd : var_init_decls) {
if (accesses.find (get<1>(vd)) == accesses.end())
continue;
out << indent << print_data_type (get<0>(vd));
out << get<1>(vd);
out << " = ";
(get<2>(vd))->print_node (out);
out << ";\n";
}
// Print the shared memory placements
vector<string> shmem_decls = get_shmem_decl ();
if (!shmem_decls.empty()) {
string shmem_list;
bool first = true;
for (vector<string>::iterator s=shmem_decls.begin(); s!=shmem_decls.end(); s++) {
if (accesses.find (*s) == accesses.end())
continue;
if (!first)
shmem_list = shmem_list + ", ";
shmem_list = shmem_list + *s;
first = false;
}
if (!shmem_list.empty ()) {
out << indent << "shmem ";
out << shmem_list;
out << ";\n";
}
}
// Print the no-shared memory placements
vector<string> noshmem_decls = get_noshmem_decl ();
if (!noshmem_decls.empty()) {
string noshmem_list;
bool first = true;
for (vector<string>::iterator s=noshmem_decls.begin(); s!=noshmem_decls.end(); s++) {
if (accesses.find (*s) == accesses.end())
continue;
if (!first)
noshmem_list = noshmem_list + ", ";
noshmem_list = noshmem_list + *s;
first = false;
}
if (!noshmem_list.empty ()) {
out << indent << "no-shmem ";
out << noshmem_list;
out << ";\n";
}
}
// Print the global memory placements
vector<string> gmem_decls = get_gmem_decl ();
if (!gmem_decls.empty()) {
string gmem_list;
bool first = true;
for (vector<string>::iterator s=gmem_decls.begin(); s!=gmem_decls.end(); s++) {
if (accesses.find (*s) == accesses.end())
continue;
if (!first)
gmem_list = gmem_list + ", ";
gmem_list = gmem_list + *s;
first = false;
}
if (!gmem_list.empty ()) {
out << indent << "gmem ";
out << gmem_list;
out << ";\n";
}
}
// Print each statement
for (auto stmt : stmts) {
int stmt_num = stmt->get_stmt_num ();
if (find (cluster_stmts.begin(), cluster_stmts.end(), stmt_num) == cluster_stmts.end())
continue;
stringstream lhs_out, rhs_out;
exprNode *lhs = stmt->get_lhs_expr ();
exprNode *rhs = stmt->get_rhs_expr ();
STMT_OP op_type = stmt->get_op_type ();
lhs->print_node (lhs_out);
rhs->print_node (rhs_out);
out << indent << lhs_out.str () << print_stmt_op (op_type) << rhs_out.str () << ";\n";
}
out << "}\n";
}
}
void stencilDefn::identify_separable_clusters (string name, map<int,vector<int>> &clusters) {
map<int, vector<int>> unified_dependences;
unify_dependences (unified_dependences);
if (DEBUG) {
cout << "\nUnified dependence graph for stencil " << name << " : " << endl;
if (!unified_dependences.empty ()) {
for (auto d : unified_dependences) {
cout << "\t" << d.first << " - ";
vector<int> rhs = d.second;
for (auto s : d.second)
cout << s << " ";
cout << endl;
}
}
}
// Are there multiple output statements that are not source?
vector<int> sinks, dep_dest, dep_src;
for (auto d : unified_dependences) {
dep_dest.push_back (d.first);
dep_src.insert (dep_src.end(), (d.second).begin(), (d.second).end());
}
for (auto d : dep_dest) {
if (find (dep_src.begin(), dep_src.end(), d) == dep_src.end())
sinks.push_back (d);
}
if (DEBUG) {
cout << "Independent statements for stencil " << name << " : ";
for (auto o : sinks)
cout << o << " ";
cout << endl;
}
map<int, vector<int>> expanded_deps;
expanded_deps.insert (unified_dependences.begin(), unified_dependences.end());
for (auto d : unified_dependences) {
int key = d.first;
for (auto e : expanded_deps) {
vector<int> &val = e.second;
if (find (val.begin(), val.end(), key) != val.end()) {
//val.erase (remove(val.begin(), val.end(), key), val.end());
for (auto f : d.second) {
if (find (val.begin(), val.end(), f) == val.end())
val.push_back (f);
}
}
expanded_deps[e.first] = val;
}
}
for (auto s : sinks) {
clusters[s] = expanded_deps[s];
}
// If there are two clusers such that they have the same
// array LHS, and one of them is a +=, then merge them
map<int, vector<tuple<string,STMT_OP>>> lhs_op_map;
vector<stmtNode*> orig_stmts = stmt_list->get_stmt_list ();
for (auto c : clusters) {
vector<tuple<string,STMT_OP>> lhs_acc;
vector<int> stmt_vec = c.second;
stmt_vec.push_back (c.first);
for (auto p : stmt_vec) {
string lhs_name = (orig_stmts[p]->get_lhs_expr())->get_name ();
if (is_array_decl (lhs_name)) {
STMT_OP op_type = orig_stmts[p]->get_op_type ();
tuple<string,STMT_OP> tp = make_tuple (lhs_name, op_type);
if (find (lhs_acc.begin(), lhs_acc.end(), tp) == lhs_acc.end())
lhs_acc.push_back (tp);
}
}
lhs_op_map[c.first] = lhs_acc;
}
for (map<int,vector<int>>::iterator it=clusters.begin(); it!=clusters.end(); it++) {
vector<tuple<string,STMT_OP>> it_vec = lhs_op_map[it->first];
for (map<int,vector<int>>::iterator jt=next(it); jt!=clusters.end(); jt++) {
vector<tuple<string,STMT_OP>> jt_vec = lhs_op_map[jt->first];
// Check if it and jt have the same lhs with one (or both) +=. Then they can't be separated
bool merge = false;
for (auto a : it_vec) {
string a_lhs = get<0>(a);
STMT_OP a_op = get<1>(a);
for (auto b : jt_vec) {
string b_lhs = get<0>(b);
STMT_OP b_op = get<1>(b);
if ((a_lhs.compare(b_lhs)==0) && ((a_op != ST_EQ) || (b_op != ST_EQ)))
merge = true;
}
}
if (merge) {
// Put jt_vec into it_vec
for (auto b : jt_vec) {
if (find (it_vec.begin(), it_vec.end(), b) == it_vec.end())
it_vec.push_back (b);
}
lhs_op_map[it->first] = it_vec;
// Mere jt and it in clusters
for (auto rhs : jt->second) {
if (find ((it->second).begin(), (it->second).end(), rhs) == (it->second).end())
(it->second).push_back (rhs);
}
int lhs = jt->first;
if (find ((it->second).begin(), (it->second).end(), lhs) == (it->second).end())
(it->second).push_back (lhs);
jt = clusters.erase (jt);
}
}
}
if (DEBUG) {
cout << "Clusters for stencil " << name << " :\n";
for (auto c : clusters) {
cout << "\t" << c.first << " : ";
for (auto l : c.second) {
cout << l << " ";
}
cout << endl;
}
}
}
void stencilDefn::print_stencil_defn (string stencil_name) {
cout << "\nAfter offsetting planes, stencil " << stencil_name << "{\n";
vector<stmtNode*> stmts = stmt_list->get_stmt_list ();
for (auto st: stmts) {
for (auto stmt : decomposed_stmts[st->get_stmt_num()]) {
stringstream lhs_out, rhs_out;
exprNode *lhs = stmt->get_lhs_expr ();
exprNode *rhs = stmt->get_rhs_expr ();
STMT_OP op_type = stmt->get_op_type ();
lhs->print_node (lhs_out);
rhs->print_node (rhs_out);
cout << "\t" << lhs_out.str () << print_stmt_op (op_type) << rhs_out.str () << ";\n";
}
}
cout << "}\n";
}
void stencilDefn::compute_hull (vector<Range*> &initial_domain, vector<int> pointwise_occurrences) {
// Map from array name to its hull
map<string, vector<Range*>> hull_map;
output_hull = vector<Range*> (initial_domain);
vector<stmtNode*> stmts = stmt_list->get_stmt_list ();
// Go through the statements and compute hull for each
for (auto st : stmts) {
int stmt_num = st->get_stmt_num ();
vector<Range*> lhs_hull = vector<Range*> (initial_domain);
vector<Range*> rhs_hull = vector<Range*> (initial_domain);
string lhs_name = (st->get_lhs_expr())->get_name ();
// Get the offset if the accesses along stream dimension need amendment
int offset = (stream && (plane_offset.find(stmt_num) != plane_offset.end ())) ? plane_offset[stmt_num] : 0;
for (vector<stmtNode*>::iterator it=decomposed_stmts[stmt_num].begin(); it!=decomposed_stmts[stmt_num].end(); it++) {
exprNode *lhs = (*it)->get_lhs_expr ();
exprNode *rhs = (*it)->get_rhs_expr ();
rhs->compute_hull (hull_map, rhs_hull, initial_domain, stream, stream_dim, offset);
if (!retiming_feasible && (*it)->get_op_type() != ST_EQ)
lhs->compute_hull (hull_map, rhs_hull, initial_domain, stream, stream_dim, offset);
// Intersect lhs_hull with the statement domain if non empty
vector<Range*> initial_stmt_domain = (*it)->get_stmt_domain ();
if (!initial_stmt_domain.empty ()) {
int pos = 0;
vector<Range*>::iterator b_dom = initial_stmt_domain.begin();
for (vector<Range*>::iterator a_dom=lhs_hull.begin(); a_dom!=lhs_hull.end(); a_dom++,b_dom++,pos++) {
// Intersection of lo_a and lo_b;
exprNode *lo_a = (*a_dom)->get_lo_range ();
exprNode *lo_b = (*b_dom)->get_lo_range ();
string lo_a_id = "", lo_b_id = "";
int lo_a_val = 0, lo_b_val = 0;
lo_a->decompose_access_fsm (lo_a_id, lo_a_val);
lo_b->decompose_access_fsm (lo_b_id, lo_b_val);
assert (lo_a_id.compare (lo_b_id) == 0);
exprNode *lo = (lo_a_val >= lo_b_val) ? lo_a : lo_b;
// Intersection of hi_a and hi_b;
exprNode *hi_a = (*a_dom)->get_hi_range ();
exprNode *hi_b = (*b_dom)->get_hi_range ();
string hi_a_id = "", hi_b_id = "";
int hi_a_val = 0, hi_b_val = 0;
hi_a->decompose_access_fsm (hi_a_id, hi_a_val);
hi_b->decompose_access_fsm (hi_b_id, hi_b_val);
assert (hi_a_id.compare (hi_b_id) == 0);
exprNode *hi = (hi_a_val <= hi_b_val) ? hi_a : hi_b;
lhs_hull[pos] = new Range (lo, hi);
}
}
map<string, vector<Range*>> empty_map;
if (retiming_feasible) {
empty_map[lhs_name] = vector<Range*> (rhs_hull);
}
lhs->compute_hull (empty_map, lhs_hull, initial_domain, stream, stream_dim, offset);
// Intersect lhs_hull with rhs_hull
int pos = 0;
vector<Range*>::iterator b_dom = rhs_hull.begin();
for (vector<Range*>::iterator a_dom=lhs_hull.begin(); a_dom!=lhs_hull.end(); a_dom++,b_dom++,pos++) {
// Intersection of lo_a and lo_b;
exprNode *lo_a = (*a_dom)->get_lo_range ();
exprNode *lo_b = (*b_dom)->get_lo_range ();
string lo_a_id = "", lo_b_id = "";
int lo_a_val = 0, lo_b_val = 0;
lo_a->decompose_access_fsm (lo_a_id, lo_a_val);
lo_b->decompose_access_fsm (lo_b_id, lo_b_val);
assert (lo_a_id.compare (lo_b_id) == 0);
exprNode *lo = (lo_a_val >= lo_b_val) ? lo_a : lo_b;
// Intersection of hi_a and hi_b;
exprNode *hi_a = (*a_dom)->get_hi_range ();
exprNode *hi_b = (*b_dom)->get_hi_range ();
string hi_a_id = "", hi_b_id = "";
int hi_a_val = 0, hi_b_val = 0;
hi_a->decompose_access_fsm (hi_a_id, hi_a_val);
hi_b->decompose_access_fsm (hi_b_id, hi_b_val);
assert (hi_a_id.compare (hi_b_id) == 0);
exprNode *hi = (hi_a_val <= hi_b_val) ? hi_a : hi_b;
lhs_hull[pos] = new Range (lo, hi);
}
}
// Put the lhs hull into hull_map
hull_map[lhs_name] = vector<Range*> (lhs_hull);
// Populate the stmt_hull map
stmt_hull[stmt_num] = make_tuple (lhs_hull, rhs_hull);
// Intersect lhs_hull with output_hull
int pos = 0;
vector<Range*>::iterator d_dom = lhs_hull.begin();
for (vector<Range*>::iterator c_dom=output_hull.begin(); c_dom!=output_hull.end(); c_dom++,d_dom++,pos++) {
// Intersection of lo_c and lo_b;
exprNode *lo_c = (*c_dom)->get_lo_range ();
exprNode *lo_d = (*d_dom)->get_lo_range ();
string lo_c_id = "", lo_d_id = "";
int lo_c_val = 0, lo_d_val = 0;
lo_c->decompose_access_fsm (lo_c_id, lo_c_val);
lo_d->decompose_access_fsm (lo_d_id, lo_d_val);
assert (lo_c_id.compare (lo_d_id) == 0);
exprNode *lo = (lo_c_val >= lo_d_val) ? lo_c : lo_d;
// Intersection of hi_c and hi_d;
exprNode *hi_c = (*c_dom)->get_hi_range ();
exprNode *hi_d = (*d_dom)->get_hi_range ();
string hi_c_id = "", hi_d_id = "";
int hi_c_val = 0, hi_d_val = 0;
hi_c->decompose_access_fsm (hi_c_id, hi_c_val);
hi_d->decompose_access_fsm (hi_d_id, hi_d_val);
assert (hi_c_id.compare (hi_d_id) == 0);
exprNode *hi = (hi_c_val <= hi_d_val) ? hi_c : hi_d;
output_hull[pos] = new Range (lo, hi);
}
}
// Adjust hull for pointwise occurrences
for (int it=stmts.size()-2; it>=0; it--) {
// Is the lhs of this statement a pointwise occurrence?
int src_num = stmts[it]->get_stmt_num ();
if (find (pointwise_occurrences.begin(), pointwise_occurrences.end(), src_num) == pointwise_occurrences.end())
continue;
// Check all the dependences, take their intersection with the lhs of this statement
vector<Range*> &src_hull = get<0>(stmt_hull[src_num]);
for (int jt=it+1; jt<(int)stmts.size(); jt++) {
int dest_num = stmts[jt]->get_stmt_num ();
if (raw_dependence_exists (src_num, dest_num) || waw_dependence_exists (src_num, dest_num) || accumulation_dependence_exists (src_num, dest_num) || war_dependence_exists (src_num, dest_num)) {
vector<Range*> dest_hull = get<0>(stmt_hull[dest_num]);
// Take intersection of src_hull and dest_hull
vector<Range*>::iterator b_dom = dest_hull.begin ();
int pos = 0;
for (vector<Range*>::iterator a_dom=src_hull.begin(); a_dom!=src_hull.end(); a_dom++,b_dom++,pos++) {
// Intersection of lo_a and lo_b;
exprNode *lo_a = (*a_dom)->get_lo_range ();
exprNode *lo_b = (*b_dom)->get_lo_range ();
string lo_a_id = "", lo_b_id = "";
int lo_a_val = 0, lo_b_val = 0;
lo_a->decompose_access_fsm (lo_a_id, lo_a_val);
lo_b->decompose_access_fsm (lo_b_id, lo_b_val);
assert (lo_a_id.compare (lo_b_id) == 0);
exprNode *lo = (lo_a_val >= lo_b_val) ? lo_a : lo_b;
// Intersection of hi_a and hi_b;
exprNode *hi_a = (*a_dom)->get_hi_range ();
exprNode *hi_b = (*b_dom)->get_hi_range ();
string hi_a_id = "", hi_b_id = "";
int hi_a_val = 0, hi_b_val = 0;
hi_a->decompose_access_fsm (hi_a_id, hi_a_val);
hi_b->decompose_access_fsm (hi_b_id, hi_b_val);
assert (hi_a_id.compare (hi_b_id) == 0);
exprNode *hi = (hi_a_val <= hi_b_val) ? hi_a : hi_b;
src_hull[pos] = new Range (lo, hi);
}
break;
}
}
}
// Print the hull after correction
if (DEBUG) {
for (auto hull : hull_map) {
cout << indent << "hull for array " << hull.first << " : [";
for (vector<Range *>::iterator d=(hull.second).begin(); d!=(hull.second).end(); d++) {
exprNode *lo = (*d)->get_lo_range ();
exprNode *hi = (*d)->get_hi_range ();
string lo_id = "", hi_id = "";
int lo_val = 0, hi_val = 0;
lo->decompose_access_fsm (lo_id, lo_val);
hi->decompose_access_fsm (hi_id, hi_val);
stringstream lo_out, hi_out;
if (!lo_id.empty ()) {
cout << lo_id;
if (lo_val > 0)
cout << "+";
}
cout << lo_val;
cout << ":";
if (!hi_id.empty()) {
cout << hi_id;
if (hi_val > 0)
cout << "+";
}
cout << hi_val;
if (d != prev((hull.second).end()))
cout << " ,";
}
cout << "]\n";
}
cout << indent << "overall domain hull : [";
for (vector<Range *>::iterator d=output_hull.begin(); d!=output_hull.end(); d++) {
exprNode *lo = (*d)->get_lo_range ();
exprNode *hi = (*d)->get_hi_range ();
string lo_id = "", hi_id = "";
int lo_val = 0, hi_val = 0;
lo->decompose_access_fsm (lo_id, lo_val);
hi->decompose_access_fsm (hi_id, hi_val);
stringstream lo_out, hi_out;
if (!lo_id.empty ()) {
cout << lo_id;
if (lo_val > 0)
cout << "+";
}
if (lo_val != 0)
cout << lo_val;
cout << ":";
if (!hi_id.empty()) {
cout << hi_id;
if (hi_val > 0)
cout << "+";
}
if (hi_val != 0)
cout << hi_val;
if (d != prev(output_hull.end()))
cout << " ,";
}
cout << "]\n";
}
}
// Iterate over all the producers in the DAG that have no predecessors, and take
// the intersection of their hull. At present, this is not enough, since there may
// be cases where the dependence is pointwise.
vector<Range*> stencilDefn::get_starting_hull (void) {
vector<Range*> ret = vector<Range*> (output_hull);
bool ret_set = false;
vector<stmtNode*> stmts = stmt_list->get_stmt_list ();
for (auto stmt : stmts) {
int stmt_num = stmt->get_stmt_num ();
if ((raw_dependence_graph.find (stmt_num) == raw_dependence_graph.end ()) &&
(raw_dependence_graph.find (stmt_num) == raw_dependence_graph.end ()) &&
(raw_dependence_graph.find (stmt_num) == raw_dependence_graph.end ())) {
// Take intersection
if (!ret_set)
ret = vector<Range*> (get<1>(resource_hull[stmt_num]));
else {
vector<Range*>::iterator b_dom = (get<1>(resource_hull[stmt_num])).begin();
int pos = 0;
for (vector<Range*>::iterator a_dom=ret.begin(); a_dom!=ret.end(); a_dom++,b_dom++,pos++) {
// Intersection of lo_a and lo_b;
exprNode *lo_a = (*a_dom)->get_lo_range ();
exprNode *lo_b = (*b_dom)->get_lo_range ();
string lo_a_id = "", lo_b_id = "";
int lo_a_val = 0, lo_b_val = 0;
lo_a->decompose_access_fsm (lo_a_id, lo_a_val);
lo_b->decompose_access_fsm (lo_b_id, lo_b_val);
assert (lo_a_id.compare (lo_b_id) == 0);
exprNode *lo = (lo_a_val >= lo_b_val) ? lo_a : lo_b;
// Intersection of hi_a and hi_b;
exprNode *hi_a = (*a_dom)->get_hi_range ();
exprNode *hi_b = (*b_dom)->get_hi_range ();
string hi_a_id = "", hi_b_id = "";
int hi_a_val = 0, hi_b_val = 0;
hi_a->decompose_access_fsm (hi_a_id, hi_a_val);
hi_b->decompose_access_fsm (hi_b_id, hi_b_val);
assert (hi_a_id.compare (hi_b_id) == 0);
exprNode *hi = (hi_a_val <= hi_b_val) ? hi_a : hi_b;
ret[pos] = new Range (lo, hi);
}
}
ret_set = true;
}
}
// If ret is empty, create a full domain and return it
if (ret.empty ()) {
for (auto p : parameters) {
// lo expr is 0
exprNode *lo = new datatypeNode<int>(0, INT);
exprNode *hi = new binaryNode (T_MINUS, new idNode (p), new datatypeNode<int>(1, INT), false);
ret.push_back (new Range (lo, hi));
}
}
cout << indent << "ret hull : [";
for (vector<Range *>::iterator d=ret.begin(); d!=ret.end(); d++) {
exprNode *lo = (*d)->get_lo_range ();
exprNode *hi = (*d)->get_hi_range ();
string lo_id = "", hi_id = "";
int lo_val = 0, hi_val = 0;
lo->decompose_access_fsm (lo_id, lo_val);
hi->decompose_access_fsm (hi_id, hi_val);
stringstream lo_out, hi_out;
if (!lo_id.empty ()) {
cout << lo_id;
if (lo_val > 0)
cout << "+";
}
if (lo_val != 0 || lo_id.empty ())
cout << lo_val;
cout << ":";
if (!hi_id.empty()) {
cout << hi_id;
if (hi_val > 0)
cout << "+";
}
if (hi_val != 0 || hi_id.empty ())
cout << hi_val;
if (d != prev(ret.end()))
cout << " ,";
}
cout << "]\n";
return ret;
}
void stencilDefn::compute_resource_hull (vector<Range*> &initial_domain, vector<int> pointwise_occurrences) {
// Map from array name to its hull
map<string, vector<Range*>> hull_map;
// Modify the initial domain to go from 0 to MAX. For halo_dims, account for the halo
vector<Range*> full_domain;
int pos = 0;
for (vector<Range*>::iterator it=initial_domain.begin(); it!=initial_domain.end(); it++, pos++) {
string iter = iterators[pos];
int init_lo = 0, init_hi = -1;
if (halo_dims.find (iter) != halo_dims.end ()) {
tuple<int,int> halo = halo_dims[iter];
if (get<0>(halo) < 0)
init_lo += get<0>(halo);
if (get<1>(halo) > 0)
init_hi += get<1>(halo);
}
exprNode *lo = new datatypeNode<int> (init_lo, INT);
exprNode *hi = new datatypeNode<int> (init_hi, INT);
full_domain.push_back (new Range (lo, hi));
}
// Initialize the final compute hull
recompute_hull = vector<Range*> (full_domain);
vector<stmtNode*> stmts = stmt_list->get_stmt_list ();
// Go through the statements and compute hull for each
for (auto st : stmts) {
int stmt_num = st->get_stmt_num ();
vector<Range*> lhs_hull = vector<Range*> (full_domain);
vector<Range*> rhs_hull = vector<Range*> (full_domain);
string lhs_name = (st->get_lhs_expr())->get_name ();
// Get the offset if the accesses along stream dimension need amendment
int offset = (stream && (plane_offset.find(stmt_num) != plane_offset.end ())) ? plane_offset[stmt_num] : 0;
for (vector<stmtNode*>::iterator it=decomposed_stmts[stmt_num].begin(); it!=decomposed_stmts[stmt_num].end(); it++) {
exprNode *lhs = (*it)->get_lhs_expr ();
exprNode *rhs = (*it)->get_rhs_expr ();
int stmt_num = (*it)->get_stmt_num ();
rhs->compute_hull (hull_map, rhs_hull, full_domain, stmt_resource_map[stmt_num], stream, stream_dim, offset);
if (!retiming_feasible && (*it)->get_op_type () != ST_EQ)
lhs->compute_hull (hull_map, rhs_hull, full_domain, stmt_resource_map[stmt_num], stream, stream_dim, offset);
// Intersect lhs_hull with the statement domain if non empty, and all accesses are not global memory