-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ccec8a
commit 7d69937
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from io import StringIO | ||
from typing import Any | ||
|
||
from xdsl.ir import Operation | ||
from xdsl.printer import Printer | ||
from xdsl.utils.diagnostic import Diagnostic | ||
|
||
|
||
def assert_print_op( | ||
operation: Operation, | ||
expected: str, | ||
diagnostic: Diagnostic | None, | ||
print_generic_format: bool = True, | ||
print_debuginfo: bool = False, | ||
**printer_options: Any, | ||
): | ||
""" | ||
Utility function that helps to check the printing of an operation compared to | ||
some string | ||
Examples | ||
-------- | ||
To check that an operation, namely Add prints as: | ||
expected = \ | ||
builtin.module() { | ||
%0 : !i32 = arith.addi(%<UNKNOWN> : !i32, %<UNKNOWN> : !i32) | ||
-----------------------^^^^^^^^^^---------------------------------------------------------------- | ||
| ERROR: SSAValue is not part of the IR, are you sure all operations are added before their uses? | ||
------------------------------------------------------------------------------------------------- | ||
------------------------------------------^^^^^^^^^^--------------------------------------------- | ||
| ERROR: SSAValue is not part of the IR, are you sure all operations are added before their uses? | ||
------------------------------------------------------------------------------------------------- | ||
%1 : !i32 = arith.addi(%0 : !i32, %0 : !i32) | ||
} | ||
we call: | ||
.. code-block:: python | ||
assert_print_op(add, expected) | ||
Additional options can be passed to the printer using keyword arguments: | ||
.. code-block:: python | ||
assert_print_op(add, expected, print_unknown_value_error=False) | ||
""" | ||
|
||
file = StringIO("") | ||
if diagnostic is None: | ||
diagnostic = Diagnostic() | ||
printer = Printer( | ||
stream=file, | ||
print_generic_format=print_generic_format, | ||
print_debuginfo=print_debuginfo, | ||
diagnostic=diagnostic, | ||
**printer_options, | ||
) | ||
|
||
printer.print(operation) | ||
assert file.getvalue().strip() == expected.strip() |