From patchwork Fri Jul 12 20:42:30 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: 1960018 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=iLg+Xnz9; 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 4WLNpW3qZPz1xqj for ; Sat, 13 Jul 2024 06:43:15 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B79F9385ED4A for ; Fri, 12 Jul 2024 20:43:13 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by sourceware.org (Postfix) with ESMTPS id 5F19E3858433 for ; Fri, 12 Jul 2024 20:42:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5F19E3858433 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 5F19E3858433 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::429 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720816970; cv=none; b=oV7q868AYvv+tV7TSq6mtavExe2mXfAwP0Xmx3xImvbclT8blOVLwHYoYiF9VAN7HQSMH9xMg0LE8eV18k+A2JTr8Lrm4SVF+N1BQCfTctRWtMXZuNUNaoB48D4SPsABqIkVdTzM23oUCRjDMEorpiLw1LedoivU7mgJwAha234= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720816970; c=relaxed/simple; bh=WX5YUTYWTvL5w8Xzog9hbeDyJvZY+DPTqd4IAoUPsLY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=xAP2Fu3fG46Xhd4kHpKB30/te4+CXyaUCI7hNTqdAPDyeZCQKgWg8E3ri5zwKEMUexKxhwnj9XGF9uOald56zov5nQpQCcAOcUVzrO+7hUbzUs1LsaHD+SDe4Ltk1p+02OOtFO4WA8arB34f8oXeQS1J/XIK5GUK0No7vFn7LyE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wr1-x429.google.com with SMTP id ffacd0b85a97d-36794da7c96so1398355f8f.0 for ; Fri, 12 Jul 2024 13:42:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720816967; x=1721421767; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Kg2zp+nSakaDTUpQTbzc8bmVoEQW3gutvSByYgzM55s=; b=iLg+Xnz9i3bonknTeKexb5AK1LGyigD0uw+WVFp57IoNb+twUj6tTBWaQcJfH0jlZ4 O8Op27tRni1t5kDLN0ZXMC0ZV/XZHgbXjE4HTsSZ038BIYnWfEdT+GZ01S3uauW7kKZE e3D2ZAOz9avoNztBkbkNrgCYGEWvTlCxiav1WZp67iTve2GGoQIB++m+/P6fRnnwPeg7 nK0fURtjjlykDAfnKm1YkicgG/L8DgPhrrRSX4+u463HrA4yjeBhpAYr6l7kuwEiSloZ bDMvDxlruqfRXP44wqj1/cHLA+m9VeHUVQ3VF/KH/JWlF3gAdx0jfDDYGuaqoSpg7x14 c0Mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720816967; x=1721421767; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Kg2zp+nSakaDTUpQTbzc8bmVoEQW3gutvSByYgzM55s=; b=poWI1PwE4KeGAEZkvrUV0VQhAKYbqsxq95m3DtrRHzeaMOT2sQfAMuh8xf/1W+NRIM rq6eucIujtDV3STCl9Xy4R2o1uQAQMggE8H7HVSXtVBG8RpjxPhpB/Zvx4sAR3YZfKZC U7OS8pUf+XINRbxqQTbbf3ez2B5z1An7lBN5J5YCYLvITfbaHtmsVgTsvw4r5qtAJ4dW PYyryHkU9/wiCeesiHHe4TWGkv+44d2N/4fble7jqxUTAdQbMS9drZhaaIzKqt/fSgdl gea7tnfOyl2o2mRHGTs/IvZdxhn+jLgoktPYNNJVuILMZw5TCzCEpRsEl5hg+OLHOhwZ qasA== X-Gm-Message-State: AOJu0Yz3eTpnJ9EDE1k0e30Ks/VrixufwhihsJhYC14z47BqEkggYNhr odlA/sCCpv3Hjyl/mh/7VF7Iyw9G9PkC2d5YUMfzIYtjkmr2DTfd/QM9JPnmYg4= X-Google-Smtp-Source: AGHT+IHURQiAGJT6J1GqFkPyrauGTyeS3eJlN1Pi0bCKQwecdR/7naD08Ro9MT+IzFyTvEczl1KCMw== X-Received: by 2002:a5d:6986:0:b0:367:9575:2820 with SMTP id ffacd0b85a97d-367ceac57bdmr7250868f8f.45.1720816966527; Fri, 12 Jul 2024 13:42:46 -0700 (PDT) Received: from localhost.localdomain (cpc86130-nfds16-2-0-cust181.8-2.cable.virginm.net. [82.11.162.182]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-367cdfa06b6sm10895876f8f.86.2024.07.12.13.42.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Jul 2024 13:42:46 -0700 (PDT) From: Seyed Sajad Kahani To: gcc-patches@gcc.gnu.org Cc: pinskia@gmail.com, ppalka@redhat.com, Seyed Sajad Kahani Subject: [PATCH v1] c++: Hash placeholder constraint in ctp_hasher Date: Fri, 12 Jul 2024 21:42:30 +0100 Message-ID: <20240712204230.303515-1-sska1377@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 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, 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(-) 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) 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);