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>>>,
block_restored: bool,
block_artifacts: BTreeMap<Vec<u8>, BlockArtifact>,
block_compiler_version: String,
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(),
block_restored: false,
block_artifacts: BTreeMap::new(),
block_compiler_version: String::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
}
pub(super) fn admit_block(
&mut self,
device: &Device,
program: &crate::runtime::dna::ir::tile::BlockProgram,
) -> Option<Vec<u8>> {
let census = device.fusion_census();
let bounded = cfg!(oa_profile = "compatibility");
let key = block_key(program, bounded);
self.restore_once(device);
self.restore_blocks_once(device);
self.drain_pending();
if matches!(
jit_route_override(),
Some(JitRouteOverride::Tape) | Some(JitRouteOverride::Unfused)
) {
census.jit(JitOutcome::Disabled);
return None;
}
if jit_route_override() != Some(JitRouteOverride::Specialized)
&& let Some(observation) = self.observation_mut(block_signature(program))
{
observation.records = observation.records.saturating_add(1);
if !select_route(observation, true) {
census.jit(JitOutcome::PolicyTape);
return None;
}
}
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);
}
if self.pipelines.len() >= MAX_PIPELINE_ENTRIES {
census.jit(JitOutcome::Bound);
return None;
}
let expected_push =
(program.bindings.len() + program.push_scalars.len() + program.push_f32_scalars.len()) as u32
* 4;
let pipeline = (|| {
let words = match self.block_artifacts.get(&key) {
Some(artifact) => artifact.words.clone(),
None => {
let source = program.slang_source(bounded).ok()?;
let words = self
.block_compiler()?
.compile_source(&source, bounded)
.ok()?;
crate::runtime::dna::compiler::validate_with_spirv_tools(&words, bounded).ok()?;
if self.block_artifacts.len() < MAX_PIPELINE_ENTRIES {
self.block_artifacts.insert(
key.clone(),
BlockArtifact {
words: words.clone(),
push_bytes: expected_push,
bindings: program.bindings.len() as u32,
lanes: program.shape.lanes,
},
);
}
words
}
};
if crate::runtime::dna::kernel::reflect_push_constant_size(&words).ok()? != expected_push {
return None;
}
let layout = if bounded {
device.bounded_descriptor_layout(program.bindings.len() as u32)
} else {
Some(device.descriptor_layout())
}?;
device.pipeline_cache().ok().and_then(|cache| {
ComputePipeline::from_words(
device.raw_arc(),
layout,
&words,
[program.shape.lanes, 1, 1],
device.physical().limits,
cache.raw(),
)
.ok()
})
})();
match pipeline {
Some(pipeline) => {
census.jit(JitOutcome::Admitted);
self.pipelines.insert(key.clone(), Some(Arc::new(pipeline)));
Some(key)
}
None => {
census.jit(JitOutcome::PipelineRejected);
self.negative_admission(key);
None
}
}
}
fn block_compiler(&mut self) -> Option<&SlangCompiler> {
if let Some(Ok(compiler)) = &self.compiler {
self.block_compiler_version = compiler.version.clone();
}
if self.compiler.is_none() {
self.compiler = Some(SlangCompiler::open_default());
}
self.compiler.as_ref()?.as_ref().ok()
}
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);
}
}
}
}
pub(super) fn route_stats(
&mut self,
device: &Device,
signature: u64,
) -> (Option<u64>, usize, u64) {
self.restore_once(device);
self.restore_blocks_once(device);
match self.observations.get(&signature) {
Some(observation) => (
observation.specialized_p50(),
observation.specialized_ns.len() + observation.restored_samples as usize,
observation.records,
),
None => (None, 0, 0),
}
}
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}"
),
}
}
}
fn restore_blocks_once(&mut self, device: &Device) {
if self.block_restored {
return;
}
self.block_restored = true;
let Some(compiler) = self.block_compiler() else {
return;
};
let version = compiler.version.clone().into_bytes();
let bounded = cfg!(oa_profile = "compatibility");
let scope = VkCacheScope::new(device.physical());
let file = match super::vkcache::VkCacheFile::open(&scope.path(), &scope) {
Ok(file) => file,
Err(_) => return,
};
let index = match file.section(super::vkcache::SECTION_BLOCK_ARTIFACT_INDEX) {
Some(section) if section.version == 1 => jit::decode_index(§ion.bytes),
_ => return,
};
let table = match file.section(super::vkcache::SECTION_BLOCK_SPIRV_ARTIFACTS) {
Some(section) if section.version == 1 => jit::decode_spirv(§ion.bytes),
_ => return,
};
let (index, table) = match (index, table) {
(Ok(index), Ok(table)) => (index, table),
_ => return,
};
let table: BTreeMap<u64, Vec<u32>> = table
.into_iter()
.map(|record| (record.artifact_id, record.words))
.collect();
let descriptor_profile = u32::from(bounded);
for record in index {
if self.block_artifacts.len() >= MAX_PIPELINE_ENTRIES {
break;
}
if record.descriptor_profile != descriptor_profile
|| record.emitter_version != BLOCK_EMITTER_VERSION
|| record.spirv_environment != u32::from(!bounded)
|| record.compiler != version
|| !(2..=64).contains(&record.binding_count)
|| record.push_bytes == 0
|| record.push_bytes % 4 != 0
|| !(32..=1024).contains(&record.workgroup_size)
|| record.flags != 0
|| jit::artifact_id(
&record.key,
record.descriptor_profile,
record.emitter_version,
) != record.artifact_id
{
continue;
}
let Some(words) = table.get(&record.artifact_id) else {
continue;
};
if crate::runtime::dna::compiler::validate_with_spirv_tools(words, bounded).is_err()
|| crate::runtime::dna::kernel::reflect_push_constant_size(words)
.map(|size| size != record.push_bytes)
.unwrap_or(true)
{
continue;
}
self.block_artifacts.insert(
record.key,
BlockArtifact {
words: words.clone(),
push_bytes: record.push_bytes,
bindings: record.binding_count,
lanes: record.workgroup_size,
},
);
}
}
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.block_artifacts.is_empty() {
let bounded = cfg!(oa_profile = "compatibility");
let descriptor_profile = u32::from(bounded);
let mut index = Vec::with_capacity(self.block_artifacts.len());
let mut spirv = Vec::with_capacity(self.block_artifacts.len());
for (key, artifact) in &self.block_artifacts {
let artifact_id = jit::artifact_id(key, descriptor_profile, BLOCK_EMITTER_VERSION);
index.push(jit::JitIndexRecord {
artifact_id,
key: key.clone(),
descriptor_profile,
spirv_environment: u32::from(!bounded),
workgroup_size: artifact.lanes,
binding_count: artifact.bindings,
push_bytes: artifact.push_bytes,
emitter_version: BLOCK_EMITTER_VERSION,
compiler: self.block_compiler_version.clone().into_bytes(),
flags: 0,
});
spirv.push(jit::JitSpirvRecord {
artifact_id,
words: artifact.words.clone(),
});
}
if let (Ok(index), Ok(spirv)) = (jit::encode_index(&index), jit::encode_spirv(&spirv)) {
updates.push(super::vkcache::SectionUpdate::replace(
super::vkcache::SECTION_BLOCK_ARTIFACT_INDEX,
1,
index,
));
updates.push(super::vkcache::SectionUpdate::replace(
super::vkcache::SECTION_BLOCK_SPIRV_ARTIFACTS,
1,
spirv,
));
}
}
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
}
}
struct BlockArtifact {
words: Vec<u32>,
push_bytes: u32,
bindings: u32,
lanes: u32,
}
const BLOCK_EMITTER_VERSION: u32 = 2;
fn jit_missing(what: &str) -> crate::Error {
crate::Error::missing_capability(format!("JIT specialization {what} unavailable"))
}
pub(in crate::runtime) fn block_key(
program: &crate::runtime::dna::ir::tile::BlockProgram,
bounded: bool,
) -> Vec<u8> {
let mut hash = 0xcbf2_9ce4_8422_2325_u64;
for byte in format!("oa.tile.v1:{bounded}:{program:?}").bytes() {
hash ^= u64::from(byte);
hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
}
format!("oa.tile.gemm_nt.{hash:016x}").into_bytes()
}
pub(in crate::runtime) fn block_signature(
program: &crate::runtime::dna::ir::tile::BlockProgram,
) -> u64 {
jit::signature_key(&block_key(program, cfg!(oa_profile = "compatibility")))
}
#[cfg(test)]
#[path = "../../../../../test/rs/runtime/jit_policy_unit.rs"]
mod tests;