Skip to main content

naga/valid/
function.rs

1use alloc::{
2    format,
3    string::{String, ToString as _},
4};
5
6use super::{
7    analyzer::{UniformityDisruptor, UniformityRequirements},
8    ExpressionError, FunctionInfo, ModuleInfo,
9};
10use crate::arena::{Arena, UniqueArena};
11use crate::arena::{Handle, HandleSet};
12use crate::proc::TypeResolution;
13use crate::span::WithSpan;
14use crate::span::{AddSpan as _, MapErrWithSpan as _};
15
16#[derive(Clone, Debug, thiserror::Error)]
17#[cfg_attr(test, derive(PartialEq))]
18pub enum CallError {
19    #[error("Argument {index} expression is invalid")]
20    Argument {
21        index: usize,
22        source: ExpressionError,
23    },
24    #[error("Result expression {0:?} has already been introduced earlier")]
25    ResultAlreadyInScope(Handle<crate::Expression>),
26    #[error("Result expression {0:?} is populated by multiple `Call` statements")]
27    ResultAlreadyPopulated(Handle<crate::Expression>),
28    #[error("Requires {required} arguments, but {seen} are provided")]
29    ArgumentCount { required: usize, seen: usize },
30    #[error("Argument {index} value {seen_expression:?} doesn't match the type {required:?}")]
31    ArgumentType {
32        index: usize,
33        required: Handle<crate::Type>,
34        seen_expression: Handle<crate::Expression>,
35    },
36    #[error("The emitted expression doesn't match the call")]
37    ExpressionMismatch(Option<Handle<crate::Expression>>),
38}
39
40#[derive(Clone, Debug, thiserror::Error)]
41#[cfg_attr(test, derive(PartialEq))]
42pub enum AtomicError {
43    #[error("Pointer {0:?} to atomic is invalid.")]
44    InvalidPointer(Handle<crate::Expression>),
45    #[error("Address space {0:?} is not supported.")]
46    InvalidAddressSpace(crate::AddressSpace),
47    #[error("Operand {0:?} has invalid type.")]
48    InvalidOperand(Handle<crate::Expression>),
49    #[error("Operator {0:?} is not supported.")]
50    InvalidOperator(crate::AtomicFunction),
51    #[error("Result expression {0:?} is not an `AtomicResult` expression")]
52    InvalidResultExpression(Handle<crate::Expression>),
53    #[error("Result expression {0:?} is marked as an `exchange`")]
54    ResultExpressionExchange(Handle<crate::Expression>),
55    #[error("Result expression {0:?} is not marked as an `exchange`")]
56    ResultExpressionNotExchange(Handle<crate::Expression>),
57    #[error("Result type for {0:?} doesn't match the statement")]
58    ResultTypeMismatch(Handle<crate::Expression>),
59    #[error("Exchange operations must return a value")]
60    MissingReturnValue,
61    #[error("Capability {0:?} is required")]
62    MissingCapability(super::Capabilities),
63    #[error("Result expression {0:?} is populated by multiple `Atomic` statements")]
64    ResultAlreadyPopulated(Handle<crate::Expression>),
65}
66
67#[derive(Clone, Debug, thiserror::Error)]
68#[cfg_attr(test, derive(PartialEq))]
69pub enum SubgroupError {
70    #[error("Operand {0:?} has invalid type.")]
71    InvalidOperand(Handle<crate::Expression>),
72    #[error("Result type for {0:?} doesn't match the statement")]
73    ResultTypeMismatch(Handle<crate::Expression>),
74    #[error("Support for subgroup operation {0:?} is required")]
75    UnsupportedOperation(super::SubgroupOperationSet),
76    #[error("Unknown operation")]
77    UnknownOperation,
78    #[error("Invocation ID must be a const-expression")]
79    InvalidInvocationIdExprType(Handle<crate::Expression>),
80}
81
82#[derive(Clone, Debug, thiserror::Error)]
83#[cfg_attr(test, derive(PartialEq))]
84pub enum LocalVariableError {
85    #[error("Local variable has a type {0:?} that can't be stored in a local variable.")]
86    InvalidType(Handle<crate::Type>),
87    #[error("Initializer doesn't match the variable type")]
88    InitializerType,
89    #[error("Initializer is not a const or override expression")]
90    NonConstOrOverrideInitializer,
91    #[error("Local variable has a type `ray_query` and so cannot be initialized.")]
92    RayQueryWithInitializeExpression,
93    #[error("Local variable has a type `hit_object` and so cannot be initialized.")]
94    HitObjectWithInitializeExpression,
95}
96
97#[derive(Clone, Debug, thiserror::Error)]
98#[cfg_attr(test, derive(PartialEq))]
99pub enum FunctionError {
100    #[error("Expression {handle:?} is invalid")]
101    Expression {
102        handle: Handle<crate::Expression>,
103        source: ExpressionError,
104    },
105    #[error("Expression {0:?} can't be introduced - it's already in scope")]
106    ExpressionAlreadyInScope(Handle<crate::Expression>),
107    #[error("Local variable {handle:?} '{name}' is invalid")]
108    LocalVariable {
109        handle: Handle<crate::LocalVariable>,
110        name: String,
111        source: LocalVariableError,
112    },
113    #[error("Argument '{name}' at index {index} has a type that can't be passed into functions.")]
114    InvalidArgumentType { index: usize, name: String },
115    #[error("The function's given return type cannot be returned from functions")]
116    NonConstructibleReturnType,
117    #[error("Argument '{name}' at index {index} is a pointer of space {space:?}, which can't be passed into functions.")]
118    InvalidArgumentPointerSpace {
119        index: usize,
120        name: String,
121        space: crate::AddressSpace,
122    },
123    #[error("The `break` is used outside of a `loop` or `switch` context")]
124    BreakOutsideOfLoopOrSwitch,
125    #[error("The `continue` is used outside of a `loop` context")]
126    ContinueOutsideOfLoop,
127    #[error("The `return` is called within a `continuing` block")]
128    InvalidReturnSpot,
129    #[error("The `return` expression {expression:?} does not match the declared return type {expected_ty:?}")]
130    InvalidReturnType {
131        expression: Option<Handle<crate::Expression>>,
132        expected_ty: Option<Handle<crate::Type>>,
133    },
134    #[error("The `if` condition {0:?} is not a boolean scalar")]
135    InvalidIfType(Handle<crate::Expression>),
136    #[error("The `switch` value {0:?} is not an integer scalar")]
137    InvalidSwitchType(Handle<crate::Expression>),
138    #[error("Multiple `switch` cases for {0:?} are present")]
139    ConflictingSwitchCase(crate::SwitchValue),
140    #[error("The `switch` contains cases with conflicting types")]
141    ConflictingCaseType,
142    #[error("The `switch` is missing a `default` case")]
143    MissingDefaultCase,
144    #[error("Multiple `default` cases are present")]
145    MultipleDefaultCases,
146    #[error("The last `switch` case contains a `fallthrough`")]
147    LastCaseFallTrough,
148    #[error("The pointer {0:?} doesn't relate to a valid destination for a store")]
149    InvalidStorePointer(Handle<crate::Expression>),
150    #[error("Image store texture parameter type mismatch")]
151    InvalidStoreTexture {
152        actual: Handle<crate::Expression>,
153        actual_ty: crate::TypeInner,
154    },
155    #[error("Image store value parameter type mismatch")]
156    InvalidStoreValue {
157        actual: Handle<crate::Expression>,
158        actual_ty: crate::TypeInner,
159        expected_ty: crate::TypeInner,
160    },
161    #[error("The type of {value:?} doesn't match the type stored in {pointer:?}")]
162    InvalidStoreTypes {
163        pointer: Handle<crate::Expression>,
164        value: Handle<crate::Expression>,
165    },
166    #[error("Image store parameters are invalid")]
167    InvalidImageStore(#[source] ExpressionError),
168    #[error("Image atomic parameters are invalid")]
169    InvalidImageAtomic(#[source] ExpressionError),
170    #[error("Image atomic function is invalid")]
171    InvalidImageAtomicFunction(crate::AtomicFunction),
172    #[error("Image atomic value is invalid")]
173    InvalidImageAtomicValue(Handle<crate::Expression>),
174    #[error("Call to {function:?} is invalid")]
175    InvalidCall {
176        function: Handle<crate::Function>,
177        #[source]
178        error: CallError,
179    },
180    #[error("Atomic operation is invalid")]
181    InvalidAtomic(#[from] AtomicError),
182    #[error("Ray Query {0:?} is not a local variable")]
183    InvalidRayQueryExpression(Handle<crate::Expression>),
184    #[error("Acceleration structure {0:?} is not a matching expression")]
185    InvalidAccelerationStructure(Handle<crate::Expression>),
186    #[error(
187        "Acceleration structure {0:?} is missing flag vertex_return while Ray Query {1:?} does"
188    )]
189    MissingAccelerationStructureVertexReturn(Handle<crate::Expression>, Handle<crate::Expression>),
190    #[error("Ray Query {0:?} is missing flag vertex_return")]
191    MissingRayQueryVertexReturn(Handle<crate::Expression>),
192    #[error("Ray descriptor {0:?} is not a matching expression")]
193    InvalidRayDescriptor(Handle<crate::Expression>),
194    #[error("Ray Query {0:?} does not have a matching type")]
195    InvalidRayQueryType(Handle<crate::Type>),
196    #[error("Hit distance {0:?} must be an f32")]
197    InvalidHitDistanceType(Handle<crate::Expression>),
198    #[error("Shader requires capability {0:?}")]
199    MissingCapability(super::Capabilities),
200    #[error(
201        "Required uniformity of control flow for {0:?} in {1:?} is not fulfilled because of {2:?}"
202    )]
203    NonUniformControlFlow(
204        UniformityRequirements,
205        Handle<crate::Expression>,
206        UniformityDisruptor,
207    ),
208    #[error("Functions that are not entry points cannot have `@location` or `@builtin` attributes on their arguments: \"{name}\" has attributes")]
209    PipelineInputRegularFunction { name: String },
210    #[error("Functions that are not entry points cannot have `@location` or `@builtin` attributes on their return value types")]
211    PipelineOutputRegularFunction,
212    #[error("Required uniformity for WorkGroupUniformLoad is not fulfilled because of {0:?}")]
213    // The actual load statement will be "pointed to" by the span
214    NonUniformWorkgroupUniformLoad(UniformityDisruptor),
215    // This is only possible with a misbehaving frontend
216    #[error("The expression {0:?} for a WorkGroupUniformLoad isn't a WorkgroupUniformLoadResult")]
217    WorkgroupUniformLoadExpressionMismatch(Handle<crate::Expression>),
218    #[error("The expression {0:?} is not valid as a WorkGroupUniformLoad argument. It should be a Pointer in Workgroup address space")]
219    WorkgroupUniformLoadInvalidPointer(Handle<crate::Expression>),
220    #[error("Subgroup operation is invalid")]
221    InvalidSubgroup(#[from] SubgroupError),
222    #[error("Invalid target type for a cooperative store")]
223    InvalidCooperativeStoreTarget(Handle<crate::Expression>),
224    #[error("Cooperative load/store data pointer has invalid type")]
225    InvalidCooperativeDataPointer(Handle<crate::Expression>),
226    #[error("Emit statement should not cover \"result\" expressions like {0:?}")]
227    EmitResult(Handle<crate::Expression>),
228    #[error("Expression not visited by the appropriate statement")]
229    UnvisitedExpression(Handle<crate::Expression>),
230    #[error("Expression {0:?} in mesh shader intrinsic call should be `u32` (is the expression a signed integer?)")]
231    InvalidMeshFunctionCall(Handle<crate::Expression>),
232    #[error("Mesh output types differ from {0:?} to {1:?}")]
233    ConflictingMeshOutputTypes(Handle<crate::Expression>, Handle<crate::Expression>),
234    #[error("Task payload variables differ from {0:?} to {1:?}")]
235    ConflictingTaskPayloadVariables(Handle<crate::Expression>, Handle<crate::Expression>),
236    #[error("Mesh shader output at {0:?} is not a user-defined struct")]
237    InvalidMeshShaderOutputType(Handle<crate::Expression>),
238    #[error("The payload type passed to `traceRay` must be a pointer")]
239    InvalidPayloadType,
240    #[error("The payload type passed to `traceRay` must be a pointer with an address space of `ray_payload` or `incoming_ray_payload`, instead got {0:?}")]
241    InvalidPayloadAddressSpace(crate::AddressSpace),
242    #[error("The payload type ({0:?}) passed to `traceRay` does not match the previous one {1:?}")]
243    MismatchedPayloadType(Handle<crate::Type>, Handle<crate::Type>),
244    #[error("The payload passed to `traceRay` must be a pointer directly to a global variable")]
245    PayloadPointerNotGlobal,
246    #[error("Argument {argument:?} for `debugPrintf` has type {actual}, which does not match format specifier %{specifier}")]
247    InvalidDebugPrintfArgument {
248        argument: Handle<crate::Expression>,
249        specifier: String,
250        actual: String,
251    },
252    #[error("Invalid format specifier %{0} for `debugPrintf`")]
253    InvalidDebugPrintfSpecifier(String),
254    #[error("Number of `debugPrintf` arguments ({0}) does not match number of specifiers ({1})")]
255    WrongDebugPrintfArgumentCount(usize, usize),
256    #[error("Tried to store to pointer {0:?} which is a ray query and so cannot be assigned to")]
257    RayQueryStore(Handle<crate::Expression>),
258    #[error("Tried to store to pointer {0:?} which is a hit object and so cannot be assigned to")]
259    HitObjectStore(Handle<crate::Expression>),
260    #[error("Hit object {0:?} is not a local variable")]
261    InvalidHitObjectExpression(Handle<crate::Expression>),
262    #[error("Hit object {0:?} does not have a matching type")]
263    InvalidHitObjectType(Handle<crate::Type>),
264    #[error("Expression {0:?} passed to a reordering operation should be a `u32`")]
265    InvalidReorderOperand(Handle<crate::Expression>),
266}
267
268bitflags::bitflags! {
269    #[repr(transparent)]
270    #[derive(Clone, Copy)]
271    struct ControlFlowAbility: u8 {
272        /// The control can return out of this block.
273        const RETURN = 0x1;
274        /// The control can break.
275        const BREAK = 0x2;
276        /// The control can continue.
277        const CONTINUE = 0x4;
278    }
279}
280
281struct BlockInfo {
282    stages: super::ShaderStages,
283}
284
285struct BlockContext<'a> {
286    abilities: ControlFlowAbility,
287    info: &'a FunctionInfo,
288    expressions: &'a Arena<crate::Expression>,
289    types: &'a UniqueArena<crate::Type>,
290    local_vars: &'a Arena<crate::LocalVariable>,
291    global_vars: &'a Arena<crate::GlobalVariable>,
292    functions: &'a Arena<crate::Function>,
293    special_types: &'a crate::SpecialTypes,
294    prev_infos: &'a [FunctionInfo],
295    return_type: Option<Handle<crate::Type>>,
296    local_expr_kind: &'a crate::proc::ExpressionKindTracker,
297    gctx: crate::proc::GlobalCtx<'a>,
298}
299
300impl<'a> BlockContext<'a> {
301    fn new(
302        fun: &'a crate::Function,
303        module: &'a crate::Module,
304        info: &'a FunctionInfo,
305        prev_infos: &'a [FunctionInfo],
306        local_expr_kind: &'a crate::proc::ExpressionKindTracker,
307    ) -> Self {
308        Self {
309            abilities: ControlFlowAbility::RETURN,
310            info,
311            expressions: &fun.expressions,
312            types: &module.types,
313            local_vars: &fun.local_variables,
314            global_vars: &module.global_variables,
315            functions: &module.functions,
316            special_types: &module.special_types,
317            prev_infos,
318            return_type: fun.result.as_ref().map(|fr| fr.ty),
319            local_expr_kind,
320            gctx: module.to_ctx(),
321        }
322    }
323
324    const fn with_abilities(&self, abilities: ControlFlowAbility) -> Self {
325        BlockContext { abilities, ..*self }
326    }
327
328    fn get_expression(&self, handle: Handle<crate::Expression>) -> &'a crate::Expression {
329        &self.expressions[handle]
330    }
331
332    fn resolve_type_impl(
333        &self,
334        handle: Handle<crate::Expression>,
335        valid_expressions: &HandleSet<crate::Expression>,
336    ) -> Result<&TypeResolution, WithSpan<ExpressionError>> {
337        if !valid_expressions.contains(handle) {
338            Err(ExpressionError::NotInScope.with_span_handle(handle, self.expressions))
339        } else {
340            Ok(&self.info[handle].ty)
341        }
342    }
343
344    fn resolve_type(
345        &self,
346        handle: Handle<crate::Expression>,
347        valid_expressions: &HandleSet<crate::Expression>,
348    ) -> Result<&TypeResolution, WithSpan<FunctionError>> {
349        self.resolve_type_impl(handle, valid_expressions)
350            .map_err_inner(|source| FunctionError::Expression { handle, source }.with_span())
351    }
352
353    fn resolve_type_inner(
354        &self,
355        handle: Handle<crate::Expression>,
356        valid_expressions: &HandleSet<crate::Expression>,
357    ) -> Result<&crate::TypeInner, WithSpan<FunctionError>> {
358        self.resolve_type(handle, valid_expressions)
359            .map(|tr| tr.inner_with(self.types))
360    }
361
362    fn resolve_pointer_type(&self, handle: Handle<crate::Expression>) -> &crate::TypeInner {
363        self.info[handle].ty.inner_with(self.types)
364    }
365
366    fn compare_types(&self, lhs: &TypeResolution, rhs: &TypeResolution) -> bool {
367        crate::proc::compare_types(lhs, rhs, self.types)
368    }
369}
370
371impl super::Validator {
372    fn validate_call(
373        &mut self,
374        function: Handle<crate::Function>,
375        arguments: &[Handle<crate::Expression>],
376        result: Option<Handle<crate::Expression>>,
377        context: &BlockContext,
378    ) -> Result<super::ShaderStages, WithSpan<CallError>> {
379        let fun = &context.functions[function];
380        if fun.arguments.len() != arguments.len() {
381            return Err(CallError::ArgumentCount {
382                required: fun.arguments.len(),
383                seen: arguments.len(),
384            }
385            .with_span());
386        }
387        for (index, (arg, &expr)) in fun.arguments.iter().zip(arguments).enumerate() {
388            let ty = context
389                .resolve_type_impl(expr, &self.valid_expression_set)
390                .map_err_inner(|source| {
391                    CallError::Argument { index, source }
392                        .with_span_handle(expr, context.expressions)
393                })?;
394            if !context.compare_types(&TypeResolution::Handle(arg.ty), ty) {
395                return Err(CallError::ArgumentType {
396                    index,
397                    required: arg.ty,
398                    seen_expression: expr,
399                }
400                .with_span_handle(expr, context.expressions));
401            }
402        }
403
404        if let Some(expr) = result {
405            if self.valid_expression_set.insert(expr) {
406                self.valid_expression_list.push(expr);
407            } else {
408                return Err(CallError::ResultAlreadyInScope(expr)
409                    .with_span_handle(expr, context.expressions));
410            }
411            match context.expressions[expr] {
412                crate::Expression::CallResult(callee)
413                    if fun.result.is_some() && callee == function =>
414                {
415                    if !self.needs_visit.remove(expr) {
416                        return Err(CallError::ResultAlreadyPopulated(expr)
417                            .with_span_handle(expr, context.expressions));
418                    }
419                }
420                _ => {
421                    return Err(CallError::ExpressionMismatch(result)
422                        .with_span_handle(expr, context.expressions))
423                }
424            }
425        } else if fun.result.is_some() {
426            return Err(CallError::ExpressionMismatch(result).with_span());
427        }
428
429        let callee_info = &context.prev_infos[function.index()];
430        Ok(callee_info.available_stages)
431    }
432
433    fn emit_expression(
434        &mut self,
435        handle: Handle<crate::Expression>,
436        context: &BlockContext,
437    ) -> Result<(), WithSpan<FunctionError>> {
438        if self.valid_expression_set.insert(handle) {
439            self.valid_expression_list.push(handle);
440            Ok(())
441        } else {
442            Err(FunctionError::ExpressionAlreadyInScope(handle)
443                .with_span_handle(handle, context.expressions))
444        }
445    }
446
447    fn validate_atomic(
448        &mut self,
449        pointer: Handle<crate::Expression>,
450        fun: &crate::AtomicFunction,
451        value: Handle<crate::Expression>,
452        result: Option<Handle<crate::Expression>>,
453        span: crate::Span,
454        context: &BlockContext,
455    ) -> Result<(), WithSpan<FunctionError>> {
456        // The `pointer` operand must be a pointer to an atomic value.
457        let pointer_inner = context.resolve_type_inner(pointer, &self.valid_expression_set)?;
458        let crate::TypeInner::Pointer {
459            base: pointer_base,
460            space: pointer_space,
461        } = *pointer_inner
462        else {
463            log::error!("Atomic operation on type {:?}", *pointer_inner);
464            return Err(AtomicError::InvalidPointer(pointer)
465                .with_span_handle(pointer, context.expressions)
466                .into_other());
467        };
468        let crate::TypeInner::Atomic(pointer_scalar) = context.types[pointer_base].inner else {
469            log::error!(
470                "Atomic pointer to type {:?}",
471                context.types[pointer_base].inner
472            );
473            return Err(AtomicError::InvalidPointer(pointer)
474                .with_span_handle(pointer, context.expressions)
475                .into_other());
476        };
477
478        // The `value` operand must be a scalar of the same type as the atomic.
479        let value_inner = context.resolve_type_inner(value, &self.valid_expression_set)?;
480        let crate::TypeInner::Scalar(value_scalar) = *value_inner else {
481            log::error!("Atomic operand type {:?}", *value_inner);
482            return Err(AtomicError::InvalidOperand(value)
483                .with_span_handle(value, context.expressions)
484                .into_other());
485        };
486        if pointer_scalar != value_scalar {
487            log::error!("Atomic operand type {:?}", *value_inner);
488            return Err(AtomicError::InvalidOperand(value)
489                .with_span_handle(value, context.expressions)
490                .into_other());
491        }
492
493        match pointer_scalar {
494            // Check for the special restrictions on 64-bit atomic operations.
495            //
496            // We don't need to consider other widths here: this function has already checked
497            // that `pointer`'s type is an `Atomic`, and `validate_type` has already checked
498            // that `Atomic` type has a permitted scalar width.
499            crate::Scalar::I64 | crate::Scalar::U64 => {
500                // `Capabilities::SHADER_INT64_ATOMIC_ALL_OPS` enables all sorts of 64-bit
501                // atomic operations.
502                if self
503                    .capabilities
504                    .contains(super::Capabilities::SHADER_INT64_ATOMIC_ALL_OPS)
505                {
506                    // okay
507                } else {
508                    // `Capabilities::SHADER_INT64_ATOMIC_MIN_MAX` allows `Min` and
509                    // `Max` on operations in `Storage`, without a return value.
510                    if matches!(
511                        *fun,
512                        crate::AtomicFunction::Min | crate::AtomicFunction::Max
513                    ) && matches!(pointer_space, crate::AddressSpace::Storage { .. })
514                        && result.is_none()
515                    {
516                        if !self
517                            .capabilities
518                            .contains(super::Capabilities::SHADER_INT64_ATOMIC_MIN_MAX)
519                        {
520                            log::error!("Int64 min-max atomic operations are not supported");
521                            return Err(AtomicError::MissingCapability(
522                                super::Capabilities::SHADER_INT64_ATOMIC_MIN_MAX,
523                            )
524                            .with_span_handle(value, context.expressions)
525                            .into_other());
526                        }
527                    } else {
528                        // Otherwise, we require the full 64-bit atomic capability.
529                        log::error!("Int64 atomic operations are not supported");
530                        return Err(AtomicError::MissingCapability(
531                            super::Capabilities::SHADER_INT64_ATOMIC_ALL_OPS,
532                        )
533                        .with_span_handle(value, context.expressions)
534                        .into_other());
535                    }
536                }
537            }
538            // Check for the special restrictions on 32-bit floating-point atomic operations.
539            crate::Scalar::F32 => {
540                // `Capabilities::SHADER_FLOAT32_ATOMIC` allows 32-bit floating-point
541                // atomic operations `Add`, `Subtract`, and `Exchange`
542                // in the `Storage` address space.
543                if !self
544                    .capabilities
545                    .contains(super::Capabilities::SHADER_FLOAT32_ATOMIC)
546                {
547                    log::error!("Float32 atomic operations are not supported");
548                    return Err(AtomicError::MissingCapability(
549                        super::Capabilities::SHADER_FLOAT32_ATOMIC,
550                    )
551                    .with_span_handle(value, context.expressions)
552                    .into_other());
553                }
554                if !matches!(
555                    *fun,
556                    crate::AtomicFunction::Add
557                        | crate::AtomicFunction::Subtract
558                        | crate::AtomicFunction::Exchange { compare: None }
559                ) {
560                    log::error!("Float32 atomic operation {fun:?} is not supported");
561                    return Err(AtomicError::InvalidOperator(*fun)
562                        .with_span_handle(value, context.expressions)
563                        .into_other());
564                }
565                if !matches!(pointer_space, crate::AddressSpace::Storage { .. }) {
566                    log::error!(
567                        "Float32 atomic operations are only supported in the Storage address space"
568                    );
569                    return Err(AtomicError::InvalidAddressSpace(pointer_space)
570                        .with_span_handle(value, context.expressions)
571                        .into_other());
572                }
573            }
574            _ => {}
575        }
576
577        // The result expression must be appropriate to the operation.
578        match result {
579            Some(result) => {
580                // The `result` handle must refer to an `AtomicResult` expression.
581                let crate::Expression::AtomicResult {
582                    ty: result_ty,
583                    comparison,
584                } = context.expressions[result]
585                else {
586                    return Err(AtomicError::InvalidResultExpression(result)
587                        .with_span_handle(result, context.expressions)
588                        .into_other());
589                };
590
591                // Note that this expression has been visited by the proper kind
592                // of statement.
593                if !self.needs_visit.remove(result) {
594                    return Err(AtomicError::ResultAlreadyPopulated(result)
595                        .with_span_handle(result, context.expressions)
596                        .into_other());
597                }
598
599                // The constraints on the result type depend on the atomic function.
600                if let crate::AtomicFunction::Exchange {
601                    compare: Some(compare),
602                } = *fun
603                {
604                    // The comparison value must be a scalar of the same type as the
605                    // atomic we're operating on.
606                    let compare_inner =
607                        context.resolve_type_inner(compare, &self.valid_expression_set)?;
608                    if !compare_inner.non_struct_equivalent(value_inner, context.types) {
609                        log::error!(
610                            "Atomic exchange comparison has a different type from the value"
611                        );
612                        return Err(AtomicError::InvalidOperand(compare)
613                            .with_span_handle(compare, context.expressions)
614                            .into_other());
615                    }
616
617                    // The result expression must be an `__atomic_compare_exchange_result`
618                    // struct whose `old_value` member is of the same type as the atomic
619                    // we're operating on.
620                    let crate::TypeInner::Struct { ref members, .. } =
621                        context.types[result_ty].inner
622                    else {
623                        return Err(AtomicError::ResultTypeMismatch(result)
624                            .with_span_handle(result, context.expressions)
625                            .into_other());
626                    };
627                    if !super::validate_atomic_compare_exchange_struct(
628                        context.types,
629                        members,
630                        |ty: &crate::TypeInner| *ty == crate::TypeInner::Scalar(pointer_scalar),
631                    ) {
632                        return Err(AtomicError::ResultTypeMismatch(result)
633                            .with_span_handle(result, context.expressions)
634                            .into_other());
635                    }
636
637                    // The result expression must be for a comparison operation.
638                    if !comparison {
639                        return Err(AtomicError::ResultExpressionNotExchange(result)
640                            .with_span_handle(result, context.expressions)
641                            .into_other());
642                    }
643                } else {
644                    // The result expression must be a scalar of the same type as the
645                    // atomic we're operating on.
646                    let result_inner = &context.types[result_ty].inner;
647                    if !result_inner.non_struct_equivalent(value_inner, context.types) {
648                        return Err(AtomicError::ResultTypeMismatch(result)
649                            .with_span_handle(result, context.expressions)
650                            .into_other());
651                    }
652
653                    // The result expression must not be for a comparison.
654                    if comparison {
655                        return Err(AtomicError::ResultExpressionExchange(result)
656                            .with_span_handle(result, context.expressions)
657                            .into_other());
658                    }
659                }
660                self.emit_expression(result, context)?;
661            }
662
663            None => {
664                // Exchange operations must always produce a value.
665                if let crate::AtomicFunction::Exchange { compare: None } = *fun {
666                    log::error!("Atomic exchange's value is unused");
667                    return Err(AtomicError::MissingReturnValue
668                        .with_span_static(span, "atomic exchange operation")
669                        .into_other());
670                }
671            }
672        }
673
674        Ok(())
675    }
676    fn validate_subgroup_operation(
677        &mut self,
678        op: &crate::SubgroupOperation,
679        collective_op: &crate::CollectiveOperation,
680        argument: Handle<crate::Expression>,
681        result: Handle<crate::Expression>,
682        context: &BlockContext,
683    ) -> Result<(), WithSpan<FunctionError>> {
684        let argument_inner = context.resolve_type_inner(argument, &self.valid_expression_set)?;
685
686        let (is_scalar, scalar) = match *argument_inner {
687            crate::TypeInner::Scalar(scalar) => (true, scalar),
688            crate::TypeInner::Vector { scalar, .. } => (false, scalar),
689            _ => {
690                log::error!("Subgroup operand type {argument_inner:?}");
691                return Err(SubgroupError::InvalidOperand(argument)
692                    .with_span_handle(argument, context.expressions)
693                    .into_other());
694            }
695        };
696
697        use crate::ScalarKind as sk;
698        use crate::SubgroupOperation as sg;
699        match (scalar.kind, *op) {
700            (sk::Bool, sg::All | sg::Any) if is_scalar => {}
701            (sk::Sint | sk::Uint | sk::Float, sg::Add | sg::Mul | sg::Min | sg::Max) => {}
702            // Subgroup bitwise ops require >= 32-bit integers because HLSL's
703            // WaveActiveBitAnd/Or/Xor don't support 16-bit types.
704            (sk::Sint | sk::Uint, sg::And | sg::Or | sg::Xor) if scalar.width >= 4 => {}
705
706            (_, _) => {
707                log::error!("Subgroup operand type {argument_inner:?}");
708                return Err(SubgroupError::InvalidOperand(argument)
709                    .with_span_handle(argument, context.expressions)
710                    .into_other());
711            }
712        };
713
714        use crate::CollectiveOperation as co;
715        match (*collective_op, *op) {
716            (
717                co::Reduce,
718                sg::All
719                | sg::Any
720                | sg::Add
721                | sg::Mul
722                | sg::Min
723                | sg::Max
724                | sg::And
725                | sg::Or
726                | sg::Xor,
727            ) => {}
728            (co::InclusiveScan | co::ExclusiveScan, sg::Add | sg::Mul) => {}
729
730            (_, _) => {
731                return Err(SubgroupError::UnknownOperation.with_span().into_other());
732            }
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    fn validate_subgroup_gather(
748        &mut self,
749        mode: &crate::GatherMode,
750        argument: Handle<crate::Expression>,
751        result: Handle<crate::Expression>,
752        context: &BlockContext,
753    ) -> Result<(), WithSpan<FunctionError>> {
754        match *mode {
755            crate::GatherMode::BroadcastFirst => {}
756            crate::GatherMode::Broadcast(index)
757            | crate::GatherMode::Shuffle(index)
758            | crate::GatherMode::ShuffleDown(index)
759            | crate::GatherMode::ShuffleUp(index)
760            | crate::GatherMode::ShuffleXor(index)
761            | crate::GatherMode::QuadBroadcast(index) => {
762                let index_ty = context.resolve_type_inner(index, &self.valid_expression_set)?;
763                match *index_ty {
764                    crate::TypeInner::Scalar(crate::Scalar::U32) => {}
765                    _ => {
766                        log::error!(
767                            "Subgroup gather index type {index_ty:?}, expected unsigned int"
768                        );
769                        return Err(SubgroupError::InvalidOperand(argument)
770                            .with_span_handle(index, context.expressions)
771                            .into_other());
772                    }
773                }
774            }
775            crate::GatherMode::QuadSwap(_) => {}
776        }
777        match *mode {
778            crate::GatherMode::Broadcast(index) | crate::GatherMode::QuadBroadcast(index)
779                if !context.local_expr_kind.is_const(index) =>
780            {
781                return Err(SubgroupError::InvalidInvocationIdExprType(index)
782                    .with_span_handle(index, context.expressions)
783                    .into_other());
784            }
785            _ => {}
786        }
787        let argument_inner = context.resolve_type_inner(argument, &self.valid_expression_set)?;
788        if !matches!(*argument_inner,
789            crate::TypeInner::Scalar ( scalar, .. ) | crate::TypeInner::Vector { scalar, .. }
790            if matches!(scalar.kind, crate::ScalarKind::Uint | crate::ScalarKind::Sint | crate::ScalarKind::Float)
791        ) {
792            log::error!("Subgroup gather operand type {argument_inner:?}");
793            return Err(SubgroupError::InvalidOperand(argument)
794                .with_span_handle(argument, context.expressions)
795                .into_other());
796        }
797
798        self.emit_expression(result, context)?;
799        match context.expressions[result] {
800            crate::Expression::SubgroupOperationResult { ty }
801                if { &context.types[ty].inner == argument_inner } => {}
802            _ => {
803                return Err(SubgroupError::ResultTypeMismatch(result)
804                    .with_span_handle(result, context.expressions)
805                    .into_other())
806            }
807        }
808        Ok(())
809    }
810    fn validate_subgroup_ballot_find_bit(
811        &mut self,
812        argument: Handle<crate::Expression>,
813        result: Handle<crate::Expression>,
814        context: &BlockContext,
815    ) -> Result<(), WithSpan<FunctionError>> {
816        let argument_inner = context.resolve_type_inner(argument, &self.valid_expression_set)?;
817        if !matches!(
818            *argument_inner,
819            crate::TypeInner::Vector {
820                size: crate::VectorSize::Quad,
821                scalar: crate::Scalar {
822                    kind: crate::ScalarKind::Uint,
823                    width: 4,
824                },
825            }
826        ) {
827            log::error!(
828                "Subgroup ballot find-bit operand type {argument_inner:?}, expected vec4<u32>"
829            );
830            return Err(SubgroupError::InvalidOperand(argument)
831                .with_span_handle(argument, context.expressions)
832                .into_other());
833        }
834
835        self.emit_expression(result, context)?;
836        match context.expressions[result] {
837            crate::Expression::SubgroupOperationResult { ty }
838                if context.types[ty].inner == crate::TypeInner::Scalar(crate::Scalar::U32) => {}
839            _ => {
840                return Err(SubgroupError::ResultTypeMismatch(result)
841                    .with_span_handle(result, context.expressions)
842                    .into_other())
843            }
844        }
845        Ok(())
846    }
847
848    #[allow(clippy::large_stack_frames)] // TODO(https://github.com/gfx-rs/wgpu/issues/9456)
849    fn validate_block_impl(
850        &mut self,
851        statements: &crate::Block,
852        context: &BlockContext,
853    ) -> Result<BlockInfo, WithSpan<FunctionError>> {
854        use crate::{AddressSpace, Statement as S, TypeInner as Ti};
855        let mut stages = super::ShaderStages::all();
856        for (statement, &span) in statements.span_iter() {
857            match *statement {
858                S::Emit(ref range) => {
859                    for handle in range.clone() {
860                        use crate::Expression as Ex;
861                        match context.expressions[handle] {
862                            Ex::Literal(_)
863                            | Ex::Constant(_)
864                            | Ex::Override(_)
865                            | Ex::ZeroValue(_)
866                            | Ex::Compose { .. }
867                            | Ex::Access { .. }
868                            | Ex::AccessIndex { .. }
869                            | Ex::Splat { .. }
870                            | Ex::Swizzle { .. }
871                            | Ex::FunctionArgument(_)
872                            | Ex::GlobalVariable(_)
873                            | Ex::LocalVariable(_)
874                            | Ex::Load { .. }
875                            | Ex::ImageSample { .. }
876                            | Ex::ImageLoad { .. }
877                            | Ex::ImageQuery { .. }
878                            | Ex::Unary { .. }
879                            | Ex::Binary { .. }
880                            | Ex::Select { .. }
881                            | Ex::Derivative { .. }
882                            | Ex::Relational { .. }
883                            | Ex::Math { .. }
884                            | Ex::As { .. }
885                            | Ex::Bitcast { .. }
886                            | Ex::ArrayLength(_)
887                            | Ex::RayQueryGetIntersection { .. }
888                            | Ex::RayQueryVertexPositions { .. }
889                            | Ex::HitObjectQuery { .. }
890                            | Ex::CooperativeLoad { .. }
891                            | Ex::CooperativeMultiplyAdd { .. } => {
892                                self.emit_expression(handle, context)?
893                            }
894                            Ex::CallResult(_)
895                            | Ex::AtomicResult { .. }
896                            | Ex::WorkGroupUniformLoadResult { .. }
897                            | Ex::RayQueryProceedResult
898                            | Ex::SubgroupBallotResult
899                            | Ex::SubgroupOperationResult { .. } => {
900                                return Err(FunctionError::EmitResult(handle)
901                                    .with_span_handle(handle, context.expressions));
902                            }
903                        }
904                    }
905                }
906                S::Block(ref block) => {
907                    let info = self.validate_block(block, context)?;
908                    stages &= info.stages;
909                }
910                S::If {
911                    condition,
912                    ref accept,
913                    ref reject,
914                } => {
915                    match *context.resolve_type_inner(condition, &self.valid_expression_set)? {
916                        Ti::Scalar(crate::Scalar {
917                            kind: crate::ScalarKind::Bool,
918                            width: _,
919                        }) => {}
920                        _ => {
921                            return Err(FunctionError::InvalidIfType(condition)
922                                .with_span_handle(condition, context.expressions))
923                        }
924                    }
925                    stages &= self.validate_block(accept, context)?.stages;
926                    stages &= self.validate_block(reject, context)?.stages;
927                }
928                S::Switch {
929                    selector,
930                    ref cases,
931                } => {
932                    let uint = match context
933                        .resolve_type_inner(selector, &self.valid_expression_set)?
934                        .scalar_kind()
935                    {
936                        Some(crate::ScalarKind::Uint) => true,
937                        Some(crate::ScalarKind::Sint) => false,
938                        _ => {
939                            return Err(FunctionError::InvalidSwitchType(selector)
940                                .with_span_handle(selector, context.expressions))
941                        }
942                    };
943                    self.switch_values.clear();
944                    for case in cases {
945                        match case.value {
946                            crate::SwitchValue::I32(_) if !uint => {}
947                            crate::SwitchValue::U32(_) if uint => {}
948                            crate::SwitchValue::Default => {}
949                            _ => {
950                                return Err(FunctionError::ConflictingCaseType.with_span_static(
951                                    case.body
952                                        .span_iter()
953                                        .next()
954                                        .map_or(Default::default(), |(_, s)| *s),
955                                    "conflicting switch arm here",
956                                ));
957                            }
958                        };
959                        if !self.switch_values.insert(case.value) {
960                            return Err(match case.value {
961                                crate::SwitchValue::Default => FunctionError::MultipleDefaultCases
962                                    .with_span_static(
963                                        case.body
964                                            .span_iter()
965                                            .next()
966                                            .map_or(Default::default(), |(_, s)| *s),
967                                        "duplicated switch arm here",
968                                    ),
969                                _ => FunctionError::ConflictingSwitchCase(case.value)
970                                    .with_span_static(
971                                        case.body
972                                            .span_iter()
973                                            .next()
974                                            .map_or(Default::default(), |(_, s)| *s),
975                                        "conflicting switch arm here",
976                                    ),
977                            });
978                        }
979                    }
980                    if !self.switch_values.contains(&crate::SwitchValue::Default) {
981                        return Err(FunctionError::MissingDefaultCase
982                            .with_span_static(span, "missing default case"));
983                    }
984                    if let Some(case) = cases.last() {
985                        if case.fall_through {
986                            return Err(FunctionError::LastCaseFallTrough.with_span_static(
987                                case.body
988                                    .span_iter()
989                                    .next()
990                                    .map_or(Default::default(), |(_, s)| *s),
991                                "bad switch arm here",
992                            ));
993                        }
994                    }
995                    let pass_through_abilities = context.abilities
996                        & (ControlFlowAbility::RETURN | ControlFlowAbility::CONTINUE);
997                    let sub_context =
998                        context.with_abilities(pass_through_abilities | ControlFlowAbility::BREAK);
999                    for case in cases {
1000                        stages &= self.validate_block(&case.body, &sub_context)?.stages;
1001                    }
1002                }
1003                S::Loop {
1004                    ref body,
1005                    ref continuing,
1006                    break_if,
1007                } => {
1008                    // special handling for block scoping is needed here,
1009                    // because the continuing{} block inherits the scope
1010                    let base_expression_count = self.valid_expression_list.len();
1011                    let pass_through_abilities = context.abilities & ControlFlowAbility::RETURN;
1012                    stages &= self
1013                        .validate_block_impl(
1014                            body,
1015                            &context.with_abilities(
1016                                pass_through_abilities
1017                                    | ControlFlowAbility::BREAK
1018                                    | ControlFlowAbility::CONTINUE,
1019                            ),
1020                        )?
1021                        .stages;
1022                    stages &= self
1023                        .validate_block_impl(
1024                            continuing,
1025                            &context.with_abilities(ControlFlowAbility::empty()),
1026                        )?
1027                        .stages;
1028
1029                    if let Some(condition) = break_if {
1030                        match *context.resolve_type_inner(condition, &self.valid_expression_set)? {
1031                            Ti::Scalar(crate::Scalar {
1032                                kind: crate::ScalarKind::Bool,
1033                                width: _,
1034                            }) => {}
1035                            _ => {
1036                                return Err(FunctionError::InvalidIfType(condition)
1037                                    .with_span_handle(condition, context.expressions))
1038                            }
1039                        }
1040                    }
1041
1042                    for handle in self.valid_expression_list.drain(base_expression_count..) {
1043                        self.valid_expression_set.remove(handle);
1044                    }
1045                }
1046                S::Break => {
1047                    if !context.abilities.contains(ControlFlowAbility::BREAK) {
1048                        return Err(FunctionError::BreakOutsideOfLoopOrSwitch
1049                            .with_span_static(span, "invalid break"));
1050                    }
1051                }
1052                S::Continue => {
1053                    if !context.abilities.contains(ControlFlowAbility::CONTINUE) {
1054                        return Err(FunctionError::ContinueOutsideOfLoop
1055                            .with_span_static(span, "invalid continue"));
1056                    }
1057                }
1058                S::Return { value } => {
1059                    if !context.abilities.contains(ControlFlowAbility::RETURN) {
1060                        return Err(FunctionError::InvalidReturnSpot
1061                            .with_span_static(span, "invalid return"));
1062                    }
1063                    let value_ty = value
1064                        .map(|expr| context.resolve_type(expr, &self.valid_expression_set))
1065                        .transpose()?;
1066                    // We can't return pointers, but it seems best not to embed that
1067                    // assumption here, so use `TypeInner::equivalent` for comparison.
1068                    let okay = match (value_ty, context.return_type) {
1069                        (None, None) => true,
1070                        (Some(value_inner), Some(expected_ty)) => {
1071                            context.compare_types(value_inner, &TypeResolution::Handle(expected_ty))
1072                        }
1073                        (_, _) => false,
1074                    };
1075
1076                    if !okay {
1077                        log::error!(
1078                            "Returning {:?} where {:?} is expected",
1079                            value_ty,
1080                            context.return_type,
1081                        );
1082                        if let Some(handle) = value {
1083                            return Err(FunctionError::InvalidReturnType {
1084                                expression: value,
1085                                expected_ty: context.return_type,
1086                            }
1087                            .with_span_handle(handle, context.expressions));
1088                        } else {
1089                            return Err(FunctionError::InvalidReturnType {
1090                                expression: value,
1091                                expected_ty: context.return_type,
1092                            }
1093                            .with_span_static(span, "invalid return"));
1094                        }
1095                    }
1096                }
1097                S::Kill => {
1098                    stages &= super::ShaderStages::FRAGMENT;
1099                }
1100                S::ControlBarrier(barrier) | S::MemoryBarrier(barrier) => {
1101                    stages &= super::ShaderStages::COMPUTE_LIKE;
1102                    if barrier.contains(crate::Barrier::SUB_GROUP) {
1103                        if !self.capabilities.contains(
1104                            super::Capabilities::SUBGROUP | super::Capabilities::SUBGROUP_BARRIER,
1105                        ) {
1106                            return Err(FunctionError::MissingCapability(
1107                                super::Capabilities::SUBGROUP
1108                                    | super::Capabilities::SUBGROUP_BARRIER,
1109                            )
1110                            .with_span_static(span, "missing capability for this operation"));
1111                        }
1112                        if !self
1113                            .subgroup_operations
1114                            .contains(super::SubgroupOperationSet::BASIC)
1115                        {
1116                            return Err(FunctionError::InvalidSubgroup(
1117                                SubgroupError::UnsupportedOperation(
1118                                    super::SubgroupOperationSet::BASIC,
1119                                ),
1120                            )
1121                            .with_span_static(span, "support for this operation is not present"));
1122                        }
1123                    }
1124                }
1125                S::Store { pointer, value } => {
1126                    let mut current = pointer;
1127                    loop {
1128                        match context.expressions[current] {
1129                            crate::Expression::Access { base, .. }
1130                            | crate::Expression::AccessIndex { base, .. } => current = base,
1131                            crate::Expression::LocalVariable(_)
1132                            | crate::Expression::GlobalVariable(_)
1133                            | crate::Expression::FunctionArgument(_) => break,
1134                            _ => {
1135                                return Err(FunctionError::InvalidStorePointer(current)
1136                                    .with_span_handle(pointer, context.expressions))
1137                            }
1138                        }
1139                    }
1140
1141                    let value_tr = context.resolve_type(value, &self.valid_expression_set)?;
1142                    let value_ty = value_tr.inner_with(context.types);
1143                    match *value_ty {
1144                        Ti::Image { .. } | Ti::Sampler { .. } => {
1145                            return Err(FunctionError::InvalidStoreTexture {
1146                                actual: value,
1147                                actual_ty: value_ty.clone(),
1148                            }
1149                            .with_span_context((
1150                                context.expressions.get_span(value),
1151                                format!("this value is of type {value_ty:?}"),
1152                            ))
1153                            .with_span(span, "expects a texture argument"));
1154                        }
1155                        _ => {}
1156                    }
1157
1158                    let pointer_ty = context.resolve_pointer_type(pointer);
1159                    let pointer_base_tr = pointer_ty.pointer_base_type();
1160                    let pointer_base_ty = pointer_base_tr
1161                        .as_ref()
1162                        .map(|ty| ty.inner_with(context.types));
1163                    let good = if let Some(&Ti::Atomic(ref scalar)) = pointer_base_ty {
1164                        // The Naga IR allows storing a scalar to an atomic.
1165                        *value_ty == Ti::Scalar(*scalar)
1166                    } else if let Some(&Ti::RayQuery { .. }) = pointer_base_ty {
1167                        return Err(FunctionError::RayQueryStore(pointer)
1168                            .with_span_context((
1169                                context.expressions.get_span(pointer),
1170                                format!("this pointer has a base type of {pointer_base_ty:?} which cannot be stored to"),
1171                            ))
1172                            .with_span(span, "store to a type which is not allowed to be stored to"));
1173                    } else if let Some(&Ti::HitObject) = pointer_base_ty {
1174                        return Err(FunctionError::HitObjectStore(pointer)
1175                            .with_span_context((
1176                                context.expressions.get_span(pointer),
1177                                format!("this pointer has a base type of {pointer_base_ty:?} which cannot be stored to"),
1178                            ))
1179                            .with_span(span, "store to a type which is not allowed to be stored to"));
1180                    } else if let Some(tr) = pointer_base_tr {
1181                        context.compare_types(value_tr, &tr)
1182                    } else {
1183                        false
1184                    };
1185
1186                    if !good {
1187                        return Err(FunctionError::InvalidStoreTypes { pointer, value }
1188                            .with_span()
1189                            .with_handle(pointer, context.expressions)
1190                            .with_handle(value, context.expressions));
1191                    }
1192
1193                    if let Some(space) = pointer_ty.pointer_space() {
1194                        if !space.access().contains(crate::StorageAccess::STORE) {
1195                            return Err(FunctionError::InvalidStorePointer(pointer)
1196                                .with_span_static(
1197                                    context.expressions.get_span(pointer),
1198                                    "writing to this location is not permitted",
1199                                ));
1200                        }
1201                    }
1202                }
1203                S::ImageStore {
1204                    image,
1205                    coordinate,
1206                    array_index,
1207                    value,
1208                } => {
1209                    //Note: this code uses a lot of `FunctionError::InvalidImageStore`,
1210                    // and could probably be refactored.
1211                    let global_var;
1212                    let image_ty;
1213                    match *context.get_expression(image) {
1214                        crate::Expression::GlobalVariable(var_handle) => {
1215                            global_var = &context.global_vars[var_handle];
1216                            image_ty = global_var.ty;
1217                        }
1218                        // The `image` operand is indexing into a binding array,
1219                        // so punch through the `Access`* expression and look at
1220                        // the global behind it.
1221                        crate::Expression::Access { base, .. }
1222                        | crate::Expression::AccessIndex { base, .. } => {
1223                            let crate::Expression::GlobalVariable(var_handle) =
1224                                *context.get_expression(base)
1225                            else {
1226                                return Err(FunctionError::InvalidImageStore(
1227                                    ExpressionError::ExpectedGlobalVariable,
1228                                )
1229                                .with_span_handle(image, context.expressions));
1230                            };
1231                            global_var = &context.global_vars[var_handle];
1232
1233                            // The global variable must be a binding array.
1234                            let Ti::BindingArray { base, .. } = context.types[global_var.ty].inner
1235                            else {
1236                                return Err(FunctionError::InvalidImageStore(
1237                                    ExpressionError::ExpectedBindingArrayType(global_var.ty),
1238                                )
1239                                .with_span_handle(global_var.ty, context.types));
1240                            };
1241
1242                            image_ty = base;
1243                        }
1244                        _ => {
1245                            return Err(FunctionError::InvalidImageStore(
1246                                ExpressionError::ExpectedGlobalVariable,
1247                            )
1248                            .with_span_handle(image, context.expressions))
1249                        }
1250                    };
1251
1252                    // The `image` operand must be an `Image`.
1253                    let Ti::Image {
1254                        class,
1255                        arrayed,
1256                        dim,
1257                    } = context.types[image_ty].inner
1258                    else {
1259                        return Err(FunctionError::InvalidImageStore(
1260                            ExpressionError::ExpectedImageType(global_var.ty),
1261                        )
1262                        .with_span()
1263                        .with_handle(global_var.ty, context.types)
1264                        .with_handle(image, context.expressions));
1265                    };
1266
1267                    // It had better be a storage image, since we're writing to it.
1268                    let crate::ImageClass::Storage { format, .. } = class else {
1269                        return Err(FunctionError::InvalidImageStore(
1270                            ExpressionError::InvalidImageClass(class),
1271                        )
1272                        .with_span_handle(image, context.expressions));
1273                    };
1274
1275                    // The `coordinate` operand must be a vector of the appropriate size.
1276                    if context
1277                        .resolve_type_inner(coordinate, &self.valid_expression_set)?
1278                        .image_storage_coordinates()
1279                        .is_none_or(|coord_dim| coord_dim != dim)
1280                    {
1281                        return Err(FunctionError::InvalidImageStore(
1282                            ExpressionError::InvalidImageCoordinateType(dim, coordinate),
1283                        )
1284                        .with_span_handle(coordinate, context.expressions));
1285                    }
1286
1287                    // The `array_index` operand should be present if and only if
1288                    // the image itself is arrayed.
1289                    if arrayed != array_index.is_some() {
1290                        return Err(FunctionError::InvalidImageStore(
1291                            ExpressionError::InvalidImageArrayIndex,
1292                        )
1293                        .with_span_handle(coordinate, context.expressions));
1294                    }
1295
1296                    // If present, `array_index` must be a scalar integer type.
1297                    if let Some(expr) = array_index {
1298                        if !matches!(
1299                            *context.resolve_type_inner(expr, &self.valid_expression_set)?,
1300                            Ti::Scalar(crate::Scalar {
1301                                kind: crate::ScalarKind::Sint | crate::ScalarKind::Uint,
1302                                width: _,
1303                            })
1304                        ) {
1305                            return Err(FunctionError::InvalidImageStore(
1306                                ExpressionError::InvalidImageArrayIndexType(expr),
1307                            )
1308                            .with_span_handle(expr, context.expressions));
1309                        }
1310                    }
1311
1312                    let value_ty = crate::TypeInner::Vector {
1313                        size: crate::VectorSize::Quad,
1314                        scalar: format.into(),
1315                    };
1316
1317                    // The value we're writing had better match the scalar type
1318                    // for `image`'s format.
1319                    let actual_value_ty =
1320                        context.resolve_type_inner(value, &self.valid_expression_set)?;
1321                    if actual_value_ty != &value_ty {
1322                        return Err(FunctionError::InvalidStoreValue {
1323                            actual: value,
1324                            actual_ty: actual_value_ty.clone(),
1325                            expected_ty: value_ty.clone(),
1326                        }
1327                        .with_span_context((
1328                            context.expressions.get_span(value),
1329                            format!("this value is of type {actual_value_ty:?}"),
1330                        ))
1331                        .with_span(
1332                            span,
1333                            format!("expects a value argument of type {value_ty:?}"),
1334                        ));
1335                    }
1336                }
1337                S::Call {
1338                    function,
1339                    ref arguments,
1340                    result,
1341                } => match self.validate_call(function, arguments, result, context) {
1342                    Ok(callee_stages) => stages &= callee_stages,
1343                    Err(error) => {
1344                        return Err(error.and_then(|error| {
1345                            FunctionError::InvalidCall { function, error }
1346                                .with_span_static(span, "invalid function call")
1347                        }))
1348                    }
1349                },
1350                S::Atomic {
1351                    pointer,
1352                    ref fun,
1353                    value,
1354                    result,
1355                } => {
1356                    self.validate_atomic(pointer, fun, value, result, span, context)?;
1357                }
1358                S::ImageAtomic {
1359                    image,
1360                    coordinate,
1361                    array_index,
1362                    fun,
1363                    value,
1364                } => {
1365                    let var = match *context.get_expression(image) {
1366                        crate::Expression::GlobalVariable(var_handle) => {
1367                            &context.global_vars[var_handle]
1368                        }
1369                        // We're looking at a binding index situation, so punch through the index and look at the global behind it.
1370                        crate::Expression::Access { base, .. }
1371                        | crate::Expression::AccessIndex { base, .. } => {
1372                            match *context.get_expression(base) {
1373                                crate::Expression::GlobalVariable(var_handle) => {
1374                                    &context.global_vars[var_handle]
1375                                }
1376                                _ => {
1377                                    return Err(FunctionError::InvalidImageAtomic(
1378                                        ExpressionError::ExpectedGlobalVariable,
1379                                    )
1380                                    .with_span_handle(image, context.expressions))
1381                                }
1382                            }
1383                        }
1384                        _ => {
1385                            return Err(FunctionError::InvalidImageAtomic(
1386                                ExpressionError::ExpectedGlobalVariable,
1387                            )
1388                            .with_span_handle(image, context.expressions))
1389                        }
1390                    };
1391
1392                    // Punch through a binding array to get the underlying type
1393                    let global_ty = match context.types[var.ty].inner {
1394                        Ti::BindingArray { base, .. } => &context.types[base].inner,
1395                        ref inner => inner,
1396                    };
1397
1398                    let value_ty = match *global_ty {
1399                        Ti::Image {
1400                            class,
1401                            arrayed,
1402                            dim,
1403                        } => {
1404                            match context
1405                                .resolve_type_inner(coordinate, &self.valid_expression_set)?
1406                                .image_storage_coordinates()
1407                            {
1408                                Some(coord_dim) if coord_dim == dim => {}
1409                                _ => {
1410                                    return Err(FunctionError::InvalidImageAtomic(
1411                                        ExpressionError::InvalidImageCoordinateType(
1412                                            dim, coordinate,
1413                                        ),
1414                                    )
1415                                    .with_span_handle(coordinate, context.expressions));
1416                                }
1417                            };
1418                            if arrayed != array_index.is_some() {
1419                                return Err(FunctionError::InvalidImageAtomic(
1420                                    ExpressionError::InvalidImageArrayIndex,
1421                                )
1422                                .with_span_handle(coordinate, context.expressions));
1423                            }
1424                            if let Some(expr) = array_index {
1425                                match *context
1426                                    .resolve_type_inner(expr, &self.valid_expression_set)?
1427                                {
1428                                    Ti::Scalar(crate::Scalar {
1429                                        kind: crate::ScalarKind::Sint | crate::ScalarKind::Uint,
1430                                        width: _,
1431                                    }) => {}
1432                                    _ => {
1433                                        return Err(FunctionError::InvalidImageAtomic(
1434                                            ExpressionError::InvalidImageArrayIndexType(expr),
1435                                        )
1436                                        .with_span_handle(expr, context.expressions));
1437                                    }
1438                                }
1439                            }
1440                            match class {
1441                                crate::ImageClass::Storage { format, access } => {
1442                                    if !access.contains(crate::StorageAccess::ATOMIC) {
1443                                        return Err(FunctionError::InvalidImageAtomic(
1444                                            ExpressionError::InvalidImageStorageAccess(access),
1445                                        )
1446                                        .with_span_handle(image, context.expressions));
1447                                    }
1448                                    match format {
1449                                        crate::StorageFormat::R64Uint => {
1450                                            if !self.capabilities.intersects(
1451                                                super::Capabilities::TEXTURE_INT64_ATOMIC,
1452                                            ) {
1453                                                return Err(FunctionError::MissingCapability(
1454                                                    super::Capabilities::TEXTURE_INT64_ATOMIC,
1455                                                )
1456                                                .with_span_static(
1457                                                    span,
1458                                                    "missing capability for this operation",
1459                                                ));
1460                                            }
1461                                            match fun {
1462                                                crate::AtomicFunction::Min
1463                                                | crate::AtomicFunction::Max => {}
1464                                                _ => {
1465                                                    return Err(
1466                                                        FunctionError::InvalidImageAtomicFunction(
1467                                                            fun,
1468                                                        )
1469                                                        .with_span_handle(
1470                                                            image,
1471                                                            context.expressions,
1472                                                        ),
1473                                                    );
1474                                                }
1475                                            }
1476                                        }
1477                                        crate::StorageFormat::R32Sint
1478                                        | crate::StorageFormat::R32Uint => {
1479                                            if !self
1480                                                .capabilities
1481                                                .intersects(super::Capabilities::TEXTURE_ATOMIC)
1482                                            {
1483                                                return Err(FunctionError::MissingCapability(
1484                                                    super::Capabilities::TEXTURE_ATOMIC,
1485                                                )
1486                                                .with_span_static(
1487                                                    span,
1488                                                    "missing capability for this operation",
1489                                                ));
1490                                            }
1491                                            match fun {
1492                                                crate::AtomicFunction::Add
1493                                                | crate::AtomicFunction::And
1494                                                | crate::AtomicFunction::ExclusiveOr
1495                                                | crate::AtomicFunction::InclusiveOr
1496                                                | crate::AtomicFunction::Min
1497                                                | crate::AtomicFunction::Max => {}
1498                                                _ => {
1499                                                    return Err(
1500                                                        FunctionError::InvalidImageAtomicFunction(
1501                                                            fun,
1502                                                        )
1503                                                        .with_span_handle(
1504                                                            image,
1505                                                            context.expressions,
1506                                                        ),
1507                                                    );
1508                                                }
1509                                            }
1510                                        }
1511                                        _ => {
1512                                            return Err(FunctionError::InvalidImageAtomic(
1513                                                ExpressionError::InvalidImageFormat(format),
1514                                            )
1515                                            .with_span_handle(image, context.expressions));
1516                                        }
1517                                    }
1518                                    crate::TypeInner::Scalar(format.into())
1519                                }
1520                                _ => {
1521                                    return Err(FunctionError::InvalidImageAtomic(
1522                                        ExpressionError::InvalidImageClass(class),
1523                                    )
1524                                    .with_span_handle(image, context.expressions));
1525                                }
1526                            }
1527                        }
1528                        _ => {
1529                            return Err(FunctionError::InvalidImageAtomic(
1530                                ExpressionError::ExpectedImageType(var.ty),
1531                            )
1532                            .with_span()
1533                            .with_handle(var.ty, context.types)
1534                            .with_handle(image, context.expressions))
1535                        }
1536                    };
1537
1538                    if *context.resolve_type_inner(value, &self.valid_expression_set)? != value_ty {
1539                        return Err(FunctionError::InvalidImageAtomicValue(value)
1540                            .with_span_handle(value, context.expressions));
1541                    }
1542                }
1543                S::WorkGroupUniformLoad { pointer, result } => {
1544                    stages &= super::ShaderStages::COMPUTE_LIKE;
1545                    let pointer_inner =
1546                        context.resolve_type_inner(pointer, &self.valid_expression_set)?;
1547                    match *pointer_inner {
1548                        Ti::Pointer {
1549                            space: AddressSpace::WorkGroup,
1550                            ..
1551                        } => {}
1552                        Ti::ValuePointer {
1553                            space: AddressSpace::WorkGroup,
1554                            ..
1555                        } => {}
1556                        _ => {
1557                            return Err(FunctionError::WorkgroupUniformLoadInvalidPointer(pointer)
1558                                .with_span_static(span, "WorkGroupUniformLoad"))
1559                        }
1560                    }
1561                    self.emit_expression(result, context)?;
1562                    let ty = match &context.expressions[result] {
1563                        &crate::Expression::WorkGroupUniformLoadResult { ty } => ty,
1564                        _ => {
1565                            return Err(FunctionError::WorkgroupUniformLoadExpressionMismatch(
1566                                result,
1567                            )
1568                            .with_span_static(span, "WorkGroupUniformLoad"));
1569                        }
1570                    };
1571                    let expected_pointer_inner = Ti::Pointer {
1572                        base: ty,
1573                        space: AddressSpace::WorkGroup,
1574                    };
1575                    // workgroupUniformLoad on atomic<T> returns T, not atomic<T>.
1576                    // Verify the pointer's atomic scalar matches the result scalar.
1577                    let atomic_specialization_ok = match *pointer_inner {
1578                        Ti::Pointer {
1579                            base: pointer_base,
1580                            space: AddressSpace::WorkGroup,
1581                        } => match (&context.types[pointer_base].inner, &context.types[ty].inner) {
1582                            (&Ti::Atomic(pointer_scalar), &Ti::Scalar(result_scalar)) => {
1583                                pointer_scalar == result_scalar
1584                            }
1585                            _ => false,
1586                        },
1587                        _ => false,
1588                    };
1589                    if !expected_pointer_inner.non_struct_equivalent(pointer_inner, context.types)
1590                        && !atomic_specialization_ok
1591                    {
1592                        return Err(FunctionError::WorkgroupUniformLoadInvalidPointer(pointer)
1593                            .with_span_static(span, "WorkGroupUniformLoad"));
1594                    }
1595                }
1596                S::RayQuery { query, ref fun } => {
1597                    let rq_vertex_return = Self::validate_ray_query_variable(query, context, span)?;
1598                    match *fun {
1599                        crate::RayQueryFunction::Initialize {
1600                            acceleration_structure,
1601                            descriptor,
1602                        } => {
1603                            match *context.resolve_type_inner(
1604                                acceleration_structure,
1605                                &self.valid_expression_set,
1606                            )? {
1607                                Ti::AccelerationStructure { vertex_return } => {
1608                                    if (!vertex_return) && rq_vertex_return {
1609                                        return Err(FunctionError::MissingAccelerationStructureVertexReturn(acceleration_structure, query).with_span_static(span, "invalid acceleration structure"));
1610                                    }
1611                                }
1612                                _ => {
1613                                    return Err(FunctionError::InvalidAccelerationStructure(
1614                                        acceleration_structure,
1615                                    )
1616                                    .with_span_static(span, "invalid acceleration structure"))
1617                                }
1618                            }
1619                            let desc_ty_given = context
1620                                .resolve_type_inner(descriptor, &self.valid_expression_set)?;
1621                            let desc_ty_expected = context
1622                                .special_types
1623                                .ray_desc
1624                                .map(|handle| &context.types[handle].inner);
1625                            if Some(desc_ty_given) != desc_ty_expected {
1626                                return Err(FunctionError::InvalidRayDescriptor(descriptor)
1627                                    .with_span_static(span, "invalid ray descriptor"));
1628                            }
1629                        }
1630                        crate::RayQueryFunction::Proceed { result } => {
1631                            self.emit_expression(result, context)?;
1632                        }
1633                        crate::RayQueryFunction::GenerateIntersection { hit_t } => {
1634                            match *context.resolve_type_inner(hit_t, &self.valid_expression_set)? {
1635                                Ti::Scalar(crate::Scalar {
1636                                    kind: crate::ScalarKind::Float,
1637                                    width: _,
1638                                }) => {}
1639                                _ => {
1640                                    return Err(FunctionError::InvalidHitDistanceType(hit_t)
1641                                        .with_span_static(span, "invalid hit_t"))
1642                                }
1643                            }
1644                        }
1645                        crate::RayQueryFunction::ConfirmIntersection => {}
1646                        crate::RayQueryFunction::Terminate => {}
1647                        crate::RayQueryFunction::Begin => {}
1648                    }
1649                }
1650                S::SubgroupBallot { result, predicate } => {
1651                    stages &= self.subgroup_stages;
1652                    if !self.capabilities.contains(super::Capabilities::SUBGROUP) {
1653                        return Err(FunctionError::MissingCapability(
1654                            super::Capabilities::SUBGROUP,
1655                        )
1656                        .with_span_static(span, "missing capability for this operation"));
1657                    }
1658                    if !self
1659                        .subgroup_operations
1660                        .contains(super::SubgroupOperationSet::BALLOT)
1661                    {
1662                        return Err(FunctionError::InvalidSubgroup(
1663                            SubgroupError::UnsupportedOperation(
1664                                super::SubgroupOperationSet::BALLOT,
1665                            ),
1666                        )
1667                        .with_span_static(span, "support for this operation is not present"));
1668                    }
1669                    if let Some(predicate) = predicate {
1670                        let predicate_inner =
1671                            context.resolve_type_inner(predicate, &self.valid_expression_set)?;
1672                        if !matches!(
1673                            *predicate_inner,
1674                            crate::TypeInner::Scalar(crate::Scalar::BOOL,)
1675                        ) {
1676                            log::error!(
1677                                "Subgroup ballot predicate type {predicate_inner:?} expected bool"
1678                            );
1679                            return Err(SubgroupError::InvalidOperand(predicate)
1680                                .with_span_handle(predicate, context.expressions)
1681                                .into_other());
1682                        }
1683                    }
1684                    self.emit_expression(result, context)?;
1685                }
1686                S::SubgroupCollectiveOperation {
1687                    ref op,
1688                    ref collective_op,
1689                    argument,
1690                    result,
1691                } => {
1692                    stages &= self.subgroup_stages;
1693                    if !self.capabilities.contains(super::Capabilities::SUBGROUP) {
1694                        return Err(FunctionError::MissingCapability(
1695                            super::Capabilities::SUBGROUP,
1696                        )
1697                        .with_span_static(span, "missing capability for this operation"));
1698                    }
1699                    let operation = op.required_operations();
1700                    if !self.subgroup_operations.contains(operation) {
1701                        return Err(FunctionError::InvalidSubgroup(
1702                            SubgroupError::UnsupportedOperation(operation),
1703                        )
1704                        .with_span_static(span, "support for this operation is not present"));
1705                    }
1706                    self.validate_subgroup_operation(op, collective_op, argument, result, context)?;
1707                }
1708                S::SubgroupGather {
1709                    ref mode,
1710                    argument,
1711                    result,
1712                } => {
1713                    stages &= self.subgroup_stages;
1714                    if !self.capabilities.contains(super::Capabilities::SUBGROUP) {
1715                        return Err(FunctionError::MissingCapability(
1716                            super::Capabilities::SUBGROUP,
1717                        )
1718                        .with_span_static(span, "missing capability for this operation"));
1719                    }
1720                    let operation = mode.required_operations();
1721                    if !self.subgroup_operations.contains(operation) {
1722                        return Err(FunctionError::InvalidSubgroup(
1723                            SubgroupError::UnsupportedOperation(operation),
1724                        )
1725                        .with_span_static(span, "support for this operation is not present"));
1726                    }
1727                    self.validate_subgroup_gather(mode, argument, result, context)?;
1728                }
1729                S::SubgroupBallotFindBit {
1730                    order: _,
1731                    argument,
1732                    result,
1733                } => {
1734                    stages &= self.subgroup_stages;
1735                    if !self.capabilities.contains(super::Capabilities::SUBGROUP) {
1736                        return Err(FunctionError::MissingCapability(
1737                            super::Capabilities::SUBGROUP,
1738                        )
1739                        .with_span_static(span, "missing capability for this operation"));
1740                    }
1741                    if !self
1742                        .subgroup_operations
1743                        .contains(super::SubgroupOperationSet::BALLOT)
1744                    {
1745                        return Err(FunctionError::InvalidSubgroup(
1746                            SubgroupError::UnsupportedOperation(
1747                                super::SubgroupOperationSet::BALLOT,
1748                            ),
1749                        )
1750                        .with_span_static(span, "support for this operation is not present"));
1751                    }
1752                    self.validate_subgroup_ballot_find_bit(argument, result, context)?;
1753                }
1754                S::CooperativeStore { target, ref data } => {
1755                    stages &= super::ShaderStages::COMPUTE;
1756
1757                    let target_scalar =
1758                        match *context.resolve_type_inner(target, &self.valid_expression_set)? {
1759                            Ti::CooperativeMatrix { scalar, .. } => scalar,
1760                            ref other => {
1761                                log::error!("Target operand type: {other:?}");
1762                                return Err(FunctionError::InvalidCooperativeStoreTarget(target)
1763                                    .with_span_handle(target, context.expressions));
1764                            }
1765                        };
1766
1767                    let ptr_ty = context.resolve_pointer_type(data.pointer);
1768                    let ptr_scalar = ptr_ty
1769                        .pointer_base_type()
1770                        .and_then(|tr| tr.inner_with(context.types).scalar());
1771                    if ptr_scalar != Some(target_scalar) {
1772                        return Err(FunctionError::InvalidCooperativeDataPointer(data.pointer)
1773                            .with_span_handle(data.pointer, context.expressions));
1774                    }
1775
1776                    let ptr_space = ptr_ty.pointer_space().unwrap_or(AddressSpace::Handle);
1777                    if !ptr_space.access().contains(crate::StorageAccess::STORE) {
1778                        return Err(FunctionError::InvalidStorePointer(data.pointer)
1779                            .with_span_static(
1780                                context.expressions.get_span(data.pointer),
1781                                "writing to this location is not permitted",
1782                            ));
1783                    }
1784                }
1785                S::RayPipelineFunction(ref fun) => match *fun {
1786                    crate::RayPipelineFunction::TraceRay {
1787                        acceleration_structure,
1788                        descriptor,
1789                        payload,
1790                    } => {
1791                        self.validate_trace_ray_acceleration_structure(
1792                            acceleration_structure,
1793                            context,
1794                            span,
1795                        )?;
1796                        self.validate_ray_payload(payload, context)?;
1797                        self.validate_ray_descriptor(descriptor, context, span)?;
1798                    }
1799                    crate::RayPipelineFunction::ReorderThread { hint, bits } => {
1800                        stages &= super::ShaderStages::RAY_GENERATION;
1801                        // Unlike every other invocation reordering operation,
1802                        // this one doesn't mention a `hit_object`, so there is
1803                        // no type to carry the capability requirement for us.
1804                        if !self
1805                            .capabilities
1806                            .contains(super::Capabilities::RAY_TRACING_INVOCATION_REORDER)
1807                        {
1808                            return Err(FunctionError::MissingCapability(
1809                                super::Capabilities::RAY_TRACING_INVOCATION_REORDER,
1810                            )
1811                            .with_span_static(span, "missing capability for this operation"));
1812                        }
1813                        self.validate_reorder_operand(hint, context)?;
1814                        self.validate_reorder_operand(bits, context)?;
1815                    }
1816                },
1817                S::HitObject {
1818                    hit_object,
1819                    ref fun,
1820                } => {
1821                    stages &= super::ShaderStages::RAY_GENERATION
1822                        | super::ShaderStages::CLOSEST_HIT
1823                        | super::ShaderStages::MISS;
1824
1825                    let hit_object_var = match *context.get_expression(hit_object) {
1826                        crate::Expression::LocalVariable(var) => &context.local_vars[var],
1827                        ref other => {
1828                            log::error!("Unexpected hit object expression {other:?}");
1829                            return Err(FunctionError::InvalidHitObjectExpression(hit_object)
1830                                .with_span_static(span, "invalid hit object expression"));
1831                        }
1832                    };
1833                    match context.types[hit_object_var.ty].inner {
1834                        Ti::HitObject => {}
1835                        ref other => {
1836                            log::error!("Unexpected hit object type {other:?}");
1837                            return Err(FunctionError::InvalidHitObjectType(hit_object_var.ty)
1838                                .with_span_static(span, "invalid hit object type"));
1839                        }
1840                    }
1841
1842                    match *fun {
1843                        crate::HitObjectFunction::TraceRay {
1844                            acceleration_structure,
1845                            descriptor,
1846                            payload,
1847                        } => {
1848                            self.validate_trace_ray_acceleration_structure(
1849                                acceleration_structure,
1850                                context,
1851                                span,
1852                            )?;
1853                            self.validate_ray_payload(payload, context)?;
1854                            self.validate_ray_descriptor(descriptor, context, span)?;
1855                        }
1856                        crate::HitObjectFunction::RecordMiss { descriptor } => {
1857                            self.validate_ray_descriptor(descriptor, context, span)?;
1858                        }
1859                        crate::HitObjectFunction::RecordFromQuery { query } => {
1860                            // The `ray_query` type carries the
1861                            // `Capabilities::RAY_QUERY` requirement for us.
1862                            Self::validate_ray_query_variable(query, context, span)?;
1863                        }
1864                        crate::HitObjectFunction::RecordEmpty => {}
1865                        crate::HitObjectFunction::ExecuteShader { payload } => {
1866                            self.validate_ray_payload(payload, context)?;
1867                        }
1868                        crate::HitObjectFunction::Reorder { hint } => {
1869                            stages &= super::ShaderStages::RAY_GENERATION;
1870                            self.validate_reorder_hint(hint, context)?;
1871                        }
1872                    }
1873                }
1874
1875                S::DebugPrintf {
1876                    ref format,
1877                    ref arguments,
1878                } => {
1879                    if !self
1880                        .capabilities
1881                        .contains(super::Capabilities::DEBUG_PRINTF)
1882                    {
1883                        return Err(FunctionError::MissingCapability(
1884                            super::Capabilities::DEBUG_PRINTF,
1885                        )
1886                        .with_span_static(
1887                            span,
1888                            "`debugPrintf` requires the DEBUG_PRINTF capability",
1889                        ));
1890                    }
1891
1892                    let mut specifiers = alloc::vec::Vec::new();
1893                    let mut chars = format.chars().peekable();
1894                    while let Some(chr) = chars.next() {
1895                        if chr != '%' {
1896                            continue;
1897                        }
1898
1899                        // %%
1900                        if chars.next_if_eq(&'%').is_some() {
1901                            continue;
1902                        }
1903
1904                        let mut specifier = String::new();
1905
1906                        // Consume [width]
1907                        while let Some(chr) = chars.next_if(char::is_ascii_digit) {
1908                            specifier.push(chr);
1909                        }
1910
1911                        // Consume [.precision]
1912                        if let Some(dot) = chars.next_if_eq(&'.') {
1913                            specifier.push(dot);
1914                            while let Some(chr) = chars.next_if(char::is_ascii_digit) {
1915                                specifier.push(chr);
1916                            }
1917                        }
1918
1919                        let Some(chr) = chars.next() else {
1920                            return Err(FunctionError::InvalidDebugPrintfSpecifier(specifier)
1921                                .with_span_static(span, "debugPrintf format string"));
1922                        };
1923                        specifier.push(chr);
1924
1925                        let expected_type = match chr {
1926                            'd' | 'i' => crate::Scalar::I32,
1927                            'o' | 'u' | 'x' | 'X' => crate::Scalar::U32,
1928                            'e' | 'E' | 'f' | 'F' | 'g' | 'G' => crate::Scalar::F64, // Actually all floats
1929                            'l' => {
1930                                let next = chars.next();
1931                                specifier.extend(next);
1932                                match next {
1933                                    Some('u' | 'x') => crate::Scalar::U64,
1934                                    Some('d') => crate::Scalar::I64,
1935                                    _ => {
1936                                        return Err(FunctionError::InvalidDebugPrintfSpecifier(
1937                                            specifier,
1938                                        )
1939                                        .with_span_static(span, "debugPrintf format string"));
1940                                    }
1941                                }
1942                            }
1943                            _ => {
1944                                return Err(FunctionError::InvalidDebugPrintfSpecifier(specifier)
1945                                    .with_span_static(span, "debugPrintf format string"));
1946                            }
1947                        };
1948                        specifiers.push((specifier, expected_type));
1949                    }
1950
1951                    if arguments.len() != specifiers.len() {
1952                        return Err(FunctionError::WrongDebugPrintfArgumentCount(
1953                            arguments.len(),
1954                            specifiers.len(),
1955                        )
1956                        .with_span_static(span, "debugPrintf format string"));
1957                    }
1958
1959                    for (&argument, (specifier, expected_type)) in arguments.iter().zip(specifiers)
1960                    {
1961                        let resolution =
1962                            context.resolve_type(argument, &self.valid_expression_set)?;
1963                        match (resolution.inner_with(context.types), expected_type) {
1964                            // Only scalar arguments are supported for now. Vector and
1965                            // matrix arguments could be supported in the future by
1966                            // splatting them into their components in the backends.
1967                            (&Ti::Scalar(actual_type), expected_type)
1968                                if actual_type == expected_type => {}
1969                            // All floating point sizes are supported by e.g. %f.
1970                            (
1971                                &Ti::Scalar(crate::Scalar {
1972                                    kind: crate::ScalarKind::Float,
1973                                    ..
1974                                }),
1975                                crate::Scalar {
1976                                    kind: crate::ScalarKind::Float,
1977                                    ..
1978                                },
1979                            ) => {}
1980                            _ => {
1981                                return Err(FunctionError::InvalidDebugPrintfArgument {
1982                                    argument,
1983                                    specifier,
1984                                    actual: crate::common::DiagnosticDisplay((
1985                                        resolution,
1986                                        context.gctx,
1987                                    ))
1988                                    .to_string(),
1989                                }
1990                                .with_span_handle(argument, context.expressions));
1991                            }
1992                        }
1993                    }
1994                }
1995            }
1996        }
1997        Ok(BlockInfo { stages })
1998    }
1999
2000    /// Check that `query` is a local variable of a `ray_query` type, and return
2001    /// whether that type provides vertex positions.
2002    fn validate_ray_query_variable(
2003        query: Handle<crate::Expression>,
2004        context: &BlockContext,
2005        span: crate::Span,
2006    ) -> Result<bool, WithSpan<FunctionError>> {
2007        let query_var = match *context.get_expression(query) {
2008            crate::Expression::LocalVariable(var) => &context.local_vars[var],
2009            ref other => {
2010                log::error!("Unexpected ray query expression {other:?}");
2011                return Err(FunctionError::InvalidRayQueryExpression(query)
2012                    .with_span_static(span, "invalid query expression"));
2013            }
2014        };
2015        match context.types[query_var.ty].inner {
2016            crate::TypeInner::RayQuery { vertex_return } => Ok(vertex_return),
2017            ref other => {
2018                log::error!("Unexpected ray query type {other:?}");
2019                Err(FunctionError::InvalidRayQueryType(query_var.ty)
2020                    .with_span_static(span, "invalid query type"))
2021            }
2022        }
2023    }
2024
2025    /// Validate the acceleration structure operand of a ray tracing pipeline
2026    /// operation, and record whether it provides vertex positions.
2027    fn validate_trace_ray_acceleration_structure(
2028        &mut self,
2029        acceleration_structure: Handle<crate::Expression>,
2030        context: &BlockContext,
2031        span: crate::Span,
2032    ) -> Result<(), WithSpan<FunctionError>> {
2033        match *context.resolve_type_inner(acceleration_structure, &self.valid_expression_set)? {
2034            crate::TypeInner::AccelerationStructure { vertex_return } => {
2035                if !vertex_return {
2036                    self.trace_rays_vertex_return =
2037                        super::TraceRayVertexReturnState::NoVertexReturn(span);
2038                } else if let super::TraceRayVertexReturnState::NoTraceRays =
2039                    self.trace_rays_vertex_return
2040                {
2041                    self.trace_rays_vertex_return = super::TraceRayVertexReturnState::VertexReturn;
2042                }
2043                Ok(())
2044            }
2045            _ => Err(
2046                FunctionError::InvalidAccelerationStructure(acceleration_structure)
2047                    .with_span_handle(acceleration_structure, context.expressions),
2048            ),
2049        }
2050    }
2051
2052    /// Validate a ray payload pointer operand, and check that every ray tracing
2053    /// pipeline operation in the module agrees on the payload type.
2054    fn validate_ray_payload(
2055        &mut self,
2056        payload: Handle<crate::Expression>,
2057        context: &BlockContext,
2058    ) -> Result<(), WithSpan<FunctionError>> {
2059        let current_payload_ty = match *context
2060            .resolve_type_inner(payload, &self.valid_expression_set)?
2061        {
2062            crate::TypeInner::Pointer { base, space } => {
2063                match space {
2064                    crate::AddressSpace::RayPayload | crate::AddressSpace::IncomingRayPayload => {}
2065                    space => {
2066                        return Err(FunctionError::InvalidPayloadAddressSpace(space)
2067                            .with_span_handle(payload, context.expressions))
2068                    }
2069                }
2070                base
2071            }
2072            _ => {
2073                return Err(FunctionError::InvalidPayloadType
2074                    .with_span_handle(payload, context.expressions))
2075            }
2076        };
2077
2078        // spir-v requires a direct reference to a global variable.
2079        let crate::Expression::GlobalVariable(_) = context.expressions[payload] else {
2080            return Err(FunctionError::PayloadPointerNotGlobal
2081                .with_span_handle(payload, context.expressions));
2082        };
2083
2084        let ty = *self
2085            .trace_rays_payload_type
2086            .get_or_insert(current_payload_ty);
2087
2088        if ty != current_payload_ty {
2089            return Err(FunctionError::MismatchedPayloadType(current_payload_ty, ty)
2090                .with_span_handle(ty, context.types));
2091        }
2092
2093        Ok(())
2094    }
2095
2096    /// Validate that `descriptor` has the module's `RayDesc` type.
2097    fn validate_ray_descriptor(
2098        &self,
2099        descriptor: Handle<crate::Expression>,
2100        context: &BlockContext,
2101        span: crate::Span,
2102    ) -> Result<(), WithSpan<FunctionError>> {
2103        let desc_ty_given = context.resolve_type_inner(descriptor, &self.valid_expression_set)?;
2104        let desc_ty_expected = context
2105            .special_types
2106            .ray_desc
2107            .map(|handle| &context.types[handle].inner);
2108        if Some(desc_ty_given) != desc_ty_expected {
2109            return Err(FunctionError::InvalidRayDescriptor(descriptor)
2110                .with_span_static(span, "invalid ray descriptor"));
2111        }
2112        Ok(())
2113    }
2114
2115    /// Validate the optional user-supplied hint of an invocation reordering
2116    /// operation.
2117    fn validate_reorder_hint(
2118        &self,
2119        hint: Option<crate::ReorderHint>,
2120        context: &BlockContext,
2121    ) -> Result<(), WithSpan<FunctionError>> {
2122        if let Some(crate::ReorderHint { hint, bits }) = hint {
2123            self.validate_reorder_operand(hint, context)?;
2124            self.validate_reorder_operand(bits, context)?;
2125        }
2126        Ok(())
2127    }
2128
2129    /// Validate that `expr` is a `u32` scalar, as required by the operands of
2130    /// the invocation reordering operations.
2131    fn validate_reorder_operand(
2132        &self,
2133        expr: Handle<crate::Expression>,
2134        context: &BlockContext,
2135    ) -> Result<(), WithSpan<FunctionError>> {
2136        match *context.resolve_type_inner(expr, &self.valid_expression_set)? {
2137            crate::TypeInner::Scalar(crate::Scalar::U32) => Ok(()),
2138            _ => Err(FunctionError::InvalidReorderOperand(expr)
2139                .with_span_handle(expr, context.expressions)),
2140        }
2141    }
2142
2143    fn validate_block(
2144        &mut self,
2145        statements: &crate::Block,
2146        context: &BlockContext,
2147    ) -> Result<BlockInfo, WithSpan<FunctionError>> {
2148        let base_expression_count = self.valid_expression_list.len();
2149        let info = self.validate_block_impl(statements, context)?;
2150        for handle in self.valid_expression_list.drain(base_expression_count..) {
2151            self.valid_expression_set.remove(handle);
2152        }
2153        Ok(info)
2154    }
2155
2156    fn validate_local_var(
2157        &self,
2158        var: &crate::LocalVariable,
2159        gctx: crate::proc::GlobalCtx,
2160        fun_info: &FunctionInfo,
2161        local_expr_kind: &crate::proc::ExpressionKindTracker,
2162    ) -> Result<(), LocalVariableError> {
2163        log::debug!("var {var:?}");
2164        let type_info = self
2165            .types
2166            .get(var.ty.index())
2167            .ok_or(LocalVariableError::InvalidType(var.ty))?;
2168        if !type_info.flags.contains(super::TypeFlags::CONSTRUCTIBLE) {
2169            return Err(LocalVariableError::InvalidType(var.ty));
2170        }
2171
2172        if let Some(init) = var.init {
2173            if !gctx.compare_types(&TypeResolution::Handle(var.ty), &fun_info[init].ty) {
2174                return Err(LocalVariableError::InitializerType);
2175            }
2176
2177            if !local_expr_kind.is_const_or_override(init) {
2178                return Err(LocalVariableError::NonConstOrOverrideInitializer);
2179            }
2180
2181            if matches!(gctx.types[var.ty].inner, crate::TypeInner::RayQuery { .. }) {
2182                return Err(LocalVariableError::RayQueryWithInitializeExpression);
2183            }
2184
2185            if matches!(gctx.types[var.ty].inner, crate::TypeInner::HitObject) {
2186                return Err(LocalVariableError::HitObjectWithInitializeExpression);
2187            }
2188        }
2189
2190        Ok(())
2191    }
2192
2193    pub(super) fn validate_function(
2194        &mut self,
2195        fun: &crate::Function,
2196        module: &crate::Module,
2197        mod_info: &ModuleInfo,
2198        entry_point: bool,
2199    ) -> Result<FunctionInfo, WithSpan<FunctionError>> {
2200        let mut info = mod_info.process_function(fun, module, self.flags, self.capabilities)?;
2201
2202        let local_expr_kind = crate::proc::ExpressionKindTracker::from_arena(&fun.expressions);
2203
2204        for (var_handle, var) in fun.local_variables.iter() {
2205            self.validate_local_var(var, module.to_ctx(), &info, &local_expr_kind)
2206                .map_err(|source| {
2207                    FunctionError::LocalVariable {
2208                        handle: var_handle,
2209                        name: var.name.clone().unwrap_or_default(),
2210                        source,
2211                    }
2212                    .with_span_handle(var.ty, &module.types)
2213                    .with_handle(var_handle, &fun.local_variables)
2214                })?;
2215
2216            // Hit objects exist only in the ray tracing pipeline stages that can
2217            // hold a hit. The type check cannot enforce this since types are
2218            // module-wide, and an unreferenced variable has no expression or
2219            // statement to carry the restriction, so apply it here.
2220            if let crate::TypeInner::HitObject = module.types[var.ty].inner {
2221                info.available_stages &= super::ShaderStages::RAY_GENERATION
2222                    | super::ShaderStages::CLOSEST_HIT
2223                    | super::ShaderStages::MISS;
2224            }
2225        }
2226
2227        for (index, argument) in fun.arguments.iter().enumerate() {
2228            match module.types[argument.ty].inner.pointer_space() {
2229                Some(crate::AddressSpace::Private | crate::AddressSpace::Function) | None => {}
2230                Some(other) => {
2231                    return Err(FunctionError::InvalidArgumentPointerSpace {
2232                        index,
2233                        name: argument.name.clone().unwrap_or_default(),
2234                        space: other,
2235                    }
2236                    .with_span_handle(argument.ty, &module.types))
2237                }
2238            }
2239            // Check for the least informative error last.
2240            if !self.types[argument.ty.index()]
2241                .flags
2242                .contains(super::TypeFlags::ARGUMENT)
2243            {
2244                return Err(FunctionError::InvalidArgumentType {
2245                    index,
2246                    name: argument.name.clone().unwrap_or_default(),
2247                }
2248                .with_span_handle(argument.ty, &module.types));
2249            }
2250
2251            if !entry_point && argument.binding.is_some() {
2252                return Err(FunctionError::PipelineInputRegularFunction {
2253                    name: argument.name.clone().unwrap_or_default(),
2254                }
2255                .with_span_handle(argument.ty, &module.types));
2256            }
2257        }
2258
2259        if let Some(ref result) = fun.result {
2260            if !self.types[result.ty.index()]
2261                .flags
2262                .contains(super::TypeFlags::CONSTRUCTIBLE)
2263                || matches!(module.types[result.ty].inner, crate::TypeInner::HitObject)
2264            {
2265                return Err(FunctionError::NonConstructibleReturnType
2266                    .with_span_handle(result.ty, &module.types));
2267            }
2268
2269            if !entry_point && result.binding.is_some() {
2270                return Err(FunctionError::PipelineOutputRegularFunction
2271                    .with_span_handle(result.ty, &module.types));
2272            }
2273        }
2274
2275        self.valid_expression_set.clear_for_arena(&fun.expressions);
2276        self.valid_expression_list.clear();
2277        self.needs_visit.clear_for_arena(&fun.expressions);
2278        for (handle, expr) in fun.expressions.iter() {
2279            if expr.needs_pre_emit() {
2280                self.valid_expression_set.insert(handle);
2281            }
2282            if self.flags.contains(super::ValidationFlags::EXPRESSIONS) {
2283                // Mark expressions that need to be visited by a particular kind of
2284                // statement.
2285                if let crate::Expression::CallResult(_) | crate::Expression::AtomicResult { .. } =
2286                    *expr
2287                {
2288                    self.needs_visit.insert(handle);
2289                }
2290
2291                match self.validate_expression(
2292                    handle,
2293                    expr,
2294                    fun,
2295                    module,
2296                    &info,
2297                    mod_info,
2298                    &local_expr_kind,
2299                ) {
2300                    Ok(stages) => info.available_stages &= stages,
2301                    Err(source) => {
2302                        return Err(FunctionError::Expression { handle, source }
2303                            .with_span_handle(handle, &fun.expressions))
2304                    }
2305                }
2306            }
2307        }
2308
2309        if self.flags.contains(super::ValidationFlags::BLOCKS) {
2310            let stages = self
2311                .validate_block(
2312                    &fun.body,
2313                    &BlockContext::new(fun, module, &info, &mod_info.functions, &local_expr_kind),
2314                )?
2315                .stages;
2316            info.available_stages &= stages;
2317
2318            if self.flags.contains(super::ValidationFlags::EXPRESSIONS) {
2319                if let Some(handle) = self.needs_visit.iter().next() {
2320                    return Err(FunctionError::UnvisitedExpression(handle)
2321                        .with_span_handle(handle, &fun.expressions));
2322                }
2323            }
2324        }
2325        Ok(info)
2326    }
2327}