0

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
DarkAtom
  • 2,589
  • 1
  • 11
  • 27
  • The relevant option for your last question is `-mpreferred-stack-boundary=2` (this assumes and maintains a stack-alignment of `2^2 = 4`, while the default is `2^4 = 16`). Related question for more details: https://stackoverflow.com/questions/1061818/stack-allocation-padding-and-alignment. As long as you barely store something on the stack which needs to be aligned you might be fine with that option. – chtz Jun 15 '23 at 15:58
  • @chtz *As long as you barely store something on the stack which needs to be aligned you might be fine with that option.* The problem is that I don't know what the internal ImGui code does. I am also not very eager to look into the internal workings of the GUI library just to solve this single issue. – DarkAtom Jun 15 '23 at 16:10
  • Disclaimer: I've never used ImGui before, so I may be totally wrong here. But grepping through the source code, the `CALLBACK` decorator used for WinApi callbacks only appears once, in `backends/imgui_impl_glfw.cpp`, for the static function `ImGui_ImplGlfw_WndProc`. And grepping for `callback` case-insensitively in `backends/` indicates that this appears to be the only callback that is defined within ImGui that will be called from external code. (The rest is just ImGui calling user code callbacks.) So if you patch that single function you *should* probably be fine...? – chris_se Jun 15 '23 at 19:25

1 Answers1

0

The callbacks are mostly types ImGuiInputTextCallback ImGuiSizeCallback, ImDrawCallback, none of which would be called unless you use features associated to them (the two first ones are exercised in imgui_demo if you want to copy some of it into your own code for testing).

Omar
  • 627
  • 4
  • 6
  • Aren't those callbacks meant for the library's user? There is no problem when ImGui calls any of my functions. The problem comes if/when outside code (i.e code which wasn't compiled by me) calls ImGui. – DarkAtom Jun 15 '23 at 17:01
  • Only backends use callbacks of that type, so in your case it would be only the WndProc function. – Omar Jun 17 '23 at 10:27