tags:

views:

516

answers:

2

Hi there,

I'm trying to make a program compiled with GCC and using Qt and SSE intrinsics. It seems that when one of my functions is called by Qt, the stack alignment is not preserved. Here's a short example to illustrate what I mean :

#include <cstdio>
#include <emmintrin.h>
#include <QtGui/QApplication.h>
#include <QtGui/QWidget.h>


class Widget: public QWidget {
public:
    void paintEvent(QPaintEvent *) {
        __m128 a;
        printf("a: 0x%08x\n", ((void *) &a));
    }
};


int main(int argc, char** argv)
{
    QApplication application(argc, argv);
    Widget w;
    w.paintEvent(NULL); // Called from here, my function behaves correctly
    w.show();
    w.update();
    // Qt will call Widget::paintEvent and my __m128 will not be
    // aligned on 16 bytes as it should
    application.processEvents();

    return 0;
}

Here's the output:

a: 0x0023ff40 // OK, that's aligned on 16 bytes
a: 0x0023d14c // Not aligned!

Configuration:

  • Intel Core2
  • WinXP, SP3
  • GCC 4.4 (Mingw included in the Qt SDK 2010.01)

I tried to compile the example program with the same options as those I saw in the Qt makefile :

-O2 -Wall -frtti -fexceptions -mthreads

,link options:

-enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-s -mthreads

Now I don't know in which directions to search. Any hints would be appreciated. Thanks!

Fabien

A: 
void paintEvent(QPaintEvent *) __attribute__((force_align_arg_pointer));

made it work! Does anybody have a better solution?

Fabien
What happens if you try adding `-mms-bitfields` to the compiler flags?
Mihai Limbășan
+5  A: 

You can use the option -mstackrealign to do that without adding attributes to your source code:

-mstackrealign Realign the stack at entry. On the Intel x86, the -mstackrealign option will generate an alternate prologue and epilogue that realigns the runtime stack if necessary. This supports mixing legacy codes that keep a 4-byte aligned stack with modern codes that keep a 16-byte stack for SSE compatibility. See also the attribute force_align_arg_pointer, applicable to individual functions.

(from the GCC docs)

Berendra Tusla
Thanks!According to http://eigen.tuxfamily.org/dox/WrongStackAlignment.html, it seems that it's better for performance to use the force_align_arg_pointer attribute. Using -mincoming-stack-boundary=2 could be another solution (on Windows), so that "GCC knows that it really must take extra care to honor the 16 byte alignment".
Fabien