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 1sZH6F-00HSxT-Cl for pgsql-hackers@arkaria.postgresql.org; Wed, 31 Jul 2024 21:43:11 +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 1sZH6D-00EKAJ-Cm for pgsql-hackers@arkaria.postgresql.org; Wed, 31 Jul 2024 21:43:09 +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 1sZH6D-00EKAA-18 for pgsql-hackers@lists.postgresql.org; Wed, 31 Jul 2024 21:43:09 +0000 Received: from mail-io1-xd2b.google.com ([2607:f8b0:4864:20::d2b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sZH6A-002V73-Mq for pgsql-hackers@lists.postgresql.org; Wed, 31 Jul 2024 21:43:08 +0000 Received: by mail-io1-xd2b.google.com with SMTP id ca18e2360f4ac-8152f0c63c1so222900539f.1 for ; Wed, 31 Jul 2024 14:43:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722462185; x=1723066985; 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=EsqJQQUmelurdAwo9k9SLrXM3ksKO0mbFPjMB8ziV14=; b=NTRWheFlSCAqEgYalxQtKaWNiFxNdHS+yIj0VcDxhoeTe9eFkU6yhtCNm4FzOdaF9e OyRi/19X1MvKvylE7FU3+JjjV3JY2oqIF2gWkEZBh1wp+A5fy+j4k9LtJWKg/Qok/e40 Uo7l8xWu3v6mz2Kj+JdeUxVLj/qMAX/TBB9xtYrblJDjxQCpm/OgI08nYerh2ET9Yr66 KCM5lub5YWYEGoMqXBdBkUG92nnCbO50d3C1gf1lG1HYB40RqLpkhIiDQ3zZfwxGi7rY EOJYE6rrkB/tVKXIvz74tGJdQEqRqtzjwjKavK1HRy6UOjem3jSU9XkCLSE4UcDSR8rh Q+SA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722462185; x=1723066985; 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=EsqJQQUmelurdAwo9k9SLrXM3ksKO0mbFPjMB8ziV14=; b=t61Mvtwnyi9ASkY57xtrRxzOiIpOGf1frxo8u9+J5dDYRegsKC2K9hjaBnociBPWal IAcdZIijNPepoCF2Y7SiUiMrbvOpJwneYImq/fLSXwS4zex0KS10tAmxuC3+UNnP+nxe G3MjOjvcZd5bQDFUjJAZ9iim901lJtkxdIJlJU6eCh8e59+xX5aBCn4C7AqtgnKBsO36 siY2NkbUkVlwNmRCNNv9MzLRcYP6yilAgo/WroQRhjp1J47tx1ZoK5PtqC3DDFUKPQJL Vsr9GTy/XZQsS+nKQAYY98D3OuWtJFF6VSXpXGygGTRV8rfTNT8zOken5kYfWHaY6pz5 rKpQ== X-Forwarded-Encrypted: i=1; AJvYcCXfl8SnLIo7nq8lsP2jKM0n3nQkZHj453GdCF0Dv0C5+hiGlQMMD2tS/vviBRgd/3dkrWDBktVWRmXK6XtwrPmrui3b60WQ8MN1UFhyibyYzdNX X-Gm-Message-State: AOJu0Yz/c8pP1xuk8YVEo3fM4+xOYCWBUZ+6WcNngDwE+0MUVcKTE5Wg NmGTxmqkBebHGFO60qhZSdaQfQZpybCLhIBPDM6j/IoCiUzPFaeJg1LsBw== X-Google-Smtp-Source: AGHT+IF92CcUmO/BtvT/5Ll/aUthAagsmsQJCnLTPCY4Uh0cJMLp8SjVqq13TrjBYcmqKCTHFQ6slA== X-Received: by 2002:a05:6602:160a:b0:805:8c5a:2503 with SMTP id ca18e2360f4ac-81fcc0fab07mr78745139f.8.1722462184641; Wed, 31 Jul 2024 14:43:04 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4c29fc5c291sm3365214173.166.2024.07.31.14.43.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jul 2024 14:43:04 -0700 (PDT) Date: Wed, 31 Jul 2024 16:43:02 -0500 From: Nathan Bossart To: Andres Freund Cc: "Devulapalli, Raghuveer" , "Shankaran, Akash" , Tom Lane , David Rowley , Ants Aasma , Alvaro Herrera , "Amonson, Paul D" , Noah Misch , Matthias van de Meent , "pgsql-hackers@lists.postgresql.org" Subject: Re: Popcount optimization using AVX512 Message-ID: References: <20240730210701.oyn2wr4zduu2efrg@awork3.anarazel.de> <20240731004959.6ys24432n6xlgemk@awork3.anarazel.de> <20240731014651.byn3exbu6n5v2yqa@awork3.anarazel.de> <20240731024308.xma56fxidkdydzlo@awork3.anarazel.de> <20240731205254.vfpap7uxwmebqeaf@awork3.anarazel.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240731205254.vfpap7uxwmebqeaf@awork3.anarazel.de> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, Jul 31, 2024 at 01:52:54PM -0700, Andres Freund wrote: > On 2024-07-30 22:12:18 -0500, Nathan Bossart wrote: >> As I started on this, I remembered why I needed it. The file >> pg_popcount_avx512_choose.c is compiled without the AVX-512 flags in order >> to avoid inadvertently issuing any AVX-512 instructions before determining >> we have support. If that's not a concern, we could still probably remove >> the XSAVE check. > > I think it's a valid concern - but isn't that theoretically also an issue with > xsave itself? I guess practically the compiler won't do that, because there's > no practical reason to emit any instructions enabled by -mxsave (in contrast > to e.g. -mavx, which does trigger gcc to emit different instructions even for > basic math). Yeah, this crossed my mind. It's certainly not the sturdiest of assumptions... > I think enabling options like these on a per-translation-unit basis isn't > really a scalable approach. To actually be safe there could only be a single > function in each TU and that function could only be called after a cpuid check > performed in a separate TU. That a) ends up pretty unreadable b) requires > functions to be implemented in .c files, which we really don't want for some > of this. Agreed. > I think we'd be better off enabling architectural features on a per-function > basis, roughly like this: > > [...] > > /* FIXME: Should be gated by configure check of -mavx512vpopcntdq -mavx512bw support */ > pg_enable_target("avx512vpopcntdq,avx512bw") > uint64_t > pg_popcount_avx512(const char *buf, int bytes) > ... I remember wondering why the CRC-32C code wasn't already doing something like this (old compiler versions? non-gcc-like compilers?), and I'm not sure I ever discovered the reason, so out of an abundance of caution I used the same approach for AVX-512. If we can convince ourselves that __attribute__((target("..."))) is standard enough at this point, +1 for moving to that. -- nathan