On Wed, Jun 19, 2024 at 07:32:28PM +0200, Jakub Jelinek wrote:
> Ok, I've tried that, but that doesn't work, it ICEs on the
> pr114574-2.c testcase.

The following works on quick testing of dg.exp=pr11[45]*.c
but haven't bootstrapped/regtested it yet.

2024-06-19  Jakub Jelinek  <ja...@redhat.com>
            Martin Uecker  <uec...@tugraz.at>

        PR c/114930
        PR c/115502
gcc/c/
        * c-decl.cc (c_update_type_canonical): Assert t is main variant
        with 0 TYPE_QUALS.  Simplify and don't use check_qualified_type.
        Deal with the case where build_qualified_type returns
        TYPE_STRUCTURAL_EQUALITY_P type.
gcc/testsuite/
        * gcc.dg/pr114574-1.c: Require lto effective target.
        * gcc.dg/pr114574-2.c: Likewise.
        * gcc.dg/pr114930.c: New test.
        * gcc.dg/pr115502.c: New test.

--- gcc/c/c-decl.cc.jj  2024-06-07 12:17:09.582969919 +0200
+++ gcc/c/c-decl.cc     2024-06-19 19:59:24.955836263 +0200
@@ -9367,18 +9367,44 @@ is_flexible_array_member_p (bool is_last
 static void
 c_update_type_canonical (tree t)
 {
-  for (tree x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
+  gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t && !TYPE_QUALS (t));
+  for (tree x = t, l = NULL_TREE; x; l = x, x = TYPE_NEXT_VARIANT (x))
     {
       if (x != t && TYPE_STRUCTURAL_EQUALITY_P (x))
        {
-         if (TYPE_QUALS (x) == TYPE_QUALS (t))
+         if (!TYPE_QUALS (x))
            TYPE_CANONICAL (x) = TYPE_CANONICAL (t);
-         else if (TYPE_CANONICAL (t) != t
-                  || check_qualified_type (x, t, TYPE_QUALS (x)))
-           TYPE_CANONICAL (x)
-             = build_qualified_type (TYPE_CANONICAL (t), TYPE_QUALS (x));
          else
-           TYPE_CANONICAL (x) = x;
+           {
+             tree
+               c = build_qualified_type (TYPE_CANONICAL (t), TYPE_QUALS (x));
+             if (TYPE_STRUCTURAL_EQUALITY_P (c))
+               {
+                 gcc_checking_assert (TYPE_CANONICAL (t) == t);
+                 if (c == x)
+                   TYPE_CANONICAL (x) = x;
+                 else
+                   {
+                     /* build_qualified_type for this function unhelpfully
+                        moved c from some later spot in TYPE_MAIN_VARIANT (t)
+                        chain to right after t (or created it there).  Move
+                        it right before x and process c and then x.  */
+                     gcc_checking_assert (TYPE_NEXT_VARIANT (t) == c);
+                     if (l == t)
+                       x = t;
+                     else
+                       {
+                         TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (c);
+                         TYPE_NEXT_VARIANT (l) = c;
+                         TYPE_NEXT_VARIANT (c) = x;
+                         x = l;
+                       }
+                     continue;
+                   }
+               }
+             else
+               TYPE_CANONICAL (x) = TYPE_CANONICAL (c);
+           }
        }
       else if (x != t)
        continue;
--- gcc/testsuite/gcc.dg/pr114574-1.c.jj        2024-04-20 00:05:07.273690453 
+0200
+++ gcc/testsuite/gcc.dg/pr114574-1.c   2024-06-19 20:00:33.015984692 +0200
@@ -1,6 +1,6 @@
-/* PR lto/114574
- * { dg-do compile }
- * { dg-options "-flto" } */
+/* PR lto/114574 */
+/* { dg-do compile { target lto } } */
+/* { dg-options "-flto" } */
 
 const struct S * x;
 struct S {};
--- gcc/testsuite/gcc.dg/pr114574-2.c.jj        2024-04-20 00:05:07.274690440 
+0200
+++ gcc/testsuite/gcc.dg/pr114574-2.c   2024-06-19 20:00:55.084704278 +0200
@@ -1,6 +1,6 @@
-/* PR lto/114574
- * { dg-do compile }
- * { dg-options "-flto -std=c23" } */
+/* PR lto/114574 */
+/* { dg-do compile { target lto } } */
+/* { dg-options "-flto -std=c23" } */
 
 const struct S * x;
 struct S {};
--- gcc/testsuite/gcc.dg/pr114930.c.jj  2024-06-18 21:27:53.782729543 +0200
+++ gcc/testsuite/gcc.dg/pr114930.c     2024-06-18 21:27:53.782729543 +0200
@@ -0,0 +1,9 @@
+/* PR c/114930 */
+/* { dg-do compile { target lto } } */
+/* { dg-options "-std=c23 -flto" } */
+
+typedef struct WebPPicture WebPPicture;
+typedef int (*WebPProgressHook)(const WebPPicture *);
+WebPProgressHook progress_hook;
+struct WebPPicture {
+} WebPGetColorPalette(const struct WebPPicture *);
--- gcc/testsuite/gcc.dg/pr115502.c.jj  2024-06-18 21:27:53.793729408 +0200
+++ gcc/testsuite/gcc.dg/pr115502.c     2024-06-18 21:27:53.793729408 +0200
@@ -0,0 +1,9 @@
+/* PR c/115502 */
+/* { dg-do compile { target lto } } */
+/* { dg-options "-std=c23 -flto" } */
+
+typedef struct _OSet OSet;
+typedef OSet AvlTree;
+void vgPlain_OSetGen_Lookup(const OSet *);
+struct _OSet {};
+void vgPlain_OSetGen_Lookup(const AvlTree *);


        Jakub

Reply via email to