Skip to content

Commit b5dd483

Browse files
committed
[LinkerWrapper] Fix -fsave-optimization-record default file
As discussed in PR #145603, the following command fails to produce a YAML remarks file for offload LTO passes and thus for kernel-info: ``` clang -O2 -g -fopenmp --offload-arch=native test.c -foffload-lto \ -Rpass=kernel-info -fsave-optimization-record ``` The problem is that, in clang-linker-wrapper's clang call, clang names the file based on clang's main output file (from `-o`). That is a temporary file, so the YAML file becomes a temporary file, which the user never sees. This patch: - Extends clang with a hidden `-foutput-file-base=BASE` option that overrides the main output file as the base for other output files. - Makes clang honor that option only for the default YAML remarks file, but future patches could use it for other output files too. - Extends clang-linker-wrapper to specify that option to clang.
1 parent 4b52d22 commit b5dd483

File tree

6 files changed

+49
-22
lines changed

6 files changed

+49
-22
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5880,6 +5880,12 @@ def o : JoinedOrSeparate<["-"], "o">,
58805880
Visibility<[ClangOption, CC1Option, CC1AsOption, FC1Option, FlangOption]>,
58815881
HelpText<"Write output to <file>">, MetaVarName<"<file>">,
58825882
MarshallingInfoString<FrontendOpts<"OutputFile">>;
5883+
def foutput_file_base : Joined<["-"], "foutput-file-base=">,
5884+
Flags<[NoXarchOption, HelpHidden]>,
5885+
Visibility<[ClangOption]>,
5886+
HelpText<"Name extra output files after <base> not the main output file">,
5887+
MetaVarName<"<base>">,
5888+
MarshallingInfoString<FrontendOpts<"OutputFileBase">>;
58835889
def object_file_name_EQ : Joined<["-"], "object-file-name=">,
58845890
Visibility<[ClangOption, CC1Option, CC1AsOption, CLOption, DXCOption]>,
58855891
HelpText<"Set the output <file> for debug infos">, MetaVarName<"<file>">,

clang/include/clang/Frontend/FrontendOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,9 @@ class FrontendOptions {
439439
/// The output file, if any.
440440
std::string OutputFile;
441441

442+
/// The base, if any, to use instead of OutputFile for extra output files.
443+
std::string OutputFileBase;
444+
442445
/// If given, the new suffix for fix-it rewritten files.
443446
std::string FixItSuffix;
444447

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,9 @@ static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs,
294294
Format = A->getValue();
295295

296296
SmallString<128> F;
297-
const Arg *A = Args.getLastArg(options::OPT_foptimization_record_file_EQ);
298-
if (A)
297+
if (const Arg *A = Args.getLastArg(options::OPT_foptimization_record_file_EQ))
298+
F = A->getValue();
299+
else if (const Arg *A = Args.getLastArg(options::OPT_foutput_file_base))
299300
F = A->getValue();
300301
else if (Output.isFilename())
301302
F = Output.getFilename();
@@ -1320,6 +1321,9 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
13201321
if (Args.hasArg(options::OPT_ftime_report))
13211322
CmdArgs.push_back(
13221323
Args.MakeArgString(Twine(PluginOptPrefix) + "-time-passes"));
1324+
1325+
// clang-linker-wrapper adds this without checking if it is needed.
1326+
Args.ClaimAllArgs(options::OPT_foutput_file_base);
13231327
}
13241328

13251329
void tools::addOpenMPRuntimeLibraryPath(const ToolChain &TC,

clang/test/Driver/linker-wrapper.c

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

25-
// NVPTX-LINK: clang{{.*}} -o {{.*}}.img --target=nvptx64-nvidia-cuda -march=sm_70 {{.*}}.o {{.*}}.o
25+
// NVPTX-LINK: clang{{.*}} -o {{.*}}.img -foutput-file-base=a.out.nvptx64.sm_70.img --target=nvptx64-nvidia-cuda -march=sm_70 {{.*}}.o {{.*}}.o
2626

2727
// RUN: clang-offload-packager -o %t.out \
2828
// RUN: --image=file=%t.elf.o,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70 \
@@ -40,7 +40,7 @@ __attribute__((visibility("protected"), used)) int x;
4040
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
4141
// RUN: --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=AMDGPU-LINK
4242

43-
// AMDGPU-LINK: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx908 -flto -Wl,--no-undefined {{.*}}.o {{.*}}.o
43+
// AMDGPU-LINK: clang{{.*}} -o {{.*}}.img -foutput-file-base=a.out.amdgcn.gfx908.img --target=amdgcn-amd-amdhsa -mcpu=gfx908 -flto -Wl,--no-undefined {{.*}}.o {{.*}}.o
4444

4545
// RUN: clang-offload-packager -o %t.out \
4646
// RUN: --image=file=%t.amdgpu.bc,kind=openmp,triple=amdgcn-amd-amdhsa,arch=gfx1030 \
@@ -57,7 +57,7 @@ __attribute__((visibility("protected"), used)) int x;
5757
// RUN: not clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
5858
// RUN: --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=SPIRV-LINK
5959

60-
// SPIRV-LINK: clang{{.*}} -o {{.*}}.img --target=spirv64-unknown-unknown {{.*}}.o --sycl-link -Xlinker -triple=spirv64-unknown-unknown -Xlinker -arch=
60+
// SPIRV-LINK: clang{{.*}} -o {{.*}}.img -foutput-file-base=a.out.spirv64..img --target=spirv64-unknown-unknown {{.*}}.o --sycl-link -Xlinker -triple=spirv64-unknown-unknown -Xlinker -arch=
6161

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

71-
// 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
71+
// CPU-LINK: clang{{.*}} -o {{.*}}.img -foutput-file-base=a.out.x86_64..img --target=x86_64-unknown-linux-gnu -Wl,--no-undefined {{.*}}.o {{.*}}.o -Wl,-Bsymbolic -shared -Wl,--whole-archive {{.*}}.a -Wl,--no-whole-archive
7272

7373
// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.o
7474
// RUN: clang-linker-wrapper --dry-run --host-triple=x86_64-unknown-linux-gnu -mllvm -openmp-opt-disable \
@@ -100,8 +100,8 @@ __attribute__((visibility("protected"), used)) int x;
100100
// RUN: clang-linker-wrapper --dry-run --host-triple=x86_64-unknown-linux-gnu \
101101
// RUN: --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=CUDA
102102

103-
// CUDA: clang{{.*}} -o [[IMG_SM70:.+]] --target=nvptx64-nvidia-cuda -march=sm_70
104-
// CUDA: clang{{.*}} -o [[IMG_SM52:.+]] --target=nvptx64-nvidia-cuda -march=sm_52
103+
// CUDA: clang{{.*}} -o [[IMG_SM70:.+]] -foutput-file-base=a.out.nvptx64.sm_70.img --target=nvptx64-nvidia-cuda -march=sm_70
104+
// CUDA: clang{{.*}} -o [[IMG_SM52:.+]] -foutput-file-base=a.out.nvptx64.sm_52.img --target=nvptx64-nvidia-cuda -march=sm_52
105105
// CUDA: fatbinary{{.*}}-64 --create {{.*}}.fatbin --image=profile=sm_70,file=[[IMG_SM70]] --image=profile=sm_52,file=[[IMG_SM52]]
106106
// CUDA: usr/bin/ld{{.*}} {{.*}}.openmp.image.{{.*}}.o {{.*}}.cuda.image.{{.*}}.o
107107

@@ -127,8 +127,8 @@ __attribute__((visibility("protected"), used)) int x;
127127
// RUN: --compress --compression-level=6 \
128128
// RUN: --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=HIP
129129

130-
// HIP: clang{{.*}} -o [[IMG_GFX90A:.+]] --target=amdgcn-amd-amdhsa -mcpu=gfx90a
131-
// HIP: clang{{.*}} -o [[IMG_GFX908:.+]] --target=amdgcn-amd-amdhsa -mcpu=gfx908
130+
// HIP: clang{{.*}} -o [[IMG_GFX90A:.+]] -foutput-file-base=a.out.amdgcn.gfx90a.img --target=amdgcn-amd-amdhsa -mcpu=gfx90a
131+
// HIP: clang{{.*}} -o [[IMG_GFX908:.+]] -foutput-file-base=a.out.amdgcn.gfx908.img --target=amdgcn-amd-amdhsa -mcpu=gfx908
132132
// HIP: clang-offload-bundler{{.*}}-type=o -bundle-align=4096 -compress -compression-level=6 -targets=host-x86_64-unknown-linux-gnu,hip-amdgcn-amd-amdhsa--gfx90a,hip-amdgcn-amd-amdhsa--gfx908 -input={{/dev/null|NUL}} -input=[[IMG_GFX90A]] -input=[[IMG_GFX908]] -output={{.*}}.hipfb
133133

134134
// RUN: clang-offload-packager -o %t.out \
@@ -157,7 +157,7 @@ __attribute__((visibility("protected"), used)) int x;
157157
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run --clang-backend \
158158
// RUN: --linker-path=/usr/bin/ld %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=CLANG-BACKEND
159159

160-
// CLANG-BACKEND: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx908 -flto -Wl,--no-undefined {{.*}}.o
160+
// CLANG-BACKEND: clang{{.*}} -o {{.*}}.img -foutput-file-base=a.out.amdgcn.gfx908.img --target=amdgcn-amd-amdhsa -mcpu=gfx908 -flto -Wl,--no-undefined {{.*}}.o
161161

162162
// RUN: clang-offload-packager -o %t.out \
163163
// RUN: --image=file=%t.elf.o,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70
@@ -180,8 +180,8 @@ __attribute__((visibility("protected"), used)) int x;
180180
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
181181
// 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
182182

183-
// AMD-TARGET-ID: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx90a:xnack+ -flto -Wl,--no-undefined {{.*}}.o {{.*}}.o
184-
// AMD-TARGET-ID: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx90a:xnack- -flto -Wl,--no-undefined {{.*}}.o {{.*}}.o
183+
// AMD-TARGET-ID: clang{{.*}} -o {{.*}}.img -foutput-file-base=a.out.amdgcn.gfx90a:xnack+.img --target=amdgcn-amd-amdhsa -mcpu=gfx90a:xnack+ -flto -Wl,--no-undefined {{.*}}.o {{.*}}.o
184+
// AMD-TARGET-ID: clang{{.*}} -o {{.*}}.img -foutput-file-base=a.out.amdgcn.gfx90a:xnack-.img --target=amdgcn-amd-amdhsa -mcpu=gfx90a:xnack- -flto -Wl,--no-undefined {{.*}}.o {{.*}}.o
185185

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

199-
// ARCH-ALL: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx90a -flto -Wl,--no-undefined {{.*}}.o {{.*}}.o
200-
// ARCH-ALL: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa -mcpu=gfx908 -flto -Wl,--no-undefined {{.*}}.o {{.*}}.o
199+
// ARCH-ALL: clang{{.*}} -o {{.*}}.img -foutput-file-base=a.out.amdgcn.gfx90a.img --target=amdgcn-amd-amdhsa -mcpu=gfx90a -flto -Wl,--no-undefined {{.*}}.o {{.*}}.o
200+
// ARCH-ALL: clang{{.*}} -o {{.*}}.img -foutput-file-base=a.out.amdgcn.gfx908.img --target=amdgcn-amd-amdhsa -mcpu=gfx908 -flto -Wl,--no-undefined {{.*}}.o {{.*}}.o
201201

202202
// RUN: clang-offload-packager -o %t.out \
203203
// RUN: --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu \
@@ -207,7 +207,7 @@ __attribute__((visibility("protected"), used)) int x;
207207
// RUN: --linker-path=/usr/bin/ld.lld -r %t.o \
208208
// RUN: %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=RELOCATABLE-LINK
209209

210-
// RELOCATABLE-LINK: clang{{.*}} -o {{.*}}.img --target=x86_64-unknown-linux-gnu
210+
// RELOCATABLE-LINK: clang{{.*}} -o {{.*}}.img -foutput-file-base=a.out.x86_64..img --target=x86_64-unknown-linux-gnu
211211
// RELOCATABLE-LINK: /usr/bin/ld.lld{{.*}}-r
212212
// RELOCATABLE-LINK: llvm-objcopy{{.*}}a.out --remove-section .llvm.offloading
213213

@@ -219,7 +219,7 @@ __attribute__((visibility("protected"), used)) int x;
219219
// RUN: --linker-path=/usr/bin/ld.lld -r %t.o \
220220
// RUN: %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=RELOCATABLE-LINK-HIP
221221

222-
// RELOCATABLE-LINK-HIP: clang{{.*}} -o {{.*}}.img --target=amdgcn-amd-amdhsa
222+
// RELOCATABLE-LINK-HIP: clang{{.*}} -o {{.*}}.img -foutput-file-base=a.out.amdgcn.gfx90a.img --target=amdgcn-amd-amdhsa
223223
// RELOCATABLE-LINK-HIP: clang-offload-bundler{{.*}} -type=o -bundle-align=4096 -targets=host-x86_64-unknown-linux-gnu,hip-amdgcn-amd-amdhsa--gfx90a -input={{/dev/null|NUL}} -input={{.*}} -output={{.*}}
224224
// RELOCATABLE-LINK-HIP: /usr/bin/ld.lld{{.*}}-r
225225
// RELOCATABLE-LINK-HIP: llvm-objcopy{{.*}}a.out --remove-section .llvm.offloading
@@ -233,7 +233,7 @@ __attribute__((visibility("protected"), used)) int x;
233233
// RUN: --linker-path=/usr/bin/ld.lld -r %t.o \
234234
// RUN: %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=RELOCATABLE-LINK-CUDA
235235

236-
// RELOCATABLE-LINK-CUDA: clang{{.*}} -o {{.*}}.img --target=nvptx64-nvidia-cuda
236+
// RELOCATABLE-LINK-CUDA: clang{{.*}} -o {{.*}}.img -foutput-file-base=a.out.nvptx64.sm_89.img --target=nvptx64-nvidia-cuda
237237
// RELOCATABLE-LINK-CUDA: fatbinary{{.*}} -64 --create {{.*}}.fatbin --image=profile=sm_89,file={{.*}}.img
238238
// RELOCATABLE-LINK-CUDA: /usr/bin/ld.lld{{.*}}-r
239239
// RELOCATABLE-LINK-CUDA: llvm-objcopy{{.*}}a.out --remove-section .llvm.offloading

clang/test/Driver/opt-record.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,14 @@
7878
// CHECK-PASS-RPASS-SAME: "-plugin-opt=opt-remarks-hotness-threshold=100"
7979

8080
// CHECK-PASS-AUTO: "-plugin-opt=opt-remarks-hotness-threshold=auto"
81+
82+
// Check -foutput-file-base effect on -foptimization-record-file.
83+
// RUN: %clang --target=x86_64-linux -### -fuse-ld=lld -B%S/Inputs/lld -flto -fsave-optimization-record -foutput-file-base=/dir/file.ext %s 2>&1 | FileCheck %s -check-prefix=CHECK-BASE
84+
// RUN: %clang --target=x86_64-linux -### -o FOO -fuse-ld=lld -B%S/Inputs/lld -flto -fsave-optimization-record -foutput-file-base=/dir/file.ext %s 2>&1 | FileCheck %s -check-prefix=CHECK-BASE
85+
// RUN: %clang --target=x86_64-linux -### -fuse-ld=lld -B%S/Inputs/lld -flto -fsave-optimization-record -foptimization-record-file=user-file.ext -foutput-file-base=/dir/file.ext %s 2>&1 | FileCheck %s -check-prefix=CHECK-IGNORE-BASE
86+
87+
// CHECK-BASE: "-plugin-opt=opt-remarks-filename=/dir/file.ext.opt.ld.yaml"
88+
// CHECK-BASE-SAME: "-plugin-opt=opt-remarks-format=yaml"
89+
90+
// CHECK-IGNORE-BASE: "-plugin-opt=opt-remarks-filename=user-file.ext.opt.ld.yaml"
91+
// CHECK-IGNORE-BASE-SAME: "-plugin-opt=opt-remarks-format=yaml"

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "clang/Basic/TargetID.h"
1818
#include "clang/Basic/Version.h"
19+
#include "clang/Driver/Options.h"
1920
#include "llvm/ADT/MapVector.h"
2021
#include "llvm/BinaryFormat/Magic.h"
2122
#include "llvm/Bitcode/BitcodeWriter.h"
@@ -474,10 +475,10 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args,
474475
StringRef Arch = Args.getLastArgValue(OPT_arch_EQ);
475476
// Create a new file to write the linked device image to. Assume that the
476477
// input filename already has the device and architecture.
477-
auto TempFileOrErr =
478-
createOutputFile(sys::path::filename(ExecutableName) + "." +
479-
Triple.getArchName() + "." + Arch,
480-
"img");
478+
std::string OutputFileBase = "." + Triple.getArchName().str() + "." +
479+
Arch.str();
480+
auto TempFileOrErr = createOutputFile(
481+
sys::path::filename(ExecutableName) + OutputFileBase, "img");
481482
if (!TempFileOrErr)
482483
return TempFileOrErr.takeError();
483484

@@ -486,6 +487,8 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args,
486487
"--no-default-config",
487488
"-o",
488489
*TempFileOrErr,
490+
Args.MakeArgString("-foutput-file-base=" + ExecutableName +
491+
OutputFileBase + ".img"),
489492
Args.MakeArgString("--target=" + Triple.getTriple()),
490493
};
491494

0 commit comments

Comments
 (0)