Skip to content
Merged
Changes from 1 commit
Commits
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
Next Next commit
merge unaryop folding tests
  • Loading branch information
WolframAlph committed Mar 27, 2025
commit c03252cf232fbed8e9b7bbf3df4061637e4c609c
94 changes: 38 additions & 56 deletions Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,44 +307,6 @@ def test_binary_subscr_on_unicode(self):
self.assertInBytecode(code, 'BINARY_OP')
self.check_lnotab(code)

def test_folding_of_unaryops_on_constants(self):
for line, elem in (
('-0.5', -0.5), # unary negative
('-0.0', -0.0), # -0.0
('-(1.0-1.0)', -0.0), # -0.0 after folding
('-0', 0), # -0
('~-2', 1), # unary invert
('+1', 1), # unary positive
):
with self.subTest(line=line):
code = compile(line, '', 'single')
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)

# Check that -0.0 works after marshaling
def negzero():
return -(1.0-1.0)

for instr in dis.get_instructions(negzero):
self.assertFalse(instr.opname.startswith('UNARY_'))
self.check_lnotab(negzero)

# Verify that unfoldables are skipped
for line, elem, opname in (
('-"abc"', 'abc', 'UNARY_NEGATIVE'),
('~"abc"', 'abc', 'UNARY_INVERT'),
):
with self.subTest(line=line):
code = compile(line, '', 'single')
self.assertInBytecode(code, 'LOAD_CONST', elem)
self.assertInBytecode(code, opname)
self.check_lnotab(code)

def test_elim_extra_return(self):
# RETURN LOAD_CONST None RETURN --> RETURN
def f(x):
Expand Down Expand Up @@ -518,27 +480,47 @@ def test_constant_folding_small_int(self):
def test_folding_unaryop(self):
intrinsic_positive = 5
tests = [
('---1', 'UNARY_NEGATIVE', None, True),
('---""', 'UNARY_NEGATIVE', None, False),
('~~~1', 'UNARY_INVERT', None, True),
('~~~""', 'UNARY_INVERT', None, False),
('not not True', 'UNARY_NOT', None, True),
('not not x', 'UNARY_NOT', None, True), # this should be optimized regardless of constant or not
('+++1', 'CALL_INTRINSIC_1', intrinsic_positive, True),
('---x', 'UNARY_NEGATIVE', None, False),
('~~~x', 'UNARY_INVERT', None, False),
('+++x', 'CALL_INTRINSIC_1', intrinsic_positive, False),
]

for expr, opcode, oparg, optimized in tests:
with self.subTest(expr=expr, optimized=optimized):
code = compile(expr, '', 'single')
if optimized:
self.assertNotInBytecode(code, opcode, argval=oparg)
('-0', 'UNARY_NEGATIVE', None, True, 'LOAD_SMALL_INT', 0),
('-0.0', 'UNARY_NEGATIVE', None, True, 'LOAD_CONST', -0.0),
('-(1.0-1.0)', 'UNARY_NEGATIVE', None, True, 'LOAD_CONST', -0.0),
('-0.5', 'UNARY_NEGATIVE', None, True, 'LOAD_CONST', -0.5),
('---1', 'UNARY_NEGATIVE', None, True, 'LOAD_CONST', -1),
('---""', 'UNARY_NEGATIVE', None, False, None, None),
('~~~1', 'UNARY_INVERT', None, True, 'LOAD_CONST', -2),
('~~~""', 'UNARY_INVERT', None, False, None, None),
('not not True', 'UNARY_NOT', None, True, 'LOAD_CONST', True),
('not not x', 'UNARY_NOT', None, True, 'LOAD_NAME', 'x'), # this should be optimized regardless of constant or not
('+++1', 'CALL_INTRINSIC_1', intrinsic_positive, True, 'LOAD_SMALL_INT', 1),
('---x', 'UNARY_NEGATIVE', None, False, None, None),
('~~~x', 'UNARY_INVERT', None, False, None, None),
('+++x', 'CALL_INTRINSIC_1', intrinsic_positive, False, None, None),
]

for (
expr,
original_opcode,
original_argval,
is_optimized,
optimized_opcode,
optimized_argval,
) in tests:
with self.subTest(expr=expr, is_optimized=is_optimized):
code = compile(expr, "", "single")
if is_optimized:
self.assertNotInBytecode(code, original_opcode, argval=original_argval)
self.assertInBytecode(code, optimized_opcode, argval=optimized_argval)
else:
self.assertInBytecode(code, opcode, argval=oparg)
self.assertInBytecode(code, original_opcode, argval=original_argval)
self.check_lnotab(code)

# Check that -0.0 works after marshaling
def negzero():
return -(1.0-1.0)

for instr in dis.get_instructions(negzero):
self.assertFalse(instr.opname.startswith('UNARY_'))
self.check_lnotab(negzero)

def test_folding_binop(self):
tests = [
('1 + 2', False, 'NB_ADD'),
Expand Down