i386 clang optimisation problem with stack alignment
Tijl Coosemans
tijl at FreeBSD.org
Thu Sep 19 17:56:30 UTC 2013
On Wed, 18 Sep 2013 23:13:26 +0200 Dimitry Andric wrote:
> On Sep 10, 2013, at 18:34, Tijl Coosemans <tijl at freebsd.org> wrote:
>> On Tue, 10 Sep 2013 18:16:01 +0200 Tijl Coosemans wrote:
>>> I've attached a small test program extracted from multimedia/gstreamer-ffmpeg
>>> (libavcodec/h264_cabac.c:ff_h264_init_cabac_states(H264Context *h)).
>>>
>>> When you compile and run it like this on FreeBSD/i386, it results in a
>>> SIGBUS:
>>>
>>> % cc -o paddd paddd.c -O3 -msse2 -fPIE -fomit-frame-pointer
>>> % ./paddd
>>> Bus error
>>>
>>> The reason is this instruction where %esp isn't 16-byte aligned:
>>> paddd (%esp), %xmm7
>
> Hmm, as far as I can see, the problem is related to position independent
> code, in combination with omitting the frame pointer:
>
> $ cc -o paddd paddd.c -O3 -msse2 -fomit-frame-pointer
> $ ./paddd
> $
>
> $ cc -o paddd paddd.c -O3 -msse2 -fPIE -fomit-frame-pointer
> $ ./paddd
> Bus error (core dumped)
> $
>
> $ cc -o paddd paddd.c -O3 -msse2 -fPIE -fno-omit-frame-pointer
> $ ./paddd
> $
Omitting -fPIE frees up a register and that changes the generated code
too much to trigger the bug so I'm not sure it has anything to do with it.
-fomit-frame-pointer may be part of the problem though. Without a frame
pointer that holds the old value of %esp, the stack cannot be realigned
because the old value cannot be restored then. It seems clang/LLVM knows
this at least partly because with -fomit-frame-pointer it doesn't realign
stack and uses movdqu to store a value at (%esp) (instead of movdqa in
the -fno-omit-frame-pointer case).
Either clang/LLVM shouldn't use instructions like paddd in this case or it
should override -fomit-frame-pointer and use a frame pointer whenever the
stack needs realigning.
I added a comment to http://llvm.org/bugs/show_bug.cgi?id=12250 which
seems like the same bug (but on Solaris).
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.freebsd.org/pipermail/freebsd-toolchain/attachments/20130919/88699efd/attachment.sig>
More information about the freebsd-toolchain
mailing list