-
Notifications
You must be signed in to change notification settings - Fork 5
/
omegatools.cc
4644 lines (3956 loc) · 154 KB
/
omegatools.cc
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
/*****************************************************************************
Copyright (C) 2008 University of Southern California
Copyright (C) 2009-2010 University of Utah
All Rights Reserved.
Purpose:
Useful tools involving Omega manipulation.
Notes:
History:
01/2006 Created by Chun Chen.
03/2009 Upgrade Omega's interaction with compiler to IR_Code, by Chun Chen.
*****************************************************************************/
#include <codegen.h>
#include "omegatools.hh"
#include "ir_code.hh"
#include "chill_error.hh"
#include "chill_ast.hh"
#include "code_gen/CG_stringRepr.h"
#include "code_gen/CG_chillRepr.h"
#include <code_gen/CG_utils.h>
using namespace omega;
namespace {
struct DependenceLevel {
Relation r;
int level;
int dir; // direction upto current level:
// -1:negative, 0: undetermined, 1: postive
std::vector<coef_t> lbounds;
std::vector<coef_t> ubounds;
DependenceLevel(const Relation &_r, int _dims) :
r(_r), level(0), dir(0), lbounds(_dims), ubounds(_dims) {}
};
struct LinearTerm {
int coefficient;
std::vector<std::string> term;
bool is_function;
std::vector<LinearTerm> args;
void dump() {
if (term.size() == 0)
std::cout << "NO STRINGS IN LINEAR TERM\n";
for (int i = 0; i < term.size(); i++) {
printf(" term is: %s\n", term[i].c_str());
}
std::cout << coefficient << "\n";
}
bool operator==(const LinearTerm &that) const {
if (this->coefficient != that.coefficient)
return false;
if (this->is_function != that.is_function)
return false;
if ((this->term.size() == 0) && (that.term.size() == 0))
return true;
if ((this->term.size() > 0) && (that.term.size() > 0)) {
if (this->term.size() != that.term.size())
return false;
for (int i = 0; i < this->term.size(); i++) {
int j;
bool matched = false;;
for (j = 0; j < that.term.size(); j++) {
if (this->term[i].size() == that.term[j].size()
&& this->term[i] == that.term[j]) {
if (this->is_function) {
for (int k = 0; k < this->args.size(); k++) {
int j;
for (; j < that.args.size(); j++)
if (this->args[k] == that.args[j])
break;
if (j == that.args.size())
return false;
else {
matched = true;
break;
}
}
} else {
matched = true;
break;
}
}
if (matched)
break;
}
if (j == that.term.size())
return false;
}
return true;
}
return false;
}
bool operator!=(const LinearTerm &that) const {
return !(*this == that);
}
bool operator<(const LinearTerm &that) const {
if (this->term.size() < that.term.size())
return true;
else if (this->term.size() > that.term.size())
return false;
else if (this->coefficient < that.coefficient)
return true;
else if (this->coefficient > that.coefficient)
return false;
else if (this->term < that.term)
return true;
else
return false;
}
};
std::string dumpargs(std::vector<std::string> &v) {
// Mahdi: fixed a bug, empty argument list was not handlesd correctly.
if (v.size() == 0) return "";
std::string str = "(";
for (int i = 0 ; i < v.size() ; i++ ) {
if ( i ) str += ",";
str += v[i];
}
str += ")";
return str;
}
bool compareTerm(LinearTerm one, LinearTerm two) {
if ((one.term.size() == 0) && (two.term.size() == 0))
return true;
if ((one.term.size() > 0) && (two.term.size() > 0)) {
if (one.term.size() != two.term.size())
return false;
for (int i = 0; i < one.term.size(); i++) {
int j;
for (j = 0; j < two.term.size(); j++)
if (one.term[i] == two.term[j])
break;
if (j == two.term.size())
return false;
}
return true;
}
return false;
}
bool checkEquivalence(std::vector<LinearTerm> v1, std::vector<LinearTerm> v2) {
if (v1.size() != v2.size())
return false;
for (int i = 0; i < v1.size(); i++) {
int j;
for (j = 0; j < v2.size(); j++) {
if (v1[i] == v2[j] && (v1[i].coefficient == v2[j].coefficient))
break;
}
if (j == v2.size())
return false;
}
return true;
}
std::vector<LinearTerm> recursiveConstructLinearExpression(
CG_outputRepr *repr_src, IR_Code *ir, char side) {
std::vector<LinearTerm> v;
switch (ir->QueryExpOperation(repr_src)) {
case IR_OP_CONSTANT: {
std::vector<CG_outputRepr *> v_ = ir->QueryExpOperand(repr_src);
IR_ConstantRef *ref = static_cast<IR_ConstantRef *>(ir->Repr2Ref(v_[0]));
LinearTerm to_push;
to_push.is_function = false;
to_push.coefficient = ref->integer();
to_push.term = std::vector<std::string>();
v.push_back(to_push);
break;
}
case IR_OP_MACRO: {
std::vector<CG_outputRepr *> v_ = ir->QueryExpOperand(repr_src);
// Fixme: I'm a hack
IR_FunctionRef *ref;
{
CG_chillRepr *crepr = (CG_chillRepr *) v_[0];
chillAST_node *node = crepr->chillnodes[0];
ref = new IR_chillFunctionRef(ir, static_cast<chillAST_DeclRefExpr *>(node));
}
LinearTerm to_push;
to_push.is_function = true;
std::string s = ref->name();
CG_outputRepr *arguments = ir->RetrieveMacro(s);
CG_outputRepr *inner = NULL;
if (ir->QueryExpOperation(arguments) == IR_OP_ARRAY_VARIABLE) {
std::vector<CG_outputRepr *> v = ir->QueryExpOperand(arguments);
IR_Ref *ref_ = ir->Repr2Ref(v[0]);
if (ref_->n_dim() > 1)
throw ir_error(
"Multi dimensional array in loop bounds: not supported currently!\n");
if (dynamic_cast<IR_PointerArrayRef *>(ref_) != NULL) {
inner = dynamic_cast<IR_PointerArrayRef *>(ref_)->index(0);
} else if (dynamic_cast<IR_ArrayRef *>(ref_) != NULL) {
inner = dynamic_cast<IR_ArrayRef *>(ref_)->index(0);
}
}
if (inner == NULL)
throw ir_error(
"Unrecognized IR node in recursiveConstructLinearExpression\n");
std::vector<std::string> to_push_2;
to_push_2.push_back(s);
to_push.coefficient = 1;
to_push.term = to_push_2;
to_push.args = recursiveConstructLinearExpression(inner->clone(), ir,
side);
v.push_back(to_push);
break;
}
case IR_OP_VARIABLE: {
std::vector<CG_outputRepr *> v_ = ir->QueryExpOperand(repr_src);
IR_ScalarRef *ref = static_cast<IR_ScalarRef *>(ir->Repr2Ref(v_[0]));
LinearTerm to_push;
to_push.is_function = false;
std::string s = ref->name();
if (side == 'r')
s += "p";
std::vector<std::string> to_push_2;
to_push_2.push_back(s);
to_push.coefficient = 1;
to_push.term = to_push_2;
v.push_back(to_push);
break;
}
case IR_OP_ARRAY_VARIABLE: {
std::vector<CG_outputRepr *> v_ = ir->QueryExpOperand(repr_src);
IR_Ref *ref = ir->Repr2Ref(v_[0]);
std::string s = ref->name();
CG_outputRepr *repr2;
assert(ref->n_dim() == 1);
if (dynamic_cast<IR_PointerArrayRef *>(ref) != NULL) {
repr2 = dynamic_cast<IR_PointerArrayRef *>(ref)->index(0);
} else if (dynamic_cast<IR_ArrayRef *>(ref) != NULL) {
repr2 = dynamic_cast<IR_ArrayRef *>(ref)->index(0);
}
LinearTerm to_push;
to_push.is_function = true;
to_push.args = recursiveConstructLinearExpression(repr2, ir, side);
//std::string s = ref->name();
std::vector<std::string> to_push_2;
to_push_2.push_back(s);
to_push.coefficient = 1;
to_push.term = to_push_2;
v.push_back(to_push);
break;
}
case IR_OP_PLUS: {
std::vector<CG_outputRepr *> v_ = ir->QueryExpOperand(repr_src);
std::vector<LinearTerm> v1 = recursiveConstructLinearExpression(v_[0],
ir, side);
std::vector<LinearTerm> v2 = recursiveConstructLinearExpression(v_[1],
ir, side);
std::set<int> two_;
for (int i = 0; i < v1.size(); i++) {
int j;
for (j = 0; j < v2.size(); j++)
if (compareTerm(v1[i], v2[j])) {
LinearTerm temp;
temp.is_function = v1[i].is_function;
temp.coefficient = v1[i].coefficient + v2[j].coefficient;
temp.term = v1[i].term;
temp.args = v1[i].args;
temp.is_function = v1[i].is_function;
v.push_back(temp);
two_.insert(j);
break;
}
if (j == v2.size())
v.push_back(v1[i]);
}
for (int i = 0; i < v2.size(); i++)
if (two_.find(i) == two_.end())
v.push_back(v2[i]);
break;
}
case IR_OP_MINUS: {
std::vector<CG_outputRepr *> v_ = ir->QueryExpOperand(repr_src);
std::vector<LinearTerm> v1 = recursiveConstructLinearExpression(v_[0],
ir, side);
std::vector<LinearTerm> v2 = recursiveConstructLinearExpression(v_[1],
ir, side);
std::set<int> two_;
for (int i = 0; i < v1.size(); i++) {
int j;
for (j = 0; j < v2.size(); j++)
if (compareTerm(v1[i], v2[j])) {
LinearTerm temp;
temp.is_function = v1[i].is_function;
temp.coefficient = v1[i].coefficient - v2[j].coefficient;
temp.term = v1[i].term;
temp.args = v1[i].args;
temp.is_function = v1[i].is_function;
v.push_back(temp);
two_.insert(j);
break;
}
if (j == v2.size())
v.push_back(v1[i]);
}
for (int i = 0; i < v2.size(); i++)
if (two_.find(i) == two_.end())
v.push_back(v2[i]);
break;
}
case IR_OP_MULTIPLY: {
std::vector<CG_outputRepr *> v_ = ir->QueryExpOperand(repr_src);
std::vector<LinearTerm> v1 = recursiveConstructLinearExpression(v_[0],
ir, side);
std::vector<LinearTerm> v2 = recursiveConstructLinearExpression(v_[1],
ir, side);
std::set<int> two_;
for (int i = 0; i < v1.size(); i++) {
for (int j = 0; j < v2.size(); j++) {
LinearTerm temp;
temp.coefficient = v1[i].coefficient * v2[j].coefficient;
if (v1[i].term.size() > 0 && v2[j].term.size() > 0) {
temp.term = v1[i].term;
if (v1[i].is_function)
temp.args = v1[i].args;
temp.is_function = v1[i].is_function | v2[j].is_function;
temp.term.insert(temp.term.end(), v2[j].term.begin(),
v2[j].term.end());
if (v2[j].is_function)
temp.args.insert(temp.args.end(), v2[j].args.begin(),
v2[j].args.end());
} else if (v1[i].term.size() == 0 && v2[j].term.size() > 0) {
temp.term = v2[j].term;
temp.is_function = v2[j].is_function;
if (v2[j].is_function)
temp.args = v2[j].args;
} else if (v1[i].term.size() > 0 && v2[j].term.size() == 0) {
temp.term = v1[i].term;
temp.is_function = v1[i].is_function;
if (v1[i].is_function)
temp.args = v1[i].args;
} else {
temp.term = std::vector<std::string>();
temp.is_function = false;
}
int k;
for (k = 0; k < v.size(); k++) {
if (v[k] == temp) {
v[k].coefficient = v[k].coefficient + temp.coefficient;
break;
}
}
if (k == v.size())
v.push_back(temp);
}
}
break;
}
default: {
throw loop_error("unsupported operation type in array subscript");
break;
}
}
return v;
}
}
std::string tmp_e() {
static int counter = 1;
return std::string("e")+to_string(counter++);
}
// Mahdi: This is needed for a change in exp2formula's concept that itself was related to
// how iteration space is cacluated for imperfect loops.
//* When faced with nested function calls while calculated experssion for a dependence
// exp2formual, now, passes output tuple of relation (r relation in the input list) to recursive
// call to exp2formual, instead of passing input tuple of relation that previously was getting passed.
std::string rmvExtraPrime(std::string str){
if(str[(str.size()-1)] == 'p' || str[(str.size()-1)] == '\'')
str.erase(str.end()-1, str.end()); // removing extra "p" or "'"
}
// Mahdi: A helper function to correct embedded iteration space: from Tuowen's topdown branch
// buildIS is basically suppose to replace init_loop in Tuowens branch, and init_loop commented out
// however since Tuowen may want to keep somethings from init_loop I am leaving it there for now
extern std::string index_name(int level);
//-----------------------------------------------------------------------------
// Convert expression tree to omega relation. "destroy" means shallow
// deallocation of "repr", not freeing the actual code inside.
// -----------------------------------------------------------------------------
void exp2formula(Loop *loop, IR_Code *ir, Relation &r, F_And *f_root,
std::vector<Free_Var_Decl*> &freevars, CG_outputRepr *repr,
Variable_ID lhs, char side, IR_CONDITION_TYPE rel, bool destroy,
std::map<std::string, std::vector<omega::CG_outputRepr *> > &uninterpreted_symbols,
std::map<std::string, std::vector<omega::CG_outputRepr *> > &uninterpreted_symbols_stringrepr,
std::map<std::string, std::vector<omega::Relation> > &index_variables, bool extractingDepRel) {
switch (ir->QueryExpOperation(repr)) {
case IR_OP_MACRO: {
std::vector<CG_outputRepr *> v = ir->QueryExpOperand(repr);
IR_FunctionRef *ref = static_cast<IR_FunctionRef *>(ir->Repr2Ref(v[0]));
std::string s = ref->name();
Variable_ID e;
bool exists = false;
for (int i = 0; i < freevars.size(); i++)
if (freevars[i]->base_name() == s) {
e = r.get_local(freevars[i], Input_Tuple);
exists = true;
}
if (!exists) {
Free_Var_Decl *t = new Free_Var_Decl(s, 1);
e = r.get_local(t, Input_Tuple);
}
EQ_Handle h = f_root->add_EQ();
h.update_coef(lhs, 1);
h.update_coef(e, -1);
break;
}
case IR_OP_CONSTANT: {
std::vector<CG_outputRepr *> v = ir->QueryExpOperand(repr);
IR_ConstantRef *ref = static_cast<IR_ConstantRef *>(ir->Repr2Ref(v[0]));
if (!ref->is_integer())
throw ir_exp_error("non-integer constant coefficient");
coef_t c = ref->integer();
if (rel == IR_COND_GE || rel == IR_COND_GT) {
GEQ_Handle h = f_root->add_GEQ();
h.update_coef(lhs, 1);
if (rel == IR_COND_GE)
h.update_const(-c);
else
h.update_const(-c - 1);
} else if (rel == IR_COND_LE || rel == IR_COND_LT) {
GEQ_Handle h = f_root->add_GEQ();
h.update_coef(lhs, -1);
if (rel == IR_COND_LE)
h.update_const(c);
else
h.update_const(c - 1);
} else if (rel == IR_COND_EQ) {
EQ_Handle h = f_root->add_EQ();
h.update_coef(lhs, 1);
h.update_const(-c);
} else
throw std::invalid_argument("unsupported condition type");
delete v[0];
delete ref;
if (destroy)
delete repr;
break;
}
case IR_OP_ARRAY_VARIABLE: {
// Mahdi: comment: Whenever you see something like if(side == 'r'); += "p"; += "\'";
// these rae related to building data access equality for dependence relations.
// if(side == 'r') is checking to see if we have gotten an expression related to read access.
// Note, iterators for read access, at the end, are going to have
// an extra "p" in built for IEGenLib relations and an extra "\'" in CHILL specific relations.
// So exp2formula needs to know that input relation for building
// a dependence would be something like: Relation &r = {[chillidx_1]->[chillidx1p] :}
// But, both chillidx_1 and chillidx1p are (probably) representing the same iterator in the code (IR_Code *ir).
std::vector<CG_outputRepr *> v = ir->QueryExpOperand(repr);
IR_Ref *ref = ir->Repr2Ref(v[0]);
std::string s = ref->name();
int max_dim = 0;
bool need_new_fsymbol = false;
// Mahdi: comment: vars stores the prefix tuple variable up to (and including)
// the input iterator parameter to an index array.
std::set<std::string> vars;
std::vector<Relation> reprs3;
std::vector<EQ_Handle> reprs_3;
// Mahdi: comment: These are the names that are going to built for an uninetreted function call (UFC).
// Then if the UFC does not already exists in the maps, its going to be added to them.
// Different names are for differnt maps (code to omega, omega to iegenlib).
std::set<std::string> unin_syms;
CG_outputRepr *curr_repr = NULL;
CG_outputRepr *curr_repr_s = NULL;
CG_outputRepr *curr_repr_s2 = NULL;
CG_outputRepr *curr_repr_no_const = NULL;
CG_outputRepr *curr_repr_s_no_const = NULL;
CG_outputRepr *curr_repr_s2_no_const = NULL;
// Mahdi: comment: This loop traverses different dimentions of a multi dimentional array, e.g A[i1][i2][i3].
for (int i = 0; i < ref->n_dim(); i++) {
/* Mahdi: Here exp2formula recursively traverses nested calls, e.g a[b[c[i]]].
To tell the recursive calls what is the tuple declaration for the relation that
includes the overall term, here a temporary set is created out of tuple declaration of
the input omega relation to first exp2formula call. It used to be the case that
this set was created ONLY with INPUT tuple declaration of the original input relation.
And, it used to work fine, because we call exp2formula in 2 general case:
(1) when chill is building the iteration space of the statements, in which case
the input relation for first call to exp2formual is already just a set, e.g r = {[...] : ...}.
So, when we build temporary set out of set, their tuple declaration is the same.
(2) chill calls exp2formula when building some sort of dependence relation.
Depenedence relations are actually relations, meaning their tuple declaration have
input and output parts, e.g r = {[...]->[...] : ...}. Now, in this case,
the tuple declaration of the temporary set would not be same as input relation to
first call of exp2formula. The old behaivour of using only input part of tuple declaration
used to work since chill had certain form of creating iteration space for
imperfectly nested loops that was skewed toward this behaivour working.
But chill previous way of creating iteration space was not correct for general cases.
Now, that Tuowen has implemented a better way of generating iteration sapce for loop nests,
following code cannot just use the input part of tuple declaration of input relation to
create the temporary set. It needs to create the temporary set with either input or output part of
the input relation depending on whether we are creating constraints related to
write-access (input part), or constraints for read-access (output part).
*/
Relation temp(r.n_inp());
r.setup_names();
if (r.is_set())
for (int j = 1; j <= r.n_set(); j++) {
temp.name_set_var(j, r.set_var(j)->name());
}
else{
for (int j = 1; j <= r.n_inp(); j++) {
if(side == 'w') temp.name_input_var(j, r.input_var(j)->name());
else if(side == 'r') temp.name_input_var(j, r.output_var(j)->name());
}
}
F_And *temp_root = temp.add_and();
CG_outputRepr* repr2 = repr->clone();
IR_Ref *ref_tmp = ref;
if (dynamic_cast<IR_PointerArrayRef *>(ref) != NULL) {
repr2 = dynamic_cast<IR_PointerArrayRef *>(ref)->index(i);
} else if (dynamic_cast<IR_ArrayRef *>(ref) != NULL) {
repr2 = dynamic_cast<IR_ArrayRef *>(ref)->index(i);
}
//std::vector<Free_Var_Decl*> freevars_;
Free_Var_Decl *t = new Free_Var_Decl(s);
Variable_ID e = temp.get_local(t);
freevars.insert(freevars.end(), t);
exp2formula(loop, ir, temp, temp_root, freevars, repr2, e, side,
IR_COND_EQ, false, uninterpreted_symbols,
uninterpreted_symbols_stringrepr, index_variables);
EQ_Handle e1;
// Mahdi: comment: when there are nested index arrays (Val[A[B[i+1]]]).
// I think exp2formula's output is kept inide omega::Relation temp in a way that is not straightforward.
// Since things can get complicated, when we reach inner most parameter for instance for Val[A[B[i+1]] example,
// and say input relation to first call to exp2formula was:
// r = {[i] : }
// the output for inner most call would be something like:
// r = {[i] : B = i+1}
// And, then:
// // r = {[i] : A = B__(i)}
// And finally, "e" (Variable_ID lhs) should return A_(B__(i))) for the very first call to exp2formula.
// Following trying to fish out the output of recursive calls from the output relation
//std::set<std::string> arg_set_vars;
for (DNF_Iterator di(temp.query_DNF()); di; di++) {
for (EQ_Iterator ei = (*di)->EQs(); ei; ei++) {
e1 = *ei;
for (Constr_Vars_Iter cvi(*ei); cvi; cvi++)
if ((*cvi).var->kind() == Input_Var) {
// Mahdi: change to consider changes of iteration space
std::string tv_name = (*cvi).var->name();
if(tv_name[(tv_name.size()-1)] == 'p' || tv_name[(tv_name.size()-1)] == '\'')
tv_name.erase(tv_name.end()-1, tv_name.end()); // removing extra "p" or "'"
if ((*cvi).var->get_position() > max_dim)
max_dim = (*cvi).var->get_position();
std::string name = tv_name;
if (side == 'r')
name += "p";
curr_repr = ir->builder()->CreatePlus(curr_repr,
ir->builder()->CreateTimes(
ir->builder()->CreateInt(
-(*cvi).coef),
ir->builder()->CreateIdent(name)));
if (-(*cvi).coef != 1) {
curr_repr_s = ir->builder_s().CreatePlus(
curr_repr_s,
ir->builder_s().CreateTimes(
ir->builder_s().CreateInt(
-(*cvi).coef),
ir->builder_s().CreateIdent(
name)));
if (side == 'r') {
std::string name = tv_name;
curr_repr_s2 = ir->builder_s().CreatePlus(
curr_repr_s2,
ir->builder_s().CreateTimes(
ir->builder_s().CreateInt(
-(*cvi).coef),
ir->builder_s().CreateIdent(
name)));
} else {
name += "p";
curr_repr_s2 = ir->builder_s().CreatePlus(
curr_repr_s2,
ir->builder_s().CreateTimes(
ir->builder_s().CreateInt(
-(*cvi).coef),
ir->builder_s().CreateIdent(
name)));
}
} else {
curr_repr_s = ir->builder_s().CreatePlus(
curr_repr_s,
ir->builder_s().CreateIdent(name));
if (side == 'r') {
std::string name = tv_name;
curr_repr_s2 = ir->builder_s().CreatePlus(
curr_repr_s2,
ir->builder_s().CreateIdent(name));
} else {
name += "p";
curr_repr_s2 = ir->builder_s().CreatePlus(
curr_repr_s2,
ir->builder_s().CreateIdent(name));
}
}
vars.insert(
r.input_var((*cvi).var->get_position())->name());
int j;
for (j = 0; j < reprs_3.size(); j++)
if (reprs_3[j] == e1)
break;
if (j == reprs_3.size()) {
Relation c(temp.n_set());
c.copy_names(temp);
c.setup_names();
c.add_and();
reprs_3.push_back(e1);
c.and_with_EQ(e1);
}
} else if ((*cvi).var->kind() == Global_Var
&& (*cvi).var->get_global_var()->base_name()
!= s) {
Global_Var_ID g = (*cvi).var->get_global_var();
if (g->arity() > 0) {
std::vector<CG_outputRepr *> args;
std::vector<CG_outputRepr *> args2;
//ir->RetrieveMacro(g->base_name());
std::string arg_string = "(";
std::string arg_string2 = "(";
for (int l = 1; l <= g->arity(); l++) {
std::string tv_name = temp.set_var(l)->name();
if(tv_name[(tv_name.size()-1)] == 'p' || tv_name[(tv_name.size()-1)] == '\'')
tv_name.erase(tv_name.end()-1, tv_name.end()); // removing extra "p" or "'"
if (l > 1) {
arg_string += ",";
arg_string2 += ",";
}
args.push_back(
ir->builder()->CreateIdent(
tv_name));
args2.push_back(
ir->builder_s().CreateIdent(
tv_name));
arg_string += tv_name;
arg_string2 += tv_name;
if (side == 'r')
arg_string += "p";
else
arg_string2 += "p";
}
arg_string += ")";
arg_string2 += ")";
//std::string base_name = g->base_name();
std::string s = g->base_name();
arg_string = s + arg_string;
arg_string2 = s + arg_string2;
//base_name = base_name.substr(0, base_name.find_first_of("_") );
std::map<std::string, std::string>::iterator lookup2 =
loop->unin_symbol_for_iegen.find(
arg_string);
std::map<std::string, std::string>::iterator lookup3 =
loop->unin_symbol_for_iegen.find(
arg_string2);
curr_repr = ir->builder()->CreatePlus(curr_repr,
ir->builder()->CreateTimes(
ir->builder()->CreateInt(
-(*cvi).coef),
ir->builder()->CreateInvoke(
g->base_name(), args)));
if (-(*cvi).coef != 1) {
if (lookup2
!= loop->unin_symbol_for_iegen.end()){
curr_repr_s =
ir->builder_s().CreatePlus(
curr_repr_s,
ir->builder_s().CreateTimes(
ir->builder_s().CreateInt(
-(*cvi).coef),
new CG_stringRepr(
lookup2->second)));}
if (lookup3
!= loop->unin_symbol_for_iegen.end())
curr_repr_s2 =
ir->builder_s().CreatePlus(
curr_repr_s2,
ir->builder_s().CreateTimes(
ir->builder_s().CreateInt(
-(*cvi).coef),
new CG_stringRepr(
lookup3->second)));
} else {
if (lookup2
!= loop->unin_symbol_for_iegen.end())
curr_repr_s =
ir->builder_s().CreatePlus(
curr_repr_s,
new CG_stringRepr(
lookup2->second));
if (lookup3
!= loop->unin_symbol_for_iegen.end())
curr_repr_s2 =
ir->builder_s().CreatePlus(
curr_repr_s2,
new CG_stringRepr(
lookup3->second));
}
unin_syms.insert(g->base_name());
std::map<std::string, std::set<std::string> >::iterator lookup =
loop->unin_symbol_args.find(
g->base_name());
for (std::set<std::string>::iterator i =
lookup->second.begin();
i != lookup->second.end(); i++) {
for (int j = 1; j <= temp.n_inp(); j++)
if (temp.input_var(j)->name() == *i)
if (j > max_dim)
max_dim = j;
vars.insert(*i);
}
} else {
curr_repr = ir->builder()->CreateTimes(
ir->builder()->CreateInt(-(*cvi).coef),
ir->builder()->CreateIdent(
g->base_name()));
curr_repr_s = ir->builder_s().CreatePlus(
curr_repr_s,
ir->builder_s().CreateTimes(
ir->builder_s().CreateInt(
-(*cvi).coef),
ir->builder_s().CreateIdent(
g->base_name())));
vars.insert(g->base_name());
}
} // Mahdi: end of: for (Constr_Vars_Iter cvi(*ei); cvi; cvi++)
if ((*ei).get_const() != 0) {
curr_repr_no_const = curr_repr->clone();
curr_repr_s_no_const = curr_repr_s->clone();
curr_repr_s2_no_const = curr_repr_s2->clone();
curr_repr = ir->builder()->CreatePlus(curr_repr,
ir->builder()->CreateInt(-(*ei).get_const()));
curr_repr_s = ir->builder_s().CreatePlus(curr_repr_s,
ir->builder_s().CreateInt(-(*ei).get_const()));
curr_repr_s2 = ir->builder_s().CreatePlus(curr_repr_s2,
ir->builder_s().CreateInt(-(*ei).get_const()));
//need_new_fsymbol = true;
//true;
//s += "_";
} else {
curr_repr_no_const = ir->builder()->CreatePlus(
curr_repr->clone(),
ir->builder()->CreateInt(1));
curr_repr_s_no_const = ir->builder_s().CreatePlus(
curr_repr_s->clone(),
ir->builder_s().CreateInt(1));
curr_repr_s2_no_const = ir->builder_s().CreatePlus(
curr_repr_s2->clone(),
ir->builder_s().CreateInt(1));
}
}
}
//if (max_diref->n_dim())
// need_new_fsymbol = true;
}
//if(side == 'r')
// s+="'";
// Mahdi: comment: The s+= "_" are related to building a UFC like A__(i) out of A[i+1]
for (std::set<std::string>::iterator i = unin_syms.begin();
i != unin_syms.end(); i++)
s += "_" + *i;
//if(need_new_fsymbol)
// s+="_";
need_new_fsymbol = true;
Variable_ID e = find_index(r, s, side);
if (e == NULL) { // must be free variable
Free_Var_Decl *t = NULL;
Free_Var_Decl *ta = NULL;
bool changed = true;
do {
changed = false;
s += "_";
t = NULL;
for (unsigned i = 0; i < freevars.size(); i++) {
std::string ss = freevars[i]->base_name();
t = freevars[i];
if (s == ss) {
std::vector<CG_outputRepr *> curr;
curr.push_back(curr_repr->clone());
std::vector<LinearTerm> a =
recursiveConstructLinearExpression(curr_repr,
ir, side);
CG_outputRepr *args = ir->RetrieveMacro(s);
CG_outputRepr * inner = NULL;
if (ir->QueryExpOperation(args)
== IR_OP_ARRAY_VARIABLE) {
std::vector<CG_outputRepr *> v =
ir->QueryExpOperand(args);
IR_Ref * ref_ = ir->Repr2Ref(v[0]);
if (ref_->n_dim() > 1)
throw ir_error(
"Multi dimensional array in loop bounds: not supported currently!\n");
if (dynamic_cast<IR_PointerArrayRef *>(ref_) != NULL) {
inner =
dynamic_cast<IR_PointerArrayRef *>(ref_)->index(
0);
} else if (dynamic_cast<IR_ArrayRef *>(ref_) != NULL) {
inner =
dynamic_cast<IR_ArrayRef *>(ref_)->index(
0);
}
}
if (inner == NULL)
throw ir_error(
"Unrecognized exppression in exp2formula!\n");
std::vector<LinearTerm> b =
recursiveConstructLinearExpression(
inner->clone(), ir, side);
if (!checkEquivalence(a, b)) {
need_new_fsymbol = true;
changed = true;
break;
} else {
need_new_fsymbol = false;
//changed = true;
break;
}
}
}
} while (changed);
if (need_new_fsymbol) {
t = new Free_Var_Decl(s, max_dim);
//else
// t = new Free_Var_Decl(s, max_dim);
freevars.insert(freevars.end(), t);
}
Free_Var_Decl *t1 = NULL;
std::string s1 = s + "_";
bool need_new_fsymbol2 = true;
if (curr_repr_no_const != NULL) {
bool changed = true;
do {
changed = false;
s1 += "_";
t1 = NULL;
for (unsigned i = 0; i < freevars.size(); i++) {
std::string ss = freevars[i]->base_name();
t1 = freevars[i];
if (s1 == ss) {
std::vector<CG_outputRepr *> curr;
curr.push_back(curr_repr->clone());
std::vector<LinearTerm> a =
recursiveConstructLinearExpression(
curr_repr, ir, side);
CG_outputRepr *args = ir->RetrieveMacro(s);