[Mlir-commits] [mlir] [mlir][reducer] Refactor reduction-tree test (PR #184974)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Fri Mar 6 01:46:59 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-core

Author: lonely eagle (linuxlonelyeagle)

<details>
<summary>Changes</summary>

Consolidate reduction-tree pass tests into a single file using mlir-reduce's -split-input-file feature. See https://github.com/llvm/llvm-project/pull/184970.

---
Full diff: https://github.com/llvm/llvm-project/pull/184974.diff


3 Files Affected:

- (modified) mlir/lib/Tools/mlir-reduce/MlirReduceMain.cpp (+45-32) 
- (removed) mlir/test/mlir-reduce/crashop-reduction.mlir (-20) 
- (renamed) mlir/test/mlir-reduce/reduction-tree.mlir (+22-1) 


``````````diff
diff --git a/mlir/lib/Tools/mlir-reduce/MlirReduceMain.cpp b/mlir/lib/Tools/mlir-reduce/MlirReduceMain.cpp
index 34459b8564a25..6c1b6820707fa 100644
--- a/mlir/lib/Tools/mlir-reduce/MlirReduceMain.cpp
+++ b/mlir/lib/Tools/mlir-reduce/MlirReduceMain.cpp
@@ -18,6 +18,7 @@
 #include "mlir/Pass/PassManager.h"
 #include "mlir/Reducer/Passes.h"
 #include "mlir/Support/FileUtilities.h"
+#include "mlir/Support/ToolUtilities.h"
 #include "mlir/Tools/ParseUtilities.h"
 #include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/SourceMgr.h"
@@ -25,23 +26,6 @@
 
 using namespace mlir;
 
-// Parse and verify the input MLIR file. Returns null on error.
-static OwningOpRef<Operation *> loadModule(MLIRContext &context,
-                                           StringRef inputFilename,
-                                           bool insertImplictModule) {
-  // Set up the input file.
-  std::string errorMessage;
-  auto file = openInputFile(inputFilename, &errorMessage);
-  if (!file) {
-    llvm::errs() << errorMessage << "\n";
-    return nullptr;
-  }
-
-  auto sourceMgr = std::make_shared<llvm::SourceMgr>();
-  sourceMgr->AddNewSourceBuffer(std::move(file), SMLoc());
-  return parseSourceFileForTool(sourceMgr, &context, insertImplictModule);
-}
-
 LogicalResult mlir::mlirReduceMain(int argc, char **argv,
                                    MLIRContext &context) {
   // Override the default '-h' and use the default PrintHelpMessage() which
@@ -70,6 +54,18 @@ LogicalResult mlir::mlirReduceMain(int argc, char **argv,
       llvm::cl::desc("Allow operation with no registered dialects"),
       llvm::cl::init(false));
 
+  static llvm::cl::opt<std::string> splitInputFile(
+      "split-input-file", llvm::cl::ValueOptional,
+      llvm::cl::callback([&](const std::string &str) {
+        // Implicit value: use default marker if flag was used without
+        // value.
+        if (str.empty())
+          splitInputFile.setValue(kDefaultSplitMarker);
+      }),
+      llvm::cl::desc("Split the input file into chunks using the given or "
+                     "default marker and process each chunk independently"),
+      llvm::cl::init(""));
+
   llvm::cl::HideUnrelatedOptions(mlirReduceCategory);
 
   llvm::InitLLVM y(argc, argv);
@@ -93,27 +89,44 @@ LogicalResult mlir::mlirReduceMain(int argc, char **argv,
   if (!output)
     return failure();
 
-  OwningOpRef<Operation *> opRef =
-      loadModule(context, inputFilename, !noImplicitModule);
-  if (!opRef)
+  std::unique_ptr<llvm::MemoryBuffer> input =
+      openInputFile(inputFilename, &errorMessage);
+  if (!input) {
+    llvm::errs() << errorMessage << "\n";
     return failure();
+  }
 
   auto errorHandler = [&](const Twine &msg) {
     return emitError(UnknownLoc::get(&context)) << msg;
   };
 
-  // Reduction pass pipeline.
-  PassManager pm(&context, opRef.get()->getName().getStringRef());
-  if (failed(parser.addToPipeline(pm, errorHandler)))
-    return failure();
-
-  OwningOpRef<Operation *> op = opRef.get()->clone();
-
-  if (failed(pm.run(op.get())))
-    return failure();
+  auto chunkFn = [&](std::unique_ptr<llvm::MemoryBuffer> chunkBuffer,
+                     raw_ostream &os) {
+    auto sourceMgr = std::make_shared<llvm::SourceMgr>();
+    sourceMgr->AddNewSourceBuffer(std::move(chunkBuffer), SMLoc());
+    OwningOpRef<Operation *> opRef =
+        parseSourceFileForTool(sourceMgr, &context, !noImplicitModule);
+    if (!opRef)
+      return failure();
+    // Reduction pass pipeline.
+    PassManager pm(&context, opRef.get()->getName().getStringRef());
+    if (failed(parser.addToPipeline(pm, errorHandler)))
+      return failure();
+
+    OwningOpRef<Operation *> op = opRef.get()->clone();
+
+    if (failed(pm.run(op.get())))
+      return failure();
+    op.get()->print(output->os());
+    output->keep();
+    return success();
+  };
 
-  op.get()->print(output->os());
-  output->keep();
+  auto &splitInputFileDelimiter = splitInputFile.getValue();
+  if (!splitInputFileDelimiter.empty())
+    return splitAndProcessBuffer(std::move(input), chunkFn, output->os(),
+                                 splitInputFileDelimiter,
+                                 splitInputFileDelimiter);
 
-  return success();
+  return chunkFn(std::move(input), output->os());
 }
diff --git a/mlir/test/mlir-reduce/crashop-reduction.mlir b/mlir/test/mlir-reduce/crashop-reduction.mlir
deleted file mode 100644
index b7b2b4e6897a0..0000000000000
--- a/mlir/test/mlir-reduce/crashop-reduction.mlir
+++ /dev/null
@@ -1,20 +0,0 @@
-// UNSUPPORTED: system-windows
-// RUN: mlir-reduce %s -reduction-tree='traversal-mode=0 test=%S/failure-test.sh' | FileCheck %s
-// "test.op_crash_long" should be replaced with a shorter form "test.op_crash_short".
-
-// CHECK-NOT: func @simple1() {
-func.func @simple1() {
-  return
-}
-
-// CHECK-LABEL: func @simple2(%arg0: i32, %arg1: i32, %arg2: i32) {
-func.func @simple2(%arg0: i32, %arg1: i32, %arg2: i32) {
-  // CHECK-LABEL: %0 = "test.op_crash_short"() : () -> i32
-  %0 = "test.op_crash_long" (%arg0, %arg1, %arg2) : (i32, i32, i32) -> i32
-  return
-}
-
-// CHECK-NOT: func @simple5() {
-func.func @simple5() {
-  return
-}
diff --git a/mlir/test/mlir-reduce/multiple-function.mlir b/mlir/test/mlir-reduce/reduction-tree.mlir
similarity index 58%
rename from mlir/test/mlir-reduce/multiple-function.mlir
rename to mlir/test/mlir-reduce/reduction-tree.mlir
index 7c7b505146995..2aee89741b42b 100644
--- a/mlir/test/mlir-reduce/multiple-function.mlir
+++ b/mlir/test/mlir-reduce/reduction-tree.mlir
@@ -1,5 +1,26 @@
 // UNSUPPORTED: system-windows
-// RUN: mlir-reduce %s -reduction-tree='traversal-mode=0 test=%S/failure-test.sh' | FileCheck %s
+// RUN: mlir-reduce %s -split-input-file -reduction-tree='traversal-mode=0 test=%S/failure-test.sh' | FileCheck %s
+// "test.op_crash_long" should be replaced with a shorter form "test.op_crash_short".
+
+// CHECK-NOT: func @simple1() {
+func.func @simple1() {
+  return
+}
+
+// CHECK-LABEL: func @simple2(%arg0: i32, %arg1: i32, %arg2: i32) {
+func.func @simple2(%arg0: i32, %arg1: i32, %arg2: i32) {
+  // CHECK-LABEL: %0 = "test.op_crash_short"() : () -> i32
+  %0 = "test.op_crash_long" (%arg0, %arg1, %arg2) : (i32, i32, i32) -> i32
+  return
+}
+
+// CHECK-NOT: func @simple5() {
+func.func @simple5() {
+  return
+}
+
+// -----
+
 // This input should be reduced by the pass pipeline so that only
 // the @simple5 function remains as this is the shortest function
 // containing the interesting behavior.

``````````

</details>


https://github.com/llvm/llvm-project/pull/184974


More information about the Mlir-commits mailing list