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 1rmLA4-007KJW-8e for pgsql-hackers@arkaria.postgresql.org; Mon, 18 Mar 2024 22:08:52 +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 1rmLA2-006dAh-T4 for pgsql-hackers@arkaria.postgresql.org; Mon, 18 Mar 2024 22:08:51 +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 1rmLA2-006dAZ-Fe for pgsql-hackers@lists.postgresql.org; Mon, 18 Mar 2024 22:08:51 +0000 Received: from mail-il1-x130.google.com ([2607:f8b0:4864:20::130]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rmL9y-005DHs-TM for pgsql-hackers@lists.postgresql.org; Mon, 18 Mar 2024 22:08:49 +0000 Received: by mail-il1-x130.google.com with SMTP id e9e14a558f8ab-3667770b8e8so27079975ab.0 for ; Mon, 18 Mar 2024 15:08:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710799727; x=1711404527; 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=/jNdz5KSfSuf64BDZJNOs48SfeDOiy6PWwFpzwZdnx8=; b=QmsYWFkYwR0/YdoGxMuS9sgFN6GdwDPLTbnP/x2NeCxJQLu2X+5KOqO3zBxccgC3jC VJERwqocpxbeoP47a4uFEhpp5EnBXkbDs+Nf5cMK3iQl2MTLynvQZIXkeYOFhUusF5S2 w3tS2AryyiOVuBUTVCh3zeqo/ViYCMZw7TXLndxOBDMSixPQe/kOLcb/NH1SGNUUyKAp hKw6zMXPKlAW4/Svx51wkCRSBAEjxC58T95iRD9kv5prXbBK6722kEbSSQkihMo5PUGF 4GME2xlFa7APSjbNmiseNzXyGkSzTy2XONbwE919Aqtkzr+VSxn5SGCZUhf1DczFmH3M O5aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710799727; x=1711404527; 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=/jNdz5KSfSuf64BDZJNOs48SfeDOiy6PWwFpzwZdnx8=; b=erryTEirgjehUVhlChSf4WkUSSW1Mrk4BELT1bDKHjyfmRyPhUycAIzS7yrvWxk0bN R71vs8uaFT5XLU1hkdHoxVGfZ38+zMx/szG9eRrMDUGjgz5dbuXEAR//oPOkqvOMSPk/ a37h/KiqlYVXKY0CxP6CYQJ/iNRR+sFSTVhe8LGy+EZTTyoyJwxcBQcl47nVkvS6yv0Z EkgBWaOn/ZHJLnknU1+mB406LqjXlu/4kf7/QUFE6APPsK2l8mbjmBKMFwb5geg7qoZg Ue+5+qaNJqSGRJUlJZhpUyRtFgOuax+dA6OE87/4+/XW1pRuu/ALJXY82Lz3UP9OGWDt rioQ== X-Forwarded-Encrypted: i=1; AJvYcCWzoWMduHJYOjvXa4KGGapvUh6JC2XQgq5VQ739ySgO/DRzOlpri/5ajoiRbMPhamhkijaOTR4D4lPRHXwc0uuXhGPTa0J21+BaADCZ7sntXPvr X-Gm-Message-State: AOJu0YwW9vSq6+GfKYwKB0hv6XK2PuL1nCjXbih8foQyxYOXsDsHZ2OX v13PbMoAwkSbGsD+lnDhjYSoFHHTT4csM/Y4kFaH6cY4hrE2X9Mp X-Google-Smtp-Source: AGHT+IEUyqgAQgSYeo4V5XETuzHahn7tVvnJ7vsmTQ6Gim/WnKgb4JYHVb3vMc4lu9SuOnxOFo6p1A== X-Received: by 2002:a05:6e02:dcb:b0:366:b7bf:3854 with SMTP id l11-20020a056e020dcb00b00366b7bf3854mr6460176ilj.22.1710799727268; Mon, 18 Mar 2024 15:08:47 -0700 (PDT) Received: from nathanxps13 (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id e19-20020a02a793000000b0047b61fb7590sm444245jaj.81.2024.03.18.15.08.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Mar 2024 15:08:46 -0700 (PDT) Date: Mon, 18 Mar 2024 17:08:45 -0500 From: Nathan Bossart To: David Rowley Cc: "Amonson, Paul D" , 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: <20240318220845.GA776042@nathanxps13> References: <20240315150611.GA3495582@nathanxps13> <20240318152902.GA2850@nathanxps13> <20240318162018.GA204772@nathanxps13> <20240318173004.GA361138@nathanxps13> <20240318210810.GA582529@nathanxps13> <20240318212919.GA684016@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="cNdxnHkX5QqsyA0e" Content-Disposition: inline In-Reply-To: <20240318212919.GA684016@nathanxps13> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --cNdxnHkX5QqsyA0e Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Mon, Mar 18, 2024 at 04:29:19PM -0500, Nathan Bossart wrote: > Agreed. Will send an updated patch shortly. As promised... -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --cNdxnHkX5QqsyA0e Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v4-0001-inline-function-calls-in-pg_popcount-when-possibl.patch" From b673663b1d1344549cbd0912220f96ba1712afc6 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Mon, 18 Mar 2024 12:18:15 -0500 Subject: [PATCH v4 1/1] inline function calls in pg_popcount() when possible --- src/include/port/pg_bitutils.h | 5 +- src/port/pg_bitutils.c | 155 +++++++++++++++++++++++++-------- 2 files changed, 121 insertions(+), 39 deletions(-) diff --git a/src/include/port/pg_bitutils.h b/src/include/port/pg_bitutils.h index 46bf4f0103..53e5239717 100644 --- a/src/include/port/pg_bitutils.h +++ b/src/include/port/pg_bitutils.h @@ -302,17 +302,16 @@ pg_ceil_log2_64(uint64 num) /* Attempt to use the POPCNT instruction, but perform a runtime check first */ extern PGDLLIMPORT int (*pg_popcount32) (uint32 word); extern PGDLLIMPORT int (*pg_popcount64) (uint64 word); +extern PGDLLIMPORT uint64 (*pg_popcount) (const char *buf, int bytes); #else /* Use a portable implementation -- no need for a function pointer. */ extern int pg_popcount32(uint32 word); extern int pg_popcount64(uint64 word); +extern uint64 pg_popcount(const char *buf, int bytes); #endif /* TRY_POPCNT_FAST */ -/* Count the number of one-bits in a byte array */ -extern uint64 pg_popcount(const char *buf, int bytes); - /* * Rotate the bits of "word" to the right/left by n bits. */ diff --git a/src/port/pg_bitutils.c b/src/port/pg_bitutils.c index 640a89561a..1197696e97 100644 --- a/src/port/pg_bitutils.c +++ b/src/port/pg_bitutils.c @@ -103,18 +103,22 @@ const uint8 pg_number_of_ones[256] = { 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 }; -static int pg_popcount32_slow(uint32 word); -static int pg_popcount64_slow(uint64 word); +static inline int pg_popcount32_slow(uint32 word); +static inline int pg_popcount64_slow(uint64 word); +static uint64 pg_popcount_slow(const char *buf, int bytes); #ifdef TRY_POPCNT_FAST static bool pg_popcount_available(void); static int pg_popcount32_choose(uint32 word); static int pg_popcount64_choose(uint64 word); -static int pg_popcount32_fast(uint32 word); -static int pg_popcount64_fast(uint64 word); +static uint64 pg_popcount_choose(const char *buf, int bytes); +static inline int pg_popcount32_fast(uint32 word); +static inline int pg_popcount64_fast(uint64 word); +static uint64 pg_popcount_fast(const char *buf, int bytes); int (*pg_popcount32) (uint32 word) = pg_popcount32_choose; int (*pg_popcount64) (uint64 word) = pg_popcount64_choose; +uint64 (*pg_popcount) (const char *buf, int bytes) = pg_popcount_choose; #endif /* TRY_POPCNT_FAST */ #ifdef TRY_POPCNT_FAST @@ -151,11 +155,13 @@ pg_popcount32_choose(uint32 word) { pg_popcount32 = pg_popcount32_fast; pg_popcount64 = pg_popcount64_fast; + pg_popcount = pg_popcount_fast; } else { pg_popcount32 = pg_popcount32_slow; pg_popcount64 = pg_popcount64_slow; + pg_popcount = pg_popcount_slow; } return pg_popcount32(word); @@ -168,21 +174,42 @@ pg_popcount64_choose(uint64 word) { pg_popcount32 = pg_popcount32_fast; pg_popcount64 = pg_popcount64_fast; + pg_popcount = pg_popcount_fast; } else { pg_popcount32 = pg_popcount32_slow; pg_popcount64 = pg_popcount64_slow; + pg_popcount = pg_popcount_slow; } return pg_popcount64(word); } +static uint64 +pg_popcount_choose(const char *buf, int bytes) +{ + if (pg_popcount_available()) + { + pg_popcount32 = pg_popcount32_fast; + pg_popcount64 = pg_popcount64_fast; + pg_popcount = pg_popcount_fast; + } + else + { + pg_popcount32 = pg_popcount32_slow; + pg_popcount64 = pg_popcount64_slow; + pg_popcount = pg_popcount_slow; + } + + return pg_popcount(buf, bytes); +} + /* * pg_popcount32_fast * Return the number of 1 bits set in word */ -static int +static inline int pg_popcount32_fast(uint32 word) { #ifdef _MSC_VER @@ -199,7 +226,7 @@ __asm__ __volatile__(" popcntl %1,%0\n":"=q"(res):"rm"(word):"cc"); * pg_popcount64_fast * Return the number of 1 bits set in word */ -static int +static inline int pg_popcount64_fast(uint64 word) { #ifdef _MSC_VER @@ -212,6 +239,52 @@ __asm__ __volatile__(" popcntq %1,%0\n":"=q"(res):"rm"(word):"cc"); #endif } +/* + * pg_popcount_fast + * Returns the number of 1-bits in buf + */ +static uint64 +pg_popcount_fast(const char *buf, int bytes) +{ + uint64 popcnt = 0; + +#if SIZEOF_VOID_P >= 8 + /* Process in 64-bit chunks if the buffer is aligned. */ + if (buf == (const char *) TYPEALIGN(8, buf)) + { + const uint64 *words = (const uint64 *) buf; + + while (bytes >= 8) + { + popcnt += pg_popcount64_fast(*words++); + bytes -= 8; + } + + buf = (const char *) words; + } +#else + /* Process in 32-bit chunks if the buffer is aligned. */ + if (buf == (const char *) TYPEALIGN(4, buf)) + { + const uint32 *words = (const uint32 *) buf; + + while (bytes >= 4) + { + popcnt += pg_popcount32_fast(*words++); + bytes -= 4; + } + + buf = (const char *) words; + } +#endif + + /* Process any remaining bytes */ + while (bytes--) + popcnt += pg_number_of_ones[(unsigned char) *buf++]; + + return popcnt; +} + #endif /* TRY_POPCNT_FAST */ @@ -219,7 +292,7 @@ __asm__ __volatile__(" popcntq %1,%0\n":"=q"(res):"rm"(word):"cc"); * pg_popcount32_slow * Return the number of 1 bits set in word */ -static int +static inline int pg_popcount32_slow(uint32 word) { #ifdef HAVE__BUILTIN_POPCOUNT @@ -241,7 +314,7 @@ pg_popcount32_slow(uint32 word) * pg_popcount64_slow * Return the number of 1 bits set in word */ -static int +static inline int pg_popcount64_slow(uint64 word) { #ifdef HAVE__BUILTIN_POPCOUNT @@ -265,35 +338,12 @@ pg_popcount64_slow(uint64 word) #endif /* HAVE__BUILTIN_POPCOUNT */ } -#ifndef TRY_POPCNT_FAST - /* - * When the POPCNT instruction is not available, there's no point in using - * function pointers to vary the implementation between the fast and slow - * method. We instead just make these actual external functions when - * TRY_POPCNT_FAST is not defined. The compiler should be able to inline - * the slow versions here. - */ -int -pg_popcount32(uint32 word) -{ - return pg_popcount32_slow(word); -} - -int -pg_popcount64(uint64 word) -{ - return pg_popcount64_slow(word); -} - -#endif /* !TRY_POPCNT_FAST */ - -/* - * pg_popcount + * pg_popcount_slow * Returns the number of 1-bits in buf */ -uint64 -pg_popcount(const char *buf, int bytes) +static uint64 +pg_popcount_slow(const char *buf, int bytes) { uint64 popcnt = 0; @@ -305,7 +355,7 @@ pg_popcount(const char *buf, int bytes) while (bytes >= 8) { - popcnt += pg_popcount64(*words++); + popcnt += pg_popcount64_slow(*words++); bytes -= 8; } @@ -319,7 +369,7 @@ pg_popcount(const char *buf, int bytes) while (bytes >= 4) { - popcnt += pg_popcount32(*words++); + popcnt += pg_popcount32_slow(*words++); bytes -= 4; } @@ -333,3 +383,36 @@ pg_popcount(const char *buf, int bytes) return popcnt; } + +#ifndef TRY_POPCNT_FAST + +/* + * When the POPCNT instruction is not available, there's no point in using + * function pointers to vary the implementation between the fast and slow + * method. We instead just make these actual external functions when + * TRY_POPCNT_FAST is not defined. The compiler should be able to inline + * the slow versions here. + */ +int +pg_popcount32(uint32 word) +{ + return pg_popcount32_slow(word); +} + +int +pg_popcount64(uint64 word) +{ + return pg_popcount64_slow(word); +} + +/* + * pg_popcount + * Returns the number of 1-bits in buf + */ +uint64 +pg_popcount(const char *buf, int bytes) +{ + return pg_popcount_slow(buf, bytes); +} + +#endif /* !TRY_POPCNT_FAST */ -- 2.25.1 --cNdxnHkX5QqsyA0e--