Skip to content

Commit 8e5e19b

Browse files
committed
Also validate sizes
1 parent 7a09150 commit 8e5e19b

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

Lib/test/test_generated_cases.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2168,7 +2168,7 @@ def test_validate_uop_unused_input(self):
21682168
"""
21692169
self.run_cases_test(input, input2, output)
21702170

2171-
def test_validate_uop_unused_override(self):
2171+
def test_validate_uop_unused_output(self):
21722172
input = """
21732173
op(OP, ( -- unused)) {
21742174
}
@@ -2208,6 +2208,50 @@ def test_validate_uop_unused_override(self):
22082208
"""
22092209
self.run_cases_test(input, input2, output)
22102210

2211+
def test_validate_uop_input_size_mismatch(self):
2212+
input = """
2213+
op(OP, (arg1[2] -- )) {
2214+
}
2215+
"""
2216+
input2 = """
2217+
op(OP, (arg1[4] -- )) {
2218+
}
2219+
"""
2220+
output = """
2221+
"""
2222+
with self.assertRaisesRegex(SyntaxError,
2223+
"Inputs must have equal sizes"):
2224+
self.run_cases_test(input, input2, output)
2225+
2226+
def test_validate_uop_output_size_mismatch(self):
2227+
input = """
2228+
op(OP, ( -- out[2])) {
2229+
}
2230+
"""
2231+
input2 = """
2232+
op(OP, ( -- out[4])) {
2233+
}
2234+
"""
2235+
output = """
2236+
"""
2237+
with self.assertRaisesRegex(SyntaxError,
2238+
"Outputs must have equal sizes"):
2239+
self.run_cases_test(input, input2, output)
2240+
2241+
def test_validate_uop_unused_size_mismatch(self):
2242+
input = """
2243+
op(OP, (foo[2] -- )) {
2244+
}
2245+
"""
2246+
input2 = """
2247+
op(OP, (unused[4] -- )) {
2248+
}
2249+
"""
2250+
output = """
2251+
"""
2252+
with self.assertRaisesRegex(SyntaxError,
2253+
"Inputs must have equal sizes"):
2254+
self.run_cases_test(input, input2, output)
22112255

22122256
if __name__ == "__main__":
22132257
unittest.main()

Python/optimizer_bytecodes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,7 @@ dummy_func(void) {
10881088
sym_set_const(callable, (PyObject *)&PyUnicode_Type);
10891089
}
10901090

1091-
op(_CALL_LEN, (callable[1], self_or_null[1], args[oparg] -- res)) {
1091+
op(_CALL_LEN, (callable, self_or_null, args[oparg] -- res)) {
10921092
res = sym_new_type(ctx, &PyLong_Type);
10931093
}
10941094

Tools/cases_generator/optimizer_generator.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@
3030

3131

3232
def validate_uop(override: Uop, uop: Uop) -> None:
33+
"""
34+
Check that the overridden uop (defined in 'optimizer_bytecodes.c')
35+
has the same stack effects as the original uop (defined in 'bytecodes.c').
36+
37+
Ensure that:
38+
- The number of inputs and outputs is the same.
39+
- The names of the inputs and outputs are the same
40+
(except for 'unused' which is ignored).
41+
- The sizes of the inputs and outputs are the same.
42+
"""
3343
for stack_effect in ('inputs', 'outputs'):
3444
orig_effects = getattr(uop.stack, stack_effect)
3545
new_effects = getattr(override.stack, stack_effect)
@@ -43,16 +53,22 @@ def validate_uop(override: Uop, uop: Uop) -> None:
4353
raise analysis_error(msg, override.body.open)
4454

4555
for orig, new in zip(orig_effects, new_effects, strict=True):
46-
if orig.name == 'unused' or new.name == 'unused':
47-
continue
48-
if orig.name != new.name:
56+
if orig.name != new.name and orig.name != "unused" and new.name != "unused":
4957
msg = (
5058
f"{uop.name}: {stack_effect.capitalize()} must have "
5159
"equal names in bytecodes.c and optimizer_bytecodes.c "
5260
f"({orig.name} != {new.name})"
5361
)
5462
raise analysis_error(msg, override.body.open)
5563

64+
if orig.size != new.size:
65+
msg = (
66+
f"{uop.name}: {stack_effect.capitalize()} must have "
67+
"equal sizes in bytecodes.c and optimizer_bytecodes.c "
68+
f"({orig.size!r} != {new.size!r})"
69+
)
70+
raise analysis_error(msg, override.body.open)
71+
5672

5773
def type_name(var: StackItem) -> str:
5874
if var.is_array():

0 commit comments

Comments
 (0)