fix: release eq
This commit is contained in:
@@ -2,13 +2,12 @@ use std::cell::OnceCell;
|
||||
use core::mem::MaybeUninit;
|
||||
use std::rc::Rc;
|
||||
|
||||
use hashbrown::HashSet;
|
||||
use inkwell::context::Context;
|
||||
use hashbrown::{HashMap, HashSet};
|
||||
use priority_queue::PriorityQueue;
|
||||
|
||||
use crate::env::Env;
|
||||
use crate::error::Result;
|
||||
use crate::eval::jit::{JITContext, JITFunc, JITValue};
|
||||
use crate::eval::jit::{JITCompiler, JITFunc};
|
||||
use crate::eval::Evaluate;
|
||||
use crate::ir::{Dep, Downgraded, Ir, SccNode};
|
||||
use crate::ty::internal as i;
|
||||
@@ -20,34 +19,32 @@ mod test;
|
||||
type ThunkIdx = usize;
|
||||
type EnvIdx = usize;
|
||||
|
||||
pub struct Engine<'exec> {
|
||||
pub struct Engine {
|
||||
pub thunks: Box<[Ir]>,
|
||||
pub func_offset: usize,
|
||||
pub func_deps: Vec<HashSet<Dep>>,
|
||||
jit: &'exec JITContext,
|
||||
compiled: Box<[OnceCell<JITFunc<'exec>>]>,
|
||||
pub funcs: Box<[Ir]>,
|
||||
pub func_deps: Vec<HashMap<Dep, usize>>,
|
||||
jit: JITCompiler,
|
||||
compiled: Box<[OnceCell<JITFunc>]>,
|
||||
tasks: PriorityQueue<CompileTask, usize>,
|
||||
}
|
||||
|
||||
pub fn eval(downgraded: Downgraded) -> Result<Value> {
|
||||
let ctx = Context::create();
|
||||
let jit = JITContext::new(&ctx);
|
||||
let mut engine = Engine::new(
|
||||
downgraded.thunks,
|
||||
downgraded.func_offset,
|
||||
downgraded.funcs,
|
||||
downgraded.func_deps,
|
||||
&jit
|
||||
JITCompiler::new()
|
||||
);
|
||||
engine.eval(downgraded.graph)
|
||||
}
|
||||
|
||||
impl<'ctx, 'exec> Engine<'ctx, 'exec> {
|
||||
pub fn new(thunks: Box<[Ir]>, func_offset: usize, func_deps: Vec<HashSet<Dep>>, jit: &'exec JITContext<'ctx>) -> Self {
|
||||
impl Engine {
|
||||
pub fn new(thunks: Box<[Ir]>, funcs: Box<[Ir]>, func_deps: Vec<HashMap<Dep, usize>>, jit: JITCompiler) -> Self {
|
||||
Self {
|
||||
compiled: (0..thunks.len()).map(|_| OnceCell::new()).collect(),
|
||||
compiled: (0..thunks.len() + funcs.len()).map(|_| OnceCell::new()).collect(),
|
||||
tasks: PriorityQueue::new(),
|
||||
thunks,
|
||||
func_offset,
|
||||
funcs,
|
||||
func_deps,
|
||||
jit,
|
||||
}
|
||||
@@ -80,19 +77,30 @@ impl<'ctx, 'exec> Engine<'ctx, 'exec> {
|
||||
}
|
||||
|
||||
pub fn eval_thunk(&mut self, idx: usize, env: &mut Env) -> Result<i::Value> {
|
||||
let engine = self as *const Engine;
|
||||
let func = self.compiled[idx].get_or_init(|| self.jit.compile(&self.thunks[idx], idx));
|
||||
let mut ret: MaybeUninit<JITValue> = MaybeUninit::uninit();
|
||||
let mut ret: MaybeUninit<i::Value> = MaybeUninit::uninit();
|
||||
unsafe {
|
||||
func(self as *const Engine, env as *const Env, core::mem::transmute::<*mut MaybeUninit<JITValue>, *mut JITValue>(&mut ret as *mut _));
|
||||
Ok(ret.assume_init().into())
|
||||
func(engine, env, ret.as_mut_ptr());
|
||||
Ok(ret.assume_init())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn call_func(&mut self, idx: usize, env: &mut Env) -> Result<i::Value> {
|
||||
let engine = self as *const Engine;
|
||||
let func = self.compiled[idx + self.thunks.len()].get_or_init(|| self.jit.compile(&self.funcs[idx], idx));
|
||||
let mut ret: MaybeUninit<i::Value> = MaybeUninit::uninit();
|
||||
unsafe {
|
||||
func(engine, env, ret.as_mut_ptr());
|
||||
Ok(ret.assume_init())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eval_func_deps(&mut self, idx: usize, env: &mut Env) -> Result<()> {
|
||||
for dep in
|
||||
unsafe { &*(&self.func_deps[idx - self.func_offset] as *const HashSet<Dep>) }.iter()
|
||||
for (&dep, _) in
|
||||
unsafe { &*(&self.func_deps[idx] as *const HashMap<Dep, usize>) }.iter()
|
||||
{
|
||||
match *dep {
|
||||
match dep {
|
||||
Dep::Arg(idx) => {
|
||||
if let i::Value::Thunk(idx) = env.lookup_arg(idx) {
|
||||
env.insert_cache_lazy(idx, |env| {
|
||||
|
||||
Reference in New Issue
Block a user