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 1sYuS8-00F1Cv-3P for pgsql-hackers@arkaria.postgresql.org; Tue, 30 Jul 2024 21:32:16 +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 1sYuS6-00DsU7-KB for pgsql-hackers@arkaria.postgresql.org; Tue, 30 Jul 2024 21:32:14 +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 1sYuS6-00DsTv-8S for pgsql-hackers@lists.postgresql.org; Tue, 30 Jul 2024 21:32:14 +0000 Received: from mail-il1-x12b.google.com ([2607:f8b0:4864:20::12b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sYuS3-002GmG-9K for pgsql-hackers@lists.postgresql.org; Tue, 30 Jul 2024 21:32:13 +0000 Received: by mail-il1-x12b.google.com with SMTP id e9e14a558f8ab-39865a15900so18459095ab.1 for ; Tue, 30 Jul 2024 14:32:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722375130; x=1722979930; 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=Y0xkjSOAvyNLoVLlrSmNZXebgVuDDPS+RQXj9nkF0nE=; b=RPxsAnTfNo/uE5l0o44W3SGej2cWsO33yaK+5ljZM3ud+roPrwxSshXyPTC6XzAlp8 EzaLMEdRgkg8Oy85BzYjdKVVo9b0UIDmStjpBPZpNLM2Z8X+fIrjtzo3+wmD+m2HJQ7l uUMsX7sR7taffJeQp/JHlHlz4rpM1qx3XM/fJ2e4mamPrUWneJhhTIsfRXaBeJ4y6dRM AnOsl5qBrPUU87wF50ywj/LJTzi1y52hpVqmyYJecOfcckmvAedOYoMr0usOlTY/j18t B/Agla9eYMykhNYZRs4ZJp0/CQEd7HrvWUKVk3EQuooVeyjzQuSxwu38cqf73B/eIi6W PTFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722375130; x=1722979930; 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=Y0xkjSOAvyNLoVLlrSmNZXebgVuDDPS+RQXj9nkF0nE=; b=UDQUUmlHgYtbBKcSFE7TFAR9ASRFlPYA9cNKeviIC67r/DRlyxcuCWyQoh/MNM6NC/ 0xBVsCLPN28x4CWo322mmPT3D1Ow16X1C9Kxh5tidKDhSwHRfqnVcVtOlWWeAsrQqMJJ b3eVMbaETOl3ql5uitHlCYZhZ6wxzrhc6QR6bWjVaaQC8Fw+DnQq8FBly3HlvS/d6PFT i4rxTiqJ8KyJE6akRzqc50/vQdfH0XkIfhGN0h3LEnGPN4WDkakJmpOGsln0Uxhp6Taz JzEXfvPYRLw2mK4a4buCMtqnY/xqLeY5bKoeeVXRcwGM/pjuKPL8kHYle5nFywSUlNKg LoMQ== X-Forwarded-Encrypted: i=1; AJvYcCWLz6ZbBi9mixi3Rp0CNxZu7AP7PkXxHH5imzSiy2IaBCIMK1Ae6TNQe8bkFuqqnxAdnfh6FtXj5EvzCVYps7jWHrVEvso9+tiZ9LXGK1kkLSz1 X-Gm-Message-State: AOJu0YykDEI1GOxrndxxZS3kiN7h17VUMTHigqdGM/007wcPxN4xQkaR tVJYaIeevaicaDXHhOmyaEXZ4BefjS9A6kAotnnJJfJ3hSPf5BxD X-Google-Smtp-Source: AGHT+IErPPA09anDnD6cSQXayNSQquNYfaCZO64WtgaYdE0rwYHNt82ATSkYR3yCxtLrD79j2auKpw== X-Received: by 2002:a05:6e02:1a81:b0:375:a282:c96b with SMTP id e9e14a558f8ab-39aec2d7891mr111956745ab.27.1722375129871; Tue, 30 Jul 2024 14:32:09 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id e9e14a558f8ab-39ae92434c8sm39018155ab.51.2024.07.30.14.32.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jul 2024 14:32:09 -0700 (PDT) Date: Tue, 30 Jul 2024 16:32:07 -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: <20240418195346.GA3506520@nathanxps13> <20240418210158.GA3776258@nathanxps13> <20240418215902.GA3830246@nathanxps13> <20240418221358.GA3927188@nathanxps13> <20240423160207.GA841237@nathanxps13> <20240730210701.oyn2wr4zduu2efrg@awork3.anarazel.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240730210701.oyn2wr4zduu2efrg@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 02:07:01PM -0700, Andres Freund wrote: > I've noticed that the configure probes for this are quite slow - pretty much > the slowest step in a meson setup (and autoconf is similar). While looking > into this, I also noticed that afaict the tests don't do the right thing for > msvc. > > ... > [6.825] Checking if "__sync_val_compare_and_swap(int64)" : links: YES > [6.883] Checking if " __atomic_compare_exchange_n(int32)" : links: YES > [6.940] Checking if " __atomic_compare_exchange_n(int64)" : links: YES > [7.481] Checking if "XSAVE intrinsics without -mxsave" : links: NO > [8.097] Checking if "XSAVE intrinsics with -mxsave" : links: YES > [8.641] Checking if "AVX-512 popcount without -mavx512vpopcntdq -mavx512bw" : links: NO > [9.183] Checking if "AVX-512 popcount with -mavx512vpopcntdq -mavx512bw" : links: YES > [9.242] Checking if "_mm_crc32_u8 and _mm_crc32_u32 without -msse4.2" : links: NO > [9.333] Checking if "_mm_crc32_u8 and _mm_crc32_u32 with -msse4.2" : links: YES > [9.367] Checking if "x86_64: popcntq instruction" compiles: YES > [9.382] Has header "atomic.h" : NO > ... > > (the times here are a bit exaggerated, enabling them in meson also turns on > python profiling, which makes everything a bit slower) > > > Looks like this is largely the fault of including immintrin.h: > > echo -e '#include \nint main(){return _xgetbv(0) & 0xe0;}'|time gcc -mxsave -xc - -o /dev/null > 0.45user 0.04system 0:00.50elapsed 99%CPU (0avgtext+0avgdata 94184maxresident)k > > echo -e '#include \n'|time gcc -c -mxsave -xc - -o /dev/null > 0.43user 0.03system 0:00.46elapsed 99%CPU (0avgtext+0avgdata 86004maxresident)k Interesting. Thanks for bringing this to my attention. > Do we really need to link the generated programs? If we instead were able to > just rely on the preprocessor, it'd be vastly faster. > > The __sync* and __atomic* checks actually need to link, as the compiler ends > up generating calls to unimplemented functions if the compilation target > doesn't support some operation natively - but I don't think that's true for > the xsave/avx512 stuff > > Afaict we could just check for predefined preprocessor macros: > > echo|time gcc -c -mxsave -mavx512vpopcntdq -mavx512bw -xc -dM -E - -o -|grep -E '__XSAVE__|__AVX512BW__|__AVX512VPOPCNTDQ__' > #define __AVX512BW__ 1 > #define __AVX512VPOPCNTDQ__ 1 > #define __XSAVE__ 1 > 0.00user 0.00system 0:00.00elapsed 100%CPU (0avgtext+0avgdata 13292maxresident)k > > echo|time gcc -c -march=nehalem -xc -dM -E - -o -|grep -E '__XSAVE__|__AVX512BW__|__AVX512VPOPCNTDQ__' > 0.00user 0.00system 0:00.00elapsed 100%CPU (0avgtext+0avgdata 10972maxresident)k Seems promising. I can't think of a reason that wouldn't work. > Now, a reasonable counter-argument would be that only some of these macros are > defined for msvc ([1]). However, as it turns out, the test is broken > today, as msvc doesn't error out when using an intrinsic that's not > "available" by the target architecture, it seems to assume that the caller did > a cpuid check ahead of time. > > > Check out [2], it shows the various predefined macros for gcc, clang and msvc. > > > ISTM that the msvc checks for xsave/avx512 being broken should be an open > item? I'm not following this one. At the moment, we always do a runtime check for the AVX-512 stuff, so in the worst case we'd check CPUID at startup and set the function pointers appropriately, right? We could, of course, still fix it, though. -- nathan