FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-131798: JIT: Narrow the return type of `_BINARY_OP_SUBSCR_STR_INT` to `str` by tomasr8 · Pull Request #132153 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .h  (1) .py  (1) .rst  (1) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
20 changes: 20 additions & 0 deletions Lib/test/test_capi/test_opt.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,26 @@ def f(n):
self.assertIn("_TO_BOOL_STR", uops)
self.assertNotIn("_GUARD_TOS_UNICODE", uops)

def test_binary_subcsr_str_int_narrows_to_str(self):
def testfunc(n):
x = []
s = "foo"
for _ in range(n):
y = s[0] # _BINARY_OP_SUBSCR_STR_INT
z = "bar" + y # (_GUARD_TOS_UNICODE) + _BINARY_OP_ADD_UNICODE
x.append(z)
return x

res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(res, ["barf"] * TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertIn("_BINARY_OP_SUBSCR_STR_INT", uops)
# _BINARY_OP_SUBSCR_STR_INT narrows the result to 'str' so
# the unicode guard before _BINARY_OP_ADD_UNICODE is removed.
self.assertNotIn("_GUARD_TOS_UNICODE", uops)
self.assertIn("_BINARY_OP_ADD_UNICODE", uops)


def global_identity(x):
return x
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Allow the JIT to remove unicode guards after ``_BINARY_OP_SUBSCR_STR_INT``
by setting the return type to string.
4 changes: 4 additions & 0 deletions Python/optimizer_bytecodes.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ dummy_func(void) {
ctx->done = true;
}

op(_BINARY_OP_SUBSCR_STR_INT, (left, right -- res)) {
res = sym_new_type(ctx, &PyUnicode_Type);
}

op(_TO_BOOL, (value -- res)) {
int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
if (!already_bool) {
Expand Down
2 changes: 1 addition & 1 deletion Python/optimizer_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.


Back | FazBrowse Home | New Git URL