From patchwork Thu Jun 20 07:36:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1949946 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=YTZkOCsv; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4W4XPR4WCSz20KL for ; Thu, 20 Jun 2024 17:36:58 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7AC433888C71 for ; Thu, 20 Jun 2024 07:36:56 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 65EB83888C4D for ; Thu, 20 Jun 2024 07:36:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 65EB83888C4D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 65EB83888C4D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718868999; cv=none; b=GTtx0wszdsDeVSZ3pLs0CpYVkbVjCoh9QVEK6jT3VZ+ADCUNA6XoHQ0cWXJmQY3dyHFGPjcmbThKN2olnwcNNGw1mVBsx18Sip5aipp1BgqmbkTizqnnDfD9f0cssaGuDu7cvWwksmTbpi4P55hJzyNAbzEp8Vbh5C826OZeunM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718868999; c=relaxed/simple; bh=wia8SgJBWQizPvqqoqPTBYA5YFmi9n8pBqCOELtLOrM=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=Oa+KyPMHWDgFRFea3Asr/+AmJyh7eoHe+nlGQ4EvJCbNEKwB9jh7/AsbbV9rraN1saKJBsFNSgo67wUNj4ZiEwuXCPXhjkIhXTyB1pcuvde/vLc+xzG7uH5tsw75KODXMp3A+fYFMHS60pVS3NtZc95QfGS5S3tppj8Dps/4QYM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1718868997; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=NobZ7qmmwAlIlzEyPEr3/qFoNfOxOV/f3lpZziGjgHg=; b=YTZkOCsvwc5WMial33xHG6WxhiHxgo7m15yVGIsWcRyT4TCqrfF1XVO18yWSzT3JvkoToN sxFIUfbD4Kol9hAWd8lB10/woVQ+oxQTBWJ+kNLqy0+7kfugF55VANco75VnreNbrsLhup UDimD/zORxKP4ftVpUGaxJ2KzAtrqrE= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-670-B0o6cofNMjmlkeDSV-Dqfg-1; Thu, 20 Jun 2024 03:36:33 -0400 X-MC-Unique: B0o6cofNMjmlkeDSV-Dqfg-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 2FFF019560BD; Thu, 20 Jun 2024 07:36:32 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.7]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 15EAC19560AF; Thu, 20 Jun 2024 07:36:30 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 45K7aRGJ1794528 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 20 Jun 2024 09:36:28 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 45K7aRd21794527; Thu, 20 Jun 2024 09:36:27 +0200 Date: Thu, 20 Jun 2024 09:36:27 +0200 From: Jakub Jelinek To: "Joseph S. Myers" , Marek Polacek Cc: gcc-patches@gcc.gnu.org, Martin Uecker , Richard Biener Subject: [PATCH] c, v3: Fix ICE related to incomplete structures in C23 [PR114930] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-1.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! Here is a version of the c_update_type_canonical fixes which passed bootstrap/regtest. The non-trivial part is the handling of the case when build_qualified_type (TYPE_CANONICAL (t), TYPE_QUALS (x)) returns a type with NULL TYPE_CANONICAL. That should happen only if TYPE_CANONICAL (t) == t, because otherwise c_update_type_canonical should have been already called on the other type. c, the returned type, is usually x and in that case it should have TYPE_CANONICAL set to itself, or worst for whatever reason x is not the right canonical type (say it has attributes or whatever disqualifies it from check_qualified_type). In that case either it finds some pre-existing type from the variant chain of t which is later in the chain and we haven't processed it yet (but then get_qualified_type moves it right after t in: /* Put the found variant at the head of the variant list so frequently searched variants get found faster. The C++ FE benefits greatly from this. */ tree t = *tp; *tp = TYPE_NEXT_VARIANT (t); TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (mv); TYPE_NEXT_VARIANT (mv) = t; return t; optimization), or creates a fresh new type using build_variant_type_copy, which again places the new type right after t: /* Add the new type to the chain of variants of TYPE. */ TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m); TYPE_NEXT_VARIANT (m) = t; TYPE_MAIN_VARIANT (t) = m; At this point we want to make c its own canonical type (i.e. TYPE_CANONICAL (c) = c;), but also need to process pointers to it and only then return back to processing x. Processing the whole chain from c again could be costly, we could have hundreds of types in the chain already processed, and while the loop would just quickly skip them for (tree x = t, l = NULL_TREE; x; l = x, x = TYPE_NEXT_VARIANT (x)) { if (x != t && TYPE_STRUCTURAL_EQUALITY_P (x)) ... else if (x != t) continue; it feels costly. So, this patch instead moves c from right after t to right before x in the chain (that shouldn't change anything, because clearly build_qualified_type didn't find any matches in the chain before x) and continues processing the c at that position, so should handle the x that encountered this in the next iteration. We could avoid some of the moving in the chain if we processed the chain twice, once deal only with x != t && TYPE_STRUCTURAL_EQUALITY_P (x) && TYPE_CANONICAL (t) == t && check_qualified_type (t, x, TYPE_QUALS (x)) types (in that case set TYPE_CANONICAL (x) = x) and once the rest. There is still the theoretical case where build_qualified_type would return a new type and in that case we are back to the moving the type around and needing to handle it though. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2024-06-19 Jakub Jelinek Martin Uecker 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. Jakub --- gcc/c/c-decl.cc.jj 2024-06-07 12:17:09.582969919 +0200 +++ gcc/c/c-decl.cc 2024-06-19 21:06:59.863496408 +0200 @@ -9367,18 +9367,42 @@ 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) + { + TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (c); + TYPE_NEXT_VARIANT (l) = c; + TYPE_NEXT_VARIANT (c) = x; + } + TYPE_CANONICAL (c) = c; + x = c; + } + } + 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 *);