mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-22 07:21:04 +01:00
Use span information in analyzer errors (proof of concept) (#1470)
* Proof-of-concept for adding spans to validation errors. Still missing: actually printing the damn stuff. * Emit errors from analyzer in the CLI. TODO: tests, I guess! * Simplification refactoring: avoid avoiding allocations so vehemently. * Mask helper traits with `as _`. * Fix block iterator throwing up when span feature is disabled. * Nest use statements. * Add basic docs. * Axe AddSpanResult.
This commit is contained in:
1 parent
fd5085277a
commit
ec001c3ead
10 files changed
+528
-138
No files matched your search
+143
-58
@@ -6,6 +6,7 @@ use super::{
|
||||
analyzer::{UniformityDisruptor, UniformityRequirements},
|
||||
ExpressionError, FunctionInfo, ModuleInfo,
|
||||
};
|
||||
use crate::span::{AddSpan as _, MapErrWithSpan as _, WithSpan};
|
||||
|
||||
#[cfg(feature = "validate")]
|
||||
use bit_set::BitSet;
|
||||
@@ -201,11 +202,11 @@ impl<'a> BlockContext<'a> {
|
||||
&self,
|
||||
handle: Handle<crate::Expression>,
|
||||
valid_expressions: &BitSet,
|
||||
) -> Result<&crate::TypeInner, ExpressionError> {
|
||||
) -> Result<&crate::TypeInner, WithSpan<ExpressionError>> {
|
||||
if handle.index() >= self.expressions.len() {
|
||||
Err(ExpressionError::DoesntExist)
|
||||
Err(ExpressionError::DoesntExist.with_span())
|
||||
} else if !valid_expressions.contains(handle.index()) {
|
||||
Err(ExpressionError::NotInScope)
|
||||
Err(ExpressionError::NotInScope.with_span_handle(handle, self.expressions))
|
||||
} else {
|
||||
Ok(self.info[handle].ty.inner_with(self.types))
|
||||
}
|
||||
@@ -215,9 +216,9 @@ impl<'a> BlockContext<'a> {
|
||||
&self,
|
||||
handle: Handle<crate::Expression>,
|
||||
valid_expressions: &BitSet,
|
||||
) -> Result<&crate::TypeInner, FunctionError> {
|
||||
) -> Result<&crate::TypeInner, WithSpan<FunctionError>> {
|
||||
self.resolve_type_impl(handle, valid_expressions)
|
||||
.map_err(|error| FunctionError::Expression { handle, error })
|
||||
.map_err_inner(|error| FunctionError::Expression { handle, error }.with_span())
|
||||
}
|
||||
|
||||
fn resolve_pointer_type(
|
||||
@@ -243,28 +244,33 @@ impl super::Validator {
|
||||
arguments: &[Handle<crate::Expression>],
|
||||
result: Option<Handle<crate::Expression>>,
|
||||
context: &BlockContext,
|
||||
) -> Result<super::ShaderStages, CallError> {
|
||||
) -> Result<super::ShaderStages, WithSpan<CallError>> {
|
||||
let fun = context
|
||||
.functions
|
||||
.try_get(function)
|
||||
.ok_or(CallError::InvalidFunction)?;
|
||||
.ok_or(CallError::InvalidFunction)
|
||||
.map_err(WithSpan::new)?;
|
||||
if fun.arguments.len() != arguments.len() {
|
||||
return Err(CallError::ArgumentCount {
|
||||
required: fun.arguments.len(),
|
||||
seen: arguments.len(),
|
||||
});
|
||||
}
|
||||
.with_span());
|
||||
}
|
||||
for (index, (arg, &expr)) in fun.arguments.iter().zip(arguments).enumerate() {
|
||||
let ty = context
|
||||
.resolve_type_impl(expr, &self.valid_expression_set)
|
||||
.map_err(|error| CallError::Argument { index, error })?;
|
||||
.map_err_inner(|error| {
|
||||
CallError::Argument { index, error }.with_span_handle(expr, context.expressions)
|
||||
})?;
|
||||
let arg_inner = &context.types[arg.ty].inner;
|
||||
if !ty.equivalent(arg_inner, context.types) {
|
||||
return Err(CallError::ArgumentType {
|
||||
index,
|
||||
required: arg.ty,
|
||||
seen_expression: expr,
|
||||
});
|
||||
}
|
||||
.with_span_handle(expr, context.expressions));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -272,15 +278,19 @@ impl super::Validator {
|
||||
if self.valid_expression_set.insert(expr.index()) {
|
||||
self.valid_expression_list.push(expr);
|
||||
} else {
|
||||
return Err(CallError::ResultAlreadyInScope(expr));
|
||||
return Err(CallError::ResultAlreadyInScope(expr)
|
||||
.with_span_handle(expr, context.expressions));
|
||||
}
|
||||
match context.expressions[expr] {
|
||||
crate::Expression::CallResult(callee)
|
||||
if fun.result.is_some() && callee == function => {}
|
||||
_ => return Err(CallError::ExpressionMismatch(result)),
|
||||
_ => {
|
||||
return Err(CallError::ExpressionMismatch(result)
|
||||
.with_span_handle(expr, context.expressions))
|
||||
}
|
||||
}
|
||||
} else if fun.result.is_some() {
|
||||
return Err(CallError::ExpressionMismatch(result));
|
||||
return Err(CallError::ExpressionMismatch(result).with_span());
|
||||
}
|
||||
|
||||
let callee_info = &context.prev_infos[function.index()];
|
||||
@@ -295,19 +305,23 @@ impl super::Validator {
|
||||
value: Handle<crate::Expression>,
|
||||
result: Handle<crate::Expression>,
|
||||
context: &BlockContext,
|
||||
) -> Result<(), FunctionError> {
|
||||
) -> Result<(), WithSpan<FunctionError>> {
|
||||
let pointer_inner = context.resolve_type(pointer, &self.valid_expression_set)?;
|
||||
let (ptr_kind, ptr_width) = match *pointer_inner {
|
||||
crate::TypeInner::Pointer { base, .. } => match context.types[base].inner {
|
||||
crate::TypeInner::Atomic { kind, width } => (kind, width),
|
||||
ref other => {
|
||||
log::error!("Atomic pointer to type {:?}", other);
|
||||
return Err(AtomicError::InvalidPointer(pointer).into());
|
||||
return Err(AtomicError::InvalidPointer(pointer)
|
||||
.with_span_handle(pointer, context.expressions)
|
||||
.into_other());
|
||||
}
|
||||
},
|
||||
ref other => {
|
||||
log::error!("Atomic on type {:?}", other);
|
||||
return Err(AtomicError::InvalidPointer(pointer).into());
|
||||
return Err(AtomicError::InvalidPointer(pointer)
|
||||
.with_span_handle(pointer, context.expressions)
|
||||
.into_other());
|
||||
}
|
||||
};
|
||||
|
||||
@@ -316,21 +330,27 @@ impl super::Validator {
|
||||
crate::TypeInner::Scalar { width, kind } if kind == ptr_kind && width == ptr_width => {}
|
||||
ref other => {
|
||||
log::error!("Atomic operand type {:?}", other);
|
||||
return Err(AtomicError::InvalidOperand(value).into());
|
||||
return Err(AtomicError::InvalidOperand(value)
|
||||
.with_span_handle(value, context.expressions)
|
||||
.into_other());
|
||||
}
|
||||
}
|
||||
|
||||
if let crate::AtomicFunction::Exchange { compare: Some(cmp) } = *fun {
|
||||
if context.resolve_type(cmp, &self.valid_expression_set)? != value_inner {
|
||||
log::error!("Atomic exchange comparison has a different type from the value");
|
||||
return Err(AtomicError::InvalidOperand(cmp).into());
|
||||
return Err(AtomicError::InvalidOperand(cmp)
|
||||
.with_span_handle(cmp, context.expressions)
|
||||
.into_other());
|
||||
}
|
||||
}
|
||||
|
||||
if self.valid_expression_set.insert(result.index()) {
|
||||
self.valid_expression_list.push(result);
|
||||
} else {
|
||||
return Err(AtomicError::ResultAlreadyInScope(result).into());
|
||||
return Err(AtomicError::ResultAlreadyInScope(result)
|
||||
.with_span_handle(result, context.expressions)
|
||||
.into_other());
|
||||
}
|
||||
match context.expressions[result] {
|
||||
//TODO: support atomic result with comparison
|
||||
@@ -339,7 +359,11 @@ impl super::Validator {
|
||||
width,
|
||||
comparison: false,
|
||||
} if kind == ptr_kind && width == ptr_width => {}
|
||||
_ => return Err(AtomicError::ResultTypeMismatch(result).into()),
|
||||
_ => {
|
||||
return Err(AtomicError::ResultTypeMismatch(result)
|
||||
.with_span_handle(result, context.expressions)
|
||||
.into_other())
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -347,15 +371,16 @@ impl super::Validator {
|
||||
#[cfg(feature = "validate")]
|
||||
fn validate_block_impl(
|
||||
&mut self,
|
||||
statements: &[crate::Statement],
|
||||
statements: &crate::Block,
|
||||
context: &BlockContext,
|
||||
) -> Result<BlockInfo, FunctionError> {
|
||||
) -> Result<BlockInfo, WithSpan<FunctionError>> {
|
||||
use crate::{Statement as S, TypeInner as Ti};
|
||||
let mut finished = false;
|
||||
let mut stages = super::ShaderStages::all();
|
||||
for statement in statements {
|
||||
for (statement, &span) in statements.span_iter() {
|
||||
if finished {
|
||||
return Err(FunctionError::InstructionsAfterReturn);
|
||||
return Err(FunctionError::InstructionsAfterReturn
|
||||
.with_span_static(span, "instructions after return"));
|
||||
}
|
||||
match *statement {
|
||||
S::Emit(ref range) => {
|
||||
@@ -363,7 +388,8 @@ impl super::Validator {
|
||||
if self.valid_expression_set.insert(handle.index()) {
|
||||
self.valid_expression_list.push(handle);
|
||||
} else {
|
||||
return Err(FunctionError::ExpressionAlreadyInScope(handle));
|
||||
return Err(FunctionError::ExpressionAlreadyInScope(handle)
|
||||
.with_span_handle(handle, context.expressions));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -382,7 +408,10 @@ impl super::Validator {
|
||||
kind: crate::ScalarKind::Bool,
|
||||
width: _,
|
||||
} => {}
|
||||
_ => return Err(FunctionError::InvalidIfType(condition)),
|
||||
_ => {
|
||||
return Err(FunctionError::InvalidIfType(condition)
|
||||
.with_span_handle(condition, context.expressions))
|
||||
}
|
||||
}
|
||||
stages &= self.validate_block(accept, context)?.stages;
|
||||
stages &= self.validate_block(reject, context)?.stages;
|
||||
@@ -401,12 +430,22 @@ impl super::Validator {
|
||||
kind: crate::ScalarKind::Sint,
|
||||
width: _,
|
||||
} => {}
|
||||
_ => return Err(FunctionError::InvalidSwitchType(selector)),
|
||||
_ => {
|
||||
return Err(FunctionError::InvalidSwitchType(selector)
|
||||
.with_span_handle(selector, context.expressions))
|
||||
}
|
||||
}
|
||||
self.select_cases.clear();
|
||||
for case in cases {
|
||||
if !self.select_cases.insert(case.value) {
|
||||
return Err(FunctionError::ConflictingSwitchCase(case.value));
|
||||
return Err(FunctionError::ConflictingSwitchCase(case.value)
|
||||
.with_span_static(
|
||||
case.body
|
||||
.span_iter()
|
||||
.next()
|
||||
.map_or(Default::default(), |(_, s)| *s),
|
||||
"conflicting switch arm here",
|
||||
));
|
||||
}
|
||||
}
|
||||
let pass_through_abilities = context.abilities
|
||||
@@ -448,19 +487,22 @@ impl super::Validator {
|
||||
}
|
||||
S::Break => {
|
||||
if !context.abilities.contains(ControlFlowAbility::BREAK) {
|
||||
return Err(FunctionError::BreakOutsideOfLoopOrSwitch);
|
||||
return Err(FunctionError::BreakOutsideOfLoopOrSwitch
|
||||
.with_span_static(span, "invalid break"));
|
||||
}
|
||||
finished = true;
|
||||
}
|
||||
S::Continue => {
|
||||
if !context.abilities.contains(ControlFlowAbility::CONTINUE) {
|
||||
return Err(FunctionError::ContinueOutsideOfLoop);
|
||||
return Err(FunctionError::ContinueOutsideOfLoop
|
||||
.with_span_static(span, "invalid continue"));
|
||||
}
|
||||
finished = true;
|
||||
}
|
||||
S::Return { value } => {
|
||||
if !context.abilities.contains(ControlFlowAbility::RETURN) {
|
||||
return Err(FunctionError::InvalidReturnSpot);
|
||||
return Err(FunctionError::InvalidReturnSpot
|
||||
.with_span_static(span, "invalid return"));
|
||||
}
|
||||
let value_ty = value
|
||||
.map(|expr| context.resolve_type(expr, &self.valid_expression_set))
|
||||
@@ -482,7 +524,13 @@ impl super::Validator {
|
||||
value_ty,
|
||||
expected_ty
|
||||
);
|
||||
return Err(FunctionError::InvalidReturnType(value));
|
||||
if let Some(handle) = value {
|
||||
return Err(FunctionError::InvalidReturnType(value)
|
||||
.with_span_handle(handle, context.expressions));
|
||||
} else {
|
||||
return Err(FunctionError::InvalidReturnType(value)
|
||||
.with_span_static(span, "invalid return"));
|
||||
}
|
||||
}
|
||||
finished = true;
|
||||
}
|
||||
@@ -495,25 +543,34 @@ impl super::Validator {
|
||||
S::Store { pointer, value } => {
|
||||
let mut current = pointer;
|
||||
loop {
|
||||
let _ = context.resolve_pointer_type(current)?;
|
||||
match *context.get_expression(current)? {
|
||||
let _ = context
|
||||
.resolve_pointer_type(current)
|
||||
.map_err(|e| e.with_span())?;
|
||||
match context.expressions[current] {
|
||||
crate::Expression::Access { base, .. }
|
||||
| crate::Expression::AccessIndex { base, .. } => current = base,
|
||||
crate::Expression::LocalVariable(_)
|
||||
| crate::Expression::GlobalVariable(_)
|
||||
| crate::Expression::FunctionArgument(_) => break,
|
||||
_ => return Err(FunctionError::InvalidStorePointer(current)),
|
||||
_ => {
|
||||
return Err(FunctionError::InvalidStorePointer(current)
|
||||
.with_span_handle(pointer, context.expressions))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let value_ty = context.resolve_type(value, &self.valid_expression_set)?;
|
||||
match *value_ty {
|
||||
Ti::Image { .. } | Ti::Sampler { .. } => {
|
||||
return Err(FunctionError::InvalidStoreValue(value));
|
||||
return Err(FunctionError::InvalidStoreValue(value)
|
||||
.with_span_handle(value, context.expressions));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
let good = match *context.resolve_pointer_type(pointer)? {
|
||||
let good = match *context
|
||||
.resolve_pointer_type(pointer)
|
||||
.map_err(|e| e.with_span())?
|
||||
{
|
||||
Ti::Pointer { base, class: _ } => match context.types[base].inner {
|
||||
Ti::Atomic { kind, width } => *value_ty == Ti::Scalar { kind, width },
|
||||
ref other => value_ty == other,
|
||||
@@ -533,7 +590,10 @@ impl super::Validator {
|
||||
_ => false,
|
||||
};
|
||||
if !good {
|
||||
return Err(FunctionError::InvalidStoreTypes { pointer, value });
|
||||
return Err(FunctionError::InvalidStoreTypes { pointer, value }
|
||||
.with_span()
|
||||
.with_handle(pointer, context.expressions)
|
||||
.with_handle(value, context.expressions));
|
||||
}
|
||||
}
|
||||
S::ImageStore {
|
||||
@@ -544,14 +604,15 @@ impl super::Validator {
|
||||
} => {
|
||||
//Note: this code uses a lot of `FunctionError::InvalidImageStore`,
|
||||
// and could probably be refactored.
|
||||
let var = match *context.get_expression(image)? {
|
||||
let var = match *context.get_expression(image).map_err(|e| e.with_span())? {
|
||||
crate::Expression::GlobalVariable(var_handle) => {
|
||||
&context.global_vars[var_handle]
|
||||
}
|
||||
_ => {
|
||||
return Err(FunctionError::InvalidImageStore(
|
||||
ExpressionError::ExpectedGlobalVariable,
|
||||
))
|
||||
)
|
||||
.with_span_handle(image, context.expressions))
|
||||
}
|
||||
};
|
||||
|
||||
@@ -571,13 +632,15 @@ impl super::Validator {
|
||||
ExpressionError::InvalidImageCoordinateType(
|
||||
dim, coordinate,
|
||||
),
|
||||
))
|
||||
)
|
||||
.with_span_handle(coordinate, context.expressions));
|
||||
}
|
||||
};
|
||||
if arrayed != array_index.is_some() {
|
||||
return Err(FunctionError::InvalidImageStore(
|
||||
ExpressionError::InvalidImageArrayIndex,
|
||||
));
|
||||
)
|
||||
.with_span_handle(coordinate, context.expressions));
|
||||
}
|
||||
if let Some(expr) = array_index {
|
||||
match *context.resolve_type(expr, &self.valid_expression_set)? {
|
||||
@@ -588,7 +651,8 @@ impl super::Validator {
|
||||
_ => {
|
||||
return Err(FunctionError::InvalidImageStore(
|
||||
ExpressionError::InvalidImageArrayIndexType(expr),
|
||||
))
|
||||
)
|
||||
.with_span_handle(expr, context.expressions));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -603,19 +667,24 @@ impl super::Validator {
|
||||
_ => {
|
||||
return Err(FunctionError::InvalidImageStore(
|
||||
ExpressionError::InvalidImageClass(class),
|
||||
))
|
||||
)
|
||||
.with_span_handle(image, context.expressions));
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return Err(FunctionError::InvalidImageStore(
|
||||
ExpressionError::ExpectedImageType(var.ty),
|
||||
))
|
||||
)
|
||||
.with_span()
|
||||
.with_handle(var.ty, context.types)
|
||||
.with_handle(image, context.expressions))
|
||||
}
|
||||
};
|
||||
|
||||
if *context.resolve_type(value, &self.valid_expression_set)? != value_ty {
|
||||
return Err(FunctionError::InvalidStoreValue(value));
|
||||
return Err(FunctionError::InvalidStoreValue(value)
|
||||
.with_span_handle(value, context.expressions));
|
||||
}
|
||||
}
|
||||
S::Call {
|
||||
@@ -624,7 +693,12 @@ impl super::Validator {
|
||||
result,
|
||||
} => match self.validate_call(function, arguments, result, context) {
|
||||
Ok(callee_stages) => stages &= callee_stages,
|
||||
Err(error) => return Err(FunctionError::InvalidCall { function, error }),
|
||||
Err(error) => {
|
||||
return Err(error.and_then(|error| {
|
||||
FunctionError::InvalidCall { function, error }
|
||||
.with_span_static(span, "invalid function call")
|
||||
}))
|
||||
}
|
||||
},
|
||||
S::Atomic {
|
||||
pointer,
|
||||
@@ -642,9 +716,9 @@ impl super::Validator {
|
||||
#[cfg(feature = "validate")]
|
||||
fn validate_block(
|
||||
&mut self,
|
||||
statements: &[crate::Statement],
|
||||
statements: &crate::Block,
|
||||
context: &BlockContext,
|
||||
) -> Result<BlockInfo, FunctionError> {
|
||||
) -> Result<BlockInfo, WithSpan<FunctionError>> {
|
||||
let base_expression_count = self.valid_expression_list.len();
|
||||
let info = self.validate_block_impl(statements, context)?;
|
||||
for handle in self.valid_expression_list.drain(base_expression_count..) {
|
||||
@@ -693,9 +767,11 @@ impl super::Validator {
|
||||
fun: &crate::Function,
|
||||
module: &crate::Module,
|
||||
mod_info: &ModuleInfo,
|
||||
) -> Result<FunctionInfo, FunctionError> {
|
||||
) -> Result<FunctionInfo, WithSpan<FunctionError>> {
|
||||
#[cfg(feature = "validate")]
|
||||
let mut info = mod_info.process_function(fun, module, self.flags)?;
|
||||
let mut info = mod_info
|
||||
.process_function(fun, module, self.flags)
|
||||
.map_err(WithSpan::into_other)?;
|
||||
|
||||
#[cfg(not(feature = "validate"))]
|
||||
let info = mod_info.process_function(fun, module, self.flags)?;
|
||||
@@ -703,10 +779,14 @@ impl super::Validator {
|
||||
#[cfg(feature = "validate")]
|
||||
for (var_handle, var) in fun.local_variables.iter() {
|
||||
self.validate_local_var(var, &module.types, &module.constants)
|
||||
.map_err(|error| FunctionError::LocalVariable {
|
||||
handle: var_handle,
|
||||
name: var.name.clone().unwrap_or_default(),
|
||||
error,
|
||||
.map_err(|error| {
|
||||
FunctionError::LocalVariable {
|
||||
handle: var_handle,
|
||||
name: var.name.clone().unwrap_or_default(),
|
||||
error,
|
||||
}
|
||||
.with_span_handle(var.ty, &module.types)
|
||||
.with_handle(var_handle, &fun.local_variables)
|
||||
})?;
|
||||
}
|
||||
|
||||
@@ -719,7 +799,8 @@ impl super::Validator {
|
||||
return Err(FunctionError::InvalidArgumentType {
|
||||
index,
|
||||
name: argument.name.clone().unwrap_or_default(),
|
||||
});
|
||||
}
|
||||
.with_span_handle(argument.ty, &module.types));
|
||||
}
|
||||
match module.types[argument.ty].inner.pointer_class() {
|
||||
Some(crate::StorageClass::Private)
|
||||
@@ -731,7 +812,8 @@ impl super::Validator {
|
||||
index,
|
||||
name: argument.name.clone().unwrap_or_default(),
|
||||
class: other,
|
||||
})
|
||||
}
|
||||
.with_span_handle(argument.ty, &module.types))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -753,7 +835,10 @@ impl super::Validator {
|
||||
&mod_info.functions,
|
||||
) {
|
||||
Ok(stages) => info.available_stages &= stages,
|
||||
Err(error) => return Err(FunctionError::Expression { handle, error }),
|
||||
Err(error) => {
|
||||
return Err(FunctionError::Expression { handle, error }
|
||||
.with_span_handle(handle, &fun.expressions))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user