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 1rYViE-00GMJt-Q5 for pgsql-hackers@arkaria.postgresql.org; Fri, 09 Feb 2024 18:34:59 +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 1rYViD-00AujI-Lw for pgsql-hackers@arkaria.postgresql.org; Fri, 09 Feb 2024 18:34:57 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rYViD-00Auhk-87 for pgsql-hackers@lists.postgresql.org; Fri, 09 Feb 2024 18:34:57 +0000 Received: from wout1-smtp.messagingengine.com ([64.147.123.24]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rYViA-005vZQ-IP for pgsql-hackers@lists.postgresql.org; Fri, 09 Feb 2024 18:34:56 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.west.internal (Postfix) with ESMTP id AFE473200A04; Fri, 9 Feb 2024 13:34:52 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Fri, 09 Feb 2024 13:34:53 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1707503692; x=1707590092; bh=zOpLnG47ff wLkI8RVypI3flTWVUNKbSTgjQhZZziU+w=; b=ZFUgkWdHFlOkQ7mLkDebMXFJBo pLhq0wtQIf5AmWDRIUyTxgA6jAmyDWxE2DDtkLypBjse7R/6GRlPVmjqvrog0nIG yitj44xOTxz91wV73l5fFbtjBo9AyhvciUCm7iFsXfMPO7NkY4HtYUFaqo2DCRo5 SObFhEjTtRL3eryhEpjG70w0fbnir8P9wgTRxeRDCizUtqNYqUM2Y8pu6JzKIMjl 5gc+JBtbD1p6I0qZyq5VpP5Ncd4CzfB+oYQO+E/OTsL2J8sB9kMpwGCjWivHLIIl MUp/h3VmBL6AN/2T1WGN1oulPjy6BVU+BH0xX01giWys3inzXCx1pjc5dApA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1707503692; x=1707590092; bh=zOpLnG47ffwLkI8RVypI3flTWVUN KbSTgjQhZZziU+w=; b=NMeRakCQJNpSe/DA5yUePpSoGWkMQumSDao45Ax4zFpK 4BnF9sGJrmSznsjJ7oThkZIvURbdneQRq3uCGxE1E/487P7RzYMy4Z7APBqzUM3c scQpuvLephgyzO07LWyAc1LFQSUsLHRMFbEx0/iOyEDmIBdDotHjR/kHWkS+8gaa /VsaikKHVP0512WUd9lwobc7p2Stt/5y/5Mgw6RZpbaIm+l5q0zWcyc6wGHa64LO pZD/Nfx9YAPaTTOPuvxvk6jFw1iAR9WfWsGBVG0JJJueiq3cPS8d241KYeqv9F5b h4D2ilOwx+ij6YUWvO9id5/piNYAZtpoHNp4bMMoCA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrtdeigdduuddvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomheptehnughr vghsucfhrhgvuhhnugcuoegrnhgurhgvshesrghnrghrrgiivghlrdguvgeqnecuggftrf grthhtvghrnhepvdfffeevhfetveffgeeiteefhfdtvdffjeevhfeuteegleduheetvedu ieettddunecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh eprghnughrvghssegrnhgrrhgriigvlhdruggv X-ME-Proxy: Feedback-ID: id4a34324:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 9 Feb 2024 13:34:51 -0500 (EST) Date: Fri, 9 Feb 2024 10:34:50 -0800 From: Andres Freund To: "Amonson, Paul D" Cc: Alvaro Herrera , "Shankaran, Akash" , Nathan Bossart , Noah Misch , Tom Lane , Matthias van de Meent , "pgsql-hackers@lists.postgresql.org" Subject: Re: Popcount optimization using AVX512 Message-ID: <20240209183450.snllkk7erharc73y@awork3.anarazel.de> References: <202402071013.reqe3bdnjjuj@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On 2024-02-09 17:39:46 +0000, Amonson, Paul D wrote: > diff --git a/meson.build b/meson.build > index 8ed51b6aae..1e7a4dc942 100644 > --- a/meson.build > +++ b/meson.build > @@ -1773,6 +1773,45 @@ elif cc.links(''' > endif > > > +# XXX: The configure.ac check for __cpuidex() is broken, we don't copy that > +# here. To prevent problems due to two detection methods working, stop > +# checking after one. This seems like a bogus copy-paste. > +if cc.links(''' > + #include > + int main(int arg, char **argv) > + { > + unsigned int exx[4] = {0, 0, 0, 0}; > + __get_cpuid_count(7, 0, &exx[0], &exx[1], &exx[2], &exx[3]); > + } > + ''', name: '__get_cpuid_count', > + args: test_c_args) > + cdata.set('HAVE__GET_CPUID_COUNT', 1) > +elif cc.links(''' > + #include > + int main(int arg, char **argv) > + { > + unsigned int exx[4] = {0, 0, 0, 0}; > + __cpuidex(exx, 7, 0); > + } > + ''', name: '__cpuidex', > + args: test_c_args) > + cdata.set('HAVE__CPUIDEX', 1) > +endif > + > + > +# Check for header immintrin.h > +if cc.links(''' > + #include > + int main(int arg, char **argv) > + { > + return 1701; > + } > + ''', name: '__immintrin', > + args: test_c_args) > + cdata.set('HAVE__IMMINTRIN', 1) > +endif Do these all actually have to link? Invoking the linker is slow. I think you might be able to just use cc.has_header_symbol(). > +############################################################### > +# AVX 512 POPCNT Intrinsic check > +############################################################### > +have_avx512_popcnt = false > +cflags_avx512_popcnt = [] > +if host_cpu == 'x86_64' > + prog = ''' > + #include > + #include > + void main(void) > + { > + __m512i tmp __attribute__((aligned(64))); > + __m512i input = _mm512_setzero_si512(); > + __m512i output = _mm512_popcnt_epi64(input); > + uint64_t cnt = 999; > + _mm512_store_si512(&tmp, output); > + cnt = _mm512_reduce_add_epi64(tmp); > + /* return computed value, to prevent the above being optimized away */ > + return cnt == 0; > + }''' Does this work with msvc? > + if cc.links(prog, name: '_mm512_setzero_si512, _mm512_popcnt_epi64, _mm512_store_si512, and _mm512_reduce_add_epi64 with -mavx512vpopcntdq -mavx512f', That's a very long line in the output, how about using the avx feature name or something? > diff --git a/src/port/Makefile b/src/port/Makefile > index dcc8737e68..6a01a7d89a 100644 > --- a/src/port/Makefile > +++ b/src/port/Makefile > @@ -87,6 +87,11 @@ pg_crc32c_sse42.o: CFLAGS+=$(CFLAGS_CRC) > pg_crc32c_sse42_shlib.o: CFLAGS+=$(CFLAGS_CRC) > pg_crc32c_sse42_srv.o: CFLAGS+=$(CFLAGS_CRC) > > +# Newer Intel processors can use AVX-512 POPCNT Capabilities (01/30/2024) > +pg_bitutils.o: CFLAGS+=$(CFLAGS_AVX512_POPCNT) > +pg_bitutils_shlib.o: CFLAGS+=$(CFLAGS_AVX512_POPCNT) > +pg_bitutils_srv.o:CFLAGS+=$(CFLAGS_AVX512_POPCNT) > + > # all versions of pg_crc32c_armv8.o need CFLAGS_CRC > pg_crc32c_armv8.o: CFLAGS+=$(CFLAGS_CRC) > pg_crc32c_armv8_shlib.o: CFLAGS+=$(CFLAGS_CRC) > diff --git a/src/port/meson.build b/src/port/meson.build > index 69b30ab21b..1c48a3b07e 100644 > --- a/src/port/meson.build > +++ b/src/port/meson.build > @@ -184,6 +184,7 @@ foreach name, opts : pgport_variants > link_with: cflag_libs, > c_pch: pch_c_h, > kwargs: opts + { > + 'c_args': opts.get('c_args', []) + cflags_avx512_popcnt, > 'dependencies': opts['dependencies'] + [ssl], > } > ) This will build all of pgport with the avx flags, which wouldn't be correct, I think? The compiler might inject automatic uses of avx512 in places, which would cause problems, no? While you don't do the same for make, isn't even just using the avx512 for all of pg_bitutils.c broken for exactly that reson? That's why the existing code builds the files for various crc variants as their own file. Greetings, Andres Freund