Skip to content

[Remarks][Driver] Place temporary remark files next to temporary object files #502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions clang/docs/ClangCommandLineReference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1697,9 +1697,14 @@ Emit OpenMP code only for SIMD-based constructs.

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

.. option:: -foptimization-record-file=<arg>
.. option:: -foptimization-record-file=<file>

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.
Implies -fsave-optimization-record. On Darwin platforms, this
cannot be used with multiple -arch <arch> options.

.. option:: -foptimization-record-passes=<regex>

Only include passes which match a specified regular expression in the generated optimization record (by default, include all passes)

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

Expand Down Expand Up @@ -1832,6 +1837,12 @@ Turn on loop reroller

Generate a YAML optimization record file

.. program:: clang1
.. option:: -fsave-optimization-record=<format>
.. program:: clang

Generate an optimization record file in a specific format.

.. option:: -fseh-exceptions

Use SEH style exceptions
Expand Down
69 changes: 57 additions & 12 deletions clang/docs/UsersManual.rst
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,10 @@ output format of the diagnostics that it generates.

.. _opt_fsave-optimization-record:

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

Write optimization remarks to a separate file.
Enable optimization remarks during compilation and write them to a separate
file.

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

The output file is controlled by :ref:`-foptimization-record-file <opt_foptimization-record-file>`.

In the absence of an explicit output file, the file is chosen using the
following scheme:

``<base>.opt.<format>``

where ``<base>`` is based on the output file of the compilation (whether
it's explicitly specified through `-o` or not) when used with `-c` or `-S`.
For example:

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

* ``clang -fsave-optimization-record -c in.c `` will generate
``in.opt.yaml``

When targeting (Thin)LTO, the base is derived from the output filename, and
the extension is not dropped.

When targeting ThinLTO, the following scheme is used:

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

Darwin-only: 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 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.

For example:

``clang -fsave-optimization-record=bitstream in.c -o out`` will generate

* ``/var/folders/43/9y164hh52tv_2nrdxrj31nyw0000gn/T/a-9be59b.o``

* ``/var/folders/43/9y164hh52tv_2nrdxrj31nyw0000gn/T/a-9be59b.opt.bitstream``

* ``out``

* ``out.dSYM/Contents/Resources/Remarks/out``

Darwin-only: compiling for multiple architectures will use the following
scheme:

``<base>-<arch>.opt.<format>``

Note that this is incompatible with passing the
:ref:`-foptimization-record-file <opt_foptimization-record-file>` option.

.. _opt_foptimization-record-file:

**-foptimization-record-file**
Control the file to which optimization reports are written.

When optimization reports are being output (see
:ref:`-fsave-optimization-record <opt_fsave-optimization-record>`), this
option controls the file to which those reports are written.
Control the file to which optimization reports are written. This implies
:ref:`-fsave-optimization-record <opt_fsave-optimization-record>`.

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

.. _opt_foptimization-record-passes:

Expand Down
10 changes: 6 additions & 4 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -1764,16 +1764,18 @@ def foperator_arrow_depth_EQ : Joined<["-"], "foperator-arrow-depth=">,
def fsave_optimization_record : Flag<["-"], "fsave-optimization-record">,
Group<f_Group>, HelpText<"Generate a YAML optimization record file">;
def fsave_optimization_record_EQ : Joined<["-"], "fsave-optimization-record=">,
Group<f_Group>, HelpText<"Generate an optimization record file in a specific format (default: YAML)">;
Group<f_Group>, HelpText<"Generate an optimization record file in a specific format">,
MetaVarName<"<format>">;
def fno_save_optimization_record : Flag<["-"], "fno-save-optimization-record">,
Group<f_Group>, Flags<[NoArgumentUnused]>;
def foptimization_record_file_EQ : Joined<["-"], "foptimization-record-file=">,
Group<f_Group>,
HelpText<"Specify the file name of any generated YAML optimization record">;
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.">,
MetaVarName<"<file>">;
def foptimization_record_passes_EQ : Joined<["-"], "foptimization-record-passes=">,
Group<f_Group>,
HelpText<"Only include passes which match a specified regular expression in the generated optimization record (by default, include all passes)">;

HelpText<"Only include passes which match a specified regular expression in the generated optimization record (by default, include all passes)">,
MetaVarName<"<regex>">;

def ftest_coverage : Flag<["-"], "ftest-coverage">, Group<f_Group>;
def fvectorize : Flag<["-"], "fvectorize">, Group<f_Group>,
Expand Down
29 changes: 18 additions & 11 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,12 @@ static bool checkRemarksOptions(const Driver &D, const ArgList &Args,

static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs,
const llvm::Triple &Triple,
const InputInfo &Input, const JobAction &JA) {
const InputInfo &Input,
const InputInfo &Output, const JobAction &JA) {
StringRef Format = "yaml";
if (const Arg *A = Args.getLastArg(options::OPT_fsave_optimization_record_EQ))
Format = A->getValue();

CmdArgs.push_back("-opt-record-file");

const Arg *A = Args.getLastArg(options::OPT_foptimization_record_file_EQ);
Expand All @@ -1412,11 +1417,17 @@ static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs,
bool hasMultipleArchs =
Triple.isOSDarwin() && // Only supported on Darwin platforms.
Args.getAllArgValues(options::OPT_arch).size() > 1;

SmallString<128> F;

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

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

std::string Extension = "opt.";
if (const Arg *A =
Args.getLastArg(options::OPT_fsave_optimization_record_EQ))
Extension += A->getValue();
else
Extension += "yaml";
SmallString<32> Extension;
Extension += "opt.";
Extension += Format;

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

if (const Arg *A =
Args.getLastArg(options::OPT_fsave_optimization_record_EQ)) {
if (!Format.empty()) {
CmdArgs.push_back("-opt-record-format");
CmdArgs.push_back(A->getValue());
CmdArgs.push_back(Format.data());
}
}

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

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

bool RewriteImports = Args.hasFlag(options::OPT_frewrite_imports,
options::OPT_fno_rewrite_imports, false);
Expand Down
7 changes: 7 additions & 0 deletions clang/test/Driver/darwin-opt-record.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// 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
// RUN: %clang -target x86_64-apple-darwin10 -### -o FOO -fsave-optimization-record %s 2>&1 | FileCheck %s --check-prefix=CHECK-DSYMUTIL-NO-G
// RUN: %clang -target x86_64-apple-darwin10 -### -o FOO -g0 -fsave-optimization-record %s 2>&1 | FileCheck %s --check-prefix=CHECK-DSYMUTIL-G0
// 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
//
// CHECK-MULTIPLE-ARCH: "-cc1"
// CHECK-MULTIPLE-ARCH: "-opt-record-file" "FOO-x86_64.opt.yaml"
Expand All @@ -22,3 +23,9 @@
// CHECK-DSYMUTIL-G0: "-cc1"
// CHECK-DSYMUTIL-G0: ld
// CHECK-DSYMUTIL-G0: dsymutil
//
// CHECK-NO-G-PATH-BITSTREAM: "-cc1"
// CHECK-NO-G-PATH-BITSTREAM: "-opt-record-file" "[[OUTPATH:.*]].opt.bitstream"
// CHECK-NO-G-PATH-BITSTREAM: "-o" "[[OUTPATH:.*]].o"
// CHECK-NO-G-PATH-BITSTREAM: ld
// CHECK-NO-G-PATH-BITSTREAM: dsymutil