From patchwork Fri Feb 16 12:50:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 202112 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:c619:b0:108:e6aa:91d0 with SMTP id hn25csp487500dyb; Fri, 16 Feb 2024 04:51:26 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVescPCMH7R4LriwFNKBmvVqNiwxt6TxVz3X5oEmQ7DR9YaMOnVTf5J1unXeTVLzTOM/x5AFeXL7Qeh0uBfd0e9KTtMnw== X-Google-Smtp-Source: AGHT+IFI2ob3Ig+L40CkxI9Bsuw8+eUtbstAKoJZAfQ+kq8ep4SgQxH861LpVqYtT7i+fFKc+jux X-Received: by 2002:a0c:f58b:0:b0:68f:6be:9f94 with SMTP id k11-20020a0cf58b000000b0068f06be9f94mr4314369qvm.12.1708087886747; Fri, 16 Feb 2024 04:51:26 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708087886; cv=pass; d=google.com; s=arc-20160816; b=IfcI5S3NBFZ5zJg/O8lqvckXLXYfPmWgvuhYTzP5hkBUQqHNCGADb+ZOA0wIIQ8WyF uVPHqJIE/SZavTAz44gv7Dj9PDTz2LaDw0Sl2WdxY65bKCVak8sujatr9R6JrPoN7e8/ ehtNJ4g5dTV5/lR0kq0Cmx6aOWaI/DIdxzzy9k4zo35L1sPg5fAMkWzQUMgCN5vfjvR+ ggMNbjocBTrqs2081SN7sQF1CA4Dvu8FRoq354tht080hfS60WW1ubYinjVf6f7SKDZU q8pChxxcrjKy0iSjWI+pduYiYhJvnVb/z1jJOb9VbnlZMDGRYKUWtRumkOtyoIXs2hoq GAYA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:message-id:mime-version:subject :cc:to:from:date:dkim-signature:dkim-signature:dkim-signature :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=qZCa946jgOIwLhWx9nWY+xUe4QSk/zILKq6Pmkzz1ds=; fh=Wj4XyTsB67F88i/c3hWDCZG5yLJL+GmA64plAIn2D/Q=; b=zl5R6n2Jf0kgu2YwmrHe+GXD0xhpzzCAuf8J44FD83uJfPvQ1opDPlxfsqbkyQ69L6 DqQegA8k609bnTSy3usevFw+/rPTqIpa3X0CkTdX8wLDmteB5qZradrHyaP8hX0LgHef 15RrdHBhRy5Raj0l02xWLqrwLYpKkOXGYa4OYO6/doad3hPlrELGUhC+tjTVX5tmoWkc jnryfxQCzMpcih8WC9IGExTxsrqs2RKR0SkyvzyLqGMjIjj4noBH7G+LBvx2Yf3cJtyg sOc4Xbr19D9Jel4EOW3BrKVeKNAqYj/N4fOfVHtN98TwPhe81QVi8WQ7ezhVXdlytntP Y0tg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=neutral (body hash did not verify) header.i=@suse.de header.s=susede2_rsa header.b="A/0393vN"; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=1fCMNsqf; dkim=neutral (body hash did not verify) header.i=@suse.de header.s=susede2_rsa header.b=X0E1PoDs; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=SLsQVqhY; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id pt6-20020a056214048600b00681991637e6si3809763qvb.484.2024.02.16.04.51.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Feb 2024 04:51:26 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@suse.de header.s=susede2_rsa header.b="A/0393vN"; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=1fCMNsqf; dkim=neutral (body hash did not verify) header.i=@suse.de header.s=susede2_rsa header.b=X0E1PoDs; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=SLsQVqhY; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7269438582B1 for ; Fri, 16 Feb 2024 12:51:26 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by sourceware.org (Postfix) with ESMTPS id 9115C3858298 for ; Fri, 16 Feb 2024 12:50:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9115C3858298 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 9115C3858298 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708087846; cv=none; b=wawaKBoTczV6MpYVjN/stSK09jMuvHHsZwNvcoNxxQlnn9odHWNhTVrnmaYzos3G2zBAUnf5lavQl/SWMoxZ3KBgW97XTMNTWSbm5ief2uiLjAyxwxYfv4Yb7d3Lg+4n09pfr4TNlcO0oJs/ounIEP7GUUk+7Fn/cDY5s30OBZk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708087846; c=relaxed/simple; bh=+Vq2O+ucA7cs7UbZ2PWfm+paZ3vkfmTxNWWjKYYJob0=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:Date: From:To:Subject:MIME-Version:Message-Id; b=RZI1lCBHFUxbbaoAQzsCaMnNF5gyFEmKBBjHWB3cUOnX0O1OBL0EcyDazMBKU4SUBrignDzUPCUEsLrQslZvF87wZuOVlReDw66e+KFIwQX60Z49iF/+9s2b04O324iPMZ+ArxHsSjuAMmoUp6xPVFFCHagFazeoAKv1zQp3Jl8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from imap2.dmz-prg2.suse.org (imap2.dmz-prg2.suse.org [10.150.64.98]) (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-out2.suse.de (Postfix) with ESMTPS id 86EAC1FB69; Fri, 16 Feb 2024 12:50:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1708087834; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=kbYFLJBDNOoR9viuvljVW5L9amuraNeL4sP916AZCRk=; b=A/0393vN3LdtQwWzctgH76p3r5L0CIKxl5sxfUvfs2/r8+O/70ymEIG5ssG2dYHER/Ill2 Ub/cGooz5dQ4TLOcsWaKg4aGZQuQbSCqKxQXNGJG8VPRnkUusNipTRUOzhWOCzhZ2BVlky 5oa5Y48c1Tfsa+BRnYdvTmpul91ViAA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1708087834; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=kbYFLJBDNOoR9viuvljVW5L9amuraNeL4sP916AZCRk=; b=1fCMNsqfHUkekATOjDUsD0MhtpHwHrwT5w/JuzBByj6Zx8sgBL5MGqnxRqoOaFq2AXMspo 6am/o7IQo1Zi0SAg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1708087832; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=kbYFLJBDNOoR9viuvljVW5L9amuraNeL4sP916AZCRk=; b=X0E1PoDshWxEWMtWZE7UndVBk8cAxnDRurTn1/MDMAcex5R1rEEF9f37kmCxCTUsev87Ew 9Wy2ekqTjK2/gdDYL1F9fgmJsJbfwhKi7NFXfT1gZIPiVRa/9QIOfreZgyTRhJ4W0qEbWb SOR2bolonpfwEsO+DPvI5CZUGdpuRAc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1708087832; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type; bh=kbYFLJBDNOoR9viuvljVW5L9amuraNeL4sP916AZCRk=; b=SLsQVqhY1Muy+mOVN4Yf1aqod4KqWzWN4ultqDxa7NEnWGYAFdnIqNc3QYDdI/Un+Hp5nI 4dbYle72I0bEcyCg== Received: from imap2.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 imap2.dmz-prg2.suse.org (Postfix) with ESMTPS id 5F11713343; Fri, 16 Feb 2024 12:50:32 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap2.dmz-prg2.suse.org with ESMTPSA id iK2dFRhaz2XmJAAAn2gu4w (envelope-from ); Fri, 16 Feb 2024 12:50:32 +0000 Date: Fri, 16 Feb 2024 13:50:31 +0100 (CET) From: Richard Biener To: gcc-patches@gcc.gnu.org cc: matz@suse.de, richard.sandiford@arm.com Subject: [PATCH][RFC] tree-optimization/113910 - bitmap_hash is weak, improve iterative_hash_* MIME-Version: 1.0 Message-Id: <20240216125032.5F11713343@imap2.dmz-prg2.suse.org> Authentication-Results: smtp-out2.suse.de; none X-Spam-Level: X-Spam-Score: -4.30 X-Spamd-Result: default: False [-4.30 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[3]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; RCVD_COUNT_THREE(0.00)[3]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-0.999]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Status: No, score=-9.8 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791059963569791829 X-GMAIL-MSGID: 1791059963569791829 The following addresses the weak bitmap_hash function which results in points-to analysis taking a long time because of a high collision rate in one of its bitmap hash tables. Using a better hash function like in the bitmap.cc hunk below doesn't help unless one also replaces the hash function in iterative_hash_* with something faster. I've taken the implementation from BFD string merging and extracted a 4 and 8 byte worker to replace iterative_hash_hashval_t and iterative_hash_host_wide_it. I didn't yet replace the generic iterative_hash as its implementation resides in libiberty. With this hash the testcase shows 5.15% 9323 cc1plus cc1plus [.] bitmap_hash and a compile-time of 44s while using the original hash implementation this becomes 10.50% 20405 cc1plus cc1plus [.] bitmap_hash and a compile-time of 46s, still faster than using original bitmap_hash which takes 56s and while having bitmap_hash off the profile shows 21.56% 49490 cc1plus cc1plus [.] bitmap_equal_p because of collision rates in the 20s. Bootstrapped / tested on x86_64-unknown-linux-gnu. OK for stage1? Should I try to change libiberty iterative_hash or implement a generic block variant for GCCs use with a different name, no longer using libibertys iterative_hash? Thanks, Richard. PR tree-optimization/113910 * inchash.h (iterative_hash_host_wide_int): Change hash function. (iterative_hash_hashval_t): Likewise. * bitmap.cc (bitmap_hash): Hash index and bits. --- gcc/bitmap.cc | 8 +++--- gcc/inchash.h | 80 +++++++++++++++++++++++++-------------------------- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/gcc/bitmap.cc b/gcc/bitmap.cc index 459e32c1ad1..f502841f385 100644 --- a/gcc/bitmap.cc +++ b/gcc/bitmap.cc @@ -2695,18 +2695,18 @@ hashval_t bitmap_hash (const_bitmap head) { const bitmap_element *ptr; - BITMAP_WORD hash = 0; + hashval_t hash = 0; int ix; gcc_checking_assert (!head->tree_form); for (ptr = head->first; ptr; ptr = ptr->next) { - hash ^= ptr->indx; + hash = iterative_hash_hashval_t (ptr->indx, hash); for (ix = 0; ix != BITMAP_ELEMENT_WORDS; ix++) - hash ^= ptr->bits[ix]; + hash = iterative_hash_host_wide_int (ptr->bits[ix], hash); } - return iterative_hash (&hash, sizeof (hash), 0); + return hash; } diff --git a/gcc/inchash.h b/gcc/inchash.h index e88f9b5eac1..4cdef1e7fce 100644 --- a/gcc/inchash.h +++ b/gcc/inchash.h @@ -28,8 +28,8 @@ along with GCC; see the file COPYING3. If not see Currently it just implements the plain old jhash based incremental hash from gcc's tree.cc. */ -hashval_t iterative_hash_host_wide_int (HOST_WIDE_INT, hashval_t); -hashval_t iterative_hash_hashval_t (hashval_t, hashval_t); +hashval_t iterative_hash_host_wide_int (uint64_t, hashval_t); +hashval_t iterative_hash_hashval_t (uint32_t, hashval_t); namespace inchash { @@ -157,57 +157,57 @@ class hash } -/* Borrowed from hashtab.c iterative_hash implementation. */ -#define mix(a,b,c) \ -{ \ - a -= b; a -= c; a ^= (c>>13); \ - b -= c; b -= a; b ^= (a<< 8); \ - c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \ - a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \ - b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \ - c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \ - a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \ - b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \ - c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \ -} - /* Produce good hash value combining VAL and VAL2. */ inline hashval_t -iterative_hash_hashval_t (hashval_t val, hashval_t val2) +iterative_hash_hashval_t (uint32_t val, hashval_t val2) { - /* the golden ratio; an arbitrary value. */ - hashval_t a = 0x9e3779b9; + static_assert (sizeof (hashval_t) == sizeof (uint32_t), ""); + + const uint32_t mul = ((1 << 0) + (1 << 2) + (1 << 3) + (1 << 5) + + (1 << 7) + (1 << 11) + (1 << 13) + (1 << 17) + + (0 << 19) + (1 << 23) + (1 << 29) + (1u << 31)); + + const unsigned len = 4; + hashval_t acc = val2 + len * 0x9e3779b1; + + uint16_t i1 = (uint16_t)val ^ (0x396cfeb8 + len); + uint16_t i2 = (uint16_t)(val >> 16) ^ (0xbe4ba423 + len); + hashval_t m = (uint32_t)i1 * i2; - mix (a, val, val2); - return val2; + acc += m; + acc = acc ^ (acc >> 7); + uint64_t r = (uint64_t)mul * acc; + return (uint32_t)r ^ (uint32_t)(r >> 32); } /* Produce good hash value combining VAL and VAL2. */ inline hashval_t -iterative_hash_host_wide_int (HOST_WIDE_INT val, hashval_t val2) +iterative_hash_host_wide_int (uint64_t val, hashval_t val2) { - if (sizeof (HOST_WIDE_INT) == sizeof (hashval_t)) - return iterative_hash_hashval_t (val, val2); - else - { - hashval_t a = (hashval_t) val; - /* Avoid warnings about shifting of more than the width of the type on - hosts that won't execute this path. */ - int zero = 0; - hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 8 + zero)); - mix (a, b, val2); - if (sizeof (HOST_WIDE_INT) > 2 * sizeof (hashval_t)) - { - hashval_t a = (hashval_t) (val >> (sizeof (hashval_t) * 16 + zero)); - hashval_t b = (hashval_t) (val >> (sizeof (hashval_t) * 24 + zero)); - mix (a, b, val2); - } - return val2; - } + static_assert (sizeof (hashval_t) == sizeof (uint32_t), ""); + static_assert (sizeof (HOST_WIDE_INT) == sizeof (uint64_t), ""); + + const uint32_t mul = ((1 << 0) + (1 << 2) + (1 << 3) + (1 << 5) + + (1 << 7) + (1 << 11) + (1 << 13) + (1 << 17) + + (0 << 19) + (1 << 23) + (1 << 29) + (1u << 31)); + + const unsigned len = 8; + hashval_t acc = val2 + len * 0x9e3779b1; + + uint32_t i1 = (uint32_t)val ^ (0x396cfeb8 + len); + uint32_t i2 = (uint32_t)(val >> 32) ^ (0xbe4ba423 + len); + uint64_t m64 = (uint64_t)i1 * i2; + hashval_t m = (uint32_t)m64 ^ (uint32_t)(m64 >> 32); + + acc += m; + acc = acc ^ (acc >> 7); + uint64_t r = (uint64_t)mul * acc; + return (uint32_t)r ^ (uint32_t)(r >> 32); } + #endif