Skip to content

Commit 860c71a

Browse files
committed
use enum based option and flatten control flow
1 parent 56a0157 commit 860c71a

File tree

2 files changed

+39
-35
lines changed

2 files changed

+39
-35
lines changed

llvm/test/tools/llc/save-stats.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
; RUN: rm -f %{t:stem}.tmp.stats && llc -mtriple=arm64-apple-macosx --save-stats -o %t.s %s && test -f %{t:stem}.tmp.stats
55
; RUN: not llc -mtriple=arm64-apple-macosx --save-stats=invalid -o %t.s %s 2>&1 | FileCheck %s --check-prefix=INVALID_ARG
66

7-
; INVALID_ARG: llc: error: Invalid --save-stats value: invalid, must be empty, 'cwd' or 'obj'
7+
; INVALID_ARG: llc: for the --save-stats option: Cannot find option named 'invalid'!
88
define i32 @func() {
99
ret i32 0
1010
}

llvm/tools/llc/llc.cpp

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,19 @@ static cl::opt<std::string> RemarksFormat(
206206
cl::desc("The format used for serializing remarks (default: YAML)"),
207207
cl::value_desc("format"), cl::init("yaml"));
208208

209-
static cl::opt<std::string> SaveStats(
209+
enum SaveStatsMode { None, Cwd, Obj };
210+
211+
static cl::opt<SaveStatsMode> SaveStats(
210212
"save-stats",
211213
cl::desc("Save LLVM statistics to a file in the current directory"
212214
"(`-save-stats`/`-save-stats=cwd`) or the directory of the output"
213215
"file (`-save-stats=obj`). (default: cwd)"),
214-
cl::init(""), cl::ValueOptional);
216+
cl::values(clEnumValN(SaveStatsMode::Cwd, "cwd",
217+
"Save to the current working directory"),
218+
clEnumValN(SaveStatsMode::Cwd, "", ""),
219+
clEnumValN(SaveStatsMode::Obj, "obj",
220+
"Save to the output file directory")),
221+
cl::init(SaveStatsMode::None), cl::ValueOptional);
215222

216223
static cl::opt<bool> EnableNewPassManager(
217224
"enable-new-pm", cl::desc("Enable the new pass manager"), cl::init(false));
@@ -367,46 +374,43 @@ static std::unique_ptr<ToolOutputFile> GetOutputStream(const char *TargetName,
367374
}
368375

369376
static int MaybeEnableStats() {
370-
if (SaveStats.getNumOccurrences() > 0) {
371-
if (SaveStats.empty() || SaveStats == "cwd" || SaveStats == "obj") {
372-
llvm::EnableStatistics(false);
373-
} else {
374-
WithColor::error(errs(), "llc")
375-
<< "Invalid --save-stats value: " << SaveStats
376-
<< ", must be empty, 'cwd' or 'obj'\n";
377-
return 1;
378-
}
377+
if (SaveStats == SaveStatsMode::None) {
378+
return 0;
379379
}
380+
381+
llvm::EnableStatistics(false);
380382
return 0;
381383
}
382384

383385
static int MaybeSaveStats(std::string &&OutputFilename) {
384-
if (SaveStats.getNumOccurrences() > 0) {
385-
SmallString<128> StatsFilename;
386-
if (SaveStats == "obj") {
387-
StatsFilename = OutputFilename;
388-
llvm::sys::path::remove_filename(StatsFilename);
389-
} else {
390-
assert((SaveStats.empty() || SaveStats == "cwd") &&
391-
"Should have been a valid --save-stats value");
392-
}
386+
if (SaveStats == SaveStatsMode::None) {
387+
return 0;
388+
}
393389

394-
auto BaseName = llvm::sys::path::filename(OutputFilename);
395-
llvm::sys::path::append(StatsFilename, BaseName);
396-
llvm::sys::path::replace_extension(StatsFilename, "stats");
390+
SmallString<128> StatsFilename;
391+
if (SaveStats == SaveStatsMode::Obj) {
392+
StatsFilename = OutputFilename;
393+
llvm::sys::path::remove_filename(StatsFilename);
394+
} else {
395+
assert((SaveStats == SaveStatsMode::Cwd) &&
396+
"Should have been a valid --save-stats value");
397+
}
397398

398-
auto FileFlags = llvm::sys::fs::OF_TextWithCRLF;
399-
std::error_code EC;
400-
auto StatsOS =
401-
std::make_unique<llvm::raw_fd_ostream>(StatsFilename, EC, FileFlags);
402-
if (EC) {
403-
WithColor::error(errs(), "llc")
404-
<< "Unable to open statistics file: " << EC.message() << "\n";
405-
return 1;
406-
} else {
407-
llvm::PrintStatisticsJSON(*StatsOS);
408-
}
399+
auto BaseName = llvm::sys::path::filename(OutputFilename);
400+
llvm::sys::path::append(StatsFilename, BaseName);
401+
llvm::sys::path::replace_extension(StatsFilename, "stats");
402+
403+
auto FileFlags = llvm::sys::fs::OF_TextWithCRLF;
404+
std::error_code EC;
405+
auto StatsOS =
406+
std::make_unique<llvm::raw_fd_ostream>(StatsFilename, EC, FileFlags);
407+
if (EC) {
408+
WithColor::error(errs(), "llc")
409+
<< "Unable to open statistics file: " << EC.message() << "\n";
410+
return 1;
409411
}
412+
413+
llvm::PrintStatisticsJSON(*StatsOS);
410414
return 0;
411415
}
412416

0 commit comments

Comments
 (0)