vyre 0.3.0

GPU bytecode condition engine
Documentation
use crate::error::Result;
use crate::index::CompiledRuleIndex;
use crate::pattern::RuleMatch;

impl CompiledRuleIndex {
    /// Evaluate pre-computed pattern matches on GPU.
    ///
    /// The caller runs warpstate pattern matching and passes the matches here.
    /// vyre evaluates all rule conditions on GPU in a single dispatch.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Gpu`] if no GPU adapter is available.
    #[cfg(feature = "gpu")]
    pub fn evaluate_matches(
        &self,
        file_bytes: &[u8],
        matches: &[matchkit::Match],
    ) -> Result<Vec<RuleMatch>> {
        let (device, queue) = crate::gpu::gpu_pipeline::cached_device()?;
        self.evaluate_matches_with_device(file_bytes, matches, device, queue)
    }

    /// Evaluate with an externally managed GPU device.
    #[cfg(feature = "gpu")]
    pub fn evaluate_matches_with_device(
        &self,
        file_bytes: &[u8],
        matches: &[matchkit::Match],
        device: &wgpu::Device,
        queue: &wgpu::Queue,
    ) -> Result<Vec<RuleMatch>> {
        let file_ctx = crate::file_context::FileContext::from_bytes(file_bytes);
        let rule_string_counts: Vec<usize> = self.rules.iter().map(|r| r.strings.len()).collect();
        let plan = crate::gpu::gpu_pipeline::GpuEvaluationPlan {
            rule_string_counts: &rule_string_counts,
            pattern_to_rules: &self.mapping.pattern_to_rules,
            rule_list: &self.mapping.rule_list,
            string_local_ids: &self.mapping.string_local_ids,
            sentinel_pattern_ids: &[],
            max_cached_positions: self.max_cached_positions,
        };
        let hit_bits = crate::gpu::gpu_pipeline::execute_gpu(
            device, queue, &self.programs, plan, matches, file_bytes, file_ctx,
        )?;
        Ok(self.materialize_matches(&hit_bits))
    }
}