1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
|
import contextlib
import tempfile
import unittest
import os
from test import support
from test import test_tools
test_tools.skip_if_missing('cases_generator')
with test_tools.imports_under_tool('cases_generator'):
import generate_cases
import analysis
import formatting
from parsing import StackEffect
def handle_stderr():
if support.verbose > 1:
return contextlib.nullcontext()
else:
return support.captured_stderr()
class TestEffects(unittest.TestCase):
def test_effect_sizes(self):
input_effects = [
x := StackEffect("x", "", "", ""),
y := StackEffect("y", "", "", "oparg"),
z := StackEffect("z", "", "", "oparg*2"),
]
output_effects = [
StackEffect("a", "", "", ""),
StackEffect("b", "", "", "oparg*4"),
StackEffect("c", "", "", ""),
]
other_effects = [
StackEffect("p", "", "", "oparg<<1"),
StackEffect("q", "", "", ""),
StackEffect("r", "", "", ""),
]
self.assertEqual(formatting.effect_size(x), (1, ""))
self.assertEqual(formatting.effect_size(y), (0, "oparg"))
self.assertEqual(formatting.effect_size(z), (0, "oparg*2"))
self.assertEqual(
formatting.list_effect_size(input_effects),
(1, "oparg + oparg*2"),
)
self.assertEqual(
formatting.list_effect_size(output_effects),
(2, "oparg*4"),
)
self.assertEqual(
formatting.list_effect_size(other_effects),
(2, "(oparg<<1)"),
)
class TestGeneratedCases(unittest.TestCase):
def setUp(self) -> None:
super().setUp()
self.maxDiff = None
self.temp_dir = tempfile.gettempdir()
self.temp_input_filename = os.path.join(self.temp_dir, "input.txt")
self.temp_output_filename = os.path.join(self.temp_dir, "output.txt")
self.temp_metadata_filename = os.path.join(self.temp_dir, "metadata.txt")
self.temp_pymetadata_filename = os.path.join(self.temp_dir, "pymetadata.txt")
self.temp_executor_filename = os.path.join(self.temp_dir, "executor.txt")
def tearDown(self) -> None:
for filename in [
self.temp_input_filename,
self.temp_output_filename,
self.temp_metadata_filename,
self.temp_pymetadata_filename,
self.temp_executor_filename,
]:
try:
os.remove(filename)
except:
pass
super().tearDown()
def run_cases_test(self, input: str, expected: str):
with open(self.temp_input_filename, "w+") as temp_input:
temp_input.write(analysis.BEGIN_MARKER)
temp_input.write(input)
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)
with open(self.temp_output_filename) as temp_output:
lines = temp_output.readlines()
while lines and lines[0].startswith("// "):
lines.pop(0)
actual = "".join(lines)
# if actual.rstrip() != expected.rstrip():
# print("Actual:")
# print(actual)
# print("Expected:")
# print(expected)
# print("End")
self.assertEqual(actual.rstrip(), expected.rstrip())
def test_inst_no_args(self):
input = """
inst(OP, (--)) {
spam();
}
"""
output = """
TARGET(OP) {
spam();
DISPATCH();
}
"""
self.run_cases_test(input, output)
def test_inst_one_pop(self):
input = """
inst(OP, (value --)) {
spam();
}
"""
output = """
TARGET(OP) {
PyObject *value;
value = stack_pointer[-1];
spam();
STACK_SHRINK(1);
DISPATCH();
}
"""
self.run_cases_test(input, output)
def test_inst_one_push(self):
input = """
inst(OP, (-- res)) {
spam();
}
"""
output = """
TARGET(OP) {
PyObject *res;
spam();
STACK_GROW(1);
stack_pointer[-1] = res;
DISPATCH();
}
"""
self.run_cases_test(input, output)
def test_inst_one_push_one_pop(self):
input = """
inst(OP, (value -- res)) {
spam();
}
"""
output = """
TARGET(OP) {
PyObject *value;
PyObject *res;
value = stack_pointer[-1];
spam();
stack_pointer[-1] = res;
DISPATCH();
}
"""
self.run_cases_test(input, output)
def test_binary_op(self):
input = """
inst(OP, (left, right -- res)) {
spam();
}
"""
output = """
TARGET(OP) {
PyObject *right;
PyObject *left;
PyObject *res;
right = stack_pointer[-1];
left = stack_pointer[-2];
spam();
STACK_SHRINK(1);
stack_pointer[-1] = res;
DISPATCH();
}
"""
self.run_cases_test(input, output)
def test_overlap(self):
input = """
inst(OP, (left, right -- left, result)) {
spam();
}
"""
output = """
TARGET(OP) {
PyObject *right;
PyObject *left;
PyObject *result;
right = stack_pointer[-1];
left = stack_pointer[-2];
spam();
stack_pointer[-1] = result;
DISPATCH();
}
"""
self.run_cases_test(input, output)
def test_predictions_and_eval_breaker(self):
input = """
inst(OP1, (--)) {
}
inst(OP3, (arg -- res)) {
DEOPT_IF(xxx, OP1);
CHECK_EVAL_BREAKER();
}
"""
output = """
TARGET(OP1) {
PREDICTED(OP1);
DISPATCH();
}
TARGET(OP3) {
PyObject *arg;
PyObject *res;
arg = stack_pointer[-1];
DEOPT_IF(xxx, OP1);
stack_pointer[-1] = res;
CHECK_EVAL_BREAKER();
DISPATCH();
}
"""
self.run_cases_test(input, output)
def test_error_if_plain(self):
input = """
inst(OP, (--)) {
ERROR_IF(cond, label);
}
"""
output = """
TARGET(OP) {
if (cond) goto label;
DISPATCH();
}
"""
self.run_cases_test(input, output)
def test_error_if_plain_with_comment(self):
input = """
inst(OP, (--)) {
ERROR_IF(cond, label); // Comment is ok
}
"""
output = """
TARGET(OP) {
if (cond) goto label;
DISPATCH();
}
"""
self.run_cases_test(input, output)
def test_error_if_pop(self):
input = """
inst(OP, (left, right -- res)) {
ERROR_IF(cond, label);
}
"""
output = """
TARGET(OP) {
PyObject *right;
PyObject *left;
PyObject *res;
right = stack_pointer[-1];
left = stack_pointer[-2];
if (cond) goto pop_2_label;
STACK_SHRINK(1);
stack_pointer[-1] = res;
DISPATCH();
}
"""
self.run_cases_test(input, output)
def test_cache_effect(self):
input = """
inst(OP, (counter/1, extra/2, value --)) {
}
"""
output = """
TARGET(OP) {
PyObject *value;
value = stack_pointer[-1];
uint16_t counter = read_u16(&next_instr[0].cache);
uint32_t extra = read_u32(&next_instr[1].cache);
STACK_SHRINK(1);
next_instr += 3;
DISPATCH();
}
"""
self.run_cases_test(input, output)
def test_suppress_dispatch(self):
input = """
inst(OP, (--)) {
goto somewhere;
}
"""
output = """
TARGET(OP) {
goto somewhere;
}
"""
self.run_cases_test(input, output)
def test_macro_instruction(self):
input = """
inst(OP1, (counter/1, left, right -- left, right)) {
op1(left, right);
}
op(OP2, (extra/2, arg2, left, right -- res)) {
res = op2(arg2, left, right);
}
macro(OP) = OP1 + cache/2 + OP2;
inst(OP3, (unused/5, arg2, left, right -- res)) {
res = op3(arg2, left, right);
}
family(OP, INLINE_CACHE_ENTRIES_OP) = { OP3 };
"""
output = """
TARGET(OP1) {
PyObject *right;
PyObject *left;
right = stack_pointer[-1];
left = stack_pointer[-2];
uint16_t counter = read_u16(&next_instr[0].cache);
op1(left, right);
next_instr += 1;
DISPATCH();
}
TARGET(OP) {
static_assert(INLINE_CACHE_ENTRIES_OP == 5, "incorrect cache size");
PyObject *right;
PyObject *left;
PyObject *arg2;
PyObject *res;
// OP1
right = stack_pointer[-1];
left = stack_pointer[-2];
{
uint16_t counter = read_u16(&next_instr[0].cache);
op1(left, right);
}
// OP2
arg2 = stack_pointer[-3];
{
uint32_t extra = read_u32(&next_instr[3].cache);
res = op2(arg2, left, right);
}
STACK_SHRINK(2);
stack_pointer[-1] = res;
next_instr += 5;
DISPATCH();
}
TARGET(OP3) {
PyObject *right;
PyObject *left;
PyObject *arg2;
PyObject *res;
right = stack_pointer[-1];
left = stack_pointer[-2];
arg2 = stack_pointer[-3];
res = op3(arg2, left, right);
STACK_SHRINK(2);
stack_pointer[-1] = res;
next_instr += 5;
DISPATCH();
}
"""
self.run_cases_test(input, output)
def test_array_input(self):
input = """
inst(OP, (below, values[oparg*2], above --)) {
spam();
}
"""
output = """
TARGET(OP) {
PyObject *above;
PyObject **values;
PyObject *below;
above = stack_pointer[-1];
values = stack_pointer - 1 - oparg*2;
below = stack_pointer[-2 - oparg*2];
spam();
STACK_SHRINK(oparg*2);
STACK_SHRINK(2);
DISPATCH();
}
"""
self.run_cases_test(input, output)
def test_array_output(self):
input = """
inst(OP, (unused, unused -- below, values[oparg*3], above)) {
spam(values, oparg);
}
"""
output = """
TARGET(OP) {
PyObject *below;
PyObject **values;
PyObject *above;
values = stack_pointer - 1;
spam(values, oparg);
STACK_GROW(oparg*3);
stack_pointer[-2 - oparg*3] = below;
stack_pointer[-1] = above;
DISPATCH();
}
"""
self.run_cases_test(input, output)
def test_array_input_output(self):
input = """
inst(OP, (values[oparg] -- values[oparg], above)) {
spam(values, oparg);
}
"""
output = """
TARGET(OP) {
PyObject **values;
PyObject *above;
values = stack_pointer - oparg;
spam(values, oparg);
STACK_GROW(1);
stack_pointer[-1] = above;
DISPATCH();
}
"""
self.run_cases_test(input, output)
def test_array_error_if(self):
input = """
inst(OP, (extra, values[oparg] --)) {
ERROR_IF(oparg == 0, somewhere);
}
"""
output = """
TARGET(OP) {
PyObject **values;
PyObject *extra;
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);
DISPATCH();
}
"""
self.run_cases_test(input, output)
def test_cond_effect(self):
input = """
inst(OP, (aa, input if ((oparg & 1) == 1), cc -- xx, output if (oparg & 2), zz)) {
output = spam(oparg, input);
}
"""
output = """
TARGET(OP) {
PyObject *cc;
PyObject *input = NULL;
PyObject *aa;
PyObject *xx;
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)];
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;
DISPATCH();
}
"""
self.run_cases_test(input, output)
def test_macro_cond_effect(self):
input = """
op(A, (left, middle, right --)) {
# Body of A
}
op(B, (-- deep, extra if (oparg), res)) {
# Body of B
}
macro(M) = A + B;
"""
output = """
TARGET(M) {
PyObject *right;
PyObject *middle;
PyObject *left;
PyObject *deep;
PyObject *extra = NULL;
PyObject *res;
// A
right = stack_pointer[-1];
middle = stack_pointer[-2];
left = stack_pointer[-3];
{
# Body of A
}
// B
{
# 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;
DISPATCH();
}
"""
self.run_cases_test(input, output)
def test_macro_push_push(self):
input = """
op(A, (-- val1)) {
val1 = spam();
}
op(B, (-- val2)) {
val2 = spam();
}
macro(M) = A + B;
"""
output = """
TARGET(M) {
PyObject *val1;
PyObject *val2;
// A
{
val1 = spam();
}
// B
{
val2 = spam();
}
STACK_GROW(2);
stack_pointer[-2] = val1;
stack_pointer[-1] = val2;
DISPATCH();
}
"""
self.run_cases_test(input, output)
if __name__ == "__main__":
unittest.main()
|