8000 gh-93143: Avoid NULL check in LOAD_FAST based on analysis in the compiler by sweeneyde · Pull Request #93144 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-93143: Avoid NULL check in LOAD_FAST based on analysis in the compiler #93144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3627bb5
implement LOAD_FAST_KNOWN
sweeneyde May 10, 2022
ac0ad1b
fix leak and add stack effect
sweeneyde May 10, 2022
721e6ed
add failing test
sweeneyde May 10, 2022
42ef42f
Attempt exception handling
sweeneyde May 10, 2022
a8e94f8
Parameters are guaranteed to be initialized
sweeneyde May 10, 2022
038a85d
revert to LOAD_FAST on lineno setter and LocalsToFast
sweeneyde May 10, 2022
3243981
Port over superinstructions
sweeneyde May 10, 2022
0ddc721
remove debugging code so non-debug builds work.
sweeneyde May 10, 2022
35d2c51
merge with main
sweeneyde May 23, 2022
9c74fe5
Rename to LOAD_FAST_CHECK
sweeneyde May 23, 2022
b7c689b
revert debugging code
sweeneyde May 23, 2022
2c58c67
Add test cases
sweeneyde May 23, 2022
8f58dd6
Fix test_dis
sweeneyde May 23, 2022
f32c419
switch the opcode numbers back
sweeneyde May 23, 2022
1f14713
📜🤖 Added by blurb_it.
blurb-it[bot] May 23, 2022
f224e20
Don't access last instruction of empty basicblocks
sweeneyde May 23, 2022
5480a12
Merge branch 'known' of https://github.com/sweeneyde/cpython into known
sweeneyde May 23, 2022
b9da528
don't accidentally export symbols
sweeneyde May 23, 2022
2b15f1c
add description to magic number bump, only bump by 1
sweeneyde May 24, 2022
6feb63c
typo
sweeneyde May 24, 2022
90de166
merge with main
sweeneyde May 27, 2022
75ae65d
use 'instr' var again
sweeneyde May 27, 2022
70e1ba8
rename, combine switch cases
sweeneyde May 29, 2022
a1d8b01
simplify the is-a-parameter logic
sweeneyde May 29, 2022
d193540
assert no extended args
sweeneyde May 29, 2022
8aa1132
assert no superinstructions
sweeneyde May 29, 2022
138eeaf
Mark unsafe except blocks of every instruction (with any oparg)
sweeneyde May 30, 2022
202e1e7
Only add checks when deleting, not modifying, locals
sweeneyde May 30, 2022
d569c5f
Merge remote-tracking branch 'upstream/main' into known
sweeneyde May 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions Include/internal/pycore_opcode.h

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

31 changes: 16 additions & 15 deletions Include/opcode.h

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

3 changes: 2 additions & 1 deletion Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ def _write_atomic(path, data, mode=0o666):

# Python 3.12a1 3500 (Remove PRECALL opcode)
# Python 3.12a1 3501 (YIELD_VALUE oparg == stack_depth)
# Python 3.12a1 3502 (LOAD_FAST_CHECK, no NULL-check in LOAD_FAST)

# Python 3.13 will start with 3550

Expand All @@ -419,7 +420,7 @@ def _write_atomic(path, data, mode=0o666):
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated.

MAGIC_NUMBER = (3501).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3502).to_bytes(2, 'little') + b'\r\n'

_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

Expand Down
4 changes: 3 additions & 1 deletion Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,14 @@ def jabs_op(name, op):
def_op('COPY', 120)
def_op('BINARY_OP', 122)
jrel_op('SEND', 123) # Number of bytes to skip
def_op('LOAD_FAST', 124) # Local variable number
def_op('LOAD_FAST', 124) # Local variable number, no null check
haslocal.append(124)
def_op('STORE_FAST', 125) # Local variable number
haslocal.append(125)
def_op('DELETE_FAST', 126) # Local variable number
haslocal.append(126)
def_op('LOAD_FAST_CHECK', 127) # Local variable number
haslocal.append(127)
jrel_op('POP_JUMP_FORWARD_IF_NOT_NONE', 128)
jrel_op('POP_JUMP_FORWARD_IF_NONE', 129)
def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3)
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def bug42562():
--> BINARY_OP 11 (/)
POP_TOP

%3d LOAD_FAST 1 (tb)
%3d LOAD_FAST_CHECK 1 (tb)
RETURN_VALUE
>> PUSH_EXC_INFO

Expand Down Expand Up @@ -1399,7 +1399,7 @@ def _prepare_test_cases():
Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=100, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='CALL', opcode=171, arg=1, argval=1, argrepr='', offset=102, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=112, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=114, starts_line=11, is_jump_target=True, positions=None),
Instruction(opname='LOAD_FAST_CHECK', opcode=127, arg=0, argval='i', argrepr='i', offset=114, starts_line=11, is_jump_target=True, positions=None),
Instruction(opname='POP_JUMP_FORWARD_IF_FALSE', opcode=114, arg=34, argval=186, argrepr='to 186', offset=116, starts_line=None, is_jump_target=False, positions=None),
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=3, argval='print', argrepr='NULL + print', offset=118, starts_line=12, is_jump_target=True, positions=None),
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=130, starts_line=None, is_jump_target=False, positions=None),
Expand Down
180 changes: 180 additions & 0 deletions Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import dis
from itertools import combinations, product
import sys
import textwrap
import unittest

Expand Down Expand Up @@ -682,5 +683,184 @@ def test_bpo_45773_pop_jump_if_false(self):
compile("while True or not spam: pass", "<test>", "exec")


class TestMarkingVariablesAsUnKnown(BytecodeTestCase):

def setUp(self):
self.addCleanup(sys.settrace, sys.gettrace())
sys.settrace(None)

def test_load_fast_known_simple(self):
def f():
x = 1
y = x + x
self.assertInBytecode(f, 'LOAD_FAST')

def test_load_fast_unknown_simple(self):
def f():
if condition():
x = 1
print(x)
self.assertInBytecode(f, 'LOAD_FAST_CHECK')
self.assertNotInBytecode(f, 'LOAD_FAST')

def test_load_fast_unknown_because_del(self):
def f():
x = 1
del x
print(x)
self.assertInBytecode(f, 'LOAD_FAST_CHECK')
self.assertNotInBytecode(f, 'LOAD_FAST')

def test_load_fast_known_because_parameter(self):
def f1(x):
print(x)
self.assertInBytecode(f1, 'LOAD_FAST')
self.assertNotInBytecode(f1, 'LOAD_FAST_CHECK')

def f2(*, x):
print(x)
self.assertInBytecode(f2, 'LOAD_FAST')
self.assertNotInBytecode(f2, 'LOAD_FAST_CHECK')

def f3(*args):
print(args)
self.assertInBytecode(f3, 'LOAD_FAST')
self.assertNotInBytecode(f3, 'LOAD_FAST_CHECK')

def f4(**kwargs):
print(kwargs)
self.assertInBytecode(f4, 'LOAD_FAST')
self.assertNotInBytecode(f4, 'LOAD_FAST_CHECK')

def f5(x=0):
print(x)
self.assertInBytecode(f5, 'LOAD_FAST')
self.assertNotInBytecode(f5, 'LOAD_FAST_CHECK')

def test_load_fast_known_because_already_loaded(self):
def f():
if condition():
x = 1
print(x)
print(x)
self.assertInBytecode(f, 'LOAD_FAST_CHECK')
self.assertInBytecode(f, 'LOAD_FAST')

def test_load_fast_known_multiple_branches(self):
def f():
if condition():
x = 1
else:
x = 2
print(x)
self.assertInBytecode(f, 'LOAD_FAST')
self.assertNotInBytecode(f, 'LOAD_FAST_CHECK')

def test_load_fast_unknown_after_error(self):
def f():
try:
res = 1 / 0
except ZeroDivisionError:
pass
return res
# LOAD_FAST (known) still occurs in the no-exception branch.
# Assert that it doesn't occur in the LOAD_FAST_CHECK branch.
self.assertInBytecode(f, 'LOAD_FAST_CHECK')

def test_load_fast_unknown_after_error_2(self):
def f():
try:
1 / 0
except:
print(a, b, c, d, e, f, g)
a = b = c = d = e = f = g = 1
self.assertInBytecode(f, 'LOAD_FAST_CHECK')
self.assertNotInBytecode(f, 'LOAD_FAST')

def test_setting_lineno_adds_check(self):
code = textwrap.dedent("""\
def f():
x = 2
L = 3
L = 4
for i in range(55):
x + 6
del x
L = 8
L = 9
L = 10
""")
ns = {}
exec(code, ns)
f = ns['f']
self.assertInBytecode(f, "LOAD_FAST")
def trace(frame, event, arg):
if event == 'line' and frame.f_lineno == 9:
frame.f_lineno = 2
sys.settrace(None)
return None
return trace
sys.settrace(trace)
f()
self.assertNotInBytecode(f, "LOAD_FAST")

def make_function_with_no_checks(self):
code = textwrap.dedent("""\
def f():
x = 2
L = 3
L = 4
L = 5
if not L:
x + 7
y = 2
""")
ns = {}
exec(code, ns)
f = ns['f']
self.assertInBytecode(f, "LOAD_FAST")
self.assertNotInBytecode(f, "LOAD_FAST_CHECK")
return f

def test_deleting_local_adds_check(self):
f = self.make_function_with_no_checks()
def trace(frame, event, arg):
if event == 'line' and frame.f_lineno == 4:
del frame.f_locals["x"]
sys.settrace(None)
return None
return trace
sys.settrace(trace)
f()
self.assertNotInBytecode(f, "LOAD_FAST")
self.assertInBytecode(f, "LOAD_FAST_CHECK")

def test_modifying_local_does_not_add_check(self):
f = self.make_function_with_no_checks()
def trace(frame, event, arg):
if event == 'line' and frame.f_lineno == 4:
frame.f_locals["x"] = 42
sys.settrace(None)
return None
return trace
sys.settrace(trace)
f()
self.assertInBytecode(f, "LOAD_FAST")
self.assertNotInBytecode(f, "LOAD_FAST_CHECK")

def test_initializing_local_does_not_add_check(self):
f = self.make_function_with_no_checks()
def trace(frame, event, arg):
if event == 'line' and frame.f_lineno == 4:
frame.f_locals["y"] = 42
sys.settrace(None)
return None
return trace
sys.settrace(trace)
f()
self.assertInBytecode(f, "LOAD_FAST")
self.assertNotInBytecode(f, "LOAD_FAST_CHECK")


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid ``NULL`` checks for uninitialized local variables by determining at compile time which variables must be initialized.
Loading
0