Skip to content

Support recursion in JIT-ed functions #5473

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 3 commits into from
Jan 13, 2025

Conversation

theshubhamp
Copy link
Contributor

Support recursion in JIT-ed functions

JIT implementation today is experimental & doesn't support calling out (globals, other functions etc.). This PR adds support for recursion.

Summary of Changes:

  1. Added a new JitType FuncRef that stores an id-reference to functions imported for use
  2. For recursion, a self import is done & passed down to Bytecode Compiler
  3. Recursion only works when return type is annotated

On a "non-scientific" benchmark, JIT-ed function appears to be faster than rustpython/intereprted (expected) and cpython 3.13.1
image

Source of fib.py:

import time
import sys

target = int(sys.argv[1])

def fib(n: int) -> int:
  if n == 0 or n == 1:
    return 1
  fib = 0
  return fib(n-1) + fib(n-2)

start_time = time.perf_counter_ns()
val = fib(target)
time_taken = (time.perf_counter_ns() - start_time)/1000000
print("--- fib(%s)=%s --- no jit --- %s ms ---" % (target, val, time_taken))

try:
    fib.__jit__()

    start_time = time.perf_counter_ns()
    fib(target)
    time_taken = (time.perf_counter_ns() - start_time)/1000000
    print("--- fib(%s)=%s --- jit --- %s ms ---" % (target, val, time_taken))
except:
    print("--- fib(%s)=** --- jit --- not supported ---" % (target))
    pass

@theshubhamp
Copy link
Contributor Author

clippy errors are unrelated & mostly because a new rust stable shipped yesterday: https://releases.rs/docs/1.84.0/

@youknowone
Copy link
Member

@coolreader18 is working for it on #5472

@youknowone
Copy link
Member

Could you rebase it to make clippy tests pass?

JIT implementation today is experimental & doesn't support calling out (globals, other functions etc.)

This commit adds support for recursion
Co-authored-by: Jeong, YunWon <69878+youknowone@users.noreply.github.com>
Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much! The benchmark is impressive

@theshubhamp
Copy link
Contributor Author

Thank you so much! The benchmark is impressive

Welcome! Yes, performed much better than I anticipated

@youknowone youknowone merged commit 53db70e into RustPython:main Jan 13, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Trying to jit fibonacci function results in JitError: function can't be jitted
2 participants