/
expsmp.cpp
3304 lines (3030 loc) · 86.4 KB
/
expsmp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2015-2019, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/** \file
* \brief SMP expander routines
*/
#include "expsmp.h"
#include "exputil.h"
#include "exp_rte.h"
#include "dtypeutl.h"
#include "expatomics.h"
#include "regutil.h"
#include "machreg.h"
#include "ilm.h"
#include "ilmtp.h"
#include "ili.h"
#define EXPANDER_DECLARE_INTERNAL
#include "expand.h"
#include "machar.h"
#include "ccffinfo.h"
#include "kmpcutil.h"
#include "outliner.h"
#include "mp.h"
#include "x86.h"
#include "assem.h"
#include "llutil.h"
#include "llassem.h"
#include "ll_ftn.h"
#include "llmputil.h"
#if defined(OMP_OFFLOAD_LLVM) || defined(OMP_OFFLOAD_PGI)
#include "ompaccel.h"
#include "tgtutil.h"
#endif
#include "symfun.h"
#ifdef __cplusplus
inline SPTR GetPARUPLEVEL(SPTR sptr) {
return static_cast<SPTR>(PARUPLEVELG(sptr));
}
#undef PARUPLEVELG
#define PARUPLEVELG GetPARUPLEVEL
#endif
static int incrOutlinedCnt(void);
static int decrOutlinedCnt(void);
static int getOutlinedTemp(char *, int);
static int isUnnamedCs(int);
static int addMpUnp(void);
static int addMpUnv(void);
static int addMpBcsNest(void);
static int addMpEcsNest(void);
static int allocThreadprivate(SPTR sym, int *tmpthr);
#define mk_prototype mk_prototype_llvm
static int availIreg; /* next available integer register for jsr */
static int availFreg; /* next available floating point register for jsr */
static int maxIreg; /* max # of integer registers used by jsr */
static int maxFreg; /* max # of floating point registers used by jsr */
static int outlinedCnt; /* counter to record of outlined function */
static int parCnt; /* counter to record parallel regions */
static int parsectCnt; /* counter to record parallel sections */
static int critCnt; /* counter for critical sections */
static int taskbih; /* mark where task allocation should be */
static int taskCnt; /* counter for task regions */
static int taskLoopCnt; /* counter for taskloop regions */
static int taskLab; /* label after ETASK */
static int taskBv; /* bit values for flag for BTASK & TASKREG:
* 0x01 -- untied
* 0x02 -- if clause present
* 0x04 -- orphaned (dynamic, not lexically,
parallel)
* 0x08 -- nested task
* 0x10 -- forced defer (CUDA)
* 0x20 -- final task
* 0x40 -- execute immediately
*/
static int taskdup;
static int taskIfv; /* value of if clause for BTASK & TASKREG */
static SPTR taskFlags; ///< value of final clause for BTASK & TASKREG
static SPTR taskFnsptr; ///< store task func sptr
static SPTR taskAllocSptr; ///< store the return value from kmpc_alloc
static int maxOutlinedCnt; /* maximum parCnt for a function */
static int sumOutlinedCnt; /* sum of parCnts of functions already
* processed. 'sumOutlinedCnt+parCnt' can be
* the suffix of the name of a temp created for
* a outlined region within a function so that
* the temp is:
* 1) unique across functions,
* 2) reused across parallel regions within
* a function.
*/
static SPTR scopeSptr;
static int *mppgbih;
static int mppgcnt;
static int mppgBihSiz;
static struct {
int lb_ili;
int ub_ili;
int st_ili;
int lastitr;
int flags;
INT offset;
int tasklpargs[10]; /* ili in order as enum tasklooparg below */
} taskLpInfo;
enum taskloooparg {
TASKLPARG_TASK = 0,
TASKLPARG_IF_VAL,
TASKLPARG_LB,
TASKLPARG_UB,
TASKLPARG_ST,
TASKLPARG_NOGROUP,
TASKLPARG_SCHED,
TASKLPARG_GRAINSIZE,
TASKLPARG_TASKDUP,
TASKLPARG_MAX // must be last
};
#define TASK_LB taskLpInfo.lb_ili
#define TASK_LPVAR_OFFSET taskLpInfo.offset
#define TASK_UB taskLpInfo.ub_ili
#define TASK_ST taskLpInfo.st_ili
#define TASK_LASTITR taskLpInfo.lastitr
/* arguments to __kmpc_taskloop excepts ident and gtid */
#define TASKLPARGS taskLpInfo.tasklpargs
#define TASKLP_TASK taskLpInfo.tasklpargs[TASKLPARG_TASK]
#define TASKLP_IF taskLpInfo.tasklpargs[TASKLPARG_IF_VAL]
#define TASKLP_LB taskLpInfo.tasklpargs[TASKLPARG_LB]
#define TASKLP_UB taskLpInfo.tasklpargs[TASKLPARG_UB]
#define TASKLP_ST taskLpInfo.tasklpargs[TASKLPARG_ST]
#define TASKLP_NOGROUP taskLpInfo.tasklpargs[TASKLPARG_NOGROUP]
#define TASKLP_SCHED taskLpInfo.tasklpargs[TASKLPARG_SCHED]
#define TASKLP_GRAINSIZE taskLpInfo.tasklpargs[TASKLPARG_GRAINSIZE]
#define TASKLP_TASKDUP taskLpInfo.tasklpargs[TASKLPARG_TASKDUP]
typedef struct SectionsWrk_t {
SPTR lb; /* start at 0 */
SPTR ub; /* number of sections */
SPTR st; /* stride 1 */
SPTR last; /* flag for last section */
int cnt; /* running count */
int bbih; /* start block for sections */
} SectionsWrk_t;
static SectionsWrk_t sectionsWrk;
#define SECT_UB sectionsWrk.lb
#define SECT_LB sectionsWrk.ub
#define SECT_ST sectionsWrk.st
#define SECT_LAST sectionsWrk.last
#define SECT_CNT sectionsWrk.cnt
#define SECT_BBIH sectionsWrk.bbih
#define MP_NOT_IMPLEMENTED(_str) error(375, ERR_Fatal, 0, _str, NULL)
/**
\brief For use with generating an array filed with copyprivate addresses.
*/
typedef struct sptrListT {
SPTR o_sptr;
SPTR sptr; ///< either base sptr or TPpxxx thread private common block vector
int size_ili;
int vec_size_ili;
bool is_common_block;
struct sptrListT *next;
int cplus_assign_rou;
} sptrListT;
/* called once per function */
void
exp_smp_init(void)
{
parCnt = 0;
parsectCnt = 0;
outlinedCnt = 0;
critCnt = 0;
expb.lcpu2 = 0;
expb.lcpu3 = 0;
expb.ncpus2 = 0;
maxOutlinedCnt = 0;
mppgBihSiz = 16;
NEW(mppgbih, int, mppgBihSiz);
mppgcnt = 0;
taskCnt = 0;
taskLoopCnt = 0;
}
void
exp_smp_fini(void)
{
sumOutlinedCnt = +maxOutlinedCnt;
FREE(mppgbih);
}
static SPTR
getPrivateTemp(DTYPE dtype)
{
static int count;
SPTR sptr = getnewccsym('s', count++, ST_VAR);
SCP(sptr, SC_PRIVATE);
DTYPEP(sptr, dtype);
ENCLFUNCP(sptr, GBL_CURRFUNC);
return sptr;
}
static void
expSmpSectionInit(void)
{
SECT_LB = getPrivateTemp(DT_UINT);
SECT_UB = getPrivateTemp(DT_UINT);
SECT_LAST = getPrivateTemp(DT_UINT);
SECT_ST = getPrivateTemp(DT_UINT);
SECT_CNT = 0;
if (!gbl.outlined) {
SCP(SECT_LB, SC_AUTO);
SCP(SECT_UB, SC_AUTO);
SCP(SECT_LAST, SC_AUTO);
SCP(SECT_ST, SC_AUTO);
}
SECT_BBIH = expb.curbih;
}
static void
expSmpSectionEnd(void)
{
SECT_LB = getPrivateTemp(DT_UINT);
SECT_UB = getPrivateTemp(DT_UINT);
SECT_LAST = getPrivateTemp(DT_UINT);
SECT_ST = getPrivateTemp(DT_UINT);
SECT_CNT = 0;
SECT_BBIH = 0;
}
static int
sectionCreateBlock(int nextLabel, SPTR lb, SPTR ub, int myVal)
{
int ili, ubVal, lbVal;
myVal = ad_icon(myVal);
lbVal = ad3ili(IL_LD, ad_acon(lb, 0), addnme(NT_VAR, lb, 0, 0), MSZ_WORD);
ubVal = ad3ili(IL_LD, ad_acon(ub, 0), addnme(NT_VAR, ub, 0, 0), MSZ_WORD);
ili = ad4ili(IL_UICJMP, myVal, ubVal, CC_GT, nextLabel);
RFCNTI(nextLabel);
chk_block(ili);
ili = ad4ili(IL_UICJMP, myVal, lbVal, CC_LT, nextLabel);
RFCNTI(nextLabel);
return ili;
}
static int
sectionCreateLastblock(int nextLabel, SPTR lastValSym, int myVal)
{
int ili, lastVal;
myVal = ad_icon(myVal);
lastVal = ad3ili(IL_LD, ad_acon(lastValSym, 0),
addnme(NT_VAR, lastValSym, 0, 0), MSZ_WORD);
ili = ad4ili(IL_UICJMP, myVal, lastVal, CC_EQ, nextLabel);
RFCNTI(nextLabel);
return ili;
}
void
section_create_endblock(SPTR endLabel)
{
/* call kmpc_for_static_fini */
int ili;
wr_block();
cr_block();
ili = ll_make_kmpc_for_static_fini();
exp_label(endLabel);
iltb.callfg = 1;
chk_block(ili);
ili = ll_make_kmpc_barrier();
iltb.callfg = 1;
chk_block(ili);
BIH_LABEL(expb.curbih) = endLabel;
ILIBLKP(endLabel, expb.curbih);
}
/* set: 1 to set, 0 to restore
* eampp: if it is eampp, then subtract its value(1) from mppgcnt
*/
#define SET_MPPBIH 1
#define RESTORE_MPPBIH 0
#define IS_PREVMPPG 1
#define IS_NOTPREVMPPG 0
#define USE_NEXTBIH 1
#define NOTUSE_NEXTBIH 0
static void
resetMppBih(int set, int eampp)
{
static int savebih;
static int savex14;
int bih;
if (mppgcnt == 0)
return;
if (set) {
savebih = expb.curbih;
bih = mppgbih[mppgcnt - eampp];
if (savebih == bih) {
savebih = 0;
return;
}
savex14 = flg.x[14];
flg.x[14] |= 0x1000; /* don't split at calls */
wr_block();
expb.curbih = bih;
rdilts(expb.curbih);
expb.curilt = ILT_PREV(0);
} else if (savebih) { /* only reset if we save it */
wrilts(mppgbih[mppgcnt - eampp]);
expb.curbih = savebih;
rdilts(expb.curbih);
expb.curilt = ILT_PREV(0);
flg.x[14] = savex14;
}
}
static void
resetTaskBih(int set)
{
int bih;
static int savebih;
static int savex14;
if (taskbih == 0)
return;
if (set) {
savebih = expb.curbih;
savex14 = flg.x[14];
flg.x[14] |= 0x1000;
wr_block();
expb.curbih = taskbih;
rdilts(expb.curbih);
expb.curilt = ILT_PREV(0);
} else {
wrilts(taskbih);
expb.curbih = savebih;
rdilts(expb.curbih);
expb.curilt = ILT_PREV(0);
flg.x[14] = savex14;
}
}
static void
sptrListAdd(sptrListT **list, SPTR sptr, int size_ili, bool is_cmblk,
int cplus_assign_rou, int vec_size_ili, SPTR o_sptr)
{
sptrListT *node = (sptrListT *)malloc(sizeof(sptrListT));
node->o_sptr = o_sptr;
node->sptr = sptr;
node->next = *list;
node->is_common_block = is_cmblk;
node->size_ili = size_ili;
node->vec_size_ili = vec_size_ili; /* used for COPYIN_CL of arrays */
node->cplus_assign_rou = cplus_assign_rou;
*list = node;
}
static void
sptrListFree(sptrListT **list)
{
sptrListT *n = *list;
while (n) {
sptrListT *next = n->next;
free(n);
n = next;
}
*list = NULL;
}
static int
sptrListLength(const sptrListT *list)
{
int count = 0;
const sptrListT *n;
for (n = list; n; n = n->next)
++count;
return count;
}
/* Returns an ili of a temporary variable that conatins size information
* The runtime for instance, _mp_copypriv_kmpc, expects size_t* for size.
*
* 'bytes' is the actual byte size and not an sptr or ili.
*/
static int
genSizeAcon(int size_ili)
{
int ili;
SPTR tmp;
int nme;
const DTYPE dtype = (TARGET_PTRSIZE == 8) ? DT_INT8 : DT_INT;
tmp = getPrivateTemp(dtype);
SCP(tmp, SC_AUTO);
ili = ad_acon(tmp, 0);
nme = addnme(NT_VAR, tmp, 0, 0);
ADDRTKNP(tmp, 1);
if (TARGET_PTRSIZE == 8) {
ili = ad4ili(IL_STKR, size_ili, ili, nme, MSZ_I8);
} else {
size_ili = ad1ili(IL_KIMV, size_ili);
ili = ad4ili(IL_ST, size_ili, ili, nme, MSZ_WORD);
}
chk_block(ili);
return ad_acon(tmp, 0);
}
/* Given a sptr list, create an array of pairs:
* (size, address) where:
* 'size' - Pointer to a temporary variable containing the byte size of
* sptr. (size_t *)
* 'address' - Address of sptr. (void *).
*
* These pairs are represented in an array where
* the even indices are the size pointers and the odd indices the
* addresses. The sentinel/terminator is the all-zero pair.
* [(sz0,addr0), (sz1,addr1), ... (0x0, 0x0)].
*
* We represent these as an array, which is more convenient to manage
* internally. The runtime routine _mp_copypriv_kmpc expects this format.
*
* Returns: The sptr of this majestic array that we so masterfully create here.
*/
static SPTR
makeCopyprivArray(const sptrListT *list, bool pass_size_addresses)
{
int i, ili, nme, n_elts;
SPTR array;
DTYPE dtype;
int basenme, adsc;
static int id;
const sptrListT *node;
/* Count the number of items in the list */
n_elts = 0;
for (node = list; node; node = node->next)
++n_elts;
/* We represent each entry as a pair for each private variable (each node in
* sptr_list): (size, sptr)
*
* +2 for the last node, the sentinel (null node), which tells the
* runtime it has reached the end of the array. Each node is 2 array elts.
*/
n_elts = (n_elts * 2) + 2;
/* Create the array dtype: each element is word size */
array = getnewccsym('a', ++id, ST_ARRAY);
{
ADSC *adsc;
INT con[2] = {0, n_elts};
dtype = get_array_dtype(1, DT_CPTR);
adsc = AD_DPTR(dtype);
AD_LWBD(adsc, 0) = stb.i1;
AD_UPBD(adsc, 0) = getcon(con, DT_INT);
AD_NUMELM(adsc) = AD_UPBD(adsc, 0);
}
DTYPEP(array, dtype);
SCP(array, SC_AUTO);
/* Build the list: (size, sptr) pairs. */
basenme = addnme(NT_VAR, array, 0, 0);
for (node = list, i = 0; node; node = node->next, ++i) {
int sptr_nme, sptr_ili;
if (node->is_common_block || THREADG(node->sptr)) {
/* This is thread private so obtain address from the TP vector */
if (node->is_common_block)
ref_threadprivate(node->sptr, &sptr_ili, &sptr_nme);
else
ref_threadprivate_var(node->sptr, &sptr_ili, &sptr_nme, 1);
} else {
/* Else, this is not thread private */
sptr_nme = addnme(NT_VAR, node->sptr, 0, 0);
sptr_ili = mk_address(node->sptr);
}
/* array[i] = size */
nme = add_arrnme(NT_ARR, array, basenme, 0, ad_icon(i), false);
if (pass_size_addresses) { /* why do I need to pass address? */
ili = genSizeAcon(node->size_ili);
ili = ad3ili(IL_STA, ili, ad_acon(array, i * TARGET_PTRSIZE), nme);
} else {
ili = ad4ili(IL_ST, node->size_ili, ad_acon(array, i * TARGET_PTRSIZE),
nme, TARGET_PTRSIZE == 8 ? MSZ_I8 : MSZ_WORD);
}
chk_block(ili);
/* array[i+1] = local (stack based) sptr */
++i;
nme = add_arrnme(NT_ARR, array, basenme, 0, ad_icon(i), false);
ili = ad3ili(IL_STA, sptr_ili, ad_acon(array, i * TARGET_PTRSIZE), nme);
chk_block(ili);
}
/* Terminate the array with a sentinel that the runtime will recognize */
nme = add_arrnme(NT_ARR, array, basenme, 0, ad_icon(i), false);
ili = ad3ili(IL_STA, ad_aconi(0), ad_acon(array, i * TARGET_PTRSIZE), nme);
chk_block(ili);
++i;
nme = add_arrnme(NT_ARR, array, basenme, 0, ad_icon(i), false);
ili = ad3ili(IL_STA, ad_aconi(0), ad_acon(array, i * TARGET_PTRSIZE), nme);
chk_block(ili);
return array;
}
static int
mkMemcpy(void)
{
int func;
func = mk_prototype("memcpy", NULL, DT_CPTR, 3, DT_CPTR, DT_CPTR, DT_UINT8);
SCP(func, SC_EXTERN);
func = mkfunc("memcpy");
return func;
}
/**
\brief ...
\param arglist
\param opc IL_DAIR/IL_DAAR/IL_DADP/IL_DASP/IL_ARGxx (x86)
\param argili
Add argument expression \p argili to existing argument list \p arglist using
opcode \p opc. If \p arglist = 0, begin a new list.
*/
static int
jsrAddArg(int arglist, ILI_OP opc, int argili)
{
int rg;
int ili;
if (arglist == 0) {
arglist = ad1ili(IL_NULL, 0);
availIreg = 0;
availFreg = 0;
}
/*
* WARNING: For the x86, this implies that the standard call mechanism is
* being used. If there are multiple arguments, they need to be pushed
* on the stack in reverse order (first jsrAddArg() call is for the last
* argument, ...).
*/
switch (opc) {
case IL_ARGAR:
ili = ad3ili(IL_ARGAR, argili, arglist, 0);
return ili;
case IL_ARGIR:
case IL_ARGKR:
case IL_ARGSP:
case IL_ARGDP:
ili = ad2ili(opc, argili, arglist);
return ili;
default:
/* allow arguments to be passed in registers and on the stack */
break;
}
assert(is_daili_opcode(opc), "jsrAddArg: invalid opcode", opc, ERR_Fatal);
if (opc == IL_DAIR || opc == IL_DAAR || opc == IL_DAKR) {
rg = IR(availIreg++);
} else {
if (opc == IL_DADP && (availFreg & 1))
availFreg++;
rg = SP(availFreg);
availFreg++;
if (opc == IL_DADP)
availFreg++;
}
ili = ad3ili(opc, argili, rg, arglist);
return ili;
}
/** \brief Return the ili of a call to a function with name fname, and argument
* list argili. If argili = 0, argument list is empty.
*
* \param fname function name
* \param opc IL_QJSR/IL_JSR
* \param argili argument list
*/
static int
makeCall(char *fname, ILI_OP opc, int argili)
{
int ili;
bool old_share_proc, old_share_qjsr;
if (argili == 0) {
argili = ad1ili(IL_NULL, 0);
availIreg = 0;
availFreg = 0;
}
old_share_proc = share_proc_ili;
old_share_qjsr = share_qjsr_ili;
share_proc_ili = false;
share_qjsr_ili = false;
ili = ad2ili(opc, mkfunc(fname), argili);
share_proc_ili = old_share_proc;
share_qjsr_ili = old_share_qjsr;
if (availFreg > 0 && availFreg < 4)
availFreg = 4;
if (availIreg > maxIreg)
maxIreg = availIreg;
if (availFreg > maxFreg)
maxFreg = availFreg;
return ili;
}
static void
addCopyinInplace(const sptrListT *list)
{
int i, ili, nme, n_elts, dest_nme, argili, call;
int master_ili;
SPTR lab;
int altili, func;
SPTR sptr;
int indirect_load;
const sptrListT *node;
n_elts = 0;
lab = getlab();
for (node = list, i = 0; node; node = node->next, ++i) {
int sptr_nme, sptr_ili;
sptr = node->o_sptr;
indirect_load = 0;
if (STYPEG(sptr) == ST_CMBLK) {
sptr = CMEMFG(sptr);
if (!sptr)
continue;
} else if (SCG(sptr) == SC_BASED && POINTERG(sptr)) {
if (ALLOCATTRG(sptr)) {
indirect_load = 1;
}
sptr = MIDNUMG(sptr);
}
/* what we have here it TPxx, need to find the symbol it points to */
/* master copy - should be passed from previous region */
master_ili = mk_address(sptr);
/* current threadprivate copy */
sptr_ili = llGetThreadprivateAddr(node->sptr);
if (indirect_load == 1) {
sptr_nme = addnme(NT_VAR, sptr, 0, 0);
sptr_ili = ad2ili(IL_LDA, sptr_ili, sptr_nme);
master_ili = ad2ili(IL_LDA, master_ili, sptr_nme);
}
dest_nme = ILI_OPND(sptr_ili, 2);
if (n_elts == 0) {
ili = ad4ili(IL_ACJMP, sptr_ili, master_ili, CC_EQ, lab);
RFCNTI(lab);
chk_block(ili);
n_elts = 1;
}
/* now do a copy */
altili = 0;
{
func = mkMemcpy();
argili = jsrAddArg(0, IL_ARGKR, sel_iconv(node->size_ili, 1));
argili = jsrAddArg(argili, IL_ARGAR, master_ili);
argili = jsrAddArg(argili, IL_ARGAR, sptr_ili);
call = makeCall("memcpy", IL_JSR, argili);
argili = ad1ili(IL_NULL, 0);
argili =
ad4ili(IL_GARG, sel_iconv(node->size_ili, 1), argili, DT_INT8, 0);
argili = ad4ili(IL_GARG, master_ili, argili, DT_CPTR, 0);
argili = ad4ili(IL_GARG, sptr_ili, argili, DT_CPTR, 0);
altili = ad3ili(IL_GJSR, func, argili, 0);
}
ILI_ALT(call) = altili;
iltb.callfg = 1;
chk_block(call);
}
if (n_elts) {
wr_block();
cr_block();
/* create a block */
BIH_LABEL(expb.curbih) = lab;
ILIBLKP(lab, expb.curbih);
ili = ll_make_kmpc_barrier();
iltb.callfg = 1;
chk_block(ili);
wr_block();
cr_block();
}
}
static void
makeCopyprivArray_tls(const sptrListT *list)
{
int i, ili, nme, n_elts, array, dtype, basenme, adsc, argili, call;
int master_ili, thread_addr;
SPTR lab;
int altili, master_nme, func;
SPTR sptr;
const sptrListT *node;
n_elts = 0;
lab = getlab();
for (node = list, i = 0; node; node = node->next, ++i) {
int sptr_nme, sptr_ili;
sptr = MIDNUMG(node->sptr);
if (STYPEG(sptr) == ST_CMBLK) {
sptr = CMEMFG(node->o_sptr);
if (!sptr)
continue;
} else if (SCG(sptr) == SC_BASED && POINTERG(sptr)) {
sptr = MIDNUMG(sptr);
}
master_nme = addnme(NT_VAR, sptr, 0, (INT)0);
master_ili = mk_address(sptr);
basenme = addnme(NT_VAR, node->sptr, 0, (INT)0);
sptr_ili = ad2ili(IL_LDA, ad_acon(node->sptr, (INT)0), basenme);
if (n_elts == 0) {
ili = ad4ili(IL_ACJMP, sptr_ili, master_ili, CC_EQ, lab);
RFCNTI(lab);
chk_block(ili);
n_elts = 1;
}
/* now do a copy */
altili = 0;
{
func = mkMemcpy();
argili = jsrAddArg(0, IL_ARGIR, sel_iconv(node->size_ili, 0));
argili = jsrAddArg(argili, IL_ARGAR, master_ili);
argili = jsrAddArg(argili, IL_ARGAR, sptr_ili);
call = makeCall("memcpy", IL_JSR, argili);
argili = ad1ili(IL_NULL, 0);
argili =
ad4ili(IL_GARG, sel_iconv(node->size_ili, 1), argili, DT_INT8, 0);
argili = ad4ili(IL_GARG, master_ili, argili, DT_CPTR, 0);
argili = ad4ili(IL_GARG, sptr_ili, argili, DT_CPTR, 0);
altili = ad3ili(IL_GJSR, func, argili, 0);
}
ILI_ALT(call) = altili;
iltb.callfg = 1;
chk_block(call);
}
if (n_elts) {
wr_block();
cr_block();
/* create a block */
BIH_LABEL(expb.curbih) = lab;
ILIBLKP(lab, expb.curbih);
ili = ll_make_kmpc_barrier();
iltb.callfg = 1;
chk_block(ili);
wr_block();
cr_block();
}
}
static int
findEnlabBih(int func)
{
int bih;
bih = BIH_NEXT(BIHNUMG(func));
return bih;
}
static void
setTaskloopVars(SPTR lb, SPTR ub, SPTR stride, SPTR lastitr)
{
int nme, basenm, baseili, ili, bih;
SPTR arg;
int asym;
int oldbih;
ILI_OP ld, st;
MSZ msz;
oldbih = expb.curbih;
/* This code is in an outlined taskloop routine.
* Load taskloop vars from arg1 to local/private vars.
*/
arg = ll_get_hostprog_arg(GBL_CURRFUNC, 2);
basenm = addnme(NT_VAR, arg, 0, 0);
baseili = ad_acon(arg, 0);
baseili = mk_address(arg);
arg = mk_argasym(arg);
basenm = addnme(NT_VAR, arg, 0, (INT)0);
baseili = ad2ili(IL_LDA, baseili, basenm);
nme = addnme(NT_IND, lb, basenm, 0);
ili = ad3ili(IL_AADD, baseili, ad_aconi(TASK_LPVAR_OFFSET), 0);
ldst_msz(DT_INT8, &ld, &st, &msz);
ili = ad3ili(ld, ili, nme, msz);
ldst_msz(DTYPEG(lb), &ld, &st, &msz);
if (msz != MSZ_I8)
ili = kimove(ili);
ili = ad4ili(st, ili, mk_address(lb), addnme(NT_VAR, lb, 0, 0), msz);
chk_block(ili);
nme = addnme(NT_IND, ub, basenm, 0);
ili = ad3ili(IL_AADD, baseili,
ad_aconi(TASK_LPVAR_OFFSET + zsize_of(DT_INT8)), 0);
ldst_msz(DT_INT8, &ld, &st, &msz);
ili = ad3ili(ld, ili, nme, msz);
ldst_msz(DTYPEG(ub), &ld, &st, &msz);
if (msz != MSZ_I8)
ili = kimove(ili);
ili = ad4ili(st, ili, mk_address(ub), addnme(NT_VAR, ub, 0, 0), msz);
chk_block(ili);
if (STYPEG(stride) != ST_CONST) {
nme = addnme(NT_IND, stride, basenm, 0);
ili = ad3ili(IL_AADD, baseili,
ad_aconi(TASK_LPVAR_OFFSET + (zsize_of(DT_INT8) * 2)), 0);
ldst_msz(DT_INT8, &ld, &st, &msz);
ili = ad3ili(ld, ili, nme, msz);
ldst_msz(DTYPEG(stride), &ld, &st, &msz);
if (msz != MSZ_I8)
ili = kimove(ili);
ili =
ad4ili(st, ili, mk_address(stride), addnme(NT_VAR, stride, 0, 0), msz);
chk_block(ili);
}
if (lastitr && STYPEG(lastitr) != ST_CONST) {
nme = addnme(NT_IND, lastitr, basenm, 0);
ldst_msz(DT_INT, &ld, &st, &msz);
ili = ad3ili(IL_AADD, baseili,
ad_aconi(TASK_LPVAR_OFFSET + (zsize_of(DT_INT8) * 3)), 0);
ili = ad3ili(ld, ili, nme, msz);
ldst_msz(DTYPEG(lastitr), &ld, &st, &msz);
ili = ad4ili(st, ili, ad_acon(lastitr, 0), addnme(NT_VAR, lastitr, 0, 0),
msz);
chk_block(ili);
}
if (oldbih == expb.curbih) {
wr_block();
cr_block();
}
}
/* isn't there some standard routine I can subsititue for this? */
static int
getElemSize(DTYPE dtype)
{
DTYPE dd = dtype;
while (dd && (DTY(dd) == TY_ARRAY)) {
dd = DTySeqTyElement(dd);
}
if (DTY(dd) == TY_STRUCT)
return DTyAlgTySize(dd);
return 0;
}
static void
clearTaskloopInfo(void)
{
INT offset = TASK_LPVAR_OFFSET;
BZERO(&taskLpInfo, char, sizeof(taskLpInfo));
TASK_LPVAR_OFFSET = offset;
}
static int
genIntStore(SPTR sym, int rhs)
{
int ili;
int nme;
ili = ad_acon(sym, 0);
nme = addnme(NT_VAR, sym, 0, 0);
ili = ad4ili(IL_ST, rhs, ili, nme, MSZ_WORD);
return ili;
}
static int
genIntLoad(SPTR sym)
{
int ili;
int nme;
ili = ad_acon(sym, 0);
nme = addnme(NT_VAR, sym, 0, 0);
ili = ad3ili(IL_LD, ili, nme, MSZ_WORD);
return ili;
}
void
exp_smp(ILM_OP opc, ILM *ilmp, int curilm)
{
#ifdef IM_BPAR
int argili = 0;
int ili, tili, ili_arg;
int lastilt;
SPTR sym;
SPTR sptr;
int offset, savebih;
SPTR end_label, beg_label;
int off;
int addr, nmex, stili;
int prev_scope;
char name[10];
int argilm;
SPTR tpv;
int pv;
int savex14;
char *doschedule;
int semaphore, dotarget;
static int assign_rou = 0; /* C++ only, lets avoid more ifdefs */
ILM_T rou_op;
int num_elem, element_size;
loop_args_t loop_args;
LLTask *task;
bool is_cmblk;
static sptrListT *copysptr_list = NULL;
static SPTR uplevel_sptr;
static SPTR single_thread;
static SPTR in_single;
static SPTR targetfunc_sptr = SPTR_NULL, targetdevice_func_sptr = SPTR_NULL;
int target_mode = 0;
SPTR nlower, nupper, nstride;
#if defined(OMP_OFFLOAD_LLVM) || defined(OMP_OFFLOAD_PGI)
static int target_ili_num_threads = 0;
static int target_ili_num_teams= 0;
static int target_ili_thread_limit= 0;
#endif
int sz;
ISZ_T size, num_elements;
static int isTargetDevice = 0;
switch (opc) {
case IM_BPAR:
case IM_BPARN:
case IM_BPARD:
case IM_BPARA:
case IM_EPAR:
case IM_EPARD:
case IM_BTASK:
case IM_BTASKLOOP:
case IM_ETASK:
case IM_ETASKLOOP:
case IM_BTARGET:
case IM_ETARGET:
case IM_BTEAMS:
case IM_BTEAMSN:
case IM_ETEAMS:
case IM_TASKLOOPVARS:
case IM_TASKLOOPREG:
#ifdef IM_TASKPRIVATE
case IM_TASKPRIVATE:
#endif
case IM_TASKFIRSTPRIV:
case IM_ETASKLOOPREG:
case IM_BTASKDUP:
case IM_ETASKDUP:
break;
default:
ll_rewrite_ilms(-1, curilm, 0);
break;