Skip to content

Add debug options to clang-linker-wrapper #101008

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

Closed
wants to merge 3 commits into from
Closed
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
26 changes: 22 additions & 4 deletions clang/test/Driver/linker-wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ __attribute__((visibility("protected"), used)) int x;
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run --device-debug -O0 \
// RUN: --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=NVPTX-LINK-DEBUG

// NVPTX-LINK-DEBUG: clang{{.*}} -o {{.*}}.img --target=nvptx64-nvidia-cuda -march=sm_70 -O2 {{.*}}.o {{.*}}.o -g
// NVPTX-LINK-DEBUG: clang{{.*}} -o {{.*}}.img --target=nvptx64-nvidia-cuda -march=sm_70 -O2 {{.*}}.o {{.*}}.o -g

// RUN: clang-offload-packager -o %t.out \
// RUN: --image=file=%t.elf.o,kind=openmp,triple=amdgcn-amd-amdhsa,arch=gfx908 \
Expand All @@ -45,11 +45,29 @@ __attribute__((visibility("protected"), used)) int x;
// RUN: --image=file=%t.amdgpu.bc,kind=openmp,triple=amdgcn-amd-amdhsa,arch=gfx1030 \
// RUN: --image=file=%t.amdgpu.bc,kind=openmp,triple=amdgcn-amd-amdhsa,arch=gfx1030
// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.o -fembed-offload-object=%t.out
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run --save-temps -O2 \
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --wrapper-verbose --dry-run --save-temps -O2 \
// RUN: --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=AMDGPU-LTO-TEMPS

// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --wrapper-verbose --dry-run --save-temps -O2 \
// RUN: --lto-in-process --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=AMDGPU-LTO-IN-PROC
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --wrapper-verbose --dry-run --save-temps -O2 \
// RUN: --lto-debug-pass-manager \
// RUN: -offload-opt=-print-pipeline-passes \
// RUN: -offload-opt=-print-before=openmp-opt \
// RUN: -offload-opt=-print-after=openmp-opt \
// RUN: --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=AMDGPU-LTO-OPTS

// AMDGPU-LTO-TEMPS-NOT: Linking bitcode files
// AMDGPU-LTO-TEMPS: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx1030 -O2 -Wl,--no-undefined {{.*}}.o -save-temps

// AMDGPU-LTO-IN-PROC: Linking bitcode files
// AMDGPU-LTO-IN-PROC: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx1030 -O2 -Wl,--no-undefined {{.*}}.s -save-temps

// AMDGPU-LTO-OPTS: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx1030 -O2 {{.*}}.o -save-temps -Wl,--save-temps
// AMDGPU-LTO-OPTS-SAME: -Wl,--lto-debug-pass-manager
// AMDGPU-LTO-OPTS-SAME: -Wl,-mllvm=-print-pipeline-passes
// AMDGPU-LTO-OPTS-SAME: -Wl,-mllvm=-print-before=openmp-opt
// AMDGPU-LTO-OPTS-SAME: -Wl,-mllvm=-print-after=openmp-opt

// RUN: clang-offload-packager -o %t.out \
// RUN: --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu \
// RUN: --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu
Expand Down Expand Up @@ -93,7 +111,7 @@ __attribute__((visibility("protected"), used)) int x;

// CUDA: clang{{.*}} -o [[IMG_SM70:.+]] --target=nvptx64-nvidia-cuda -march=sm_70
// CUDA: clang{{.*}} -o [[IMG_SM52:.+]] --target=nvptx64-nvidia-cuda -march=sm_52
// CUDA: fatbinary{{.*}}-64 --create {{.*}}.fatbin --image=profile=sm_70,file=[[IMG_SM70]] --image=profile=sm_52,file=[[IMG_SM52]]
// CUDA: fatbinary{{.*}}-64 --create {{.*}}.fatbin --image=profile=sm_70,file=[[IMG_SM70]] --image=profile=sm_52,file=[[IMG_SM52]]
// CUDA: usr/bin/ld{{.*}} {{.*}}.openmp.image.{{.*}}.o {{.*}}.cuda.image.{{.*}}.o

// RUN: clang-offload-packager -o %t.out \
Expand Down
23 changes: 19 additions & 4 deletions clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,8 +581,16 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args) {
if (SaveTemps)
CmdArgs.push_back("-save-temps");

if (SaveTemps && linkerSupportsLTO(Args))
CmdArgs.push_back("-Wl,--save-temps");
if (linkerSupportsLTO(Args)) {
if (SaveTemps)
CmdArgs.push_back("-Wl,--save-temps");
if (Args.hasArg(OPT_lto_debug_pass_manager))
CmdArgs.push_back("-Wl,--lto-debug-pass-manager");
Comment on lines +587 to +588
Copy link
Contributor

Choose a reason for hiding this comment

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

The --save-temps thing is kind of special, I'm thining for stuff like this it's better suited for -Xoffload-linker --lto-debug-pass-manager once I merge #101032.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Verified that --device-linker=--lto-debug-pass-manager works.

I am going to close this PR since most of the functionality is redundant or now available by other means.

for (const opt::Arg *Arg : Args.filtered(OPT_offload_opt_eq_minus)) {
CmdArgs.push_back(
Args.MakeArgString("-Wl,--mllvm=" + StringRef(Arg->getValue())));
Comment on lines +589 to +591
Copy link
Contributor

Choose a reason for hiding this comment

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

This should already be handled elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll remove the commit that added this. I think I wrote it before 97c62b8f75 was merged.

}
}

if (Args.hasArg(OPT_embed_bitcode))
CmdArgs.push_back("-Wl,--lto-emit-llvm");
Expand Down Expand Up @@ -767,6 +775,8 @@ std::unique_ptr<lto::LTO> createLTO(
// TODO: Handle remark files
Conf.HasWholeProgramVisibility = Args.hasArg(OPT_whole_program);

Conf.DebugPassManager = Args.hasArg(OPT_lto_debug_pass_manager);

return std::make_unique<lto::LTO>(std::move(Conf), Backend);
}

Expand All @@ -781,6 +791,8 @@ bool isValidCIdentifier(StringRef S) {
Error linkBitcodeFiles(SmallVectorImpl<OffloadFile> &InputFiles,
SmallVectorImpl<StringRef> &OutputFiles,
const ArgList &Args) {
if (Verbose)
llvm::errs() << "Linking bitcode files\n";
Comment on lines +794 to +795
Copy link
Contributor

Choose a reason for hiding this comment

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

Verbose mode just wants to print information from the tools generally. I think if we want tracing we already have the time-trace scope.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Time-trace seems to be focused more on profiling than just tracing. It writes json to a file.
I guess, as a human user, I just wanted a simple way to see when clang-linker-wrapper was running LTO in-process.
Is there a third alternative that would work for you?

Copy link
Contributor

Choose a reason for hiding this comment

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

That sounds more like debugging than verbose output, we could add something for it but in general I don't think it adds too much, since this path isn't really used anymore.

llvm::TimeTraceScope TimeScope("Link bitcode files");
const llvm::Triple Triple(Args.getLastArgValue(OPT_triple_EQ));
StringRef Arch = Args.getLastArgValue(OPT_arch_EQ);
Expand Down Expand Up @@ -1017,6 +1029,8 @@ Expected<StringRef> writeOffloadFile(const OffloadFile &File) {
// Compile the module to an object file using the appropriate target machine for
// the host triple.
Expected<StringRef> compileModule(Module &M, OffloadKind Kind) {
if (Verbose)
llvm::errs() << "Compiling module\n";
llvm::TimeTraceScope TimeScope("Compile module");
std::string Msg;
const Target *T = TargetRegistry::lookupTarget(M.getTargetTriple(), Msg);
Expand Down Expand Up @@ -1197,7 +1211,8 @@ bundleLinkedOutput(ArrayRef<OffloadingImage> Images, const ArgList &Args,
}
}

/// Returns a new ArgList containg arguments used for the device linking phase.
/// Returns a new ArgList containing arguments used for the device linking
/// phase.
DerivedArgList getLinkerArgs(ArrayRef<OffloadFile> Input,
const InputArgList &Args) {
DerivedArgList DAL = DerivedArgList(DerivedArgList(Args));
Expand Down Expand Up @@ -1309,7 +1324,7 @@ Expected<SmallVector<StringRef>> linkAndWrapDeviceFiles(
// First link and remove all the input files containing bitcode if
// the target linker does not support it natively.
SmallVector<StringRef> InputFiles;
if (!linkerSupportsLTO(LinkerArgs))
if (!linkerSupportsLTO(LinkerArgs) || Args.hasArg(OPT_lto_in_process))
if (Error Err = linkBitcodeFiles(Input, InputFiles, LinkerArgs))
return Err;

Expand Down
6 changes: 6 additions & 0 deletions clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ def offload_opt_eq_minus : Joined<["--", "-"], "offload-opt=">, Flags<[HelpHidde
HelpText<"Options passed to LLVM, not including the Clang invocation. Use "
"'--offload-opt=--help' for a list of options.">;

// Arguments for LTO
def lto_in_process : Flag<["--"], "lto-in-process">,
Flags<[WrapperOnlyOption]>, HelpText<"Use in-process LTO even if linker supports LTO">;
def lto_debug_pass_manager : Flag<["--"], "lto-debug-pass-manager">,
Flags<[WrapperOnlyOption]>, HelpText<"Prints debug information for the new pass manager during LTO">;

// Standard linker flags also used by the linker wrapper.
def sysroot_EQ : Joined<["--"], "sysroot=">, HelpText<"Set the system root">;

Expand Down
Loading