Skip to content

Commit 62ef3d8

Browse files
committed
pythongh-104584: Clean up and fix uops tests and fix crash (python#106492)
The uops test wasn't testing anything by default, and was failing when run with -Xuops. Made the two executor-related context managers global, so TestUops can use them (notably `with temporary_optimizer(opt)`). Made clear_executor() a little more thorough. Fixed a crash upon finalizing a uop optimizer, by adding a `tp_dealloc` handler.
1 parent 3406f8c commit 62ef3d8

File tree

2 files changed

+55
-20
lines changed

2 files changed

+55
-20
lines changed

Lib/test/test_capi/test_misc.py

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2343,23 +2343,26 @@ def func():
23432343
names = ["func", "outer", "outer", "inner", "inner", "outer", "inner"]
23442344
self.do_test(func, names)
23452345

2346-
class TestOptimizerAPI(unittest.TestCase):
23472346

2348-
@contextlib.contextmanager
2349-
def temporary_optimizer(self, opt):
2350-
_testinternalcapi.set_optimizer(opt)
2351-
try:
2352-
yield
2353-
finally:
2354-
_testinternalcapi.set_optimizer(None)
2347+
@contextlib.contextmanager
2348+
def temporary_optimizer(opt):
2349+
_testinternalcapi.set_optimizer(opt)
2350+
try:
2351+
yield
2352+
finally:
2353+
_testinternalcapi.set_optimizer(None)
2354+
2355+
@contextlib.contextmanager
2356+
def clear_executors(func):
2357+
# Clear executors in func before and after running a block
2358+
func.__code__ = func.__code__.replace()
2359+
try:
2360+
yield
2361+
finally:
2362+
func.__code__ = func.__code__.replace()
23552363

2356-
@contextlib.contextmanager
2357-
def clear_executors(self, func):
2358-
try:
2359-
yield
2360-
finally:
2361-
#Clear executors
2362-
func.__code__ = func.__code__.replace()
2364+
2365+
class TestOptimizerAPI(unittest.TestCase):
23632366

23642367
def test_get_set_optimizer(self):
23652368
self.assertEqual(_testinternalcapi.get_optimizer(), None)
@@ -2381,9 +2384,9 @@ def loop():
23812384

23822385
for repeat in range(5):
23832386
opt = _testinternalcapi.get_counter_optimizer()
2384-
with self.temporary_optimizer(opt):
2387+
with temporary_optimizer(opt):
23852388
self.assertEqual(opt.get_count(), 0)
2386-
with self.clear_executors(loop):
2389+
with clear_executors(loop):
23872390
loop()
23882391
self.assertEqual(opt.get_count(), 1000)
23892392

@@ -2409,11 +2412,37 @@ def long_loop():
24092412
long_loop = ns['long_loop']
24102413

24112414
opt = _testinternalcapi.get_counter_optimizer()
2412-
with self.temporary_optimizer(opt):
2415+
with temporary_optimizer(opt):
24132416
self.assertEqual(opt.get_count(), 0)
24142417
long_loop()
24152418
self.assertEqual(opt.get_count(), 10)
24162419

24172420

2421+
class TestUops(unittest.TestCase):
2422+
2423+
def test_basic_loop(self):
2424+
def testfunc(x):
2425+
i = 0
2426+
while i < x:
2427+
i += 1
2428+
2429+
opt = _testinternalcapi.get_uop_optimizer()
2430+
2431+
with temporary_optimizer(opt):
2432+
testfunc(1000)
2433+
2434+
ex = None
2435+
for offset in range(0, len(testfunc.__code__.co_code), 2):
2436+
try:
2437+
ex = _testinternalcapi.get_executor(testfunc.__code__, offset)
2438+
break
2439+
except ValueError:
2440+
pass
2441+
self.assertIsNotNone(ex)
2442+
uops = {opname for opname, _ in ex}
2443+
self.assertIn("SAVE_IP", uops)
2444+
self.assertIn("LOAD_FAST", uops)
2445+
2446+
24182447
if __name__ == "__main__":
24192448
unittest.main()

Python/optimizer.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ uop_optimize(
458458
return trace_length;
459459
}
460460
OBJECT_STAT_INC(optimization_traces_created);
461-
_PyUOpExecutorObject *executor = (_PyUOpExecutorObject *)_PyObject_New(&UOpExecutor_Type);
461+
_PyUOpExecutorObject *executor = PyObject_New(_PyUOpExecutorObject, &UOpExecutor_Type);
462462
if (executor == NULL) {
463463
return -1;
464464
}
@@ -468,18 +468,24 @@ uop_optimize(
468468
return 1;
469469
}
470470

471+
static void
472+
uop_opt_dealloc(PyObject *self) {
473+
PyObject_Free(self);
474+
}
475+
471476
static PyTypeObject UOpOptimizer_Type = {
472477
PyVarObject_HEAD_INIT(&PyType_Type, 0)
473478
.tp_name = "uop_optimizer",
474479
.tp_basicsize = sizeof(_PyOptimizerObject),
475480
.tp_itemsize = 0,
476481
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
482+
.tp_dealloc = uop_opt_dealloc,
477483
};
478484

479485
PyObject *
480486
PyUnstable_Optimizer_NewUOpOptimizer(void)
481487
{
482-
_PyOptimizerObject *opt = (_PyOptimizerObject *)_PyObject_New(&UOpOptimizer_Type);
488+
_PyOptimizerObject *opt = PyObject_New(_PyOptimizerObject, &UOpOptimizer_Type);
483489
if (opt == NULL) {
484490
return NULL;
485491
}

0 commit comments

Comments
 (0)