Skip to content

Commit 07b8f8e

Browse files
committed
[Remarks][Driver] Place temporary remark files next to temporary object files
On Darwin, when used for generating a linked binary from a source file (through an intermediate object file), the driver will invoke `cc1` to generate a temporary object file. The temporary remark file will now be emitted next to the object file, which will then be picked up by `dsymutil` and emitted in the .dSYM bundle. This is available for all formats except YAML since by default, YAML doesn't need a section and the remark file will be lost.
1 parent f550961 commit 07b8f8e

File tree

3 files changed

+53
-20
lines changed

3 files changed

+53
-20
lines changed

clang/docs/UsersManual.rst

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -355,28 +355,47 @@ output format of the diagnostics that it generates.
355355

356356
where ``<base>`` is based on the output file of the compilation (whether
357357
it's explicitly specified through `-o` or not) when used with `-c` or `-S`.
358-
In other cases, it's based on the input file's stem. For example:
358+
For example:
359359

360360
* ``clang -fsave-optimization-record -c in.c -o out.o`` will generate
361361
``out.opt.yaml``
362362

363-
* ``clang -fsave-optimization-record in.c -o out`` will generate
363+
* ``clang -fsave-optimization-record -c in.c `` will generate
364364
``in.opt.yaml``
365365

366-
Compiling for multiple architectures will use the following scheme:
367-
368-
``<base>-<arch>.opt.<format>``
369-
370-
Note that this is only allowed on Darwin platforms and is incompatible with
371-
passing multiple ``-arch <arch>`` options.
372-
373366
When targeting (Thin)LTO, the base is derived from the output filename, and
374367
the extension is not dropped.
375368

376369
When targeting ThinLTO, the following scheme is used:
377370

378371
``<base>.opt.<format>.thin.<num>.<format>``
379372

373+
Darwin-only: when used for generating a linked binary from a source file
374+
(through an intermediate object file), the driver will invoke `cc1` to
375+
generate a temporary object file. The temporary remark file will be emitted
376+
next to the object file, which will then be picked up by `dsymutil` and
377+
emitted in the .dSYM bundle. This is available for all formats except YAML.
378+
379+
For example:
380+
381+
``clang -fsave-optimization-record=bitstream in.c -o out`` will generate
382+
383+
* ``/var/folders/43/9y164hh52tv_2nrdxrj31nyw0000gn/T/a-9be59b.o``
384+
385+
* ``/var/folders/43/9y164hh52tv_2nrdxrj31nyw0000gn/T/a-9be59b.opt.bitstream``
386+
387+
* ``out``
388+
389+
* ``out.dSYM/Contents/Resources/Remarks/out``
390+
391+
Darwin-only: compiling for multiple architectures will use the following
392+
scheme:
393+
394+
``<base>-<arch>.opt.<format>``
395+
396+
Note that this is incompatible with passing the
397+
:ref:`-foptimization-record-file <opt_foptimization-record-file>` option.
398+
380399
.. _opt_foptimization-record-file:
381400

382401
**-foptimization-record-file**

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,7 +1444,12 @@ static bool checkRemarksOptions(const Driver &D, const ArgList &Args,
14441444

14451445
static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs,
14461446
const llvm::Triple &Triple,
1447-
const InputInfo &Input, const JobAction &JA) {
1447+
const InputInfo &Input,
1448+
const InputInfo &Output, const JobAction &JA) {
1449+
StringRef Format = "yaml";
1450+
if (const Arg *A = Args.getLastArg(options::OPT_fsave_optimization_record_EQ))
1451+
Format = A->getValue();
1452+
14481453
CmdArgs.push_back("-opt-record-file");
14491454

14501455
const Arg *A = Args.getLastArg(options::OPT_foptimization_record_file_EQ);
@@ -1454,11 +1459,17 @@ static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs,
14541459
bool hasMultipleArchs =
14551460
Triple.isOSDarwin() && // Only supported on Darwin platforms.
14561461
Args.getAllArgValues(options::OPT_arch).size() > 1;
1462+
14571463
SmallString<128> F;
14581464

14591465
if (Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) {
14601466
if (Arg *FinalOutput = Args.getLastArg(options::OPT_o))
14611467
F = FinalOutput->getValue();
1468+
} else {
1469+
if (Format != "yaml" && // For YAML, keep the original behavior.
1470+
Triple.isOSDarwin() && // Enable this only on darwin, since it's the only platform supporting .dSYM bundles.
1471+
Output.isFilename())
1472+
F = Output.getFilename();
14621473
}
14631474

14641475
if (F.empty()) {
@@ -1494,12 +1505,9 @@ static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs,
14941505
llvm::sys::path::replace_extension(F, OldExtension);
14951506
}
14961507

1497-
std::string Extension = "opt.";
1498-
if (const Arg *A =
1499-
Args.getLastArg(options::OPT_fsave_optimization_record_EQ))
1500-
Extension += A->getValue();
1501-
else
1502-
Extension += "yaml";
1508+
SmallString<32> Extension;
1509+
Extension += "opt.";
1510+
Extension += Format;
15031511

15041512
llvm::sys::path::replace_extension(F, Extension);
15051513
CmdArgs.push_back(Args.MakeArgString(F));
@@ -1511,10 +1519,9 @@ static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs,
15111519
CmdArgs.push_back(A->getValue());
15121520
}
15131521

1514-
if (const Arg *A =
1515-
Args.getLastArg(options::OPT_fsave_optimization_record_EQ)) {
1522+
if (!Format.empty()) {
15161523
CmdArgs.push_back("-opt-record-format");
1517-
CmdArgs.push_back(A->getValue());
1524+
CmdArgs.push_back(Format.data());
15181525
}
15191526
}
15201527

@@ -5524,7 +5531,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
55245531

55255532
// Remarks can be enabled with any of the `-f.*optimization-record.*` flags.
55265533
if (willEmitRemarks(Args) && checkRemarksOptions(D, Args, Triple))
5527-
renderRemarksOptions(Args, CmdArgs, Triple, Input, JA);
5534+
renderRemarksOptions(Args, CmdArgs, Triple, Input, Output, JA);
55285535

55295536
bool RewriteImports = Args.hasFlag(options::OPT_frewrite_imports,
55305537
options::OPT_fno_rewrite_imports, false);

clang/test/Driver/darwin-opt-record.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// RUN: %clang -target x86_64-apple-darwin10 -### -c -o FOO -foptimization-record-file=tmp -arch x86_64 -arch x86_64h %s 2>&1 | FileCheck %s --check-prefix=CHECK-MULTIPLE-ARCH-ERROR
55
// RUN: %clang -target x86_64-apple-darwin10 -### -o FOO -fsave-optimization-record %s 2>&1 | FileCheck %s --check-prefix=CHECK-DSYMUTIL-NO-G
66
// RUN: %clang -target x86_64-apple-darwin10 -### -o FOO -g0 -fsave-optimization-record %s 2>&1 | FileCheck %s --check-prefix=CHECK-DSYMUTIL-G0
7+
// RUN: %clang -target x86_64-apple-darwin10 -### -o FOO -fsave-optimization-record=bitstream %s 2>&1 | FileCheck %s --check-prefix=CHECK-NO-G-PATH-BITSTREAM
78
//
89
// CHECK-MULTIPLE-ARCH: "-cc1"
910
// CHECK-MULTIPLE-ARCH: "-opt-record-file" "FOO-x86_64.opt.yaml"
@@ -22,3 +23,9 @@
2223
// CHECK-DSYMUTIL-G0: "-cc1"
2324
// CHECK-DSYMUTIL-G0: ld
2425
// CHECK-DSYMUTIL-G0: dsymutil
26+
//
27+
// CHECK-NO-G-PATH-BITSTREAM: "-cc1"
28+
// CHECK-NO-G-PATH-BITSTREAM: "-opt-record-file" "[[OUTPATH:.*]].opt.bitstream"
29+
// CHECK-NO-G-PATH-BITSTREAM: "-o" "[[OUTPATH:.*]].o"
30+
// CHECK-NO-G-PATH-BITSTREAM: ld
31+
// CHECK-NO-G-PATH-BITSTREAM: dsymutil

0 commit comments

Comments
 (0)