I am writing a C++ GUI application using Dear ImGui (with Win32 as backend). My compiler is GCC 13.1.0 (part of the MinGW toolchain), set to 32-bit mode (-m32
).
I should mention that, for efficiency reasons, I am using the -msse2
and -mfpmath=sse
flags.
Normally, GCC aligns the stack at 16-bytes on function calls and expects that any functions that it generates will be called this way. The Windows API (and probably DirectX too) only align the stack to 4 bytes.
I have written my WndProc
function (which is a callback), but noticed that GCC doesn't realign the stack to the expected 16 bytes. For this simple case, I used __attribute__((force_align_arg_pointer))
to tell GCC to expect an unaligned stack (and thus realign it itself).
The problem is that I don't know which functions inside the ImGui library are actually used as callbacks (which can be called by outside code like Win32 or DirectX). I don't want to have to search through the entire ImGui code looking for callbacks and adding the attribute to each of them. And since ImGui is a call-heavy API, using -mstackrealign
would create a significant overhead because not all functions need to realign their stacks.
Does ImGui have a macro attribute (that I can redefine to include the force_align_arg_pointer
attribute) which is applied only to its internal callback functions?
And if not, is it possible to force GCC to use 4-byte stack alignment? Or would it be more efficient to resort to using x87 instead of SSE in this case?
Here is how I invoke GCC:
i686-w64-mingw32-g++ -std=c++20 -pedantic-errors -Wall -m32 -malign-double -msse2 -mfpmath=sse -DNDEBUG -O2 -s -c -MMD -o <output_file>.o <input_file>.cpp