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
|
/* OpenACC worker partitioning via middle end neutering/broadcasting scheme
Copyright (C) 2015-2021 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"
/* Loop structure of the function. The entire function is described as
a NULL loop. */
/* Adapted from 'gcc/config/nvptx/nvptx.c: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.c: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.c: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.c: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.c: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 (TREE_CODE (var) == VAR_DECL)
{
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 (TREE_CODE (var) == VAR_DECL && 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,
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, prop_set);
if (par->next)
find_local_vars_to_propagate (par->next, outer_mask, partitioned_var_uses,
gang_private_vars, 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)
|| gang_private_vars->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 (!(*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);
}
}
}
gsi = gsi_start_bb (skip_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);
update_stmt (acc_bar);
}
/* Build COMPONENT_REF and set TREE_THIS_VOLATILE and TREE_READONLY on it
as appropriate. */
/* Adapted from 'gcc/omp-low.c:omp_build_component_ref'. */
static tree
oacc_build_component_ref (tree obj, tree field)
{
tree field_type = TREE_TYPE (field);
tree obj_type = TREE_TYPE (obj);
if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (obj_type)))
field_type = build_qualified_type
(field_type,
KEEP_QUAL_ADDR_SPACE (TYPE_QUALS (obj_type)));
tree ret = build3 (COMPONENT_REF, field_type, obj, field, NULL);
if (TREE_THIS_VOLATILE (field))
TREE_THIS_VOLATILE (ret) |= 1;
if (TREE_READONLY (field))
TREE_READONLY (ret) |= 1;
return ret;
}
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 = oacc_build_component_ref (x, field);
return x;
}
static tree
build_sender_ref (tree var, tree sender_decl, field_map_t *fields)
{
tree field = *fields->get (var);
return oacc_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)
{
/* 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");
tree receiver_decl
= targetm.goacc.create_worker_broadcast_record (record_type, false,
".oacc_worker_i");
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,
build_fold_addr_expr (sender_decl));
gimple_call_set_lhs (call, lhs);
gsi_insert_before (&start, call, GSI_NEW_STMT);
update_stmt (call);
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 ();
decl = builtin_decl_explicit (BUILT_IN_GOACC_BARRIER);
gimple *acc_bar = gimple_build_call (decl, 0);
gimple_stmt_iterator bar_gsi = gsi_start_bb (barrier_block);
gsi_insert_before (&bar_gsi, acc_bar, GSI_NEW_STMT);
cond = gimple_build_cond (NE_EXPR, recv_tmp, zero_ptr, NULL_TREE, NULL_TREE);
gsi_insert_after (&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 (TREE_CODE (var) == VAR_DECL);
/* If we had no record type, we will have no fields map. */
field_map_t **fields_p = record_field_map->get (record_type);
field_map_t *fields = fields_p ? *fields_p : NULL;
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 (TREE_CODE (var) == VAR_DECL)
{
/* 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 (TREE_CODE (var) == VAR_DECL)
{
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 ();
}
}
/* 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, 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);
}
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)
{
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;
}
tree record_type = (tree) block->aux;
if (has_defs)
worker_single_copy (block, block, &def_escapes_block,
&worker_partitioned_uses, record_type,
record_field_map);
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);
if (par->next)
neuter_worker_single (par->next, outer_mask, worker_single, vector_single,
prop_set, partitioned_var_uses, record_field_map);
}
static int
execute_omp_oacc_neuter_broadcast ()
{
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;
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, &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);
field_map_t *fields = new field_map_t;
bool existed;
existed = record_field_map.put (record_type, fields);
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;
}
}
neuter_worker_single (par, mask, worker_single, vector_single, &prop_set,
&partitioned_var_uses, &record_field_map);
for (auto it : record_field_map)
delete it.second;
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);
}
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: */
virtual bool gate (function *fun)
{
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;
/* Not relevant for 'num_workers(1)'. */
int worker_dim
= oacc_get_fn_dim_size (fun->decl, GOMP_DIM_WORKER);
if (worker_dim == 1)
return false;
return true;
}
virtual unsigned int execute (function *)
{
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);
}
|