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
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
|
/* OpenACC worker partitioning via middle end neutering/broadcasting scheme
Copyright (C) 2015-2024 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3, or (at your
option) any later version.
GCC is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "rtl.h"
#include "tree.h"
#include "gimple.h"
#include "tree-pass.h"
#include "ssa.h"
#include "cgraph.h"
#include "pretty-print.h"
#include "fold-const.h"
#include "gimplify.h"
#include "gimple-iterator.h"
#include "gimple-walk.h"
#include "tree-inline.h"
#include "langhooks.h"
#include "omp-general.h"
#include "omp-low.h"
#include "gimple-pretty-print.h"
#include "cfghooks.h"
#include "insn-config.h"
#include "recog.h"
#include "internal-fn.h"
#include "bitmap.h"
#include "tree-nested.h"
#include "stor-layout.h"
#include "tree-ssa-threadupdate.h"
#include "tree-into-ssa.h"
#include "splay-tree.h"
#include "target.h"
#include "cfgloop.h"
#include "tree-cfg.h"
#include "omp-offload.h"
#include "attribs.h"
#include "targhooks.h"
#include "diagnostic-core.h"
/* Loop structure of the function. The entire function is described as
a NULL loop. */
/* Adapted from 'gcc/config/nvptx/nvptx.cc:struct parallel'. */
struct parallel_g
{
/* Parent parallel. */
parallel_g *parent;
/* Next sibling parallel. */
parallel_g *next;
/* First child parallel. */
parallel_g *inner;
/* Partitioning mask of the parallel. */
unsigned mask;
/* Partitioning used within inner parallels. */
unsigned inner_mask;
/* Location of parallel forked and join. The forked is the first
block in the parallel and the join is the first block after of
the partition. */
basic_block forked_block;
basic_block join_block;
gimple *forked_stmt;
gimple *join_stmt;
gimple *fork_stmt;
gimple *joining_stmt;
/* Basic blocks in this parallel, but not in child parallels. The
FORKED and JOINING blocks are in the partition. The FORK and JOIN
blocks are not. */
auto_vec<basic_block> blocks;
tree record_type;
tree sender_decl;
tree receiver_decl;
public:
parallel_g (parallel_g *parent, unsigned mode);
~parallel_g ();
};
/* Constructor links the new parallel into it's parent's chain of
children. */
parallel_g::parallel_g (parallel_g *parent_, unsigned mask_)
:parent (parent_), next (0), inner (0), mask (mask_), inner_mask (0)
{
forked_block = join_block = 0;
forked_stmt = join_stmt = NULL;
fork_stmt = joining_stmt = NULL;
record_type = NULL_TREE;
sender_decl = NULL_TREE;
receiver_decl = NULL_TREE;
if (parent)
{
next = parent->inner;
parent->inner = this;
}
}
parallel_g::~parallel_g ()
{
delete inner;
delete next;
}
static bool
local_var_based_p (tree decl)
{
switch (TREE_CODE (decl))
{
case VAR_DECL:
return !is_global_var (decl);
case COMPONENT_REF:
case BIT_FIELD_REF:
case ARRAY_REF:
return local_var_based_p (TREE_OPERAND (decl, 0));
default:
return false;
}
}
/* Map of basic blocks to gimple stmts. */
typedef hash_map<basic_block, gimple *> bb_stmt_map_t;
/* Calls to OpenACC routines are made by all workers/wavefronts/warps, since
the routine likely contains partitioned loops (else will do its own
neutering and variable propagation). Return TRUE if a function call CALL
should be made in (worker) single mode instead, rather than redundant
mode. */
static bool
omp_sese_active_worker_call (gcall *call)
{
#define GOMP_DIM_SEQ GOMP_DIM_MAX
tree fndecl = gimple_call_fndecl (call);
if (!fndecl)
return true;
tree attrs = oacc_get_fn_attrib (fndecl);
if (!attrs)
return true;
int level = oacc_fn_attrib_level (attrs);
/* Neither regular functions nor "seq" routines should be run by all threads
in worker-single mode. */
return level == -1 || level == GOMP_DIM_SEQ;
#undef GOMP_DIM_SEQ
}
/* Split basic blocks such that each forked and join unspecs are at
the start of their basic blocks. Thus afterwards each block will
have a single partitioning mode. We also do the same for return
insns, as they are executed by every thread. Return the
partitioning mode of the function as a whole. Populate MAP with
head and tail blocks. We also clear the BB visited flag, which is
used when finding partitions. */
/* Adapted from 'gcc/config/nvptx/nvptx.cc:nvptx_split_blocks'. */
static void
omp_sese_split_blocks (bb_stmt_map_t *map)
{
auto_vec<gimple *> worklist;
basic_block block;
/* Locate all the reorg instructions of interest. */
FOR_ALL_BB_FN (block, cfun)
{
/* Clear visited flag, for use by parallel locator */
block->flags &= ~BB_VISITED;
for (gimple_stmt_iterator gsi = gsi_start_bb (block);
!gsi_end_p (gsi);
gsi_next (&gsi))
{
gimple *stmt = gsi_stmt (gsi);
if (gimple_call_internal_p (stmt, IFN_UNIQUE))
{
enum ifn_unique_kind k = ((enum ifn_unique_kind)
TREE_INT_CST_LOW (gimple_call_arg (stmt, 0)));
if (k == IFN_UNIQUE_OACC_JOIN)
worklist.safe_push (stmt);
else if (k == IFN_UNIQUE_OACC_FORK)
{
gcc_assert (gsi_one_before_end_p (gsi));
basic_block forked_block = single_succ (block);
gimple_stmt_iterator gsi2 = gsi_start_bb (forked_block);
/* We push a NOP as a placeholder for the "forked" stmt.
This is then recognized in omp_sese_find_par. */
gimple *nop = gimple_build_nop ();
gsi_insert_before (&gsi2, nop, GSI_SAME_STMT);
worklist.safe_push (nop);
}
}
else if (gimple_code (stmt) == GIMPLE_RETURN
|| gimple_code (stmt) == GIMPLE_COND
|| gimple_code (stmt) == GIMPLE_SWITCH
|| (gimple_code (stmt) == GIMPLE_CALL
&& !gimple_call_internal_p (stmt)
&& !omp_sese_active_worker_call (as_a <gcall *> (stmt))))
worklist.safe_push (stmt);
else if (is_gimple_assign (stmt))
{
tree lhs = gimple_assign_lhs (stmt);
/* Force assignments to components/fields/elements of local
aggregates into fully-partitioned (redundant) mode. This
avoids having to broadcast the whole aggregate. The RHS of
the assignment will be propagated using the normal
mechanism. */
switch (TREE_CODE (lhs))
{
case COMPONENT_REF:
case BIT_FIELD_REF:
case ARRAY_REF:
{
tree aggr = TREE_OPERAND (lhs, 0);
if (local_var_based_p (aggr))
worklist.safe_push (stmt);
}
break;
default:
;
}
}
}
}
/* Split blocks on the worklist. */
unsigned ix;
gimple *stmt;
for (ix = 0; worklist.iterate (ix, &stmt); ix++)
{
basic_block block = gimple_bb (stmt);
if (gimple_code (stmt) == GIMPLE_COND)
{
gcond *orig_cond = as_a <gcond *> (stmt);
tree_code code = gimple_expr_code (orig_cond);
tree pred = make_ssa_name (boolean_type_node);
gimple *asgn = gimple_build_assign (pred, code,
gimple_cond_lhs (orig_cond),
gimple_cond_rhs (orig_cond));
gcond *new_cond
= gimple_build_cond (NE_EXPR, pred, boolean_false_node,
gimple_cond_true_label (orig_cond),
gimple_cond_false_label (orig_cond));
gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
gsi_insert_before (&gsi, asgn, GSI_SAME_STMT);
gsi_replace (&gsi, new_cond, true);
edge e = split_block (block, asgn);
block = e->dest;
map->get_or_insert (block) = new_cond;
}
else if ((gimple_code (stmt) == GIMPLE_CALL
&& !gimple_call_internal_p (stmt))
|| is_gimple_assign (stmt))
{
gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
gsi_prev (&gsi);
edge call = split_block (block, gsi_stmt (gsi));
gimple *call_stmt = gsi_stmt (gsi_start_bb (call->dest));
edge call_to_ret = split_block (call->dest, call_stmt);
map->get_or_insert (call_to_ret->src) = call_stmt;
}
else
{
gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
gsi_prev (&gsi);
if (gsi_end_p (gsi))
map->get_or_insert (block) = stmt;
else
{
/* Split block before insn. The insn is in the new block. */
edge e = split_block (block, gsi_stmt (gsi));
block = e->dest;
map->get_or_insert (block) = stmt;
}
}
}
}
static const char *
mask_name (unsigned mask)
{
switch (mask)
{
case 0: return "gang redundant";
case 1: return "gang partitioned";
case 2: return "worker partitioned";
case 3: return "gang+worker partitioned";
case 4: return "vector partitioned";
case 5: return "gang+vector partitioned";
case 6: return "worker+vector partitioned";
case 7: return "fully partitioned";
default: return "<illegal>";
}
}
/* Dump this parallel and all its inner parallels. */
/* Adapted from 'gcc/config/nvptx/nvptx.cc:nvptx_dump_pars'. */
static void
omp_sese_dump_pars (parallel_g *par, unsigned depth)
{
fprintf (dump_file, "%u: mask %d (%s) head=%d, tail=%d\n",
depth, par->mask, mask_name (par->mask),
par->forked_block ? par->forked_block->index : -1,
par->join_block ? par->join_block->index : -1);
fprintf (dump_file, " blocks:");
basic_block block;
for (unsigned ix = 0; par->blocks.iterate (ix, &block); ix++)
fprintf (dump_file, " %d", block->index);
fprintf (dump_file, "\n");
if (par->inner)
omp_sese_dump_pars (par->inner, depth + 1);
if (par->next)
omp_sese_dump_pars (par->next, depth);
}
/* If BLOCK contains a fork/join marker, process it to create or
terminate a loop structure. Add this block to the current loop,
and then walk successor blocks. */
/* Adapted from 'gcc/config/nvptx/nvptx.cc:nvptx_find_par'. */
static parallel_g *
omp_sese_find_par (bb_stmt_map_t *map, parallel_g *par, basic_block block)
{
if (block->flags & BB_VISITED)
return par;
block->flags |= BB_VISITED;
if (gimple **stmtp = map->get (block))
{
gimple *stmt = *stmtp;
if (gimple_code (stmt) == GIMPLE_COND
|| gimple_code (stmt) == GIMPLE_SWITCH
|| gimple_code (stmt) == GIMPLE_RETURN
|| (gimple_code (stmt) == GIMPLE_CALL
&& !gimple_call_internal_p (stmt))
|| is_gimple_assign (stmt))
{
/* A single block that is forced to be at the maximum partition
level. Make a singleton par for it. */
par = new parallel_g (par, GOMP_DIM_MASK (GOMP_DIM_GANG)
| GOMP_DIM_MASK (GOMP_DIM_WORKER)
| GOMP_DIM_MASK (GOMP_DIM_VECTOR));
par->forked_block = block;
par->forked_stmt = stmt;
par->blocks.safe_push (block);
par = par->parent;
goto walk_successors;
}
else if (gimple_nop_p (stmt))
{
basic_block pred = single_pred (block);
gcc_assert (pred);
gimple_stmt_iterator gsi = gsi_last_bb (pred);
gimple *final_stmt = gsi_stmt (gsi);
if (gimple_call_internal_p (final_stmt, IFN_UNIQUE))
{
gcall *call = as_a <gcall *> (final_stmt);
enum ifn_unique_kind k = ((enum ifn_unique_kind)
TREE_INT_CST_LOW (gimple_call_arg (call, 0)));
if (k == IFN_UNIQUE_OACC_FORK)
{
HOST_WIDE_INT dim
= TREE_INT_CST_LOW (gimple_call_arg (call, 2));
unsigned mask = (dim >= 0) ? GOMP_DIM_MASK (dim) : 0;
par = new parallel_g (par, mask);
par->forked_block = block;
par->forked_stmt = final_stmt;
par->fork_stmt = stmt;
}
else
gcc_unreachable ();
}
else
gcc_unreachable ();
}
else if (gimple_call_internal_p (stmt, IFN_UNIQUE))
{
gcall *call = as_a <gcall *> (stmt);
enum ifn_unique_kind k = ((enum ifn_unique_kind)
TREE_INT_CST_LOW (gimple_call_arg (call, 0)));
if (k == IFN_UNIQUE_OACC_JOIN)
{
HOST_WIDE_INT dim = TREE_INT_CST_LOW (gimple_call_arg (stmt, 2));
unsigned mask = (dim >= 0) ? GOMP_DIM_MASK (dim) : 0;
gcc_assert (par->mask == mask);
par->join_block = block;
par->join_stmt = stmt;
par = par->parent;
}
else
gcc_unreachable ();
}
else
gcc_unreachable ();
}
if (par)
/* Add this block onto the current loop's list of blocks. */
par->blocks.safe_push (block);
else
/* This must be the entry block. Create a NULL parallel. */
par = new parallel_g (0, 0);
walk_successors:
/* Walk successor blocks. */
edge e;
edge_iterator ei;
FOR_EACH_EDGE (e, ei, block->succs)
omp_sese_find_par (map, par, e->dest);
return par;
}
/* DFS walk the CFG looking for fork & join markers. Construct
loop structures as we go. MAP is a mapping of basic blocks
to head & tail markers, discovered when splitting blocks. This
speeds up the discovery. We rely on the BB visited flag having
been cleared when splitting blocks. */
/* Adapted from 'gcc/config/nvptx/nvptx.cc:nvptx_discover_pars'. */
static parallel_g *
omp_sese_discover_pars (bb_stmt_map_t *map)
{
basic_block block;
/* Mark exit blocks as visited. */
block = EXIT_BLOCK_PTR_FOR_FN (cfun);
block->flags |= BB_VISITED;
/* And entry block as not. */
block = ENTRY_BLOCK_PTR_FOR_FN (cfun);
block->flags &= ~BB_VISITED;
parallel_g *par = omp_sese_find_par (map, 0, block);
if (dump_file)
{
fprintf (dump_file, "\nLoops\n");
omp_sese_dump_pars (par, 0);
fprintf (dump_file, "\n");
}
return par;
}
static void
populate_single_mode_bitmaps (parallel_g *par, bitmap worker_single,
bitmap vector_single, unsigned outer_mask,
int depth)
{
unsigned mask = outer_mask | par->mask;
basic_block block;
for (unsigned i = 0; par->blocks.iterate (i, &block); i++)
{
if ((mask & GOMP_DIM_MASK (GOMP_DIM_WORKER)) == 0)
bitmap_set_bit (worker_single, block->index);
if ((mask & GOMP_DIM_MASK (GOMP_DIM_VECTOR)) == 0)
bitmap_set_bit (vector_single, block->index);
}
if (par->inner)
populate_single_mode_bitmaps (par->inner, worker_single, vector_single,
mask, depth + 1);
if (par->next)
populate_single_mode_bitmaps (par->next, worker_single, vector_single,
outer_mask, depth);
}
/* A map from SSA names or var decls to record fields. */
typedef hash_map<tree, tree> field_map_t;
/* For each propagation record type, this is a map from SSA names or var decls
to propagate, to the field in the record type that should be used for
transmission and reception. */
typedef hash_map<tree, field_map_t> record_field_map_t;
static void
install_var_field (tree var, tree record_type, field_map_t *fields)
{
tree name;
char tmp[20];
if (TREE_CODE (var) == SSA_NAME)
{
name = SSA_NAME_IDENTIFIER (var);
if (!name)
{
sprintf (tmp, "_%u", (unsigned) SSA_NAME_VERSION (var));
name = get_identifier (tmp);
}
}
else if (VAR_P (var))
{
name = DECL_NAME (var);
if (!name)
{
sprintf (tmp, "D_%u", (unsigned) DECL_UID (var));
name = get_identifier (tmp);
}
}
else
gcc_unreachable ();
gcc_assert (!fields->get (var));
tree type = TREE_TYPE (var);
if (POINTER_TYPE_P (type)
&& TYPE_RESTRICT (type))
type = build_qualified_type (type, TYPE_QUALS (type) & ~TYPE_QUAL_RESTRICT);
tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL, name, type);
if (VAR_P (var) && type == TREE_TYPE (var))
{
SET_DECL_ALIGN (field, DECL_ALIGN (var));
DECL_USER_ALIGN (field) = DECL_USER_ALIGN (var);
TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (var);
}
else
SET_DECL_ALIGN (field, TYPE_ALIGN (type));
fields->put (var, field);
insert_field_into_struct (record_type, field);
}
/* Sets of SSA_NAMES or VAR_DECLs to propagate. */
typedef hash_set<tree> propagation_set;
static void
find_ssa_names_to_propagate (parallel_g *par, unsigned outer_mask,
bitmap worker_single, bitmap vector_single,
vec<propagation_set *> *prop_set)
{
unsigned mask = outer_mask | par->mask;
if (par->inner)
find_ssa_names_to_propagate (par->inner, mask, worker_single,
vector_single, prop_set);
if (par->next)
find_ssa_names_to_propagate (par->next, outer_mask, worker_single,
vector_single, prop_set);
if (mask & GOMP_DIM_MASK (GOMP_DIM_WORKER))
{
basic_block block;
int ix;
for (ix = 0; par->blocks.iterate (ix, &block); ix++)
{
for (gphi_iterator psi = gsi_start_phis (block);
!gsi_end_p (psi); gsi_next (&psi))
{
gphi *phi = psi.phi ();
use_operand_p use;
ssa_op_iter iter;
FOR_EACH_PHI_ARG (use, phi, iter, SSA_OP_USE)
{
tree var = USE_FROM_PTR (use);
if (TREE_CODE (var) != SSA_NAME)
continue;
gimple *def_stmt = SSA_NAME_DEF_STMT (var);
if (gimple_nop_p (def_stmt))
continue;
basic_block def_bb = gimple_bb (def_stmt);
if (bitmap_bit_p (worker_single, def_bb->index))
{
if (!(*prop_set)[def_bb->index])
(*prop_set)[def_bb->index] = new propagation_set;
propagation_set *ws_prop = (*prop_set)[def_bb->index];
ws_prop->add (var);
}
}
}
for (gimple_stmt_iterator gsi = gsi_start_bb (block);
!gsi_end_p (gsi); gsi_next (&gsi))
{
use_operand_p use;
ssa_op_iter iter;
gimple *stmt = gsi_stmt (gsi);
FOR_EACH_SSA_USE_OPERAND (use, stmt, iter, SSA_OP_USE)
{
tree var = USE_FROM_PTR (use);
gimple *def_stmt = SSA_NAME_DEF_STMT (var);
if (gimple_nop_p (def_stmt))
continue;
basic_block def_bb = gimple_bb (def_stmt);
if (bitmap_bit_p (worker_single, def_bb->index))
{
if (!(*prop_set)[def_bb->index])
(*prop_set)[def_bb->index] = new propagation_set;
propagation_set *ws_prop = (*prop_set)[def_bb->index];
ws_prop->add (var);
}
}
}
}
}
}
/* Callback for walk_gimple_stmt to find RHS VAR_DECLs (uses) in a
statement. */
static tree
find_partitioned_var_uses_1 (tree *node, int *, void *data)
{
walk_stmt_info *wi = (walk_stmt_info *) data;
hash_set<tree> *partitioned_var_uses = (hash_set<tree> *) wi->info;
if (!wi->is_lhs && VAR_P (*node))
partitioned_var_uses->add (*node);
return NULL_TREE;
}
static void
find_partitioned_var_uses (parallel_g *par, unsigned outer_mask,
hash_set<tree> *partitioned_var_uses)
{
unsigned mask = outer_mask | par->mask;
if (par->inner)
find_partitioned_var_uses (par->inner, mask, partitioned_var_uses);
if (par->next)
find_partitioned_var_uses (par->next, outer_mask, partitioned_var_uses);
if (mask & GOMP_DIM_MASK (GOMP_DIM_WORKER))
{
basic_block block;
int ix;
for (ix = 0; par->blocks.iterate (ix, &block); ix++)
for (gimple_stmt_iterator gsi = gsi_start_bb (block);
!gsi_end_p (gsi); gsi_next (&gsi))
{
walk_stmt_info wi;
memset (&wi, 0, sizeof (wi));
wi.info = (void *) partitioned_var_uses;
walk_gimple_stmt (&gsi, NULL, find_partitioned_var_uses_1, &wi);
}
}
}
/* Gang-private variables (typically placed in a GPU's shared memory) do not
need to be processed by the worker-propagation mechanism. Populate the
GANG_PRIVATE_VARS set with any such variables found in the current
function. */
static void
find_gang_private_vars (hash_set<tree> *gang_private_vars)
{
basic_block block;
FOR_EACH_BB_FN (block, cfun)
{
for (gimple_stmt_iterator gsi = gsi_start_bb (block);
!gsi_end_p (gsi);
gsi_next (&gsi))
{
gimple *stmt = gsi_stmt (gsi);
if (gimple_call_internal_p (stmt, IFN_UNIQUE))
{
enum ifn_unique_kind k = ((enum ifn_unique_kind)
TREE_INT_CST_LOW (gimple_call_arg (stmt, 0)));
if (k == IFN_UNIQUE_OACC_PRIVATE)
{
HOST_WIDE_INT level
= TREE_INT_CST_LOW (gimple_call_arg (stmt, 2));
if (level != GOMP_DIM_GANG)
continue;
for (unsigned i = 3; i < gimple_call_num_args (stmt); i++)
{
tree arg = gimple_call_arg (stmt, i);
gcc_assert (TREE_CODE (arg) == ADDR_EXPR);
tree decl = TREE_OPERAND (arg, 0);
gang_private_vars->add (decl);
}
}
}
}
}
}
static void
find_local_vars_to_propagate (parallel_g *par, unsigned outer_mask,
hash_set<tree> *partitioned_var_uses,
hash_set<tree> *gang_private_vars,
bitmap writes_gang_private,
vec<propagation_set *> *prop_set)
{
unsigned mask = outer_mask | par->mask;
if (par->inner)
find_local_vars_to_propagate (par->inner, mask, partitioned_var_uses,
gang_private_vars, writes_gang_private,
prop_set);
if (par->next)
find_local_vars_to_propagate (par->next, outer_mask, partitioned_var_uses,
gang_private_vars, writes_gang_private,
prop_set);
if (!(mask & GOMP_DIM_MASK (GOMP_DIM_WORKER)))
{
basic_block block;
int ix;
for (ix = 0; par->blocks.iterate (ix, &block); ix++)
{
for (gimple_stmt_iterator gsi = gsi_start_bb (block);
!gsi_end_p (gsi); gsi_next (&gsi))
{
gimple *stmt = gsi_stmt (gsi);
tree var;
unsigned i;
FOR_EACH_LOCAL_DECL (cfun, i, var)
{
if (!VAR_P (var)
|| is_global_var (var)
|| AGGREGATE_TYPE_P (TREE_TYPE (var))
|| !partitioned_var_uses->contains (var))
continue;
if (stmt_may_clobber_ref_p (stmt, var))
{
if (dump_file)
{
fprintf (dump_file, "bb %u: local variable may be "
"clobbered in %s mode: ", block->index,
mask_name (mask));
print_generic_expr (dump_file, var, TDF_SLIM);
fprintf (dump_file, "\n");
}
if (gang_private_vars->contains (var))
{
/* If we write a gang-private variable, we want a
barrier at the end of the block. */
bitmap_set_bit (writes_gang_private, block->index);
continue;
}
if (!(*prop_set)[block->index])
(*prop_set)[block->index] = new propagation_set;
propagation_set *ws_prop
= (*prop_set)[block->index];
ws_prop->add (var);
}
}
}
}
}
}
/* Transform basic blocks FROM, TO (which may be the same block) into:
if (GOACC_single_start ())
BLOCK;
GOACC_barrier ();
\ | /
+----+
| | (new) predicate block
+----+--
\ | / \ | / |t \
+----+ +----+ +----+ |
| | | | ===> | | | f (old) from block
+----+ +----+ +----+ |
| t/ \f | /
+----+/
(split (split before | | skip block
at end) condition) +----+
t/ \f
*/
static void
worker_single_simple (basic_block from, basic_block to,
hash_set<tree> *def_escapes_block)
{
gimple *call, *cond;
tree lhs, decl;
basic_block skip_block;
gimple_stmt_iterator gsi = gsi_last_bb (to);
if (EDGE_COUNT (to->succs) > 1)
{
gcc_assert (gimple_code (gsi_stmt (gsi)) == GIMPLE_COND);
gsi_prev (&gsi);
}
edge e = split_block (to, gsi_stmt (gsi));
skip_block = e->dest;
gimple_stmt_iterator start = gsi_after_labels (from);
decl = builtin_decl_explicit (BUILT_IN_GOACC_SINGLE_START);
lhs = create_tmp_var (TREE_TYPE (TREE_TYPE (decl)));
call = gimple_build_call (decl, 0);
gimple_call_set_lhs (call, lhs);
gsi_insert_before (&start, call, GSI_NEW_STMT);
update_stmt (call);
cond = gimple_build_cond (EQ_EXPR, lhs,
fold_convert_loc (UNKNOWN_LOCATION,
TREE_TYPE (lhs),
boolean_true_node),
NULL_TREE, NULL_TREE);
gsi_insert_after (&start, cond, GSI_NEW_STMT);
update_stmt (cond);
edge et = split_block (from, cond);
et->flags &= ~EDGE_FALLTHRU;
et->flags |= EDGE_TRUE_VALUE;
/* Make the active worker the more probable path so we prefer fallthrough
(letting the idle workers jump around more). */
et->probability = profile_probability::likely ();
edge ef = make_edge (from, skip_block, EDGE_FALSE_VALUE);
ef->probability = et->probability.invert ();
basic_block neutered = split_edge (ef);
gimple_stmt_iterator neut_gsi = gsi_last_bb (neutered);
for (gsi = gsi_start_bb (et->dest); !gsi_end_p (gsi); gsi_next (&gsi))
{
gimple *stmt = gsi_stmt (gsi);
ssa_op_iter iter;
tree var;
FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
{
if (def_escapes_block->contains (var))
{
gphi *join_phi = create_phi_node (NULL_TREE, skip_block);
create_new_def_for (var, join_phi,
gimple_phi_result_ptr (join_phi));
add_phi_arg (join_phi, var, e, UNKNOWN_LOCATION);
tree neutered_def = copy_ssa_name (var, NULL);
/* We really want "don't care" or some value representing
undefined here, but optimizers will probably get rid of the
zero-assignments anyway. */
gassign *zero = gimple_build_assign (neutered_def,
build_zero_cst (TREE_TYPE (neutered_def)));
gsi_insert_after (&neut_gsi, zero, GSI_CONTINUE_LINKING);
update_stmt (zero);
add_phi_arg (join_phi, neutered_def, single_succ_edge (neutered),
UNKNOWN_LOCATION);
update_stmt (join_phi);
}
}
}
}
static tree
build_receiver_ref (tree var, tree receiver_decl, field_map_t *fields)
{
tree x = build_simple_mem_ref (receiver_decl);
tree field = *fields->get (var);
TREE_THIS_NOTRAP (x) = 1;
x = omp_build_component_ref (x, field);
return x;
}
static tree
build_sender_ref (tree var, tree sender_decl, field_map_t *fields)
{
if (POINTER_TYPE_P (TREE_TYPE (sender_decl)))
sender_decl = build_simple_mem_ref (sender_decl);
tree field = *fields->get (var);
return omp_build_component_ref (sender_decl, field);
}
static int
sort_by_ssa_version_or_uid (const void *p1, const void *p2)
{
const tree t1 = *(const tree *)p1;
const tree t2 = *(const tree *)p2;
if (TREE_CODE (t1) == SSA_NAME && TREE_CODE (t2) == SSA_NAME)
return SSA_NAME_VERSION (t1) - SSA_NAME_VERSION (t2);
else if (TREE_CODE (t1) == SSA_NAME && TREE_CODE (t2) != SSA_NAME)
return -1;
else if (TREE_CODE (t1) != SSA_NAME && TREE_CODE (t2) == SSA_NAME)
return 1;
else
return DECL_UID (t1) - DECL_UID (t2);
}
static int
sort_by_size_then_ssa_version_or_uid (const void *p1, const void *p2)
{
const tree t1 = *(const tree *)p1;
const tree t2 = *(const tree *)p2;
unsigned HOST_WIDE_INT s1 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (t1)));
unsigned HOST_WIDE_INT s2 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (t2)));
if (s1 != s2)
return s2 - s1;
else
return sort_by_ssa_version_or_uid (p1, p2);
}
static void
worker_single_copy (basic_block from, basic_block to,
hash_set<tree> *def_escapes_block,
hash_set<tree> *worker_partitioned_uses,
tree record_type, record_field_map_t *record_field_map,
unsigned HOST_WIDE_INT placement,
bool isolate_broadcasts, bool has_gang_private_write)
{
/* If we only have virtual defs, we'll have no record type, but we still want
to emit single_copy_start and (particularly) single_copy_end to act as
a vdef source on the neutered edge representing memory writes on the
non-neutered edge. */
if (!record_type)
record_type = char_type_node;
tree sender_decl
= targetm.goacc.create_worker_broadcast_record (record_type, true,
".oacc_worker_o",
placement);
tree receiver_decl
= targetm.goacc.create_worker_broadcast_record (record_type, false,
".oacc_worker_i",
placement);
gimple_stmt_iterator gsi = gsi_last_bb (to);
if (EDGE_COUNT (to->succs) > 1)
gsi_prev (&gsi);
edge e = split_block (to, gsi_stmt (gsi));
basic_block barrier_block = e->dest;
gimple_stmt_iterator start = gsi_after_labels (from);
tree decl = builtin_decl_explicit (BUILT_IN_GOACC_SINGLE_COPY_START);
tree lhs = create_tmp_var (TREE_TYPE (TREE_TYPE (decl)));
gimple *call
= gimple_build_call (decl, 1,
POINTER_TYPE_P (TREE_TYPE (sender_decl))
? sender_decl : build_fold_addr_expr (sender_decl));
gimple_call_set_lhs (call, lhs);
gsi_insert_before (&start, call, GSI_NEW_STMT);
update_stmt (call);
/* The shared-memory range for this block overflowed. Add a barrier before
the GOACC_single_copy_start call. */
if (isolate_broadcasts)
{
decl = builtin_decl_explicit (BUILT_IN_GOACC_BARRIER);
gimple *acc_bar = gimple_build_call (decl, 0);
gsi_insert_before (&start, acc_bar, GSI_SAME_STMT);
}
tree conv_tmp = make_ssa_name (TREE_TYPE (receiver_decl));
gimple *conv = gimple_build_assign (conv_tmp,
fold_convert (TREE_TYPE (receiver_decl),
lhs));
update_stmt (conv);
gsi_insert_after (&start, conv, GSI_NEW_STMT);
gimple *asgn = gimple_build_assign (receiver_decl, conv_tmp);
gsi_insert_after (&start, asgn, GSI_NEW_STMT);
update_stmt (asgn);
tree zero_ptr = build_int_cst (TREE_TYPE (receiver_decl), 0);
tree recv_tmp = make_ssa_name (TREE_TYPE (receiver_decl));
asgn = gimple_build_assign (recv_tmp, receiver_decl);
gsi_insert_after (&start, asgn, GSI_NEW_STMT);
update_stmt (asgn);
gimple *cond = gimple_build_cond (EQ_EXPR, recv_tmp, zero_ptr, NULL_TREE,
NULL_TREE);
update_stmt (cond);
gsi_insert_after (&start, cond, GSI_NEW_STMT);
edge et = split_block (from, cond);
et->flags &= ~EDGE_FALLTHRU;
et->flags |= EDGE_TRUE_VALUE;
/* Make the active worker the more probable path so we prefer fallthrough
(letting the idle workers jump around more). */
et->probability = profile_probability::likely ();
basic_block body = et->dest;
edge ef = make_edge (from, barrier_block, EDGE_FALSE_VALUE);
ef->probability = et->probability.invert ();
gimple_stmt_iterator bar_gsi = gsi_start_bb (barrier_block);
cond = gimple_build_cond (NE_EXPR, recv_tmp, zero_ptr, NULL_TREE, NULL_TREE);
if (record_type != char_type_node || has_gang_private_write)
{
decl = builtin_decl_explicit (BUILT_IN_GOACC_BARRIER);
gimple *acc_bar = gimple_build_call (decl, 0);
gsi_insert_before (&bar_gsi, acc_bar, GSI_NEW_STMT);
gsi_insert_after (&bar_gsi, cond, GSI_NEW_STMT);
}
else
gsi_insert_before (&bar_gsi, cond, GSI_NEW_STMT);
edge et2 = split_block (barrier_block, cond);
et2->flags &= ~EDGE_FALLTHRU;
et2->flags |= EDGE_TRUE_VALUE;
et2->probability = profile_probability::unlikely ();
basic_block exit_block = et2->dest;
basic_block copyout_block = split_edge (et2);
edge ef2 = make_edge (barrier_block, exit_block, EDGE_FALSE_VALUE);
ef2->probability = et2->probability.invert ();
gimple_stmt_iterator copyout_gsi = gsi_start_bb (copyout_block);
edge copyout_to_exit = single_succ_edge (copyout_block);
gimple_seq sender_seq = NULL;
/* Make sure we iterate over definitions in a stable order. */
auto_vec<tree> escape_vec (def_escapes_block->elements ());
for (hash_set<tree>::iterator it = def_escapes_block->begin ();
it != def_escapes_block->end (); ++it)
escape_vec.quick_push (*it);
escape_vec.qsort (sort_by_ssa_version_or_uid);
for (unsigned i = 0; i < escape_vec.length (); i++)
{
tree var = escape_vec[i];
if (TREE_CODE (var) == SSA_NAME && SSA_NAME_IS_VIRTUAL_OPERAND (var))
continue;
tree barrier_def = 0;
if (TREE_CODE (var) == SSA_NAME)
{
gimple *def_stmt = SSA_NAME_DEF_STMT (var);
if (gimple_nop_p (def_stmt))
continue;
/* The barrier phi takes one result from the actual work of the
block we're neutering, and the other result is constant zero of
the same type. */
gphi *barrier_phi = create_phi_node (NULL_TREE, barrier_block);
barrier_def = create_new_def_for (var, barrier_phi,
gimple_phi_result_ptr (barrier_phi));
add_phi_arg (barrier_phi, var, e, UNKNOWN_LOCATION);
add_phi_arg (barrier_phi, build_zero_cst (TREE_TYPE (var)), ef,
UNKNOWN_LOCATION);
update_stmt (barrier_phi);
}
else
gcc_assert (VAR_P (var));
/* If we had no record type, we will have no fields map. */
field_map_t *fields = record_field_map->get (record_type);
if (worker_partitioned_uses->contains (var)
&& fields
&& fields->get (var))
{
tree neutered_def = make_ssa_name (TREE_TYPE (var));
/* Receive definition from shared memory block. */
tree receiver_ref = build_receiver_ref (var, receiver_decl, fields);
gassign *recv = gimple_build_assign (neutered_def,
receiver_ref);
gsi_insert_after (©out_gsi, recv, GSI_CONTINUE_LINKING);
update_stmt (recv);
if (VAR_P (var))
{
/* If it's a VAR_DECL, we only copied to an SSA temporary. Copy
to the final location now. */
gassign *asgn = gimple_build_assign (var, neutered_def);
gsi_insert_after (©out_gsi, asgn, GSI_CONTINUE_LINKING);
update_stmt (asgn);
}
else
{
/* If it's an SSA name, create a new phi at the join node to
represent either the output from the active worker (the
barrier) or the inactive workers (the copyout block). */
gphi *join_phi = create_phi_node (NULL_TREE, exit_block);
create_new_def_for (barrier_def, join_phi,
gimple_phi_result_ptr (join_phi));
add_phi_arg (join_phi, barrier_def, ef2, UNKNOWN_LOCATION);
add_phi_arg (join_phi, neutered_def, copyout_to_exit,
UNKNOWN_LOCATION);
update_stmt (join_phi);
}
/* Send definition to shared memory block. */
tree sender_ref = build_sender_ref (var, sender_decl, fields);
if (TREE_CODE (var) == SSA_NAME)
{
gassign *send = gimple_build_assign (sender_ref, var);
gimple_seq_add_stmt (&sender_seq, send);
update_stmt (send);
}
else if (VAR_P (var))
{
tree tmp = make_ssa_name (TREE_TYPE (var));
gassign *send = gimple_build_assign (tmp, var);
gimple_seq_add_stmt (&sender_seq, send);
update_stmt (send);
send = gimple_build_assign (sender_ref, tmp);
gimple_seq_add_stmt (&sender_seq, send);
update_stmt (send);
}
else
gcc_unreachable ();
}
}
/* The shared-memory range for this block overflowed. Add a barrier at the
end. */
if (isolate_broadcasts)
{
gsi = gsi_start_bb (exit_block);
decl = builtin_decl_explicit (BUILT_IN_GOACC_BARRIER);
gimple *acc_bar = gimple_build_call (decl, 0);
gsi_insert_before (&gsi, acc_bar, GSI_SAME_STMT);
}
/* It's possible for the ET->DEST block (the work done by the active thread)
to finish with a control-flow insn, e.g. a UNIQUE function call. Split
the block and add SENDER_SEQ in the latter part to avoid having control
flow in the middle of a BB. */
decl = builtin_decl_explicit (BUILT_IN_GOACC_SINGLE_COPY_END);
call = gimple_build_call (decl, 1,
POINTER_TYPE_P (TREE_TYPE (sender_decl))
? sender_decl
: build_fold_addr_expr (sender_decl));
gimple_seq_add_stmt (&sender_seq, call);
gsi = gsi_last_bb (body);
gimple *last = gsi_stmt (gsi);
basic_block sender_block = split_block (body, last)->dest;
gsi = gsi_last_bb (sender_block);
gsi_insert_seq_after (&gsi, sender_seq, GSI_CONTINUE_LINKING);
}
typedef hash_map<basic_block, std::pair<unsigned HOST_WIDE_INT, bool> >
blk_offset_map_t;
static void
neuter_worker_single (parallel_g *par, unsigned outer_mask,
bitmap worker_single, bitmap vector_single,
vec<propagation_set *> *prop_set,
hash_set<tree> *partitioned_var_uses,
record_field_map_t *record_field_map,
blk_offset_map_t *blk_offset_map,
bitmap writes_gang_private)
{
unsigned mask = outer_mask | par->mask;
if ((mask & GOMP_DIM_MASK (GOMP_DIM_WORKER)) == 0)
{
basic_block block;
for (unsigned i = 0; par->blocks.iterate (i, &block); i++)
{
bool has_defs = false;
hash_set<tree> def_escapes_block;
hash_set<tree> worker_partitioned_uses;
unsigned j;
tree var;
FOR_EACH_SSA_NAME (j, var, cfun)
{
if (SSA_NAME_IS_VIRTUAL_OPERAND (var))
{
has_defs = true;
continue;
}
gimple *def_stmt = SSA_NAME_DEF_STMT (var);
if (gimple_nop_p (def_stmt))
continue;
if (gimple_bb (def_stmt)->index != block->index)
continue;
gimple *use_stmt;
imm_use_iterator use_iter;
bool uses_outside_block = false;
bool worker_partitioned_use = false;
FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, var)
{
int blocknum = gimple_bb (use_stmt)->index;
/* Don't propagate SSA names that are only used in the
current block, unless the usage is in a phi node: that
means the name left the block, then came back in at the
top. */
if (blocknum != block->index
|| gimple_code (use_stmt) == GIMPLE_PHI)
uses_outside_block = true;
if (!bitmap_bit_p (worker_single, blocknum))
worker_partitioned_use = true;
}
if (uses_outside_block)
def_escapes_block.add (var);
if (worker_partitioned_use)
{
worker_partitioned_uses.add (var);
has_defs = true;
}
}
propagation_set *ws_prop = (*prop_set)[block->index];
if (ws_prop)
{
for (propagation_set::iterator it = ws_prop->begin ();
it != ws_prop->end ();
++it)
{
tree var = *it;
if (TREE_CODE (var) == VAR_DECL)
{
def_escapes_block.add (var);
if (partitioned_var_uses->contains (var))
{
worker_partitioned_uses.add (var);
has_defs = true;
}
}
}
delete ws_prop;
(*prop_set)[block->index] = 0;
}
bool only_marker_fns = true;
bool join_block = false;
for (gimple_stmt_iterator gsi = gsi_start_bb (block);
!gsi_end_p (gsi);
gsi_next (&gsi))
{
gimple *stmt = gsi_stmt (gsi);
if (gimple_code (stmt) == GIMPLE_CALL
&& gimple_call_internal_p (stmt, IFN_UNIQUE))
{
enum ifn_unique_kind k = ((enum ifn_unique_kind)
TREE_INT_CST_LOW (gimple_call_arg (stmt, 0)));
if (k != IFN_UNIQUE_OACC_PRIVATE
&& k != IFN_UNIQUE_OACC_JOIN
&& k != IFN_UNIQUE_OACC_FORK
&& k != IFN_UNIQUE_OACC_HEAD_MARK
&& k != IFN_UNIQUE_OACC_TAIL_MARK)
only_marker_fns = false;
else if (k == IFN_UNIQUE_OACC_JOIN)
/* The JOIN marker is special in that it *cannot* be
predicated for worker zero, because it may be lowered
to a barrier instruction and all workers must typically
execute that barrier. We shouldn't be doing any
broadcasts from the join block anyway. */
join_block = true;
}
else if (gimple_code (stmt) == GIMPLE_CALL
&& gimple_call_internal_p (stmt, IFN_GOACC_LOOP))
/* Empty. */;
else if (gimple_nop_p (stmt))
/* Empty. */;
else
only_marker_fns = false;
}
/* We can skip predicating this block for worker zero if the only
thing it contains is marker functions that will be removed in the
oaccdevlow pass anyway.
Don't do this if the block has (any) phi nodes, because those
might define SSA names that need broadcasting.
TODO: We might be able to skip transforming blocks that only
contain some other trivial statements too. */
if (only_marker_fns && !phi_nodes (block))
continue;
gcc_assert (!join_block);
if (has_defs)
{
tree record_type = (tree) block->aux;
std::pair<unsigned HOST_WIDE_INT, bool> *off_rngalloc
= blk_offset_map->get (block);
gcc_assert (!record_type || off_rngalloc);
unsigned HOST_WIDE_INT offset
= off_rngalloc ? off_rngalloc->first : 0;
bool range_allocated
= off_rngalloc ? off_rngalloc->second : true;
bool has_gang_private_write
= bitmap_bit_p (writes_gang_private, block->index);
worker_single_copy (block, block, &def_escapes_block,
&worker_partitioned_uses, record_type,
record_field_map,
offset, !range_allocated,
has_gang_private_write);
}
else
worker_single_simple (block, block, &def_escapes_block);
}
}
if ((outer_mask & GOMP_DIM_MASK (GOMP_DIM_WORKER)) == 0)
{
basic_block block;
for (unsigned i = 0; par->blocks.iterate (i, &block); i++)
for (gimple_stmt_iterator gsi = gsi_start_bb (block);
!gsi_end_p (gsi);
gsi_next (&gsi))
{
gimple *stmt = gsi_stmt (gsi);
if (gimple_code (stmt) == GIMPLE_CALL
&& !gimple_call_internal_p (stmt)
&& !omp_sese_active_worker_call (as_a <gcall *> (stmt)))
{
/* If we have an OpenACC routine call in worker-single mode,
place barriers before and afterwards to prevent
clobbering re-used shared memory regions (as are used
for AMDGCN at present, for example). */
tree decl = builtin_decl_explicit (BUILT_IN_GOACC_BARRIER);
gsi_insert_before (&gsi, gimple_build_call (decl, 0),
GSI_SAME_STMT);
gsi_insert_after (&gsi, gimple_build_call (decl, 0),
GSI_NEW_STMT);
}
}
}
if (par->inner)
neuter_worker_single (par->inner, mask, worker_single, vector_single,
prop_set, partitioned_var_uses, record_field_map,
blk_offset_map, writes_gang_private);
if (par->next)
neuter_worker_single (par->next, outer_mask, worker_single, vector_single,
prop_set, partitioned_var_uses, record_field_map,
blk_offset_map, writes_gang_private);
}
static void
dfs_broadcast_reachable_1 (basic_block bb, sbitmap reachable)
{
if (bb->flags & BB_VISITED)
return;
bb->flags |= BB_VISITED;
if (bb->succs)
{
edge e;
edge_iterator ei;
FOR_EACH_EDGE (e, ei, bb->succs)
{
basic_block dest = e->dest;
if (dest->aux)
bitmap_set_bit (reachable, dest->index);
else
dfs_broadcast_reachable_1 (dest, reachable);
}
}
}
typedef std::pair<int, tree> idx_decl_pair_t;
typedef auto_vec<splay_tree> used_range_vec_t;
static int
sort_size_descending (const void *a, const void *b)
{
const idx_decl_pair_t *pa = (const idx_decl_pair_t *) a;
const idx_decl_pair_t *pb = (const idx_decl_pair_t *) b;
unsigned HOST_WIDE_INT asize = tree_to_uhwi (TYPE_SIZE_UNIT (pa->second));
unsigned HOST_WIDE_INT bsize = tree_to_uhwi (TYPE_SIZE_UNIT (pb->second));
return bsize - asize;
}
class addr_range
{
public:
addr_range (unsigned HOST_WIDE_INT addr_lo, unsigned HOST_WIDE_INT addr_hi)
: lo (addr_lo), hi (addr_hi)
{ }
addr_range (const addr_range &ar) : lo (ar.lo), hi (ar.hi)
{ }
addr_range () : lo (0), hi (0)
{ }
bool invalid () { return lo == 0 && hi == 0; }
unsigned HOST_WIDE_INT lo;
unsigned HOST_WIDE_INT hi;
};
static int
splay_tree_compare_addr_range (splay_tree_key a, splay_tree_key b)
{
addr_range *ar = (addr_range *) a;
addr_range *br = (addr_range *) b;
if (ar->lo == br->lo && ar->hi == br->hi)
return 0;
if (ar->hi <= br->lo)
return -1;
else if (ar->lo >= br->hi)
return 1;
return 0;
}
static void
splay_tree_free_key (splay_tree_key k)
{
addr_range *ar = (addr_range *) k;
delete ar;
}
static addr_range
first_fit_range (splay_tree s, unsigned HOST_WIDE_INT size,
unsigned HOST_WIDE_INT align, addr_range *bounds)
{
splay_tree_node min = splay_tree_min (s);
if (min)
{
splay_tree_node next;
while ((next = splay_tree_successor (s, min->key)))
{
unsigned HOST_WIDE_INT lo = ((addr_range *) min->key)->hi;
unsigned HOST_WIDE_INT hi = ((addr_range *) next->key)->lo;
unsigned HOST_WIDE_INT base = (lo + align - 1) & ~(align - 1);
if (base + size <= hi)
return addr_range (base, base + size);
min = next;
}
unsigned HOST_WIDE_INT base = ((addr_range *)min->key)->hi;
base = (base + align - 1) & ~(align - 1);
if (base + size <= bounds->hi)
return addr_range (base, base + size);
else
return addr_range ();
}
else
{
unsigned HOST_WIDE_INT lo = bounds->lo;
lo = (lo + align - 1) & ~(align - 1);
if (lo + size <= bounds->hi)
return addr_range (lo, lo + size);
else
return addr_range ();
}
}
static int
merge_ranges_1 (splay_tree_node n, void *ptr)
{
splay_tree accum = (splay_tree) ptr;
addr_range ar = *(addr_range *) n->key;
splay_tree_node old = splay_tree_lookup (accum, n->key);
/* We might have an overlap. Create a new range covering the
overlapping parts. */
if (old)
{
addr_range *old_ar = (addr_range *) old->key;
ar.lo = MIN (old_ar->lo, ar.lo);
ar.hi = MAX (old_ar->hi, ar.hi);
splay_tree_remove (accum, old->key);
}
addr_range *new_ar = new addr_range (ar);
splay_tree_insert (accum, (splay_tree_key) new_ar, n->value);
return 0;
}
static void
merge_ranges (splay_tree accum, splay_tree sp)
{
splay_tree_foreach (sp, merge_ranges_1, (void *) accum);
}
static void
oacc_do_neutering (unsigned HOST_WIDE_INT bounds_lo,
unsigned HOST_WIDE_INT bounds_hi)
{
bb_stmt_map_t bb_stmt_map;
auto_bitmap worker_single, vector_single;
omp_sese_split_blocks (&bb_stmt_map);
if (dump_file)
{
fprintf (dump_file, "\n\nAfter splitting:\n\n");
dump_function_to_file (current_function_decl, dump_file, dump_flags);
}
unsigned mask = 0;
/* If this is a routine, calculate MASK as if the outer levels are already
partitioned. */
{
tree attr = oacc_get_fn_attrib (current_function_decl);
tree dims = TREE_VALUE (attr);
unsigned ix;
for (ix = 0; ix != GOMP_DIM_MAX; ix++, dims = TREE_CHAIN (dims))
{
tree allowed = TREE_PURPOSE (dims);
if (allowed && integer_zerop (allowed))
mask |= GOMP_DIM_MASK (ix);
}
}
parallel_g *par = omp_sese_discover_pars (&bb_stmt_map);
populate_single_mode_bitmaps (par, worker_single, vector_single, mask, 0);
basic_block bb;
FOR_ALL_BB_FN (bb, cfun)
bb->aux = NULL;
vec<propagation_set *> prop_set (vNULL);
prop_set.safe_grow_cleared (last_basic_block_for_fn (cfun), true);
find_ssa_names_to_propagate (par, mask, worker_single, vector_single,
&prop_set);
hash_set<tree> partitioned_var_uses;
hash_set<tree> gang_private_vars;
auto_bitmap writes_gang_private;
find_gang_private_vars (&gang_private_vars);
find_partitioned_var_uses (par, mask, &partitioned_var_uses);
find_local_vars_to_propagate (par, mask, &partitioned_var_uses,
&gang_private_vars, writes_gang_private,
&prop_set);
record_field_map_t record_field_map;
FOR_ALL_BB_FN (bb, cfun)
{
propagation_set *ws_prop = prop_set[bb->index];
if (ws_prop)
{
tree record_type = lang_hooks.types.make_type (RECORD_TYPE);
tree name = create_tmp_var_name (".oacc_ws_data_s");
name = build_decl (UNKNOWN_LOCATION, TYPE_DECL, name, record_type);
DECL_ARTIFICIAL (name) = 1;
DECL_NAMELESS (name) = 1;
TYPE_NAME (record_type) = name;
TYPE_ARTIFICIAL (record_type) = 1;
auto_vec<tree> field_vec (ws_prop->elements ());
for (hash_set<tree>::iterator it = ws_prop->begin ();
it != ws_prop->end (); ++it)
field_vec.quick_push (*it);
field_vec.qsort (sort_by_size_then_ssa_version_or_uid);
bool existed;
field_map_t *fields
= &record_field_map.get_or_insert (record_type, &existed);
gcc_checking_assert (!existed);
/* Insert var fields in reverse order, so the last inserted element
is the first in the structure. */
for (int i = field_vec.length () - 1; i >= 0; i--)
install_var_field (field_vec[i], record_type, fields);
layout_type (record_type);
bb->aux = (tree) record_type;
}
}
sbitmap *reachable
= sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
last_basic_block_for_fn (cfun));
bitmap_vector_clear (reachable, last_basic_block_for_fn (cfun));
auto_vec<std::pair<int, tree> > priority;
FOR_ALL_BB_FN (bb, cfun)
{
if (bb->aux)
{
tree record_type = (tree) bb->aux;
basic_block bb2;
FOR_ALL_BB_FN (bb2, cfun)
bb2->flags &= ~BB_VISITED;
priority.safe_push (std::make_pair (bb->index, record_type));
dfs_broadcast_reachable_1 (bb, reachable[bb->index]);
}
}
sbitmap *inverted
= sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
last_basic_block_for_fn (cfun));
bitmap_vector_clear (inverted, last_basic_block_for_fn (cfun));
for (int i = 0; i < last_basic_block_for_fn (cfun); i++)
{
sbitmap_iterator bi;
unsigned int j;
EXECUTE_IF_SET_IN_BITMAP (reachable[i], 0, j, bi)
bitmap_set_bit (inverted[j], i);
}
for (int i = 0; i < last_basic_block_for_fn (cfun); i++)
bitmap_ior (reachable[i], reachable[i], inverted[i]);
sbitmap_vector_free (inverted);
used_range_vec_t used_ranges;
used_ranges.safe_grow_cleared (last_basic_block_for_fn (cfun));
blk_offset_map_t blk_offset_map;
addr_range worker_shm_bounds (bounds_lo, bounds_hi);
priority.qsort (sort_size_descending);
for (unsigned int i = 0; i < priority.length (); i++)
{
idx_decl_pair_t p = priority[i];
int blkno = p.first;
tree record_type = p.second;
HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (record_type));
HOST_WIDE_INT align = TYPE_ALIGN_UNIT (record_type);
splay_tree conflicts = splay_tree_new (splay_tree_compare_addr_range,
splay_tree_free_key, NULL);
if (!used_ranges[blkno])
used_ranges[blkno] = splay_tree_new (splay_tree_compare_addr_range,
splay_tree_free_key, NULL);
else
merge_ranges (conflicts, used_ranges[blkno]);
sbitmap_iterator bi;
unsigned int j;
EXECUTE_IF_SET_IN_BITMAP (reachable[blkno], 0, j, bi)
if (used_ranges[j])
merge_ranges (conflicts, used_ranges[j]);
addr_range ar
= first_fit_range (conflicts, size, align, &worker_shm_bounds);
splay_tree_delete (conflicts);
if (ar.invalid ())
{
unsigned HOST_WIDE_INT base
= (bounds_lo + align - 1) & ~(align - 1);
if (base + size > bounds_hi)
error_at (UNKNOWN_LOCATION, "shared-memory region overflow");
std::pair<unsigned HOST_WIDE_INT, bool> base_inrng
= std::make_pair (base, false);
blk_offset_map.put (BASIC_BLOCK_FOR_FN (cfun, blkno), base_inrng);
}
else
{
splay_tree_node old = splay_tree_lookup (used_ranges[blkno],
(splay_tree_key) &ar);
if (old)
{
fprintf (stderr, "trying to map [%d..%d] but [%d..%d] is "
"already mapped in block %d\n", (int) ar.lo,
(int) ar.hi, (int) ((addr_range *) old->key)->lo,
(int) ((addr_range *) old->key)->hi, blkno);
abort ();
}
addr_range *arp = new addr_range (ar);
splay_tree_insert (used_ranges[blkno], (splay_tree_key) arp,
(splay_tree_value) blkno);
std::pair<unsigned HOST_WIDE_INT, bool> base_inrng
= std::make_pair (ar.lo, true);
blk_offset_map.put (BASIC_BLOCK_FOR_FN (cfun, blkno), base_inrng);
}
}
sbitmap_vector_free (reachable);
neuter_worker_single (par, mask, worker_single, vector_single, &prop_set,
&partitioned_var_uses, &record_field_map,
&blk_offset_map, writes_gang_private);
record_field_map.empty ();
/* These are supposed to have been 'delete'd by 'neuter_worker_single'. */
for (auto it : prop_set)
gcc_checking_assert (!it);
prop_set.release ();
delete par;
/* This doesn't seem to make a difference. */
loops_state_clear (LOOP_CLOSED_SSA);
/* Neutering worker-single neutered blocks will invalidate dominance info.
It may be possible to incrementally update just the affected blocks, but
obliterate everything for now. */
free_dominance_info (CDI_DOMINATORS);
free_dominance_info (CDI_POST_DOMINATORS);
if (dump_file)
{
fprintf (dump_file, "\n\nAfter neutering:\n\n");
dump_function_to_file (current_function_decl, dump_file, dump_flags);
}
}
static int
execute_omp_oacc_neuter_broadcast ()
{
unsigned HOST_WIDE_INT reduction_size[GOMP_DIM_MAX];
unsigned HOST_WIDE_INT private_size[GOMP_DIM_MAX];
for (unsigned i = 0; i < GOMP_DIM_MAX; i++)
{
reduction_size[i] = 0;
private_size[i] = 0;
}
/* Calculate shared memory size required for reduction variables and
gang-private memory for this offloaded function. */
basic_block bb;
FOR_ALL_BB_FN (bb, cfun)
{
for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
!gsi_end_p (gsi);
gsi_next (&gsi))
{
gimple *stmt = gsi_stmt (gsi);
if (!is_gimple_call (stmt))
continue;
gcall *call = as_a <gcall *> (stmt);
if (!gimple_call_internal_p (call))
continue;
enum internal_fn ifn_code = gimple_call_internal_fn (call);
switch (ifn_code)
{
default: break;
case IFN_GOACC_REDUCTION:
if (integer_minus_onep (gimple_call_arg (call, 3)))
continue;
else
{
unsigned code = TREE_INT_CST_LOW (gimple_call_arg (call, 0));
/* Only count reduction variables once: the choice to pick
the setup call is fairly arbitrary. */
if (code == IFN_GOACC_REDUCTION_SETUP)
{
int level = TREE_INT_CST_LOW (gimple_call_arg (call, 3));
tree var = gimple_call_arg (call, 2);
tree offset = gimple_call_arg (call, 5);
tree var_type = TREE_TYPE (var);
unsigned HOST_WIDE_INT limit
= (tree_to_uhwi (offset)
+ tree_to_uhwi (TYPE_SIZE_UNIT (var_type)));
reduction_size[level]
= MAX (reduction_size[level], limit);
}
}
break;
case IFN_UNIQUE:
{
enum ifn_unique_kind kind
= ((enum ifn_unique_kind)
TREE_INT_CST_LOW (gimple_call_arg (call, 0)));
if (kind == IFN_UNIQUE_OACC_PRIVATE)
{
HOST_WIDE_INT level
= TREE_INT_CST_LOW (gimple_call_arg (call, 2));
if (level == -1)
break;
for (unsigned i = 3;
i < gimple_call_num_args (call);
i++)
{
tree arg = gimple_call_arg (call, i);
gcc_assert (TREE_CODE (arg) == ADDR_EXPR);
tree decl = TREE_OPERAND (arg, 0);
unsigned HOST_WIDE_INT align = DECL_ALIGN_UNIT (decl);
private_size[level] = ((private_size[level] + align - 1)
& ~(align - 1));
unsigned HOST_WIDE_INT decl_size
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (decl)));
private_size[level] += decl_size;
}
}
}
break;
}
}
}
int dims[GOMP_DIM_MAX];
for (unsigned i = 0; i < GOMP_DIM_MAX; i++)
dims[i] = oacc_get_fn_dim_size (current_function_decl, i);
/* Find bounds of shared-memory buffer space we can use. */
unsigned HOST_WIDE_INT bounds_lo = 0, bounds_hi = 0;
if (targetm.goacc.shared_mem_layout)
targetm.goacc.shared_mem_layout (&bounds_lo, &bounds_hi, dims,
private_size, reduction_size);
/* Perform worker partitioning unless we know 'num_workers(1)'. */
if (dims[GOMP_DIM_WORKER] != 1)
oacc_do_neutering (bounds_lo, bounds_hi);
return 0;
}
namespace {
const pass_data pass_data_omp_oacc_neuter_broadcast =
{
GIMPLE_PASS, /* type */
"omp_oacc_neuter_broadcast", /* name */
OPTGROUP_OMP, /* optinfo_flags */
TV_NONE, /* tv_id */
PROP_cfg, /* properties_required */
0, /* properties_provided */
0, /* properties_destroyed */
0, /* todo_flags_start */
TODO_update_ssa | TODO_cleanup_cfg, /* todo_flags_finish */
};
class pass_omp_oacc_neuter_broadcast : public gimple_opt_pass
{
public:
pass_omp_oacc_neuter_broadcast (gcc::context *ctxt)
: gimple_opt_pass (pass_data_omp_oacc_neuter_broadcast, ctxt)
{}
/* opt_pass methods: */
bool gate (function *fun) final override
{
if (!flag_openacc)
return false;
if (!targetm.goacc.create_worker_broadcast_record)
return false;
/* Only relevant for OpenACC offloaded functions. */
tree attr = oacc_get_fn_attrib (fun->decl);
if (!attr)
return false;
return true;
}
unsigned int execute (function *) final override
{
return execute_omp_oacc_neuter_broadcast ();
}
}; // class pass_omp_oacc_neuter_broadcast
} // anon namespace
gimple_opt_pass *
make_pass_omp_oacc_neuter_broadcast (gcc::context *ctxt)
{
return new pass_omp_oacc_neuter_broadcast (ctxt);
}
|