GH-111485: Test the new cases generator (GH-113252)

This commit is contained in:
Mark Shannon 2023-12-18 11:14:40 +00:00 committed by GitHub
parent 4a24bf9a13
commit 771903596b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 116 additions and 88 deletions

View file

@ -32,6 +32,7 @@ def skip_if_different_mount_drives():
import analysis
import formatting
from parsing import StackEffect
import tier1_generator
def handle_stderr():
@ -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()
@ -163,7 +163,7 @@ def test_inst_one_pop(self):
PyObject *value;
value = stack_pointer[-1];
spam();
STACK_SHRINK(1);
stack_pointer += -1;
DISPATCH();
}
"""
@ -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();
}
"""
@ -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();
}
"""
@ -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];
@ -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];
@ -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();
}
"""
@ -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();
}
"""
@ -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();
}
"""
@ -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();
}
@ -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;
@ -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();
}
"""
@ -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();
}
"""
@ -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();
}
"""
@ -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();
}
"""
@ -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();
}
"""
@ -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();
}
"""
@ -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();
}
"""
@ -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();
}
"""

View file

@ -1126,7 +1126,7 @@
}
null = NULL;
stack_pointer[0] = res;
if (oparg & 1) stack_pointer[1] = null;
if (oparg & 1) stack_pointer[1] = null;
stack_pointer += 1 + ((oparg & 1));
break;
}
@ -1162,7 +1162,7 @@
STAT_INC(LOAD_GLOBAL, hit);
null = NULL;
stack_pointer[0] = res;
if (oparg & 1) stack_pointer[1] = null;
if (oparg & 1) stack_pointer[1] = null;
stack_pointer += 1 + ((oparg & 1));
break;
}
@ -1180,7 +1180,7 @@
STAT_INC(LOAD_GLOBAL, hit);
null = NULL;
stack_pointer[0] = res;
if (oparg & 1) stack_pointer[1] = null;
if (oparg & 1) stack_pointer[1] = null;
stack_pointer += 1 + ((oparg & 1));
break;
}
@ -1612,7 +1612,7 @@
if (attr == NULL) goto pop_1_error_tier_two;
}
stack_pointer[-1] = attr;
if (oparg & 1) stack_pointer[0] = self_or_null;
if (oparg & 1) stack_pointer[0] = self_or_null;
stack_pointer += ((oparg & 1));
break;
}
@ -1652,7 +1652,7 @@
null = NULL;
Py_DECREF(owner);
stack_pointer[-1] = attr;
if (oparg & 1) stack_pointer[0] = null;
if (oparg & 1) stack_pointer[0] = null;
stack_pointer += ((oparg & 1));
break;
}
@ -1686,7 +1686,7 @@
null = NULL;
Py_DECREF(owner);
stack_pointer[-1] = attr;
if (oparg & 1) stack_pointer[0] = null;
if (oparg & 1) stack_pointer[0] = null;
stack_pointer += ((oparg & 1));
break;
}
@ -1730,7 +1730,7 @@
null = NULL;
Py_DECREF(owner);
stack_pointer[-1] = attr;
if (oparg & 1) stack_pointer[0] = null;
if (oparg & 1) stack_pointer[0] = null;
stack_pointer += ((oparg & 1));
break;
}
@ -1750,7 +1750,7 @@
null = NULL;
Py_DECREF(owner);
stack_pointer[-1] = attr;
if (oparg & 1) stack_pointer[0] = null;
if (oparg & 1) stack_pointer[0] = null;
stack_pointer += ((oparg & 1));
break;
}
@ -1778,7 +1778,7 @@
null = NULL;
Py_DECREF(owner);
stack_pointer[-1] = attr;
if (oparg & 1) stack_pointer[0] = null;
if (oparg & 1) stack_pointer[0] = null;
stack_pointer += ((oparg & 1));
break;
}
@ -2467,7 +2467,7 @@
assert(_PyType_HasFeature(Py_TYPE(attr), Py_TPFLAGS_METHOD_DESCRIPTOR));
self = owner;
stack_pointer[-1] = attr;
if (1) stack_pointer[0] = self;
if (1) stack_pointer[0] = self;
stack_pointer += (((1) ? 1 : 0));
break;
}
@ -2487,7 +2487,7 @@
attr = Py_NewRef(descr);
self = owner;
stack_pointer[-1] = attr;
if (1) stack_pointer[0] = self;
if (1) stack_pointer[0] = self;
stack_pointer += (((1) ? 1 : 0));
break;
}
@ -2550,7 +2550,7 @@
attr = Py_NewRef(descr);
self = owner;
stack_pointer[-1] = attr;
if (1) stack_pointer[0] = self;
if (1) stack_pointer[0] = self;
stack_pointer += (((1) ? 1 : 0));
break;
}
@ -3199,7 +3199,7 @@
PyObject *start;
PyObject *slice;
oparg = CURRENT_OPARG();
if (oparg == 3) { step = stack_pointer[-(((oparg == 3) ? 1 : 0))]; }
if (oparg == 3) { step = stack_pointer[-(((oparg == 3) ? 1 : 0))]; }
stop = stack_pointer[-1 - (((oparg == 3) ? 1 : 0))];
start = stack_pointer[-2 - (((oparg == 3) ? 1 : 0))];
slice = PySlice_New(start, stop, step);

View file

@ -678,7 +678,7 @@
PyObject *stop;
PyObject *start;
PyObject *slice;
if (oparg == 3) { step = stack_pointer[-(((oparg == 3) ? 1 : 0))]; }
if (oparg == 3) { step = stack_pointer[-(((oparg == 3) ? 1 : 0))]; }
stop = stack_pointer[-1 - (((oparg == 3) ? 1 : 0))];
start = stack_pointer[-2 - (((oparg == 3) ? 1 : 0))];
slice = PySlice_New(start, stop, step);
@ -1161,7 +1161,7 @@
PyObject *callargs;
PyObject *func;
PyObject *result;
if (oparg & 1) { kwargs = stack_pointer[-((oparg & 1))]; }
if (oparg & 1) { kwargs = stack_pointer[-((oparg & 1))]; }
callargs = stack_pointer[-1 - ((oparg & 1))];
func = stack_pointer[-3 - ((oparg & 1))];
// DICT_MERGE is called before this opcode if there are kwargs.
@ -3385,7 +3385,7 @@
}
}
stack_pointer[-1] = attr;
if (oparg & 1) stack_pointer[0] = self_or_null;
if (oparg & 1) stack_pointer[0] = self_or_null;
stack_pointer += ((oparg & 1));
DISPATCH();
}
@ -3418,7 +3418,7 @@
Py_DECREF(owner);
}
stack_pointer[-1] = attr;
if (oparg & 1) stack_pointer[0] = null;
if (oparg & 1) stack_pointer[0] = null;
stack_pointer += ((oparg & 1));
DISPATCH();
}
@ -3494,7 +3494,7 @@
}
/* Skip 5 cache entries */
stack_pointer[-1] = attr;
if (oparg & 1) stack_pointer[0] = null;
if (oparg & 1) stack_pointer[0] = null;
stack_pointer += ((oparg & 1));
DISPATCH();
}
@ -3536,7 +3536,7 @@
self = owner;
}
stack_pointer[-1] = attr;
if (1) stack_pointer[0] = self;
if (1) stack_pointer[0] = self;
stack_pointer += (((1) ? 1 : 0));
DISPATCH();
}
@ -3571,7 +3571,7 @@
self = owner;
}
stack_pointer[-1] = attr;
if (1) stack_pointer[0] = self;
if (1) stack_pointer[0] = self;
stack_pointer += (((1) ? 1 : 0));
DISPATCH();
}
@ -3618,7 +3618,7 @@
self = owner;
}
stack_pointer[-1] = attr;
if (1) stack_pointer[0] = self;
if (1) stack_pointer[0] = self;
stack_pointer += (((1) ? 1 : 0));
DISPATCH();
}
@ -3657,7 +3657,7 @@
}
/* Skip 5 cache entries */
stack_pointer[-1] = attr;
if (oparg & 1) stack_pointer[0] = null;
if (oparg & 1) stack_pointer[0] = null;
stack_pointer += ((oparg & 1));
DISPATCH();
}
@ -3799,7 +3799,7 @@
}
/* Skip 5 cache entries */
stack_pointer[-1] = attr;
if (oparg & 1) stack_pointer[0] = null;
if (oparg & 1) stack_pointer[0] = null;
stack_pointer += ((oparg & 1));
DISPATCH();
}
@ -3855,7 +3855,7 @@
}
/* Skip 5 cache entries */
stack_pointer[-1] = attr;
if (oparg & 1) stack_pointer[0] = null;
if (oparg & 1) stack_pointer[0] = null;
stack_pointer += ((oparg & 1));
DISPATCH();
}
@ -4083,7 +4083,7 @@
null = NULL;
}
stack_pointer[0] = res;
if (oparg & 1) stack_pointer[1] = null;
if (oparg & 1) stack_pointer[1] = null;
stack_pointer += 1 + ((oparg & 1));
DISPATCH();
}
@ -4124,7 +4124,7 @@
null = NULL;
}
stack_pointer[0] = res;
if (oparg & 1) stack_pointer[1] = null;
if (oparg & 1) stack_pointer[1] = null;
stack_pointer += 1 + ((oparg & 1));
DISPATCH();
}
@ -4158,7 +4158,7 @@
null = NULL;
}
stack_pointer[0] = res;
if (oparg & 1) stack_pointer[1] = null;
if (oparg & 1) stack_pointer[1] = null;
stack_pointer += 1 + ((oparg & 1));
DISPATCH();
}
@ -4286,7 +4286,7 @@
null = NULL;
}
stack_pointer[-3] = attr;
if (oparg & 1) stack_pointer[-2] = null;
if (oparg & 1) stack_pointer[-2] = null;
stack_pointer += -2 + ((oparg & 1));
DISPATCH();
}

View file

@ -444,7 +444,8 @@ def analyze_forest(forest: list[parser.AstNode]) -> Analysis:
if target.text in instructions:
instructions[target.text].is_target = True
# Hack
instructions["BINARY_OP_INPLACE_ADD_UNICODE"].family = families["BINARY_OP"]
if "BINARY_OP_INPLACE_ADD_UNICODE" in instructions:
instructions["BINARY_OP_INPLACE_ADD_UNICODE"].family = families["BINARY_OP"]
return Analysis(instructions, uops, families, pseudos)

View file

@ -38,7 +38,8 @@ def maybe_dedent(self, txt: str) -> None:
parens = txt.count("(") - txt.count(")")
if parens < 0:
self.indents.pop()
elif "}" in txt or is_label(txt):
braces = txt.count("{") - txt.count("}")
if braces < 0 or is_label(txt):
self.indents.pop()
def maybe_indent(self, txt: str) -> None:
@ -50,11 +51,13 @@ def maybe_indent(self, txt: str) -> None:
self.indents.append(offset)
if is_label(txt):
self.indents.append(self.indents[-1] + 4)
elif "{" in txt:
if 'extern "C"' in txt:
self.indents.append(self.indents[-1])
else:
self.indents.append(self.indents[-1] + 4)
else:
braces = txt.count("{") - txt.count("}")
if braces > 0:
if 'extern "C"' in txt:
self.indents.append(self.indents[-1])
else:
self.indents.append(self.indents[-1] + 4)
def emit_text(self, txt: str) -> None:
self.out.write(txt)

View file

@ -22,8 +22,10 @@
def root_relative_path(filename: str) -> str:
return Path(filename).absolute().relative_to(ROOT).as_posix()
try:
return Path(filename).absolute().relative_to(ROOT).as_posix()
except ValueError:
return filename
def write_header(generator: str, sources: list[str], outfile: TextIO) -> None:
outfile.write(

View file

@ -148,7 +148,7 @@ def flush(self, out: CWriter) -> None:
cast = "(PyObject *)" if var.type else ""
if var.name != "unused" and not var.is_array():
if var.condition:
out.emit(f" if ({var.condition}) ")
out.emit(f"if ({var.condition}) ")
out.emit(
f"stack_pointer[{self.base_offset.to_c()}] = {cast}{var.name};\n"
)

View file

@ -181,6 +181,14 @@ def generate_tier1(
"input", nargs=argparse.REMAINDER, help="Instruction definition file(s)"
)
def generate_tier1_from_files(
filenames: list[str], outfilename: str, lines: bool
) -> None:
data = analyze_files(filenames)
with open(outfilename, "w") as outfile:
generate_tier1(filenames, data, outfile, lines)
if __name__ == "__main__":
args = arg_parser.parse_args()
if len(args.input) == 0: