[LLVMdev] X86 gcc and clang have incompatible calling conventions for returning some small structs?

Kees van Reeuwijk reeuwijk at few.vu.nl
Thu Jul 8 05:58:32 PDT 2010


Hello,

I think I have come across an inconsistency between gcc and clang/llvm with respect to returning small structs. Given the 
following code:


> struct s {
>     int a;
>     int b;
> };
>
> struct s3 {
>     int a;
>     int b;
>     int c;
> };
>
> struct s new_s(int v){
>     struct s res;
>     res.a = v;
>     res.b = -v;
>     return res;
> }
>
> struct s3 new_s3(int v){
>     struct s3 res;
>     res.a = v;
>     res.b = -v;
>     res.c = v<<2;
>     return res;
> }

For the function new_s() both the current Ubuntu gcc and the current Ubuntu clang generate code to return struct s in registers. 
However for the function new_s3() the gcc compiler uses the stack, whereas clang uses registers, so the generated code is not 
compatible.

$ gcc --version
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
$ clang --version
clang version 1.1 (branches/release_27)
Target: x86_64-pc-linux-gnu
Thread model: posix


It is possible that this is specific to the Ubuntu clang or gcc, but it seems unlikely that the Ubuntu people would tinker with this.


More generally, I'm looking for the exact specification of the calling conventions that llvm supports, in particular for returning 
small structs. I'm also looking for a way to let llvm always use the stack for such small structures, since that's the calling 
convention on the platform I'm porting to (Minix 3).

Thanks for any help with this.

-- 
Dr. Ir. Kees van Reeuwijk, Vrije Universiteit Amsterdam



More information about the llvm-dev mailing list