Skip to content

Riscv cf

riscv_cf

RISCV_Cf = Dialect('riscv_cf', [JOp, BranchOp, BeqOp, BneOp, BltOp, BgeOp, BltuOp, BgeuOp, BnezOp]) module-attribute

ConditionalBranchOperation

Bases: RISCVInstruction, HasCanonicalizationPatternsInterface, ABC

A base class for RISC-V branch operations. Lowers to RsRsOffOperation.

Source code in xdsl/dialects/riscv_cf.py
class ConditionalBranchOperation(
    riscv.abstract_ops.RISCVInstruction, HasCanonicalizationPatternsInterface, ABC
):
    """
    A base class for RISC-V branch operations. Lowers to RsRsOffOperation.
    """

    rs1 = operand_def(riscv.registers.IntRegisterType)
    rs2 = operand_def(riscv.registers.IntRegisterType)

    then_arguments = var_operand_def(riscv.registers.RISCVRegisterType)
    else_arguments = var_operand_def(riscv.registers.RISCVRegisterType)

    irdl_options = (AttrSizedOperandSegments(),)

    then_block = successor_def()
    else_block = successor_def()

    traits = traits_def(IsTerminator())

    def __init__(
        self,
        rs1: OperandArg,
        rs2: OperandArg,
        then_arguments: Sequence[SSAValue],
        else_arguments: Sequence[SSAValue],
        then_block: Successor,
        else_block: Successor,
        *,
        comment: str | StringAttr | None = None,
    ):
        if isinstance(comment, str):
            comment = StringAttr(comment)

        super().__init__(
            operands=[rs1, rs2, then_arguments, else_arguments],
            attributes={
                "comment": comment,
            },
            successors=(then_block, else_block),
        )

    def verify_(self) -> None:
        # The then block must start with a label op

        then_block_first_op = self.then_block.first_op

        if not isinstance(then_block_first_op, riscv.ops.LabelOp):
            raise VerifyException(
                "riscv_cf branch op then block first op must be a label"
            )

        # Types of arguments must match arg types of blocks
        _verify_successor_args(self.then_arguments, self.then_block)
        _verify_successor_args(self.else_arguments, self.else_block)

        # The else block must be the one immediately following this one

        parent_block = self.parent
        if parent_block is None:
            return

        parent_region = parent_block.parent
        if parent_region is None:
            return

        if parent_block.next_block is not self.else_block:
            raise VerifyException(
                "riscv_cf branch op else block must be immediately after op"
            )

    def assembly_line_args(
        self,
    ) -> tuple[riscv.abstract_ops.AssemblyInstructionArg, ...]:
        then_label = self.then_block.first_op
        assert isinstance(then_label, riscv.ops.LabelOp)
        return self.rs1, self.rs2, then_label.label

    def print(self, printer: Printer) -> None:
        printer.print_string(" ")
        _print_type_pair(printer, self.rs1)
        printer.print_string(", ")
        _print_type_pair(printer, self.rs2)
        printer.print_string(", ")
        printer.print_block_name(self.then_block)
        printer.print_string("(")
        printer.print_list(
            self.then_arguments, lambda val: _print_type_pair(printer, val)
        )
        printer.print_string("), ")
        printer.print_block_name(self.else_block)
        printer.print_string("(")
        printer.print_list(
            self.else_arguments, lambda val: _print_type_pair(printer, val)
        )
        printer.print_string(")")
        if self.attributes:
            printer.print_op_attributes(
                self.attributes,
                reserved_attr_names="operandSegmentSizes",
                print_keyword=True,
            )

    @classmethod
    def parse(cls, parser: Parser) -> Self:
        rs1 = _parse_type_pair(parser)
        parser.parse_punctuation(",")
        rs2 = _parse_type_pair(parser)
        parser.parse_punctuation(",")
        then_block = parser.parse_successor()
        then_args = parser.parse_comma_separated_list(
            parser.Delimiter.PAREN, lambda: _parse_type_pair(parser)
        )
        parser.parse_punctuation(",")
        else_block = parser.parse_successor()
        else_args = parser.parse_comma_separated_list(
            parser.Delimiter.PAREN, lambda: _parse_type_pair(parser)
        )
        attrs = parser.parse_optional_attr_dict_with_keyword()
        op = cls(rs1, rs2, then_args, else_args, then_block, else_block)
        if attrs is not None:
            op.attributes |= attrs.data
        return op

    @abstractmethod
    def const_evaluate(self, rs1: int, rs2: int, bitwidth: int) -> bool:
        pass

    @classmethod
    def get_canonicalization_patterns(cls) -> tuple[RewritePattern, ...]:
        from xdsl.transforms.canonicalization_patterns.riscv_cf import (
            ElideConstantBranches,
        )

        return (ElideConstantBranches(),)

rs1 = operand_def(riscv.registers.IntRegisterType) class-attribute instance-attribute

rs2 = operand_def(riscv.registers.IntRegisterType) class-attribute instance-attribute

then_arguments = var_operand_def(riscv.registers.RISCVRegisterType) class-attribute instance-attribute

else_arguments = var_operand_def(riscv.registers.RISCVRegisterType) class-attribute instance-attribute

irdl_options = (AttrSizedOperandSegments(),) class-attribute instance-attribute

then_block = successor_def() class-attribute instance-attribute

else_block = successor_def() class-attribute instance-attribute

traits = traits_def(IsTerminator()) class-attribute instance-attribute

__init__(rs1: OperandArg, rs2: OperandArg, then_arguments: Sequence[SSAValue], else_arguments: Sequence[SSAValue], then_block: Successor, else_block: Successor, *, comment: str | StringAttr | None = None)

Source code in xdsl/dialects/riscv_cf.py
def __init__(
    self,
    rs1: OperandArg,
    rs2: OperandArg,
    then_arguments: Sequence[SSAValue],
    else_arguments: Sequence[SSAValue],
    then_block: Successor,
    else_block: Successor,
    *,
    comment: str | StringAttr | None = None,
):
    if isinstance(comment, str):
        comment = StringAttr(comment)

    super().__init__(
        operands=[rs1, rs2, then_arguments, else_arguments],
        attributes={
            "comment": comment,
        },
        successors=(then_block, else_block),
    )

verify_() -> None

Source code in xdsl/dialects/riscv_cf.py
def verify_(self) -> None:
    # The then block must start with a label op

    then_block_first_op = self.then_block.first_op

    if not isinstance(then_block_first_op, riscv.ops.LabelOp):
        raise VerifyException(
            "riscv_cf branch op then block first op must be a label"
        )

    # Types of arguments must match arg types of blocks
    _verify_successor_args(self.then_arguments, self.then_block)
    _verify_successor_args(self.else_arguments, self.else_block)

    # The else block must be the one immediately following this one

    parent_block = self.parent
    if parent_block is None:
        return

    parent_region = parent_block.parent
    if parent_region is None:
        return

    if parent_block.next_block is not self.else_block:
        raise VerifyException(
            "riscv_cf branch op else block must be immediately after op"
        )

assembly_line_args() -> tuple[riscv.abstract_ops.AssemblyInstructionArg, ...]

Source code in xdsl/dialects/riscv_cf.py
def assembly_line_args(
    self,
) -> tuple[riscv.abstract_ops.AssemblyInstructionArg, ...]:
    then_label = self.then_block.first_op
    assert isinstance(then_label, riscv.ops.LabelOp)
    return self.rs1, self.rs2, then_label.label

print(printer: Printer) -> None

Source code in xdsl/dialects/riscv_cf.py
def print(self, printer: Printer) -> None:
    printer.print_string(" ")
    _print_type_pair(printer, self.rs1)
    printer.print_string(", ")
    _print_type_pair(printer, self.rs2)
    printer.print_string(", ")
    printer.print_block_name(self.then_block)
    printer.print_string("(")
    printer.print_list(
        self.then_arguments, lambda val: _print_type_pair(printer, val)
    )
    printer.print_string("), ")
    printer.print_block_name(self.else_block)
    printer.print_string("(")
    printer.print_list(
        self.else_arguments, lambda val: _print_type_pair(printer, val)
    )
    printer.print_string(")")
    if self.attributes:
        printer.print_op_attributes(
            self.attributes,
            reserved_attr_names="operandSegmentSizes",
            print_keyword=True,
        )

parse(parser: Parser) -> Self classmethod

Source code in xdsl/dialects/riscv_cf.py
@classmethod
def parse(cls, parser: Parser) -> Self:
    rs1 = _parse_type_pair(parser)
    parser.parse_punctuation(",")
    rs2 = _parse_type_pair(parser)
    parser.parse_punctuation(",")
    then_block = parser.parse_successor()
    then_args = parser.parse_comma_separated_list(
        parser.Delimiter.PAREN, lambda: _parse_type_pair(parser)
    )
    parser.parse_punctuation(",")
    else_block = parser.parse_successor()
    else_args = parser.parse_comma_separated_list(
        parser.Delimiter.PAREN, lambda: _parse_type_pair(parser)
    )
    attrs = parser.parse_optional_attr_dict_with_keyword()
    op = cls(rs1, rs2, then_args, else_args, then_block, else_block)
    if attrs is not None:
        op.attributes |= attrs.data
    return op

const_evaluate(rs1: int, rs2: int, bitwidth: int) -> bool abstractmethod

Source code in xdsl/dialects/riscv_cf.py
@abstractmethod
def const_evaluate(self, rs1: int, rs2: int, bitwidth: int) -> bool:
    pass

get_canonicalization_patterns() -> tuple[RewritePattern, ...] classmethod

Source code in xdsl/dialects/riscv_cf.py
@classmethod
def get_canonicalization_patterns(cls) -> tuple[RewritePattern, ...]:
    from xdsl.transforms.canonicalization_patterns.riscv_cf import (
        ElideConstantBranches,
    )

    return (ElideConstantBranches(),)

BeqOp dataclass

Bases: ConditionalBranchOperation

Take the branch if registers rs1 and rs2 are equal.

if (x[rs1] == x[rs2]) pc += sext(offset)

See external documentation.

Source code in xdsl/dialects/riscv_cf.py
@irdl_op_definition
class BeqOp(ConditionalBranchOperation):
    """
    Take the branch if registers rs1 and rs2 are equal.

    if (x[rs1] == x[rs2]) pc += sext(offset)

    See external [documentation](https://msyksphinz-self.github.io/riscv-isadoc/html/rvi.html#beq).
    """

    name = "riscv_cf.beq"

    def const_evaluate(self, rs1: int, rs2: int, bitwidth: int) -> bool:
        lhs = to_unsigned(rs1, bitwidth)
        rhs = to_unsigned(rs2, bitwidth)
        return lhs == rhs

name = 'riscv_cf.beq' class-attribute instance-attribute

const_evaluate(rs1: int, rs2: int, bitwidth: int) -> bool

Source code in xdsl/dialects/riscv_cf.py
def const_evaluate(self, rs1: int, rs2: int, bitwidth: int) -> bool:
    lhs = to_unsigned(rs1, bitwidth)
    rhs = to_unsigned(rs2, bitwidth)
    return lhs == rhs

BneOp dataclass

Bases: ConditionalBranchOperation

Take the branch if registers rs1 and rs2 are not equal.

if (x[rs1] != x[rs2]) pc += sext(offset)

See external documentation.

Source code in xdsl/dialects/riscv_cf.py
@irdl_op_definition
class BneOp(ConditionalBranchOperation):
    """
    Take the branch if registers rs1 and rs2 are not equal.

    if (x[rs1] != x[rs2]) pc += sext(offset)

    See external [documentation](https://msyksphinz-self.github.io/riscv-isadoc/html/rvi.html#bne).
    """

    name = "riscv_cf.bne"

    def const_evaluate(self, rs1: int, rs2: int, bitwidth: int) -> bool:
        lhs = to_unsigned(rs1, bitwidth)
        rhs = to_unsigned(rs2, bitwidth)
        return lhs != rhs

name = 'riscv_cf.bne' class-attribute instance-attribute

const_evaluate(rs1: int, rs2: int, bitwidth: int) -> bool

Source code in xdsl/dialects/riscv_cf.py
def const_evaluate(self, rs1: int, rs2: int, bitwidth: int) -> bool:
    lhs = to_unsigned(rs1, bitwidth)
    rhs = to_unsigned(rs2, bitwidth)
    return lhs != rhs

BltOp dataclass

Bases: ConditionalBranchOperation

Take the branch if registers rs1 is less than rs2, using signed comparison.

if (x[rs1] <s x[rs2]) pc += sext(offset)

See external documentation.

Source code in xdsl/dialects/riscv_cf.py
@irdl_op_definition
class BltOp(ConditionalBranchOperation):
    """
    Take the branch if registers rs1 is less than rs2, using signed comparison.

    if (x[rs1] <s x[rs2]) pc += sext(offset)

    See external [documentation](https://msyksphinz-self.github.io/riscv-isadoc/html/rvi.html#blt).
    """

    name = "riscv_cf.blt"

    def const_evaluate(self, rs1: int, rs2: int, bitwidth: int) -> bool:
        lhs = to_signed(rs1, bitwidth)
        rhs = to_signed(rs2, bitwidth)
        return lhs < rhs

name = 'riscv_cf.blt' class-attribute instance-attribute

const_evaluate(rs1: int, rs2: int, bitwidth: int) -> bool

Source code in xdsl/dialects/riscv_cf.py
def const_evaluate(self, rs1: int, rs2: int, bitwidth: int) -> bool:
    lhs = to_signed(rs1, bitwidth)
    rhs = to_signed(rs2, bitwidth)
    return lhs < rhs

BgeOp dataclass

Bases: ConditionalBranchOperation

Take the branch if registers rs1 is greater than or equal to rs2, using signed comparison.

if (x[rs1] >=s x[rs2]) pc += sext(offset)

See external documentation.

Source code in xdsl/dialects/riscv_cf.py
@irdl_op_definition
class BgeOp(ConditionalBranchOperation):
    """
    Take the branch if registers rs1 is greater than or equal to rs2, using signed comparison.

    if (x[rs1] >=s x[rs2]) pc += sext(offset)

    See external [documentation](https://msyksphinz-self.github.io/riscv-isadoc/html/rvi.html#bge).
    """

    name = "riscv_cf.bge"

    def const_evaluate(self, rs1: int, rs2: int, bitwidth: int) -> bool:
        lhs = to_signed(rs1, bitwidth)
        rhs = to_signed(rs2, bitwidth)
        return lhs >= rhs

name = 'riscv_cf.bge' class-attribute instance-attribute

const_evaluate(rs1: int, rs2: int, bitwidth: int) -> bool

Source code in xdsl/dialects/riscv_cf.py
def const_evaluate(self, rs1: int, rs2: int, bitwidth: int) -> bool:
    lhs = to_signed(rs1, bitwidth)
    rhs = to_signed(rs2, bitwidth)
    return lhs >= rhs

BltuOp dataclass

Bases: ConditionalBranchOperation

Take the branch if registers rs1 is less than rs2, using unsigned comparison.

if (x[rs1] <u x[rs2]) pc += sext(offset)

See external documentation.

Source code in xdsl/dialects/riscv_cf.py
@irdl_op_definition
class BltuOp(ConditionalBranchOperation):
    """
    Take the branch if registers rs1 is less than rs2, using unsigned comparison.

    if (x[rs1] <u x[rs2]) pc += sext(offset)

    See external [documentation](https://msyksphinz-self.github.io/riscv-isadoc/html/rvi.html#bltu).
    """

    name = "riscv_cf.bltu"

    def const_evaluate(self, rs1: int, rs2: int, bitwidth: int) -> bool:
        lhs = to_unsigned(rs1, bitwidth)
        rhs = to_unsigned(rs2, bitwidth)
        return lhs < rhs

name = 'riscv_cf.bltu' class-attribute instance-attribute

const_evaluate(rs1: int, rs2: int, bitwidth: int) -> bool

Source code in xdsl/dialects/riscv_cf.py
def const_evaluate(self, rs1: int, rs2: int, bitwidth: int) -> bool:
    lhs = to_unsigned(rs1, bitwidth)
    rhs = to_unsigned(rs2, bitwidth)
    return lhs < rhs

BgeuOp dataclass

Bases: ConditionalBranchOperation

Take the branch if registers rs1 is greater than or equal to rs2, using unsigned comparison.

if (x[rs1] >=u x[rs2]) pc += sext(offset)

See external documentation.

Source code in xdsl/dialects/riscv_cf.py
@irdl_op_definition
class BgeuOp(ConditionalBranchOperation):
    """
    Take the branch if registers rs1 is greater than or equal to rs2, using unsigned comparison.

    if (x[rs1] >=u x[rs2]) pc += sext(offset)

    See external [documentation](https://msyksphinz-self.github.io/riscv-isadoc/html/rvi.html#bgeu).
    """

    name = "riscv_cf.bgeu"

    def const_evaluate(self, rs1: int, rs2: int, bitwidth: int) -> bool:
        lhs = to_unsigned(rs1, bitwidth)
        rhs = to_unsigned(rs2, bitwidth)
        return lhs >= rhs

name = 'riscv_cf.bgeu' class-attribute instance-attribute

const_evaluate(rs1: int, rs2: int, bitwidth: int) -> bool

Source code in xdsl/dialects/riscv_cf.py
def const_evaluate(self, rs1: int, rs2: int, bitwidth: int) -> bool:
    lhs = to_unsigned(rs1, bitwidth)
    rhs = to_unsigned(rs2, bitwidth)
    return lhs >= rhs

BranchOp

Bases: RISCVAsmOperation

Branches to a different block, which must follow this operation's block in the parent region. Is not printed in assembly.

Source code in xdsl/dialects/riscv_cf.py
@irdl_op_definition
class BranchOp(riscv.abstract_ops.RISCVAsmOperation):
    """
    Branches to a different block, which must follow this operation's block in the parent
    region. Is not printed in assembly.
    """

    name = "riscv_cf.branch"

    block_arguments = var_operand_def(riscv.registers.RISCVRegisterType)
    successor = successor_def()
    comment = opt_attr_def(StringAttr)
    """
    An optional comment that will be printed along with the instruction.
    """

    traits = traits_def(IsTerminator())

    def __init__(
        self,
        block_arguments: Sequence[SSAValue],
        successor: Successor,
        *,
        comment: str | StringAttr | None = None,
    ):
        if isinstance(comment, str):
            comment = StringAttr(comment)

        super().__init__(
            operands=[block_arguments],
            attributes={
                "comment": comment,
            },
            successors=(successor,),
        )

    def verify_(self) -> None:
        # Types of arguments must match arg types of blocks
        _verify_successor_args(self.block_arguments, self.successor)

        # The successor must immediately follow the parent block.
        if (parent_block := self.parent) is None or (
            parent_region := parent_block.parent
        ) is None:
            return

        parent_index = parent_region.get_block_index(parent_block)
        successor_index = parent_region.get_block_index(self.successor)

        if parent_index + 1 != successor_index:
            raise VerifyException(
                "Successor block must be immediately after parent block in the parent "
                "region."
            )

    def print(self, printer: Printer) -> None:
        printer.print_string(" ")
        printer.print_block_name(self.successor)
        printer.print_string("(")
        printer.print_list(
            self.block_arguments, lambda val: _print_type_pair(printer, val)
        )
        printer.print_string(")")
        if self.attributes:
            printer.print_op_attributes(self.attributes, print_keyword=True)

    @classmethod
    def parse(cls, parser: Parser) -> Self:
        successor = parser.parse_successor()
        block_arguments = parser.parse_comma_separated_list(
            parser.Delimiter.PAREN, lambda: _parse_type_pair(parser)
        )
        attrs = parser.parse_optional_attr_dict_with_keyword()
        op = cls(block_arguments, successor)
        if attrs is not None:
            op.attributes |= attrs.data
        return op

    def assembly_line(self) -> str | None:
        if self.comment is None:
            return None

        return f"    # {self.comment.data}"

name = 'riscv_cf.branch' class-attribute instance-attribute

block_arguments = var_operand_def(riscv.registers.RISCVRegisterType) class-attribute instance-attribute

successor = successor_def() class-attribute instance-attribute

comment = opt_attr_def(StringAttr) class-attribute instance-attribute

An optional comment that will be printed along with the instruction.

traits = traits_def(IsTerminator()) class-attribute instance-attribute

__init__(block_arguments: Sequence[SSAValue], successor: Successor, *, comment: str | StringAttr | None = None)

Source code in xdsl/dialects/riscv_cf.py
def __init__(
    self,
    block_arguments: Sequence[SSAValue],
    successor: Successor,
    *,
    comment: str | StringAttr | None = None,
):
    if isinstance(comment, str):
        comment = StringAttr(comment)

    super().__init__(
        operands=[block_arguments],
        attributes={
            "comment": comment,
        },
        successors=(successor,),
    )

verify_() -> None

Source code in xdsl/dialects/riscv_cf.py
def verify_(self) -> None:
    # Types of arguments must match arg types of blocks
    _verify_successor_args(self.block_arguments, self.successor)

    # The successor must immediately follow the parent block.
    if (parent_block := self.parent) is None or (
        parent_region := parent_block.parent
    ) is None:
        return

    parent_index = parent_region.get_block_index(parent_block)
    successor_index = parent_region.get_block_index(self.successor)

    if parent_index + 1 != successor_index:
        raise VerifyException(
            "Successor block must be immediately after parent block in the parent "
            "region."
        )

print(printer: Printer) -> None

Source code in xdsl/dialects/riscv_cf.py
def print(self, printer: Printer) -> None:
    printer.print_string(" ")
    printer.print_block_name(self.successor)
    printer.print_string("(")
    printer.print_list(
        self.block_arguments, lambda val: _print_type_pair(printer, val)
    )
    printer.print_string(")")
    if self.attributes:
        printer.print_op_attributes(self.attributes, print_keyword=True)

parse(parser: Parser) -> Self classmethod

Source code in xdsl/dialects/riscv_cf.py
@classmethod
def parse(cls, parser: Parser) -> Self:
    successor = parser.parse_successor()
    block_arguments = parser.parse_comma_separated_list(
        parser.Delimiter.PAREN, lambda: _parse_type_pair(parser)
    )
    attrs = parser.parse_optional_attr_dict_with_keyword()
    op = cls(block_arguments, successor)
    if attrs is not None:
        op.attributes |= attrs.data
    return op

assembly_line() -> str | None

Source code in xdsl/dialects/riscv_cf.py
def assembly_line(self) -> str | None:
    if self.comment is None:
        return None

    return f"    # {self.comment.data}"

JOp

Bases: RISCVInstruction

A pseudo-instruction, for unconditional jumps you don't expect to return from. Is equivalent to JalOp with rd = x0. Used to be a part of the spec, removed in 2.0.

Source code in xdsl/dialects/riscv_cf.py
@irdl_op_definition
class JOp(riscv.abstract_ops.RISCVInstruction):
    """
    A pseudo-instruction, for unconditional jumps you don't expect to return from.
    Is equivalent to JalOp with `rd` = `x0`.
    Used to be a part of the spec, removed in 2.0.
    """

    name = "riscv_cf.j"

    block_arguments = var_operand_def(riscv.registers.RISCVRegisterType)

    successor = successor_def()

    traits = traits_def(IsTerminator())

    def __init__(
        self,
        block_arguments: Sequence[SSAValue],
        successor: Successor,
        *,
        comment: str | StringAttr | None = None,
    ):
        if isinstance(comment, str):
            comment = StringAttr(comment)

        super().__init__(
            operands=[block_arguments],
            attributes={
                "comment": comment,
            },
            successors=(successor,),
        )

    def verify_(self) -> None:
        # Types of arguments must match arg types of blocks

        _verify_successor_args(self.block_arguments, self.successor)

        if not isinstance(self.successor.first_op, riscv.ops.LabelOp):
            raise VerifyException(
                "riscv_cf.j operation successor must have a riscv.label operation as a "
                f"first argument, found {self.successor.first_op}"
            )

    def print(self, printer: Printer) -> None:
        printer.print_string(" ")
        printer.print_block_name(self.successor)
        printer.print_string("(")
        printer.print_list(
            self.block_arguments, lambda val: _print_type_pair(printer, val)
        )
        printer.print_string(")")
        if self.attributes:
            printer.print_op_attributes(self.attributes, print_keyword=True)

    @classmethod
    def parse(cls, parser: Parser) -> Self:
        successor = parser.parse_successor()
        block_arguments = parser.parse_comma_separated_list(
            parser.Delimiter.PAREN, lambda: _parse_type_pair(parser)
        )
        attrs = parser.parse_optional_attr_dict_with_keyword()
        op = cls(block_arguments, successor)
        if attrs is not None:
            op.attributes |= attrs.data
        return op

    def assembly_line_args(
        self,
    ) -> tuple[riscv.abstract_ops.AssemblyInstructionArg, ...]:
        dest_label = self.successor.first_op
        assert isinstance(dest_label, riscv.ops.LabelOp)
        return (dest_label.label,)

name = 'riscv_cf.j' class-attribute instance-attribute

block_arguments = var_operand_def(riscv.registers.RISCVRegisterType) class-attribute instance-attribute

successor = successor_def() class-attribute instance-attribute

traits = traits_def(IsTerminator()) class-attribute instance-attribute

__init__(block_arguments: Sequence[SSAValue], successor: Successor, *, comment: str | StringAttr | None = None)

Source code in xdsl/dialects/riscv_cf.py
def __init__(
    self,
    block_arguments: Sequence[SSAValue],
    successor: Successor,
    *,
    comment: str | StringAttr | None = None,
):
    if isinstance(comment, str):
        comment = StringAttr(comment)

    super().__init__(
        operands=[block_arguments],
        attributes={
            "comment": comment,
        },
        successors=(successor,),
    )

verify_() -> None

Source code in xdsl/dialects/riscv_cf.py
def verify_(self) -> None:
    # Types of arguments must match arg types of blocks

    _verify_successor_args(self.block_arguments, self.successor)

    if not isinstance(self.successor.first_op, riscv.ops.LabelOp):
        raise VerifyException(
            "riscv_cf.j operation successor must have a riscv.label operation as a "
            f"first argument, found {self.successor.first_op}"
        )

print(printer: Printer) -> None

Source code in xdsl/dialects/riscv_cf.py
def print(self, printer: Printer) -> None:
    printer.print_string(" ")
    printer.print_block_name(self.successor)
    printer.print_string("(")
    printer.print_list(
        self.block_arguments, lambda val: _print_type_pair(printer, val)
    )
    printer.print_string(")")
    if self.attributes:
        printer.print_op_attributes(self.attributes, print_keyword=True)

parse(parser: Parser) -> Self classmethod

Source code in xdsl/dialects/riscv_cf.py
@classmethod
def parse(cls, parser: Parser) -> Self:
    successor = parser.parse_successor()
    block_arguments = parser.parse_comma_separated_list(
        parser.Delimiter.PAREN, lambda: _parse_type_pair(parser)
    )
    attrs = parser.parse_optional_attr_dict_with_keyword()
    op = cls(block_arguments, successor)
    if attrs is not None:
        op.attributes |= attrs.data
    return op

assembly_line_args() -> tuple[riscv.abstract_ops.AssemblyInstructionArg, ...]

Source code in xdsl/dialects/riscv_cf.py
def assembly_line_args(
    self,
) -> tuple[riscv.abstract_ops.AssemblyInstructionArg, ...]:
    dest_label = self.successor.first_op
    assert isinstance(dest_label, riscv.ops.LabelOp)
    return (dest_label.label,)

BnezOp

Bases: RISCVInstruction

A pseudo-instruction that branches if register rs is not equal to zero. Equivalent to BneOp with rs2 = x0.

if (x[rs] != 0) pc += sext(offset)

Source code in xdsl/dialects/riscv_cf.py
@irdl_op_definition
class BnezOp(riscv.abstract_ops.RISCVInstruction):
    """
    A pseudo-instruction that branches if register rs is not equal to zero.
    Equivalent to BneOp with rs2 = x0.

    if (x[rs] != 0) pc += sext(offset)
    """

    name = "riscv_cf.bnez"

    rs = operand_def(riscv.registers.IntRegisterType)

    then_arguments = var_operand_def(riscv.registers.RISCVRegisterType)
    else_arguments = var_operand_def(riscv.registers.RISCVRegisterType)

    irdl_options = (AttrSizedOperandSegments(),)

    then_block = successor_def()
    else_block = successor_def()

    traits = traits_def(IsTerminator())

    def __init__(
        self,
        rs: OperandArg,
        then_arguments: Sequence[SSAValue],
        else_arguments: Sequence[SSAValue],
        then_block: Successor,
        else_block: Successor,
        *,
        comment: str | StringAttr | None = None,
    ):
        if isinstance(comment, str):
            comment = StringAttr(comment)

        super().__init__(
            operands=[rs, then_arguments, else_arguments],
            attributes={
                "comment": comment,
            },
            successors=(then_block, else_block),
        )

    def verify_(self) -> None:
        if not isinstance(self.then_block.first_op, riscv.ops.LabelOp):
            raise VerifyException(
                "riscv_cf.bnez operation then block must have a riscv.label operation as a "
                f"first argument, found {self.then_block.first_op}"
            )

        _verify_successor_args(self.then_arguments, self.then_block)
        _verify_successor_args(self.else_arguments, self.else_block)

        parent_block = self.parent
        if parent_block is None:
            return

        parent_region = parent_block.parent
        if parent_region is None:
            return

        if parent_block.next_block is not self.else_block:
            raise VerifyException(
                "riscv_cf.bnez operation else block must be immediately after op"
            )

    def print(self, printer: Printer) -> None:
        printer.print_string(" ")
        _print_type_pair(printer, self.rs)
        printer.print_string(", ")
        printer.print_block_name(self.then_block)
        printer.print_string("(")
        printer.print_list(
            self.then_arguments, lambda val: _print_type_pair(printer, val)
        )
        printer.print_string("), ")
        printer.print_block_name(self.else_block)
        printer.print_string("(")
        printer.print_list(
            self.else_arguments, lambda val: _print_type_pair(printer, val)
        )
        printer.print_string(")")
        if self.attributes:
            printer.print_op_attributes(
                self.attributes,
                reserved_attr_names="operandSegmentSizes",
                print_keyword=True,
            )

    @classmethod
    def parse(cls, parser: Parser) -> Self:
        rs = _parse_type_pair(parser)
        parser.parse_punctuation(",")
        then_block = parser.parse_successor()
        then_args = parser.parse_comma_separated_list(
            parser.Delimiter.PAREN, lambda: _parse_type_pair(parser)
        )
        parser.parse_punctuation(",")
        else_block = parser.parse_successor()
        else_args = parser.parse_comma_separated_list(
            parser.Delimiter.PAREN, lambda: _parse_type_pair(parser)
        )
        attrs = parser.parse_optional_attr_dict_with_keyword()
        op = cls(rs, then_args, else_args, then_block, else_block)
        if attrs is not None:
            op.attributes |= attrs.data
        return op

    def assembly_line_args(
        self,
    ) -> tuple[riscv.abstract_ops.AssemblyInstructionArg, ...]:
        then_label = self.then_block.first_op
        assert isinstance(then_label, riscv.ops.LabelOp)
        return (self.rs, then_label.label)

name = 'riscv_cf.bnez' class-attribute instance-attribute

rs = operand_def(riscv.registers.IntRegisterType) class-attribute instance-attribute

then_arguments = var_operand_def(riscv.registers.RISCVRegisterType) class-attribute instance-attribute

else_arguments = var_operand_def(riscv.registers.RISCVRegisterType) class-attribute instance-attribute

irdl_options = (AttrSizedOperandSegments(),) class-attribute instance-attribute

then_block = successor_def() class-attribute instance-attribute

else_block = successor_def() class-attribute instance-attribute

traits = traits_def(IsTerminator()) class-attribute instance-attribute

__init__(rs: OperandArg, then_arguments: Sequence[SSAValue], else_arguments: Sequence[SSAValue], then_block: Successor, else_block: Successor, *, comment: str | StringAttr | None = None)

Source code in xdsl/dialects/riscv_cf.py
def __init__(
    self,
    rs: OperandArg,
    then_arguments: Sequence[SSAValue],
    else_arguments: Sequence[SSAValue],
    then_block: Successor,
    else_block: Successor,
    *,
    comment: str | StringAttr | None = None,
):
    if isinstance(comment, str):
        comment = StringAttr(comment)

    super().__init__(
        operands=[rs, then_arguments, else_arguments],
        attributes={
            "comment": comment,
        },
        successors=(then_block, else_block),
    )

verify_() -> None

Source code in xdsl/dialects/riscv_cf.py
def verify_(self) -> None:
    if not isinstance(self.then_block.first_op, riscv.ops.LabelOp):
        raise VerifyException(
            "riscv_cf.bnez operation then block must have a riscv.label operation as a "
            f"first argument, found {self.then_block.first_op}"
        )

    _verify_successor_args(self.then_arguments, self.then_block)
    _verify_successor_args(self.else_arguments, self.else_block)

    parent_block = self.parent
    if parent_block is None:
        return

    parent_region = parent_block.parent
    if parent_region is None:
        return

    if parent_block.next_block is not self.else_block:
        raise VerifyException(
            "riscv_cf.bnez operation else block must be immediately after op"
        )

print(printer: Printer) -> None

Source code in xdsl/dialects/riscv_cf.py
def print(self, printer: Printer) -> None:
    printer.print_string(" ")
    _print_type_pair(printer, self.rs)
    printer.print_string(", ")
    printer.print_block_name(self.then_block)
    printer.print_string("(")
    printer.print_list(
        self.then_arguments, lambda val: _print_type_pair(printer, val)
    )
    printer.print_string("), ")
    printer.print_block_name(self.else_block)
    printer.print_string("(")
    printer.print_list(
        self.else_arguments, lambda val: _print_type_pair(printer, val)
    )
    printer.print_string(")")
    if self.attributes:
        printer.print_op_attributes(
            self.attributes,
            reserved_attr_names="operandSegmentSizes",
            print_keyword=True,
        )

parse(parser: Parser) -> Self classmethod

Source code in xdsl/dialects/riscv_cf.py
@classmethod
def parse(cls, parser: Parser) -> Self:
    rs = _parse_type_pair(parser)
    parser.parse_punctuation(",")
    then_block = parser.parse_successor()
    then_args = parser.parse_comma_separated_list(
        parser.Delimiter.PAREN, lambda: _parse_type_pair(parser)
    )
    parser.parse_punctuation(",")
    else_block = parser.parse_successor()
    else_args = parser.parse_comma_separated_list(
        parser.Delimiter.PAREN, lambda: _parse_type_pair(parser)
    )
    attrs = parser.parse_optional_attr_dict_with_keyword()
    op = cls(rs, then_args, else_args, then_block, else_block)
    if attrs is not None:
        op.attributes |= attrs.data
    return op

assembly_line_args() -> tuple[riscv.abstract_ops.AssemblyInstructionArg, ...]

Source code in xdsl/dialects/riscv_cf.py
def assembly_line_args(
    self,
) -> tuple[riscv.abstract_ops.AssemblyInstructionArg, ...]:
    then_label = self.then_block.first_op
    assert isinstance(then_label, riscv.ops.LabelOp)
    return (self.rs, then_label.label)