[Mlir-commits] [mlir] [mlir] Default `mlir-query` input to stdin (PR #156324)
Denzel-Brian Budii
llvmlistbot at llvm.org
Mon Sep 1 05:37:11 PDT 2025
https://github.com/chios202 created https://github.com/llvm/llvm-project/pull/156324
Currently ,when piping a pass pipeline into `mlir-query` , you must explicitly pass `-` to read from the standard input:
```shell
./mlir-opt input.mlir -canonicalize | ./mlir-query - -c "<your_query_1>" -c "<your_query_2>" ... -c "<your_query_N>"
```
With this PR, the explicit `-` is no longer required:
```shell
./mlir-opt input.mlir -canonicalize | ./mlir-query -c "<your_query_1>" -c "<your_query_2>" ... -c "<your_query_N>"
```
>From ca7edc4867b510168ed79a3afcc81cfa64050c81 Mon Sep 17 00:00:00 2001
From: Denzel-Brian Budii <chio.star at yahoo.com>
Date: Mon, 1 Sep 2025 10:48:47 +0000
Subject: [PATCH] Default input to stdin
---
mlir/lib/Tools/mlir-query/MlirQueryMain.cpp | 11 ++++++++-
mlir/test/mlir-query/slice-reproducer.mlir | 27 +++++++++++++++++++++
2 files changed, 37 insertions(+), 1 deletion(-)
create mode 100644 mlir/test/mlir-query/slice-reproducer.mlir
diff --git a/mlir/lib/Tools/mlir-query/MlirQueryMain.cpp b/mlir/lib/Tools/mlir-query/MlirQueryMain.cpp
index 99500508ef045..6945c096124fd 100644
--- a/mlir/lib/Tools/mlir-query/MlirQueryMain.cpp
+++ b/mlir/lib/Tools/mlir-query/MlirQueryMain.cpp
@@ -21,6 +21,7 @@
#include "llvm/LineEditor/LineEditor.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/InitLLVM.h"
+#include "llvm/Support/Process.h"
#include "llvm/Support/SourceMgr.h"
//===----------------------------------------------------------------------===//
@@ -43,7 +44,7 @@ mlir::mlirQueryMain(int argc, char **argv, MLIRContext &context,
llvm::cl::value_desc("command"), llvm::cl::cat(mlirQueryCategory));
static llvm::cl::opt<std::string> inputFilename(
- llvm::cl::Positional, llvm::cl::desc("<input file>"),
+ llvm::cl::Positional, llvm::cl::desc("<input file>"), llvm::cl::init("-"),
llvm::cl::cat(mlirQueryCategory));
static llvm::cl::opt<bool> noImplicitModule{
@@ -68,6 +69,14 @@ mlir::mlirQueryMain(int argc, char **argv, MLIRContext &context,
return mlir::success();
}
+ // When reading from stdin and the input is a tty, it is often a user mistake
+ // and the process "appears to be stuck". Print a message to let the user
+ // know!
+ if (inputFilename == "-" &&
+ llvm::sys::Process::FileDescriptorIsDisplayed(fileno(stdin)))
+ llvm::errs() << "(processing input from stdin now, hit ctrl-c/ctrl-d to "
+ "interrupt)\n";
+
// Set up the input file.
std::string errorMessage;
auto file = openInputFile(inputFilename, &errorMessage);
diff --git a/mlir/test/mlir-query/slice-reproducer.mlir b/mlir/test/mlir-query/slice-reproducer.mlir
new file mode 100644
index 0000000000000..126d9a71b57c9
--- /dev/null
+++ b/mlir/test/mlir-query/slice-reproducer.mlir
@@ -0,0 +1,27 @@
+// RUN: mlir-opt %s -slice-analysis-test -split-input-file | mlir-query -c "match getDefinitions(hasOpName(\"memref.dealloc\"), 2, false, true, true).extract(\"backward_slice\")" | FileCheck %s
+
+func.func @slicing_linalg_op(%arg0 : index, %arg1 : index, %arg2 : index) {
+ %a = memref.alloc(%arg0, %arg2) : memref<?x?xf32>
+ %b = memref.alloc(%arg2, %arg1) : memref<?x?xf32>
+ %c = memref.alloc(%arg0, %arg1) : memref<?x?xf32>
+ %d = memref.alloc(%arg0, %arg1) : memref<?x?xf32>
+ linalg.matmul ins(%a, %b : memref<?x?xf32>, memref<?x?xf32>)
+ outs(%c : memref<?x?xf32>)
+ linalg.matmul ins(%a, %b : memref<?x?xf32>, memref<?x?xf32>)
+ outs(%d : memref<?x?xf32>)
+ memref.dealloc %c : memref<?x?xf32>
+ memref.dealloc %b : memref<?x?xf32>
+ memref.dealloc %a : memref<?x?xf32>
+ memref.dealloc %d : memref<?x?xf32>
+ return
+}
+
+// CHECK: func.func @backward_slice(
+// CHECK-SAME: %[[ARG0:[a-zA-Z0-9_]+]]: index,
+// CHECK-SAME: %[[ARG1:[a-zA-Z0-9_]+]]: index,
+// CHECK-SAME: %[[ARG2:[a-zA-Z0-9_]+]]: index) -> (memref<?x?xf32>, memref<?x?xf32>, memref<?x?xf32>, memref<?x?xf32>) {
+// CHECK: %[[ALLOC:.*]] = memref.alloc(%[[ARG1]], %[[ARG2]]) : memref<?x?xf32>
+// CHECK-NEXT: %[[ALLOC_0:.*]] = memref.alloc(%[[ARG0]], %[[ARG2]]) : memref<?x?xf32>
+// CHECK-NEXT: %[[ALLOC_1:.*]] = memref.alloc(%[[ARG1]], %[[ARG0]]) : memref<?x?xf32>
+// CHECK-NEXT: %[[ALLOC_2:.*]] = memref.alloc(%[[ARG1]], %[[ARG2]]) : memref<?x?xf32>
+// CHECK-NEXT: return %[[ALLOC]], %[[ALLOC_0]], %[[ALLOC_1]], %[[ALLOC_2]] : memref<?x?xf32>, memref<?x?xf32>, memref<?x?xf32>, memref<?x?xf32>
More information about the Mlir-commits
mailing list