-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[opt] Add --save-stats option #167304
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
[opt] Add --save-stats option #167304
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,33 +13,43 @@ | |
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "llvm/CodeGen/CommandFlags.h" | ||
| #include "llvm/ADT/SmallString.h" | ||
| #include "llvm/ADT/Statistic.h" | ||
| #include "llvm/ADT/StringExtras.h" | ||
| #include "llvm/ADT/StringRef.h" | ||
| #include "llvm/IR/Instructions.h" | ||
| #include "llvm/IR/Intrinsics.h" | ||
| #include "llvm/IR/Module.h" | ||
| #include "llvm/MC/MCTargetOptionsCommandFlags.h" | ||
| #include "llvm/MC/TargetRegistry.h" | ||
| #include "llvm/Support/CommandLine.h" | ||
| #include "llvm/Support/FileSystem.h" | ||
| #include "llvm/Support/MemoryBuffer.h" | ||
| #include "llvm/Support/Path.h" | ||
| #include "llvm/Support/WithColor.h" | ||
| #include "llvm/Support/raw_ostream.h" | ||
| #include "llvm/Target/TargetMachine.h" | ||
| #include "llvm/TargetParser/Host.h" | ||
| #include "llvm/TargetParser/SubtargetFeature.h" | ||
| #include "llvm/TargetParser/Triple.h" | ||
| #include <cassert> | ||
| #include <memory> | ||
| #include <optional> | ||
| #include <system_error> | ||
|
|
||
| using namespace llvm; | ||
|
|
||
| #define CGOPT(TY, NAME) \ | ||
| static cl::opt<TY> *NAME##View; \ | ||
| TY codegen::get##NAME() { \ | ||
| assert(NAME##View && "RegisterCodeGenFlags not created."); \ | ||
| assert(NAME##View && "Flag not registered."); \ | ||
| return *NAME##View; \ | ||
| } | ||
|
|
||
| #define CGLIST(TY, NAME) \ | ||
| static cl::list<TY> *NAME##View; \ | ||
| std::vector<TY> codegen::get##NAME() { \ | ||
| assert(NAME##View && "RegisterCodeGenFlags not created."); \ | ||
| assert(NAME##View && "Flag not registered."); \ | ||
| return *NAME##View; \ | ||
| } | ||
|
|
||
|
|
@@ -110,13 +120,14 @@ CGOPT(bool, DebugStrictDwarf) | |
| CGOPT(unsigned, AlignLoops) | ||
| CGOPT(bool, JMCInstrument) | ||
| CGOPT(bool, XCOFFReadOnlyPointers) | ||
| CGOPT(codegen::SaveStatsMode, SaveStats) | ||
|
|
||
| codegen::RegisterCodeGenFlags::RegisterCodeGenFlags() { | ||
| #define CGBINDOPT(NAME) \ | ||
| do { \ | ||
| NAME##View = std::addressof(NAME); \ | ||
| } while (0) | ||
|
|
||
| codegen::RegisterCodeGenFlags::RegisterCodeGenFlags() { | ||
| static cl::opt<std::string> MArch( | ||
| "march", cl::desc("Architecture to generate code for (see --version)")); | ||
| CGBINDOPT(MArch); | ||
|
|
@@ -515,11 +526,25 @@ codegen::RegisterCodeGenFlags::RegisterCodeGenFlags() { | |
| cl::init(false)); | ||
| CGBINDOPT(DisableIntegratedAS); | ||
|
|
||
| #undef CGBINDOPT | ||
|
Member
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. Should be moved after RegisterSaveStatsFlag
Contributor
Author
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. I intentionally removed it because I dont think its needed anymore moving |
||
|
|
||
| mc::RegisterMCTargetOptionsFlags(); | ||
| } | ||
|
|
||
| codegen::RegisterSaveStatsFlag::RegisterSaveStatsFlag() { | ||
| static cl::opt<SaveStatsMode> 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::values(clEnumValN(SaveStatsMode::Cwd, "cwd", | ||
| "Save to the current working directory"), | ||
| clEnumValN(SaveStatsMode::Cwd, "", ""), | ||
| clEnumValN(SaveStatsMode::Obj, "obj", | ||
| "Save to the output file directory")), | ||
| cl::init(SaveStatsMode::None), cl::ValueOptional); | ||
| CGBINDOPT(SaveStats); | ||
| } | ||
|
|
||
| llvm::BasicBlockSection | ||
| codegen::getBBSectionsMode(llvm::TargetOptions &Options) { | ||
| if (getBBSections() == "all") | ||
|
|
@@ -763,3 +788,43 @@ codegen::createTargetMachineForTriple(StringRef TargetTriple, | |
| TargetTriple); | ||
| return std::unique_ptr<TargetMachine>(Target); | ||
| } | ||
|
|
||
| int codegen::MaybeEnableStatistics() { | ||
tomershafir marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (getSaveStats() == SaveStatsMode::None) | ||
| return 0; | ||
|
|
||
| llvm::EnableStatistics(false); | ||
| return 0; | ||
| } | ||
|
|
||
| int codegen::MaybeSaveStatistics(StringRef OutputFilename, StringRef ToolName) { | ||
| auto SaveStatsValue = getSaveStats(); | ||
| if (SaveStatsValue == codegen::SaveStatsMode::None) | ||
| return 0; | ||
|
|
||
| SmallString<128> StatsFilename; | ||
| if (SaveStatsValue == codegen::SaveStatsMode::Obj) { | ||
| StatsFilename = OutputFilename; | ||
| llvm::sys::path::remove_filename(StatsFilename); | ||
| } else { | ||
| assert(SaveStatsValue == codegen::SaveStatsMode::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(), ToolName) | ||
| << "Unable to open statistics file: " << EC.message() << "\n"; | ||
| return 1; | ||
| } | ||
|
|
||
| llvm::PrintStatisticsJSON(*StatsOS); | ||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| ; REQUIRES: asserts | ||
|
|
||
| ; Ensure the test runs in a temp directory. See https://github.com/llvm/llvm-project/pull/167403#event-20848739526 | ||
| ; RUN: rm -rf %t.dir && mkdir -p %t.dir && cd %t.dir | ||
|
|
||
| ; RUN: opt -S -passes=instcombine --save-stats=obj -o %t.ll %s && cat %t.stats | FileCheck %s | ||
| ; RUN: opt -S -passes=instcombine --save-stats=cwd -o %t.ll %s && cat %{t:stem}.tmp.stats | FileCheck %s | ||
| ; RUN: opt -S -passes=instcombine --save-stats -o %t.ll %s && cat %{t:stem}.tmp.stats | FileCheck %s | ||
| ; RUN: not opt -S --save-stats=invalid -o %t.ll %s 2>&1 | FileCheck %s --check-prefix=INVALID_ARG | ||
|
|
||
| ; CHECK: { | ||
| ; CHECK: "instcombine.NumWorklistIterations": | ||
| ; CHECK: } | ||
|
|
||
| ; INVALID_ARG: {{.*}}opt{{.*}}: for the --save-stats option: Cannot find option named 'invalid'! | ||
| define i32 @func() { | ||
| ret i32 0 | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.