Skip to content

Commit

Permalink
[Remarks][Driver] Use different remark files when targeting multiple …
Browse files Browse the repository at this point in the history
…architectures

When the driver is targeting multiple architectures at once, for things
like Universal Mach-Os, we need to emit different remark files for each
cc1 invocation to avoid overwriting the files from a different
invocation.

For example:

$ clang -c -o foo.o -fsave-optimization-record -arch x86_64 -arch x86_64h

will create two remark files:

* foo-x86_64.opt.yaml
* foo-x86_64h.opt.yaml
  • Loading branch information
francisvm committed Nov 18, 2019
1 parent 1689ad2 commit b4e2b11
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
18 changes: 18 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Expand Up @@ -5403,6 +5403,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
if (A) {
CmdArgs.push_back(A->getValue());
} else {
bool hasMultipleArchs =
Args.getAllArgValues(options::OPT_arch).size() > 1;
SmallString<128> F;

if (Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) {
Expand All @@ -5427,6 +5429,22 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
}
}

// If we're having more than one "-arch", we should name the files
// differently so that every cc1 invocation writes to a different file.
// We're doing that by appending "-<arch>" with "<arch>" being the arch
// name from the triple.
if (hasMultipleArchs) {
// First, remember the extension.
SmallString<64> OldExtension = llvm::sys::path::extension(F);
// then, remove it.
llvm::sys::path::replace_extension(F, "");
// attach -<arch> to it.
F += "-";
F += Triple.getArchName();
// put back the extension.
llvm::sys::path::replace_extension(F, OldExtension);
}

std::string Extension = "opt.";
if (const Arg *A =
Args.getLastArg(options::OPT_fsave_optimization_record_EQ))
Expand Down
6 changes: 6 additions & 0 deletions clang/test/Driver/opt-record.c
Expand Up @@ -18,6 +18,7 @@
// RUN: %clang -### -S -o FOO -fsave-optimization-record -fsave-optimization-record=some-format %s 2>&1 | FileCheck %s -check-prefix=CHECK-EQ-FORMAT
// RUN: %clang -### -S -o FOO -fsave-optimization-record=some-format %s 2>&1 | FileCheck %s -check-prefix=CHECK-EQ-FORMAT
// RUN: %clang -### -S -o FOO -fsave-optimization-record=some-format -fno-save-optimization-record %s 2>&1 | FileCheck %s --check-prefix=CHECK-FOPT-DISABLE-FORMAT
// RUN: %clang -### -S -o FOO -fsave-optimization-record -arch x86_64 -arch x86_64h %s 2>&1 | FileCheck %s --check-prefix=CHECK-MULTIPLE-ARCH
//
// CHECK: "-cc1"
// CHECK: "-opt-record-file" "FOO.opt.yaml"
Expand All @@ -41,3 +42,8 @@
// CHECK-EQ-FORMAT: "-opt-record-format" "some-format"

// CHECK-FOPT-DISABLE-FORMAT-NOT: "-fno-save-optimization-record"

// CHECK-MULTIPLE-ARCH: "-cc1"
// CHECK-MULTIPLE-ARCH: "-opt-record-file" "FOO-x86_64.opt.yaml"
// CHECK-MULTIPLE-ARCH: "-cc1"
// CHECK-MULTIPLE-ARCH: "-opt-record-file" "FOO-x86_64h.opt.yaml"

0 comments on commit b4e2b11

Please sign in to comment.