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: (symref) migrate to init and properties #3817

Merged
merged 1 commit into from
Feb 4, 2025
Merged
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
dialects: (symref) migrate to init and properties
superlopuh committed Feb 1, 2025
commit 2abd55bc0e840d079dea3c8325ea9c1bec0b774e
6 changes: 3 additions & 3 deletions xdsl/frontend/code_generation.py
Original file line number Diff line number Diff line change
@@ -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:
@@ -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:
23 changes: 10 additions & 13 deletions xdsl/frontend/symref.py
Original file line number Diff line number Diff line change
@@ -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(