use super::{pipeline::ComputePipeline, vkcache::VkCacheScope};
use crate::runtime::{
Device,
device::{
timestamp::RouteTimings,
vkcache::jit::{self, RouteObservationRecord},
},
dna::{
compiler::{
JitArtifacts, SlangCompiler,
worker::{CompileRequest, CompileWorker},
},
fusion::census::{FusionCensus, JitOutcome},
ir::elementwise::Program,
tuning::{JitRouteOverride, RouteObservation, jit_route_override, select_route},
},
};
use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque};
use std::sync::Arc;
use std::time::Instant;
const MAX_OBSERVED_SIGNATURES: usize = 256;
const MAX_PENDING_TIMINGS: usize = 64;
const MAX_PIPELINE_ENTRIES: usize = 64;
const OBSERVATION_VERSION: u32 = 2;
pub(super) struct JitFusion {
restored: bool,
compiler: Option<crate::Result<SlangCompiler>>,
artifacts: JitArtifacts,
pipelines: HashMap<Vec<u8>, Option<Arc<ComputePipeline>>>,
observations: BTreeMap<u64, RouteObservation>,
pending: VecDeque<Arc<RouteTimings>>,
worker: Option<CompileWorker>,
in_flight: BTreeSet<Vec<u8>>,
}
impl JitFusion {
pub(super) fn new() -> Self {
Self {
restored: false,
compiler: None,
artifacts: JitArtifacts::new(),
pipelines: HashMap::new(),
observations: BTreeMap::new(),
pending: VecDeque::new(),
worker: None,
in_flight: BTreeSet::new(),
}
}
pub(super) fn admit(
&mut self,
device: &Device,
program: &Program,
tape_eligible: bool,
) -> Option<Vec<u8>> {
let census = device.fusion_census();
self.restore_once(device);
self.drain_pending();
if matches!(
jit_route_override(),
Some(JitRouteOverride::Tape) | Some(JitRouteOverride::Unfused)
) {
census.jit(JitOutcome::Disabled);
return None;
}
let signature = jit::signature_key(&program.canonical_key());
if jit_route_override() != Some(JitRouteOverride::Specialized)
&& let Some(observation) = self.observation_mut(signature)
{
observation.records = observation.records.saturating_add(1);
if !select_route(observation, tape_eligible) {
census.jit(JitOutcome::PolicyTape);
return None;
}
}
let key = program.artifact_key();
if let Some(slot) = self.pipelines.get(&key) {
census.jit(if slot.is_some() {
JitOutcome::Reused
} else {
JitOutcome::CachedNegative
});
return slot.is_some().then_some(key);
}
self.drain_compiles(census);
if self.pipelines.contains_key(&key) {
census.jit(JitOutcome::CachedNegative);
return None;
}
let bounded = cfg!(oa_profile = "compatibility");
if self.artifacts.get(program, bounded).is_some() {
return self.publish(device, &key).map(|_| key);
}
if self.in_flight.contains(&key) {
census.jit(JitOutcome::Pending);
return None;
}
if self.pipelines.len() + self.in_flight.len() >= MAX_PIPELINE_ENTRIES
|| self.artifacts.len() + self.in_flight.len() >= JitArtifacts::MAX_CHAINS
{
census.jit(JitOutcome::Bound);
return None;
}
self.queue_compile(census, program, bounded);
None
}
fn queue_compile(&mut self, census: &FusionCensus, program: &Program, bounded: bool) {
let key = program.artifact_key();
if self.worker.is_none() {
let Some(compiler) = self.worker_compiler() else {
census.jit(JitOutcome::CompilerUnavailable);
self.negative_admission(key);
return;
};
match CompileWorker::spawn(compiler) {
Ok(worker) => self.worker = Some(worker),
Err(error) => {
crate::log_warn!(
crate::LogComponent::RUNTIME,
"JIT specialization worker unavailable: {error}"
);
census.jit(JitOutcome::CompilerUnavailable);
self.negative_admission(key);
return;
}
}
}
let request = CompileRequest {
program: program.clone(),
bounded,
};
match self
.worker
.as_ref()
.map(|worker| worker.requests.try_send(request))
{
Some(Ok(())) => {
self.in_flight.insert(key);
census.jit(JitOutcome::Queued);
}
Some(Err(std::sync::mpsc::TrySendError::Full(_))) => {
census.jit(JitOutcome::Bound);
}
_ => {
census.jit(JitOutcome::CompilerUnavailable);
self.negative_admission(key);
self.worker = None;
}
}
}
fn worker_compiler(&mut self) -> Option<SlangCompiler> {
match self.compiler.take() {
Some(Ok(compiler)) => Some(compiler),
Some(Err(error)) => {
self.compiler = Some(Err(error));
None
}
None => match SlangCompiler::open_default() {
Ok(compiler) => Some(compiler),
Err(error) => {
crate::log_warn!(
crate::LogComponent::RUNTIME,
"JIT specialization compiler unavailable: {error}"
);
self.compiler = Some(Err(error));
None
}
},
}
}
fn negative_admission(&mut self, key: Vec<u8>) {
if self.pipelines.len() < MAX_PIPELINE_ENTRIES {
self.pipelines.insert(key, None);
}
}
fn drain_compiles(&mut self, census: &FusionCensus) {
let mut dead = false;
loop {
let result = match self.worker.as_mut().map(|worker| worker.results.try_recv()) {
Some(Ok(result)) => result,
Some(Err(std::sync::mpsc::TryRecvError::Disconnected)) => {
dead = true;
break;
}
_ => break,
};
self.in_flight.remove(&result.key);
match result.result {
Ok(chain) => {
census.jit_compile(chain.compile_time);
if let Err(error) = self.artifacts.insert(&result.compiler_version, chain) {
crate::log_warn!(
crate::LogComponent::RUNTIME,
"JIT specialization artifact rejected: {error}"
);
census.jit(JitOutcome::Bound);
self.negative_admission(result.key);
}
}
Err(outcome) => {
census.jit(outcome);
self.negative_admission(result.key);
}
}
}
if dead {
self.worker = None;
for key in std::mem::take(&mut self.in_flight) {
census.jit(JitOutcome::CompilerUnavailable);
self.negative_admission(key);
}
}
}
fn publish(&mut self, device: &Device, key: &[u8]) -> Option<Arc<ComputePipeline>> {
if self.pipelines.len() >= MAX_PIPELINE_ENTRIES {
return None;
}
let census = device.fusion_census();
let bounded = cfg!(oa_profile = "compatibility");
let artifact = self.artifacts.get_key(bounded, key)?;
let pipeline_started = Instant::now();
let layout = if bounded {
device.bounded_descriptor_layout(artifact.bindings)
} else {
Some(device.descriptor_layout())
};
let pipeline = layout
.ok_or_else(|| jit_missing("descriptor layout"))
.and_then(|layout| {
device
.pipeline_cache()
.map_err(|_| jit_missing("pipeline cache"))
.and_then(|cache| {
ComputePipeline::from_words(
device.raw_arc(),
layout,
artifact.words(),
[256, 1, 1],
device.physical().limits,
cache.raw(),
)
})
});
census.jit_pipeline(pipeline_started.elapsed());
match pipeline {
Ok(pipeline) => {
census.jit(JitOutcome::Admitted);
let pipeline = Arc::new(pipeline);
self.pipelines.insert(key.to_vec(), Some(pipeline.clone()));
Some(pipeline)
}
Err(error) => {
census.jit(JitOutcome::PipelineRejected);
crate::log_warn!(
crate::LogComponent::RUNTIME,
"JIT specialization pipeline rejected: {error:?}"
);
self.pipelines.insert(key.to_vec(), None);
None
}
}
}
pub(super) fn pipeline(&mut self, device: &Device, key: &[u8]) -> Option<Arc<ComputePipeline>> {
let census = device.fusion_census();
self.drain_compiles(census);
if let Some(slot) = self.pipelines.get(key) {
return slot.clone();
}
self.publish(device, key)
}
pub(super) fn observe(&mut self, timings: Arc<RouteTimings>) {
if self.pending.len() >= MAX_PENDING_TIMINGS {
self.pending.pop_front();
}
self.pending.push_back(timings);
}
fn drain_pending(&mut self) {
let mut index = 0;
while index < self.pending.len() {
let Some(durations) = self.pending[index].try_durations() else {
index += 1;
continue;
};
self.pending.remove(index);
for (signature, route, duration) in durations {
if let Some(observation) = self.observation_mut(signature) {
observation.push(route, duration.as_nanos() as u64);
}
}
}
}
fn observation_mut(&mut self, signature: u64) -> Option<&mut RouteObservation> {
if !self.observations.contains_key(&signature)
&& self.observations.len() >= MAX_OBSERVED_SIGNATURES
{
return None;
}
Some(
self
.observations
.entry(signature)
.or_insert_with(RouteObservation::new),
)
}
fn restore_once(&mut self, device: &Device) {
if self.restored {
return;
}
self.restored = true;
let scope = VkCacheScope::new(device.physical());
self.artifacts = JitArtifacts::restore(&scope.path(), &scope).unwrap_or_else(|error| {
crate::log_warn!(
crate::LogComponent::RUNTIME,
"discarding persisted JIT artifacts: {error}"
);
JitArtifacts::new()
});
if let Ok(file) = super::vkcache::VkCacheFile::open(&scope.path(), &scope)
&& let Some(section) = file.section(super::vkcache::SECTION_FUSION_ROUTE_OBSERVATIONS)
&& section.version == OBSERVATION_VERSION
{
match jit::decode_observations(§ion.bytes) {
Ok(records) => {
for record in &records {
if self.observations.len() >= MAX_OBSERVED_SIGNATURES {
break;
}
self
.observations
.entry(record.signature_key)
.or_insert_with(|| RouteObservation::restore(record));
}
}
Err(error) => crate::log_warn!(
crate::LogComponent::RUNTIME,
"discarding persisted fusion route observations: {error}"
),
}
}
}
pub(super) fn section_updates(
&mut self,
census: &FusionCensus,
) -> Vec<super::vkcache::SectionUpdate> {
self.drain_compiles(census);
let mut updates = self.artifacts.section_updates().unwrap_or_default();
if !self.observations.is_empty() {
let records: Vec<RouteObservationRecord> = self
.observations
.iter()
.map(|(signature, observation)| observation.record(*signature))
.collect();
if let Ok(payload) = jit::encode_observations(&records) {
updates.push(super::vkcache::SectionUpdate::replace(
super::vkcache::SECTION_FUSION_ROUTE_OBSERVATIONS,
OBSERVATION_VERSION,
payload,
));
}
}
updates
}
}
fn jit_missing(what: &str) -> crate::Error {
crate::Error::missing_capability(format!("JIT specialization {what} unavailable"))
}
#[cfg(test)]
#[path = "../../../../../test/rs/runtime/jit_policy_unit.rs"]
mod tests;