Skip to content

Commit 1a001de

Browse files
committed
[mlir-reduce] Improve diagnostic message and clean build dependency
Reviewed By: rriddle Differential Revision: https://reviews.llvm.org/D104443
1 parent db9df43 commit 1a001de

File tree

6 files changed

+52
-49
lines changed

6 files changed

+52
-49
lines changed

mlir/include/mlir/Reducer/Passes.td

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,33 @@ include "mlir/Pass/PassBase.td"
1818
def CommonReductionPassOptions {
1919
list<Option> options = [
2020
Option<"testerName", "test", "std::string", /* default */"",
21-
"The filename of the tester">,
21+
"The location of the tester which tests the file interestingness">,
2222
ListOption<"testerArgs", "test-arg", "std::string",
23+
"arguments of the tester",
2324
"llvm::cl::ZeroOrMore, llvm::cl::MiscFlags::CommaSeparated">,
2425
];
2526
}
2627

2728
def ReductionTree : Pass<"reduction-tree"> {
28-
let summary = "A general reduction tree pass for the MLIR Reduce Tool";
29+
let summary = "Reduce the input with reduction-tree algorithm";
2930

3031
let constructor = "mlir::createReductionTreePass()";
3132

3233
let options = [
3334
Option<"traversalModeId", "traversal-mode", "unsigned",
34-
/* default */"0", "The graph traversal mode">,
35+
/* default */"0",
36+
"The graph traversal mode, the default is single-path mode">,
3537
] # CommonReductionPassOptions.options;
3638
}
3739

3840
def OptReduction : Pass<"opt-reduction-pass", "ModuleOp"> {
39-
let summary = "A reduction pass wrapper for optimization passes";
41+
let summary = "A wrapper pass that reduces the file with optimization passes";
4042

4143
let constructor = "mlir::createOptReductionPass()";
4244

4345
let options = [
4446
Option<"optPass", "opt-pass", "std::string", /* default */"",
45-
"The optimization pass will be run dynamically in OptReductionPass">,
47+
"The optimization passes used for reduction, e.g., symbol-dce">,
4648
] # CommonReductionPassOptions.options;
4749
}
4850

mlir/lib/Reducer/OptReductionPass.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ void OptReductionPass::runOnOperation() {
4444

4545
PassManager passManager(module.getContext());
4646
if (failed(parsePassPipeline(optPass, passManager))) {
47-
LLVM_DEBUG(llvm::dbgs() << "\nFailed to parse pass pipeline");
48-
return;
47+
module.emitError() << "\nfailed to parse pass pipeline";
48+
return signalPassFailure();
4949
}
5050

5151
std::pair<Tester::Interestingness, int> original = test.isInteresting(module);
5252
if (original.first != Tester::Interestingness::True) {
53-
LLVM_DEBUG(llvm::dbgs() << "\nThe original input is not interested");
54-
return;
53+
module.emitError() << "\nthe original input is not interested";
54+
return signalPassFailure();
5555
}
5656

5757
if (failed(passManager.run(moduleVariant))) {
58-
LLVM_DEBUG(llvm::dbgs() << "\nFailed to run pass pipeline");
59-
return;
58+
module.emitError() << "\nfailed to run pass pipeline";
59+
return signalPassFailure();
6060
}
6161

6262
std::pair<Tester::Interestingness, int> reduced =

mlir/lib/Reducer/ReductionTreePass.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ static void applyPatterns(Region &region,
7676
/// alternative way to remove operations, which is using `eraseOpNotInRange` to
7777
/// erase the operations not in the range specified by ReductionNode.
7878
template <typename IteratorType>
79-
static void findOptimal(ModuleOp module, Region &region,
80-
const FrozenRewritePatternSet &patterns,
81-
const Tester &test, bool eraseOpNotInRange) {
79+
static LogicalResult findOptimal(ModuleOp module, Region &region,
80+
const FrozenRewritePatternSet &patterns,
81+
const Tester &test, bool eraseOpNotInRange) {
8282
std::pair<Tester::Interestingness, size_t> initStatus =
8383
test.isInteresting(module);
8484
// While exploring the reduction tree, we always branch from an interesting
8585
// node. Thus the root node must be interesting.
8686
if (initStatus.first != Tester::Interestingness::True)
87-
return;
87+
return module.emitWarning() << "uninterested module will not be reduced";
8888

8989
llvm::SpecificBumpPtrAllocator<ReductionNode> allocator;
9090

@@ -137,23 +137,25 @@ static void findOptimal(ModuleOp module, Region &region,
137137
if (test.isInteresting(module).second != smallestNode->getSize())
138138
llvm::report_fatal_error(
139139
"Reduced module doesn't have consistent size with smallestNode");
140+
return success();
140141
}
141142

142143
template <typename IteratorType>
143-
static void findOptimal(ModuleOp module, Region &region,
144-
const FrozenRewritePatternSet &patterns,
145-
const Tester &test) {
144+
static LogicalResult findOptimal(ModuleOp module, Region &region,
145+
const FrozenRewritePatternSet &patterns,
146+
const Tester &test) {
146147
// We separate the reduction process into 2 steps, the first one is to erase
147148
// redundant operations and the second one is to apply the reducer patterns.
148149

149150
// In the first phase, we don't apply any patterns so that we only select the
150151
// range of operations to keep to the module stay interesting.
151-
findOptimal<IteratorType>(module, region, /*patterns=*/{}, test,
152-
/*eraseOpNotInRange=*/true);
152+
if (failed(findOptimal<IteratorType>(module, region, /*patterns=*/{}, test,
153+
/*eraseOpNotInRange=*/true)))
154+
return failure();
153155
// In the second phase, we suppose that no operation is redundant, so we try
154156
// to rewrite the operation into simpler form.
155-
findOptimal<IteratorType>(module, region, patterns, test,
156-
/*eraseOpNotInRange=*/false);
157+
return findOptimal<IteratorType>(module, region, patterns, test,
158+
/*eraseOpNotInRange=*/false);
157159
}
158160

159161
namespace {
@@ -192,7 +194,7 @@ class ReductionTreePass : public ReductionTreeBase<ReductionTreePass> {
192194
void runOnOperation() override;
193195

194196
private:
195-
void reduceOp(ModuleOp module, Region &region);
197+
LogicalResult reduceOp(ModuleOp module, Region &region);
196198

197199
FrozenRewritePatternSet reducerPatterns;
198200
};
@@ -221,7 +223,8 @@ void ReductionTreePass::runOnOperation() {
221223

222224
for (Region &region : op->getRegions())
223225
if (!region.empty())
224-
reduceOp(module, region);
226+
if (failed(reduceOp(module, region)))
227+
return signalPassFailure();
225228

226229
for (Region &region : op->getRegions())
227230
for (Operation &op : region.getOps())
@@ -230,15 +233,14 @@ void ReductionTreePass::runOnOperation() {
230233
} while (!workList.empty());
231234
}
232235

233-
void ReductionTreePass::reduceOp(ModuleOp module, Region &region) {
236+
LogicalResult ReductionTreePass::reduceOp(ModuleOp module, Region &region) {
234237
Tester test(testerName, testerArgs);
235238
switch (traversalModeId) {
236239
case TraversalMode::SinglePath:
237-
findOptimal<ReductionNode::iterator<TraversalMode::SinglePath>>(
240+
return findOptimal<ReductionNode::iterator<TraversalMode::SinglePath>>(
238241
module, region, reducerPatterns, test);
239-
break;
240242
default:
241-
llvm_unreachable("Unsupported mode");
243+
return module.emitError() << "unsupported traversal mode detected";
242244
}
243245
}
244246

mlir/lib/Tools/mlir-reduce/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
set(LLVM_OPTIONAL_SOURCES
2-
MlirReduceMain.cpp
3-
)
4-
5-
set(LLVM_LINK_COMPONENTS
6-
Support
7-
)
8-
91
add_mlir_library(MLIRReduceLib
102
MlirReduceMain.cpp
113

mlir/lib/Tools/mlir-reduce/MlirReduceMain.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,36 @@ static LogicalResult loadModule(MLIRContext &context, OwningModuleRef &module,
3939

4040
LogicalResult mlir::mlirReduceMain(int argc, char **argv,
4141
MLIRContext &context) {
42+
// Override the default '-h' and use the default PrintHelpMessage() which
43+
// won't print options in categories.
44+
static llvm::cl::opt<bool> Help("h", llvm::cl::desc("Alias for -help"),
45+
llvm::cl::Hidden);
46+
47+
static llvm::cl::OptionCategory MLIRReduceCategory("mlir-reduce options");
48+
4249
static llvm::cl::opt<std::string> inputFilename(
43-
llvm::cl::Positional, llvm::cl::Required, llvm::cl::desc("<input file>"));
50+
llvm::cl::Positional, llvm::cl::desc("<input file>"),
51+
llvm::cl::cat(MLIRReduceCategory));
4452

4553
static llvm::cl::opt<std::string> outputFilename(
4654
"o", llvm::cl::desc("Output filename for the reduced test case"),
47-
llvm::cl::init("-"));
55+
llvm::cl::init("-"), llvm::cl::cat(MLIRReduceCategory));
56+
57+
llvm::cl::HideUnrelatedOptions(MLIRReduceCategory);
4858

4959
llvm::InitLLVM y(argc, argv);
5060

5161
registerReducerPasses();
52-
registerMLIRContextCLOptions();
53-
registerPassManagerCLOptions();
5462

5563
PassPipelineCLParser parser("", "Reduction Passes to Run");
5664
llvm::cl::ParseCommandLineOptions(argc, argv,
5765
"MLIR test case reduction tool.\n");
5866

67+
if (Help) {
68+
llvm::cl::PrintHelpMessage();
69+
return success();
70+
}
71+
5972
std::string errorMessage;
6073

6174
auto output = openOutputFile(outputFilename, &errorMessage);

mlir/tools/mlir-reduce/CMakeLists.txt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
21
get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
3-
4-
if(MLIR_INCLUDE_TESTS)
5-
set(test_libs
6-
MLIRTestDialect
7-
)
8-
endif()
2+
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
93

104
set(LIBS
11-
${dialect_libs}
125
${conversion_libs}
13-
${test_libs}
6+
${dialect_libs}
147
MLIRDialect
158
MLIRIR
169
MLIRPass
1710
MLIRReduceLib
11+
MLIRTestDialect
1812
)
1913

2014
add_llvm_tool(mlir-reduce

0 commit comments

Comments
 (0)