public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonny Grant <jg@jguk.org>
To: Richard Earnshaw <Richard.Earnshaw@foss.arm.com>,
	gcc-help <gcc-help@gcc.gnu.org>
Subject: Re: Could __builtin_printf parameters be optimized when being compiled
Date: Wed, 15 Feb 2023 15:10:15 +0000	[thread overview]
Message-ID: <6bad0ce4-667f-0705-fc40-c40c9e8dec8f@jguk.org> (raw)
In-Reply-To: <c06d6bde-5adc-3d81-90b1-36ba5555dc8c@foss.arm.com>

Thank you for your quick reply Richard.

On 15/02/2023 14:30, Richard Earnshaw wrote:
> 
> 
> On 15/02/2023 14:18, Jonny Grant wrote:
>> Hi
>> Has GCC considered an improvement to "compile out" from the builtin printf the strings? That being to change it to just be something like puts("file /app/example.cpp:4")
>> I had a look, but couldn't find it being asked before.
>>
>> This is just a short example to demonstrate.
>> It would be useful to see the exact string in the debugger "file /app/example.cpp:4", also it saves a few lines of asm.
>>
>> https://godbolt.org/z/aKz3o6aPd
>>
>>
>> int main()
>> {
>>      __builtin_printf("file %s:%d", __FILE__, __LINE__);
>> }
>>
>>
>> .LC0:
>>          .string "/app/example.cpp"
>> .LC1:
>>          .string "file %s:%d"
>> main:
>>          subq    $8, %rsp
>>          movl    $4, %edx
>>          movl    $.LC0, %esi
>>          xorl    %eax, %eax
>>          movl    $.LC1, %edi
>>          call    printf
>>          xorl    %eax, %eax
>>          addq    $8, %rsp
>>          ret
> 
> We already do when the printf contains simply the format string and no additional arguments.
> 
> 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.

> - does it cause string bloat (what if there were 30 or so such statements in your program all identical except for the line number)?

That's probably what I am expecting, to see those 30 different formatted strings.

> - does it even happen often enough to be worth adding (and maintaining) support?  Nothing comes for free in a compiler and the optimisations have to be worth-while in the real world.
> 
> R.

You're completely right, it could bloat the file with strings.

I can do some with multi-line literals, to get "file /app/example.cpp compiled Feb 15 2023"
__FILE__ and __DATE__ worked ok.

but it didn't like me putting __PRETTY_FUNCTION__ in the middle. Maybe I'm missing something obvious. Likewise I can't use __builtin_LINE() as that is a function rather than a string. Maybe __PRETTY_FUNCTION__ and __FUNCTION__ are calls to  __builtin_FUNCTION().

int main()
{
    const char * s = "file " \
        __FILE__ \
        " compiled " \
        __DATE__ \
        "\n";

    __builtin_printf("%s", s);
    __builtin_printf("%s\n", __PRETTY_FUNCTION__);  // didn't work when I put in the middle.


https://godbolt.org/z/xso9soWaf

Regards
Jonny

  reply	other threads:[~2023-02-15 15:10 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-15 14:18 Jonny Grant
2023-02-15 14:30 ` Richard Earnshaw
2023-02-15 15:10   ` Jonny Grant [this message]
2023-02-15 15:27     ` Richard Earnshaw
2023-02-16 13:47       ` Jonny Grant
2023-02-15 15:49     ` Segher Boessenkool
2023-02-15 16:42       ` Richard Earnshaw
2023-02-16 13:48       ` Jonny Grant

Reply instructions:

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

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

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6bad0ce4-667f-0705-fc40-c40c9e8dec8f@jguk.org \
    --to=jg@jguk.org \
    --cc=Richard.Earnshaw@foss.arm.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).