pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Nathan Bossart <nathandbossart@gmail.com>
To: Shankaran, Akash <akash.shankaran@intel.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: David Rowley <dgrowleyml@gmail.com>
Cc: Ants Aasma <ants.aasma@cybertec.at>
Cc: Alvaro Herrera <alvherre@alvh.no-ip.org>
Cc: Amonson, Paul D <paul.d.amonson@intel.com>
Cc: Andres Freund <andres@anarazel.de>
Cc: Noah Misch <noah@leadboat.com>
Cc: Matthias van de Meent <boekewurm+postgres@gmail.com>
Cc: pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>
Cc: Devulapalli, Raghuveer <raghuveer.devulapalli@intel.com>
Subject: Re: Popcount optimization using AVX512
Date: Thu, 18 Apr 2024 14:53:46 -0500
Message-ID: <20240418195346.GA3506520@nathanxps13> (raw)
In-Reply-To: <PH0PR11MB50007F79C92E3B0C7C1E6D6FF20E2@PH0PR11MB5000.namprd11.prod.outlook.com>
References: <CAApHDvrqVoduZRNTct5yn_6e=i7TkqhzSxt96WQQDxEkgdGeWg@mail.gmail.com>
	<20240406011704.GB167028@nathanxps13>
	<CAApHDvq32zJMbwOg69fAogJ9mFDdYyYp8-1A4xiFMv63HnSZ-A@mail.gmail.com>
	<20240406194101.GA533391@nathanxps13>
	<2999492.1712536932@sss.pgh.pa.us>
	<20240408012332.GA1351547@nathanxps13>
	<20240408013002.GA1352058@nathanxps13>
	<3010179.1712540139@sss.pgh.pa.us>
	<20240418024459.GA3385227@nathanxps13>
	<PH0PR11MB50007F79C92E3B0C7C1E6D6FF20E2@PH0PR11MB5000.namprd11.prod.outlook.com>

On Thu, Apr 18, 2024 at 06:12:22PM +0000, Shankaran, Akash wrote:
> Good find. I confirmed after speaking with an intel expert, and from the intel AVX-512 manual [0] section 14.3, which recommends to check bit27. From the manual:
> 
> "Prior to using Intel AVX, the application must identify that the operating system supports the XGETBV instruction,
> the YMM register state, in addition to processor's support for YMM state management using XSAVE/XRSTOR and
> AVX instructions. The following simplified sequence accomplishes both and is strongly recommended.
> 1) Detect CPUID.1:ECX.OSXSAVE[bit 27] = 1 (XGETBV enabled for application use1).
> 2) Issue XGETBV and verify that XCR0[2:1] = '11b' (XMM state and YMM state are enabled by OS).
> 3) detect CPUID.1:ECX.AVX[bit 28] = 1 (AVX instructions supported).
> (Step 3 can be done in any order relative to 1 and 2.)"

Thanks for confirming.  IIUC my patch should be sufficient, then.

> It also seems that step 1 and step 2 need to be done prior to the CPUID OSXSAVE check in the popcount code.

This seems to contradict the note about doing step 3 at any point, and
given step 1 is the OSXSAVE check, I'm not following what this means,
anyway.

I'm also wondering if we need to check that (_xgetbv(0) & 0xe6) == 0xe6
instead of just (_xgetbv(0) & 0xe0) != 0, as the status of the lower half
of some of the ZMM registers is stored in the SSE and AVX state [0].  I
don't know how likely it is that 0xe0 would succeed but 0xe6 wouldn't, but
we might as well make it correct.

[0] https://en.wikipedia.org/wiki/Control_register#cite_ref-23

-- 
Nathan Bossart
Amazon Web Services: https://aws.amazon.com





view thread (173+ messages)  latest in thread

Message-ID: <20240418195346.GA3506520@nathanxps13>
Permalink:  ../20240418195346.GA3506520@nathanxps13/
Also on:    postgresql.org/message-id/20240418195346.GA3506520@nathanxps13

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: nathandbossart@gmail.com, akash.shankaran@intel.com, tgl@sss.pgh.pa.us, dgrowleyml@gmail.com, ants.aasma@cybertec.at, alvherre@alvh.no-ip.org, paul.d.amonson@intel.com, andres@anarazel.de, noah@leadboat.com, boekewurm+postgres@gmail.com, pgsql-hackers@lists.postgresql.org, raghuveer.devulapalli@intel.com
  Subject: Re: Popcount optimization using AVX512
  In-Reply-To: <20240418195346.GA3506520@nathanxps13>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox