Skip to content

Commit 400835e

Browse files
authored
gh-106812: Refactor cases_generator to allow uops with array stack effects (#107564)
Introducing a new file, stacking.py, that takes over several responsibilities related to symbolic evaluation of push/pop operations, with more generality.
1 parent 407d7fd commit 400835e

12 files changed

+1798
-1098
lines changed

Include/internal/pycore_opcode_metadata.h

+15-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_generated_cases.py

+81-77
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
test_tools.skip_if_missing('cases_generator')
88
with test_tools.imports_under_tool('cases_generator'):
9+
import generate_cases
910
import analysis
1011
import formatting
11-
import generate_cases
1212
from parsing import StackEffect
1313

1414

@@ -46,28 +46,11 @@ def test_effect_sizes(self):
4646
(2, "(oparg<<1)"),
4747
)
4848

49-
self.assertEqual(
50-
formatting.string_effect_size(
51-
formatting.list_effect_size(input_effects),
52-
), "1 + oparg + oparg*2",
53-
)
54-
self.assertEqual(
55-
formatting.string_effect_size(
56-
formatting.list_effect_size(output_effects),
57-
),
58-
"2 + oparg*4",
59-
)
60-
self.assertEqual(
61-
formatting.string_effect_size(
62-
formatting.list_effect_size(other_effects),
63-
),
64-
"2 + (oparg<<1)",
65-
)
66-
6749

6850
class TestGeneratedCases(unittest.TestCase):
6951
def setUp(self) -> None:
7052
super().setUp()
53+
self.maxDiff = None
7154

7255
self.temp_dir = tempfile.gettempdir()
7356
self.temp_input_filename = os.path.join(self.temp_dir, "input.txt")
@@ -140,7 +123,8 @@ def test_inst_one_pop(self):
140123
"""
141124
output = """
142125
TARGET(OP) {
143-
PyObject *value = stack_pointer[-1];
126+
PyObject *value;
127+
value = stack_pointer[-1];
144128
spam();
145129
STACK_SHRINK(1);
146130
DISPATCH();
@@ -173,8 +157,9 @@ def test_inst_one_push_one_pop(self):
173157
"""
174158
output = """
175159
TARGET(OP) {
176-
PyObject *value = stack_pointer[-1];
160+
PyObject *value;
177161
PyObject *res;
162+
value = stack_pointer[-1];
178163
spam();
179164
stack_pointer[-1] = res;
180165
DISPATCH();
@@ -190,9 +175,11 @@ def test_binary_op(self):
190175
"""
191176
output = """
192177
TARGET(OP) {
193-
PyObject *right = stack_pointer[-1];
194-
PyObject *left = stack_pointer[-2];
178+
PyObject *right;
179+
PyObject *left;
195180
PyObject *res;
181+
right = stack_pointer[-1];
182+
left = stack_pointer[-2];
196183
spam();
197184
STACK_SHRINK(1);
198185
stack_pointer[-1] = res;
@@ -209,9 +196,11 @@ def test_overlap(self):
209196
"""
210197
output = """
211198
TARGET(OP) {
212-
PyObject *right = stack_pointer[-1];
213-
PyObject *left = stack_pointer[-2];
199+
PyObject *right;
200+
PyObject *left;
214201
PyObject *result;
202+
right = stack_pointer[-1];
203+
left = stack_pointer[-2];
215204
spam();
216205
stack_pointer[-1] = result;
217206
DISPATCH();
@@ -235,8 +224,9 @@ def test_predictions_and_eval_breaker(self):
235224
}
236225
237226
TARGET(OP3) {
238-
PyObject *arg = stack_pointer[-1];
227+
PyObject *arg;
239228
PyObject *res;
229+
arg = stack_pointer[-1];
240230
DEOPT_IF(xxx, OP1);
241231
stack_pointer[-1] = res;
242232
CHECK_EVAL_BREAKER();
@@ -281,9 +271,11 @@ def test_error_if_pop(self):
281271
"""
282272
output = """
283273
TARGET(OP) {
284-
PyObject *right = stack_pointer[-1];
285-
PyObject *left = stack_pointer[-2];
274+
PyObject *right;
275+
PyObject *left;
286276
PyObject *res;
277+
right = stack_pointer[-1];
278+
left = stack_pointer[-2];
287279
if (cond) goto pop_2_label;
288280
STACK_SHRINK(1);
289281
stack_pointer[-1] = res;
@@ -299,7 +291,8 @@ def test_cache_effect(self):
299291
"""
300292
output = """
301293
TARGET(OP) {
302-
PyObject *value = stack_pointer[-1];
294+
PyObject *value;
295+
value = stack_pointer[-1];
303296
uint16_t counter = read_u16(&next_instr[0].cache);
304297
uint32_t extra = read_u32(&next_instr[1].cache);
305298
STACK_SHRINK(1);
@@ -338,47 +331,49 @@ def test_macro_instruction(self):
338331
"""
339332
output = """
340333
TARGET(OP1) {
341-
PyObject *right = stack_pointer[-1];
342-
PyObject *left = stack_pointer[-2];
334+
PyObject *right;
335+
PyObject *left;
336+
right = stack_pointer[-1];
337+
left = stack_pointer[-2];
343338
uint16_t counter = read_u16(&next_instr[0].cache);
344339
op1(left, right);
345340
next_instr += 1;
346341
DISPATCH();
347342
}
348343
349344
TARGET(OP) {
350-
PyObject *_tmp_1 = stack_pointer[-1];
351-
PyObject *_tmp_2 = stack_pointer[-2];
352-
PyObject *_tmp_3 = stack_pointer[-3];
345+
static_assert(INLINE_CACHE_ENTRIES_OP == 5, "incorrect cache size");
346+
PyObject *right;
347+
PyObject *left;
348+
PyObject *arg2;
349+
PyObject *res;
350+
// OP1
351+
right = stack_pointer[-1];
352+
left = stack_pointer[-2];
353353
{
354-
PyObject *right = _tmp_1;
355-
PyObject *left = _tmp_2;
356354
uint16_t counter = read_u16(&next_instr[0].cache);
357355
op1(left, right);
358-
_tmp_2 = left;
359-
_tmp_1 = right;
360356
}
357+
// OP2
358+
arg2 = stack_pointer[-3];
361359
{
362-
PyObject *right = _tmp_1;
363-
PyObject *left = _tmp_2;
364-
PyObject *arg2 = _tmp_3;
365-
PyObject *res;
366360
uint32_t extra = read_u32(&next_instr[3].cache);
367361
res = op2(arg2, left, right);
368-
_tmp_3 = res;
369362
}
370-
next_instr += 5;
371-
static_assert(INLINE_CACHE_ENTRIES_OP == 5, "incorrect cache size");
372363
STACK_SHRINK(2);
373-
stack_pointer[-1] = _tmp_3;
364+
stack_pointer[-1] = res;
365+
next_instr += 5;
374366
DISPATCH();
375367
}
376368
377369
TARGET(OP3) {
378-
PyObject *right = stack_pointer[-1];
379-
PyObject *left = stack_pointer[-2];
380-
PyObject *arg2 = stack_pointer[-3];
370+
PyObject *right;
371+
PyObject *left;
372+
PyObject *arg2;
381373
PyObject *res;
374+
right = stack_pointer[-1];
375+
left = stack_pointer[-2];
376+
arg2 = stack_pointer[-3];
382377
res = op3(arg2, left, right);
383378
STACK_SHRINK(2);
384379
stack_pointer[-1] = res;
@@ -396,9 +391,12 @@ def test_array_input(self):
396391
"""
397392
output = """
398393
TARGET(OP) {
399-
PyObject *above = stack_pointer[-1];
400-
PyObject **values = (stack_pointer - (1 + oparg*2));
401-
PyObject *below = stack_pointer[-(2 + oparg*2)];
394+
PyObject *above;
395+
PyObject **values;
396+
PyObject *below;
397+
above = stack_pointer[-1];
398+
values = stack_pointer - 1 - oparg*2;
399+
below = stack_pointer[-2 - oparg*2];
402400
spam();
403401
STACK_SHRINK(oparg*2);
404402
STACK_SHRINK(2);
@@ -416,12 +414,13 @@ def test_array_output(self):
416414
output = """
417415
TARGET(OP) {
418416
PyObject *below;
419-
PyObject **values = stack_pointer - (2) + 1;
417+
PyObject **values;
420418
PyObject *above;
419+
values = stack_pointer - 1;
421420
spam(values, oparg);
422421
STACK_GROW(oparg*3);
422+
stack_pointer[-2 - oparg*3] = below;
423423
stack_pointer[-1] = above;
424-
stack_pointer[-(2 + oparg*3)] = below;
425424
DISPATCH();
426425
}
427426
"""
@@ -435,8 +434,9 @@ def test_array_input_output(self):
435434
"""
436435
output = """
437436
TARGET(OP) {
438-
PyObject **values = (stack_pointer - oparg);
437+
PyObject **values;
439438
PyObject *above;
439+
values = stack_pointer - oparg;
440440
spam(values, oparg);
441441
STACK_GROW(1);
442442
stack_pointer[-1] = above;
@@ -453,8 +453,10 @@ def test_array_error_if(self):
453453
"""
454454
output = """
455455
TARGET(OP) {
456-
PyObject **values = (stack_pointer - oparg);
457-
PyObject *extra = stack_pointer[-(1 + oparg)];
456+
PyObject **values;
457+
PyObject *extra;
458+
values = stack_pointer - oparg;
459+
extra = stack_pointer[-1 - oparg];
458460
if (oparg == 0) { STACK_SHRINK(oparg); goto pop_1_somewhere; }
459461
STACK_SHRINK(oparg);
460462
STACK_SHRINK(1);
@@ -471,18 +473,21 @@ def test_cond_effect(self):
471473
"""
472474
output = """
473475
TARGET(OP) {
474-
PyObject *cc = stack_pointer[-1];
475-
PyObject *input = ((oparg & 1) == 1) ? stack_pointer[-(1 + (((oparg & 1) == 1) ? 1 : 0))] : NULL;
476-
PyObject *aa = stack_pointer[-(2 + (((oparg & 1) == 1) ? 1 : 0))];
476+
PyObject *cc;
477+
PyObject *input = NULL;
478+
PyObject *aa;
477479
PyObject *xx;
478480
PyObject *output = NULL;
479481
PyObject *zz;
482+
cc = stack_pointer[-1];
483+
if ((oparg & 1) == 1) { input = stack_pointer[-1 - ((oparg & 1) == 1 ? 1 : 0)]; }
484+
aa = stack_pointer[-2 - ((oparg & 1) == 1 ? 1 : 0)];
480485
output = spam(oparg, input);
481486
STACK_SHRINK((((oparg & 1) == 1) ? 1 : 0));
482487
STACK_GROW(((oparg & 2) ? 1 : 0));
488+
stack_pointer[-2 - (oparg & 2 ? 1 : 0)] = xx;
489+
if (oparg & 2) { stack_pointer[-1 - (oparg & 2 ? 1 : 0)] = output; }
483490
stack_pointer[-1] = zz;
484-
if (oparg & 2) { stack_pointer[-(1 + ((oparg & 2) ? 1 : 0))] = output; }
485-
stack_pointer[-(2 + ((oparg & 2) ? 1 : 0))] = xx;
486491
DISPATCH();
487492
}
488493
"""
@@ -500,29 +505,28 @@ def test_macro_cond_effect(self):
500505
"""
501506
output = """
502507
TARGET(M) {
503-
PyObject *_tmp_1 = stack_pointer[-1];
504-
PyObject *_tmp_2 = stack_pointer[-2];
505-
PyObject *_tmp_3 = stack_pointer[-3];
508+
PyObject *right;
509+
PyObject *middle;
510+
PyObject *left;
511+
PyObject *deep;
512+
PyObject *extra = NULL;
513+
PyObject *res;
514+
// A
515+
right = stack_pointer[-1];
516+
middle = stack_pointer[-2];
517+
left = stack_pointer[-3];
506518
{
507-
PyObject *right = _tmp_1;
508-
PyObject *middle = _tmp_2;
509-
PyObject *left = _tmp_3;
510519
# Body of A
511520
}
521+
// B
512522
{
513-
PyObject *deep;
514-
PyObject *extra = NULL;
515-
PyObject *res;
516523
# Body of B
517-
_tmp_3 = deep;
518-
if (oparg) { _tmp_2 = extra; }
519-
_tmp_1 = res;
520524
}
521525
STACK_SHRINK(1);
522526
STACK_GROW((oparg ? 1 : 0));
523-
stack_pointer[-1] = _tmp_1;
524-
if (oparg) { stack_pointer[-2] = _tmp_2; }
525-
stack_pointer[-3] = _tmp_3;
527+
stack_pointer[-2 - (oparg ? 1 : 0)] = deep;
528+
if (oparg) { stack_pointer[-1 - (oparg ? 1 : 0)] = extra; }
529+
stack_pointer[-1] = res;
526530
DISPATCH();
527531
}
528532
"""

0 commit comments

Comments
 (0)