Skip to content

Command line tool

command_line_tool

CommandLineTool

Source code in xdsl/tools/command_line_tool.py
class CommandLineTool:
    ctx: Context
    args: argparse.Namespace
    """
    The argument parsers namespace which holds the parsed commandline
    attributes.
    """

    available_frontends: dict[
        str, Callable[[IO[bytes]], ModuleOp] | Callable[[], type[Frontend]]
    ]
    """
    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 = ",".join(self.available_frontends)
        arg_parser.add_argument(
            "-f",
            "--frontend",
            type=str,
            required=False,
            help=f"Frontend to be used for the input. If not set, "
            f"the xdsl frontend or the one for the file extension "
            f"is used. Available frontends are: {frontends}",
        )
        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[bytes], str]:
        """
        Get the binary input stream to parse from, along with the file extension.
        """
        if self.args.input_file is None:
            f = sys.stdin.buffer
            file_extension = "mlir"
        else:
            f = open(self.args.input_file, "rb")
            _, 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.
        """
        from xdsl.universe import Universe

        multiverse = Universe.get_multiverse()
        for frontend_name, frontend_factory in multiverse.all_frontends.items():
            if frontend_name not in self.available_frontends:
                self.available_frontends[frontend_name] = frontend_factory

    def parse_chunk(
        self, chunk: IO[bytes], file_extension: str, start_offset: int = 0
    ) -> ModuleOp | None:
        """
        Parse the input file by invoking the frontend specified.
        Uses self.args.frontend if provided, otherwise falls back to file_extension.
        """
        # Determine frontend key (from -f flag or file extension)
        frontend_key = self.args.frontend or file_extension

        # Parse spec (handles both "mlir" and "mlir{arg=val}")
        frontend_spec = parse_spec(frontend_key)
        frontend_entry = self.available_frontends[frontend_spec.name]
        sig = inspect.signature(frontend_entry)

        if not sig.parameters:
            # New-style: factory -> Frontend class
            factory = cast(Callable[[], type[Frontend]], frontend_entry)
            frontend = factory().from_spec(frontend_spec)

            # Handle --no-implicit-module for MLIR frontend (backward compat, overrides spec)
            if frontend_spec.name == "mlir":
                frontend = dataclasses.replace(
                    frontend,
                    no_implicit_module=self.args.no_implicit_module,
                )

            return frontend.parse(self.ctx, chunk, self.get_input_name())
        else:
            # Legacy: function (IO[bytes]) -> ModuleOp (spec args silently ignored)
            legacy = cast(Callable[[IO[bytes]], ModuleOp], frontend_entry)
            return legacy(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[bytes]], ModuleOp] | Callable[[], type[Frontend]]] 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
def register_all_arguments(self, arg_parser: argparse.ArgumentParser):
    arg_parser.add_argument(
        "input_file", type=str, nargs="?", help="path to input file"
    )

    frontends = ",".join(self.available_frontends)
    arg_parser.add_argument(
        "-f",
        "--frontend",
        type=str,
        required=False,
        help=f"Frontend to be used for the input. If not set, "
        f"the xdsl frontend or the one for the file extension "
        f"is used. Available frontends are: {frontends}",
    )
    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[bytes], str]

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

Source code in xdsl/tools/command_line_tool.py
def get_input_stream(self) -> tuple[IO[bytes], str]:
    """
    Get the binary input stream to parse from, along with the file extension.
    """
    if self.args.input_file is None:
        f = sys.stdin.buffer
        file_extension = "mlir"
    else:
        f = open(self.args.input_file, "rb")
        _, 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
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
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
def register_all_frontends(self):
    """
    Register all frontends that can be used.

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

    multiverse = Universe.get_multiverse()
    for frontend_name, frontend_factory in multiverse.all_frontends.items():
        if frontend_name not in self.available_frontends:
            self.available_frontends[frontend_name] = frontend_factory

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

Parse the input file by invoking the frontend specified. Uses self.args.frontend if provided, otherwise falls back to file_extension.

Source code in xdsl/tools/command_line_tool.py
def parse_chunk(
    self, chunk: IO[bytes], file_extension: str, start_offset: int = 0
) -> ModuleOp | None:
    """
    Parse the input file by invoking the frontend specified.
    Uses self.args.frontend if provided, otherwise falls back to file_extension.
    """
    # Determine frontend key (from -f flag or file extension)
    frontend_key = self.args.frontend or file_extension

    # Parse spec (handles both "mlir" and "mlir{arg=val}")
    frontend_spec = parse_spec(frontend_key)
    frontend_entry = self.available_frontends[frontend_spec.name]
    sig = inspect.signature(frontend_entry)

    if not sig.parameters:
        # New-style: factory -> Frontend class
        factory = cast(Callable[[], type[Frontend]], frontend_entry)
        frontend = factory().from_spec(frontend_spec)

        # Handle --no-implicit-module for MLIR frontend (backward compat, overrides spec)
        if frontend_spec.name == "mlir":
            frontend = dataclasses.replace(
                frontend,
                no_implicit_module=self.args.no_implicit_module,
            )

        return frontend.parse(self.ctx, chunk, self.get_input_name())
    else:
        # Legacy: function (IO[bytes]) -> ModuleOp (spec args silently ignored)
        legacy = cast(Callable[[IO[bytes]], ModuleOp], frontend_entry)
        return legacy(chunk)