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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
|
/*
* Copyright (c) 2017 Lima Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sub license,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
#include <limits.h>
#include "gpir.h"
/*
* GP scheduling algorithm (by Connor Abbott <cwabbott0@gmail.com>)
*
* The GP pipeline has three main stages:
*
* --------------------------------------------------------
* | |
* | Register/Attr/Temp Fetch |
* | |
* --------------------------------------------------------
* | | | | | | |
* | Mul0 | Mul1 | Add0 | Add1 | Cplx | Pass |
* | | | | | | |
* --------------------------------------------------------
* | | | |
* | Complex1 | Temp/Register/Varying | Pass |
* | Stage 2 | Store | Stage 2 |
* | | | |
* --------------------------------------------------------
*
* Because of this setup, storing a register has a latency of three cycles.
* Also, the register file is organized into 4-component vectors, and the
* load stage can only load two vectors at a time. Aside from these highly
* constrained register load/store units, there is an explicit bypass
* network, where each unit (mul0/mul1/etc.) can access the results of the
* any unit from the previous two cycles directly, except for the complex
* unit whose result can only be accessed for one cycle (since it's expected
* to be used directly by the complex2 instruction in the following cycle).
*
* Because of the very restricted register file, and because only rarely are
* all the units in use at the same time, it can be very beneficial to use
* the unused units to "thread" a value from source to destination by using
* moves in the otherwise-unused units, without involving the register file
* at all. It's very difficult to fully exploit this with a traditional
* scheduler, so we need to do something a little un-traditional. The 512
* instruction limit means that for more complex shaders, we need to do as
* well as possible or else the app won't even work.
*
* The scheduler works by considering the bypass network as a kind of
* register file. It's a quite unusual register file, since registers have to
* be assigned "on the fly" as we schedule operations, but with some care, we
* can use something conceptually similar to a linear-scan allocator to
* successfully schedule nodes to instructions without running into
* conflicts.
*
* Values in the IR are separated into normal values, or "value registers",
* which is what normal nodes like add, mul, etc. produce, and which only
* live inside one basic block, and registers, which can span multiple basic
* blocks but have to be accessed via special load_reg/store_reg nodes. RA
* assigns physical registers to both value registers and normal registers,
* treating load_reg/store_reg as a move instruction, but these are only used
* directly for normal registers -- the physreg assigned to a value register
* is "fake," and is only used inside the scheduler. Before scheduling we
* insert read-after-write dependencies, even for value registers, as if
* we're going to use those, but then we throw them away. For example, if we
* had something like:
*
* (*)r2 = add (*)r1, (*)r2
* (*)r1 = load_reg r0
*
* we'd insert a write-after-read dependency between the add and load_reg,
* even though the starred registers aren't actually used by the scheduler
* after this step. This step is crucial since it guarantees that during any
* point in the schedule, the number of live registers + live value registers
* will never exceed the capacity of the register file and the bypass network
* combined. This is because each live register/value register will have a
* different fake number, thanks to the fake dependencies inserted before
* scheduling. This allows us to not have to worry about spilling to
* temporaries, which is only done ahead of time.
*
* The scheduler is a bottom-up scheduler. It keeps track of each live value
* register, and decides on-the-fly which value registers to keep in the
* bypass network and which to "spill" to registers. Of particular importance
* is the "ready list," which consists of "input nodes" (nodes that produce a
* value that can be consumed via the bypass network), both "partially ready"
* (only some of the uses have been scheduled) and "fully ready" (all uses
* have been scheduled), as well as other non-input nodes like register
* stores. Each input node on the ready list represents a live value register
* before the current instruction. There must be at most 11 such input nodes
* at all times, since there are only 11 slots in the next two instructions
* which can reach the current instruction.
*
* An input node is a "max node" if it has a use two cycles ago, which must be
* connected to a definition this cycle. Otherwise it may be a "next max node"
* if it will be a max node on the next instruction (i.e. it has a use at most
* one cycle ago), or it may be neither if all of its uses are this cycle. As
* we keep adding instructions to the front, input nodes graduate from
* neither, to next max, to max, unless we decide to insert a move to keep it
* alive longer, at which point any uses after the current instruction are
* rewritten to be uses of the move so that the original node returns to
* neither. The scheduler decides which nodes to try freely, but we have to
* reserve slots for two different reasons: (1) out of the 5 non-complex
* slots, we reserve a slot for each max node, so that we can connect a
* definition to the use 2 cycles ago. (2) Out of all 6 slots, we reserve a
* slot for every next-max node above 5, so that for the next instruction
* there are no more than 5 max nodes. When a max or next-max node gets
* scheduled, the corresponding reservation is reduced by one. At the end, we
* insert moves for every slot that was reserved. The reservation is actually
* managed by nir_instr, and all we have to do is tell it how many to reserve
* at the beginning and then tell it which nodes are max/next-max nodes. When
* we start scheduling an instruction, there will be at most 5 max nodes
* thanks to the previous instruction's next-max reservation/move insertion.
* Since there are at most 11 total input nodes, if there are N max nodes,
* there are at most 11 - N next-max nodes, and therefore at most 11 - N - 5 =
* 6 - N slots need to be reserved for next-max nodes, and so at most
* 6 - N + N = 6 slots need to be reserved in total, exactly the total number
* of slots. So, thanks to the total input node restriction, we will never
* need to reserve too many slots.
*
* It sometimes happens that scheduling a given node will violate this total
* input node restriction, or that a reservation will mean that we can't
* schedule it. We first schedule a node "speculatively" to see if this is a
* problem. If some of the node's sources are loads, then we can schedule
* the node and its dependent loads in one swoop to avoid going over the
* pressure limit. If that fails, we can try to spill a ready or
* partially-ready input node to a register by rewriting all of its uses to
* refer to a register load. This removes it from the list of ready and
* partially ready input nodes as all of its uses are now unscheduled. If
* successful, we can then proceed with scheduling the original node. All of
* this happens "speculatively," meaning that afterwards the node is removed
* and the entire state of the scheduler is reverted to before it was tried, to
* ensure that we never get into an invalid state and run out of spots for
* moves. In try_nodes(), we try to schedule each node speculatively on the
* ready list, keeping only the nodes that could be successfully scheduled, so
* that when we finally decide which node to actually schedule, we know it
* will succeed. This is how we decide on the fly which values go in
* registers and which go in the bypass network. Note that "unspilling" a
* value is simply a matter of scheduling the store_reg instruction created
* when we spill.
*
* The careful accounting of live value registers, reservations for moves, and
* speculative scheduling guarantee that we never run into a failure case
* while scheduling. However, we need to make sure that this scheduler will
* not get stuck in an infinite loop, i.e. that we'll always make forward
* progress by eventually scheduling a non-move node. If we run out of value
* registers, then we may have to spill a node to a register. If we
* were to schedule one of the fully-ready nodes, then we'd have 11 + N live
* value registers before the current instruction. But since there are at most
* 64+11 live registers and register values total thanks to the fake
* dependencies we inserted before scheduling, there are at most 64 - N live
* physical registers, and therefore there are at least N registers available
* for spilling. Not all these registers will be available immediately, since
* in order to spill a node to a given register we have to ensure that there
* are slots available to rewrite every use to a load instruction, and that
* may not be the case. There may also be intervening writes which prevent
* some registers from being used. However, these are all temporary problems,
* since as we create each instruction, we create additional register load
* slots that can be freely used for spilling, and we create more move nodes
* which means that the uses of the nodes we're trying to spill keep moving
* forward. This means that eventually, these problems will go away, at which
* point we'll be able to spill a node successfully, so eventually we'll be
* able to schedule the first node on the ready list.
*/
typedef struct {
/* This is the list of ready and partially-ready nodes. A partially-ready
* node must have at least one input dependency already scheduled.
*/
struct list_head ready_list;
/* The number of ready or partially-ready nodes with at least one input
* dependency already scheduled. In other words, the number of live value
* registers. This must be at most 11.
*/
int ready_list_slots;
/* The physical registers live into the current instruction. */
uint64_t live_physregs;
/* The current instruction. */
gpir_instr *instr;
/* The current basic block. */
gpir_block *block;
/* True if at least one node failed to schedule due to lack of available
* value registers.
*/
bool try_spill_all;
/* The number of max nodes needed to spill to successfully schedule the
* instruction.
*/
int max_node_spill_needed;
/* The number of max and next-max nodes needed to spill to successfully
* schedule the instruction.
*/
int total_spill_needed;
} sched_ctx;
static int gpir_min_dist_alu(gpir_dep *dep)
{
switch (dep->pred->op) {
case gpir_op_load_uniform:
case gpir_op_load_temp:
case gpir_op_load_reg:
case gpir_op_load_attribute:
return 0;
case gpir_op_complex1:
return 2;
default:
return 1;
}
}
static int gpir_get_min_dist(gpir_dep *dep)
{
switch (dep->type) {
case GPIR_DEP_INPUT:
switch (dep->succ->op) {
case gpir_op_store_temp:
case gpir_op_store_reg:
case gpir_op_store_varying:
/* Stores must use an alu node as input. Also, complex1 takes two
* cycles, which means that its result cannot be stored to a register
* as part of the normal path, and therefore it must also have a move
* inserted.
*/
if (dep->pred->type == gpir_node_type_load ||
dep->pred->op == gpir_op_complex1)
return INT_MAX >> 2;
else
return 0;
default:
return gpir_min_dist_alu(dep);
}
case GPIR_DEP_OFFSET:
assert(dep->succ->op == gpir_op_store_temp);
return gpir_min_dist_alu(dep);
case GPIR_DEP_READ_AFTER_WRITE:
if (dep->succ->op == gpir_op_load_temp &&
dep->pred->op == gpir_op_store_temp) {
return 4;
} else if (dep->succ->op == gpir_op_load_reg &&
dep->pred->op == gpir_op_store_reg) {
return 3;
} else if ((dep->pred->op == gpir_op_store_temp_load_off0 ||
dep->pred->op == gpir_op_store_temp_load_off1 ||
dep->pred->op == gpir_op_store_temp_load_off2) &&
dep->succ->op == gpir_op_load_uniform) {
return 4;
} else {
/* Fake dependency */
return 0;
}
case GPIR_DEP_WRITE_AFTER_READ:
return 0;
}
return 0;
}
static int gpir_max_dist_alu(gpir_dep *dep)
{
switch (dep->pred->op) {
case gpir_op_load_uniform:
case gpir_op_load_temp:
return 0;
case gpir_op_load_attribute:
return 1;
case gpir_op_load_reg:
if (dep->pred->sched.pos < GPIR_INSTR_SLOT_REG0_LOAD0 ||
dep->pred->sched.pos > GPIR_INSTR_SLOT_REG0_LOAD3)
return 0;
else
return 1;
case gpir_op_exp2_impl:
case gpir_op_log2_impl:
case gpir_op_rcp_impl:
case gpir_op_rsqrt_impl:
case gpir_op_store_temp_load_off0:
case gpir_op_store_temp_load_off1:
case gpir_op_store_temp_load_off2:
return 1;
case gpir_op_mov:
if (dep->pred->sched.pos == GPIR_INSTR_SLOT_COMPLEX)
return 1;
else
return 2;
default:
return 2;
}
}
static int gpir_get_max_dist(gpir_dep *dep)
{
switch (dep->type) {
case GPIR_DEP_INPUT:
switch (dep->succ->op) {
case gpir_op_store_temp:
case gpir_op_store_reg:
case gpir_op_store_varying:
return 0;
default:
return gpir_max_dist_alu(dep);
}
case GPIR_DEP_OFFSET:
assert(dep->succ->op == gpir_op_store_temp);
return gpir_max_dist_alu(dep);
default:
return INT_MAX >> 2; /* Don't want to overflow... */
}
}
static void schedule_update_distance(gpir_node *node)
{
if (gpir_node_is_leaf(node)) {
node->sched.dist = 0;
return;
}
gpir_node_foreach_pred(node, dep) {
gpir_node *pred = dep->pred;
if (pred->sched.dist < 0)
schedule_update_distance(pred);
int dist = pred->sched.dist + gpir_min_dist_alu(dep);
if (node->sched.dist < dist)
node->sched.dist = dist;
}
}
static bool gpir_is_input_node(gpir_node *node)
{
gpir_node_foreach_succ(node, dep) {
if (dep->type == GPIR_DEP_INPUT)
return true;
}
return false;
}
/* Get the number of slots required for a node on the ready list.
*/
static int gpir_get_slots_required(gpir_node *node)
{
if (!gpir_is_input_node(node))
return 0;
/* Note that we assume every node only consumes one slot, even dual-slot
* instructions. While dual-slot instructions may consume more than one
* slot, we can always safely insert a move if it turns out that there
* isn't enough space for them. There's the risk that we get stuck in an
* infinite loop if all the fully ready nodes are dual-slot nodes, but we
* rely on spilling to registers to save us here.
*/
return 1;
}
static void verify_ready_list(sched_ctx *ctx)
{
list_for_each_entry(gpir_node, node, &ctx->ready_list, list) {
if (!gpir_is_input_node(node)) {
assert(node->sched.ready);
}
if (node->sched.ready) {
/* Every successor must have been scheduled */
gpir_node_foreach_succ(node, dep) {
assert(dep->succ->sched.instr);
}
} else {
/* There must be at least one successor that's not scheduled. */
bool unscheduled = false;
gpir_node_foreach_succ(node, dep) {
unscheduled |= !(dep->succ->sched.instr);
}
assert(unscheduled);
}
}
}
static void schedule_insert_ready_list(sched_ctx *ctx,
gpir_node *insert_node)
{
/* if this node is fully ready or partially ready
* fully ready: all successors have been scheduled
* partially ready: part of input successors have been scheduled
*
* either fully ready or partially ready node need be inserted to
* the ready list, but we only schedule a move node for partially
* ready node.
*/
bool ready = true, insert = false;
gpir_node_foreach_succ(insert_node, dep) {
gpir_node *succ = dep->succ;
if (succ->sched.instr) {
if (dep->type == GPIR_DEP_INPUT)
insert = true;
}
else
ready = false;
}
insert_node->sched.ready = ready;
/* for root node */
insert |= ready;
if (!insert || insert_node->sched.inserted)
return;
struct list_head *insert_pos = &ctx->ready_list;
list_for_each_entry(gpir_node, node, &ctx->ready_list, list) {
if (insert_node->sched.dist > node->sched.dist) {
insert_pos = &node->list;
break;
}
}
list_addtail(&insert_node->list, insert_pos);
insert_node->sched.inserted = true;
ctx->ready_list_slots += gpir_get_slots_required(insert_node);
}
static int gpir_get_max_start(gpir_node *node)
{
int max_start = 0;
/* find the max start instr constrainted by all successors */
gpir_node_foreach_succ(node, dep) {
gpir_node *succ = dep->succ;
if (!succ->sched.instr)
continue;
int start = succ->sched.instr->index + gpir_get_min_dist(dep);
if (start > max_start)
max_start = start;
}
return max_start;
}
static int gpir_get_min_end(gpir_node *node)
{
int min_end = INT_MAX;
/* find the min end instr constrainted by all successors */
gpir_node_foreach_succ(node, dep) {
gpir_node *succ = dep->succ;
if (!succ->sched.instr)
continue;
int end = succ->sched.instr->index + gpir_get_max_dist(dep);
if (end < min_end)
min_end = end;
}
return min_end;
}
static gpir_node *gpir_sched_instr_has_load(gpir_instr *instr, gpir_node *node)
{
gpir_load_node *load = gpir_node_to_load(node);
for (int i = GPIR_INSTR_SLOT_REG0_LOAD0; i <= GPIR_INSTR_SLOT_MEM_LOAD3; i++) {
if (!instr->slots[i])
continue;
gpir_load_node *iload = gpir_node_to_load(instr->slots[i]);
if (load->node.op == iload->node.op &&
load->index == iload->index &&
load->component == iload->component)
return &iload->node;
}
return NULL;
}
/* Simply place the node into the given instruction without trying to deal
* with liveness or the ready list. This will only fail if the instruction
* cannot be placed due to a lack of available slots. In addition to normal
* node placement, this is also used for placing loads when spilling to
* registers.
*/
static bool _try_place_node(sched_ctx *ctx, gpir_instr *instr, gpir_node *node)
{
if (node->type == gpir_node_type_load) {
gpir_node *load = gpir_sched_instr_has_load(instr, node);
if (load) {
/* This node may have a store as a successor, in which case we have to
* fail it exactly like below in order to later create a move node in
* between.
*/
if (instr->index < gpir_get_max_start(node))
return false;
gpir_debug("same load %d in instr %d for node %d\n",
load->index, instr->index, node->index);
/* not really merge two node, just fake scheduled same place */
node->sched.instr = load->sched.instr;
node->sched.pos = load->sched.pos;
return true;
}
}
node->sched.instr = instr;
int max_node_spill_needed = INT_MAX;
int total_spill_needed = INT_MAX;
int *slots = gpir_op_infos[node->op].slots;
for (int i = 0; slots[i] != GPIR_INSTR_SLOT_END; i++) {
node->sched.pos = slots[i];
if (instr->index >= gpir_get_max_start(node) &&
instr->index <= gpir_get_min_end(node) &&
gpir_instr_try_insert_node(instr, node))
return true;
if (ctx->instr->non_cplx_slot_difference ||
ctx->instr->slot_difference) {
/* If one of these fields is non-zero, then we could insert the node
* here after spilling. To get an accurate count of how many nodes we
* need to spill, we need to choose one of the positions where there
* were nonzero slot differences, preferably one with the smallest
* difference (so we don't have to spill as much).
*/
if (ctx->instr->non_cplx_slot_difference < max_node_spill_needed ||
ctx->instr->slot_difference < total_spill_needed) {
max_node_spill_needed = ctx->instr->non_cplx_slot_difference;
total_spill_needed = ctx->instr->slot_difference;
}
}
}
if (max_node_spill_needed != INT_MAX) {
/* Indicate how many spill nodes are needed. */
ctx->max_node_spill_needed = MAX2(ctx->max_node_spill_needed,
max_node_spill_needed);
ctx->total_spill_needed = MAX2(ctx->total_spill_needed,
total_spill_needed);
}
node->sched.instr = NULL;
node->sched.pos = -1;
return false;
}
/* Try to place just the node given, updating the ready list. If "speculative"
* is true, then this is part ofthe pre-commit phase. If false, then we have
* committed to placing this node, so update liveness and ready list
* information.
*/
static bool schedule_try_place_node(sched_ctx *ctx, gpir_node *node,
bool speculative)
{
if (!_try_place_node(ctx, ctx->instr, node)) {
if (!speculative)
gpir_debug("failed to place %d\n", node->index);
return false;
}
ctx->ready_list_slots -= gpir_get_slots_required(node);
if (!speculative) {
gpir_debug("placed node %d\n", node->index);
/* We assume here that writes are placed before reads. If this changes,
* then this needs to be updated.
*/
if (node->op == gpir_op_store_reg) {
gpir_store_node *store = gpir_node_to_store(node);
ctx->live_physregs &=
~(1ull << (4 * store->index + store->component));
if (store->child->sched.physreg_store == store)
store->child->sched.physreg_store = NULL;
}
if (node->op == gpir_op_load_reg) {
gpir_load_node *load = gpir_node_to_load(node);
ctx->live_physregs |=
(1ull << (4 * load->index + load->component));
}
list_del(&node->list);
list_add(&node->list, &ctx->block->node_list);
gpir_node_foreach_pred(node, dep) {
gpir_node *pred = dep->pred;
schedule_insert_ready_list(ctx, pred);
}
} else {
gpir_node_foreach_pred(node, dep) {
gpir_node *pred = dep->pred;
if (!pred->sched.inserted && dep->type == GPIR_DEP_INPUT)
ctx->ready_list_slots += gpir_get_slots_required(pred);
}
}
return true;
}
static gpir_node *create_move(sched_ctx *ctx, gpir_node *node)
{
gpir_alu_node *move = gpir_node_create(node->block, gpir_op_mov);
if (unlikely(!move))
return NULL;
move->children[0] = node;
move->num_child = 1;
move->node.sched.instr = NULL;
move->node.sched.pos = -1;
move->node.sched.dist = node->sched.dist;
move->node.sched.max_node = node->sched.max_node;
move->node.sched.next_max_node = node->sched.next_max_node;
move->node.sched.complex_allowed = node->sched.complex_allowed;
gpir_debug("create move %d for %d\n", move->node.index, node->index);
ctx->ready_list_slots--;
list_del(&node->list);
node->sched.max_node = false;
node->sched.next_max_node = false;
node->sched.ready = false;
node->sched.inserted = false;
gpir_node_replace_succ(&move->node, node);
gpir_node_add_dep(&move->node, node, GPIR_DEP_INPUT);
schedule_insert_ready_list(ctx, &move->node);
return &move->node;
}
/* Once we schedule the successor, would the predecessor be fully ready? */
static bool pred_almost_ready(gpir_dep *dep)
{
bool fully_ready = true;
gpir_node_foreach_succ(dep->pred, other_dep) {
gpir_node *succ = other_dep->succ;
if (!succ->sched.instr && dep->succ != other_dep->succ) {
fully_ready = false;
break;
}
}
return fully_ready;
}
/* Recursively try to schedule a node and all its dependent nodes that can fit
* in the same instruction. There is a simple heuristic scoring system to try
* to group together nodes that load different components of the same input,
* to avoid bottlenecking for operations like matrix multiplies that are
* mostly input-bound.
*/
static int _schedule_try_node(sched_ctx *ctx, gpir_node *node, bool speculative)
{
if (!schedule_try_place_node(ctx, node, speculative))
return INT_MIN;
int score = 0;
gpir_node_foreach_pred(node, dep) {
if (!gpir_is_input_node(dep->pred))
continue;
int pred_score = INT_MIN;
if (pred_almost_ready(dep)) {
if (dep->pred->type == gpir_node_type_load ||
node->type == gpir_node_type_store) {
pred_score = _schedule_try_node(ctx, dep->pred, speculative);
}
}
if (dep->pred->type == gpir_node_type_load ||
node->type == gpir_node_type_store) {
if (pred_score == INT_MIN) {
if (node->op == gpir_op_mov) {
/* The only moves on the ready list are for loads that we
* couldn't schedule immediately, as created below. If we
* couldn't schedule the load, there's no point scheduling
* the move. The normal move threading logic will ensure
* that another move is created if we're about to go too far
* from the uses of this move.
*/
assert(speculative);
return INT_MIN;
} else if (!speculative && dep->pred->type == gpir_node_type_load) {
/* We couldn't schedule the load right away, so it will have
* to happen in some earlier instruction and then be moved
* into a value register and threaded to the use by "node".
* We create the move right away, so that later we'll fail
* to schedule it if there isn't a slot for a move
* available.
*/
create_move(ctx, dep->pred);
}
/* Penalize nodes whose dependent ops we couldn't schedule.
*/
score--;
} else {
score += pred_score;
continue;
}
}
}
return score;
}
/* If we speculatively tried a node, undo everything.
*/
static void schedule_undo_node(sched_ctx *ctx, gpir_node *node)
{
gpir_instr_remove_node(ctx->instr, node);
gpir_node_foreach_pred(node, dep) {
gpir_node *pred = dep->pred;
if (pred->sched.instr) {
schedule_undo_node(ctx, pred);
}
}
}
/* Try to schedule a node. We also try to schedule any predecessors that can
* be part of the same instruction. If "speculative" is true, then we don't
* actually change any state, only returning the score were the node to be
* scheduled, with INT_MIN meaning "cannot be scheduled at all".
*/
static int schedule_try_node(sched_ctx *ctx, gpir_node *node, bool speculative)
{
int prev_slots = ctx->ready_list_slots;
int score = _schedule_try_node(ctx, node, speculative);
if (ctx->ready_list_slots > GPIR_VALUE_REG_NUM) {
assert(speculative);
ctx->total_spill_needed = MAX2(ctx->total_spill_needed,
ctx->ready_list_slots - GPIR_VALUE_REG_NUM);
score = INT_MIN;
}
if (speculative) {
ctx->ready_list_slots = prev_slots;
if (node->sched.instr)
schedule_undo_node(ctx, node);
}
return score;
}
/* This is called when we want to spill "node" by inserting loads at its uses.
* It returns all the possible registers we can use so that all the loads will
* successfully be inserted. Also return the first instruction we'll need to
* insert a load for.
*/
static uint64_t get_available_regs(sched_ctx *ctx, gpir_node *node,
int *min_index)
{
uint64_t available = ~0ull;
gpir_node_foreach_succ(node, dep) {
if (dep->type != GPIR_DEP_INPUT)
continue;
gpir_node *use = dep->succ;
gpir_instr *instr = use->sched.instr;
if (!instr) {
/* This use isn't scheduled, so no need to spill it. */
continue;
}
if (use->type == gpir_node_type_store) {
/* We're trying to spill something that was recently stored... just
* bail out.
*/
return 0;
}
if (use->op == gpir_op_mov && instr == ctx->instr) {
/* We try to spill the sources of this move, so we can free up space
* in the current instruction.
*
* TODO: should we go back further? It might let us schedule the
* write earlier in some cases, but then we might fail to spill.
*/
available &= get_available_regs(ctx, use, min_index);
} else {
if (instr->index < *min_index)
*min_index = instr->index;
uint64_t use_available = 0;
if (instr->reg0_use_count == 0)
use_available = ~0ull;
else if (!instr->reg0_is_attr)
use_available = 0xf << (4 * instr->reg0_index);
if (instr->reg1_use_count == 0)
use_available = ~0ull;
else
use_available |= 0xf << (4 * instr->reg1_index);
available &= use_available;
}
}
return available;
}
/* Using "min_index" returned by get_available_regs(), figure out which
* registers are killed by a write after or during the current instruction and
* hence we can't use for spilling. Writes that haven't been scheduled yet
* should be reflected in live_physregs.
*/
static uint64_t get_killed_regs(sched_ctx *ctx, int min_index)
{
uint64_t killed = 0;
list_for_each_entry(gpir_instr, instr, &ctx->block->instr_list, list) {
if (instr->index <= min_index)
break;
for (int slot = GPIR_INSTR_SLOT_STORE0; slot <= GPIR_INSTR_SLOT_STORE3;
slot++) {
if (!instr->slots[slot])
continue;
gpir_store_node *store = gpir_node_to_store(instr->slots[slot]);
if (store->node.op != gpir_op_store_reg)
continue;
killed |= 1ull << (4 * store->index + store->component);
}
}
return killed;
}
/* Actually spill a node so that it is no longer in the ready list. Note that
* this must exactly follow the logic of get_available_regs() or else the
* loads could fail to schedule.
*/
static void spill_node(sched_ctx *ctx, gpir_node *node, gpir_store_node *store)
{
gpir_node_foreach_succ_safe(node, dep) {
if (dep->type != GPIR_DEP_INPUT)
continue;
gpir_node *use = dep->succ;
gpir_instr *instr = use->sched.instr;
if (!instr)
continue;
if (use->op == gpir_op_mov && instr == ctx->instr) {
spill_node(ctx, use, store);
} else {
gpir_load_node *load = gpir_node_create(ctx->block, gpir_op_load_reg);
load->index = store->index;
load->component = store->component;
list_add(&load->node.list, &ctx->block->node_list);
gpir_node_replace_child(dep->succ, dep->pred, &load->node);
gpir_node_replace_pred(dep, &load->node);
gpir_node_add_dep(&load->node, &store->node, GPIR_DEP_READ_AFTER_WRITE);
gpir_debug("spilling use %d of node %d to load node %d\n",
use->index, node->index, load->node.index);
MAYBE_UNUSED bool result = _try_place_node(ctx, use->sched.instr, &load->node);
assert(result);
}
}
if (node->op == gpir_op_mov) {
/* We replaced all the uses of the move, so it's dead now. */
gpir_instr_remove_node(node->sched.instr, node);
gpir_node_delete(node);
} else {
/* We deleted all the uses of the node except the store, so it's not
* live anymore.
*/
list_del(&node->list);
node->sched.inserted = false;
ctx->ready_list_slots--;
if (node->sched.max_node) {
node->sched.max_node = false;
ctx->instr->alu_num_slot_needed_by_max--;
}
if (node->sched.next_max_node) {
node->sched.next_max_node = false;
ctx->instr->alu_num_slot_needed_by_next_max--;
}
}
}
static bool used_by_store(gpir_node *node, gpir_instr *instr)
{
gpir_node_foreach_succ(node, dep) {
if (dep->type != GPIR_DEP_INPUT)
continue;
if (dep->succ->type == gpir_node_type_store &&
dep->succ->sched.instr == instr)
return true;
}
return false;
}
static bool try_spill_node(sched_ctx *ctx, gpir_node *node)
{
assert(node->op != gpir_op_mov);
if (used_by_store(node, ctx->instr))
return false;
gpir_debug("trying to spill %d\n", node->index);
int min_instr = INT_MAX;
uint64_t available = get_available_regs(ctx, node, &min_instr);
available &= ~get_killed_regs(ctx, min_instr);
if (node->sched.physreg_store) {
gpir_store_node *store = node->sched.physreg_store;
if (!(available & (1ull << (4 * store->index + store->component))))
return false;
} else {
available &= ~ctx->live_physregs;
if (available == 0)
return false;
/* TODO: use a better heuristic for choosing an available register? */
int physreg = ffsll(available) - 1;
ctx->live_physregs |= (1ull << physreg);
/* TODO: when we support multiple basic blocks, there may be register
* loads/stores to this register other than this one that haven't been
* scheduled yet so we may need to insert write-after-read dependencies.
*/
gpir_store_node *store = gpir_node_create(ctx->block, gpir_op_store_reg);
store->index = physreg / 4;
store->component = physreg % 4;
store->child = node;
store->node.sched.max_node = false;
store->node.sched.next_max_node = false;
store->node.sched.complex_allowed = false;
store->node.sched.pos = -1;
store->node.sched.instr = NULL;
store->node.sched.inserted = false;
store->node.sched.dist = node->sched.dist;
if (node->op == gpir_op_complex1) {
/* Complex1 cannot be directly stored, and has a latency of 2 */
store->node.sched.dist += 2;
}
node->sched.physreg_store = store;
gpir_node_add_dep(&store->node, node, GPIR_DEP_INPUT);
node->sched.ready = false;
schedule_insert_ready_list(ctx, &store->node);
}
gpir_debug("spilling %d to $%d.%c, store %d\n", node->index,
node->sched.physreg_store->index,
"xyzw"[node->sched.physreg_store->component],
node->sched.physreg_store->node.index);
spill_node(ctx, node, node->sched.physreg_store);
return true;
}
static bool try_spill_nodes(sched_ctx *ctx, gpir_node *orig_node)
{
/* First, try to spill max nodes. */
list_for_each_entry_safe_rev(gpir_node, node, &ctx->ready_list, list) {
if (ctx->max_node_spill_needed <= 0)
break;
/* orig_node is the node we're trying to schedule, so spilling it makes
* no sense. Also don't try to spill any nodes in front of it, since
* they might be scheduled instead.
*/
if (node == orig_node)
break;
if (node->op == gpir_op_mov) {
/* Don't try to spill loads, since that only adds another load and
* store which is likely pointless.
*/
continue;
}
if (!gpir_is_input_node(node) || !node->sched.max_node)
continue;
if (try_spill_node(ctx, node)) {
ctx->max_node_spill_needed--;
ctx->total_spill_needed--;
}
}
/* Now, try to spill the remaining nodes. */
list_for_each_entry_safe_rev(gpir_node, node, &ctx->ready_list, list) {
if (ctx->total_spill_needed <= 0)
break;
if (node == orig_node)
break;
if (node->op == gpir_op_mov)
continue;
if (!gpir_is_input_node(node) ||
!(node->sched.max_node || node->sched.next_max_node))
continue;
if (try_spill_node(ctx, node))
ctx->total_spill_needed--;
}
return ctx->total_spill_needed <= 0 && ctx->max_node_spill_needed <= 0;
}
static int gpir_get_curr_ready_list_slots(sched_ctx *ctx)
{
int total = 0;
list_for_each_entry(gpir_node, node, &ctx->ready_list, list) {
total += gpir_get_slots_required(node);
}
return total;
}
/* What gpir_get_min_end() would return if node were replaced with a move
* instruction not in the complex slot. Normally this is 2 + min_end, except
* for some store instructions which must have the move node in the same
* instruction.
*/
static int gpir_get_min_end_as_move(gpir_node *node)
{
int min = INT_MAX;
gpir_node_foreach_succ(node, dep) {
gpir_node *succ = dep->succ;
if (succ->sched.instr && dep->type == GPIR_DEP_INPUT) {
switch (succ->op) {
case gpir_op_store_temp:
case gpir_op_store_reg:
case gpir_op_store_varying:
continue;
default:
break;
}
if (min > succ->sched.instr->index + 2)
min = succ->sched.instr->index + 2;
}
}
return min;
}
/* The second source for add0, add1, mul0, and mul1 units cannot be complex.
* The hardware overwrites the add second sources with 0 and mul second
* sources with 1. This can be a problem if we need to insert more next-max
* moves but we only have values that can't use the complex unit for moves.
*
* Fortunately, we only need to insert a next-max move if there are more than
* 5 next-max nodes, but there are only 4 sources in the previous instruction
* that make values not complex-capable, which means there can be at most 4
* non-complex-capable values. Hence there will always be at least two values
* that can be rewritten to use a move in the complex slot. However, we have
* to be careful not to waste those values by putting both of them in a
* non-complex slot. This is handled for us by gpir_instr, which will reject
* such instructions. We just need to tell it which nodes can use complex, and
* it will do the accounting to figure out what is safe.
*/
static bool can_use_complex(gpir_node *node)
{
gpir_node_foreach_succ(node, dep) {
if (dep->type != GPIR_DEP_INPUT)
continue;
gpir_node *succ = dep->succ;
if (succ->type != gpir_node_type_alu)
continue;
/* Note: this must be consistent with gpir_codegen_{mul,add}_slot{0,1}
*/
gpir_alu_node *alu = gpir_node_to_alu(succ);
switch (alu->node.op) {
case gpir_op_complex1:
/* complex1 puts its third source in the fourth slot */
if (alu->children[1] == node || alu->children[2] == node)
return false;
break;
case gpir_op_complex2:
/* complex2 has its source duplicated, since it actually takes two
* sources but we only ever use it with both sources the same. Hence
* its source can never be the complex slot.
*/
return false;
case gpir_op_select:
/* Select has its sources rearranged */
if (alu->children[0] == node)
return false;
break;
default:
assert(alu->num_child <= 2);
if (alu->num_child == 2 && alu->children[1] == node)
return false;
break;
}
}
return true;
}
/* Initialize node->sched.max_node and node->sched.next_max_node for every
* input node on the ready list. We should only need to do this once per
* instruction, at the beginning, since we never add max nodes to the ready
* list.
*/
static void sched_find_max_nodes(sched_ctx *ctx)
{
ctx->instr->alu_num_slot_needed_by_next_max = -5;
ctx->instr->alu_num_slot_needed_by_max = 0;
list_for_each_entry(gpir_node, node, &ctx->ready_list, list) {
if (!gpir_is_input_node(node))
continue;
int min_end_move = gpir_get_min_end_as_move(node);
node->sched.max_node = (min_end_move == ctx->instr->index);
node->sched.next_max_node = (min_end_move == ctx->instr->index + 1);
if (node->sched.next_max_node)
node->sched.complex_allowed = can_use_complex(node);
if (node->sched.max_node)
ctx->instr->alu_num_slot_needed_by_max++;
if (node->sched.next_max_node)
ctx->instr->alu_num_slot_needed_by_next_max++;
}
}
/* Verify the invariants described in gpir.h, as well as making sure the
* counts are correct.
*/
static void verify_max_nodes(sched_ctx *ctx)
{
int alu_num_slot_needed_by_max = 0;
int alu_num_slot_needed_by_next_max = -5;
int alu_num_slot_needed_by_store = 0;
int alu_num_slot_needed_by_non_cplx_store = 0;
list_for_each_entry(gpir_node, node, &ctx->ready_list, list) {
if (!gpir_is_input_node(node))
continue;
if (node->sched.max_node)
alu_num_slot_needed_by_max++;
if (node->sched.next_max_node)
alu_num_slot_needed_by_next_max++;
if (used_by_store(node, ctx->instr)) {
alu_num_slot_needed_by_store++;
if (node->sched.next_max_node && !node->sched.complex_allowed)
alu_num_slot_needed_by_non_cplx_store++;
}
}
assert(ctx->instr->alu_num_slot_needed_by_max == alu_num_slot_needed_by_max);
assert(ctx->instr->alu_num_slot_needed_by_next_max == alu_num_slot_needed_by_next_max);
assert(ctx->instr->alu_num_slot_needed_by_store == alu_num_slot_needed_by_store);
assert(ctx->instr->alu_num_slot_needed_by_non_cplx_store ==
alu_num_slot_needed_by_non_cplx_store);
assert(ctx->instr->alu_num_slot_free >= alu_num_slot_needed_by_store + alu_num_slot_needed_by_max + MAX2(alu_num_slot_needed_by_next_max, 0));
assert(ctx->instr->alu_non_cplx_slot_free >= alu_num_slot_needed_by_max + alu_num_slot_needed_by_non_cplx_store);
}
static bool try_node(sched_ctx *ctx)
{
gpir_node *best_node = NULL;
int best_score = INT_MIN;
/* Spilling will delete arbitrary nodes after the current one in the ready
* list, which means that we always need to look up the next node in the
* list at the end of each iteration. While list_for_each_entry() works for
* this purpose, its sanity checking assumes that you don't want to modify
* the list at all. We know better here, so we have to open-code
* list_for_each_entry() without the check in order to not assert.
*/
for (gpir_node *node = LIST_ENTRY(gpir_node, ctx->ready_list.next, list);
&node->list != &ctx->ready_list;
node = LIST_ENTRY(gpir_node, node->list.next, list)) {
if (best_score != INT_MIN) {
if (node->sched.dist < best_node->sched.dist)
break;
}
if (node->sched.ready) {
ctx->total_spill_needed = 0;
ctx->max_node_spill_needed = 0;
int score = schedule_try_node(ctx, node, true);
if (score == INT_MIN && !best_node &&
ctx->total_spill_needed > 0 &&
try_spill_nodes(ctx, node)) {
score = schedule_try_node(ctx, node, true);
}
if (score > best_score) {
best_score = score;
best_node = node;
}
}
}
if (best_node) {
gpir_debug("scheduling %d (score = %d)%s\n", best_node->index,
best_score, best_node->sched.max_node ? " (max)" : "");
MAYBE_UNUSED int score = schedule_try_node(ctx, best_node, false);
assert(score != INT_MIN);
return true;
}
return false;
}
static void place_move(sched_ctx *ctx, gpir_node *node)
{
gpir_node *move = create_move(ctx, node);
gpir_node_foreach_succ_safe(move, dep) {
gpir_node *succ = dep->succ;
if (!succ->sched.instr ||
ctx->instr->index < succ->sched.instr->index + gpir_get_min_dist(dep)) {
gpir_node_replace_pred(dep, node);
if (dep->type == GPIR_DEP_INPUT)
gpir_node_replace_child(succ, move, node);
}
}
MAYBE_UNUSED int score = schedule_try_node(ctx, move, false);
assert(score != INT_MIN);
}
/* For next-max nodes, not every node can be offloaded to a move in the
* complex slot. If we run out of non-complex slots, then such nodes cannot
* have moves placed for them. There should always be sufficient
* complex-capable nodes so that this isn't a problem.
*/
static bool can_place_move(sched_ctx *ctx, gpir_node *node)
{
if (!node->sched.next_max_node)
return true;
if (node->sched.complex_allowed)
return true;
return ctx->instr->alu_non_cplx_slot_free > 0;
}
static bool sched_move(sched_ctx *ctx)
{
list_for_each_entry(gpir_node, node, &ctx->ready_list, list) {
if (node->sched.max_node) {
place_move(ctx, node);
return true;
}
}
if (ctx->instr->alu_num_slot_needed_by_store > 0) {
list_for_each_entry(gpir_node, node, &ctx->ready_list, list) {
if (used_by_store(node, ctx->instr)) {
place_move(ctx, node);
/* If we have a store of a load, then we need to make sure that we
* immediately schedule the dependent load, or create a move
* instruction for it, like we would with a normal instruction.
* The rest of the code isn't set up to handle load nodes in the
* ready list -- see the comments in _schedule_try_node().
*/
if (node->type == gpir_node_type_load) {
if (!schedule_try_place_node(ctx, node, false)) {
create_move(ctx, node);
}
}
return true;
}
}
}
/* complex1 is a bit a special case, since it has a latency of 2 cycles.
* Once it is fully ready, we need to group all its uses in the same
* instruction, and then we need to avoid creating any moves in the next
* cycle in order to get it scheduled. Failing to do any of these things
* could result in a cycle penalty, or even worse, an infinite loop of
* inserting moves. If it is a next-max node and ready, then it has a use
* in the previous cycle. If it has a use in the current cycle as well,
* then we want to insert a move node to make it ready in two cycles -- if
* we don't, then there will be at least a one cycle penalty. Otherwise, it
* will be ready next cycle, and we shouldn't insert a move node, or else
* we'll also have a one cycle penalty.
*/
if (ctx->instr->alu_num_slot_free > 0) {
list_for_each_entry(gpir_node, node, &ctx->ready_list, list) {
if (!can_place_move(ctx, node))
continue;
if (node->sched.next_max_node && node->op == gpir_op_complex1 &&
node->sched.ready) {
bool skip = true;
gpir_node_foreach_succ(node, dep) {
if (dep->type != GPIR_DEP_INPUT)
continue;
gpir_node *succ = dep->succ;
if (!succ->sched.instr ||
succ->sched.instr->index != ctx->instr->index - 1) {
skip = false;
break;
}
}
if (skip)
continue;
place_move(ctx, node);
return true;
}
}
}
/* Once we've made all the required moves, we're free to use any extra
* slots to schedule more moves for next max nodes. Besides sometimes being
* necessary, this can free up extra space in the next instruction. We walk
* from back to front so that we pick nodes less likely to be scheduled
* next first -- an extra move would be unnecessary there. But make sure
* not to handle the complex1 case handled above.
*/
if (ctx->instr->alu_num_slot_free > 0) {
list_for_each_entry_rev(gpir_node, node, &ctx->ready_list, list) {
if (!can_place_move(ctx, node))
continue;
if (node->sched.next_max_node &&
!(node->op == gpir_op_complex1 && node->sched.ready)) {
place_move(ctx, node);
return true;
}
}
}
/* We may have skipped complex1 above, but if we run out of space, we still
* need to insert the move.
*/
if (ctx->instr->alu_num_slot_needed_by_next_max > 0) {
list_for_each_entry(gpir_node, node, &ctx->ready_list, list) {
if (!can_place_move(ctx, node))
continue;
if (node->sched.next_max_node) {
place_move(ctx, node);
return true;
}
}
}
return false;
}
static bool gpir_sched_instr_pass(sched_ctx *ctx)
{
if (try_node(ctx))
return true;
if (sched_move(ctx))
return true;
return false;
}
static void schedule_print_pre_one_instr(sched_ctx *ctx)
{
if (!(lima_debug & LIMA_DEBUG_GP))
return;
printf("instr %d for ready list:", ctx->instr->index);
list_for_each_entry(gpir_node, node, &ctx->ready_list, list) {
printf(" %d/%c (%d, %d, %s)", node->index, node->sched.ready ? 'r' : 'p',
node->sched.dist, gpir_get_slots_required(node),
node->sched.max_node ? "max" : (node->sched.next_max_node ? "next" : "none"));
}
printf("\nlive physregs: ");
for (unsigned i = 0; i < 16; i++) {
if (ctx->live_physregs & (0xfull << (4 * i))) {
printf("$%d.", i);
for (unsigned j = 0; j < 4; j++) {
if (ctx->live_physregs & (1ull << (4 * i + j)))
printf("%c", "xyzw"[j]);
}
printf(" ");
}
}
printf("\n");
}
static void schedule_print_post_one_instr(gpir_instr *instr)
{
if (!(lima_debug & LIMA_DEBUG_GP))
return;
printf("post schedule instr");
for (int i = 0; i < GPIR_INSTR_SLOT_NUM; i++) {
if (instr->slots[i])
printf(" %d/%d", i, instr->slots[i]->index);
}
printf("\n");
}
static bool schedule_one_instr(sched_ctx *ctx)
{
gpir_instr *instr = gpir_instr_create(ctx->block);
if (unlikely(!instr))
return false;
ctx->instr = instr;
sched_find_max_nodes(ctx);
schedule_print_pre_one_instr(ctx);
while (gpir_sched_instr_pass(ctx)) {
assert(ctx->ready_list_slots == gpir_get_curr_ready_list_slots(ctx));
#ifndef NDEBUG
verify_max_nodes(ctx);
verify_ready_list(ctx);
#endif
}
schedule_print_post_one_instr(instr);
return true;
}
static bool schedule_block(gpir_block *block)
{
/* calculate distance */
list_for_each_entry(gpir_node, node, &block->node_list, list) {
if (gpir_node_is_root(node))
schedule_update_distance(node);
}
sched_ctx ctx;
list_inithead(&ctx.ready_list);
ctx.block = block;
ctx.ready_list_slots = 0;
/* TODO initialize with block live out once we have proper liveness
* tracking
*/
ctx.live_physregs = 0;
/* construct the ready list from root nodes */
list_for_each_entry_safe(gpir_node, node, &block->node_list, list) {
if (gpir_node_is_root(node))
schedule_insert_ready_list(&ctx, node);
}
list_inithead(&block->node_list);
while (!list_empty(&ctx.ready_list)) {
if (!schedule_one_instr(&ctx))
return false;
}
return true;
}
static void schedule_build_dependency(gpir_block *block)
{
gpir_node *last_written[GPIR_VALUE_REG_NUM + GPIR_PHYSICAL_REG_NUM] = {0};
/* merge dummy_f/m to the node created from */
list_for_each_entry_safe(gpir_node, node, &block->node_list, list) {
if (node->op == gpir_op_dummy_m) {
gpir_alu_node *alu = gpir_node_to_alu(node);
gpir_node *origin = alu->children[0];
gpir_node *dummy_f = alu->children[1];
gpir_node_foreach_succ(node, dep) {
gpir_node *succ = dep->succ;
/* origin and node may have same succ (by VREG/INPUT or
* VREG/VREG dep), so use gpir_node_add_dep() instead of
* gpir_node_replace_pred() */
gpir_node_add_dep(succ, origin, dep->type);
gpir_node_replace_child(succ, node, origin);
}
gpir_node_delete(dummy_f);
gpir_node_delete(node);
}
}
/* Forward dependencies. We only need to add these for register loads,
* since value registers already have an input dependency.
*/
list_for_each_entry(gpir_node, node, &block->node_list, list) {
if (node->op == gpir_op_load_reg) {
gpir_load_node *load = gpir_node_to_load(node);
unsigned index = 4 * load->index + load->component;
if (last_written[index]) {
gpir_node_add_dep(node, last_written[index], GPIR_DEP_READ_AFTER_WRITE);
}
}
if (node->value_reg >= 0)
last_written[node->value_reg] = node;
}
memset(last_written, 0, sizeof(last_written));
/* False dependencies. For value registers, these exist only to make sure
* that the maximum pressure isn't exceeded and are hence "fake".
*/
list_for_each_entry_rev(gpir_node, node, &block->node_list, list) {
if (node->op == gpir_op_load_reg) {
gpir_load_node *load = gpir_node_to_load(node);
unsigned index = 4 * load->index + load->component;
if (last_written[index]) {
gpir_node_add_dep(last_written[index], node, GPIR_DEP_WRITE_AFTER_READ);
}
} else {
gpir_node_foreach_pred(node, dep) {
if (dep->type == GPIR_DEP_INPUT) {
int index = dep->pred->value_reg;
if (index >= 0 && last_written[index]) {
gpir_node_add_dep(last_written[index], node,
GPIR_DEP_WRITE_AFTER_READ);
}
}
}
}
if (node->value_reg >= 0)
last_written[node->value_reg] = node;
}
}
static void print_statistic(gpir_compiler *comp, int save_index)
{
int num_nodes[gpir_op_num] = {0};
int num_created_nodes[gpir_op_num] = {0};
list_for_each_entry(gpir_block, block, &comp->block_list, list) {
list_for_each_entry(gpir_node, node, &block->node_list, list) {
num_nodes[node->op]++;
if (node->index >= save_index)
num_created_nodes[node->op]++;
}
}
printf("====== gpir scheduler statistic ======\n");
printf("---- how many nodes are scheduled ----\n");
int n = 0, l = 0;
for (int i = 0; i < gpir_op_num; i++) {
if (num_nodes[i]) {
printf("%10s:%-6d", gpir_op_infos[i].name, num_nodes[i]);
n += num_nodes[i];
if (!(++l % 4))
printf("\n");
}
}
if (l % 4)
printf("\n");
printf("\ntotal: %d\n", n);
printf("---- how many nodes are created ----\n");
n = l = 0;
for (int i = 0; i < gpir_op_num; i++) {
if (num_created_nodes[i]) {
printf("%10s:%-6d", gpir_op_infos[i].name, num_created_nodes[i]);
n += num_created_nodes[i];
if (!(++l % 4))
printf("\n");
}
}
if (l % 4)
printf("\n");
printf("\ntotal: %d\n", n);
printf("------------------------------------\n");
}
bool gpir_schedule_prog(gpir_compiler *comp)
{
int save_index = comp->cur_index;
/* init schedule info */
int index = 0;
list_for_each_entry(gpir_block, block, &comp->block_list, list) {
block->sched.instr_index = 0;
list_for_each_entry(gpir_node, node, &block->node_list, list) {
node->sched.instr = NULL;
node->sched.pos = -1;
node->sched.index = index++;
node->sched.dist = -1;
/* TODO when we support multiple basic blocks, we need a way to keep
* track of this for physregs allocated before the scheduler.
*/
node->sched.physreg_store = NULL;
node->sched.ready = false;
node->sched.inserted = false;
node->sched.complex_allowed = false;
node->sched.max_node = false;
node->sched.next_max_node = false;
}
}
/* build dependency */
list_for_each_entry(gpir_block, block, &comp->block_list, list) {
schedule_build_dependency(block);
}
//gpir_debug("after scheduler build reg dependency\n");
//gpir_node_print_prog_dep(comp);
list_for_each_entry(gpir_block, block, &comp->block_list, list) {
if (!schedule_block(block)) {
gpir_error("fail schedule block\n");
return false;
}
}
if (lima_debug & LIMA_DEBUG_GP) {
print_statistic(comp, save_index);
gpir_instr_print_prog(comp);
}
return true;
}
|