-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathprecedences.cpp
More file actions
1212 lines (1148 loc) · 38.9 KB
/
Copy pathprecedences.cpp
File metadata and controls
1212 lines (1148 loc) · 38.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
/*
* Main authors:
* Mats Carlsson <mats.carlsson@ri.se>
*
* This file is part of Unison, see http://unison-code.github.io
*
* Copyright (c) 2016, RISE SICS AB
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "precedences.hpp"
void gen_fixed_precedences(const Parameters& input, precedence_set& PI) {
// For all Dep in JSON.dep, Dist in JSON.dist, in parallel
assert(input.dep.size() == input.dist.size());
for(unsigned int i = 0; i < input.dep.size(); ++i) {
const vector<vector<int>> Dep = input.dep[i];
const vector<vector<int>> Dist = input.dist[i];
assert(Dep.size() == Dist.size());
// For all <o,o'> in Dep, dij in Dist, in parallel
for (unsigned int j = 0; j < Dep.size(); j++) {
const int o = Dep[j][0];
const int o1 = Dep[j][1];
vector<int> dij = Dist[j];
// Io <- OperInsns(o)
vector<instruction> Io = oper_insns(input, o);
// Conj <- empty
presolver_conj Conj;
// If o is optional
if(!is_mandatory(input, o)) {
// Delete first element from Io and dij
Io.erase(Io.begin());
dij.erase(dij.begin());
// Conj <- Conj U {a(o)}
Conj.push_back(UnisonConstraintExpr(ACTIVE_EXPR, {o}, {}));
}
// If o' is optional
if(!is_mandatory(input, o1)) {
// Conj <- Conj U {a(o)}
Conj.push_back(UnisonConstraintExpr(ACTIVE_EXPR, {o1}, {}));
}
// (First, I compute d1 and the condition)
int d1 = dij.front();
bool same_all_dij = all_of(dij.begin(), dij.end(),
[d1](int v){ return v == d1;});
// if o is a mandatory branch and all values of dij are the same d1
if(is_mandatory(input, o) && oper_type(input, o) == BRANCH &&
same_all_dij) {
// PI <- PI U {<o, o', d', {}>,<o, o',-d', {}}
PresolverPrecedence pred(o, o1, d1, {{}});
PresolverPrecedence pred1(o1, o, -d1, {{}});
PI.push_back(pred);
PI.push_back(pred1);
} else if (same_all_dij) {
// else if all values of dij are the same d'
PresolverPrecedence pred(o, o1, d1, presolver_disj({Conj}));
// PI <- PI U {<o, o', d', {Conj}}
PI.push_back(pred);
} else {
// For all i' in Io, d' in dij, in parallel
assert(Io.size() == dij.size());
for (unsigned int i = 0; i < Io.size(); i++) {
instruction i1 = Io[i];
int d1 = dij[i];
// {Conj U {o(o, i')}}
presolver_conj c = Conj;
UnisonConstraintExpr e(IMPLEMENTS_EXPR, {o,i1}, {});
vector_insert(c, e);
PresolverPrecedence pred(o, o1, d1, presolver_disj({c}));
PI.push_back(pred);
}
}
}
}
}
static bool distinct_cycle(const Parameters& input,
operation i,
operation j,
const vector<vector<vector<int>>>& min_con_erg) {
for (resource r : input.R)
if (min_con_erg[i][r][0] + min_con_erg[j][r][0] > input.cap[r])
return true;
return false;
}
void gen_min_con_erg(const Parameters& input,
vector<vector<vector<int>>>& min_con_erg) {
for(operation o : input.O) {
min_con_erg[o] = vector<vector<int>>(input.R.size());
for(resource r : input.R) {
int minc = 9999;
int mine = 9999;
for(instruction i : input.instructions[o]) {
if(i != NULL_INSTRUCTION && minc>input.con[i][r]) {
minc = input.con[i][r];
mine = minc * input.dur[i][r];
if(minc==0)
break;
}
}
min_con_erg[o][r] = vector<int>(2);
min_con_erg[o][r][0] = minc;
min_con_erg[o][r][1] = mine;
}
}
}
void gen_precs_precedences(const Parameters& input,
const vector<vector<vector<int>>>& min_con_erg,
precedence_set& PI) {
presolver_conj ConjTrue;
presolver_disj DisjTrue({ConjTrue});
// FUN-FUN ---> no-op
// X-FUN ---> X-[TAIL]CALL if X is not [TAIL]CALL and can't be issued with [TAIL]CALL
// [TAIL]CALL-X ---> FUN-X if X is not FUN
for(const vector<operation>& edge : input.precs) {
operation i = edge[0];
operation j = edge[1];
operation isucc = i;
operation jpred = j;
if (input.type[i] == CALL || input.type[i] == TAILCALL)
while (input.type[isucc] != FUN)
isucc++;
if (input.type[j] == FUN)
while (input.type[jpred] != CALL && input.type[jpred] != TAILCALL)
jpred--;
if (input.type[i] == FUN && input.type[j] == FUN) {
} else if (input.type[i] != CALL && input.type[i] != TAILCALL && input.type[j] == FUN &&
distinct_cycle(input, i, jpred, min_con_erg)) {
PI.push_back(PresolverPrecedence(i, jpred, 1, DisjTrue));
} else if ((input.type[i] == CALL || input.type[i] == TAILCALL) && input.type[j] != FUN) {
PI.push_back(PresolverPrecedence(isucc, j, 1, DisjTrue));
} else {
PI.push_back(PresolverPrecedence(i, j, 1, DisjTrue));
}
}
}
multimap<operation, instruction> build_oI (const presolver_disj& Y) {
multimap<operation, instruction> oI;
for(const presolver_conj& c : Y) {
for(const UnisonConstraintExpr& l : c) {
// If I am not processing the right operation, continue
if(l.id != IMPLEMENTS_EXPR)
continue;
// Insert o, i
oI.insert(make_pair(l.data[0], l.data[1]));
}
}
return oI;
}
/**************** region precedences ******************/
#define FastPair(o1,o2) (((unsigned long)(o1)<<32) + (unsigned long)(o2))
#define FastPairSource(P) ((int)((P) >> 32))
#define FastPairTarget(P) ((int)((P) & (unsigned long)(0xFFFFFFFF)))
static int min_latency(const Parameters& input,
operation o1,
operation o2,
operand d,
operand u) {
int ld = 1000000000;
int lu = 1000000000;
for(unsigned int ii = 0; ii < input.instructions[o1].size(); ++ii) {
if(input.instructions[o1][ii] != NULL_INSTRUCTION) {
unsigned int di = index_of(input.operands[o1], d);
int lat = input.lat[o1][ii][di];
ld = ld < lat ? ld : lat;
}
}
for(unsigned int ii = 0; ii < input.instructions[o2].size(); ++ii) {
if(input.instructions[o2][ii] != NULL_INSTRUCTION) {
unsigned int ui = index_of(input.operands[o2], u);
int lat = input.lat[o2][ii][ui];
lu = lu < lat ? lu : lat;
}
}
return max(ld+lu,0);
};
void gen_region_precedences(const Parameters& input,
const vector<vector<vector<int>>>& min_con_erg,
const precedence_set& PI,
precedence_set& PO) {
set<UnisonConstraintExpr> entailed;
map<unsigned long,int> pweight;
map<unsigned long,int> pweight_c;
presolver_conj ConjTrue;
presolver_disj DisjTrue({ConjTrue});
map<UnisonConstraintExpr,vector<PresolverPrecedence>> prec_index;
gen_region_init(input, entailed, pweight, prec_index, PI);
for(block b : input.B)
gen_region_precedences_block(input, b, min_con_erg, pweight, pweight_c, DisjTrue, PO);
for(operand p : input.P) {
temporary t0 = input.temps[p][0];
if(input.use[p] && t0 != NULL_TEMPORARY) {
for (const auto e : entailed)
if (e.id == CONNECTS_EXPR && e.data[0] == p)
goto nextp;
for(temporary t : input.temps[p]) {
if (t != t0)
gen_region_precedences_cond(input, p, t, min_con_erg, pweight, prec_index, PO);
}
}
nextp: ;
}
}
void gen_region_init(const Parameters& input,
set<UnisonConstraintExpr>& entailed,
map<unsigned long,int>& pweight,
map<UnisonConstraintExpr,vector<PresolverPrecedence>>& prec_index,
const precedence_set& PI) {
for (const PresolverActiveTable& pa : input.active_tables) {
if (pa.tuples.size() == 1) {
for (unsigned int i=0; i<pa.os.size(); i++)
if (pa.tuples[0][i]) {
operation o = pa.os[i];
entailed.insert(UnisonConstraintExpr(ACTIVE_EXPR, {o}, {}));
for (operand p : input.operands[o])
if (input.use[p] && input.temps[p].size() == 2 && input.temps[p][0] == NULL_TEMPORARY)
entailed.insert(UnisonConstraintExpr(CONNECTS_EXPR, {p,input.temps[p][1]}, {}));
}
}
}
for (const PresolverCopyTmpTable& pa : input.tmp_tables) {
if (pa.tuples.size() == 1) {
int no = pa.os.size();
int np = pa.ps.size();
for (int i=no; i<no+np; i++) {
int ti = pa.tuples[0][i];
entailed.insert(UnisonConstraintExpr(CONNECTS_EXPR, {pa.ps[i-no],ti}, {}));
}
}
}
for (const PresolverPrecedence& pre : PI) {
operation src = pre.i;
operation dest = pre.j;
if (src < dest && pre.n >= 0) {
if (disj_is_true(pre.d)) {
unsigned long key = FastPair(src,dest);
if (pweight.find(key) == pweight.end() || pweight[key] < pre.n)
pweight[key] = pre.n;
} else if (pre.d.size()==1 && pre.d[0].size()==1) {
prec_index[pre.d[0][0]].push_back(pre);
}
}
}
for (const auto e : entailed)
if (prec_index.find(e) != prec_index.end())
for (const PresolverPrecedence& pre : prec_index[e]) {
operation src = pre.i;
operation dest = pre.j;
unsigned long key = FastPair(src,dest);
if (pweight.find(key) == pweight.end() || pweight[key] < pre.n)
pweight[key] = pre.n;
}
// simulate data precedences: KILL precedences are redundant
for (operand p : input.P) {
if (input.use[p]) {
operation o2 = input.oper[p];
temporary t = -1;
for (const auto e : entailed) {
if (e.id == CONNECTS_EXPR && e.data[0] == p) {
t = e.data[1]; break;
}
}
if (t < 0 && (is_mandatory(input, o2) || entailed.count(UnisonConstraintExpr(ACTIVE_EXPR, {o2}, {})))) {
t = first_temp_but_null(input, p);
}
if (t >= 0) {
operation o1 = input.def_opr[t];
operand d = input.definer[t];
if (o1 < o2 && !input.global_operand[d] && !input.global_operand[p] && input.type[o2] != KILL) {
unsigned long key = FastPair(o1,o2);
int distance = min_latency(input, o1, o2, d, p);
if (pweight.find(key) == pweight.end() || pweight[key] < distance)
pweight[key] = distance;
}
}
}
}
}
void gen_region_precedences_cond(const Parameters& input,
const operand p,
const temporary t,
const vector<vector<vector<int>>>& min_con_erg,
map<unsigned long,int>& pweight,
map<UnisonConstraintExpr,vector<PresolverPrecedence>>& prec_index,
precedence_set& PO) {
block b = input.pb[p];
set<UnisonConstraintExpr> entailed_c;
map<unsigned long,int> pweight_c;
vector<int> def_use_c;
UnisonConstraintExpr lit = UnisonConstraintExpr(CONNECTS_EXPR, {p,t}, {});
presolver_disj cond({{lit}});
// cerr << "% region precedences assuming " << show(lit) << endl;
entailed_c.insert(lit);
entailed_c.insert(UnisonConstraintExpr(ACTIVE_EXPR, {input.def_opr[t]}, {}));
for (const PresolverCopyTmpTable& pa : input.tmp_tables) {
int no = pa.os.size();
int np = pa.ps.size();
int nt = pa.tuples.size();
int pix = -1;
for (int ii=0; ii<np && pix<0; ii++)
if (pa.ps[ii] == p)
pix = ii;
if (pix >= 0) {
int tuno = -1;
for (int jj=0; jj<nt && tuno<nt; jj++)
if (pa.tuples[jj][no+pix] == t)
tuno = (tuno == -1 ? jj : nt);
if (tuno < 0) {
// cerr << "% no support" << endl;
return;
} else if (tuno >= 0 && tuno < nt) {
// cerr << "% single support" << endl;
for (int jj=0; jj<no; jj++)
if (pa.tuples[tuno][jj]) {
operation o1 = pa.os[jj];
entailed_c.insert(UnisonConstraintExpr(ACTIVE_EXPR, {o1}, {}));
}
for (int jj=0; jj<np; jj++) {
operand p1 = pa.ps[jj];
temporary t1 = pa.tuples[tuno][no+jj];
entailed_c.insert(UnisonConstraintExpr(CONNECTS_EXPR, {p1,t1}, {}));
}
// } else {
// cerr << "% multiple support" << endl;
}
}
}
for (const auto e : entailed_c)
if (prec_index.find(e) != prec_index.end())
for (const PresolverPrecedence& pre : prec_index[e]) {
operation src = pre.i;
operation dest = pre.j;
unsigned long key = FastPair(src,dest);
if (pweight_c.find(key) == pweight_c.end() || pweight_c[key] < pre.n)
pweight_c[key] = pre.n;
}
// simulate data precedences: KILL precedences are redundant
for (const auto e : entailed_c) {
operand u, d;
if (e.id == CONNECTS_EXPR) {
u = e.data[0];
d = input.definer[e.data[1]];
} else if (e.id == ACTIVE_EXPR) {
u = first_use(input, e.data[0]);
d = input.definer[first_temp_but_null(input, u)];
} else {
continue;
}
operation o1 = input.oper[d];
operation o2 = input.oper[u];
if(o1 < o2 && !input.global_operand[d] && !input.global_operand[u] && input.type[o2] != KILL) {
unsigned long key = FastPair(o1,o2);
int distance = min_latency(input, o1, o2, d, u);
if (pweight_c.find(key) == pweight_c.end() || pweight_c[key] < distance)
pweight_c[key] = distance;
}
}
gen_region_precedences_block(input, b, min_con_erg, pweight, pweight_c, cond, PO);
}
void gen_region_precedences_block(const Parameters& input,
const block b,
const vector<vector<vector<int>>>& min_con_erg,
map<unsigned long,int>& pweight,
map<unsigned long,int>& pweight_c,
const presolver_disj& cond,
precedence_set& PO) {
vector<vector<operation>> edges;
for(const auto kv : pweight)
if(input.oblock[FastPairSource(kv.first)] == b)
edges.push_back({FastPairSource(kv.first), FastPairTarget(kv.first)});
for(const auto kv : pweight_c)
edges.push_back({FastPairSource(kv.first), FastPairTarget(kv.first)});
vector<operation> pnodes;
Digraph G = Digraph(edges);
partition_nodes(G, pnodes);
gen_region_per_partition(input, G, pnodes, min_con_erg, pweight, pweight_c, cond, PO);
}
void partition_nodes(Digraph& G,
vector<operation>& pnodes) {
operation l = G.vertices()[0];
for(operation v : G.vertices()) {
if (v==l)
pnodes.push_back(v);
for(operation n : G.neighbors(v))
l = max(n,l);
}
}
static bool has_edge_inside(map<unsigned long,int>& pweight_c,
operation lb,
operation ub) {
for (const auto kv : pweight_c) {
operation src = FastPairSource(kv.first);
operation tgt = FastPairTarget(kv.first);
if (lb <= src && tgt <= ub)
return true;
}
return false;
}
void gen_region_per_partition(const Parameters& input,
Digraph& G, // "precs" edges for 1 block
const vector<operation>& pnodes,
const vector<vector<vector<int>>>& min_con_erg,
map<unsigned long,int>& pweight,
map<unsigned long,int>& pweight_c,
const presolver_disj& cond,
precedence_set& PO) {
map<operation,vector<pair<operation,operation>>> M;
int multiplier = input.O.size();
for(const pair<operation,operation>& edge : G.edges()) {
for(operation b : pnodes) {
if(b >= edge.second) {
M[b].push_back(edge);
break;
}
}
}
for(const pair<operation,vector<pair<operation,operation>>>& b_edges : M) {
Digraph G = Digraph(b_edges.second);
Digraph H = G.transpose();
map<operation,operation> R;
for(operation vi : H.vertices()) {
for(operation vj : H.vertices()) {
if (vj >= vi) break;
operation vji = multiplier*vj+vi;
operation r1 = -1;
operation r2 = -1;
for(operation vk : H.neighbors(vi)) {
if (r2 > -1) break;
operation vjk = multiplier*vj+vk;
if(R.find(vjk) != R.end()) {
operation Rjk = R[vjk];
if (r1 == -1 || r1 == Rjk)
r1 = Rjk;
else
r2 = Rjk;
}
}
if(r1 == -1) {
if(ord_contains(H.neighbors(vi),vj)) {
R[vji] = vi;
}
} else if(r2 == -1) {
R[vji] = r1;
} else {
R[vji] = vi;
if (pweight_c.empty() || has_edge_inside(pweight_c, vj, vi))
gen_region(input, vj, vi, G, H, min_con_erg, pweight, pweight_c, cond, PO);
}
}
}
}
}
/* - use the Van Beek approx: max_r (r1(src,sink,r) + r2(src,sink,r) + r3(src,sink,r) - 1)
*
* - onpath(src,sink,r) = {i | i uses resource r and is in src-sink region}
*
* - r1(src,sink,r) = min{cp(src,k) | k in onpath(src,sink,r)}
*
* - r3(src,sink,r) = min{cp(k,sink) | k in onpath(src,sink,r)}
*
* - r2(src,sink,r) = min #cycles to issue onpath(src,sink,r) =
* G <- onpath(src,sink,r) as a digraph
* F <- finishers({},{},0,G,r)
* return ceiling((sum(k in F)(con_r(k)) + sum(k in V(G)\F)(dur_r(k)*con_r(k))) / cap_r)
*
* - finishers(G,r) =
* FF <- finishers({},{},0,G,r)
* return F in FF with max sum(k in F)(dur_r(k)*con_r(k) - con_r(k))
*
* - finishers(I,O,C,G,r) =
* find k in V(G) | k not_in I /\ k not_in O /\ G.succ(k) subset I /\ C+con_r(k) <= cap_r
* if (no such k)
* return {I}
* else
* return finishers(I+k,O,C+con_r(k),G,r) union finishers(I,O+k,C,G,r)
*
*/
void gen_region(const Parameters& input,
operation src,
operation sink,
Digraph& G, // forward
Digraph& H, // backward
const vector<vector<vector<int>>>& min_con_erg,
map<unsigned long,int>& pweight,
map<unsigned long,int>& pweight_c,
const presolver_disj& cond,
precedence_set& PO) {
int glb = 0;
vector<operation> inside = ord_intersection(G.reachables(src), H.reachables(sink)); // excludes src, sink
vector<operation> region = ord_union(inside, {src,sink});
vector<operation> reverse = vector<operation>(region.size());
unsigned int rs = region.size();
for (unsigned int ii = 0; ii < rs; ii++)
reverse[rs-ii-1] = region[ii];
map<operation,int> src_cps = dag_longest_paths_fwd(region, pweight, pweight_c);
map<operation,int> sink_cps = dag_longest_paths_bwd(reverse, pweight, pweight_c);
vector<resource> subsumed = input.subsumed_resources[input.oblock[src]];
for (resource r : input.R) {
if (!ord_contains(subsumed, r)) {
vector<pair<int,int>> edges;
for (operation o1 : region)
if (min_con_erg[o1][r][1]>0)
for (operation o2 : G.reachables(o1))
if (ord_contains(region,o2) && min_con_erg[o2][r][1]>0)
edges.push_back(make_pair(o1,o2));
Digraph R = Digraph(edges);
vector<operation> RV = R.vertices();
if (RV.size()>0) {
int r1=0, r2=0, r3=0;
vector<operation> F;
if (min_con_erg[src][r][1]==0) {
r1 = 1000000000;
for (operation o : RV) {
int cp = src_cps[o];
r1 = cp < r1 ? cp : r1;
}
}
if (min_con_erg[sink][r][1]==0) {
r3 = 1000000000;
for (operation o : RV) {
int cp = sink_cps[o];
r3 = cp < r3 ? cp : r3;
}
}
for (operation o : RV)
if (min_con_erg[o][r][0] < min_con_erg[o][r][1]) {
F = region_finishers(R, r, input.cap[r], min_con_erg);
break;
}
for (operation o : RV)
if (ord_contains(F,o))
r2 = r2 + min_con_erg[o][r][0];
else
r2 = r2 + min_con_erg[o][r][1];
r2 = (r2-1)/input.cap[r]+1;
r2 = r1+r2+r3-1;
glb = r2 > glb ? r2 : glb;
}
}
}
if (glb <= src_cps[sink]) {
} else if (disj_is_true(cond)) {
PresolverPrecedence pred(src, sink, glb, cond);
PO.push_back(pred);
pweight[FastPair(src,sink)] = glb;
// cerr << "% NEW c(" << src << ") + " << glb << " <= c(" << sink << ")" << endl;
} else {
PresolverPrecedence pred(src, sink, glb, cond);
PO.push_back(pred);
pweight_c[FastPair(src,sink)] = glb;
// cerr << "% NEW c(" << src << ") + " << glb << " <= c(" << sink << ")" << endl;
}
}
// Find all longest paths from implicit src, assuming we have a DAG, assuming ascending vertices by top sort
map<operation,int> dag_longest_paths_fwd(vector<operation>& region,
map<unsigned long,int>& pweight,
map<unsigned long,int>& pweight_c) {
map<operation,int> L;
for(operation v : region)
L[v] = 0;
for(operation b : region)
for(operation v : region)
if(b<v) {
unsigned long key = FastPair(b,v);
int w = -1;
int w_c = -1;
if(pweight.find(key) != pweight.end())
w = pweight[key];
if(pweight_c.find(key) != pweight_c.end())
w_c = pweight_c[key];
if(max(w,w_c) >= 0)
L[v] = max(L[v],L[b]+max(w,w_c));
}
return L;
}
// Find all longest paths from implicit src, assuming we have a DAG, assuming ascending vertices by top sort
map<operation,int> dag_longest_paths_bwd(vector<operation>& region,
map<unsigned long,int>& pweight,
map<unsigned long,int>& pweight_c) {
map<operation,int> L;
for(operation v : region)
L[v] = 0;
for(operation b : region)
for(operation v : region)
if(b>v) {
unsigned long key = FastPair(v,b);
int w = -1;
int w_c = -1;
if(pweight.find(key) != pweight.end())
w = pweight[key];
if(pweight_c.find(key) != pweight_c.end())
w_c = pweight_c[key];
if(max(w,w_c) >= 0)
L[v] = max(L[v],L[b]+max(w,w_c));
}
return L;
}
vector<operation> region_finishers(Digraph& R,
resource r, int cap,
const vector<vector<vector<int>>>& min_con_erg) {
vector<operation> In;
vector<operation> Out;
vector<operation> Empty;
pair<int,vector<operation>> incumbent;
incumbent = make_pair(-1,Empty);
region_finishers_rec(In, Out, 0, 0, incumbent, R, r, cap, min_con_erg);
return incumbent.second;
}
void region_finishers_rec(vector<operation>& In,
vector<operation>& Out,
int load,
int decr,
pair<int,vector<operation>>& incumbent,
Digraph& R,
resource r, int cap,
const vector<vector<vector<int>>>& min_con_erg) {
if (incumbent.first<decr) {
incumbent.first = decr;
incumbent.second.clear();
for (operation o : In)
incumbent.second.push_back(o);
}
for (operation o : R.vertices()) {
int inc = min_con_erg[o][r][0];
if (load+inc <= cap &&
!ord_contains(In,o) && !ord_contains(Out,o) && ord_difference(R.neighbors(o),In).size()==0) {
vector_insert(In,o);
region_finishers_rec(In, Out, load+inc, decr + min_con_erg[o][r][1] - min_con_erg[o][r][0], incumbent, R, r, cap, min_con_erg);
vector_erase(In,o);
vector_insert(Out,o);
region_finishers_rec(In, Out, load, decr, incumbent, R, r, cap, min_con_erg);
vector_erase(Out,o);
return;
}
}
}
void normalize_precedences(const Parameters& input, const precedence_set& P, vector<UnisonConstraintExpr>& P1) {
// M <- P' <- empty
map<PrecedenceEdge, presolver_disj> M;
// For all <src, dest, d, D> in P
for(unsigned int ii=0; ii<P.size(); ii++) {
const PresolverPrecedence& p = P[ii];
operation src = p.i;
operation dest = p.j;
int d = p.n;
presolver_disj D = p.d;
// check first whether we are subsumed by an uncond precedence with greater latency
for(unsigned int jj=ii+1; jj<P.size(); jj++) {
const PresolverPrecedence& p2 = P[jj];
if (p2.i!=src || p2.j!=dest) break;
if (p2.n>=d && disj_is_true(p2.d)) {
goto next;
}
}
// For all C in D
for(const presolver_conj& C : D) {
// C <- { L in C | L != o(o,i) || OperInsns(o) != i}
presolver_conj conj;
std::copy_if(C.begin(), C.end(), std::back_inserter(conj),
[&input](const UnisonConstraintExpr L) {
const int o = L.data[0];
const int i = L.data[1];
return L.id != IMPLEMENTS_EXPR ||
(oper_insns(input,o) != vector<instruction>({i}));
});
PrecedenceEdge e;
e.i = src;
e.j = dest;
e.n = d;
M[e].push_back(conj);
}
next: ;
}
// For all <src, dest, d> -> D in M
for(const pair<PrecedenceEdge, presolver_disj>& ed : M) {
PrecedenceEdge e = ed.first;
presolver_disj D = ed.second;
// if {a(o)} in D
presolver_disj::iterator it = std::find_if(D.begin(), D.end(),
[](presolver_conj c){
return c.size()==1 && c[0].id == ACTIVE_EXPR;
});
if(it != D.end()) {
// D <- {{a(o)}}
D = {*it};
} else {
// D <- KernelSet(D, empty);
D = kernel_set(D, presolver_disj(), -1);
// K <- intersection of all conjunctions in D
// i.e. the set of literals present in every conjunction in D
vector<UnisonConstraintExpr> K = std::accumulate(D.begin(), D.end(), D.front(),
[](vector<UnisonConstraintExpr> acc, const presolver_conj c) {
// Return intersection of accumulator w/ the conjunction
// c : filter acc from elements not in c;
vector<UnisonConstraintExpr> res;
std::copy_if(acc.begin(), acc.end(), std::back_inserter(res),
[&c](UnisonConstraintExpr lit){ return ord_contains(c, lit); });
return res; });
// Y <- { C \ K | C in D }
presolver_disj Y;
// Remove elements existing in K from every element C in D;
std::transform(D.begin(), D.end(), std::back_inserter(Y),
[&K](const presolver_conj C) {
// diff <- C \ K
presolver_conj diff;
std::copy_if(C.begin(), C.end(), std::back_inserter(diff),
[&K](UnisonConstraintExpr lit){ return !ord_contains(K, lit); });
return diff;
});
multimap<operation, instruction> oI = build_oI(Y);
for(operation o : input.O) {
auto range = oI.equal_range(o);
// If there are is a set I for o
if(std::distance(range.first, range.second) > 0) {
// Build it
vector<instruction> I, oper_instr;
std::transform(range.first, range.second,
std::back_inserter(I),
[](pair<operation, instruction> p){ return p.second; });
// OpenInsns(o)
oper_instr = oper_insns(input, o);
// Y == {{o(o,i)} | i in I}
bool Y_matches_I = I.size() == Y.size(); // Because I was build using Y
if(subseteq(I, oper_instr) && Y_matches_I) {
// D <- { K U {!o(o,i) | i in OperInsns(o) \ I in i != (-)}}
//
// Generate the set of OperInsns(o) \ I
// where i != NULL_INSTRUCTION
vector<instruction> S;
std::copy_if(oper_instr.begin(), oper_instr.end(),
std::back_inserter(S),
[&I](instruction i) {
return !ord_contains(I, i) && i != NULL_INSTRUCTION;
});
presolver_conj conj;
// Union with K
for(const UnisonConstraintExpr& k : K) {
conj.push_back(k);
}
// Negate o(o,i) where i in S and make it a conjunction
for(instruction i : S) {
UnisonConstraintExpr nlit(IMPLEMENTS_EXPR, {o,i}, {});
UnisonConstraintExpr lit(NOT_EXPR, {}, {nlit});
conj.push_back(lit);
}
conj = normal_conjunction(input, conj);
D = { conj };
}
}
}
}
// P' <- P' U {<src, dest, d, D>}
UnisonConstraintExpr expr(DISTANCE_EXPR, {e.i,e.j,e.n}, {});
if (!disj_is_true(D)) {
expr = UnisonConstraintExpr(IMPLIES_EXPR, {}, {disj_to_expr(D),expr});
}
P1.push_back(expr);
}
sort(P1.begin(), P1.end()); // canonicalize
}
void gen_before_precedences(const Parameters& input,
PresolverOptions & options,
const vector<PresolverBeforeJSON>& before,
const vector<vector<vector<int>>>& min_con_erg,
precedence_set& PI,
Support::Timer & t) {
// M <- empty
map<PrecedenceEdge, presolver_disj> M;
unsigned int i = 0;
// For all <p,q,Disj> in Before
for(const PresolverBeforeJSON& b : before) {
// M <- M U GenBeforePrecedences1(p,q,Disj)
gen_before_precedences1(input, b.p, b.q, expr_to_disj(b.e), min_con_erg, M);
if ((i % 16 == 0) &&
timeout(t, options, "gen_before_precedences (1st loop)", t, false))
return;
i++;
}
i = 0;
// return {<p,s,n,KernelSet(Disj, empty)> | <p,s,n> -> Disj in M }
for(auto pd : M) {
PrecedenceEdge k = pd.first;
presolver_disj disj = pd.second;
sort(disj.begin(), disj.end());
disj.erase(unique(disj.begin(), disj.end()), disj.end());
PresolverPrecedence e(k.i, k.j, k.n, kernel_set(disj, {}, -1));
PI.push_back(e);
if ((i % 16 == 0) &&
timeout(t, options, "gen_before_precedences (2nd loop)", t, false))
return;
i++;
}
}
static void before_rule(const Parameters& input,
const presolver_conj& conj1,
const operation o,
const operation o1,
const presolver_conj& conj,
const vector<vector<vector<int>>>& min_con_erg,
map<PrecedenceEdge, presolver_disj>& M) {
if(o != o1) {
PrecedenceEdge e;
e.i = o;
e.j = o1;
e.n = distinct_cycle(input, o, o1, min_con_erg) ? 1 : 0;
presolver_conj u;
for(const UnisonConstraintExpr& l : conj1) {
if (l.id != CONNECTS_EXPR || opnd_temps(input, l.data[0]).size() > 1) // not entailed?
vector_insert(u, l);
}
for(const UnisonConstraintExpr& l : conj) {
if (l.id != CONNECTS_EXPR || opnd_temps(input, l.data[0]).size() > 1) // not entailed?
vector_insert(u, l);
}
M[e].push_back(u);
}
}
void gen_before_precedences1(const Parameters& input,
operand p, operand q,
const presolver_disj& disj,
const vector<vector<vector<int>>>& min_con_erg,
map<PrecedenceEdge, presolver_disj>& M) {
operand o_p = opnd_oper(input, p);
operand o_q = opnd_oper(input, q);
for(const presolver_conj& conj : disj) {
before_rule(input, { }, o_p, o_q, conj, min_con_erg, M);
}
if(!input.use[p]) {
temporary t = first_temp_but_null(input, p);
for(operand r : temp_uses(input, t)) {
operation o_r = opnd_oper(input, r);
for(const presolver_conj& conj : disj) {
UnisonConstraintExpr lit1(CONNECTS_EXPR, {r,t}, {});
before_rule(input, { lit1 }, o_r, o_q, conj, min_con_erg, M);
}
}
}
if(input.use[q]) {
for(temporary t : opnd_temps(input, q)) {
if(t!=NULL_TEMPORARY) {
operation o_r = input.def_opr[t];
for(const presolver_conj& conj : disj) {
UnisonConstraintExpr lit1(CONNECTS_EXPR, {q,t}, {});
before_rule(input, { lit1 }, o_p, o_r, conj, min_con_erg, M);
}
}
}
}
}
void gen_long_latency(Parameters& input) {
// Phase 1: collect relevant def-use pairs where at least one operand is global and may require nonzero slack
set<vector<operand>> seen;
vector<operand> queue;
for(operation o : input.O) {
block b = input.oblock[o];
vector<operand> operands = input.operands[o];
vector<instruction> instructions = input.instructions[o];
for(unsigned int i = 0; i < instructions.size(); ++i) {
for(unsigned int pp = 0; pp < operands.size(); ++pp) {
operand p = operands[pp];
if(!input.use[p] && input.lat[o][i][pp] > 1) {
temporary t = input.single_temp[p];
operation o2 = input.out[b];
for(operand q : input.operands[o2]) {
if(ord_contains(input.temps[q],t)) {
vector<operand> pq = {p,q};
if (seen.find(pq) == seen.end()) {
seen.insert(pq);
queue.push_back(q);
input.long_latency_def_use.push_back(pq);
}
}
}
} else if(input.use[p] && input.lat[o][i][pp] > 0) {
temporary t = input.real_temps[p][0];
operation o2 = input.in[b];
for(operand q : input.operands[o2]) {
if(input.single_temp[q]==t) {
vector<operand> pq = {q,p};
if (seen.find(pq) == seen.end()) {
seen.insert(pq);
queue.push_back(q);
input.long_latency_def_use.push_back(pq);
}
}
}
}
}
}
}
while(!queue.empty()) {
operand q = queue.back();
operation o0 = input.oper[q];
queue.pop_back();
for(operand p2 : input.congr[input.operand_congruence[q]]) {
operation o = input.oper[p2];
if(input.type[o0] == OUT && input.type[o] == IN) {
temporary t = input.single_temp[p2];
block b = input.oblock[o];
operation o2 = input.out[b];
for(operand q2 : input.operands[o2]) {
if(ord_contains(input.temps[q2],t)) {
vector<operand> pq2 = {p2,q2};
if (seen.find(pq2) == seen.end()) {
seen.insert(pq2);
queue.push_back(q2);
input.long_latency_def_use.push_back(pq2);
}
}
}
} else if(input.type[o0] == IN && input.type[o] == OUT) {
temporary t = input.real_temps[p2][0];
block b = input.oblock[o];
operation o2 = input.in[b];
for(operand q2 : input.operands[o2]) {
if(input.single_temp[q2]==t) {
vector<operand> pq2 = {q2,p2};
if (seen.find(pq2) == seen.end()) {
seen.insert(pq2);
queue.push_back(q2);
input.long_latency_def_use.push_back(pq2);
}
}
}
}
}
}
// Phase 2: build index