Skip to content

Support recursion in JIT-ed functions - #5473

Merged
youknowone merged 3 commits into
RustPython:mainfrom
theshubhamp:jit-recursion
Jan 13, 2025
Merged

youknowone merged 3 commits into
RustPython:mainfrom
theshubhamp:jit-recursion

Conversation

@theshubhamp

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

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

@youknowone

Copy link
Copy Markdown
Member

@coolreader18 is working for it on #5472

@youknowone

Copy link
Copy Markdown
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
Comment thread vm/src/builtins/function/jitfunc.rs Resolved

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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
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