Skip to content

Commit

Permalink
dialects: Add constant operations in asl
Browse files Browse the repository at this point in the history
  • Loading branch information
math-fehr committed Dec 7, 2024
1 parent aded9a7 commit 220d53a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
48 changes: 44 additions & 4 deletions asl_xdsl/dialects/asl.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from collections.abc import Sequence
from collections.abc import Mapping, Sequence

from xdsl.dialects import builtin
from xdsl.ir import (
Expand All @@ -11,8 +11,15 @@
TypeAttribute,
VerifyException,
)
from xdsl.irdl import ParameterDef, irdl_attr_definition
from xdsl.parser import AttrParser
from xdsl.irdl import (
IRDLOperation,
ParameterDef,
irdl_attr_definition,
irdl_op_definition,
prop_def,
result_def,
)
from xdsl.parser import AttrParser, Parser
from xdsl.printer import Printer


Expand Down Expand Up @@ -159,8 +166,41 @@ def print_parameters(self, printer: Printer) -> None:
printer.print(">")


@irdl_op_definition
class ConstantBoolOp(IRDLOperation):
"""A constant boolean operation."""

name = "asl.constant_bool"

value = prop_def(BoolAttr)
res = result_def(BoolType())

def __init__(self, value: bool | BoolAttr, attr_dict: Mapping[str, Attribute] = {}):
if isinstance(value, bool):
value = BoolAttr(value)
super().__init__(
result_types=[BoolType()],
properties={"value": value},
attributes=attr_dict,
)

@classmethod
def parse(cls, parser: Parser) -> ConstantBoolOp:
"""Parse the operation."""
value = parser.parse_boolean()
attr_dict = parser.parse_optional_attr_dict()
return ConstantBoolOp(value, attr_dict)

def print(self, printer: Printer) -> None:
"""Print the operation."""
printer.print(" ", "true" if self.value else "false")
if self.attributes:
printer.print(" ")
printer.print_attr_dict(self.attributes)


ASLDialect = Dialect(
"asl",
[],
[ConstantBoolOp],
[BoolType, BoolAttr, IntegerType, IntegerAttr, BitVectorType, BitVectorAttr],
)
11 changes: 11 additions & 0 deletions tests/filecheck/dialects/asl/constant_ops.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: asl-opt %s | asl-opt %s | filecheck %s

builtin.module {
%true = asl.constant_bool true {attr_dict}
%false = asl.constant_bool false {attr_dict}
}

// CHECK: builtin.module {
// CHECK-NEXT: %true = asl.constant_bool true {"attr_dict"}
// CHECK-NEXT: %false = asl.constant_bool true {"attr_dict"}
// CHECK-NEXT: }

0 comments on commit 220d53a

Please sign in to comment.