Skip to content

Commit

Permalink
[Driver][MSVC] Support DWARF fission when using LTO on Windows
Browse files Browse the repository at this point in the history
D154070 has added /dwodir to lld/COFF to tells LTO backend to create dwo
directory and files. This patch makes clang to emit /dwodir to lld when
user specify -gsplit-dwarf with LTO. This behavior is simiar to DWARF
fission with LTO for ELF.

A simple use case:
$clang-cl -c -flto -gdwarf main.c -o main.o
$clang-cl -c -flto -gdwarf a.c -o a.o
$clang-cl -flto -fuse-ld=lld -gdwarf -gsplit-dwarf main.o a.o

This'll generate a dwo file: main.exe_dwo/0.dwo

Reviewed By: mstorsjo, MaskRay, hans

Differential Revision: https://reviews.llvm.org/D154295
  • Loading branch information
HaohaiWen committed Jul 6, 2023
1 parent 34de494 commit f97b61e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 8 additions & 1 deletion clang/lib/Driver/ToolChains/MSVC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,18 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
else if (Linker.equals_insensitive("lld"))
Linker = "lld-link";

if (Linker == "lld-link")
if (Linker == "lld-link") {
for (Arg *A : Args.filtered(options::OPT_vfsoverlay))
CmdArgs.push_back(
Args.MakeArgString(std::string("/vfsoverlay:") + A->getValue()));

if (C.getDriver().isUsingLTO() &&
Args.hasFlag(options::OPT_gsplit_dwarf, options::OPT_gno_split_dwarf,
false))
CmdArgs.push_back(Args.MakeArgString(Twine("/dwodir:") +
Output.getFilename() + "_dwo"));
}

// Add filenames, libraries, and other linker inputs.
for (const auto &Input : Inputs) {
if (Input.isFilename()) {
Expand Down
7 changes: 5 additions & 2 deletions clang/test/Driver/lto-dwo.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Confirm that -gsplit-dwarf=DIR is passed to linker

// RUN: %clang --target=x86_64-unknown-linux -### %s -flto=thin -gsplit-dwarf -o a.out 2> %t
// RUN: FileCheck -check-prefix=CHECK-LINK-DWO-DIR-DEFAULT < %t %s
// RUN: FileCheck -check-prefix=CHECK-LINK-ELF-DWO-DIR-DEFAULT < %t %s
// RUN: %clang_cl --target=x86_64-unknown-windows-msvc -### -fuse-ld=lld -flto -gsplit-dwarf -o a.out -- %s 2> %t
// RUN: FileCheck -check-prefix=CHECK-LINK-COFF-DWO-DIR-DEFAULT < %t %s
//
// CHECK-LINK-DWO-DIR-DEFAULT: "-plugin-opt=dwo_dir=a.out_dwo"
// CHECK-LINK-ELF-DWO-DIR-DEFAULT: "-plugin-opt=dwo_dir=a.out_dwo"
// CHECK-LINK-COFF-DWO-DIR-DEFAULT: "/dwodir:a.out_dwo"

0 comments on commit f97b61e

Please sign in to comment.