Skip to content

Ml program

ml_program

MLProgram = Dialect('ml_program', [GlobalOp, GlobalLoadConstantOp]) module-attribute

GlobalOp

Bases: IRDLOperation

Module level declaration of a global variable

See external documentation.

Source code in xdsl/dialects/ml_program.py
 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
111
112
113
@irdl_op_definition
class GlobalOp(IRDLOperation):
    """
    Module level declaration of a global variable


    See external [documentation](https://mlir.llvm.org/docs/Dialects/MLProgramOps/#ml_programglobal-ml_programglobalop).
    """

    name = "ml_program.global"

    sym_name = attr_def(SymbolNameConstraint())
    type = attr_def(TypeAttribute)
    is_mutable = opt_attr_def(UnitAttr)
    value = opt_attr_def()
    sym_visibility = attr_def(StringAttr)

    traits = traits_def(SymbolOpInterface())

    def __init__(
        self,
        sym_name: Attribute,
        type: Attribute,
        is_mutable: Attribute | None,
        value: Attribute | None,
        sym_visibility: Attribute,
    ):
        super().__init__(
            attributes={
                "sym_name": sym_name,
                "type": type,
                "is_mutable": is_mutable,
                "value": value,
                "sym_visibility": sym_visibility,
            },
        )

    def _verify(self) -> None:
        if isinstance(self.is_mutable, NoneType) and isinstance(self.value, NoneType):
            raise VerifyException("Immutable global must have an initial value")

    def print(self, printer: Printer):
        printer.print_string(" ")
        if self.sym_visibility:
            printer.print_string(self.sym_visibility.data)
            printer.print_string(" ")
        if self.is_mutable:
            printer.print_string("mutable ")
        printer.print_string("@")
        printer.print_string(self.sym_name.data)
        if self.value:
            printer.print_string("(")
            printer.print_attribute(self.value)
            printer.print_string(")")
        printer.print_string(" : ")
        printer.print_attribute(self.type)

    @classmethod
    def parse(cls, parser: Parser) -> Self:
        attrs = parser.parse_optional_attr_dict()
        sym_visibility = parser.parse_visibility_keyword()

        if parser.parse_optional_keyword("mutable"):
            is_mutable = UnitAttr()
        else:
            is_mutable = None
        sym_name = parser.parse_symbol_name().data
        if parser.parse_optional_punctuation("("):
            value = parser.parse_attribute()
            parser.parse_punctuation(")")
        else:
            value = None
        parser.parse_punctuation(":")
        type = parser.parse_type()
        global_op = cls(
            StringAttr(sym_name),
            type,
            is_mutable,
            value,
            sym_visibility,
        )
        global_op.attributes |= attrs
        return global_op

name = 'ml_program.global' class-attribute instance-attribute

sym_name = attr_def(SymbolNameConstraint()) class-attribute instance-attribute

type = attr_def(TypeAttribute) class-attribute instance-attribute

is_mutable = opt_attr_def(UnitAttr) class-attribute instance-attribute

value = opt_attr_def() class-attribute instance-attribute

sym_visibility = attr_def(StringAttr) class-attribute instance-attribute

traits = traits_def(SymbolOpInterface()) class-attribute instance-attribute

__init__(sym_name: Attribute, type: Attribute, is_mutable: Attribute | None, value: Attribute | None, sym_visibility: Attribute)

Source code in xdsl/dialects/ml_program.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def __init__(
    self,
    sym_name: Attribute,
    type: Attribute,
    is_mutable: Attribute | None,
    value: Attribute | None,
    sym_visibility: Attribute,
):
    super().__init__(
        attributes={
            "sym_name": sym_name,
            "type": type,
            "is_mutable": is_mutable,
            "value": value,
            "sym_visibility": sym_visibility,
        },
    )

print(printer: Printer)

Source code in xdsl/dialects/ml_program.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def print(self, printer: Printer):
    printer.print_string(" ")
    if self.sym_visibility:
        printer.print_string(self.sym_visibility.data)
        printer.print_string(" ")
    if self.is_mutable:
        printer.print_string("mutable ")
    printer.print_string("@")
    printer.print_string(self.sym_name.data)
    if self.value:
        printer.print_string("(")
        printer.print_attribute(self.value)
        printer.print_string(")")
    printer.print_string(" : ")
    printer.print_attribute(self.type)

parse(parser: Parser) -> Self classmethod

Source code in xdsl/dialects/ml_program.py
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
@classmethod
def parse(cls, parser: Parser) -> Self:
    attrs = parser.parse_optional_attr_dict()
    sym_visibility = parser.parse_visibility_keyword()

    if parser.parse_optional_keyword("mutable"):
        is_mutable = UnitAttr()
    else:
        is_mutable = None
    sym_name = parser.parse_symbol_name().data
    if parser.parse_optional_punctuation("("):
        value = parser.parse_attribute()
        parser.parse_punctuation(")")
    else:
        value = None
    parser.parse_punctuation(":")
    type = parser.parse_type()
    global_op = cls(
        StringAttr(sym_name),
        type,
        is_mutable,
        value,
        sym_visibility,
    )
    global_op.attributes |= attrs
    return global_op

GlobalLoadConstantOp

Bases: IRDLOperation

Direct load a constant value from a global

See external documentation.

Source code in xdsl/dialects/ml_program.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
@irdl_op_definition
class GlobalLoadConstantOp(IRDLOperation):
    """
    Direct load a constant value from a global


    See external [documentation](https://mlir.llvm.org/docs/Dialects/MLProgramOps/#ml_programglobal_load_const-ml_programgloballoadconstop).
    """

    name = "ml_program.global_load_const"

    global_attr = attr_def(SymbolRefAttr)
    result = result_def()

    def __init__(
        self,
        global_attr: Attribute,
        result_type: Attribute | None,
    ):
        super().__init__(
            attributes={
                "global_attr": global_attr,
            },
            result_types=[result_type],
        )

    def print(self, printer: Printer):
        printer.print_string(" ")
        printer.print_attribute(self.global_attr)
        printer.print_string(" : ")
        printer.print_attribute(self.result.type)

    @classmethod
    def parse(cls, parser: Parser) -> Self:
        attrs = parser.parse_optional_attr_dict()
        global_attr = parser.parse_attribute()
        parser.parse_punctuation(":")
        result_type = parser.parse_attribute()

        global_const = cls(
            global_attr,
            result_type,
        )
        global_const.attributes |= attrs
        return global_const

name = 'ml_program.global_load_const' class-attribute instance-attribute

global_attr = attr_def(SymbolRefAttr) class-attribute instance-attribute

result = result_def() class-attribute instance-attribute

__init__(global_attr: Attribute, result_type: Attribute | None)

Source code in xdsl/dialects/ml_program.py
130
131
132
133
134
135
136
137
138
139
140
def __init__(
    self,
    global_attr: Attribute,
    result_type: Attribute | None,
):
    super().__init__(
        attributes={
            "global_attr": global_attr,
        },
        result_types=[result_type],
    )

print(printer: Printer)

Source code in xdsl/dialects/ml_program.py
142
143
144
145
146
def print(self, printer: Printer):
    printer.print_string(" ")
    printer.print_attribute(self.global_attr)
    printer.print_string(" : ")
    printer.print_attribute(self.result.type)

parse(parser: Parser) -> Self classmethod

Source code in xdsl/dialects/ml_program.py
148
149
150
151
152
153
154
155
156
157
158
159
160
@classmethod
def parse(cls, parser: Parser) -> Self:
    attrs = parser.parse_optional_attr_dict()
    global_attr = parser.parse_attribute()
    parser.parse_punctuation(":")
    result_type = parser.parse_attribute()

    global_const = cls(
        global_attr,
        result_type,
    )
    global_const.attributes |= attrs
    return global_const