Skip to content

GH-113710: Tier 2 optimizer: check the function instead of checking globals. #116410

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

Conversation

markshannon
Copy link
Member

@markshannon markshannon commented Mar 6, 2024

Since the globals and builtin fields of function objects are immutable, we can check the function instead of the globals and builtins.

This is:

  • Never slower, we never add more checks
  • Faster for inlined calls as _INIT_CALL_PY_EXACT_ARGS implicitly checks the function, so no additional checks are needed.
  • Faster in many other cases, as we can replace _CHECK_GLOBALS + _CHECK_BUILTINS with _CHECK_FUNCTION.

@markshannon markshannon merged commit 27858e2 into python:main Mar 6, 2024
@gvanrossum
Copy link
Member

@markshannon, Is this correct? WhenI run a really simple test with debug output, I get a DEOPT message for _CHECK_FUNCTION for each iteration; before this PR I only got one DEOPT message at the end of the loop.

Code:

import _testinternalcapi

def cast(typ, val):
    return val

def testfunc(n):
    x = 0
    for i in range(n):
        x = cast(int, i)
    return x

def main():
    count = 20
    a = testfunc(count)
    assert a + 1 == count, (a, count)
    old_opt = _testinternalcapi.get_optimizer()
    opt = _testinternalcapi.new_uop_optimizer()
    _testinternalcapi.set_optimizer(opt)
    a = testfunc(count)
    _testinternalcapi.set_optimizer(old_opt)
    assert a + 1 == count, (a, count)

main()

Before:

Optimized executor (length 27):
   0 OPTIMIZED: _START_EXECUTOR (52685, target=-842150451, operand=12a70eb70)
   1 OPTIMIZED: _CHECK_VALIDITY (0, target=33, operand=1031faf9a)
   2 OPTIMIZED: _SET_IP (0, target=14, operand=1031faf74)
   3 OPTIMIZED: _ITER_CHECK_RANGE (19, target=14, operand=0)
   4 OPTIMIZED: _GUARD_NOT_EXHAUSTED_RANGE (19, target=14, operand=0)
   5 OPTIMIZED: _ITER_NEXT_RANGE (19, target=14, operand=0)
   6 OPTIMIZED: _CHECK_VALIDITY (0, target=16, operand=1031faf78)
   7 OPTIMIZED: _STORE_FAST_2 (2, target=16, operand=0)
   8 OPTIMIZED: _CHECK_GLOBALS (3, target=17, operand=10330f050)
   9 OPTIMIZED: _LOAD_CONST_INLINE_WITH_NULL (3, target=17, operand=1032fa150)
  10 OPTIMIZED: _CHECK_BUILTINS (4, target=22, operand=10323fa70)
  11 OPTIMIZED: _LOAD_CONST_INLINE_BORROW (4, target=22, operand=102e39508)
  12 OPTIMIZED: _LOAD_FAST_2 (2, target=27, operand=0)
  13 OPTIMIZED: _SET_IP (0, target=28, operand=1031faf90)
  14 OPTIMIZED: _CHECK_FUNCTION_EXACT_ARGS (2, target=28, operand=2c9)
  15 OPTIMIZED: _CHECK_STACK_SPACE (2, target=28, operand=0)
  16 OPTIMIZED: _INIT_CALL_PY_EXACT_ARGS_2 (2, target=28, operand=0)
  17 OPTIMIZED: _SAVE_RETURN_OFFSET (4, target=28, operand=0)
  18 OPTIMIZED: _PUSH_FRAME (2, target=28, operand=1032fa150)
  19 OPTIMIZED: _CHECK_VALIDITY (0, target=0, operand=1033690f8)
  20 OPTIMIZED: _RESUME_CHECK (0, target=0, operand=0)
  21 OPTIMIZED: _LOAD_FAST_1 (1, target=1, operand=0)
  22 OPTIMIZED: _SET_IP (0, target=2, operand=1033690fc)
  23 OPTIMIZED: _POP_FRAME (0, target=2, operand=10336fa10)
  24 OPTIMIZED: _CHECK_VALIDITY (0, target=32, operand=1031faf98)
  25 OPTIMIZED: _STORE_FAST_1 (1, target=32, operand=0)
  26 OPTIMIZED: _JUMP_TO_TOP (0, target=0, operand=0)
DEOPT: [UOp _GUARD_NOT_EXHAUSTED_RANGE (19, target=14, operand=0) -> FOR_ITER_RANGE]

After:

Optimized executor (length 26):
   0 OPTIMIZED: _START_EXECUTOR (52685, target=-842150451, operand=12670ec50)
   1 OPTIMIZED: _CHECK_VALIDITY (0, target=33, operand=100dd2f9a)
   2 OPTIMIZED: _SET_IP (0, target=14, operand=100dd2f74)
   3 OPTIMIZED: _ITER_CHECK_RANGE (19, target=14, operand=0)
   4 OPTIMIZED: _GUARD_NOT_EXHAUSTED_RANGE (19, target=14, operand=0)
   5 OPTIMIZED: _ITER_NEXT_RANGE (19, target=14, operand=0)
   6 OPTIMIZED: _CHECK_VALIDITY (0, target=16, operand=100dd2f78)
   7 OPTIMIZED: _STORE_FAST_2 (2, target=16, operand=0)
   8 OPTIMIZED: _CHECK_FUNCTION (3, target=17, operand=100ee7050)
   9 OPTIMIZED: _LOAD_CONST_INLINE_WITH_NULL (3, target=17, operand=100ed2210)
  10 OPTIMIZED: _LOAD_CONST_INLINE_BORROW (4, target=22, operand=100a11508)
  11 OPTIMIZED: _LOAD_FAST_2 (2, target=27, operand=0)
  12 OPTIMIZED: _SET_IP (0, target=28, operand=100dd2f90)
  13 OPTIMIZED: _CHECK_FUNCTION_EXACT_ARGS (2, target=28, operand=2c9)
  14 OPTIMIZED: _CHECK_STACK_SPACE (2, target=28, operand=0)
  15 OPTIMIZED: _INIT_CALL_PY_EXACT_ARGS_2 (2, target=28, operand=0)
  16 OPTIMIZED: _SAVE_RETURN_OFFSET (4, target=28, operand=0)
  17 OPTIMIZED: _PUSH_FRAME (2, target=28, operand=100ed2210)
  18 OPTIMIZED: _CHECK_VALIDITY (0, target=0, operand=100f410f8)
  19 OPTIMIZED: _RESUME_CHECK (0, target=0, operand=0)
  20 OPTIMIZED: _LOAD_FAST_1 (1, target=1, operand=0)
  21 OPTIMIZED: _SET_IP (0, target=2, operand=100f410fc)
  22 OPTIMIZED: _POP_FRAME (0, target=2, operand=100f47ad0)
  23 OPTIMIZED: _CHECK_VALIDITY (0, target=32, operand=100dd2f98)
  24 OPTIMIZED: _STORE_FAST_1 (1, target=32, operand=0)
  25 OPTIMIZED: _JUMP_TO_TOP (0, target=0, operand=0)
DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE]
DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE]
DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE]
DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE]
DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE]
DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE]
DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE]
DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE]
DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE]
DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE]
DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE]
DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE]
DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE]
DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE]
DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE]
DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE]
DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE]
DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE]
DEOPT: [UOp _CHECK_FUNCTION (3, target=17, operand=100ee7050) -> FOR_ITER_RANGE]
DEOPT: [UOp _GUARD_NOT_EXHAUSTED_RANGE (19, target=14, operand=0) -> FOR_ITER_RANGE]

That's the _CHECK_FUNCTION call at index 8 deoptimizing every time.

@brandtbucher
Copy link
Member

@markshannon, Is this correct? WhenI run a really simple test with debug output, I get a DEOPT message for _CHECK_FUNCTION for each iteration; before this PR I only got one DEOPT message at the end of the loop.

Yep, the bug is that this PR doesn't update the operand, just the opcode. So it's incorrectly comparing the function to the builtin or globals dicts.

@markshannon
Copy link
Member Author

#116460

adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 25, 2024
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
@markshannon markshannon deleted the globals-check-to-function-check branch August 6, 2024 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants