fhahn created this revision. fhahn added reviewers: anemet, tejohnson, thegameg. Herald added subscribers: cfe-commits, dang, dexonsmith, steven_wu, hiraditya, inglorion, mehdi_amini. Herald added projects: clang, LLVM.
Gold and ld on Linux already support saving stats, but the infrastructure is missing on MacOS. Unfortunately it seems like the configuration from lib/LTO/LTO.cpp is not used. This patch adds a new LTOStatsFile option and adds plumbing in Clang to use it on MacOS, similar to the way remarks are handled. Currnetly the handling of LTO flags seems quite spread out, with a bunch of duplication. But I am not sure if there is an easy way to improve that? Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D60516 Files: clang/lib/Driver/ToolChains/Darwin.cpp llvm/include/llvm/LTO/LTO.h llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h llvm/lib/LTO/LTO.cpp llvm/lib/LTO/LTOCodeGenerator.cpp
Index: llvm/lib/LTO/LTOCodeGenerator.cpp =================================================================== --- llvm/lib/LTO/LTOCodeGenerator.cpp +++ llvm/lib/LTO/LTOCodeGenerator.cpp @@ -95,6 +95,11 @@ "lto-pass-remarks-with-hotness", cl::desc("With PGO, include profile count in optimization remarks"), cl::Hidden); + +cl::opt<std::string> LTOStatsFile( + "lto-stats-file", + cl::desc("With PGO, include profile count in optimization remarks"), + cl::Hidden); } LTOCodeGenerator::LTOCodeGenerator(LLVMContext &Context) @@ -518,6 +523,14 @@ } DiagnosticOutputFile = std::move(*DiagFileOrErr); + // Setup output file to emit statistics. + auto StatsFileOrErr = lto::setupStatsFile(LTOStatsFile); + if (!StatsFileOrErr) { + errs() << "Error: " << toString(StatsFileOrErr.takeError()) << "\n"; + report_fatal_error("Can't get an output file for the statistics"); + } + StatsFile = std::move(StatsFileOrErr.get()); + // We always run the verifier once on the merged module, the `DisableVerify` // parameter only applies to subsequent verify. verifyMergedModuleOnce(); @@ -585,8 +598,12 @@ ShouldRestoreGlobalsLinkage); // If statistics were requested, print them out after codegen. - if (llvm::AreStatisticsEnabled()) + if (llvm::AreStatisticsEnabled() && !StatsFile) llvm::PrintStatistics(); + + if (StatsFile) + PrintStatisticsJSON(StatsFile->os()); + reportAndResetTimings(); finishOptimizationRemarks(); Index: llvm/lib/LTO/LTO.cpp =================================================================== --- llvm/lib/LTO/LTO.cpp +++ llvm/lib/LTO/LTO.cpp @@ -880,16 +880,10 @@ isPrevailing, Conf.OptLevel > 0); // Setup output file to emit statistics. - std::unique_ptr<ToolOutputFile> StatsFile = nullptr; - if (!Conf.StatsFile.empty()) { - EnableStatistics(false); - std::error_code EC; - StatsFile = - llvm::make_unique<ToolOutputFile>(Conf.StatsFile, EC, sys::fs::F_None); - if (EC) - return errorCodeToError(EC); - StatsFile->keep(); - } + auto StatsFileOrErr = setupStatsFile(Conf.StatsFile); + if (!StatsFileOrErr) + return StatsFileOrErr.takeError(); + std::unique_ptr<ToolOutputFile> StatsFile = std::move(StatsFileOrErr.get()); // Finalize linking of regular LTO modules containing summaries now that // we have computed liveness information. @@ -1338,3 +1332,20 @@ DiagnosticFile->keep(); return std::move(DiagnosticFile); } + +Expected<std::unique_ptr<ToolOutputFile>> +lto::setupStatsFile(StringRef StatsFilename) { + // Setup output file to emit statistics. + if (StatsFilename.empty()) + return nullptr; + + llvm::EnableStatistics(false); + std::error_code EC; + auto StatsFile = + llvm::make_unique<ToolOutputFile>(StatsFilename, EC, sys::fs::F_None); + if (EC) + return errorCodeToError(EC); + + StatsFile->keep(); + return std::move(StatsFile); +} Index: llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h =================================================================== --- llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h +++ llvm/include/llvm/LTO/legacy/LTOCodeGenerator.h @@ -241,6 +241,7 @@ TargetMachine::CodeGenFileType FileType = TargetMachine::CGFT_ObjectFile; std::unique_ptr<ToolOutputFile> DiagnosticOutputFile; bool Freestanding = false; + std::unique_ptr<ToolOutputFile> StatsFile = nullptr; }; } #endif Index: llvm/include/llvm/LTO/LTO.h =================================================================== --- llvm/include/llvm/LTO/LTO.h +++ llvm/include/llvm/LTO/LTO.h @@ -87,6 +87,10 @@ StringRef LTORemarksPasses, bool LTOPassRemarksWithHotness, int Count = -1); +/// Setups the output file for saving statistics. +Expected<std::unique_ptr<ToolOutputFile>> +setupStatsFile(StringRef StatsFilename); + class LTO; struct SymbolResolution; class ThinBackendProc; Index: clang/lib/Driver/ToolChains/Darwin.cpp =================================================================== --- clang/lib/Driver/ToolChains/Darwin.cpp +++ clang/lib/Driver/ToolChains/Darwin.cpp @@ -514,6 +514,14 @@ } } + // Setup statistics file output. + SmallString<128> StatsFile = + getStatsFileName(Args, Output, Inputs[0], getToolChain().getDriver()); + if (!StatsFile.empty()) { + CmdArgs.push_back("-mllvm"); + CmdArgs.push_back(Args.MakeArgString("-lto-stats-file=" + StatsFile.str())); + } + // It seems that the 'e' option is completely ignored for dynamic executables // (the default), and with static executables, the last one wins, as expected. Args.AddAllArgs(CmdArgs, {options::OPT_d_Flag, options::OPT_s, options::OPT_t,
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits