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 1rs9SE-0059Wd-9M for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Apr 2024 22:51:38 +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 1rs9RE-00AAAK-Tm for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Apr 2024 22:50:36 +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 1rs9RE-00AAAB-IN for pgsql-hackers@lists.postgresql.org; Wed, 03 Apr 2024 22:50:36 +0000 Received: from mail-pg1-x531.google.com ([2607:f8b0:4864:20::531]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rs9RB-000Ux0-Dj for pgsql-hackers@lists.postgresql.org; Wed, 03 Apr 2024 22:50:35 +0000 Received: by mail-pg1-x531.google.com with SMTP id 41be03b00d2f7-5cdbc4334edso305134a12.3 for ; Wed, 03 Apr 2024 15:50:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712184631; x=1712789431; 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=qJM0DnnXaKoU2xh4/t8YpgP540DkJG9eUD1aZLM/6JY=; b=Wg4LZw9qjj/bsUtQn7X5SiB1FvxTOAG1qIzQOe1eJpT2pJXZX3uXNJc517tHvWGP7H xMEz2qGTTOtQlgA3644Yp4fZcc3xCYcbRTcTznLL+qlJrgtlCozsmHGEFuTiJrar8uEG QBwbz3qz03qQCtPrTujy5M3nlku2NNqtEKxqSSl4E71xvSIPtE8w+Ddyig9wlN8OwX+Q Rcm2liVtwpiFeNQBzBH0Yl3x02ZG12UXqhfKJHvfVNC+VAYs3Nk/ac6EqCwqKDHX3FON M/xfwWTQlsamt03TNRgbFrcl0WNkoeasjbhbo667TB7BTdW3Fb3Ngn/vUfiSgEVsnm/J x/YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712184631; x=1712789431; 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=qJM0DnnXaKoU2xh4/t8YpgP540DkJG9eUD1aZLM/6JY=; b=soUFWT292DM/CCqOwUF1gQySq8cua/8dNoptdJXGRiQyHwxduVyu7ONtqg/YP9FZ7h jCIMZ6SJPrnu5GYaXqyJVkuhTXUFZdprKaeWyj73jfn6VgmFyqT+2YHVGOOirqTB0UFU mV/+PBCzjO2GIDtG3OZRM4M7GBOpKLixzhRf/e7q8gCGoxF5l85Ij1mXEvM9pnQ7m/fr hQbJzMrZOAyHgr3i20ZN00v3VpQMosVi1FNGmqoWRMM+7fbaDJyxN0HBNxrsqYNGUWf9 uZE3ae5/Syk5x4BHGCuDPdPqVA1bK8dvSPKG7untIiopYFnBnCpEttffsEFIu7CABRT7 fGZA== X-Forwarded-Encrypted: i=1; AJvYcCVqepRDsTFC838QPj1+SlUs4XhWD4tPs4d+1lIHABbe6YemMumkbx1tebiJaql3+bnVbnHSrRyqzP/kD+k/+6V6My9YRVrFjnWulb3fdBbQt7sb X-Gm-Message-State: AOJu0Yxcb3EVhQwJZEiMfP3uCS1GOrI7TLfGJijWI8Neh74orPARmol6 zkINqVxpIKwsKcqlDeFtKXX0G/BqzbbTHAztivczPuVM6w/ScC7M X-Google-Smtp-Source: AGHT+IEdHKpWVsIVUUIr/NlslyWt28Jo0RDsiKsiXpvNtHCScR1XjaAxP6CN5TzbMEJJgX7t4LbBwA== X-Received: by 2002:a05:6a20:9c90:b0:1a3:a984:4b8b with SMTP id mj16-20020a056a209c9000b001a3a9844b8bmr1147492pzb.50.1712184631193; Wed, 03 Apr 2024 15:50:31 -0700 (PDT) Received: from nathanxps13 ([64.124.160.6]) by smtp.gmail.com with ESMTPSA id nh12-20020a17090b364c00b0029c61521eb5sm224476pjb.43.2024.04.03.15.50.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 15:50:30 -0700 (PDT) Date: Wed, 3 Apr 2024 17:50:29 -0500 From: Nathan Bossart To: Ants Aasma Cc: Alvaro Herrera , "Amonson, Paul D" , Tom Lane , David Rowley , Andres Freund , "Shankaran, Akash" , Noah Misch , Matthias van de Meent , "pgsql-hackers@lists.postgresql.org" Subject: Re: Popcount optimization using AVX512 Message-ID: <20240403225029.GA3851411@nathanxps13> References: <20240401011708.GA2128739@nathanxps13> <202404011106.y4fci35kzdqt@alvherre.pgsql> <20240401155338.GA2296072@nathanxps13> <20240401213140.GA2362108@nathanxps13> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Apr 02, 2024 at 11:30:39PM +0300, Ants Aasma wrote: > On Tue, 2 Apr 2024 at 00:31, Nathan Bossart wrote: >> On Tue, Apr 02, 2024 at 12:11:59AM +0300, Ants Aasma wrote: >> > What about using the masking capabilities of AVX-512 to handle the >> > tail in the same code path? Masked out portions of a load instruction >> > will not generate an exception. To allow byte level granularity >> > masking, -mavx512bw is needed. Based on wikipedia this will only >> > disable this fast path on Knights Mill (Xeon Phi), in all other cases >> > VPOPCNTQ implies availability of BW. >> >> Sounds promising. IMHO we should really be sure that these kinds of loads >> won't generate segfaults and the like due to the masked-out portions. I >> searched around a little bit but haven't found anything that seemed >> definitive. > > After sleeping on the problem, I think we can avoid this question > altogether while making the code faster by using aligned accesses. > Loads that straddle cache line boundaries run internally as 2 load > operations. Gut feel says that there are enough out-of-order resources > available to make it not matter in most cases. But even so, not doing > the extra work is surely better. Attached is another approach that > does aligned accesses, and thereby avoids going outside bounds. > > Would be interesting to see how well that fares in the small use case. > Anything that fits into one aligned cache line should be constant > speed, and there is only one branch, but the mask setup and folding > the separate popcounts together should add up to about 20-ish cycles > of overhead. I tested your patch in comparison to v25 and saw the following: bytes v25 v25+ants 2 1108.205 1033.132 4 1311.227 1289.373 8 1927.954 2360.113 16 2281.091 2365.408 32 3856.992 2390.688 64 3648.72 3242.498 128 4108.549 3607.148 256 4910.076 4496.852 For 2 bytes and 4 bytes, the inlining should take effect, so any difference there is likely just noise. At 8 bytes, we are calling the function pointer, and there is a small regression with the masking approach. However, by 16 bytes, the masking approach is on par with v25, and it wins for all larger buffers, although the gains seem to taper off a bit. If we can verify this approach won't cause segfaults and can stomach the regression between 8 and 16 bytes, I'd happily pivot to this approach so that we can avoid the function call dance that I have in v25. Thoughts? -- Nathan Bossart Amazon Web Services: https://aws.amazon.com