feat: JIT (WIP)
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
use std::cell::OnceCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use hashbrown::HashSet;
|
||||
use inkwell::context::Context;
|
||||
use priority_queue::PriorityQueue;
|
||||
|
||||
use crate::env::Env;
|
||||
use crate::error::Result;
|
||||
use crate::eval::jit::{JITContext, JITFunc};
|
||||
use crate::eval::Evaluate;
|
||||
use crate::ir::{Dep, Downgraded, Ir, SccNode};
|
||||
use crate::ty::internal as i;
|
||||
@@ -16,29 +19,36 @@ mod test;
|
||||
type ThunkIdx = usize;
|
||||
type EnvIdx = usize;
|
||||
|
||||
pub struct Engine {
|
||||
pub struct Engine<'ctx: 'exec, 'exec> {
|
||||
pub thunks: Box<[Ir]>,
|
||||
pub func_offset: usize,
|
||||
pub func_deps: Vec<HashSet<Dep>>,
|
||||
jit: &'exec JITContext<'ctx>,
|
||||
compiled: Box<[OnceCell<JITFunc<'ctx, 'exec>>]>,
|
||||
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.func_deps,
|
||||
&jit
|
||||
);
|
||||
engine.eval(downgraded.graph)
|
||||
}
|
||||
|
||||
impl Engine {
|
||||
pub fn new(thunks: Box<[Ir]>, func_offset: usize, func_deps: Vec<HashSet<Dep>>) -> Self {
|
||||
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 {
|
||||
Self {
|
||||
compiled: (0..thunks.len()).map(|_| OnceCell::new()).collect(),
|
||||
tasks: PriorityQueue::new(),
|
||||
thunks,
|
||||
func_offset,
|
||||
func_deps,
|
||||
jit,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,8 +79,8 @@ impl Engine {
|
||||
}
|
||||
|
||||
pub fn eval_thunk(&mut self, idx: usize, env: &mut Env) -> Result<i::Value> {
|
||||
let self_mut = unsafe { &mut *(self as *mut Self) };
|
||||
self.thunks[idx].eval(self_mut, env)
|
||||
let func = self.compiled[idx].get_or_init(|| self.jit.compile(&self.thunks[idx]));
|
||||
Ok(unsafe { func(self as *const Engine, env as *const Env).into() })
|
||||
}
|
||||
|
||||
pub fn eval_func_deps(&mut self, idx: usize, env: &mut Env) -> Result<()> {
|
||||
|
||||
Reference in New Issue
Block a user