Skip to main content

cubecl_spirv/
lib.rs

1#![allow(unknown_lints, unnecessary_transmutes)]
2
3use cubecl_runtime::kernel::BufferIOAttr;
4use std::{
5    fmt::{Debug, Display},
6    sync::Arc,
7};
8
9use cubecl_core::prelude::Visibility;
10use rspirv::{binary::Disassemble, dr::Module};
11
12pub mod attributes;
13pub mod compiler;
14pub mod lower;
15pub mod ops;
16pub mod target;
17pub mod types;
18
19pub use compiler::*;
20use serde::{Deserialize, Serialize};
21pub use target::*;
22
23#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct SpirvKernel {
25    #[serde(skip)]
26    pub module: Option<Arc<Module>>,
27
28    pub assembled_module: Vec<u32>,
29    pub bindings: Vec<Visibility>,
30    /// What the kernel does with each buffer binding, by buffer position --
31    /// the four-state answer the launch path's taint bookkeeping consumes,
32    /// never collapsed the way [`bindings`](Self::bindings) is. Defaulted for
33    /// entries persisted before the field existed, which reads as no answer:
34    /// every buffer both read and written.
35    #[serde(default)]
36    pub io: Option<Vec<BufferIOAttr>>,
37    pub shared_size: usize,
38    pub immediate_size: Option<usize>,
39    pub info_visibility: Visibility,
40}
41
42impl Eq for SpirvKernel {}
43impl PartialEq for SpirvKernel {
44    fn eq(&self, other: &Self) -> bool {
45        self.assembled_module == other.assembled_module
46    }
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
50pub struct SpirvCacheEntry {
51    pub entrypoint_name: String,
52    pub kernel: SpirvKernel,
53}
54
55impl SpirvCacheEntry {
56    pub fn new(entrypoint_name: String, kernel: SpirvKernel) -> Self {
57        SpirvCacheEntry {
58            entrypoint_name,
59            kernel,
60        }
61    }
62}
63
64impl Display for SpirvKernel {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        if let Some(module) = &self.module {
67            write!(f, "{}", module.disassemble())
68        } else {
69            f.write_str("SPIR-V")
70        }
71    }
72}