This implements support of lexical block scopes for function-local types and static locals. Before this patch and its clang counterparts (see D125694 and D125695), LLVM ignores the fact that such entities are being defined within a lexical block and puts them directly into a parent function.
Consider the following example:
1 int main() { 2 int a = 0; 3 switch(a) { 4 case 1: { 5 static const int a = 1; 6 } 7 case 2: { 8 static const int a = 2; 9 } 10 } 11 }
All the 'a''s DW_TAG_variable go to subprogram's scope which confuses debuggers when 'a' variable is being inspected.
0x0000002a: DW_TAG_subprogram ... 0x00000043: DW_TAG_variable DW_AT_name ("a") DW_AT_type (0x0000007c "const int") DW_AT_decl_file ("temp.cpp") DW_AT_decl_line (5) DW_AT_location (DW_OP_addr 0x402004) 0x00000058: DW_TAG_variable DW_AT_name ("a") DW_AT_type (0x0000007c "const int") DW_AT_decl_file ("temp.cpp") DW_AT_decl_line (8) DW_AT_location (DW_OP_addr 0x402008) 0x0000006d: DW_TAG_variable DW_AT_location (DW_OP_fbreg -8) DW_AT_name ("a") DW_AT_decl_file ("temp.cpp") DW_AT_decl_line (2) DW_AT_type (0x00000081 "int")
This fixes
- https://github.com/llvm/llvm-project/issues/19612,
- https://github.com/llvm/llvm-project/issues/29985.
In the same time, it also unifies implementation of local imported entities allowing all the function-local declarations to be handled the same way,
thus fixes
This is an alternative implementation for D113741 which relies on localDecls field of DISubprogram or DILexicalBlock for getting local declarations (types, imports or static variable) for a particular local scope.
It has 2 issues from D113741 resolved:
- Previous patch may misscope type declarations belong to a lexical block. This happened because it wasn't possible to know if there are any types belonging to a particular local scope by the time we started to emit this scope. So, we may skip emission of lexical blocks even if they contain some type declarations, and later couldn't find a proper parent for those types. localDecls tracks all the types declared within a particular local scope, so we no longer skip non-empty lexical blocks.
- Previous patch didn't have a mapping between a local declaration and its containing subprogram DIE (and CU DIE), which may cause issues like https://reviews.llvm.org/D113741#3206883. Now we are able to create such mapping basing on localDecls of DISubprogram or DILexicalBlock.
Another thing that should be noted that with this patch we will no longer emit local declarations if its parent scope doesn't have a LexicalScope calculated for it (i.e. there are no DILocations that point to this scope or any of its children scopes) unless something refers such a declaration (relevant for types), see changes in llvm/test/Instrumentation/InstrProfiling/debug-info-correlate.ll as an example. I guess this shouldn't affect debugging experience.
The patch contains a lot of changes in the tests, but most of them trivial and just reflect changes in metadata, order of metadata and DWARF entities:
Order changes only:
- clang/test/CodeGen/debug-info-codeview-unnamed.c
- clang/test/CodeGen/debug-info-unused-types.c
- clang/test/CodeGen/debug-info-unused-types.cpp
- clang/test/CodeGenCXX/debug-info-anon-union-vars.cpp
- clang/test/CodeGenCXX/debug-info-codeview-unnamed.cpp
- clang/test/CodeGenCXX/debug-info-gline-tables-only-codeview.cpp
- clang/test/CodeGenCXX/debug-lambda-this.cpp
- clang/test/CodeGenCXX/debug-info-cxx1y.cpp
- llvm/test/DebugInfo/Generic/namespace.ll
- llvm/test/DebugInfo/X86/gnu-public-names.ll
Empty retainedNodes no longer emitted:
- clang/test/CodeGen/attr-btf_tag-disubprogram-callsite.c
- clang/test/CodeGenCXX/aix-static-init-debug-info.cpp
- clang/test/CodeGenCXX/debug-info-template.cpp
- clang/test/CodeGenObjC/debug-info-category.m
Local entities moved from CU to SP:
- clang/test/CodeGenCXX/debug-info-namespace.cpp
- llvm/test/CodeGen/AArch64/arm64-2011-03-17-AsmPrinterCrash.ll
- llvm/test/CodeGen/BPF/BTF/static-var-inited-sec.ll
- llvm/test/CodeGen/BPF/BTF/static-var-inited.ll
- llvm/test/CodeGen/BPF/BTF/static-var-readonly-sec.ll
- llvm/test/CodeGen/BPF/BTF/static-var-readonly.ll
- llvm/test/CodeGen/BPF/BTF/static-var-sec.ll
- llvm/test/CodeGen/BPF/BTF/static-var.ll
- llvm/test/CodeGen/BPF/dwarfdump.ll
- llvm/test/CodeGen/PowerPC/pr24546.ll
- llvm/test/CodeGen/X86/dbg-distringtype-uint.ll
- llvm/test/DebugInfo/Generic/2009-11-05-DeadGlobalVariable.ll
- llvm/test/DebugInfo/X86/DW_AT_specification.ll
- llvm/test/DebugInfo/X86/dimodule-external-fortran.ll
- llvm/test/DebugInfo/X86/distringtype.ll
- llvm/test/DebugInfo/X86/dwarfdump-DIImportedEntity_elements.ll
- llvm/test/DebugInfo/X86/fission-inline.ll
- llvm/test/DebugInfo/X86/fission-local-import.ll
- llvm/test/DebugInfo/X86/fission-no-inline-gsym.ll
- llvm/test/DebugInfo/X86/lexical-block-file-inline.ll
- llvm/test/DebugInfo/X86/namelist1.ll
- llvm/test/DebugInfo/X86/namelist2.ll
- llvm/test/DebugInfo/omit-empty.ll
Non-trivial changes:
- llvm/test/DebugInfo/Generic/imported-name-inlined.ll
- llvm/test/DebugInfo/Generic/verifier-invalid-disubprogram.ll
- llvm/test/Instrumentation/InstrProfiling/debug-info-correlate.ll
- llvm/test/ThinLTO/X86/debuginfo-cu-import.ll
- llvm/unittests/Transforms/Utils/CloningTest.cpp
So here we discard LLVM IR metadata that have local declarations tied to a CU, right? This will break compatibility with old LLVM IR. Can we do some upgrade to convert this "old style" metadata the way we expect it now? Does it make sense to support both variants?