| 1 |
use alloc::{format, string::String}; |
| 2 |
|
| 3 |
use super::validate_atomic_compare_exchange_struct; |
| 4 |
use super::{ |
| 5 |
analyzer::{UniformityDisruptor, UniformityRequirements}, |
| 6 |
ExpressionError, FunctionInfo, ModuleInfo, |
| 7 |
}; |
| 8 |
use crate::arena::{Arena, UniqueArena}; |
| 9 |
use crate::arena::{Handle, HandleSet}; |
| 10 |
use crate::proc::TypeResolution; |
| 11 |
use crate::span::WithSpan; |
| 12 |
use crate::span::{AddSpan as _, MapErrWithSpan as _}; |
| 13 |
|
| 14 |
#[derive(Clone, Debug, thiserror::Error)] |
| 15 |
#[cfg_attr(test, derive(PartialEq))] |
| 16 |
pub enum CallError { |
| 17 |
#[error("Argument {index} expression is invalid")] |
| 18 |
Argument { |
| 19 |
index: usize, |
| 20 |
source: ExpressionError, |
| 21 |
}, |
| 22 |
#[error("Result expression {0:?} has already been introduced earlier")] |
| 23 |
ResultAlreadyInScope(Handle<crate::Expression>), |
| 24 |
#[error("Result expression {0:?} is populated by multiple `Call` statements")] |
| 25 |
ResultAlreadyPopulated(Handle<crate::Expression>), |
| 26 |
#[error("Requires {required} arguments, but {seen} are provided")] |
| 27 |
ArgumentCount { required: usize, seen: usize }, |
| 28 |
#[error("Argument {index} value {seen_expression:?} doesn't match the type {required:?}")] |
| 29 |
ArgumentType { |
| 30 |
index: usize, |
| 31 |
required: Handle<crate::Type>, |
| 32 |
seen_expression: Handle<crate::Expression>, |
| 33 |
}, |
| 34 |
#[error("The emitted expression doesn't match the call")] |
| 35 |
ExpressionMismatch(Option<Handle<crate::Expression>>), |
| 36 |
} |
| 37 |
|
| 38 |
#[derive(Clone, Debug, thiserror::Error)] |
| 39 |
#[cfg_attr(test, derive(PartialEq))] |
| 40 |
pub enum AtomicError { |
| 41 |
#[error("Pointer {0:?} to atomic is invalid.")] |
| 42 |
InvalidPointer(Handle<crate::Expression>), |
| 43 |
#[error("Address space {0:?} is not supported.")] |
| 44 |
InvalidAddressSpace(crate::AddressSpace), |
| 45 |
#[error("Operand {0:?} has invalid type.")] |
| 46 |
InvalidOperand(Handle<crate::Expression>), |
| 47 |
#[error("Operator {0:?} is not supported.")] |
| 48 |
InvalidOperator(crate::AtomicFunction), |
| 49 |
#[error("Result expression {0:?} is not an `AtomicResult` expression")] |
| 50 |
InvalidResultExpression(Handle<crate::Expression>), |
| 51 |
#[error("Result expression {0:?} is marked as an `exchange`")] |
| 52 |
ResultExpressionExchange(Handle<crate::Expression>), |
| 53 |
#[error("Result expression {0:?} is not marked as an `exchange`")] |
| 54 |
ResultExpressionNotExchange(Handle<crate::Expression>), |
| 55 |
#[error("Result type for {0:?} doesn't match the statement")] |
| 56 |
ResultTypeMismatch(Handle<crate::Expression>), |
| 57 |
#[error("Exchange operations must return a value")] |
| 58 |
MissingReturnValue, |
| 59 |
#[error("Capability {0:?} is required")] |
| 60 |
MissingCapability(super::Capabilities), |
| 61 |
#[error("Result expression {0:?} is populated by multiple `Atomic` statements")] |
| 62 |
ResultAlreadyPopulated(Handle<crate::Expression>), |
| 63 |
} |
| 64 |
|
| 65 |
#[derive(Clone, Debug, thiserror::Error)] |
| 66 |
#[cfg_attr(test, derive(PartialEq))] |
| 67 |
pub enum SubgroupError { |
| 68 |
#[error("Operand {0:?} has invalid type.")] |
| 69 |
InvalidOperand(Handle<crate::Expression>), |
| 70 |
#[error("Result type for {0:?} doesn't match the statement")] |
| 71 |
ResultTypeMismatch(Handle<crate::Expression>), |
| 72 |
#[error("Support for subgroup operation {0:?} is required")] |
| 73 |
UnsupportedOperation(super::SubgroupOperationSet), |
| 74 |
#[error("Unknown operation")] |
| 75 |
UnknownOperation, |
| 76 |
} |
| 77 |
|
| 78 |
#[derive(Clone, Debug, thiserror::Error)] |
| 79 |
#[cfg_attr(test, derive(PartialEq))] |
| 80 |
pub enum LocalVariableError { |
| 81 |
#[error("Local variable has a type {0:?} that can't be stored in a local variable.")] |
| 82 |
InvalidType(Handle<crate::Type>), |
| 83 |
#[error("Initializer doesn't match the variable type")] |
| 84 |
InitializerType, |
| 85 |
#[error("Initializer is not a const or override expression")] |
| 86 |
NonConstOrOverrideInitializer, |
| 87 |
} |
| 88 |
|
| 89 |
#[derive(Clone, Debug, thiserror::Error)] |
| 90 |
#[cfg_attr(test, derive(PartialEq))] |
| 91 |
pub enum FunctionError { |
| 92 |
#[error("Expression {handle:?} is invalid")] |
| 93 |
Expression { |
| 94 |
handle: Handle<crate::Expression>, |
| 95 |
source: ExpressionError, |
| 96 |
}, |
| 97 |
#[error("Expression {0:?} can't be introduced - it's already in scope")] |
| 98 |
ExpressionAlreadyInScope(Handle<crate::Expression>), |
| 99 |
#[error("Local variable {handle:?} '{name}' is invalid")] |
| 100 |
LocalVariable { |
| 101 |
handle: Handle<crate::LocalVariable>, |
| 102 |
name: String, |
| 103 |
source: LocalVariableError, |
| 104 |
}, |
| 105 |
#[error("Argument '{name}' at index {index} has a type that can't be passed into functions.")] |
| 106 |
InvalidArgumentType { index: usize, name: String }, |
| 107 |
#[error("The function's given return type cannot be returned from functions")] |
| 108 |
NonConstructibleReturnType, |
| 109 |
#[error("Argument '{name}' at index {index} is a pointer of space {space:?}, which can't be passed into functions.")] |
| 110 |
InvalidArgumentPointerSpace { |
| 111 |
index: usize, |
| 112 |
name: String, |
| 113 |
space: crate::AddressSpace, |
| 114 |
}, |
| 115 |
#[error("There are instructions after `return`/`break`/`continue`")] |
| 116 |
InstructionsAfterReturn, |
| 117 |
#[error("The `break` is used outside of a `loop` or `switch` context")] |
| 118 |
BreakOutsideOfLoopOrSwitch, |
| 119 |
#[error("The `continue` is used outside of a `loop` context")] |
| 120 |
ContinueOutsideOfLoop, |
| 121 |
#[error("The `return` is called within a `continuing` block")] |
| 122 |
InvalidReturnSpot, |
| 123 |
#[error("The `return` expression {expression:?} does not match the declared return type {expected_ty:?}")] |
| 124 |
InvalidReturnType { |
| 125 |
expression: Option<Handle<crate::Expression>>, |
| 126 |
expected_ty: Option<Handle<crate::Type>>, |
| 127 |
}, |
| 128 |
#[error("The `if` condition {0:?} is not a boolean scalar")] |
| 129 |
InvalidIfType(Handle<crate::Expression>), |
| 130 |
#[error("The `switch` value {0:?} is not an integer scalar")] |
| 131 |
InvalidSwitchType(Handle<crate::Expression>), |
| 132 |
#[error("Multiple `switch` cases for {0:?} are present")] |
| 133 |
ConflictingSwitchCase(crate::SwitchValue), |
| 134 |
#[error("The `switch` contains cases with conflicting types")] |
| 135 |
ConflictingCaseType, |
| 136 |
#[error("The `switch` is missing a `default` case")] |
| 137 |
MissingDefaultCase, |
| 138 |
#[error("Multiple `default` cases are present")] |
| 139 |
MultipleDefaultCases, |
| 140 |
#[error("The last `switch` case contains a `fallthrough`")] |
| 141 |
LastCaseFallTrough, |
| 142 |
#[error("The pointer {0:?} doesn't relate to a valid destination for a store")] |
| 143 |
InvalidStorePointer(Handle<crate::Expression>), |
| 144 |
#[error("Image store texture parameter type mismatch")] |
| 145 |
InvalidStoreTexture { |
| 146 |
actual: Handle<crate::Expression>, |
| 147 |
actual_ty: crate::TypeInner, |
| 148 |
}, |
| 149 |
#[error("Image store value parameter type mismatch")] |
| 150 |
InvalidStoreValue { |
| 151 |
actual: Handle<crate::Expression>, |
| 152 |
actual_ty: crate::TypeInner, |
| 153 |
expected_ty: crate::TypeInner, |
| 154 |
}, |
| 155 |
#[error("The type of {value:?} doesn't match the type stored in {pointer:?}")] |
| 156 |
InvalidStoreTypes { |
| 157 |
pointer: Handle<crate::Expression>, |
| 158 |
value: Handle<crate::Expression>, |
| 159 |
}, |
| 160 |
#[error("Image store parameters are invalid")] |
| 161 |
InvalidImageStore(#[source] ExpressionError), |
| 162 |
#[error("Image atomic parameters are invalid")] |
| 163 |
InvalidImageAtomic(#[source] ExpressionError), |
| 164 |
#[error("Image atomic function is invalid")] |
| 165 |
InvalidImageAtomicFunction(crate::AtomicFunction), |
| 166 |
#[error("Image atomic value is invalid")] |
| 167 |
InvalidImageAtomicValue(Handle<crate::Expression>), |
| 168 |
#[error("Call to {function:?} is invalid")] |
| 169 |
InvalidCall { |
| 170 |
function: Handle<crate::Function>, |
| 171 |
#[source] |
| 172 |
error: CallError, |
| 173 |
}, |
| 174 |
#[error("Atomic operation is invalid")] |
| 175 |
InvalidAtomic(#[from] AtomicError), |
| 176 |
#[error("Ray Query {0:?} is not a local variable")] |
| 177 |
InvalidRayQueryExpression(Handle<crate::Expression>), |
| 178 |
#[error("Acceleration structure {0:?} is not a matching expression")] |
| 179 |
InvalidAccelerationStructure(Handle<crate::Expression>), |
| 180 |
#[error( |
| 181 |
"Acceleration structure {0:?} is missing flag vertex_return while Ray Query {1:?} does" |
| 182 |
)] |
| 183 |
MissingAccelerationStructureVertexReturn(Handle<crate::Expression>, Handle<crate::Expression>), |
| 184 |
#[error("Ray Query {0:?} is missing flag vertex_return")] |
| 185 |
MissingRayQueryVertexReturn(Handle<crate::Expression>), |
| 186 |
#[error("Ray descriptor {0:?} is not a matching expression")] |
| 187 |
InvalidRayDescriptor(Handle<crate::Expression>), |
| 188 |
#[error("Ray Query {0:?} does not have a matching type")] |
| 189 |
InvalidRayQueryType(Handle<crate::Type>), |
| 190 |
#[error("Hit distance {0:?} must be an f32")] |
| 191 |
InvalidHitDistanceType(Handle<crate::Expression>), |
| 192 |
#[error("Shader requires capability {0:?}")] |
| 193 |
MissingCapability(super::Capabilities), |
| 194 |
#[error( |
| 195 |
"Required uniformity of control flow for {0:?} in {1:?} is not fulfilled because of {2:?}" |
| 196 |
)] |
| 197 |
NonUniformControlFlow( |
| 198 |
UniformityRequirements, |
| 199 |
Handle<crate::Expression>, |
| 200 |
UniformityDisruptor, |
| 201 |
), |
| 202 |
#[error("Functions that are not entry points cannot have `@location` or `@builtin` attributes on their arguments: \"{name}\" has attributes")] |
| 203 |
PipelineInputRegularFunction { name: String }, |
| 204 |
#[error("Functions that are not entry points cannot have `@location` or `@builtin` attributes on their return value types")] |
| 205 |
PipelineOutputRegularFunction, |
| 206 |
#[error("Required uniformity for WorkGroupUniformLoad is not fulfilled because of {0:?}")] |
| 207 |
// The actual load statement will be "pointed to" by the span |
| 208 |
NonUniformWorkgroupUniformLoad(UniformityDisruptor), |
| 209 |
// This is only possible with a misbehaving frontend |
| 210 |
#[error("The expression {0:?} for a WorkGroupUniformLoad isn't a WorkgroupUniformLoadResult")] |
| 211 |
WorkgroupUniformLoadExpressionMismatch(Handle<crate::Expression>), |
| 212 |
#[error("The expression {0:?} is not valid as a WorkGroupUniformLoad argument. It should be a Pointer in Workgroup address space")] |
| 213 |
WorkgroupUniformLoadInvalidPointer(Handle<crate::Expression>), |
| 214 |
#[error("Subgroup operation is invalid")] |
| 215 |
InvalidSubgroup(#[from] SubgroupError), |
| 216 |
#[error("Emit statement should not cover \"result\" expressions like {0:?}")] |
| 217 |
EmitResult(Handle<crate::Expression>), |
| 218 |
#[error("Expression not visited by the appropriate statement")] |
| 219 |
UnvisitedExpression(Handle<crate::Expression>), |
| 220 |
} |
| 221 |
|
| 222 |
bitflags::bitflags! { |
| 223 |
#[repr(transparent)] |
| 224 |
#[derive(Clone, Copy)] |
| 225 |
struct ControlFlowAbility: u8 { |
| 226 |
/// The control can return out of this block. |
| 227 |
const RETURN = 0x1; |
| 228 |
/// The control can break. |
| 229 |
const BREAK = 0x2; |
| 230 |
/// The control can continue. |
| 231 |
const CONTINUE = 0x4; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
struct BlockInfo { |
| 236 |
stages: super::ShaderStages, |
| 237 |
finished: bool, |
| 238 |
} |
| 239 |
|
| 240 |
struct BlockContext<'a> { |
| 241 |
abilities: ControlFlowAbility, |
| 242 |
info: &'a FunctionInfo, |
| 243 |
expressions: &'a Arena<crate::Expression>, |
| 244 |
types: &'a UniqueArena<crate::Type>, |
| 245 |
local_vars: &'a Arena<crate::LocalVariable>, |
| 246 |
global_vars: &'a Arena<crate::GlobalVariable>, |
| 247 |
functions: &'a Arena<crate::Function>, |
| 248 |
special_types: &'a crate::SpecialTypes, |
| 249 |
prev_infos: &'a [FunctionInfo], |
| 250 |
return_type: Option<Handle<crate::Type>>, |
| 251 |
} |
| 252 |
|
| 253 |
impl<'a> BlockContext<'a> { |
| 254 |
fn new( |
| 255 |
fun: &'a crate::Function, |
| 256 |
module: &'a crate::Module, |
| 257 |
info: &'a FunctionInfo, |
| 258 |
prev_infos: &'a [FunctionInfo], |
| 259 |
) -> Self { |
| 260 |
Self { |
| 261 |
abilities: ControlFlowAbility::RETURN, |
| 262 |
info, |
| 263 |
expressions: &fun.expressions, |
| 264 |
types: &module.types, |
| 265 |
local_vars: &fun.local_variables, |
| 266 |
global_vars: &module.global_variables, |
| 267 |
functions: &module.functions, |
| 268 |
special_types: &module.special_types, |
| 269 |
prev_infos, |
| 270 |
return_type: fun.result.as_ref().map(|fr| fr.ty), |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
const fn with_abilities(&self, abilities: ControlFlowAbility) -> Self { |
| 275 |
BlockContext { abilities, ..*self } |
| 276 |
} |
| 277 |
|
| 278 |
fn get_expression(&self, handle: Handle<crate::Expression>) -> &'a crate::Expression { |
| 279 |
&self.expressions[handle] |
| 280 |
} |
| 281 |
|
| 282 |
fn resolve_type_impl( |
| 283 |
&self, |
| 284 |
handle: Handle<crate::Expression>, |
| 285 |
valid_expressions: &HandleSet<crate::Expression>, |
| 286 |
) -> Result<&TypeResolution, WithSpan<ExpressionError>> { |
| 287 |
if !valid_expressions.contains(handle) { |
| 288 |
Err(ExpressionError::NotInScope.with_span_handle(handle, self.expressions)) |
| 289 |
} else { |
| 290 |
Ok(&self.info[handle].ty) |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
fn resolve_type( |
| 295 |
&self, |
| 296 |
handle: Handle<crate::Expression>, |
| 297 |
valid_expressions: &HandleSet<crate::Expression>, |
| 298 |
) -> Result<&TypeResolution, WithSpan<FunctionError>> { |
| 299 |
self.resolve_type_impl(handle, valid_expressions) |
| 300 |
.map_err_inner(|source| FunctionError::Expression { handle, source }.with_span()) |
| 301 |
} |
| 302 |
|
| 303 |
fn resolve_type_inner( |
| 304 |
&self, |
| 305 |
handle: Handle<crate::Expression>, |
| 306 |
valid_expressions: &HandleSet<crate::Expression>, |
| 307 |
) -> Result<&crate::TypeInner, WithSpan<FunctionError>> { |
| 308 |
self.resolve_type(handle, valid_expressions) |
| 309 |
.map(|tr| tr.inner_with(self.types)) |
| 310 |
} |
| 311 |
|
| 312 |
fn resolve_pointer_type(&self, handle: Handle<crate::Expression>) -> &crate::TypeInner { |
| 313 |
self.info[handle].ty.inner_with(self.types) |
| 314 |
} |
| 315 |
|
| 316 |
fn compare_types(&self, lhs: &TypeResolution, rhs: &TypeResolution) -> bool { |
| 317 |
crate::proc::compare_types(lhs, rhs, self.types) |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
impl super::Validator { |
| 322 |
fn validate_call( |
| 323 |
&mut self, |
| 324 |
function: Handle<crate::Function>, |
| 325 |
arguments: &[Handle<crate::Expression>], |
| 326 |
result: Option<Handle<crate::Expression>>, |
| 327 |
context: &BlockContext, |
| 328 |
) -> Result<super::ShaderStages, WithSpan<CallError>> { |
| 329 |
let fun = &context.functions[function]; |
| 330 |
if fun.arguments.len() != arguments.len() { |
| 331 |
return Err(CallError::ArgumentCount { |
| 332 |
required: fun.arguments.len(), |
| 333 |
seen: arguments.len(), |
| 334 |
} |
| 335 |
.with_span()); |
| 336 |
} |
| 337 |
for (index, (arg, &expr)) in fun.arguments.iter().zip(arguments).enumerate() { |
| 338 |
let ty = context |
| 339 |
.resolve_type_impl(expr, &self.valid_expression_set) |
| 340 |
.map_err_inner(|source| { |
| 341 |
CallError::Argument { index, source } |
| 342 |
.with_span_handle(expr, context.expressions) |
| 343 |
})?; |
| 344 |
if !context.compare_types(&TypeResolution::Handle(arg.ty), ty) { |
| 345 |
return Err(CallError::ArgumentType { |
| 346 |
index, |
| 347 |
required: arg.ty, |
| 348 |
seen_expression: expr, |
| 349 |
} |
| 350 |
.with_span_handle(expr, context.expressions)); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
if let Some(expr) = result { |
| 355 |
if self.valid_expression_set.insert(expr) { |
| 356 |
self.valid_expression_list.push(expr); |
| 357 |
} else { |
| 358 |
return Err(CallError::ResultAlreadyInScope(expr) |
| 359 |
.with_span_handle(expr, context.expressions)); |
| 360 |
} |
| 361 |
match context.expressions[expr] { |
| 362 |
crate::Expression::CallResult(callee) |
| 363 |
if fun.result.is_some() && callee == function => |
| 364 |
{ |
| 365 |
if !self.needs_visit.remove(expr) { |
| 366 |
return Err(CallError::ResultAlreadyPopulated(expr) |
| 367 |
.with_span_handle(expr, context.expressions)); |
| 368 |
} |
| 369 |
} |
| 370 |
_ => { |
| 371 |
return Err(CallError::ExpressionMismatch(result) |
| 372 |
.with_span_handle(expr, context.expressions)) |
| 373 |
} |
| 374 |
} |
| 375 |
} else if fun.result.is_some() { |
| 376 |
return Err(CallError::ExpressionMismatch(result).with_span()); |
| 377 |
} |
| 378 |
|
| 379 |
let callee_info = &context.prev_infos[function.index()]; |
| 380 |
Ok(callee_info.available_stages) |
| 381 |
} |
| 382 |
|
| 383 |
fn emit_expression( |
| 384 |
&mut self, |
| 385 |
handle: Handle<crate::Expression>, |
| 386 |
context: &BlockContext, |
| 387 |
) -> Result<(), WithSpan<FunctionError>> { |
| 388 |
if self.valid_expression_set.insert(handle) { |
| 389 |
self.valid_expression_list.push(handle); |
| 390 |
Ok(()) |
| 391 |
} else { |
| 392 |
Err(FunctionError::ExpressionAlreadyInScope(handle) |
| 393 |
.with_span_handle(handle, context.expressions)) |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
fn validate_atomic( |
| 398 |
&mut self, |
| 399 |
pointer: Handle<crate::Expression>, |
| 400 |
fun: &crate::AtomicFunction, |
| 401 |
value: Handle<crate::Expression>, |
| 402 |
result: Option<Handle<crate::Expression>>, |
| 403 |
span: crate::Span, |
| 404 |
context: &BlockContext, |
| 405 |
) -> Result<(), WithSpan<FunctionError>> { |
| 406 |
// The `pointer` operand must be a pointer to an atomic value. |
| 407 |
let pointer_inner = context.resolve_type_inner(pointer, &self.valid_expression_set)?; |
| 408 |
let crate::TypeInner::Pointer { |
| 409 |
base: pointer_base, |
| 410 |
space: pointer_space, |
| 411 |
} = *pointer_inner |
| 412 |
else { |
| 413 |
log::error!("Atomic operation on type {:?}", *pointer_inner); |
| 414 |
return Err(AtomicError::InvalidPointer(pointer) |
| 415 |
.with_span_handle(pointer, context.expressions) |
| 416 |
.into_other()); |
| 417 |
}; |
| 418 |
let crate::TypeInner::Atomic(pointer_scalar) = context.types[pointer_base].inner else { |
| 419 |
log::error!( |
| 420 |
"Atomic pointer to type {:?}", |
| 421 |
context.types[pointer_base].inner |
| 422 |
); |
| 423 |
return Err(AtomicError::InvalidPointer(pointer) |
| 424 |
.with_span_handle(pointer, context.expressions) |
| 425 |
.into_other()); |
| 426 |
}; |
| 427 |
|
| 428 |
// The `value` operand must be a scalar of the same type as the atomic. |
| 429 |
let value_inner = context.resolve_type_inner(value, &self.valid_expression_set)?; |
| 430 |
let crate::TypeInner::Scalar(value_scalar) = *value_inner else { |
| 431 |
log::error!("Atomic operand type {:?}", *value_inner); |
| 432 |
return Err(AtomicError::InvalidOperand(value) |
| 433 |
.with_span_handle(value, context.expressions) |
| 434 |
.into_other()); |
| 435 |
}; |
| 436 |
if pointer_scalar != value_scalar { |
| 437 |
log::error!("Atomic operand type {:?}", *value_inner); |
| 438 |
return Err(AtomicError::InvalidOperand(value) |
| 439 |
.with_span_handle(value, context.expressions) |
| 440 |
.into_other()); |
| 441 |
} |
| 442 |
|
| 443 |
match pointer_scalar { |
| 444 |
// Check for the special restrictions on 64-bit atomic operations. |
| 445 |
// |
| 446 |
// We don't need to consider other widths here: this function has already checked |
| 447 |
// that `pointer`'s type is an `Atomic`, and `validate_type` has already checked |
| 448 |
// that `Atomic` type has a permitted scalar width. |
| 449 |
crate::Scalar::I64 | crate::Scalar::U64 => { |
| 450 |
// `Capabilities::SHADER_INT64_ATOMIC_ALL_OPS` enables all sorts of 64-bit |
| 451 |
// atomic operations. |
| 452 |
if self |
| 453 |
.capabilities |
| 454 |
.contains(super::Capabilities::SHADER_INT64_ATOMIC_ALL_OPS) |
| 455 |
{ |
| 456 |
// okay |
| 457 |
} else { |
| 458 |
// `Capabilities::SHADER_INT64_ATOMIC_MIN_MAX` allows `Min` and |
| 459 |
// `Max` on operations in `Storage`, without a return value. |
| 460 |
if matches!( |
| 461 |
*fun, |
| 462 |
crate::AtomicFunction::Min | crate::AtomicFunction::Max |
| 463 |
) && matches!(pointer_space, crate::AddressSpace::Storage { .. }) |
| 464 |
&& result.is_none() |
| 465 |
{ |
| 466 |
if !self |
| 467 |
.capabilities |
| 468 |
.contains(super::Capabilities::SHADER_INT64_ATOMIC_MIN_MAX) |
| 469 |
{ |
| 470 |
log::error!("Int64 min-max atomic operations are not supported"); |
| 471 |
return Err(AtomicError::MissingCapability( |
| 472 |
super::Capabilities::SHADER_INT64_ATOMIC_MIN_MAX, |
| 473 |
) |
| 474 |
.with_span_handle(value, context.expressions) |
| 475 |
.into_other()); |
| 476 |
} |
| 477 |
} else { |
| 478 |
// Otherwise, we require the full 64-bit atomic capability. |
| 479 |
log::error!("Int64 atomic operations are not supported"); |
| 480 |
return Err(AtomicError::MissingCapability( |
| 481 |
super::Capabilities::SHADER_INT64_ATOMIC_ALL_OPS, |
| 482 |
) |
| 483 |
.with_span_handle(value, context.expressions) |
| 484 |
.into_other()); |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
// Check for the special restrictions on 32-bit floating-point atomic operations. |
| 489 |
crate::Scalar::F32 => { |
| 490 |
// `Capabilities::SHADER_FLOAT32_ATOMIC` allows 32-bit floating-point |
| 491 |
// atomic operations `Add`, `Subtract`, and `Exchange` |
| 492 |
// in the `Storage` address space. |
| 493 |
if !self |
| 494 |
.capabilities |
| 495 |
.contains(super::Capabilities::SHADER_FLOAT32_ATOMIC) |
| 496 |
{ |
| 497 |
log::error!("Float32 atomic operations are not supported"); |
| 498 |
return Err(AtomicError::MissingCapability( |
| 499 |
super::Capabilities::SHADER_FLOAT32_ATOMIC, |
| 500 |
) |
| 501 |
.with_span_handle(value, context.expressions) |
| 502 |
.into_other()); |
| 503 |
} |
| 504 |
if !matches!( |
| 505 |
*fun, |
| 506 |
crate::AtomicFunction::Add |
| 507 |
| crate::AtomicFunction::Subtract |
| 508 |
| crate::AtomicFunction::Exchange { compare: None } |
| 509 |
) { |
| 510 |
log::error!("Float32 atomic operation {:?} is not supported", fun); |
| 511 |
return Err(AtomicError::InvalidOperator(*fun) |
| 512 |
.with_span_handle(value, context.expressions) |
| 513 |
.into_other()); |
| 514 |
} |
| 515 |
if !matches!(pointer_space, crate::AddressSpace::Storage { .. }) { |
| 516 |
log::error!( |
| 517 |
"Float32 atomic operations are only supported in the Storage address space" |
| 518 |
); |
| 519 |
return Err(AtomicError::InvalidAddressSpace(pointer_space) |
| 520 |
.with_span_handle(value, context.expressions) |
| 521 |
.into_other()); |
| 522 |
} |
| 523 |
} |
| 524 |
_ => {} |
| 525 |
} |
| 526 |
|
| 527 |
// The result expression must be appropriate to the operation. |
| 528 |
match result { |
| 529 |
Some(result) => { |
| 530 |
// The `result` handle must refer to an `AtomicResult` expression. |
| 531 |
let crate::Expression::AtomicResult { |
| 532 |
ty: result_ty, |
| 533 |
comparison, |
| 534 |
} = context.expressions[result] |
| 535 |
else { |
| 536 |
return Err(AtomicError::InvalidResultExpression(result) |
| 537 |
.with_span_handle(result, context.expressions) |
| 538 |
.into_other()); |
| 539 |
}; |
| 540 |
|
| 541 |
// Note that this expression has been visited by the proper kind |
| 542 |
// of statement. |
| 543 |
if !self.needs_visit.remove(result) { |
| 544 |
return Err(AtomicError::ResultAlreadyPopulated(result) |
| 545 |
.with_span_handle(result, context.expressions) |
| 546 |
.into_other()); |
| 547 |
} |
| 548 |
|
| 549 |
// The constraints on the result type depend on the atomic function. |
| 550 |
if let crate::AtomicFunction::Exchange { |
| 551 |
compare: Some(compare), |
| 552 |
} = *fun |
| 553 |
{ |
| 554 |
// The comparison value must be a scalar of the same type as the |
| 555 |
// atomic we're operating on. |
| 556 |
let compare_inner = |
| 557 |
context.resolve_type_inner(compare, &self.valid_expression_set)?; |
| 558 |
if !compare_inner.non_struct_equivalent(value_inner, context.types) { |
| 559 |
log::error!( |
| 560 |
"Atomic exchange comparison has a different type from the value" |
| 561 |
); |
| 562 |
return Err(AtomicError::InvalidOperand(compare) |
| 563 |
.with_span_handle(compare, context.expressions) |
| 564 |
.into_other()); |
| 565 |
} |
| 566 |
|
| 567 |
// The result expression must be an `__atomic_compare_exchange_result` |
| 568 |
// struct whose `old_value` member is of the same type as the atomic |
| 569 |
// we're operating on. |
| 570 |
let crate::TypeInner::Struct { ref members, .. } = |
| 571 |
context.types[result_ty].inner |
| 572 |
else { |
| 573 |
return Err(AtomicError::ResultTypeMismatch(result) |
| 574 |
.with_span_handle(result, context.expressions) |
| 575 |
.into_other()); |
| 576 |
}; |
| 577 |
if !validate_atomic_compare_exchange_struct( |
| 578 |
context.types, |
| 579 |
members, |
| 580 |
|ty: &crate::TypeInner| *ty == crate::TypeInner::Scalar(pointer_scalar), |
| 581 |
) { |
| 582 |
return Err(AtomicError::ResultTypeMismatch(result) |
| 583 |
.with_span_handle(result, context.expressions) |
| 584 |
.into_other()); |
| 585 |
} |
| 586 |
|
| 587 |
// The result expression must be for a comparison operation. |
| 588 |
if !comparison { |
| 589 |
return Err(AtomicError::ResultExpressionNotExchange(result) |
| 590 |
.with_span_handle(result, context.expressions) |
| 591 |
.into_other()); |
| 592 |
} |
| 593 |
} else { |
| 594 |
// The result expression must be a scalar of the same type as the |
| 595 |
// atomic we're operating on. |
| 596 |
let result_inner = &context.types[result_ty].inner; |
| 597 |
if !result_inner.non_struct_equivalent(value_inner, context.types) { |
| 598 |
return Err(AtomicError::ResultTypeMismatch(result) |
| 599 |
.with_span_handle(result, context.expressions) |
| 600 |
.into_other()); |
| 601 |
} |
| 602 |
|
| 603 |
// The result expression must not be for a comparison. |
| 604 |
if comparison { |
| 605 |
return Err(AtomicError::ResultExpressionExchange(result) |
| 606 |
.with_span_handle(result, context.expressions) |
| 607 |
.into_other()); |
| 608 |
} |
| 609 |
} |
| 610 |
self.emit_expression(result, context)?; |
| 611 |
} |
| 612 |
|
| 613 |
None => { |
| 614 |
// Exchange operations must always produce a value. |
| 615 |
if let crate::AtomicFunction::Exchange { compare: None } = *fun { |
| 616 |
log::error!("Atomic exchange's value is unused"); |
| 617 |
return Err(AtomicError::MissingReturnValue |
| 618 |
.with_span_static(span, "atomic exchange operation") |
| 619 |
.into_other()); |
| 620 |
} |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
Ok(()) |
| 625 |
} |
| 626 |
fn validate_subgroup_operation( |
| 627 |
&mut self, |
| 628 |
op: &crate::SubgroupOperation, |
| 629 |
collective_op: &crate::CollectiveOperation, |
| 630 |
argument: Handle<crate::Expression>, |
| 631 |
result: Handle<crate::Expression>, |
| 632 |
context: &BlockContext, |
| 633 |
) -> Result<(), WithSpan<FunctionError>> { |
| 634 |
let argument_inner = context.resolve_type_inner(argument, &self.valid_expression_set)?; |
| 635 |
|
| 636 |
let (is_scalar, scalar) = match *argument_inner { |
| 637 |
crate::TypeInner::Scalar(scalar) => (true, scalar), |
| 638 |
crate::TypeInner::Vector { scalar, .. } => (false, scalar), |
| 639 |
_ => { |
| 640 |
log::error!("Subgroup operand type {:?}", argument_inner); |
| 641 |
return Err(SubgroupError::InvalidOperand(argument) |
| 642 |
.with_span_handle(argument, context.expressions) |
| 643 |
.into_other()); |
| 644 |
} |
| 645 |
}; |
| 646 |
|
| 647 |
use crate::ScalarKind as sk; |
| 648 |
use crate::SubgroupOperation as sg; |
| 649 |
match (scalar.kind, *op) { |
| 650 |
(sk::Bool, sg::All | sg::Any) if is_scalar => {} |
| 651 |
(sk::Sint | sk::Uint | sk::Float, sg::Add | sg::Mul | sg::Min | sg::Max) => {} |
| 652 |
(sk::Sint | sk::Uint, sg::And | sg::Or | sg::Xor) => {} |
| 653 |
|
| 654 |
(_, _) => { |
| 655 |
log::error!("Subgroup operand type {:?}", argument_inner); |
| 656 |
return Err(SubgroupError::InvalidOperand(argument) |
| 657 |
.with_span_handle(argument, context.expressions) |
| 658 |
.into_other()); |
| 659 |
} |
| 660 |
}; |
| 661 |
|
| 662 |
use crate::CollectiveOperation as co; |
| 663 |
match (*collective_op, *op) { |
| 664 |
( |
| 665 |
co::Reduce, |
| 666 |
sg::All |
| 667 |
| sg::Any |
| 668 |
| sg::Add |
| 669 |
| sg::Mul |
| 670 |
| sg::Min |
| 671 |
| sg::Max |
| 672 |
| sg::And |
| 673 |
| sg::Or |
| 674 |
| sg::Xor, |
| 675 |
) => {} |
| 676 |
(co::InclusiveScan | co::ExclusiveScan, sg::Add | sg::Mul) => {} |
| 677 |
|
| 678 |
(_, _) => { |
| 679 |
return Err(SubgroupError::UnknownOperation.with_span().into_other()); |
| 680 |
} |
| 681 |
}; |
| 682 |
|
| 683 |
self.emit_expression(result, context)?; |
| 684 |
match context.expressions[result] { |
| 685 |
crate::Expression::SubgroupOperationResult { ty } |
| 686 |
if { &context.types[ty].inner == argument_inner } => {} |
| 687 |
_ => { |
| 688 |
return Err(SubgroupError::ResultTypeMismatch(result) |
| 689 |
.with_span_handle(result, context.expressions) |
| 690 |
.into_other()) |
| 691 |
} |
| 692 |
} |
| 693 |
Ok(()) |
| 694 |
} |
| 695 |
fn validate_subgroup_gather( |
| 696 |
&mut self, |
| 697 |
mode: &crate::GatherMode, |
| 698 |
argument: Handle<crate::Expression>, |
| 699 |
result: Handle<crate::Expression>, |
| 700 |
context: &BlockContext, |
| 701 |
) -> Result<(), WithSpan<FunctionError>> { |
| 702 |
match *mode { |
| 703 |
crate::GatherMode::BroadcastFirst => {} |
| 704 |
crate::GatherMode::Broadcast(index) |
| 705 |
| crate::GatherMode::Shuffle(index) |
| 706 |
| crate::GatherMode::ShuffleDown(index) |
| 707 |
| crate::GatherMode::ShuffleUp(index) |
| 708 |
| crate::GatherMode::ShuffleXor(index) => { |
| 709 |
let index_ty = context.resolve_type_inner(index, &self.valid_expression_set)?; |
| 710 |
match *index_ty { |
| 711 |
crate::TypeInner::Scalar(crate::Scalar::U32) => {} |
| 712 |
_ => { |
| 713 |
log::error!( |
| 714 |
"Subgroup gather index type {:?}, expected unsigned int", |
| 715 |
index_ty |
| 716 |
); |
| 717 |
return Err(SubgroupError::InvalidOperand(argument) |
| 718 |
.with_span_handle(index, context.expressions) |
| 719 |
.into_other()); |
| 720 |
} |
| 721 |
} |
| 722 |
} |
| 723 |
} |
| 724 |
let argument_inner = context.resolve_type_inner(argument, &self.valid_expression_set)?; |
| 725 |
if !matches!(*argument_inner, |
| 726 |
crate::TypeInner::Scalar ( scalar, .. ) | crate::TypeInner::Vector { scalar, .. } |
| 727 |
if matches!(scalar.kind, crate::ScalarKind::Uint | crate::ScalarKind::Sint | crate::ScalarKind::Float) |
| 728 |
) { |
| 729 |
log::error!("Subgroup gather operand type {:?}", argument_inner); |
| 730 |
return Err(SubgroupError::InvalidOperand(argument) |
| 731 |
.with_span_handle(argument, context.expressions) |
| 732 |
.into_other()); |
| 733 |
} |
| 734 |
|
| 735 |
self.emit_expression(result, context)?; |
| 736 |
match context.expressions[result] { |
| 737 |
crate::Expression::SubgroupOperationResult { ty } |
| 738 |
if { &context.types[ty].inner == argument_inner } => {} |
| 739 |
_ => { |
| 740 |
return Err(SubgroupError::ResultTypeMismatch(result) |
| 741 |
.with_span_handle(result, context.expressions) |
| 742 |
.into_other()) |
| 743 |
} |
| 744 |
} |
| 745 |
Ok(()) |
| 746 |
} |
| 747 |
|
| 748 |
fn validate_block_impl( |
| 749 |
&mut self, |
| 750 |
statements: &crate::Block, |
| 751 |
context: &BlockContext, |
| 752 |
) -> Result<BlockInfo, WithSpan<FunctionError>> { |
| 753 |
use crate::{AddressSpace, Statement as S, TypeInner as Ti}; |
| 754 |
let mut finished = false; |
| 755 |
let mut stages = super::ShaderStages::all(); |
| 756 |
for (statement, &span) in statements.span_iter() { |
| 757 |
if finished { |
| 758 |
return Err(FunctionError::InstructionsAfterReturn |
| 759 |
.with_span_static(span, "instructions after return")); |
| 760 |
} |
| 761 |
match *statement { |
| 762 |
S::Emit(ref range) => { |
| 763 |
for handle in range.clone() { |
| 764 |
use crate::Expression as Ex; |
| 765 |
match context.expressions[handle] { |
| 766 |
Ex::Literal(_) |
| 767 |
| Ex::Constant(_) |
| 768 |
| Ex::Override(_) |
| 769 |
| Ex::ZeroValue(_) |
| 770 |
| Ex::Compose { .. } |
| 771 |
| Ex::Access { .. } |
| 772 |
| Ex::AccessIndex { .. } |
| 773 |
| Ex::Splat { .. } |
| 774 |
| Ex::Swizzle { .. } |
| 775 |
| Ex::FunctionArgument(_) |
| 776 |
| Ex::GlobalVariable(_) |
| 777 |
| Ex::LocalVariable(_) |
| 778 |
| Ex::Load { .. } |
| 779 |
| Ex::ImageSample { .. } |
| 780 |
| Ex::ImageLoad { .. } |
| 781 |
| Ex::ImageQuery { .. } |
| 782 |
| Ex::Unary { .. } |
| 783 |
| Ex::Binary { .. } |
| 784 |
| Ex::Select { .. } |
| 785 |
| Ex::Derivative { .. } |
| 786 |
| Ex::Relational { .. } |
| 787 |
| Ex::Math { .. } |
| 788 |
| Ex::As { .. } |
| 789 |
| Ex::ArrayLength(_) |
| 790 |
| Ex::RayQueryGetIntersection { .. } |
| 791 |
| Ex::RayQueryVertexPositions { .. } => { |
| 792 |
self.emit_expression(handle, context)? |
| 793 |
} |
| 794 |
Ex::CallResult(_) |
| 795 |
| Ex::AtomicResult { .. } |
| 796 |
| Ex::WorkGroupUniformLoadResult { .. } |
| 797 |
| Ex::RayQueryProceedResult |
| 798 |
| Ex::SubgroupBallotResult |
| 799 |
| Ex::SubgroupOperationResult { .. } => { |
| 800 |
return Err(FunctionError::EmitResult(handle) |
| 801 |
.with_span_handle(handle, context.expressions)); |
| 802 |
} |
| 803 |
} |
| 804 |
} |
| 805 |
} |
| 806 |
S::Block(ref block) => { |
| 807 |
let info = self.validate_block(block, context)?; |
| 808 |
stages &= info.stages; |
| 809 |
finished = info.finished; |
| 810 |
} |
| 811 |
S::If { |
| 812 |
condition, |
| 813 |
ref accept, |
| 814 |
ref reject, |
| 815 |
} => { |
| 816 |
match *context.resolve_type_inner(condition, &self.valid_expression_set)? { |
| 817 |
Ti::Scalar(crate::Scalar { |
| 818 |
kind: crate::ScalarKind::Bool, |
| 819 |
width: _, |
| 820 |
}) => {} |
| 821 |
_ => { |
| 822 |
return Err(FunctionError::InvalidIfType(condition) |
| 823 |
.with_span_handle(condition, context.expressions)) |
| 824 |
} |
| 825 |
} |
| 826 |
stages &= self.validate_block(accept, context)?.stages; |
| 827 |
stages &= self.validate_block(reject, context)?.stages; |
| 828 |
} |
| 829 |
S::Switch { |
| 830 |
selector, |
| 831 |
ref cases, |
| 832 |
} => { |
| 833 |
let uint = match context |
| 834 |
.resolve_type_inner(selector, &self.valid_expression_set)? |
| 835 |
.scalar_kind() |
| 836 |
{ |
| 837 |
Some(crate::ScalarKind::Uint) => true, |
| 838 |
Some(crate::ScalarKind::Sint) => false, |
| 839 |
_ => { |
| 840 |
return Err(FunctionError::InvalidSwitchType(selector) |
| 841 |
.with_span_handle(selector, context.expressions)) |
| 842 |
} |
| 843 |
}; |
| 844 |
self.switch_values.clear(); |
| 845 |
for case in cases { |
| 846 |
match case.value { |
| 847 |
crate::SwitchValue::I32(_) if !uint => {} |
| 848 |
crate::SwitchValue::U32(_) if uint => {} |
| 849 |
crate::SwitchValue::Default => {} |
| 850 |
_ => { |
| 851 |
return Err(FunctionError::ConflictingCaseType.with_span_static( |
| 852 |
case.body |
| 853 |
.span_iter() |
| 854 |
.next() |
| 855 |
.map_or(Default::default(), |(_, s)| *s), |
| 856 |
"conflicting switch arm here", |
| 857 |
)); |
| 858 |
} |
| 859 |
}; |
| 860 |
if !self.switch_values.insert(case.value) { |
| 861 |
return Err(match case.value { |
| 862 |
crate::SwitchValue::Default => FunctionError::MultipleDefaultCases |
| 863 |
.with_span_static( |
| 864 |
case.body |
| 865 |
.span_iter() |
| 866 |
.next() |
| 867 |
.map_or(Default::default(), |(_, s)| *s), |
| 868 |
"duplicated switch arm here", |
| 869 |
), |
| 870 |
_ => FunctionError::ConflictingSwitchCase(case.value) |
| 871 |
.with_span_static( |
| 872 |
case.body |
| 873 |
.span_iter() |
| 874 |
.next() |
| 875 |
.map_or(Default::default(), |(_, s)| *s), |
| 876 |
"conflicting switch arm here", |
| 877 |
), |
| 878 |
}); |
| 879 |
} |
| 880 |
} |
| 881 |
if !self.switch_values.contains(&crate::SwitchValue::Default) { |
| 882 |
return Err(FunctionError::MissingDefaultCase |
| 883 |
.with_span_static(span, "missing default case")); |
| 884 |
} |
| 885 |
if let Some(case) = cases.last() { |
| 886 |
if case.fall_through { |
| 887 |
return Err(FunctionError::LastCaseFallTrough.with_span_static( |
| 888 |
case.body |
| 889 |
.span_iter() |
| 890 |
.next() |
| 891 |
.map_or(Default::default(), |(_, s)| *s), |
| 892 |
"bad switch arm here", |
| 893 |
)); |
| 894 |
} |
| 895 |
} |
| 896 |
let pass_through_abilities = context.abilities |
| 897 |
& (ControlFlowAbility::RETURN | ControlFlowAbility::CONTINUE); |
| 898 |
let sub_context = |
| 899 |
context.with_abilities(pass_through_abilities | ControlFlowAbility::BREAK); |
| 900 |
for case in cases { |
| 901 |
stages &= self.validate_block(&case.body, &sub_context)?.stages; |
| 902 |
} |
| 903 |
} |
| 904 |
S::Loop { |
| 905 |
ref body, |
| 906 |
ref continuing, |
| 907 |
break_if, |
| 908 |
} => { |
| 909 |
// special handling for block scoping is needed here, |
| 910 |
// because the continuing{} block inherits the scope |
| 911 |
let base_expression_count = self.valid_expression_list.len(); |
| 912 |
let pass_through_abilities = context.abilities & ControlFlowAbility::RETURN; |
| 913 |
stages &= self |
| 914 |
.validate_block_impl( |
| 915 |
body, |
| 916 |
&context.with_abilities( |
| 917 |
pass_through_abilities |
| 918 |
| ControlFlowAbility::BREAK |
| 919 |
| ControlFlowAbility::CONTINUE, |
| 920 |
), |
| 921 |
)? |
| 922 |
.stages; |
| 923 |
stages &= self |
| 924 |
.validate_block_impl( |
| 925 |
continuing, |
| 926 |
&context.with_abilities(ControlFlowAbility::empty()), |
| 927 |
)? |
| 928 |
.stages; |
| 929 |
|
| 930 |
if let Some(condition) = break_if { |
| 931 |
match *context.resolve_type_inner(condition, &self.valid_expression_set)? { |
| 932 |
Ti::Scalar(crate::Scalar { |
| 933 |
kind: crate::ScalarKind::Bool, |
| 934 |
width: _, |
| 935 |
}) => {} |
| 936 |
_ => { |
| 937 |
return Err(FunctionError::InvalidIfType(condition) |
| 938 |
.with_span_handle(condition, context.expressions)) |
| 939 |
} |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
for handle in self.valid_expression_list.drain(base_expression_count..) { |
| 944 |
self.valid_expression_set.remove(handle); |
| 945 |
} |
| 946 |
} |
| 947 |
S::Break => { |
| 948 |
if !context.abilities.contains(ControlFlowAbility::BREAK) { |
| 949 |
return Err(FunctionError::BreakOutsideOfLoopOrSwitch |
| 950 |
.with_span_static(span, "invalid break")); |
| 951 |
} |
| 952 |
finished = true; |
| 953 |
} |
| 954 |
S::Continue => { |
| 955 |
if !context.abilities.contains(ControlFlowAbility::CONTINUE) { |
| 956 |
return Err(FunctionError::ContinueOutsideOfLoop |
| 957 |
.with_span_static(span, "invalid continue")); |
| 958 |
} |
| 959 |
finished = true; |
| 960 |
} |
| 961 |
S::Return { value } => { |
| 962 |
if !context.abilities.contains(ControlFlowAbility::RETURN) { |
| 963 |
return Err(FunctionError::InvalidReturnSpot |
| 964 |
.with_span_static(span, "invalid return")); |
| 965 |
} |
| 966 |
let value_ty = value |
| 967 |
.map(|expr| context.resolve_type(expr, &self.valid_expression_set)) |
| 968 |
.transpose()?; |
| 969 |
// We can't return pointers, but it seems best not to embed that |
| 970 |
// assumption here, so use `TypeInner::equivalent` for comparison. |
| 971 |
let okay = match (value_ty, context.return_type) { |
| 972 |
(None, None) => true, |
| 973 |
(Some(value_inner), Some(expected_ty)) => { |
| 974 |
context.compare_types(value_inner, &TypeResolution::Handle(expected_ty)) |
| 975 |
} |
| 976 |
(_, _) => false, |
| 977 |
}; |
| 978 |
|
| 979 |
if !okay { |
| 980 |
log::error!( |
| 981 |
"Returning {:?} where {:?} is expected", |
| 982 |
value_ty, |
| 983 |
context.return_type, |
| 984 |
); |
| 985 |
if let Some(handle) = value { |
| 986 |
return Err(FunctionError::InvalidReturnType { |
| 987 |
expression: value, |
| 988 |
expected_ty: context.return_type, |
| 989 |
} |
| 990 |
.with_span_handle(handle, context.expressions)); |
| 991 |
} else { |
| 992 |
return Err(FunctionError::InvalidReturnType { |
| 993 |
expression: value, |
| 994 |
expected_ty: context.return_type, |
| 995 |
} |
| 996 |
.with_span_static(span, "invalid return")); |
| 997 |
} |
| 998 |
} |
| 999 |
finished = true; |
| 1000 |
} |
| 1001 |
S::Kill => { |
| 1002 |
stages &= super::ShaderStages::FRAGMENT; |
| 1003 |
finished = true; |
| 1004 |
} |
| 1005 |
S::Barrier(barrier) => { |
| 1006 |
stages &= super::ShaderStages::COMPUTE; |
| 1007 |
if barrier.contains(crate::Barrier::SUB_GROUP) { |
| 1008 |
if !self.capabilities.contains( |
| 1009 |
super::Capabilities::SUBGROUP | super::Capabilities::SUBGROUP_BARRIER, |
| 1010 |
) { |
| 1011 |
return Err(FunctionError::MissingCapability( |
| 1012 |
super::Capabilities::SUBGROUP |
| 1013 |
| super::Capabilities::SUBGROUP_BARRIER, |
| 1014 |
) |
| 1015 |
.with_span_static(span, "missing capability for this operation")); |
| 1016 |
} |
| 1017 |
if !self |
| 1018 |
.subgroup_operations |
| 1019 |
.contains(super::SubgroupOperationSet::BASIC) |
| 1020 |
{ |
| 1021 |
return Err(FunctionError::InvalidSubgroup( |
| 1022 |
SubgroupError::UnsupportedOperation( |
| 1023 |
super::SubgroupOperationSet::BASIC, |
| 1024 |
), |
| 1025 |
) |
| 1026 |
.with_span_static(span, "support for this operation is not present")); |
| 1027 |
} |
| 1028 |
} |
| 1029 |
} |
| 1030 |
S::Store { pointer, value } => { |
| 1031 |
let mut current = pointer; |
| 1032 |
loop { |
| 1033 |
match context.expressions[current] { |
| 1034 |
crate::Expression::Access { base, .. } |
| 1035 |
| crate::Expression::AccessIndex { base, .. } => current = base, |
| 1036 |
crate::Expression::LocalVariable(_) |
| 1037 |
| crate::Expression::GlobalVariable(_) |
| 1038 |
| crate::Expression::FunctionArgument(_) => break, |
| 1039 |
_ => { |
| 1040 |
return Err(FunctionError::InvalidStorePointer(current) |
| 1041 |
.with_span_handle(pointer, context.expressions)) |
| 1042 |
} |
| 1043 |
} |
| 1044 |
} |
| 1045 |
|
| 1046 |
let value_tr = context.resolve_type(value, &self.valid_expression_set)?; |
| 1047 |
let value_ty = value_tr.inner_with(context.types); |
| 1048 |
match *value_ty { |
| 1049 |
Ti::Image { .. } | Ti::Sampler { .. } => { |
| 1050 |
return Err(FunctionError::InvalidStoreTexture { |
| 1051 |
actual: value, |
| 1052 |
actual_ty: value_ty.clone(), |
| 1053 |
} |
| 1054 |
.with_span_context(( |
| 1055 |
context.expressions.get_span(value), |
| 1056 |
format!("this value is of type {value_ty:?}"), |
| 1057 |
)) |
| 1058 |
.with_span(span, "expects a texture argument")); |
| 1059 |
} |
| 1060 |
_ => {} |
| 1061 |
} |
| 1062 |
|
| 1063 |
let pointer_ty = context.resolve_pointer_type(pointer); |
| 1064 |
let pointer_base_tr = pointer_ty.pointer_base_type(); |
| 1065 |
let pointer_base_ty = pointer_base_tr |
| 1066 |
.as_ref() |
| 1067 |
.map(|ty| ty.inner_with(context.types)); |
| 1068 |
let good = if let Some(&Ti::Atomic(ref scalar)) = pointer_base_ty { |
| 1069 |
// The Naga IR allows storing a scalar to an atomic. |
| 1070 |
*value_ty == Ti::Scalar(*scalar) |
| 1071 |
} else if let Some(tr) = pointer_base_tr { |
| 1072 |
context.compare_types(value_tr, &tr) |
| 1073 |
} else { |
| 1074 |
false |
| 1075 |
}; |
| 1076 |
|
| 1077 |
if !good { |
| 1078 |
return Err(FunctionError::InvalidStoreTypes { pointer, value } |
| 1079 |
.with_span() |
| 1080 |
.with_handle(pointer, context.expressions) |
| 1081 |
.with_handle(value, context.expressions)); |
| 1082 |
} |
| 1083 |
|
| 1084 |
if let Some(space) = pointer_ty.pointer_space() { |
| 1085 |
if !space.access().contains(crate::StorageAccess::STORE) { |
| 1086 |
return Err(FunctionError::InvalidStorePointer(pointer) |
| 1087 |
.with_span_static( |
| 1088 |
context.expressions.get_span(pointer), |
| 1089 |
"writing to this location is not permitted", |
| 1090 |
)); |
| 1091 |
} |
| 1092 |
} |
| 1093 |
} |
| 1094 |
S::ImageStore { |
| 1095 |
image, |
| 1096 |
coordinate, |
| 1097 |
array_index, |
| 1098 |
value, |
| 1099 |
} => { |
| 1100 |
//Note: this code uses a lot of `FunctionError::InvalidImageStore`, |
| 1101 |
// and could probably be refactored. |
| 1102 |
let global_var; |
| 1103 |
let image_ty; |
| 1104 |
match *context.get_expression(image) { |
| 1105 |
crate::Expression::GlobalVariable(var_handle) => { |
| 1106 |
global_var = &context.global_vars[var_handle]; |
| 1107 |
image_ty = global_var.ty; |
| 1108 |
} |
| 1109 |
// The `image` operand is indexing into a binding array, |
| 1110 |
// so punch through the `Access`* expression and look at |
| 1111 |
// the global behind it. |
| 1112 |
crate::Expression::Access { base, .. } |
| 1113 |
| crate::Expression::AccessIndex { base, .. } => { |
| 1114 |
let crate::Expression::GlobalVariable(var_handle) = |
| 1115 |
*context.get_expression(base) |
| 1116 |
else { |
| 1117 |
return Err(FunctionError::InvalidImageStore( |
| 1118 |
ExpressionError::ExpectedGlobalVariable, |
| 1119 |
) |
| 1120 |
.with_span_handle(image, context.expressions)); |
| 1121 |
}; |
| 1122 |
global_var = &context.global_vars[var_handle]; |
| 1123 |
|
| 1124 |
// The global variable must be a binding array. |
| 1125 |
let Ti::BindingArray { base, .. } = context.types[global_var.ty].inner |
| 1126 |
else { |
| 1127 |
return Err(FunctionError::InvalidImageStore( |
| 1128 |
ExpressionError::ExpectedBindingArrayType(global_var.ty), |
| 1129 |
) |
| 1130 |
.with_span_handle(global_var.ty, context.types)); |
| 1131 |
}; |
| 1132 |
|
| 1133 |
image_ty = base; |
| 1134 |
} |
| 1135 |
_ => { |
| 1136 |
return Err(FunctionError::InvalidImageStore( |
| 1137 |
ExpressionError::ExpectedGlobalVariable, |
| 1138 |
) |
| 1139 |
.with_span_handle(image, context.expressions)) |
| 1140 |
} |
| 1141 |
}; |
| 1142 |
|
| 1143 |
// The `image` operand must be an `Image`. |
| 1144 |
let Ti::Image { |
| 1145 |
class, |
| 1146 |
arrayed, |
| 1147 |
dim, |
| 1148 |
} = context.types[image_ty].inner |
| 1149 |
else { |
| 1150 |
return Err(FunctionError::InvalidImageStore( |
| 1151 |
ExpressionError::ExpectedImageType(global_var.ty), |
| 1152 |
) |
| 1153 |
.with_span() |
| 1154 |
.with_handle(global_var.ty, context.types) |
| 1155 |
.with_handle(image, context.expressions)); |
| 1156 |
}; |
| 1157 |
|
| 1158 |
// It had better be a storage image, since we're writing to it. |
| 1159 |
let crate::ImageClass::Storage { format, .. } = class else { |
| 1160 |
return Err(FunctionError::InvalidImageStore( |
| 1161 |
ExpressionError::InvalidImageClass(class), |
| 1162 |
) |
| 1163 |
.with_span_handle(image, context.expressions)); |
| 1164 |
}; |
| 1165 |
|
| 1166 |
// The `coordinate` operand must be a vector of the appropriate size. |
| 1167 |
if context |
| 1168 |
.resolve_type_inner(coordinate, &self.valid_expression_set)? |
| 1169 |
.image_storage_coordinates() |
| 1170 |
.is_none_or(|coord_dim| coord_dim != dim) |
| 1171 |
{ |
| 1172 |
return Err(FunctionError::InvalidImageStore( |
| 1173 |
ExpressionError::InvalidImageCoordinateType(dim, coordinate), |
| 1174 |
) |
| 1175 |
.with_span_handle(coordinate, context.expressions)); |
| 1176 |
} |
| 1177 |
|
| 1178 |
// The `array_index` operand should be present if and only if |
| 1179 |
// the image itself is arrayed. |
| 1180 |
if arrayed != array_index.is_some() { |
| 1181 |
return Err(FunctionError::InvalidImageStore( |
| 1182 |
ExpressionError::InvalidImageArrayIndex, |
| 1183 |
) |
| 1184 |
.with_span_handle(coordinate, context.expressions)); |
| 1185 |
} |
| 1186 |
|
| 1187 |
// If present, `array_index` must be a scalar integer type. |
| 1188 |
if let Some(expr) = array_index { |
| 1189 |
if !matches!( |
| 1190 |
*context.resolve_type_inner(expr, &self.valid_expression_set)?, |
| 1191 |
Ti::Scalar(crate::Scalar { |
| 1192 |
kind: crate::ScalarKind::Sint | crate::ScalarKind::Uint, |
| 1193 |
width: _, |
| 1194 |
}) |
| 1195 |
) { |
| 1196 |
return Err(FunctionError::InvalidImageStore( |
| 1197 |
ExpressionError::InvalidImageArrayIndexType(expr), |
| 1198 |
) |
| 1199 |
.with_span_handle(expr, context.expressions)); |
| 1200 |
} |
| 1201 |
} |
| 1202 |
|
| 1203 |
let value_ty = crate::TypeInner::Vector { |
| 1204 |
size: crate::VectorSize::Quad, |
| 1205 |
scalar: format.into(), |
| 1206 |
}; |
| 1207 |
|
| 1208 |
// The value we're writing had better match the scalar type |
| 1209 |
// for `image`'s format. |
| 1210 |
let actual_value_ty = |
| 1211 |
context.resolve_type_inner(value, &self.valid_expression_set)?; |
| 1212 |
if actual_value_ty != &value_ty { |
| 1213 |
return Err(FunctionError::InvalidStoreValue { |
| 1214 |
actual: value, |
| 1215 |
actual_ty: actual_value_ty.clone(), |
| 1216 |
expected_ty: value_ty.clone(), |
| 1217 |
} |
| 1218 |
.with_span_context(( |
| 1219 |
context.expressions.get_span(value), |
| 1220 |
format!("this value is of type {actual_value_ty:?}"), |
| 1221 |
)) |
| 1222 |
.with_span( |
| 1223 |
span, |
| 1224 |
format!("expects a value argument of type {value_ty:?}"), |
| 1225 |
)); |
| 1226 |
} |
| 1227 |
} |
| 1228 |
S::Call { |
| 1229 |
function, |
| 1230 |
ref arguments, |
| 1231 |
result, |
| 1232 |
} => match self.validate_call(function, arguments, result, context) { |
| 1233 |
Ok(callee_stages) => stages &= callee_stages, |
| 1234 |
Err(error) => { |
| 1235 |
return Err(error.and_then(|error| { |
| 1236 |
FunctionError::InvalidCall { function, error } |
| 1237 |
.with_span_static(span, "invalid function call") |
| 1238 |
})) |
| 1239 |
} |
| 1240 |
}, |
| 1241 |
S::Atomic { |
| 1242 |
pointer, |
| 1243 |
ref fun, |
| 1244 |
value, |
| 1245 |
result, |
| 1246 |
} => { |
| 1247 |
self.validate_atomic(pointer, fun, value, result, span, context)?; |
| 1248 |
} |
| 1249 |
S::ImageAtomic { |
| 1250 |
image, |
| 1251 |
coordinate, |
| 1252 |
array_index, |
| 1253 |
fun, |
| 1254 |
value, |
| 1255 |
} => { |
| 1256 |
let var = match *context.get_expression(image) { |
| 1257 |
crate::Expression::GlobalVariable(var_handle) => { |
| 1258 |
&context.global_vars[var_handle] |
| 1259 |
} |
| 1260 |
// We're looking at a binding index situation, so punch through the index and look at the global behind it. |
| 1261 |
crate::Expression::Access { base, .. } |
| 1262 |
| crate::Expression::AccessIndex { base, .. } => { |
| 1263 |
match *context.get_expression(base) { |
| 1264 |
crate::Expression::GlobalVariable(var_handle) => { |
| 1265 |
&context.global_vars[var_handle] |
| 1266 |
} |
| 1267 |
_ => { |
| 1268 |
return Err(FunctionError::InvalidImageAtomic( |
| 1269 |
ExpressionError::ExpectedGlobalVariable, |
| 1270 |
) |
| 1271 |
.with_span_handle(image, context.expressions)) |
| 1272 |
} |
| 1273 |
} |
| 1274 |
} |
| 1275 |
_ => { |
| 1276 |
return Err(FunctionError::InvalidImageAtomic( |
| 1277 |
ExpressionError::ExpectedGlobalVariable, |
| 1278 |
) |
| 1279 |
.with_span_handle(image, context.expressions)) |
| 1280 |
} |
| 1281 |
}; |
| 1282 |
|
| 1283 |
// Punch through a binding array to get the underlying type |
| 1284 |
let global_ty = match context.types[var.ty].inner { |
| 1285 |
Ti::BindingArray { base, .. } => &context.types[base].inner, |
| 1286 |
ref inner => inner, |
| 1287 |
}; |
| 1288 |
|
| 1289 |
let value_ty = match *global_ty { |
| 1290 |
Ti::Image { |
| 1291 |
class, |
| 1292 |
arrayed, |
| 1293 |
dim, |
| 1294 |
} => { |
| 1295 |
match context |
| 1296 |
.resolve_type_inner(coordinate, &self.valid_expression_set)? |
| 1297 |
.image_storage_coordinates() |
| 1298 |
{ |
| 1299 |
Some(coord_dim) if coord_dim == dim => {} |
| 1300 |
_ => { |
| 1301 |
return Err(FunctionError::InvalidImageAtomic( |
| 1302 |
ExpressionError::InvalidImageCoordinateType( |
| 1303 |
dim, coordinate, |
| 1304 |
), |
| 1305 |
) |
| 1306 |
.with_span_handle(coordinate, context.expressions)); |
| 1307 |
} |
| 1308 |
}; |
| 1309 |
if arrayed != array_index.is_some() { |
| 1310 |
return Err(FunctionError::InvalidImageAtomic( |
| 1311 |
ExpressionError::InvalidImageArrayIndex, |
| 1312 |
) |
| 1313 |
.with_span_handle(coordinate, context.expressions)); |
| 1314 |
} |
| 1315 |
if let Some(expr) = array_index { |
| 1316 |
match *context |
| 1317 |
.resolve_type_inner(expr, &self.valid_expression_set)? |
| 1318 |
{ |
| 1319 |
Ti::Scalar(crate::Scalar { |
| 1320 |
kind: crate::ScalarKind::Sint | crate::ScalarKind::Uint, |
| 1321 |
width: _, |
| 1322 |
}) => {} |
| 1323 |
_ => { |
| 1324 |
return Err(FunctionError::InvalidImageAtomic( |
| 1325 |
ExpressionError::InvalidImageArrayIndexType(expr), |
| 1326 |
) |
| 1327 |
.with_span_handle(expr, context.expressions)); |
| 1328 |
} |
| 1329 |
} |
| 1330 |
} |
| 1331 |
match class { |
| 1332 |
crate::ImageClass::Storage { format, access } => { |
| 1333 |
if !access.contains(crate::StorageAccess::ATOMIC) { |
| 1334 |
return Err(FunctionError::InvalidImageAtomic( |
| 1335 |
ExpressionError::InvalidImageStorageAccess(access), |
| 1336 |
) |
| 1337 |
.with_span_handle(image, context.expressions)); |
| 1338 |
} |
| 1339 |
match format { |
| 1340 |
crate::StorageFormat::R64Uint => { |
| 1341 |
if !self.capabilities.intersects( |
| 1342 |
super::Capabilities::TEXTURE_INT64_ATOMIC, |
| 1343 |
) { |
| 1344 |
return Err(FunctionError::MissingCapability( |
| 1345 |
super::Capabilities::TEXTURE_INT64_ATOMIC, |
| 1346 |
) |
| 1347 |
.with_span_static( |
| 1348 |
span, |
| 1349 |
"missing capability for this operation", |
| 1350 |
)); |
| 1351 |
} |
| 1352 |
match fun { |
| 1353 |
crate::AtomicFunction::Min |
| 1354 |
| crate::AtomicFunction::Max => {} |
| 1355 |
_ => { |
| 1356 |
return Err( |
| 1357 |
FunctionError::InvalidImageAtomicFunction( |
| 1358 |
fun, |
| 1359 |
) |
| 1360 |
.with_span_handle( |
| 1361 |
image, |
| 1362 |
context.expressions, |
| 1363 |
), |
| 1364 |
); |
| 1365 |
} |
| 1366 |
} |
| 1367 |
} |
| 1368 |
crate::StorageFormat::R32Sint |
| 1369 |
| crate::StorageFormat::R32Uint => { |
| 1370 |
if !self |
| 1371 |
.capabilities |
| 1372 |
.intersects(super::Capabilities::TEXTURE_ATOMIC) |
| 1373 |
{ |
| 1374 |
return Err(FunctionError::MissingCapability( |
| 1375 |
super::Capabilities::TEXTURE_ATOMIC, |
| 1376 |
) |
| 1377 |
.with_span_static( |
| 1378 |
span, |
| 1379 |
"missing capability for this operation", |
| 1380 |
)); |
| 1381 |
} |
| 1382 |
match fun { |
| 1383 |
crate::AtomicFunction::Add |
| 1384 |
| crate::AtomicFunction::And |
| 1385 |
| crate::AtomicFunction::ExclusiveOr |
| 1386 |
| crate::AtomicFunction::InclusiveOr |
| 1387 |
| crate::AtomicFunction::Min |
| 1388 |
| crate::AtomicFunction::Max => {} |
| 1389 |
_ => { |
| 1390 |
return Err( |
| 1391 |
FunctionError::InvalidImageAtomicFunction( |
| 1392 |
fun, |
| 1393 |
) |
| 1394 |
.with_span_handle( |
| 1395 |
image, |
| 1396 |
context.expressions, |
| 1397 |
), |
| 1398 |
); |
| 1399 |
} |
| 1400 |
} |
| 1401 |
} |
| 1402 |
_ => { |
| 1403 |
return Err(FunctionError::InvalidImageAtomic( |
| 1404 |
ExpressionError::InvalidImageFormat(format), |
| 1405 |
) |
| 1406 |
.with_span_handle(image, context.expressions)); |
| 1407 |
} |
| 1408 |
} |
| 1409 |
crate::TypeInner::Scalar(format.into()) |
| 1410 |
} |
| 1411 |
_ => { |
| 1412 |
return Err(FunctionError::InvalidImageAtomic( |
| 1413 |
ExpressionError::InvalidImageClass(class), |
| 1414 |
) |
| 1415 |
.with_span_handle(image, context.expressions)); |
| 1416 |
} |
| 1417 |
} |
| 1418 |
} |
| 1419 |
_ => { |
| 1420 |
return Err(FunctionError::InvalidImageAtomic( |
| 1421 |
ExpressionError::ExpectedImageType(var.ty), |
| 1422 |
) |
| 1423 |
.with_span() |
| 1424 |
.with_handle(var.ty, context.types) |
| 1425 |
.with_handle(image, context.expressions)) |
| 1426 |
} |
| 1427 |
}; |
| 1428 |
|
| 1429 |
if *context.resolve_type_inner(value, &self.valid_expression_set)? != value_ty { |
| 1430 |
return Err(FunctionError::InvalidImageAtomicValue(value) |
| 1431 |
.with_span_handle(value, context.expressions)); |
| 1432 |
} |
| 1433 |
} |
| 1434 |
S::WorkGroupUniformLoad { pointer, result } => { |
| 1435 |
stages &= super::ShaderStages::COMPUTE; |
| 1436 |
let pointer_inner = |
| 1437 |
context.resolve_type_inner(pointer, &self.valid_expression_set)?; |
| 1438 |
match *pointer_inner { |
| 1439 |
Ti::Pointer { |
| 1440 |
space: AddressSpace::WorkGroup, |
| 1441 |
.. |
| 1442 |
} => {} |
| 1443 |
Ti::ValuePointer { |
| 1444 |
space: AddressSpace::WorkGroup, |
| 1445 |
.. |
| 1446 |
} => {} |
| 1447 |
_ => { |
| 1448 |
return Err(FunctionError::WorkgroupUniformLoadInvalidPointer(pointer) |
| 1449 |
.with_span_static(span, "WorkGroupUniformLoad")) |
| 1450 |
} |
| 1451 |
} |
| 1452 |
self.emit_expression(result, context)?; |
| 1453 |
let ty = match &context.expressions[result] { |
| 1454 |
&crate::Expression::WorkGroupUniformLoadResult { ty } => ty, |
| 1455 |
_ => { |
| 1456 |
return Err(FunctionError::WorkgroupUniformLoadExpressionMismatch( |
| 1457 |
result, |
| 1458 |
) |
| 1459 |
.with_span_static(span, "WorkGroupUniformLoad")); |
| 1460 |
} |
| 1461 |
}; |
| 1462 |
let expected_pointer_inner = Ti::Pointer { |
| 1463 |
base: ty, |
| 1464 |
space: AddressSpace::WorkGroup, |
| 1465 |
}; |
| 1466 |
if !expected_pointer_inner.non_struct_equivalent(pointer_inner, context.types) { |
| 1467 |
return Err(FunctionError::WorkgroupUniformLoadInvalidPointer(pointer) |
| 1468 |
.with_span_static(span, "WorkGroupUniformLoad")); |
| 1469 |
} |
| 1470 |
} |
| 1471 |
S::RayQuery { query, ref fun } => { |
| 1472 |
let query_var = match *context.get_expression(query) { |
| 1473 |
crate::Expression::LocalVariable(var) => &context.local_vars[var], |
| 1474 |
ref other => { |
| 1475 |
log::error!("Unexpected ray query expression {other:?}"); |
| 1476 |
return Err(FunctionError::InvalidRayQueryExpression(query) |
| 1477 |
.with_span_static(span, "invalid query expression")); |
| 1478 |
} |
| 1479 |
}; |
| 1480 |
let rq_vertex_return = match context.types[query_var.ty].inner { |
| 1481 |
Ti::RayQuery { vertex_return } => vertex_return, |
| 1482 |
ref other => { |
| 1483 |
log::error!("Unexpected ray query type {other:?}"); |
| 1484 |
return Err(FunctionError::InvalidRayQueryType(query_var.ty) |
| 1485 |
.with_span_static(span, "invalid query type")); |
| 1486 |
} |
| 1487 |
}; |
| 1488 |
match *fun { |
| 1489 |
crate::RayQueryFunction::Initialize { |
| 1490 |
acceleration_structure, |
| 1491 |
descriptor, |
| 1492 |
} => { |
| 1493 |
match *context.resolve_type_inner( |
| 1494 |
acceleration_structure, |
| 1495 |
&self.valid_expression_set, |
| 1496 |
)? { |
| 1497 |
Ti::AccelerationStructure { vertex_return } => { |
| 1498 |
if (!vertex_return) && rq_vertex_return { |
| 1499 |
return Err(FunctionError::MissingAccelerationStructureVertexReturn(acceleration_structure, query).with_span_static(span, "invalid acceleration structure")); |
| 1500 |
} |
| 1501 |
} |
| 1502 |
_ => { |
| 1503 |
return Err(FunctionError::InvalidAccelerationStructure( |
| 1504 |
acceleration_structure, |
| 1505 |
) |
| 1506 |
.with_span_static(span, "invalid acceleration structure")) |
| 1507 |
} |
| 1508 |
} |
| 1509 |
let desc_ty_given = context |
| 1510 |
.resolve_type_inner(descriptor, &self.valid_expression_set)?; |
| 1511 |
let desc_ty_expected = context |
| 1512 |
.special_types |
| 1513 |
.ray_desc |
| 1514 |
.map(|handle| &context.types[handle].inner); |
| 1515 |
if Some(desc_ty_given) != desc_ty_expected { |
| 1516 |
return Err(FunctionError::InvalidRayDescriptor(descriptor) |
| 1517 |
.with_span_static(span, "invalid ray descriptor")); |
| 1518 |
} |
| 1519 |
} |
| 1520 |
crate::RayQueryFunction::Proceed { result } => { |
| 1521 |
self.emit_expression(result, context)?; |
| 1522 |
} |
| 1523 |
crate::RayQueryFunction::GenerateIntersection { hit_t } => { |
| 1524 |
match *context.resolve_type_inner(hit_t, &self.valid_expression_set)? { |
| 1525 |
Ti::Scalar(crate::Scalar { |
| 1526 |
kind: crate::ScalarKind::Float, |
| 1527 |
width: _, |
| 1528 |
}) => {} |
| 1529 |
_ => { |
| 1530 |
return Err(FunctionError::InvalidHitDistanceType(hit_t) |
| 1531 |
.with_span_static(span, "invalid hit_t")) |
| 1532 |
} |
| 1533 |
} |
| 1534 |
} |
| 1535 |
crate::RayQueryFunction::ConfirmIntersection => {} |
| 1536 |
crate::RayQueryFunction::Terminate => {} |
| 1537 |
} |
| 1538 |
} |
| 1539 |
S::SubgroupBallot { result, predicate } => { |
| 1540 |
stages &= self.subgroup_stages; |
| 1541 |
if !self.capabilities.contains(super::Capabilities::SUBGROUP) { |
| 1542 |
return Err(FunctionError::MissingCapability( |
| 1543 |
super::Capabilities::SUBGROUP, |
| 1544 |
) |
| 1545 |
.with_span_static(span, "missing capability for this operation")); |
| 1546 |
} |
| 1547 |
if !self |
| 1548 |
.subgroup_operations |
| 1549 |
.contains(super::SubgroupOperationSet::BALLOT) |
| 1550 |
{ |
| 1551 |
return Err(FunctionError::InvalidSubgroup( |
| 1552 |
SubgroupError::UnsupportedOperation( |
| 1553 |
super::SubgroupOperationSet::BALLOT, |
| 1554 |
), |
| 1555 |
) |
| 1556 |
.with_span_static(span, "support for this operation is not present")); |
| 1557 |
} |
| 1558 |
if let Some(predicate) = predicate { |
| 1559 |
let predicate_inner = |
| 1560 |
context.resolve_type_inner(predicate, &self.valid_expression_set)?; |
| 1561 |
if !matches!( |
| 1562 |
*predicate_inner, |
| 1563 |
crate::TypeInner::Scalar(crate::Scalar::BOOL,) |
| 1564 |
) { |
| 1565 |
log::error!( |
| 1566 |
"Subgroup ballot predicate type {:?} expected bool", |
| 1567 |
predicate_inner |
| 1568 |
); |
| 1569 |
return Err(SubgroupError::InvalidOperand(predicate) |
| 1570 |
.with_span_handle(predicate, context.expressions) |
| 1571 |
.into_other()); |
| 1572 |
} |
| 1573 |
} |
| 1574 |
self.emit_expression(result, context)?; |
| 1575 |
} |
| 1576 |
S::SubgroupCollectiveOperation { |
| 1577 |
ref op, |
| 1578 |
ref collective_op, |
| 1579 |
argument, |
| 1580 |
result, |
| 1581 |
} => { |
| 1582 |
stages &= self.subgroup_stages; |
| 1583 |
if !self.capabilities.contains(super::Capabilities::SUBGROUP) { |
| 1584 |
return Err(FunctionError::MissingCapability( |
| 1585 |
super::Capabilities::SUBGROUP, |
| 1586 |
) |
| 1587 |
.with_span_static(span, "missing capability for this operation")); |
| 1588 |
} |
| 1589 |
let operation = op.required_operations(); |
| 1590 |
if !self.subgroup_operations.contains(operation) { |
| 1591 |
return Err(FunctionError::InvalidSubgroup( |
| 1592 |
SubgroupError::UnsupportedOperation(operation), |
| 1593 |
) |
| 1594 |
.with_span_static(span, "support for this operation is not present")); |
| 1595 |
} |
| 1596 |
self.validate_subgroup_operation(op, collective_op, argument, result, context)?; |
| 1597 |
} |
| 1598 |
S::SubgroupGather { |
| 1599 |
ref mode, |
| 1600 |
argument, |
| 1601 |
result, |
| 1602 |
} => { |
| 1603 |
stages &= self.subgroup_stages; |
| 1604 |
if !self.capabilities.contains(super::Capabilities::SUBGROUP) { |
| 1605 |
return Err(FunctionError::MissingCapability( |
| 1606 |
super::Capabilities::SUBGROUP, |
| 1607 |
) |
| 1608 |
.with_span_static(span, "missing capability for this operation")); |
| 1609 |
} |
| 1610 |
let operation = mode.required_operations(); |
| 1611 |
if !self.subgroup_operations.contains(operation) { |
| 1612 |
return Err(FunctionError::InvalidSubgroup( |
| 1613 |
SubgroupError::UnsupportedOperation(operation), |
| 1614 |
) |
| 1615 |
.with_span_static(span, "support for this operation is not present")); |
| 1616 |
} |
| 1617 |
self.validate_subgroup_gather(mode, argument, result, context)?; |
| 1618 |
} |
| 1619 |
} |
| 1620 |
} |
| 1621 |
Ok(BlockInfo { stages, finished }) |
| 1622 |
} |
| 1623 |
|
| 1624 |
fn validate_block( |
| 1625 |
&mut self, |
| 1626 |
statements: &crate::Block, |
| 1627 |
context: &BlockContext, |
| 1628 |
) -> Result<BlockInfo, WithSpan<FunctionError>> { |
| 1629 |
let base_expression_count = self.valid_expression_list.len(); |
| 1630 |
let info = self.validate_block_impl(statements, context)?; |
| 1631 |
for handle in self.valid_expression_list.drain(base_expression_count..) { |
| 1632 |
self.valid_expression_set.remove(handle); |
| 1633 |
} |
| 1634 |
Ok(info) |
| 1635 |
} |
| 1636 |
|
| 1637 |
fn validate_local_var( |
| 1638 |
&self, |
| 1639 |
var: &crate::LocalVariable, |
| 1640 |
gctx: crate::proc::GlobalCtx, |
| 1641 |
fun_info: &FunctionInfo, |
| 1642 |
local_expr_kind: &crate::proc::ExpressionKindTracker, |
| 1643 |
) -> Result<(), LocalVariableError> { |
| 1644 |
log::debug!("var {:?}", var); |
| 1645 |
let type_info = self |
| 1646 |
.types |
| 1647 |
.get(var.ty.index()) |
| 1648 |
.ok_or(LocalVariableError::InvalidType(var.ty))?; |
| 1649 |
if !type_info.flags.contains(super::TypeFlags::CONSTRUCTIBLE) { |
| 1650 |
return Err(LocalVariableError::InvalidType(var.ty)); |
| 1651 |
} |
| 1652 |
|
| 1653 |
if let Some(init) = var.init { |
| 1654 |
if !gctx.compare_types(&TypeResolution::Handle(var.ty), &fun_info[init].ty) { |
| 1655 |
return Err(LocalVariableError::InitializerType); |
| 1656 |
} |
| 1657 |
|
| 1658 |
if !local_expr_kind.is_const_or_override(init) { |
| 1659 |
return Err(LocalVariableError::NonConstOrOverrideInitializer); |
| 1660 |
} |
| 1661 |
} |
| 1662 |
|
| 1663 |
Ok(()) |
| 1664 |
} |
| 1665 |
|
| 1666 |
pub(super) fn validate_function( |
| 1667 |
&mut self, |
| 1668 |
fun: &crate::Function, |
| 1669 |
module: &crate::Module, |
| 1670 |
mod_info: &ModuleInfo, |
| 1671 |
entry_point: bool, |
| 1672 |
) -> Result<FunctionInfo, WithSpan<FunctionError>> { |
| 1673 |
let mut info = mod_info.process_function(fun, module, self.flags, self.capabilities)?; |
| 1674 |
|
| 1675 |
let local_expr_kind = crate::proc::ExpressionKindTracker::from_arena(&fun.expressions); |
| 1676 |
|
| 1677 |
for (var_handle, var) in fun.local_variables.iter() { |
| 1678 |
self.validate_local_var(var, module.to_ctx(), &info, &local_expr_kind) |
| 1679 |
.map_err(|source| { |
| 1680 |
FunctionError::LocalVariable { |
| 1681 |
handle: var_handle, |
| 1682 |
name: var.name.clone().unwrap_or_default(), |
| 1683 |
source, |
| 1684 |
} |
| 1685 |
.with_span_handle(var.ty, &module.types) |
| 1686 |
.with_handle(var_handle, &fun.local_variables) |
| 1687 |
})?; |
| 1688 |
} |
| 1689 |
|
| 1690 |
for (index, argument) in fun.arguments.iter().enumerate() { |
| 1691 |
match module.types[argument.ty].inner.pointer_space() { |
| 1692 |
Some(crate::AddressSpace::Private | crate::AddressSpace::Function) | None => {} |
| 1693 |
Some(other) => { |
| 1694 |
return Err(FunctionError::InvalidArgumentPointerSpace { |
| 1695 |
index, |
| 1696 |
name: argument.name.clone().unwrap_or_default(), |
| 1697 |
space: other, |
| 1698 |
} |
| 1699 |
.with_span_handle(argument.ty, &module.types)) |
| 1700 |
} |
| 1701 |
} |
| 1702 |
// Check for the least informative error last. |
| 1703 |
if !self.types[argument.ty.index()] |
| 1704 |
.flags |
| 1705 |
.contains(super::TypeFlags::ARGUMENT) |
| 1706 |
{ |
| 1707 |
return Err(FunctionError::InvalidArgumentType { |
| 1708 |
index, |
| 1709 |
name: argument.name.clone().unwrap_or_default(), |
| 1710 |
} |
| 1711 |
.with_span_handle(argument.ty, &module.types)); |
| 1712 |
} |
| 1713 |
|
| 1714 |
if !entry_point && argument.binding.is_some() { |
| 1715 |
return Err(FunctionError::PipelineInputRegularFunction { |
| 1716 |
name: argument.name.clone().unwrap_or_default(), |
| 1717 |
} |
| 1718 |
.with_span_handle(argument.ty, &module.types)); |
| 1719 |
} |
| 1720 |
} |
| 1721 |
|
| 1722 |
if let Some(ref result) = fun.result { |
| 1723 |
if !self.types[result.ty.index()] |
| 1724 |
.flags |
| 1725 |
.contains(super::TypeFlags::CONSTRUCTIBLE) |
| 1726 |
{ |
| 1727 |
return Err(FunctionError::NonConstructibleReturnType |
| 1728 |
.with_span_handle(result.ty, &module.types)); |
| 1729 |
} |
| 1730 |
|
| 1731 |
if !entry_point && result.binding.is_some() { |
| 1732 |
return Err(FunctionError::PipelineOutputRegularFunction |
| 1733 |
.with_span_handle(result.ty, &module.types)); |
| 1734 |
} |
| 1735 |
} |
| 1736 |
|
| 1737 |
self.valid_expression_set.clear_for_arena(&fun.expressions); |
| 1738 |
self.valid_expression_list.clear(); |
| 1739 |
self.needs_visit.clear_for_arena(&fun.expressions); |
| 1740 |
for (handle, expr) in fun.expressions.iter() { |
| 1741 |
if expr.needs_pre_emit() { |
| 1742 |
self.valid_expression_set.insert(handle); |
| 1743 |
} |
| 1744 |
if self.flags.contains(super::ValidationFlags::EXPRESSIONS) { |
| 1745 |
// Mark expressions that need to be visited by a particular kind of |
| 1746 |
// statement. |
| 1747 |
if let crate::Expression::CallResult(_) | crate::Expression::AtomicResult { .. } = |
| 1748 |
*expr |
| 1749 |
{ |
| 1750 |
self.needs_visit.insert(handle); |
| 1751 |
} |
| 1752 |
|
| 1753 |
match self.validate_expression( |
| 1754 |
handle, |
| 1755 |
expr, |
| 1756 |
fun, |
| 1757 |
module, |
| 1758 |
&info, |
| 1759 |
mod_info, |
| 1760 |
&local_expr_kind, |
| 1761 |
) { |
| 1762 |
Ok(stages) => info.available_stages &= stages, |
| 1763 |
Err(source) => { |
| 1764 |
return Err(FunctionError::Expression { handle, source } |
| 1765 |
.with_span_handle(handle, &fun.expressions)) |
| 1766 |
} |
| 1767 |
} |
| 1768 |
} |
| 1769 |
} |
| 1770 |
|
| 1771 |
if self.flags.contains(super::ValidationFlags::BLOCKS) { |
| 1772 |
let stages = self |
| 1773 |
.validate_block( |
| 1774 |
&fun.body, |
| 1775 |
&BlockContext::new(fun, module, &info, &mod_info.functions), |
| 1776 |
)? |
| 1777 |
.stages; |
| 1778 |
info.available_stages &= stages; |
| 1779 |
|
| 1780 |
if self.flags.contains(super::ValidationFlags::EXPRESSIONS) { |
| 1781 |
if let Some(handle) = self.needs_visit.iter().next() { |
| 1782 |
return Err(FunctionError::UnvisitedExpression(handle) |
| 1783 |
.with_span_handle(handle, &fun.expressions)); |
| 1784 |
} |
| 1785 |
} |
| 1786 |
} |
| 1787 |
Ok(info) |
| 1788 |
} |
| 1789 |
} |