The original issue ICE'ed a bit earlier, but it couldn't be reproduced;
it also stopped for the user with -save-temps.

However, I could create a failing testcase for duplicate variant, ICEing
a few lines later. - Seemingly, when the feature originally landed, it
worked (the code looks to reasonable for it not to). - But current mainline
did not contain a testcase for. With this commit, there is a testcase, which
worked after a minor tweak to extracting the proper tree.

Committed as r17-756-g64be1fa670a04c and we might
consider backporting it to GCC 16.

Tobias
commit 64be1fa670a04c81d48dfdaa074d8bf8f935d746
Author: Tobias Burnus <[email protected]>
Date:   Mon May 25 13:13:46 2026 +0200

    OpenMP: Fix omp_check_for_duplicate_variant [PR125377]
    
    Follow up to r16-5508-g77b8221af8fc82; seemingly, a testcase never got
    added - and the internal representation changed afther the code was
    written. At least the previous code ICE'd for the included testcase.
    
            PR c/125377
    
    gcc/ChangeLog:
    
            * omp-general.cc (omp_check_for_duplicate_variant): Fix used
            tree for 'inform' part of the error message.
    
    gcc/testsuite/ChangeLog:
    
            * c-c++-common/gomp/begin-declare-variant-1.c: New test.
---
 gcc/omp-general.cc                                       |  2 +-
 .../c-c++-common/gomp/begin-declare-variant-1.c          | 16 ++++++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/gcc/omp-general.cc b/gcc/omp-general.cc
index 7ab9770b98f..118653c53d2 100644
--- a/gcc/omp-general.cc
+++ b/gcc/omp-general.cc
@@ -1562,7 +1562,7 @@ omp_check_for_duplicate_variant (location_t loc, tree base_decl, tree ctx)
 	  error_at (loc,
 		    "multiple definitions of variants with the same "
 		    "context selector violate the one-definition rule");
-	  inform (DECL_SOURCE_LOCATION (TREE_PURPOSE (attr)),
+	  inform (DECL_SOURCE_LOCATION (TREE_PURPOSE (TREE_VALUE (attr))),
 		  "previous variant declaration here");
 	  return false;
 	}
diff --git a/gcc/testsuite/c-c++-common/gomp/begin-declare-variant-1.c b/gcc/testsuite/c-c++-common/gomp/begin-declare-variant-1.c
new file mode 100644
index 00000000000..7301dee28c0
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/begin-declare-variant-1.c
@@ -0,0 +1,16 @@
+/* PR c/125377 */
+
+static int omp_is_initial_device(void);
+// { dg-warning "'omp_is_initial_device' used but never defined" "" { target c } .-1 }
+#pragma omp begin declare variant match(device={kind(host)})
+    static inline int omp_is_initial_device(void) { return 1; }  // { dg-note "previous variant declaration here" }
+#pragma omp end declare variant
+
+#pragma omp begin declare variant match(device={kind(host)})
+    static inline int omp_is_initial_device(void) { return 1; } // { dg-error "multiple definitions of variants with the same context selector violate the one-definition rule" }
+#pragma omp end declare variant
+
+int main()
+{
+  __builtin_printf("%d\n", omp_is_initial_device());
+}

Reply via email to