https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123082
--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #1)
> Work around use GNU C++ VLA extension:
> ```
> void mgsort(int l, int r) {
> if (l == r) {
> return;
> }
> int mid = (l + r) >> 1;
> mgsort(l, mid);
> mgsort(mid + 1, r);
>
> int t = 0; // not const or constexpr to force VLA
> int b[N+t];
> std::merge(a + l, a + mid + 1, a + mid + 1, a + r + 1, b + l);
> std::memcpy(a + l, b + l, sizeof(int) * (r - l + 1));
> }
> ```
Or with moving the tail into a separate function and making that
[[gnu::noinline]].
I'm afraid there isn't much that can be done about it otherwise, except for
VLAs GCC never allocates further stack on automatic variable declarations,
there is possible stack sharing where multiple variables with non-overlapping
ranges can reuse the same stack slot(s), but other than that the whole frame is
always allocated in the prologue and released in the epilogue. There is shrink
wrapping and separate shrink wrapping, but I think no shrink wrapping will move
the prologue across non-inlined function calls.
So guess this is more as a request to outline the tail of the function into a
separate function because it will be beneficial for some reason (in this case
because of recursion and large stack use).