From 99c463512a0458ff1a1928d670cded3b320eb990 Mon Sep 17 00:00:00 2001 From: Shashi Shankar Date: Fri, 27 Feb 2026 01:50:10 +0100 Subject: [PATCH] [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 --- mlir/lib/Debug/DebugCounter.cpp | 33 ++++++++++--------- .../debugcounter-invalid-cl-options.mlir | 27 +++++++++++++++ 2 files changed, 44 insertions(+), 16 deletions(-) create mode 100644 mlir/test/mlir-opt/debugcounter-invalid-cl-options.mlir diff --git a/mlir/lib/Debug/DebugCounter.cpp b/mlir/lib/Debug/DebugCounter.cpp index e026a982859a..879f6c9365cf 100644 --- a/mlir/lib/Debug/DebugCounter.cpp +++ b/mlir/lib/Debug/DebugCounter.cpp @@ -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); } } } diff --git a/mlir/test/mlir-opt/debugcounter-invalid-cl-options.mlir b/mlir/test/mlir-opt/debugcounter-invalid-cl-options.mlir new file mode 100644 index 000000000000..ab66fedede6e --- /dev/null +++ b/mlir/test/mlir-opt/debugcounter-invalid-cl-options.mlir @@ -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`