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 1sYy1E-00FVZI-0l for pgsql-hackers@arkaria.postgresql.org; Wed, 31 Jul 2024 01:20:43 +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 1sYy1C-00FnW1-8Q for pgsql-hackers@arkaria.postgresql.org; Wed, 31 Jul 2024 01:20:42 +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 1sYy1B-00FnVs-UZ for pgsql-hackers@lists.postgresql.org; Wed, 31 Jul 2024 01:20:41 +0000 Received: from mail-il1-x133.google.com ([2607:f8b0:4864:20::133]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sYy19-002IGm-4V for pgsql-hackers@lists.postgresql.org; Wed, 31 Jul 2024 01:20:40 +0000 Received: by mail-il1-x133.google.com with SMTP id e9e14a558f8ab-39ae7b28dadso1448085ab.0 for ; Tue, 30 Jul 2024 18:20:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722388838; x=1722993638; 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=nRo9WPNh+32hVuootggNQ/cUouTFzw177SfLO8CnH/o=; b=jqirOocIm1HBGyacFw6xJwCTr1KxWuYnDExv9WkrGUO1Vsnr78rbklrWPcatJxTDwI Turt0XxqbYjjrnhEVoTqXCHDhNt3VWQzN0ZQEMRjJNfuUa0vtkKebt0yugGa+g8r+Xlh XQ0204XBh2UHaQTRqqHwjPW7K23yrY7CPfYEQpP2UyKjFKzFGZPJMzso8wgZRkBsc6X7 ub98Ef2waPulQ7cb4pgqRn8CrEBWOn9IvBV4UZMJpTt9352WZtUVOYl7QL1RPvZTmyRe Ho/ZjzjGnPeqzK1t/mjIG77hLgd5U3mkz7oHVm+xeJ+50+hV9k5s9WQJcG6QMYQl64S+ lBFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722388838; x=1722993638; 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=nRo9WPNh+32hVuootggNQ/cUouTFzw177SfLO8CnH/o=; b=jjmmLu95Z7ZGQNAZGao5bq4Tkg0UJ4RSN51WquXHoFU8FM3dxdVTQf/beciRTrSRqe 2ZJTo0ZH2RpeviR741XNi5QjhGAOlXM+h0UzFLBxt8tOf65r/0Hcic3mD+p15sCMh3Do cP93OaTYx/S/YaSW8nIfkAJ/EYvN1BiLhk20p2k8jb89dNNY3r66Nft8zmNOL9BDz1Cm 847V9TNeF4gXm4HNMuIat6FeH0/GTVvUIWV/iBc5mT5BoY990BdR3tboGHv3GbKWGgSD QLOAMOliajWm/Z7Sgmzz7puh/7a0gE2Uvusg0DC22LyBsfk/jPGmS+vbFm4AYgJ+zcQj ygoQ== X-Forwarded-Encrypted: i=1; AJvYcCVlV9xfyJk5qozS3gJmkUxpgE0krs9xpj255+dbncKTUrQe+EquommcRZD7Oc+rmnmd5xKLg0TX6zGr5wNqUflxcsmu346bWXK1pJQrJPqP/efP X-Gm-Message-State: AOJu0Yzb5F1GlrSXc+n0AlZMcPmwD/xMfXxMCCGC960R7GIup0SWQ1kh iFsEG9iLpOR2jerDzBBQnZ/yP+Vt1/xY6+YNr9NQxcGS4wgiZZPz X-Google-Smtp-Source: AGHT+IEYvuwKExiMy99MDW8242aKnMdgIyOV7k6PHNl4wfTCJwL0KHK52QUsZzffsgwSW4RQGrlNAA== X-Received: by 2002:a05:6e02:2197:b0:399:4525:c2bc with SMTP id e9e14a558f8ab-39b06b2c290mr28469815ab.10.1722388837768; Tue, 30 Jul 2024 18:20:37 -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-4c29fc60a93sm2947599173.169.2024.07.30.18.20.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jul 2024 18:20:37 -0700 (PDT) Date: Tue, 30 Jul 2024 20:20:34 -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: <20240418210158.GA3776258@nathanxps13> <20240418215902.GA3830246@nathanxps13> <20240418221358.GA3927188@nathanxps13> <20240423160207.GA841237@nathanxps13> <20240730210701.oyn2wr4zduu2efrg@awork3.anarazel.de> <20240731004959.6ys24432n6xlgemk@awork3.anarazel.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240731004959.6ys24432n6xlgemk@awork3.anarazel.de> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Jul 30, 2024 at 05:49:59PM -0700, Andres Freund wrote: > Ah, I somehow thought we'd avoid the runtime check in case we determine at > compile time we don't need any extra flags to enable the AVX512 stuff (similar > to how we deal with crc32). But it looks like that's not the case - which > seems pretty odd to me: > > This turns something that can be a single instruction into an indirect > function call, even if we could know that it's guaranteed to be available for > the compilation target, due to -march=.... > > It's one thing for the avx512 path to have that overhead, but it's > particularly absurd for pg_popcount32/pg_popcount64, where > > a) The function call overhead is a larger proportion of the cost. > b) the instruction is almost universally available, including in the > architecture baseline x86-64-v2, which several distros are using as the > x86-64 baseline. Yeah, pg_popcount32/64 have been doing this since v12 (02a6a54). Until v17 (cc4826d), pg_popcount() repeatedly calls these function pointers, too. I think it'd be awesome if we could start requiring some of these "almost universally available" instructions, but AFAICT that brings its own complexity [0]. > Why are we actually checking for xsave? We're not using xsave itself and I > couldn't find a comment in 792752af4eb5 explaining what we're using it as a > proxy for? Is that just to know if _xgetbv() exists? Is it actually possible > that xsave isn't available when avx512 is? Yes, it's to verify we have XGETBV, which IIUC requires support from both the processor and the OS (see 598e011 and upthread discussion). AFAIK the way we are detecting AVX-512 support is quite literally by-the-book unless I've gotten something wrong. [0] https://postgr.es/m/ZmpG2ZzT30Q75BZO%40nathan -- nathan