vyre 0.4.0

GPU compute intermediate representation with a standard operation library
Documentation
//! Proptest-generated lower tests.
//!
//! Random valid Programs are lowered to WGSL and compiled into a real wgpu
//! compute pipeline. Any failure in
//! lowering or GPU compilation is a bug.

use proptest::prelude::*;
use vyre::ir::validate;
use vyre::lower::wgsl::lower;

use crate::support::arb_program;

/// Returns the cached GPU device or panics with the probe error.
fn gpu_device() -> &'static (wgpu::Device, wgpu::Queue) {
    vyre_wgpu::runtime::cached_device()
        .unwrap_or_else(|error| panic!("Fix: GPU is mandatory for generated lower tests: {error}"))
}

/// Compile WGSL into a wgpu compute pipeline.
///
/// Panics if the WGSL is invalid, which is exactly what this test guards
/// against: a program that passed IR validation and lowering must compile on
/// the GPU.
fn compile_wgsl(device: &wgpu::Device, wgsl: &str) -> wgpu::ComputePipeline {
    let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
        label: Some("lower_generated_test"),
        source: wgpu::ShaderSource::Wgsl(wgsl.into()),
    });
    device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
        label: Some("lower_generated_test"),
        layout: None,
        module: &module,
        entry_point: Some("main"),
        compilation_options: wgpu::PipelineCompilationOptions::default(),
        cache: None,
    })
}

proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    /// Every generated program that passes validation must lower successfully,
    /// and compile into a real wgpu pipeline.
    #[test]
    fn valid_programs_lower_and_compile(program in arb_program()) {
        let errors = validate(&program);
        prop_assume!(errors.is_empty(), "program failed validation: {:?}", errors);

        let wgsl = lower(&program).expect("valid program must lower to WGSL");

        let (device, _queue) = gpu_device();
        let _pipeline = compile_wgsl(device, &wgsl);
    }
}