Re: [PATCH 1/3] powerpc: Add support for 128-bit cmpxchg
From: Christophe Leroy (CS GROUP)
Date: Thu Dec 03 2026 - 04:41:48 EST
Le 02/12/2026 à 18:52, David Laight a écrit :
On Wed, 2 Dec 2026 11:13:24 +0100
"Christophe Leroy (CS GROUP)" <chleroy@xxxxxxxxxx> wrote:
Le 02/12/2026 à 10:57, David Laight a écrit :
On Mon, 30 Nov 2026 18:58:11 +0100
"Christophe Leroy (CS GROUP)" <chleroy@xxxxxxxxxx> wrote:
Why a memory clobber ? Is it to get a barrier ? Otherwise I see no
direct hidden memory modification in the sequence.
I found something horrid during testing:
The compiler (gcc) will spill the value of a memory operand to stack.
So:
long f(long *p)
{
register long x asm("r3");
asm( "opcode %[x],%[p]" : [x] "=&r" (x) : [p] "m" (*p));
return x;
}
compiles to:
f:
ld 9,0(3)
std 9,-16(1)
ori 2,2,0
opcode 3,-16(1)
blr
What 'opcode' is that ? Because powerpc is a RISC processor, there is no
way ALU operation can operate on something else than registers.
A memory read would do.
I was playing around trying to get the compiler to generate addressing
modes that lq doesn't support.
The point is that the asm block isn't given a reference to the address
it expects.
It also means that the size of the variable has to be right.
If it can do the same for an output then asm blocks that try to do
atomic operations will do them on a copy.
Or consider (with -mno-altivec) this:
u128 f(int xx, u128 *p)
{
register u128 x asm("r4");
asm( "lq %[x],%[p]" : [x] "=&r" (x) : [p] "m" (*p));
return x;
}
generates:
f:
ld 9,0(4)
ld 10,8(4)
std 9,-16(1)
std 10,-8(1)
ori 2,2,0
lq 4,-16(1)
mr 3,4
mr 4,5
blr
A little contrived and the byte order is wrong but it is failing to
do the expected atomic read of the 128bit value.
Looks like the "m" generates the copy.
When you use "+m" it doesn't.
By the way you should use "+Q" instead of "+m" I think.
With:
u128 f(int xx, u128 *p)
{
register u128 x asm("r4");
asm volatile("lq %[x],%[mem]" : [x] "=&r" (x), [mem] "+Q" (*p));
return x;
}
I get
0000000000005490 <f>:
5490: 78 23 89 7c mr r9,r4
5494: 00 00 89 e0 lq r4,0(r9)
5498: 78 23 83 7c mr r3,r4
549c: 78 2b a4 7c mr r4,r5
54a0: 20 00 80 4e blr
Christophe