usesuper::{
analyzer::{UniformityDisruptor, UniformityRequirements},
ExpressionError, FunctionInfo, ModuleInfo,
}; usecrate::span::WithSpan; usecrate::span::{AddSpan as _, MapErrWithSpan as _};
#[derive(Clone, Debug, thiserror::Error)] #[cfg_attr(test, derive(PartialEq))] pubenum CallError { #[error("Argument {index} expression is invalid")]
Argument {
index: usize,
source: ExpressionError,
}, #[error("Result expression {0:?} has already been introduced earlier")]
ResultAlreadyInScope(Handle<crate::Expression>), #[error("Result expression {0:?} is populated by multiple `Call` statements")]
ResultAlreadyPopulated(Handle<crate::Expression>), #[error("Result value is invalid")]
ResultValue(#[source] ExpressionError), #[error("Requires {required} arguments, but {seen} are provided")]
ArgumentCount { required: usize, seen: usize }, #[error("Argument {index} value {seen_expression:?} doesn't match the type {required:?}")]
ArgumentType {
index: usize,
required: Handle<crate::Type>,
seen_expression: Handle<crate::Expression>,
}, #[error("The emitted expression doesn't match the call")]
ExpressionMismatch(Option<Handle<crate::Expression>>),
}
#[derive(Clone, Debug, thiserror::Error)] #[cfg_attr(test, derive(PartialEq))] pubenum AtomicError { #[error("Pointer {0:?} to atomic is invalid.")]
InvalidPointer(Handle<crate::Expression>), #[error("Address space {0:?} is not supported.")]
InvalidAddressSpace(crate::AddressSpace), #[error("Operand {0:?} has invalid type.")]
InvalidOperand(Handle<crate::Expression>), #[error("Operator {0:?} is not supported.")]
InvalidOperator(crate::AtomicFunction), #[error("Result expression {0:?} is not an `AtomicResult` expression")]
InvalidResultExpression(Handle<crate::Expression>), #[error("Result expression {0:?} is marked as an `exchange`")]
ResultExpressionExchange(Handle<crate::Expression>), #[error("Result expression {0:?} is not marked as an `exchange`")]
ResultExpressionNotExchange(Handle<crate::Expression>), #[error("Result type for {0:?} doesn't match the statement")]
ResultTypeMismatch(Handle<crate::Expression>), #[error("Exchange operations must return a value")]
MissingReturnValue, #[error("Capability {0:?} is required")]
MissingCapability(super::Capabilities), #[error("Result expression {0:?} is populated by multiple `Atomic` statements")]
ResultAlreadyPopulated(Handle<crate::Expression>),
}
#[derive(Clone, Debug, thiserror::Error)] #[cfg_attr(test, derive(PartialEq))] pubenum SubgroupError { #[error("Operand {0:?} has invalid type.")]
InvalidOperand(Handle<crate::Expression>), #[error("Result type for {0:?} doesn't match the statement")]
ResultTypeMismatch(Handle<crate::Expression>), #[error("Support for subgroup operation {0:?} is required")]
UnsupportedOperation(super::SubgroupOperationSet), #[error("Unknown operation")]
UnknownOperation,
}
#[derive(Clone, Debug, thiserror::Error)] #[cfg_attr(test, derive(PartialEq))] pubenum LocalVariableError { #[error("Local variable has a type {0:?} that can't be stored in a local variable.")]
InvalidType(Handle<crate::Type>), #[error("Initializer doesn't match the variable type")]
InitializerType, #[error("Initializer is not a const or override expression")]
NonConstOrOverrideInitializer,
}
#[derive(Clone, Debug, thiserror::Error)] #[cfg_attr(test, derive(PartialEq))] pubenum FunctionError { #[error("Expression {handle:?} is invalid")]
Expression {
handle: Handle<crate::Expression>,
source: ExpressionError,
}, #[error("Expression {0:?} can't be introduced - it's already in scope")]
ExpressionAlreadyInScope(Handle<crate::Expression>), #[error("Local variable {handle:?} '{name}' is invalid")]
LocalVariable {
handle: Handle<crate::LocalVariable>,
name: String,
source: LocalVariableError,
}, #[error("Argument '{name}' at index {index} has a type that can't be passed into functions.")]
InvalidArgumentType { index: usize, name: String }, #[error("The function's given return type cannot be returned from functions")]
NonConstructibleReturnType, #[error("Argument '{name}' at index {index} is a pointer of space {space:?}, which can't be passed into functions.")]
InvalidArgumentPointerSpace {
index: usize,
name: String,
space: crate::AddressSpace,
}, #[error("There are instructions after `return`/`break`/`continue`")]
InstructionsAfterReturn, #[error("The `break` is used outside of a `loop` or `switch` context")]
BreakOutsideOfLoopOrSwitch, #[error("The `continue` is used outside of a `loop` context")]
ContinueOutsideOfLoop, #[error("The `return` is called within a `continuing` block")]
InvalidReturnSpot, #[error("The `return` value {0:?} does not match the function return value")]
InvalidReturnType(Option<Handle<crate::Expression>>), #[error("The `if` condition {0:?} is not a boolean scalar")]
InvalidIfType(Handle<crate::Expression>), #[error("The `switch` value {0:?} is not an integer scalar")]
InvalidSwitchType(Handle<crate::Expression>), #[error("Multiple `switch` cases for {0:?} are present")]
ConflictingSwitchCase(crate::SwitchValue), #[error("The `switch` contains cases with conflicting types")]
ConflictingCaseType, #[error("The `switch` is missing a `default` case")]
MissingDefaultCase, #[error("Multiple `default` cases are present")]
MultipleDefaultCases, #[error("The last `switch` case contains a `fallthrough`")]
LastCaseFallTrough, #[error("The pointer {0:?} doesn't relate to a valid destination for a store")]
InvalidStorePointer(Handle<crate::Expression>), #[error("The value {0:?} can not be stored")]
InvalidStoreValue(Handle<crate::Expression>), #[error("The type of {value:?} doesn't match the type stored in {pointer:?}")]
InvalidStoreTypes {
pointer: Handle<crate::Expression>,
value: Handle<crate::Expression>,
}, #[error("Image store parameters are invalid")]
InvalidImageStore(#[source] ExpressionError), #[error("Image atomic parameters are invalid")]
InvalidImageAtomic(#[source] ExpressionError), #[error("Image atomic function is invalid")]
InvalidImageAtomicFunction(crate::AtomicFunction), #[error("Image atomic value is invalid")]
InvalidImageAtomicValue(Handle<crate::Expression>), #[error("Call to {function:?} is invalid")]
InvalidCall {
function: Handle<crate::Function>, #[source]
error: CallError,
}, #[error("Atomic operation is invalid")]
InvalidAtomic(#[from] AtomicError), #[error("Ray Query {0:?} is not a local variable")]
InvalidRayQueryExpression(Handle<crate::Expression>), #[error("Acceleration structure {0:?} is not a matching expression")]
InvalidAccelerationStructure(Handle<crate::Expression>), #[error("Ray descriptor {0:?} is not a matching expression")]
InvalidRayDescriptor(Handle<crate::Expression>), #[error("Ray Query {0:?} does not have a matching type")]
InvalidRayQueryType(Handle<crate::Type>), #[error("Shader requires capability {0:?}")]
MissingCapability(super::Capabilities), #[error( "Required uniformity of control flow for {0:?} in {1:?} is not fulfilled because of {2:?}"
)]
NonUniformControlFlow(
UniformityRequirements,
Handle<crate::Expression>,
UniformityDisruptor,
), #[error("Functions that are not entry points cannot have `@location` or `@builtin` attributes on their arguments: \"{name}\" has attributes")]
PipelineInputRegularFunction { name: String }, #[error("Functions that are not entry points cannot have `@location` or `@builtin` attributes on their return value types")]
PipelineOutputRegularFunction, #[error("Required uniformity for WorkGroupUniformLoad is not fulfilled because of {0:?}")] // The actual load statement will be "pointed to" by the span
NonUniformWorkgroupUniformLoad(UniformityDisruptor), // This is only possible with a misbehaving frontend #[error("The expression {0:?} for a WorkGroupUniformLoad isn't a WorkgroupUniformLoadResult")]
WorkgroupUniformLoadExpressionMismatch(Handle<crate::Expression>), #[error("The expression {0:?} is not valid as a WorkGroupUniformLoad argument. It should be a Pointer in Workgroup address space")]
WorkgroupUniformLoadInvalidPointer(Handle<crate::Expression>), #[error("Subgroup operation is invalid")]
InvalidSubgroup(#[from] SubgroupError), #[error("Emit statement should not cover \"result\" expressions like {0:?}")]
EmitResult(Handle<crate::Expression>), #[error("Expression not visited by the appropriate statement")]
UnvisitedExpression(Handle<crate::Expression>),
}
bitflags::bitflags! { #[repr(transparent)] #[derive(Clone, Copy)] struct ControlFlowAbility: u8 { /// The control can return out of this block. constRETURN = 0x1; /// The control can break. constBREAK = 0x2; /// The control can continue. constCONTINUE = 0x4;
}
}
fn validate_atomic(
&mutself,
pointer: Handle<crate::Expression>,
fun: &crate::AtomicFunction,
value: Handle<crate::Expression>,
result: Option<Handle<crate::Expression>>,
span: crate::Span,
context: &BlockContext,
) -> Result<(), WithSpan<FunctionError>> { // The `pointer` operand must be a pointer to an atomic value. let pointer_inner = context.resolve_type(pointer, &self.valid_expression_set)?; letcrate::TypeInner::Pointer {
base: pointer_base,
space: pointer_space,
} = *pointer_inner else {
log::error!("Atomic operation on type {:?}", *pointer_inner); return Err(AtomicError::InvalidPointer(pointer)
.with_span_handle(pointer, context.expressions)
.into_other());
}; letcrate::TypeInner::Atomic(pointer_scalar) = context.types[pointer_base].inner else {
log::error!( "Atomic pointer to type {:?}",
context.types[pointer_base].inner
); return Err(AtomicError::InvalidPointer(pointer)
.with_span_handle(pointer, context.expressions)
.into_other());
};
// The `value` operand must be a scalar of the same type as the atomic. let value_inner = context.resolve_type(value, &self.valid_expression_set)?; letcrate::TypeInner::Scalar(value_scalar) = *value_inner else {
log::error!("Atomic operand type {:?}", *value_inner); return Err(AtomicError::InvalidOperand(value)
.with_span_handle(value, context.expressions)
.into_other());
}; if pointer_scalar != value_scalar {
log::error!("Atomic operand type {:?}", *value_inner); return Err(AtomicError::InvalidOperand(value)
.with_span_handle(value, context.expressions)
.into_other());
}
match pointer_scalar { // Check for the special restrictions on 64-bit atomic operations. // // We don't need to consider other widths here: this function has already checked // that `pointer`'s type is an `Atomic`, and `validate_type` has already checked // that `Atomic` type has a permitted scalar width. crate::Scalar::I64 | crate::Scalar::U64 => { // `Capabilities::SHADER_INT64_ATOMIC_ALL_OPS` enables all sorts of 64-bit // atomic operations. ifself
.capabilities
.contains(super::Capabilities::SHADER_INT64_ATOMIC_ALL_OPS)
{ // okay
} else { // `Capabilities::SHADER_INT64_ATOMIC_MIN_MAX` allows `Min` and // `Max` on operations in `Storage`, without a return value. if matches!(
*fun, crate::AtomicFunction::Min | crate::AtomicFunction::Max
) && matches!(pointer_space, crate::AddressSpace::Storage { .. })
&& result.is_none()
{ if !self
.capabilities
.contains(super::Capabilities::SHADER_INT64_ATOMIC_MIN_MAX)
{
log::error!("Int64 min-max atomic operations are not supported"); return Err(AtomicError::MissingCapability( super::Capabilities::SHADER_INT64_ATOMIC_MIN_MAX,
)
.with_span_handle(value, context.expressions)
.into_other());
}
} else { // Otherwise, we require the full 64-bit atomic capability.
log::error!("Int64 atomic operations are not supported"); return Err(AtomicError::MissingCapability( super::Capabilities::SHADER_INT64_ATOMIC_ALL_OPS,
)
.with_span_handle(value, context.expressions)
.into_other());
}
}
} // Check for the special restrictions on 32-bit floating-point atomic operations. crate::Scalar::F32 => { // `Capabilities::SHADER_FLOAT32_ATOMIC` allows 32-bit floating-point // atomic operations `Add`, `Subtract`, and `Exchange` // in the `Storage` address space. if !self
.capabilities
.contains(super::Capabilities::SHADER_FLOAT32_ATOMIC)
{
log::error!("Float32 atomic operations are not supported"); return Err(AtomicError::MissingCapability( super::Capabilities::SHADER_FLOAT32_ATOMIC,
)
.with_span_handle(value, context.expressions)
.into_other());
} if !matches!(
*fun, crate::AtomicFunction::Add
| crate::AtomicFunction::Subtract
| crate::AtomicFunction::Exchange { compare: None }
) {
log::error!("Float32 atomic operation {:?} is not supported", fun); return Err(AtomicError::InvalidOperator(*fun)
.with_span_handle(value, context.expressions)
.into_other());
} if !matches!(pointer_space, crate::AddressSpace::Storage { .. }) {
log::error!( "Float32 atomic operations are only supported in the Storage address space"
); return Err(AtomicError::InvalidAddressSpace(pointer_space)
.with_span_handle(value, context.expressions)
.into_other());
}
}
_ => {}
}
// The result expression must be appropriate to the operation. match result {
Some(result) => { // The `result` handle must refer to an `AtomicResult` expression. letcrate::Expression::AtomicResult {
ty: result_ty,
comparison,
} = context.expressions[result] else { return Err(AtomicError::InvalidResultExpression(result)
.with_span_handle(result, context.expressions)
.into_other());
};
// Note that this expression has been visited by the proper kind // of statement. if !self.needs_visit.remove(result) { return Err(AtomicError::ResultAlreadyPopulated(result)
.with_span_handle(result, context.expressions)
.into_other());
}
// The constraints on the result type depend on the atomic function. ifletcrate::AtomicFunction::Exchange {
compare: Some(compare),
} = *fun
{ // The comparison value must be a scalar of the same type as the // atomic we're operating on. let compare_inner =
context.resolve_type(compare, &self.valid_expression_set)?; if !compare_inner.equivalent(value_inner, context.types) {
log::error!( "Atomic exchange comparison has a different type from the value"
); return Err(AtomicError::InvalidOperand(compare)
.with_span_handle(compare, context.expressions)
.into_other());
}
// The result expression must be an `__atomic_compare_exchange_result` // struct whose `old_value` member is of the same type as the atomic // we're operating on. letcrate::TypeInner::Struct { ref members, .. } =
context.types[result_ty].inner else { return Err(AtomicError::ResultTypeMismatch(result)
.with_span_handle(result, context.expressions)
.into_other());
}; if !validate_atomic_compare_exchange_struct(
context.types,
members,
|ty: &crate::TypeInner| *ty == crate::TypeInner::Scalar(pointer_scalar),
) { return Err(AtomicError::ResultTypeMismatch(result)
.with_span_handle(result, context.expressions)
.into_other());
}
// The result expression must be for a comparison operation. if !comparison { return Err(AtomicError::ResultExpressionNotExchange(result)
.with_span_handle(result, context.expressions)
.into_other());
}
} else { // The result expression must be a scalar of the same type as the // atomic we're operating on. let result_inner = &context.types[result_ty].inner; if !result_inner.equivalent(value_inner, context.types) { return Err(AtomicError::ResultTypeMismatch(result)
.with_span_handle(result, context.expressions)
.into_other());
}
// The result expression must not be for a comparison. if comparison { return Err(AtomicError::ResultExpressionExchange(result)
.with_span_handle(result, context.expressions)
.into_other());
}
} self.emit_expression(result, context)?;
}
None => { // Exchange operations must always produce a value. ifletcrate::AtomicFunction::Exchange { compare: None } = *fun {
log::error!("Atomic exchange's value is unused"); return Err(AtomicError::MissingReturnValue
.with_span_static(span, "atomic exchange operation")
.into_other());
}
}
}
iflet Some(space) = pointer_ty.pointer_space() { if !space.access().contains(crate::StorageAccess::STORE) { return Err(FunctionError::InvalidStorePointer(pointer)
.with_span_static(
context.expressions.get_span(pointer), "writing to this location is not permitted",
));
}
}
}
S::ImageStore {
image,
coordinate,
array_index,
value,
} => { //Note: this code uses a lot of `FunctionError::InvalidImageStore`, // and could probably be refactored. let global_var; let image_ty; match *context.get_expression(image) { crate::Expression::GlobalVariable(var_handle) => {
global_var = &context.global_vars[var_handle];
image_ty = global_var.ty;
} // The `image` operand is indexing into a binding array, // so punch through the `Access`* expression and look at // the global behind it. crate::Expression::Access { base, .. }
| crate::Expression::AccessIndex { base, .. } => { letcrate::Expression::GlobalVariable(var_handle) =
*context.get_expression(base) else { return Err(FunctionError::InvalidImageStore(
ExpressionError::ExpectedGlobalVariable,
)
.with_span_handle(image, context.expressions));
};
global_var = &context.global_vars[var_handle];
// The global variable must be a binding array. let Ti::BindingArray { base, .. } = context.types[global_var.ty].inner else { return Err(FunctionError::InvalidImageStore(
ExpressionError::ExpectedBindingArrayType(global_var.ty),
)
.with_span_handle(global_var.ty, context.types));
};
// The `image` operand must be an `Image`. let Ti::Image {
class,
arrayed,
dim,
} = context.types[image_ty].inner else { return Err(FunctionError::InvalidImageStore(
ExpressionError::ExpectedImageType(global_var.ty),
)
.with_span()
.with_handle(global_var.ty, context.types)
.with_handle(image, context.expressions));
};
// It had better be a storage image, since we're writing to it. letcrate::ImageClass::Storage { format, .. } = class else { return Err(FunctionError::InvalidImageStore(
ExpressionError::InvalidImageClass(class),
)
.with_span_handle(image, context.expressions));
};
// The `coordinate` operand must be a vector of the appropriate size. if !context
.resolve_type(coordinate, &self.valid_expression_set)?
.image_storage_coordinates()
.is_some_and(|coord_dim| coord_dim == dim)
{ return Err(FunctionError::InvalidImageStore(
ExpressionError::InvalidImageCoordinateType(dim, coordinate),
)
.with_span_handle(coordinate, context.expressions));
}
// The `array_index` operand should be present if and only if // the image itself is arrayed. if arrayed != array_index.is_some() { return Err(FunctionError::InvalidImageStore(
ExpressionError::InvalidImageArrayIndex,
)
.with_span_handle(coordinate, context.expressions));
}
// If present, `array_index` must be a scalar integer type. iflet Some(expr) = array_index { if !matches!(
*context.resolve_type(expr, &self.valid_expression_set)?,
Ti::Scalar(crate::Scalar {
kind: crate::ScalarKind::Sint | crate::ScalarKind::Uint,
width: _,
})
) { return Err(FunctionError::InvalidImageStore(
ExpressionError::InvalidImageArrayIndexType(expr),
)
.with_span_handle(expr, context.expressions));
}
}
let value_ty = crate::TypeInner::Vector {
size: crate::VectorSize::Quad,
scalar: format.into(),
};
// The value we're writing had better match the scalar type // for `image`'s format. if *context.resolve_type(value, &self.valid_expression_set)? != value_ty { return Err(FunctionError::InvalidStoreValue(value)
.with_span_handle(value, context.expressions));
}
}
S::Call {
function, ref arguments,
result,
} => matchself.validate_call(function, arguments, result, context) {
Ok(callee_stages) => stages &= callee_stages,
Err(error) => { return Err(error.and_then(|error| {
FunctionError::InvalidCall { function, error }
.with_span_static(span, "invalid function call")
}))
}
},
S::Atomic {
pointer, ref fun,
value,
result,
} => { self.validate_atomic(pointer, fun, value, result, span, context)?;
}
S::ImageAtomic {
image,
coordinate,
array_index,
fun,
value,
} => { let var = match *context.get_expression(image) { crate::Expression::GlobalVariable(var_handle) => {
&context.global_vars[var_handle]
} // We're looking at a binding index situation, so punch through the index and look at the global behind it. crate::Expression::Access { base, .. }
| crate::Expression::AccessIndex { base, .. } => { match *context.get_expression(base) { crate::Expression::GlobalVariable(var_handle) => {
&context.global_vars[var_handle]
}
_ => { return Err(FunctionError::InvalidImageAtomic(
ExpressionError::ExpectedGlobalVariable,
)
.with_span_handle(image, context.expressions))
}
}
}
_ => { return Err(FunctionError::InvalidImageAtomic(
ExpressionError::ExpectedGlobalVariable,
)
.with_span_handle(image, context.expressions))
}
};
// Punch through a binding array to get the underlying type let global_ty = match context.types[var.ty].inner {
Ti::BindingArray { base, .. } => &context.types[base].inner, ref inner => inner,
};
self.valid_expression_set.clear_for_arena(&fun.expressions); self.valid_expression_list.clear(); self.needs_visit.clear_for_arena(&fun.expressions); for (handle, expr) in fun.expressions.iter() { if expr.needs_pre_emit() { self.valid_expression_set.insert(handle);
} ifself.flags.contains(super::ValidationFlags::EXPRESSIONS) { // Mark expressions that need to be visited by a particular kind of // statement. ifletcrate::Expression::CallResult(_) | crate::Expression::AtomicResult { .. } =
*expr
{ self.needs_visit.insert(handle);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.