1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/// Errors reported while adding semantic-program structure.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ProgramBuildError {
/// A value token was issued by another builder.
#[error("value does not belong to this semantic-program builder")]
ForeignValue,
/// An import root belongs to another semantic program.
#[error("import root does not belong to the source semantic program")]
ForeignImportRoot,
/// Import bindings were frozen for another semantic program.
#[error("import bindings do not belong to the source semantic program")]
ForeignBindings,
/// Structured control flow is reserved for the later region/block model.
#[error("semantic control-flow construct {construct:?} is not supported")]
UnsupportedControlFlow {
/// Frontend construct that could not be represented.
construct: &'static str,
},
/// A frozen source program failed import-time structural validation.
#[error("source semantic program is invalid for import: {source}")]
InvalidImport {
#[source]
source: ProgramStructuralError,
},
/// A binding target is a computed value rather than an external input.
#[error("tensor bindings may target only semantic-program inputs")]
BindingTargetNotInput,
/// A shape guard target is not produced by a semantic operation.
#[error("semantic shape guards may target only operation outputs")]
GuardTargetNotOperationOutput,
/// An external input already has a tensor binding.
#[error("semantic-program input already has a tensor binding")]
DuplicateBinding,
/// The builder cannot represent another value slot.
#[error("semantic-program value count exceeds the supported u32 range")]
TooManyValues,
/// An operation received the wrong number of SSA inputs.
#[error("semantic operation expects {expected} inputs, got {actual}")]
Arity {
/// Declared input count.
expected: usize,
/// Supplied input count.
actual: usize,
},
/// Semantic dtype or shape inference failed.
#[error("semantic operation metadata inference failed: {source}")]
Metadata {
/// Original typed runtime inference error.
#[source]
source: Box<crate::Error>,
},
/// Inference did not return the declared number of outputs.
#[error("semantic operation declares {expected} outputs, inferred {actual}")]
OutputMetadataCount {
/// Declared output count.
expected: usize,
/// Inferred metadata count.
actual: usize,
},
/// An extension did not explicitly declare its observable effects.
#[error("extension family {family:?} has no semantic effect declaration")]
UndeclaredExtensionEffects {
/// Stable extension family.
family: &'static str,
},
/// An extension did not explicitly declare output aliases.
#[error("extension family {family:?} has no semantic alias declaration")]
UndeclaredExtensionAliases {
/// Stable extension family.
family: &'static str,
},
/// An extension declared an invalid resource family.
#[error("extension family {family:?} declared an invalid effect resource: {source}")]
InvalidEffectResource {
/// Stable extension family.
family: &'static str,
/// Typed resource validation failure.
#[source]
source: EffectResourceError,
},
/// An alias referenced an input or output outside the operation arity.
#[error(
"semantic alias index is out of bounds: output {output}/{output_count}, input {input:?}/{input_count}"
)]
AliasOutOfBounds {
/// Referenced output.
output: usize,
/// Output arity.
output_count: usize,
/// Referenced input, when applicable.
input: Option<usize>,
/// Input arity.
input_count: usize,
},
/// Alias declarations did not cover every output exactly once.
#[error("semantic aliases must cover {expected} outputs exactly once, got {actual}")]
AliasCoverage {
/// Output arity.
expected: usize,
/// Number of distinct valid output declarations.
actual: usize,
},
}
/// Errors reported while querying an immutable semantic program.
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
pub enum ProgramQueryError {
/// A value token belongs to another program or names no frozen value.
#[error("value does not belong to this semantic program")]
ForeignValue,
}
/// Internal structural validation failures detected during atomic freeze.
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ProgramStructuralError {
/// An operation references a value outside the frozen value table.
#[error("semantic operation references a value outside the program")]
InvalidValueReference,
/// An operation output is not strictly after all prior values.
#[error("semantic operation outputs violate SSA ordering")]
InvalidSsaOrder,
}
/// Tensor-binding validation failures detected during atomic freeze.
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ProgramBindingError {
/// A pending binding no longer names an external input.
#[error("tensor binding target is not a semantic-program input")]
InvalidTarget,
/// Tensor dtype differs from the input declaration.
#[error("tensor binding dtype mismatch: expected {expected:?}, got {actual:?}")]
DTypeMismatch {
/// Declared input dtype.
expected: tenferro_tensor::DType,
/// Bound tensor dtype.
actual: tenferro_tensor::DType,
},
/// Tensor rank differs from the input declaration.
#[error("tensor binding rank mismatch: expected {expected}, got {actual}")]
RankMismatch {
/// Declared input rank.
expected: usize,
/// Bound tensor rank.
actual: usize,
},
/// A statically exact dimension differs from the input declaration.
#[error("tensor binding extent mismatch at axis {axis}: expected {expected}, got {actual}")]
ExactExtentMismatch {
/// Mismatching axis.
axis: usize,
/// Declared exact extent.
expected: usize,
/// Bound tensor extent.
actual: usize,
},
/// A bounded dimension exceeds the declared upper bound.
#[error(
"tensor binding extent exceeds upper bound at axis {axis}: bound {bound}, got {actual}"
)]
UpperBoundExceeded {
/// Axis whose bound was exceeded.
axis: usize,
/// Declared upper bound.
bound: usize,
/// Bound tensor extent.
actual: usize,
},
}
/// Errors reported while atomically freezing semantic structure and bindings.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ProgramFinishError {
/// One requested output belongs to another builder or names no value.
#[error("program output does not belong to this semantic-program builder")]
ForeignOutput,
/// Frozen structure failed an invariant check.
#[error("semantic-program structural validation failed: {source}")]
StructuralValidation {
/// Typed structural invariant failure.
#[source]
source: ProgramStructuralError,
},
/// A tensor default or large constant does not match its input declaration.
#[error("semantic-program binding finalization failed: {source}")]
BindingFinalization {
/// Typed binding mismatch.
#[source]
source: ProgramBindingError,
},
}
/// Failures produced by a validated semantic transform transaction.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum SemanticTransformError {
/// Program construction or import failed inside the transform.
#[error(transparent)]
Build(#[from] ProgramBuildError),
/// Atomic freeze failed after the transform returned roots.
#[error(transparent)]
Finish(#[from] ProgramFinishError),
/// The transform returned a value not owned by its destination builder.
#[error("semantic transform returned a foreign destination value")]
ForeignReturnedValue,
/// The transform did not carry every input tensor binding forward.
#[error("semantic transform discarded one or more tensor bindings")]
DroppedBindings,
/// The transform deliberately rejected this input.
#[error("semantic transform rejected the input")]
Rejected,
}
/// Invalid typed effect-resource identity.
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum EffectResourceError {
/// Resource-family names must be nonempty and versioned.
#[error("effect resource family must be a nonempty versioned identifier")]
InvalidFamily,
}