Skip to content

Command line tool

command_line_tool

CommandLineTool

Source code in xdsl/tools/command_line_tool.py
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
class CommandLineTool:
    ctx: Context
    args: argparse.Namespace
    """
    The argument parsers namespace which holds the parsed commandline
    attributes.
    """

    available_frontends: dict[str, Callable[[IO[str]], ModuleOp]]
    """
    A mapping from file extension to a frontend that can handle this
    file type.
    """

    def register_all_arguments(self, arg_parser: argparse.ArgumentParser):
        arg_parser.add_argument(
            "input_file", type=str, nargs="?", help="path to input file"
        )

        frontends = [name for name in self.available_frontends]
        arg_parser.add_argument(
            "-f",
            "--frontend",
            type=str,
            required=False,
            choices=frontends,
            help="Frontend to be used for the input. If not set, "
            "the xdsl frontend or the one for the file extension "
            "is used.",
        )
        arg_parser.add_argument("--disable-verify", default=False, action="store_true")

        arg_parser.add_argument(
            "--allow-unregistered-dialect",
            default=False,
            action="store_true",
            help="Allow the parsing of unregistered dialects.",
        )

        arg_parser.add_argument(
            "--no-implicit-module",
            default=False,
            action="store_true",
            help="Disable implicit addition of a top-level module op during parsing.",
        )

    def get_input_stream(self) -> tuple[IO[str], str]:
        """
        Get the input stream to parse from, along with the file extension.
        """
        if self.args.input_file is None:
            f = sys.stdin
            file_extension = "mlir"
        else:
            f = open(self.args.input_file)
            _, file_extension = os.path.splitext(self.args.input_file)
            file_extension = file_extension.replace(".", "")
        return f, file_extension

    def get_input_name(self):
        return self.args.input_file or "stdin"

    def register_all_dialects(self):
        """
        Register all dialects that can be used.

        Add other/additional dialects by overloading this function.
        """
        from xdsl.universe import Universe

        multiverse = Universe.get_multiverse()
        for dialect_name, dialect_factory in multiverse.all_dialects.items():
            self.ctx.register_dialect(dialect_name, dialect_factory)

    def register_all_frontends(self):
        """
        Register all frontends that can be used.

        Add other/additional frontends by overloading this function.
        """

        def parse_mlir(io: IO[str]):
            return Parser(
                self.ctx,
                io.read(),
                self.get_input_name(),
            ).parse_module(not self.args.no_implicit_module)

        self.available_frontends["mlir"] = parse_mlir

    def parse_chunk(
        self, chunk: IO[str], file_extension: str, start_offset: int = 0
    ) -> ModuleOp | None:
        """
        Parse the input file by invoking the parser specified by the `parser`
        argument. If not set, the parser registered for this file extension
        is used.
        """
        return self.available_frontends[file_extension](chunk)

ctx: Context instance-attribute

args: argparse.Namespace instance-attribute

The argument parsers namespace which holds the parsed commandline attributes.

available_frontends: dict[str, Callable[[IO[str]], ModuleOp]] instance-attribute

A mapping from file extension to a frontend that can handle this file type.

register_all_arguments(arg_parser: argparse.ArgumentParser)

Source code in xdsl/tools/command_line_tool.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def register_all_arguments(self, arg_parser: argparse.ArgumentParser):
    arg_parser.add_argument(
        "input_file", type=str, nargs="?", help="path to input file"
    )

    frontends = [name for name in self.available_frontends]
    arg_parser.add_argument(
        "-f",
        "--frontend",
        type=str,
        required=False,
        choices=frontends,
        help="Frontend to be used for the input. If not set, "
        "the xdsl frontend or the one for the file extension "
        "is used.",
    )
    arg_parser.add_argument("--disable-verify", default=False, action="store_true")

    arg_parser.add_argument(
        "--allow-unregistered-dialect",
        default=False,
        action="store_true",
        help="Allow the parsing of unregistered dialects.",
    )

    arg_parser.add_argument(
        "--no-implicit-module",
        default=False,
        action="store_true",
        help="Disable implicit addition of a top-level module op during parsing.",
    )

get_input_stream() -> tuple[IO[str], str]

Get the input stream to parse from, along with the file extension.

Source code in xdsl/tools/command_line_tool.py
58
59
60
61
62
63
64
65
66
67
68
69
def get_input_stream(self) -> tuple[IO[str], str]:
    """
    Get the input stream to parse from, along with the file extension.
    """
    if self.args.input_file is None:
        f = sys.stdin
        file_extension = "mlir"
    else:
        f = open(self.args.input_file)
        _, file_extension = os.path.splitext(self.args.input_file)
        file_extension = file_extension.replace(".", "")
    return f, file_extension

get_input_name()

Source code in xdsl/tools/command_line_tool.py
71
72
def get_input_name(self):
    return self.args.input_file or "stdin"

register_all_dialects()

Register all dialects that can be used.

Add other/additional dialects by overloading this function.

Source code in xdsl/tools/command_line_tool.py
74
75
76
77
78
79
80
81
82
83
84
def register_all_dialects(self):
    """
    Register all dialects that can be used.

    Add other/additional dialects by overloading this function.
    """
    from xdsl.universe import Universe

    multiverse = Universe.get_multiverse()
    for dialect_name, dialect_factory in multiverse.all_dialects.items():
        self.ctx.register_dialect(dialect_name, dialect_factory)

register_all_frontends()

Register all frontends that can be used.

Add other/additional frontends by overloading this function.

Source code in xdsl/tools/command_line_tool.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def register_all_frontends(self):
    """
    Register all frontends that can be used.

    Add other/additional frontends by overloading this function.
    """

    def parse_mlir(io: IO[str]):
        return Parser(
            self.ctx,
            io.read(),
            self.get_input_name(),
        ).parse_module(not self.args.no_implicit_module)

    self.available_frontends["mlir"] = parse_mlir

parse_chunk(chunk: IO[str], file_extension: str, start_offset: int = 0) -> ModuleOp | None

Parse the input file by invoking the parser specified by the parser argument. If not set, the parser registered for this file extension is used.

Source code in xdsl/tools/command_line_tool.py
102
103
104
105
106
107
108
109
110
def parse_chunk(
    self, chunk: IO[str], file_extension: str, start_offset: int = 0
) -> ModuleOp | None:
    """
    Parse the input file by invoking the parser specified by the `parser`
    argument. If not set, the parser registered for this file extension
    is used.
    """
    return self.available_frontends[file_extension](chunk)