Index: docs/CommandGuide/clang.rst =================================================================== --- docs/CommandGuide/clang.rst +++ docs/CommandGuide/clang.rst @@ -408,6 +408,12 @@ Save intermediate compilation results. +.. option:: -save-stats, -save-stats=cwd, -save-stats=obj + + Save internal code generation (LLVM) statistics to a file in the current + directory (:option:`-save-stats`/:option:`-save-stats=cwd`) or the directory + of the output file (:option:`-save-state=obj`). + .. option:: -integrated-as, -no-integrated-as Used to enable and disable, respectively, the use of the integrated Index: include/clang/Basic/DiagnosticFrontendKinds.td =================================================================== --- include/clang/Basic/DiagnosticFrontendKinds.td +++ include/clang/Basic/DiagnosticFrontendKinds.td @@ -107,6 +107,8 @@ "unable to open CC_PRINT_HEADERS file: %0 (using stderr)">; def warn_fe_cc_log_diagnostics_failure : Warning< "unable to open CC_LOG_DIAGNOSTICS file: %0 (using stderr)">; +def warn_fe_unable_to_open_stats_file : Warning< + "unable to open statistics output file '%0': '%1'">; def err_fe_no_pch_in_dir : Error< "no suitable precompiled header file found in directory '%0'">; def err_fe_action_not_available : Error< Index: include/clang/Driver/CC1Options.td =================================================================== --- include/clang/Driver/CC1Options.td +++ include/clang/Driver/CC1Options.td @@ -509,6 +509,8 @@ def print_stats : Flag<["-"], "print-stats">, HelpText<"Print performance metrics and statistics">; +def stats_file : Joined<["-"], "stats-file=">, + HelpText<"Filename to write statistics to">; def fdump_record_layouts : Flag<["-"], "fdump-record-layouts">, HelpText<"Dump record layout information">; def fdump_record_layouts_simple : Flag<["-"], "fdump-record-layouts-simple">, Index: include/clang/Driver/Options.td =================================================================== --- include/clang/Driver/Options.td +++ include/clang/Driver/Options.td @@ -1876,6 +1876,11 @@ def save_temps : Flag<["-", "--"], "save-temps">, Flags<[DriverOption]>, Alias, AliasArgs<["cwd"]>, HelpText<"Save intermediate compilation results">; +def save_stats_EQ : Joined<["-", "--"], "save-stats=">, Flags<[DriverOption]>, + HelpText<"Save llvm statistics.">; +def save_stats : Flag<["-", "--"], "save-stats">, Flags<[DriverOption]>, + Alias, AliasArgs<["cwd"]>, + HelpText<"Save llvm statistics.">; def via_file_asm : Flag<["-", "--"], "via-file-asm">, InternalDebugOpt, HelpText<"Write assembly to file for input to assemble jobs">; def sectalign : MultiArg<["-"], "sectalign", 3>; Index: include/clang/Frontend/FrontendOptions.h =================================================================== --- include/clang/Frontend/FrontendOptions.h +++ include/clang/Frontend/FrontendOptions.h @@ -273,6 +273,9 @@ // included by this file. std::string FindPchSource; + /// Filename to write statistics to. + std::string StatsFile; + public: FrontendOptions() : DisableFree(false), RelocatablePCH(false), ShowHelp(false), Index: lib/Driver/Tools.cpp =================================================================== --- lib/Driver/Tools.cpp +++ lib/Driver/Tools.cpp @@ -6101,6 +6101,33 @@ A->claim(); } + // Setup statistics file output. + if (const Arg *A = Args.getLastArg(options::OPT_save_stats_EQ)) { + StringRef SaveStats = A->getValue(); + + SmallString<128> StatsFile; + bool DoSaveStats = false; + if (SaveStats == "obj") { + if (Output.isFilename()) { + StatsFile.assign(Output.getFilename()); + llvm::sys::path::remove_filename(StatsFile); + } + DoSaveStats = true; + } else if (SaveStats == "cwd") { + DoSaveStats = true; + } else { + D.Diag(diag::err_drv_invalid_value) << A->getAsString(Args) << SaveStats; + } + + if (DoSaveStats) { + StringRef BaseName = llvm::sys::path::filename(Input.getBaseInput()); + llvm::sys::path::append(StatsFile, BaseName); + llvm::sys::path::replace_extension(StatsFile, "stats"); + CmdArgs.push_back(Args.MakeArgString(Twine("-stats-file=") + + StatsFile)); + } + } + // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option // parser. Args.AddAllArgValues(CmdArgs, options::OPT_Xclang); Index: lib/Frontend/CompilerInstance.cpp =================================================================== --- lib/Frontend/CompilerInstance.cpp +++ lib/Frontend/CompilerInstance.cpp @@ -858,7 +858,7 @@ if (getFrontendOpts().ShowTimers) createFrontendTimer(); - if (getFrontendOpts().ShowStats) + if (getFrontendOpts().ShowStats || !getFrontendOpts().StatsFile.empty()) llvm::EnableStatistics(); for (const FrontendInputFile &FIF : getFrontendOpts().Inputs) { @@ -892,9 +892,24 @@ OS << " generated.\n"; } - if (getFrontendOpts().ShowStats && hasFileManager()) { - getFileManager().PrintStats(); - OS << "\n"; + if (getFrontendOpts().ShowStats) { + if (hasFileManager()) { + getFileManager().PrintStats(); + OS << '\n'; + } + llvm::PrintStatistics(OS); + } + StringRef StatsFile = getFrontendOpts().StatsFile; + if (!StatsFile.empty()) { + std::error_code EC; + auto StatS = llvm::make_unique(StatsFile, EC, + llvm::sys::fs::F_Text); + if (EC) { + getDiagnostics().Report(diag::warn_fe_unable_to_open_stats_file) + << StatsFile << EC.message(); + } else { + llvm::PrintStatisticsJSON(*StatS); + } } return !getDiagnostics().getClient()->getNumErrors(); Index: lib/Frontend/CompilerInvocation.cpp =================================================================== --- lib/Frontend/CompilerInvocation.cpp +++ lib/Frontend/CompilerInvocation.cpp @@ -1252,6 +1252,7 @@ Opts.AuxTriple = llvm::Triple::normalize(Args.getLastArgValue(OPT_aux_triple)); Opts.FindPchSource = Args.getLastArgValue(OPT_find_pch_source_EQ); + Opts.StatsFile = Args.getLastArgValue(OPT_stats_file); if (const Arg *A = Args.getLastArg(OPT_arcmt_check, OPT_arcmt_modify, Index: lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp =================================================================== --- lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp +++ lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp @@ -194,8 +194,10 @@ } ~AnalysisConsumer() override { - if (Opts->PrintStats) + if (Opts->PrintStats) { delete TUTotalTimer; + llvm::PrintStatistics(); + } } void DigestAnalyzerOptions() { Index: test/Driver/save-stats.c =================================================================== --- /dev/null +++ test/Driver/save-stats.c @@ -0,0 +1,20 @@ +// RUN: %clang -target x86_64-apple-darwin -save-stats %s -### 2>&1 | FileCheck %s +// RUN: %clang -target x86_64-apple-darwin -save-stats=cwd %s -### 2>&1 | FileCheck %s +// CHECK: "-stats-file=save-stats.stats" +// CHECK: "{{.*}}save-stats.c" + +// RUN: %clang -target x86_64-apple-darwin -S %s -### 2>&1 | FileCheck %s -check-prefix=NO-STATS +// NO-STATS-NO: -stats-file +// NO-STATS: "{{.*}}save-stats.c" +// NO-STATS-NO: -stats-file + +// RUN: %clang -target x86_64-apple-darwin -save-stats=obj -c -o obj/dir/save-stats.o %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-OBJ +// CHECK-OBJ: "-stats-file=obj/dir{{.}}save-stats.stats" +// CHECK-OBJ: "-o" "obj/dir{{.}}save-stats.o" + +// RUN: %clang -target x86_64-apple-darwin -save-stats=obj -c %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-OBJ-NOO +// CHECK-OBJ-NOO: "-stats-file=save-stats.stats" +// CHECK-OBJ-NOO: "-o" "save-stats.o" + +// RUN: %clang -target x86_64-apple-darwin -save-stats=bla -c %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-INVALID +// CHECK-INVALID: invalid value 'bla' in '-save-stats=bla' Index: test/Frontend/stats-file.c =================================================================== --- /dev/null +++ test/Frontend/stats-file.c @@ -0,0 +1,8 @@ +// RUN: %clang_cc1 -emit-llvm -o /dev/null -stats-file=%t %s +// RUN: FileCheck -input-file=%t %s +// CHECK: { +// ... here come some json values ... +// CHECK: } + +// RUN: %clang_cc1 -emit-llvm -o %t -stats-file=%S/doesnotexist/bla %s 2>&1 | FileCheck -check-prefix=OUTPUTFAIL %s +// OUTPUTFAIL: warning: unable to open statistic output file '{{.*}}{{[/\\]}}doesnotexist{{[/\\]}}bla': '{{[Nn]}}o such file or directory' Index: test/Misc/warning-flags.c =================================================================== --- test/Misc/warning-flags.c +++ test/Misc/warning-flags.c @@ -18,7 +18,7 @@ The list of warnings below should NEVER grow. It should gradually shrink to 0. -CHECK: Warnings without flags (84): +CHECK: Warnings without flags (85): CHECK-NEXT: ext_excess_initializers CHECK-NEXT: ext_excess_initializers_in_char_array_initializer CHECK-NEXT: ext_expected_semi_decl_list @@ -66,6 +66,7 @@ CHECK-NEXT: warn_fe_cc_log_diagnostics_failure CHECK-NEXT: warn_fe_cc_print_header_failure CHECK-NEXT: warn_fe_macro_contains_embedded_newline +CHECK-NEXT: warn_fe_unable_to_open_stats_file CHECK-NEXT: warn_file_asm_volatile CHECK-NEXT: warn_ignoring_ftabstop_value CHECK-NEXT: warn_implements_nscopying