feat: no clone in JIT

IMPORTANT: should not drop or create values in JIT anymore
This commit is contained in:
2025-05-21 20:48:56 +08:00
parent 177acfabcf
commit 2a19ddb279
11 changed files with 105 additions and 82 deletions

View File

@@ -9,7 +9,7 @@ use inkwell::values::{BasicValueEnum, FunctionValue};
use crate::jit::JITValueData;
use crate::ty::internal::{Thunk, Value};
use crate::vm::{VmEnv, VM};
use crate::vm::{VM, VmEnv};
use super::{JITValue, ValueTag};
@@ -204,20 +204,17 @@ impl<'ctx> Helpers<'ctx> {
}
}
#[unsafe(no_mangle)]
extern "C" fn helper_debug(value: JITValue) {
dbg!(value.tag);
}
#[unsafe(no_mangle)]
extern "C" fn helper_capture_env(thunk: JITValue, env: *const VmEnv) {
let thunk: &Thunk = unsafe { std::mem::transmute(thunk.data.ptr.as_ref().unwrap()) };
let thunk = unsafe { (thunk.data.ptr as *const Thunk).as_ref().unwrap() };
let env = unsafe { Rc::from_raw(env) };
thunk.capture(env.clone());
std::mem::forget(env);
}
#[unsafe(no_mangle)]
extern "C" fn helper_neg(rhs: JITValue, _env: *const VmEnv) -> JITValue {
use ValueTag::*;
match rhs.tag {
@@ -237,7 +234,6 @@ extern "C" fn helper_neg(rhs: JITValue, _env: *const VmEnv) -> JITValue {
}
}
#[unsafe(no_mangle)]
extern "C" fn helper_not(rhs: JITValue, _env: *const VmEnv) -> JITValue {
use ValueTag::*;
match rhs.tag {
@@ -251,7 +247,6 @@ extern "C" fn helper_not(rhs: JITValue, _env: *const VmEnv) -> JITValue {
}
}
#[unsafe(no_mangle)]
extern "C" fn helper_add(lhs: JITValue, rhs: JITValue) -> JITValue {
use ValueTag::*;
match (lhs.tag, rhs.tag) {
@@ -269,7 +264,6 @@ extern "C" fn helper_add(lhs: JITValue, rhs: JITValue) -> JITValue {
}
}
#[unsafe(no_mangle)]
extern "C" fn helper_sub(lhs: JITValue, rhs: JITValue) -> JITValue {
use ValueTag::*;
match (lhs.tag, rhs.tag) {
@@ -287,7 +281,6 @@ extern "C" fn helper_sub(lhs: JITValue, rhs: JITValue) -> JITValue {
}
}
#[unsafe(no_mangle)]
extern "C" fn helper_eq(lhs: JITValue, rhs: JITValue) -> JITValue {
use ValueTag::*;
match (lhs.tag, rhs.tag) {
@@ -305,7 +298,6 @@ extern "C" fn helper_eq(lhs: JITValue, rhs: JITValue) -> JITValue {
}
}
#[unsafe(no_mangle)]
extern "C" fn helper_or(lhs: JITValue, rhs: JITValue) -> JITValue {
use ValueTag::*;
match (lhs.tag, rhs.tag) {
@@ -323,7 +315,6 @@ extern "C" fn helper_or(lhs: JITValue, rhs: JITValue) -> JITValue {
}
}
#[unsafe(no_mangle)]
extern "C" fn helper_call<'jit>(
func: JITValue,
args: *mut JITValue,
@@ -346,14 +337,12 @@ extern "C" fn helper_call<'jit>(
}
}
#[unsafe(no_mangle)]
extern "C" fn helper_lookup<'jit, 'vm>(sym: usize, env: *const VmEnv<'jit, 'vm>) -> JITValue {
let env = unsafe { env.as_ref() }.unwrap();
let val = env.lookup(&sym);
val.cloned().unwrap().into()
let val: JITValue = env.lookup(&sym).unwrap().into();
val
}
#[unsafe(no_mangle)]
extern "C" fn helper_force<'jit>(thunk: JITValue, vm: *const VM<'jit>) -> JITValue {
let mut val = Value::from(thunk);
val.force(unsafe { vm.as_ref() }.unwrap()).unwrap();

View File

@@ -12,7 +12,7 @@ use crate::error::*;
use crate::stack::Stack;
use crate::ty::common::Const;
use crate::ty::internal::{Thunk, Value};
use crate::vm::{VmEnv, VM};
use crate::vm::{VM, VmEnv};
mod helpers;
@@ -57,6 +57,12 @@ pub union JITValueData {
impl<'jit: 'vm, 'vm> From<JITValue> for Value<'jit, 'vm> {
fn from(value: JITValue) -> Self {
use ValueTag::*;
match value.tag {
List | AttrSet | String | Function | Thunk | Path => unsafe {
Rc::increment_strong_count(value.data.ptr);
},
_ => (),
}
match value.tag {
Int => Value::Const(Const::Int(unsafe { value.data.int })),
Null => Value::Const(Const::Null),
@@ -67,6 +73,30 @@ impl<'jit: 'vm, 'vm> From<JITValue> for Value<'jit, 'vm> {
}
}
impl From<&Value<'_, '_>> for JITValue {
fn from(value: &Value<'_, '_>) -> Self {
match value {
Value::Const(Const::Int(int)) => JITValue {
tag: ValueTag::Int,
data: JITValueData { int: *int },
},
Value::Func(func) => JITValue {
tag: ValueTag::Function,
data: JITValueData {
ptr: Rc::as_ptr(func) as *const _,
},
},
Value::Thunk(thunk) => JITValue {
tag: ValueTag::Thunk,
data: JITValueData {
ptr: Rc::as_ptr(thunk) as *const _,
},
},
_ => todo!(),
}
}
}
impl From<Value<'_, '_>> for JITValue {
fn from(value: Value<'_, '_>) -> Self {
match value {
@@ -144,14 +174,7 @@ impl<'vm, 'ctx: 'vm> JITContext<'ctx> {
let env = func_.get_nth_param(1).unwrap().into_pointer_value();
let entry = self.context.append_basic_block(func_, "entry");
self.builder.position_at_end(entry);
self.build_expr(
&mut iter,
vm,
env,
&mut stack,
func_,
func.opcodes.len(),
)?;
self.build_expr(&mut iter, vm, env, &mut stack, func_, func.opcodes.len())?;
assert_eq!(stack.len(), 1);
let value = stack.pop();