Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#304 64 bit AVX generated mis-aligned access

open-works-for-me
nobody
None
5
2014-08-20
2012-06-11
Ian McConnell
No

I am developing some math code using AVX intrinsics to speed it up. The code runs correctly on 32/64 linux and 32 bit Windows, but when I compile the code for 64 bit Windows (from Ubuntu), the exe crashes with illegal instruction. I've submitted a bug report to gcc
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53485
but it's sat there marked as UNCONFIRMED. Can anyone confirm the bug for me?

The bug seems to be long standing as I can reproduce it with gcc 4.6.3 (x86_64-w64-mingw32-gcc-4.6 Ubuntu Precise) or with the nightly builds
gcc version 4.8.0 20120610 (experimental) (GCC)

./mingw/bin/x86_64-w64-mingw32-gcc -mavx bug.c -o bug.exe -g
BUG PROGRAM START: We should get this message once.
DEBUG fun1 start...
Illegal instruction
$ ./mingw64/bin/gdb bug.exe
(gdb) r
Starting program: bug.exe
[New Thread 2596.0x65c]
BUG PROGRAM START: We should get this message once.
DEBUG fun1 start...

Program received signal SIGILL, Illegal instruction.
fun1 () at bug.c:95
95 v1 = vfone;
(gdb) x/i $pc
=> 0x4019e2 <fun1+66>: vmovaps 0x7636(%rip),%ymm0 # 0x409020 <vfone>

I had a look through the bug tracker and this report
https://sourceforge.net/tracker/?func=detail&aid=3098320&group_id=202880&atid=983354
suggested trying -mstackrealign
./mingw/bin/x86_64-w64-mingw32-gcc -mavx bug.c -o bug.exe -g -mstackrealign
but I still get the "Illegal instruction". Any other suggestions for fixes or work arounds welcome.

Discussion

  • Ian McConnell
    Ian McConnell
    2012-06-11

    Source code generating mis-aligned access on 64 bit

     
    Attachments
  • Kai Tietz
    Kai Tietz
    2012-07-11

    • status: open --> pending-works-for-me
     
  • Kai Tietz
    Kai Tietz
    2012-07-11

    Are you sure that you have a cpu capable to support avx?

    I tested code it runs. if it would be an alignment-issue, as you were indicating to, we would see a different exception raised.

     
  • Ian McConnell
    Ian McConnell
    2012-07-11

    Oops! Yes you are right. The illegal instruction does come from a machine that doesn't have AVX. I didn't have access to the original machine when I was submitting the bug report and so I used a virtual machine which turns out not to support AVX.

    Running the code on a proper AVX machine actually gives:
    $ x86_64-w64-mingw32-gcc-4.8.0 -mavx bug.c -o bug.exe -g
    $ ./bug.exe
    BUG PROGRAM START: We should get this message once.
    DEBUG fun1 start...
    BUG PROGRAM START: We should get this message once.
    DEBUG fun1 start...
    BUG PROGRAM START: We should get this message once.
    DEBUG fun1 start...
    BUG PROGRAM START: We should get this message once.
    DEBUG fun1 start...
    BUG PROGRAM START: We should get this message once.
    DEBUG fun1 start...
    BUG PROGRAM START: We should get this message once.
    DEBUG fun1 start...

    Adding -mstackrealign gives the same output. ie "BUG PROGRAM START.." repeated six times.

    If I use the Intel Software Development Emulator which also supports AVX
    $ ./sde/sde -- ./bug.exe
    BUG PROGRAM START: We should get this message once.
    DEBUG fun1 start...
    SDE ERROR: TID: 0 executed instruction with an unaligned memory reference to address 0x22fdd0 INSTR: 0x0004019ef: IFORM: VMOVAPS_MEMqq_YMMqq :: vmovaps ymmword ptr [rbp-0x60], ymm0

    or if I use gdb:
    $ ./mingw64/bin/gdb.exe bug.exe
    This GDB was configured as "x86_64-w64-mingw32".
    (gdb) r
    Starting program: bug.exe
    [New Thread 2760.0xf9c]
    BUG PROGRAM START: We should get this message once.
    DEBUG fun1 start...

    Program received signal SIGSEGV, Segmentation fault.
    0x00000000004019ef in fun1 () at bug.c:99
    99 v1 = bug_vec_f(v1);
    (gdb) x/i $pc
    => 0x4019ef <fun1+95>: vmovaps %ymm0,-0x60(%rbp)

    I did find this article
    http://stackoverflow.com/questions/5983389/how-to-align-stack-at-32-byte-boundary-in-gcc
    which references
    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49001
    and using it I was able to write a wrapper around gcc so that it changes vmovaps to vmovups in the assembler.
    Crude, but it works for me
    $ x86_64-w64-mingw32-gcc-fix.sh -mavx bug.c -o bug.exe -g
    $ bug.exe
    BUG PROGRAM START: We should get this message once.
    DEBUG fun1 start...
    DEBUG m1
    DEBUG m2
    DEBUG m4
    DEBUG m5
    DEBUG m6
    DEBUG m1
    ...
    There doesn't seem to have been any progress on gcc bug 49001 for over a year, but I don't mind if this one is marked as a duplicate of that one. Unfortunately, I don't understand the stack alignment issues mentioned in 49001, so I can't add to it.

     
  • Ian McConnell
    Ian McConnell
    2012-07-11

    • status: pending-works-for-me --> open-works-for-me