[MLIR] Do not abort on invalid --mlir-debug-counter values (#181751)

Use `cl::Option::error()` diagnostics for invalid `--mlir-debug-counter`
arguments and exit with status 1 (no stack dump).

Added `mlir/test/mlir-opt/debugcounter-invalid-cl-options.mlir`
covering:
  - non-numeric value (`-1n`)
  - missing `=`
  - missing `-skip`/`-count` suffix

Fixes #180117
This commit is contained in:
Shashi Shankar 2026-02-27 01:50:10 +01:00 committed by GitHub
parent d149830b98
commit 99c463512a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 16 deletions

View File

@ -132,22 +132,23 @@ void DebugCounter::applyCLOptions() {
// Debug counter arguments are expected to be in the form: `counter=value`.
auto [counterName, counterValueStr] = arg.split('=');
if (counterValueStr.empty()) {
llvm::errs() << "error: expected DebugCounter argument to have an `=` "
"separating the counter name and value, but the provided "
"argument was: `"
<< arg << "`\n";
llvm::report_fatal_error(
"Invalid DebugCounter command-line configuration");
clOptions->counters.error(
llvm::Twine(
"expected DebugCounter argument to have an `=` separating "
"the counter name and value, but the provided argument "
"was: `") +
arg + "`");
exit(1);
}
// Extract the counter value.
int64_t counterValue;
if (counterValueStr.getAsInteger(0, counterValue)) {
llvm::errs() << "error: expected DebugCounter counter value to be "
"numeric, but got `"
<< counterValueStr << "`\n";
llvm::report_fatal_error(
"Invalid DebugCounter command-line configuration");
clOptions->counters.error(
llvm::Twine("expected DebugCounter counter value to be numeric, but "
"got `") +
counterValueStr + "`");
exit(1);
}
// Now we need to see if this is the skip or the count, remove the suffix,
@ -159,11 +160,11 @@ void DebugCounter::applyCLOptions() {
counters[counterName].countToStopAfter = counterValue;
} else {
llvm::errs() << "error: expected DebugCounter counter name to end with "
"either `-skip` or `-count`, but got`"
<< counterName << "`\n";
llvm::report_fatal_error(
"Invalid DebugCounter command-line configuration");
clOptions->counters.error(
llvm::Twine("expected DebugCounter counter name to end with either "
"`-skip` or `-count`, but got `") +
counterName + "`");
exit(1);
}
}
}

View File

@ -0,0 +1,27 @@
// RUN: not mlir-opt %s --mlir-disable-threading \
// RUN: --mlir-debug-counter=unique-tag-for-my-action-skip=-1n 2>&1 \
// RUN: | FileCheck %s --check-prefix=BADNUM
//
// RUN: not mlir-opt %s --mlir-disable-threading \
// RUN: --mlir-debug-counter=unique-tag-for-my-action-skip 2>&1 \
// RUN: | FileCheck %s --check-prefix=NOEQ
//
// RUN: not mlir-opt %s --mlir-disable-threading \
// RUN: --mlir-debug-counter=unique-tag-for-my-action=-1 2>&1 \
// RUN: | FileCheck %s --check-prefix=BADSFX
func.func @foo() {
return
}
// BADNUM-NOT: LLVM ERROR
// BADNUM-NOT: Stack dump:
// BADNUM: {{.*}}: for the {{-+}}mlir-debug-counter option: expected DebugCounter counter value to be numeric, but got `-1n`
// NOEQ-NOT: LLVM ERROR
// NOEQ-NOT: Stack dump:
// NOEQ: {{.*}}: for the {{-+}}mlir-debug-counter option: expected DebugCounter argument to have an `=` separating the counter name and value, but the provided argument was: `unique-tag-for-my-action-skip`
// BADSFX-NOT: LLVM ERROR
// BADSFX-NOT: Stack dump:
// BADSFX: {{.*}}: for the {{-+}}mlir-debug-counter option: expected DebugCounter counter name to end with either `-skip` or `-count`, but got `unique-tag-for-my-action`