xref: /llvm-project/flang/test/Semantics/OpenMP/clause-validity01.f90 (revision 03cbe42627c7a7940b47cc1a2cda0120bc9c6d5e)
1! REQUIRES: openmp_runtime
2
3! RUN: %python %S/../test_errors.py %s %flang_fc1 %openmp_flags %openmp_module_flag -fopenmp-version=51
4use omp_lib
5! Check OpenMP clause validity for the following directives:
6!
7!    2.5 PARALLEL construct
8!    2.7.1 Loop construct
9!    ...
10
11  use iso_c_binding
12  integer :: b = 128
13  integer, allocatable :: allc
14  type(C_PTR) :: cpt
15  integer :: z, c = 32
16  integer, parameter :: num = 16
17  real(8) :: arrayA(256), arrayB(512)
18
19  integer(omp_memspace_handle_kind) :: xy_memspace = omp_default_mem_space
20  type(omp_alloctrait) :: xy_traits(1) = [omp_alloctrait(omp_atk_alignment,64)]
21  integer(omp_allocator_handle_kind) :: xy_alloc
22  xy_alloc = omp_init_allocator(xy_memspace, 1, xy_traits)
23
24  arrayA = 1.414
25  arrayB = 3.14
26  N = 1024
27
28! 2.5 parallel-clause -> if-clause |
29!                        num-threads-clause |
30!                        default-clause |
31!                        private-clause |
32!                        firstprivate-clause |
33!                        shared-clause |
34!                        copyin-clause |
35!                        reduction-clause |
36!                        proc-bind-clause |
37!                        allocate-clause
38
39  !$omp parallel
40  do i = 1, N
41     a = 3.14
42  enddo
43  !$omp end parallel
44
45  !$omp parallel private(b) allocate(b)
46  do i = 1, N
47     a = 3.14
48  enddo
49  !$omp end parallel
50
51  !$omp parallel private(c, b) allocate(omp_default_mem_space : b, c)
52  do i = 1, N
53     a = 3.14
54  enddo
55  !$omp end parallel
56
57  !$omp parallel allocate(b) allocate(c) private(b, c)
58  do i = 1, N
59     a = 3.14
60  enddo
61  !$omp end parallel
62
63  !$omp parallel allocate(xy_alloc :b) private(b)
64  do i = 1, N
65     a = 3.14
66  enddo
67  !$omp end parallel
68
69  !$omp task private(b) allocate(b)
70  do i = 1, N
71     z = 2
72  end do
73  !$omp end task
74
75  !$omp teams private(b) allocate(b)
76  do i = 1, N
77     z = 2
78  end do
79  !$omp end teams
80
81  !$omp target private(b) allocate(b)
82  do i = 1, N
83     z = 2
84  end do
85  !$omp end target
86
87  !ERROR: ALLOCATE clause is not allowed on the TARGET DATA directive
88  !$omp target data map(from: b) allocate(b)
89  do i = 1, N
90     z = 2
91  enddo
92  !$omp end target data
93
94  !ERROR: SCHEDULE clause is not allowed on the PARALLEL directive
95  !$omp parallel schedule(static)
96  do i = 1, N
97     a = 3.14
98  enddo
99  !$omp end parallel
100
101  !ERROR: COLLAPSE clause is not allowed on the PARALLEL directive
102  !$omp parallel collapse(2)
103  do i = 1, N
104     do j = 1, N
105        a = 3.14
106     enddo
107  enddo
108  !$omp end parallel
109
110  !ERROR: The parameter of the COLLAPSE clause must be a constant positive integer expression
111  !$omp do collapse(-1)
112  do i = 1, N
113    do j = 1, N
114      a = 3.14
115    enddo
116  enddo
117  !$omp end do
118
119  a = 1.0
120  !$omp parallel firstprivate(a)
121  do i = 1, N
122     a = 3.14
123  enddo
124  !ERROR: NUM_THREADS clause is not allowed on the END PARALLEL directive
125  !$omp end parallel num_threads(4)
126
127  !ERROR: LASTPRIVATE clause is not allowed on the PARALLEL directive
128  !ERROR: NUM_TASKS clause is not allowed on the PARALLEL directive
129  !ERROR: INBRANCH clause is not allowed on the PARALLEL directive
130  !$omp parallel lastprivate(a) NUM_TASKS(4) inbranch
131  do i = 1, N
132     a = 3.14
133  enddo
134  !$omp end parallel
135
136  !ERROR: At most one NUM_THREADS clause can appear on the PARALLEL directive
137  !$omp parallel num_threads(2) num_threads(4)
138  do i = 1, N
139     a = 3.14
140  enddo
141  !$omp end parallel
142
143  !ERROR: The parameter of the NUM_THREADS clause must be a positive integer expression
144  !$omp parallel num_threads(1-4)
145  do i = 1, N
146     a = 3.14
147  enddo
148  !ERROR: NOWAIT clause is not allowed on the END PARALLEL directive
149  !$omp end parallel nowait
150
151  !$omp parallel num_threads(num-10)
152  do i = 1, N
153     a = 3.14
154  enddo
155  !$omp end parallel
156
157  !$omp parallel num_threads(b+1)
158  do i = 1, N
159     a = 3.14
160  enddo
161  !$omp end parallel
162
163  !$omp parallel
164  do i = 1, N
165  enddo
166  !ERROR: Unmatched END TARGET directive
167  !$omp end target
168
169  ! OMP 5.0 - 2.6 Restriction point 1
170  outofparallel: do k =1, 10
171  !$omp parallel
172  !$omp do
173  outer: do i=0, 10
174    inner: do j=1, 10
175      exit
176      !ERROR: EXIT statement terminates associated loop of an OpenMP DO construct
177      exit outer
178      !ERROR: EXIT to construct 'outofparallel' outside of PARALLEL construct is not allowed
179      !ERROR: EXIT to construct 'outofparallel' outside of DO construct is not allowed
180      exit outofparallel
181    end do inner
182  end do outer
183  !$omp end do
184  !$omp end parallel
185  end do outofparallel
186
187! 2.7.1  do-clause -> private-clause |
188!                     firstprivate-clause |
189!                     lastprivate-clause |
190!                     linear-clause |
191!                     reduction-clause |
192!                     schedule-clause |
193!                     collapse-clause |
194!                     ordered-clause
195
196  !ERROR: When SCHEDULE clause has AUTO specified, it must not have chunk size specified
197  !ERROR: At most one SCHEDULE clause can appear on the DO directive
198  !ERROR: When SCHEDULE clause has RUNTIME specified, it must not have chunk size specified
199  !$omp do schedule(auto, 2) schedule(runtime, 2)
200  do i = 1, N
201     a = 3.14
202  enddo
203
204  !ERROR: A modifier may not be specified in a LINEAR clause on the DO directive
205  !$omp do linear(ref(b))
206  do i = 1, N
207     a = 3.14
208  enddo
209
210  !ERROR: The NONMONOTONIC modifier can only be specified with SCHEDULE(DYNAMIC) or SCHEDULE(GUIDED)
211  !ERROR: The NONMONOTONIC modifier cannot be specified if an ORDERED clause is specified
212  !$omp do schedule(NONMONOTONIC:static) ordered
213  do i = 1, N
214     a = 3.14
215  enddo
216
217  !$omp do schedule(simd, monotonic:dynamic)
218  do i = 1, N
219     a = 3.14
220  enddo
221
222  !ERROR: Clause LINEAR is not allowed if clause ORDERED appears on the DO directive
223  !ERROR: The parameter of the ORDERED clause must be a constant positive integer expression
224  !ERROR: 'b' appears in more than one data-sharing clause on the same OpenMP directive
225  !$omp do ordered(1-1) private(b) linear(b) linear(a)
226  do i = 1, N
227     a = 3.14
228  enddo
229
230  !ERROR: Clause LINEAR is not allowed if clause ORDERED appears on the DO directive
231  !ERROR: The parameter of the ORDERED clause must be a constant positive integer expression
232  !$omp do ordered(1-1) linear(a)
233  do i = 1, N
234     a = 3.14
235  enddo
236
237  !ERROR: The parameter of the ORDERED clause must be greater than or equal to the parameter of the COLLAPSE clause
238  !$omp do collapse(num-14) ordered(1)
239  do i = 1, N
240     do j = 1, N
241        do k = 1, N
242           a = 3.14
243        enddo
244     enddo
245  enddo
246
247  !$omp parallel do simd if(parallel:a>1.)
248  do i = 1, N
249  enddo
250  !$omp end parallel do simd
251
252  !ERROR: TARGET is not a constituent of the PARALLEL DO directive
253  !$omp parallel do if(target:a>1.)
254  do i = 1, N
255  enddo
256  !ERROR: Unmatched END SIMD directive
257  !$omp end simd
258
259! 2.7.2 sections-clause -> private-clause |
260!                         firstprivate-clause |
261!                         lastprivate-clause |
262!                         reduction-clause
263
264  !$omp parallel
265  !$omp sections
266  !$omp section
267  a = 0.0
268  !$omp section
269  b = 1
270  !$omp end sections nowait
271  !$omp end parallel
272
273  !$omp parallel
274  !$omp sections
275  !$omp section
276  a = 0.0
277  !ERROR: Unmatched END PARALLEL SECTIONS directive
278  !$omp end parallel sections
279  !$omp end parallel
280
281  !$omp parallel
282  !$omp sections
283  a = 0.0
284  b = 1
285  !$omp section
286  c = 1
287  d = 2
288  !ERROR: NUM_THREADS clause is not allowed on the END SECTIONS directive
289  !$omp end sections num_threads(4)
290
291  !$omp parallel
292  !$omp sections
293    b = 1
294  !$omp section
295    c = 1
296    d = 2
297  !ERROR: At most one NOWAIT clause can appear on the END SECTIONS directive
298  !$omp end sections nowait nowait
299  !$omp end parallel
300
301  !$omp end parallel
302
303! 2.11.2 parallel-sections-clause -> parallel-clause |
304!                                    sections-clause
305
306  !$omp parallel sections num_threads(4) private(b) lastprivate(d)
307  a = 0.0
308  !$omp section
309  b = 1
310  c = 2
311  !$omp section
312  d = 3
313  !$omp end parallel sections
314
315  !ERROR: At most one NUM_THREADS clause can appear on the PARALLEL SECTIONS directive
316  !$omp parallel sections num_threads(1) num_threads(4)
317  a = 0.0
318  !ERROR: Unmatched END SECTIONS directive
319  !$omp end sections
320
321  !$omp parallel sections
322  !ERROR: NOWAIT clause is not allowed on the END PARALLEL SECTIONS directive
323  !$omp end parallel sections nowait
324
325! 2.7.3 single-clause -> private-clause |
326!                        firstprivate-clause
327!   end-single-clause -> copyprivate-clause |
328!                        nowait-clause
329
330  !$omp parallel
331  b = 1
332  !ERROR: LASTPRIVATE clause is not allowed on the SINGLE directive
333  !ERROR: NOWAIT clause is not allowed on the OMP SINGLE directive, use it on OMP END SINGLE directive
334  !$omp single private(a) lastprivate(c) nowait
335  a = 3.14
336  !ERROR: Clause NOWAIT is not allowed if clause COPYPRIVATE appears on the END SINGLE directive
337  !ERROR: COPYPRIVATE variable 'a' may not appear on a PRIVATE or FIRSTPRIVATE clause on a SINGLE construct
338  !ERROR: At most one NOWAIT clause can appear on the END SINGLE directive
339  !$omp end single copyprivate(a) nowait nowait
340  c = 2
341  !$omp end parallel
342
343! 2.7.4 workshare
344
345  !$omp parallel
346  !$omp workshare
347  a = 1.0
348  !$omp end workshare nowait
349  !ERROR: NUM_THREADS clause is not allowed on the WORKSHARE directive
350  !$omp workshare num_threads(4)
351  a = 1.0
352  !ERROR: COPYPRIVATE clause is not allowed on the END WORKSHARE directive
353  !$omp end workshare nowait copyprivate(a)
354  !ERROR: NOWAIT clause is not allowed on the OMP WORKSHARE directive, use it on OMP END WORKSHARE directive
355  !$omp workshare nowait
356  !$omp end workshare
357  !$omp end parallel
358
359! 2.8.1 simd-clause -> safelen-clause |
360!                      simdlen-clause |
361!                      linear-clause |
362!                      aligned-clause |
363!                      private-clause |
364!                      lastprivate-clause |
365!                      reduction-clause |
366!                      collapse-clause
367
368  a = 0.0
369  !ERROR: TASK_REDUCTION clause is not allowed on the SIMD directive
370  !$omp simd private(b) reduction(+:a) task_reduction(+:a)
371  do i = 1, N
372     a = a + b + 3.14
373  enddo
374
375  !ERROR: At most one SAFELEN clause can appear on the SIMD directive
376  !$omp simd safelen(1) safelen(2)
377  do i = 1, N
378     a = 3.14
379  enddo
380
381  !ERROR: The parameter of the SIMDLEN clause must be a constant positive integer expression
382  !$omp simd simdlen(-1)
383  do i = 1, N
384     a = 3.14
385  enddo
386
387  !ERROR: The alignment value should be a constant positive integer
388  !$omp simd aligned(cpt:-2)
389  do i = 1, N
390     a = 3.14
391  enddo
392
393  !$omp parallel
394  !ERROR: The parameter of the SIMDLEN clause must be less than or equal to the parameter of the SAFELEN clause
395  !$omp simd safelen(1+1) simdlen(1+2)
396  do i = 1, N
397     a = 3.14
398  enddo
399  !$omp end parallel
400
401  !ERROR: The `SAFELEN` clause cannot appear in the `SIMD` directive with `ORDER(CONCURRENT)` clause
402  !$omp simd order(concurrent) safelen(1+2)
403  do i = 1, N
404    a = 3.14
405  enddo
406
407! 2.11.1 parallel-do-clause -> parallel-clause |
408!                              do-clause
409
410  !ERROR: At most one PROC_BIND clause can appear on the PARALLEL DO directive
411  !ERROR: A modifier may not be specified in a LINEAR clause on the PARALLEL DO directive
412  !$omp parallel do proc_bind(master) proc_bind(close) linear(val(b))
413  do i = 1, N
414     a = 3.14
415  enddo
416
417! 2.8.3 do-simd-clause -> do-clause |
418!                         simd-clause
419
420  !$omp parallel
421  !ERROR: No ORDERED clause with a parameter can be specified on the DO SIMD directive
422  !ERROR: NOGROUP clause is not allowed on the DO SIMD directive
423  !ERROR: NOWAIT clause is not allowed on the OMP DO SIMD directive, use it on OMP END DO SIMD directive
424  !$omp do simd ordered(2) NOGROUP nowait
425  do i = 1, N
426     do j = 1, N
427        a = 3.14
428     enddo
429  enddo
430  !omp end do nowait
431  !$omp end parallel
432
433! 2.11.4 parallel-do-simd-clause -> parallel-clause |
434!                                   do-simd-clause
435
436  !$omp parallel do simd collapse(2) safelen(2) &
437  !$omp & simdlen(1) private(c) firstprivate(a) proc_bind(spread)
438  do i = 1, N
439     do j = 1, N
440        a = 3.14
441     enddo
442  enddo
443
444! 2.9.2 taskloop -> TASKLOOP [taskloop-clause[ [,] taskloop-clause]...]
445!       taskloop-clause -> if-clause |
446!                          shared-clause |
447!                          private-clause |
448!                          firstprivate-clause |
449!                          lastprivate-clause |
450!                          default-clause |
451!                          grainsize-clause |
452!                          num-tasks-clause |
453!                          collapse-clause |
454!                          final-clause |
455!                          priority-clause |
456!                          untied-clause |
457!                          mergeable-clause |
458!                          nogroup-clause
459
460  !$omp taskloop
461  do i = 1, N
462     a = 3.14
463  enddo
464
465  !ERROR: SCHEDULE clause is not allowed on the TASKLOOP directive
466  !$omp taskloop schedule(static)
467  do i = 1, N
468     a = 3.14
469  enddo
470
471  !ERROR: GRAINSIZE and NUM_TASKS clauses are mutually exclusive and may not appear on the same TASKLOOP directive
472  !$omp taskloop num_tasks(3) grainsize(2)
473  do i = 1,N
474     a = 3.14
475  enddo
476
477  !ERROR: At most one NUM_TASKS clause can appear on the TASKLOOP directive
478  !ERROR: TASK_REDUCTION clause is not allowed on the TASKLOOP directive
479  !$omp taskloop num_tasks(3) num_tasks(2) task_reduction(*:a)
480  do i = 1,N
481    a = 3.14
482  enddo
483
484! 2.13.1 master
485
486  !$omp parallel
487  !WARNING: OpenMP directive MASTER has been deprecated, please use MASKED instead.
488  !$omp master
489  a=3.14
490  !$omp end master
491  !$omp end parallel
492
493  !$omp parallel
494  !WARNING: OpenMP directive MASTER has been deprecated, please use MASKED instead.
495  !ERROR: NUM_THREADS clause is not allowed on the MASTER directive
496  !$omp master num_threads(4)
497  a=3.14
498  !$omp end master
499  !$omp end parallel
500
501! Standalone Directives (basic)
502
503  !$omp taskyield
504  !$omp barrier
505  !$omp taskwait
506  !ERROR: The SINK and SOURCE dependence types can only be used with the ORDERED directive, used here in the TASKWAIT construct
507  !$omp taskwait depend(source)
508  ! !$omp taskwait depend(sink:i-1)
509  ! !$omp target enter data map(to:arrayA) map(alloc:arrayB)
510  ! !$omp target update from(arrayA) to(arrayB)
511  ! !$omp target exit data map(from:arrayA) map(delete:arrayB)
512  !$omp flush (c)
513  !$omp flush acq_rel
514  !$omp flush release
515  !$omp flush acquire
516  !ERROR: If memory-order-clause is RELEASE, ACQUIRE, or ACQ_REL, list items must not be specified on the FLUSH directive
517  !$omp flush release (c)
518  !$omp flush seq_cst
519  !ERROR: RELAXED clause is not allowed on the FLUSH directive
520  !$omp flush relaxed
521
522! 2.13.2 critical Construct
523
524  ! !$omp critical (first)
525  a = 3.14
526  ! !$omp end critical (first)
527
528! 2.9.1 task-clause -> if-clause |
529!                      final-clause |
530!                      untied-clause |
531!                      default-clause |
532!                      mergeable-clause |
533!                      private-clause |
534!                      firstprivate-clause |
535!                      shared-clause |
536!                      depend-clause |
537!                      priority-clause
538
539  !$omp task shared(a) default(none) if(task:a > 1.)
540  a = 1.
541  !$omp end task
542
543  !ERROR: TASKLOOP is not a constituent of the TASK directive
544  !$omp task private(a) if(taskloop:a.eq.1)
545  a = 1.
546  !$omp end task
547
548  !ERROR: LASTPRIVATE clause is not allowed on the TASK directive
549  !ERROR: At most one FINAL clause can appear on the TASK directive
550  !$omp task lastprivate(b) final(a.GE.1) final(.false.)
551  b = 1
552  !$omp end task
553
554  !ERROR: The parameter of the PRIORITY clause must be a positive integer expression
555  !$omp task priority(-1) firstprivate(a) mergeable
556  a = 3.14
557  !$omp end task
558
559! 2.9.3 taskloop-simd-clause -> taskloop-clause |
560!                               simd-clause
561
562  !$omp taskloop simd
563  do i = 1, N
564     a = 3.14
565  enddo
566  !$omp end taskloop simd
567
568  !$omp taskloop simd reduction(+:a)
569  do i = 1, N
570     a = a + 3.14
571  enddo
572  !ERROR: Unmatched END TASKLOOP directive
573  !$omp end taskloop
574
575  !ERROR: GRAINSIZE and NUM_TASKS clauses are mutually exclusive and may not appear on the same TASKLOOP SIMD directive
576  !$omp taskloop simd num_tasks(3) grainsize(2)
577  do i = 1,N
578     a = 3.14
579  enddo
580
581  allocate(allc)
582  !ERROR: The parameter of the SIMDLEN clause must be a constant positive integer expression
583  !ERROR: The alignment value should be a constant positive integer
584  !$omp taskloop simd simdlen(-1) aligned(allc:-2)
585  do i = 1, N
586     allc = 3.14
587  enddo
588
589  !$omp target enter data map(alloc:A) device(0)
590  !$omp target exit data map(delete:A) device(0)
591
592end program
593