Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a more complete example to mlir-reduce docs #116085

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

j2kun
Copy link
Contributor

@j2kun j2kun commented Nov 13, 2024

This PR was started because I noticed bugs in the docs:

  • The interestingness test checked for an error, but did not redirect stderr to stdout to include it in the pipe.
  • The interestingness test pipe ended with grep, but then checked for an exit code of 1 to mark the input as interesting. However, a pipe's overall exit code is the exit code of the rightmost command, and grep's exit code is zero when it detects the query string, and 1 when it does not detect the query string. So the test was backwards.

Then I wanted to test my fixes to be sure I got it right, and I realized that the doc has no properly runnable example. I tried adapting some tests but the use of the test dialect gave me some issues, so I felt it would be more straightforward to add a test that only uses builtin dialects. Because I could not find an upstream error to demonstrate, I instead just made the test check for the presence of a particular op, and the example I came up with hits a stack trace in the reduce tool:

PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: bazel-bin/external/llvm-project/mlir/mlir-reduce /home/j2kun/fhe/heir/query-test.mlir "-reduction-tree=traversal-mode=0 test=query-test.sh"
 #0 0x000055f2b0c7f4ad llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /proc/self/cwd/external/llvm-project/llvm/lib/Support/Unix/Signals.inc:723:11
 #1 0x000055f2b0c7f99b PrintStackTraceSignalHandler(void*) /proc/self/cwd/external/llvm-project/llvm/lib/Support/Unix/Signals.inc:798:1
 #2 0x000055f2b0c7d056 llvm::sys::RunSignalHandlers() /proc/self/cwd/external/llvm-project/llvm/lib/Support/Signals.cpp:105:5
 #3 0x000055f2b0c80095 SignalHandler(int) /proc/self/cwd/external/llvm-project/llvm/lib/Support/Unix/Signals.inc:413:1
 #4 0x00007f16b8d3c520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #5 0x000055f2a66fb5b4 mlir::Operation::getNumOperands() /proc/self/cwd/external/llvm-project/mlir/include/mlir/IR/Operation.h:342:12
 #6 0x000055f2b0b43c6a mlir::Operation::clone(mlir::IRMapping&, mlir::Operation::CloneOptions) /proc/self/cwd/external/llvm-project/mlir/lib/IR/Operation.cpp:723:22
 #7 0x000055f2afd53ec3 mlir::ReductionNode::initialize(mlir::ModuleOp, mlir::Region&) /proc/self/cwd/external/llvm-project/mlir/lib/Reducer/ReductionNode.cpp:42:41
 #8 0x000055f2afd53d8b mlir::ReductionNode::ReductionNode(mlir::ReductionNode*, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>> const&, llvm::SpecificBumpPtrAllocator<mlir::ReductionNode>&) /proc/self/cwd/external/llvm-project/mlir/lib/Reducer/ReductionNode.cpp:34:16
 #9 0x000055f2afd54731 mlir::ReductionNode::generateNewVariants()::$_1::operator()(std::vector<std::pair<int, int>, std::allocator<std::pair<int, int>>> const&) const /proc/self/cwd/external/llvm-project/mlir/lib/Reducer/ReductionNode.cpp:56:39
#10 0x000055f2afd543d2 mlir::ReductionNode::generateNewVariants() /proc/self/cwd/external/llvm-project/mlir/lib/Reducer/ReductionNode.cpp:92:22
#11 0x000055f2afd549fd mlir::ReductionNode::iterator<(mlir::TraversalMode)0>::getNeighbors(mlir::ReductionNode*) /proc/self/cwd/external/llvm-project/mlir/lib/Reducer/ReductionNode.cpp:154:16
#12 0x000055f2afd52bed mlir::ReductionNode::BaseIterator<mlir::ReductionNode::iterator<(mlir::TraversalMode)0>>::getNeighbors(mlir::ReductionNode*) /proc/self/cwd/external/llvm-project/mlir/include/mlir/Reducer/ReductionNode.h:135:38
#13 0x000055f2afd4fbc6 mlir::ReductionNode::BaseIterator<mlir::ReductionNode::iterator<(mlir::TraversalMode)0>>::operator++() /proc/self/cwd/external/llvm-project/mlir/include/mlir/Reducer/ReductionNode.h:119:34
#14 0x000055f2afd4e745 llvm::LogicalResult findOptimal<mlir::ReductionNode::iterator<(mlir::TraversalMode)0>>(mlir::ModuleOp, mlir::Region&, mlir::FrozenRewritePatternSet const&, mlir::Tester const&, bool) /proc/self/cwd/external/llvm-project/mlir/lib/Reducer/ReductionTreePass.cpp:123:5
#15 0x000055f2afd4e053 llvm::LogicalResult findOptimal<mlir::ReductionNode::iterator<(mlir::TraversalMode)0>>(mlir::ModuleOp, mlir::Region&, mlir::FrozenRewritePatternSet const&, mlir::Tester const&) /proc/self/cwd/external/llvm-project/mlir/lib/Reducer/ReductionTreePass.cpp:159:14
#16 0x000055f2afd4dedd (anonymous namespace)::ReductionTreePass::reduceOp(mlir::ModuleOp, mlir::Region&) /proc/self/cwd/external/llvm-project/mlir/lib/Reducer/ReductionTreePass.cpp:252:12
#17 0x000055f2afd4d68b (anonymous namespace)::ReductionTreePass::runOnOperation() /proc/self/cwd/external/llvm-project/mlir/lib/Reducer/ReductionTreePass.cpp:238:20
#18 0x000055f2b081b58b mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int)::$_1::operator()() const /proc/self/cwd/external/llvm-project/mlir/lib/Pass/Pass.cpp:0:17
#19 0x000055f2b081b525 void llvm::function_ref<void ()>::callback_fn<mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int)::$_1>(long) /proc/self/cwd/external/llvm-project/llvm/include/llvm/ADT/STLFunctionalExtras.h:45:5
#20 0x000055f2ae7049d9 llvm::function_ref<void ()>::operator()() const /proc/self/cwd/external/llvm-project/llvm/include/llvm/ADT/STLFunctionalExtras.h:68:5
#21 0x000055f2b081e635 void mlir::MLIRContext::executeAction<mlir::PassExecutionAction, mlir::Pass&>(llvm::function_ref<void ()>, llvm::ArrayRef<mlir::IRUnit>, mlir::Pass&) /proc/self/cwd/external/llvm-project/mlir/include/mlir/IR/MLIRContext.h:281:3
#22 0x000055f2b0815e03 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) /proc/self/cwd/external/llvm-project/mlir/lib/Pass/Pass.cpp:532:17
#23 0x000055f2b08165bf mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) /proc/self/cwd/external/llvm-project/mlir/lib/Pass/Pass.cpp:592:16
#24 0x000055f2b0818998 mlir::PassManager::runPasses(mlir::Operation*, mlir::AnalysisManager) /proc/self/cwd/external/llvm-project/mlir/lib/Pass/Pass.cpp:905:10
#25 0x000055f2b081880e mlir::PassManager::run(mlir::Operation*) /proc/self/cwd/external/llvm-project/mlir/lib/Pass/Pass.cpp:885:60
#26 0x000055f2acdfdadf mlir::mlirReduceMain(int, char**, mlir::MLIRContext&) /proc/self/cwd/external/llvm-project/mlir/lib/Tools/mlir-reduce/MlirReduceMain.cpp:108:17
#27 0x000055f2a65a4464 main /proc/self/cwd/external/llvm-project/mlir/tools/mlir-reduce/mlir-reduce.cpp:40:17
#28 0x00007f16b8d23d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#29 0x00007f16b8d23e40 call_init ./csu/../csu/libc-start.c:128:20
#30 0x00007f16b8d23e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#31 0x000055f2a65a4325 _start (bazel-bin/external/llvm-project/mlir/mlir-reduce+0x52d8325)
[1]    17696 segmentation fault  bazel-bin/external/llvm-project/mlir/mlir-reduce

@jpienaar do you see anything obvious I'm doing wrong in my example that would cause this stack trace?

@j2kun j2kun requested a review from jpienaar November 13, 2024 18:05
@llvmbot llvmbot added the mlir label Nov 13, 2024
@j2kun j2kun marked this pull request as draft November 13, 2024 18:05
@llvmbot
Copy link
Member

llvmbot commented Nov 13, 2024

@llvm/pr-subscribers-mlir

Author: Jeremy Kun (j2kun)

Changes

This PR was started because I noticed bugs in the docs:

  • The interestingness test checked for an error, but did not redirect stderr to stdout to include it in the pipe.
  • The interestingness test pipe ended with grep, but then checked for an exit code of 1 to mark the input as interesting. However, a pipe's overall exit code is the exit code of the rightmost command, and grep's exit code is zero when it detects the query string, and 1 when it does not detect the query string. So the test was backwards.

Then I wanted to test my fixes to be sure I got it right, and I realized that the doc has no properly runnable example. I tried adapting some tests but the use of the test dialect gave me some issues, so I felt it would be more straightforward to add a test that only uses builtin dialects. Because I could not find an upstream error to demonstrate, I instead just made the test check for the presence of a particular op, and the example I came up with hits a stack trace in the reduce tool:

PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: bazel-bin/external/llvm-project/mlir/mlir-reduce /home/j2kun/fhe/heir/query-test.mlir "-reduction-tree=traversal-mode=0 test=query-test.sh"
 #<!-- -->0 0x000055f2b0c7f4ad llvm::sys::PrintStackTrace(llvm::raw_ostream&amp;, int) /proc/self/cwd/external/llvm-project/llvm/lib/Support/Unix/Signals.inc:723:11
 #<!-- -->1 0x000055f2b0c7f99b PrintStackTraceSignalHandler(void*) /proc/self/cwd/external/llvm-project/llvm/lib/Support/Unix/Signals.inc:798:1
 #<!-- -->2 0x000055f2b0c7d056 llvm::sys::RunSignalHandlers() /proc/self/cwd/external/llvm-project/llvm/lib/Support/Signals.cpp:105:5
 #<!-- -->3 0x000055f2b0c80095 SignalHandler(int) /proc/self/cwd/external/llvm-project/llvm/lib/Support/Unix/Signals.inc:413:1
 #<!-- -->4 0x00007f16b8d3c520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #<!-- -->5 0x000055f2a66fb5b4 mlir::Operation::getNumOperands() /proc/self/cwd/external/llvm-project/mlir/include/mlir/IR/Operation.h:342:12
 #<!-- -->6 0x000055f2b0b43c6a mlir::Operation::clone(mlir::IRMapping&amp;, mlir::Operation::CloneOptions) /proc/self/cwd/external/llvm-project/mlir/lib/IR/Operation.cpp:723:22
 #<!-- -->7 0x000055f2afd53ec3 mlir::ReductionNode::initialize(mlir::ModuleOp, mlir::Region&amp;) /proc/self/cwd/external/llvm-project/mlir/lib/Reducer/ReductionNode.cpp:42:41
 #<!-- -->8 0x000055f2afd53d8b mlir::ReductionNode::ReductionNode(mlir::ReductionNode*, std::vector&lt;std::pair&lt;int, int&gt;, std::allocator&lt;std::pair&lt;int, int&gt;&gt;&gt; const&amp;, llvm::SpecificBumpPtrAllocator&lt;mlir::ReductionNode&gt;&amp;) /proc/self/cwd/external/llvm-project/mlir/lib/Reducer/ReductionNode.cpp:34:16
 #<!-- -->9 0x000055f2afd54731 mlir::ReductionNode::generateNewVariants()::$_1::operator()(std::vector&lt;std::pair&lt;int, int&gt;, std::allocator&lt;std::pair&lt;int, int&gt;&gt;&gt; const&amp;) const /proc/self/cwd/external/llvm-project/mlir/lib/Reducer/ReductionNode.cpp:56:39
#<!-- -->10 0x000055f2afd543d2 mlir::ReductionNode::generateNewVariants() /proc/self/cwd/external/llvm-project/mlir/lib/Reducer/ReductionNode.cpp:92:22
#<!-- -->11 0x000055f2afd549fd mlir::ReductionNode::iterator&lt;(mlir::TraversalMode)0&gt;::getNeighbors(mlir::ReductionNode*) /proc/self/cwd/external/llvm-project/mlir/lib/Reducer/ReductionNode.cpp:154:16
#<!-- -->12 0x000055f2afd52bed mlir::ReductionNode::BaseIterator&lt;mlir::ReductionNode::iterator&lt;(mlir::TraversalMode)0&gt;&gt;::getNeighbors(mlir::ReductionNode*) /proc/self/cwd/external/llvm-project/mlir/include/mlir/Reducer/ReductionNode.h:135:38
#<!-- -->13 0x000055f2afd4fbc6 mlir::ReductionNode::BaseIterator&lt;mlir::ReductionNode::iterator&lt;(mlir::TraversalMode)0&gt;&gt;::operator++() /proc/self/cwd/external/llvm-project/mlir/include/mlir/Reducer/ReductionNode.h:119:34
#<!-- -->14 0x000055f2afd4e745 llvm::LogicalResult findOptimal&lt;mlir::ReductionNode::iterator&lt;(mlir::TraversalMode)0&gt;&gt;(mlir::ModuleOp, mlir::Region&amp;, mlir::FrozenRewritePatternSet const&amp;, mlir::Tester const&amp;, bool) /proc/self/cwd/external/llvm-project/mlir/lib/Reducer/ReductionTreePass.cpp:123:5
#<!-- -->15 0x000055f2afd4e053 llvm::LogicalResult findOptimal&lt;mlir::ReductionNode::iterator&lt;(mlir::TraversalMode)0&gt;&gt;(mlir::ModuleOp, mlir::Region&amp;, mlir::FrozenRewritePatternSet const&amp;, mlir::Tester const&amp;) /proc/self/cwd/external/llvm-project/mlir/lib/Reducer/ReductionTreePass.cpp:159:14
#<!-- -->16 0x000055f2afd4dedd (anonymous namespace)::ReductionTreePass::reduceOp(mlir::ModuleOp, mlir::Region&amp;) /proc/self/cwd/external/llvm-project/mlir/lib/Reducer/ReductionTreePass.cpp:252:12
#<!-- -->17 0x000055f2afd4d68b (anonymous namespace)::ReductionTreePass::runOnOperation() /proc/self/cwd/external/llvm-project/mlir/lib/Reducer/ReductionTreePass.cpp:238:20
#<!-- -->18 0x000055f2b081b58b mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int)::$_1::operator()() const /proc/self/cwd/external/llvm-project/mlir/lib/Pass/Pass.cpp:0:17
#<!-- -->19 0x000055f2b081b525 void llvm::function_ref&lt;void ()&gt;::callback_fn&lt;mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int)::$_1&gt;(long) /proc/self/cwd/external/llvm-project/llvm/include/llvm/ADT/STLFunctionalExtras.h:45:5
#<!-- -->20 0x000055f2ae7049d9 llvm::function_ref&lt;void ()&gt;::operator()() const /proc/self/cwd/external/llvm-project/llvm/include/llvm/ADT/STLFunctionalExtras.h:68:5
#<!-- -->21 0x000055f2b081e635 void mlir::MLIRContext::executeAction&lt;mlir::PassExecutionAction, mlir::Pass&amp;&gt;(llvm::function_ref&lt;void ()&gt;, llvm::ArrayRef&lt;mlir::IRUnit&gt;, mlir::Pass&amp;) /proc/self/cwd/external/llvm-project/mlir/include/mlir/IR/MLIRContext.h:281:3
#<!-- -->22 0x000055f2b0815e03 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) /proc/self/cwd/external/llvm-project/mlir/lib/Pass/Pass.cpp:532:17
#<!-- -->23 0x000055f2b08165bf mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&amp;, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) /proc/self/cwd/external/llvm-project/mlir/lib/Pass/Pass.cpp:592:16
#<!-- -->24 0x000055f2b0818998 mlir::PassManager::runPasses(mlir::Operation*, mlir::AnalysisManager) /proc/self/cwd/external/llvm-project/mlir/lib/Pass/Pass.cpp:905:10
#<!-- -->25 0x000055f2b081880e mlir::PassManager::run(mlir::Operation*) /proc/self/cwd/external/llvm-project/mlir/lib/Pass/Pass.cpp:885:60
#<!-- -->26 0x000055f2acdfdadf mlir::mlirReduceMain(int, char**, mlir::MLIRContext&amp;) /proc/self/cwd/external/llvm-project/mlir/lib/Tools/mlir-reduce/MlirReduceMain.cpp:108:17
#<!-- -->27 0x000055f2a65a4464 main /proc/self/cwd/external/llvm-project/mlir/tools/mlir-reduce/mlir-reduce.cpp:40:17
#<!-- -->28 0x00007f16b8d23d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#<!-- -->29 0x00007f16b8d23e40 call_init ./csu/../csu/libc-start.c:128:20
#<!-- -->30 0x00007f16b8d23e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#<!-- -->31 0x000055f2a65a4325 _start (bazel-bin/external/llvm-project/mlir/mlir-reduce+0x52d8325)
[1]    17696 segmentation fault  bazel-bin/external/llvm-project/mlir/mlir-reduce

@jpienaar do you see anything obvious I'm doing wrong in my example that would cause this stack trace?


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

1 Files Affected:

  • (modified) mlir/docs/Tools/mlir-reduce.md (+48-9)
diff --git a/mlir/docs/Tools/mlir-reduce.md b/mlir/docs/Tools/mlir-reduce.md
index b60cdd799ba0c6..e01b57ef0bab45 100644
--- a/mlir/docs/Tools/mlir-reduce.md
+++ b/mlir/docs/Tools/mlir-reduce.md
@@ -26,27 +26,66 @@ to the tree traversal strategy. The different strategies may lead to different
 results and different time complexity. You can run as
 `-reduction-tree='traversal-mode=0'` to select the mode for example.
 
+### Example MLIR input
+
+```mlir
+// query-test.mlir
+func.func @func1() {
+  // A func can be pruned if it's not relevant to the error.
+  return
+}
+
+func.func @func2() -> i32 {
+  %0 = arith.constant 1 : i32
+  %1 = arith.constant 2 : i32
+  %2 = arith.constant 2.2 : f32
+  %3 = arith.constant 5.3 : f32
+  %4 = arith.addi %0, %1 : i32
+  %5 = arith.addf %2, %3 : f32
+  %6 = arith.muli %4, %4 : i32
+  %7 = arith.subi %6, %4 : i32
+  %8 = arith.fptosi %5 : f32 to i32
+  %9 = arith.addi %7, %8 : i32
+  return %9 : i32
+}
+```
+
 ### Write the script for testing interestingness
 
-As mentioned, you need to provide a command to `mlir-reduce` which identifies
-cases you're interested in. For each intermediate output generated during
-reduction, `mlir-reduce` will run the command over the it, the script should
-returns 1 for interesting case, 0 otherwise. The sample script,
+You need to provide a command to `mlir-reduce` which identifies cases you're
+interested in. For each intermediate output generated during reduction,
+`mlir-reduce` will run the command over the it, the script should returns 1 for
+interesting case, 0 otherwise. For the IR above, a sample script might simply
+look for the presence of the `arith.fptosi` operation. A more realistic script
+would check for the presence of a particular kind of error message in stderr.
 
 ```shell
-mlir-opt -convert-vector-to-spirv $1 | grep "failed to materialize"
-if [[ $? -eq 1 ]]; then
+# query-test.sh
+# `2>&1` redirects stderr (where errors and diagnostics are printed) to stdout
+# so it can be piped to grep.
+mlir-opt $1 2>&1 | grep "arith.fptosi"
+
+# grep's exit code is 0 if the queried string is found
+if [[ $? -eq 0 ]]; then
   exit 1
 else
   exit 0
 fi
 ```
 
-The sample usage will be like, note that the `test` argument is part of the mode
-argument.
+### Running the example
+
+The sample usage will be as follows, noting that the `test` argument is part of
+the mode argument.
 
 ```shell
-mlir-reduce $INPUT -reduction-tree='traversal-mode=0 test=$TEST_SCRIPT'
+mlir-reduce query-test.mlir -reduction-tree='traversal-mode=0 test=query-test.sh'
+```
+
+The output:
+
+```
+TODO: the usage above produces a stack trace
 ```
 
 ## Available reduction strategies

Copy link
Member

@jpienaar jpienaar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a nice addition. Sorry didn't notice before as it was/is draft.

@j2kun
Copy link
Contributor Author

j2kun commented Mar 10, 2025

@jpienaar I'm happy to merge this as-is, if you think it would be an improvement over what's there (even with the TODO?), but I still have questions about how to properly test it. Do you have an example that I can reproduce that only uses upstream dialects?

@j2kun j2kun marked this pull request as ready for review March 10, 2025 04:32
@j2kun j2kun force-pushed the mlir-reduce-docs branch from b970411 to 63e660f Compare March 10, 2025 04:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants