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

GH-98831: Add some tests for generate_cases.py #100763

Merged
merged 3 commits into from
Jan 6, 2023
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
310 changes: 310 additions & 0 deletions Tools/cases_generator/test_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,310 @@
# Sorry for using pytest, these tests are mostly just for me.
# Use pytest -vv for best results.

import tempfile

import generate_cases


def run_cases_test(input: str, expected: str):
temp_input = tempfile.NamedTemporaryFile("w+")
temp_input.write(generate_cases.BEGIN_MARKER)
temp_input.write(input)
temp_input.write(generate_cases.END_MARKER)
temp_input.flush()
temp_output = tempfile.NamedTemporaryFile("w+")
a = generate_cases.Analyzer(temp_input.name, temp_output.name)
a.parse()
a.analyze()
if a.errors:
raise RuntimeError(f"Found {a.errors} errors")
a.write_instructions()
temp_output.seek(0)
lines = temp_output.readlines()
while lines and lines[0].startswith("// "):
lines.pop(0)
actual = "".join(lines)
assert actual.rstrip() == expected.rstrip()

def test_legacy():
input = """
inst(OP) {
spam();
}
"""
output = """
TARGET(OP) {
spam();
DISPATCH();
}
"""
run_cases_test(input, output)

def test_inst_no_args():
input = """
inst(OP, (--)) {
spam();
}
"""
output = """
TARGET(OP) {
spam();
DISPATCH();
}
"""
run_cases_test(input, output)

def test_inst_one_pop():
input = """
inst(OP, (value --)) {
spam();
}
"""
output = """
TARGET(OP) {
PyObject *value = PEEK(1);
spam();
STACK_SHRINK(1);
DISPATCH();
}
"""
run_cases_test(input, output)

def test_inst_one_push():
input = """
inst(OP, (-- res)) {
spam();
}
"""
output = """
TARGET(OP) {
PyObject *res;
spam();
STACK_GROW(1);
POKE(1, res);
DISPATCH();
}
"""
run_cases_test(input, output)

def test_inst_one_push_one_pop():
input = """
inst(OP, (value -- res)) {
spam();
}
"""
output = """
TARGET(OP) {
PyObject *value = PEEK(1);
PyObject *res;
spam();
POKE(1, res);
DISPATCH();
}
"""
run_cases_test(input, output)

def test_binary_op():
input = """
inst(OP, (left, right -- res)) {
spam();
}
"""
output = """
TARGET(OP) {
PyObject *right = PEEK(1);
PyObject *left = PEEK(2);
PyObject *res;
spam();
STACK_SHRINK(1);
POKE(1, res);
DISPATCH();
}
"""
run_cases_test(input, output)

def test_predictions():
input = """
inst(OP1, (--)) {
}
inst(OP2, (--)) {
}
inst(OP3, (--)) {
DEOPT_IF(xxx, OP1);
PREDICT(OP2);
}
"""
output = """
TARGET(OP1) {
PREDICTED(OP1);
DISPATCH();
}

TARGET(OP2) {
PREDICTED(OP2);
DISPATCH();
}

TARGET(OP3) {
DEOPT_IF(xxx, OP1);
PREDICT(OP2);
DISPATCH();
}
"""
run_cases_test(input, output)

def test_error_if_plain():
input = """
inst(OP, (--)) {
ERROR_IF(cond, label);
}
"""
output = """
TARGET(OP) {
if (cond) goto label;
DISPATCH();
}
"""
run_cases_test(input, output)

def test_error_if_pop():
input = """
inst(OP, (left, right -- res)) {
ERROR_IF(cond, label);
}
"""
output = """
TARGET(OP) {
PyObject *right = PEEK(1);
PyObject *left = PEEK(2);
PyObject *res;
if (cond) goto pop_2_label;
STACK_SHRINK(1);
POKE(1, res);
DISPATCH();
}
"""
run_cases_test(input, output)

def test_cache_effect():
input = """
inst(OP, (counter/1, extra/2, value --)) {
}
"""
output = """
TARGET(OP) {
PyObject *value = PEEK(1);
uint16_t counter = read_u16(&next_instr[0].cache);
uint32_t extra = read_u32(&next_instr[1].cache);
STACK_SHRINK(1);
iritkatriel marked this conversation as resolved.
Show resolved Hide resolved
JUMPBY(3);
DISPATCH();
}
"""
run_cases_test(input, output)

def test_suppress_dispatch():
input = """
inst(OP, (--)) {
goto somewhere;
}
"""
output = """
TARGET(OP) {
goto somewhere;
}
"""
run_cases_test(input, output)

def test_super_instruction():
# TODO: Test cache effect
input = """
inst(OP1, (counter/1, arg --)) {
op1();
}
inst(OP2, (extra/2, arg --)) {
op2();
}
super(OP) = OP1 + OP2;
"""
output = """
TARGET(OP1) {
PyObject *arg = PEEK(1);
uint16_t counter = read_u16(&next_instr[0].cache);
op1();
STACK_SHRINK(1);
JUMPBY(1);
DISPATCH();
}

TARGET(OP2) {
PyObject *arg = PEEK(1);
uint32_t extra = read_u32(&next_instr[0].cache);
op2();
STACK_SHRINK(1);
JUMPBY(2);
DISPATCH();
}

TARGET(OP) {
PyObject *_tmp_1 = PEEK(1);
PyObject *_tmp_2 = PEEK(2);
{
PyObject *arg = _tmp_1;
uint16_t counter = read_u16(&next_instr[0].cache);
op1();
}
JUMPBY(1);
NEXTOPARG();
JUMPBY(1);
{
PyObject *arg = _tmp_2;
uint32_t extra = read_u32(&next_instr[0].cache);
op2();
}
JUMPBY(2);
STACK_SHRINK(2);
DISPATCH();
}
"""
run_cases_test(input, output)

def test_macro_instruction():
input = """
inst(OP1, (counter/1, arg --)) {
op1();
}
op(OP2, (extra/2, arg --)) {
op2();
}
macro(OP) = OP1 + cache/2 + OP2;
"""
output = """
TARGET(OP1) {
PyObject *arg = PEEK(1);
uint16_t counter = read_u16(&next_instr[0].cache);
op1();
STACK_SHRINK(1);
JUMPBY(1);
DISPATCH();
}

TARGET(OP) {
PyObject *_tmp_1 = PEEK(1);
PyObject *_tmp_2 = PEEK(2);
{
PyObject *arg = _tmp_1;
uint16_t counter = read_u16(&next_instr[0].cache);
op1();
}
{
PyObject *arg = _tmp_2;
uint32_t extra = read_u32(&next_instr[3].cache);
op2();
}
JUMPBY(5);
STACK_SHRINK(2);
DISPATCH();
}
"""
run_cases_test(input, output)