use super::{CompiledChain, SlangCompiler};
use crate::runtime::dna::{fusion::census::JitOutcome, ir::elementwise::Program};
const MAX_QUEUED_COMPILATIONS: usize = 16;
pub(in crate::runtime) struct CompileRequest {
pub(in crate::runtime) program: Program,
pub(in crate::runtime) bounded: bool,
}
pub(in crate::runtime) struct CompileResult {
pub(in crate::runtime) key: Vec<u8>,
pub(in crate::runtime) compiler_version: String,
pub(in crate::runtime) result: Result<CompiledChain, JitOutcome>,
}
pub(in crate::runtime) struct CompileWorker {
pub(in crate::runtime) requests: std::sync::mpsc::SyncSender<CompileRequest>,
pub(in crate::runtime) results: std::sync::mpsc::Receiver<CompileResult>,
_thread: std::thread::JoinHandle<()>,
}
impl CompileWorker {
pub(in crate::runtime) fn spawn(compiler: SlangCompiler) -> std::io::Result<Self> {
let (requests, inbox) = std::sync::mpsc::sync_channel(MAX_QUEUED_COMPILATIONS);
let (results, outbox) = std::sync::mpsc::channel();
let thread = std::thread::Builder::new()
.name("oa-jit-compile".into())
.spawn(move || compile_worker_main(compiler, inbox, results))?;
Ok(Self {
requests,
results: outbox,
_thread: thread,
})
}
}
fn compile_worker_main(
compiler: SlangCompiler,
inbox: std::sync::mpsc::Receiver<CompileRequest>,
outbox: std::sync::mpsc::Sender<CompileResult>,
) {
while let Ok(request) = inbox.recv() {
let result = CompileResult {
key: request.program.artifact_key(),
compiler_version: compiler.version.clone(),
result: compiler
.compile_program(&request.program, request.bounded)
.map_err(|error| {
crate::log_warn!(
crate::LogComponent::RUNTIME,
"JIT specialization admission rejected: {error:?}"
);
JitOutcome::CompileRejected
}),
};
if outbox.send(result).is_err() {
return;
}
}
}