Skip to content

Commit 1435c8e

Browse files
committed
Reapply "[LinkerWrapper] Clean up options after proper forwarding" (#126495)
Summary: The test failed because it no longer passed Rpass by default without LTO. I think that's desirable as it matches the standard behavior. This reverts commit 6fd99de.
1 parent 035bc52 commit 1435c8e

File tree

6 files changed

+41
-53
lines changed

6 files changed

+41
-53
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -9282,13 +9282,24 @@ void LinkerWrapper::ConstructJob(Compilation &C, const JobAction &JA,
92829282
for (StringRef Arg : LinkerArgs)
92839283
CmdArgs.push_back(Args.MakeArgString(
92849284
"--device-linker=" + TC->getTripleString() + "=" + Arg));
9285+
9286+
// Forward the LTO mode relying on the Driver's parsing.
9287+
if (C.getDriver().getOffloadLTOMode() == LTOK_Full)
9288+
CmdArgs.push_back(Args.MakeArgString(
9289+
"--device-compiler=" + TC->getTripleString() + "=-flto=full"));
9290+
else if (C.getDriver().getOffloadLTOMode() == LTOK_Thin)
9291+
CmdArgs.push_back(Args.MakeArgString(
9292+
"--device-compiler=" + TC->getTripleString() + "=-flto=thin"));
92859293
}
92869294
}
92879295

92889296
CmdArgs.push_back(
92899297
Args.MakeArgString("--host-triple=" + getToolChain().getTripleString()));
92909298
if (Args.hasArg(options::OPT_v))
92919299
CmdArgs.push_back("--wrapper-verbose");
9300+
if (Arg *A = Args.getLastArg(options::OPT_cuda_path_EQ))
9301+
CmdArgs.push_back(
9302+
Args.MakeArgString(Twine("--cuda-path=") + A->getValue()));
92929303

92939304
// Construct the link job so we can wrap around it.
92949305
Linker->ConstructJob(C, JA, Output, Inputs, Args, LinkingOutput);

clang/test/Driver/linker-wrapper.c

+12-12
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ __attribute__((visibility("protected"), used)) int x;
2121
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
2222
// RUN: --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=NVPTX-LINK
2323

24-
// NVPTX-LINK: clang{{.*}} -o {{.*}}.img --target=nvptx64-nvidia-cuda -march=sm_70 -O2 -flto {{.*}}.o {{.*}}.o
24+
// NVPTX-LINK: clang{{.*}} -o {{.*}}.img --target=nvptx64-nvidia-cuda -march=sm_70 {{.*}}.o {{.*}}.o
2525

2626
// RUN: clang-offload-packager -o %t.out \
2727
// RUN: --image=file=%t.elf.o,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70 \
2828
// RUN: --image=file=%t.elf.o,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70
2929
// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.o -fembed-offload-object=%t.out
30-
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run --device-debug -O0 \
30+
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run --device-compiler=-g \
3131
// RUN: --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=NVPTX-LINK-DEBUG
3232

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

3535
// RUN: clang-offload-packager -o %t.out \
3636
// RUN: --image=file=%t.elf.o,kind=openmp,triple=amdgcn-amd-amdhsa,arch=gfx908 \
@@ -39,16 +39,16 @@ __attribute__((visibility("protected"), used)) int x;
3939
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
4040
// RUN: --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=AMDGPU-LINK
4141

42-
// AMDGPU-LINK: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx908 -O2 -flto -Wl,--no-undefined {{.*}}.o {{.*}}.o
42+
// AMDGPU-LINK: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx908 -Wl,--no-undefined {{.*}}.o {{.*}}.o
4343

4444
// RUN: clang-offload-packager -o %t.out \
4545
// RUN: --image=file=%t.amdgpu.bc,kind=openmp,triple=amdgcn-amd-amdhsa,arch=gfx1030 \
4646
// RUN: --image=file=%t.amdgpu.bc,kind=openmp,triple=amdgcn-amd-amdhsa,arch=gfx1030
4747
// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.o -fembed-offload-object=%t.out
48-
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run --save-temps -O2 \
48+
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run --device-compiler=--save-temps \
4949
// RUN: --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=AMDGPU-LTO-TEMPS
5050

51-
// AMDGPU-LTO-TEMPS: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx1030 -O2 -flto -Wl,--no-undefined {{.*}}.o -save-temps
51+
// AMDGPU-LTO-TEMPS: clang{{.*}} --target=amdgcn-amd-amdhsa -mcpu=gfx1030 {{.*}}-save-temps
5252

5353
// RUN: clang-offload-packager -o %t.out \
5454
// RUN: --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu \
@@ -59,7 +59,7 @@ __attribute__((visibility("protected"), used)) int x;
5959
// RUN: --linker-path=/usr/bin/ld.lld --whole-archive %t.a --no-whole-archive \
6060
// RUN: %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=CPU-LINK
6161

62-
// CPU-LINK: clang{{.*}} -o {{.*}}.img --target=x86_64-unknown-linux-gnu -O2 -flto -Wl,--no-undefined {{.*}}.o {{.*}}.o -Wl,-Bsymbolic -shared -Wl,--whole-archive {{.*}}.a -Wl,--no-whole-archive
62+
// CPU-LINK: clang{{.*}} -o {{.*}}.img --target=x86_64-unknown-linux-gnu -Wl,--no-undefined {{.*}}.o {{.*}}.o -Wl,-Bsymbolic -shared -Wl,--whole-archive {{.*}}.a -Wl,--no-whole-archive
6363

6464
// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.o
6565
// RUN: clang-linker-wrapper --dry-run --host-triple=x86_64-unknown-linux-gnu -mllvm -openmp-opt-disable \
@@ -148,7 +148,7 @@ __attribute__((visibility("protected"), used)) int x;
148148
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run --clang-backend \
149149
// RUN: --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=CLANG-BACKEND
150150

151-
// CLANG-BACKEND: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx908 -O2 -flto -Wl,--no-undefined {{.*}}.o
151+
// CLANG-BACKEND: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx908 -Wl,--no-undefined {{.*}}.o
152152

153153
// RUN: clang-offload-packager -o %t.out \
154154
// RUN: --image=file=%t.elf.o,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70
@@ -171,8 +171,8 @@ __attribute__((visibility("protected"), used)) int x;
171171
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
172172
// RUN: --linker-path=/usr/bin/ld %t-on.o %t-off.o %t.a -o a.out 2>&1 | FileCheck %s --check-prefix=AMD-TARGET-ID
173173

174-
// AMD-TARGET-ID: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx90a:xnack+ -O2 -flto -Wl,--no-undefined {{.*}}.o {{.*}}.o
175-
// AMD-TARGET-ID: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx90a:xnack- -O2 -flto -Wl,--no-undefined {{.*}}.o {{.*}}.o
174+
// AMD-TARGET-ID: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx90a:xnack+ -Wl,--no-undefined {{.*}}.o {{.*}}.o
175+
// AMD-TARGET-ID: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx90a:xnack- -Wl,--no-undefined {{.*}}.o {{.*}}.o
176176

177177
// RUN: clang-offload-packager -o %t-lib.out \
178178
// RUN: --image=file=%t.elf.o,kind=openmp,triple=amdgcn-amd-amdhsa,arch=generic
@@ -187,8 +187,8 @@ __attribute__((visibility("protected"), used)) int x;
187187
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
188188
// RUN: --linker-path=/usr/bin/ld %t1.o %t2.o %t.a -o a.out 2>&1 | FileCheck %s --check-prefix=ARCH-ALL
189189

190-
// ARCH-ALL: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx90a -O2 -flto -Wl,--no-undefined {{.*}}.o {{.*}}.o
191-
// ARCH-ALL: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx908 -O2 -flto -Wl,--no-undefined {{.*}}.o {{.*}}.o
190+
// ARCH-ALL: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx90a -Wl,--no-undefined {{.*}}.o {{.*}}.o
191+
// ARCH-ALL: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx908 -Wl,--no-undefined {{.*}}.o {{.*}}.o
192192

193193
// RUN: clang-offload-packager -o %t.out \
194194
// RUN: --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu \

clang/test/Driver/openmp-offload.c

+10
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,13 @@
208208
// RUN: -fsyntax-only %s 2>&1 | FileCheck -check-prefix=CHK-SYNTAX-ONLY-ARGS %s
209209
// CHK-SYNTAX-ONLY-ARGS: "-cc1" "-triple" "powerpc64le-ibm-linux-gnu"{{.*}}"-fsyntax-only"
210210
// CHK-SYNTAX-ONLY-ARGS: "-cc1" "-triple" "powerpc64le-unknown-linux"{{.*}}"-fsyntax-only"
211+
212+
//
213+
// Ensure `-foffload-lto` is forwarded properly.
214+
//
215+
// RUN: %clang -### --target=powerpc64le-linux -fopenmp=libomp -fopenmp-targets=powerpc64le-ibm-linux-gnu \
216+
// RUN: -foffload-lto %s 2>&1 | FileCheck -check-prefix=CHK-DEVICE-LTO-FULL %s
217+
// CHK-DEVICE-LTO-FULL: clang-linker-wrapper{{.*}} "--device-compiler=powerpc64le-ibm-linux-gnu=-flto=full"
218+
// RUN: %clang -### --target=powerpc64le-linux -fopenmp=libomp -fopenmp-targets=powerpc64le-ibm-linux-gnu \
219+
// RUN: -foffload-lto=thin %s 2>&1 | FileCheck -check-prefix=CHK-DEVICE-LTO-THIN %s
220+
// CHK-DEVICE-LTO-THIN: clang-linker-wrapper{{.*}} "--device-compiler=powerpc64le-ibm-linux-gnu=-flto=thin"

clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

-21
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,6 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args) {
483483
if (!TempFileOrErr)
484484
return TempFileOrErr.takeError();
485485

486-
StringRef OptLevel = Args.getLastArgValue(OPT_opt_level, "O2");
487486
SmallVector<StringRef, 16> CmdArgs{
488487
*ClangPath,
489488
"--no-default-config",
@@ -496,10 +495,7 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args) {
496495
Triple.isAMDGPU() ? CmdArgs.push_back(Args.MakeArgString("-mcpu=" + Arch))
497496
: CmdArgs.push_back(Args.MakeArgString("-march=" + Arch));
498497

499-
CmdArgs.push_back(Args.MakeArgString("-" + OptLevel));
500-
501498
// Forward all of the `--offload-opt` and similar options to the device.
502-
CmdArgs.push_back("-flto");
503499
for (auto &Arg : Args.filtered(OPT_offload_opt_eq_minus, OPT_mllvm))
504500
CmdArgs.append(
505501
{"-Xlinker",
@@ -548,29 +544,12 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args) {
548544
CmdArgs.append({"-Xlinker", Args.MakeArgString(
549545
"-mllvm=" + StringRef(Arg->getValue()))});
550546

551-
if (Args.hasArg(OPT_debug))
552-
CmdArgs.push_back("-g");
553-
554-
if (SaveTemps)
555-
CmdArgs.push_back("-save-temps");
556-
557547
if (SaveTemps && linkerSupportsLTO(Args))
558548
CmdArgs.push_back("-Wl,--save-temps");
559549

560550
if (Args.hasArg(OPT_embed_bitcode))
561551
CmdArgs.push_back("-Wl,--lto-emit-llvm");
562552

563-
if (Verbose)
564-
CmdArgs.push_back("-v");
565-
566-
if (!CudaBinaryPath.empty())
567-
CmdArgs.push_back(Args.MakeArgString("--cuda-path=" + CudaBinaryPath));
568-
569-
for (StringRef Arg : Args.getAllArgValues(OPT_ptxas_arg))
570-
llvm::copy(
571-
SmallVector<StringRef>({"-Xcuda-ptxas", Args.MakeArgString(Arg)}),
572-
std::back_inserter(CmdArgs));
573-
574553
for (StringRef Arg : Args.getAllArgValues(OPT_linker_arg_EQ))
575554
CmdArgs.append({"-Xlinker", Args.MakeArgString(Arg)});
576555
for (StringRef Arg : Args.getAllArgValues(OPT_compiler_arg_EQ))

clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td

+5-17
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ def cuda_path_EQ : Joined<["--"], "cuda-path=">,
1717
Flags<[WrapperOnlyOption]>, MetaVarName<"<dir>">,
1818
HelpText<"Set the system CUDA path">;
1919
def host_triple_EQ : Joined<["--"], "host-triple=">,
20-
Flags<[WrapperOnlyOption]>, MetaVarName<"<triple>">,
21-
HelpText<"Triple to use for the host compilation">;
22-
def opt_level : Joined<["--"], "opt-level=">,
23-
Flags<[WrapperOnlyOption]>, MetaVarName<"<O0, O1, O2, or O3>">,
24-
HelpText<"Optimization level for LTO">;
20+
Flags<[WrapperOnlyOption]>,
21+
MetaVarName<"<triple>">,
22+
HelpText<"Triple to use for the host compilation">;
2523
def device_linker_args_EQ : Joined<["--"], "device-linker=">,
2624
Flags<[WrapperOnlyOption]>, MetaVarName<"<value> or <triple>=<value>">,
2725
HelpText<"Arguments to pass to the device linker invocation">;
@@ -34,18 +32,8 @@ def dry_run : Flag<["--"], "dry-run">,
3432
def verbose : Flag<["--"], "wrapper-verbose">,
3533
Flags<[WrapperOnlyOption]>, HelpText<"Verbose output from tools">;
3634
def embed_bitcode : Flag<["--"], "embed-bitcode">,
37-
Flags<[WrapperOnlyOption]>, HelpText<"Embed linked bitcode in the module">;
38-
def debug : Flag<["--"], "device-debug">, Flags<[WrapperOnlyOption]>,
39-
HelpText<"Use debugging">;
40-
def ptxas_arg : Joined<["--"], "ptxas-arg=">,
41-
Flags<[WrapperOnlyOption]>,
42-
HelpText<"Argument to pass to the 'ptxas' invocation">;
43-
def pass_remarks_EQ : Joined<["--"], "pass-remarks=">,
44-
Flags<[WrapperOnlyOption]>, HelpText<"Pass remarks for LTO">;
45-
def pass_remarks_missed_EQ : Joined<["--"], "pass-remarks-missed=">,
46-
Flags<[WrapperOnlyOption]>, HelpText<"Pass remarks for LTO">;
47-
def pass_remarks_analysis_EQ : Joined<["--"], "pass-remarks-analysis=">,
48-
Flags<[WrapperOnlyOption]>, HelpText<"Pass remarks for LTO">;
35+
Flags<[WrapperOnlyOption]>,
36+
HelpText<"Embed linked bitcode in the module">;
4937
def print_wrapped_module : Flag<["--"], "print-wrapped-module">,
5038
Flags<[WrapperOnlyOption]>,
5139
HelpText<"Print the wrapped module's IR for testing">;

offload/test/offloading/bug51781.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
// SPMDize. There is no main thread, so there's no issue.
77
//
8-
// RUN: %libomptarget-compile-generic -O2 -Rpass=openmp-opt > %t.spmd 2>&1
8+
// RUN: %libomptarget-compile-generic -O2 -foffload-lto -Rpass=openmp-opt > %t.spmd 2>&1
99
// RUN: %fcheck-nvptx64-nvidia-cuda -check-prefix=SPMD -input-file=%t.spmd
1010
// RUN: %fcheck-amdgcn-amd-amdhsa -check-prefix=SPMD -input-file=%t.spmd
1111
// RUN: %libomptarget-run-generic 2>&1 | %fcheck-generic
@@ -15,7 +15,7 @@
1515
// Use the custom state machine, which must avoid the same barrier problem as
1616
// the generic state machine.
1717
//
18-
// RUN: %libomptarget-compile-generic -O2 -Rpass=openmp-opt \
18+
// RUN: %libomptarget-compile-generic -O2 -foffload-lto -Rpass=openmp-opt \
1919
// RUN: -mllvm -openmp-opt-disable-spmdization > %t.custom 2>&1
2020
// RUN: %fcheck-nvptx64-nvidia-cuda -check-prefix=CUSTOM -input-file=%t.custom
2121
// RUN: %fcheck-amdgcn-amd-amdhsa -check-prefix=CUSTOM -input-file=%t.custom
@@ -24,7 +24,7 @@
2424
// Repeat with reduction clause, which has managed to break the custom state
2525
// machine in the past.
2626
//
27-
// RUN: %libomptarget-compile-generic -O2 -Rpass=openmp-opt -DADD_REDUCTION \
27+
// RUN: %libomptarget-compile-generic -O2 -foffload-lto -Rpass=openmp-opt -DADD_REDUCTION \
2828
// RUN: -mllvm -openmp-opt-disable-spmdization > %t.custom 2>&1
2929
// RUN: %fcheck-nvptx64-nvidia-cuda -check-prefix=CUSTOM -input-file=%t.custom
3030
// RUN: %fcheck-amdgcn-amd-amdhsa -check-prefix=CUSTOM -input-file=%t.custom

0 commit comments

Comments
 (0)