public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Henrik Holst <henrik.holst@millistream.com>
To: gcc@gcc.gnu.org
Subject: read_only access attribute as optimizer hint
Date: Tue, 6 Sep 2022 16:22:06 +0200	[thread overview]
Message-ID: <CAGwK9dx8f1p+aDAq=ZKzo5BapUti2RMvVpfa94kRaEq9i9HeNA@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1928 bytes --]

Hi all,

  is there any reason why the access attribute is not used as hints to the
optimizer?

If we take this ancient example:

void foo(const int *);

int bar(void)
{
    int x = 0;
    int y = 0;

    for (int i = 0; i < 10; i++) {
        foo(&x);
        y += x;  // this load not optimized out
    }
    return y;
}

The load of X is not optimized out in the loop since the compiler does not
know if the external function foo() will cast away the const internally.
However changing the x variable to const as in:

void foo(const int *);

int bar(void)
{
    const int x = 0;
    int y = 0;

    for (int i = 0; i < 10; i++) {
        foo(&x);
        y += x;  // this load is now optimized out
    }
    return y;
}

The load of x is now optimized out since it is undefined behaviour if bar()
casts the const away when x is declared to be const.

Now what strikes me as odd however is that declaring the function access
attribute to read_only does not hint the compiler to optimize out the load
of x even though read_only is defined as being stronger than const ("The
mode implies a stronger guarantee than the const qualifier which, when cast
away from a pointer, does not prevent the pointed-to object from being
modified."), so in the following code:

__attribute__ ((access (read_only, 1))) void foo(const int *);

int bar(void)
{
    int x = 0;
    int y = 0;

    for (int i = 0; i < 10; i++) {
        foo(&x);
        y += x;  // this load not optimized out even though we have set the
access to read_only
    }
    return y;
}

The load of x should really be optimized out but isn't. So is this an
oversight in gcc or is the access attribute completely ignored by the
optimizer for some good reason?

If there is no good reason for this then changing this to hint the
optimizer should enable some nice optimizations of external functions where
const in the declaration is not cast away.

Regards,
  Henrik Holst

             reply	other threads:[~2022-09-06 14:22 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-06 14:22 Henrik Holst [this message]
2022-09-06 14:47 ` Richard Biener
2022-09-06 15:19   ` Henrik Holst
2022-09-07  7:48     ` Richard Biener
2022-09-07 11:37       ` Henrik Holst
2022-09-07 12:03         ` Richard Biener

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='CAGwK9dx8f1p+aDAq=ZKzo5BapUti2RMvVpfa94kRaEq9i9HeNA@mail.gmail.com' \
    --to=henrik.holst@millistream.com \
    --cc=gcc@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).