On Fri Aug 25 17:18:18 2023 +0000, Zebediah Figura wrote:
I meant if you build a library on MinGW with the intent to have it
loadable by any code—on Windows, natively. You don't control the caller's alignment or whoever uses your library. Wouldn't this be a much more widespread issue in such case? It would require the library to (a) distribute binaries prebuilt with MinGW (which I get the impression is rare compared to building them with MSVC, even in the Free Software world), (b) manually use SSE or aligned types, (c) *not* build their libraries with -msse2 or anything. And for all we know, maybe it *is* a widespread issue, and all of those libraries just end up using -mstackrealign or something.
-mpreferred-stack-boundary is how you want to keep the stack in our
own functions, i.e. how they "prefer" it. I think it should also be set to 2 to prevent pointless realignments unless necessary. (note that realignment should happen if required by the function, already) See, that's what it sounds like just from the name, but then why is there a warning about ABI in the documentation? Why would setting it, as Richard Biener suggests, make a difference? And for that matter, a truly static function that isn't exposed as a function pointer doesn't need to *have* an ABI; the compiler can and will pass the parameters however it feels like.
Well it can make a difference if you prefer "less" alignment than the ABI, so when you call a function expecting 16 byte alignment, and your preferred one is 4 bytes, you won't align it to 16 before calling, and then probably crash.
Of course this shouldn't apply to Win32 PE DLLs. GCC seems a bit stubborn about that, just because Linux enforced 16-byte alignment at some point on their ABI, it's not the same on Windows.
I don't even understand why the GCC devs have so many workarounds for this. Why does -msse2 or SSE even matter at all? The simple fact is that incoming alignment is only guaranteed to be 4 bytes on some platforms, such as i686-MinGW (32-bit Windows PE), and GCC should realign the stack *if* it needs more alignment than that, SSE or not. **Any** variable requiring more alignment should enforce that.
Why complicate this with SSE checks? It's not like SSE is special here, it's all about variable alignment. They should simply just set the default incoming and preferred to 2 (aka 4 bytes) on MinGW i686, but I think we should also set it in Wine to deal with old compilers if they even patch this up upstream in the first place.
BTW here's an example of incoming=2 and preferred=4: it will assume incoming stack (if function is not static, that is, since it doesn't know the caller) is 4 byte aligned, and so it will realign it since it "prefers" 16 bytes, whether it actually needs 16 byte alignment or not.
incoming=2 and preferred=2 won't realign unless a variable explicitly requires more than 4 bytes alignment, so it's the most ideal, and how MSVC behaves when building 32-bit.