diff --git a/Cargo.toml b/Cargo.toml index 4942fd37d..2eaf31095 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ path = "./benchmarks/bench.rs" default = ["threading", "pylib"] flame-it = ["rustpython-vm/flame-it", "flame", "flamescope"] freeze-stdlib = ["rustpython-vm/freeze-stdlib"] +jit = ["rustpython-vm/jit"] threading = ["rustpython-vm/threading"] ssl = ["rustpython-vm/ssl"] diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 451a79df0..6f2d27f2e 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -13,6 +13,7 @@ default = ["compile-parse", "threading"] vm-tracing-logging = [] flame-it = ["flame", "flamer"] freeze-stdlib = ["rustpython-pylib"] +jit = ["rustpython-jit"] threading = ["rustpython-common/threading"] compile-parse = ["rustpython-parser", "rustpython-compiler"] @@ -43,7 +44,7 @@ rustpython-derive = { path = "../derive", version = "0.1.2" } rustpython-parser = { path = "../parser", optional = true, version = "0.1.2" } rustpython-compiler = { path = "../compiler", optional = true, version = "0.1.2" } rustpython-bytecode = { path = "../bytecode", version = "0.1.2" } -rustpython-jit = { path = "../jit", version = "0.1.2" } +rustpython-jit = { path = "../jit", optional = true, version = "0.1.2" } rustpython-pylib = { path = "pylib-crate", optional = true, version = "0.1.0" } serde = { version = "1.0.66", features = ["derive"] } byteorder = "1.2.6" diff --git a/vm/src/obj/objfunction.rs b/vm/src/obj/objfunction.rs index 08e24c9b7..e8dc379fd 100644 --- a/vm/src/obj/objfunction.rs +++ b/vm/src/obj/objfunction.rs @@ -9,15 +9,19 @@ use crate::function::{OptionalArg, PyFuncArgs}; use crate::obj::objasyncgenerator::PyAsyncGen; use crate::obj::objcoroutine::PyCoroutine; use crate::obj::objgenerator::PyGenerator; +#[cfg(feature = "jit")] +use crate::pyobject::IntoPyObject; use crate::pyobject::{ - BorrowValue, IdProtocol, IntoPyObject, ItemProtocol, PyClassImpl, PyContext, PyObjectRef, + BorrowValue, IdProtocol, ItemProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TypeProtocol, }; use crate::scope::Scope; use crate::slots::{SlotCall, SlotDescriptor}; use crate::VirtualMachine; use itertools::Itertools; +#[cfg(feature = "jit")] use rustpython_common::cell::PyMutex; +#[cfg(feature = "jit")] use rustpython_jit::CompiledCode; pub type PyFunctionRef = PyRef; @@ -26,6 +30,7 @@ pub type PyFunctionRef = PyRef; #[derive(Debug)] pub struct PyFunction { code: PyCodeRef, + #[cfg(feature = "jit")] jitted_code: PyMutex>, scope: Scope, defaults: Option, @@ -57,6 +62,7 @@ impl PyFunction { ) -> Self { PyFunction { code, + #[cfg(feature = "jit")] jitted_code: PyMutex::new(None), scope, defaults, @@ -227,6 +233,7 @@ impl PyFunction { scope: &Scope, vm: &VirtualMachine, ) -> PyResult { + #[cfg(feature = "jit")] if let Some(jitted_code) = &*self.jitted_code.lock() { return Ok(jitted_code.invoke().into_pyobject(vm)); } @@ -294,15 +301,24 @@ impl PyFunction { self.scope.globals.clone() } + #[allow(unused_variables)] #[pymethod(magic)] fn jit(&self, vm: &VirtualMachine) -> PyResult<()> { - let mut guard = self.jitted_code.lock(); - match rustpython_jit::compile(&self.code.code) { - Ok(code) => { - *guard = Some(code); - Ok(()) + // TODO move cfg onto function when https://github.com/RustPython/RustPython/pull/2120 lands + #[cfg(feature = "jit")] + { + let mut guard = self.jitted_code.lock(); + match rustpython_jit::compile(&self.code.code) { + Ok(code) => { + *guard = Some(code); + Ok(()) + } + Err(err) => Err(vm.new_runtime_error(err.to_string())), } - Err(err) => Err(vm.new_runtime_error(err.to_string())), + } + #[cfg(not(feature = "jit"))] + { + Ok(()) } } }