From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mailrelay.tugraz.at (mailrelay.tugraz.at [129.27.2.202]) by sourceware.org (Postfix) with ESMTPS id CA3803845BC1 for ; Fri, 3 May 2024 15:32:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CA3803845BC1 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=tugraz.at Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tugraz.at ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CA3803845BC1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=129.27.2.202 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714750347; cv=none; b=avz8qvyIgvMU6WGq4KEO462K6498uKIBwiw31jpKRuV90aIIbDvI+GW+eupw6tAna1WCbxfelGC3waizHv7Z/yUTBGkfn7GJm7pZ1ThjM7QpxqttJk3Lj2g0wz7uWecz4oYMmNnABDu8hajC+MTh1+njLfGZBQjyDtqaTw8Awj8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714750347; c=relaxed/simple; bh=yNy4kjWMw/UQ6zNYzkjrp/ZTi3bUq7LJGFeIiCRUmbI=; h=DKIM-Signature:Message-ID:Subject:From:To:Date:MIME-Version; b=iH+U8s++Sq84+BLGFA1P9fiJ/U70JvqqTG0RIZ741MydVIqSoNtCbMBUM+nvxPi6PzYEJOVWbJGr2d7QzHCgKKc343bJKUg2W3bWUorYBCtHbPzL88OD+xWmM3ScFOhDwJj3RdnFCY6XP0NIKnMbDwBh1N161QHX42uokE1/bxI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from [192.168.0.221] (84-115-223-216.cable.dynamic.surfer.at [84.115.223.216]) by mailrelay.tugraz.at (Postfix) with ESMTPSA id 4VWFCx2zmtz1HNLp; Fri, 3 May 2024 17:32:13 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 mailrelay.tugraz.at 4VWFCx2zmtz1HNLp DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tugraz.at; s=mailrelay; t=1714750333; bh=EwLMEbX1RuHZhf/0O+UHgKdXudEAFk3CJnl2wcuj9sk=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=HzfQB1fUyUmquOxjhmwVXcO5JWL7Z79YXQD+fXFuXG6jKrwogOHUbBfE49uwbXwWn CJON7DPCV4RwNPXwladDa91rgddNHFX1XdA+dPEQb8hbiYxhOYOJ/3T3AblcT3W7tb FMeYi83hlKuRMNQ2ISLx/KQh82/XN9TyJapfORiw= Message-ID: <3616afcc455c1f80e2bb8a80a408994a67062d85.camel@tugraz.at> Subject: Re: [PATCH] middle-end/114931 - type_hash_canon and structual equality types From: Martin Uecker To: Richard Biener , gcc-patches@gcc.gnu.org Cc: Jakub Jelinek Date: Fri, 03 May 2024 17:32:12 +0200 In-Reply-To: <20240503121400.C8A4313991@imap1.dmz-prg2.suse.org> References: <20240503121400.C8A4313991@imap1.dmz-prg2.suse.org> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.46.4-2 MIME-Version: 1.0 X-TUG-Backscatter-control: G/VXY7/6zeyuAY/PU2/0qw X-Spam-Scanner: SpamAssassin 3.003001 X-Spam-Score-relay: -1.9 X-Scanned-By: MIMEDefang 2.74 on 129.27.10.117 X-Spam-Status: No, score=-8.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_ABUSEAT,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Am Freitag, dem 03.05.2024 um 14:13 +0200 schrieb Richard Biener: > TYPE_STRUCTURAL_EQUALITY_P is part of our type system so we have > to make sure to include that into the type unification done via > type_hash_canon. This requires the flag to be set before querying > the hash which is the biggest part of the patch. I assume this does not affect structs / unions because they do not make this mechanism of type unification (each tagged type is a unique type), but only derived types that end up having TYPE_STRUCTURAL_EQUALITY_P because they are constructed from incomplete structs / unions before TYPE_CANONICAL is set. I do not yet understand why this change is needed. Type identity should not be affected by setting TYPE_CANONICAL, so why do we need to keep such types separate? I understand that we created some inconsistencies, but I do not see why this change is needed to fix it. But I also haven't understood how we ended up with a TYPE_CANONICAL having TYPE_STRUCTURAL_EQUALITY_P in PR 114931 ... Martin >=20 > Bootstrapped and tested on x86_64-unknown-linux-gnu for all languages. >=20 > As said in the PR this merely makes sure to keep individual types > consistent with themselves. We still will have a set of types > with TYPE_STRUCTURAL_EQUALITY_P and a set without that might be > otherwise identical. That could be only avoided with changes in > the frontend. >=20 > OK for trunk? >=20 > Thanks, > Richard. >=20 > PR middle-end/114931 > gcc/ > * tree.cc (type_hash_canon_hash): Hash TYPE_STRUCTURAL_EQUALITY_P. > (type_cache_hasher::equal): Compare TYPE_STRUCTURAL_EQUALITY_P. > (build_array_type_1): Set TYPE_STRUCTURAL_EQUALITY_P before > probing with type_hash_canon. > (build_function_type): Likewise. > (build_method_type_directly): Likewise. > (build_offset_type): Likewise. > (build_complex_type): Likewise. > * attribs.cc (build_type_attribute_qual_variant): Likewise. >=20 > gcc/c-family/ > * c-common.cc (complete_array_type): Set TYPE_STRUCTURAL_EQUALITY_P > before probing with type_hash_canon. >=20 > gcc/testsuite/ > * gcc.dg/pr114931.c: New testcase. > --- > gcc/attribs.cc | 20 +++++----- > gcc/c-family/c-common.cc | 11 ++++-- > gcc/testsuite/gcc.dg/pr114931.c | 10 +++++ > gcc/tree.cc | 65 +++++++++++++++++++++++---------- > 4 files changed, 74 insertions(+), 32 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/pr114931.c >=20 > diff --git a/gcc/attribs.cc b/gcc/attribs.cc > index 12ffc5f170a..3ab0b0fd87a 100644 > --- a/gcc/attribs.cc > +++ b/gcc/attribs.cc > @@ -1336,6 +1336,16 @@ build_type_attribute_qual_variant (tree otype, tre= e attribute, int quals) > tree dtype =3D ntype =3D build_distinct_type_copy (ttype); > =20 > TYPE_ATTRIBUTES (ntype) =3D attribute; > + /* If the target-dependent attributes make NTYPE different from > + its canonical type, we will need to use structural equality > + checks for this type. > + > + We shouldn't get here for stripping attributes from a type; > + the no-attribute type might not need structural comparison. But > + we can if was discarded from type_hash_table. */ > + if (TYPE_STRUCTURAL_EQUALITY_P (ttype) > + || !comp_type_attributes (ntype, ttype)) > + SET_TYPE_STRUCTURAL_EQUALITY (ntype); > =20 > hashval_t hash =3D type_hash_canon_hash (ntype); > ntype =3D type_hash_canon (hash, ntype); > @@ -1343,16 +1353,6 @@ build_type_attribute_qual_variant (tree otype, tre= e attribute, int quals) > if (ntype !=3D dtype) > /* This variant was already in the hash table, don't mess with > TYPE_CANONICAL. */; > - else if (TYPE_STRUCTURAL_EQUALITY_P (ttype) > - || !comp_type_attributes (ntype, ttype)) > - /* If the target-dependent attributes make NTYPE different from > - its canonical type, we will need to use structural equality > - checks for this type. > - > - We shouldn't get here for stripping attributes from a type; > - the no-attribute type might not need structural comparison. But > - we can if was discarded from type_hash_table. */ > - SET_TYPE_STRUCTURAL_EQUALITY (ntype); > else if (TYPE_CANONICAL (ntype) =3D=3D ntype) > TYPE_CANONICAL (ntype) =3D TYPE_CANONICAL (ttype); > =20 > diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc > index 01e3d247fc2..032dcb4b41d 100644 > --- a/gcc/c-family/c-common.cc > +++ b/gcc/c-family/c-common.cc > @@ -7115,6 +7115,13 @@ complete_array_type (tree *ptype, tree initial_val= ue, bool do_default) > TYPE_TYPELESS_STORAGE (main_type) =3D TYPE_TYPELESS_STORAGE (type); > layout_type (main_type); > =20 > + /* Set TYPE_STRUCTURAL_EQUALITY_P early. */ > + if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type)) > + || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type))) > + SET_TYPE_STRUCTURAL_EQUALITY (main_type); > + else > + TYPE_CANONICAL (main_type) =3D main_type; > + > /* Make sure we have the canonical MAIN_TYPE. */ > hashval_t hashcode =3D type_hash_canon_hash (main_type); > main_type =3D type_hash_canon (hashcode, main_type); > @@ -7122,7 +7129,7 @@ complete_array_type (tree *ptype, tree initial_valu= e, bool do_default) > /* Fix the canonical type. */ > if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type)) > || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type))) > - SET_TYPE_STRUCTURAL_EQUALITY (main_type); > + gcc_assert (TYPE_STRUCTURAL_EQUALITY_P (main_type)); > else if (TYPE_CANONICAL (TREE_TYPE (main_type)) !=3D TREE_TYPE (main_t= ype) > || (TYPE_CANONICAL (TYPE_DOMAIN (main_type)) > !=3D TYPE_DOMAIN (main_type))) > @@ -7130,8 +7137,6 @@ complete_array_type (tree *ptype, tree initial_valu= e, bool do_default) > =3D build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)), > TYPE_CANONICAL (TYPE_DOMAIN (main_type)), > TYPE_TYPELESS_STORAGE (main_type)); > - else > - TYPE_CANONICAL (main_type) =3D main_type; > =20 > if (quals =3D=3D 0) > type =3D main_type; > diff --git a/gcc/testsuite/gcc.dg/pr114931.c b/gcc/testsuite/gcc.dg/pr114= 931.c > new file mode 100644 > index 00000000000..d690ed70e52 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/pr114931.c > @@ -0,0 +1,10 @@ > +/* { dg-do compile } */ > +/* { dg-options "-std=3Dc23" } */ > + > +struct Tcl_Obj; > +void(Tcl_FreeInternalRepProc)(struct Tcl_Obj *); > +typedef struct Tcl_Obj { > +} Tcl_Obj; > +struct { > + void (*tclFreeObj)(Tcl_Obj *); > +} Tcl_InitStubs; > diff --git a/gcc/tree.cc b/gcc/tree.cc > index 780662549fe..6564b002dc1 100644 > --- a/gcc/tree.cc > +++ b/gcc/tree.cc > @@ -6012,6 +6012,8 @@ type_hash_canon_hash (tree type) > =20 > hstate.add_int (TREE_CODE (type)); > =20 > + hstate.add_flag (TYPE_STRUCTURAL_EQUALITY_P (type)); > + > if (TREE_TYPE (type)) > hstate.add_object (TYPE_HASH (TREE_TYPE (type))); > =20 > @@ -6109,6 +6111,10 @@ type_cache_hasher::equal (type_hash *a, type_hash = *b) > || TYPE_MODE (a->type) !=3D TYPE_MODE (b->type))) > return false; > =20 > + if (TYPE_STRUCTURAL_EQUALITY_P (a->type) > + !=3D TYPE_STRUCTURAL_EQUALITY_P (b->type)) > + return false; > + > switch (TREE_CODE (a->type)) > { > case VOID_TYPE: > @@ -7347,6 +7353,14 @@ build_array_type_1 (tree elt_type, tree index_type= , bool typeless_storage, > TYPE_DOMAIN (t) =3D index_type; > TYPE_ADDR_SPACE (t) =3D TYPE_ADDR_SPACE (elt_type); > TYPE_TYPELESS_STORAGE (t) =3D typeless_storage; > + > + /* Set TYPE_STRUCTURAL_EQUALITY_P. */ > + if (set_canonical > + && (TYPE_STRUCTURAL_EQUALITY_P (elt_type) > + || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)) > + || in_lto_p)) > + SET_TYPE_STRUCTURAL_EQUALITY (t); > + > layout_type (t); > =20 > if (shared) > @@ -7363,7 +7377,7 @@ build_array_type_1 (tree elt_type, tree index_type,= bool typeless_storage, > if (TYPE_STRUCTURAL_EQUALITY_P (elt_type) > || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)) > || in_lto_p) > - SET_TYPE_STRUCTURAL_EQUALITY (t); > + gcc_unreachable (); > else if (TYPE_CANONICAL (elt_type) !=3D elt_type > || (index_type && TYPE_CANONICAL (index_type) !=3D index_type)) > TYPE_CANONICAL (t) > @@ -7510,21 +7524,25 @@ build_function_type (tree value_type, tree arg_ty= pes, > TYPE_NO_NAMED_ARGS_STDARG_P (t) =3D 1; > } > =20 > - /* If we already have such a type, use the old one. */ > - hashval_t hash =3D type_hash_canon_hash (t); > - tree probe_type =3D t; > - t =3D type_hash_canon (hash, t); > - if (t !=3D probe_type) > - return t; > - > /* Set up the canonical type. */ > any_structural_p =3D TYPE_STRUCTURAL_EQUALITY_P (value_type); > any_noncanonical_p =3D TYPE_CANONICAL (value_type) !=3D value_type; > canon_argtypes =3D maybe_canonicalize_argtypes (arg_types, > &any_structural_p, > &any_noncanonical_p); > + /* Set TYPE_STRUCTURAL_EQUALITY_P early. */ > if (any_structural_p) > SET_TYPE_STRUCTURAL_EQUALITY (t); > + > + /* If we already have such a type, use the old one. */ > + hashval_t hash =3D type_hash_canon_hash (t); > + tree probe_type =3D t; > + t =3D type_hash_canon (hash, t); > + if (t !=3D probe_type) > + return t; > + > + if (any_structural_p) > + gcc_assert (TYPE_STRUCTURAL_EQUALITY_P (t)); > else if (any_noncanonical_p) > TYPE_CANONICAL (t) =3D build_function_type (TYPE_CANONICAL (value_ty= pe), > canon_argtypes); > @@ -7667,13 +7685,6 @@ build_method_type_directly (tree basetype, > argtypes =3D tree_cons (NULL_TREE, ptype, argtypes); > TYPE_ARG_TYPES (t) =3D argtypes; > =20 > - /* If we already have such a type, use the old one. */ > - hashval_t hash =3D type_hash_canon_hash (t); > - tree probe_type =3D t; > - t =3D type_hash_canon (hash, t); > - if (t !=3D probe_type) > - return t; > - > /* Set up the canonical type. */ > any_structural_p > =3D (TYPE_STRUCTURAL_EQUALITY_P (basetype) > @@ -7684,8 +7695,20 @@ build_method_type_directly (tree basetype, > canon_argtypes =3D maybe_canonicalize_argtypes (TREE_CHAIN (argtypes), > &any_structural_p, > &any_noncanonical_p); > + > + /* Set TYPE_STRUCTURAL_EQUALITY_P early. */ > if (any_structural_p) > SET_TYPE_STRUCTURAL_EQUALITY (t); > + > + /* If we already have such a type, use the old one. */ > + hashval_t hash =3D type_hash_canon_hash (t); > + tree probe_type =3D t; > + t =3D type_hash_canon (hash, t); > + if (t !=3D probe_type) > + return t; > + > + if (any_structural_p) > + gcc_assert (TYPE_STRUCTURAL_EQUALITY_P (t)); > else if (any_noncanonical_p) > TYPE_CANONICAL (t) > =3D build_method_type_directly (TYPE_CANONICAL (basetype), > @@ -7726,6 +7749,9 @@ build_offset_type (tree basetype, tree type) > =20 > TYPE_OFFSET_BASETYPE (t) =3D TYPE_MAIN_VARIANT (basetype); > TREE_TYPE (t) =3D type; > + if (TYPE_STRUCTURAL_EQUALITY_P (basetype) > + || TYPE_STRUCTURAL_EQUALITY_P (type)) > + SET_TYPE_STRUCTURAL_EQUALITY (t); > =20 > /* If we already have such a type, use the old one. */ > hashval_t hash =3D type_hash_canon_hash (t); > @@ -7741,7 +7767,7 @@ build_offset_type (tree basetype, tree type) > { > if (TYPE_STRUCTURAL_EQUALITY_P (basetype) > || TYPE_STRUCTURAL_EQUALITY_P (type)) > - SET_TYPE_STRUCTURAL_EQUALITY (t); > + gcc_unreachable (); > else if (TYPE_CANONICAL (TYPE_MAIN_VARIANT (basetype)) !=3D basety= pe > || TYPE_CANONICAL (type) !=3D type) > TYPE_CANONICAL (t) > @@ -7770,6 +7796,8 @@ build_complex_type (tree component_type, bool named= ) > tree probe =3D make_node (COMPLEX_TYPE); > =20 > TREE_TYPE (probe) =3D TYPE_MAIN_VARIANT (component_type); > + if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (probe))) > + SET_TYPE_STRUCTURAL_EQUALITY (probe); > =20 > /* If we already have such a type, use the old one. */ > hashval_t hash =3D type_hash_canon_hash (probe); > @@ -7781,11 +7809,10 @@ build_complex_type (tree component_type, bool nam= ed) > out the type. We need to check the canonicalization and > maybe set the name. */ > gcc_checking_assert (COMPLETE_TYPE_P (t) > - && !TYPE_NAME (t) > - && TYPE_CANONICAL (t) =3D=3D t); > + && !TYPE_NAME (t)); > =20 > if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (t))) > - SET_TYPE_STRUCTURAL_EQUALITY (t); > + ; > else if (TYPE_CANONICAL (TREE_TYPE (t)) !=3D TREE_TYPE (t)) > TYPE_CANONICAL (t) > =3D build_complex_type (TYPE_CANONICAL (TREE_TYPE (t)), named);