[DebugInfo][DwarfDebug] Defer parent attachment of function-local DIEs until endModule() (#230866)
As it was discussed previously in
https://github.com/llvm/llvm-project/pull/142166#issuecomment-2950057682,
when DIEs for function-local types are emitted in DWARF, they should be
placed in their corresponding abstract subprograms, or abstract lexical
blocks inside them (if abstract DIE for subprogram exists).
If a function-local entity DIE is emitted in
`DwarfDebug::endFunctionImpl()`, DwarfDebug may not know yet if an
abstract subprogram should be created for the scope of function-local
entity, thus,
function-local type may be wrongly placed (inside concrete subprogram
DIE instead of abstract SP DIE, see
https://github.com/llvm/llvm-project/issues/29985,
https://github.com/llvm/llvm-project/issues/55680 ).
Initially, to solve that for subprograms that represent fully inlined
functions, LexicalScopes class was modified to build a map of
subprograms attached to `llvm::Function`s, so that if it is known that a
subprogram is fully inlined (not attached to any Function), i.e. it
requires an abstract DIE, its abstract DIE can be created on demand
https://github.com/llvm/llvm-project/pull/160786. This solved the
problem for fully inlined subprograms, but the case of `llvm::Function`s
that are not optimized out was still uncovered
(https://github.com/llvm/llvm-project/issues/55680).
Then, emission of retained local types was postponed to
`DwarfDebug::endModule()` in
https://github.com/llvm/llvm-project/pull/165032. By the moment of
`DwarfDebug::endModule()` call, all necessary abstract DIEs should be
emitted. However, when DIEs for local variables of a function-local type
are emitted, local type DIE creation is still triggered inside
`DwarfDebug::endFunctionImpl()` call
The call chain is:
`DwarfDebug::endFunctionImpl()` ->
`TheCU.constructSubprogramScopeDIE()` ->
`DwarfCompileUnit::createAndAddScopeChildren()` ->
`DwarfCompileUnit::constructVariableDIE()` ->
`DwarfCompileUnit::applyCommonDbgVariableAttributes()` ->
`DwarfUnit::addType()`.
Thus, function-local types may still be emitted into concrete subprogram
DIEs instead of abstract ones.
To fix the issue, when a DIE for a function-local entity is created
prior to `DwarfDebug::endModule()` call, and an abstract DIE for its
scope does not exist (but it may be created later), the function-local
DIE is attached to a temporary parent DIE within the scope's CU. Later
in `DwarfDebug::endModule()`, by the point when all abstract scope DIEs
should be created, all such DIEs are reattached to their proper parents
(abstract or concrete).
Fixes https://github.com/llvm/llvm-project/issues/55680.
Authored-By: Kristina Bessonova <kbessonova@accesssoftek.com>