Skip to content

Commit

Permalink
pythonGH-111485: Test the new cases generator (pythonGH-113252)
Browse files Browse the repository at this point in the history
  • Loading branch information
markshannon authored and aisk committed Feb 11, 2024
1 parent 92410c8 commit fc93c01
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 88 deletions.
114 changes: 64 additions & 50 deletions Lib/test/test_generated_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def skip_if_different_mount_drives():
import analysis
import formatting
from parsing import StackEffect
import tier1_generator


def handle_stderr():
Expand Down Expand Up @@ -108,13 +109,12 @@ def run_cases_test(self, input: str, expected: str):
temp_input.write(analysis.END_MARKER)
temp_input.flush()

a = generate_cases.Generator([self.temp_input_filename])
with handle_stderr():
a.parse()
a.analyze()
if a.errors:
raise RuntimeError(f"Found {a.errors} errors")
a.write_instructions(self.temp_output_filename, False)
tier1_generator.generate_tier1_from_files(
[self.temp_input_filename],
self.temp_output_filename,
False
)

with open(self.temp_output_filename) as temp_output:
lines = temp_output.readlines()
Expand Down Expand Up @@ -163,7 +163,7 @@ def test_inst_one_pop(self):
PyObject *value;
value = stack_pointer[-1];
spam();
STACK_SHRINK(1);
stack_pointer += -1;
DISPATCH();
}
"""
Expand All @@ -182,8 +182,8 @@ def test_inst_one_push(self):
INSTRUCTION_STATS(OP);
PyObject *res;
spam();
STACK_GROW(1);
stack_pointer[-1] = res;
stack_pointer[0] = res;
stack_pointer += 1;
DISPATCH();
}
"""
Expand Down Expand Up @@ -227,8 +227,8 @@ def test_binary_op(self):
right = stack_pointer[-1];
left = stack_pointer[-2];
spam();
STACK_SHRINK(1);
stack_pointer[-1] = res;
stack_pointer[-2] = res;
stack_pointer += -1;
DISPATCH();
}
"""
Expand Down Expand Up @@ -273,7 +273,6 @@ def test_predictions_and_eval_breaker(self):
next_instr += 1;
INSTRUCTION_STATS(OP1);
PREDICTED(OP1);
static_assert(INLINE_CACHE_ENTRIES_OP1 == 0, "incorrect cache size");
PyObject *arg;
PyObject *rest;
arg = stack_pointer[-1];
Expand All @@ -285,6 +284,7 @@ def test_predictions_and_eval_breaker(self):
frame->instr_ptr = next_instr;
next_instr += 1;
INSTRUCTION_STATS(OP3);
static_assert(INLINE_CACHE_ENTRIES_OP1 == 0, "incorrect cache size");
PyObject *arg;
PyObject *res;
arg = stack_pointer[-1];
Expand Down Expand Up @@ -325,6 +325,7 @@ def test_error_if_plain_with_comment(self):
next_instr += 1;
INSTRUCTION_STATS(OP);
if (cond) goto label;
// Comment is ok
DISPATCH();
}
"""
Expand All @@ -347,8 +348,8 @@ def test_error_if_pop(self):
right = stack_pointer[-1];
left = stack_pointer[-2];
if (cond) goto pop_2_label;
STACK_SHRINK(1);
stack_pointer[-1] = res;
stack_pointer[-2] = res;
stack_pointer += -1;
DISPATCH();
}
"""
Expand All @@ -368,7 +369,7 @@ def test_cache_effect(self):
value = stack_pointer[-1];
uint16_t counter = read_u16(&this_instr[1].cache);
uint32_t extra = read_u32(&this_instr[2].cache);
STACK_SHRINK(1);
stack_pointer += -1;
DISPATCH();
}
"""
Expand Down Expand Up @@ -411,26 +412,26 @@ def test_macro_instruction(self):
INSTRUCTION_STATS(OP);
PREDICTED(OP);
_Py_CODEUNIT *this_instr = next_instr - 6;
static_assert(INLINE_CACHE_ENTRIES_OP == 5, "incorrect cache size");
PyObject *right;
PyObject *left;
PyObject *arg2;
PyObject *res;
// OP1
// _OP1
right = stack_pointer[-1];
left = stack_pointer[-2];
{
uint16_t counter = read_u16(&this_instr[1].cache);
op1(left, right);
}
/* Skip 2 cache entries */
// OP2
arg2 = stack_pointer[-3];
{
uint32_t extra = read_u32(&this_instr[4].cache);
res = op2(arg2, left, right);
}
STACK_SHRINK(2);
stack_pointer[-1] = res;
stack_pointer[-3] = res;
stack_pointer += -2;
DISPATCH();
}
Expand All @@ -451,6 +452,7 @@ def test_macro_instruction(self):
frame->instr_ptr = next_instr;
next_instr += 6;
INSTRUCTION_STATS(OP3);
static_assert(INLINE_CACHE_ENTRIES_OP == 5, "incorrect cache size");
PyObject *right;
PyObject *left;
PyObject *arg2;
Expand All @@ -459,8 +461,24 @@ def test_macro_instruction(self):
left = stack_pointer[-2];
arg2 = stack_pointer[-3];
res = op3(arg2, left, right);
STACK_SHRINK(2);
stack_pointer[-1] = res;
stack_pointer[-3] = res;
stack_pointer += -2;
DISPATCH();
}
"""
self.run_cases_test(input, output)
def test_unused_caches(self):
input = """
inst(OP, (unused/1, unused/2 --)) {
body();
}
"""
output = """
TARGET(OP) {
frame->instr_ptr = next_instr;
next_instr += 4;
INSTRUCTION_STATS(OP);
body();
DISPATCH();
}
"""
Expand Down Expand Up @@ -519,11 +537,10 @@ def test_array_input(self):
PyObject **values;
PyObject *below;
above = stack_pointer[-1];
values = stack_pointer - 1 - oparg*2;
values = &stack_pointer[-1 - oparg*2];
below = stack_pointer[-2 - oparg*2];
spam();
STACK_SHRINK(oparg*2);
STACK_SHRINK(2);
stack_pointer += -2 - oparg*2;
DISPATCH();
}
"""
Expand All @@ -543,11 +560,11 @@ def test_array_output(self):
PyObject *below;
PyObject **values;
PyObject *above;
values = stack_pointer - 1;
values = &stack_pointer[-1];
spam(values, oparg);
STACK_GROW(oparg*3);
stack_pointer[-2 - oparg*3] = below;
stack_pointer[-1] = above;
stack_pointer[-2] = below;
stack_pointer[-1 + oparg*3] = above;
stack_pointer += oparg*3;
DISPATCH();
}
"""
Expand All @@ -566,10 +583,10 @@ def test_array_input_output(self):
INSTRUCTION_STATS(OP);
PyObject **values;
PyObject *above;
values = stack_pointer - oparg;
values = &stack_pointer[-oparg];
spam(values, oparg);
STACK_GROW(1);
stack_pointer[-1] = above;
stack_pointer[0] = above;
stack_pointer += 1;
DISPATCH();
}
"""
Expand All @@ -588,11 +605,10 @@ def test_array_error_if(self):
INSTRUCTION_STATS(OP);
PyObject **values;
PyObject *extra;
values = stack_pointer - oparg;
values = &stack_pointer[-oparg];
extra = stack_pointer[-1 - oparg];
if (oparg == 0) { STACK_SHRINK(oparg); goto pop_1_somewhere; }
STACK_SHRINK(oparg);
STACK_SHRINK(1);
if (oparg == 0) { stack_pointer += -1 - oparg; goto somewhere; }
stack_pointer += -1 - oparg;
DISPATCH();
}
"""
Expand All @@ -616,14 +632,13 @@ def test_cond_effect(self):
PyObject *output = NULL;
PyObject *zz;
cc = stack_pointer[-1];
if ((oparg & 1) == 1) { input = stack_pointer[-1 - ((oparg & 1) == 1 ? 1 : 0)]; }
aa = stack_pointer[-2 - ((oparg & 1) == 1 ? 1 : 0)];
if ((oparg & 1) == 1) { input = stack_pointer[-1 - ((((oparg & 1) == 1) ? 1 : 0))]; }
aa = stack_pointer[-2 - ((((oparg & 1) == 1) ? 1 : 0))];
output = spam(oparg, input);
STACK_SHRINK((((oparg & 1) == 1) ? 1 : 0));
STACK_GROW(((oparg & 2) ? 1 : 0));
stack_pointer[-2 - (oparg & 2 ? 1 : 0)] = xx;
if (oparg & 2) { stack_pointer[-1 - (oparg & 2 ? 1 : 0)] = output; }
stack_pointer[-1] = zz;
stack_pointer[-2 - ((((oparg & 1) == 1) ? 1 : 0))] = xx;
if (oparg & 2) stack_pointer[-1 - ((((oparg & 1) == 1) ? 1 : 0))] = output;
stack_pointer[-1 - ((((oparg & 1) == 1) ? 1 : 0)) + (((oparg & 2) ? 1 : 0))] = zz;
stack_pointer += -((((oparg & 1) == 1) ? 1 : 0)) + (((oparg & 2) ? 1 : 0));
DISPATCH();
}
"""
Expand Down Expand Up @@ -661,11 +676,10 @@ def test_macro_cond_effect(self):
{
# Body of B
}
STACK_SHRINK(1);
STACK_GROW((oparg ? 1 : 0));
stack_pointer[-2 - (oparg ? 1 : 0)] = deep;
if (oparg) { stack_pointer[-1 - (oparg ? 1 : 0)] = extra; }
stack_pointer[-1] = res;
stack_pointer[-3] = deep;
if (oparg) stack_pointer[-2] = extra;
stack_pointer[-2 + (((oparg) ? 1 : 0))] = res;
stack_pointer += -1 + (((oparg) ? 1 : 0));
DISPATCH();
}
"""
Expand Down Expand Up @@ -696,9 +710,9 @@ def test_macro_push_push(self):
{
val2 = spam();
}
STACK_GROW(2);
stack_pointer[-2] = val1;
stack_pointer[-1] = val2;
stack_pointer[0] = val1;
stack_pointer[1] = val2;
stack_pointer += 2;
DISPATCH();
}
"""
Expand Down
26 changes: 13 additions & 13 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fc93c01

Please sign in to comment.