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 1riLPQ-00Ghsj-OE for pgsql-hackers@arkaria.postgresql.org; Thu, 07 Mar 2024 21:36: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 1riLPO-008jye-Uf for pgsql-hackers@arkaria.postgresql.org; Thu, 07 Mar 2024 21:36:11 +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 1riLPO-008jyW-LH for pgsql-hackers@lists.postgresql.org; Thu, 07 Mar 2024 21:36:11 +0000 Received: from mail-il1-x12f.google.com ([2607:f8b0:4864:20::12f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1riLPK-003KHK-Bb for pgsql-hackers@lists.postgresql.org; Thu, 07 Mar 2024 21:36:10 +0000 Received: by mail-il1-x12f.google.com with SMTP id e9e14a558f8ab-3651edae0a1so523425ab.0 for ; Thu, 07 Mar 2024 13:36:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709847364; x=1710452164; 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=frA+E4WkXKYegtRi7u2oz8+XnbwqH3HK7JcA7X53Sug=; b=DPL2ta7WHgdr4h7eHVp9xnUgJX/cHklyrdy0l71sfz/VtUc8oHohX8d1N++r7b2dEb REjqjvXLC0tuWffNAczuKJexPIJdaWwtRsswdwI+rTpfeUVNL69mMRQGPiAcsnet6+cf UfKNFVHHTBnd2D7Bx6df+DTx/j137Obb9osoK1DHcizevleQx5yNJCB0vvhFhyDnr2He EqqhEM+DXz4pNxnEKf6yI3FR5ugCur94wGQEqhKntJ0pEqzILk4c6iatoMJosIYADXZg oAluln6QRd2+Mh/x6eLoy0+hcF4Iq4Kpe2YCiGjAOv5rTI7Ols4Kjt7FAZwHUhREO6f2 ciNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709847364; x=1710452164; 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=frA+E4WkXKYegtRi7u2oz8+XnbwqH3HK7JcA7X53Sug=; b=mUlVsn8qvGczGCxAsx94+pSee9BBIxcjUE5yVgec2z2DzBL84B8sh95czRi09C9fGY 640fA5su0I+IGecuvI7W5SXBJPJrn8S5YSsCdV1eVAUVkM7CS9chw8MSBU3EASKUos8N diz9YxwtAy1Eq0MgINbAY9JHvNTrm8eK+Dsjno+R9lAypNEBu7x+sqgQX/xExpkCAbyo kDeU/otRwu4ZeQCpZftzTBB+Qf3kV9/PpRzxxMNZW5yv+fNOSJDl3vxrGHvEN9gVfU43 eUciOFrTqFj2qug/hQQGeuYuPA1ip3/OMVY35Sm1s/KSOMMrzzq0l2LGWk4syVsKW6lT S7RQ== X-Forwarded-Encrypted: i=1; AJvYcCXATPJfhoslVHHbO6fAny+/WWOAoAsmAiY5/eSER97DN0PyiOrrmtKN9ykv2zwbl8HLdE13Rtr3nQ/cJFQQmALJ/708RCUYz5N8JTXJg+n5yPNX X-Gm-Message-State: AOJu0YwCidyhALx89TDE7y1ckH71gg0yz2XwYayUXooZSZbnBQu1chCC jgu2PG1a25n8yEF/Od4fMxzbAgxxRCRFFzMBP4ilr3vw5+372o6s X-Google-Smtp-Source: AGHT+IFBzG4s6Vt+OGHvrFfhV45d22xvacw6BUZh3MWkzEA1nvLViKjTqsYyra7x85+J1k5tLNFmHw== X-Received: by 2002:a05:6e02:154e:b0:365:a9fd:1a7a with SMTP id j14-20020a056e02154e00b00365a9fd1a7amr16774221ilu.26.1709847362913; Thu, 07 Mar 2024 13:36:02 -0800 (PST) Received: from nathanxps13 (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id el23-20020a0566384d9700b00474b1455a1asm4126404jab.170.2024.03.07.13.36.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Mar 2024 13:36:02 -0800 (PST) Date: Thu, 7 Mar 2024 15:36:00 -0600 From: Nathan Bossart To: "Amonson, Paul D" Cc: Andres Freund , Alvaro Herrera , "Shankaran, Akash" , Noah Misch , Tom Lane , Matthias van de Meent , "pgsql-hackers@lists.postgresql.org" Subject: Re: Popcount optimization using AVX512 Message-ID: <20240307213600.GA563369@nathanxps13> References: <20240301214457.GA3024365@nathanxps13> <20240304222118.GB3367383@nathanxps13> <20240305163730.GA3479896@nathanxps13> <20240307173318.GA455851@nathanxps13> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240307173318.GA455851@nathanxps13> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk As promised... > +# Check for Intel AVX512 intrinsics to do POPCNT calculations. > +# > +PGAC_AVX512_POPCNT_INTRINSICS([]) > +if test x"$pgac_avx512_popcnt_intrinsics" != x"yes"; then > + PGAC_AVX512_POPCNT_INTRINSICS([-mavx512vpopcntdq -mavx512f]) > +fi > +AC_SUBST(CFLAGS_AVX512_POPCNT) I'm curious why we need both -mavx512vpopcntdq and -mavx512f. On my machine, -mavx512vpopcntdq alone is enough to pass this test, so if there are other instructions required that need -mavx512f, then we might need to expand the test. > 13 files changed, 657 insertions(+), 119 deletions(-) I still think it's worth breaking this change into at least 2 patches. In particular, I think there's an opportunity to do the refactoring into pg_popcnt_choose.c and pg_popcnt_x86_64_accel.c prior to adding the AVX512 stuff. These changes are likely straightforward, and getting them out of the way early would make it easier to focus on the more interesting changes. IMHO there are a lot of moving parts in this patch. > +#undef HAVE__GET_CPUID_COUNT > + > +/* Define to 1 if you have immintrin. */ > +#undef HAVE__IMMINTRIN Is this missing HAVE__CPUIDEX? > uint64 > -pg_popcount(const char *buf, int bytes) > +pg_popcount_slow(const char *buf, int bytes) > { > uint64 popcnt = 0; > > -#if SIZEOF_VOID_P >= 8 > +#if SIZEOF_VOID_P == 8 > /* Process in 64-bit chunks if the buffer is aligned. */ > if (buf == (const char *) TYPEALIGN(8, buf)) > { > @@ -311,7 +224,7 @@ pg_popcount(const char *buf, int bytes) > > buf = (const char *) words; > } > -#else > +#elif SIZEOF_VOID_P == 4 > /* Process in 32-bit chunks if the buffer is aligned. */ > if (buf == (const char *) TYPEALIGN(4, buf)) > { Apologies for harping on this, but I'm still not seeing the need for these SIZEOF_VOID_P changes. While it's unlikely that this makes any practical difference, I see no reason to more strictly check SIZEOF_VOID_P here. > + /* Process any remaining bytes */ > + while (bytes--) > + popcnt += pg_number_of_ones[(unsigned char) *buf++]; > + return popcnt; > +#else > + return pg_popcount_slow(buf, bytes); > +#endif /* USE_AVX512_CODE */ nitpick: Could we call pg_popcount_slow() in a common section for these "remaining bytes?" > +#if defined(_MSC_VER) > + pg_popcount_indirect = pg_popcount512_fast; > +#else > + pg_popcount = pg_popcount512_fast; > +#endif These _MSC_VER sections are interesting. I'm assuming this is the workaround for the MSVC linking issue you mentioned above. I haven't looked too closely, but I wonder if the CRC32C code (see src/include/port/pg_crc32c.h) is doing something different to avoid this issue. Upthread, Alvaro suggested a benchmark [0] that might be useful. I scanned through this thread and didn't see any recent benchmark results for the latest form of the patch. I think it's worth verifying that we are still seeing the expected improvements. [0] https://postgr.es/m/202402071953.5c4z7t6kl7ts%40alvherre.pgsql -- Nathan Bossart Amazon Web Services: https://aws.amazon.com