Skip to content

Commit 36daa2c

Browse files
authored
Merge pull request #502 from francisvm/57984755/apple/stable/20190619
[Remarks][Driver] Place temporary remark files next to temporary object files
2 parents 2c365b6 + 48072d3 commit 36daa2c

File tree

5 files changed

+101
-29
lines changed

5 files changed

+101
-29
lines changed

clang/docs/ClangCommandLineReference.rst

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,9 +1697,14 @@ Emit OpenMP code only for SIMD-based constructs.
16971697

16981698
.. option:: -foperator-arrow-depth=<arg>
16991699

1700-
.. option:: -foptimization-record-file=<arg>
1700+
.. option:: -foptimization-record-file=<file>
17011701

1702-
Specify the output name of the file containing the optimization remarks. Implies -fsave-optimization-record. On Darwin platforms, this cannot be used with multiple -arch <arch> options.
1702+
Implies -fsave-optimization-record. On Darwin platforms, this
1703+
cannot be used with multiple -arch <arch> options.
1704+
1705+
.. option:: -foptimization-record-passes=<regex>
1706+
1707+
Only include passes which match a specified regular expression in the generated optimization record (by default, include all passes)
17031708

17041709
.. option:: -foptimize-sibling-calls, -fno-optimize-sibling-calls
17051710

@@ -1832,6 +1837,12 @@ Turn on loop reroller
18321837

18331838
Generate a YAML optimization record file
18341839

1840+
.. program:: clang1
1841+
.. option:: -fsave-optimization-record=<format>
1842+
.. program:: clang
1843+
1844+
Generate an optimization record file in a specific format.
1845+
18351846
.. option:: -fseh-exceptions
18361847

18371848
Use SEH style exceptions

clang/docs/UsersManual.rst

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,10 @@ output format of the diagnostics that it generates.
324324

325325
.. _opt_fsave-optimization-record:
326326

327-
.. option:: -fsave-optimization-record[=<format>]
327+
.. option:: -f[no-]save-optimization-record[=<format>]
328328

329-
Write optimization remarks to a separate file.
329+
Enable optimization remarks during compilation and write them to a separate
330+
file.
330331

331332
This option, which defaults to off, controls whether Clang writes
332333
optimization reports to a separate file. By recording diagnostics in a file,
@@ -345,20 +346,64 @@ output format of the diagnostics that it generates.
345346
``-fsave-optimization-record=bitstream``: A binary format based on LLVM
346347
Bitstream.
347348

349+
The output file is controlled by :ref:`-foptimization-record-file <opt_foptimization-record-file>`.
350+
351+
In the absence of an explicit output file, the file is chosen using the
352+
following scheme:
353+
354+
``<base>.opt.<format>``
355+
356+
where ``<base>`` is based on the output file of the compilation (whether
357+
it's explicitly specified through `-o` or not) when used with `-c` or `-S`.
358+
For example:
359+
360+
* ``clang -fsave-optimization-record -c in.c -o out.o`` will generate
361+
``out.opt.yaml``
362+
363+
* ``clang -fsave-optimization-record -c in.c `` will generate
364+
``in.opt.yaml``
365+
366+
When targeting (Thin)LTO, the base is derived from the output filename, and
367+
the extension is not dropped.
368+
369+
When targeting ThinLTO, the following scheme is used:
370+
371+
``<base>.opt.<format>.thin.<num>.<format>``
372+
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+
348399
.. _opt_foptimization-record-file:
349400

350401
**-foptimization-record-file**
351-
Control the file to which optimization reports are written.
352-
353-
When optimization reports are being output (see
354-
:ref:`-fsave-optimization-record <opt_fsave-optimization-record>`), this
355-
option controls the file to which those reports are written.
402+
Control the file to which optimization reports are written. This implies
403+
:ref:`-fsave-optimization-record <opt_fsave-optimization-record>`.
356404

357-
If this option is not used, optimization records are output to a file named
358-
after the primary file being compiled. If that's "foo.c", for example,
359-
optimization records are output to "foo.opt.yaml". If a specific
360-
serialization format is specified, the file will be named
361-
"foo.opt.<format>".
405+
On Darwin platforms, this is incompatible with passing multiple
406+
``-arch <arch>`` options.
362407

363408
.. _opt_foptimization-record-passes:
364409

clang/include/clang/Driver/Options.td

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,16 +1764,18 @@ def foperator_arrow_depth_EQ : Joined<["-"], "foperator-arrow-depth=">,
17641764
def fsave_optimization_record : Flag<["-"], "fsave-optimization-record">,
17651765
Group<f_Group>, HelpText<"Generate a YAML optimization record file">;
17661766
def fsave_optimization_record_EQ : Joined<["-"], "fsave-optimization-record=">,
1767-
Group<f_Group>, HelpText<"Generate an optimization record file in a specific format (default: YAML)">;
1767+
Group<f_Group>, HelpText<"Generate an optimization record file in a specific format">,
1768+
MetaVarName<"<format>">;
17681769
def fno_save_optimization_record : Flag<["-"], "fno-save-optimization-record">,
17691770
Group<f_Group>, Flags<[NoArgumentUnused]>;
17701771
def foptimization_record_file_EQ : Joined<["-"], "foptimization-record-file=">,
17711772
Group<f_Group>,
1772-
HelpText<"Specify the file name of any generated YAML optimization record">;
1773+
HelpText<"Specify the output name of the file containing the optimization remarks. Implies -fsave-optimization-record. On Darwin platforms, this cannot be used with multiple -arch <arch> options.">,
1774+
MetaVarName<"<file>">;
17731775
def foptimization_record_passes_EQ : Joined<["-"], "foptimization-record-passes=">,
17741776
Group<f_Group>,
1775-
HelpText<"Only include passes which match a specified regular expression in the generated optimization record (by default, include all passes)">;
1776-
1777+
HelpText<"Only include passes which match a specified regular expression in the generated optimization record (by default, include all passes)">,
1778+
MetaVarName<"<regex>">;
17771779

17781780
def ftest_coverage : Flag<["-"], "ftest-coverage">, Group<f_Group>;
17791781
def fvectorize : Flag<["-"], "fvectorize">, Group<f_Group>,

clang/lib/Driver/ToolChains/Clang.cpp

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

14031403
static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs,
14041404
const llvm::Triple &Triple,
1405-
const InputInfo &Input, const JobAction &JA) {
1405+
const InputInfo &Input,
1406+
const InputInfo &Output, const JobAction &JA) {
1407+
StringRef Format = "yaml";
1408+
if (const Arg *A = Args.getLastArg(options::OPT_fsave_optimization_record_EQ))
1409+
Format = A->getValue();
1410+
14061411
CmdArgs.push_back("-opt-record-file");
14071412

14081413
const Arg *A = Args.getLastArg(options::OPT_foptimization_record_file_EQ);
@@ -1412,11 +1417,17 @@ static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs,
14121417
bool hasMultipleArchs =
14131418
Triple.isOSDarwin() && // Only supported on Darwin platforms.
14141419
Args.getAllArgValues(options::OPT_arch).size() > 1;
1420+
14151421
SmallString<128> F;
14161422

14171423
if (Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) {
14181424
if (Arg *FinalOutput = Args.getLastArg(options::OPT_o))
14191425
F = FinalOutput->getValue();
1426+
} else {
1427+
if (Format != "yaml" && // For YAML, keep the original behavior.
1428+
Triple.isOSDarwin() && // Enable this only on darwin, since it's the only platform supporting .dSYM bundles.
1429+
Output.isFilename())
1430+
F = Output.getFilename();
14201431
}
14211432

14221433
if (F.empty()) {
@@ -1452,12 +1463,9 @@ static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs,
14521463
llvm::sys::path::replace_extension(F, OldExtension);
14531464
}
14541465

1455-
std::string Extension = "opt.";
1456-
if (const Arg *A =
1457-
Args.getLastArg(options::OPT_fsave_optimization_record_EQ))
1458-
Extension += A->getValue();
1459-
else
1460-
Extension += "yaml";
1466+
SmallString<32> Extension;
1467+
Extension += "opt.";
1468+
Extension += Format;
14611469

14621470
llvm::sys::path::replace_extension(F, Extension);
14631471
CmdArgs.push_back(Args.MakeArgString(F));
@@ -1469,10 +1477,9 @@ static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs,
14691477
CmdArgs.push_back(A->getValue());
14701478
}
14711479

1472-
if (const Arg *A =
1473-
Args.getLastArg(options::OPT_fsave_optimization_record_EQ)) {
1480+
if (!Format.empty()) {
14741481
CmdArgs.push_back("-opt-record-format");
1475-
CmdArgs.push_back(A->getValue());
1482+
CmdArgs.push_back(Format.data());
14761483
}
14771484
}
14781485

@@ -5283,7 +5290,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
52835290

52845291
// Remarks can be enabled with any of the `-f.*optimization-record.*` flags.
52855292
if (willEmitRemarks(Args) && checkRemarksOptions(D, Args, Triple))
5286-
renderRemarksOptions(Args, CmdArgs, Triple, Input, JA);
5293+
renderRemarksOptions(Args, CmdArgs, Triple, Input, Output, JA);
52875294

52885295
bool RewriteImports = Args.hasFlag(options::OPT_frewrite_imports,
52895296
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)