This is the mail archive of the
mailing list for the GCC project.
Re: AW: basic asm and memory clobbers - Proposed solution
- From: David Wohlferd <dw at LimeGreenSocks dot com>
- To: Bernd Edlinger <bernd dot edlinger at hotmail dot de>, David Brown <david at westcontrol dot com>
- Cc: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Wed, 2 Dec 2015 15:27:54 -0800
- Subject: Re: AW: basic asm and memory clobbers - Proposed solution
- Authentication-results: sourceware.org; auth=none
- References: <HE1PR07MB09059B9BF765EAA730813EE1E40E0 at HE1PR07MB0905 dot eurprd07 dot prod dot outlook dot com>
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;
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;
x = 0;
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.