Re: Constraint for inline assembly with one register used for both an input variable and an output variable

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi!

On Fri, Jun 06, 2025 at 04:25:55PM +0800, LIU Hao wrote:
> >>>register int *p1 asm ("r0") = …;
> >>>register int *p2 asm ("r1") = …;
> >>>register int *result asm ("r0");
> >>>asm ("sysint" : "=r" (result) : "0" (p1), "r" (p2));
> >>>
> >>>Note the use of constraint "0" for p1 which shares the same register as
> >>>result.  But currently in the Linux kernel vDSO, they are actually
> >>>written like
> >>>
> >>>asm ("sysint" : "=r" (result) : "r" (p1), "r" (p2));
> >>>
> >>>So the question here: is using "r" for p1 valid or not?  Do we need to
> >>>"fix" this everywhere for the vDSO?
> >>
> >>That code is perfectly fine, since the register numbers for p1 and
> >>result are the same "r0" at the point of the inline asm.
> >
> >Yup.  Various compiler passes will not realise "result" is the same reg
> >as "p1", but that probably won't limit optimisation here.
> 
> FWIW, maybe that's too much into the implementation details of GCC. Clang 

This is the gcc-help@ ML.  Anything LLVM is out of scope.

LLVM claims they implement all GNU C extensions faithfully.  Are you
saying they do not?

> (And this asm statement is likely a candidate for `volatile`.)

It is very much not.  If "result" is unused you *want* the code to be
optimised away, and you *want* it to get CSE applied where appropriate,
etc.

"volatile" has a meaning.  It means "this asm has some unspecified
side effect (that needs to end up in the target code as well)".  It does
not mean "I am scared the code will not work correctly, I probably made
some mistakes in it".  You should not write extended asm (or any other
asm, or any other computer languge) if you do not know what you are
doing.

There are easier, more expressive, less dangerous ways than asm to do
most things.


Segher



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux