Skip to content

[DebugInfo] Init DwarfVersion of MCOptions like the other. #146666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ static bool initTargetOptions(const CompilerInstance &CI,
break;
}

Options.MCOptions.DwarfVersion = CodeGenOpts.DwarfVersion;
Options.MCOptions.SplitDwarfFile = CodeGenOpts.SplitDwarfFile;
Options.MCOptions.EmitDwarfUnwind = CodeGenOpts.getEmitDwarfUnwind();
Options.MCOptions.EmitCompactUnwindNonCanonical =
Expand Down
18 changes: 18 additions & 0 deletions clang/test/CodeGen/dwarf-version.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@
// RUN: not %clang -target powerpc64-ibm-aix-xcoff -gdwarf-5 -S -emit-llvm -o - %s 2>&1| \
// RUN: FileCheck %s --check-prefix=UNSUPPORTED-VER5

// Check what version of dwarf is used to emit debug info when compiling ir with clang.
// RUN: %clang -target x86_64-linux-gnu -g -S -emit-llvm -o - %s \
// RUN: | %clang -target x86_64-linux-gnu -gdwarf-4 -x ir -c -o - - \
// RUN: | llvm-dwarfdump -v - \
// RUN: | FileCheck %s --check-prefix=SINGLE-4
// RUN: %clang -target x86_64-linux-gnu -gdwarf-4 -S -emit-llvm -o - %s \
// RUN: | %clang -target x86_64-linux-gnu -g -x ir -c -o - - \
// RUN: | llvm-dwarfdump -v - \
// RUN: | FileCheck %s --check-prefix=SINGLE-5
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually the behavior we want? Normally, I'd expect bitcode files to already have a version specified; when clang generates LLVM IR, it generates the DWARF version as metadata. This patch explicitly overrides that version in favor of whatever is specified on the command-line when the bitcode is lowered to asm.

Overriding the version won't really work right, anyway: clang generates different debug info depending on the version it's targeting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually the behavior we want? Normally, I'd expect bitcode files to already have a version specified; when clang generates LLVM IR, it generates the DWARF version as metadata. This patch explicitly overrides that version in favor of whatever is specified on the command-line when the bitcode is lowered to asm.

DwarfDebug::DwarfDebug(AsmPrinter *A)
...
  unsigned DwarfVersionNumber = Asm->TM.Options.MCOptions.DwarfVersion;
  unsigned DwarfVersion = DwarfVersionNumber ? DwarfVersionNumber
                                    : MMI->getModule()->getDwarfVersion();
...
  Dwarf64 &=
      ((Asm->TM.Options.MCOptions.Dwarf64 || MMI->getModule()->isDwarf64()) &&
       TT.isOSBinFormatELF()) ||
      TT.isOSBinFormatXCOFF();

The effective value of DwarfVersion was designed to let MCOptions override the metadata, including Dwarf64. However, unlike other options, DwarfVersion was not initialized by CodeGenOpts.

Overriding the version won't really work right, anyway: clang generates different debug info depending on the version it's targeting.

Sorry, could you please explain in detail why it's not working properly?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Thanks for the test case)
(I'd like to hear more of @efriedma-quic's perspective/understanding to be sure)

Though I think for LTO - I believe the metadata already gets min/maxed (whichever way we do that - ah, it's "Max" - so if you have IR bitcode that's DWARFv4 and some that's DWARFv5 and you IR link it, you get DWARFv5 across the board) - so DWARF metadata emitted for 4 can end up emitted as 5 at least... going backwards might be less well/not at all tested, though...

Not sure what the right direction is here, then.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overriding the version won't really work right, anyway: clang generates different debug info depending on the version it's targeting.

Sorry, could you please explain in detail why it's not working properly?

clang CodeGen has checks for the target DWARF version. If clang generates v5 DWARF and you override it to v4, you won't get the same result you would if you originally generated v5. Which could mean debuggers get confused by the resulting DWARF.


int main (void) {
return 0;
}
Expand All @@ -67,3 +77,11 @@ int main (void) {
// CODEVIEW: !{i32 2, !"CodeView", i32 1}
// NOCODEVIEW-NOT: !"CodeView"
// NODWARF-NOT: !"Dwarf Version"

// SINGLE-4: .debug_info contents:
// SINGLE-4: 0x00000000: Compile Unit: {{.*}} version = 0x0004, abbr_offset
// SINGLE-4: 0x0000000b: DW_TAG_compile_unit

// SINGLE-5: .debug_info contents:
// SINGLE-5: 0x00000000: Compile Unit: {{.*}} version = 0x0005, unit_type = DW_UT_compile, abbr_offset
// SINGLE-5: 0x0000000c: DW_TAG_compile_unit