[flang-commits] [flang] [flang] Inherit target specific code for BIND(C) types on Windows (PR #129579)

via flang-commits flang-commits at lists.llvm.org
Fri Mar 7 01:20:15 PST 2025


jeanPerier wrote:

> > why clang does not set the byval attribute when passing the struct in memory (like it is done for linux)?
> 
> "byval" is weird: it actually allocates memory on the stack, as part of the argument list, then copies the argument into that memory. Old targets sometimes pass memory like this. The designers for more modern ABIs have recognized that passing around large values like that isn't a good idea, so they don't do that; they pass a pointer to a temporary allocated on the stack. Try looking at the assembly on x86_64-linux-gnu vs. x86_64-windows-msvc for `struct S { int x[100]; }; void f(struct S); void g() { f((struct S){}); }` if you're confused.

Thanks @efriedma-quic, that clarified the difference for me! Looking at the callee side, I actually see now that for x86_64-linux-gnu, the callee is looking for the argument in memory at specific `%rsp + offset`, while windows fetches the memory from the argument registers like for normal pointer arguments.

> The "caller-allocated temporary memory must be 16-byte aligned" specifically only applies to calls; in other contexts, normal alignment rules apply.

My question was more specific to returned struct (sret). Looking at the ABI spec, I do not see a specific requirement for the alignment of the return value https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-170#return-values, does that mean normal alignment rules apply for the hidden argument for the result, while it is always 16 for struct value argument passed in memory?

Also, looking at LLVM IR emitted by clang for x86_64-windows-msvc, I see that memory allocated for struct value _argument_ is not always 16 byte aligned (see the `%2 = alloca %struct.S, align 4` in the [godbolt LLVM IR for your example](https://godbolt.org/z/jK1znT6dE)). Is this a bug on clang side, or am I misreading https://learn.microsoft.com/en-us/cpp/build/x64-calling-convention?view=msvc-170#parameter-passing requirements?



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


More information about the flang-commits mailing list