From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by sourceware.org (Postfix) with ESMTPS id 29A193858C66 for ; Fri, 3 May 2024 12:14:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 29A193858C66 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 29A193858C66 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714738444; cv=none; b=vzsDjcQmgahyED1aOdpQ6jRA/hL0g2VL5AFQ8IL4KFfovkH+XTSlSWYctyFV5cTOdeGwZElysWiTCvwCx3o5bm5+kUh6m6Nx/IVBSKdZgiVyQcRVZPZhnA/ghY43jsOcA/lFHwo0HcBYDeMti+xet7rt4BeonTLzweC6e3aSCVc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714738444; c=relaxed/simple; bh=I1Mcy7gSyDOxmwlFTR+wTc3lO9xodezZoT8aWit+Q6Q=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version:Message-Id; b=Fw1Ro4xH5hRxBYBUOLrn1zjAs6zZUNzf2sTa/UT2fD0sRccYiZzW6/WMM2175YvV0VZsq2NfFUqWw0w/IPbO0twX142YkjRUPYe5UJ82xcEsVUFl65Sfg7DzWKAhvH+68AwfzOFJjNBXmP/RCitr3Gs398UDY0BRhFsT3hsqlrE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id F1E4233930; Fri, 3 May 2024 12:14:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1714738441; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=1NfcdGGalXGgFrdeZCv6m6mrdYCGTmP0EP9DTQX2oyc=; b=gjEl0BV9Ne5ldPHrUE8kb7vXnwl8T+WA6dk/k47ibP7yWXXY7ocDBa8zDFcXTFjIwNT5bd ZK3K9TSh6zjYrnT8F0q5lG6oSzPKpVPg2itq86djmZYa+TgeeS2opHREbldDdo/QgmfvEa P/D3713PlHiVt4Vn5ZkTN4zHZB39fOc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1714738441; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=1NfcdGGalXGgFrdeZCv6m6mrdYCGTmP0EP9DTQX2oyc=; b=lbWmKYcUU8Hky3hC7+kbphJu+XtU/yxD1Ks0kkm3KGi19hLHFVlqBYyYO6iuwn9bS6Qy6n BQEr4UzHcIWPHGBQ== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1714738440; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=1NfcdGGalXGgFrdeZCv6m6mrdYCGTmP0EP9DTQX2oyc=; b=sSjd1zfRjowugbl2dgzH8R1r070l3fA1yO4FfPn7BaCfI/L6KBNG7y9neeD41iiuGAMzSN e55OTT7lYBtJD628a7byDUZgwGe08aqmPk8zf0LZ09nYAdzzqwND24ZhIjn68iAPLzdonc VJcuLZKCsABG5UgS8WtCzD8k1kBCXqo= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1714738440; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=1NfcdGGalXGgFrdeZCv6m6mrdYCGTmP0EP9DTQX2oyc=; b=UmPxI3mS3e0TUODWSFJOUHYm68p4uA8J682vWI4yKNVm6QsUW91xsTuRN9ZMZGuITD+t81 UzNMkodZ30Jzj5AA== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id C8A4313991; Fri, 3 May 2024 12:14:00 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id Q/ZNLwjVNGaVaAAAD6G6ig (envelope-from ); Fri, 03 May 2024 12:14:00 +0000 Date: Fri, 3 May 2024 14:13:56 +0200 (CEST) From: Richard Biener To: gcc-patches@gcc.gnu.org cc: Jakub Jelinek , uecker@tugraz.at Subject: [PATCH] middle-end/114931 - type_hash_canon and structual equality types MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Message-Id: <20240503121400.C8A4313991@imap1.dmz-prg2.suse.org> X-Spam-Score: -4.30 X-Spam-Level: X-Spamd-Result: default: False [-4.30 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; RCVD_VIA_SMTP_AUTH(0.00)[]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; MISSING_XM_UA(0.00)[]; TO_DN_SOME(0.00)[]; RCVD_TLS_ALL(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[3]; FROM_EQ_ENVFROM(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,imap1.dmz-prg2.suse.org:rdns] X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,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: 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. Bootstrapped and tested on x86_64-unknown-linux-gnu for all languages. 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. OK for trunk? Thanks, Richard. 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. gcc/c-family/ * c-common.cc (complete_array_type): Set TYPE_STRUCTURAL_EQUALITY_P before probing with type_hash_canon. 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 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, tree attribute, int quals) tree dtype = ntype = build_distinct_type_copy (ttype); TYPE_ATTRIBUTES (ntype) = 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); hashval_t hash = type_hash_canon_hash (ntype); ntype = type_hash_canon (hash, ntype); @@ -1343,16 +1353,6 @@ build_type_attribute_qual_variant (tree otype, tree attribute, int quals) if (ntype != 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) == ntype) TYPE_CANONICAL (ntype) = TYPE_CANONICAL (ttype); 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_value, bool do_default) TYPE_TYPELESS_STORAGE (main_type) = TYPE_TYPELESS_STORAGE (type); layout_type (main_type); + /* 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) = main_type; + /* Make sure we have the canonical MAIN_TYPE. */ hashval_t hashcode = type_hash_canon_hash (main_type); main_type = type_hash_canon (hashcode, main_type); @@ -7122,7 +7129,7 @@ complete_array_type (tree *ptype, tree initial_value, 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)) != TREE_TYPE (main_type) || (TYPE_CANONICAL (TYPE_DOMAIN (main_type)) != TYPE_DOMAIN (main_type))) @@ -7130,8 +7137,6 @@ complete_array_type (tree *ptype, tree initial_value, bool do_default) = 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) = main_type; if (quals == 0) type = main_type; diff --git a/gcc/testsuite/gcc.dg/pr114931.c b/gcc/testsuite/gcc.dg/pr114931.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=c23" } */ + +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) hstate.add_int (TREE_CODE (type)); + hstate.add_flag (TYPE_STRUCTURAL_EQUALITY_P (type)); + if (TREE_TYPE (type)) hstate.add_object (TYPE_HASH (TREE_TYPE (type))); @@ -6109,6 +6111,10 @@ type_cache_hasher::equal (type_hash *a, type_hash *b) || TYPE_MODE (a->type) != TYPE_MODE (b->type))) return false; + if (TYPE_STRUCTURAL_EQUALITY_P (a->type) + != 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) = index_type; TYPE_ADDR_SPACE (t) = TYPE_ADDR_SPACE (elt_type); TYPE_TYPELESS_STORAGE (t) = 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); 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) != elt_type || (index_type && TYPE_CANONICAL (index_type) != index_type)) TYPE_CANONICAL (t) @@ -7510,21 +7524,25 @@ build_function_type (tree value_type, tree arg_types, TYPE_NO_NAMED_ARGS_STDARG_P (t) = 1; } - /* If we already have such a type, use the old one. */ - hashval_t hash = type_hash_canon_hash (t); - tree probe_type = t; - t = type_hash_canon (hash, t); - if (t != probe_type) - return t; - /* Set up the canonical type. */ any_structural_p = TYPE_STRUCTURAL_EQUALITY_P (value_type); any_noncanonical_p = TYPE_CANONICAL (value_type) != value_type; canon_argtypes = 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 = type_hash_canon_hash (t); + tree probe_type = t; + t = type_hash_canon (hash, t); + if (t != probe_type) + return t; + + if (any_structural_p) + gcc_assert (TYPE_STRUCTURAL_EQUALITY_P (t)); else if (any_noncanonical_p) TYPE_CANONICAL (t) = build_function_type (TYPE_CANONICAL (value_type), canon_argtypes); @@ -7667,13 +7685,6 @@ build_method_type_directly (tree basetype, argtypes = tree_cons (NULL_TREE, ptype, argtypes); TYPE_ARG_TYPES (t) = argtypes; - /* If we already have such a type, use the old one. */ - hashval_t hash = type_hash_canon_hash (t); - tree probe_type = t; - t = type_hash_canon (hash, t); - if (t != probe_type) - return t; - /* Set up the canonical type. */ any_structural_p = (TYPE_STRUCTURAL_EQUALITY_P (basetype) @@ -7684,8 +7695,20 @@ build_method_type_directly (tree basetype, canon_argtypes = 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 = type_hash_canon_hash (t); + tree probe_type = t; + t = type_hash_canon (hash, t); + if (t != probe_type) + return t; + + if (any_structural_p) + gcc_assert (TYPE_STRUCTURAL_EQUALITY_P (t)); else if (any_noncanonical_p) TYPE_CANONICAL (t) = build_method_type_directly (TYPE_CANONICAL (basetype), @@ -7726,6 +7749,9 @@ build_offset_type (tree basetype, tree type) TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype); TREE_TYPE (t) = type; + if (TYPE_STRUCTURAL_EQUALITY_P (basetype) + || TYPE_STRUCTURAL_EQUALITY_P (type)) + SET_TYPE_STRUCTURAL_EQUALITY (t); /* If we already have such a type, use the old one. */ hashval_t hash = 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)) != basetype || TYPE_CANONICAL (type) != type) TYPE_CANONICAL (t) @@ -7770,6 +7796,8 @@ build_complex_type (tree component_type, bool named) tree probe = make_node (COMPLEX_TYPE); TREE_TYPE (probe) = TYPE_MAIN_VARIANT (component_type); + if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (probe))) + SET_TYPE_STRUCTURAL_EQUALITY (probe); /* If we already have such a type, use the old one. */ hashval_t hash = type_hash_canon_hash (probe); @@ -7781,11 +7809,10 @@ build_complex_type (tree component_type, bool named) 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) == t); + && !TYPE_NAME (t)); if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (t))) - SET_TYPE_STRUCTURAL_EQUALITY (t); + ; else if (TYPE_CANONICAL (TREE_TYPE (t)) != TREE_TYPE (t)) TYPE_CANONICAL (t) = build_complex_type (TYPE_CANONICAL (TREE_TYPE (t)), named); -- 2.35.3