Skip to content

Commit

Permalink
dialects: (symref) migrate to init and properties
Browse files Browse the repository at this point in the history
  • Loading branch information
superlopuh committed Feb 1, 2025
1 parent 73d3592 commit 2abd55b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
6 changes: 3 additions & 3 deletions xdsl/frontend/code_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,8 @@ def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
symbol_name = str(arg.arg)
block_arg = entry_block.insert_arg(argument_types[i], i)
self.symbol_table[symbol_name] = argument_types[i]
entry_block.add_op(symref.DeclareOp.get(symbol_name))
entry_block.add_op(symref.UpdateOp.get(symbol_name, block_arg))
entry_block.add_op(symref.DeclareOp(symbol_name))
entry_block.add_op(symref.UpdateOp(symbol_name, block_arg))

# Parse function body.
for stmt in node.body:
Expand Down Expand Up @@ -567,7 +567,7 @@ def visit_expr(expr: ast.expr) -> tuple[Attribute, Region]:
self.inserter.insert_op(op)

def visit_Name(self, node: ast.Name):
fetch_op = symref.FetchOp.get(node.id, self.get_symbol(node))
fetch_op = symref.FetchOp(node.id, self.get_symbol(node))
self.inserter.insert_op(fetch_op)

def visit_Pass(self, node: ast.Pass) -> None:
Expand Down
23 changes: 10 additions & 13 deletions xdsl/frontend/symref.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,52 @@
from xdsl.ir import Attribute, Dialect, Operation, SSAValue
from xdsl.irdl import (
IRDLOperation,
attr_def,
irdl_op_definition,
operand_def,
prop_def,
result_def,
)


@irdl_op_definition
class DeclareOp(IRDLOperation):
name = "symref.declare"
sym_name = attr_def(StringAttr)
sym_name = prop_def(StringAttr)

assembly_format = "$sym_name attr-dict"

@staticmethod
def get(sym_name: str | StringAttr) -> DeclareOp:
def __init__(self, sym_name: str | StringAttr):
if isinstance(sym_name, str):
sym_name = StringAttr(sym_name)
return DeclareOp.build(attributes={"sym_name": sym_name})
super().__init__(properties={"sym_name": sym_name})


@irdl_op_definition
class FetchOp(IRDLOperation):
name = "symref.fetch"
value = result_def()
symbol = attr_def(SymbolRefAttr)
symbol = prop_def(SymbolRefAttr)

assembly_format = "$symbol attr-dict `:` type($value)"

@staticmethod
def get(symbol: str | SymbolRefAttr, result_type: Attribute) -> FetchOp:
def __init__(self, symbol: str | SymbolRefAttr, result_type: Attribute):
if isinstance(symbol, str):
symbol = SymbolRefAttr(symbol)
return FetchOp.build(attributes={"symbol": symbol}, result_types=[result_type])
super().__init__(properties={"symbol": symbol}, result_types=[result_type])


@irdl_op_definition
class UpdateOp(IRDLOperation):
name = "symref.update"
value = operand_def()
symbol = attr_def(SymbolRefAttr)
symbol = prop_def(SymbolRefAttr)

assembly_format = "$symbol `=` $value attr-dict `:` type($value)"

@staticmethod
def get(symbol: str | SymbolRefAttr, value: Operation | SSAValue) -> UpdateOp:
def __init__(self, symbol: str | SymbolRefAttr, value: Operation | SSAValue):
if isinstance(symbol, str):
symbol = SymbolRefAttr(symbol)
return UpdateOp.build(operands=[value], attributes={"symbol": symbol})
super().__init__(operands=[value], properties={"symbol": symbol})


Symref = Dialect(
Expand Down

0 comments on commit 2abd55b

Please sign in to comment.