Skip to content

Builder

builder

PyASTBuilder dataclass

Builder for xDSL modules from aspects of a Python function.

Source code in xdsl/frontend/pyast/utils/builder.py
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
@dataclass
class PyASTBuilder:
    """Builder for xDSL modules from aspects of a Python function."""

    type_registry: TypeRegistry
    """Mappings between source code and IR type."""

    function_registry: FunctionRegistry
    """Mappings between functions and their operation types."""

    file: str | None
    """The file path of the function being built."""

    globals: dict[str, Any]
    """Global information for the function being built, including all the imports."""

    function_ast: ast.FunctionDef
    """The AST tree for the function being built."""

    build_context: Context
    """The xDSL context to use when applying transformations to the built module."""

    post_transforms: PassPipeline
    """An ordered list of passes and callbacks to apply to the built module."""

    def build(self) -> ModuleOp:
        """Build a module from the builder state."""
        # Convert the Python AST into xDSL IR objects
        type_converter = TypeConverter(
            self.globals,
            self.type_registry,
            self.function_registry,
        )
        module = CodeGeneration.run_with_type_converter(
            type_converter,
            self.function_ast,
            self.file,
        )
        module.verify()

        self.post_transforms.apply(self.build_context.clone(), module)
        return module

type_registry: TypeRegistry instance-attribute

Mappings between source code and IR type.

function_registry: FunctionRegistry instance-attribute

Mappings between functions and their operation types.

file: str | None instance-attribute

The file path of the function being built.

globals: dict[str, Any] instance-attribute

Global information for the function being built, including all the imports.

function_ast: ast.FunctionDef instance-attribute

The AST tree for the function being built.

build_context: Context instance-attribute

The xDSL context to use when applying transformations to the built module.

post_transforms: PassPipeline instance-attribute

An ordered list of passes and callbacks to apply to the built module.

__init__(type_registry: TypeRegistry, function_registry: FunctionRegistry, file: str | None, globals: dict[str, Any], function_ast: ast.FunctionDef, build_context: Context, post_transforms: PassPipeline) -> None

build() -> ModuleOp

Build a module from the builder state.

Source code in xdsl/frontend/pyast/utils/builder.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def build(self) -> ModuleOp:
    """Build a module from the builder state."""
    # Convert the Python AST into xDSL IR objects
    type_converter = TypeConverter(
        self.globals,
        self.type_registry,
        self.function_registry,
    )
    module = CodeGeneration.run_with_type_converter(
        type_converter,
        self.function_ast,
        self.file,
    )
    module.verify()

    self.post_transforms.apply(self.build_context.clone(), module)
    return module