From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 9B8BD5A0272 for ; Fri, 22 Sep 2023 16:06:42 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1695391594; bh=hb/Wm43HSjjVhgsjlLrMDig2NK3kz1kiK6hiEGxJHPI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b161PLatTl3WNX2uGa9q7nUyJV92QyrGtiHjD1w3Lwn962FqOaTvIrikahHQBXsz3 ZWAndL+jRfx/T232IKd2rlwgDIOhXfIF+r2jVRWRW+u9gL4ZE2AUG3tsZ63MR1mOGx Mos5tkV+xS1X+VMtLVB7To7KMQIe+1GryWxP60FA= Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4RsYwV1Tszz4xPR; Sat, 23 Sep 2023 00:06:34 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 09/10] siphash, checksum: Move TBAA explanation to checksum.c Date: Sat, 23 Sep 2023 00:06:29 +1000 Message-ID: <20230922140630.3184256-10-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230922140630.3184256-1-david@gibson.dropbear.id.au> References: <20230922140630.3184256-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: TKX763VWNPSJSAD7XTQAM7EA54BWYHDG X-Message-ID-Hash: TKX763VWNPSJSAD7XTQAM7EA54BWYHDG X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: A number of checksum and hash functions require workarounds for the odd behaviour of Type-Baased Alias Analysis. We have a detailed comment about this on siphash_8b() and other functions reference that. Move the main comment to csume_16b() instead, because we're going to reorganise things in siphash.c. Signed-off-by: David Gibson --- checksum.c | 19 ++++++++++++++----- siphash.c | 19 +++++-------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/checksum.c b/checksum.c index f2b82a4..03b8a7c 100644 --- a/checksum.c +++ b/checksum.c @@ -69,8 +69,17 @@ * * Return: 32-bit sum of 16-bit words */ +/* Type-Based Alias Analysis (TBAA) optimisation in gcc 11 and 12 (-flto -O2) + * makes these functions essentially useless by allowing reordering of stores of + * input data across function calls. Not even declaring @in as char pointer is + * enough: disable gcc's interpretation of strict aliasing altogether. See also: + * + * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106706 + * https://stackoverflow.com/questions/2958633/gcc-strict-aliasing-and-horror-stories + * https://lore.kernel.org/all/alpine.LFD.2.00.0901121128080.6528__33422.5328093909$1232291247$gmane$org@localhost.localdomain/ + */ /* NOLINTNEXTLINE(clang-diagnostic-unknown-attributes) */ -__attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */ +__attribute__((optimize("-fno-strict-aliasing"))) uint32_t sum_16b(const void *buf, size_t len) { const uint16_t *p = buf; @@ -110,7 +119,7 @@ uint16_t csum_fold(uint32_t sum) * Return: 16-bit IPv4-style checksum */ /* NOLINTNEXTLINE(clang-diagnostic-unknown-attributes) */ -__attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */ +__attribute__((optimize("-fno-strict-aliasing"))) /* See csum_16b() */ uint16_t csum_unaligned(const void *buf, size_t len, uint32_t init) { return (uint16_t)~csum_fold(sum_16b(buf, len) + init); @@ -247,7 +256,7 @@ void csum_icmp6(struct icmp6hdr *icmp6hr, * - coding style adaptation */ /* NOLINTNEXTLINE(clang-diagnostic-unknown-attributes) */ -__attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */ +__attribute__((optimize("-fno-strict-aliasing"))) /* See csum_16b() */ static uint32_t csum_avx2(const void *buf, size_t len, uint32_t init) { __m256i a, b, sum256, sum_a_hi, sum_a_lo, sum_b_hi, sum_b_lo, c, d; @@ -395,7 +404,7 @@ less_than_128_bytes: * Return: 16-bit folded, complemented checksum sum */ /* NOLINTNEXTLINE(clang-diagnostic-unknown-attributes) */ -__attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */ +__attribute__((optimize("-fno-strict-aliasing"))) /* See csum_16b() */ uint16_t csum(const void *buf, size_t len, uint32_t init) { return (uint16_t)~csum_fold(csum_avx2(buf, len, init)); @@ -412,7 +421,7 @@ uint16_t csum(const void *buf, size_t len, uint32_t init) * Return: 16-bit folded, complemented checksum */ /* NOLINTNEXTLINE(clang-diagnostic-unknown-attributes) */ -__attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */ +__attribute__((optimize("-fno-strict-aliasing"))) /* See csum_16b() */ uint16_t csum(const void *buf, size_t len, uint32_t init) { return csum_unaligned(buf, len, init); diff --git a/siphash.c b/siphash.c index 91bcc5d..d2b068c 100644 --- a/siphash.c +++ b/siphash.c @@ -24,17 +24,8 @@ * * Return: the 64-bit hash output */ -/* Type-Based Alias Analysis (TBAA) optimisation in gcc 11 and 12 (-flto -O2) - * makes these functions essentially useless by allowing reordering of stores of - * input data across function calls. Not even declaring @in as char pointer is - * enough: disable gcc's interpretation of strict aliasing altogether. See also: - * - * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106706 - * https://stackoverflow.com/questions/2958633/gcc-strict-aliasing-and-horror-stories - * https://lore.kernel.org/all/alpine.LFD.2.00.0901121128080.6528__33422.5328093909$1232291247$gmane$org@localhost.localdomain/ - */ /* NOLINTNEXTLINE(clang-diagnostic-unknown-attributes) */ -__attribute__((optimize("-fno-strict-aliasing"))) +__attribute__((optimize("-fno-strict-aliasing"))) /* See csum_16b() */ /* cppcheck-suppress unusedFunction */ uint64_t siphash_8b(const uint8_t *in, const uint64_t *k) { @@ -53,7 +44,7 @@ uint64_t siphash_8b(const uint8_t *in, const uint64_t *k) * Return: the 64-bit hash output */ /* NOLINTNEXTLINE(clang-diagnostic-unknown-attributes) */ -__attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */ +__attribute__((optimize("-fno-strict-aliasing"))) /* See csum_16b() */ /* cppcheck-suppress unusedFunction */ uint64_t siphash_12b(const uint8_t *in, const uint64_t *k) { @@ -73,7 +64,7 @@ uint64_t siphash_12b(const uint8_t *in, const uint64_t *k) * Return: the 64-bit hash output */ /* NOLINTNEXTLINE(clang-diagnostic-unknown-attributes) */ -__attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */ +__attribute__((optimize("-fno-strict-aliasing"))) /* See csum_16b() */ uint64_t siphash_20b(const uint8_t *in, const uint64_t *k) { struct siphash_state state = SIPHASH_INIT(k); @@ -94,7 +85,7 @@ uint64_t siphash_20b(const uint8_t *in, const uint64_t *k) * Return: the 64-bit hash output */ /* NOLINTNEXTLINE(clang-diagnostic-unknown-attributes) */ -__attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */ +__attribute__((optimize("-fno-strict-aliasing"))) /* See csum_16b() */ /* cppcheck-suppress unusedFunction */ uint64_t siphash_32b(const uint8_t *in, const uint64_t *k) { @@ -116,7 +107,7 @@ uint64_t siphash_32b(const uint8_t *in, const uint64_t *k) * Return: the 64-bit hash output */ /* NOLINTNEXTLINE(clang-diagnostic-unknown-attributes) */ -__attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */ +__attribute__((optimize("-fno-strict-aliasing"))) /* See csum_16b() */ uint64_t siphash_36b(const uint8_t *in, const uint64_t *k) { struct siphash_state state = SIPHASH_INIT(k); -- 2.41.0