Passing structures by value on Windows

Hello, LLVM team!

I faced a problem with passing structures by value on Windows (both
x86 and x64). I tried to write simple { i32 i32 } Vector2D addition
using IR builder (signature is Vector2D add(Vector2D a, Vector2D b)).
x86 version worked fine, but on x64 function returned incorrect value.

Some research showed, that { i16 i16 } addition also fails on x86, so
I guess the problem is in passing structures as values.

On x64 VC++ passes two { i32 i32 } structs in RCX and RDX respectively
and reads result from RAX, but it seems LLVM reads parameters from
ECX, EDX (first vector) and R8D, R9D (second vector).

Currently, I can't figure out how to dump IR, but there is a link with
disassembly shown by Visual Studio for generated functions:
comparing { i32 i32 } add on 32-bit and 64-bit (first one works):
http://pastebin.com/ijjCNWKJ

Best regards,
Milovanov Victor.

Hello

On x64 VC++ passes two { i32 i32 } structs in RCX and RDX respectively
and reads result from RAX, but it seems LLVM reads parameters from
ECX, EDX (first vector) and R8D, R9D (second vector).

It's a frontend task to lower the code to follow the vendor ABI. In
this case you have to bitcast your struct to i64 before passing as
argument.

Is this documented somethere?

This is the standard assumption. The calling convention can be complex
enough and operate with the language-dependent definitions (e.g. "C"
structs, etc.), thus frontend should lower such constructs as needed.

For reference, normal x86-64 ABI has pretty complex rules wrt passing
aggregates by value. You might want to look into llvm-gcc as an
example.

Ok, but how to bitcast structure? Documentation says that bitcast does not applicable to aggregate values.

2010/6/3 Anton Korobeynikov <anton@korobeynikov.info>:

Just make an alloca, store the members of the struct to it, cast it to
an i64* (or whatever is appropriate), and load from it.

-Eli