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

misc: add ShrinkException for easy test case shrinking #3610

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions xdsl/utils/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@ class InvalidIRException(Exception):
pass


class ShrinkException(Exception):
"""
Exception used for shrinking purposes.

When using DRMacIver's [Shrink Ray](https://github.com/DRMacIver/shrinkray) library,
this exception can be used to reduce test cases. For example, to find a smaller
version of a case that has some behavior you are interested in, raise this exception
on the line of code you want to hit, and pass the `--shrink` argument to `xdsl-opt`.

Shrinkray currently requires the executable passed in to take no arguments, so to
shrink a test case like this one that raises a ShrinkException when you call it like
this: `xdsl-opt input_file.mlir -p my,pass,pipeline`, you need to create a new
executable `.sh` file with the following contents:

```sh
xdsl-opt -p my,pass,pipeline --shrink
```

It can then be invoked with `shrinkray FILENAME.sh input_file.mlir`.
"""

pass


class InterpretationError(Exception):
"""
An error that can be raised during interpretation, or Interpreter setup.
Expand Down
16 changes: 15 additions & 1 deletion xdsl/xdsl_opt_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from xdsl.printer import Printer
from xdsl.tools.command_line_tool import CommandLineTool
from xdsl.transforms import get_all_passes
from xdsl.utils.exceptions import DiagnosticException
from xdsl.utils.exceptions import DiagnosticException, ShrinkException
from xdsl.utils.parse_pipeline import parse_pipeline


Expand Down Expand Up @@ -75,9 +75,16 @@ def run(self):
output_stream.flush()
finally:
chunk.close()
except ShrinkException:
assert self.args.shrink
print("Success, can shrink")
exit(0)
finally:
if output_stream is not sys.stdout:
output_stream.close()
if self.args.shrink:
print("Failure, can't shrink")
exit(1)

def register_all_arguments(self, arg_parser: argparse.ArgumentParser):
"""
Expand Down Expand Up @@ -171,6 +178,13 @@ def register_all_arguments(self, arg_parser: argparse.ArgumentParser):
version=f"xdsl-opt built from xdsl version {version('xdsl')}\n",
)

arg_parser.add_argument(
"--shrink",
default=False,
action="store_true",
help="Return success on exit if ShrinkException was raised.",
)

def register_pass(
self, pass_name: str, pass_factory: Callable[[], type[ModulePass]]
):
Expand Down
Loading