Skip to main content

vyre_driver_cuda/egraph_kernel_plan/
error.rs

1use std::fmt;
2
3use crate::backend::staging_reserve::CudaStorageReserveFailure;
4
5/// Error returned when e-graph kernel work cannot be planned.
6#[derive(Clone, Debug, Eq, PartialEq)]
7pub enum CudaEGraphKernelPlanError {
8    /// Threads per block was zero.
9    ZeroThreadsPerBlock,
10    /// Maximum blocks per launch was zero.
11    ZeroMaxBlocksPerLaunch,
12    /// Count arithmetic overflowed.
13    CountOverflow {
14        /// Field being computed.
15        field: &'static str,
16    },
17    /// PTX generation received an invalid CUDA SM target.
18    InvalidPtxTarget {
19        /// Invalid `sm_XX` target.
20        target_sm: u32,
21    },
22    /// Packed image metadata did not match the checked CUDA kernel view.
23    ImageViewMismatch {
24        /// Field that disagreed.
25        field: &'static str,
26        /// Count from the packed foundation image.
27        image: usize,
28        /// Count from the CUDA kernel view.
29        view: usize,
30    },
31    /// A packed row child span pointed outside the packed child column.
32    ImageColumnOutOfBounds {
33        /// Column being decoded.
34        column: &'static str,
35        /// Row being decoded.
36        row: u32,
37        /// Start index into the column.
38        start: usize,
39        /// End index into the column.
40        end: usize,
41        /// Column length.
42        len: usize,
43    },
44    /// A pair ordinal did not identify a valid row pair in a signature bucket.
45    SignaturePairOrdinalOutOfBounds {
46        /// Signature bucket being decoded.
47        bucket_index: u32,
48        /// Pair ordinal inside the bucket's triangular pair space.
49        pair_ordinal: u64,
50        /// Number of candidate pairs in the bucket.
51        candidate_pair_count: u64,
52    },
53    /// A signature bucket's row range pointed outside the bucket row table.
54    SignatureBucketRowsOutOfBounds {
55        /// Signature bucket being decoded.
56        bucket_index: u32,
57        /// First row offset in the bucket row table.
58        first_bucket_row: usize,
59        /// Bucket row count.
60        row_count: usize,
61        /// Available row table length.
62        bucket_rows_len: usize,
63    },
64    /// Planner storage reservation failed.
65    StorageReserveFailed {
66        /// Field being reserved.
67        field: &'static str,
68        /// Requested element count.
69        requested: usize,
70        /// Allocator error text.
71        message: String,
72    },
73}
74
75impl CudaStorageReserveFailure for CudaEGraphKernelPlanError {
76    fn storage_reserve_failed(field: &'static str, requested: usize, message: String) -> Self {
77        Self::StorageReserveFailed {
78            field,
79            requested,
80            message,
81        }
82    }
83}
84
85impl fmt::Display for CudaEGraphKernelPlanError {
86    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87        match self {
88            Self::ZeroThreadsPerBlock => write!(
89                f,
90                "CUDA e-graph kernel planner received zero threads per block. Fix: choose a non-zero launch width before planning equality-saturation work."
91            ),
92            Self::ZeroMaxBlocksPerLaunch => write!(
93                f,
94                "CUDA e-graph kernel planner received zero max blocks per launch. Fix: choose a non-zero launch partition limit."
95            ),
96            Self::CountOverflow { field } => write!(
97                f,
98                "CUDA e-graph kernel planner overflowed while computing {field}. Fix: shard the resident e-graph image before launch planning."
99            ),
100            Self::InvalidPtxTarget { target_sm } => write!(
101                f,
102                "CUDA e-graph structural-equivalence PTX generation received invalid sm_{target_sm}. Fix: pass the backend's probed CUDA PTX target."
103            ),
104            Self::ImageViewMismatch { field, image, view } => write!(
105                f,
106                "CUDA e-graph kernel planner received mismatched {field}: packed image has {image}, kernel view has {view}. Fix: build the view from the same upload plan/image."
107            ),
108            Self::ImageColumnOutOfBounds {
109                column,
110                row,
111                start,
112                end,
113                len,
114            } => write!(
115                f,
116                "CUDA e-graph kernel planner decoded row {row} span {column}[{start}..{end}) but {column} has {len} entries. Fix: rebuild the packed e-graph image from a validated snapshot."
117            ),
118            Self::SignaturePairOrdinalOutOfBounds {
119                bucket_index,
120                pair_ordinal,
121                candidate_pair_count,
122            } => write!(
123                f,
124                "CUDA e-graph signature bucket {bucket_index} pair ordinal {pair_ordinal} is outside {candidate_pair_count} candidate pairs. Fix: launch only planned pair-wave ranges."
125            ),
126            Self::SignatureBucketRowsOutOfBounds {
127                bucket_index,
128                first_bucket_row,
129                row_count,
130                bucket_rows_len,
131            } => write!(
132                f,
133                "CUDA e-graph signature bucket {bucket_index} row range [{first_bucket_row}..{}) exceeds bucket row table length {bucket_rows_len}. Fix: rebuild the signature bucket plan.",
134                first_bucket_row.saturating_add(*row_count)
135            ),
136            Self::StorageReserveFailed {
137                field,
138                requested,
139                message,
140            } => write!(
141                f,
142                "CUDA e-graph kernel planner could not reserve {requested} {field} entries: {message}. Fix: shard the resident e-graph image before launch planning."
143            ),
144        }
145    }
146}
147
148impl std::error::Error for CudaEGraphKernelPlanError {}