This is an archive of the discontinued LLVM Phabricator instance.

[COFF] Support -gsplit-dwarf for COFF on Windows
ClosedPublic

Authored by HaohaiWen on Jun 13 2023, 1:28 AM.

Details

Summary

D152340 has split WinCOFFObjectWriter to WinCOFFWriter. This patch adds
another WinCOFFWriter as DwoWriter to write Dwo sections to dwo file.
Driver options are also updated accordingly to support -gsplit-dwarf in
CL mode.

e.g. $ clang-cl -c -gdwarf -gsplit-dwarf foo.c

Like what -gsplit-dwarf did in ELF, using this option will create DWARF object
(.dwo) file. DWARF debug info is split between COFF object and DWARF object
file. It can reduce the executable file size especially for large project.

Diff Detail

Event Timeline

HaohaiWen created this revision.Jun 13 2023, 1:28 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 13 2023, 1:28 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
HaohaiWen requested review of this revision.Jun 13 2023, 1:28 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJun 13 2023, 1:28 AM

Fix sections binding check

skan added inline comments.Jun 15 2023, 2:56 AM
llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
1895 ↗(On Diff #531219)

The comment is confusing according to line 1900. Could you refine it?

llvm/test/DebugInfo/COFF/dwarf-headers.ll
76

Simplify it to "clang"?

llvm/test/DebugInfo/COFF/fission-cu.ll
20

Simplify the producer to "clang"?

119

Why do we check .rela* section? It's linux relocation section name

skan added inline comments.Jun 15 2023, 3:01 AM
llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
1896 ↗(On Diff #531219)

Should we merge the check into line 1894?

llvm/test/DebugInfo/COFF/fission-sections.ll
20

ditto

HaohaiWen updated this revision to Diff 532060.Jun 16 2023, 2:36 AM
HaohaiWen marked an inline comment as done.

Address skan's comments

llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
1896 ↗(On Diff #531219)

It's more elegant to not merge.

llvm/test/DebugInfo/COFF/dwarf-headers.ll
76

I have attached source code in comments. It's better to save llvm version here to help to reproduce it.

MaskRay added inline comments.Jun 16 2023, 2:48 PM
clang/test/Driver/split-debug.c
19

Use --target= for new tests. -target has been deprecated since Clang 3.x

llvm/lib/MC/WinCOFFObjectWriter.cpp
124

static

HaohaiWen updated this revision to Diff 532507.Jun 18 2023, 7:34 PM

Use --target

HaohaiWen marked 7 inline comments as done.Jun 18 2023, 7:35 PM
HaohaiWen added inline comments.
llvm/lib/MC/WinCOFFObjectWriter.cpp
124

It's already in anonymous namespace.

skan accepted this revision.Jun 18 2023, 10:35 PM

LGTM for the MC part.

This revision is now accepted and ready to land.Jun 18 2023, 10:35 PM
HaohaiWen marked an inline comment as done.Jun 19 2023, 6:08 PM

Any other comments? If not , I'll land this patch tomorrow.

Looking....

HaohaiWen edited reviewers, added: MaskRay; removed: maskray0.
MaskRay added inline comments.Jun 20 2023, 11:47 PM
clang/lib/Driver/Driver.cpp
3928

Add a test to show how OPT__SLASH_o passes a -dumpdir to CC1.

llvm/lib/MC/MCAsmBackend.cpp
74

Move before ELF for an alphabetical order.

75
llvm/lib/MC/WinCOFFObjectWriter.cpp
124

Perhaps that can be fixed separate, but the standard recommends static over an unnamed namespace for functions:
https://llvm.org/docs/CodingStandards.html#anonymous-namespaces

848–851

this cascading if/for with a single-line body doesn't need {}

1154

++J. See coding standards

MaskRay added inline comments.Jun 20 2023, 11:54 PM
clang/include/clang/Driver/Options.td
1167

This exposes -dumpdir to clang-cl which may not be useful.

clang/test/Driver/split-debug.c
19

You can use -gno-split-dwarf -gsplit-dwarf to test CoreOption on -gno-split-dwarf.

55
llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
1895 ↗(On Diff #531219)

I'll change this to ends_with(".dwo") separately.

check-llvm check-clang passes even if I remove if (isa<COFFObjectFile>(&Obj) && Section.relocations().empty()) continue. What does it do?

HaohaiWen updated this revision to Diff 533182.Jun 21 2023, 1:51 AM
HaohaiWen marked 10 inline comments as done.

Address maskray's comments

HaohaiWen added inline comments.Jun 21 2023, 1:52 AM
clang/include/clang/Driver/Options.td
1167

clang -gdwarf -gsplit-dwarf foo.cpp -o foo.exe -### will be expanded to
clang -cc1 ... "-dumpdir" "foo.exe-" .... "-split-dwarf-file" "foo.exe-foo.dwo"

dwo name will be prefixed by dumpdir if it's specified.

This dumpdir is required for clang-cl to have same dwo filename behavior as clang.

clang/lib/Driver/Driver.cpp
3928

Already tested in (-o is OPT__SLASH_o in cl mode)

clang/test/Driver/split-debug.c: 62
// RUN: %clang_cl -### --target=x86_64-unknown-windows-msvc -gsplit-dwarf -g %s -o obj/out 2>&1 | FileCheck %s --check-prefix=SPLIT_LINK
/ SPLIT_LINK:      "-dumpdir" "obj/out-"
// SPLIT_LINK:      "-debug-info-kind=constructor"
// SPLIT_LINK-SAME: "-split-dwarf-file" "obj/out-split-debug.dwo" "-split-dwarf-output" "obj/out-split-debug.dwo"
clang/test/Driver/split-debug.c
19

Good suggestion.

llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
1895 ↗(On Diff #531219)

For ELF, if section a.dwo has relocations, it'll have a a.dwo.rela section, this section is RelocatedSection.
All dwo section should not have relocations, so if this is a dwo file, then RelocatedSection should be Obj.section_end(). Otherwise it will report warning.

For COFF, relocations of section A are directly stored in section A. RelocatedSection is Section itself so it will never be Obj.section_end(). Then this warning will always be reported. What we need to do is to check Section.relocations().empty() for COFF sections.

MaskRay requested changes to this revision.Jun 21 2023, 10:59 AM
MaskRay added inline comments.
clang/include/clang/Driver/Options.td
1167

CoreOption allows -dumpdir to be used with clang-cl. If you do this, I expect a driver test case.

clang-cl passes -dumpdir to clang -cc1. -dumpdir has the CC1Option flag, so -cc1 accepts the option.

This revision now requires changes to proceed.Jun 21 2023, 10:59 AM

CodeView is the default debug info format for COFF. Perhaps -gsplit-dwarf should be rejected when the default -gcodeview is used, with a test.

CodeView is the default debug info format for COFF. Perhaps -gsplit-dwarf should be rejected when the default -gcodeview is used, with a test.

IIRC some users do generate both CodeView and DWARF debug info at the same time, for some use cases.

HaohaiWen updated this revision to Diff 533623.Jun 22 2023, 8:22 AM

Remove CoreOption for dumpdir

HaohaiWen marked an inline comment as done.Jun 22 2023, 8:23 AM
MaskRay added a reviewer: dblaikie.
MaskRay added a subscriber: debug-info.
MaskRay added inline comments.
llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
1896 ↗(On Diff #531219)

D153602 should make this change unneeded.

Tests seem mostly fine, but since this is the primary patch for a major feature, consider adding more descriptions how this works?

For example, you can add something like the following to the summary/commit message, which should make a curious reader know how to play with this new feature.

% cat a.c
int main() {}
% clang --target=x86_64-windows-gnu -g -gsplit-dwarf -c a.c
% llvm-objdump -h a.dwo
...

CodeView is the primary debug info format. I expect some description about it as well, even if we decide to let -gsplit-dwarf be silently ignored in CodeView and no-DWARF mode.

HaohaiWen updated this revision to Diff 533967.Jun 23 2023, 7:51 AM

Restore DWARFContext.cpp since D153602 has fixed that.

HaohaiWen edited the summary of this revision. (Show Details)Jun 23 2023, 8:13 AM
HaohaiWen edited the summary of this revision. (Show Details)Jun 23 2023, 8:19 AM

@MaskRay, any other comments? I'd like to land this patch.

MaskRay accepted this revision.Jun 24 2023, 7:30 PM
This revision is now accepted and ready to land.Jun 24 2023, 7:30 PM
thakis added a subscriber: thakis.Jun 25 2023, 7:35 AM

Looks like this breaks tests on Mac: http://45.33.8.238/macm1/63504/step_7.txt

Please take a look and revert for now if it takes a while to fix.

HaohaiWen reopened this revision.Jun 25 2023, 8:14 PM

The path of this test you were running (/Users/thakis/src/llvm-project/clang/test/Driver/split-debug.c) started with /User which was interpreted as OPT_U option. Therefore split-debug.c haven't been treated as input file.
I'll add -- to tests to force clang-cl treat it as input file.

This revision is now accepted and ready to land.Jun 25 2023, 8:14 PM
HaohaiWen updated this revision to Diff 534413.Jun 25 2023, 8:20 PM
HaohaiWen edited the summary of this revision. (Show Details)

Rebase

HaohaiWen updated this revision to Diff 534417.Jun 25 2023, 8:41 PM

Add -- to clang_cl input file

HaohaiWen closed this revision.Jun 26 2023, 6:00 AM