-
Notifications
You must be signed in to change notification settings - Fork 18.1k
[tools][llc] Add --save-stats option
#163967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+92
−4
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
|
|
||
| ; RUN: rm -f %t.stats && llc -mtriple=arm64-apple-macosx --save-stats=obj -o %t.s %s && test -f %t.stats | ||
| ; RUN: rm -f %{t:stem}.tmp.stats && llc -mtriple=arm64-apple-macosx --save-stats=cwd -o %t.s %s && test -f %{t:stem}.tmp.stats | ||
| ; RUN: rm -f %{t:stem}.tmp.stats && llc -mtriple=arm64-apple-macosx --save-stats -o %t.s %s && test -f %{t:stem}.tmp.stats | ||
| ; RUN: not llc -mtriple=arm64-apple-macosx --save-stats=invalid -o %t.s %s 2>&1 | FileCheck %s --check-prefix=INVALID_ARG | ||
|
|
||
| ; INVALID_ARG: llc: error: Invalid --save-stats value: invalid, must be empty, 'cwd' or 'obj' | ||
| define i32 @func() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be some CHECK lines in here for at least a little bit of the structure of the stats file. |
||
| ret i32 0 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |||||||
| #include "NewPMDriver.h" | ||||||||
| #include "llvm/ADT/STLExtras.h" | ||||||||
| #include "llvm/ADT/ScopeExit.h" | ||||||||
| #include "llvm/ADT/Statistic.h" | ||||||||
| #include "llvm/Analysis/TargetLibraryInfo.h" | ||||||||
| #include "llvm/CodeGen/CommandFlags.h" | ||||||||
| #include "llvm/CodeGen/LinkAllAsmWriterComponents.h" | ||||||||
|
|
@@ -45,6 +46,7 @@ | |||||||
| #include "llvm/Support/FormattedStream.h" | ||||||||
| #include "llvm/Support/InitLLVM.h" | ||||||||
| #include "llvm/Support/PGOOptions.h" | ||||||||
| #include "llvm/Support/Path.h" | ||||||||
| #include "llvm/Support/PluginLoader.h" | ||||||||
| #include "llvm/Support/SourceMgr.h" | ||||||||
| #include "llvm/Support/TargetSelect.h" | ||||||||
|
|
@@ -57,6 +59,7 @@ | |||||||
| #include "llvm/TargetParser/SubtargetFeature.h" | ||||||||
| #include "llvm/TargetParser/Triple.h" | ||||||||
| #include "llvm/Transforms/Utils/Cloning.h" | ||||||||
| #include <cassert> | ||||||||
| #include <memory> | ||||||||
| #include <optional> | ||||||||
| using namespace llvm; | ||||||||
|
|
@@ -203,6 +206,13 @@ static cl::opt<std::string> RemarksFormat( | |||||||
| cl::desc("The format used for serializing remarks (default: YAML)"), | ||||||||
| cl::value_desc("format"), cl::init("yaml")); | ||||||||
|
|
||||||||
| static cl::opt<std::string> SaveStats( | ||||||||
| "save-stats", | ||||||||
| cl::desc("Save LLVM statistics to a file in the current directory" | ||||||||
| "(`-save-stats`/`-save-stats=cwd`) or the directory of the output" | ||||||||
| "file (`-save-stats=obj`). (default: cwd)"), | ||||||||
| cl::init(""), cl::ValueOptional); | ||||||||
|
|
||||||||
| static cl::opt<bool> EnableNewPassManager( | ||||||||
| "enable-new-pm", cl::desc("Enable the new pass manager"), cl::init(false)); | ||||||||
|
|
||||||||
|
|
@@ -276,7 +286,8 @@ static void setPGOOptions(TargetMachine &TM) { | |||||||
| TM.setPGOOption(PGOOpt); | ||||||||
| } | ||||||||
|
|
||||||||
| static int compileModule(char **, LLVMContext &); | ||||||||
| static int compileModule(char **argv, LLVMContext &Context, | ||||||||
| std::string &OutputFilename); | ||||||||
|
|
||||||||
| [[noreturn]] static void reportError(Twine Msg, StringRef Filename = "") { | ||||||||
| SmallString<256> Prefix; | ||||||||
|
|
@@ -355,6 +366,50 @@ static std::unique_ptr<ToolOutputFile> GetOutputStream(const char *TargetName, | |||||||
| return FDOut; | ||||||||
| } | ||||||||
|
|
||||||||
| static int MaybeEnableStats() { | ||||||||
| if (SaveStats.getNumOccurrences() > 0) { | ||||||||
|
jroelofs marked this conversation as resolved.
Outdated
|
||||||||
| if (SaveStats.empty() || SaveStats == "cwd" || SaveStats == "obj") { | ||||||||
| llvm::EnableStatistics(false); | ||||||||
| } else { | ||||||||
|
jroelofs marked this conversation as resolved.
Outdated
|
||||||||
| WithColor::error(errs(), "llc") | ||||||||
| << "Invalid --save-stats value: " << SaveStats | ||||||||
| << ", must be empty, 'cwd' or 'obj'\n"; | ||||||||
|
jroelofs marked this conversation as resolved.
Outdated
|
||||||||
| return 1; | ||||||||
| } | ||||||||
| } | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
| return 0; | ||||||||
| } | ||||||||
|
|
||||||||
| static int MaybeSaveStats(std::string &&OutputFilename) { | ||||||||
| if (SaveStats.getNumOccurrences() > 0) { | ||||||||
|
jroelofs marked this conversation as resolved.
Outdated
|
||||||||
| SmallString<128> StatsFilename; | ||||||||
| if (SaveStats == "obj") { | ||||||||
| StatsFilename = OutputFilename; | ||||||||
| llvm::sys::path::remove_filename(StatsFilename); | ||||||||
| } else { | ||||||||
| assert((SaveStats.empty() || SaveStats == "cwd") && | ||||||||
| "Should have been a valid --save-stats value"); | ||||||||
| } | ||||||||
|
|
||||||||
| auto BaseName = llvm::sys::path::filename(OutputFilename); | ||||||||
| llvm::sys::path::append(StatsFilename, BaseName); | ||||||||
| llvm::sys::path::replace_extension(StatsFilename, "stats"); | ||||||||
|
|
||||||||
| auto FileFlags = llvm::sys::fs::OF_TextWithCRLF; | ||||||||
| std::error_code EC; | ||||||||
| auto StatsOS = | ||||||||
| std::make_unique<llvm::raw_fd_ostream>(StatsFilename, EC, FileFlags); | ||||||||
| if (EC) { | ||||||||
| WithColor::error(errs(), "llc") | ||||||||
| << "Unable to open statistics file: " << EC.message() << "\n"; | ||||||||
| return 1; | ||||||||
| } else { | ||||||||
| llvm::PrintStatisticsJSON(*StatsOS); | ||||||||
| } | ||||||||
|
jroelofs marked this conversation as resolved.
Outdated
|
||||||||
| } | ||||||||
| return 0; | ||||||||
| } | ||||||||
|
|
||||||||
| // main - Entry point for the llc compiler. | ||||||||
| // | ||||||||
| int main(int argc, char **argv) { | ||||||||
|
|
@@ -432,18 +487,23 @@ int main(int argc, char **argv) { | |||||||
| reportError(std::move(E), RemarksFilename); | ||||||||
| LLVMRemarkFileHandle RemarksFile = std::move(*RemarksFileOrErr); | ||||||||
|
|
||||||||
| if (int RetVal = MaybeEnableStats()) | ||||||||
| return RetVal; | ||||||||
| std::string OutputFilename; | ||||||||
|
|
||||||||
| if (InputLanguage != "" && InputLanguage != "ir" && InputLanguage != "mir") | ||||||||
| reportError("input language must be '', 'IR' or 'MIR'"); | ||||||||
|
|
||||||||
| // Compile the module TimeCompilations times to give better compile time | ||||||||
| // metrics. | ||||||||
| for (unsigned I = TimeCompilations; I; --I) | ||||||||
| if (int RetVal = compileModule(argv, Context)) | ||||||||
| if (int RetVal = compileModule(argv, Context, OutputFilename)) | ||||||||
| return RetVal; | ||||||||
|
|
||||||||
| if (RemarksFile) | ||||||||
| RemarksFile->keep(); | ||||||||
| return 0; | ||||||||
|
|
||||||||
| return MaybeSaveStats(std::move(OutputFilename)); | ||||||||
| } | ||||||||
|
|
||||||||
| static bool addPass(PassManagerBase &PM, const char *argv0, StringRef PassName, | ||||||||
|
|
@@ -475,7 +535,8 @@ static bool addPass(PassManagerBase &PM, const char *argv0, StringRef PassName, | |||||||
| return false; | ||||||||
| } | ||||||||
|
|
||||||||
| static int compileModule(char **argv, LLVMContext &Context) { | ||||||||
| static int compileModule(char **argv, LLVMContext &Context, | ||||||||
| std::string &OutputFilename) { | ||||||||
| // Load the module to be compiled... | ||||||||
| SMDiagnostic Err; | ||||||||
| std::unique_ptr<Module> M; | ||||||||
|
|
@@ -659,6 +720,9 @@ static int compileModule(char **argv, LLVMContext &Context) { | |||||||
| // Ensure the filename is passed down to CodeViewDebug. | ||||||||
| Target->Options.ObjectFilenameForDebug = Out->outputFilename(); | ||||||||
|
|
||||||||
| // Return a copy of the output filename via the output param | ||||||||
| OutputFilename = Out->outputFilename(); | ||||||||
|
|
||||||||
| // Tell target that this tool is not necessarily used with argument ABI | ||||||||
| // compliance (i.e. narrow integer argument extensions). | ||||||||
| Target->Options.VerifyArgABICompliance = 0; | ||||||||
|
|
||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.