Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dialects: (builtin) Parse @ module names #3664

Merged
merged 9 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tests/filecheck/dialects/builtin/module.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ builtin.module {
builtin.module attributes {a = "foo", b = "bar", unit} {}
// CHECK-NEXT: builtin.module attributes {"a" = "foo", "b" = "bar", "unit"} {
// CHECK-NEXT: }
builtin.module @moduleName {}
// CHECK-NEXT: builtin.module @moduleName {
// CHECK-NEXT: }
builtin.module @otherModule attributes {dialect.attr} {}
// CHECK-NEXT: builtin.module @otherModule attributes {"dialect.attr"} {
// CHECK-NEXT: }
module {}
// CHECK-NEXT: builtin.module {
// CHECK-NEXT: }
Expand Down
15 changes: 11 additions & 4 deletions xdsl/dialects/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
irdl_attr_definition,
irdl_op_definition,
irdl_to_attr_constraint,
opt_attr_def,
opt_prop_def,
region_def,
traits_def,
var_operand_def,
Expand Down Expand Up @@ -1628,7 +1628,7 @@ def verify(self):
class ModuleOp(IRDLOperation):
name = "builtin.module"

sym_name = opt_attr_def(StringAttr)
sym_name = opt_prop_def(StringAttr)

body = region_def("single_block")

Expand All @@ -1643,21 +1643,25 @@ def __init__(
self,
ops: list[Operation] | Region,
attributes: Mapping[str, Attribute] | None = None,
sym_name: StringAttr | None = None,
):
if attributes is None:
attributes = {}
if isinstance(ops, Region):
region = ops
else:
region = Region(Block(ops))
super().__init__(regions=[region], attributes=attributes)
properties: dict[str, Attribute | None] = {"sym_name": sym_name}
super().__init__(regions=[region], attributes=attributes, properties=properties)

@property
def ops(self) -> BlockOps:
return self.body.ops

@classmethod
def parse(cls, parser: Parser) -> ModuleOp:
module_name = parser.parse_optional_symbol_name()

attributes = parser.parse_optional_attr_dict_with_keyword()
if attributes is not None:
attributes = attributes.data
Expand All @@ -1667,9 +1671,12 @@ def parse(cls, parser: Parser) -> ModuleOp:
if not region.blocks:
region.add_block(Block())

return ModuleOp(region, attributes)
return ModuleOp(region, attributes, module_name)

def print(self, printer: Printer) -> None:
if self.sym_name is not None:
printer.print(f" @{self.sym_name.data}")

if len(self.attributes) != 0:
printer.print(" attributes ")
printer.print_op_attributes(self.attributes)
Expand Down
Loading