llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

PowerPC ABI incompatibility with GNU on complex arguments

Open ukalappa-mips opened this issue 4 years ago • 11 comments

We recently moved llvm from GCC and found that their is ABI difference in the two compilers. GNU passes and returns double complex in GP registers. LLVM passes an returns the value on the stack.

return double complex numbers (the assumption is this applies to complex, double complex, and long double complex).

https://godbolt.org/z/qqzzesK9r

Its case with ppc32 and we agree that the ABI insist the same semantics like LLVM ,can we have change the semantics like GCC with option ?

ukalappa-mips avatar Jun 14 '22 04:06 ukalappa-mips

@llvm/issue-subscribers-backend-powerpc

llvmbot avatar Jun 14 '22 04:06 llvmbot

So, likely the issue should to be reported to GCC?

asl avatar Jun 15 '22 09:06 asl

@asl ,my bad http://refspecs.linux-foundation.org/elf/elfspec_ppc.pdf according to this , A struct, union, or long double, any of which shall be treated as a pointer to the object, or to a copy of the object where necessary to enforce call-by-value semantics. Only if the caller can ascertain that the object is "constant" can it pass a pointer to the object itself.

I think GCC following like to a copy of the object where necessary to enforce call-by-value semantics.

So i guess we need to have this implementation in llvm too and same goes to return value .

@chmeeedalf and @nemanjai your thoughts here ?

ukalappa-mips avatar Jun 15 '22 14:06 ukalappa-mips

@asl , @nemanjai and @chmeeedalf

We made the changes ,can i raise a review with llvm trunk ?

ukalappa-mips avatar Jun 28 '22 04:06 ukalappa-mips

This is still a problem on ppc32. If I call csin(3) in libm, gcc compiled my call, and clang had compiled libm, then it segfaults.

The ppc32 ABI has 2 ways to pass complex args. The old version from 1995 (elfspec_ppc.pdf) predates C99 and doesn't mention complex args, but the new version from 2011 (Power-Arch-32-bit-ABI-supp-1.0-Unified.pdf) specifies ATR-PASS-COMPLEX-IN-STRUCT and ATR-PASS-COMPLEX-IN-GPRS. clang looks like ATR-PASS-COMPLEX-IN-STRUCT, but gcc looks like ATR-PASS-COMPLEX-IN-GPRS. These are not compatible.

I looked at cc -fno-stack-protector -fno-pie -O1 -S ctry.c, where ctry.c is,

float _Complex v1;
double _Complex v2;
float _Complex csinf(float _Complex);
double _Complex csin(double _Complex);
void f1(void) { v1 = csinf(v1); }
void f2(void) { v2 = csin(v2); }

clang main from July 12 (8fe076f) passes complex in structs. csinf(v1) returns a small struct, so it uses -maix-struct-return on Linux or -msvr4-struct-return on BSD. clang -target powerpc-linux calls csinf(v1) with r3 = address of return value, r4 = address of v1. clang -target powerpc-openbsd calls csinf(v1) with r3 = address of v1, and expects the return value in r3:r4. clang with either target calls csin(v2) with r3 = address of return value, r4 = address of v1.

gcc passes complex in general-purpose registers. gcc calls csinf(v1) with r3:r4 = v1, and expects the return value in r3:r4. gcc calls csin(v2) with r3:r4:r5:r6 = v2, and expects the return value in r3:r4:r5:r6. This is awkward because it doesn't use the floating-point registers. The ABI and gcc do align register pairs as odd-even, but don't align register quads. So, gcc calls f(int, float complex) with r3 = int, r5:r6 = float complex, skipping r4 to align the pair; but gcc calls g(int, double complex) with r3 = int, r4:r5:r6:r7 = double complex, without skipping registers.

kernigh avatar Jul 14 '22 00:07 kernigh

@asl , @nemanjai and @chmeeedalf

We made the changes ,can i raise a review with llvm trunk ?

Sorry for delay, I've been in the process of moving over the last couple months.

Please do submit a review for further discussion. It may get a wider audience there, too.

chmeeedalf avatar Jul 14 '22 15:07 chmeeedalf

@kernigh

brad0 avatar Nov 27 '22 07:11 brad0

ping.

brad0 avatar Dec 25 '22 06:12 brad0

https://reviews.llvm.org/D146942

Long5hot avatar Aug 02 '23 13:08 Long5hot

https://github.com/llvm/llvm-project/pull/77732

thesamesam avatar Jan 24 '24 08:01 thesamesam

Just curious, @ukalappa-mips what made you close this issue? The PR adding the flag looks stalled, and as far as I can tell this issue has not been resolved

https://godbolt.org/z/bhjz8s3K5

folkertdev avatar Jul 09 '26 18:07 folkertdev