From patchwork Tue Jul 9 13:45:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Seyed Sajad Kahani X-Patchwork-Id: 1958441 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=TwV/3VNy; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4WJMhr6KrLz1xpd for ; Tue, 9 Jul 2024 23:46:20 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9DD0C38449C7 for ; Tue, 9 Jul 2024 13:46:18 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-yw1-x1130.google.com (mail-yw1-x1130.google.com [IPv6:2607:f8b0:4864:20::1130]) by sourceware.org (Postfix) with ESMTPS id 76C5E386C5B7 for ; Tue, 9 Jul 2024 13:45:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 76C5E386C5B7 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 76C5E386C5B7 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1130 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720532743; cv=none; b=DphltKw8UuQEspkrUZ9aRxazaWONkyzvf3aLPoJ2czFXHo0XxLyl/vL8JEzSoQpTSCEyc5d8TIvTO49F1vRL2OoxFlZGyADknBu5AsOwXBtWg5MDm0O9jPTqTNURyoOK/4nT8/kyWndfahvyOkAcpfl9ZHpgPjWnXl/qA73WAeA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720532743; c=relaxed/simple; bh=D50eBNUO6FK1isEhgz7CT86HW6Fr3F6PrzOqA5vc+zs=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=ZStL5rrfUEIBFY0r3viFQxt6HNcSnMFORn5LerdidHUSLkgn8oLKc0AS3ngqVzzKr/c1J4+3Hw/RQ8QVkML4lotNTbD/3BH8Oa4pYD6LKQ8n33QFg4yQvdjS4vFjdJ1jCKaUU1cDuyDeL5prlyFDQ8sYViXFgVpu0BkqAd9kjr4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yw1-x1130.google.com with SMTP id 00721157ae682-64b3655297aso50145707b3.1 for ; Tue, 09 Jul 2024 06:45:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720532740; x=1721137540; darn=gcc.gnu.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=6nKeYMZiBXQHwWC3MDJcmBhDKsrdYMt493VpUGDDdq0=; b=TwV/3VNyGxKNK6YFYfMoH55o+HdTQE9KW24DoEsBhwMPcB52gwgixHnI6f14CAMtFb 1uVmH/SYzW5eGlo+tcJ0z+rXGdEigZio4LwKPwb1bInit/Y5xLX1DUBUeyrz451XGTem rN0sS38KGtx9vhMGjTPHlFGmLhG7Mz3KCHaoZFtn79aBPSI3HvEBnG7aTNxOQ656M7Mr WJKQv03lq9q+55soI9WFzL67/T68tuQOoJckDOyt3ppSDm2p5asmxGOgiKJCxGK/VW8g QK9VIZ3iTy+GPSKmwZrglAW4La4kfRJ5nt4B6u39UkbKmHtH/xLY8OAHxd0R4JhrLQMT iW9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720532740; x=1721137540; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=6nKeYMZiBXQHwWC3MDJcmBhDKsrdYMt493VpUGDDdq0=; b=pCBErNue6mMPwvSL+XZRLrPLMlCVRT2Dj79WlluQ92G49aAkypRZiCpg4MBaApXsOQ B8f6O1/BmEs8TeQnAhOAoQ3KHfyUA3qhVln7BCv900MCETazAtglvmGDAnjJwEeV7Gci QrOTkgRgKBBN7IGtM1OGSvcpbN5msmDAt1c+ZKP5Qr6ozpDULcCZecf5kJcnjH6IMgev 6Cqjb8jV3eLhWAJkuagXdiQx75/o962uofkLv8u45aBSWleM37YTr3FNbwjOdakoy1dT oAKq0t47Je9ODpbFXQsp/fMY+/jcsuJTI/a/Fq7oyqx7wH7zjXTVVP3f/s6uV4QMv6L3 CAcw== X-Gm-Message-State: AOJu0YzC7G5f6looOSQfj9XadCwi+mORqu/zPaAL1k6Kj3LpyXG3CY1t LWacH2r9Qcny+BaPO1ES2BX6AF7hxcddgFzR5OLcsdzJ4NSpMlzl6ziCV5QSZ7r1xEc8H36ZdrJ tn56N395Ifd13+XCgkfFyF1bppXPKYoqH X-Google-Smtp-Source: AGHT+IFlo+af4A7FKbXTqIo+VPVn1LyocIrRhdylG6GL2LgxiCZYGRlhQ/+vRmXcxqnukprULHC89K88pH618U8alkI= X-Received: by 2002:a05:690c:dd4:b0:649:4256:9d49 with SMTP id 00721157ae682-658f09d2315mr37217027b3.42.1720532739638; Tue, 09 Jul 2024 06:45:39 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Seyed Sajad Kahani Date: Tue, 9 Jul 2024 14:45:03 +0100 Message-ID: Subject: [PATCH v1] c++: Hash placeholder constraint in ctp_hasher To: gcc-patches@gcc.gnu.org X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, 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 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: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org This patch addresses a difference between the hash function and the equality function for canonical types of template parameters (ctp_hasher). The equality function uses comptypes (typeck.cc) (with COMPARE_STRUCTURAL) and checks constraint equality for two auto nodes (typeck.cc:1586), while the hash function ignores it (pt.cc:4528). This leads to hash collisions that can be avoided by using `hash_placeholder_constraint` (constraint.cc:1150). * constraint.cc (hash_placeholder_constraint): Rename to iterative_hash_placeholder_constraint. (iterative_hash_placeholder_constraint): Rename from hash_placeholder_constraint and add the initial val argument. * cp-tree.h (hash_placeholder_constraint): Rename to iterative_hash_placeholder_constraint. (iterative_hash_placeholder_constraint): Renamed from hash_placeholder_constraint and add the initial val argument. * pt.cc (struct ctp_hasher): Updated to use iterative_hash_placeholder_constraint in the case of a valid placeholder constraint. (auto_hash::hash): Reflect the renaming of hash_placeholder_constraint to iterative_hash_placeholder_constraint. --- gcc/cp/constraint.cc | 4 ++-- gcc/cp/cp-tree.h | 2 +- gcc/cp/pt.cc | 9 +++++++-- 3 files changed, 10 insertions(+), 5 deletions(-) if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t))) /* Matching constrained-type-specifiers denote the same template parameter, so hash the constraint. */ - return hash_placeholder_constraint (c); + return iterative_hash_placeholder_constraint (c, 0); else /* But unconstrained autos are all separate, so just hash the pointer. */ return iterative_hash_object (t, 0); diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc index ebf4255e5..78aacb77a 100644 --- a/gcc/cp/constraint.cc +++ b/gcc/cp/constraint.cc @@ -1971,13 +1971,13 @@ equivalent_placeholder_constraints (tree c1, tree c2) /* Return a hash value for the placeholder ATOMIC_CONSTR C. */ hashval_t -hash_placeholder_constraint (tree c) +iterative_hash_placeholder_constraint (tree c, hashval_t val) { tree t, a; placeholder_extract_concept_and_args (c, t, a); /* Like hash_tmpl_and_args, but skip the first argument. */ - hashval_t val = iterative_hash_object (DECL_UID (t), 0); + val = iterative_hash_object (DECL_UID (t), val); for (int i = TREE_VEC_LENGTH (a)-1; i > 0; --i) val = iterative_hash_template_arg (TREE_VEC_ELT (a, i), val); diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 4bb3e9c49..294e88f75 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -8588,7 +8588,7 @@ extern tree_pair finish_type_constraints (tree, tree, tsubst_flags_t); extern tree build_constrained_parameter (tree, tree, tree = NULL_TREE); extern void placeholder_extract_concept_and_args (tree, tree&, tree&); extern bool equivalent_placeholder_constraints (tree, tree); -extern hashval_t hash_placeholder_constraint (tree); +extern hashval_t iterative_hash_placeholder_constraint (tree, hashval_t); extern bool deduce_constrained_parameter (tree, tree&, tree&); extern tree resolve_constraint_check (tree); extern tree check_function_concept (tree); diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index d1316483e..9a80c44a5 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -4525,7 +4525,12 @@ struct ctp_hasher : ggc_ptr_hash val = iterative_hash_object (TEMPLATE_TYPE_LEVEL (t), val); val = iterative_hash_object (TEMPLATE_TYPE_IDX (t), val); if (TREE_CODE (t) == TEMPLATE_TYPE_PARM) - val = iterative_hash_template_arg (CLASS_PLACEHOLDER_TEMPLATE (t), val); + { + val + = iterative_hash_template_arg (CLASS_PLACEHOLDER_TEMPLATE (t), val); + if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t))) + val = iterative_hash_placeholder_constraint(c, val); + } if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM) val = iterative_hash_template_arg (TYPE_TI_ARGS (t), val); --comparing_specializations; @@ -29605,7 +29610,7 @@ auto_hash::hash (tree t)