public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "felix.von.s at posteo dot de" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug other/79469] Feature request: provide `__builtin_assume` builtin function to allow more aggressive optimizations and to match clang
Date: Fri, 27 Mar 2020 17:53:44 +0000	[thread overview]
Message-ID: <bug-79469-4-ipTuvKvpt4@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-79469-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79469

felix <felix.von.s at posteo dot de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |felix.von.s at posteo dot de

--- Comment #1 from felix <felix.von.s at posteo dot de> ---
> Unfortunately the macro trick is arcane and has one potential issue: the compiler may actually generate code to check `cond` at run-time if it cannot deduce that it's a pure function.

Oh, don't worry; the Other Compiler’s solution isn’t perfect either :)

    __builtin_assume(({ for (;;) {}; 1; }));

This emits code for the infinite loop. For a less silly example:

    __attribute__((__const__,__always_inline__))
    inline static int is_pow2(unsigned j) {
        __auto_type i = j;

        for (int c = 0; c < CHAR_BIT * sizeof(i); ++c) {
            i = (i >> 1) | ((i & 1) << (CHAR_BIT * sizeof(i) - 1));
            if (i == 1)
                break;
        }

        return i == 1;
    }

    int foo(void) {
        extern unsigned bar(void);
        __auto_type x = bar();

        __builtin_assume(is_pow2(x));

        return __builtin_popcount(x);
    }

This will also emit code for the loop, even though the result is not used at
runtime. The code is not very idiomatic, but I don’t believe it’s wrong either:
the __attribute__((const)) on is_pow2 I consider correct, because the side
effects do not escape the function.

If anyone takes this up, it would be nice if GCC at least did not outright
pessimise such code.

       reply	other threads:[~2020-03-27 17:53 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-79469-4@http.gcc.gnu.org/bugzilla/>
2020-03-27 17:53 ` felix.von.s at posteo dot de [this message]
2020-04-17 11:43 ` felix.von.s at posteo dot de
2021-11-24 16:27 ` ktkachov at gcc dot gnu.org
2022-10-26 19:44 ` pinskia at gcc dot gnu.org
2022-10-26 19:52 ` jakub at gcc dot gnu.org
2024-03-05  0:53 ` pinskia at gcc dot gnu.org

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=bug-79469-4-ipTuvKvpt4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).