From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate.crashing.org (gate.crashing.org [63.228.1.57]) by sourceware.org (Postfix) with ESMTP id 50D383858D35 for ; Wed, 15 Feb 2023 15:50:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 50D383858D35 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=kernel.crashing.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=kernel.crashing.org Received: from gate.crashing.org (localhost.localdomain [127.0.0.1]) by gate.crashing.org (8.14.1/8.14.1) with ESMTP id 31FFnbl3022209; Wed, 15 Feb 2023 09:49:37 -0600 Received: (from segher@localhost) by gate.crashing.org (8.14.1/8.14.1/Submit) id 31FFnalS022204; Wed, 15 Feb 2023 09:49:36 -0600 X-Authentication-Warning: gate.crashing.org: segher set sender to segher@kernel.crashing.org using -f Date: Wed, 15 Feb 2023 09:49:36 -0600 From: Segher Boessenkool To: Jonny Grant Cc: Richard Earnshaw , gcc-help Subject: Re: Could __builtin_printf parameters be optimized when being compiled Message-ID: <20230215154936.GG25951@gate.crashing.org> References: <03717f06-9818-0c2a-6625-429887c55a17@jguk.org> <6bad0ce4-667f-0705-fc40-c40c9e8dec8f@jguk.org> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <6bad0ce4-667f-0705-fc40-c40c9e8dec8f@jguk.org> User-Agent: Mutt/1.4.2.3i X-Spam-Status: No, score=-3.0 required=5.0 tests=BAYES_00,JMQ_SPF_NEUTRAL,KAM_DMARC_STATUS,SPF_HELO_PASS,SPF_PASS,TXREP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Wed, Feb 15, 2023 at 03:10:15PM +0000, Jonny Grant wrote: > On 15/02/2023 14:30, Richard Earnshaw wrote: > >> int main() > >> { > >>      __builtin_printf("file %s:%d", __FILE__, __LINE__); > >> } Note that the format needs to end in "\n" if you want this to be converted to a puts() call. Without it it strangely doesn't seem to be optimised, although fprintf() is converted to fwrite() in such cases. This could be done for printf() as well: you are guaranteed to have access to stdout as well, at least if you use printf() instead of the builtin (I'm not sure if we do then). > > I guess it might be possible to handle cases where all the arguments are constant, but even that has its problems, eg: > > > > - can we guarantee identical output to the platform printf? > > That's a good question. I was using __builtin_printf so that should be GCC I expect. Not every printf() implementation has exactly the same output in all cases. Another practically important consideration is what it does to i18n. You really need to leave the format string unmodified for that to be able to work. Segher