feat: gc-arena

finally...
This commit is contained in:
2025-05-28 21:52:13 +08:00
parent c3ace28af1
commit 99dce2e778
7 changed files with 98 additions and 99 deletions

View File

@@ -12,7 +12,7 @@ use crate::jit::JITValueData;
use crate::ty::internal::{Thunk, Value}; use crate::ty::internal::{Thunk, Value};
use crate::vm::VM; use crate::vm::VM;
use super::{JITValue, ValueTag}; use super::{JITContext, JITValue, ValueTag};
pub struct Helpers<'ctx> { pub struct Helpers<'ctx> {
pub int_type: IntType<'ctx>, pub int_type: IntType<'ctx>,
@@ -103,7 +103,7 @@ impl<'ctx> Helpers<'ctx> {
let call = module.add_function( let call = module.add_function(
"call", "call",
value_type.fn_type( value_type.fn_type(
&[value_type.into(), value_type.into(), ptr_type.into()], &[value_type.into(), value_type.into(), ptr_type.into(), ptr_type.into()],
false, false,
), ),
None, None,
@@ -115,7 +115,7 @@ impl<'ctx> Helpers<'ctx> {
); );
let force = module.add_function( let force = module.add_function(
"force", "force",
value_type.fn_type(&[value_type.into(), ptr_type.into()], false), value_type.fn_type(&[value_type.into(), ptr_type.into(), ptr_type.into(), ptr_type.into()], false),
None, None,
); );
@@ -310,30 +310,26 @@ extern "C" fn helper_or(lhs: JITValue, rhs: JITValue) -> JITValue {
}, },
}, },
_ => todo!( _ => todo!(
"Substruction not implemented for {:?} and {:?}", "Substraction not implemented for {:?} and {:?}",
lhs.tag, lhs.tag,
rhs.tag rhs.tag
), ),
} }
} }
extern "C" fn helper_call( extern "C" fn helper_call<'gc>(
func: JITValue, func: JITValue,
arg: JITValue, arg: JITValue,
vm: *const VM, vm: *const VM<'gc>,
mc: *const Mutation, mc: *const Mutation<'gc>,
) -> JITValue { ) -> JITValue {
use ValueTag::*; let vm = unsafe { vm.as_ref() }.unwrap();
let mc = unsafe { mc.as_ref() }.unwrap();
let arg = Value::from(arg);
match func.tag { match func.tag {
Function => { ValueTag::Function => {
let mut func: Value = func.into(); let func = Value::from(func).unwrap_func();
func.call( func.call_compile(arg, vm, mc).unwrap().into()
arg.into(),
unsafe { vm.as_ref() }.unwrap(),
unsafe { mc.as_ref() }.unwrap(),
)
.unwrap();
func.into()
} }
_ => todo!(), _ => todo!(),
} }
@@ -345,15 +341,22 @@ extern "C" fn helper_lookup(sym: usize, env: *const VmEnv) -> JITValue {
val val
} }
extern "C" fn helper_force(thunk: JITValue, vm: *const VM, mc: *const Mutation) -> JITValue { extern "C" fn helper_force<'gc>(thunk: JITValue, vm: *const VM<'gc>, mc: *const Mutation<'gc>, jit: *const JITContext<'gc>) -> JITValue {
todo!() if !matches!(thunk.tag, ValueTag::Thunk) {
/* let mut val = Value::from(thunk); return thunk;
val.force( }
unsafe { vm.as_ref() }.unwrap(),
unsafe { mc.as_ref() }.unwrap(), let vm = unsafe { vm.as_ref() }.unwrap();
) let mc = unsafe { mc.as_ref() }.unwrap();
.unwrap(); let thunk = Value::from(thunk).unwrap_thunk();
val.into() */ if let Some(val) = thunk.get_value() {
return val.into()
}
let (opcodes, env) = thunk.suspend(mc).unwrap();
let func = unsafe { jit.as_ref() }.unwrap().compile_seq(opcodes.iter().copied(), vm).unwrap();
let val = unsafe { func.call(env.as_ref() as *const _, mc as *const _) };
thunk.insert_value(val.into(), mc);
val
} }
extern "C" fn helper_new_thunk(opcodes: *const OpCodes, mc: *const Mutation) -> JITValue { extern "C" fn helper_new_thunk(opcodes: *const OpCodes, mc: *const Mutation) -> JITValue {

View File

@@ -8,7 +8,7 @@ use inkwell::execution_engine::{ExecutionEngine, JitFunction};
use inkwell::module::Module; use inkwell::module::Module;
use inkwell::values::{BasicValueEnum, FunctionValue, PointerValue}; use inkwell::values::{BasicValueEnum, FunctionValue, PointerValue};
use crate::bytecode::{Func, OpCode, UnOp}; use crate::bytecode::{OpCode, UnOp};
use crate::env::VmEnv; use crate::env::VmEnv;
use crate::error::*; use crate::error::*;
use crate::stack::Stack; use crate::stack::Stack;
@@ -28,6 +28,7 @@ const STACK_SIZE: usize = 8 * 1024 / size_of::<Value>();
#[repr(u64)] #[repr(u64)]
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum ValueTag { pub enum ValueTag {
Null,
Int, Int,
Float, Float,
String, String,
@@ -37,7 +38,6 @@ pub enum ValueTag {
Function, Function,
Thunk, Thunk,
Path, Path,
Null,
} }
#[repr(C)] #[repr(C)]
@@ -164,6 +164,7 @@ unsafe impl<'gc> Collect<'gc> for JITContext<'gc> {
fn trace<T: gc_arena::collect::Trace<'gc>>(&self, _: &mut T) {} fn trace<T: gc_arena::collect::Trace<'gc>>(&self, _: &mut T) {}
const NEEDS_TRACE: bool = false; const NEEDS_TRACE: bool = false;
} }
impl<'gc> JITContext<'gc> { impl<'gc> JITContext<'gc> {
pub fn new(context: &'gc Context) -> Self { pub fn new(context: &'gc Context) -> Self {
// force linker to link JIT engine // force linker to link JIT engine
@@ -196,9 +197,12 @@ impl<'gc> JITContext<'gc> {
.unwrap() .unwrap()
} }
pub fn compile_function(&self, func: &'gc Func, vm: &'gc VM<'gc>) -> Result<JITFunc<'gc>> { pub fn compile_seq(
&self,
opcodes: impl Iterator<Item = OpCode> + ExactSizeIterator + DoubleEndedIterator,
vm: &'gc VM<'gc>,
) -> Result<JITFunc<'gc>> {
let mut stack = Stack::<_, STACK_SIZE>::new(); let mut stack = Stack::<_, STACK_SIZE>::new();
let mut iter = func.opcodes.iter().copied();
let func_ = self let func_ = self
.module .module
.add_function("nixjit_function", self.helpers.func_type, None); .add_function("nixjit_function", self.helpers.func_type, None);
@@ -206,15 +210,8 @@ impl<'gc> JITContext<'gc> {
let mc = func_.get_nth_param(1).unwrap().into_pointer_value(); let mc = func_.get_nth_param(1).unwrap().into_pointer_value();
let entry = self.context.append_basic_block(func_, "entry"); let entry = self.context.append_basic_block(func_, "entry");
self.builder.position_at_end(entry); self.builder.position_at_end(entry);
self.build_expr( let len = opcodes.len();
&mut iter, self.build_expr(&mut opcodes.rev(), vm, env, mc, &mut stack, func_, len)?;
vm,
env,
mc,
&mut stack,
func_,
func.opcodes.len(),
)?;
assert_eq!(stack.len(), 1); assert_eq!(stack.len(), 1);
let value = stack.pop(); let value = stack.pop();
@@ -336,6 +333,25 @@ impl<'gc> JITContext<'gc> {
.unwrap_left() .unwrap_left()
.into(), .into(),
)?, )?,
OpCode::ForceValue => {
let thunk = stack.pop();
let _ = stack.push(
self.builder
.build_direct_call(
self.helpers.force,
&[
thunk.into(),
self.new_ptr(vm as *const _).into(),
mc.into(),
self.new_ptr(self as *const _).into(),
],
"call_force",
)?
.try_as_basic_value()
.left()
.unwrap(),
);
}
OpCode::CaptureEnv => { OpCode::CaptureEnv => {
let thunk = *stack.tos(); let thunk = *stack.tos();
self.builder.build_direct_call( self.builder.build_direct_call(
@@ -442,21 +458,12 @@ impl<'gc> JITContext<'gc> {
)?, )?,
OpCode::Call => { OpCode::Call => {
let arg = stack.pop(); let arg = stack.pop();
let func = self let func = stack.pop();
.builder
.build_direct_call(
self.helpers.force,
&[stack.pop().into(), self.new_ptr(vm).into()],
"force",
)?
.try_as_basic_value()
.left()
.unwrap();
let ret = self let ret = self
.builder .builder
.build_direct_call( .build_direct_call(
self.helpers.call, self.helpers.call,
&[func.into(), arg.into(), self.new_ptr(vm).into()], &[func.into(), arg.into(), self.new_ptr(vm).into(), mc.into()],
"call", "call",
)? )?
.try_as_basic_value() .try_as_basic_value()

View File

@@ -7,7 +7,6 @@ use hashbrown::{HashMap, HashSet};
use itertools::Itertools; use itertools::Itertools;
use crate::env::VmEnv; use crate::env::VmEnv;
use crate::error::Result;
use crate::vm::VM; use crate::vm::VM;
use super::super::public as p; use super::super::public as p;
@@ -85,16 +84,6 @@ impl<'jit: 'vm, 'vm, 'gc> AttrSet<'gc> {
Self { data } Self { data }
} }
pub fn force_deep(&mut self, vm: &VM, mc: &Mutation<'gc>) -> Result<()> {
todo!()
/* let mut map: Vec<_> = self.data.iter().map(|(k, v)| (*k, v.clone())).collect();
for (_, v) in map.iter_mut() {
v.force_deep(vm, mc)?;
}
self.data = map.into_iter().collect();
Ok(()) */
}
pub fn eq_impl(&self, other: &AttrSet<'gc>) -> bool { pub fn eq_impl(&self, other: &AttrSet<'gc>) -> bool {
self.data.iter().len() == other.data.iter().len() self.data.iter().len() == other.data.iter().len()
&& std::iter::zip( && std::iter::zip(

View File

@@ -1,7 +1,7 @@
use std::cell::Cell; use std::cell::Cell;
use gc_arena::lock::{GcRefLock, RefLock}; use gc_arena::lock::{GcRefLock, RefLock};
use gc_arena::{Arena, Collect, Gc, Mutation, Rootable}; use gc_arena::{Collect, Gc, Mutation};
use hashbrown::HashMap; use hashbrown::HashMap;
use itertools::Itertools; use itertools::Itertools;
@@ -11,7 +11,7 @@ use crate::error::Result;
use crate::ir; use crate::ir;
use crate::jit::JITFunc; use crate::jit::JITFunc;
use crate::ty::internal::{Thunk, Value}; use crate::ty::internal::{Thunk, Value};
use crate::vm::{GcRoot, VM, eval}; use crate::vm::VM;
#[derive(Debug, Clone, Collect)] #[derive(Debug, Clone, Collect)]
#[collect(no_drop)] #[collect(no_drop)]
@@ -44,7 +44,6 @@ impl From<ir::Param> for Param {
} }
} }
#[derive(Clone)]
pub struct Func<'gc> { pub struct Func<'gc> {
pub func: &'gc BFunc, pub func: &'gc BFunc,
pub env: Gc<'gc, VmEnv<'gc>>, pub env: Gc<'gc, VmEnv<'gc>>,
@@ -70,15 +69,13 @@ impl<'gc> Func<'gc> {
} }
} }
pub fn call( pub fn call_compile(
&self, &self,
arg: Value<'gc>, arg: Value<'gc>,
vm: &'gc VM<'gc>, vm: &'gc VM<'gc>,
mc: &Mutation<'gc>, mc: &Mutation<'gc>,
arena: &Arena<impl for<'a> Rootable<'a, Root = GcRoot<'a>>>,
) -> Result<Value<'gc>> { ) -> Result<Value<'gc>> {
todo!() use Param::*;
/* use Param::*;
let mut env = self.env; let mut env = self.env;
env = match self.func.param.clone() { env = match self.func.param.clone() {
@@ -117,17 +114,13 @@ impl<'gc> Func<'gc> {
} }
}; };
let count = self.count.get(); let compiled = self
self.count.replace(count + 1); .compiled
if count >= 1 { .borrow_mut(mc)
let compiled = &mut *self.compiled.borrow_mut(mc); .get_or_insert_with(|| vm.compile_func(self.func))
let compiled = compiled.get_or_insert_with(|| vm.compile_func(self.func)); .clone();
let ret = unsafe { compiled.call(env.as_ref() as *const VmEnv, mc as *const _) }; let ret = unsafe { compiled.call(env.as_ref() as *const VmEnv, mc as *const _) };
return Ok(ret.into()); Ok(ret.into())
}
eval(self.func.opcodes.iter().copied(), arena, |val, _| {
Ok(val)
}) */
} }
} }

View File

@@ -1,12 +1,11 @@
use hashbrown::HashSet; use hashbrown::HashSet;
use derive_more::Constructor; use derive_more::Constructor;
use gc_arena::{Arena, Collect, Rootable}; use gc_arena::Collect;
use rpds::Vector; use rpds::Vector;
use crate::error::Result;
use crate::ty::public as p; use crate::ty::public as p;
use crate::vm::{GcRoot, VM}; use crate::vm::VM;
use super::Value; use super::Value;

View File

@@ -604,13 +604,13 @@ impl<'gc> Thunk<'gc> {
} }
} }
pub fn suspend(&self, mc: &Mutation<'gc>) -> (&'gc OpCodes, Gc<'gc, VmEnv<'gc>>) { pub fn suspend(&self, mc: &Mutation<'gc>) -> Result<(&'gc OpCodes, Gc<'gc, VmEnv<'gc>>)> {
let _Thunk::Code(opcodes, env) = let _Thunk::Code(opcodes, env) =
std::mem::replace(&mut *self.thunk.borrow_mut(mc), _Thunk::Suspended) std::mem::replace(&mut *self.thunk.borrow_mut(mc), _Thunk::Suspended)
else { else {
unreachable!() return Err(Error::EvalError("infinite recursion occured".into()))
}; };
(opcodes, env.unwrap()) Ok((opcodes, env.unwrap()))
} }
pub fn insert_value(&self, value: Value<'gc>, mc: &Mutation<'gc>) { pub fn insert_value(&self, value: Value<'gc>, mc: &Mutation<'gc>) {

View File

@@ -31,8 +31,10 @@ struct ContextWrapper(Context);
pub fn run(mut prog: Program) -> Result<p::Value> { pub fn run(mut prog: Program) -> Result<p::Value> {
let mut arena: Arena<Rootable![GcRoot<'_>]> = Arena::new(|mc| { let mut arena: Arena<Rootable![GcRoot<'_>]> = Arena::new(|mc| {
let jit = Gc::new(mc, ContextWrapper(Context::create())); let jit = Gc::new(mc, ContextWrapper(Context::create()));
let thunks = std::mem::take(&mut prog.thunks); let mut thunks = std::mem::take(&mut prog.thunks);
let funcs = std::mem::take(&mut prog.funcs); thunks.iter_mut().for_each(|code| code.reverse());
let mut funcs = std::mem::take(&mut prog.funcs);
funcs.iter_mut().for_each(|F { opcodes, .. }| opcodes.reverse());
let symbols = std::mem::take(&mut prog.symbols); let symbols = std::mem::take(&mut prog.symbols);
let symmap = std::mem::take(&mut prog.symmap); let symmap = std::mem::take(&mut prog.symmap);
let consts = std::mem::take(&mut prog.consts); let consts = std::mem::take(&mut prog.consts);
@@ -52,6 +54,7 @@ pub fn run(mut prog: Program) -> Result<p::Value> {
envs: vec![env(&vm, mc)], envs: vec![env(&vm, mc)],
} }
}); });
prog.top_level.reverse();
eval(prog.top_level, &mut arena, |val, root, _| { eval(prog.top_level, &mut arena, |val, root, _| {
Ok(val.to_public(&root.vm, &mut HashSet::new())) Ok(val.to_public(&root.vm, &mut HashSet::new()))
}) })
@@ -62,7 +65,6 @@ pub fn eval<T, F: for<'gc> FnOnce(Value<'gc>, &mut GcRoot<'gc>, &Mutation<'gc>)
f: F, f: F,
) -> Result<T> { ) -> Result<T> {
let mut opcodes = opcodes.into_vec(); let mut opcodes = opcodes.into_vec();
opcodes.reverse();
while let Some(opcode) = opcodes.pop() { while let Some(opcode) = opcodes.pop() {
arena.mutate_root(|mc, root| { arena.mutate_root(|mc, root| {
let consq = single_op( let consq = single_op(
@@ -77,9 +79,7 @@ pub fn eval<T, F: for<'gc> FnOnce(Value<'gc>, &mut GcRoot<'gc>, &Mutation<'gc>)
Consq::Jmp(step) => opcodes.resize_with(opcodes.len() - step, || unreachable!()), Consq::Jmp(step) => opcodes.resize_with(opcodes.len() - step, || unreachable!()),
Consq::Force => { Consq::Force => {
let thunk = root.stack.tos().as_ref().unwrap_thunk(); let thunk = root.stack.tos().as_ref().unwrap_thunk();
let (code, env) = thunk.suspend(mc); let (code, env) = thunk.suspend(mc)?;
let mut code = code.to_vec();
code.reverse();
opcodes.push(OpCode::InsertValue); opcodes.push(OpCode::InsertValue);
opcodes.push(OpCode::PopEnv); opcodes.push(OpCode::PopEnv);
opcodes.extend(code); opcodes.extend(code);
@@ -90,8 +90,8 @@ pub fn eval<T, F: for<'gc> FnOnce(Value<'gc>, &mut GcRoot<'gc>, &Mutation<'gc>)
use Param::*; use Param::*;
let arg = root.stack.pop(); let arg = root.stack.pop();
let func = root.stack.pop().unwrap_func(); let func = root.stack.pop().unwrap_func();
let mut env = func.env; let env = func.env;
env = match func.func.param.clone() { let env = match func.func.param.clone() {
Ident(ident) => env.enter_arg(ident, arg, mc), Ident(ident) => env.enter_arg(ident, arg, mc),
Formals { Formals {
formals, formals,
@@ -130,16 +130,24 @@ pub fn eval<T, F: for<'gc> FnOnce(Value<'gc>, &mut GcRoot<'gc>, &Mutation<'gc>)
env.enter_let(Gc::new(mc, new.into()), mc) env.enter_let(Gc::new(mc, new.into()), mc)
} }
}; };
root.envs.push(env); let count = func.count.get();
let mut code = func.func.opcodes.to_vec(); func.count.set(count + 1);
code.reverse(); if count >= 1 {
opcodes.push(OpCode::PopEnv); let compiled = func.compiled.borrow_mut(mc).get_or_insert_with(|| root.vm.compile_func(func.func)).clone();
opcodes.extend(code); let ret = unsafe { compiled.call(env.as_ref() as *const VmEnv, mc as *const _) };
root.stack.push(ret.into())?;
} else {
root.envs.push(env);
opcodes.push(OpCode::PopEnv);
opcodes.extend(&func.func.opcodes);
}
} }
} }
Result::Ok(()) Result::Ok(())
})?; })?;
arena.finish_cycle();
} }
arena.collect_debt();
arena.mutate_root(|mc, root| { arena.mutate_root(|mc, root| {
assert_eq!(root.stack.len(), 1); assert_eq!(root.stack.len(), 1);
let ret = root.stack.pop(); let ret = root.stack.pop();
@@ -407,6 +415,6 @@ impl<'gc> VM<'gc> {
} }
pub fn compile_func(&'gc self, func: &'gc F) -> JITFunc<'gc> { pub fn compile_func(&'gc self, func: &'gc F) -> JITFunc<'gc> {
self.jit.compile_function(func, self).unwrap() self.jit.compile_seq(func.opcodes.iter().copied(), self).unwrap()
} }
} }