Skip to content

Commit

Permalink
[Clang] Simplify specifying passes via -Xoffload-linker (llvm#102483)
Browse files Browse the repository at this point in the history
Make it possible to do things like the following, regardless of whether
the offload target is nvptx or amdgpu:

```
$ clang -O1 -g -fopenmp --offload-arch=native test.c                       \
    -Xoffload-linker -mllvm=-pass-remarks=inline                           \
    -Xoffload-linker -mllvm=-force-remove-attribute=g.internalized:noinline\
    -Xoffload-linker --lto-newpm-passes='forceattrs,default<O1>'           \
    -Xoffload-linker --lto-debug-pass-manager                              \
    -foffload-lto
```

To accomplish that:

- In clang-linker-wrapper, do not forward options via `-Wl` if they
might have literal commas. Use `-Xlinker` instead.
- In clang-nvlink-wrapper, accept `--lto-debug-pass-manager` and
`--lto-newpm-passes`.
- In clang-nvlink-wrapper, drop `-passes` because it's inconsistent with
the interface of `lld`, which is used instead of clang-nvlink-wrapper
when the target is amdgpu. Without this patch, `-passes` is passed to
`nvlink`, producing an error anyway.

---------

Co-authored-by: Joseph Huber <huberjn@outlook.com>
  • Loading branch information
2 people authored and bwendling committed Aug 15, 2024
1 parent 90ba008 commit e742674
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 23 deletions.
18 changes: 12 additions & 6 deletions clang/test/Driver/linker-wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,15 @@ __attribute__((visibility("protected"), used)) int x;
// RUN: clang-offload-packager -o %t.out \
// RUN: --image=file=%t.elf.o,kind=openmp,triple=amdgcn-amd-amdhsa,arch=gfx908
// 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 --offload-opt=-pass-remarks=foo \
// RUN: --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=OFFLOAD-OPT
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run -mllvm -pass-remarks=foo \
// RUN: --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=OFFLOAD-OPT

// OFFLOAD-OPT: clang{{.*}}-Wl,--plugin-opt=-pass-remarks=foo
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
// RUN: --offload-opt=-pass-remarks=foo,bar --linker-path=/usr/bin/ld \
// RUN: %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=OFFLOAD-OPT
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
// RUN: -mllvm -pass-remarks=foo,bar --linker-path=/usr/bin/ld \
// RUN: %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=MLLVM

// MLLVM: clang{{.*}}-Xlinker --plugin-opt=-pass-remarks=foo,bar
// OFFLOAD-OPT: clang{{.*}}-Xlinker --plugin-opt=-pass-remarks=foo,bar
// MLLVM-SAME: -Xlinker -mllvm=-pass-remarks=foo,bar
// OFFLOAD-OPT-NOT: -Xlinker -mllvm=-pass-remarks=foo,bar
// OFFLOAD-OPT-SAME: {{$}}
8 changes: 8 additions & 0 deletions clang/test/Driver/nvlink-wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,11 @@ int baz() { return y + x; }
// RUN: clang-nvlink-wrapper --dry-run %t.o %t-u.o %t-y.a \
// RUN: -arch sm_52 --cuda-path/opt/cuda -o a.out 2>&1 | FileCheck %s --check-prefix=PATH
// PATH-NOT: --cuda-path=/opt/cuda

//
// Check that passes can be specified and debugged.
//
// RUN: clang-nvlink-wrapper --dry-run %t.o %t-u.o %t-y.a \
// RUN: --lto-debug-pass-manager --lto-newpm-passes=forceattrs \
// RUN: -arch sm_52 -o a.out 2>&1 | FileCheck %s --check-prefix=PASSES
// PASSES: Running pass: ForceFunctionAttrsPass
9 changes: 5 additions & 4 deletions clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,9 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args) {
// Forward all of the `--offload-opt` and similar options to the device.
if (linkerSupportsLTO(Args)) {
for (auto &Arg : Args.filtered(OPT_offload_opt_eq_minus, OPT_mllvm))
CmdArgs.push_back(
Args.MakeArgString("-Wl,--plugin-opt=" + StringRef(Arg->getValue())));
CmdArgs.append(
{"-Xlinker",
Args.MakeArgString("--plugin-opt=" + StringRef(Arg->getValue()))});
}

if (!Triple.isNVPTX())
Expand Down Expand Up @@ -572,8 +573,8 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args) {

// Pass on -mllvm options to the linker invocation.
for (const opt::Arg *Arg : Args.filtered(OPT_mllvm))
CmdArgs.push_back(
Args.MakeArgString("-Wl,-mllvm=" + StringRef(Arg->getValue())));
CmdArgs.append({"-Xlinker", Args.MakeArgString(
"-mllvm=" + StringRef(Arg->getValue()))});

if (Args.hasArg(OPT_debug))
CmdArgs.push_back("-g");
Expand Down
15 changes: 2 additions & 13 deletions clang/tools/clang-nvlink-wrapper/ClangNVLinkWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,6 @@ static cl::list<std::string>
PassPlugins("load-pass-plugin",
cl::desc("Load passes from plugin library"));

static cl::opt<std::string> PassPipeline(
"passes",
cl::desc(
"A textual description of the pass pipeline. To have analysis passes "
"available before a certain pass, add 'require<foo-analysis>'. "
"'-passes' overrides the pass pipeline (but not all effects) from "
"specifying '--opt-level=O?' (O2 is the default) to "
"clang-linker-wrapper. Be sure to include the corresponding "
"'default<O?>' in '-passes'."));
static cl::alias PassPipeline2("p", cl::aliasopt(PassPipeline),
cl::desc("Alias for -passes"));

static void printVersion(raw_ostream &OS) {
OS << clang::getClangToolFullVersion("clang-nvlink-wrapper") << '\n';
}
Expand Down Expand Up @@ -365,8 +353,9 @@ Expected<std::unique_ptr<lto::LTO>> createLTO(const ArgList &Args) {
Conf.OptLevel = Args.getLastArgValue(OPT_O, "2")[0] - '0';
Conf.DefaultTriple = Triple.getTriple();

Conf.OptPipeline = PassPipeline;
Conf.OptPipeline = Args.getLastArgValue(OPT_lto_newpm_passes, "");
Conf.PassPlugins = PassPlugins;
Conf.DebugPassManager = Args.hasArg(OPT_lto_debug_pass_manager);

Conf.DiagHandler = diagnosticHandler;
Conf.CGFileType = CodeGenFileType::AssemblyFile;
Expand Down
8 changes: 8 additions & 0 deletions clang/tools/clang-nvlink-wrapper/NVLinkOpts.td
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// We try to create options similar to lld's. That way, options passed to clang
// -Xoffload-linker can be the same whether offloading to nvptx or amdgpu.

include "llvm/Option/OptParser.td"

def WrapperOnlyOption : OptionFlag;
Expand Down Expand Up @@ -70,6 +73,11 @@ def plugin_opt : Joined<["--", "-"], "plugin-opt=">, Flags<[WrapperOnlyOption]>,
HelpText<"Options passed to LLVM, not including the Clang invocation. Use "
"'--plugin-opt=--help' for a list of options.">;

def lto_newpm_passes : Joined<["--"], "lto-newpm-passes=">,
Flags<[WrapperOnlyOption]>, HelpText<"Passes to run during LTO">;
def lto_debug_pass_manager : Flag<["--"], "lto-debug-pass-manager">,
Flags<[WrapperOnlyOption]>, HelpText<"Debug new pass manager">;

def save_temps : Flag<["--", "-"], "save-temps">,
Flags<[WrapperOnlyOption]>, HelpText<"Save intermediate results">;

Expand Down

0 comments on commit e742674

Please sign in to comment.