diff options
| author | Hal Finkel <hfinkel@anl.gov> | 2016-10-11 00:26:09 +0000 |
|---|---|---|
| committer | Hal Finkel <hfinkel@anl.gov> | 2016-10-11 00:26:09 +0000 |
| commit | 8f96e82cb8ccfee75e56328159a84c0c7002f83f (patch) | |
| tree | 7c98131814a67ebc206de0f59963f38ffb8780bf | |
| parent | 4043ca7394046be5c47740e9c5a83382ee734419 (diff) | |
| download | bcm5719-llvm-8f96e82cb8ccfee75e56328159a84c0c7002f83f.tar.gz bcm5719-llvm-8f96e82cb8ccfee75e56328159a84c0c7002f83f.zip | |
Add an option to save the backend-produced YAML optimization record to a file
The backend now has the capability to save information from optimizations, the
same information that can be used to generate optimization diagnostics but in
machine-consumable form, into an output file. This can be enabled when using
opt (see r282539), and this change enables it when using clang. The idea is
that other tools will be able to consume these files, and perhaps in
combination with the original source code, produce various kinds of
optimization reports for users (and for compiler developers).
We now have at-least two tools that can consume these files:
* tools/llvm-opt-report
* utils/opt-viewer
Using the flag -fsave-optimization-record will cause the YAML file to be
generated; the file name will be based on the output file name (if we're using
-c or -S and have an output name), or the input file name. When we're using
CUDA, or some other offloading mechanism, separate files are generated for each
backend target. The output file name can be specified by the user using
-foptimization-record-file=filename.
Differential Revision: https://reviews.llvm.org/D25225
llvm-svn: 283834
| -rw-r--r-- | clang/include/clang/Driver/CC1Options.td | 3 | ||||
| -rw-r--r-- | clang/include/clang/Driver/Options.td | 9 | ||||
| -rw-r--r-- | clang/include/clang/Frontend/CodeGenOptions.h | 4 | ||||
| -rw-r--r-- | clang/lib/CodeGen/CodeGenAction.cpp | 23 | ||||
| -rw-r--r-- | clang/lib/Driver/Tools.cpp | 33 | ||||
| -rw-r--r-- | clang/lib/Frontend/CompilerInvocation.cpp | 4 | ||||
| -rw-r--r-- | clang/test/CodeGen/Inputs/opt-record.proftext | 26 | ||||
| -rw-r--r-- | clang/test/CodeGen/opt-record.c | 33 | ||||
| -rw-r--r-- | clang/test/Driver/opt-record.c | 18 |
9 files changed, 153 insertions, 0 deletions
diff --git a/clang/include/clang/Driver/CC1Options.td b/clang/include/clang/Driver/CC1Options.td index 73ab5e68e93..cac33b69c26 100644 --- a/clang/include/clang/Driver/CC1Options.td +++ b/clang/include/clang/Driver/CC1Options.td @@ -507,6 +507,9 @@ def arcmt_modify : Flag<["-"], "arcmt-modify">, def arcmt_migrate : Flag<["-"], "arcmt-migrate">, HelpText<"Apply modifications and produces temporary files that conform to ARC">; +def opt_record_file : Separate<["-"], "opt-record-file">, + HelpText<"File name to use for YAML optimization record output">; + def print_stats : Flag<["-"], "print-stats">, HelpText<"Print performance metrics and statistics">; def stats_file : Joined<["-"], "stats-file=">, diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index ee558f0e7f0..56dfc6de266 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -1192,6 +1192,15 @@ def ftemplate_backtrace_limit_EQ : Joined<["-"], "ftemplate-backtrace-limit=">, Group<f_Group>; def foperator_arrow_depth_EQ : Joined<["-"], "foperator-arrow-depth=">, Group<f_Group>; + +def fsave_optimization_record : Flag<["-"], "fsave-optimization-record">, + Group<f_Group>, HelpText<"Generate a YAML optimization record file">; +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">; + def ftest_coverage : Flag<["-"], "ftest-coverage">, Group<f_Group>; def fvectorize : Flag<["-"], "fvectorize">, Group<f_Group>, HelpText<"Enable the loop vectorization passes">; diff --git a/clang/include/clang/Frontend/CodeGenOptions.h b/clang/include/clang/Frontend/CodeGenOptions.h index 0839bc75e4b..22d2d4dd6dc 100644 --- a/clang/include/clang/Frontend/CodeGenOptions.h +++ b/clang/include/clang/Frontend/CodeGenOptions.h @@ -181,6 +181,10 @@ public: /// object file. std::vector<std::string> CudaGpuBinaryFileNames; + /// The name of the file to which the backend should save YAML optimization + /// records. + std::string OptRecordFile; + /// Regular expression to select optimizations for which we should enable /// optimization remarks. Transformation passes whose name matches this /// expression (and support this feature), will emit a diagnostic diff --git a/clang/lib/CodeGen/CodeGenAction.cpp b/clang/lib/CodeGen/CodeGenAction.cpp index 15e4f4883e8..657af24e739 100644 --- a/clang/lib/CodeGen/CodeGenAction.cpp +++ b/clang/lib/CodeGen/CodeGenAction.cpp @@ -33,6 +33,8 @@ #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/SourceMgr.h" #include "llvm/Support/Timer.h" +#include "llvm/Support/ToolOutputFile.h" +#include "llvm/Support/YAMLTraits.h" #include <memory> using namespace clang; using namespace llvm; @@ -181,6 +183,24 @@ namespace clang { Ctx.setDiagnosticHandler(DiagnosticHandler, this); Ctx.setDiagnosticHotnessRequested(CodeGenOpts.DiagnosticsWithHotness); + std::unique_ptr<llvm::tool_output_file> OptRecordFile; + if (!CodeGenOpts.OptRecordFile.empty()) { + std::error_code EC; + OptRecordFile = + llvm::make_unique<llvm::tool_output_file>(CodeGenOpts.OptRecordFile, + EC, sys::fs::F_None); + if (EC) { + Diags.Report(diag::err_cannot_open_file) << + CodeGenOpts.OptRecordFile << EC.message(); + return; + } + + Ctx.setDiagnosticsOutputFile(new yaml::Output(OptRecordFile->os())); + + if (CodeGenOpts.getProfileUse() != CodeGenOptions::ProfileNone) + Ctx.setDiagnosticHotnessRequested(true); + } + // Link LinkModule into this module if present, preserving its validity. for (auto &I : LinkModules) { unsigned LinkFlags = I.first; @@ -198,6 +218,9 @@ namespace clang { Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext); Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext); + + if (OptRecordFile) + OptRecordFile->keep(); } void HandleTagDeclDefinition(TagDecl *D) override { diff --git a/clang/lib/Driver/Tools.cpp b/clang/lib/Driver/Tools.cpp index ee1aa6d9d7c..1e5dbb54bf4 100644 --- a/clang/lib/Driver/Tools.cpp +++ b/clang/lib/Driver/Tools.cpp @@ -6080,6 +6080,39 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-fno-math-builtin"); } + if (Args.hasFlag(options::OPT_fsave_optimization_record, + options::OPT_fno_save_optimization_record, false)) { + CmdArgs.push_back("-opt-record-file"); + + const Arg *A = Args.getLastArg(options::OPT_foptimization_record_file_EQ); + if (A) { + CmdArgs.push_back(A->getValue()); + } else { + SmallString<128> F; + if (Output.isFilename() && (Args.hasArg(options::OPT_c) || + Args.hasArg(options::OPT_S))) { + F = Output.getFilename(); + } else { + // Use the compilation directory. + F = llvm::sys::path::stem(Input.getBaseInput()); + + // If we're compiling for an offload architecture (i.e. a CUDA device), + // we need to make the file name for the device compilation different + // from the host compilation. + if (!JA.isDeviceOffloading(Action::OFK_None) && + !JA.isDeviceOffloading(Action::OFK_Host)) { + llvm::sys::path::replace_extension(F, ""); + F += JA.getOffloadingFileNamePrefix(Triple.normalize()); + F += "-"; + F += JA.getOffloadingArch(); + } + } + + llvm::sys::path::replace_extension(F, "opt.yaml"); + CmdArgs.push_back(Args.MakeArgString(F)); + } + } + // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM. // // FIXME: Now that PR4941 has been fixed this can be enabled. diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index be01114c2f2..39588e8af86 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -826,6 +826,10 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK, Opts.LinkerOptions = Args.getAllArgValues(OPT_linker_option); bool NeedLocTracking = false; + Opts.OptRecordFile = Args.getLastArgValue(OPT_opt_record_file); + if (!Opts.OptRecordFile.empty()) + NeedLocTracking = true; + if (Arg *A = Args.getLastArg(OPT_Rpass_EQ)) { Opts.OptimizationRemarkPattern = GenerateOptimizationRemarkRegex(Diags, Args, A); diff --git a/clang/test/CodeGen/Inputs/opt-record.proftext b/clang/test/CodeGen/Inputs/opt-record.proftext new file mode 100644 index 00000000000..cf2530c6be5 --- /dev/null +++ b/clang/test/CodeGen/Inputs/opt-record.proftext @@ -0,0 +1,26 @@ +foo +# Func Hash: +0 +# Num Counters: +1 +# Counter Values: +30 + +bar +# Func Hash: +0 +# Num Counters: +1 +# Counter Values: +30 + +Test +# Func Hash: +269 +# Num Counters: +3 +# Counter Values: +1 +30 +15 + diff --git a/clang/test/CodeGen/opt-record.c b/clang/test/CodeGen/opt-record.c new file mode 100644 index 00000000000..3bc3c41f784 --- /dev/null +++ b/clang/test/CodeGen/opt-record.c @@ -0,0 +1,33 @@ +// RUN: %clang_cc1 -O3 -triple x86_64-unknown-linux-gnu -target-cpu x86-64 %s -o %t -dwarf-column-info -opt-record-file %t.yaml -emit-obj +// RUN: cat %t.yaml | FileCheck %s +// RUN: llvm-profdata merge %S/Inputs/opt-record.proftext -o %t.profdata +// RUN: %clang_cc1 -O3 -triple x86_64-unknown-linux-gnu -target-cpu x86-64 -fprofile-instrument-use-path=%t.profdata %s -o %t -dwarf-column-info -opt-record-file %t.yaml -emit-obj +// RUN: cat %t.yaml | FileCheck -check-prefix=CHECK -check-prefix=CHECK-PGO %s +// REQUIRES: x86-registered-target + +void bar(); +void foo() { bar(); } + +void Test(int *res, int *c, int *d, int *p, int n) { + int i; + +#pragma clang loop vectorize(assume_safety) + for (i = 0; i < 1600; i++) { + res[i] = (p[i] == 0) ? res[i] : res[i] + d[i]; + } +} + +// CHECK: --- !Missed +// CHECK: Pass: inline +// CHECK: Name: NoDefinition +// CHECK: DebugLoc: +// CHECK: Function: foo +// CHECK-PGO: Hotness: + +// CHECK: --- !Passed +// CHECK: Pass: loop-vectorize +// CHECK: Name: Vectorized +// CHECK: DebugLoc: +// CHECK: Function: Test +// CHECK-PGO: Hotness: + diff --git a/clang/test/Driver/opt-record.c b/clang/test/Driver/opt-record.c new file mode 100644 index 00000000000..c7db6533c4e --- /dev/null +++ b/clang/test/Driver/opt-record.c @@ -0,0 +1,18 @@ +// RUN: %clang -### -S -o FOO -fsave-optimization-record %s 2>&1 | FileCheck %s +// RUN: %clang -### -c -o FOO -fsave-optimization-record %s 2>&1 | FileCheck %s +// RUN: %clang -### -c -fsave-optimization-record %s 2>&1 | FileCheck %s -check-prefix=CHECK-NO-O +// RUN: %clang -### -fsave-optimization-record %s 2>&1 | FileCheck %s -check-prefix=CHECK-NO-O +// RUN: %clang -### -S -fsave-optimization-record -x cuda -nocudainc -nocudalib %s 2>&1 | FileCheck %s -check-prefix=CHECK-NO-O -check-prefix=CHECK-CUDA-DEV +// RUN: %clang -### -fsave-optimization-record -x cuda -nocudainc -nocudalib %s 2>&1 | FileCheck %s -check-prefix=CHECK-NO-O -check-prefix=CHECK-CUDA-DEV +// RUN: %clang -### -S -o FOO -fsave-optimization-record -foptimization-record-file=BAR.txt %s 2>&1 | FileCheck %s -check-prefix=CHECK-EQ + +// CHECK: "-cc1" +// CHECK: "-opt-record-file" "FOO.opt.yaml" + +// CHECK-NO-O: "-cc1" +// CHECK-NO-O-DAG: "-opt-record-file" "opt-record.opt.yaml" +// CHECK-CUDA-DEV-DAG: "-opt-record-file" "opt-record-device-cuda-nvptx64-nvidia-cuda-sm_20.opt.yaml" + +// CHECK-EQ: "-cc1" +// CHECK-EQ: "-opt-record-file" "BAR.txt" + |

