[PATCH] Correctly align arguments after a byval struct is passed on the stack

Manman Ren manman.ren at gmail.com
Tue Mar 4 10:05:18 PST 2014

On Tue, Mar 4, 2014 at 3:36 AM, Oliver Stannard <oliver.stannard at arm.com>wrote:

> Hi rengolin,
> This patch fixes a bug that would cause an 8-byte-aligned function argument
> to be consistently misaligned in some situations involving byval arguments.
> The following function signature is one of these cases:
> %struct12bytes = type { i32, i32, i32 }
> define void @foo(i32 %a, %struct12bytes* byval %b, i64 %c) {
> When arguments are initially been allocated, %b has 12 bytes reserved on
> the
> stack, but this is later adjusted to use registers r1..r3 instead of the
> stack. However, the 12 bytes are still used when calculating the alignment
> of %c, causing %c to be allocated 4 bytes above the top of %b, at stack
> offset 0x10. The stack pointer is moved down 12 bytes at the beginning of
> the function  to make space for the part of %b which is in registers (all
> of
> it in this example), plus another 4 bytes to maintain 8-byte SP alignment.
> However, the 4 bytes of alignment gap between %b and %c are still present,
> resulting in %c being loaded from SP+0x14, which is not 8-byte aligned.
> The solution to this is to not allocate stack space for the part of a byval
> which will be passed in registers.

Maybe I didn't get the whole picture, but I think we need to allocate stack
space  for
the part of a byval that will be passed in registers, so the part passed in
and the part passed on stack will be laid out contiguously on stack.


This requires some extra work to
> calculate the location of byval arguments on the stack, especially in the
> case of multiple small byvals in registers, as previously the stack
> locations were used to record the location of a byval after storing to the
> stack.
> This bug can be triggered from C, using a similar test case, but argument b
> must be larger than 64 bytes to cause clang to emit a byval argument.
> I have put this patch through a night of random PCS testing with no
> failures.
> http://llvm-reviews.chandlerc.com/D2933
> Files:
>   include/llvm/CodeGen/CallingConvLower.h
>   lib/Target/ARM/ARMFrameLowering.cpp
>   lib/Target/ARM/ARMISelLowering.cpp
>   lib/Target/ARM/ARMISelLowering.h
>   lib/Target/ARM/Thumb1FrameLowering.cpp
>   test/CodeGen/ARM/2013-04-05-Small-ByVal-Structs-PR15293.ll
>   test/CodeGen/ARM/2013-05-02-AAPCS-ByVal-Structs-C4-C5-VFP.ll
>   test/CodeGen/ARM/2014-02-21-byval-reg-split-alignment.ll
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140304/65d33d55/attachment.html>

More information about the llvm-commits mailing list