Skip to content

Commit b150b6a

Browse files
authored
gh-131798: Optimize _UNARY_INVERT (GH-135222)
1 parent c19e36c commit b150b6a

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,6 +2275,21 @@ def f(n):
22752275
self.assertIn("_UNPACK_SEQUENCE_TWO_TUPLE", uops)
22762276
self.assertNotIn("_GUARD_TOS_TUPLE", uops)
22772277

2278+
def test_unary_invert_long_type(self):
2279+
def testfunc(n):
2280+
for _ in range(n):
2281+
a = 9397
2282+
x = ~a + ~a
2283+
2284+
testfunc(TIER2_THRESHOLD)
2285+
2286+
ex = get_first_executor(testfunc)
2287+
self.assertIsNotNone(ex)
2288+
uops = get_opnames(ex)
2289+
2290+
self.assertNotIn("_GUARD_TOS_INT", uops)
2291+
self.assertNotIn("_GUARD_NOS_INT", uops)
2292+
22782293

22792294
def global_identity(x):
22802295
return x
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Optimize ``_UNARY_INVERT`` in JIT-compiled code.

Python/optimizer_bytecodes.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,15 @@ dummy_func(void) {
467467
res = sym_new_truthiness(ctx, value, false);
468468
}
469469

470+
op(_UNARY_INVERT, (value -- res)) {
471+
if (sym_matches_type(value, &PyLong_Type)) {
472+
res = sym_new_type(ctx, &PyLong_Type);
473+
}
474+
else {
475+
res = sym_new_not_null(ctx);
476+
}
477+
}
478+
470479
op(_COMPARE_OP, (left, right -- res)) {
471480
if (oparg & 16) {
472481
res = sym_new_type(ctx, &PyBool_Type);

Python/optimizer_cases.c.h

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)