GH-125837: Split LOAD_CONST into three. (GH-125972)

* Add LOAD_CONST_IMMORTAL opcode

* Add LOAD_SMALL_INT opcode

* Remove RETURN_CONST opcode
This commit is contained in:
Mark Shannon 2024-10-29 11:15:42 +00:00 committed by GitHub
parent 67f5c5bd6f
commit faa3272fb8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
33 changed files with 706 additions and 538 deletions

View file

@ -114,7 +114,7 @@ class TestTranforms(BytecodeTestCase):
return None
self.assertNotInBytecode(f, 'LOAD_GLOBAL')
self.assertInBytecode(f, 'RETURN_CONST', None)
self.assertInBytecode(f, 'LOAD_CONST', None)
self.check_lnotab(f)
def test_while_one(self):
@ -131,7 +131,7 @@ class TestTranforms(BytecodeTestCase):
def test_pack_unpack(self):
for line, elem in (
('a, = a,', 'RETURN_CONST',),
('a, = a,', 'LOAD_CONST',),
('a, b = a, b', 'SWAP',),
('a, b, c = a, b, c', 'SWAP',),
):
@ -162,7 +162,7 @@ class TestTranforms(BytecodeTestCase):
# One LOAD_CONST for the tuple, one for the None return value
load_consts = [instr for instr in dis.get_instructions(code)
if instr.opname == 'LOAD_CONST']
self.assertEqual(len(load_consts), 1)
self.assertEqual(len(load_consts), 2)
self.check_lnotab(code)
# Bug 1053819: Tuple of constants misidentified when presented with:
@ -248,14 +248,17 @@ class TestTranforms(BytecodeTestCase):
):
with self.subTest(line=line):
code = compile(line, '', 'single')
self.assertInBytecode(code, 'LOAD_CONST', elem)
if isinstance(elem, int):
self.assertInBytecode(code, 'LOAD_SMALL_INT', elem)
else:
self.assertInBytecode(code, 'LOAD_CONST', elem)
for instr in dis.get_instructions(code):
self.assertFalse(instr.opname.startswith('BINARY_'))
self.check_lnotab(code)
# Verify that unfoldables are skipped
code = compile('a=2+"b"', '', 'single')
self.assertInBytecode(code, 'LOAD_CONST', 2)
self.assertInBytecode(code, 'LOAD_SMALL_INT', 2)
self.assertInBytecode(code, 'LOAD_CONST', 'b')
self.check_lnotab(code)
@ -307,7 +310,10 @@ class TestTranforms(BytecodeTestCase):
):
with self.subTest(line=line):
code = compile(line, '', 'single')
self.assertInBytecode(code, 'LOAD_CONST', elem)
if isinstance(elem, int):
self.assertInBytecode(code, 'LOAD_SMALL_INT', elem)
else:
self.assertInBytecode(code, 'LOAD_CONST', elem)
for instr in dis.get_instructions(code):
self.assertFalse(instr.opname.startswith('UNARY_'))
self.check_lnotab(code)
@ -989,9 +995,11 @@ class DirectCfgOptimizerTests(CfgOptimizationTestCase):
expected_insts = [
('LOAD_NAME', 1, 11),
('POP_JUMP_IF_TRUE', lbl := self.Label(), 12),
('RETURN_CONST', 1, 13),
('LOAD_CONST', 1, 13),
('RETURN_VALUE', None, 13),
lbl,
('RETURN_CONST', 2, 14),
('LOAD_CONST', 2, 14),
('RETURN_VALUE', None, 14),
]
self.cfg_optimization_test(insts,
expected_insts,
@ -1013,7 +1021,8 @@ class DirectCfgOptimizerTests(CfgOptimizationTestCase):
expected_insts = [
('NOP', None, 11),
('NOP', None, 12),
('RETURN_CONST', 1, 14),
('LOAD_CONST', 1, 14),
('RETURN_VALUE', None, 14),
]
self.cfg_optimization_test(insts,
expected_insts,
@ -1057,15 +1066,19 @@ class DirectCfgOptimizerTests(CfgOptimizationTestCase):
insts = [
('SETUP_FINALLY', handler := self.Label(), 10),
('POP_BLOCK', None, -1),
('RETURN_CONST', 1, 11),
('LOAD_CONST', 1, 11),
('RETURN_VALUE', None, 11),
handler,
('RETURN_CONST', 2, 12),
('LOAD_CONST', 2, 12),
('RETURN_VALUE', None, 12),
]
expected_insts = [
('SETUP_FINALLY', handler := self.Label(), 10),
('RETURN_CONST', 1, 11),
('LOAD_CONST', 1, 11),
('RETURN_VALUE', None, 11),
handler,
('RETURN_CONST', 2, 12),
('LOAD_CONST', 2, 12),
('RETURN_VALUE', None, 12),
]
self.cfg_optimization_test(insts, expected_insts, consts=list(range(5)))
@ -1088,7 +1101,8 @@ class DirectCfgOptimizerTests(CfgOptimizationTestCase):
('NOP', None, 3),
('STORE_FAST', 1, 4),
('POP_TOP', None, 4),
('RETURN_CONST', 0)
('LOAD_CONST', 0, 5),
('RETURN_VALUE', None, 5)
]
self.cfg_optimization_test(insts, expected_insts, consts=list(range(3)), nlocals=1)
@ -1109,7 +1123,8 @@ class DirectCfgOptimizerTests(CfgOptimizationTestCase):
('NOP', None, 3),
('POP_TOP', None, 4),
('STORE_FAST', 1, 4),
('RETURN_CONST', 0, 5)
('LOAD_CONST', 0, 5),
('RETURN_VALUE', None, 5)
]
self.cfg_optimization_test(insts, expected_insts, consts=list(range(3)), nlocals=1)
@ -1131,7 +1146,8 @@ class DirectCfgOptimizerTests(CfgOptimizationTestCase):
('STORE_FAST', 1, 4),
('STORE_FAST', 1, 5),
('STORE_FAST', 1, 6),
('RETURN_CONST', 0, 5)
('LOAD_CONST', 0, 5),
('RETURN_VALUE', None, 5)
]
self.cfg_optimization_test(insts, expected_insts, consts=list(range(3)), nlocals=1)