Skip to content

Commit 48072d3

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. (cherry picked from commit 07b8f8e) rdar://57984755
1 parent cc101b1 commit 48072d3

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
@@ -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)