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 1rxXpS-009Z4W-G1 for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Apr 2024 19:53:54 +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 1rxXpQ-009GMM-Jw for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Apr 2024 19:53:52 +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 1rxXpQ-009GME-AY for pgsql-hackers@lists.postgresql.org; Thu, 18 Apr 2024 19:53:52 +0000 Received: from mail-io1-xd31.google.com ([2607:f8b0:4864:20::d31]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rxXpN-003VIB-Qc for pgsql-hackers@lists.postgresql.org; Thu, 18 Apr 2024 19:53:51 +0000 Received: by mail-io1-xd31.google.com with SMTP id ca18e2360f4ac-7da42e1750aso13569439f.0 for ; Thu, 18 Apr 2024 12:53:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1713470029; x=1714074829; 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=si8zo9B5s+Z9TaurxaJjxyrBqENLopETWqKbATRX9UU=; b=WUrVVdSGHtur5PAGOeVWTbuyylc3mv4YMGJEvdbGaEPdWfI7ycbxQqygbVJEXwlfO1 2i+MOXCat1fgs1gKL5dSEqhJ/3ALMGmulPBL6O0aeMiMbiLsC+wfnq9z+O/PhMklhplx A1bQ5qlj+wfX4BeVvC1OZLcxnvd9dShDLkUiPH1M2k0ZRnLpiLGq4z1sRjtUM7AsFE+y AsmLt/pFGryr5gSfMa4sZ8DAzzaKPt4diA+TntmCpaxlPbJdscQyyqcOp/wk/K7n8kWj nN0qrnFwIT3dEdQu6MJZPaXQYkp5vz7HDeGRD1hgEpBRrhmM120CGU3jL5ZMKUc/FvTz mX0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713470029; x=1714074829; 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=si8zo9B5s+Z9TaurxaJjxyrBqENLopETWqKbATRX9UU=; b=Y4SgHdyUuN/Eibmbln7yNRLo1Z/V1mWaevMYf6AUG8+X3n0M4JlZkxn3GT0nrsaxF+ oVWi4W6rWo+Hng+tcuWbDvptM3xAOwzEOyVIyGoPJ+7iwGTG/xDw0WjYmcFQNSrxIHPv Bwlz9dzxPNI4Z07zKWWsAkJszN6LQA9fBIhBYrVQMhBHhMjYJFKmsRhhnzWxQ+gLbPCG ed8t1VHPlVJhXcEvkaqBD6vxT2+CbQDiO7ab+NWbd/c6iphZeVErxSyViEziJYYEnBYu OvyCCos2pbDMYMPocAO5GqMlG/7I+sKxR3R/65RFRRBaO+FrJ9Z5E57fTNffWp4sJ8Z7 fW6w== X-Forwarded-Encrypted: i=1; AJvYcCWIDIKxhLTp2MacEuX2aH7CDiy5D2ebp9IGhcgQNEdwkRYi0ZNbRIeKHr91UdDTgmuN6ZjO2t310kdLfKW3TPhdDP1vrxOhN7McNwNHNRXTm0v5 X-Gm-Message-State: AOJu0YytkVnj3nBG2/l2bB7YA3UyYYo8rRdTGKPm7yf7Dx3H8+9co3C3 7RpavBRlW5FM5IoTBblB7rJWaPojgtGRsFtUFaBDzx0uAM18RByN X-Google-Smtp-Source: AGHT+IFu2aDutu/9X/hVXCnXc6S530qFUnEuwWLxc+gXxUgGg1uyU0R/5eO0t+d7uPl9zR41AkG2hw== X-Received: by 2002:a05:6602:428b:b0:7da:18a7:97d3 with SMTP id cd11-20020a056602428b00b007da18a797d3mr289119iob.2.1713470029068; Thu, 18 Apr 2024 12:53:49 -0700 (PDT) Received: from nathanxps13 (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id kw22-20020a056638931600b00482ed278935sm591523jab.8.2024.04.18.12.53.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Apr 2024 12:53:48 -0700 (PDT) Date: Thu, 18 Apr 2024 14:53:46 -0500 From: Nathan Bossart To: "Shankaran, Akash" Cc: Tom Lane , David Rowley , Ants Aasma , Alvaro Herrera , "Amonson, Paul D" , Andres Freund , Noah Misch , Matthias van de Meent , "pgsql-hackers@lists.postgresql.org" , "Devulapalli, Raghuveer" Subject: Re: Popcount optimization using AVX512 Message-ID: <20240418195346.GA3506520@nathanxps13> References: <20240406011704.GB167028@nathanxps13> <20240406194101.GA533391@nathanxps13> <2999492.1712536932@sss.pgh.pa.us> <20240408012332.GA1351547@nathanxps13> <20240408013002.GA1352058@nathanxps13> <3010179.1712540139@sss.pgh.pa.us> <20240418024459.GA3385227@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 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