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 1rYx9Q-0018cs-4W for pgsql-hackers@arkaria.postgresql.org; Sat, 10 Feb 2024 23:52:52 +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 1rYx9O-000Q1l-UX for pgsql-hackers@arkaria.postgresql.org; Sat, 10 Feb 2024 23:52:50 +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 1rYx9O-000Q1d-EH for pgsql-hackers@lists.postgresql.org; Sat, 10 Feb 2024 23:52:50 +0000 Received: from mail-yw1-x112a.google.com ([2607:f8b0:4864:20::112a]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rYx9H-0067Pp-7r for pgsql-hackers@lists.postgresql.org; Sat, 10 Feb 2024 23:52:48 +0000 Received: by mail-yw1-x112a.google.com with SMTP id 00721157ae682-60492003050so21587897b3.0 for ; Sat, 10 Feb 2024 15:52:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1707609162; x=1708213962; darn=lists.postgresql.org; h=user-agent: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=hU1GxCUrA0hkRDUnAx05SKmMlZqOA8IfmywVBFDumkg=; b=RS3b3rJmsagKvjFxWRDAXf7VyxDPr+6FGDfhZExxIlufMi+QkvJjW8CeLp0b7sNwTA U/jzsd50SLxWj1nO6qiCV0RjY51os6hx7lYiZ62jwQODuZoiNyHD8vXQCa7mMqi/zowV 5NSqPahKhMVqRl2FzatovSv9N+QRcHmWrJFr0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707609162; x=1708213962; h=user-agent: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=hU1GxCUrA0hkRDUnAx05SKmMlZqOA8IfmywVBFDumkg=; b=jhB6ruHq1/g620IHqWMWaPd+V/KgFBmrSnBuuEQlHEP+2A1IjWeoqLCp3/b9o+M9fF ZfNZvcKnv3MkjX+9+TVIus5maPHCdhjUQb+979h/a3Nc4j9FqR5U4QPu4QCOtUOFP2xL RfT0IKdHPeTyL013Pfcl6sXMvdeWutAw44rrGHodkRaw4WUuuv1LOiBqelSEhV/GG53y 4AsAVSLmbQKGSqnTkeQf+p9jVuk3dbvjbIDt8XXOoFKN9y5R29mXUuh2ulz6/+3vAeLE 5WktV91vR6T1P9KAOPj2xgLfilf1E9E1tmlQCHRbCwenGBMZt8Drvl3vhmxgarRa+BpZ zRXQ== X-Gm-Message-State: AOJu0Yy96K354V98hLfkj7aOsZhfYFtF6wgf+Y56bP+bOJh2SoU7NS2W rHBLiDJBAJkgEodIa8y+o5uOp7TrzZqRY83DlhggOEpaXA2J5vbLn9TpgxPKaQ== X-Google-Smtp-Source: AGHT+IFSEOexZ+An0Yzg1U+XgaZEhvvQkxpY/XuKpzy8KbmrIE9DS39MFZQfv6W/slbmUzmWG1LTUg== X-Received: by 2002:a0d:ca57:0:b0:604:6aed:e27c with SMTP id m84-20020a0dca57000000b006046aede27cmr2605572ywd.16.1707609161856; Sat, 10 Feb 2024 15:52:41 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWeD0253QosCQIeQVWUgaSIbgrIKTL+58WsZ/9VicQoGcii3Wvdy8xf7TwicV0AIAQMMJxFgtY1GDyhLdczqd7jrHIkFcZ5pgMsBRTyapMPB8JlJX2uuG1xkaL7zucypiouK+e7tDV9cRjcmcO/pGyHpPM5/ULBRPPpa2eVx/4NYJ3CgcqK1UUJnS5+Mp3EFN819qxuhMVsRBJe03Jy8sjeJwiz1FuBUCiFs6eT/fFOUBFHl1CgvtJDx3fkp9m43at4j27H09a90DLkSgww3sFD4zQ= Received: from rfd.leadboat.com ([2600:1702:a20:5750::48]) by smtp.gmail.com with ESMTPSA id d133-20020a0ddb8b000000b005ff9264e841sm536520ywe.6.2024.02.10.15.52.40 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 10 Feb 2024 15:52:41 -0800 (PST) Date: Sat, 10 Feb 2024 15:52:38 -0800 From: Noah Misch To: Andres Freund Cc: Alvaro Herrera , "Shankaran, Akash" , Nathan Bossart , "Amonson, Paul D" , Tom Lane , Matthias van de Meent , "pgsql-hackers@lists.postgresql.org" Subject: Re: Popcount optimization using AVX512 Message-ID: <20240210235238.eb@rfd.leadboat.com> References: <202401250949.xrwkd46bgbpl@alvherre.pgsql> <202401260642.3rxzgb2xrhkl@alvherre.pgsql> <20240209182432.7c2peq5cgbdl43um@awork3.anarazel.de> <20240209232757.5d@rfd.leadboat.com> <20240210043323.uofbl6h7twao2mqg@awork3.anarazel.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240210043323.uofbl6h7twao2mqg@awork3.anarazel.de> User-Agent: Mutt/2.2.12 (2023-09-09) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Feb 09, 2024 at 08:33:23PM -0800, Andres Freund wrote: > On 2024-02-09 15:27:57 -0800, Noah Misch wrote: > > On Fri, Feb 09, 2024 at 10:24:32AM -0800, Andres Freund wrote: > > > On 2024-01-26 07:42:33 +0100, Alvaro Herrera wrote: > > > > This suggests that finding a way to make the ifunc stuff work (with good > > > > performance) is critical to this work. > > > > > > Ifuncs are effectively implemented as a function call via a pointer, they're > > > not magic, unfortunately. The sole trick they provide is that you don't > > > manually have to use the function pointer. > > > > The IFUNC creators introduced it so glibc could use arch-specific memcpy with > > the instruction sequence of a non-pointer, extern function call, not the > > instruction sequence of a function pointer call. > > My understanding is that the ifunc mechanism just avoid the need for repeated > indirect calls/jumps to implement a single function call, not the use of > indirect function calls at all. Calls into shared libraries, like libc, are > indirected via the GOT / PLT, i.e. an indirect function call/jump. Without > ifuncs, the target of the function call would then have to dispatch to the > resolved function. Ifuncs allow to avoid this repeated dispatch by moving the > dispatch to the dynamic linker stage, modifying the contents of the GOT/PLT to > point to the right function. Thus ifuncs are an optimization when calling a > function in a shared library that's then dispatched depending on the cpu > capabilities. > > However, in our case, where the code is in the same binary, function calls > implemented in the main binary directly (possibly via a static library) don't > go through GOT/PLT. In such a case, use of ifuncs turns a normal direct > function call into one going through the GOT/PLT, i.e. makes it indirect. The > same is true for calls within a shared library if either explicit symbol > visibility is used, or -symbolic, -Wl,-Bsymbolic or such is used. Therefore > there's no efficiency gain of ifuncs over a call via function pointer. > > > This isn't because ifunc is implemented badly or something - the reason for > this is that dynamic relocations aren't typically implemented by patching all > callsites (".text relocations"), which is what you would need to avoid the > need for an indirect call to something that fundamentally cannot be a constant > address at link time. The reason text relocations are disfavored is that > they can make program startup quite slow, that they require allowing > modifications to executable pages which are disliked due to the security > implications, and that they make the code non-shareable, as the in-memory > executable code has to differ from the on-disk code. > > > I actually think ifuncs within the same binary are a tad *slower* than plain > function pointer calls, unless -fno-plt is used. Without -fno-plt, an ifunc is > called by 1) a direct call into the PLT, 2) loading the target address from > the GOT, 3) making an an indirect jump to that address. Whereas a "plain > indirect function call" is just 1) load target address from variable 2) making > an indirect jump to that address. With -fno-plt the callsites themselves load > the address from the GOT. That sounds more accurate than what I wrote. Thanks.