From fb505593183b09b0e64025bc32ac027e933512cb Mon Sep 17 00:00:00 2001 From: "Ka Wing, Li" <68145845+kingiler@users.noreply.github.com> Date: Thu, 13 Jul 2023 15:24:52 +0000 Subject: [PATCH 1/3] core: Provide better error message --- tests/dialects/test_riscv.py | 10 +++++----- xdsl/dialects/riscv.py | 16 ++++++++++++---- xdsl/irdl.py | 5 +++-- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/tests/dialects/test_riscv.py b/tests/dialects/test_riscv.py index 5146e411b6..04e6eb5ade 100644 --- a/tests/dialects/test_riscv.py +++ b/tests/dialects/test_riscv.py @@ -232,17 +232,17 @@ def test_immediate_shift_inst(): riscv.SlliOp(a1, (1 << 5) - 1, rd=riscv.Registers.A0) -def check_float_register(): +def test_float_register(): with pytest.raises(VerifyException): - riscv.RegisterType(riscv.Register("ft9")) + riscv.RegisterType(riscv.Register("ft9")).verify() with pytest.raises(VerifyException): - riscv.FloatRegisterType(riscv.Register("a0")) + riscv.FloatRegisterType(riscv.Register("a0")).verify() a1 = TestSSAValue(riscv.Registers.A1) a2 = TestSSAValue(riscv.Registers.A2) with pytest.raises(VerifyException): - riscv.FAddSOp(a1, a2) + riscv.FAddSOp(a1, a2).verify() f1 = TestSSAValue(riscv.Registers.FT0) f2 = TestSSAValue(riscv.Registers.FT1) - riscv.FAddSOp(f1, f2) + riscv.FAddSOp(f1, f2).verify() diff --git a/xdsl/dialects/riscv.py b/xdsl/dialects/riscv.py index 9e078f012b..5f74d93ec4 100644 --- a/xdsl/dialects/riscv.py +++ b/xdsl/dialects/riscv.py @@ -160,9 +160,13 @@ def print_parameter(self, printer: Printer) -> None: printer.print_string(name) def verify(self) -> None: - if self.data.name is None or self.data.name.startswith("j"): + name = self.data.name + if name is None or name.startswith("j"): return - assert self.data.name in Register.RV32I_INDEX_BY_NAME.keys() + if name not in Register.RV32I_INDEX_BY_NAME.keys(): + raise VerifyException( + f"{name} not in {Register.RV32I_INDEX_BY_NAME.keys()}" + ) @irdl_attr_definition @@ -196,9 +200,13 @@ def print_parameter(self, printer: Printer) -> None: printer.print_string(name) def verify(self) -> None: - if self.data.name is None or self.data.name.startswith("j"): + name = self.data.name + if name is None or name.startswith("j"): return - assert self.data.name in Register.RV32F_INDEX_BY_NAME.keys() + if name not in Register.RV32F_INDEX_BY_NAME.keys(): + raise VerifyException( + f"{name} not in {Register.RV32F_INDEX_BY_NAME.keys()}" + ) class Registers(ABC): diff --git a/xdsl/irdl.py b/xdsl/irdl.py index 73d8d22ba0..dadaeb848b 100644 --- a/xdsl/irdl.py +++ b/xdsl/irdl.py @@ -48,10 +48,10 @@ # pyright: reportMissingParameterType=false, reportUnknownParameterType=false -def error(op: Operation, msg: str): +def error(op: Operation, msg: str, e: Exception): diag = Diagnostic() diag.add_message(op, msg) - diag.raise_exception(f"{op.name} operation does not verify", op) + diag.raise_exception(f"{op.name} operation does not verify", op, type(e), e) class IRDLAnnotations(Enum): @@ -1448,6 +1448,7 @@ def verify_arg(arg: Any, arg_def: Any, arg_idx: int) -> None: op, f"{get_construct_name(construct)} at position " f"{arg_idx} does not verify!\n{e}", + e, ) for def_idx, (_, arg_def) in enumerate(get_construct_defs(op_def, construct)): From cff3036d35030f65243f834cfd27ad73d7e69060 Mon Sep 17 00:00:00 2001 From: "Ka Wing, Li" <68145845+kingiler@users.noreply.github.com> Date: Fri, 14 Jul 2023 11:07:49 +0000 Subject: [PATCH 2/3] dialect: Update test --- tests/dialects/test_riscv.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/dialects/test_riscv.py b/tests/dialects/test_riscv.py index 04e6eb5ade..968556a5c2 100644 --- a/tests/dialects/test_riscv.py +++ b/tests/dialects/test_riscv.py @@ -233,14 +233,14 @@ def test_immediate_shift_inst(): def test_float_register(): - with pytest.raises(VerifyException): - riscv.RegisterType(riscv.Register("ft9")).verify() - with pytest.raises(VerifyException): - riscv.FloatRegisterType(riscv.Register("a0")).verify() + with pytest.raises(VerifyException, match="not in"): + riscv.RegisterType(riscv.Register("ft9")) + with pytest.raises(VerifyException, match="not in"): + riscv.FloatRegisterType(riscv.Register("a0")) a1 = TestSSAValue(riscv.Registers.A1) a2 = TestSSAValue(riscv.Registers.A2) - with pytest.raises(VerifyException): + with pytest.raises(VerifyException, match="Operation does not verify"): riscv.FAddSOp(a1, a2).verify() f1 = TestSSAValue(riscv.Registers.FT0) From 41bb9cb514508e5881c96e188841edf639c23824 Mon Sep 17 00:00:00 2001 From: "Ka Wing, Li" <68145845+kingiler@users.noreply.github.com> Date: Mon, 17 Jul 2023 10:59:52 +0000 Subject: [PATCH 3/3] dialect: Shorter error message --- xdsl/dialects/riscv.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/xdsl/dialects/riscv.py b/xdsl/dialects/riscv.py index d4fd5f7c22..aa9825f2ce 100644 --- a/xdsl/dialects/riscv.py +++ b/xdsl/dialects/riscv.py @@ -173,10 +173,8 @@ def verify(self) -> None: name = self.data.name if name is None or name.startswith("j"): return - if name not in Register.RV32I_INDEX_BY_NAME.keys(): - raise VerifyException( - f"{name} not in {Register.RV32I_INDEX_BY_NAME.keys()}" - ) + if name not in Register.RV32I_INDEX_BY_NAME: + raise VerifyException(f"{name} not in RV32I") @irdl_attr_definition @@ -218,10 +216,8 @@ def verify(self) -> None: name = self.data.name if name is None or name.startswith("j"): return - if name not in Register.RV32F_INDEX_BY_NAME.keys(): - raise VerifyException( - f"{name} not in {Register.RV32F_INDEX_BY_NAME.keys()}" - ) + if name not in Register.RV32F_INDEX_BY_NAME: + raise VerifyException(f"{name} not in RV32F") class Registers(ABC):