Skip to main content

naga/valid/
analyzer.rs

1//! Module analyzer.
2//!
3//! Figures out the following properties:
4//! - control flow uniformity
5//! - texture/sampler pairs
6//! - expression reference counts
7
8use alloc::{boxed::Box, vec};
9use core::ops;
10
11use super::{ExpressionError, FunctionError, ModuleInfo, ShaderStages, ValidationFlags};
12use crate::diagnostic_filter::{DiagnosticFilterNode, StandardFilterableTriggeringRule};
13use crate::span::{AddSpan as _, WithSpan};
14use crate::{
15    arena::{Arena, Handle},
16    proc::{ResolveContext, TypeResolution},
17};
18
19pub type NonUniformResult = Option<Handle<crate::Expression>>;
20
21const DISABLE_UNIFORMITY_REQ_FOR_FRAGMENT_STAGE: bool = true;
22
23bitflags::bitflags! {
24    /// Kinds of expressions that require uniform control flow.
25    #[cfg_attr(feature = "serialize", derive(serde::Serialize))]
26    #[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
27    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
28    pub struct UniformityRequirements: u8 {
29        const WORK_GROUP_BARRIER = 0x1;
30        const DERIVATIVE = if DISABLE_UNIFORMITY_REQ_FOR_FRAGMENT_STAGE { 0 } else { 0x2 };
31        const IMPLICIT_LEVEL = if DISABLE_UNIFORMITY_REQ_FOR_FRAGMENT_STAGE { 0 } else { 0x4 };
32        const COOP_OPS = 0x8;
33    }
34}
35
36/// Uniform control flow characteristics.
37#[derive(Clone, Debug)]
38#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
39#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
40#[cfg_attr(test, derive(PartialEq))]
41pub struct Uniformity {
42    /// A child expression with non-uniform result.
43    ///
44    /// This means, when the relevant invocations are scheduled on a compute unit,
45    /// they have to use vector registers to store an individual value
46    /// per invocation.
47    ///
48    /// Whenever the control flow is conditioned on such value,
49    /// the hardware needs to keep track of the mask of invocations,
50    /// and process all branches of the control flow.
51    ///
52    /// Any operations that depend on non-uniform results also produce non-uniform.
53    pub non_uniform_result: NonUniformResult,
54    /// If this expression requires uniform control flow, store the reason here.
55    pub requirements: UniformityRequirements,
56}
57
58impl Uniformity {
59    const fn new() -> Self {
60        Uniformity {
61            non_uniform_result: None,
62            requirements: UniformityRequirements::empty(),
63        }
64    }
65}
66
67bitflags::bitflags! {
68    #[derive(Clone, Copy, Debug, PartialEq)]
69    struct ExitFlags: u8 {
70        /// Control flow may return from the function, which makes all the
71        /// subsequent statements within the current function (only!)
72        /// to be executed in a non-uniform control flow.
73        const MAY_RETURN = 0x1;
74        /// Control flow may be killed. Anything after [`Statement::Kill`] is
75        /// considered inside non-uniform context.
76        ///
77        /// [`Statement::Kill`]: crate::Statement::Kill
78        const MAY_KILL = 0x2;
79    }
80}
81
82/// Uniformity characteristics of a function.
83#[cfg_attr(test, derive(Debug, PartialEq))]
84struct FunctionUniformity {
85    result: Uniformity,
86    exit: ExitFlags,
87}
88
89impl ops::BitOr for FunctionUniformity {
90    type Output = Self;
91    fn bitor(self, other: Self) -> Self {
92        FunctionUniformity {
93            result: Uniformity {
94                non_uniform_result: self
95                    .result
96                    .non_uniform_result
97                    .or(other.result.non_uniform_result),
98                requirements: self.result.requirements | other.result.requirements,
99            },
100            exit: self.exit | other.exit,
101        }
102    }
103}
104
105impl FunctionUniformity {
106    const fn new() -> Self {
107        FunctionUniformity {
108            result: Uniformity::new(),
109            exit: ExitFlags::empty(),
110        }
111    }
112
113    /// Returns a disruptor based on the stored exit flags, if any.
114    const fn exit_disruptor(&self) -> Option<UniformityDisruptor> {
115        if self.exit.contains(ExitFlags::MAY_RETURN) {
116            Some(UniformityDisruptor::Return)
117        } else if self.exit.contains(ExitFlags::MAY_KILL) {
118            Some(UniformityDisruptor::Discard)
119        } else {
120            None
121        }
122    }
123}
124
125bitflags::bitflags! {
126    /// Indicates how a global variable is used.
127    #[cfg_attr(feature = "serialize", derive(serde::Serialize))]
128    #[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
129    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
130    pub struct GlobalUse: u8 {
131        /// Data will be read from the variable.
132        const READ = 0x1;
133        /// Data will be written to the variable.
134        const WRITE = 0x2;
135        /// The information about the data is queried.
136        const QUERY = 0x4;
137        /// Atomic operations will be performed on the variable.
138        const ATOMIC = 0x8;
139    }
140}
141
142#[derive(Clone, Debug, Eq, Hash, PartialEq)]
143#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
144#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
145pub struct SamplingKey {
146    pub image: Handle<crate::GlobalVariable>,
147    pub sampler: Handle<crate::GlobalVariable>,
148}
149
150#[derive(Clone, Debug)]
151#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
152#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
153/// Information about an expression in a function body.
154pub struct ExpressionInfo {
155    /// Whether this expression is uniform, and why.
156    ///
157    /// If this expression's value is not uniform, this is the handle
158    /// of the expression from which this one's non-uniformity
159    /// originates. Otherwise, this is `None`.
160    pub uniformity: Uniformity,
161
162    /// The number of direct references to this expression in statements and
163    /// other expressions.
164    ///
165    /// This is a _local_ reference count only, it may be non-zero for
166    /// expressions that are ultimately unused.
167    pub ref_count: usize,
168
169    /// The global variable into which this expression produces a pointer.
170    ///
171    /// This is `None` unless this expression is either a
172    /// [`GlobalVariable`], or an [`Access`] or [`AccessIndex`] that
173    /// ultimately refers to some part of a global.
174    ///
175    /// [`Load`] expressions applied to pointer-typed arguments could
176    /// refer to globals, but we leave this as `None` for them.
177    ///
178    /// [`GlobalVariable`]: crate::Expression::GlobalVariable
179    /// [`Access`]: crate::Expression::Access
180    /// [`AccessIndex`]: crate::Expression::AccessIndex
181    /// [`Load`]: crate::Expression::Load
182    assignable_global: Option<Handle<crate::GlobalVariable>>,
183
184    /// The type of this expression.
185    pub ty: TypeResolution,
186}
187
188impl ExpressionInfo {
189    const fn new() -> Self {
190        ExpressionInfo {
191            uniformity: Uniformity::new(),
192            ref_count: 0,
193            assignable_global: None,
194            // this doesn't matter at this point, will be overwritten
195            ty: TypeResolution::Value(crate::TypeInner::Scalar(crate::Scalar {
196                kind: crate::ScalarKind::Bool,
197                width: 0,
198            })),
199        }
200    }
201}
202
203#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
204#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
205#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
206enum GlobalOrArgument {
207    Global(Handle<crate::GlobalVariable>),
208    Argument(u32),
209}
210
211impl GlobalOrArgument {
212    fn from_expression(
213        expression_arena: &Arena<crate::Expression>,
214        expression: Handle<crate::Expression>,
215    ) -> Result<GlobalOrArgument, ExpressionError> {
216        Ok(match expression_arena[expression] {
217            crate::Expression::GlobalVariable(var) => GlobalOrArgument::Global(var),
218            crate::Expression::FunctionArgument(i) => GlobalOrArgument::Argument(i),
219            crate::Expression::Access { base, .. }
220            | crate::Expression::AccessIndex { base, .. } => match expression_arena[base] {
221                crate::Expression::GlobalVariable(var) => GlobalOrArgument::Global(var),
222                _ => return Err(ExpressionError::ExpectedGlobalOrArgument),
223            },
224            _ => return Err(ExpressionError::ExpectedGlobalOrArgument),
225        })
226    }
227}
228
229#[derive(Debug, Clone, PartialEq, Eq, Hash)]
230#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
231#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
232struct Sampling {
233    image: GlobalOrArgument,
234    sampler: GlobalOrArgument,
235}
236
237#[derive(Debug, Clone)]
238#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
239#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
240pub struct FunctionInfo {
241    /// Validation flags.
242    flags: ValidationFlags,
243    /// Set of shader stages where calling this function is valid.
244    pub available_stages: ShaderStages,
245    /// Uniformity characteristics.
246    pub uniformity: Uniformity,
247    /// Function may kill the invocation.
248    pub may_kill: bool,
249
250    /// All pairs of (texture, sampler) globals that may be used together in
251    /// sampling operations by this function and its callees. This includes
252    /// pairings that arise when this function passes textures and samplers as
253    /// arguments to its callees.
254    ///
255    /// This table does not include uses of textures and samplers passed as
256    /// arguments to this function itself, since we do not know which globals
257    /// those will be. However, this table *is* exhaustive when computed for an
258    /// entry point function: entry points never receive textures or samplers as
259    /// arguments, so all an entry point's sampling can be reported in terms of
260    /// globals.
261    ///
262    /// The GLSL back end uses this table to construct reflection info that
263    /// clients need to construct texture-combined sampler values.
264    pub sampling_set: crate::FastHashSet<SamplingKey>,
265
266    /// How this function and its callees use this module's globals.
267    ///
268    /// This is indexed by `Handle<GlobalVariable>` indices. However,
269    /// `FunctionInfo` implements `core::ops::Index<Handle<GlobalVariable>>`,
270    /// so you can simply index this struct with a global handle to retrieve
271    /// its usage information.
272    pub global_uses: Box<[GlobalUse]>,
273
274    /// Information about each expression in this function's body.
275    ///
276    /// This is indexed by `Handle<Expression>` indices. However, `FunctionInfo`
277    /// implements `core::ops::Index<Handle<Expression>>`, so you can simply
278    /// index this struct with an expression handle to retrieve its
279    /// `ExpressionInfo`.
280    expressions: Box<[ExpressionInfo]>,
281
282    /// All (texture, sampler) pairs that may be used together in sampling
283    /// operations by this function and its callees, whether they are accessed
284    /// as globals or passed as arguments.
285    ///
286    /// Participants are represented by [`GlobalVariable`] handles whenever
287    /// possible, and otherwise by indices of this function's arguments.
288    ///
289    /// When analyzing a function call, we combine this data about the callee
290    /// with the actual arguments being passed to produce the callers' own
291    /// `sampling_set` and `sampling` tables.
292    ///
293    /// [`GlobalVariable`]: crate::GlobalVariable
294    sampling: crate::FastHashSet<Sampling>,
295
296    /// Indicates that the function is using dual source blending.
297    pub dual_source_blending: bool,
298
299    /// The leaf of all module-wide diagnostic filter rules tree parsed from directives in this
300    /// module.
301    ///
302    /// See [`DiagnosticFilterNode`] for details on how the tree is represented and used in
303    /// validation.
304    diagnostic_filter_leaf: Option<Handle<DiagnosticFilterNode>>,
305}
306
307impl FunctionInfo {
308    pub const fn global_variable_count(&self) -> usize {
309        self.global_uses.len()
310    }
311    pub const fn expression_count(&self) -> usize {
312        self.expressions.len()
313    }
314    pub fn dominates_global_use(&self, other: &Self) -> bool {
315        for (self_global_uses, other_global_uses) in
316            self.global_uses.iter().zip(other.global_uses.iter())
317        {
318            if !self_global_uses.contains(*other_global_uses) {
319                return false;
320            }
321        }
322        true
323    }
324}
325
326impl ops::Index<Handle<crate::GlobalVariable>> for FunctionInfo {
327    type Output = GlobalUse;
328    fn index(&self, handle: Handle<crate::GlobalVariable>) -> &GlobalUse {
329        &self.global_uses[handle.index()]
330    }
331}
332
333impl ops::Index<Handle<crate::Expression>> for FunctionInfo {
334    type Output = ExpressionInfo;
335    fn index(&self, handle: Handle<crate::Expression>) -> &ExpressionInfo {
336        &self.expressions[handle.index()]
337    }
338}
339
340/// Disruptor of the uniform control flow.
341#[derive(Clone, Copy, Debug, thiserror::Error)]
342#[cfg_attr(test, derive(PartialEq))]
343pub enum UniformityDisruptor {
344    #[error("Expression {0:?} produced non-uniform result, and control flow depends on it")]
345    Expression(Handle<crate::Expression>),
346    #[error("There is a Return earlier in the control flow of the function")]
347    Return,
348    #[error("There is a Discard earlier in the entry point across all called functions")]
349    Discard,
350}
351
352impl FunctionInfo {
353    /// Record a use of `expr` of the sort given by `global_use`.
354    ///
355    /// Bump `expr`'s reference count, and return its uniformity.
356    ///
357    /// If `expr` is a pointer to a global variable, or some part of
358    /// a global variable, add `global_use` to that global's set of
359    /// uses.
360    #[must_use]
361    fn add_ref_impl(
362        &mut self,
363        expr: Handle<crate::Expression>,
364        global_use: GlobalUse,
365    ) -> NonUniformResult {
366        let info = &mut self.expressions[expr.index()];
367        info.ref_count += 1;
368        // Record usage if this expression may access a global
369        if let Some(global) = info.assignable_global {
370            self.global_uses[global.index()] |= global_use;
371        }
372        info.uniformity.non_uniform_result
373    }
374
375    /// Note an entry point's use of `global` not recorded by [`ModuleInfo::process_function`].
376    ///
377    /// Most global variable usage should be recorded via [`add_ref_impl`] in the process
378    /// of expression behavior analysis by [`ModuleInfo::process_function`]. But that code
379    /// has no access to entrypoint-specific information, so interface analysis uses this
380    /// function to record global uses there (like task shader payloads).
381    ///
382    /// [`add_ref_impl`]: Self::add_ref_impl
383    pub(super) fn insert_global_use(
384        &mut self,
385        global_use: GlobalUse,
386        global: Handle<crate::GlobalVariable>,
387    ) {
388        self.global_uses[global.index()] |= global_use;
389    }
390
391    /// Record a use of `expr` for its value.
392    ///
393    /// This is used for almost all expression references. Anything
394    /// that writes to the value `expr` points to, or otherwise wants
395    /// contribute flags other than `GlobalUse::READ`, should use
396    /// `add_ref_impl` directly.
397    #[must_use]
398    fn add_ref(&mut self, expr: Handle<crate::Expression>) -> NonUniformResult {
399        self.add_ref_impl(expr, GlobalUse::READ)
400    }
401
402    /// Record uses of the operands of an optional invocation reordering hint.
403    fn add_reorder_hint_refs(&mut self, hint: Option<crate::ReorderHint>) {
404        if let Some(crate::ReorderHint { hint, bits }) = hint {
405            let _ = self.add_ref(hint);
406            let _ = self.add_ref(bits);
407        }
408    }
409
410    /// Record a use of `expr`, and indicate which global variable it
411    /// refers to, if any.
412    ///
413    /// Bump `expr`'s reference count, and return its uniformity.
414    ///
415    /// If `expr` is a pointer to a global variable, or some part
416    /// thereof, store that global in `*assignable_global`. Leave the
417    /// global's uses unchanged.
418    ///
419    /// This is used to determine the [`assignable_global`] for
420    /// [`Access`] and [`AccessIndex`] expressions that ultimately
421    /// refer to a global variable. Those expressions don't contribute
422    /// any usage to the global themselves; that depends on how other
423    /// expressions use them.
424    ///
425    /// [`assignable_global`]: ExpressionInfo::assignable_global
426    /// [`Access`]: crate::Expression::Access
427    /// [`AccessIndex`]: crate::Expression::AccessIndex
428    #[must_use]
429    fn add_assignable_ref(
430        &mut self,
431        expr: Handle<crate::Expression>,
432        assignable_global: &mut Option<Handle<crate::GlobalVariable>>,
433    ) -> NonUniformResult {
434        let info = &mut self.expressions[expr.index()];
435        info.ref_count += 1;
436        // propagate the assignable global up the chain, till it either hits
437        // a value-type expression, or the assignment statement.
438        if let Some(global) = info.assignable_global {
439            if let Some(_old) = assignable_global.replace(global) {
440                unreachable!()
441            }
442        }
443        info.uniformity.non_uniform_result
444    }
445
446    /// Inherit information from a called function.
447    fn process_call(
448        &mut self,
449        callee: &Self,
450        arguments: &[Handle<crate::Expression>],
451        expression_arena: &Arena<crate::Expression>,
452    ) -> Result<FunctionUniformity, WithSpan<FunctionError>> {
453        self.sampling_set
454            .extend(callee.sampling_set.iter().cloned());
455        for sampling in callee.sampling.iter() {
456            // If the callee was passed the texture or sampler as an argument,
457            // we may now be able to determine which globals those referred to.
458            let image_storage = match sampling.image {
459                GlobalOrArgument::Global(var) => GlobalOrArgument::Global(var),
460                GlobalOrArgument::Argument(i) => {
461                    let Some(handle) = arguments.get(i as usize).cloned() else {
462                        // Argument count mismatch, will be reported later by validate_call
463                        break;
464                    };
465                    GlobalOrArgument::from_expression(expression_arena, handle).map_err(
466                        |source| {
467                            FunctionError::Expression { handle, source }
468                                .with_span_handle(handle, expression_arena)
469                        },
470                    )?
471                }
472            };
473
474            let sampler_storage = match sampling.sampler {
475                GlobalOrArgument::Global(var) => GlobalOrArgument::Global(var),
476                GlobalOrArgument::Argument(i) => {
477                    let Some(handle) = arguments.get(i as usize).cloned() else {
478                        // Argument count mismatch, will be reported later by validate_call
479                        break;
480                    };
481                    GlobalOrArgument::from_expression(expression_arena, handle).map_err(
482                        |source| {
483                            FunctionError::Expression { handle, source }
484                                .with_span_handle(handle, expression_arena)
485                        },
486                    )?
487                }
488            };
489
490            // If we've managed to pin both the image and sampler down to
491            // specific globals, record that in our `sampling_set`. Otherwise,
492            // record as much as we do know in our own `sampling` table, for our
493            // callers to sort out.
494            match (image_storage, sampler_storage) {
495                (GlobalOrArgument::Global(image), GlobalOrArgument::Global(sampler)) => {
496                    self.sampling_set.insert(SamplingKey { image, sampler });
497                }
498                (image, sampler) => {
499                    self.sampling.insert(Sampling { image, sampler });
500                }
501            }
502        }
503
504        // Inherit global use and immediate slot tracking from our callees.
505        for (mine, other) in self.global_uses.iter_mut().zip(callee.global_uses.iter()) {
506            *mine |= *other;
507        }
508
509        Ok(FunctionUniformity {
510            result: callee.uniformity.clone(),
511            exit: if callee.may_kill {
512                ExitFlags::MAY_KILL
513            } else {
514                ExitFlags::empty()
515            },
516        })
517    }
518
519    /// Compute the [`ExpressionInfo`] for `handle`.
520    ///
521    /// Replace the dummy entry in [`self.expressions`] for `handle`
522    /// with a real `ExpressionInfo` value describing that expression.
523    ///
524    /// This function is called as part of a forward sweep through the
525    /// arena, so we can assume that all earlier expressions in the
526    /// arena already have valid info. Since expressions only depend
527    /// on earlier expressions, this includes all our subexpressions.
528    ///
529    /// Adjust the reference counts on all expressions we use.
530    ///
531    /// Also populate the [`sampling_set`], [`sampling`] and
532    /// [`global_uses`] fields of `self`.
533    ///
534    /// [`self.expressions`]: FunctionInfo::expressions
535    /// [`sampling_set`]: FunctionInfo::sampling_set
536    /// [`sampling`]: FunctionInfo::sampling
537    /// [`global_uses`]: FunctionInfo::global_uses
538    #[allow(clippy::or_fun_call)]
539    fn process_expression(
540        &mut self,
541        handle: Handle<crate::Expression>,
542        expression_arena: &Arena<crate::Expression>,
543        other_functions: &[FunctionInfo],
544        resolve_context: &ResolveContext,
545        capabilities: super::Capabilities,
546    ) -> Result<(), ExpressionError> {
547        use crate::{Expression as E, SampleLevel as Sl};
548
549        let expression = &expression_arena[handle];
550        let mut assignable_global = None;
551        let uniformity = match *expression {
552            E::Access { base, index } => {
553                let base_ty = self[base].ty.inner_with(resolve_context.types);
554
555                // build up the caps needed if this is indexed non-uniformly
556                let mut needed_caps = super::Capabilities::empty();
557                let is_binding_array = match *base_ty {
558                    crate::TypeInner::BindingArray {
559                        base: array_element_ty_handle,
560                        ..
561                    } => {
562                        // We're a binding array, so lets use the type of _what_ we are array of to determine if we can non-uniformly index it.
563                        let array_element_ty =
564                            &resolve_context.types[array_element_ty_handle].inner;
565
566                        needed_caps |= match *array_element_ty {
567                            // If we're an image, use the appropriate capability.
568                            crate::TypeInner::Image { class, .. } => match class {
569                                crate::ImageClass::Storage { .. } => {
570                                    super::Capabilities::STORAGE_TEXTURE_BINDING_ARRAY_NON_UNIFORM_INDEXING
571                                }
572                                _ => {
573                                    super::Capabilities::TEXTURE_AND_SAMPLER_BINDING_ARRAY_NON_UNIFORM_INDEXING
574                                }
575                            },
576                            crate::TypeInner::Sampler { .. } => {
577                                super::Capabilities::TEXTURE_AND_SAMPLER_BINDING_ARRAY_NON_UNIFORM_INDEXING
578                            }
579                            // If we're anything but an image or sampler, assume we're a buffer and use the address space.
580                            _ => {
581                                if let E::GlobalVariable(global_handle) = expression_arena[base] {
582                                    let global = &resolve_context.global_vars[global_handle];
583                                    match global.space {
584                                        crate::AddressSpace::Uniform => {
585                                            super::Capabilities::BUFFER_BINDING_ARRAY_NON_UNIFORM_INDEXING
586                                        }
587                                        crate::AddressSpace::Storage { .. } => {
588                                            super::Capabilities::STORAGE_BUFFER_BINDING_ARRAY_NON_UNIFORM_INDEXING
589                                        }
590                                        _ => unreachable!(),
591                                    }
592                                } else {
593                                    unreachable!()
594                                }
595                            }
596                        };
597
598                        true
599                    }
600                    _ => false,
601                };
602
603                if self[index].uniformity.non_uniform_result.is_some()
604                    && !capabilities.contains(needed_caps)
605                    && is_binding_array
606                {
607                    return Err(ExpressionError::MissingCapabilities(needed_caps));
608                }
609
610                Uniformity {
611                    non_uniform_result: self
612                        .add_assignable_ref(base, &mut assignable_global)
613                        .or(self.add_ref(index)),
614                    requirements: UniformityRequirements::empty(),
615                }
616            }
617            E::AccessIndex { base, .. } => Uniformity {
618                non_uniform_result: self.add_assignable_ref(base, &mut assignable_global),
619                requirements: UniformityRequirements::empty(),
620            },
621            // always uniform
622            E::Splat { size: _, value } => Uniformity {
623                non_uniform_result: self.add_ref(value),
624                requirements: UniformityRequirements::empty(),
625            },
626            E::Swizzle { vector, .. } => Uniformity {
627                non_uniform_result: self.add_ref(vector),
628                requirements: UniformityRequirements::empty(),
629            },
630            E::Literal(_) | E::Constant(_) | E::Override(_) | E::ZeroValue(_) => Uniformity::new(),
631            E::Compose { ref components, .. } => {
632                let non_uniform_result = components
633                    .iter()
634                    .fold(None, |nur, &comp| nur.or(self.add_ref(comp)));
635                Uniformity {
636                    non_uniform_result,
637                    requirements: UniformityRequirements::empty(),
638                }
639            }
640            // depends on the builtin
641            E::FunctionArgument(index) => {
642                let arg = &resolve_context.arguments[index as usize];
643                let uniform = match arg.binding {
644                    Some(crate::Binding::BuiltIn(
645                        // per-work-group built-ins are uniform
646                        crate::BuiltIn::WorkGroupId
647                        | crate::BuiltIn::WorkGroupSize
648                        | crate::BuiltIn::NumWorkGroups,
649                    )) => true,
650                    _ => false,
651                };
652                Uniformity {
653                    non_uniform_result: if uniform { None } else { Some(handle) },
654                    requirements: UniformityRequirements::empty(),
655                }
656            }
657            // depends on the address space
658            E::GlobalVariable(gh) => {
659                use crate::AddressSpace as As;
660                assignable_global = Some(gh);
661                let var = &resolve_context.global_vars[gh];
662                let uniform = match var.space {
663                    // local data is non-uniform
664                    As::Function | As::Private | As::RayPayload | As::IncomingRayPayload => false,
665                    // workgroup memory is exclusively accessed by the group
666                    // task payload memory is very similar to workgroup memory
667                    As::WorkGroup | As::TaskPayload => true,
668                    // uniform data
669                    As::Uniform | As::Immediate => true,
670                    // storage data is only uniform when read-only
671                    As::Storage { access } => !access.contains(crate::StorageAccess::STORE),
672                    As::Handle => false,
673                };
674                Uniformity {
675                    non_uniform_result: if uniform { None } else { Some(handle) },
676                    requirements: UniformityRequirements::empty(),
677                }
678            }
679            E::LocalVariable(_) => Uniformity {
680                non_uniform_result: Some(handle),
681                requirements: UniformityRequirements::empty(),
682            },
683            E::Load { pointer } => {
684                let non_uniform_result = self.add_ref(pointer);
685                Uniformity {
686                    non_uniform_result,
687                    requirements: UniformityRequirements::empty(),
688                }
689            }
690            E::ImageSample {
691                image,
692                sampler,
693                gather: _,
694                coordinate,
695                array_index,
696                offset,
697                level,
698                depth_ref,
699                clamp_to_edge: _,
700            } => {
701                let image_storage = GlobalOrArgument::from_expression(expression_arena, image)?;
702                let sampler_storage = GlobalOrArgument::from_expression(expression_arena, sampler)?;
703
704                match (image_storage, sampler_storage) {
705                    (GlobalOrArgument::Global(image), GlobalOrArgument::Global(sampler)) => {
706                        self.sampling_set.insert(SamplingKey { image, sampler });
707                    }
708                    _ => {
709                        self.sampling.insert(Sampling {
710                            image: image_storage,
711                            sampler: sampler_storage,
712                        });
713                    }
714                }
715
716                // "nur" == "Non-Uniform Result"
717                let array_nur = array_index.and_then(|h| self.add_ref(h));
718                let level_nur = match level {
719                    Sl::Auto | Sl::Zero => None,
720                    Sl::Exact(h) | Sl::Bias(h) => self.add_ref(h),
721                    Sl::Gradient { x, y } => self.add_ref(x).or(self.add_ref(y)),
722                };
723                let dref_nur = depth_ref.and_then(|h| self.add_ref(h));
724                let offset_nur = offset.and_then(|h| self.add_ref(h));
725                Uniformity {
726                    non_uniform_result: self
727                        .add_ref(image)
728                        .or(self.add_ref(sampler))
729                        .or(self.add_ref(coordinate))
730                        .or(array_nur)
731                        .or(level_nur)
732                        .or(dref_nur)
733                        .or(offset_nur),
734                    requirements: if level.implicit_derivatives() {
735                        UniformityRequirements::IMPLICIT_LEVEL
736                    } else {
737                        UniformityRequirements::empty()
738                    },
739                }
740            }
741            E::ImageLoad {
742                image,
743                coordinate,
744                array_index,
745                sample,
746                level,
747            } => {
748                let array_nur = array_index.and_then(|h| self.add_ref(h));
749                let sample_nur = sample.and_then(|h| self.add_ref(h));
750                let level_nur = level.and_then(|h| self.add_ref(h));
751                Uniformity {
752                    non_uniform_result: self
753                        .add_ref(image)
754                        .or(self.add_ref(coordinate))
755                        .or(array_nur)
756                        .or(sample_nur)
757                        .or(level_nur),
758                    requirements: UniformityRequirements::empty(),
759                }
760            }
761            E::ImageQuery { image, query } => {
762                let query_nur = match query {
763                    crate::ImageQuery::Size { level: Some(h) } => self.add_ref(h),
764                    _ => None,
765                };
766                Uniformity {
767                    non_uniform_result: self.add_ref_impl(image, GlobalUse::QUERY).or(query_nur),
768                    requirements: UniformityRequirements::empty(),
769                }
770            }
771            E::Unary { expr, .. } => Uniformity {
772                non_uniform_result: self.add_ref(expr),
773                requirements: UniformityRequirements::empty(),
774            },
775            E::Binary { left, right, .. } => Uniformity {
776                non_uniform_result: self.add_ref(left).or(self.add_ref(right)),
777                requirements: UniformityRequirements::empty(),
778            },
779            E::Select {
780                condition,
781                accept,
782                reject,
783            } => Uniformity {
784                non_uniform_result: self
785                    .add_ref(condition)
786                    .or(self.add_ref(accept))
787                    .or(self.add_ref(reject)),
788                requirements: UniformityRequirements::empty(),
789            },
790            // explicit derivatives require uniform
791            E::Derivative { expr, .. } => Uniformity {
792                //Note: taking a derivative of a uniform doesn't make it non-uniform
793                non_uniform_result: self.add_ref(expr),
794                requirements: UniformityRequirements::DERIVATIVE,
795            },
796            E::Relational { argument, .. } => Uniformity {
797                non_uniform_result: self.add_ref(argument),
798                requirements: UniformityRequirements::empty(),
799            },
800            E::Math {
801                fun: _,
802                arg,
803                arg1,
804                arg2,
805                arg3,
806            } => {
807                let arg1_nur = arg1.and_then(|h| self.add_ref(h));
808                let arg2_nur = arg2.and_then(|h| self.add_ref(h));
809                let arg3_nur = arg3.and_then(|h| self.add_ref(h));
810                Uniformity {
811                    non_uniform_result: self.add_ref(arg).or(arg1_nur).or(arg2_nur).or(arg3_nur),
812                    requirements: UniformityRequirements::empty(),
813                }
814            }
815            E::As { expr, .. } | E::Bitcast { expr, .. } => Uniformity {
816                non_uniform_result: self.add_ref(expr),
817                requirements: UniformityRequirements::empty(),
818            },
819            E::CallResult(function) => other_functions[function.index()].uniformity.clone(),
820            E::AtomicResult { .. } | E::RayQueryProceedResult => Uniformity {
821                non_uniform_result: Some(handle),
822                requirements: UniformityRequirements::empty(),
823            },
824            E::WorkGroupUniformLoadResult { .. } => Uniformity {
825                // The result of WorkGroupUniformLoad is always uniform by definition
826                non_uniform_result: None,
827                // The call is what cares about uniformity, not the expression
828                // This expression is never emitted, so this requirement should never be used anyway?
829                requirements: UniformityRequirements::empty(),
830            },
831            E::ArrayLength(expr) => Uniformity {
832                non_uniform_result: self.add_ref_impl(expr, GlobalUse::QUERY),
833                requirements: UniformityRequirements::empty(),
834            },
835            E::RayQueryGetIntersection {
836                query,
837                committed: _,
838            } => Uniformity {
839                non_uniform_result: self.add_ref(query),
840                requirements: UniformityRequirements::empty(),
841            },
842            E::SubgroupBallotResult => Uniformity {
843                non_uniform_result: Some(handle),
844                requirements: UniformityRequirements::empty(),
845            },
846            E::SubgroupOperationResult { .. } => Uniformity {
847                non_uniform_result: Some(handle),
848                requirements: UniformityRequirements::empty(),
849            },
850            E::RayQueryVertexPositions {
851                query,
852                committed: _,
853            } => Uniformity {
854                non_uniform_result: self.add_ref(query),
855                requirements: UniformityRequirements::empty(),
856            },
857            E::HitObjectQuery {
858                hit_object,
859                query: _,
860            } => Uniformity {
861                non_uniform_result: self.add_ref(hit_object),
862                requirements: UniformityRequirements::empty(),
863            },
864            E::CooperativeLoad { ref data, .. } => Uniformity {
865                non_uniform_result: self.add_ref(data.pointer).or(self.add_ref(data.stride)),
866                requirements: UniformityRequirements::COOP_OPS,
867            },
868            E::CooperativeMultiplyAdd { a, b, c } => Uniformity {
869                non_uniform_result: self.add_ref(a).or(self.add_ref(b).or(self.add_ref(c))),
870                requirements: UniformityRequirements::COOP_OPS,
871            },
872        };
873
874        let ty = resolve_context.resolve(expression, |h| Ok(&self[h].ty))?;
875        self.expressions[handle.index()] = ExpressionInfo {
876            uniformity,
877            ref_count: 0,
878            assignable_global,
879            ty,
880        };
881        Ok(())
882    }
883
884    /// Analyzes the uniformity requirements of a block (as a sequence of statements).
885    /// Returns the uniformity characteristics at the *function* level, i.e.
886    /// whether or not the function requires to be called in uniform control flow,
887    /// and whether the produced result is not disrupting the control flow.
888    ///
889    /// The parent control flow is uniform if `disruptor.is_none()`.
890    ///
891    /// Returns a `NonUniformControlFlow` error if any of the expressions in the block
892    /// require uniformity, but the current flow is non-uniform.
893    #[allow(clippy::or_fun_call)]
894    fn process_block(
895        &mut self,
896        statements: &crate::Block,
897        other_functions: &[FunctionInfo],
898        mut disruptor: Option<UniformityDisruptor>,
899        expression_arena: &Arena<crate::Expression>,
900        diagnostic_filter_arena: &Arena<DiagnosticFilterNode>,
901    ) -> Result<FunctionUniformity, WithSpan<FunctionError>> {
902        use crate::Statement as S;
903
904        let mut combined_uniformity = FunctionUniformity::new();
905        for statement in statements {
906            let uniformity = match *statement {
907                S::Emit(ref range) => {
908                    let mut requirements = UniformityRequirements::empty();
909                    for expr in range.clone() {
910                        let req = self.expressions[expr.index()].uniformity.requirements;
911                        if self
912                            .flags
913                            .contains(ValidationFlags::CONTROL_FLOW_UNIFORMITY)
914                            && !req.is_empty()
915                        {
916                            if let Some(cause) = disruptor {
917                                let severity = DiagnosticFilterNode::search(
918                                    self.diagnostic_filter_leaf,
919                                    diagnostic_filter_arena,
920                                    StandardFilterableTriggeringRule::DerivativeUniformity,
921                                );
922                                severity.report_diag(
923                                    FunctionError::NonUniformControlFlow(req, expr, cause)
924                                        .with_span_handle(expr, expression_arena),
925                                    // TODO: Yes, this isn't contextualized with source, because
926                                    // the user is supposed to render what would normally be an
927                                    // error here. Once we actually support warning-level
928                                    // diagnostic items, then we won't need this non-compliant hack:
929                                    // <https://github.com/gfx-rs/wgpu/issues/6458>
930                                    |e, level| log::log!(level, "{e}"),
931                                )?;
932                            }
933                        }
934                        requirements |= req;
935                    }
936                    FunctionUniformity {
937                        result: Uniformity {
938                            non_uniform_result: None,
939                            requirements,
940                        },
941                        exit: ExitFlags::empty(),
942                    }
943                }
944                S::Break | S::Continue => FunctionUniformity::new(),
945                S::Kill => FunctionUniformity {
946                    result: Uniformity::new(),
947                    exit: if disruptor.is_some() {
948                        ExitFlags::MAY_KILL
949                    } else {
950                        ExitFlags::empty()
951                    },
952                },
953                S::ControlBarrier(_) | S::MemoryBarrier(_) => FunctionUniformity {
954                    result: Uniformity {
955                        non_uniform_result: None,
956                        requirements: UniformityRequirements::WORK_GROUP_BARRIER,
957                    },
958                    exit: ExitFlags::empty(),
959                },
960                S::WorkGroupUniformLoad { pointer, .. } => {
961                    let _condition_nur = self.add_ref(pointer);
962
963                    // Don't check that this call occurs in uniform control flow until Naga implements WGSL's standard
964                    // uniformity analysis (https://github.com/gfx-rs/naga/issues/1744).
965                    // The uniformity analysis Naga uses now is less accurate than the one in the WGSL standard,
966                    // causing Naga to reject correct uses of `workgroupUniformLoad` in some interesting programs.
967
968                    /*
969                    if self
970                        .flags
971                        .contains(super::ValidationFlags::CONTROL_FLOW_UNIFORMITY)
972                    {
973                        let condition_nur = self.add_ref(pointer);
974                        let this_disruptor =
975                            disruptor.or(condition_nur.map(UniformityDisruptor::Expression));
976                        if let Some(cause) = this_disruptor {
977                            return Err(FunctionError::NonUniformWorkgroupUniformLoad(cause)
978                                .with_span_static(*span, "WorkGroupUniformLoad"));
979                        }
980                    } */
981                    FunctionUniformity {
982                        result: Uniformity {
983                            non_uniform_result: None,
984                            requirements: UniformityRequirements::WORK_GROUP_BARRIER,
985                        },
986                        exit: ExitFlags::empty(),
987                    }
988                }
989                S::Block(ref b) => self.process_block(
990                    b,
991                    other_functions,
992                    disruptor,
993                    expression_arena,
994                    diagnostic_filter_arena,
995                )?,
996                S::If {
997                    condition,
998                    ref accept,
999                    ref reject,
1000                } => {
1001                    let condition_nur = self.add_ref(condition);
1002                    let branch_disruptor =
1003                        disruptor.or(condition_nur.map(UniformityDisruptor::Expression));
1004                    let accept_uniformity = self.process_block(
1005                        accept,
1006                        other_functions,
1007                        branch_disruptor,
1008                        expression_arena,
1009                        diagnostic_filter_arena,
1010                    )?;
1011                    let reject_uniformity = self.process_block(
1012                        reject,
1013                        other_functions,
1014                        branch_disruptor,
1015                        expression_arena,
1016                        diagnostic_filter_arena,
1017                    )?;
1018                    accept_uniformity | reject_uniformity
1019                }
1020                S::Switch {
1021                    selector,
1022                    ref cases,
1023                } => {
1024                    let selector_nur = self.add_ref(selector);
1025                    let branch_disruptor =
1026                        disruptor.or(selector_nur.map(UniformityDisruptor::Expression));
1027                    let mut uniformity = FunctionUniformity::new();
1028                    let mut case_disruptor = branch_disruptor;
1029                    for case in cases.iter() {
1030                        let case_uniformity = self.process_block(
1031                            &case.body,
1032                            other_functions,
1033                            case_disruptor,
1034                            expression_arena,
1035                            diagnostic_filter_arena,
1036                        )?;
1037                        case_disruptor = if case.fall_through {
1038                            case_disruptor.or(case_uniformity.exit_disruptor())
1039                        } else {
1040                            branch_disruptor
1041                        };
1042                        uniformity = uniformity | case_uniformity;
1043                    }
1044                    uniformity
1045                }
1046                S::Loop {
1047                    ref body,
1048                    ref continuing,
1049                    break_if,
1050                } => {
1051                    let body_uniformity = self.process_block(
1052                        body,
1053                        other_functions,
1054                        disruptor,
1055                        expression_arena,
1056                        diagnostic_filter_arena,
1057                    )?;
1058                    let continuing_disruptor = disruptor.or(body_uniformity.exit_disruptor());
1059                    let continuing_uniformity = self.process_block(
1060                        continuing,
1061                        other_functions,
1062                        continuing_disruptor,
1063                        expression_arena,
1064                        diagnostic_filter_arena,
1065                    )?;
1066                    if let Some(expr) = break_if {
1067                        let _ = self.add_ref(expr);
1068                    }
1069                    body_uniformity | continuing_uniformity
1070                }
1071                S::Return { value } => FunctionUniformity {
1072                    result: Uniformity {
1073                        non_uniform_result: value.and_then(|expr| self.add_ref(expr)),
1074                        requirements: UniformityRequirements::empty(),
1075                    },
1076                    exit: if disruptor.is_some() {
1077                        ExitFlags::MAY_RETURN
1078                    } else {
1079                        ExitFlags::empty()
1080                    },
1081                },
1082                // Here and below, the used expressions are already emitted,
1083                // and their results do not affect the function return value,
1084                // so we can ignore their non-uniformity.
1085                S::Store { pointer, value } => {
1086                    let _ = self.add_ref_impl(pointer, GlobalUse::WRITE);
1087                    let _ = self.add_ref(value);
1088                    FunctionUniformity::new()
1089                }
1090                S::ImageStore {
1091                    image,
1092                    coordinate,
1093                    array_index,
1094                    value,
1095                } => {
1096                    let _ = self.add_ref_impl(image, GlobalUse::WRITE);
1097                    if let Some(expr) = array_index {
1098                        let _ = self.add_ref(expr);
1099                    }
1100                    let _ = self.add_ref(coordinate);
1101                    let _ = self.add_ref(value);
1102                    FunctionUniformity::new()
1103                }
1104                S::Call {
1105                    function,
1106                    ref arguments,
1107                    result: _,
1108                } => {
1109                    for &argument in arguments {
1110                        let _ = self.add_ref(argument);
1111                    }
1112                    let info = &other_functions[function.index()];
1113                    //Note: the result is validated by the Validator, not here
1114                    self.process_call(info, arguments, expression_arena)?
1115                }
1116                S::Atomic {
1117                    pointer,
1118                    ref fun,
1119                    value,
1120                    result: _,
1121                } => {
1122                    let _ = self.add_ref_impl(pointer, GlobalUse::READ | GlobalUse::WRITE);
1123                    let _ = self.add_ref(value);
1124                    if let crate::AtomicFunction::Exchange { compare: Some(cmp) } = *fun {
1125                        let _ = self.add_ref(cmp);
1126                    }
1127                    FunctionUniformity::new()
1128                }
1129                S::ImageAtomic {
1130                    image,
1131                    coordinate,
1132                    array_index,
1133                    fun: _,
1134                    value,
1135                } => {
1136                    let _ = self.add_ref_impl(image, GlobalUse::ATOMIC);
1137                    let _ = self.add_ref(coordinate);
1138                    if let Some(expr) = array_index {
1139                        let _ = self.add_ref(expr);
1140                    }
1141                    let _ = self.add_ref(value);
1142                    FunctionUniformity::new()
1143                }
1144                S::RayQuery { query, ref fun } => {
1145                    let _ = self.add_ref(query);
1146                    match *fun {
1147                        crate::RayQueryFunction::Initialize {
1148                            acceleration_structure,
1149                            descriptor,
1150                        } => {
1151                            let _ = self.add_ref(acceleration_structure);
1152                            let _ = self.add_ref(descriptor);
1153                        }
1154                        crate::RayQueryFunction::Proceed { result: _ } => {}
1155                        crate::RayQueryFunction::GenerateIntersection { hit_t } => {
1156                            let _ = self.add_ref(hit_t);
1157                        }
1158                        crate::RayQueryFunction::ConfirmIntersection => {}
1159                        crate::RayQueryFunction::Terminate => {}
1160                        crate::RayQueryFunction::Begin => {}
1161                    }
1162                    FunctionUniformity::new()
1163                }
1164                S::SubgroupBallot {
1165                    result: _,
1166                    predicate,
1167                } => {
1168                    if let Some(predicate) = predicate {
1169                        let _ = self.add_ref(predicate);
1170                    }
1171                    FunctionUniformity::new()
1172                }
1173                S::SubgroupCollectiveOperation {
1174                    op: _,
1175                    collective_op: _,
1176                    argument,
1177                    result: _,
1178                } => {
1179                    let _ = self.add_ref(argument);
1180                    FunctionUniformity::new()
1181                }
1182                S::SubgroupGather {
1183                    mode,
1184                    argument,
1185                    result: _,
1186                } => {
1187                    let _ = self.add_ref(argument);
1188                    match mode {
1189                        crate::GatherMode::BroadcastFirst => {}
1190                        crate::GatherMode::Broadcast(index)
1191                        | crate::GatherMode::Shuffle(index)
1192                        | crate::GatherMode::ShuffleDown(index)
1193                        | crate::GatherMode::ShuffleUp(index)
1194                        | crate::GatherMode::ShuffleXor(index)
1195                        | crate::GatherMode::QuadBroadcast(index) => {
1196                            let _ = self.add_ref(index);
1197                        }
1198                        crate::GatherMode::QuadSwap(_) => {}
1199                    }
1200                    FunctionUniformity::new()
1201                }
1202                S::SubgroupBallotFindBit {
1203                    order: _,
1204                    argument,
1205                    result: _,
1206                } => {
1207                    let _ = self.add_ref(argument);
1208                    FunctionUniformity::new()
1209                }
1210                S::CooperativeStore { target, ref data } => FunctionUniformity {
1211                    result: Uniformity {
1212                        non_uniform_result: self
1213                            .add_ref(target)
1214                            .or(self.add_ref_impl(data.pointer, GlobalUse::WRITE))
1215                            .or(self.add_ref(data.stride)),
1216                        requirements: UniformityRequirements::COOP_OPS,
1217                    },
1218                    exit: ExitFlags::empty(),
1219                },
1220                S::RayPipelineFunction(ref fun) => {
1221                    match *fun {
1222                        crate::RayPipelineFunction::TraceRay {
1223                            acceleration_structure,
1224                            descriptor,
1225                            payload,
1226                        } => {
1227                            let _ = self.add_ref(acceleration_structure);
1228                            let _ = self.add_ref(descriptor);
1229                            let _ = self.add_ref(payload);
1230                        }
1231                        crate::RayPipelineFunction::ReorderThread { hint, bits } => {
1232                            let _ = self.add_ref(hint);
1233                            let _ = self.add_ref(bits);
1234                        }
1235                    }
1236                    FunctionUniformity::new()
1237                }
1238                S::HitObject {
1239                    hit_object,
1240                    ref fun,
1241                } => {
1242                    let _ = self.add_ref(hit_object);
1243                    match *fun {
1244                        crate::HitObjectFunction::TraceRay {
1245                            acceleration_structure,
1246                            descriptor,
1247                            payload,
1248                        } => {
1249                            let _ = self.add_ref(acceleration_structure);
1250                            let _ = self.add_ref(descriptor);
1251                            let _ = self.add_ref(payload);
1252                        }
1253                        crate::HitObjectFunction::RecordMiss { descriptor } => {
1254                            let _ = self.add_ref(descriptor);
1255                        }
1256                        crate::HitObjectFunction::RecordFromQuery { query } => {
1257                            let _ = self.add_ref(query);
1258                        }
1259                        crate::HitObjectFunction::RecordEmpty => {}
1260                        crate::HitObjectFunction::ExecuteShader { payload } => {
1261                            let _ = self.add_ref(payload);
1262                        }
1263                        crate::HitObjectFunction::Reorder { hint } => {
1264                            self.add_reorder_hint_refs(hint);
1265                        }
1266                    }
1267                    FunctionUniformity::new()
1268                }
1269                S::DebugPrintf {
1270                    format: _,
1271                    ref arguments,
1272                } => {
1273                    let mut requirements = UniformityRequirements::empty();
1274                    for &expr in arguments {
1275                        let _ = self.add_ref(expr);
1276                        requirements |= self.expressions[expr.index()].uniformity.requirements;
1277                    }
1278                    FunctionUniformity {
1279                        result: Uniformity {
1280                            non_uniform_result: None,
1281                            requirements,
1282                        },
1283                        exit: ExitFlags::empty(),
1284                    }
1285                }
1286            };
1287
1288            disruptor = disruptor.or(uniformity.exit_disruptor());
1289            combined_uniformity = combined_uniformity | uniformity;
1290        }
1291        Ok(combined_uniformity)
1292    }
1293}
1294
1295impl ModuleInfo {
1296    /// Populates `self.const_expression_types`
1297    pub(super) fn process_const_expression(
1298        &mut self,
1299        handle: Handle<crate::Expression>,
1300        resolve_context: &ResolveContext,
1301        gctx: crate::proc::GlobalCtx,
1302    ) -> Result<(), super::ConstExpressionError> {
1303        self.const_expression_types[handle.index()] =
1304            resolve_context.resolve(&gctx.global_expressions[handle], |h| Ok(&self[h]))?;
1305        Ok(())
1306    }
1307
1308    /// Builds the `FunctionInfo` based on the function, and validates the
1309    /// uniform control flow if required by the expressions of this function.
1310    pub(super) fn process_function(
1311        &self,
1312        fun: &crate::Function,
1313        module: &crate::Module,
1314        flags: ValidationFlags,
1315        capabilities: super::Capabilities,
1316    ) -> Result<FunctionInfo, WithSpan<FunctionError>> {
1317        let mut info = FunctionInfo {
1318            flags,
1319            available_stages: ShaderStages::all(),
1320            uniformity: Uniformity::new(),
1321            may_kill: false,
1322            sampling_set: crate::FastHashSet::default(),
1323            global_uses: vec![GlobalUse::empty(); module.global_variables.len()].into_boxed_slice(),
1324            expressions: vec![ExpressionInfo::new(); fun.expressions.len()].into_boxed_slice(),
1325            sampling: crate::FastHashSet::default(),
1326            dual_source_blending: false,
1327            diagnostic_filter_leaf: fun.diagnostic_filter_leaf,
1328        };
1329        let resolve_context =
1330            ResolveContext::with_locals(module, &fun.local_variables, &fun.arguments);
1331
1332        for (handle, _) in fun.expressions.iter() {
1333            if let Err(source) = info.process_expression(
1334                handle,
1335                &fun.expressions,
1336                &self.functions,
1337                &resolve_context,
1338                capabilities,
1339            ) {
1340                return Err(FunctionError::Expression { handle, source }
1341                    .with_span_handle(handle, &fun.expressions));
1342            }
1343        }
1344
1345        for (_, expr) in fun.local_variables.iter() {
1346            if let Some(init) = expr.init {
1347                let _ = info.add_ref(init);
1348            }
1349        }
1350
1351        let uniformity = info.process_block(
1352            &fun.body,
1353            &self.functions,
1354            None,
1355            &fun.expressions,
1356            &module.diagnostic_filters,
1357        )?;
1358        info.uniformity = uniformity.result;
1359        info.may_kill = uniformity.exit.contains(ExitFlags::MAY_KILL);
1360
1361        // If there are any globals referenced directly by a named expression,
1362        // ensure they are marked as used even if they are not referenced
1363        // anywhere else. An important case where this matters is phony
1364        // assignments used to include a global in the shader's resource
1365        // interface. https://www.w3.org/TR/WGSL/#phony-assignment-section
1366        for &handle in fun.named_expressions.keys() {
1367            if let Some(global) = info[handle].assignable_global {
1368                if info.global_uses[global.index()].is_empty() {
1369                    info.global_uses[global.index()] = GlobalUse::QUERY;
1370                }
1371            }
1372        }
1373
1374        Ok(info)
1375    }
1376
1377    pub fn get_entry_point(&self, index: usize) -> &FunctionInfo {
1378        &self.entry_points[index]
1379    }
1380}
1381
1382#[test]
1383fn uniform_control_flow() {
1384    use crate::{Expression as E, Statement as S};
1385
1386    let mut type_arena = crate::UniqueArena::new();
1387    let ty = type_arena.insert(
1388        crate::Type {
1389            name: None,
1390            inner: crate::TypeInner::Vector {
1391                size: crate::VectorSize::Bi,
1392                scalar: crate::Scalar::F32,
1393            },
1394        },
1395        Default::default(),
1396    );
1397    let mut global_var_arena = Arena::new();
1398    let non_uniform_global = global_var_arena.append(
1399        crate::GlobalVariable {
1400            name: None,
1401            init: None,
1402            ty,
1403            space: crate::AddressSpace::Handle,
1404            binding: None,
1405            memory_decorations: crate::MemoryDecorations::empty(),
1406        },
1407        Default::default(),
1408    );
1409    let uniform_global = global_var_arena.append(
1410        crate::GlobalVariable {
1411            name: None,
1412            init: None,
1413            ty,
1414            binding: None,
1415            space: crate::AddressSpace::Uniform,
1416            memory_decorations: crate::MemoryDecorations::empty(),
1417        },
1418        Default::default(),
1419    );
1420
1421    let mut expressions = Arena::new();
1422    // checks the uniform control flow
1423    let constant_expr = expressions.append(E::Literal(crate::Literal::U32(0)), Default::default());
1424    // checks the non-uniform control flow
1425    let derivative_expr = expressions.append(
1426        E::Derivative {
1427            axis: crate::DerivativeAxis::X,
1428            ctrl: crate::DerivativeControl::None,
1429            expr: constant_expr,
1430        },
1431        Default::default(),
1432    );
1433    let emit_range_constant_derivative = expressions.range_from(0);
1434    let non_uniform_global_expr =
1435        expressions.append(E::GlobalVariable(non_uniform_global), Default::default());
1436    let uniform_global_expr =
1437        expressions.append(E::GlobalVariable(uniform_global), Default::default());
1438    let emit_range_globals = expressions.range_from(2);
1439
1440    // checks the QUERY flag
1441    let query_expr = expressions.append(E::ArrayLength(uniform_global_expr), Default::default());
1442    // checks the transitive WRITE flag
1443    let access_expr = expressions.append(
1444        E::AccessIndex {
1445            base: non_uniform_global_expr,
1446            index: 1,
1447        },
1448        Default::default(),
1449    );
1450    let emit_range_query_access_globals = expressions.range_from(2);
1451
1452    let mut info = FunctionInfo {
1453        flags: ValidationFlags::all(),
1454        available_stages: ShaderStages::all(),
1455        uniformity: Uniformity::new(),
1456        may_kill: false,
1457        sampling_set: crate::FastHashSet::default(),
1458        global_uses: vec![GlobalUse::empty(); global_var_arena.len()].into_boxed_slice(),
1459        expressions: vec![ExpressionInfo::new(); expressions.len()].into_boxed_slice(),
1460        sampling: crate::FastHashSet::default(),
1461        dual_source_blending: false,
1462        diagnostic_filter_leaf: None,
1463    };
1464    let resolve_context = ResolveContext {
1465        constants: &Arena::new(),
1466        overrides: &Arena::new(),
1467        types: &type_arena,
1468        special_types: &crate::SpecialTypes::default(),
1469        global_vars: &global_var_arena,
1470        local_vars: &Arena::new(),
1471        functions: &Arena::new(),
1472        arguments: &[],
1473    };
1474    for (handle, _) in expressions.iter() {
1475        info.process_expression(
1476            handle,
1477            &expressions,
1478            &[],
1479            &resolve_context,
1480            super::Capabilities::empty(),
1481        )
1482        .unwrap();
1483    }
1484    assert_eq!(info[non_uniform_global_expr].ref_count, 1);
1485    assert_eq!(info[uniform_global_expr].ref_count, 1);
1486    assert_eq!(info[query_expr].ref_count, 0);
1487    assert_eq!(info[access_expr].ref_count, 0);
1488    assert_eq!(info[non_uniform_global], GlobalUse::empty());
1489    assert_eq!(info[uniform_global], GlobalUse::QUERY);
1490
1491    let stmt_emit1 = S::Emit(emit_range_globals.clone());
1492    let stmt_if_uniform = S::If {
1493        condition: uniform_global_expr,
1494        accept: crate::Block::new(),
1495        reject: vec![
1496            S::Emit(emit_range_constant_derivative.clone()),
1497            S::Store {
1498                pointer: constant_expr,
1499                value: derivative_expr,
1500            },
1501        ]
1502        .into(),
1503    };
1504    assert_eq!(
1505        info.process_block(
1506            &vec![stmt_emit1, stmt_if_uniform].into(),
1507            &[],
1508            None,
1509            &expressions,
1510            &Arena::new(),
1511        ),
1512        Ok(FunctionUniformity {
1513            result: Uniformity {
1514                non_uniform_result: None,
1515                requirements: UniformityRequirements::DERIVATIVE,
1516            },
1517            exit: ExitFlags::empty(),
1518        }),
1519    );
1520    assert_eq!(info[constant_expr].ref_count, 2);
1521    assert_eq!(info[uniform_global], GlobalUse::READ | GlobalUse::QUERY);
1522
1523    let stmt_emit2 = S::Emit(emit_range_globals.clone());
1524    let stmt_if_non_uniform = S::If {
1525        condition: non_uniform_global_expr,
1526        accept: vec![
1527            S::Emit(emit_range_constant_derivative),
1528            S::Store {
1529                pointer: constant_expr,
1530                value: derivative_expr,
1531            },
1532        ]
1533        .into(),
1534        reject: crate::Block::new(),
1535    };
1536    {
1537        let block_info = info.process_block(
1538            &vec![stmt_emit2.clone(), stmt_if_non_uniform.clone()].into(),
1539            &[],
1540            None,
1541            &expressions,
1542            &Arena::new(),
1543        );
1544        if DISABLE_UNIFORMITY_REQ_FOR_FRAGMENT_STAGE {
1545            assert_eq!(info[derivative_expr].ref_count, 2);
1546        } else {
1547            assert_eq!(
1548                block_info,
1549                Err(FunctionError::NonUniformControlFlow(
1550                    UniformityRequirements::DERIVATIVE,
1551                    derivative_expr,
1552                    UniformityDisruptor::Expression(non_uniform_global_expr)
1553                )
1554                .with_span()),
1555            );
1556            assert_eq!(info[derivative_expr].ref_count, 1);
1557
1558            // Test that the same thing passes when we disable the `derivative_uniformity`
1559            let mut diagnostic_filters = Arena::new();
1560            let diagnostic_filter_leaf = diagnostic_filters.append(
1561                DiagnosticFilterNode {
1562                    inner: crate::diagnostic_filter::DiagnosticFilter {
1563                        new_severity: crate::diagnostic_filter::Severity::Off,
1564                        triggering_rule:
1565                            crate::diagnostic_filter::FilterableTriggeringRule::Standard(
1566                                StandardFilterableTriggeringRule::DerivativeUniformity,
1567                            ),
1568                    },
1569                    parent: None,
1570                },
1571                crate::Span::default(),
1572            );
1573            let mut info = FunctionInfo {
1574                diagnostic_filter_leaf: Some(diagnostic_filter_leaf),
1575                ..info.clone()
1576            };
1577
1578            let block_info = info.process_block(
1579                &vec![stmt_emit2, stmt_if_non_uniform].into(),
1580                &[],
1581                None,
1582                &expressions,
1583                &diagnostic_filters,
1584            );
1585            assert_eq!(
1586                block_info,
1587                Ok(FunctionUniformity {
1588                    result: Uniformity {
1589                        non_uniform_result: None,
1590                        requirements: UniformityRequirements::DERIVATIVE,
1591                    },
1592                    exit: ExitFlags::empty()
1593                }),
1594            );
1595            assert_eq!(info[derivative_expr].ref_count, 2);
1596        }
1597    }
1598    assert_eq!(info[non_uniform_global], GlobalUse::READ);
1599
1600    let stmt_emit3 = S::Emit(emit_range_globals);
1601    let stmt_return_non_uniform = S::Return {
1602        value: Some(non_uniform_global_expr),
1603    };
1604    assert_eq!(
1605        info.process_block(
1606            &vec![stmt_emit3, stmt_return_non_uniform].into(),
1607            &[],
1608            Some(UniformityDisruptor::Return),
1609            &expressions,
1610            &Arena::new(),
1611        ),
1612        Ok(FunctionUniformity {
1613            result: Uniformity {
1614                non_uniform_result: Some(non_uniform_global_expr),
1615                requirements: UniformityRequirements::empty(),
1616            },
1617            exit: ExitFlags::MAY_RETURN,
1618        }),
1619    );
1620    assert_eq!(info[non_uniform_global_expr].ref_count, 3);
1621
1622    // Check that uniformity requirements reach through a pointer
1623    let stmt_emit4 = S::Emit(emit_range_query_access_globals);
1624    let stmt_assign = S::Store {
1625        pointer: access_expr,
1626        value: query_expr,
1627    };
1628    let stmt_return_pointer = S::Return {
1629        value: Some(access_expr),
1630    };
1631    let stmt_kill = S::Kill;
1632    assert_eq!(
1633        info.process_block(
1634            &vec![stmt_emit4, stmt_assign, stmt_kill, stmt_return_pointer].into(),
1635            &[],
1636            Some(UniformityDisruptor::Discard),
1637            &expressions,
1638            &Arena::new(),
1639        ),
1640        Ok(FunctionUniformity {
1641            result: Uniformity {
1642                non_uniform_result: Some(non_uniform_global_expr),
1643                requirements: UniformityRequirements::empty(),
1644            },
1645            exit: ExitFlags::all(),
1646        }),
1647    );
1648    assert_eq!(info[non_uniform_global], GlobalUse::READ | GlobalUse::WRITE);
1649}