public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* how to disable optimization for particular assignment statements?
@ 2013-11-21  8:22 Andrew Makhorin
  2013-11-21  8:55 ` Marc Glisse
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Makhorin @ 2013-11-21  8:22 UTC (permalink / raw)
  To: gcc-help

Hello,

I have a C code like this:

int foo(void)
{     int phase;
      . . .
      phase = 1;
      phase = 2;
      phase = 3;
      . . .
}

In case of -O0 gcc generates machine instructions for every
assignment 'phase = ...'. But in case of -O2 gcc does not generate
instructions for some assigments. Of course, this is correct. However,
is there any way to tell gcc that 'phase' object is inspected by another
thread, so it should not remove such statements?

Thank you,

Andrew Makhorin


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: how to disable optimization for particular assignment statements?
  2013-11-21  8:22 how to disable optimization for particular assignment statements? Andrew Makhorin
@ 2013-11-21  8:55 ` Marc Glisse
  2013-11-21  9:20   ` Marc Glisse
  0 siblings, 1 reply; 4+ messages in thread
From: Marc Glisse @ 2013-11-21  8:55 UTC (permalink / raw)
  To: Andrew Makhorin; +Cc: gcc-help

On Thu, 21 Nov 2013, Andrew Makhorin wrote:

> Hello,
>
> I have a C code like this:
>
> int foo(void)
> {     int phase;
>      . . .
>      phase = 1;
>      phase = 2;
>      phase = 3;
>      . . .
> }
>
> In case of -O0 gcc generates machine instructions for every
> assignment 'phase = ...'. But in case of -O2 gcc does not generate
> instructions for some assigments. Of course, this is correct. However,
> is there any way to tell gcc that 'phase' object is inspected by another
> thread, so it should not remove such statements?

volatile

-- 
Marc Glisse

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: how to disable optimization for particular assignment statements?
  2013-11-21  8:55 ` Marc Glisse
@ 2013-11-21  9:20   ` Marc Glisse
  2013-11-23  2:48     ` Ian Lance Taylor
  0 siblings, 1 reply; 4+ messages in thread
From: Marc Glisse @ 2013-11-21  9:20 UTC (permalink / raw)
  To: gcc-help; +Cc: Andrew Makhorin

On Thu, 21 Nov 2013, Marc Glisse wrote:

> On Thu, 21 Nov 2013, Andrew Makhorin wrote:
>
>> Hello,
>> 
>> I have a C code like this:
>> 
>> int foo(void)
>> {     int phase;
>>      . . .
>>      phase = 1;
>>      phase = 2;
>>      phase = 3;
>>      . . .
>> }
>> 
>> In case of -O0 gcc generates machine instructions for every
>> assignment 'phase = ...'. But in case of -O2 gcc does not generate
>> instructions for some assigments. Of course, this is correct. However,
>> is there any way to tell gcc that 'phase' object is inspected by another
>> thread, so it should not remove such statements?
>
> volatile

Well, volatile will prevent the operations from being removed. For proper 
synchronization with other threads, using atomic operations with the right 
synchronization parameter sounds better.

http://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html

-- 
Marc Glisse

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: how to disable optimization for particular assignment statements?
  2013-11-21  9:20   ` Marc Glisse
@ 2013-11-23  2:48     ` Ian Lance Taylor
  0 siblings, 0 replies; 4+ messages in thread
From: Ian Lance Taylor @ 2013-11-23  2:48 UTC (permalink / raw)
  To: gcc-help; +Cc: Andrew Makhorin

On Thu, Nov 21, 2013 at 12:55 AM, Marc Glisse <marc.glisse@inria.fr> wrote:
> On Thu, 21 Nov 2013, Marc Glisse wrote:
>
>> On Thu, 21 Nov 2013, Andrew Makhorin wrote:
>>
>>> Hello,
>>>
>>> I have a C code like this:
>>>
>>> int foo(void)
>>> {     int phase;
>>>      . . .
>>>      phase = 1;
>>>      phase = 2;
>>>      phase = 3;
>>>      . . .
>>> }
>>>
>>> In case of -O0 gcc generates machine instructions for every
>>> assignment 'phase = ...'. But in case of -O2 gcc does not generate
>>> instructions for some assigments. Of course, this is correct. However,
>>> is there any way to tell gcc that 'phase' object is inspected by another
>>> thread, so it should not remove such statements?
>>
>>
>> volatile
>
>
> Well, volatile will prevent the operations from being removed. For proper
> synchronization with other threads, using atomic operations with the right
> synchronization parameter sounds better.

Basic rule of thumb: if code is not thread-safe, adding volatile will
never make it thread-safe.  That rule of thumb applies here.  Adding
volatile will not work, because it will not make the changes to the
value visible to other processors.


> http://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html

Yes, if you have decided that this is what you really want to do,
using the atomic (or sync) builtins is the only viable approach.

Ian

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-11-22 19:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-21  8:22 how to disable optimization for particular assignment statements? Andrew Makhorin
2013-11-21  8:55 ` Marc Glisse
2013-11-21  9:20   ` Marc Glisse
2013-11-23  2:48     ` Ian Lance Taylor

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).