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

tests: Add test for xdsl_opt passes #320

Merged
merged 1 commit into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions tests/xdsl_opt/constant_program.xdsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
builtin.module() {
%0 : !i32 = arith.constant() ["value" = 5 : !i32]
}
26 changes: 26 additions & 0 deletions tests/xdsl_opt/test_xdsl_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from contextlib import redirect_stdout
from io import StringIO
import os
from xdsl.ir import MLContext
from xdsl.dialects.builtin import ModuleOp
from xdsl.rewriter import Rewriter


def test_opt():
Expand Down Expand Up @@ -72,3 +75,26 @@ def test_print_to_file():
expected = open(filename_out, 'r').read()

assert inp.strip() == expected.strip()


def test_operation_deletion():
filename_in = 'tests/xdsl_opt/constant_program.xdsl'
filename_out = 'tests/xdsl_opt/empty_program.xdsl'

class xDSLOptMainPass(xDSLOptMain):

def register_all_passes(self):

def remove_constant(ctx: MLContext, module: ModuleOp):
module.ops[0].detach()

self.available_passes['remove-constant'] = remove_constant

opt = xDSLOptMainPass(args=[filename_in, '-p', 'remove-constant'])

f = StringIO("")
with redirect_stdout(f):
opt.run()
expected = open(filename_out, 'r').read()

assert f.getvalue().strip() == expected.strip()
10 changes: 7 additions & 3 deletions xdsl/xdsl_opt_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ class xDSLOptMain:
attributes.
"""

available_frontends: Dict[str, Callable[[IOBase], ModuleOp]] = {}
available_frontends: Dict[str, Callable[[IOBase], ModuleOp]]
"""
A mapping from file extension to a frontend that can handle this
file type.
"""

available_passes: Dict[str, Callable[[MLContext, ModuleOp], None]] = {}
available_passes: Dict[str, Callable[[MLContext, ModuleOp], None]]
"""
A mapping from pass names to functions that apply the pass to a ModuleOp.
"""

available_targets: Dict[str, Callable[[ModuleOp, IOBase], None]] = {}
available_targets: Dict[str, Callable[[ModuleOp, IOBase], None]]
"""
A mapping from target names to functions that serialize a ModuleOp into a
stream.
Expand All @@ -56,6 +56,10 @@ class xDSLOptMain:
def __init__(self,
description: str = 'xDSL modular optimizer driver',
args=None):
self.available_frontends = {}
self.available_passes = {}
self.available_targets = {}

self.ctx = MLContext()
self.register_all_dialects()
self.register_all_frontends()
Expand Down