xref: /llvm-project/mlir/python/mlir/dialects/builtin.py (revision 537b2aa264c5a9879a80289c8d123b39e520eb15)
19f3f6d7bSStella Laurenzo#  Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
29f3f6d7bSStella Laurenzo#  See https://llvm.org/LICENSE.txt for license information.
39f3f6d7bSStella Laurenzo#  SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
49f3f6d7bSStella Laurenzo
5*537b2aa2SMaksim Leventalfrom typing import Dict, Optional
6*537b2aa2SMaksim Levental
79f3f6d7bSStella Laurenzofrom ._builtin_ops_gen import *
8a2288a89SMaksim Leventalfrom ._builtin_ops_gen import _Dialect
9*537b2aa2SMaksim Leventalfrom ..extras.meta import region_op
10a2288a89SMaksim Levental
11a2288a89SMaksim Leventaltry:
12a2288a89SMaksim Levental    from ..ir import *
13a2288a89SMaksim Levental    from ._ods_common import _cext as _ods_cext
14a2288a89SMaksim Leventalexcept ImportError as e:
15a2288a89SMaksim Levental    raise RuntimeError("Error loading imports from extension module") from e
16a2288a89SMaksim Levental
17a2288a89SMaksim Levental
18a2288a89SMaksim Levental@_ods_cext.register_operation(_Dialect, replace=True)
19a2288a89SMaksim Leventalclass ModuleOp(ModuleOp):
20a2288a89SMaksim Levental    """Specialization for the module op class."""
21a2288a89SMaksim Levental
22a2288a89SMaksim Levental    def __init__(self, *, loc=None, ip=None):
23a2288a89SMaksim Levental        super().__init__(loc=loc, ip=ip)
24a2288a89SMaksim Levental        body = self.regions[0].blocks.append()
25a2288a89SMaksim Levental
26a2288a89SMaksim Levental    @property
27a2288a89SMaksim Levental    def body(self):
28a2288a89SMaksim Levental        return self.regions[0].blocks[0]
29*537b2aa2SMaksim Levental
30*537b2aa2SMaksim Levental
31*537b2aa2SMaksim Levental@region_op
32*537b2aa2SMaksim Leventaldef module(
33*537b2aa2SMaksim Levental    *,
34*537b2aa2SMaksim Levental    sym_name=None,
35*537b2aa2SMaksim Levental    sym_visibility=None,
36*537b2aa2SMaksim Levental    attrs: Optional[Dict[str, Attribute]] = None,
37*537b2aa2SMaksim Levental    loc=None,
38*537b2aa2SMaksim Levental    ip=None,
39*537b2aa2SMaksim Levental):
40*537b2aa2SMaksim Levental    mod = ModuleOp.__base__(
41*537b2aa2SMaksim Levental        sym_name=sym_name, sym_visibility=sym_visibility, loc=loc, ip=ip
42*537b2aa2SMaksim Levental    )
43*537b2aa2SMaksim Levental    if attrs is None:
44*537b2aa2SMaksim Levental        attrs = {}
45*537b2aa2SMaksim Levental    for attr_name, attr in attrs.items():
46*537b2aa2SMaksim Levental        mod.operation.attributes[attr_name] = attr
47*537b2aa2SMaksim Levental
48*537b2aa2SMaksim Levental    return mod
49