This is the mail archive of the
mailing list for the GCC project.
Re: basic asm and memory clobbers - Proposed solution
- From: Bernd Edlinger <bernd dot edlinger at hotmail dot de>
- To: David Wohlferd <dw at LimeGreenSocks dot com>, David Brown <david at westcontrol dot com>
- Cc: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Thu, 3 Dec 2015 05:29:36 +0000
- Subject: Re: basic asm and memory clobbers - Proposed solution
- Authentication-results: sourceware.org; auth=none
- Authentication-results: LimeGreenSocks.com; dkim=none (message not signed) header.d=none;LimeGreenSocks.com; dmarc=none action=none header.from=hotmail.de;
- References: <HE1PR07MB09059B9BF765EAA730813EE1E40E0 at HE1PR07MB0905 dot eurprd07 dot prod dot outlook dot com> <565F7E7A dot 8040004 at LimeGreenSocks dot com>
- Spamdiagnosticmetadata: NSPM
- Spamdiagnosticoutput: 1:23
On 03.12.2015 00:27 David Wohlferd wrote:
> On 12/2/2015 3:34 AM, Bernd Edlinger wrote:
>>> Surely in code like that, you would make "x" volatile? Memory clobbers
>>> are not a substitute for correct use of volatile accesses.
>> It is as I wrote, a memory clobber is the only way to guarantee that
>> the asm statement is not move somewhere else.
>> I changed the example to use volatile and compiled it
>> with gcc-Version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
>> volatile int x;
>> x = 1;
>> asm volatile("nop");
>> x = 0;
>> gcc -S -O2 test.c gives:
>> movl $1, x(%rip)
>> movl $0, x(%rip)
>> # 6 "test.c" 1
>> # 0 "" 2
>> While it works with asm volatile("nop" ::: "memory").
>> Likewise for "cli" and "sti" if you try to implement critical sections.
>> Although, these instructions do not touch any memory, we
>> need the memory clobber to prevent code motion.
> If the goal is to order things wrt x, why wouldn't you just reference x?
> x = 1;
> asm volatile("nop":"+m"(x));
> x = 0;
Exactly, that is what I mean. Either the asm can use memory clobber
or it can use output and/or input clobbers or any combination of that.
The problem with basic asm is not that it is basic, but that it does not
have any outputs nor any inputs and it has no way to specify what it
Therefore I think the condition for the warning should not be that the
asm is "basic", but that has zero outputs, zero inputs and zero clobbers.
That would make more sense to me.
> If you have a dependency, stating it explicitly seems a much better
> approach than hoping that the implied semantics of a memory clobber
> might get you what you want. Not only is it crystal clear for the
> optimizers what the ordering needs to be, people maintaining the code
> can better understand your intent as well.
> In summary: Yes inline asm can float. Clobbers might help. But I
> don't believe this is related to the "remove basic asm in functions"
> work. If a warning here is merited (and I'm not yet convinced), a
> separate case should be made for it.
> However it does seem like a good fit for a section in a "How to
> convert basic asm to extended asm" doc. The extended docs don't
> mention this need or how extended can be used to address it. It's a
> good reason for basic asm users to switch.