-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dialects: (riscv) Add HasInsntrait and implement trait for some opera…
…tions (#2784) In order to make progress on #2468 I factored out the insn representation. This will hopefully also make it usable for the RISC-V backend efforts. --------- Co-authored-by: Joren Dumoulin <joren.dumoulin@kuleuven.be>
- Loading branch information
1 parent
ae1e3bf
commit 772df11
Showing
4 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import pytest | ||
|
||
from xdsl.dialects import riscv_snitch | ||
from xdsl.dialects.riscv import RISCVInstruction | ||
from xdsl.traits import HasInsnRepresentation | ||
|
||
ground_truth = { | ||
"dmsrc": ".insn r 0x2b, 0, 0, x0, {0}, {1}", | ||
"dmdst": ".insn r 0x2b, 0, 1, x0, {0}, {1}", | ||
"dmcpyi": ".insn r 0x2b, 0, 2, {0}, {1}, {2}", | ||
"dmcpy": ".insn r 0x2b, 0, 3, {0}, {1}, {2}", | ||
"dmstati": ".insn r 0x2b, 0, 4, {0}, {1}, {2}", | ||
"dmstat": ".insn r 0x2b, 0, 5, {0}, {1}, {2}", | ||
"dmstr": ".insn r 0x2b, 0, 6, x0, {0}, {1}", | ||
"dmrep": ".insn r 0x2b, 0, 7, x0, {0}, x0", | ||
} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"op", | ||
( | ||
riscv_snitch.DMSourceOp, | ||
riscv_snitch.DMDestinationOp, | ||
riscv_snitch.DMCopyImmOp, | ||
riscv_snitch.DMCopyOp, | ||
riscv_snitch.DMStatImmOp, | ||
riscv_snitch.DMStatOp, | ||
riscv_snitch.DMStrideOp, | ||
riscv_snitch.DMRepOp, | ||
), | ||
) | ||
def test_insn_repr(op: RISCVInstruction): | ||
trait = op.get_trait(HasInsnRepresentation) | ||
assert trait is not None | ||
# Limitation of Pyright, see https://github.com/microsoft/pyright/issues/7105 | ||
# We are currently stuck on an older version of Pyright, the update is | ||
# tracked in https://github.com/xdslproject/xdsl/issues/2791 | ||
assert ( | ||
trait.get_insn(op) # pyright: ignore[reportGeneralTypeIssues] | ||
== ground_truth[op.name[13:]] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from dataclasses import dataclass, field | ||
|
||
from xdsl.ir import Operation | ||
from xdsl.traits import HasInsnRepresentation | ||
|
||
|
||
@dataclass(frozen=True) | ||
class StaticInsnRepresentation(HasInsnRepresentation): | ||
""" | ||
Returns the first parameter as an insn template string. | ||
See https://sourceware.org/binutils/docs/as/RISC_002dV_002dDirectives.html for more information | ||
""" | ||
|
||
insn: str = field(kw_only=True) | ||
|
||
def get_insn(self, op: Operation) -> str: | ||
""" | ||
Return the insn representation of the operation for printing. | ||
""" | ||
return self.insn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters