From 3bc90a75bf9273c8340ac40b675c6ce2b1195f37 Mon Sep 17 00:00:00 2001 From: Sasha Lopoukhine Date: Mon, 9 Dec 2024 21:41:04 +0000 Subject: [PATCH 1/3] misc: add ShrinkException for easy test case shrinking --- xdsl/utils/exceptions.py | 24 ++++++++++++++++++++++++ xdsl/xdsl_opt_main.py | 16 +++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/xdsl/utils/exceptions.py b/xdsl/utils/exceptions.py index d3c15d99fa..d0c460456c 100644 --- a/xdsl/utils/exceptions.py +++ b/xdsl/utils/exceptions.py @@ -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. diff --git a/xdsl/xdsl_opt_main.py b/xdsl/xdsl_opt_main.py index 830295f63f..99f5a1286d 100644 --- a/xdsl/xdsl_opt_main.py +++ b/xdsl/xdsl_opt_main.py @@ -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 @@ -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): """ @@ -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]] ): From 4f40af03314f8a28f5e2f42897c73ea42ed6b0b1 Mon Sep 17 00:00:00 2001 From: Sasha Lopoukhine Date: Wed, 11 Dec 2024 11:41:32 +0000 Subject: [PATCH 2/3] add comments --- xdsl/xdsl_opt_main.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xdsl/xdsl_opt_main.py b/xdsl/xdsl_opt_main.py index 99f5a1286d..903bd9e3e7 100644 --- a/xdsl/xdsl_opt_main.py +++ b/xdsl/xdsl_opt_main.py @@ -78,12 +78,14 @@ def run(self): except ShrinkException: assert self.args.shrink print("Success, can shrink") + # Exit with value 0 to let shrinkray know that it 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 with non-0 value to let shrinkray know that it cannot shrink exit(1) def register_all_arguments(self, arg_parser: argparse.ArgumentParser): From 178ad1b08e94e9221bf90a98ca081eea5fd983af Mon Sep 17 00:00:00 2001 From: Sasha Lopoukhine Date: Mon, 16 Dec 2024 14:22:49 +0000 Subject: [PATCH 3/3] simplify ShrinkException doc comment --- xdsl/utils/exceptions.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/xdsl/utils/exceptions.py b/xdsl/utils/exceptions.py index d0c460456c..b4dcac24f3 100644 --- a/xdsl/utils/exceptions.py +++ b/xdsl/utils/exceptions.py @@ -48,16 +48,9 @@ class ShrinkException(Exception): 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`. + To shrink a test case that raises a ShrinkException when called like this: + `xdsl-opt input_file.mlir -p my,pass,pipeline`, it needs to be changed to: + `shrinkray "xdsl-opt -p my,pass,pipeline --shrink" input_file.mlir`. """ pass