]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
OpenMP: Check additional restrictions on context selector properties
authorSandra Loosemore <sloosemore@baylibre.com>
Fri, 6 Sep 2024 20:58:13 +0000 (20:58 +0000)
committerSandra Loosemore <sloosemore@baylibre.com>
Tue, 24 Sep 2024 15:04:08 +0000 (15:04 +0000)
TR13 (pre-6.0) of the OpenMP spec says:

"Each trait-property may only be specified once in a trait selector
other than those in the construct selector set."

and

"If trait-property any is specified in the kind trait-selector of the
device selector set or the target_device selector sets, no other
trait-property may be specified in the same selector set."

These restrictions (with slightly different wording) date back to
OpenMP 5.1, but were not in 5.0 which was the basis for GCC's
implementation.

This patch adds a diagnostic, adds new testcases, and fixes some older
testcases that include now-invalid selectors.

gcc/ChangeLog
* omp-general.cc (omp_check_context_selector): Reject other
properties in the same selector set with kind(any).  Also reject
duplicate name-list properties.

gcc/testsuite/ChangeLog
* c-c++-common/gomp/declare-variant-10.c: Fix broken tests.
* c-c++-common/gomp/declare-variant-3.c: Likewise.
* c-c++-common/gomp/declare-variant-9.c: Likewise.
* c-c++-common/gomp/declare-variant-any.c: New.
* c-c++-common/gomp/declare-variant-duplicates.c: New.
* gfortran.dg/gomp/declare-variant-10.f90: Fix broken tests.
* gfortran.dg/gomp/declare-variant-3.f90: Likewise.
* gfortran.dg/gomp/declare-variant-9.f90: Likewise.
* gfortran.dg/gomp/declare-variant-any.f90: New.
* gfortran.dg/gomp/declare-variant-duplicates.f90: New.

gcc/omp-general.cc
gcc/testsuite/c-c++-common/gomp/declare-variant-10.c
gcc/testsuite/c-c++-common/gomp/declare-variant-3.c
gcc/testsuite/c-c++-common/gomp/declare-variant-9.c
gcc/testsuite/c-c++-common/gomp/declare-variant-any.c [new file with mode: 0644]
gcc/testsuite/c-c++-common/gomp/declare-variant-duplicates.c [new file with mode: 0644]
gcc/testsuite/gfortran.dg/gomp/declare-variant-10.f90
gcc/testsuite/gfortran.dg/gomp/declare-variant-3.f90
gcc/testsuite/gfortran.dg/gomp/declare-variant-9.f90
gcc/testsuite/gfortran.dg/gomp/declare-variant-any.f90 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/gomp/declare-variant-duplicates.f90 [new file with mode: 0644]

index c93bf129e4d789249479755100e69056debcf923..9713e684e830a271ad1fc3fac7cf77bba36fdc7d 100644 (file)
@@ -1293,6 +1293,8 @@ omp_check_context_selector (location_t loc, tree ctx)
   for (tree tss = ctx; tss; tss = TREE_CHAIN (tss))
     {
       enum omp_tss_code tss_code = OMP_TSS_CODE (tss);
+      bool saw_any_prop = false;
+      bool saw_other_prop = false;
 
       /* We can parse this, but not handle it yet.  */
       if (tss_code == OMP_TRAIT_SET_TARGET_DEVICE)
@@ -1329,9 +1331,61 @@ omp_check_context_selector (location_t loc, tree ctx)
          else
            ts_seen[ts_code] = true;
 
+         /* If trait-property "any" is specified in the "kind"
+            trait-selector of the "device" selector set or the
+            "target_device" selector sets, no other trait-property
+            may be specified in the same selector set.  */
+         if (ts_code == OMP_TRAIT_DEVICE_KIND)
+           for (tree p = OMP_TS_PROPERTIES (ts); p; p = TREE_CHAIN (p))
+             {
+               const char *prop = omp_context_name_list_prop (p);
+               if (!prop)
+                 continue;
+               else if (strcmp (prop, "any") == 0)
+                 saw_any_prop = true;
+               else
+                 saw_other_prop = true;
+             }
+         /* It seems slightly suspicious that the spec's language covers
+            the device_num selector too, but
+              target_device={device_num(whatever),kind(any)}
+            is probably not terribly useful anyway.  */
+         else if (ts_code == OMP_TRAIT_DEVICE_ARCH
+                  || ts_code == OMP_TRAIT_DEVICE_ISA
+                  || ts_code == OMP_TRAIT_DEVICE_NUM)
+           saw_other_prop = true;
+
+         /* Each trait-property can only be specified once in a trait-selector
+            other than the construct selector set.  FIXME: only handles
+            name-list properties, not clause-list properties, since the
+            "requires" selector is not implemented yet (PR 113067).  */
+         if (tss_code != OMP_TRAIT_SET_CONSTRUCT)
+           for (tree p1 = OMP_TS_PROPERTIES (ts); p1; p1 = TREE_CHAIN (p1))
+             {
+               if (OMP_TP_NAME (p1) != OMP_TP_NAMELIST_NODE)
+                 break;
+               const char *n1 = omp_context_name_list_prop (p1);
+               if (!n1)
+                 continue;
+               for (tree p2 = TREE_CHAIN (p1); p2; p2 = TREE_CHAIN (p2))
+                 {
+                   const char *n2 = omp_context_name_list_prop (p2);
+                   if (!n2)
+                     continue;
+                   if (!strcmp (n1, n2))
+                     {
+                       error_at (loc,
+                                 "trait-property %qs specified more "
+                                 "than once in %qs selector",
+                                 n1, OMP_TS_NAME (ts));
+                       return error_mark_node;
+                     }
+                 }
+             }
+
+         /* Check for unknown properties.  */
          if (omp_ts_map[ts_code].valid_properties == NULL)
            continue;
-
          for (tree p = OMP_TS_PROPERTIES (ts); p; p = TREE_CHAIN (p))
            for (unsigned j = 0; ; j++)
              {
@@ -1381,6 +1435,14 @@ omp_check_context_selector (location_t loc, tree ctx)
                  break;
              }
        }
+
+      if (saw_any_prop && saw_other_prop)
+       {
+         error_at (loc,
+                   "no other trait-property may be specified "
+                   "in the same selector set with %<kind(\"any\")%>");
+         return error_mark_node;
+       }
     }
   return ctx;
 }
index 2b8a39425b13c0b0dba6599e7001435efc81015d..e77693430d1a41f9e0c90c45e58567df8a03d31f 100644 (file)
@@ -7,7 +7,7 @@ void f01 (void);
 #pragma omp declare variant (f01) match (device={isa(avx512f,avx512bw)})
 void f02 (void);
 void f03 (void);
-#pragma omp declare variant (f03) match (device={kind("any"),arch(x86_64),isa(avx512f,avx512bw)})
+#pragma omp declare variant (f03) match (device={arch(x86_64),isa(avx512f,avx512bw)})
 void f04 (void);
 void f05 (void);
 #pragma omp declare variant (f05) match (device={kind(gpu)})
@@ -28,7 +28,7 @@ void f15 (void);
 #pragma omp declare variant (f15) match (device={isa(sse4,ssse3),arch(i386)})
 void f16 (void);
 void f17 (void);
-#pragma omp declare variant (f17) match (device={kind(any,fpga)})
+#pragma omp declare variant (f17) match (device={kind(fpga)})
 void f18 (void);
 
 #pragma omp declare target
index f5d7797f45840889375a3756b78ac0200e90767f..0d772d7aaabaa0ffea8a704debb8f86a3dff745f 100644 (file)
@@ -29,13 +29,13 @@ void f17 (void);
 void f18 (void);
 #pragma omp declare variant (f13) match (device={kind(fpga)})
 void f19 (void);
-#pragma omp declare variant (f13) match (device={kind(any,any)})
+#pragma omp declare variant (f13) match (device={kind(any)})
 void f20 (void);
 #pragma omp declare variant (f13) match (device={kind(host,nohost)})
 void f21 (void);
 #pragma omp declare variant (f13) match (device={kind("cpu","gpu","fpga")})
 void f22 (void);
-#pragma omp declare variant (f13) match (device={kind(any,cpu,nohost)})
+#pragma omp declare variant (f13) match (device={kind(cpu,nohost)})
 void f23 (void);
 #pragma omp declare variant (f13) match (device={isa(avx)})
 void f24 (void);
@@ -139,12 +139,8 @@ void f72 (void);
 void f73 (void);
 #pragma omp declare variant (f13) match (user={condition(score(25):1)})
 void f74 (void);
-#pragma omp declare variant (f13) match (device={kind(any,"any")})
+#pragma omp declare variant (f13) match (device={kind("any")})
 void f75 (void);
-#pragma omp declare variant (f13) match (device={kind("any","any")})
-void f76 (void);
-#pragma omp declare variant (f13) match (device={kind("any",any)})
-void f77 (void);
 #pragma omp declare variant (f13) match (implementation={vendor(nvidia)})
 void f78 (void);
 #pragma omp declare variant (f13) match (user={condition(score(0):0)})
index 5ee75892f2deb3f787b45d3b113b0265a8ef4194..da96c81eb6fad284986a06d9237a42a99ea17b1d 100644 (file)
@@ -7,7 +7,7 @@ void f01 (void);
 #pragma omp declare variant (f01) match (device={isa("avx512f",avx512bw)})
 void f02 (void);
 void f03 (void);
-#pragma omp declare variant (f03) match (device={kind(any),arch(x86_64),isa("avx512f","avx512bw")})
+#pragma omp declare variant (f03) match (device={arch(x86_64),isa("avx512f","avx512bw")})
 void f04 (void);
 void f05 (void);
 #pragma omp declare variant (f05) match (device={kind(gpu)})
@@ -28,7 +28,7 @@ void f15 (void);
 #pragma omp declare variant (f15) match (device={isa(sse4,ssse3),arch(i386)})
 void f16 (void);
 void f17 (void);
-#pragma omp declare variant (f17) match (device={kind("any","fpga")})
+#pragma omp declare variant (f17) match (device={kind("fpga")})
 void f18 (void);
 
 void
diff --git a/gcc/testsuite/c-c++-common/gomp/declare-variant-any.c b/gcc/testsuite/c-c++-common/gomp/declare-variant-any.c
new file mode 100644 (file)
index 0000000..cddf73b
--- /dev/null
@@ -0,0 +1,17 @@
+/* Check that errors are detected if other trait-properties are given with
+   kind(any).  */
+
+extern int f1 (int);
+extern int f2 (int);
+extern int f3 (int);
+extern int f4 (int);
+extern int f5 (int);
+extern int f6 (int);
+
+#pragma omp declare variant (f1) match (device={kind(any,gpu)})  /* { dg-error "no other trait-property may be specified" } */
+#pragma omp declare variant (f2) match (device={kind(cpu,"any")})  /* { dg-error "no other trait-property may be specified" } */
+#pragma omp declare variant (f3) match (device={kind("any"),arch(x86_64)})  /* { dg-error "no other trait-property may be specified" } */
+#pragma omp declare variant (f4) match (device={arch(x86_64),kind(any)})  /* { dg-error "no other trait-property may be specified" } */
+#pragma omp declare variant (f5) match (device={kind(any,"any")})  /* { dg-error "trait-property .any. specified more than once" } */
+#pragma omp declare variant (f6) match (device={kind("any",any)})  /* { dg-error "trait-property .any. specified more than once" } */
+int f (int);
diff --git a/gcc/testsuite/c-c++-common/gomp/declare-variant-duplicates.c b/gcc/testsuite/c-c++-common/gomp/declare-variant-duplicates.c
new file mode 100644 (file)
index 0000000..47d34fc
--- /dev/null
@@ -0,0 +1,13 @@
+/* Check that errors are detected if duplicates appear in name-list
+   properties.  */
+
+extern int f1 (int);
+extern int f2 (int);
+extern int f3 (int);
+extern int f4 (int);
+
+#pragma omp declare variant (f1) match (device={kind(cpu,gpu,"cpu")})  /* { dg-error "trait-property .cpu. specified more than once" } */
+#pragma omp declare variant (f2) match (device={isa(sse4,"avx",avx)})  /* { dg-error "trait-property .avx. specified more than once" } */
+#pragma omp declare variant (f3) match (device={arch(x86_64,i386,aarch64,"i386")})  /* { dg-error "trait-property .i386. specified more than once" } */
+#pragma omp declare variant (f4) match (implementation={vendor(llvm,gnu,"arm",gnu)})  /* { dg-error "trait-property .gnu. specified more than once" } */
+int f (int);
index 2f09146a10d2b6f1c0bab44cdbd29ee72250446e..01f59c528087aed7575562792987c96318fc4ed7 100644 (file)
@@ -15,7 +15,7 @@ contains
   subroutine f03 ()
   end subroutine
   subroutine f04 ()
-    !$omp declare variant (f03) match (device={kind("any"),arch(x86_64),isa(avx512f,avx512bw)})
+    !$omp declare variant (f03) match (device={arch(x86_64),isa(avx512f,avx512bw)})
   end subroutine
   subroutine f05 ()
   end subroutine
@@ -50,7 +50,7 @@ contains
   subroutine f17 ()
   end subroutine
   subroutine f18 ()
-    !$omp declare variant (f17) match (device={kind(any,fpga)})
+    !$omp declare variant (f17) match (device={kind(fpga)})
   end subroutine
 
   subroutine test1 ()
index 90674281f226ac1a8525727c9b978e3f8cb154dc..891f1d282a53ce1761809fecf4278855948004d8 100644 (file)
@@ -51,7 +51,7 @@ contains
     !$omp declare variant (f13) match (device={kind(fpga)})
   end subroutine
   subroutine f20 ()
-    !$omp declare variant (f13) match (device={kind(any,any)})
+    !$omp declare variant (f13) match (device={kind(any)})
   end subroutine
   subroutine f21 ()
     !$omp declare variant (f13) match (device={kind(host,nohost)})
@@ -60,7 +60,7 @@ contains
     !$omp declare variant (f13) match (device={kind("cpu","gpu","fpga")})
   end subroutine
   subroutine f23 ()
-    !$omp declare variant (f13) match (device={kind(any,cpu,nohost)})
+    !$omp declare variant (f13) match (device={kind(cpu,nohost)})
   end subroutine
   subroutine f24 ()
     !$omp declare variant (f13) match (device={isa(avx)})
@@ -222,13 +222,7 @@ contains
     !$omp declare variant (f13) match (user={condition(score(25):.true.)})
   end subroutine
   subroutine f75 ()
-    !$omp declare variant (f13) match (device={kind(any,"any")})
-  end subroutine
-  subroutine f76 ()
-    !$omp declare variant (f13) match (device={kind("any","any")})
-  end subroutine
-  subroutine f77 ()
-    !$omp declare variant (f13) match (device={kind("any",any)})
+    !$omp declare variant (f13) match (device={kind("any")})
   end subroutine
   subroutine f78 ()
     !$omp declare variant (f13) match (implementation={vendor(nvidia)})
index ebd066609f3a7d20f96d6446db4c5ffc5dcbae3b..297bff97d5e0c74c5e8848303737df79242d2b89 100644 (file)
@@ -38,7 +38,7 @@ contains
   subroutine f17 ()
   end subroutine
   subroutine f18 ()
-    !$omp declare variant (f17) match (device={kind("any","fpga")})
+    !$omp declare variant (f17) match (device={kind("fpga")})
   end subroutine
 
   subroutine test1 ()
diff --git a/gcc/testsuite/gfortran.dg/gomp/declare-variant-any.f90 b/gcc/testsuite/gfortran.dg/gomp/declare-variant-any.f90
new file mode 100644 (file)
index 0000000..c22e27c
--- /dev/null
@@ -0,0 +1,40 @@
+! Check that errors are detected if other trait-properties are given with
+! kind(any).
+
+integer function f1 (x)
+  integer, intent(in) :: x
+  f1 = x + 1
+end function
+integer function f2 (x)
+  integer, intent(in) :: x
+  f2 = x + 2
+end function
+integer function f3 (x)
+  integer, intent(in) :: x
+  f3 = x + 3
+end function
+integer function f4 (x)
+  integer, intent(in) :: x
+  f4 = x + 4
+end function
+integer function f5 (x)
+  integer, intent(in) :: x
+  f4 = x + 5
+end function
+integer function f6 (x)
+  integer, intent(in) :: x
+  f4 = x + 6
+end function
+
+integer function f (x)
+  integer, intent(in) :: x
+
+  !$omp declare variant (f1) match (device={kind(any,gpu)})  ! { dg-error "no other trait-property may be specified" }
+  !$omp declare variant (f2) match (device={kind(cpu,"any")})  ! { dg-error "no other trait-property may be specified" }
+  !$omp declare variant (f3) match (device={kind("any"),arch(x86_64)})  ! { dg-error "no other trait-property may be specified" }
+  !$omp declare variant (f4) match (device={arch(x86_64),kind(any)})  ! { dg-error "no other trait-property may be specified" }
+  !$omp declare variant (f5) match (device={kind(any,"any")})  ! { dg-error "trait-property .any. specified more than once" }
+  !$omp declare variant (f6) match (device={kind("any",any)})  ! { dg-error "trait-property .any. specified more than once" }
+
+  f = x
+end function  
diff --git a/gcc/testsuite/gfortran.dg/gomp/declare-variant-duplicates.f90 b/gcc/testsuite/gfortran.dg/gomp/declare-variant-duplicates.f90
new file mode 100644 (file)
index 0000000..16dc085
--- /dev/null
@@ -0,0 +1,30 @@
+! Check that errors are detected if duplicates appear in name-list
+! properties.
+
+integer function f1 (x)
+  integer, intent(in) :: x
+  f1 = x + 1
+end function
+integer function f2 (x)
+  integer, intent(in) :: x
+  f2 = x + 2
+end function
+integer function f3 (x)
+  integer, intent(in) :: x
+  f3 = x + 3
+end function
+integer function f4 (x)
+  integer, intent(in) :: x
+  f4 = x + 4
+end function
+
+integer function f (x)
+  integer, intent(in) :: x
+
+  !$omp declare variant (f1) match (device={kind(cpu,gpu,"cpu")})  ! { dg-error "trait-property .cpu. specified more than once" }
+  !$omp declare variant (f2) match (device={isa(sse4,"avx",avx)})  ! { dg-error "trait-property .avx. specified more than once" }
+  !$omp declare variant (f3) match (device={arch(x86_64,i386,aarch64,"i386")})  ! { dg-error "trait-property .i386. specified more than once" }
+  !$omp declare variant (f4) match (implementation={vendor(llvm,gnu,"arm",gnu)})  ! { dg-error "trait-property .gnu. specified more than once" }
+
+  f = x
+end function