* Unjustified optimization due to restricted struct members?
@ 2023-11-30 11:05 Ties Klappe
2023-11-30 12:12 ` Richard Biener
2023-11-30 17:16 ` Joseph Myers
0 siblings, 2 replies; 5+ messages in thread
From: Ties Klappe @ 2023-11-30 11:05 UTC (permalink / raw)
To: gcc
[-- Attachment #1: Type: text/plain, Size: 983 bytes --]
When reading section 6.7.3.1 of the C standard (quoted below) about
the *restrict
*type qualifier, the first section talks about *ordinary identifiers*.
These are defined in section 6.2.3, and exclude members of structures.
Let D be a declaration of an ordinary identifier that provides a means of
> designating an object P as a restrict-qualified pointer to type T.
I would assume that this means that in the code excerpt below the function
*h* cannot be optimized by substituting the load of *b.p *for *10*, as the
standard does not specify what it means for a struct member to be restrict
qualified. However, the code is still optimized by gcc (but not Clang), as
can be seen here: https://godbolt.org/z/hEnKKoaae
struct bar {
int* restrict p;
int* restrict q;
};
int h(struct bar b) {
*b.p = 10;
*b.q = 11;
return *b.p;
}
Was this a deliberate choice, or does it simply follow from how restrict is
supported in gcc (and could this be considered a bug w.r.t. the standard)?
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Unjustified optimization due to restricted struct members?
2023-11-30 11:05 Unjustified optimization due to restricted struct members? Ties Klappe
@ 2023-11-30 12:12 ` Richard Biener
2023-11-30 12:50 ` Ties Klappe
2023-11-30 17:16 ` Joseph Myers
1 sibling, 1 reply; 5+ messages in thread
From: Richard Biener @ 2023-11-30 12:12 UTC (permalink / raw)
To: Ties Klappe; +Cc: gcc
On Thu, Nov 30, 2023 at 12:07 PM Ties Klappe via Gcc <gcc@gcc.gnu.org> wrote:
>
> When reading section 6.7.3.1 of the C standard (quoted below) about
> the *restrict
> *type qualifier, the first section talks about *ordinary identifiers*.
> These are defined in section 6.2.3, and exclude members of structures.
>
> Let D be a declaration of an ordinary identifier that provides a means of
> > designating an object P as a restrict-qualified pointer to type T.
>
>
> I would assume that this means that in the code excerpt below the function
> *h* cannot be optimized by substituting the load of *b.p *for *10*, as the
> standard does not specify what it means for a struct member to be restrict
> qualified. However, the code is still optimized by gcc (but not Clang), as
> can be seen here: https://godbolt.org/z/hEnKKoaae
>
> struct bar {
> int* restrict p;
> int* restrict q;
> };
>
> int h(struct bar b) {
> *b.p = 10;
> *b.q = 11;
> return *b.p;
> }
>
> Was this a deliberate choice, or does it simply follow from how restrict is
> supported in gcc (and could this be considered a bug w.r.t. the standard)?
Hmm, this was a deliberate choice (it also works for global 'b'), I didn't think
the standard would exclude that. Note GCCs C++ standard library makes
use of restrict qualified pointers as structure members for example.
Richard.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Unjustified optimization due to restricted struct members?
2023-11-30 12:12 ` Richard Biener
@ 2023-11-30 12:50 ` Ties Klappe
2023-11-30 13:06 ` Richard Biener
0 siblings, 1 reply; 5+ messages in thread
From: Ties Klappe @ 2023-11-30 12:50 UTC (permalink / raw)
To: Richard Biener; +Cc: gcc
[-- Attachment #1: Type: text/plain, Size: 2003 bytes --]
Thank you Richard.
Similar to the struct example, I was also wondering about why the following
code does *not* get optimized (e.g. https://godbolt.org/z/9eGrjjK81):
int f(int* restrict a[restrict 2]) {
*(a[0]) = 10;
*(a[1]) = 11;
return *(a[0]);
}
Do you happen to know why a reload via a[0] is required? I would have
expected to see the same optimization as is performed for the struct
example.
Kind regards,
Ties
Op do 30 nov 2023 om 13:16 schreef Richard Biener <
richard.guenther@gmail.com>:
> On Thu, Nov 30, 2023 at 12:07 PM Ties Klappe via Gcc <gcc@gcc.gnu.org>
> wrote:
> >
> > When reading section 6.7.3.1 of the C standard (quoted below) about
> > the *restrict
> > *type qualifier, the first section talks about *ordinary identifiers*.
> > These are defined in section 6.2.3, and exclude members of structures.
> >
> > Let D be a declaration of an ordinary identifier that provides a means of
> > > designating an object P as a restrict-qualified pointer to type T.
> >
> >
> > I would assume that this means that in the code excerpt below the
> function
> > *h* cannot be optimized by substituting the load of *b.p *for *10*, as
> the
> > standard does not specify what it means for a struct member to be
> restrict
> > qualified. However, the code is still optimized by gcc (but not Clang),
> as
> > can be seen here: https://godbolt.org/z/hEnKKoaae
> >
> > struct bar {
> > int* restrict p;
> > int* restrict q;
> > };
> >
> > int h(struct bar b) {
> > *b.p = 10;
> > *b.q = 11;
> > return *b.p;
> > }
> >
> > Was this a deliberate choice, or does it simply follow from how restrict
> is
> > supported in gcc (and could this be considered a bug w.r.t. the
> standard)?
>
> Hmm, this was a deliberate choice (it also works for global 'b'), I didn't
> think
> the standard would exclude that. Note GCCs C++ standard library makes
> use of restrict qualified pointers as structure members for example.
>
> Richard.
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Unjustified optimization due to restricted struct members?
2023-11-30 12:50 ` Ties Klappe
@ 2023-11-30 13:06 ` Richard Biener
0 siblings, 0 replies; 5+ messages in thread
From: Richard Biener @ 2023-11-30 13:06 UTC (permalink / raw)
To: Ties Klappe; +Cc: gcc
On Thu, Nov 30, 2023 at 1:50 PM Ties Klappe <tg.klappe@gmail.com> wrote:
>
> Thank you Richard.
>
> Similar to the struct example, I was also wondering about why the following code does not get optimized (e.g. https://godbolt.org/z/9eGrjjK81):
>
> int f(int* restrict a[restrict 2]) {
> *(a[0]) = 10;
> *(a[1]) = 11;
> return *(a[0]);
> }
>
> Do you happen to know why a reload via a[0] is required? I would have expected to see the same optimization as is performed for the struct example.
It's not implemented. I think there's even a bugreport about this,
basically restrict
gets only "one layer deep".
Richard.
> Kind regards,
> Ties
>
> Op do 30 nov 2023 om 13:16 schreef Richard Biener <richard.guenther@gmail.com>:
>>
>> On Thu, Nov 30, 2023 at 12:07 PM Ties Klappe via Gcc <gcc@gcc.gnu.org> wrote:
>> >
>> > When reading section 6.7.3.1 of the C standard (quoted below) about
>> > the *restrict
>> > *type qualifier, the first section talks about *ordinary identifiers*.
>> > These are defined in section 6.2.3, and exclude members of structures.
>> >
>> > Let D be a declaration of an ordinary identifier that provides a means of
>> > > designating an object P as a restrict-qualified pointer to type T.
>> >
>> >
>> > I would assume that this means that in the code excerpt below the function
>> > *h* cannot be optimized by substituting the load of *b.p *for *10*, as the
>> > standard does not specify what it means for a struct member to be restrict
>> > qualified. However, the code is still optimized by gcc (but not Clang), as
>> > can be seen here: https://godbolt.org/z/hEnKKoaae
>> >
>> > struct bar {
>> > int* restrict p;
>> > int* restrict q;
>> > };
>> >
>> > int h(struct bar b) {
>> > *b.p = 10;
>> > *b.q = 11;
>> > return *b.p;
>> > }
>> >
>> > Was this a deliberate choice, or does it simply follow from how restrict is
>> > supported in gcc (and could this be considered a bug w.r.t. the standard)?
>>
>> Hmm, this was a deliberate choice (it also works for global 'b'), I didn't think
>> the standard would exclude that. Note GCCs C++ standard library makes
>> use of restrict qualified pointers as structure members for example.
>>
>> Richard.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Unjustified optimization due to restricted struct members?
2023-11-30 11:05 Unjustified optimization due to restricted struct members? Ties Klappe
2023-11-30 12:12 ` Richard Biener
@ 2023-11-30 17:16 ` Joseph Myers
1 sibling, 0 replies; 5+ messages in thread
From: Joseph Myers @ 2023-11-30 17:16 UTC (permalink / raw)
To: Ties Klappe; +Cc: gcc
On Thu, 30 Nov 2023, Ties Klappe via Gcc wrote:
> When reading section 6.7.3.1 of the C standard (quoted below) about
> the *restrict
> *type qualifier, the first section talks about *ordinary identifiers*.
> These are defined in section 6.2.3, and exclude members of structures.
>
> Let D be a declaration of an ordinary identifier that provides a means of
> > designating an object P as a restrict-qualified pointer to type T.
>
>
> I would assume that this means that in the code excerpt below the function
> *h* cannot be optimized by substituting the load of *b.p *for *10*, as the
> standard does not specify what it means for a struct member to be restrict
> qualified. However, the code is still optimized by gcc (but not Clang), as
> can be seen here: https://godbolt.org/z/hEnKKoaae
>
> struct bar {
> int* restrict p;
> int* restrict q;
> };
>
> int h(struct bar b) {
In this code, 'b' is the ordinary identifier; "struct bar b" is the
declaration D. The declaration does not itself need to have pointer type.
--
Joseph S. Myers
joseph@codesourcery.com
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-11-30 17:16 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-30 11:05 Unjustified optimization due to restricted struct members? Ties Klappe
2023-11-30 12:12 ` Richard Biener
2023-11-30 12:50 ` Ties Klappe
2023-11-30 13:06 ` Richard Biener
2023-11-30 17:16 ` Joseph Myers
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).