December 23, 2022
https://issues.dlang.org/show_bug.cgi?id=16098

Dennis <dkorpel@live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dkorpel@live.nl

--- Comment #7 from Dennis <dkorpel@live.nl> ---
(In reply to Walter Bright from comment #6)
> One way to fix this is to:

I think it's easier to just align the stack, which is already done for 64-bit platforms and OSX, but not for Win32. I tried to enable it for win32 too:

https://github.com/dlang/dmd/pull/14401

But the test suite failed so it requires some debugging.

--
December 23, 2022
https://issues.dlang.org/show_bug.cgi?id=16098

--- Comment #8 from Walter Bright <bugzilla@digitalmars.com> ---
Increasing the alignment has some problems:

1. won't fit with the C ABI. I.e. if other functions are not following those alignments, and you call them from D and pass a callback, the callback won't be aligned. Or if the C functions call D functions, no alignment.

2. alignment requirements for some SIMD instructions keeps getting bigger and bigger. Increasing the alignment for functions is not very future proof.

3. larger alignments are rare. But increasing the alignment means it is done for every function making for a lot more stack space being used.

But, it turns out that the code in DMD to do closures is almost exactly what we need for implementing an "aligned closure" allocated on the stack.

The code generator doesn't need to be touched.

--
December 26, 2022
https://issues.dlang.org/show_bug.cgi?id=16098

--- Comment #9 from Dlang Bot <dlang-bot@dlang.rocks> ---
@WalterBright created dlang/dmd pull request #14740 "partial Issue 16098 - align(N) not respected for stack variables if N…" mentioning this issue:

- partial Issue 16098 - align(N) not respected for stack variables if N >
platform stack alignment

https://github.com/dlang/dmd/pull/14740

--
December 26, 2022
https://issues.dlang.org/show_bug.cgi?id=16098

--- Comment #10 from Walter Bright <bugzilla@digitalmars.com> ---
Partial fix:

https://github.com/dlang/dmd/pull/14740

--
December 26, 2022
https://issues.dlang.org/show_bug.cgi?id=16098

--- Comment #11 from Dennis <dkorpel@live.nl> ---
(In reply to Walter Bright from comment #8)
> Increasing the alignment has some problems:

Those problems describe the situation where you increase the assumed alignment for all functions to a single larger constant, but that's not what I'm saying.

The thing is, this issue has already been mostly fixed by Suleyman Sahmi, but the bot didn't close this issue when the PR was merged in 2019:

https://github.com/dlang/dmd/pull/9143

The algorithm is this:

Find the maximum alignment by iterating over the types of all local variables in the function. If it's larger than the minimum stack alignment guaranteed by the platform, generate code in the function prolog to align the stack to the required alignment.

For example:
```
struct S { align(128) int i; }

void f()
{
    S x;
}
```
Generates:
```
void onlineapp.f():
        push    RBP
        mov     RBP,RSP
        and     RSP,0FFFFFF80h // < alignment of stack pointer to multiple of
128
        sub     RSP,080h

```

The limitations are this:
- It's only enabled for 64-bit builds and OSX (`&& (I64 || config.exe ==
EX_OSX)`)
- It only looks at variable types, so an `align(128) int x;` is still assumed
to have alignment 4

--
December 30, 2022
https://issues.dlang.org/show_bug.cgi?id=16098

--- Comment #12 from Walter Bright <bugzilla@digitalmars.com> ---
I'm a little curious how SSoulaimane's solutions fixes things like two base pointers are required, one for the parameters and one for the rest. Then there's the problem with nested functions accessing the stack frame of the outer function - wouldn't two base pointers be needed? What about aligning the variables in a dynamic closure?

I can't say I understand his solution.

My PR fixes the dynamic closure problem. Once that is merged, I have a followup fix ready to go that solves the other problems. No changes to the code generator are required. It's also a lot simpler.

--
December 30, 2022
https://issues.dlang.org/show_bug.cgi?id=16098

--- Comment #13 from Vladimir Panteleev <dlang-bugzilla@thecybershadow.net> ---
@Walter Bright, I think you may have posted the above comment to the wrong issue.

--
December 31, 2022
https://issues.dlang.org/show_bug.cgi?id=16098

--- Comment #14 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/dmd pull request #14740 "partial Issue 16098 - align(N) not respected for stack variables if N…" was merged into master:

- f846e92fab15ad0bb9bab0e2d91835bfa64b52d8 by Walter Bright:
  partial Issue 16098 - align(N) not respected for stack variables if N >
platform stack alignment

https://github.com/dlang/dmd/pull/14740

--
December 31, 2022
https://issues.dlang.org/show_bug.cgi?id=16098

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #15 from Dlang Bot <dlang-bot@dlang.rocks> ---
@WalterBright created dlang/dmd pull request #14764 "Fix16098" fixing this issue:

- fix 16098

- fix Issue 16098 - align(N) not respected for stack variables if N > platform
stack alignment

https://github.com/dlang/dmd/pull/14764

--
January 02, 2023
https://issues.dlang.org/show_bug.cgi?id=16098

--- Comment #16 from Walter Bright <bugzilla@digitalmars.com> ---
More progress:

https://github.com/dlang/dmd/pull/14770

--