[RTG] Add operations to report test result (#8751)

This commit is contained in:
Martin Erhart 2025-07-21 18:43:42 +01:00 committed by GitHub
parent 331820d6d0
commit f1352362d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 84 additions and 3 deletions

View File

@ -4,7 +4,7 @@
from . import tests
from . import core
from .tests import test, embed_comment
from .tests import test, embed_comment, report_success, report_failure
from .labels import Label, LabelType
from .rtg import rtg
from .rtgtest import rtgtest

View File

@ -7,7 +7,6 @@ from .core import CodeGenRoot, CodeGenObject
from .rtg import rtg
from .support import _FromCirctValue
from .configs import PythonParam
from .labels import Label
from types import SimpleNamespace
@ -69,3 +68,19 @@ def embed_comment(comment: str) -> None:
"""
rtg.CommentOp(comment)
def report_success() -> None:
"""
Exit this test and report a success.
"""
rtg.TestSuccessOp()
def report_failure(message: str) -> None:
"""
Exit this test and report a failure with the provided error message.
"""
rtg.TestFailureOp(message)

View File

@ -0,0 +1,23 @@
# RUN: %rtgtool% %s --seed=0 --output-format=mlir | FileCheck %s
from pyrtg import test, config, Config, report_success, report_failure
# CHECK-LABEL: rtg.target @Singleton : !rtg.dict<>
# CHECK-NEXT: }
@config
class Singleton(Config):
pass
# CHECK-LABEL: rtg.test @test94_report_test_result
# CHECK-NEXT: rtg.test.success
# CHECK-NEXT: rtg.test.failure "this is a failure message"
# CHECK-NEXT: }
@test(Singleton)
def test94_report_test_result(config):
report_success()
report_failure("this is a failure message")

View File

@ -727,6 +727,10 @@ def TestOp : RTGOp<"test", [
The arguments must match the fields of the dict type in the target attribute
exactly. The test must not have any additional arguments and cannot be
referenced by an `rtg.get_sequence` operation.
If the end of the test is reached without executing an `rtg.test.success`
or `rtg.test.failure` it is as if an `rtg.test.success` is executed at the
very end.
}];
let arguments = (ins SymbolNameAttr:$sym_name, StrAttr:$templateName,
@ -738,6 +742,19 @@ def TestOp : RTGOp<"test", [
let hasVerifier = 1;
}
def TestSuccessOp : RTGOp<"test.success", []> {
let summary = "exit the test and report success";
let assemblyFormat = "attr-dict";
}
def TestFailureOp : RTGOp<"test.failure", []> {
let summary = "exit the test and report failure";
let arguments = (ins StrAttr:$errorMessage);
let assemblyFormat = "$errorMessage attr-dict";
}
def TargetOp : RTGOp<"target", [
IsolatedFromAbove,
Symbol,

View File

@ -44,7 +44,7 @@ public:
// Registers
FixedRegisterOp, VirtualRegisterOp,
// RTG tests
TestOp, TargetOp, YieldOp, ValidateOp,
TestOp, TargetOp, YieldOp, ValidateOp, TestSuccessOp, TestFailureOp,
// Integers
RandomNumberInRangeOp,
// Sequences
@ -133,6 +133,8 @@ public:
HANDLE(TargetOp, Unhandled);
HANDLE(YieldOp, Unhandled);
HANDLE(ValidateOp, Unhandled);
HANDLE(TestSuccessOp, Unhandled);
HANDLE(TestFailureOp, Unhandled);
HANDLE(FixedRegisterOp, Unhandled);
HANDLE(VirtualRegisterOp, Unhandled);
HANDLE(IntToImmediateOp, Unhandled);

View File

@ -1684,6 +1684,14 @@ public:
FailureOr<DeletionKind> visitOp(LabelOp op) { return DeletionKind::Keep; }
FailureOr<DeletionKind> visitOp(TestSuccessOp op) {
return DeletionKind::Keep;
}
FailureOr<DeletionKind> visitOp(TestFailureOp op) {
return DeletionKind::Keep;
}
FailureOr<DeletionKind> visitOp(RandomNumberInRangeOp op) {
size_t lower = get<size_t>(op.getLowerBound());
size_t upper = get<size_t>(op.getUpperBound());

View File

@ -247,3 +247,11 @@ rtg.test @immediateOps() {
%3 = rtg.constant #rtg.isa.immediate<8, 175>
%4 = rtg.isa.slice_immediate %3 from 4 : !rtg.isa.immediate<8> -> !rtg.isa.immediate<2>
}
// CHECK-LABEL: rtg.test @testReportOps
rtg.test @testReportOps() {
// CHECK-NEXT: rtg.test.success
rtg.test.success
// CHECK-NEXT: rtg.test.failure "error message"
rtg.test.failure "error message"
}

View File

@ -803,6 +803,14 @@ rtg.test @immediateOps(singleton = %none: index) {
func.call @dummy6(%slice) : (!rtg.isa.immediate<2>) -> ()
}
// CHECK-LABEL: rtg.test @testSuccessAndFailure
rtg.test @testSuccessAndFailure(singleton = %none: index) {
// CHECK-NEXT: rtg.test.success
rtg.test.success
// CHECK-NEXT: rtg.test.failure "Error Message"
rtg.test.failure "Error Message"
}
// -----
rtg.target @singletonTarget : !rtg.dict<singleton: index> {