Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rxZmj-009n9u-Cu for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Apr 2024 21:59:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rxZmg-00AoKQ-W0 for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Apr 2024 21:59:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rxZmg-00AoKI-IK for pgsql-hackers@lists.postgresql.org; Thu, 18 Apr 2024 21:59:10 +0000 Received: from mail-il1-x12d.google.com ([2607:f8b0:4864:20::12d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rxZmd-001h6B-Ia for pgsql-hackers@lists.postgresql.org; Thu, 18 Apr 2024 21:59:09 +0000 Received: by mail-il1-x12d.google.com with SMTP id e9e14a558f8ab-36afb4b4a3eso4565275ab.3 for ; Thu, 18 Apr 2024 14:59:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713477545; x=1714082345; darn=lists.postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=HBYVAKvAMrI2qqLyWjFbesQ6Oe3ryuawqQ3qjFS6MC8=; b=dvSCNcG4lTFaA97+V5R4Rfq0zVFHT9c+km+EuBgSpnkgx4v4F0MuMPvq5zksYYEagW uEmq7sDeLyzOfcrCJRF9hRfQ0v9Y3NHypmXkRoLIMeeL0rrBzoxhjo1Y8uMItSZ9PozE B16SfeaylbUra+HSuKrqHf6NLEIBjO6Jbdt0/GwmrRLi9xn0lIO6ACCwXyqyuurMKhjY 1HJFE1c7m8xC/9OT99q3j+zWSVpjnnKh2OQYdTsNtXhAgZ231b9CivFFJurJbWmw0wyA IlqyikVPcG7gs2bmuHgT3sURZN2rdhtZEGlIjzxow5+V4EZlTqm/THVcX9VRQ9m526xP Y14w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713477545; x=1714082345; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=HBYVAKvAMrI2qqLyWjFbesQ6Oe3ryuawqQ3qjFS6MC8=; b=SNdfNqj0Mdm9o+Oc1vMSK3iSlOgu2PHiJ+YNMrLGyKQ1iRjUsEiN773jb903i3wZ9M I9+yLZ9yR7njsH78ZyZkfL0clgmc1zJKztuCbI0ub8JUYXrXDU43mLlPVH7iq5fmUoGY CdTQ2BKTJ1T0ZoeA4RBn6wB+fpL5Bp/n72nOkwE2IzN1k0UShGPjkLX0GQZPUFalyHW9 ljdXm1eKQccTVrK5tQmtJ7wrrFk7t01sfEFL+ziPkc4FO93I0Hjev4x9pNhtMX8qjCQb gjs1otIxL9w7r4YSQT03+EwBpK+pQpru2jAw0S3VIvbTP4r2Kc93EZoW2Bfdx/wgI3tp FcoA== X-Forwarded-Encrypted: i=1; AJvYcCVtS9xsVoBO+jeCi76TSIZx8zg0/66z8vlPc98ri/RBPtd9zlahP1nEi1ji3mccK1qhmItowNfq7TBPtn68+mUuGW/l2+TeaQmQfafJz6r6cqB7 X-Gm-Message-State: AOJu0YyCeEqNHlLLPFvj+wNLFtxGVmhFdwoSaf5kaiV08gP091mplZeQ EV1+DqpywR0vKD+p97D8RjAg5jM0+cfT2qJvM3WacTYie4Aox0H6 X-Google-Smtp-Source: AGHT+IH1AdZJ2GyL1eZiQSHvHG3F+sQi7P/yWJ0xv5R7mRgF+vE2EeG+SSjV2xGH7Aly5biLiW4xkg== X-Received: by 2002:a05:6e02:1389:b0:36b:2c9f:425e with SMTP id d9-20020a056e02138900b0036b2c9f425emr550153ilo.4.1713477545341; Thu, 18 Apr 2024 14:59:05 -0700 (PDT) Received: from nathanxps13 (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id du12-20020a056638604c00b00482b2dc023esm668731jab.18.2024.04.18.14.59.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 14:59:04 -0700 (PDT) Date: Thu, 18 Apr 2024 16:59:02 -0500 From: Nathan Bossart To: "Devulapalli, Raghuveer" Cc: "Shankaran, Akash" , Tom Lane , David Rowley , Ants Aasma , Alvaro Herrera , "Amonson, Paul D" , Andres Freund , Noah Misch , Matthias van de Meent , "pgsql-hackers@lists.postgresql.org" Subject: Re: Popcount optimization using AVX512 Message-ID: <20240418215902.GA3830246@nathanxps13> References: <2999492.1712536932@sss.pgh.pa.us> <20240408012332.GA1351547@nathanxps13> <20240408013002.GA1352058@nathanxps13> <3010179.1712540139@sss.pgh.pa.us> <20240418024459.GA3385227@nathanxps13> <20240418195346.GA3506520@nathanxps13> <20240418210158.GA3776258@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="x+6KMIRAuhnl3hBn" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --x+6KMIRAuhnl3hBn Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Thu, Apr 18, 2024 at 09:29:55PM +0000, Devulapalli, Raghuveer wrote: > (1) Shouldn't it be: return (_xgetbv(0) & 0xe6) == 0xe6; ? Otherwise > zmm_regs_available() will return false.. Yes, that's a mistake. I fixed that in v3. > (2) Nitpick: avx512_popcnt_available and avx512_bw_available() run the > same cpuid leaf. You could combine them into one to avoid running cpuid > twice. My apologies, I should have mentioned this before.. Good call. The byte-and-word instructions were a late addition to the patch, so I missed this originally. On that note, is it necessary to also check for avx512f? At the moment, we are assuming that's supported if the other AVX-512 instructions are available. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --x+6KMIRAuhnl3hBn Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v3-0001-osxsave.patch" From e04c348eb389c6aa1597ac35d57b5e7ae7075381 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Thu, 18 Apr 2024 15:57:56 -0500 Subject: [PATCH v3 1/1] osxsave --- src/port/pg_popcount_avx512_choose.c | 80 ++++++++++++++++------------ 1 file changed, 47 insertions(+), 33 deletions(-) diff --git a/src/port/pg_popcount_avx512_choose.c b/src/port/pg_popcount_avx512_choose.c index ae3fa3d306..b37107803a 100644 --- a/src/port/pg_popcount_avx512_choose.c +++ b/src/port/pg_popcount_avx512_choose.c @@ -34,39 +34,13 @@ #ifdef TRY_POPCNT_FAST /* - * Returns true if the CPU supports the instructions required for the AVX-512 - * pg_popcount() implementation. + * Does CPUID say there's support for XSAVE instructions? */ -bool -pg_popcount_avx512_available(void) +static inline bool +xsave_available(void) { unsigned int exx[4] = {0, 0, 0, 0}; - /* Does CPUID say there's support for AVX-512 popcount instructions? */ -#if defined(HAVE__GET_CPUID_COUNT) - __get_cpuid_count(7, 0, &exx[0], &exx[1], &exx[2], &exx[3]); -#elif defined(HAVE__CPUIDEX) - __cpuidex(exx, 7, 0); -#else -#error cpuid instruction not available -#endif - if ((exx[2] & (1 << 14)) == 0) /* avx512-vpopcntdq */ - return false; - - /* Does CPUID say there's support for AVX-512 byte and word instructions? */ - memset(exx, 0, sizeof(exx)); -#if defined(HAVE__GET_CPUID_COUNT) - __get_cpuid_count(7, 0, &exx[0], &exx[1], &exx[2], &exx[3]); -#elif defined(HAVE__CPUIDEX) - __cpuidex(exx, 7, 0); -#else -#error cpuid instruction not available -#endif - if ((exx[1] & (1 << 30)) == 0) /* avx512-bw */ - return false; - - /* Does CPUID say there's support for XSAVE instructions? */ - memset(exx, 0, sizeof(exx)); #if defined(HAVE__GET_CPUID) __get_cpuid(1, &exx[0], &exx[1], &exx[2], &exx[3]); #elif defined(HAVE__CPUID) @@ -74,15 +48,55 @@ pg_popcount_avx512_available(void) #else #error cpuid instruction not available #endif - if ((exx[2] & (1 << 26)) == 0) /* xsave */ - return false; + return (exx[2] & (1 << 27)) != 0; /* osxsave */ +} - /* Does XGETBV say the ZMM registers are enabled? */ +/* + * Does XGETBV say the ZMM registers are enabled? + * + * NB: Caller is responsible for verifying that xsave_available() returns true + * before calling this. + */ +static inline bool +zmm_regs_available(void) +{ #ifdef HAVE_XSAVE_INTRINSICS - return (_xgetbv(0) & 0xe0) != 0; + return (_xgetbv(0) & 0xe6) == 0xe6; #else return false; #endif } +/* + * Does CPUID say there's support for AVX-512 popcount and byte-and-word + * instructions? + */ +static inline bool +avx512_popcnt_available(void) +{ + unsigned int exx[4] = {0, 0, 0, 0}; + +#if defined(HAVE__GET_CPUID_COUNT) + __get_cpuid_count(7, 0, &exx[0], &exx[1], &exx[2], &exx[3]); +#elif defined(HAVE__CPUIDEX) + __cpuidex(exx, 7, 0); +#else +#error cpuid instruction not available +#endif + return (exx[2] & (1 << 14)) != 0 && /* avx512-vpopcntdq */ + (exx[1] & (1 << 30)) != 0; /* avx512-bw */ +} + +/* + * Returns true if the CPU supports the instructions required for the AVX-512 + * pg_popcount() implementation. + */ +bool +pg_popcount_avx512_available(void) +{ + return xsave_available() && + zmm_regs_available() && + avx512_popcnt_available(); +} + #endif /* TRY_POPCNT_FAST */ -- 2.25.1 --x+6KMIRAuhnl3hBn--