-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
base: main
Are you sure you want to change the base?
Conversation
@llvm/pr-subscribers-mlir Author: Jeremy Kun (j2kun) ChangesThis PR was started because I noticed bugs in the docs:
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
@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:
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
|
4c5b388
to
b970411
Compare
There was a problem hiding this 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.
@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? |
This PR was started because I noticed bugs in the docs:
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:@jpienaar do you see anything obvious I'm doing wrong in my example that would cause this stack trace?