This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: O2 Agressive Optimisation by GCC
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Allan Sandfeld Jensen <linux at carewolf dot com>
- Cc: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>, Umesh Kalappa <umesh dot kalappa0 at gmail dot com>
- Date: Fri, 20 Jul 2018 23:21:48 +0100
- Subject: Re: O2 Agressive Optimisation by GCC
- References: <CAGfacvTvkR6d+dqG0Et04gxqcz9JcH3JV1vi8S=WbfJbOnv3JA@mail.gmail.com> <1700194.Gr8vF7hmjn@twilight>
On Fri, 20 Jul 2018 at 23:06, Allan Sandfeld Jensen wrote:
>
> On Freitag, 20. Juli 2018 14:19:12 CEST Umesh Kalappa wrote:
> > Hi All ,
> >
> > We are looking at the C sample i.e
> >
> > extern int i,j;
> >
> > int test()
> > {
> > while(1)
> > { i++;
> > j=20;
> > }
> > return 0;
> > }
> >
> > command used :(gcc 8.1.0)
> > gcc -S test.c -O2
> >
> > the generated asm for x86
> >
> > .L2:
> > jmp .L2
> >
> > we understand that,the infinite loop is not deterministic ,compiler
> > is free to treat as that as UB and do aggressive optimization ,but we
> > need keep the side effects like j=20 untouched by optimization .
> >
> > Please note that using the volatile qualifier for i and j or empty
> > asm("") in the while loop,will stop the optimizer ,but we don't want
> > do that.
> >
> But you need to do that! If you want changes to a variable to be observable in
> another thread, you need to use either volatile,
No, volatile doesn't work for that.
http://www.isvolatileusefulwiththreads.in/C/
> atomic, or some kind of
> memory barrier implicit or explicit. This is the same if the loop wasn't
> infinite, the compiler would keep the value in register during the loop and
> only write it to memory on exiting the test() function.
>
> 'Allan
>
>