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]

 



在 2025-6-5 21:18, Segher Boessenkool 写道:
Hi!

On Thu, Jun 05, 2025 at 02:14:24PM +0200, Georg-Johann Lay via Gcc-help wrote:
Am 05.06.25 um 13:43 schrieb Xi Ruoyao via Gcc-help:
When we write syscall wrappers with inline assembly, we often need to
assign one register for both an input variable and an output variable.
Per https://gcc.gnu.org/onlinedocs/gcc/Local-Register-Variables.html,
such an inline assembly can be written

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 does not treat `p1` and `result` as referencing the same datum; a value that is written into one of them is not visible from the other, which can produce strange results like https://gcc.godbolt.org/z/nohrc9oxq

So the suggestion below is much much better.

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


Btw, you can write the code as

register int *p1 asm ("r0") = …;
register int *p2 asm ("r1") = …;
int *result;
asm ("sysint" : "+r" (p1) : "0" (p1), "r" (p2));
result = p1;

(and by choosing better variable names, "r0" and "r1" perhaps, you don't
need to write a copy).



--
Best regards,
LIU Hao

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature


[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