From patchwork Fri May 3 15:13:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 794387 Delivered-To: patch@linaro.org Received: by 2002:adf:a153:0:b0:34d:5089:5a9e with SMTP id r19csp788781wrr; Fri, 3 May 2024 08:14:24 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVsnKMHXPdNJUtOiqupszHh6+xXrEGeT4R4DYtRETWFDf1Q5FLIdcONWqDuaTcvfQfeESRBfGJQSwNZXCwqP4Ze X-Google-Smtp-Source: AGHT+IGFuc3fuT4o4ULVTQKX4mlPBO3+Z4mttFNFCDvr4OZyPOrnkq6+bOWXo4XlQ0rjEGxmb7Rj X-Received: by 2002:a05:622a:2d1:b0:43b:c4:4188 with SMTP id a17-20020a05622a02d100b0043b00c44188mr3324574qtx.41.1714749264545; Fri, 03 May 2024 08:14:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1714749264; cv=none; d=google.com; s=arc-20160816; b=fVSuv0fnZ1+7kaFb43m4d6ILZHMykMv7srWLVwDeknYatOarQ3Wte/422cvG0T6bCa cnGZ8RSBfrBUVocjDSiRV/WJkRJhvI6U4V2remHIRfa1of8Ua76ywx+eratJOC8Z6pnm 6wK9k2lO3Dh3TAOO/utfEbz5msbbmZ1DncSeifZ+dYKiO6//j9zrzP5LFvuNGDlYZ7Jd /qbT/I3k4VQMSyt+YrIi08XFwM3gqM2pp5KfGtH/3eCXfz3vTQjyY9LemQo5VApzlT6W yvHU7YiLyf9gPENEQjpJGPRtwvjLKy0NE+QvC7i0YfxKWyJmVl+krF1ghN5izNHA3XJa ysNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Aco89HGhDUqxtzZSl7gQEXCIzX7CLhr3rgmAxnnD0GI=; fh=IOfZmL/4G2LGBtSV+LzySu7eotL7HJ1AQcRx3etIBXU=; b=l1LzSVxdbBgibw49psUdvlhzf9n6VHwEvN4yISJm0AsU/Ps3GnWRYPbfZKfRglw7J5 ULDrkS8w4GP+xvbonkUs6D0hlMJ9zWCSsl+7qFKx90ELG5NL652roSpu6e1heirF7m+H bVNZJb5f5afBRKVHzxIsj5Mj/qcYMyXMLV1G5WUFacugpi8iaeCvGschmRYfMHUaz2SB bYuKe5THd6z4X4ZcuURhWGDzfRone8zyuPWDduIp04UzKfPAQNoEdnR3IwbGszMVZYHz 6bosSv6vjmdfu/0rMrutSW3IA9fjy15zUonuxPWU5DXMomJUzF5S3tJtthNSPJ+NEsKG uXSg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ZJtpmc25; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id v5-20020ac85785000000b0043aedd74579si3681241qta.287.2024.05.03.08.14.24 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 03 May 2024 08:14:24 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ZJtpmc25; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s2ubH-0005my-HK; Fri, 03 May 2024 11:13:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s2ubD-0005jg-O6 for qemu-devel@nongnu.org; Fri, 03 May 2024 11:13:23 -0400 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1s2ubA-00075F-Ni for qemu-devel@nongnu.org; Fri, 03 May 2024 11:13:23 -0400 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-1e4266673bbso83928225ad.2 for ; Fri, 03 May 2024 08:13:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1714749199; x=1715353999; darn=nongnu.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=Aco89HGhDUqxtzZSl7gQEXCIzX7CLhr3rgmAxnnD0GI=; b=ZJtpmc25y35pC+s7AZ5mKmcMQf2jbRLNyCOtIXMs/ZTmzT1SIyCsjf4ijvZWOiaFep mlWoEdm7ZRN3gOdIABc+1qgypua1fQBCXVoqNNw6vY2HX6UM0CHaodAG2rF3wkfWBWlg I/aa8OuXAtmC7Tx0T/Ark6DLiQfevhPn6R8u0tsKaJ1YVO+MmFsJbxwWf4AhlzVQyyWY Tm43nnjLjHbzA4wpifcUZU6NoXnm+k47MHYPPuFXsbV5zRZBZKHfgawt+gdnVUTQr0Pg p4mFSF5QG7qEVaaTM9v6gYEQptzyheDs6Y7/AJ1ikudKsRr9MWoE69T82r0W+QXnsA7Y Z5JA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714749199; x=1715353999; 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=Aco89HGhDUqxtzZSl7gQEXCIzX7CLhr3rgmAxnnD0GI=; b=dE5y5dbrgNSoTyaveT2aMHtg4UUvJfU1n+aSvBr76JH165fHp9snIU6Av6zwYi8Qe0 xu8//g68ebekziKzXndSQ7t6yqcdLZcu9+2yhJBf01IxRTPaE97j0eQ74BD6VwUr6Z9t Hty+N39YjNn8V+7OgPA7gsbMqgnbB3HTDuud1uRXi6cp4tqrRtxcQ9sK5kJTkDMzborR KZBQaVqiSaMRGgJkd0pMX3orXOMbSt8Fasxz5zYPGwRvIg3/zZ6tmnXjf7VJP9t24J+j KijGbrHb/nygakuS5BbPyfhlLFkhJ/RzO6N8QMzqt5e3uhXPQoJcv6J2azGdwstig970 qAHw== X-Gm-Message-State: AOJu0YycUObg96EIc/HLjOwPyX64bJdEEOpJuJmnyferSHuFtQw9w7zW Pt8aHytXvHmZOZeejj9bYTYrNA3p+RxDE0XxQ8h4vURIGQ6n86K2RUMyongsMil1f8SudAdXJf5 J X-Received: by 2002:a17:902:9a94:b0:1e8:32ed:6f6d with SMTP id w20-20020a1709029a9400b001e832ed6f6dmr2563343plp.39.1714749199132; Fri, 03 May 2024 08:13:19 -0700 (PDT) Received: from stoup.. (174-21-72-5.tukw.qwest.net. [174.21.72.5]) by smtp.gmail.com with ESMTPSA id p10-20020a170902c70a00b001e81c778784sm3366611plp.67.2024.05.03.08.13.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 May 2024 08:13:18 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Alexander Monakov , Mikhail Romanov Subject: [PULL 03/10] util/bufferiszero: Reorganize for early test for acceleration Date: Fri, 3 May 2024 08:13:07 -0700 Message-Id: <20240503151314.336357-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240503151314.336357-1-richard.henderson@linaro.org> References: <20240503151314.336357-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::630; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x630.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Alexander Monakov Test for length >= 256 inline, where is is often a constant. Before calling into the accelerated routine, sample three bytes from the buffer, which handles most non-zero buffers. Signed-off-by: Alexander Monakov Signed-off-by: Mikhail Romanov Message-Id: <20240206204809.9859-3-amonakov@ispras.ru> [rth: Use __builtin_constant_p; move the indirect call out of line.] Signed-off-by: Richard Henderson --- include/qemu/cutils.h | 32 ++++++++++++++++- util/bufferiszero.c | 84 +++++++++++++++++-------------------------- 2 files changed, 63 insertions(+), 53 deletions(-) diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h index 92c927a6a3..741dade7cf 100644 --- a/include/qemu/cutils.h +++ b/include/qemu/cutils.h @@ -187,9 +187,39 @@ char *freq_to_str(uint64_t freq_hz); /* used to print char* safely */ #define STR_OR_NULL(str) ((str) ? (str) : "null") -bool buffer_is_zero(const void *buf, size_t len); +/* + * Check if a buffer is all zeroes. + */ + +bool buffer_is_zero_ool(const void *vbuf, size_t len); +bool buffer_is_zero_ge256(const void *vbuf, size_t len); bool test_buffer_is_zero_next_accel(void); +static inline bool buffer_is_zero_sample3(const char *buf, size_t len) +{ + /* + * For any reasonably sized buffer, these three samples come from + * three different cachelines. In qemu-img usage, we find that + * each byte eliminates more than half of all buffer testing. + * It is therefore critical to performance that the byte tests + * short-circuit, so that we do not pull in additional cache lines. + * Do not "optimize" this to !(a | b | c). + */ + return !buf[0] && !buf[len - 1] && !buf[len / 2]; +} + +#ifdef __OPTIMIZE__ +static inline bool buffer_is_zero(const void *buf, size_t len) +{ + return (__builtin_constant_p(len) && len >= 256 + ? buffer_is_zero_sample3(buf, len) && + buffer_is_zero_ge256(buf, len) + : buffer_is_zero_ool(buf, len)); +} +#else +#define buffer_is_zero buffer_is_zero_ool +#endif + /* * Implementation of ULEB128 (http://en.wikipedia.org/wiki/LEB128) * Input is limited to 14-bit numbers diff --git a/util/bufferiszero.c b/util/bufferiszero.c index 641d5f9b9e..972f394cbd 100644 --- a/util/bufferiszero.c +++ b/util/bufferiszero.c @@ -26,8 +26,9 @@ #include "qemu/bswap.h" #include "host/cpuinfo.h" -static bool -buffer_zero_int(const void *buf, size_t len) +static bool (*buffer_is_zero_accel)(const void *, size_t); + +static bool buffer_is_zero_integer(const void *buf, size_t len) { if (unlikely(len < 8)) { /* For a very small buffer, simply accumulate all the bytes. */ @@ -128,60 +129,38 @@ buffer_zero_avx2(const void *buf, size_t len) } #endif /* CONFIG_AVX2_OPT */ -/* - * Make sure that these variables are appropriately initialized when - * SSE2 is enabled on the compiler command-line, but the compiler is - * too old to support CONFIG_AVX2_OPT. - */ -#if defined(CONFIG_AVX2_OPT) -# define INIT_USED 0 -# define INIT_LENGTH 0 -# define INIT_ACCEL buffer_zero_int -#else -# ifndef __SSE2__ -# error "ISA selection confusion" -# endif -# define INIT_USED CPUINFO_SSE2 -# define INIT_LENGTH 64 -# define INIT_ACCEL buffer_zero_sse2 -#endif - -static unsigned used_accel = INIT_USED; -static unsigned length_to_accel = INIT_LENGTH; -static bool (*buffer_accel)(const void *, size_t) = INIT_ACCEL; - static unsigned __attribute__((noinline)) select_accel_cpuinfo(unsigned info) { /* Array is sorted in order of algorithm preference. */ static const struct { unsigned bit; - unsigned len; bool (*fn)(const void *, size_t); } all[] = { #ifdef CONFIG_AVX2_OPT - { CPUINFO_AVX2, 128, buffer_zero_avx2 }, + { CPUINFO_AVX2, buffer_zero_avx2 }, #endif - { CPUINFO_SSE2, 64, buffer_zero_sse2 }, - { CPUINFO_ALWAYS, 0, buffer_zero_int }, + { CPUINFO_SSE2, buffer_zero_sse2 }, + { CPUINFO_ALWAYS, buffer_is_zero_integer }, }; for (unsigned i = 0; i < ARRAY_SIZE(all); ++i) { if (info & all[i].bit) { - length_to_accel = all[i].len; - buffer_accel = all[i].fn; + buffer_is_zero_accel = all[i].fn; return all[i].bit; } } return 0; } -#if defined(CONFIG_AVX2_OPT) +static unsigned used_accel; + static void __attribute__((constructor)) init_accel(void) { used_accel = select_accel_cpuinfo(cpuinfo_init()); } -#endif /* CONFIG_AVX2_OPT */ + +#define INIT_ACCEL NULL bool test_buffer_is_zero_next_accel(void) { @@ -194,36 +173,37 @@ bool test_buffer_is_zero_next_accel(void) used_accel |= used; return used; } - -static bool select_accel_fn(const void *buf, size_t len) -{ - if (likely(len >= length_to_accel)) { - return buffer_accel(buf, len); - } - return buffer_zero_int(buf, len); -} - #else -#define select_accel_fn buffer_zero_int bool test_buffer_is_zero_next_accel(void) { return false; } + +#define INIT_ACCEL buffer_is_zero_integer #endif -/* - * Checks if a buffer is all zeroes - */ -bool buffer_is_zero(const void *buf, size_t len) +static bool (*buffer_is_zero_accel)(const void *, size_t) = INIT_ACCEL; + +bool buffer_is_zero_ool(const void *buf, size_t len) { if (unlikely(len == 0)) { return true; } + if (!buffer_is_zero_sample3(buf, len)) { + return false; + } + /* All bytes are covered for any len <= 3. */ + if (unlikely(len <= 3)) { + return true; + } - /* Fetch the beginning of the buffer while we select the accelerator. */ - __builtin_prefetch(buf); - - /* Use an optimized zero check if possible. Note that this also - includes a check for an unrolled loop over 64-bit integers. */ - return select_accel_fn(buf, len); + if (likely(len >= 256)) { + return buffer_is_zero_accel(buf, len); + } + return buffer_is_zero_integer(buf, len); +} + +bool buffer_is_zero_ge256(const void *buf, size_t len) +{ + return buffer_is_zero_accel(buf, len); }