Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1guPxE-0007gg-Dg for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Feb 2019 22:58:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1guPxC-0001it-W8 for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Feb 2019 22:58:02 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1guPxC-0001il-Ky for pgsql-hackers@lists.postgresql.org; Thu, 14 Feb 2019 22:58:02 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1guPx9-0006Nb-SL for pgsql-hackers@lists.postgresql.org; Thu, 14 Feb 2019 22:58:01 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x1EMvsRI004023; Thu, 14 Feb 2019 17:57:54 -0500 From: Tom Lane To: Alvaro Herrera cc: Thomas Munro , Andrew Gierth , David Rowley , PostgreSQL Hackers Subject: Re: Using POPCNT and other advanced bit manipulation instructions In-reply-to: <20190214222838.GA10839@alvherre.pgsql> References: <20190214222838.GA10839@alvherre.pgsql> Comments: In-reply-to Alvaro Herrera message dated "Thu, 14 Feb 2019 19:28:38 -0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <4021.1550185074.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 14 Feb 2019 17:57:54 -0500 Message-ID: <4022.1550185074@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alvaro Herrera writes: > That leads me to the attached patch. It creates a new file > pg_popcount.c which is the only one compiled with -mpopcnt (if > available); if there's no compiler switch to enable POPCNT, we just > don't compile the file. I'm not sure that's kosher -- in particular I'm > not sure if it can fail when POPCNT is enabled by other flags and > -mpopcnt is not needed at all. I think our c-compiler.m4 stuff is a bit > too simplistic there: it just assumes that -mpopcnt is always required. Yes, the configure test for this stuff is really pretty broken. It's conflating two nearly independent questions: (1) does the compiler have __builtin_popcount(), and (2) does the compiler accept -mpopcnt. It is certainly the case that (1) may hold without (2); in fact, every recent non-x86_64 gcc is a counterexample to how that's done in HEAD. I think we need a clean test for __builtin_popcount(), and to be willing to use it if available, independently of -mpopcnt. Then separately we should test to see if -mpopcnt works, probably with the same infrastructure we use for checking for other compiler flags, viz # Optimization flags for specific files that benefit from vectorization PGAC_PROG_CC_VAR_OPT(CFLAGS_VECTOR, [-funroll-loops]) PGAC_PROG_CC_VAR_OPT(CFLAGS_VECTOR, [-ftree-vectorize]) + # Optimization flags for bit-twiddling + PGAC_PROG_CC_VAR_OPT(CFLAGS_POPCNT, [-mpopcnt]) # We want to suppress clang's unhelpful unused-command-line-argument wa= rnings Then the correct test to see if we want to build pg_popcount.c (BTW, please pick a less generic name for that) and the choose function is whether we have *both* HAVE__BUILTIN_POPCOUNT and nonempty CFLAGS_POPCNT. I don't think this'd be fooled by user-specified CFLAGS. The worst possible outcome is that it builds a function that we intended would use POPCNT but it's falling back to some other implementation, in case the compiler has a switch named -mpopcnt but it doesn't do what we think it does, or the user overrode things by adding -mno-popcnt. That would really be nearly cost-free, other than the overhead of the choose function the first time through: both of the execution functions would be reducing to __builtin_popcount(), for whatever version of that the compiler is giving us, so the choice wouldn't matter. regards, tom lane