ldc icon indicating copy to clipboard operation
ldc copied to clipboard

Use templated lowering for `_d_newarray*`

Open Albert24GG opened this issue 5 months ago • 4 comments

I am not sure why, but the dynamic array creation still relies on the old hooks, despite having templated versions of _d_newarray* that the expressions are lowered to in expressionsem. The old hooks are soon going to be removed, because they were only used by AA hooks but now that is no longer the case.

I did not remove DtoNewDynArray since it is still used for ArrayLiteralExp,at least until the templated version of _d_arrayliteralTX will be integrated.

Albert24GG avatar Aug 08 '25 14:08 Albert24GG

Thx! I don't really recall whether the GC2Stack regressions were the reason, but they are definitely a problem. - We have an extra LLVM pass that looks for GC allocations that don't escape their scope (after inlining & optimizing), and convert them to stack allocations instead. This was all based on the old non-templated druntime hooks, based on TypeInfos etc.

So for instance, this now fails: https://github.com/ldc-developers/ldc/blob/9db6d953149f4698bf017e0ac42f188f58e303a4/tests/codegen/gc2stack.d#L10-L18

as the GC2Stack pass doesn't eliminate the superfluos GC allocation anymore when switching to the templated lowering.

kinke avatar Aug 08 '25 17:08 kinke

@kinke I took a look over the GC2Stack pass and I see it is indeed one of the last passes. The current implementation that uses the TypeInfo version is based on the fact that the call itself will not be inlined since the function is extern and needs to be linked. Now when switching to the templated version, the pass does not find any call to _d_newarrayT since it was probably inlined. Do you have any idea/suggestion on how to deal with it?

Albert24GG avatar Aug 12 '25 11:08 Albert24GG

Once everything GC-related is templated (or at least the stuff that makes sense), the GC2Stack would need to be overhauled, incl. accounting for the new unstable mangled names and populating the TypeInfo LLVM meta data (that we emit in IR for the later GC2Stack pass) differently etc. That's probably a bigger task, and I think only makes sense tackling once all hooks have been revised.

In the meantime, here for array GC allocations, I went the conservative route of using the old non-templated hook to keep GC2Stack working, as the druntime helpers are still available.

kinke avatar Aug 12 '25 14:08 kinke

@kinke Okay, then I'll leave this PR open until everything is ready so that the idea does not get lost.

Albert24GG avatar Aug 13 '25 18:08 Albert24GG