Skip to main content

naga/back/msl/
writer.rs

1use alloc::{
2    format,
3    string::{String, ToString},
4    vec,
5    vec::Vec,
6};
7use core::{
8    cmp::Ordering,
9    fmt::{Display, Error as FmtError, Formatter, Write},
10    iter,
11};
12use num_traits::real::Real as _;
13
14use half::f16;
15
16use super::{
17    ray::RT_NAMESPACE, sampler as sm, Error, LocationMode, Options, PipelineOptions,
18    TranslationInfo, NAMESPACE, WRAPPED_ARRAY_FIELD,
19};
20use crate::{
21    arena::{Handle, HandleSet},
22    back::{
23        self, get_entry_points,
24        msl::{mesh_shader::NestedFunctionInfo, BackendResult, EntryPointArgument},
25        Baked,
26    },
27    common,
28    proc::{
29        self, concrete_int_scalars,
30        index::{self, BoundsCheck},
31        ExternalTextureNameKey, NameKey, TypeResolution,
32    },
33    valid, FastHashMap, FastHashSet,
34};
35
36// This is a hack: we need to pass a pointer to an atomic,
37// but generally the backend isn't putting "&" in front of every pointer.
38// Some more general handling of pointers is needed to be implemented here.
39const ATOMIC_REFERENCE: &str = "&";
40
41pub(crate) const ATOMIC_COMP_EXCH_FUNCTION: &str = "naga_atomic_compare_exchange_weak_explicit";
42pub(crate) const MODF_FUNCTION: &str = "naga_modf";
43pub(crate) const FREXP_FUNCTION: &str = "naga_frexp";
44pub(crate) const ABS_FUNCTION: &str = "naga_abs";
45pub(crate) const DIV_FUNCTION: &str = "naga_div";
46pub(crate) const DOT_FUNCTION_PREFIX: &str = "naga_dot";
47pub(crate) const MOD_FUNCTION: &str = "naga_mod";
48pub(crate) const NEG_FUNCTION: &str = "naga_neg";
49pub(crate) const F2I32_FUNCTION: &str = "naga_f2i32";
50pub(crate) const F2U32_FUNCTION: &str = "naga_f2u32";
51pub(crate) const F2I64_FUNCTION: &str = "naga_f2i64";
52pub(crate) const F2U64_FUNCTION: &str = "naga_f2u64";
53pub(crate) const IMAGE_LOAD_EXTERNAL_FUNCTION: &str = "nagaTextureLoadExternal";
54pub(crate) const IMAGE_SIZE_EXTERNAL_FUNCTION: &str = "nagaTextureDimensionsExternal";
55pub(crate) const IMAGE_SAMPLE_BASE_CLAMP_TO_EDGE_FUNCTION: &str =
56    "nagaTextureSampleBaseClampToEdge";
57/// For some reason, Metal does not let you have `metal::texture<..>*` as a buffer argument.
58/// However, if you put that texture inside a struct, everything is totally fine. This
59/// baffles me to no end.
60///
61/// As such, we wrap all argument buffers in a struct that has a single generic `<T>` field.
62/// This allows `NagaArgumentBufferWrapper<metal::texture<..>>*` to work. The astute among
63/// you have noticed that this should be exactly the same to the compiler, and you're correct.
64pub(crate) const ARGUMENT_BUFFER_WRAPPER_STRUCT: &str = "NagaArgumentBufferWrapper";
65/// Name of the struct that is declared to wrap the 3 textures and parameters
66/// buffer that [`crate::ImageClass::External`] variables are lowered to,
67/// allowing them to be conveniently passed to user-defined or wrapper
68/// functions. The struct is declared in [`Writer::write_type_defs`].
69pub(crate) const EXTERNAL_TEXTURE_WRAPPER_STRUCT: &str = "NagaExternalTextureWrapper";
70pub(crate) const COOPERATIVE_LOAD_FUNCTION: &str = "NagaCooperativeLoad";
71pub(crate) const COOPERATIVE_MULTIPLY_ADD_FUNCTION: &str = "NagaCooperativeMultiplyAdd";
72
73/// Write the Metal name for a Naga numeric type: scalar, vector, or matrix.
74///
75/// The `sizes` slice determines whether this function writes a
76/// scalar, vector, or matrix type:
77///
78/// - An empty slice produces a scalar type.
79/// - A one-element slice produces a vector type.
80/// - A two element slice `[ROWS COLUMNS]` produces a matrix of the given size.
81fn put_numeric_type(
82    out: &mut impl Write,
83    scalar: crate::Scalar,
84    sizes: &[crate::VectorSize],
85) -> Result<(), FmtError> {
86    match (scalar, sizes) {
87        (scalar, &[]) => {
88            write!(out, "{}", scalar.to_msl_name())
89        }
90        (scalar, &[rows]) => {
91            write!(
92                out,
93                "{}::{}{}",
94                NAMESPACE,
95                scalar.to_msl_name(),
96                common::vector_size_str(rows)
97            )
98        }
99        (scalar, &[rows, columns]) => {
100            write!(
101                out,
102                "{}::{}{}x{}",
103                NAMESPACE,
104                scalar.to_msl_name(),
105                common::vector_size_str(columns),
106                common::vector_size_str(rows)
107            )
108        }
109        (_, _) => Ok(()), // not meaningful
110    }
111}
112
113const fn scalar_is_int(scalar: crate::Scalar) -> bool {
114    use crate::ScalarKind::*;
115    match scalar.kind {
116        Sint | Uint | AbstractInt | Bool => true,
117        Float | AbstractFloat => false,
118    }
119}
120
121/// Prefix for cached clamped level-of-detail values for `ImageLoad` expressions.
122const CLAMPED_LOD_LOAD_PREFIX: &str = "clamped_lod_e";
123
124/// Prefix for reinterpreted expressions using `as_type<T>(...)`.
125const REINTERPRET_PREFIX: &str = "reinterpreted_";
126
127/// Wrapper for identifier names for clamped level-of-detail values
128///
129/// Values of this type implement [`core::fmt::Display`], formatting as
130/// the name of the variable used to hold the cached clamped
131/// level-of-detail value for an `ImageLoad` expression.
132struct ClampedLod(Handle<crate::Expression>);
133
134impl Display for ClampedLod {
135    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
136        self.0.write_prefixed(f, CLAMPED_LOD_LOAD_PREFIX)
137    }
138}
139
140/// Wrapper for generating `struct _mslBufferSizes` member names for
141/// runtime-sized array lengths.
142///
143/// On Metal, `wgpu_hal` passes the element counts for all runtime-sized arrays
144/// as an argument to the entry point. This argument's type in the MSL is
145/// `struct _mslBufferSizes`, a Naga-synthesized struct with a `uint` member for
146/// each global variable containing a runtime-sized array.
147///
148/// If `global` is a [`Handle`] for a [`GlobalVariable`] that contains a
149/// runtime-sized array, then the value `ArraySize(global)` implements
150/// [`core::fmt::Display`], formatting as the name of the struct member carrying
151/// the number of elements in that runtime-sized array.
152///
153/// [`GlobalVariable`]: crate::GlobalVariable
154struct ArraySizeMember(Handle<crate::GlobalVariable>);
155
156impl Display for ArraySizeMember {
157    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
158        self.0.write_prefixed(f, "size")
159    }
160}
161
162/// Wrapper for reinterpreted variables using `as_type<target_type>(orig)`.
163///
164/// Implements [`core::fmt::Display`], formatting as a name derived from
165/// `target_type` and the variable name of `orig`.
166#[derive(Clone, Copy)]
167struct Reinterpreted<'a> {
168    target_type: &'a str,
169    orig: Handle<crate::Expression>,
170}
171
172impl<'a> Reinterpreted<'a> {
173    const fn new(target_type: &'a str, orig: Handle<crate::Expression>) -> Self {
174        Self { target_type, orig }
175    }
176}
177
178impl Display for Reinterpreted<'_> {
179    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
180        f.write_str(REINTERPRET_PREFIX)?;
181        f.write_str(self.target_type)?;
182        self.orig.write_prefixed(f, "_e")
183    }
184}
185
186pub(super) struct TypeContext<'a> {
187    pub handle: Handle<crate::Type>,
188    pub gctx: proc::GlobalCtx<'a>,
189    pub names: &'a FastHashMap<NameKey, String>,
190    pub access: crate::StorageAccess,
191    pub first_time: bool,
192}
193
194impl TypeContext<'_> {
195    fn scalar(&self) -> Option<crate::Scalar> {
196        let ty = &self.gctx.types[self.handle];
197        ty.inner.scalar()
198    }
199
200    fn vector_size(&self) -> Option<crate::VectorSize> {
201        let ty = &self.gctx.types[self.handle];
202        match ty.inner {
203            crate::TypeInner::Vector { size, .. } => Some(size),
204            _ => None,
205        }
206    }
207
208    fn unwrap_array(self) -> Self {
209        match self.gctx.types[self.handle].inner {
210            crate::TypeInner::Array { base, .. } => Self {
211                handle: base,
212                ..self
213            },
214            _ => self,
215        }
216    }
217}
218
219impl Display for TypeContext<'_> {
220    fn fmt(&self, out: &mut Formatter<'_>) -> Result<(), FmtError> {
221        let ty = &self.gctx.types[self.handle];
222        if ty.needs_alias() && !self.first_time {
223            let name = &self.names[&NameKey::Type(self.handle)];
224            return write!(out, "{name}");
225        }
226
227        match ty.inner {
228            crate::TypeInner::Scalar(scalar) => put_numeric_type(out, scalar, &[]),
229            crate::TypeInner::Atomic(scalar) => {
230                write!(out, "{}::atomic_{}", NAMESPACE, scalar.to_msl_name())
231            }
232            crate::TypeInner::Vector { size, scalar } => put_numeric_type(out, scalar, &[size]),
233            crate::TypeInner::Matrix {
234                columns,
235                rows,
236                scalar,
237            } => put_numeric_type(out, scalar, &[rows, columns]),
238            // Requires Metal-2.3
239            crate::TypeInner::CooperativeMatrix {
240                columns,
241                rows,
242                scalar,
243                role: _,
244            } => {
245                write!(
246                    out,
247                    "{NAMESPACE}::simdgroup_{}{}x{}",
248                    scalar.to_msl_name(),
249                    columns as u32,
250                    rows as u32,
251                )
252            }
253            crate::TypeInner::Pointer { base, space } => {
254                let sub = Self {
255                    handle: base,
256                    first_time: false,
257                    ..*self
258                };
259                let space_name = match space.to_msl_name() {
260                    Some(name) => name,
261                    None => return Ok(()),
262                };
263                write!(out, "{space_name} {sub}&")
264            }
265            crate::TypeInner::ValuePointer {
266                size,
267                scalar,
268                space,
269            } => {
270                match space.to_msl_name() {
271                    Some(name) => write!(out, "{name} ")?,
272                    None => return Ok(()),
273                };
274                match size {
275                    Some(rows) => put_numeric_type(out, scalar, &[rows])?,
276                    None => put_numeric_type(out, scalar, &[])?,
277                };
278
279                write!(out, "&")
280            }
281            crate::TypeInner::Array { base, .. } => {
282                let sub = Self {
283                    handle: base,
284                    first_time: false,
285                    ..*self
286                };
287                // Array lengths go at the end of the type definition,
288                // so just print the element type here.
289                write!(out, "{sub}")
290            }
291            crate::TypeInner::Struct { .. } => unreachable!(),
292            crate::TypeInner::Image {
293                dim,
294                arrayed,
295                class,
296            } => {
297                let dim_str = match dim {
298                    crate::ImageDimension::D1 => "1d",
299                    crate::ImageDimension::D2 => "2d",
300                    crate::ImageDimension::D3 => "3d",
301                    crate::ImageDimension::Cube => "cube",
302                };
303                let (texture_str, msaa_str, scalar, access) = match class {
304                    crate::ImageClass::Sampled { kind, multi } => {
305                        let (msaa_str, access) = if multi {
306                            ("_ms", "read")
307                        } else {
308                            ("", "sample")
309                        };
310                        let scalar = crate::Scalar { kind, width: 4 };
311                        ("texture", msaa_str, scalar, access)
312                    }
313                    crate::ImageClass::Depth { multi } => {
314                        let (msaa_str, access) = if multi {
315                            ("_ms", "read")
316                        } else {
317                            ("", "sample")
318                        };
319                        let scalar = crate::Scalar {
320                            kind: crate::ScalarKind::Float,
321                            width: 4,
322                        };
323                        ("depth", msaa_str, scalar, access)
324                    }
325                    crate::ImageClass::Storage { format, .. } => {
326                        let access = if self
327                            .access
328                            .contains(crate::StorageAccess::LOAD | crate::StorageAccess::STORE)
329                        {
330                            "read_write"
331                        } else if self.access.contains(crate::StorageAccess::STORE) {
332                            "write"
333                        } else if self.access.contains(crate::StorageAccess::LOAD) {
334                            "read"
335                        } else {
336                            log::warn!(
337                                "Storage access for {:?} (name '{}'): {:?}",
338                                self.handle,
339                                ty.name.as_deref().unwrap_or_default(),
340                                self.access
341                            );
342                            unreachable!("module is not valid");
343                        };
344                        ("texture", "", format.into(), access)
345                    }
346                    crate::ImageClass::External => {
347                        return write!(out, "{EXTERNAL_TEXTURE_WRAPPER_STRUCT}");
348                    }
349                };
350                let base_name = scalar.to_msl_name();
351                let array_str = if arrayed { "_array" } else { "" };
352                write!(
353                    out,
354                    "{NAMESPACE}::{texture_str}{dim_str}{msaa_str}{array_str}<{base_name}, {NAMESPACE}::access::{access}>",
355                )
356            }
357            crate::TypeInner::Sampler { comparison: _ } => {
358                write!(out, "{NAMESPACE}::sampler")
359            }
360            crate::TypeInner::AccelerationStructure { vertex_return } => {
361                if vertex_return {
362                    unimplemented!("metal does not support vertex ray hit return")
363                }
364                write!(out, "{RT_NAMESPACE}::instance_acceleration_structure")
365            }
366            crate::TypeInner::RayQuery { vertex_return } => {
367                if vertex_return {
368                    unimplemented!("metal does not support vertex ray hit return")
369                }
370                write!(out, "{}", super::ray::metal_intersector_ty())
371            }
372            crate::TypeInner::HitObject => {
373                unimplemented!("metal does not support ray tracing invocation reordering")
374            }
375            crate::TypeInner::BindingArray { base, .. } => {
376                let base_inner = &self.gctx.types[base].inner;
377                let base_tyname = Self {
378                    handle: base,
379                    first_time: false,
380                    ..*self
381                };
382                match *base_inner {
383                    crate::TypeInner::Struct { .. } => {
384                        // Buffers in a binding array are pointers declared as `device T*`, so members use `->`.
385                        // Textures and samplers stay as plain values inside the wrapper.
386                        write!(
387                            out,
388                            "device {ARGUMENT_BUFFER_WRAPPER_STRUCT}<device {base_tyname}*>*"
389                        )
390                    }
391                    _ => {
392                        write!(
393                            out,
394                            "constant {ARGUMENT_BUFFER_WRAPPER_STRUCT}<{base_tyname}>*"
395                        )
396                    }
397                }
398            }
399        }
400    }
401}
402
403pub(super) struct TypedGlobalVariable<'a> {
404    pub module: &'a crate::Module,
405    pub names: &'a FastHashMap<NameKey, String>,
406    pub handle: Handle<crate::GlobalVariable>,
407    pub usage: valid::GlobalUse,
408    pub reference: bool,
409}
410
411struct TypedGlobalVariableParts {
412    ty_name: String,
413    var_name: String,
414}
415
416impl TypedGlobalVariable<'_> {
417    fn to_parts(&self) -> Result<TypedGlobalVariableParts, Error> {
418        let var = &self.module.global_variables[self.handle];
419        let name = &self.names[&NameKey::GlobalVariable(self.handle)];
420
421        let storage_access = match var.space {
422            crate::AddressSpace::Storage { access } => access,
423            _ => match self.module.types[var.ty].inner {
424                crate::TypeInner::Image {
425                    class: crate::ImageClass::Storage { access, .. },
426                    ..
427                } => access,
428                crate::TypeInner::BindingArray { base, .. } => {
429                    match self.module.types[base].inner {
430                        crate::TypeInner::Image {
431                            class: crate::ImageClass::Storage { access, .. },
432                            ..
433                        } => access,
434                        _ => crate::StorageAccess::default(),
435                    }
436                }
437                _ => crate::StorageAccess::default(),
438            },
439        };
440        let ty_name = TypeContext {
441            handle: var.ty,
442            gctx: self.module.to_ctx(),
443            names: self.names,
444            access: storage_access,
445            first_time: false,
446        };
447
448        let (coherent, space, access, reference) = if matches!(
449            self.module.types[var.ty].inner,
450            crate::TypeInner::BindingArray { .. }
451        ) {
452            ("", "", "", "")
453        } else {
454            let access = if var.space.needs_access_qualifier()
455                && !self.usage.intersects(valid::GlobalUse::WRITE)
456            {
457                "const"
458            } else {
459                ""
460            };
461            match (var.space.to_msl_name(), var.space) {
462                (Some(space), crate::AddressSpace::WorkGroup) => {
463                    ("", space, access, if self.reference { "&" } else { "" })
464                }
465                (Some(space), _) if self.reference => {
466                    let coherent = if var
467                        .memory_decorations
468                        .contains(crate::MemoryDecorations::COHERENT)
469                    {
470                        "coherent "
471                    } else {
472                        ""
473                    };
474                    (coherent, space, access, "&")
475                }
476                _ => ("", "", "", ""),
477            }
478        };
479
480        let ty = format!(
481            "{coherent}{space}{}{ty_name}{}{access}{reference}",
482            if space.is_empty() { "" } else { " " },
483            if access.is_empty() { "" } else { " " },
484        );
485
486        Ok(TypedGlobalVariableParts {
487            ty_name: ty,
488            var_name: name.clone(),
489        })
490    }
491    pub(super) fn try_fmt<W: Write>(&self, out: &mut W) -> BackendResult {
492        let parts = self.to_parts()?;
493
494        Ok(write!(out, "{} {}", parts.ty_name, parts.var_name)?)
495    }
496}
497
498#[derive(Eq, PartialEq, Hash)]
499pub(super) enum WrappedFunction {
500    UnaryOp {
501        op: crate::UnaryOperator,
502        ty: (Option<crate::VectorSize>, crate::Scalar),
503    },
504    BinaryOp {
505        op: crate::BinaryOperator,
506        left_ty: (Option<crate::VectorSize>, crate::Scalar),
507        right_ty: (Option<crate::VectorSize>, crate::Scalar),
508    },
509    Math {
510        fun: crate::MathFunction,
511        arg_ty: (Option<crate::VectorSize>, crate::Scalar),
512    },
513    Cast {
514        src_scalar: crate::Scalar,
515        vector_size: Option<crate::VectorSize>,
516        dst_scalar: crate::Scalar,
517    },
518    ImageLoad {
519        class: crate::ImageClass,
520    },
521    ImageSample {
522        class: crate::ImageClass,
523        clamp_to_edge: bool,
524    },
525    ImageQuerySize {
526        class: crate::ImageClass,
527    },
528    CooperativeLoad {
529        space_name: &'static str,
530        columns: crate::CooperativeSize,
531        rows: crate::CooperativeSize,
532        scalar: crate::Scalar,
533    },
534    CooperativeMultiplyAdd {
535        space_name: &'static str,
536        columns: crate::CooperativeSize,
537        rows: crate::CooperativeSize,
538        intermediate: crate::CooperativeSize,
539        ab_scalar: crate::Scalar,
540        c_scalar: crate::Scalar,
541    },
542    RayQueryGetIntersection {
543        committed: bool,
544    },
545}
546
547#[expect(missing_debug_implementations, reason = "would be way too verbose?")]
548pub struct Writer<W> {
549    pub(super) out: W,
550    pub(super) names: FastHashMap<NameKey, String>,
551    /// Expressions we have given a value to, mapped to the names of the
552    /// temporaries holding it.
553    ///
554    /// Entries are added to this map when the expression's definition is
555    /// written, and are removed only when we move on to the next function.
556    /// Removing entries immediately when the enclosing block ends is not
557    /// necessary, because they go out of scope at that point and will not be
558    /// referenced in a valid module.
559    pub(super) named_expressions: crate::NamedExpressions,
560    /// Expressions whose temporaries have been "hoisted" out of a loop body,
561    /// mapped to the names of those temporaries.
562    ///
563    /// Entries are added to this map when the relocated definition is written
564    /// (before the loop body). At the point the original definition appeared
565    /// (within the loop body), its entry is moved from
566    /// [`Self::hoisted_loop_expressions`] to [`Self::named_expressions`],
567    /// and an assignment (`name = value`) rather than declaration
568    /// (`T name = value`) is written to the output.
569    ///
570    /// [`Self::put_expression`] consults both maps, so that the `continuing`
571    /// block (written before the loop body) can read the value the body
572    /// stored in the temporary on the previous pass.
573    ///
574    /// See [the `hoist` module](super::hoist) for why this is needed.
575    pub(super) hoisted_loop_expressions: FastHashMap<Handle<crate::Expression>, String>,
576    /// Set of expressions that need to be baked to avoid unnecessary repetition in output
577    need_bake_expressions: back::NeedBakeExpressions,
578    pub(super) namer: proc::Namer,
579    pub(super) wrapped_functions: FastHashSet<WrappedFunction>,
580    emit_int_div_checks: bool,
581    /// Set of (struct type, struct field index) denoting which fields require
582    /// padding inserted **before** them (i.e. between fields at index - 1 and index)
583    struct_member_pads: FastHashSet<(Handle<crate::Type>, u32)>,
584    needs_object_memory_barriers: bool,
585}
586
587impl crate::Scalar {
588    pub(super) fn to_msl_name(self) -> &'static str {
589        use crate::ScalarKind as Sk;
590        match self {
591            Self {
592                kind: Sk::Float,
593                width: 4,
594            } => "float",
595            Self {
596                kind: Sk::Float,
597                width: 2,
598            } => "half",
599            Self {
600                kind: Sk::Sint,
601                width: 2,
602            } => "short",
603            Self {
604                kind: Sk::Uint,
605                width: 2,
606            } => "ushort",
607            Self {
608                kind: Sk::Sint,
609                width: 4,
610            } => "int",
611            Self {
612                kind: Sk::Uint,
613                width: 4,
614            } => "uint",
615            Self {
616                kind: Sk::Sint,
617                width: 8,
618            } => "long",
619            Self {
620                kind: Sk::Uint,
621                width: 8,
622            } => "ulong",
623            Self {
624                kind: Sk::Bool,
625                width: _,
626            } => "bool",
627            Self {
628                kind: Sk::AbstractInt | Sk::AbstractFloat,
629                width: _,
630            } => unreachable!("Found Abstract scalar kind"),
631            _ => unreachable!("Unsupported scalar kind: {:?}", self),
632        }
633    }
634}
635
636/// Returns `true` if `binding` is the depth that a fragment shader writes.
637const fn is_frag_depth(binding: Option<&crate::Binding>) -> bool {
638    matches!(
639        binding,
640        Some(crate::Binding::BuiltIn(crate::BuiltIn::FragDepth))
641    )
642}
643
644/// Returns `true` if the depth that a fragment shader writes is the only thing that
645/// `entry_point` returns.
646fn only_writes_frag_depth(module: &crate::ir::Module, entry_point: &crate::ir::EntryPoint) -> bool {
647    entry_point
648        .function
649        .result
650        .as_ref()
651        .is_some_and(|result| match module.types[result.ty].inner {
652            crate::TypeInner::Struct { ref members, .. } if result.binding.is_none() => members
653                .iter()
654                .all(|member| is_frag_depth(member.binding.as_ref())),
655            _ => is_frag_depth(result.binding.as_ref()),
656        })
657}
658
659/// Returns `true` if the depth that `entry_point` writes must be discarded.
660///
661/// MSL has no way to specify that depth is unchanged, and it does not allow functions that
662/// force early fragment tests to output depth. Discarding the depth has the same effect
663/// as either of those, and keeps early depth tests enabled.
664fn discards_frag_depth(entry_point: &crate::ir::EntryPoint) -> bool {
665    entry_point.stage == crate::ShaderStage::Fragment
666        && matches!(
667            entry_point.early_depth_test,
668            Some(
669                crate::EarlyDepthTest::Force
670                    | crate::EarlyDepthTest::Allow {
671                        conservative: crate::ConservativeDepth::Unchanged
672                    }
673            )
674        )
675}
676
677/// Returns `true` if `entry_point` has nothing to return once its depth is discarded.
678fn only_writes_discarded_depth(
679    module: &crate::ir::Module,
680    entry_point: &crate::ir::EntryPoint,
681) -> bool {
682    discards_frag_depth(entry_point) && only_writes_frag_depth(module, entry_point)
683}
684
685const fn separate(need_separator: bool) -> &'static str {
686    if need_separator {
687        ","
688    } else {
689        ""
690    }
691}
692
693fn should_pack_struct_member(
694    members: &[crate::StructMember],
695    span: u32,
696    index: usize,
697    module: &crate::Module,
698) -> Option<crate::Scalar> {
699    let member = &members[index];
700
701    let ty_inner = &module.types[member.ty].inner;
702    let last_offset = member.offset + ty_inner.size(module.to_ctx());
703    let next_offset = match members.get(index + 1) {
704        Some(next) => next.offset,
705        None => span,
706    };
707    let is_tight = next_offset == last_offset;
708
709    match *ty_inner {
710        crate::TypeInner::Vector {
711            size: crate::VectorSize::Tri,
712            scalar: scalar @ crate::Scalar { width: 4 | 2, .. },
713        } if is_tight => Some(scalar),
714        _ => None,
715    }
716}
717
718impl crate::AddressSpace {
719    /// Returns true if global variables in this address space are
720    /// passed in function arguments. These arguments need to be
721    /// passed through any functions called from the entry point.
722    const fn needs_pass_through(&self) -> bool {
723        match *self {
724            Self::Uniform
725            | Self::Storage { .. }
726            | Self::Private
727            | Self::WorkGroup
728            | Self::Immediate
729            | Self::Handle
730            | Self::TaskPayload => true,
731            Self::Function => false,
732            Self::RayPayload | Self::IncomingRayPayload => unreachable!(),
733        }
734    }
735
736    /// Returns true if the address space may need a "const" qualifier.
737    const fn needs_access_qualifier(&self) -> bool {
738        match *self {
739            //Note: we are ignoring the storage access here, and instead
740            // rely on the actual use of a global by functions. This means we
741            // may end up with "const" even if the binding is read-write,
742            // and that should be OK.
743            Self::Storage { .. } => true,
744            Self::TaskPayload => true,
745            Self::RayPayload | Self::IncomingRayPayload => unimplemented!(),
746            // These should always be read-write.
747            Self::Private | Self::WorkGroup => false,
748            // These translate to `constant` address space, no need for qualifiers.
749            Self::Uniform | Self::Immediate => false,
750            // Not applicable.
751            Self::Handle | Self::Function => false,
752        }
753    }
754
755    const fn to_msl_name(self) -> Option<&'static str> {
756        match self {
757            Self::Handle => None,
758            Self::Uniform | Self::Immediate => Some("constant"),
759            Self::Storage { .. } => Some("device"),
760            // note for `RayPayload`, this probably needs to be emulated as a
761            // private variable, as metal has essentially an inout input
762            // for where it is passed.
763            Self::Private | Self::Function | Self::RayPayload => Some("thread"),
764            Self::WorkGroup => Some("threadgroup"),
765            Self::TaskPayload => Some("object_data"),
766            Self::IncomingRayPayload => Some("ray_data"),
767        }
768    }
769}
770
771impl crate::Type {
772    // Returns `true` if we need to emit an alias for this type.
773    const fn needs_alias(&self) -> bool {
774        use crate::TypeInner as Ti;
775
776        match self.inner {
777            // value types are concise enough, we only alias them if they are named
778            Ti::Scalar(_)
779            | Ti::Vector { .. }
780            | Ti::Matrix { .. }
781            | Ti::CooperativeMatrix { .. }
782            | Ti::Atomic(_)
783            | Ti::Pointer { .. }
784            | Ti::ValuePointer { .. } => self.name.is_some(),
785            // composite types are better to be aliased, regardless of the name
786            Ti::Struct { .. } | Ti::Array { .. } => true,
787            // handle types may be different, depending on the global var access, so we always inline them
788            Ti::Image { .. }
789            | Ti::Sampler { .. }
790            | Ti::AccelerationStructure { .. }
791            | Ti::RayQuery { .. }
792            | Ti::HitObject
793            | Ti::BindingArray { .. } => false,
794        }
795    }
796}
797
798#[derive(Clone, Copy)]
799pub(super) enum FunctionOrigin {
800    Handle(Handle<crate::Function>),
801    EntryPoint(proc::EntryPointIndex),
802}
803
804pub(super) trait NameKeyExt {
805    fn local(origin: FunctionOrigin, local_handle: Handle<crate::LocalVariable>) -> NameKey {
806        match origin {
807            FunctionOrigin::Handle(handle) => NameKey::FunctionLocal(handle, local_handle),
808            FunctionOrigin::EntryPoint(idx) => NameKey::EntryPointLocal(idx, local_handle),
809        }
810    }
811
812    /// Return the name key for a local variable used by ReadZeroSkipWrite bounds-check
813    /// policy when it needs to produce a pointer-typed result for an OOB access. These
814    /// are unique per accessed type, so the second argument is a type handle. See docs
815    /// for [`crate::back::msl`].
816    fn oob_local_for_type(origin: FunctionOrigin, ty: Handle<crate::Type>) -> NameKey {
817        match origin {
818            FunctionOrigin::Handle(handle) => NameKey::FunctionOobLocal(handle, ty),
819            FunctionOrigin::EntryPoint(idx) => NameKey::EntryPointOobLocal(idx, ty),
820        }
821    }
822}
823
824impl NameKeyExt for NameKey {}
825
826/// A level of detail argument.
827///
828/// When [`BoundsCheckPolicy::Restrict`] applies to an [`ImageLoad`] access, we
829/// save the clamped level of detail in a temporary variable whose name is based
830/// on the handle of the `ImageLoad` expression. But for other policies, we just
831/// use the expression directly.
832///
833/// [`BoundsCheckPolicy::Restrict`]: index::BoundsCheckPolicy::Restrict
834/// [`ImageLoad`]: crate::Expression::ImageLoad
835#[derive(Clone, Copy)]
836enum LevelOfDetail {
837    Direct(Handle<crate::Expression>),
838    Restricted(Handle<crate::Expression>),
839}
840
841/// Values needed to select a particular texel for [`ImageLoad`] and [`ImageStore`].
842///
843/// When this is used in code paths unconcerned with the `Restrict` bounds check
844/// policy, the `LevelOfDetail` enum introduces an unneeded match, since `level`
845/// will always be either `None` or `Some(Direct(_))`. But this turns out not to
846/// be too awkward. If that changes, we can revisit.
847///
848/// [`ImageLoad`]: crate::Expression::ImageLoad
849/// [`ImageStore`]: crate::Statement::ImageStore
850struct TexelAddress {
851    coordinate: Handle<crate::Expression>,
852    array_index: Option<Handle<crate::Expression>>,
853    sample: Option<Handle<crate::Expression>>,
854    level: Option<LevelOfDetail>,
855}
856
857pub(super) struct ExpressionContext<'a> {
858    pub(super) function: &'a crate::Function,
859    pub(super) origin: FunctionOrigin,
860    pub(super) info: &'a valid::FunctionInfo,
861    pub(super) module: &'a crate::Module,
862    pub(super) mod_info: &'a valid::ModuleInfo,
863    pub(super) pipeline_options: &'a PipelineOptions,
864    pub(super) lang_version: (u8, u8),
865    pub(super) policies: index::BoundsCheckPolicies,
866
867    /// The set of expressions used as indices in `ReadZeroSkipWrite`-policy
868    /// accesses. These may need to be cached in temporary variables. See
869    /// `index::find_checked_indexes` for details.
870    pub(super) guarded_indices: HandleSet<crate::Expression>,
871    /// See [`Writer::gen_force_bounded_loop_statements`] for details.
872    pub(super) force_loop_bounding: bool,
873    /// Whether to emit safety checks for integer division/modulo.
874    emit_int_div_checks: bool,
875    pub(super) ray_query_initialization_tracking: bool,
876}
877
878impl<'a> ExpressionContext<'a> {
879    fn resolve_type(&self, handle: Handle<crate::Expression>) -> &'a crate::TypeInner {
880        self.info[handle].ty.inner_with(&self.module.types)
881    }
882
883    /// Walks from an inner pointer toward a storage binding array global and
884    /// returns the element index at that global for MSL runtime buffer sizing.
885    fn binding_array_index_from_chain(
886        &self,
887        mut expr: Handle<crate::Expression>,
888        global: Handle<crate::GlobalVariable>,
889    ) -> Option<index::GuardedIndex> {
890        let expressions = &self.function.expressions;
891        loop {
892            match expressions[expr] {
893                crate::Expression::Load { pointer } => expr = pointer,
894                crate::Expression::Access { base, index } => {
895                    if matches!(
896                        expressions[base],
897                        crate::Expression::GlobalVariable(g) if g == global
898                    ) {
899                        return Some(index::GuardedIndex::Expression(index));
900                    }
901                    expr = base;
902                }
903                crate::Expression::AccessIndex { base, index } => {
904                    if matches!(
905                        expressions[base],
906                        crate::Expression::GlobalVariable(g) if g == global
907                    ) {
908                        return Some(index::GuardedIndex::Known(index));
909                    }
910                    expr = base;
911                }
912                crate::Expression::GlobalVariable(_) => return None,
913                _ => return None,
914            }
915        }
916    }
917
918    /// Whether `expr` is directly indexing a global in the outer `Access`/`AccessIndex` shape.
919    fn is_global_access_chain(&self, expr: Handle<crate::Expression>) -> bool {
920        let expressions = &self.function.expressions;
921        match expressions[expr] {
922            crate::Expression::Access { base, .. } => match expressions[base] {
923                crate::Expression::GlobalVariable(_) => true,
924                crate::Expression::Access { .. } => self.is_global_access_chain(base),
925                _ => false,
926            },
927            crate::Expression::AccessIndex { base, .. } => {
928                matches!(expressions[base], crate::Expression::GlobalVariable(_))
929            }
930            _ => false,
931        }
932    }
933
934    fn struct_member_needs_arrow(
935        &self,
936        base: Handle<crate::Expression>,
937        originating_global_ty: impl FnOnce(&crate::TypeInner) -> bool,
938    ) -> bool {
939        let originating_matches = match self.function.originating_global(base) {
940            Some(gv) => {
941                originating_global_ty(&self.module.types[self.module.global_variables[gv].ty].inner)
942            }
943            None => false,
944        };
945        originating_matches && self.is_global_access_chain(base)
946    }
947
948    /// Return true if calls to `image`'s `read` and `write` methods should supply a level of detail.
949    ///
950    /// Only mipmapped images need to specify a level of detail. Since 1D
951    /// textures cannot have mipmaps, MSL requires that the level argument to
952    /// texture1d queries and accesses must be a constexpr 0. It's easiest
953    /// just to omit the level entirely for 1D textures.
954    fn image_needs_lod(&self, image: Handle<crate::Expression>) -> bool {
955        let image_ty = self.resolve_type(image);
956        if let crate::TypeInner::Image { dim, class, .. } = *image_ty {
957            class.is_mipmapped() && dim != crate::ImageDimension::D1
958        } else {
959            false
960        }
961    }
962
963    fn choose_bounds_check_policy(
964        &self,
965        pointer: Handle<crate::Expression>,
966    ) -> index::BoundsCheckPolicy {
967        self.policies
968            .choose_policy(pointer, &self.module.types, self.info)
969    }
970
971    /// See docs for [`proc::index::access_needs_check`].
972    fn access_needs_check(
973        &self,
974        base: Handle<crate::Expression>,
975        index: index::GuardedIndex,
976    ) -> Option<index::IndexableLength> {
977        index::access_needs_check(
978            base,
979            index,
980            self.module,
981            &self.function.expressions,
982            self.info,
983        )
984    }
985
986    /// See docs for [`proc::index::bounds_check_iter`].
987    fn bounds_check_iter(
988        &self,
989        chain: Handle<crate::Expression>,
990    ) -> impl Iterator<Item = BoundsCheck> + '_ {
991        index::bounds_check_iter(chain, self.module, self.function, self.info)
992    }
993
994    /// See docs for [`proc::index::oob_local_types`].
995    fn oob_local_types(&self) -> FastHashSet<Handle<crate::Type>> {
996        index::oob_local_types(self.module, self.function, self.info, self.policies)
997    }
998
999    fn get_packed_vec_kind(&self, expr_handle: Handle<crate::Expression>) -> Option<crate::Scalar> {
1000        match self.function.expressions[expr_handle] {
1001            crate::Expression::AccessIndex { base, index } => {
1002                let ty = match *self.resolve_type(base) {
1003                    crate::TypeInner::Pointer { base, .. } => &self.module.types[base].inner,
1004                    ref ty => ty,
1005                };
1006                match *ty {
1007                    crate::TypeInner::Struct {
1008                        ref members, span, ..
1009                    } => should_pack_struct_member(members, span, index as usize, self.module),
1010                    _ => None,
1011                }
1012            }
1013            _ => None,
1014        }
1015    }
1016}
1017
1018pub(super) struct StatementContext<'a> {
1019    pub(super) expression: ExpressionContext<'a>,
1020    pub(super) result_struct: Option<&'a str>,
1021}
1022
1023impl<W: Write> Writer<W> {
1024    /// Creates a new `Writer` instance.
1025    pub fn new(out: W) -> Self {
1026        Writer {
1027            out,
1028            names: FastHashMap::default(),
1029            named_expressions: Default::default(),
1030            hoisted_loop_expressions: FastHashMap::default(),
1031            need_bake_expressions: Default::default(),
1032            namer: proc::Namer::default(),
1033            wrapped_functions: FastHashSet::default(),
1034            emit_int_div_checks: true,
1035            struct_member_pads: FastHashSet::default(),
1036            needs_object_memory_barriers: false,
1037        }
1038    }
1039
1040    /// Finishes writing and returns the output.
1041    // See https://github.com/rust-lang/rust-clippy/issues/4979.
1042    pub fn finish(self) -> W {
1043        self.out
1044    }
1045
1046    /// Generates statements to be inserted immediately before and at the very
1047    /// start of the body of each loop, to defeat MSL infinite loop reasoning.
1048    /// The 0th item of the returned tuple should be inserted immediately prior
1049    /// to the loop and the 1st item should be inserted at the very start of
1050    /// the loop body.
1051    ///
1052    /// # What is this trying to solve?
1053    ///
1054    /// In Metal Shading Language, an infinite loop has undefined behavior.
1055    /// (This rule is inherited from C++14.) This means that, if the MSL
1056    /// compiler determines that a given loop will never exit, it may assume
1057    /// that it is never reached. It may thus assume that any conditions
1058    /// sufficient to cause the loop to be reached must be false. Like many
1059    /// optimizing compilers, MSL uses this kind of analysis to establish limits
1060    /// on the range of values variables involved in those conditions might
1061    /// hold.
1062    ///
1063    /// For example, suppose the MSL compiler sees the code:
1064    ///
1065    /// ```ignore
1066    /// if (i >= 10) {
1067    ///     while (true) { }
1068    /// }
1069    /// ```
1070    ///
1071    /// It will recognize that the `while` loop will never terminate, conclude
1072    /// that it must be unreachable, and thus infer that, if this code is
1073    /// reached, then `i < 10` at that point.
1074    ///
1075    /// Now suppose that, at some point where `i` has the same value as above,
1076    /// the compiler sees the code:
1077    ///
1078    /// ```ignore
1079    /// if (i < 10) {
1080    ///     a[i] = 1;
1081    /// }
1082    /// ```
1083    ///
1084    /// Because the compiler is confident that `i < 10`, it will make the
1085    /// assignment to `a[i]` unconditional, rewriting this code as, simply:
1086    ///
1087    /// ```ignore
1088    /// a[i] = 1;
1089    /// ```
1090    ///
1091    /// If that `if` condition was injected by Naga to implement a bounds check,
1092    /// the MSL compiler's optimizations could allow out-of-bounds array
1093    /// accesses to occur.
1094    ///
1095    /// Naga cannot feasibly anticipate whether the MSL compiler will determine
1096    /// that a loop is infinite, so an attacker could craft a Naga module
1097    /// containing an infinite loop protected by conditions that cause the Metal
1098    /// compiler to remove bounds checks that Naga injected elsewhere in the
1099    /// function.
1100    ///
1101    /// This rewrite could occur even if the conditional assignment appears
1102    /// *before* the `while` loop, as long as `i < 10` by the time the loop is
1103    /// reached. This would allow the attacker to save the results of
1104    /// unauthorized reads somewhere accessible before entering the infinite
1105    /// loop. But even worse, the MSL compiler has been observed to simply
1106    /// delete the infinite loop entirely, so that even code dominated by the
1107    /// loop becomes reachable. This would make the attack even more flexible,
1108    /// since shaders that would appear to never terminate would actually exit
1109    /// nicely, after having stolen data from elsewhere in the GPU address
1110    /// space.
1111    ///
1112    /// To avoid UB, Naga must persuade the MSL compiler that no loop Naga
1113    /// generates is infinite. One approach would be to add inline assembly to
1114    /// each loop that is annotated as potentially branching out of the loop,
1115    /// but which in fact generates no instructions. Unfortunately, inline
1116    /// assembly is not handled correctly by some Metal device drivers.
1117    ///
1118    /// A previously used approach was to add the following code to the bottom
1119    /// of every loop:
1120    ///
1121    /// ```ignore
1122    /// if (volatile bool unpredictable = false; unpredictable)
1123    ///     break;
1124    /// ```
1125    ///
1126    /// Although the `if` condition will always be false in any real execution,
1127    /// the `volatile` qualifier prevents the compiler from assuming this. Thus,
1128    /// it must assume that the `break` might be reached, and hence that the
1129    /// loop is not unbounded. This prevents the range analysis impact described
1130    /// above. Unfortunately this prevented the compiler from making important,
1131    /// and safe, optimizations such as loop unrolling and was observed to
1132    /// significantly hurt performance.
1133    ///
1134    /// Our current approach declares a counter before every loop and
1135    /// increments it every iteration, breaking after 2^64 iterations:
1136    ///
1137    /// ```ignore
1138    /// uint2 loop_bound = uint2(0);
1139    /// while (true) {
1140    ///   if (metal::all(loop_bound == uint2(4294967295))) { break; }
1141    ///   loop_bound += uint2(loop_bound.y == 4294967295, 1);
1142    /// }
1143    /// ```
1144    ///
1145    /// This convinces the compiler that the loop is finite and therefore may
1146    /// execute, whilst at the same time allowing optimizations such as loop
1147    /// unrolling. Furthermore the 64-bit counter is large enough it seems
1148    /// implausible that it would affect the execution of any shader.
1149    ///
1150    /// This approach is also used by Chromium WebGPU's Dawn shader compiler:
1151    /// <https://dawn.googlesource.com/dawn/+/d9e2d1f718678ebee0728b999830576c410cce0a/src/tint/lang/core/ir/transform/prevent_infinite_loops.cc>
1152    fn gen_force_bounded_loop_statements(
1153        &mut self,
1154        level: back::Level,
1155        context: &StatementContext,
1156    ) -> Option<(String, String)> {
1157        if !context.expression.force_loop_bounding {
1158            return None;
1159        }
1160
1161        let loop_bound_name = self.namer.call("loop_bound");
1162        // Count down from u32::MAX rather than up from 0 to avoid hang on
1163        // certain Intel drivers. See <https://github.com/gfx-rs/wgpu/issues/7319>.
1164        let decl = format!("{level}uint2 {loop_bound_name} = uint2({}u);", u32::MAX);
1165        let level = level.next();
1166        let break_and_inc = format!(
1167            "{level}if ({NAMESPACE}::all({loop_bound_name} == uint2(0u))) {{ break; }}
1168{level}{loop_bound_name} -= uint2({loop_bound_name}.y == 0u, 1u);"
1169        );
1170
1171        Some((decl, break_and_inc))
1172    }
1173
1174    fn put_call_parameters(
1175        &mut self,
1176        parameters: impl Iterator<Item = Handle<crate::Expression>>,
1177        context: &ExpressionContext,
1178    ) -> BackendResult {
1179        self.put_call_parameters_impl(parameters, context, |writer, context, expr| {
1180            writer.put_expression(expr, context, true)
1181        })
1182    }
1183
1184    fn put_call_parameters_impl<C, E>(
1185        &mut self,
1186        parameters: impl Iterator<Item = Handle<crate::Expression>>,
1187        ctx: &C,
1188        put_expression: E,
1189    ) -> BackendResult
1190    where
1191        E: Fn(&mut Self, &C, Handle<crate::Expression>) -> BackendResult,
1192    {
1193        write!(self.out, "(")?;
1194        for (i, handle) in parameters.enumerate() {
1195            if i != 0 {
1196                write!(self.out, ", ")?;
1197            }
1198            put_expression(self, ctx, handle)?;
1199        }
1200        write!(self.out, ")")?;
1201        Ok(())
1202    }
1203
1204    /// Writes the local variables of the given function, as well as any extra
1205    /// out-of-bounds locals that are needed.
1206    ///
1207    /// The names of the OOB locals are also added to `self.names` at the same
1208    /// time.
1209    fn put_locals(&mut self, context: &ExpressionContext) -> BackendResult {
1210        let oob_local_types = context.oob_local_types();
1211        for &ty in oob_local_types.iter() {
1212            let name_key = NameKey::oob_local_for_type(context.origin, ty);
1213            self.names.insert(name_key, self.namer.call("oob"));
1214        }
1215
1216        for (name_key, ty, init) in context
1217            .function
1218            .local_variables
1219            .iter()
1220            .map(|(local_handle, local)| {
1221                let name_key = NameKey::local(context.origin, local_handle);
1222                (name_key, local.ty, local.init)
1223            })
1224            .chain(oob_local_types.iter().map(|&ty| {
1225                let name_key = NameKey::oob_local_for_type(context.origin, ty);
1226                (name_key, ty, None)
1227            }))
1228        {
1229            let ty_name = TypeContext {
1230                handle: ty,
1231                gctx: context.module.to_ctx(),
1232                names: &self.names,
1233                access: crate::StorageAccess::empty(),
1234                first_time: false,
1235            };
1236            write!(
1237                self.out,
1238                "{}{} {}",
1239                back::INDENT,
1240                ty_name,
1241                self.names[&name_key]
1242            )?;
1243            match init {
1244                Some(value) => {
1245                    write!(self.out, " = ")?;
1246                    self.put_expression(value, context, true)?;
1247                }
1248                None => {
1249                    write!(self.out, " = {{}}")?;
1250                }
1251            };
1252            writeln!(self.out, ";")?;
1253
1254            // If this variable is a ray query, put in an initialization tracker.
1255            if context.ray_query_initialization_tracking {
1256                if let crate::TypeInner::RayQuery { .. } = context.module.types[ty].inner {
1257                    writeln!(
1258                        self.out,
1259                        "{}uint {}{} = 0u;",
1260                        back::INDENT,
1261                        super::ray::RAY_QUERY_TRACKER_VARIABLE_PREFIX,
1262                        self.names[&name_key]
1263                    )?;
1264
1265                    writeln!(
1266                        self.out,
1267                        "{}float {}{} = 0.0;",
1268                        back::INDENT,
1269                        super::ray::RAY_QUERY_T_MAX_TRACKER_VARIABLE_PREFIX,
1270                        self.names[&name_key]
1271                    )?;
1272                }
1273            }
1274        }
1275        Ok(())
1276    }
1277
1278    fn put_level_of_detail(
1279        &mut self,
1280        level: LevelOfDetail,
1281        context: &ExpressionContext,
1282    ) -> BackendResult {
1283        match level {
1284            LevelOfDetail::Direct(expr) => self.put_expression(expr, context, true)?,
1285            LevelOfDetail::Restricted(load) => write!(self.out, "{}", ClampedLod(load))?,
1286        }
1287        Ok(())
1288    }
1289
1290    fn put_image_query(
1291        &mut self,
1292        image: Handle<crate::Expression>,
1293        query: &str,
1294        level: Option<LevelOfDetail>,
1295        context: &ExpressionContext,
1296    ) -> BackendResult {
1297        self.put_expression(image, context, false)?;
1298        write!(self.out, ".get_{query}(")?;
1299        if let Some(level) = level {
1300            self.put_level_of_detail(level, context)?;
1301        }
1302        write!(self.out, ")")?;
1303        Ok(())
1304    }
1305
1306    fn put_image_size_query(
1307        &mut self,
1308        image: Handle<crate::Expression>,
1309        level: Option<LevelOfDetail>,
1310        kind: crate::ScalarKind,
1311        context: &ExpressionContext,
1312    ) -> BackendResult {
1313        if context.resolve_type(image).is_external_image() {
1314            write!(self.out, "{IMAGE_SIZE_EXTERNAL_FUNCTION}(")?;
1315            self.put_expression(image, context, true)?;
1316            write!(self.out, ")")?;
1317            return Ok(());
1318        }
1319
1320        //Note: MSL only has separate width/height/depth queries,
1321        // so compose the result of them.
1322        let dim = match *context.resolve_type(image) {
1323            crate::TypeInner::Image { dim, .. } => dim,
1324            ref other => unreachable!("Unexpected type {:?}", other),
1325        };
1326        let scalar = crate::Scalar { kind, width: 4 };
1327        let coordinate_type = scalar.to_msl_name();
1328        match dim {
1329            crate::ImageDimension::D1 => {
1330                // Since 1D textures never have mipmaps, MSL requires that the
1331                // `level` argument be a constexpr 0. It's simplest for us just
1332                // to pass `None` and omit the level entirely.
1333                if kind == crate::ScalarKind::Uint {
1334                    // No need to construct a vector. No cast needed.
1335                    self.put_image_query(image, "width", None, context)?;
1336                } else {
1337                    // There's no definition for `int` in the `metal` namespace.
1338                    write!(self.out, "int(")?;
1339                    self.put_image_query(image, "width", None, context)?;
1340                    write!(self.out, ")")?;
1341                }
1342            }
1343            crate::ImageDimension::D2 => {
1344                write!(self.out, "{NAMESPACE}::{coordinate_type}2(")?;
1345                self.put_image_query(image, "width", level, context)?;
1346                write!(self.out, ", ")?;
1347                self.put_image_query(image, "height", level, context)?;
1348                write!(self.out, ")")?;
1349            }
1350            crate::ImageDimension::D3 => {
1351                write!(self.out, "{NAMESPACE}::{coordinate_type}3(")?;
1352                self.put_image_query(image, "width", level, context)?;
1353                write!(self.out, ", ")?;
1354                self.put_image_query(image, "height", level, context)?;
1355                write!(self.out, ", ")?;
1356                self.put_image_query(image, "depth", level, context)?;
1357                write!(self.out, ")")?;
1358            }
1359            crate::ImageDimension::Cube => {
1360                write!(self.out, "{NAMESPACE}::{coordinate_type}2(")?;
1361                self.put_image_query(image, "width", level, context)?;
1362                write!(self.out, ")")?;
1363            }
1364        }
1365        Ok(())
1366    }
1367
1368    fn put_cast_to_uint_scalar_or_vector(
1369        &mut self,
1370        expr: Handle<crate::Expression>,
1371        context: &ExpressionContext,
1372    ) -> BackendResult {
1373        // coordinates in IR are int, but Metal expects uint
1374        match *context.resolve_type(expr) {
1375            crate::TypeInner::Scalar(_) => {
1376                put_numeric_type(&mut self.out, crate::Scalar::U32, &[])?
1377            }
1378            crate::TypeInner::Vector { size, .. } => {
1379                put_numeric_type(&mut self.out, crate::Scalar::U32, &[size])?
1380            }
1381            _ => {
1382                return Err(Error::GenericValidation(
1383                    "Invalid type for image coordinate".into(),
1384                ))
1385            }
1386        };
1387
1388        write!(self.out, "(")?;
1389        self.put_expression(expr, context, true)?;
1390        write!(self.out, ")")?;
1391        Ok(())
1392    }
1393
1394    fn put_image_sample_level(
1395        &mut self,
1396        image: Handle<crate::Expression>,
1397        level: crate::SampleLevel,
1398        context: &ExpressionContext,
1399    ) -> BackendResult {
1400        let has_levels = context.image_needs_lod(image);
1401        match level {
1402            crate::SampleLevel::Auto => {}
1403            crate::SampleLevel::Zero => {
1404                //TODO: do we support Zero on `Sampled` image classes?
1405            }
1406            _ if !has_levels => {
1407                log::warn!("1D image can't be sampled with level {level:?}");
1408            }
1409            crate::SampleLevel::Exact(h) => {
1410                write!(self.out, ", {NAMESPACE}::level(")?;
1411                self.put_expression(h, context, true)?;
1412                write!(self.out, ")")?;
1413            }
1414            crate::SampleLevel::Bias(h) => {
1415                write!(self.out, ", {NAMESPACE}::bias(")?;
1416                self.put_expression(h, context, true)?;
1417                write!(self.out, ")")?;
1418            }
1419            crate::SampleLevel::Gradient { x, y } => {
1420                let dim = match *context.resolve_type(image) {
1421                    crate::TypeInner::Image { dim, .. } => dim,
1422                    ref other => unreachable!("Unexpected type {other:?}"),
1423                };
1424                let gradient = match dim {
1425                    crate::ImageDimension::D2 => "gradient2d",
1426                    crate::ImageDimension::D3 => "gradient3d",
1427                    crate::ImageDimension::Cube => "gradientcube",
1428                    // `image_needs_lod` is false for 1D images, so the
1429                    // `!has_levels` arm above has already handled them.
1430                    crate::ImageDimension::D1 => {
1431                        unreachable!("1D image can't be sampled with a gradient")
1432                    }
1433                };
1434                write!(self.out, ", {NAMESPACE}::{gradient}(")?;
1435                self.put_expression(x, context, true)?;
1436                write!(self.out, ", ")?;
1437                self.put_expression(y, context, true)?;
1438                write!(self.out, ")")?;
1439            }
1440        }
1441        Ok(())
1442    }
1443
1444    fn put_image_coordinate_limits(
1445        &mut self,
1446        image: Handle<crate::Expression>,
1447        level: Option<LevelOfDetail>,
1448        context: &ExpressionContext,
1449    ) -> BackendResult {
1450        self.put_image_size_query(image, level, crate::ScalarKind::Uint, context)?;
1451        write!(self.out, " - 1")?;
1452        Ok(())
1453    }
1454
1455    /// General function for writing restricted image indexes.
1456    ///
1457    /// This is used to produce restricted mip levels, array indices, and sample
1458    /// indices for [`ImageLoad`] and [`ImageStore`] accesses under the
1459    /// [`Restrict`] bounds check policy.
1460    ///
1461    /// This function writes an expression of the form:
1462    ///
1463    /// ```ignore
1464    ///
1465    ///     metal::min(uint(INDEX), IMAGE.LIMIT_METHOD() - 1)
1466    ///
1467    /// ```
1468    ///
1469    /// [`ImageLoad`]: crate::Expression::ImageLoad
1470    /// [`ImageStore`]: crate::Statement::ImageStore
1471    /// [`Restrict`]: index::BoundsCheckPolicy::Restrict
1472    fn put_restricted_scalar_image_index(
1473        &mut self,
1474        image: Handle<crate::Expression>,
1475        index: Handle<crate::Expression>,
1476        limit_method: &str,
1477        context: &ExpressionContext,
1478    ) -> BackendResult {
1479        write!(self.out, "{NAMESPACE}::min(uint(")?;
1480        self.put_expression(index, context, true)?;
1481        write!(self.out, "), ")?;
1482        self.put_expression(image, context, false)?;
1483        write!(self.out, ".{limit_method}() - 1)")?;
1484        Ok(())
1485    }
1486
1487    fn put_restricted_texel_address(
1488        &mut self,
1489        image: Handle<crate::Expression>,
1490        address: &TexelAddress,
1491        context: &ExpressionContext,
1492    ) -> BackendResult {
1493        // Write the coordinate.
1494        write!(self.out, "{NAMESPACE}::min(")?;
1495        self.put_cast_to_uint_scalar_or_vector(address.coordinate, context)?;
1496        write!(self.out, ", ")?;
1497        self.put_image_coordinate_limits(image, address.level, context)?;
1498        write!(self.out, ")")?;
1499
1500        // Write the array index, if present.
1501        if let Some(array_index) = address.array_index {
1502            write!(self.out, ", ")?;
1503            self.put_restricted_scalar_image_index(image, array_index, "get_array_size", context)?;
1504        }
1505
1506        // Write the sample index, if present.
1507        if let Some(sample) = address.sample {
1508            write!(self.out, ", ")?;
1509            self.put_restricted_scalar_image_index(image, sample, "get_num_samples", context)?;
1510        }
1511
1512        // The level of detail should be clamped and cached by
1513        // `put_cache_restricted_level`, so we don't need to clamp it here.
1514        if let Some(level) = address.level {
1515            write!(self.out, ", ")?;
1516            self.put_level_of_detail(level, context)?;
1517        }
1518
1519        Ok(())
1520    }
1521
1522    /// Write an expression that is true if the given image access is in bounds.
1523    fn put_image_access_bounds_check(
1524        &mut self,
1525        image: Handle<crate::Expression>,
1526        address: &TexelAddress,
1527        context: &ExpressionContext,
1528    ) -> BackendResult {
1529        let mut conjunction = "";
1530
1531        // First, check the level of detail. Only if that is in bounds can we
1532        // use it to find the appropriate bounds for the coordinates.
1533        let level = if let Some(level) = address.level {
1534            write!(self.out, "uint(")?;
1535            self.put_level_of_detail(level, context)?;
1536            write!(self.out, ") < ")?;
1537            self.put_expression(image, context, true)?;
1538            write!(self.out, ".get_num_mip_levels()")?;
1539            conjunction = " && ";
1540            Some(level)
1541        } else {
1542            None
1543        };
1544
1545        // Check sample index, if present.
1546        if let Some(sample) = address.sample {
1547            write!(self.out, "uint(")?;
1548            self.put_expression(sample, context, true)?;
1549            write!(self.out, ") < ")?;
1550            self.put_expression(image, context, true)?;
1551            write!(self.out, ".get_num_samples()")?;
1552            conjunction = " && ";
1553        }
1554
1555        // Check array index, if present.
1556        if let Some(array_index) = address.array_index {
1557            write!(self.out, "{conjunction}uint(")?;
1558            self.put_expression(array_index, context, true)?;
1559            write!(self.out, ") < ")?;
1560            self.put_expression(image, context, true)?;
1561            write!(self.out, ".get_array_size()")?;
1562            conjunction = " && ";
1563        }
1564
1565        // Finally, check if the coordinates are within bounds.
1566        let coord_is_vector = match *context.resolve_type(address.coordinate) {
1567            crate::TypeInner::Vector { .. } => true,
1568            _ => false,
1569        };
1570        write!(self.out, "{conjunction}")?;
1571        if coord_is_vector {
1572            write!(self.out, "{NAMESPACE}::all(")?;
1573        }
1574        self.put_cast_to_uint_scalar_or_vector(address.coordinate, context)?;
1575        write!(self.out, " < ")?;
1576        self.put_image_size_query(image, level, crate::ScalarKind::Uint, context)?;
1577        if coord_is_vector {
1578            write!(self.out, ")")?;
1579        }
1580
1581        Ok(())
1582    }
1583
1584    fn put_image_load(
1585        &mut self,
1586        load: Handle<crate::Expression>,
1587        image: Handle<crate::Expression>,
1588        mut address: TexelAddress,
1589        context: &ExpressionContext,
1590    ) -> BackendResult {
1591        if context.resolve_type(image).is_external_image() {
1592            write!(self.out, "{IMAGE_LOAD_EXTERNAL_FUNCTION}(")?;
1593            self.put_expression(image, context, true)?;
1594            write!(self.out, ", ")?;
1595            self.put_cast_to_uint_scalar_or_vector(address.coordinate, context)?;
1596            write!(self.out, ")")?;
1597            return Ok(());
1598        }
1599
1600        match context.policies.image_load {
1601            proc::BoundsCheckPolicy::Restrict => {
1602                // Use the cached restricted level of detail, if any. Omit the
1603                // level altogether for 1D textures.
1604                if address.level.is_some() {
1605                    address.level = if context.image_needs_lod(image) {
1606                        Some(LevelOfDetail::Restricted(load))
1607                    } else {
1608                        None
1609                    }
1610                }
1611
1612                self.put_expression(image, context, false)?;
1613                write!(self.out, ".read(")?;
1614                self.put_restricted_texel_address(image, &address, context)?;
1615                write!(self.out, ")")?;
1616            }
1617            proc::BoundsCheckPolicy::ReadZeroSkipWrite => {
1618                write!(self.out, "(")?;
1619                self.put_image_access_bounds_check(image, &address, context)?;
1620                write!(self.out, " ? ")?;
1621                self.put_unchecked_image_load(image, &address, context)?;
1622                write!(self.out, ": DefaultConstructible())")?;
1623            }
1624            proc::BoundsCheckPolicy::Unchecked => {
1625                self.put_unchecked_image_load(image, &address, context)?;
1626            }
1627        }
1628
1629        Ok(())
1630    }
1631
1632    fn put_unchecked_image_load(
1633        &mut self,
1634        image: Handle<crate::Expression>,
1635        address: &TexelAddress,
1636        context: &ExpressionContext,
1637    ) -> BackendResult {
1638        self.put_expression(image, context, false)?;
1639        write!(self.out, ".read(")?;
1640        // coordinates in IR are int, but Metal expects uint
1641        self.put_cast_to_uint_scalar_or_vector(address.coordinate, context)?;
1642        if let Some(expr) = address.array_index {
1643            write!(self.out, ", ")?;
1644            self.put_expression(expr, context, true)?;
1645        }
1646        if let Some(sample) = address.sample {
1647            write!(self.out, ", ")?;
1648            self.put_expression(sample, context, true)?;
1649        }
1650        if let Some(level) = address.level {
1651            if context.image_needs_lod(image) {
1652                write!(self.out, ", ")?;
1653                self.put_level_of_detail(level, context)?;
1654            }
1655        }
1656        write!(self.out, ")")?;
1657
1658        Ok(())
1659    }
1660
1661    fn put_image_atomic(
1662        &mut self,
1663        level: back::Level,
1664        image: Handle<crate::Expression>,
1665        address: &TexelAddress,
1666        fun: crate::AtomicFunction,
1667        value: Handle<crate::Expression>,
1668        context: &StatementContext,
1669    ) -> BackendResult {
1670        write!(self.out, "{level}")?;
1671        self.put_expression(image, &context.expression, false)?;
1672        let op = if context.expression.resolve_type(value).scalar_width() == Some(8) {
1673            fun.to_msl_64_bit()?
1674        } else {
1675            fun.to_msl()
1676        };
1677        write!(self.out, ".atomic_{op}(")?;
1678        // coordinates in IR are int, but Metal expects uint
1679        self.put_cast_to_uint_scalar_or_vector(address.coordinate, &context.expression)?;
1680        write!(self.out, ", ")?;
1681        self.put_expression(value, &context.expression, true)?;
1682        writeln!(self.out, ");")?;
1683
1684        // Workaround for Apple Metal TBDR driver bug: fragment shader atomic
1685        // texture writes randomly drop unless followed by a standard texture
1686        // write. Insert a dead-code write behind an unprovable condition so
1687        // the compiler emits proper memory safety barriers.
1688        // See: https://projects.blender.org/blender/blender/commit/aa95220576706122d79c91c7f5c522e6c7416425
1689        let value_ty = context.expression.resolve_type(value);
1690        let zero_value = match (value_ty.scalar_kind(), value_ty.scalar_width()) {
1691            (Some(crate::ScalarKind::Sint), _) => "int4(0)",
1692            (_, Some(8)) => "ulong4(0uL)",
1693            _ => "uint4(0u)",
1694        };
1695        let coord_ty = context.expression.resolve_type(address.coordinate);
1696        let x = if matches!(coord_ty, crate::TypeInner::Scalar(_)) {
1697            ""
1698        } else {
1699            ".x"
1700        };
1701        write!(self.out, "{level}if (")?;
1702        self.put_expression(address.coordinate, &context.expression, true)?;
1703        write!(self.out, "{x} == -99999) {{ ")?;
1704        self.put_expression(image, &context.expression, false)?;
1705        write!(self.out, ".write({zero_value}, ")?;
1706        self.put_cast_to_uint_scalar_or_vector(address.coordinate, &context.expression)?;
1707        if let Some(array_index) = address.array_index {
1708            write!(self.out, ", ")?;
1709            self.put_expression(array_index, &context.expression, true)?;
1710        }
1711        writeln!(self.out, "); }}")?;
1712
1713        Ok(())
1714    }
1715
1716    fn put_image_store(
1717        &mut self,
1718        level: back::Level,
1719        image: Handle<crate::Expression>,
1720        address: &TexelAddress,
1721        value: Handle<crate::Expression>,
1722        context: &StatementContext,
1723    ) -> BackendResult {
1724        write!(self.out, "{level}")?;
1725        self.put_expression(image, &context.expression, false)?;
1726        write!(self.out, ".write(")?;
1727        self.put_expression(value, &context.expression, true)?;
1728        write!(self.out, ", ")?;
1729        // coordinates in IR are int, but Metal expects uint
1730        self.put_cast_to_uint_scalar_or_vector(address.coordinate, &context.expression)?;
1731        if let Some(expr) = address.array_index {
1732            write!(self.out, ", ")?;
1733            self.put_expression(expr, &context.expression, true)?;
1734        }
1735        writeln!(self.out, ");")?;
1736
1737        Ok(())
1738    }
1739
1740    /// Write the maximum valid index of the dynamically sized array at the end of `handle`.
1741    ///
1742    /// The 'maximum valid index' is simply one less than the array's length.
1743    ///
1744    /// This emits an expression of the form `a / b`, so the caller must
1745    /// parenthesize its output if it will be applying operators of higher
1746    /// precedence.
1747    ///
1748    /// `handle` must be the handle of a global variable whose final member is a
1749    /// dynamically sized array.
1750    ///
1751    /// `chain_expr` sits on the pointer path from an inner access, such as the
1752    /// value passed to array length, back toward this global. For storage binding
1753    /// arrays, that path tells us which element index to use in `_buffer_sizes`.
1754    fn binding_array_layout_count(
1755        module: &crate::Module,
1756        pipeline_options: &PipelineOptions,
1757        global: Handle<crate::GlobalVariable>,
1758    ) -> u32 {
1759        let var = &module.global_variables[global];
1760        let crate::TypeInner::BindingArray { size, .. } = module.types[var.ty].inner else {
1761            unreachable!("binding_array_layout_count called on non-binding-array global");
1762        };
1763        let from_shader = match size {
1764            crate::ArraySize::Constant(n) => n.get(),
1765            crate::ArraySize::Pending(_) | crate::ArraySize::Dynamic => 0,
1766        };
1767        let from_layout = var
1768            .binding
1769            .and_then(|br| pipeline_options.binding_array_length_map.get(&br))
1770            .copied()
1771            .unwrap_or(0);
1772        from_shader.max(from_layout).max(1)
1773    }
1774
1775    fn put_binding_array_size_member_index(
1776        &mut self,
1777        index: index::GuardedIndex,
1778        context: &ExpressionContext,
1779    ) -> BackendResult {
1780        match index {
1781            index::GuardedIndex::Expression(expr) => {
1782                write!(self.out, "unsigned(")?;
1783                self.put_expression(expr, context, true)?;
1784                write!(self.out, ")")?;
1785            }
1786            index::GuardedIndex::Known(value) => write!(self.out, "{value}u")?,
1787        }
1788        Ok(())
1789    }
1790
1791    fn put_dynamic_array_max_index(
1792        &mut self,
1793        handle: Handle<crate::GlobalVariable>,
1794        chain_expr: Handle<crate::Expression>,
1795        context: &ExpressionContext,
1796    ) -> BackendResult {
1797        let global = &context.module.global_variables[handle];
1798        let (offset, array_ty) = match context.module.types[global.ty].inner {
1799            crate::TypeInner::Struct { ref members, .. } => match members.last() {
1800                Some(&crate::StructMember { offset, ty, .. }) => (offset, ty),
1801                None => return Err(Error::GenericValidation("Struct has no members".into())),
1802            },
1803            crate::TypeInner::BindingArray { base, .. } => match context.module.types[base].inner {
1804                crate::TypeInner::Struct { ref members, .. } => match members.last() {
1805                    Some(&crate::StructMember { offset, ty, .. }) => (offset, ty),
1806                    None => return Err(Error::GenericValidation("Struct has no members".into())),
1807                },
1808                _ => {
1809                    return Err(Error::GenericValidation(
1810                        "binding_array element must be a struct with a runtime-sized array field"
1811                            .into(),
1812                    ))
1813                }
1814            },
1815            crate::TypeInner::Array {
1816                size: crate::ArraySize::Dynamic,
1817                ..
1818            } => (0, global.ty),
1819            ref ty => {
1820                return Err(Error::GenericValidation(format!(
1821                    "Expected type with dynamic array, got {ty:?}"
1822                )))
1823            }
1824        };
1825
1826        let (size, stride) = match context.module.types[array_ty].inner {
1827            crate::TypeInner::Array { base, stride, .. } => (
1828                context.module.types[base]
1829                    .inner
1830                    .size(context.module.to_ctx()),
1831                stride,
1832            ),
1833            ref ty => {
1834                return Err(Error::GenericValidation(format!(
1835                    "Expected array type, got {ty:?}"
1836                )))
1837            }
1838        };
1839
1840        // When the stride length is larger than the size, the final element's stride of
1841        // bytes would have padding following the value. But the buffer size in
1842        // `buffer_sizes.sizeN` may not include this padding - it only needs to be large
1843        // enough to hold the actual values' bytes.
1844        //
1845        // So subtract off the size to get a byte size that falls at the start or within
1846        // the final element. Then divide by the stride size, to get one less than the
1847        // length, and then add one. This works even if the buffer size does include the
1848        // stride padding, since division rounds towards zero (MSL 2.4 §6.1). It will fail
1849        // if there are zero elements in the array, but the WebGPU `validating shader binding`
1850        // rules, together with draw-time validation when `minBindingSize` is zero,
1851        // prevent that.
1852        write!(
1853            self.out,
1854            "(_buffer_sizes.{member}",
1855            member = ArraySizeMember(handle),
1856        )?;
1857        if let crate::TypeInner::BindingArray { .. } = context.module.types[global.ty].inner {
1858            let Some(array_index) = context.binding_array_index_from_chain(chain_expr, handle)
1859            else {
1860                return Err(Error::GenericValidation(
1861                    "Could not find binding_array index for buffer size".into(),
1862                ));
1863            };
1864            write!(self.out, "[")?;
1865            match array_index {
1866                index::GuardedIndex::Expression(expr) => {
1867                    write!(self.out, "unsigned(")?;
1868                    self.put_expression(expr, context, true)?;
1869                    write!(self.out, ")")?;
1870                }
1871                index::GuardedIndex::Known(i) => {
1872                    write!(self.out, "{i}u")?;
1873                }
1874            }
1875            write!(self.out, "]")?;
1876        }
1877        write!(
1878            self.out,
1879            " - {offset} - {size}) / {stride}",
1880            offset = offset,
1881            size = size,
1882            stride = stride,
1883        )?;
1884        Ok(())
1885    }
1886
1887    /// Emit code for the arithmetic expression of the dot product.
1888    ///
1889    /// The argument `extractor` is a function that accepts a `Writer`, a vector, and
1890    /// an index. It writes out the expression for the vector component at that index.
1891    fn put_dot_product<T: Copy>(
1892        &mut self,
1893        arg: T,
1894        arg1: T,
1895        size: usize,
1896        extractor: impl Fn(&mut Self, T, usize) -> BackendResult,
1897    ) -> BackendResult {
1898        // Write parentheses around the dot product expression to prevent operators
1899        // with different precedences from applying earlier.
1900        write!(self.out, "(")?;
1901
1902        // Cycle through all the components of the vector
1903        for index in 0..size {
1904            // Write the addition to the previous product
1905            // This will print an extra '+' at the beginning but that is fine in msl
1906            write!(self.out, " + ")?;
1907            extractor(self, arg, index)?;
1908            write!(self.out, " * ")?;
1909            extractor(self, arg1, index)?;
1910        }
1911
1912        write!(self.out, ")")?;
1913        Ok(())
1914    }
1915
1916    /// Emit code for the WGSL functions `pack4x{I, U}8[Clamp]`.
1917    fn put_pack4x8(
1918        &mut self,
1919        arg: Handle<crate::Expression>,
1920        context: &ExpressionContext<'_>,
1921        was_signed: bool,
1922        clamp_bounds: Option<(&str, &str)>,
1923    ) -> Result<(), Error> {
1924        let write_arg = |this: &mut Self| -> BackendResult {
1925            if let Some((min, max)) = clamp_bounds {
1926                // Clamping with scalar bounds works (component-wise) even for packed_[u]char4.
1927                write!(this.out, "{NAMESPACE}::clamp(")?;
1928                this.put_expression(arg, context, true)?;
1929                write!(this.out, ", {min}, {max})")?;
1930            } else {
1931                this.put_expression(arg, context, true)?;
1932            }
1933            Ok(())
1934        };
1935
1936        if context.lang_version >= (2, 1) {
1937            let packed_type = if was_signed {
1938                "packed_char4"
1939            } else {
1940                "packed_uchar4"
1941            };
1942            // Metal uses little endian byte order, which matches what WGSL expects here.
1943            write!(self.out, "as_type<uint>({packed_type}(")?;
1944            write_arg(self)?;
1945            write!(self.out, "))")?;
1946        } else {
1947            // MSL < 2.1 doesn't support `as_type` casting between packed chars and scalars.
1948            if was_signed {
1949                write!(self.out, "uint(")?;
1950            }
1951            write!(self.out, "(")?;
1952            write_arg(self)?;
1953            write!(self.out, "[0] & 0xFF) | ((")?;
1954            write_arg(self)?;
1955            write!(self.out, "[1] & 0xFF) << 8) | ((")?;
1956            write_arg(self)?;
1957            write!(self.out, "[2] & 0xFF) << 16) | ((")?;
1958            write_arg(self)?;
1959            write!(self.out, "[3] & 0xFF) << 24)")?;
1960            if was_signed {
1961                write!(self.out, ")")?;
1962            }
1963        }
1964
1965        Ok(())
1966    }
1967
1968    /// Emit code for the isign expression.
1969    ///
1970    fn put_isign(
1971        &mut self,
1972        arg: Handle<crate::Expression>,
1973        context: &ExpressionContext,
1974    ) -> BackendResult {
1975        write!(self.out, "{NAMESPACE}::select({NAMESPACE}::select(")?;
1976        let scalar = context
1977            .resolve_type(arg)
1978            .scalar()
1979            .expect("put_isign should only be called for args which have an integer scalar type")
1980            .to_msl_name();
1981        match context.resolve_type(arg) {
1982            &crate::TypeInner::Vector { size, .. } => {
1983                let size = common::vector_size_str(size);
1984                write!(self.out, "{scalar}{size}(-1), {scalar}{size}(1)")?;
1985            }
1986            _ => {
1987                write!(self.out, "{scalar}(-1), {scalar}(1)")?;
1988            }
1989        }
1990        write!(self.out, ", (")?;
1991        self.put_expression(arg, context, true)?;
1992        write!(self.out, " > 0)), {scalar}(0), (")?;
1993        self.put_expression(arg, context, true)?;
1994        write!(self.out, " == 0))")?;
1995        Ok(())
1996    }
1997
1998    pub(super) fn put_const_expression(
1999        &mut self,
2000        expr_handle: Handle<crate::Expression>,
2001        module: &crate::Module,
2002        mod_info: &valid::ModuleInfo,
2003        arena: &crate::Arena<crate::Expression>,
2004    ) -> BackendResult {
2005        self.put_possibly_const_expression(
2006            expr_handle,
2007            arena,
2008            module,
2009            mod_info,
2010            &(module, mod_info),
2011            |&(_, mod_info), expr| &mod_info[expr],
2012            |writer, &(module, _), expr| writer.put_const_expression(expr, module, mod_info, arena),
2013        )
2014    }
2015
2016    fn put_literal(&mut self, literal: crate::Literal) -> BackendResult {
2017        match literal {
2018            crate::Literal::F64(_) => {
2019                return Err(Error::CapabilityNotSupported(valid::Capabilities::FLOAT64))
2020            }
2021            crate::Literal::F16(value) => {
2022                if value.is_infinite() {
2023                    let sign = if value.is_sign_negative() { "-" } else { "" };
2024                    write!(self.out, "{sign}INFINITY")?;
2025                } else if value.is_nan() {
2026                    write!(self.out, "NAN")?;
2027                } else {
2028                    let suffix = if value.fract() == f16::from_f32(0.0) {
2029                        ".0h"
2030                    } else {
2031                        "h"
2032                    };
2033                    write!(self.out, "{value}{suffix}")?;
2034                }
2035            }
2036            crate::Literal::F32(value) => {
2037                if value.is_infinite() {
2038                    let sign = if value.is_sign_negative() { "-" } else { "" };
2039                    write!(self.out, "{sign}INFINITY")?;
2040                } else if value.is_nan() {
2041                    write!(self.out, "NAN")?;
2042                } else {
2043                    let suffix = if value.fract() == 0.0 { ".0" } else { "" };
2044                    write!(self.out, "{value}{suffix}")?;
2045                }
2046            }
2047            crate::Literal::U16(value) => {
2048                write!(self.out, "static_cast<ushort>({value})")?;
2049            }
2050            crate::Literal::I16(value) => {
2051                write!(self.out, "static_cast<short>({value})")?;
2052            }
2053            crate::Literal::U32(value) => {
2054                write!(self.out, "{value}u")?;
2055            }
2056            crate::Literal::I32(value) => {
2057                // `-2147483648` is parsed as unary negation of positive 2147483648.
2058                // 2147483648 is too large for int32_t meaning the expression gets
2059                // promoted to a int64_t which is not our intention. Avoid this by instead
2060                // using `-2147483647 - 1`.
2061                if value == i32::MIN {
2062                    write!(self.out, "({} - 1)", value + 1)?;
2063                } else {
2064                    write!(self.out, "{value}")?;
2065                }
2066            }
2067            crate::Literal::U64(value) => {
2068                write!(self.out, "{value}uL")?;
2069            }
2070            crate::Literal::I64(value) => {
2071                // `-9223372036854775808` is parsed as unary negation of positive
2072                // 9223372036854775808. 9223372036854775808 is too large for int64_t
2073                // causing Metal to emit a `-Wconstant-conversion` warning, and change the
2074                // value to `-9223372036854775808`. Which would then be negated, possibly
2075                // causing undefined behaviour. Avoid this by instead using
2076                // `-9223372036854775808L - 1L`.
2077                if value == i64::MIN {
2078                    write!(self.out, "({}L - 1L)", value + 1)?;
2079                } else {
2080                    write!(self.out, "{value}L")?;
2081                }
2082            }
2083            crate::Literal::Bool(value) => {
2084                write!(self.out, "{value}")?;
2085            }
2086            crate::Literal::AbstractInt(_) | crate::Literal::AbstractFloat(_) => {
2087                return Err(Error::GenericValidation(
2088                    "Unsupported abstract literal".into(),
2089                ));
2090            }
2091        }
2092        Ok(())
2093    }
2094
2095    #[allow(clippy::too_many_arguments)]
2096    fn put_possibly_const_expression<C, I, E>(
2097        &mut self,
2098        expr_handle: Handle<crate::Expression>,
2099        expressions: &crate::Arena<crate::Expression>,
2100        module: &crate::Module,
2101        mod_info: &valid::ModuleInfo,
2102        ctx: &C,
2103        get_expr_ty: I,
2104        put_expression: E,
2105    ) -> BackendResult
2106    where
2107        I: Fn(&C, Handle<crate::Expression>) -> &TypeResolution,
2108        E: Fn(&mut Self, &C, Handle<crate::Expression>) -> BackendResult,
2109    {
2110        match expressions[expr_handle] {
2111            crate::Expression::Literal(literal) => {
2112                self.put_literal(literal)?;
2113            }
2114            crate::Expression::Constant(handle) => {
2115                let constant = &module.constants[handle];
2116                if constant.name.is_some() {
2117                    write!(self.out, "{}", self.names[&NameKey::Constant(handle)])?;
2118                } else {
2119                    self.put_const_expression(
2120                        constant.init,
2121                        module,
2122                        mod_info,
2123                        &module.global_expressions,
2124                    )?;
2125                }
2126            }
2127            crate::Expression::ZeroValue(ty) => {
2128                let ty_name = TypeContext {
2129                    handle: ty,
2130                    gctx: module.to_ctx(),
2131                    names: &self.names,
2132                    access: crate::StorageAccess::empty(),
2133                    first_time: false,
2134                };
2135                write!(self.out, "{ty_name} {{}}")?;
2136            }
2137            crate::Expression::Compose { ty, ref components } => {
2138                let ty_name = TypeContext {
2139                    handle: ty,
2140                    gctx: module.to_ctx(),
2141                    names: &self.names,
2142                    access: crate::StorageAccess::empty(),
2143                    first_time: false,
2144                };
2145                write!(self.out, "{ty_name}")?;
2146                match module.types[ty].inner {
2147                    crate::TypeInner::Scalar(_)
2148                    | crate::TypeInner::Vector { .. }
2149                    | crate::TypeInner::Matrix { .. } => {
2150                        self.put_call_parameters_impl(
2151                            components.iter().copied(),
2152                            ctx,
2153                            put_expression,
2154                        )?;
2155                    }
2156                    crate::TypeInner::Array { .. } => {
2157                        // Naga Arrays are Metal arrays wrapped in structs, so
2158                        // we need two levels of braces.
2159                        write!(self.out, " {{{{")?;
2160                        for (index, &component) in components.iter().enumerate() {
2161                            if index != 0 {
2162                                write!(self.out, ", ")?;
2163                            }
2164                            put_expression(self, ctx, component)?;
2165                        }
2166                        write!(self.out, "}}}}")?;
2167                    }
2168                    crate::TypeInner::Struct { .. } => {
2169                        write!(self.out, " {{")?;
2170                        for (index, &component) in components.iter().enumerate() {
2171                            if index != 0 {
2172                                write!(self.out, ", ")?;
2173                            }
2174                            // insert padding initialization, if needed
2175                            if self.struct_member_pads.contains(&(ty, index as u32)) {
2176                                write!(self.out, "{{}}, ")?;
2177                            }
2178                            put_expression(self, ctx, component)?;
2179                        }
2180                        write!(self.out, "}}")?;
2181                    }
2182                    _ => return Err(Error::UnsupportedCompose(ty)),
2183                }
2184            }
2185            crate::Expression::Splat { size, value } => {
2186                let scalar = match *get_expr_ty(ctx, value).inner_with(&module.types) {
2187                    crate::TypeInner::Scalar(scalar) => scalar,
2188                    ref ty => {
2189                        return Err(Error::GenericValidation(format!(
2190                            "Expected splat value type must be a scalar, got {ty:?}",
2191                        )))
2192                    }
2193                };
2194                put_numeric_type(&mut self.out, scalar, &[size])?;
2195                write!(self.out, "(")?;
2196                put_expression(self, ctx, value)?;
2197                write!(self.out, ")")?;
2198            }
2199            _ => {
2200                return Err(Error::Override);
2201            }
2202        }
2203
2204        Ok(())
2205    }
2206
2207    /// Write a polyfill for [`Statement::SubgroupBallotFindBit`], since MSL has no
2208    /// built-in for it: a chain of ternaries over the already-evaluated ballot
2209    /// `words` (`words[0..4]` is `x, y, z, w`). `priority` counts up from 0 (the
2210    /// lowest-priority word, used as the fallback base case) to 3 (the
2211    /// highest-priority word, checked first: `x` for LSB, `w` for MSB), each
2212    /// overriding the lower-priority result when that word has any bit set.
2213    ///
2214    /// [`Statement::SubgroupBallotFindBit`]: crate::Statement::SubgroupBallotFindBit
2215    fn put_ballot_find_bit(
2216        &mut self,
2217        words: &[String; 4],
2218        order: crate::BallotFindBitOrder,
2219        priority: u32,
2220    ) -> BackendResult {
2221        let (word_index, base) = match (order, priority) {
2222            (crate::BallotFindBitOrder::Lsb, 0) => (3, 96),
2223            (crate::BallotFindBitOrder::Lsb, 1) => (2, 64),
2224            (crate::BallotFindBitOrder::Lsb, 2) => (1, 32),
2225            (crate::BallotFindBitOrder::Lsb, 3) => (0, 0),
2226            (crate::BallotFindBitOrder::Msb, 0) => (0, 0),
2227            (crate::BallotFindBitOrder::Msb, 1) => (1, 32),
2228            (crate::BallotFindBitOrder::Msb, 2) => (2, 64),
2229            (crate::BallotFindBitOrder::Msb, 3) => (3, 96),
2230            (_, _) => unreachable!(),
2231        };
2232        let word = &words[word_index];
2233
2234        if priority == 0 {
2235            self.put_ballot_find_bit_value(order, word, base)?;
2236        } else {
2237            write!(self.out, "({word} != 0u ? ")?;
2238            self.put_ballot_find_bit_value(order, word, base)?;
2239            write!(self.out, " : ")?;
2240            self.put_ballot_find_bit(words, order, priority - 1)?;
2241            write!(self.out, ")")?;
2242        }
2243        Ok(())
2244    }
2245
2246    /// Write the least/most significant set bit of an already-evaluated `u32`
2247    /// ballot word, offset by that word's base invocation index. Mirrors the
2248    /// `Mf::FirstTrailingBit`/`Mf::FirstLeadingBit` formulas above, specialized
2249    /// for a known-unsigned 32-bit scalar. Used by [`Self::put_ballot_find_bit`].
2250    fn put_ballot_find_bit_value(
2251        &mut self,
2252        order: crate::BallotFindBitOrder,
2253        word: &str,
2254        base: u32,
2255    ) -> BackendResult {
2256        match order {
2257            crate::BallotFindBitOrder::Lsb => {
2258                write!(self.out, "((({NAMESPACE}::ctz({word}) + 1) % 33) - 1)")?;
2259            }
2260            crate::BallotFindBitOrder::Msb => {
2261                write!(
2262                    self.out,
2263                    "{NAMESPACE}::select(31 - {NAMESPACE}::clz({word}), uint(-1), {word} == 0)"
2264                )?;
2265            }
2266        }
2267        if base != 0 {
2268            write!(self.out, " + {base}u")?;
2269        }
2270        Ok(())
2271    }
2272
2273    /// Emit code for the expression `expr_handle`.
2274    ///
2275    /// The `is_scoped` argument is true if the surrounding operators have the
2276    /// precedence of the comma operator, or lower. So, for example:
2277    ///
2278    /// - Pass `true` for `is_scoped` when writing function arguments, an
2279    ///   expression statement, an initializer expression, or anything already
2280    ///   wrapped in parenthesis.
2281    ///
2282    /// - Pass `false` if it is an operand of a `?:` operator, a `[]`, or really
2283    ///   almost anything else.
2284    pub(super) fn put_expression(
2285        &mut self,
2286        expr_handle: Handle<crate::Expression>,
2287        context: &ExpressionContext,
2288        is_scoped: bool,
2289    ) -> BackendResult {
2290        // A hoisted temporary has no value yet at the point it was declared,
2291        // but it does hold one by the time anything reads it: the `continuing`
2292        // block sees what the loop body left there on the previous pass.
2293        let stored = self
2294            .named_expressions
2295            .get(&expr_handle)
2296            .or_else(|| self.hoisted_loop_expressions.get(&expr_handle));
2297        if let Some(name) = stored {
2298            write!(self.out, "{name}")?;
2299            return Ok(());
2300        }
2301
2302        let expression = &context.function.expressions[expr_handle];
2303        match *expression {
2304            crate::Expression::Literal(_)
2305            | crate::Expression::Constant(_)
2306            | crate::Expression::ZeroValue(_)
2307            | crate::Expression::Compose { .. }
2308            | crate::Expression::Splat { .. } => {
2309                self.put_possibly_const_expression(
2310                    expr_handle,
2311                    &context.function.expressions,
2312                    context.module,
2313                    context.mod_info,
2314                    context,
2315                    |context, expr: Handle<crate::Expression>| &context.info[expr].ty,
2316                    |writer, context, expr| writer.put_expression(expr, context, true),
2317                )?;
2318            }
2319            crate::Expression::Override(_) => return Err(Error::Override),
2320            crate::Expression::Access { base, .. }
2321            | crate::Expression::AccessIndex { base, .. } => {
2322                // This is an acceptable place to generate a `ReadZeroSkipWrite` check.
2323                // Since `put_bounds_checks` and `put_access_chain` handle an entire
2324                // access chain at a time, recursing back through `put_expression` only
2325                // for index expressions and the base object, we will never see intermediate
2326                // `Access` or `AccessIndex` expressions here.
2327                let policy = context.choose_bounds_check_policy(base);
2328                if policy == index::BoundsCheckPolicy::ReadZeroSkipWrite
2329                    && self.put_bounds_checks(
2330                        expr_handle,
2331                        context,
2332                        back::Level(0),
2333                        if is_scoped { "" } else { "(" },
2334                    )?
2335                {
2336                    write!(self.out, " ? ")?;
2337                    self.put_access_chain(expr_handle, policy, context)?;
2338                    write!(self.out, " : ")?;
2339
2340                    if context.resolve_type(base).pointer_space().is_some() {
2341                        // We can't just use `DefaultConstructible` if this is a pointer.
2342                        // Instead, we create a dummy local variable to serve as pointer
2343                        // target if the access is out of bounds.
2344                        let result_ty = context.info[expr_handle]
2345                            .ty
2346                            .inner_with(&context.module.types)
2347                            .pointer_base_type();
2348                        let result_ty_handle = match result_ty {
2349                            Some(TypeResolution::Handle(handle)) => handle,
2350                            Some(TypeResolution::Value(_)) => {
2351                                // As long as the result of a pointer access expression is
2352                                // passed to a function or stored in a let binding, the
2353                                // type will be in the arena. If additional uses of
2354                                // pointers become valid, this assumption might no longer
2355                                // hold. Note that the LHS of a load or store doesn't
2356                                // take this path -- there is dedicated code in `put_load`
2357                                // and `put_store`.
2358                                unreachable!(
2359                                    "Expected type {result_ty:?} of access through pointer type {base:?} to be in the arena",
2360                                );
2361                            }
2362                            None => {
2363                                unreachable!(
2364                                    "Expected access through pointer type {base:?} to return a pointer, but got {result_ty:?}",
2365                                )
2366                            }
2367                        };
2368                        let name_key =
2369                            NameKey::oob_local_for_type(context.origin, result_ty_handle);
2370                        self.out.write_str(&self.names[&name_key])?;
2371                    } else {
2372                        write!(self.out, "DefaultConstructible()")?;
2373                    }
2374
2375                    if !is_scoped {
2376                        write!(self.out, ")")?;
2377                    }
2378                } else {
2379                    self.put_access_chain(expr_handle, policy, context)?;
2380                }
2381            }
2382            crate::Expression::Swizzle {
2383                size,
2384                vector,
2385                pattern,
2386            } => {
2387                self.put_wrapped_expression_for_packed_vec3_access(
2388                    vector,
2389                    context,
2390                    false,
2391                    &Self::put_expression,
2392                )?;
2393                write!(self.out, ".")?;
2394                for &sc in pattern[..size as usize].iter() {
2395                    write!(self.out, "{}", back::COMPONENTS[sc as usize])?;
2396                }
2397            }
2398            crate::Expression::FunctionArgument(index) => {
2399                let name_key = match context.origin {
2400                    FunctionOrigin::Handle(handle) => NameKey::FunctionArgument(handle, index),
2401                    FunctionOrigin::EntryPoint(ep_index) => {
2402                        NameKey::EntryPointArgument(ep_index, index)
2403                    }
2404                };
2405                let name = &self.names[&name_key];
2406                write!(self.out, "{name}")?;
2407            }
2408            crate::Expression::GlobalVariable(handle) => {
2409                let name = &self.names[&NameKey::GlobalVariable(handle)];
2410                write!(self.out, "{name}")?;
2411            }
2412            crate::Expression::LocalVariable(handle) => {
2413                let name_key = NameKey::local(context.origin, handle);
2414                let name = &self.names[&name_key];
2415                write!(self.out, "{name}")?;
2416            }
2417            crate::Expression::Load { pointer } => self.put_load(pointer, context, is_scoped)?,
2418            crate::Expression::ImageSample {
2419                coordinate,
2420                image,
2421                sampler,
2422                clamp_to_edge: true,
2423                gather: None,
2424                array_index: None,
2425                offset: None,
2426                level: crate::SampleLevel::Zero,
2427                depth_ref: None,
2428            } => {
2429                write!(self.out, "{IMAGE_SAMPLE_BASE_CLAMP_TO_EDGE_FUNCTION}(")?;
2430                self.put_expression(image, context, true)?;
2431                write!(self.out, ", ")?;
2432                self.put_expression(sampler, context, true)?;
2433                write!(self.out, ", ")?;
2434                self.put_expression(coordinate, context, true)?;
2435                write!(self.out, ")")?;
2436            }
2437            crate::Expression::ImageSample {
2438                image,
2439                sampler,
2440                gather,
2441                coordinate,
2442                array_index,
2443                offset,
2444                level,
2445                depth_ref,
2446                clamp_to_edge,
2447            } => {
2448                if clamp_to_edge {
2449                    return Err(Error::GenericValidation(
2450                        "ImageSample::clamp_to_edge should have been validated out".to_string(),
2451                    ));
2452                }
2453
2454                let main_op = match gather {
2455                    Some(_) => "gather",
2456                    None => "sample",
2457                };
2458                let comparison_op = match depth_ref {
2459                    Some(_) => "_compare",
2460                    None => "",
2461                };
2462                self.put_expression(image, context, false)?;
2463                write!(self.out, ".{main_op}{comparison_op}(")?;
2464                self.put_expression(sampler, context, true)?;
2465                write!(self.out, ", ")?;
2466                self.put_expression(coordinate, context, true)?;
2467                if let Some(expr) = array_index {
2468                    write!(self.out, ", ")?;
2469                    self.put_expression(expr, context, true)?;
2470                }
2471                if let Some(dref) = depth_ref {
2472                    write!(self.out, ", ")?;
2473                    self.put_expression(dref, context, true)?;
2474                }
2475
2476                self.put_image_sample_level(image, level, context)?;
2477
2478                if let Some(offset) = offset {
2479                    write!(self.out, ", ")?;
2480                    self.put_expression(offset, context, true)?;
2481                }
2482
2483                match gather {
2484                    None | Some(crate::SwizzleComponent::X) => {}
2485                    Some(component) => {
2486                        let is_cube_map = match *context.resolve_type(image) {
2487                            crate::TypeInner::Image {
2488                                dim: crate::ImageDimension::Cube,
2489                                ..
2490                            } => true,
2491                            _ => false,
2492                        };
2493                        // Offset always comes before the gather, except
2494                        // in cube maps where it's not applicable
2495                        if offset.is_none() && !is_cube_map {
2496                            write!(self.out, ", {NAMESPACE}::int2(0)")?;
2497                        }
2498                        let letter = back::COMPONENTS[component as usize];
2499                        write!(self.out, ", {NAMESPACE}::component::{letter}")?;
2500                    }
2501                }
2502                write!(self.out, ")")?;
2503            }
2504            crate::Expression::ImageLoad {
2505                image,
2506                coordinate,
2507                array_index,
2508                sample,
2509                level,
2510            } => {
2511                let address = TexelAddress {
2512                    coordinate,
2513                    array_index,
2514                    sample,
2515                    level: level.map(LevelOfDetail::Direct),
2516                };
2517                self.put_image_load(expr_handle, image, address, context)?;
2518            }
2519            //Note: for all the queries, the signed integers are expected,
2520            // so a conversion is needed.
2521            crate::Expression::ImageQuery { image, query } => match query {
2522                crate::ImageQuery::Size { level } => {
2523                    self.put_image_size_query(
2524                        image,
2525                        level.map(LevelOfDetail::Direct),
2526                        crate::ScalarKind::Uint,
2527                        context,
2528                    )?;
2529                }
2530                crate::ImageQuery::NumLevels => {
2531                    self.put_expression(image, context, false)?;
2532                    write!(self.out, ".get_num_mip_levels()")?;
2533                }
2534                crate::ImageQuery::NumLayers => {
2535                    self.put_expression(image, context, false)?;
2536                    write!(self.out, ".get_array_size()")?;
2537                }
2538                crate::ImageQuery::NumSamples => {
2539                    self.put_expression(image, context, false)?;
2540                    write!(self.out, ".get_num_samples()")?;
2541                }
2542            },
2543            crate::Expression::Unary { op, expr } => {
2544                let op_str = match op {
2545                    crate::UnaryOperator::Negate => {
2546                        match context.resolve_type(expr).scalar_kind() {
2547                            Some(crate::ScalarKind::Sint) => NEG_FUNCTION,
2548                            _ => "-",
2549                        }
2550                    }
2551                    crate::UnaryOperator::LogicalNot => "!",
2552                    crate::UnaryOperator::BitwiseNot => "~",
2553                };
2554                write!(self.out, "{op_str}(")?;
2555                self.put_expression(expr, context, false)?;
2556                write!(self.out, ")")?;
2557            }
2558            crate::Expression::Binary { op, left, right } => {
2559                let kind = context
2560                    .resolve_type(left)
2561                    .scalar_kind()
2562                    .ok_or(Error::UnsupportedBinaryOp(op))?;
2563
2564                if op == crate::BinaryOperator::Divide
2565                    && (kind == crate::ScalarKind::Sint || kind == crate::ScalarKind::Uint)
2566                    && context.emit_int_div_checks
2567                {
2568                    write!(self.out, "{DIV_FUNCTION}(")?;
2569                    self.put_expression(left, context, true)?;
2570                    write!(self.out, ", ")?;
2571                    self.put_expression(right, context, true)?;
2572                    write!(self.out, ")")?;
2573                } else if op == crate::BinaryOperator::Modulo
2574                    && (kind == crate::ScalarKind::Sint || kind == crate::ScalarKind::Uint)
2575                    && context.emit_int_div_checks
2576                {
2577                    write!(self.out, "{MOD_FUNCTION}(")?;
2578                    self.put_expression(left, context, true)?;
2579                    write!(self.out, ", ")?;
2580                    self.put_expression(right, context, true)?;
2581                    write!(self.out, ")")?;
2582                } else if op == crate::BinaryOperator::Modulo && kind == crate::ScalarKind::Float {
2583                    // TODO: handle undefined behavior of BinaryOperator::Modulo
2584                    //
2585                    // float:
2586                    // if right == 0 return ? see https://github.com/gpuweb/gpuweb/issues/2798
2587                    write!(self.out, "{NAMESPACE}::fmod(")?;
2588                    self.put_expression(left, context, true)?;
2589                    write!(self.out, ", ")?;
2590                    self.put_expression(right, context, true)?;
2591                    write!(self.out, ")")?;
2592                } else if (op == crate::BinaryOperator::Add
2593                    || op == crate::BinaryOperator::Subtract
2594                    || op == crate::BinaryOperator::Multiply)
2595                    && kind == crate::ScalarKind::Sint
2596                {
2597                    let to_unsigned = |ty: &crate::TypeInner| match *ty {
2598                        crate::TypeInner::Scalar(scalar) => {
2599                            Ok(crate::TypeInner::Scalar(crate::Scalar {
2600                                kind: crate::ScalarKind::Uint,
2601                                ..scalar
2602                            }))
2603                        }
2604                        crate::TypeInner::Vector { size, scalar } => Ok(crate::TypeInner::Vector {
2605                            size,
2606                            scalar: crate::Scalar {
2607                                kind: crate::ScalarKind::Uint,
2608                                ..scalar
2609                            },
2610                        }),
2611                        _ => Err(Error::UnsupportedBitCast(ty.clone())),
2612                    };
2613
2614                    // Avoid undefined behaviour due to overflowing signed
2615                    // integer arithmetic. Cast the operands to unsigned prior
2616                    // to performing the operation, then cast the result back
2617                    // to signed.
2618                    self.put_bitcasted_expression(
2619                        context.resolve_type(expr_handle),
2620                        expr_handle,
2621                        context,
2622                        &|writer, context, is_scoped| {
2623                            writer.put_binop(
2624                                op,
2625                                left,
2626                                right,
2627                                context,
2628                                is_scoped,
2629                                &|writer, expr, context, _is_scoped| {
2630                                    writer.put_bitcasted_expression(
2631                                        &to_unsigned(context.resolve_type(expr))?,
2632                                        expr,
2633                                        context,
2634                                        &|writer, context, is_scoped| {
2635                                            writer.put_expression(expr, context, is_scoped)
2636                                        },
2637                                    )
2638                                },
2639                            )
2640                        },
2641                    )?;
2642                } else {
2643                    self.put_binop(op, left, right, context, is_scoped, &Self::put_expression)?;
2644                }
2645            }
2646            crate::Expression::Select {
2647                condition,
2648                accept,
2649                reject,
2650            } => match *context.resolve_type(condition) {
2651                crate::TypeInner::Scalar(crate::Scalar {
2652                    kind: crate::ScalarKind::Bool,
2653                    ..
2654                }) => {
2655                    if !is_scoped {
2656                        write!(self.out, "(")?;
2657                    }
2658                    self.put_expression(condition, context, false)?;
2659                    write!(self.out, " ? ")?;
2660                    self.put_expression(accept, context, false)?;
2661                    write!(self.out, " : ")?;
2662                    self.put_expression(reject, context, false)?;
2663                    if !is_scoped {
2664                        write!(self.out, ")")?;
2665                    }
2666                }
2667                crate::TypeInner::Vector {
2668                    scalar:
2669                        crate::Scalar {
2670                            kind: crate::ScalarKind::Bool,
2671                            ..
2672                        },
2673                    ..
2674                } => {
2675                    write!(self.out, "{NAMESPACE}::select(")?;
2676                    self.put_expression(reject, context, true)?;
2677                    write!(self.out, ", ")?;
2678                    self.put_expression(accept, context, true)?;
2679                    write!(self.out, ", ")?;
2680                    self.put_expression(condition, context, true)?;
2681                    write!(self.out, ")")?;
2682                }
2683                ref ty => {
2684                    return Err(Error::GenericValidation(format!(
2685                        "Expected select condition to be a non-bool type, got {ty:?}",
2686                    )))
2687                }
2688            },
2689            crate::Expression::Derivative { axis, expr, .. } => {
2690                use crate::DerivativeAxis as Axis;
2691                let op = match axis {
2692                    Axis::X => "dfdx",
2693                    Axis::Y => "dfdy",
2694                    Axis::Width => "fwidth",
2695                };
2696                write!(self.out, "{NAMESPACE}::{op}")?;
2697                self.put_call_parameters(iter::once(expr), context)?;
2698            }
2699            crate::Expression::Relational { fun, argument } => {
2700                let op = match fun {
2701                    crate::RelationalFunction::Any => "any",
2702                    crate::RelationalFunction::All => "all",
2703                    crate::RelationalFunction::IsNan => "isnan",
2704                    crate::RelationalFunction::IsInf => "isinf",
2705                };
2706                write!(self.out, "{NAMESPACE}::{op}")?;
2707                self.put_call_parameters(iter::once(argument), context)?;
2708            }
2709            crate::Expression::Math {
2710                fun,
2711                arg,
2712                arg1,
2713                arg2,
2714                arg3,
2715            } => {
2716                use crate::MathFunction as Mf;
2717
2718                let arg_type = context.resolve_type(arg);
2719                let scalar_argument = match arg_type {
2720                    &crate::TypeInner::Scalar(_) => true,
2721                    _ => false,
2722                };
2723
2724                let fun_name = match fun {
2725                    // comparison
2726                    Mf::Abs => "abs",
2727                    Mf::Min => "min",
2728                    Mf::Max => "max",
2729                    Mf::Clamp => "clamp",
2730                    Mf::Saturate => "saturate",
2731                    // trigonometry
2732                    Mf::Cos => "cos",
2733                    Mf::Cosh => "cosh",
2734                    Mf::Sin => "sin",
2735                    Mf::Sinh => "sinh",
2736                    Mf::Tan => "tan",
2737                    Mf::Tanh => "tanh",
2738                    Mf::Acos => "acos",
2739                    Mf::Asin => "asin",
2740                    Mf::Atan => "atan",
2741                    Mf::Atan2 => "atan2",
2742                    Mf::Asinh => "asinh",
2743                    Mf::Acosh => "acosh",
2744                    Mf::Atanh => "atanh",
2745                    Mf::Radians => "",
2746                    Mf::Degrees => "",
2747                    // decomposition
2748                    Mf::Ceil => "ceil",
2749                    Mf::Floor => "floor",
2750                    Mf::Round => "rint",
2751                    Mf::Fract => "fract",
2752                    Mf::Trunc => "trunc",
2753                    Mf::Modf => MODF_FUNCTION,
2754                    Mf::Frexp => FREXP_FUNCTION,
2755                    Mf::Ldexp => "ldexp",
2756                    // exponent
2757                    Mf::Exp => "exp",
2758                    Mf::Exp2 => "exp2",
2759                    Mf::Log => "log",
2760                    Mf::Log2 => "log2",
2761                    Mf::Pow => "pow",
2762                    // geometry
2763                    Mf::Dot => match *context.resolve_type(arg) {
2764                        crate::TypeInner::Vector {
2765                            scalar:
2766                                crate::Scalar {
2767                                    // Resolve float values to MSL's builtin dot function.
2768                                    kind: crate::ScalarKind::Float,
2769                                    ..
2770                                },
2771                            ..
2772                        } => "dot",
2773                        crate::TypeInner::Vector {
2774                            size,
2775                            scalar:
2776                                scalar @ crate::Scalar {
2777                                    kind: crate::ScalarKind::Sint | crate::ScalarKind::Uint,
2778                                    ..
2779                                },
2780                        } => {
2781                            // Integer vector dot: call our mangled helper `dot_{type}{N}(a, b)`.
2782                            let fun_name = self.get_dot_wrapper_function_helper_name(scalar, size);
2783                            write!(self.out, "{fun_name}(")?;
2784                            self.put_expression(arg, context, true)?;
2785                            write!(self.out, ", ")?;
2786                            self.put_expression(arg1.unwrap(), context, true)?;
2787                            write!(self.out, ")")?;
2788                            return Ok(());
2789                        }
2790                        _ => unreachable!(
2791                            "Correct TypeInner for dot product should be already validated"
2792                        ),
2793                    },
2794                    fun @ (Mf::Dot4I8Packed | Mf::Dot4U8Packed) => {
2795                        if context.lang_version >= (2, 1) {
2796                            // Write potentially optimizable code using `packed_(u?)char4`.
2797                            // The two function arguments were already reinterpreted as packed (signed
2798                            // or unsigned) chars in `Self::put_block`.
2799                            let packed_type = match fun {
2800                                Mf::Dot4I8Packed => "packed_char4",
2801                                Mf::Dot4U8Packed => "packed_uchar4",
2802                                _ => unreachable!(),
2803                            };
2804
2805                            return self.put_dot_product(
2806                                Reinterpreted::new(packed_type, arg),
2807                                Reinterpreted::new(packed_type, arg1.unwrap()),
2808                                4,
2809                                |writer, arg, index| {
2810                                    // MSL implicitly promotes these (signed or unsigned) chars to
2811                                    // `int` or `uint` in the multiplication, so no overflow can occur.
2812                                    write!(writer.out, "{arg}[{index}]")?;
2813                                    Ok(())
2814                                },
2815                            );
2816                        } else {
2817                            // Fall back to a polyfill since MSL < 2.1 doesn't seem to support
2818                            // bitcasting from uint to `packed_char4` or `packed_uchar4`.
2819                            // See <https://github.com/gfx-rs/wgpu/pull/7574#issuecomment-2835464472>.
2820                            let conversion = match fun {
2821                                Mf::Dot4I8Packed => "int",
2822                                Mf::Dot4U8Packed => "",
2823                                _ => unreachable!(),
2824                            };
2825
2826                            return self.put_dot_product(
2827                                arg,
2828                                arg1.unwrap(),
2829                                4,
2830                                |writer, arg, index| {
2831                                    write!(writer.out, "({conversion}(")?;
2832                                    writer.put_expression(arg, context, true)?;
2833                                    if index == 3 {
2834                                        write!(writer.out, ") >> 24)")?;
2835                                    } else {
2836                                        write!(writer.out, ") << {} >> 24)", (3 - index) * 8)?;
2837                                    }
2838                                    Ok(())
2839                                },
2840                            );
2841                        }
2842                    }
2843                    Mf::Outer => return Err(Error::UnsupportedCall(format!("{fun:?}"))),
2844                    Mf::Cross => "cross",
2845                    Mf::Distance => "distance",
2846                    Mf::Length if scalar_argument => "abs",
2847                    Mf::Length => "length",
2848                    Mf::Normalize => "normalize",
2849                    Mf::FaceForward => "faceforward",
2850                    Mf::Reflect => "reflect",
2851                    Mf::Refract => "refract",
2852                    // computational
2853                    Mf::Sign => match arg_type.scalar_kind() {
2854                        Some(crate::ScalarKind::Sint) => {
2855                            return self.put_isign(arg, context);
2856                        }
2857                        _ => "sign",
2858                    },
2859                    Mf::Fma => "fma",
2860                    Mf::Mix => "mix",
2861                    Mf::Step => "step",
2862                    Mf::SmoothStep => "smoothstep",
2863                    Mf::Sqrt => "sqrt",
2864                    Mf::InverseSqrt => "rsqrt",
2865                    Mf::Inverse => return Err(Error::UnsupportedCall(format!("{fun:?}"))),
2866                    Mf::Transpose => "transpose",
2867                    Mf::Determinant => "determinant",
2868                    Mf::QuantizeToF16 => "",
2869                    // bits
2870                    Mf::CountTrailingZeros => "ctz",
2871                    Mf::CountLeadingZeros => "clz",
2872                    Mf::CountOneBits => "popcount",
2873                    Mf::ReverseBits => "reverse_bits",
2874                    Mf::ExtractBits => "",
2875                    Mf::InsertBits => "",
2876                    Mf::FirstTrailingBit => "",
2877                    Mf::FirstLeadingBit => "",
2878                    // data packing
2879                    Mf::Pack4x8snorm => "pack_float_to_snorm4x8",
2880                    Mf::Pack4x8unorm => "pack_float_to_unorm4x8",
2881                    Mf::Pack2x16snorm => "pack_float_to_snorm2x16",
2882                    Mf::Pack2x16unorm => "pack_float_to_unorm2x16",
2883                    Mf::Pack2x16float => "",
2884                    Mf::Pack4xI8 => "",
2885                    Mf::Pack4xU8 => "",
2886                    Mf::Pack4xI8Clamp => "",
2887                    Mf::Pack4xU8Clamp => "",
2888                    // data unpacking
2889                    Mf::Unpack4x8snorm => "unpack_snorm4x8_to_float",
2890                    Mf::Unpack4x8unorm => "unpack_unorm4x8_to_float",
2891                    Mf::Unpack2x16snorm => "unpack_snorm2x16_to_float",
2892                    Mf::Unpack2x16unorm => "unpack_unorm2x16_to_float",
2893                    Mf::Unpack2x16float => "",
2894                    Mf::Unpack4xI8 => "",
2895                    Mf::Unpack4xU8 => "",
2896                };
2897
2898                match fun {
2899                    // reverse_bits is listed as requiring MSL 2.1 but that
2900                    // is a copy/paste error. Looking at previous snapshots
2901                    // on web.archive.org it's present in MSL 1.2.
2902                    //
2903                    // https://developer.apple.com/library/archive/documentation/Miscellaneous/Conceptual/MetalProgrammingGuide/WhatsNewiniOS10tvOS10andOSX1012/WhatsNewiniOS10tvOS10andOSX1012.html
2904                    // also talks about MSL 1.2 adding "New integer
2905                    // functions to extract, insert, and reverse bits, as
2906                    // described in Integer Functions."
2907                    Mf::ReverseBits | Mf::ExtractBits | Mf::InsertBits
2908                        if context.lang_version < (1, 2) =>
2909                    {
2910                        return Err(Error::UnsupportedFunction(fun_name.to_string()));
2911                    }
2912                    _ => {}
2913                }
2914
2915                match fun {
2916                    Mf::Abs if arg_type.scalar_kind() == Some(crate::ScalarKind::Sint) => {
2917                        write!(self.out, "{ABS_FUNCTION}(")?;
2918                        self.put_expression(arg, context, true)?;
2919                        write!(self.out, ")")?;
2920                    }
2921                    Mf::Distance if scalar_argument => {
2922                        write!(self.out, "{NAMESPACE}::abs(")?;
2923                        self.put_expression(arg, context, false)?;
2924                        write!(self.out, " - ")?;
2925                        self.put_expression(arg1.unwrap(), context, false)?;
2926                        write!(self.out, ")")?;
2927                    }
2928                    Mf::FirstTrailingBit => {
2929                        let scalar = context.resolve_type(arg).scalar().unwrap();
2930                        let constant = scalar.width * 8 + 1;
2931
2932                        write!(self.out, "((({NAMESPACE}::ctz(")?;
2933                        self.put_expression(arg, context, true)?;
2934                        write!(self.out, ") + 1) % {constant}) - 1)")?;
2935                    }
2936                    Mf::FirstLeadingBit => {
2937                        let inner = context.resolve_type(arg);
2938                        let scalar = inner.scalar().unwrap();
2939                        let constant = scalar.width * 8 - 1;
2940
2941                        write!(
2942                            self.out,
2943                            "{NAMESPACE}::select({constant} - {NAMESPACE}::clz("
2944                        )?;
2945
2946                        if scalar.kind == crate::ScalarKind::Sint {
2947                            write!(self.out, "{NAMESPACE}::select(")?;
2948                            self.put_expression(arg, context, true)?;
2949                            write!(self.out, ", ~")?;
2950                            self.put_expression(arg, context, true)?;
2951                            write!(self.out, ", ")?;
2952                            self.put_expression(arg, context, true)?;
2953                            write!(self.out, " < 0)")?;
2954                        } else {
2955                            self.put_expression(arg, context, true)?;
2956                        }
2957
2958                        write!(self.out, "), ")?;
2959
2960                        // or metal will complain that select is ambiguous
2961                        match *inner {
2962                            crate::TypeInner::Vector { size, scalar } => {
2963                                let size = common::vector_size_str(size);
2964                                let name = scalar.to_msl_name();
2965                                write!(self.out, "{name}{size}")?;
2966                            }
2967                            crate::TypeInner::Scalar(scalar) => {
2968                                let name = scalar.to_msl_name();
2969                                write!(self.out, "{name}")?;
2970                            }
2971                            _ => (),
2972                        }
2973
2974                        write!(self.out, "(-1), ")?;
2975                        self.put_expression(arg, context, true)?;
2976                        write!(self.out, " == 0")?;
2977                        if scalar.kind == crate::ScalarKind::Sint {
2978                            write!(self.out, " || ")?;
2979                            self.put_expression(arg, context, true)?;
2980                            write!(self.out, " == -1")?;
2981                        }
2982                        write!(self.out, ")")?;
2983                    }
2984                    Mf::Unpack2x16float => {
2985                        write!(self.out, "float2(as_type<half2>(")?;
2986                        self.put_expression(arg, context, false)?;
2987                        write!(self.out, "))")?;
2988                    }
2989                    Mf::Pack2x16float => {
2990                        write!(self.out, "as_type<uint>(half2(")?;
2991                        self.put_expression(arg, context, false)?;
2992                        write!(self.out, "))")?;
2993                    }
2994                    Mf::ExtractBits => {
2995                        // The behavior of ExtractBits is undefined when offset + count > bit_width. We need
2996                        // to first sanitize the offset and count first. If we don't do this, Apple chips
2997                        // will return out-of-spec values if the extracted range is not within the bit width.
2998                        //
2999                        // This encodes the exact formula specified by the wgsl spec, without temporary values:
3000                        // https://gpuweb.github.io/gpuweb/wgsl/#extractBits-unsigned-builtin
3001                        //
3002                        // w = sizeof(x) * 8
3003                        // o = min(offset, w)
3004                        // tmp = w - o
3005                        // c = min(count, tmp)
3006                        //
3007                        // bitfieldExtract(x, o, c)
3008                        //
3009                        // extract_bits(e, min(offset, w), min(count, w - min(offset, w))))
3010
3011                        let scalar_bits = context.resolve_type(arg).scalar_width().unwrap() * 8;
3012
3013                        write!(self.out, "{NAMESPACE}::extract_bits(")?;
3014                        self.put_expression(arg, context, true)?;
3015                        write!(self.out, ", {NAMESPACE}::min(")?;
3016                        self.put_expression(arg1.unwrap(), context, true)?;
3017                        write!(self.out, ", {scalar_bits}u), {NAMESPACE}::min(")?;
3018                        self.put_expression(arg2.unwrap(), context, true)?;
3019                        write!(self.out, ", {scalar_bits}u - {NAMESPACE}::min(")?;
3020                        self.put_expression(arg1.unwrap(), context, true)?;
3021                        write!(self.out, ", {scalar_bits}u)))")?;
3022                    }
3023                    Mf::InsertBits => {
3024                        // The behavior of InsertBits has the same issue as ExtractBits.
3025                        //
3026                        // insertBits(e, newBits, min(offset, w), min(count, w - min(offset, w))))
3027
3028                        let scalar_bits = context.resolve_type(arg).scalar_width().unwrap() * 8;
3029
3030                        write!(self.out, "{NAMESPACE}::insert_bits(")?;
3031                        self.put_expression(arg, context, true)?;
3032                        write!(self.out, ", ")?;
3033                        self.put_expression(arg1.unwrap(), context, true)?;
3034                        write!(self.out, ", {NAMESPACE}::min(")?;
3035                        self.put_expression(arg2.unwrap(), context, true)?;
3036                        write!(self.out, ", {scalar_bits}u), {NAMESPACE}::min(")?;
3037                        self.put_expression(arg3.unwrap(), context, true)?;
3038                        write!(self.out, ", {scalar_bits}u - {NAMESPACE}::min(")?;
3039                        self.put_expression(arg2.unwrap(), context, true)?;
3040                        write!(self.out, ", {scalar_bits}u)))")?;
3041                    }
3042                    Mf::Radians => {
3043                        write!(self.out, "((")?;
3044                        self.put_expression(arg, context, false)?;
3045                        write!(self.out, ") * 0.017453292519943295474)")?;
3046                    }
3047                    Mf::Degrees => {
3048                        write!(self.out, "((")?;
3049                        self.put_expression(arg, context, false)?;
3050                        write!(self.out, ") * 57.295779513082322865)")?;
3051                    }
3052                    Mf::Modf | Mf::Frexp => {
3053                        write!(self.out, "{fun_name}")?;
3054                        self.put_call_parameters(iter::once(arg), context)?;
3055                    }
3056                    Mf::Pack4xI8 => self.put_pack4x8(arg, context, true, None)?,
3057                    Mf::Pack4xU8 => self.put_pack4x8(arg, context, false, None)?,
3058                    Mf::Pack4xI8Clamp => {
3059                        self.put_pack4x8(arg, context, true, Some(("-128", "127")))?
3060                    }
3061                    Mf::Pack4xU8Clamp => {
3062                        self.put_pack4x8(arg, context, false, Some(("0", "255")))?
3063                    }
3064                    fun @ (Mf::Unpack4xI8 | Mf::Unpack4xU8) => {
3065                        let sign_prefix = if matches!(fun, Mf::Unpack4xU8) {
3066                            "u"
3067                        } else {
3068                            ""
3069                        };
3070
3071                        if context.lang_version >= (2, 1) {
3072                            // Metal uses little endian byte order, which matches what WGSL expects here.
3073                            write!(
3074                                self.out,
3075                                "{sign_prefix}int4(as_type<packed_{sign_prefix}char4>("
3076                            )?;
3077                            self.put_expression(arg, context, true)?;
3078                            write!(self.out, "))")?;
3079                        } else {
3080                            // MSL < 2.1 doesn't support `as_type` casting between packed chars and scalars.
3081                            write!(self.out, "({sign_prefix}int4(")?;
3082                            self.put_expression(arg, context, true)?;
3083                            write!(self.out, ", ")?;
3084                            self.put_expression(arg, context, true)?;
3085                            write!(self.out, " >> 8, ")?;
3086                            self.put_expression(arg, context, true)?;
3087                            write!(self.out, " >> 16, ")?;
3088                            self.put_expression(arg, context, true)?;
3089                            write!(self.out, " >> 24) << 24 >> 24)")?;
3090                        }
3091                    }
3092                    Mf::QuantizeToF16 => {
3093                        match *context.resolve_type(arg) {
3094                            crate::TypeInner::Scalar { .. } => write!(self.out, "float(half(")?,
3095                            crate::TypeInner::Vector { size, .. } => write!(
3096                                self.out,
3097                                "{NAMESPACE}::float{size}({NAMESPACE}::half{size}(",
3098                                size = common::vector_size_str(size),
3099                            )?,
3100                            _ => unreachable!(
3101                                "Correct TypeInner for QuantizeToF16 should be already validated"
3102                            ),
3103                        };
3104
3105                        self.put_expression(arg, context, true)?;
3106                        write!(self.out, "))")?;
3107                    }
3108                    _ => {
3109                        write!(self.out, "{NAMESPACE}::{fun_name}")?;
3110                        self.put_call_parameters(
3111                            iter::once(arg).chain(arg1).chain(arg2).chain(arg3),
3112                            context,
3113                        )?;
3114                    }
3115                }
3116            }
3117            crate::Expression::As { expr, target } => match *context.resolve_type(expr) {
3118                crate::TypeInner::Scalar(src) | crate::TypeInner::Vector { scalar: src, .. } => {
3119                    if src.kind == crate::ScalarKind::Float
3120                        && (target.kind == crate::ScalarKind::Sint
3121                            || target.kind == crate::ScalarKind::Uint)
3122                    {
3123                        // Use helper functions for float to int casts in order to avoid
3124                        // undefined behaviour when value is out of range for the target
3125                        // type.
3126                        let fun_name = match (target.kind, target.width) {
3127                            (crate::ScalarKind::Sint, 4) => F2I32_FUNCTION,
3128                            (crate::ScalarKind::Uint, 4) => F2U32_FUNCTION,
3129                            (crate::ScalarKind::Sint, 8) => F2I64_FUNCTION,
3130                            (crate::ScalarKind::Uint, 8) => F2U64_FUNCTION,
3131                            _ => unreachable!(),
3132                        };
3133                        write!(self.out, "{fun_name}(")?;
3134                        self.put_expression(expr, context, true)?;
3135                        write!(self.out, ")")?;
3136                    } else {
3137                        write!(self.out, "static_cast<")?;
3138                        match *context.resolve_type(expr) {
3139                            crate::TypeInner::Vector { size, .. } => {
3140                                put_numeric_type(&mut self.out, target, &[size])?
3141                            }
3142                            _ => put_numeric_type(&mut self.out, target, &[])?,
3143                        };
3144                        write!(self.out, ">(")?;
3145                        self.put_expression(expr, context, true)?;
3146                        write!(self.out, ")")?;
3147                    }
3148                }
3149                crate::TypeInner::Matrix { columns, rows, .. } => {
3150                    put_numeric_type(&mut self.out, target, &[rows, columns])?;
3151                    write!(self.out, "(")?;
3152                    self.put_expression(expr, context, true)?;
3153                    write!(self.out, ")")?;
3154                }
3155                ref ty => {
3156                    return Err(Error::GenericValidation(format!(
3157                        "Unsupported type for As: {ty:?}"
3158                    )))
3159                }
3160            },
3161            crate::Expression::Bitcast { expr, ty } => {
3162                write!(self.out, "as_type<")?;
3163                match context.module.types[ty].inner {
3164                    crate::TypeInner::Scalar(scalar) => {
3165                        put_numeric_type(&mut self.out, scalar, &[])?
3166                    }
3167                    crate::TypeInner::Vector { size, scalar } => {
3168                        put_numeric_type(&mut self.out, scalar, &[size])?
3169                    }
3170                    ref other => {
3171                        return Err(Error::GenericValidation(format!(
3172                            "Unsupported type for Bitcast: {other:?}"
3173                        )))
3174                    }
3175                }
3176                write!(self.out, ">(")?;
3177                self.put_expression(expr, context, true)?;
3178                write!(self.out, ")")?;
3179            }
3180            // These have no inline form, so they are only ever read through
3181            // the temporary the statement that produced them stored them in,
3182            // which one of the two tables above names. The one place that is
3183            // not obviously true is a loop's `continuing` block, which we write
3184            // out above the body that defines what it reads; see the `hoist`
3185            // module for how those temporaries get declared in time.
3186            crate::Expression::CallResult(_)
3187            | crate::Expression::AtomicResult { .. }
3188            | crate::Expression::WorkGroupUniformLoadResult { .. }
3189            | crate::Expression::SubgroupBallotResult
3190            | crate::Expression::SubgroupOperationResult { .. }
3191            | crate::Expression::RayQueryProceedResult => {
3192                unreachable!("statement result {expr_handle:?} has no temporary holding it")
3193            }
3194            crate::Expression::ArrayLength(expr) => {
3195                let global = context.function.originating_global(expr).ok_or_else(|| {
3196                    Error::GenericValidation(format!(
3197                        "Could not find global variable for ArrayLength operand {:?}",
3198                        context.function.expressions[expr]
3199                    ))
3200                })?;
3201
3202                if !is_scoped {
3203                    write!(self.out, "(")?;
3204                }
3205                write!(self.out, "1 + ")?;
3206                self.put_dynamic_array_max_index(global, expr, context)?;
3207                if !is_scoped {
3208                    write!(self.out, ")")?;
3209                }
3210            }
3211            crate::Expression::RayQueryVertexPositions { .. }
3212            | crate::Expression::HitObjectQuery { .. } => {
3213                unimplemented!()
3214            }
3215            crate::Expression::RayQueryGetIntersection { query, committed } => {
3216                if context.lang_version < (2, 4) {
3217                    return Err(Error::UnsupportedRayTracing);
3218                }
3219
3220                // See comment in `write_ray_query_stmt` for why this is valid
3221                let crate::Expression::LocalVariable(query_var) =
3222                    context.function.expressions[query]
3223                else {
3224                    unreachable!()
3225                };
3226
3227                let tracker_expr_name = format!(
3228                    "{}{}",
3229                    super::ray::RAY_QUERY_TRACKER_VARIABLE_PREFIX,
3230                    self.names[&NameKey::local(context.origin, query_var)]
3231                );
3232
3233                write!(
3234                    self.out,
3235                    "{}_{committed}(",
3236                    super::ray::INTERSECTION_FUNCTION_NAME
3237                )?;
3238                self.put_expression(query, context, true)?;
3239                if context.ray_query_initialization_tracking {
3240                    write!(self.out, ", {tracker_expr_name}")?;
3241                }
3242                write!(self.out, ")")?;
3243            }
3244            crate::Expression::CooperativeLoad { ref data, .. } => {
3245                if context.lang_version < (2, 3) {
3246                    return Err(Error::UnsupportedCooperativeMatrix);
3247                }
3248                write!(self.out, "{COOPERATIVE_LOAD_FUNCTION}(")?;
3249                write!(self.out, "&")?;
3250                self.put_access_chain(data.pointer, context.policies.index, context)?;
3251                write!(self.out, ", ")?;
3252                self.put_expression(data.stride, context, true)?;
3253                // Metal's `simdgroup_load` treats its `transpose` flag as
3254                // "memory is transposed from the simdgroup_matrix's canonical
3255                // layout". On Apple GPUs that canonical layout is row-major,
3256                // so `transpose=false` loads from row-major memory. WGSL's
3257                // `coopLoadT` (row_major=true) = row-major memory, so it must
3258                // map to `transpose=false`. Hence the negation.
3259                write!(self.out, ", {})", !data.row_major)?;
3260            }
3261            crate::Expression::CooperativeMultiplyAdd { a, b, c } => {
3262                if context.lang_version < (2, 3) {
3263                    return Err(Error::UnsupportedCooperativeMatrix);
3264                }
3265                write!(self.out, "{COOPERATIVE_MULTIPLY_ADD_FUNCTION}(")?;
3266                self.put_expression(a, context, true)?;
3267                write!(self.out, ", ")?;
3268                self.put_expression(b, context, true)?;
3269                write!(self.out, ", ")?;
3270                self.put_expression(c, context, true)?;
3271                write!(self.out, ")")?;
3272            }
3273        }
3274        Ok(())
3275    }
3276
3277    /// Emits code for a binary operation, using the provided callback to emit
3278    /// the left and right operands.
3279    fn put_binop<F>(
3280        &mut self,
3281        op: crate::BinaryOperator,
3282        left: Handle<crate::Expression>,
3283        right: Handle<crate::Expression>,
3284        context: &ExpressionContext,
3285        is_scoped: bool,
3286        put_expression: &F,
3287    ) -> BackendResult
3288    where
3289        F: Fn(&mut Self, Handle<crate::Expression>, &ExpressionContext, bool) -> BackendResult,
3290    {
3291        let op_str = back::binary_operation_str(op);
3292
3293        if !is_scoped {
3294            write!(self.out, "(")?;
3295        }
3296
3297        // Cast packed vector if necessary
3298        // Packed vector - matrix multiplications are not supported in MSL
3299        if op == crate::BinaryOperator::Multiply
3300            && matches!(
3301                context.resolve_type(right),
3302                &crate::TypeInner::Matrix { .. }
3303            )
3304        {
3305            self.put_wrapped_expression_for_packed_vec3_access(
3306                left,
3307                context,
3308                false,
3309                put_expression,
3310            )?;
3311        } else {
3312            put_expression(self, left, context, false)?;
3313        }
3314
3315        write!(self.out, " {op_str} ")?;
3316
3317        // See comment above
3318        if op == crate::BinaryOperator::Multiply
3319            && matches!(context.resolve_type(left), &crate::TypeInner::Matrix { .. })
3320        {
3321            self.put_wrapped_expression_for_packed_vec3_access(
3322                right,
3323                context,
3324                false,
3325                put_expression,
3326            )?;
3327        } else {
3328            put_expression(self, right, context, false)?;
3329        }
3330
3331        if !is_scoped {
3332            write!(self.out, ")")?;
3333        }
3334
3335        Ok(())
3336    }
3337
3338    /// Used by expressions like Swizzle and Binary since they need packed_vec3's to be casted to a vec3
3339    fn put_wrapped_expression_for_packed_vec3_access<F>(
3340        &mut self,
3341        expr_handle: Handle<crate::Expression>,
3342        context: &ExpressionContext,
3343        is_scoped: bool,
3344        put_expression: &F,
3345    ) -> BackendResult
3346    where
3347        F: Fn(&mut Self, Handle<crate::Expression>, &ExpressionContext, bool) -> BackendResult,
3348    {
3349        if let Some(scalar) = context.get_packed_vec_kind(expr_handle) {
3350            write!(self.out, "{}::{}3(", NAMESPACE, scalar.to_msl_name())?;
3351            put_expression(self, expr_handle, context, is_scoped)?;
3352            write!(self.out, ")")?;
3353        } else {
3354            put_expression(self, expr_handle, context, is_scoped)?;
3355        }
3356        Ok(())
3357    }
3358
3359    /// Emits code for an expression using the provided callback, wrapping the
3360    /// result in a bitcast to the type `cast_to`.
3361    fn put_bitcasted_expression<F>(
3362        &mut self,
3363        cast_to: &crate::TypeInner,
3364        inner_expr: Handle<crate::Expression>,
3365        context: &ExpressionContext,
3366        put_expression: &F,
3367    ) -> BackendResult
3368    where
3369        F: Fn(&mut Self, &ExpressionContext, bool) -> BackendResult,
3370    {
3371        // For sub-32-bit types, C++ integer promotion can widen the inner
3372        // expression (e.g. `ushort + ushort` promotes to `int`), making a
3373        // direct `as_type<short>(int_expr)` invalid due to size mismatch.
3374        // We wrap with `static_cast` to truncate back before the bitcast.
3375        let needs_truncation = match *cast_to {
3376            crate::TypeInner::Scalar(scalar) => scalar.width < 4,
3377            crate::TypeInner::Vector { scalar, .. } => scalar.width < 4,
3378            _ => false,
3379        };
3380
3381        write!(self.out, "as_type<")?;
3382        match *cast_to {
3383            crate::TypeInner::Scalar(scalar) => put_numeric_type(&mut self.out, scalar, &[])?,
3384            crate::TypeInner::Vector { size, scalar } => {
3385                put_numeric_type(&mut self.out, scalar, &[size])?
3386            }
3387            _ => return Err(Error::UnsupportedBitCast(cast_to.clone())),
3388        };
3389        write!(self.out, ">(")?;
3390
3391        if needs_truncation {
3392            write!(self.out, "static_cast<")?;
3393            // Cast to the unsigned version of the target type to truncate
3394            let unsigned_scalar = match *cast_to {
3395                crate::TypeInner::Scalar(scalar) => crate::Scalar {
3396                    kind: crate::ScalarKind::Uint,
3397                    ..scalar
3398                },
3399                crate::TypeInner::Vector { scalar, .. } => crate::Scalar {
3400                    kind: crate::ScalarKind::Uint,
3401                    ..scalar
3402                },
3403                _ => unreachable!(),
3404            };
3405            match *cast_to {
3406                crate::TypeInner::Scalar(_) => {
3407                    put_numeric_type(&mut self.out, unsigned_scalar, &[])?
3408                }
3409                crate::TypeInner::Vector { size, .. } => {
3410                    put_numeric_type(&mut self.out, unsigned_scalar, &[size])?
3411                }
3412                _ => unreachable!(),
3413            };
3414            write!(self.out, ">(")?;
3415        }
3416
3417        // if it's packed, we must unpack it (e.g., float3(val)) before the bitcast.
3418        if let Some(scalar) = context.get_packed_vec_kind(inner_expr) {
3419            put_numeric_type(&mut self.out, scalar, &[crate::VectorSize::Tri])?;
3420            write!(self.out, "(")?;
3421            put_expression(self, context, true)?;
3422            write!(self.out, ")")?;
3423        } else {
3424            put_expression(self, context, true)?;
3425        }
3426
3427        if needs_truncation {
3428            write!(self.out, ")")?;
3429        }
3430
3431        write!(self.out, ")")?;
3432        Ok(())
3433    }
3434
3435    /// Write a `GuardedIndex` as a Metal expression.
3436    fn put_index(
3437        &mut self,
3438        index: index::GuardedIndex,
3439        context: &ExpressionContext,
3440        is_scoped: bool,
3441    ) -> BackendResult {
3442        match index {
3443            index::GuardedIndex::Expression(expr) => {
3444                self.put_expression(expr, context, is_scoped)?
3445            }
3446            index::GuardedIndex::Known(value) => write!(self.out, "{value}")?,
3447        }
3448        Ok(())
3449    }
3450
3451    /// Emit an index bounds check condition for `chain`, if required.
3452    ///
3453    /// `chain` is a subtree of `Access` and `AccessIndex` expressions,
3454    /// operating either on a pointer to a value, or on a value directly. If we cannot
3455    /// statically determine that all indexing operations in `chain` are within
3456    /// bounds, then write a conditional expression to check them dynamically,
3457    /// and return true. All accesses in the chain are checked by the generated
3458    /// expression.
3459    ///
3460    /// This assumes that the [`BoundsCheckPolicy`] for `chain` is [`ReadZeroSkipWrite`].
3461    ///
3462    /// The text written is of the form:
3463    ///
3464    /// ```ignore
3465    /// {level}{prefix}uint(i) < 4 && uint(j) < 10
3466    /// ```
3467    ///
3468    /// where `{level}` and `{prefix}` are the arguments to this function. For [`Store`]
3469    /// statements, presumably these arguments start an indented `if` statement; for
3470    /// [`Load`] expressions, the caller is probably building up a ternary `?:`
3471    /// expression. In either case, what is written is not a complete syntactic structure
3472    /// in its own right, and the caller will have to finish it off if we return `true`.
3473    ///
3474    /// If no expression is written, return false.
3475    ///
3476    /// [`BoundsCheckPolicy`]: index::BoundsCheckPolicy
3477    /// [`ReadZeroSkipWrite`]: index::BoundsCheckPolicy::ReadZeroSkipWrite
3478    /// [`Store`]: crate::Statement::Store
3479    /// [`Load`]: crate::Expression::Load
3480    fn put_bounds_checks(
3481        &mut self,
3482        chain: Handle<crate::Expression>,
3483        context: &ExpressionContext,
3484        level: back::Level,
3485        prefix: &'static str,
3486    ) -> Result<bool, Error> {
3487        let mut check_written = false;
3488
3489        // Iterate over the access chain, handling each required bounds check.
3490        for item in context.bounds_check_iter(chain) {
3491            let BoundsCheck {
3492                base,
3493                index,
3494                length,
3495            } = item;
3496
3497            if check_written {
3498                write!(self.out, " && ")?;
3499            } else {
3500                write!(self.out, "{level}{prefix}")?;
3501                check_written = true;
3502            }
3503
3504            // Check that the index falls within bounds. Do this with a single
3505            // comparison, by casting the index to `uint` first, so that negative
3506            // indices become large positive values.
3507            write!(self.out, "uint(")?;
3508            self.put_index(index, context, true)?;
3509            self.out.write_str(") < ")?;
3510            match length {
3511                index::IndexableLength::Known(value) => write!(self.out, "{value}")?,
3512                index::IndexableLength::Dynamic => {
3513                    let global = context.function.originating_global(base).ok_or_else(|| {
3514                        Error::GenericValidation("Could not find originating global".into())
3515                    })?;
3516                    if matches!(
3517                        context.module.types[context.module.global_variables[global].ty].inner,
3518                        crate::TypeInner::BindingArray { .. }
3519                    ) {
3520                        write!(
3521                            self.out,
3522                            "{} && _buffer_sizes.{}[",
3523                            Self::binding_array_layout_count(
3524                                context.module,
3525                                context.pipeline_options,
3526                                global,
3527                            ),
3528                            ArraySizeMember(global),
3529                        )?;
3530                        self.put_binding_array_size_member_index(index, context)?;
3531                        write!(self.out, "] != 0u")?;
3532                    } else {
3533                        write!(self.out, "1 + ")?;
3534                        self.put_dynamic_array_max_index(global, base, context)?
3535                    }
3536                }
3537            }
3538        }
3539
3540        Ok(check_written)
3541    }
3542
3543    /// Write the access chain `chain`.
3544    ///
3545    /// `chain` is a subtree of [`Access`] and [`AccessIndex`] expressions,
3546    /// operating either on a pointer to a value, or on a value directly.
3547    ///
3548    /// Generate bounds checks code only if `policy` is [`Restrict`]. The
3549    /// [`ReadZeroSkipWrite`] policy requires checks before any accesses take place, so
3550    /// that must be handled in the caller.
3551    ///
3552    /// Handle the entire chain, recursing back into `put_expression` only for index
3553    /// expressions and the base expression that originates the pointer or composite value
3554    /// being accessed. This allows `put_expression` to assume that any `Access` or
3555    /// `AccessIndex` expressions it sees are the top of a chain, so it can emit
3556    /// `ReadZeroSkipWrite` checks.
3557    ///
3558    /// [`Access`]: crate::Expression::Access
3559    /// [`AccessIndex`]: crate::Expression::AccessIndex
3560    /// [`Restrict`]: crate::proc::index::BoundsCheckPolicy::Restrict
3561    /// [`ReadZeroSkipWrite`]: crate::proc::index::BoundsCheckPolicy::ReadZeroSkipWrite
3562    fn put_access_chain(
3563        &mut self,
3564        chain: Handle<crate::Expression>,
3565        policy: index::BoundsCheckPolicy,
3566        context: &ExpressionContext,
3567    ) -> BackendResult {
3568        match context.function.expressions[chain] {
3569            crate::Expression::Access { base, index } => {
3570                let mut base_ty = context.resolve_type(base);
3571
3572                // Look through any pointers to see what we're really indexing.
3573                if let crate::TypeInner::Pointer { base, space: _ } = *base_ty {
3574                    base_ty = &context.module.types[base].inner;
3575                }
3576
3577                self.put_subscripted_access_chain(
3578                    base,
3579                    base_ty,
3580                    index::GuardedIndex::Expression(index),
3581                    policy,
3582                    context,
3583                )?;
3584            }
3585            crate::Expression::AccessIndex { base, index } => {
3586                let base_resolution = &context.info[base].ty;
3587                let mut base_ty = base_resolution.inner_with(&context.module.types);
3588                let mut base_ty_handle = base_resolution.handle();
3589
3590                // Look through any pointers to see what we're really indexing.
3591                if let crate::TypeInner::Pointer { base, space: _ } = *base_ty {
3592                    base_ty = &context.module.types[base].inner;
3593                    base_ty_handle = Some(base);
3594                }
3595
3596                // Handle structs and anything else that can use `.x` syntax here, so
3597                // `put_subscripted_access_chain` won't have to handle the absurd case of
3598                // indexing a struct with an expression.
3599                match *base_ty {
3600                    crate::TypeInner::Struct { .. } => {
3601                        let base_ty = base_ty_handle.unwrap();
3602                        self.put_access_chain(base, policy, context)?;
3603                        let name = &self.names[&NameKey::StructMember(base_ty, index)];
3604                        write!(
3605                            self.out,
3606                            "{}{name}",
3607                            if context.struct_member_needs_arrow(base, |ty| {
3608                                matches!(ty, crate::TypeInner::BindingArray { .. })
3609                            }) {
3610                                "->"
3611                            } else {
3612                                "."
3613                            },
3614                        )?;
3615                    }
3616                    crate::TypeInner::ValuePointer { .. } | crate::TypeInner::Vector { .. } => {
3617                        self.put_access_chain(base, policy, context)?;
3618                        // Prior to Metal v2.1 component access for packed vectors wasn't available
3619                        // however array indexing is
3620                        if context.get_packed_vec_kind(base).is_some() {
3621                            write!(self.out, "[{index}]")?;
3622                        } else {
3623                            write!(self.out, ".{}", back::COMPONENTS[index as usize])?;
3624                        }
3625                    }
3626                    _ => {
3627                        self.put_subscripted_access_chain(
3628                            base,
3629                            base_ty,
3630                            index::GuardedIndex::Known(index),
3631                            policy,
3632                            context,
3633                        )?;
3634                    }
3635                }
3636            }
3637            _ => self.put_expression(chain, context, false)?,
3638        }
3639
3640        Ok(())
3641    }
3642
3643    /// Write a `[]`-style access of `base` by `index`.
3644    ///
3645    /// If `policy` is [`Restrict`], then generate code as needed to force all index
3646    /// values within bounds.
3647    ///
3648    /// The `base_ty` argument must be the type we are actually indexing, like [`Array`] or
3649    /// [`Vector`]. In other words, it's `base`'s type with any surrounding [`Pointer`]
3650    /// removed. Our callers often already have this handy.
3651    ///
3652    /// This only emits `[]` expressions; it doesn't handle struct member accesses or
3653    /// referencing vector components by name.
3654    ///
3655    /// [`Restrict`]: crate::proc::index::BoundsCheckPolicy::Restrict
3656    /// [`Array`]: crate::TypeInner::Array
3657    /// [`Vector`]: crate::TypeInner::Vector
3658    /// [`Pointer`]: crate::TypeInner::Pointer
3659    fn put_subscripted_access_chain(
3660        &mut self,
3661        base: Handle<crate::Expression>,
3662        base_ty: &crate::TypeInner,
3663        index: index::GuardedIndex,
3664        policy: index::BoundsCheckPolicy,
3665        context: &ExpressionContext,
3666    ) -> BackendResult {
3667        let accessing_wrapped_array = match *base_ty {
3668            crate::TypeInner::Array {
3669                size: crate::ArraySize::Constant(_) | crate::ArraySize::Pending(_),
3670                ..
3671            } => true,
3672            _ => false,
3673        };
3674        let accessing_wrapped_binding_array =
3675            matches!(*base_ty, crate::TypeInner::BindingArray { .. });
3676
3677        self.put_access_chain(base, policy, context)?;
3678        if accessing_wrapped_array {
3679            write!(self.out, ".{WRAPPED_ARRAY_FIELD}")?;
3680        }
3681        write!(self.out, "[")?;
3682
3683        // Decide whether this index needs to be clamped to fall within range.
3684        let restriction_needed = if policy == index::BoundsCheckPolicy::Restrict {
3685            context.access_needs_check(base, index)
3686        } else {
3687            None
3688        };
3689        if let Some(limit) = restriction_needed {
3690            write!(self.out, "{NAMESPACE}::min(unsigned(")?;
3691            self.put_index(index, context, true)?;
3692            write!(self.out, "), ")?;
3693            match limit {
3694                index::IndexableLength::Known(limit) => {
3695                    write!(self.out, "{}u", limit - 1)?;
3696                }
3697                index::IndexableLength::Dynamic => {
3698                    let global = context.function.originating_global(base).ok_or_else(|| {
3699                        Error::GenericValidation("Could not find originating global".into())
3700                    })?;
3701                    self.put_dynamic_array_max_index(global, base, context)?;
3702                }
3703            }
3704            write!(self.out, ")")?;
3705        } else {
3706            self.put_index(index, context, true)?;
3707        }
3708
3709        write!(self.out, "]")?;
3710
3711        if accessing_wrapped_binding_array {
3712            write!(self.out, ".{WRAPPED_ARRAY_FIELD}")?;
3713        }
3714
3715        Ok(())
3716    }
3717
3718    fn put_load(
3719        &mut self,
3720        pointer: Handle<crate::Expression>,
3721        context: &ExpressionContext,
3722        is_scoped: bool,
3723    ) -> BackendResult {
3724        // Since access chains never cross between address spaces, we can just
3725        // check the index bounds check policy once at the top.
3726        let policy = context.choose_bounds_check_policy(pointer);
3727        if policy == index::BoundsCheckPolicy::ReadZeroSkipWrite
3728            && self.put_bounds_checks(
3729                pointer,
3730                context,
3731                back::Level(0),
3732                if is_scoped { "" } else { "(" },
3733            )?
3734        {
3735            write!(self.out, " ? ")?;
3736            self.put_unchecked_load(pointer, policy, context)?;
3737            write!(self.out, " : DefaultConstructible()")?;
3738
3739            if !is_scoped {
3740                write!(self.out, ")")?;
3741            }
3742        } else {
3743            self.put_unchecked_load(pointer, policy, context)?;
3744        }
3745
3746        Ok(())
3747    }
3748
3749    fn put_unchecked_load(
3750        &mut self,
3751        pointer: Handle<crate::Expression>,
3752        policy: index::BoundsCheckPolicy,
3753        context: &ExpressionContext,
3754    ) -> BackendResult {
3755        let is_atomic_pointer = context
3756            .resolve_type(pointer)
3757            .is_atomic_pointer(&context.module.types);
3758
3759        if is_atomic_pointer {
3760            write!(
3761                self.out,
3762                "{NAMESPACE}::atomic_load_explicit({ATOMIC_REFERENCE}"
3763            )?;
3764            self.put_access_chain(pointer, policy, context)?;
3765            write!(self.out, ", {NAMESPACE}::memory_order_relaxed)")?;
3766        } else {
3767            // We don't do any dereferencing with `*` here as pointer arguments to functions
3768            // are done by `&` references and not `*` pointers. These do not need to be
3769            // dereferenced.
3770            self.put_access_chain(pointer, policy, context)?;
3771        }
3772
3773        Ok(())
3774    }
3775
3776    fn put_return_value(
3777        &mut self,
3778        level: back::Level,
3779        expr_handle: Handle<crate::Expression>,
3780        result_struct: Option<&str>,
3781        context: &ExpressionContext,
3782    ) -> BackendResult {
3783        let entry_point = match context.origin {
3784            FunctionOrigin::EntryPoint(index) => Some(&context.module.entry_points[index as usize]),
3785            FunctionOrigin::Handle(_) => None,
3786        };
3787        if entry_point.is_some_and(|ep| only_writes_discarded_depth(context.module, ep)) {
3788            writeln!(self.out, "{level}return;")?;
3789            return Ok(());
3790        }
3791
3792        match result_struct {
3793            Some(struct_name) => {
3794                let mut has_point_size = false;
3795                let result_ty = context.function.result.as_ref().unwrap().ty;
3796                match context.module.types[result_ty].inner {
3797                    crate::TypeInner::Struct { ref members, .. } => {
3798                        let tmp = self.namer.call("_tmp");
3799                        write!(self.out, "{level}const auto {tmp} = ")?;
3800                        self.put_expression(expr_handle, context, true)?;
3801                        writeln!(self.out, ";")?;
3802                        write!(self.out, "{level}return {struct_name} {{")?;
3803
3804                        let mut is_first = true;
3805
3806                        for (index, member) in members.iter().enumerate() {
3807                            if let Some(crate::Binding::BuiltIn(crate::BuiltIn::PointSize)) =
3808                                member.binding
3809                            {
3810                                has_point_size = true;
3811                                if !context.pipeline_options.allow_and_force_point_size {
3812                                    continue;
3813                                }
3814                            }
3815
3816                            if entry_point.is_some_and(discards_frag_depth)
3817                                && is_frag_depth(member.binding.as_ref())
3818                            {
3819                                continue;
3820                            }
3821
3822                            let comma = if is_first { "" } else { "," };
3823                            is_first = false;
3824                            let name = &self.names[&NameKey::StructMember(result_ty, index as u32)];
3825                            // HACK: we are forcefully deduplicating the expression here
3826                            // to convert from a wrapped struct to a raw array, e.g.
3827                            // `float gl_ClipDistance1 [[clip_distance]] [1];`.
3828                            if let crate::TypeInner::Array {
3829                                size: crate::ArraySize::Constant(size),
3830                                ..
3831                            } = context.module.types[member.ty].inner
3832                            {
3833                                write!(self.out, "{comma} {{")?;
3834                                for j in 0..size.get() {
3835                                    if j != 0 {
3836                                        write!(self.out, ",")?;
3837                                    }
3838                                    write!(self.out, "{tmp}.{name}.{WRAPPED_ARRAY_FIELD}[{j}]")?;
3839                                }
3840                                write!(self.out, "}}")?;
3841                            } else {
3842                                write!(self.out, "{comma} {tmp}.{name}")?;
3843                            }
3844                        }
3845                    }
3846                    _ => {
3847                        write!(self.out, "{level}return {struct_name} {{ ")?;
3848                        self.put_expression(expr_handle, context, true)?;
3849                    }
3850                }
3851
3852                if let FunctionOrigin::EntryPoint(ep_index) = context.origin {
3853                    let stage = context.module.entry_points[ep_index as usize].stage;
3854                    if context.pipeline_options.allow_and_force_point_size
3855                        && stage == crate::ShaderStage::Vertex
3856                        && !has_point_size
3857                    {
3858                        // point size was injected and comes last
3859                        write!(self.out, ", 1.0")?;
3860                    }
3861                }
3862                write!(self.out, " }}")?;
3863            }
3864            None => {
3865                write!(self.out, "{level}return ")?;
3866                self.put_expression(expr_handle, context, true)?;
3867            }
3868        }
3869        writeln!(self.out, ";")?;
3870        Ok(())
3871    }
3872
3873    /// Helper method used to find which expressions of a given function require baking
3874    ///
3875    /// # Notes
3876    /// This function overwrites the contents of `self.need_bake_expressions`
3877    fn update_expressions_to_bake(
3878        &mut self,
3879        func: &crate::Function,
3880        info: &valid::FunctionInfo,
3881        context: &ExpressionContext,
3882    ) {
3883        use crate::Expression;
3884        self.need_bake_expressions.clear();
3885
3886        for (expr_handle, expr) in func.expressions.iter() {
3887            // Expressions whose reference count is above the
3888            // threshold should always be stored in temporaries.
3889            let expr_info = &info[expr_handle];
3890            let min_ref_count = func.expressions[expr_handle].bake_ref_count();
3891            if min_ref_count <= expr_info.ref_count {
3892                self.need_bake_expressions.insert(expr_handle);
3893            } else {
3894                match expr_info.ty {
3895                    // force ray desc to be baked: it's used multiple times internally
3896                    TypeResolution::Handle(h)
3897                        if Some(h) == context.module.special_types.ray_desc =>
3898                    {
3899                        self.need_bake_expressions.insert(expr_handle);
3900                    }
3901                    _ => {}
3902                }
3903            }
3904
3905            if let Expression::Math {
3906                fun,
3907                arg,
3908                arg1,
3909                arg2,
3910                ..
3911            } = *expr
3912            {
3913                match fun {
3914                    // WGSL's `dot` function works on any `vecN` type, but Metal's only
3915                    // works on floating-point vectors, so we emit inline code for
3916                    // integer vector `dot` calls. But that code uses each argument `N`
3917                    // times, once for each component (see `put_dot_product`), so to
3918                    // avoid duplicated evaluation, we must bake integer operands.
3919                    // This applies both when using the polyfill (because of the duplicate
3920                    // evaluation issue) and when we don't use the polyfill (because we
3921                    // need them to be emitted before casting to packed chars -- see the
3922                    // comment at the call to `put_casting_to_packed_chars`).
3923                    crate::MathFunction::Dot4U8Packed | crate::MathFunction::Dot4I8Packed => {
3924                        self.need_bake_expressions.insert(arg);
3925                        self.need_bake_expressions.insert(arg1.unwrap());
3926                    }
3927                    crate::MathFunction::FirstLeadingBit => {
3928                        self.need_bake_expressions.insert(arg);
3929                    }
3930                    // On MSL < 2.1, we emit a polyfill for these functions that uses the
3931                    // argument multiple times. This is no longer necessary on MSL >= 2.1.
3932                    crate::MathFunction::Pack4xI8
3933                    | crate::MathFunction::Pack4xU8
3934                    | crate::MathFunction::Pack4xI8Clamp
3935                    | crate::MathFunction::Pack4xU8Clamp
3936                    | crate::MathFunction::Unpack4xI8
3937                    | crate::MathFunction::Unpack4xU8
3938                        if context.lang_version < (2, 1) =>
3939                    {
3940                        self.need_bake_expressions.insert(arg);
3941                    }
3942                    crate::MathFunction::ExtractBits => {
3943                        // Only argument 1 is re-used.
3944                        self.need_bake_expressions.insert(arg1.unwrap());
3945                    }
3946                    crate::MathFunction::InsertBits => {
3947                        // Only argument 2 is re-used.
3948                        self.need_bake_expressions.insert(arg2.unwrap());
3949                    }
3950                    crate::MathFunction::Sign => {
3951                        // WGSL's `sign` function works also on signed ints, but Metal's only
3952                        // works on floating points, so we emit inline code for integer `sign`
3953                        // calls. But that code uses each argument 2 times (see `put_isign`),
3954                        // so to avoid duplicated evaluation, we must bake the argument.
3955                        let inner = context.resolve_type(expr_handle);
3956                        if inner.scalar_kind() == Some(crate::ScalarKind::Sint) {
3957                            self.need_bake_expressions.insert(arg);
3958                        }
3959                    }
3960                    _ => {}
3961                }
3962            }
3963        }
3964    }
3965
3966    /// Write the type of `handle`'s value, as used when declaring a temporary
3967    /// to hold it.
3968    pub(super) fn put_baked_type(
3969        &mut self,
3970        handle: Handle<crate::Expression>,
3971        context: &ExpressionContext,
3972    ) -> BackendResult {
3973        match context.info[handle].ty {
3974            TypeResolution::Handle(ty_handle) => {
3975                let ty_name = TypeContext {
3976                    handle: ty_handle,
3977                    gctx: context.module.to_ctx(),
3978                    names: &self.names,
3979                    access: crate::StorageAccess::empty(),
3980                    first_time: false,
3981                };
3982                write!(self.out, "{ty_name}")?;
3983            }
3984            TypeResolution::Value(crate::TypeInner::Scalar(scalar)) => {
3985                put_numeric_type(&mut self.out, scalar, &[])?;
3986            }
3987            TypeResolution::Value(crate::TypeInner::Vector { size, scalar }) => {
3988                put_numeric_type(&mut self.out, scalar, &[size])?;
3989            }
3990            TypeResolution::Value(crate::TypeInner::Matrix {
3991                columns,
3992                rows,
3993                scalar,
3994            }) => {
3995                put_numeric_type(&mut self.out, scalar, &[rows, columns])?;
3996            }
3997            TypeResolution::Value(crate::TypeInner::CooperativeMatrix {
3998                columns,
3999                rows,
4000                scalar,
4001                role: _,
4002            }) => {
4003                write!(
4004                    self.out,
4005                    "{}::simdgroup_{}{}x{}",
4006                    NAMESPACE,
4007                    scalar.to_msl_name(),
4008                    columns as u32,
4009                    rows as u32,
4010                )?;
4011            }
4012            TypeResolution::Value(ref other) => {
4013                log::warn!("Type {other:?} isn't a known local");
4014                return Err(Error::FeatureNotImplemented("weird local type".to_string()));
4015            }
4016        }
4017
4018        Ok(())
4019    }
4020
4021    /// Declare a temporary named `name` to hold `handle`'s value, writing
4022    /// everything up to and including the `=`.
4023    ///
4024    /// `name` must be unique within the function. Take it from either
4025    /// [`Baked`], which derives a name from the expression's handle, or
4026    /// [`Self::namer`], which tracks the names it hands out.
4027    pub(super) fn start_baking_expression(
4028        &mut self,
4029        handle: Handle<crate::Expression>,
4030        context: &ExpressionContext,
4031        name: &str,
4032    ) -> BackendResult {
4033        self.put_baked_type(handle, context)?;
4034        write!(self.out, " {name} = ")?;
4035
4036        Ok(())
4037    }
4038
4039    /// Write the left-hand side of the assignment that stores a statement's
4040    /// `result`, and register the name of the temporary holding it in
4041    /// [`Self::named_expressions`].
4042    ///
4043    /// Normally this declares the temporary. If it was instead declared ahead
4044    /// of an enclosing loop by [`Self::hoist_continuing_dependencies`], we only
4045    /// assign to it, and move it from [`Self::hoisted_loop_expressions`] to
4046    /// [`Self::named_expressions`] now that it has a value.
4047    ///
4048    /// Returns the name, for statements that need to refer to it again.
4049    pub(super) fn write_result_assignment(
4050        &mut self,
4051        result: Handle<crate::Expression>,
4052        context: &ExpressionContext,
4053    ) -> Result<String, Error> {
4054        let name = match self.hoisted_loop_expressions.remove(&result) {
4055            Some(hoisted) => {
4056                write!(self.out, "{hoisted} = ")?;
4057                hoisted
4058            }
4059            None => {
4060                let name = Baked(result).to_string();
4061                self.start_baking_expression(result, context, &name)?;
4062                name
4063            }
4064        };
4065        self.named_expressions.insert(result, name.clone());
4066        Ok(name)
4067    }
4068
4069    /// Cache a clamped level of detail value, if necessary.
4070    ///
4071    /// [`ImageLoad`] accesses covered by [`BoundsCheckPolicy::Restrict`] use a
4072    /// properly clamped level of detail value both in the access itself, and
4073    /// for fetching the size of the requested MIP level, needed to clamp the
4074    /// coordinates. To avoid recomputing this clamped level of detail, we cache
4075    /// it in a temporary variable, as part of the [`Emit`] statement covering
4076    /// the [`ImageLoad`] expression.
4077    ///
4078    /// [`ImageLoad`]: crate::Expression::ImageLoad
4079    /// [`BoundsCheckPolicy::Restrict`]: index::BoundsCheckPolicy::Restrict
4080    /// [`Emit`]: crate::Statement::Emit
4081    fn put_cache_restricted_level(
4082        &mut self,
4083        load: Handle<crate::Expression>,
4084        image: Handle<crate::Expression>,
4085        mip_level: Option<Handle<crate::Expression>>,
4086        indent: back::Level,
4087        context: &StatementContext,
4088    ) -> BackendResult {
4089        // Does this image access actually require (or even permit) a
4090        // level-of-detail, and does the policy require us to restrict it?
4091        let level_of_detail = match mip_level {
4092            Some(level) => level,
4093            None => return Ok(()),
4094        };
4095
4096        if context.expression.policies.image_load != index::BoundsCheckPolicy::Restrict
4097            || !context.expression.image_needs_lod(image)
4098        {
4099            return Ok(());
4100        }
4101
4102        write!(self.out, "{}uint {} = ", indent, ClampedLod(load),)?;
4103        self.put_restricted_scalar_image_index(
4104            image,
4105            level_of_detail,
4106            "get_num_mip_levels",
4107            &context.expression,
4108        )?;
4109        writeln!(self.out, ";")?;
4110
4111        Ok(())
4112    }
4113
4114    /// Convert the arguments of `Dot4{I, U}Packed` to `packed_(u?)char4`.
4115    ///
4116    /// Caches the results in temporary variables (whose names are derived from
4117    /// the original variable names). This caching avoids the need to redo the
4118    /// casting for each vector component when emitting the dot product.
4119    fn put_casting_to_packed_chars(
4120        &mut self,
4121        fun: crate::MathFunction,
4122        arg0: Handle<crate::Expression>,
4123        arg1: Handle<crate::Expression>,
4124        indent: back::Level,
4125        context: &StatementContext<'_>,
4126    ) -> Result<(), Error> {
4127        let packed_type = match fun {
4128            crate::MathFunction::Dot4I8Packed => "packed_char4",
4129            crate::MathFunction::Dot4U8Packed => "packed_uchar4",
4130            _ => unreachable!(),
4131        };
4132
4133        for arg in [arg0, arg1] {
4134            write!(
4135                self.out,
4136                "{indent}{packed_type} {0} = as_type<{packed_type}>(",
4137                Reinterpreted::new(packed_type, arg)
4138            )?;
4139            self.put_expression(arg, &context.expression, true)?;
4140            writeln!(self.out, ");")?;
4141        }
4142
4143        Ok(())
4144    }
4145
4146    /// Whether writing `handle` out stores its value in a temporary.
4147    pub(super) fn bakes_expression(
4148        &self,
4149        handle: Handle<crate::Expression>,
4150        context: &ExpressionContext,
4151    ) -> bool {
4152        // There is no way to write the result of a statement inline, so it
4153        // always gets a temporary, whether or not anything asked for one.
4154        if super::hoist::is_statement_result(&context.function.expressions[handle]) {
4155            return true;
4156        }
4157
4158        // If this expression is an index that we're going to first compare
4159        // against a limit, and then actually use as an index, then we may
4160        // want to cache it in a temporary, to avoid evaluating it twice.
4161        if context.guarded_indices.contains(handle) {
4162            return true;
4163        }
4164
4165        if self.need_bake_expressions.contains(&handle) {
4166            return true;
4167        }
4168
4169        // Don't bake pointer expressions
4170        if context.resolve_type(handle).pointer_space().is_some() {
4171            return false;
4172        }
4173
4174        // The `crate::Function::named_expressions` table holds expressions that
4175        // should be saved in temporaries once they are `Emit`ted.
4176        context.function.named_expressions.contains_key(&handle)
4177    }
4178
4179    /// The name of the temporary to store `handle`'s value in, if writing it
4180    /// out introduces one.
4181    ///
4182    /// This reserves the name, so call it at most once per expression.
4183    pub(super) fn baked_name(
4184        &mut self,
4185        handle: Handle<crate::Expression>,
4186        context: &ExpressionContext,
4187    ) -> Option<String> {
4188        if !self.bakes_expression(handle, context) {
4189            return None;
4190        }
4191        Some(match context.function.named_expressions.get(&handle) {
4192            // Don't assume the names in `named_expressions` are unique, or even
4193            // valid. Use the `Namer`.
4194            Some(name) => self.namer.call(name),
4195            None => Baked(handle).to_string(),
4196        })
4197    }
4198
4199    fn put_block(
4200        &mut self,
4201        level: back::Level,
4202        statements: &[crate::Statement],
4203        context: &StatementContext,
4204    ) -> BackendResult {
4205        for statement in statements {
4206            log::trace!("statement[{}] {:?}", level.0, statement);
4207            match *statement {
4208                crate::Statement::Emit(ref range) => {
4209                    for handle in range.clone() {
4210                        use crate::MathFunction as Mf;
4211
4212                        match context.expression.function.expressions[handle] {
4213                            // `ImageLoad` expressions covered by the `Restrict` bounds check policy
4214                            // may need to cache a clamped version of their level-of-detail argument.
4215                            crate::Expression::ImageLoad {
4216                                image,
4217                                level: mip_level,
4218                                ..
4219                            } => {
4220                                self.put_cache_restricted_level(
4221                                    handle, image, mip_level, level, context,
4222                                )?;
4223                            }
4224
4225                            // If we are going to write a `Dot4I8Packed` or `Dot4U8Packed` on Metal
4226                            // 2.1+ then we introduce two intermediate variables that recast the two
4227                            // arguments as packed (signed or unsigned) chars. The actual dot product
4228                            // is implemented in `Self::put_expression`, and it uses both of these
4229                            // intermediate variables multiple times. There's no danger that the
4230                            // original arguments get modified between the definition of these
4231                            // intermediate variables and the implementation of the actual dot
4232                            // product since we require the inputs of `Dot4{I, U}Packed` to be baked.
4233                            crate::Expression::Math {
4234                                fun: fun @ (Mf::Dot4I8Packed | Mf::Dot4U8Packed),
4235                                arg,
4236                                arg1,
4237                                ..
4238                            } if context.expression.lang_version >= (2, 1) => {
4239                                self.put_casting_to_packed_chars(
4240                                    fun,
4241                                    arg,
4242                                    arg1.unwrap(),
4243                                    level,
4244                                    context,
4245                                )?;
4246                            }
4247
4248                            _ => (),
4249                        }
4250
4251                        // A temporary already declared ahead of an enclosing
4252                        // loop just needs assigning to; otherwise, decide
4253                        // whether this expression needs one at all.
4254                        let hoisted = self.hoisted_loop_expressions.remove(&handle);
4255                        let declare = hoisted.is_none();
4256                        let expr_name =
4257                            hoisted.or_else(|| self.baked_name(handle, &context.expression));
4258
4259                        if let Some(name) = expr_name {
4260                            write!(self.out, "{level}")?;
4261                            if declare {
4262                                self.start_baking_expression(handle, &context.expression, &name)?;
4263                            } else {
4264                                write!(self.out, "{name} = ")?;
4265                            }
4266                            self.put_expression(handle, &context.expression, true)?;
4267                            // The temporary only counts as holding this
4268                            // expression's value once we have written it.
4269                            self.named_expressions.insert(handle, name);
4270                            writeln!(self.out, ";")?;
4271                        }
4272                    }
4273                }
4274                crate::Statement::Block(ref block) => {
4275                    if !block.is_empty() {
4276                        writeln!(self.out, "{level}{{")?;
4277                        self.put_block(level.next(), block, context)?;
4278                        writeln!(self.out, "{level}}}")?;
4279                    }
4280                }
4281                crate::Statement::If {
4282                    condition,
4283                    ref accept,
4284                    ref reject,
4285                } => {
4286                    write!(self.out, "{level}if (")?;
4287                    self.put_expression(condition, &context.expression, true)?;
4288                    writeln!(self.out, ") {{")?;
4289                    self.put_block(level.next(), accept, context)?;
4290                    if !reject.is_empty() {
4291                        writeln!(self.out, "{level}}} else {{")?;
4292                        self.put_block(level.next(), reject, context)?;
4293                    }
4294                    writeln!(self.out, "{level}}}")?;
4295                }
4296                crate::Statement::Switch {
4297                    selector,
4298                    ref cases,
4299                } => {
4300                    write!(self.out, "{level}switch(")?;
4301                    self.put_expression(selector, &context.expression, true)?;
4302                    writeln!(self.out, ") {{")?;
4303                    let lcase = level.next();
4304                    for case in cases.iter() {
4305                        match case.value {
4306                            crate::SwitchValue::I32(value) => {
4307                                write!(self.out, "{lcase}case {value}:")?;
4308                            }
4309                            crate::SwitchValue::U32(value) => {
4310                                write!(self.out, "{lcase}case {value}u:")?;
4311                            }
4312                            crate::SwitchValue::Default => {
4313                                write!(self.out, "{lcase}default:")?;
4314                            }
4315                        }
4316
4317                        let write_block_braces = !(case.fall_through && case.body.is_empty());
4318                        if write_block_braces {
4319                            writeln!(self.out, " {{")?;
4320                        } else {
4321                            writeln!(self.out)?;
4322                        }
4323
4324                        self.put_block(lcase.next(), &case.body, context)?;
4325                        if !case.fall_through && case.body.last().is_none_or(|s| !s.is_terminator())
4326                        {
4327                            writeln!(self.out, "{}break;", lcase.next())?;
4328                        }
4329
4330                        if write_block_braces {
4331                            writeln!(self.out, "{lcase}}}")?;
4332                        }
4333                    }
4334                    writeln!(self.out, "{level}}}")?;
4335                }
4336                crate::Statement::Loop {
4337                    ref body,
4338                    ref continuing,
4339                    break_if,
4340                } => {
4341                    let force_loop_bound_statements =
4342                        self.gen_force_bounded_loop_statements(level, context);
4343                    let gate_name = (!continuing.is_empty() || break_if.is_some())
4344                        .then(|| self.namer.call("loop_init"));
4345
4346                    // We generate `continuing` above `body`, so the temporaries
4347                    // it needs from `body` must be declared outside the loop.
4348                    if gate_name.is_some() {
4349                        self.hoist_continuing_dependencies(
4350                            level, body, continuing, break_if, context,
4351                        )?;
4352                    }
4353
4354                    if let Some((ref decl, _)) = force_loop_bound_statements {
4355                        writeln!(self.out, "{decl}")?;
4356                    }
4357                    if let Some(ref gate_name) = gate_name {
4358                        writeln!(self.out, "{level}bool {gate_name} = true;")?;
4359                    }
4360
4361                    writeln!(self.out, "{level}while(true) {{",)?;
4362                    if let Some((_, ref break_and_inc)) = force_loop_bound_statements {
4363                        writeln!(self.out, "{break_and_inc}")?;
4364                    }
4365                    if let Some(ref gate_name) = gate_name {
4366                        let lif = level.next();
4367                        let lcontinuing = lif.next();
4368                        writeln!(self.out, "{lif}if (!{gate_name}) {{")?;
4369                        self.put_block(lcontinuing, continuing, context)?;
4370                        if let Some(condition) = break_if {
4371                            write!(self.out, "{lcontinuing}if (")?;
4372                            self.put_expression(condition, &context.expression, true)?;
4373                            writeln!(self.out, ") {{")?;
4374                            writeln!(self.out, "{}break;", lcontinuing.next())?;
4375                            writeln!(self.out, "{lcontinuing}}}")?;
4376                        }
4377                        writeln!(self.out, "{lif}}}")?;
4378                        writeln!(self.out, "{lif}{gate_name} = false;")?;
4379                    }
4380                    self.put_block(level.next(), body, context)?;
4381
4382                    writeln!(self.out, "{level}}}")?;
4383                }
4384                crate::Statement::Break => {
4385                    writeln!(self.out, "{level}break;")?;
4386                }
4387                crate::Statement::Continue => {
4388                    writeln!(self.out, "{level}continue;")?;
4389                }
4390                crate::Statement::Return {
4391                    value: Some(expr_handle),
4392                } => {
4393                    self.put_return_value(
4394                        level,
4395                        expr_handle,
4396                        context.result_struct,
4397                        &context.expression,
4398                    )?;
4399                }
4400                crate::Statement::Return { value: None } => {
4401                    writeln!(self.out, "{level}return;")?;
4402                }
4403                crate::Statement::Kill => {
4404                    writeln!(self.out, "{level}{NAMESPACE}::discard_fragment();")?;
4405                }
4406                crate::Statement::ControlBarrier(flags)
4407                | crate::Statement::MemoryBarrier(flags) => {
4408                    self.write_barrier(flags, level)?;
4409                }
4410                crate::Statement::Store { pointer, value } => {
4411                    self.put_store(pointer, value, level, context)?
4412                }
4413                crate::Statement::ImageStore {
4414                    image,
4415                    coordinate,
4416                    array_index,
4417                    value,
4418                } => {
4419                    let address = TexelAddress {
4420                        coordinate,
4421                        array_index,
4422                        sample: None,
4423                        level: None,
4424                    };
4425                    self.put_image_store(level, image, &address, value, context)?
4426                }
4427                crate::Statement::Call {
4428                    function,
4429                    ref arguments,
4430                    result,
4431                } => {
4432                    write!(self.out, "{level}")?;
4433                    if let Some(expr) = result {
4434                        self.write_result_assignment(expr, &context.expression)?;
4435                    }
4436                    let fun_name = &self.names[&NameKey::Function(function)];
4437                    write!(self.out, "{fun_name}(")?;
4438                    // first, write down the actual arguments
4439                    for (i, &handle) in arguments.iter().enumerate() {
4440                        if i != 0 {
4441                            write!(self.out, ", ")?;
4442                        }
4443                        self.put_expression(handle, &context.expression, true)?;
4444                    }
4445                    // follow-up with any global resources used
4446                    let mut separate = !arguments.is_empty();
4447                    let fun_info = &context.expression.mod_info[function];
4448                    let mut needs_buffer_sizes = false;
4449                    for (handle, var) in context.expression.module.global_variables.iter() {
4450                        if fun_info[handle].is_empty() {
4451                            continue;
4452                        }
4453                        if var.space.needs_pass_through() {
4454                            let name = &self.names[&NameKey::GlobalVariable(handle)];
4455                            if separate {
4456                                write!(self.out, ", ")?;
4457                            } else {
4458                                separate = true;
4459                            }
4460                            write!(self.out, "{name}")?;
4461                        }
4462                        needs_buffer_sizes |= context.expression.module.types[var.ty]
4463                            .inner
4464                            .needs_host_buffer_byte_size(&context.expression.module.types);
4465                    }
4466                    if needs_buffer_sizes {
4467                        if separate {
4468                            write!(self.out, ", ")?;
4469                        }
4470                        write!(self.out, "_buffer_sizes")?;
4471                    }
4472
4473                    // done
4474                    writeln!(self.out, ");")?;
4475                }
4476                crate::Statement::Atomic {
4477                    pointer,
4478                    ref fun,
4479                    value,
4480                    result,
4481                } => {
4482                    let context = &context.expression;
4483
4484                    // This backend supports `SHADER_INT64_ATOMIC_MIN_MAX` but not
4485                    // `SHADER_INT64_ATOMIC_ALL_OPS`, so we can assume that if `result` is
4486                    // `Some`, we are not operating on a 64-bit value, and that if we are
4487                    // operating on a 64-bit value, `result` is `None`.
4488                    write!(self.out, "{level}")?;
4489                    let fun_key = if let Some(result) = result {
4490                        self.write_result_assignment(result, context)?;
4491                        fun.to_msl()
4492                    } else if context.resolve_type(value).scalar_width() == Some(8) {
4493                        fun.to_msl_64_bit()?
4494                    } else {
4495                        fun.to_msl()
4496                    };
4497
4498                    // If the pointer we're passing to the atomic operation needs to be conditional
4499                    // for `ReadZeroSkipWrite`, the condition needs to *surround* the atomic op, and
4500                    // the pointer operand should be unchecked.
4501                    let policy = context.choose_bounds_check_policy(pointer);
4502                    let checked = policy == index::BoundsCheckPolicy::ReadZeroSkipWrite
4503                        && self.put_bounds_checks(pointer, context, back::Level(0), "")?;
4504
4505                    // If requested and successfully put bounds checks, continue the ternary expression.
4506                    if checked {
4507                        write!(self.out, " ? ")?;
4508                    }
4509
4510                    // Put the atomic function invocation.
4511                    match *fun {
4512                        crate::AtomicFunction::Exchange { compare: Some(cmp) } => {
4513                            write!(self.out, "{ATOMIC_COMP_EXCH_FUNCTION}({ATOMIC_REFERENCE}")?;
4514                            self.put_access_chain(pointer, policy, context)?;
4515                            write!(self.out, ", ")?;
4516                            self.put_expression(cmp, context, true)?;
4517                            write!(self.out, ", ")?;
4518                            self.put_expression(value, context, true)?;
4519                            write!(self.out, ")")?;
4520                        }
4521                        _ => {
4522                            write!(
4523                                self.out,
4524                                "{NAMESPACE}::atomic_{fun_key}_explicit({ATOMIC_REFERENCE}"
4525                            )?;
4526                            self.put_access_chain(pointer, policy, context)?;
4527                            write!(self.out, ", ")?;
4528                            self.put_expression(value, context, true)?;
4529                            write!(self.out, ", {NAMESPACE}::memory_order_relaxed)")?;
4530                        }
4531                    }
4532
4533                    // Finish the ternary expression.
4534                    if checked {
4535                        write!(self.out, " : DefaultConstructible()")?;
4536                    }
4537
4538                    // Done
4539                    writeln!(self.out, ";")?;
4540                }
4541                crate::Statement::ImageAtomic {
4542                    image,
4543                    coordinate,
4544                    array_index,
4545                    fun,
4546                    value,
4547                } => {
4548                    let address = TexelAddress {
4549                        coordinate,
4550                        array_index,
4551                        sample: None,
4552                        level: None,
4553                    };
4554                    self.put_image_atomic(level, image, &address, fun, value, context)?
4555                }
4556                crate::Statement::WorkGroupUniformLoad { pointer, result } => {
4557                    self.write_barrier(crate::Barrier::WORK_GROUP, level)?;
4558
4559                    write!(self.out, "{level}")?;
4560                    self.write_result_assignment(result, &context.expression)?;
4561                    self.put_load(pointer, &context.expression, true)?;
4562
4563                    writeln!(self.out, ";")?;
4564                    self.write_barrier(crate::Barrier::WORK_GROUP, level)?;
4565                }
4566                crate::Statement::RayQuery { query, ref fun } => {
4567                    self.write_ray_query_stmt(level, context, query, fun)?;
4568                }
4569                crate::Statement::SubgroupBallot { result, predicate } => {
4570                    write!(self.out, "{level}")?;
4571                    self.write_result_assignment(result, &context.expression)?;
4572                    // `simd_ballot` holds one bit per lane, and SIMD groups can be 64 lanes wide
4573                    // (AMD GPUs). Lanes 32..63 go in the second component, as WGSL expects.
4574                    write!(
4575                        self.out,
4576                        "{NAMESPACE}::uint4(as_type<{NAMESPACE}::uint2>((uint64_t){NAMESPACE}::simd_ballot("
4577                    )?;
4578                    if let Some(predicate) = predicate {
4579                        self.put_expression(predicate, &context.expression, true)?;
4580                    } else {
4581                        write!(self.out, "true")?;
4582                    }
4583                    writeln!(self.out, ")), 0, 0);")?;
4584                }
4585                crate::Statement::SubgroupCollectiveOperation {
4586                    op,
4587                    collective_op,
4588                    argument,
4589                    result,
4590                } => {
4591                    write!(self.out, "{level}")?;
4592                    self.write_result_assignment(result, &context.expression)?;
4593                    match (collective_op, op) {
4594                        (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::All) => {
4595                            write!(self.out, "{NAMESPACE}::simd_all(")?
4596                        }
4597                        (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Any) => {
4598                            write!(self.out, "{NAMESPACE}::simd_any(")?
4599                        }
4600                        (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Add) => {
4601                            write!(self.out, "{NAMESPACE}::simd_sum(")?
4602                        }
4603                        (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Mul) => {
4604                            write!(self.out, "{NAMESPACE}::simd_product(")?
4605                        }
4606                        (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Max) => {
4607                            write!(self.out, "{NAMESPACE}::simd_max(")?
4608                        }
4609                        (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Min) => {
4610                            write!(self.out, "{NAMESPACE}::simd_min(")?
4611                        }
4612                        (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::And) => {
4613                            write!(self.out, "{NAMESPACE}::simd_and(")?
4614                        }
4615                        (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Or) => {
4616                            write!(self.out, "{NAMESPACE}::simd_or(")?
4617                        }
4618                        (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Xor) => {
4619                            write!(self.out, "{NAMESPACE}::simd_xor(")?
4620                        }
4621                        (
4622                            crate::CollectiveOperation::ExclusiveScan,
4623                            crate::SubgroupOperation::Add,
4624                        ) => write!(self.out, "{NAMESPACE}::simd_prefix_exclusive_sum(")?,
4625                        (
4626                            crate::CollectiveOperation::ExclusiveScan,
4627                            crate::SubgroupOperation::Mul,
4628                        ) => write!(self.out, "{NAMESPACE}::simd_prefix_exclusive_product(")?,
4629                        (
4630                            crate::CollectiveOperation::InclusiveScan,
4631                            crate::SubgroupOperation::Add,
4632                        ) => write!(self.out, "{NAMESPACE}::simd_prefix_inclusive_sum(")?,
4633                        (
4634                            crate::CollectiveOperation::InclusiveScan,
4635                            crate::SubgroupOperation::Mul,
4636                        ) => write!(self.out, "{NAMESPACE}::simd_prefix_inclusive_product(")?,
4637                        _ => unimplemented!(),
4638                    }
4639                    self.put_expression(argument, &context.expression, true)?;
4640                    writeln!(self.out, ");")?;
4641                }
4642                crate::Statement::SubgroupGather {
4643                    mode,
4644                    argument,
4645                    result,
4646                } => {
4647                    write!(self.out, "{level}")?;
4648                    self.write_result_assignment(result, &context.expression)?;
4649                    match mode {
4650                        crate::GatherMode::BroadcastFirst => {
4651                            write!(self.out, "{NAMESPACE}::simd_broadcast_first(")?;
4652                        }
4653                        crate::GatherMode::Broadcast(_) => {
4654                            write!(self.out, "{NAMESPACE}::simd_broadcast(")?;
4655                        }
4656                        crate::GatherMode::Shuffle(_) => {
4657                            write!(self.out, "{NAMESPACE}::simd_shuffle(")?;
4658                        }
4659                        crate::GatherMode::ShuffleDown(_) => {
4660                            write!(self.out, "{NAMESPACE}::simd_shuffle_down(")?;
4661                        }
4662                        crate::GatherMode::ShuffleUp(_) => {
4663                            write!(self.out, "{NAMESPACE}::simd_shuffle_up(")?;
4664                        }
4665                        crate::GatherMode::ShuffleXor(_) => {
4666                            write!(self.out, "{NAMESPACE}::simd_shuffle_xor(")?;
4667                        }
4668                        crate::GatherMode::QuadBroadcast(_) => {
4669                            write!(self.out, "{NAMESPACE}::quad_broadcast(")?;
4670                        }
4671                        crate::GatherMode::QuadSwap(_) => {
4672                            write!(self.out, "{NAMESPACE}::quad_shuffle_xor(")?;
4673                        }
4674                    }
4675                    self.put_expression(argument, &context.expression, true)?;
4676                    match mode {
4677                        crate::GatherMode::BroadcastFirst => {}
4678                        crate::GatherMode::Broadcast(index)
4679                        | crate::GatherMode::Shuffle(index)
4680                        | crate::GatherMode::ShuffleDown(index)
4681                        | crate::GatherMode::ShuffleUp(index)
4682                        | crate::GatherMode::ShuffleXor(index)
4683                        | crate::GatherMode::QuadBroadcast(index) => {
4684                            write!(self.out, ", ")?;
4685                            self.put_expression(index, &context.expression, true)?;
4686                        }
4687                        crate::GatherMode::QuadSwap(direction) => {
4688                            write!(self.out, ", ")?;
4689                            match direction {
4690                                crate::Direction::X => {
4691                                    write!(self.out, "1u")?;
4692                                }
4693                                crate::Direction::Y => {
4694                                    write!(self.out, "2u")?;
4695                                }
4696                                crate::Direction::Diagonal => {
4697                                    write!(self.out, "3u")?;
4698                                }
4699                            }
4700                        }
4701                    }
4702                    writeln!(self.out, ");")?;
4703                }
4704                crate::Statement::SubgroupBallotFindBit {
4705                    order,
4706                    argument,
4707                    result,
4708                } => {
4709                    // Evaluate each ballot word once into a local, rather
4710                    // than repeatedly writing `argument.x`/`.y`/`.z`/`.w` in
4711                    // the ternary chain below.
4712                    const COMPONENTS: [char; 4] = ['x', 'y', 'z', 'w'];
4713                    let mut words: [String; 4] = core::array::from_fn(|_| String::new());
4714                    for (word, &component) in words.iter_mut().zip(&COMPONENTS) {
4715                        write!(self.out, "{level}uint ")?;
4716                        let name = self.namer.call("");
4717                        write!(self.out, "{name} = ")?;
4718                        self.put_expression(argument, &context.expression, true)?;
4719                        writeln!(self.out, ".{component};")?;
4720                        *word = name;
4721                    }
4722
4723                    write!(self.out, "{level}")?;
4724                    self.write_result_assignment(result, &context.expression)?;
4725                    self.put_ballot_find_bit(&words, order, 3)?;
4726                    writeln!(self.out, ";")?;
4727                }
4728                crate::Statement::CooperativeStore { target, ref data } => {
4729                    write!(self.out, "{level}simdgroup_store(")?;
4730                    self.put_expression(target, &context.expression, true)?;
4731                    write!(self.out, ", &")?;
4732                    self.put_access_chain(
4733                        data.pointer,
4734                        context.expression.policies.index,
4735                        &context.expression,
4736                    )?;
4737                    write!(self.out, ", ")?;
4738                    self.put_expression(data.stride, &context.expression, true)?;
4739                    // See the comment in `CooperativeLoad` above: WGSL's
4740                    // row_major flag is negated when emitting Metal's
4741                    // `transpose` flag, so a col-major store (row_major=false)
4742                    // must use `transpose=true`.
4743                    if !data.row_major {
4744                        let matrix_origin = "0";
4745                        let transpose = true;
4746                        write!(self.out, ", {matrix_origin}, {transpose}")?;
4747                    }
4748                    writeln!(self.out, ");")?;
4749                }
4750                crate::Statement::RayPipelineFunction(_) | crate::Statement::HitObject { .. } => {
4751                    unreachable!()
4752                }
4753                crate::Statement::DebugPrintf {
4754                    ref format,
4755                    ref arguments,
4756                } => {
4757                    // Shader logging is only available in MSL 3.2 and later.
4758                    if context.expression.lang_version < (3, 2) {
4759                        return Err(Error::UnsupportedDebugPrintf);
4760                    }
4761
4762                    write!(
4763                        self.out,
4764                        "{level}metal::os_log_default.log_info(\"{}\"",
4765                        format
4766                    )?;
4767                    for &arg in arguments {
4768                        write!(self.out, ", ")?;
4769                        self.put_expression(arg, &context.expression, true)?;
4770                    }
4771                    writeln!(self.out, ");")?;
4772                }
4773            }
4774        }
4775
4776        Ok(())
4777    }
4778
4779    fn put_store(
4780        &mut self,
4781        pointer: Handle<crate::Expression>,
4782        value: Handle<crate::Expression>,
4783        level: back::Level,
4784        context: &StatementContext,
4785    ) -> BackendResult {
4786        let policy = context.expression.choose_bounds_check_policy(pointer);
4787        if policy == index::BoundsCheckPolicy::ReadZeroSkipWrite
4788            && self.put_bounds_checks(pointer, &context.expression, level, "if (")?
4789        {
4790            writeln!(self.out, ") {{")?;
4791            self.put_unchecked_store(pointer, value, policy, level.next(), context)?;
4792            writeln!(self.out, "{level}}}")?;
4793        } else {
4794            self.put_unchecked_store(pointer, value, policy, level, context)?;
4795        }
4796
4797        Ok(())
4798    }
4799
4800    fn put_unchecked_store(
4801        &mut self,
4802        pointer: Handle<crate::Expression>,
4803        value: Handle<crate::Expression>,
4804        policy: index::BoundsCheckPolicy,
4805        level: back::Level,
4806        context: &StatementContext,
4807    ) -> BackendResult {
4808        let is_atomic_pointer = context
4809            .expression
4810            .resolve_type(pointer)
4811            .is_atomic_pointer(&context.expression.module.types);
4812
4813        if is_atomic_pointer {
4814            write!(
4815                self.out,
4816                "{level}{NAMESPACE}::atomic_store_explicit({ATOMIC_REFERENCE}"
4817            )?;
4818            self.put_access_chain(pointer, policy, &context.expression)?;
4819            write!(self.out, ", ")?;
4820            self.put_expression(value, &context.expression, true)?;
4821            writeln!(self.out, ", {NAMESPACE}::memory_order_relaxed);")?;
4822        } else {
4823            write!(self.out, "{level}")?;
4824            self.put_access_chain(pointer, policy, &context.expression)?;
4825            write!(self.out, " = ")?;
4826            self.put_expression(value, &context.expression, true)?;
4827            writeln!(self.out, ";")?;
4828        }
4829
4830        Ok(())
4831    }
4832
4833    pub fn write(
4834        &mut self,
4835        module: &crate::Module,
4836        info: &valid::ModuleInfo,
4837        options: &Options,
4838        pipeline_options: &PipelineOptions,
4839    ) -> Result<TranslationInfo, Error> {
4840        self.emit_int_div_checks = options.emit_int_div_checks;
4841        self.names.clear();
4842        self.namer.reset(
4843            module,
4844            &super::keywords::RESERVED_SET,
4845            proc::KeywordSet::empty(),
4846            proc::CaseInsensitiveKeywordSet::empty(),
4847            &[
4848                CLAMPED_LOD_LOAD_PREFIX,
4849                super::ray::INTERSECTION_FUNCTION_NAME,
4850                super::ray::RAY_QUERY_TRACKER_VARIABLE_PREFIX,
4851                super::ray::RAY_QUERY_T_MAX_TRACKER_VARIABLE_PREFIX,
4852            ],
4853            &mut self.names,
4854        );
4855        self.wrapped_functions.clear();
4856        self.struct_member_pads.clear();
4857
4858        writeln!(
4859            self.out,
4860            "// language: metal{}.{}",
4861            options.lang_version.0, options.lang_version.1
4862        )?;
4863        writeln!(self.out, "#include <metal_stdlib>")?;
4864        writeln!(self.out, "#include <simd/simd.h>")?;
4865        writeln!(self.out)?;
4866        // Work around Metal bug where `uint` is not available by default
4867        writeln!(self.out, "using {NAMESPACE}::uint;")?;
4868
4869        if module.uses_mesh_shaders() && options.lang_version < (3, 0) {
4870            return Err(Error::UnsupportedMeshShader);
4871        }
4872        self.needs_object_memory_barriers = module
4873            .entry_points
4874            .iter()
4875            .any(|e| e.stage == crate::ShaderStage::Task && e.task_payload.is_some());
4876
4877        if module.special_types.ray_desc.is_some()
4878            || module.special_types.ray_intersection.is_some()
4879        {
4880            if options.lang_version < (2, 4) {
4881                return Err(Error::UnsupportedRayTracing);
4882            }
4883        }
4884
4885        if options
4886            .bounds_check_policies
4887            .contains(index::BoundsCheckPolicy::ReadZeroSkipWrite)
4888        {
4889            self.put_default_constructible()?;
4890        }
4891        writeln!(self.out)?;
4892
4893        {
4894            // Make a `Vec` of all the `GlobalVariable`s that contain
4895            // runtime-sized arrays.
4896            let globals: Vec<Handle<crate::GlobalVariable>> = module
4897                .global_variables
4898                .iter()
4899                .filter(|&(_, var)| {
4900                    module.types[var.ty]
4901                        .inner
4902                        .needs_host_buffer_byte_size(&module.types)
4903                })
4904                .map(|(handle, _)| handle)
4905                .collect();
4906
4907            let mut buffer_indices = vec![];
4908            for vbm in &pipeline_options.vertex_buffer_mappings {
4909                buffer_indices.push(vbm.id);
4910            }
4911
4912            if !globals.is_empty() || !buffer_indices.is_empty() {
4913                writeln!(self.out, "struct _mslBufferSizes {{")?;
4914
4915                for global in globals {
4916                    let var = &module.global_variables[global];
4917                    let var_ty = var.ty;
4918                    match module.types[var_ty].inner {
4919                        crate::TypeInner::BindingArray { .. } => {
4920                            let n =
4921                                Self::binding_array_layout_count(module, pipeline_options, global);
4922                            writeln!(
4923                                self.out,
4924                                "{}uint {}[{n}];",
4925                                back::INDENT,
4926                                ArraySizeMember(global),
4927                            )?;
4928                        }
4929                        _ => writeln!(
4930                            self.out,
4931                            "{}uint {};",
4932                            back::INDENT,
4933                            ArraySizeMember(global)
4934                        )?,
4935                    }
4936                }
4937
4938                for idx in buffer_indices {
4939                    writeln!(self.out, "{}uint buffer_size{};", back::INDENT, idx)?;
4940                }
4941
4942                writeln!(self.out, "}};")?;
4943                writeln!(self.out)?;
4944            }
4945        };
4946
4947        self.write_type_defs(module)?;
4948        self.write_global_constants(module, info)?;
4949        self.write_functions(module, info, options, pipeline_options)
4950    }
4951
4952    /// Write the definition for the `DefaultConstructible` class.
4953    ///
4954    /// The [`ReadZeroSkipWrite`] bounds check policy requires us to be able to
4955    /// produce 'zero' values for any type, including structs, arrays, and so
4956    /// on. We could do this by emitting default constructor applications, but
4957    /// that would entail printing the name of the type, which is more trouble
4958    /// than you'd think. Instead, we just construct this magic C++14 class that
4959    /// can be converted to any type that can be default constructed, using
4960    /// template parameter inference to detect which type is needed, so we don't
4961    /// have to figure out the name.
4962    ///
4963    /// [`ReadZeroSkipWrite`]: index::BoundsCheckPolicy::ReadZeroSkipWrite
4964    fn put_default_constructible(&mut self) -> BackendResult {
4965        let tab = back::INDENT;
4966        writeln!(self.out, "struct DefaultConstructible {{")?;
4967        writeln!(self.out, "{tab}template<typename T>")?;
4968        writeln!(self.out, "{tab}operator T() && {{")?;
4969        writeln!(self.out, "{tab}{tab}return T {{}};")?;
4970        writeln!(self.out, "{tab}}}")?;
4971        writeln!(self.out, "}};")?;
4972        Ok(())
4973    }
4974
4975    fn write_type_defs(&mut self, module: &crate::Module) -> BackendResult {
4976        let mut generated_argument_buffer_wrapper = false;
4977        let mut generated_external_texture_wrapper = false;
4978        for (handle, ty) in module.types.iter() {
4979            match ty.inner {
4980                crate::TypeInner::BindingArray { .. } if !generated_argument_buffer_wrapper => {
4981                    writeln!(self.out, "template <typename T>")?;
4982                    writeln!(self.out, "struct {ARGUMENT_BUFFER_WRAPPER_STRUCT} {{")?;
4983                    writeln!(self.out, "{}T {WRAPPED_ARRAY_FIELD};", back::INDENT)?;
4984                    writeln!(self.out, "}};")?;
4985                    generated_argument_buffer_wrapper = true;
4986                }
4987                crate::TypeInner::Image {
4988                    class: crate::ImageClass::External,
4989                    ..
4990                } if !generated_external_texture_wrapper => {
4991                    let params_ty_name = &self.names
4992                        [&NameKey::Type(module.special_types.external_texture_params.unwrap())];
4993                    writeln!(self.out, "struct {EXTERNAL_TEXTURE_WRAPPER_STRUCT} {{")?;
4994                    writeln!(
4995                        self.out,
4996                        "{}{NAMESPACE}::texture2d<float, {NAMESPACE}::access::sample> plane0;",
4997                        back::INDENT
4998                    )?;
4999                    writeln!(
5000                        self.out,
5001                        "{}{NAMESPACE}::texture2d<float, {NAMESPACE}::access::sample> plane1;",
5002                        back::INDENT
5003                    )?;
5004                    writeln!(
5005                        self.out,
5006                        "{}{NAMESPACE}::texture2d<float, {NAMESPACE}::access::sample> plane2;",
5007                        back::INDENT
5008                    )?;
5009                    writeln!(self.out, "{}{params_ty_name} params;", back::INDENT)?;
5010                    writeln!(self.out, "}};")?;
5011                    generated_external_texture_wrapper = true;
5012                }
5013                _ => {}
5014            }
5015
5016            if !ty.needs_alias() {
5017                continue;
5018            }
5019            let name = &self.names[&NameKey::Type(handle)];
5020            match ty.inner {
5021                // Naga IR can pass around arrays by value, but Metal, following
5022                // C++, performs an array-to-pointer conversion (C++ [conv.array])
5023                // on expressions of array type, so assigning the array by value
5024                // isn't possible. However, Metal *does* assign structs by
5025                // value. So in our Metal output, we wrap all array types in
5026                // synthetic struct types:
5027                //
5028                //     struct type1 {
5029                //         float inner[10]
5030                //     };
5031                //
5032                // Then we carefully include `.inner` (`WRAPPED_ARRAY_FIELD`) in
5033                // any expression that actually wants access to the array.
5034                crate::TypeInner::Array {
5035                    base,
5036                    size,
5037                    stride: _,
5038                } => {
5039                    let base_name = TypeContext {
5040                        handle: base,
5041                        gctx: module.to_ctx(),
5042                        names: &self.names,
5043                        access: crate::StorageAccess::empty(),
5044                        first_time: false,
5045                    };
5046
5047                    match size.resolve(module.to_ctx())? {
5048                        proc::IndexableLength::Known(size) => {
5049                            writeln!(self.out, "struct {name} {{")?;
5050                            writeln!(
5051                                self.out,
5052                                "{}{} {}[{}];",
5053                                back::INDENT,
5054                                base_name,
5055                                WRAPPED_ARRAY_FIELD,
5056                                size
5057                            )?;
5058                            writeln!(self.out, "}};")?;
5059                        }
5060                        proc::IndexableLength::Dynamic => {
5061                            writeln!(self.out, "typedef {base_name} {name}[1];")?;
5062                        }
5063                    }
5064                }
5065                crate::TypeInner::Struct {
5066                    ref members, span, ..
5067                } => {
5068                    writeln!(self.out, "struct {name} {{")?;
5069                    let mut last_offset = 0;
5070                    for (index, member) in members.iter().enumerate() {
5071                        if member.offset > last_offset {
5072                            self.struct_member_pads.insert((handle, index as u32));
5073                            let pad = member.offset - last_offset;
5074                            writeln!(self.out, "{}char _pad{}[{}];", back::INDENT, index, pad)?;
5075                        }
5076                        let ty_inner = &module.types[member.ty].inner;
5077                        last_offset = member.offset + ty_inner.size(module.to_ctx());
5078
5079                        let member_name = &self.names[&NameKey::StructMember(handle, index as u32)];
5080
5081                        // If the member should be packed (as is the case for a misaligned vec3) issue a packed vector
5082                        match should_pack_struct_member(members, span, index, module) {
5083                            Some(scalar) => {
5084                                writeln!(
5085                                    self.out,
5086                                    "{}{}::packed_{}3 {};",
5087                                    back::INDENT,
5088                                    NAMESPACE,
5089                                    scalar.to_msl_name(),
5090                                    member_name
5091                                )?;
5092                            }
5093                            None => {
5094                                let base_name = TypeContext {
5095                                    handle: member.ty,
5096                                    gctx: module.to_ctx(),
5097                                    names: &self.names,
5098                                    access: crate::StorageAccess::empty(),
5099                                    first_time: false,
5100                                };
5101                                writeln!(
5102                                    self.out,
5103                                    "{}{} {};",
5104                                    back::INDENT,
5105                                    base_name,
5106                                    member_name
5107                                )?;
5108
5109                                // for 3-component vectors, add one component
5110                                if let crate::TypeInner::Vector {
5111                                    size: crate::VectorSize::Tri,
5112                                    scalar,
5113                                } = *ty_inner
5114                                {
5115                                    last_offset += scalar.width as u32;
5116                                }
5117                            }
5118                        }
5119                    }
5120                    if last_offset < span {
5121                        let pad = span - last_offset;
5122                        writeln!(
5123                            self.out,
5124                            "{}char _pad{}[{}];",
5125                            back::INDENT,
5126                            members.len(),
5127                            pad
5128                        )?;
5129                    }
5130                    writeln!(self.out, "}};")?;
5131                }
5132                _ => {
5133                    let ty_name = TypeContext {
5134                        handle,
5135                        gctx: module.to_ctx(),
5136                        names: &self.names,
5137                        access: crate::StorageAccess::empty(),
5138                        first_time: true,
5139                    };
5140                    writeln!(self.out, "typedef {ty_name} {name};")?;
5141                }
5142            }
5143        }
5144
5145        // Write functions to create special types.
5146        for (type_key, struct_ty) in module.special_types.predeclared_types.iter() {
5147            match type_key {
5148                &crate::PredeclaredType::ModfResult { size, scalar }
5149                | &crate::PredeclaredType::FrexpResult { size, scalar } => {
5150                    let arg_type_name_owner;
5151                    let arg_type_name = if let Some(size) = size {
5152                        arg_type_name_owner = format!(
5153                            "{NAMESPACE}::{}{}",
5154                            if scalar.width == 8 { "double" } else { "float" },
5155                            size as u8
5156                        );
5157                        &arg_type_name_owner
5158                    } else if scalar.width == 8 {
5159                        "double"
5160                    } else {
5161                        "float"
5162                    };
5163
5164                    let other_type_name_owner;
5165                    let (defined_func_name, called_func_name, other_type_name) =
5166                        if matches!(type_key, &crate::PredeclaredType::ModfResult { .. }) {
5167                            (MODF_FUNCTION, "modf", arg_type_name)
5168                        } else {
5169                            let other_type_name = if let Some(size) = size {
5170                                other_type_name_owner = format!("int{}", size as u8);
5171                                &other_type_name_owner
5172                            } else {
5173                                "int"
5174                            };
5175                            (FREXP_FUNCTION, "frexp", other_type_name)
5176                        };
5177
5178                    let struct_name = &self.names[&NameKey::Type(*struct_ty)];
5179
5180                    writeln!(self.out)?;
5181                    writeln!(
5182                        self.out,
5183                        "{struct_name} {defined_func_name}({arg_type_name} arg) {{
5184    {other_type_name} other;
5185    {arg_type_name} fract = {NAMESPACE}::{called_func_name}(arg, other);
5186    return {struct_name}{{ fract, other }};
5187}}"
5188                    )?;
5189                }
5190                &crate::PredeclaredType::AtomicCompareExchangeWeakResult(scalar) => {
5191                    let arg_type_name = scalar.to_msl_name();
5192                    let called_func_name = "atomic_compare_exchange_weak_explicit";
5193                    let defined_func_name = ATOMIC_COMP_EXCH_FUNCTION;
5194                    let struct_name = &self.names[&NameKey::Type(*struct_ty)];
5195
5196                    writeln!(self.out)?;
5197
5198                    for address_space_name in ["device", "threadgroup"] {
5199                        writeln!(
5200                            self.out,
5201                            "\
5202template <typename A>
5203{struct_name} {defined_func_name}(
5204    {address_space_name} A *atomic_ptr,
5205    {arg_type_name} cmp,
5206    {arg_type_name} v
5207) {{
5208    bool swapped = {NAMESPACE}::{called_func_name}(
5209        atomic_ptr, &cmp, v,
5210        metal::memory_order_relaxed, metal::memory_order_relaxed
5211    );
5212    return {struct_name}{{cmp, swapped}};
5213}}"
5214                        )?;
5215                    }
5216                }
5217            }
5218        }
5219
5220        Ok(())
5221    }
5222
5223    /// Writes all named constants
5224    fn write_global_constants(
5225        &mut self,
5226        module: &crate::Module,
5227        mod_info: &valid::ModuleInfo,
5228    ) -> BackendResult {
5229        let constants = module.constants.iter().filter(|&(_, c)| c.name.is_some());
5230
5231        for (handle, constant) in constants {
5232            let ty_name = TypeContext {
5233                handle: constant.ty,
5234                gctx: module.to_ctx(),
5235                names: &self.names,
5236                access: crate::StorageAccess::empty(),
5237                first_time: false,
5238            };
5239            let name = &self.names[&NameKey::Constant(handle)];
5240            write!(self.out, "constant {ty_name} {name} = ")?;
5241            self.put_const_expression(constant.init, module, mod_info, &module.global_expressions)?;
5242            writeln!(self.out, ";")?;
5243        }
5244
5245        Ok(())
5246    }
5247
5248    fn put_inline_sampler_properties(
5249        &mut self,
5250        level: back::Level,
5251        sampler: &sm::InlineSampler,
5252        options: &Options,
5253    ) -> BackendResult {
5254        for (&letter, address) in ['s', 't', 'r'].iter().zip(sampler.address.iter()) {
5255            writeln!(
5256                self.out,
5257                "{}{}::{}_address::{},",
5258                level,
5259                NAMESPACE,
5260                letter,
5261                address.as_str(),
5262            )?;
5263        }
5264        writeln!(
5265            self.out,
5266            "{}{}::mag_filter::{},",
5267            level,
5268            NAMESPACE,
5269            sampler.mag_filter.as_str(),
5270        )?;
5271        writeln!(
5272            self.out,
5273            "{}{}::min_filter::{},",
5274            level,
5275            NAMESPACE,
5276            sampler.min_filter.as_str(),
5277        )?;
5278        if let Some(filter) = sampler.mip_filter {
5279            writeln!(
5280                self.out,
5281                "{}{}::mip_filter::{},",
5282                level,
5283                NAMESPACE,
5284                filter.as_str(),
5285            )?;
5286        }
5287        // avoid setting it on platforms that don't support it
5288        if sampler.border_color != sm::BorderColor::TransparentBlack {
5289            writeln!(
5290                self.out,
5291                "{}{}::border_color::{},",
5292                level,
5293                NAMESPACE,
5294                sampler.border_color.as_str(),
5295            )?;
5296        }
5297
5298        if options.lang_version >= (1, 2) {
5299            if let Some(ref lod) = sampler.lod_clamp {
5300                writeln!(
5301                    self.out,
5302                    "{}{}::lod_clamp({},{}),",
5303                    level, NAMESPACE, lod.start, lod.end,
5304                )?;
5305            }
5306            if let Some(aniso) = sampler.max_anisotropy {
5307                writeln!(
5308                    self.out,
5309                    "{}{}::max_anisotropy({}),",
5310                    level,
5311                    NAMESPACE,
5312                    aniso.get(),
5313                )?;
5314            }
5315        }
5316
5317        if sampler.compare_func != sm::CompareFunc::Never {
5318            writeln!(
5319                self.out,
5320                "{}{}::compare_func::{},",
5321                level,
5322                NAMESPACE,
5323                sampler.compare_func.as_str(),
5324            )?;
5325        }
5326        writeln!(
5327            self.out,
5328            "{}{}::coord::{}",
5329            level,
5330            NAMESPACE,
5331            sampler.coord.as_str()
5332        )?;
5333        Ok(())
5334    }
5335
5336    fn write_unpacking_function(
5337        &mut self,
5338        format: nt::VertexFormat,
5339    ) -> Result<(String, u32, Option<crate::VectorSize>, crate::Scalar), Error> {
5340        use crate::{Scalar, VectorSize};
5341        use nt::VertexFormat::*;
5342        match format {
5343            Uint8 => {
5344                let name = self.namer.call("unpackUint8");
5345                writeln!(self.out, "uint {name}(metal::uchar b0) {{")?;
5346                writeln!(self.out, "{}return uint(b0);", back::INDENT)?;
5347                writeln!(self.out, "}}")?;
5348                Ok((name, 1, None, Scalar::U32))
5349            }
5350            Uint8x2 => {
5351                let name = self.namer.call("unpackUint8x2");
5352                writeln!(
5353                    self.out,
5354                    "metal::uint2 {name}(metal::uchar b0, \
5355                                         metal::uchar b1) {{"
5356                )?;
5357                writeln!(self.out, "{}return metal::uint2(b0, b1);", back::INDENT)?;
5358                writeln!(self.out, "}}")?;
5359                Ok((name, 2, Some(VectorSize::Bi), Scalar::U32))
5360            }
5361            Uint8x4 => {
5362                let name = self.namer.call("unpackUint8x4");
5363                writeln!(
5364                    self.out,
5365                    "metal::uint4 {name}(metal::uchar b0, \
5366                                         metal::uchar b1, \
5367                                         metal::uchar b2, \
5368                                         metal::uchar b3) {{"
5369                )?;
5370                writeln!(
5371                    self.out,
5372                    "{}return metal::uint4(b0, b1, b2, b3);",
5373                    back::INDENT
5374                )?;
5375                writeln!(self.out, "}}")?;
5376                Ok((name, 4, Some(VectorSize::Quad), Scalar::U32))
5377            }
5378            Sint8 => {
5379                let name = self.namer.call("unpackSint8");
5380                writeln!(self.out, "int {name}(metal::uchar b0) {{")?;
5381                writeln!(self.out, "{}return int(as_type<char>(b0));", back::INDENT)?;
5382                writeln!(self.out, "}}")?;
5383                Ok((name, 1, None, Scalar::I32))
5384            }
5385            Sint8x2 => {
5386                let name = self.namer.call("unpackSint8x2");
5387                writeln!(
5388                    self.out,
5389                    "metal::int2 {name}(metal::uchar b0, \
5390                                        metal::uchar b1) {{"
5391                )?;
5392                writeln!(
5393                    self.out,
5394                    "{}return metal::int2(as_type<char>(b0), \
5395                                          as_type<char>(b1));",
5396                    back::INDENT
5397                )?;
5398                writeln!(self.out, "}}")?;
5399                Ok((name, 2, Some(VectorSize::Bi), Scalar::I32))
5400            }
5401            Sint8x4 => {
5402                let name = self.namer.call("unpackSint8x4");
5403                writeln!(
5404                    self.out,
5405                    "metal::int4 {name}(metal::uchar b0, \
5406                                        metal::uchar b1, \
5407                                        metal::uchar b2, \
5408                                        metal::uchar b3) {{"
5409                )?;
5410                writeln!(
5411                    self.out,
5412                    "{}return metal::int4(as_type<char>(b0), \
5413                                          as_type<char>(b1), \
5414                                          as_type<char>(b2), \
5415                                          as_type<char>(b3));",
5416                    back::INDENT
5417                )?;
5418                writeln!(self.out, "}}")?;
5419                Ok((name, 4, Some(VectorSize::Quad), Scalar::I32))
5420            }
5421            Unorm8 => {
5422                let name = self.namer.call("unpackUnorm8");
5423                writeln!(self.out, "float {name}(metal::uchar b0) {{")?;
5424                writeln!(
5425                    self.out,
5426                    "{}return float(float(b0) / 255.0f);",
5427                    back::INDENT
5428                )?;
5429                writeln!(self.out, "}}")?;
5430                Ok((name, 1, None, Scalar::F32))
5431            }
5432            Unorm8x2 => {
5433                let name = self.namer.call("unpackUnorm8x2");
5434                writeln!(
5435                    self.out,
5436                    "metal::float2 {name}(metal::uchar b0, \
5437                                          metal::uchar b1) {{"
5438                )?;
5439                writeln!(
5440                    self.out,
5441                    "{}return metal::float2(float(b0) / 255.0f, \
5442                                            float(b1) / 255.0f);",
5443                    back::INDENT
5444                )?;
5445                writeln!(self.out, "}}")?;
5446                Ok((name, 2, Some(VectorSize::Bi), Scalar::F32))
5447            }
5448            Unorm8x4 => {
5449                let name = self.namer.call("unpackUnorm8x4");
5450                writeln!(
5451                    self.out,
5452                    "metal::float4 {name}(metal::uchar b0, \
5453                                          metal::uchar b1, \
5454                                          metal::uchar b2, \
5455                                          metal::uchar b3) {{"
5456                )?;
5457                writeln!(
5458                    self.out,
5459                    "{}return metal::float4(float(b0) / 255.0f, \
5460                                            float(b1) / 255.0f, \
5461                                            float(b2) / 255.0f, \
5462                                            float(b3) / 255.0f);",
5463                    back::INDENT
5464                )?;
5465                writeln!(self.out, "}}")?;
5466                Ok((name, 4, Some(VectorSize::Quad), Scalar::F32))
5467            }
5468            Snorm8 => {
5469                let name = self.namer.call("unpackSnorm8");
5470                writeln!(self.out, "float {name}(metal::uchar b0) {{")?;
5471                writeln!(
5472                    self.out,
5473                    "{}return float(metal::max(-1.0f, as_type<char>(b0) / 127.0f));",
5474                    back::INDENT
5475                )?;
5476                writeln!(self.out, "}}")?;
5477                Ok((name, 1, None, Scalar::F32))
5478            }
5479            Snorm8x2 => {
5480                let name = self.namer.call("unpackSnorm8x2");
5481                writeln!(
5482                    self.out,
5483                    "metal::float2 {name}(metal::uchar b0, \
5484                                          metal::uchar b1) {{"
5485                )?;
5486                writeln!(
5487                    self.out,
5488                    "{}return metal::float2(metal::max(-1.0f, as_type<char>(b0) / 127.0f), \
5489                                            metal::max(-1.0f, as_type<char>(b1) / 127.0f));",
5490                    back::INDENT
5491                )?;
5492                writeln!(self.out, "}}")?;
5493                Ok((name, 2, Some(VectorSize::Bi), Scalar::F32))
5494            }
5495            Snorm8x4 => {
5496                let name = self.namer.call("unpackSnorm8x4");
5497                writeln!(
5498                    self.out,
5499                    "metal::float4 {name}(metal::uchar b0, \
5500                                          metal::uchar b1, \
5501                                          metal::uchar b2, \
5502                                          metal::uchar b3) {{"
5503                )?;
5504                writeln!(
5505                    self.out,
5506                    "{}return metal::float4(metal::max(-1.0f, as_type<char>(b0) / 127.0f), \
5507                                            metal::max(-1.0f, as_type<char>(b1) / 127.0f), \
5508                                            metal::max(-1.0f, as_type<char>(b2) / 127.0f), \
5509                                            metal::max(-1.0f, as_type<char>(b3) / 127.0f));",
5510                    back::INDENT
5511                )?;
5512                writeln!(self.out, "}}")?;
5513                Ok((name, 4, Some(VectorSize::Quad), Scalar::F32))
5514            }
5515            Uint16 => {
5516                let name = self.namer.call("unpackUint16");
5517                writeln!(
5518                    self.out,
5519                    "metal::uint {name}(metal::uint b0, \
5520                                        metal::uint b1) {{"
5521                )?;
5522                writeln!(
5523                    self.out,
5524                    "{}return metal::uint(b1 << 8 | b0);",
5525                    back::INDENT
5526                )?;
5527                writeln!(self.out, "}}")?;
5528                Ok((name, 2, None, Scalar::U32))
5529            }
5530            Uint16x2 => {
5531                let name = self.namer.call("unpackUint16x2");
5532                writeln!(
5533                    self.out,
5534                    "metal::uint2 {name}(metal::uint b0, \
5535                                         metal::uint b1, \
5536                                         metal::uint b2, \
5537                                         metal::uint b3) {{"
5538                )?;
5539                writeln!(
5540                    self.out,
5541                    "{}return metal::uint2(b1 << 8 | b0, \
5542                                           b3 << 8 | b2);",
5543                    back::INDENT
5544                )?;
5545                writeln!(self.out, "}}")?;
5546                Ok((name, 4, Some(VectorSize::Bi), Scalar::U32))
5547            }
5548            Uint16x4 => {
5549                let name = self.namer.call("unpackUint16x4");
5550                writeln!(
5551                    self.out,
5552                    "metal::uint4 {name}(metal::uint b0, \
5553                                         metal::uint b1, \
5554                                         metal::uint b2, \
5555                                         metal::uint b3, \
5556                                         metal::uint b4, \
5557                                         metal::uint b5, \
5558                                         metal::uint b6, \
5559                                         metal::uint b7) {{"
5560                )?;
5561                writeln!(
5562                    self.out,
5563                    "{}return metal::uint4(b1 << 8 | b0, \
5564                                           b3 << 8 | b2, \
5565                                           b5 << 8 | b4, \
5566                                           b7 << 8 | b6);",
5567                    back::INDENT
5568                )?;
5569                writeln!(self.out, "}}")?;
5570                Ok((name, 8, Some(VectorSize::Quad), Scalar::U32))
5571            }
5572            Sint16 => {
5573                let name = self.namer.call("unpackSint16");
5574                writeln!(
5575                    self.out,
5576                    "int {name}(metal::ushort b0, \
5577                                metal::ushort b1) {{"
5578                )?;
5579                writeln!(
5580                    self.out,
5581                    "{}return int(as_type<short>(metal::ushort(b1 << 8 | b0)));",
5582                    back::INDENT
5583                )?;
5584                writeln!(self.out, "}}")?;
5585                Ok((name, 2, None, Scalar::I32))
5586            }
5587            Sint16x2 => {
5588                let name = self.namer.call("unpackSint16x2");
5589                writeln!(
5590                    self.out,
5591                    "metal::int2 {name}(metal::ushort b0, \
5592                                        metal::ushort b1, \
5593                                        metal::ushort b2, \
5594                                        metal::ushort b3) {{"
5595                )?;
5596                writeln!(
5597                    self.out,
5598                    "{}return metal::int2(as_type<short>(metal::ushort(b1 << 8 | b0)), \
5599                                          as_type<short>(metal::ushort(b3 << 8 | b2)));",
5600                    back::INDENT
5601                )?;
5602                writeln!(self.out, "}}")?;
5603                Ok((name, 4, Some(VectorSize::Bi), Scalar::I32))
5604            }
5605            Sint16x4 => {
5606                let name = self.namer.call("unpackSint16x4");
5607                writeln!(
5608                    self.out,
5609                    "metal::int4 {name}(metal::ushort b0, \
5610                                        metal::ushort b1, \
5611                                        metal::ushort b2, \
5612                                        metal::ushort b3, \
5613                                        metal::ushort b4, \
5614                                        metal::ushort b5, \
5615                                        metal::ushort b6, \
5616                                        metal::ushort b7) {{"
5617                )?;
5618                writeln!(
5619                    self.out,
5620                    "{}return metal::int4(as_type<short>(metal::ushort(b1 << 8 | b0)), \
5621                                          as_type<short>(metal::ushort(b3 << 8 | b2)), \
5622                                          as_type<short>(metal::ushort(b5 << 8 | b4)), \
5623                                          as_type<short>(metal::ushort(b7 << 8 | b6)));",
5624                    back::INDENT
5625                )?;
5626                writeln!(self.out, "}}")?;
5627                Ok((name, 8, Some(VectorSize::Quad), Scalar::I32))
5628            }
5629            Unorm16 => {
5630                let name = self.namer.call("unpackUnorm16");
5631                writeln!(
5632                    self.out,
5633                    "float {name}(metal::ushort b0, \
5634                                  metal::ushort b1) {{"
5635                )?;
5636                writeln!(
5637                    self.out,
5638                    "{}return float(float(b1 << 8 | b0) / 65535.0f);",
5639                    back::INDENT
5640                )?;
5641                writeln!(self.out, "}}")?;
5642                Ok((name, 2, None, Scalar::F32))
5643            }
5644            Unorm16x2 => {
5645                let name = self.namer.call("unpackUnorm16x2");
5646                writeln!(
5647                    self.out,
5648                    "metal::float2 {name}(metal::ushort b0, \
5649                                          metal::ushort b1, \
5650                                          metal::ushort b2, \
5651                                          metal::ushort b3) {{"
5652                )?;
5653                writeln!(
5654                    self.out,
5655                    "{}return metal::float2(float(b1 << 8 | b0) / 65535.0f, \
5656                                            float(b3 << 8 | b2) / 65535.0f);",
5657                    back::INDENT
5658                )?;
5659                writeln!(self.out, "}}")?;
5660                Ok((name, 4, Some(VectorSize::Bi), Scalar::F32))
5661            }
5662            Unorm16x4 => {
5663                let name = self.namer.call("unpackUnorm16x4");
5664                writeln!(
5665                    self.out,
5666                    "metal::float4 {name}(metal::ushort b0, \
5667                                          metal::ushort b1, \
5668                                          metal::ushort b2, \
5669                                          metal::ushort b3, \
5670                                          metal::ushort b4, \
5671                                          metal::ushort b5, \
5672                                          metal::ushort b6, \
5673                                          metal::ushort b7) {{"
5674                )?;
5675                writeln!(
5676                    self.out,
5677                    "{}return metal::float4(float(b1 << 8 | b0) / 65535.0f, \
5678                                            float(b3 << 8 | b2) / 65535.0f, \
5679                                            float(b5 << 8 | b4) / 65535.0f, \
5680                                            float(b7 << 8 | b6) / 65535.0f);",
5681                    back::INDENT
5682                )?;
5683                writeln!(self.out, "}}")?;
5684                Ok((name, 8, Some(VectorSize::Quad), Scalar::F32))
5685            }
5686            Snorm16 => {
5687                let name = self.namer.call("unpackSnorm16");
5688                writeln!(
5689                    self.out,
5690                    "float {name}(metal::ushort b0, \
5691                                  metal::ushort b1) {{"
5692                )?;
5693                writeln!(
5694                    self.out,
5695                    "{}return metal::unpack_snorm2x16_to_float(b1 << 8 | b0).x;",
5696                    back::INDENT
5697                )?;
5698                writeln!(self.out, "}}")?;
5699                Ok((name, 2, None, Scalar::F32))
5700            }
5701            Snorm16x2 => {
5702                let name = self.namer.call("unpackSnorm16x2");
5703                writeln!(
5704                    self.out,
5705                    "metal::float2 {name}(uint b0, \
5706                                          uint b1, \
5707                                          uint b2, \
5708                                          uint b3) {{"
5709                )?;
5710                writeln!(
5711                    self.out,
5712                    "{}return metal::unpack_snorm2x16_to_float(b3 << 24 | b2 << 16 | b1 << 8 | b0);",
5713                    back::INDENT
5714                )?;
5715                writeln!(self.out, "}}")?;
5716                Ok((name, 4, Some(VectorSize::Bi), Scalar::F32))
5717            }
5718            Snorm16x4 => {
5719                let name = self.namer.call("unpackSnorm16x4");
5720                writeln!(
5721                    self.out,
5722                    "metal::float4 {name}(uint b0, \
5723                                          uint b1, \
5724                                          uint b2, \
5725                                          uint b3, \
5726                                          uint b4, \
5727                                          uint b5, \
5728                                          uint b6, \
5729                                          uint b7) {{"
5730                )?;
5731                writeln!(
5732                    self.out,
5733                    "{}return metal::float4(metal::unpack_snorm2x16_to_float(b3 << 24 | b2 << 16 | b1 << 8 | b0), \
5734                                            metal::unpack_snorm2x16_to_float(b7 << 24 | b6 << 16 | b5 << 8 | b4));",
5735                    back::INDENT
5736                )?;
5737                writeln!(self.out, "}}")?;
5738                Ok((name, 8, Some(VectorSize::Quad), Scalar::F32))
5739            }
5740            Float16 => {
5741                let name = self.namer.call("unpackFloat16");
5742                writeln!(
5743                    self.out,
5744                    "float {name}(metal::ushort b0, \
5745                                  metal::ushort b1) {{"
5746                )?;
5747                writeln!(
5748                    self.out,
5749                    "{}return float(as_type<half>(metal::ushort(b1 << 8 | b0)));",
5750                    back::INDENT
5751                )?;
5752                writeln!(self.out, "}}")?;
5753                Ok((name, 2, None, Scalar::F32))
5754            }
5755            Float16x2 => {
5756                let name = self.namer.call("unpackFloat16x2");
5757                writeln!(
5758                    self.out,
5759                    "metal::float2 {name}(metal::ushort b0, \
5760                                          metal::ushort b1, \
5761                                          metal::ushort b2, \
5762                                          metal::ushort b3) {{"
5763                )?;
5764                writeln!(
5765                    self.out,
5766                    "{}return metal::float2(as_type<half>(metal::ushort(b1 << 8 | b0)), \
5767                                            as_type<half>(metal::ushort(b3 << 8 | b2)));",
5768                    back::INDENT
5769                )?;
5770                writeln!(self.out, "}}")?;
5771                Ok((name, 4, Some(VectorSize::Bi), Scalar::F32))
5772            }
5773            Float16x4 => {
5774                let name = self.namer.call("unpackFloat16x4");
5775                writeln!(
5776                    self.out,
5777                    "metal::float4 {name}(metal::ushort b0, \
5778                                        metal::ushort b1, \
5779                                        metal::ushort b2, \
5780                                        metal::ushort b3, \
5781                                        metal::ushort b4, \
5782                                        metal::ushort b5, \
5783                                        metal::ushort b6, \
5784                                        metal::ushort b7) {{"
5785                )?;
5786                writeln!(
5787                    self.out,
5788                    "{}return metal::float4(as_type<half>(metal::ushort(b1 << 8 | b0)), \
5789                                          as_type<half>(metal::ushort(b3 << 8 | b2)), \
5790                                          as_type<half>(metal::ushort(b5 << 8 | b4)), \
5791                                          as_type<half>(metal::ushort(b7 << 8 | b6)));",
5792                    back::INDENT
5793                )?;
5794                writeln!(self.out, "}}")?;
5795                Ok((name, 8, Some(VectorSize::Quad), Scalar::F32))
5796            }
5797            Float32 => {
5798                let name = self.namer.call("unpackFloat32");
5799                writeln!(
5800                    self.out,
5801                    "float {name}(uint b0, \
5802                                  uint b1, \
5803                                  uint b2, \
5804                                  uint b3) {{"
5805                )?;
5806                writeln!(
5807                    self.out,
5808                    "{}return as_type<float>(b3 << 24 | b2 << 16 | b1 << 8 | b0);",
5809                    back::INDENT
5810                )?;
5811                writeln!(self.out, "}}")?;
5812                Ok((name, 4, None, Scalar::F32))
5813            }
5814            Float32x2 => {
5815                let name = self.namer.call("unpackFloat32x2");
5816                writeln!(
5817                    self.out,
5818                    "metal::float2 {name}(uint b0, \
5819                                          uint b1, \
5820                                          uint b2, \
5821                                          uint b3, \
5822                                          uint b4, \
5823                                          uint b5, \
5824                                          uint b6, \
5825                                          uint b7) {{"
5826                )?;
5827                writeln!(
5828                    self.out,
5829                    "{}return metal::float2(as_type<float>(b3 << 24 | b2 << 16 | b1 << 8 | b0), \
5830                                            as_type<float>(b7 << 24 | b6 << 16 | b5 << 8 | b4));",
5831                    back::INDENT
5832                )?;
5833                writeln!(self.out, "}}")?;
5834                Ok((name, 8, Some(VectorSize::Bi), Scalar::F32))
5835            }
5836            Float32x3 => {
5837                let name = self.namer.call("unpackFloat32x3");
5838                writeln!(
5839                    self.out,
5840                    "metal::float3 {name}(uint b0, \
5841                                          uint b1, \
5842                                          uint b2, \
5843                                          uint b3, \
5844                                          uint b4, \
5845                                          uint b5, \
5846                                          uint b6, \
5847                                          uint b7, \
5848                                          uint b8, \
5849                                          uint b9, \
5850                                          uint b10, \
5851                                          uint b11) {{"
5852                )?;
5853                writeln!(
5854                    self.out,
5855                    "{}return metal::float3(as_type<float>(b3 << 24 | b2 << 16 | b1 << 8 | b0), \
5856                                            as_type<float>(b7 << 24 | b6 << 16 | b5 << 8 | b4), \
5857                                            as_type<float>(b11 << 24 | b10 << 16 | b9 << 8 | b8));",
5858                    back::INDENT
5859                )?;
5860                writeln!(self.out, "}}")?;
5861                Ok((name, 12, Some(VectorSize::Tri), Scalar::F32))
5862            }
5863            Float32x4 => {
5864                let name = self.namer.call("unpackFloat32x4");
5865                writeln!(
5866                    self.out,
5867                    "metal::float4 {name}(uint b0, \
5868                                          uint b1, \
5869                                          uint b2, \
5870                                          uint b3, \
5871                                          uint b4, \
5872                                          uint b5, \
5873                                          uint b6, \
5874                                          uint b7, \
5875                                          uint b8, \
5876                                          uint b9, \
5877                                          uint b10, \
5878                                          uint b11, \
5879                                          uint b12, \
5880                                          uint b13, \
5881                                          uint b14, \
5882                                          uint b15) {{"
5883                )?;
5884                writeln!(
5885                    self.out,
5886                    "{}return metal::float4(as_type<float>(b3 << 24 | b2 << 16 | b1 << 8 | b0), \
5887                                            as_type<float>(b7 << 24 | b6 << 16 | b5 << 8 | b4), \
5888                                            as_type<float>(b11 << 24 | b10 << 16 | b9 << 8 | b8), \
5889                                            as_type<float>(b15 << 24 | b14 << 16 | b13 << 8 | b12));",
5890                    back::INDENT
5891                )?;
5892                writeln!(self.out, "}}")?;
5893                Ok((name, 16, Some(VectorSize::Quad), Scalar::F32))
5894            }
5895            Uint32 => {
5896                let name = self.namer.call("unpackUint32");
5897                writeln!(
5898                    self.out,
5899                    "uint {name}(uint b0, \
5900                                 uint b1, \
5901                                 uint b2, \
5902                                 uint b3) {{"
5903                )?;
5904                writeln!(
5905                    self.out,
5906                    "{}return (b3 << 24 | b2 << 16 | b1 << 8 | b0);",
5907                    back::INDENT
5908                )?;
5909                writeln!(self.out, "}}")?;
5910                Ok((name, 4, None, Scalar::U32))
5911            }
5912            Uint32x2 => {
5913                let name = self.namer.call("unpackUint32x2");
5914                writeln!(
5915                    self.out,
5916                    "uint2 {name}(uint b0, \
5917                                  uint b1, \
5918                                  uint b2, \
5919                                  uint b3, \
5920                                  uint b4, \
5921                                  uint b5, \
5922                                  uint b6, \
5923                                  uint b7) {{"
5924                )?;
5925                writeln!(
5926                    self.out,
5927                    "{}return uint2((b3 << 24 | b2 << 16 | b1 << 8 | b0), \
5928                                    (b7 << 24 | b6 << 16 | b5 << 8 | b4));",
5929                    back::INDENT
5930                )?;
5931                writeln!(self.out, "}}")?;
5932                Ok((name, 8, Some(VectorSize::Bi), Scalar::U32))
5933            }
5934            Uint32x3 => {
5935                let name = self.namer.call("unpackUint32x3");
5936                writeln!(
5937                    self.out,
5938                    "uint3 {name}(uint b0, \
5939                                  uint b1, \
5940                                  uint b2, \
5941                                  uint b3, \
5942                                  uint b4, \
5943                                  uint b5, \
5944                                  uint b6, \
5945                                  uint b7, \
5946                                  uint b8, \
5947                                  uint b9, \
5948                                  uint b10, \
5949                                  uint b11) {{"
5950                )?;
5951                writeln!(
5952                    self.out,
5953                    "{}return uint3((b3 << 24 | b2 << 16 | b1 << 8 | b0), \
5954                                    (b7 << 24 | b6 << 16 | b5 << 8 | b4), \
5955                                    (b11 << 24 | b10 << 16 | b9 << 8 | b8));",
5956                    back::INDENT
5957                )?;
5958                writeln!(self.out, "}}")?;
5959                Ok((name, 12, Some(VectorSize::Tri), Scalar::U32))
5960            }
5961            Uint32x4 => {
5962                let name = self.namer.call("unpackUint32x4");
5963                writeln!(
5964                    self.out,
5965                    "{NAMESPACE}::uint4 {name}(uint b0, \
5966                                  uint b1, \
5967                                  uint b2, \
5968                                  uint b3, \
5969                                  uint b4, \
5970                                  uint b5, \
5971                                  uint b6, \
5972                                  uint b7, \
5973                                  uint b8, \
5974                                  uint b9, \
5975                                  uint b10, \
5976                                  uint b11, \
5977                                  uint b12, \
5978                                  uint b13, \
5979                                  uint b14, \
5980                                  uint b15) {{"
5981                )?;
5982                writeln!(
5983                    self.out,
5984                    "{}return {NAMESPACE}::uint4((b3 << 24 | b2 << 16 | b1 << 8 | b0), \
5985                                    (b7 << 24 | b6 << 16 | b5 << 8 | b4), \
5986                                    (b11 << 24 | b10 << 16 | b9 << 8 | b8), \
5987                                    (b15 << 24 | b14 << 16 | b13 << 8 | b12));",
5988                    back::INDENT
5989                )?;
5990                writeln!(self.out, "}}")?;
5991                Ok((name, 16, Some(VectorSize::Quad), Scalar::U32))
5992            }
5993            Sint32 => {
5994                let name = self.namer.call("unpackSint32");
5995                writeln!(
5996                    self.out,
5997                    "int {name}(uint b0, \
5998                                uint b1, \
5999                                uint b2, \
6000                                uint b3) {{"
6001                )?;
6002                writeln!(
6003                    self.out,
6004                    "{}return as_type<int>(b3 << 24 | b2 << 16 | b1 << 8 | b0);",
6005                    back::INDENT
6006                )?;
6007                writeln!(self.out, "}}")?;
6008                Ok((name, 4, None, Scalar::I32))
6009            }
6010            Sint32x2 => {
6011                let name = self.namer.call("unpackSint32x2");
6012                writeln!(
6013                    self.out,
6014                    "metal::int2 {name}(uint b0, \
6015                                        uint b1, \
6016                                        uint b2, \
6017                                        uint b3, \
6018                                        uint b4, \
6019                                        uint b5, \
6020                                        uint b6, \
6021                                        uint b7) {{"
6022                )?;
6023                writeln!(
6024                    self.out,
6025                    "{}return metal::int2(as_type<int>(b3 << 24 | b2 << 16 | b1 << 8 | b0), \
6026                                          as_type<int>(b7 << 24 | b6 << 16 | b5 << 8 | b4));",
6027                    back::INDENT
6028                )?;
6029                writeln!(self.out, "}}")?;
6030                Ok((name, 8, Some(VectorSize::Bi), Scalar::I32))
6031            }
6032            Sint32x3 => {
6033                let name = self.namer.call("unpackSint32x3");
6034                writeln!(
6035                    self.out,
6036                    "metal::int3 {name}(uint b0, \
6037                                        uint b1, \
6038                                        uint b2, \
6039                                        uint b3, \
6040                                        uint b4, \
6041                                        uint b5, \
6042                                        uint b6, \
6043                                        uint b7, \
6044                                        uint b8, \
6045                                        uint b9, \
6046                                        uint b10, \
6047                                        uint b11) {{"
6048                )?;
6049                writeln!(
6050                    self.out,
6051                    "{}return metal::int3(as_type<int>(b3 << 24 | b2 << 16 | b1 << 8 | b0), \
6052                                          as_type<int>(b7 << 24 | b6 << 16 | b5 << 8 | b4), \
6053                                          as_type<int>(b11 << 24 | b10 << 16 | b9 << 8 | b8));",
6054                    back::INDENT
6055                )?;
6056                writeln!(self.out, "}}")?;
6057                Ok((name, 12, Some(VectorSize::Tri), Scalar::I32))
6058            }
6059            Sint32x4 => {
6060                let name = self.namer.call("unpackSint32x4");
6061                writeln!(
6062                    self.out,
6063                    "metal::int4 {name}(uint b0, \
6064                                        uint b1, \
6065                                        uint b2, \
6066                                        uint b3, \
6067                                        uint b4, \
6068                                        uint b5, \
6069                                        uint b6, \
6070                                        uint b7, \
6071                                        uint b8, \
6072                                        uint b9, \
6073                                        uint b10, \
6074                                        uint b11, \
6075                                        uint b12, \
6076                                        uint b13, \
6077                                        uint b14, \
6078                                        uint b15) {{"
6079                )?;
6080                writeln!(
6081                    self.out,
6082                    "{}return metal::int4(as_type<int>(b3 << 24 | b2 << 16 | b1 << 8 | b0), \
6083                                          as_type<int>(b7 << 24 | b6 << 16 | b5 << 8 | b4), \
6084                                          as_type<int>(b11 << 24 | b10 << 16 | b9 << 8 | b8), \
6085                                          as_type<int>(b15 << 24 | b14 << 16 | b13 << 8 | b12));",
6086                    back::INDENT
6087                )?;
6088                writeln!(self.out, "}}")?;
6089                Ok((name, 16, Some(VectorSize::Quad), Scalar::I32))
6090            }
6091            Unorm10_10_10_2 => {
6092                let name = self.namer.call("unpackUnorm10_10_10_2");
6093                writeln!(
6094                    self.out,
6095                    "metal::float4 {name}(uint b0, \
6096                                          uint b1, \
6097                                          uint b2, \
6098                                          uint b3) {{"
6099                )?;
6100                writeln!(
6101                    self.out,
6102                    // The following is correct for RGBA packing, but our format seems to
6103                    // match ABGR, which can be fed into the Metal builtin function
6104                    // unpack_unorm10a2_to_float.
6105                    /*
6106                    "{}uint v = (b3 << 24 | b2 << 16 | b1 << 8 | b0); \
6107                       uint r = (v & 0xFFC00000) >> 22; \
6108                       uint g = (v & 0x003FF000) >> 12; \
6109                       uint b = (v & 0x00000FFC) >> 2; \
6110                       uint a = (v & 0x00000003); \
6111                       return metal::float4(float(r) / 1023.0f, float(g) / 1023.0f, float(b) / 1023.0f, float(a) / 3.0f);",
6112                    */
6113                    "{}return metal::unpack_unorm10a2_to_float(b3 << 24 | b2 << 16 | b1 << 8 | b0);",
6114                    back::INDENT
6115                )?;
6116                writeln!(self.out, "}}")?;
6117                Ok((name, 4, Some(VectorSize::Quad), Scalar::F32))
6118            }
6119            Snorm10_10_10_2 => {
6120                let name = self.namer.call("unpackSnorm10_10_10_2");
6121                writeln!(
6122                    self.out,
6123                    "metal::float4 {name}(uint b0, \
6124                                          uint b1, \
6125                                          uint b2, \
6126                                          uint b3) {{"
6127                )?;
6128                writeln!(
6129                    self.out,
6130                    // Sign-extend by shifting to the top of the word and back.
6131                    "{}uint v = (b3 << 24 | b2 << 16 | b1 << 8 | b0); \
6132                       return metal::max(metal::float4(float(as_type<int>(v << 22) >> 22) / 511.0f, \
6133                                                       float(as_type<int>(v << 12) >> 22) / 511.0f, \
6134                                                       float(as_type<int>(v << 2) >> 22) / 511.0f, \
6135                                                       float(as_type<int>(v) >> 30)), \
6136                                         metal::float4(-1.0f));",
6137                    back::INDENT
6138                )?;
6139                writeln!(self.out, "}}")?;
6140                Ok((name, 4, Some(VectorSize::Quad), Scalar::F32))
6141            }
6142            Unorm8x4Bgra => {
6143                let name = self.namer.call("unpackUnorm8x4Bgra");
6144                writeln!(
6145                    self.out,
6146                    "metal::float4 {name}(metal::uchar b0, \
6147                                          metal::uchar b1, \
6148                                          metal::uchar b2, \
6149                                          metal::uchar b3) {{"
6150                )?;
6151                writeln!(
6152                    self.out,
6153                    "{}return metal::float4(float(b2) / 255.0f, \
6154                                            float(b1) / 255.0f, \
6155                                            float(b0) / 255.0f, \
6156                                            float(b3) / 255.0f);",
6157                    back::INDENT
6158                )?;
6159                writeln!(self.out, "}}")?;
6160                Ok((name, 4, Some(VectorSize::Quad), Scalar::F32))
6161            }
6162            Float64 | Float64x2 | Float64x3 | Float64x4 => unreachable!(),
6163        }
6164    }
6165
6166    fn write_wrapped_unary_op(
6167        &mut self,
6168        module: &crate::Module,
6169        func_ctx: &back::FunctionCtx,
6170        op: crate::UnaryOperator,
6171        operand: Handle<crate::Expression>,
6172    ) -> BackendResult {
6173        let operand_ty = func_ctx.resolve_type(operand, &module.types);
6174        match op {
6175            // Negating the TYPE_MIN of a two's complement signed integer
6176            // type causes overflow, which is undefined behaviour in MSL. To
6177            // avoid this we bitcast the value to unsigned and negate it,
6178            // then bitcast back to signed.
6179            // This adheres to the WGSL spec in that the negative of the
6180            // type's minimum value should equal to the minimum value.
6181            crate::UnaryOperator::Negate
6182                if operand_ty.scalar_kind() == Some(crate::ScalarKind::Sint) =>
6183            {
6184                let Some((vector_size, scalar)) = operand_ty.vector_size_and_scalar() else {
6185                    return Ok(());
6186                };
6187                let wrapped = WrappedFunction::UnaryOp {
6188                    op,
6189                    ty: (vector_size, scalar),
6190                };
6191                if !self.wrapped_functions.insert(wrapped) {
6192                    return Ok(());
6193                }
6194
6195                let unsigned_scalar = crate::Scalar {
6196                    kind: crate::ScalarKind::Uint,
6197                    ..scalar
6198                };
6199                let mut type_name = String::new();
6200                let mut unsigned_type_name = String::new();
6201                match vector_size {
6202                    None => {
6203                        put_numeric_type(&mut type_name, scalar, &[])?;
6204                        put_numeric_type(&mut unsigned_type_name, unsigned_scalar, &[])?
6205                    }
6206                    Some(size) => {
6207                        put_numeric_type(&mut type_name, scalar, &[size])?;
6208                        put_numeric_type(&mut unsigned_type_name, unsigned_scalar, &[size])?;
6209                    }
6210                };
6211
6212                writeln!(self.out, "{type_name} {NEG_FUNCTION}({type_name} val) {{")?;
6213                let level = back::Level(1);
6214                // For sub-32-bit types, C++ integer promotion widens
6215                // `-as_type<ushort>(val)` to `int`, so we need static_cast
6216                // to truncate back before the outer as_type bitcast.
6217                if scalar.width < 4 {
6218                    writeln!(
6219                        self.out,
6220                        "{level}return as_type<{type_name}>(static_cast<{unsigned_type_name}>(-as_type<{unsigned_type_name}>(val)));"
6221                    )?;
6222                } else {
6223                    writeln!(
6224                        self.out,
6225                        "{level}return as_type<{type_name}>(-as_type<{unsigned_type_name}>(val));"
6226                    )?;
6227                }
6228                writeln!(self.out, "}}")?;
6229                writeln!(self.out)?;
6230            }
6231            _ => {}
6232        }
6233        Ok(())
6234    }
6235
6236    fn write_wrapped_binary_op(
6237        &mut self,
6238        module: &crate::Module,
6239        func_ctx: &back::FunctionCtx,
6240        expr: Handle<crate::Expression>,
6241        op: crate::BinaryOperator,
6242        left: Handle<crate::Expression>,
6243        right: Handle<crate::Expression>,
6244    ) -> BackendResult {
6245        let expr_ty = func_ctx.resolve_type(expr, &module.types);
6246        let left_ty = func_ctx.resolve_type(left, &module.types);
6247        let right_ty = func_ctx.resolve_type(right, &module.types);
6248        match (op, expr_ty.scalar_kind()) {
6249            // Signed integer division of TYPE_MIN / -1, or signed or
6250            // unsigned division by zero, gives an unspecified value in MSL.
6251            // We override the divisor to 1 in these cases.
6252            // This adheres to the WGSL spec in that:
6253            // * TYPE_MIN / -1 == TYPE_MIN
6254            // * x / 0 == x
6255            (
6256                crate::BinaryOperator::Divide,
6257                Some(crate::ScalarKind::Sint | crate::ScalarKind::Uint),
6258            ) if self.emit_int_div_checks => {
6259                let Some(left_wrapped_ty) = left_ty.vector_size_and_scalar() else {
6260                    return Ok(());
6261                };
6262                let Some(right_wrapped_ty) = right_ty.vector_size_and_scalar() else {
6263                    return Ok(());
6264                };
6265                let wrapped = WrappedFunction::BinaryOp {
6266                    op,
6267                    left_ty: left_wrapped_ty,
6268                    right_ty: right_wrapped_ty,
6269                };
6270                if !self.wrapped_functions.insert(wrapped) {
6271                    return Ok(());
6272                }
6273
6274                let Some((vector_size, scalar)) = expr_ty.vector_size_and_scalar() else {
6275                    return Ok(());
6276                };
6277                let mut type_name = String::new();
6278                match vector_size {
6279                    None => put_numeric_type(&mut type_name, scalar, &[])?,
6280                    Some(size) => put_numeric_type(&mut type_name, scalar, &[size])?,
6281                };
6282                writeln!(
6283                    self.out,
6284                    "{type_name} {DIV_FUNCTION}({type_name} lhs, {type_name} rhs) {{"
6285                )?;
6286                let level = back::Level(1);
6287                // The literal operands of `metal::select` must have the same
6288                // scalar type as `rhs`, otherwise the call is ambiguous.
6289                // Sub-32-bit types need typed literal wrappers (e.g.
6290                // `short(1)`), and 64-bit types need suffixed literals (e.g.
6291                // `1L` / `1uL`). Bare `1` / `1u` are only correct for 32-bit.
6292                let (lp, rp) = if scalar.width < 4 {
6293                    (format!("{type_name}("), ")".to_string())
6294                } else {
6295                    (String::new(), String::new())
6296                };
6297                match scalar.kind {
6298                    crate::ScalarKind::Sint => {
6299                        let (min_val, suffix) = match scalar.width {
6300                            2 => (crate::Literal::I16(i16::MIN), ""),
6301                            4 => (crate::Literal::I32(i32::MIN), ""),
6302                            8 => (crate::Literal::I64(i64::MIN), "L"),
6303                            _ => {
6304                                return Err(Error::GenericValidation(format!(
6305                                    "Unexpected width for scalar {scalar:?}"
6306                                )));
6307                            }
6308                        };
6309                        write!(
6310                            self.out,
6311                            "{level}return lhs / metal::select(rhs, {lp}1{suffix}{rp}, (lhs == "
6312                        )?;
6313                        self.put_literal(min_val)?;
6314                        writeln!(
6315                            self.out,
6316                            " & rhs == {lp}-1{suffix}{rp}) | (rhs == {lp}0{suffix}{rp}));"
6317                        )?
6318                    }
6319                    crate::ScalarKind::Uint => {
6320                        let suffix = match scalar.width {
6321                            8 => "uL",
6322                            4 => "u",
6323                            _ => "",
6324                        };
6325                        writeln!(
6326                            self.out,
6327                            "{level}return lhs / metal::select(rhs, {lp}1{suffix}{rp}, rhs == {lp}0{suffix}{rp});"
6328                        )?
6329                    }
6330                    _ => unreachable!(),
6331                }
6332                writeln!(self.out, "}}")?;
6333                writeln!(self.out)?;
6334            }
6335            // Integer modulo where one or both operands are negative, or the
6336            // divisor is zero, is undefined behaviour in MSL. To avoid this
6337            // we use the following equation:
6338            //
6339            // dividend - (dividend / divisor) * divisor
6340            //
6341            // overriding the divisor to 1 if either it is 0, or it is -1
6342            // and the dividend is TYPE_MIN.
6343            //
6344            // This adheres to the WGSL spec in that:
6345            // * TYPE_MIN % -1 == 0
6346            // * x % 0 == 0
6347            (
6348                crate::BinaryOperator::Modulo,
6349                Some(crate::ScalarKind::Sint | crate::ScalarKind::Uint),
6350            ) if self.emit_int_div_checks => {
6351                let Some(left_wrapped_ty) = left_ty.vector_size_and_scalar() else {
6352                    return Ok(());
6353                };
6354                let Some((right_vector_size, right_scalar)) = right_ty.vector_size_and_scalar()
6355                else {
6356                    return Ok(());
6357                };
6358                let wrapped = WrappedFunction::BinaryOp {
6359                    op,
6360                    left_ty: left_wrapped_ty,
6361                    right_ty: (right_vector_size, right_scalar),
6362                };
6363                if !self.wrapped_functions.insert(wrapped) {
6364                    return Ok(());
6365                }
6366
6367                let Some((vector_size, scalar)) = expr_ty.vector_size_and_scalar() else {
6368                    return Ok(());
6369                };
6370                let mut type_name = String::new();
6371                match vector_size {
6372                    None => put_numeric_type(&mut type_name, scalar, &[])?,
6373                    Some(size) => put_numeric_type(&mut type_name, scalar, &[size])?,
6374                };
6375                let mut rhs_type_name = String::new();
6376                match right_vector_size {
6377                    None => put_numeric_type(&mut rhs_type_name, right_scalar, &[])?,
6378                    Some(size) => put_numeric_type(&mut rhs_type_name, right_scalar, &[size])?,
6379                };
6380
6381                writeln!(
6382                    self.out,
6383                    "{type_name} {MOD_FUNCTION}({type_name} lhs, {type_name} rhs) {{"
6384                )?;
6385                let level = back::Level(1);
6386                // See the corresponding comment in the `Divide` arm above:
6387                // `metal::select` literal operands must match `rhs`'s scalar
6388                // type exactly to avoid overload ambiguity.
6389                let (lp, rp) = if scalar.width < 4 {
6390                    (format!("{type_name}("), ")".to_string())
6391                } else {
6392                    (String::new(), String::new())
6393                };
6394                match scalar.kind {
6395                    crate::ScalarKind::Sint => {
6396                        let (min_val, suffix) = match scalar.width {
6397                            2 => (crate::Literal::I16(i16::MIN), ""),
6398                            4 => (crate::Literal::I32(i32::MIN), ""),
6399                            8 => (crate::Literal::I64(i64::MIN), "L"),
6400                            _ => {
6401                                return Err(Error::GenericValidation(format!(
6402                                    "Unexpected width for scalar {scalar:?}"
6403                                )));
6404                            }
6405                        };
6406                        write!(
6407                            self.out,
6408                            "{level}{rhs_type_name} divisor = metal::select(rhs, {lp}1{suffix}{rp}, (lhs == "
6409                        )?;
6410                        self.put_literal(min_val)?;
6411                        writeln!(
6412                            self.out,
6413                            " & rhs == {lp}-1{suffix}{rp}) | (rhs == {lp}0{suffix}{rp}));"
6414                        )?;
6415                        writeln!(self.out, "{level}return lhs - (lhs / divisor) * divisor;")?
6416                    }
6417                    crate::ScalarKind::Uint => {
6418                        let suffix = match scalar.width {
6419                            8 => "uL",
6420                            4 => "u",
6421                            _ => "",
6422                        };
6423                        writeln!(
6424                            self.out,
6425                            "{level}return lhs % metal::select(rhs, {lp}1{suffix}{rp}, rhs == {lp}0{suffix}{rp});"
6426                        )?
6427                    }
6428                    _ => unreachable!(),
6429                }
6430                writeln!(self.out, "}}")?;
6431                writeln!(self.out)?;
6432            }
6433            _ => {}
6434        }
6435        Ok(())
6436    }
6437
6438    /// Build the mangled helper name for integer vector dot products.
6439    ///
6440    /// `scalar` must be a concrete integer scalar type.
6441    ///
6442    /// Result format: `{DOT_FUNCTION_PREFIX}_{type}{N}` (e.g., `naga_dot_int3`).
6443    fn get_dot_wrapper_function_helper_name(
6444        &self,
6445        scalar: crate::Scalar,
6446        size: crate::VectorSize,
6447    ) -> String {
6448        // Check for consistency with [`super::keywords::RESERVED_SET`]
6449        debug_assert!(concrete_int_scalars().any(|s| s == scalar));
6450
6451        let type_name = scalar.to_msl_name();
6452        let size_suffix = common::vector_size_str(size);
6453        format!("{DOT_FUNCTION_PREFIX}_{type_name}{size_suffix}")
6454    }
6455
6456    #[allow(clippy::too_many_arguments)]
6457    fn write_wrapped_math_function(
6458        &mut self,
6459        module: &crate::Module,
6460        func_ctx: &back::FunctionCtx,
6461        fun: crate::MathFunction,
6462        arg: Handle<crate::Expression>,
6463        _arg1: Option<Handle<crate::Expression>>,
6464        _arg2: Option<Handle<crate::Expression>>,
6465        _arg3: Option<Handle<crate::Expression>>,
6466    ) -> BackendResult {
6467        let arg_ty = func_ctx.resolve_type(arg, &module.types);
6468        match fun {
6469            // Taking the absolute value of the TYPE_MIN of a two's
6470            // complement signed integer type causes overflow, which is
6471            // undefined behaviour in MSL. To avoid this, when the value is
6472            // negative we bitcast the value to unsigned and negate it, then
6473            // bitcast back to signed.
6474            // This adheres to the WGSL spec in that the absolute of the
6475            // type's minimum value should equal to the minimum value.
6476            crate::MathFunction::Abs if arg_ty.scalar_kind() == Some(crate::ScalarKind::Sint) => {
6477                let Some((vector_size, scalar)) = arg_ty.vector_size_and_scalar() else {
6478                    return Ok(());
6479                };
6480                let wrapped = WrappedFunction::Math {
6481                    fun,
6482                    arg_ty: (vector_size, scalar),
6483                };
6484                if !self.wrapped_functions.insert(wrapped) {
6485                    return Ok(());
6486                }
6487
6488                let unsigned_scalar = crate::Scalar {
6489                    kind: crate::ScalarKind::Uint,
6490                    ..scalar
6491                };
6492                let mut type_name = String::new();
6493                let mut unsigned_type_name = String::new();
6494                match vector_size {
6495                    None => {
6496                        put_numeric_type(&mut type_name, scalar, &[])?;
6497                        put_numeric_type(&mut unsigned_type_name, unsigned_scalar, &[])?
6498                    }
6499                    Some(size) => {
6500                        put_numeric_type(&mut type_name, scalar, &[size])?;
6501                        put_numeric_type(&mut unsigned_type_name, unsigned_scalar, &[size])?;
6502                    }
6503                };
6504
6505                writeln!(self.out, "{type_name} {ABS_FUNCTION}({type_name} val) {{")?;
6506                let level = back::Level(1);
6507                let zero = if scalar.width < 4 {
6508                    format!("{type_name}(0)")
6509                } else {
6510                    "0".to_string()
6511                };
6512                let neg_expr = if scalar.width < 4 {
6513                    format!(
6514                        "static_cast<{unsigned_type_name}>(-as_type<{unsigned_type_name}>(val))"
6515                    )
6516                } else {
6517                    format!("-as_type<{unsigned_type_name}>(val)")
6518                };
6519                writeln!(self.out, "{level}return metal::select(as_type<{type_name}>({neg_expr}), val, val >= {zero});")?;
6520                writeln!(self.out, "}}")?;
6521                writeln!(self.out)?;
6522            }
6523
6524            crate::MathFunction::Dot => match *arg_ty {
6525                crate::TypeInner::Vector { size, scalar }
6526                    if matches!(
6527                        scalar.kind,
6528                        crate::ScalarKind::Sint | crate::ScalarKind::Uint
6529                    ) =>
6530                {
6531                    // De-duplicate per (fun, arg type) like other wrapped math functions
6532                    let wrapped = WrappedFunction::Math {
6533                        fun,
6534                        arg_ty: (Some(size), scalar),
6535                    };
6536                    if !self.wrapped_functions.insert(wrapped) {
6537                        return Ok(());
6538                    }
6539
6540                    let mut vec_ty = String::new();
6541                    put_numeric_type(&mut vec_ty, scalar, &[size])?;
6542                    let mut ret_ty = String::new();
6543                    put_numeric_type(&mut ret_ty, scalar, &[])?;
6544
6545                    let fun_name = self.get_dot_wrapper_function_helper_name(scalar, size);
6546
6547                    // Emit function signature and body using put_dot_product for the expression
6548                    writeln!(self.out, "{ret_ty} {fun_name}({vec_ty} a, {vec_ty} b) {{")?;
6549                    let level = back::Level(1);
6550                    write!(self.out, "{level}return ")?;
6551                    self.put_dot_product("a", "b", size as usize, |writer, name, index| {
6552                        write!(writer.out, "{name}.{}", back::COMPONENTS[index])?;
6553                        Ok(())
6554                    })?;
6555                    writeln!(self.out, ";")?;
6556                    writeln!(self.out, "}}")?;
6557                    writeln!(self.out)?;
6558                }
6559                _ => {}
6560            },
6561
6562            _ => {}
6563        }
6564        Ok(())
6565    }
6566
6567    fn write_wrapped_cast(
6568        &mut self,
6569        module: &crate::Module,
6570        func_ctx: &back::FunctionCtx,
6571        expr: Handle<crate::Expression>,
6572        dst_scalar: crate::Scalar,
6573    ) -> BackendResult {
6574        // Avoid undefined behaviour when casting from a float to integer
6575        // when the value is out of range for the target type. Additionally
6576        // ensure we clamp to the correct value as per the WGSL spec.
6577        //
6578        // https://www.w3.org/TR/WGSL/#floating-point-conversion:
6579        // * If X is exactly representable in the target type T, then the
6580        //   result is that value.
6581        // * Otherwise, the result is the value in T closest to
6582        //   truncate(X) and also exactly representable in the original
6583        //   floating point type.
6584        let src_ty = func_ctx.resolve_type(expr, &module.types);
6585        let Some((vector_size, src_scalar)) = src_ty.vector_size_and_scalar() else {
6586            return Ok(());
6587        };
6588        if src_scalar.kind != crate::ScalarKind::Float
6589            || (dst_scalar.kind != crate::ScalarKind::Sint
6590                && dst_scalar.kind != crate::ScalarKind::Uint)
6591        {
6592            return Ok(());
6593        }
6594        let wrapped = WrappedFunction::Cast {
6595            src_scalar,
6596            vector_size,
6597            dst_scalar,
6598        };
6599        if !self.wrapped_functions.insert(wrapped) {
6600            return Ok(());
6601        }
6602        let (min, max) = proc::min_max_float_representable_by(src_scalar, dst_scalar);
6603
6604        let mut src_type_name = String::new();
6605        match vector_size {
6606            None => put_numeric_type(&mut src_type_name, src_scalar, &[])?,
6607            Some(size) => put_numeric_type(&mut src_type_name, src_scalar, &[size])?,
6608        };
6609        let mut dst_type_name = String::new();
6610        match vector_size {
6611            None => put_numeric_type(&mut dst_type_name, dst_scalar, &[])?,
6612            Some(size) => put_numeric_type(&mut dst_type_name, dst_scalar, &[size])?,
6613        };
6614        let fun_name = match dst_scalar {
6615            crate::Scalar::I32 => F2I32_FUNCTION,
6616            crate::Scalar::U32 => F2U32_FUNCTION,
6617            crate::Scalar::I64 => F2I64_FUNCTION,
6618            crate::Scalar::U64 => F2U64_FUNCTION,
6619            _ => unreachable!(),
6620        };
6621
6622        writeln!(
6623            self.out,
6624            "{dst_type_name} {fun_name}({src_type_name} value) {{"
6625        )?;
6626        let level = back::Level(1);
6627        write!(
6628            self.out,
6629            "{level}return static_cast<{dst_type_name}>({NAMESPACE}::clamp(value, "
6630        )?;
6631        self.put_literal(min)?;
6632        write!(self.out, ", ")?;
6633        self.put_literal(max)?;
6634        writeln!(self.out, "));")?;
6635        writeln!(self.out, "}}")?;
6636        writeln!(self.out)?;
6637        Ok(())
6638    }
6639
6640    /// Helper function used by [`Self::write_wrapped_image_load`] and
6641    /// [`Self::write_wrapped_image_sample`] to write the shared YUV to RGB
6642    /// conversion code for external textures. Expects the preceding code to
6643    /// declare the Y component as a `float` variable of name `y`, the UV
6644    /// components as a `float2` variable of name `uv`, and the external
6645    /// texture params as a variable of name `params`. The emitted code will
6646    /// return the result.
6647    fn write_convert_yuv_to_rgb_and_return(
6648        &mut self,
6649        level: back::Level,
6650        y: &str,
6651        uv: &str,
6652        params: &str,
6653    ) -> BackendResult {
6654        let l1 = level;
6655        let l2 = l1.next();
6656
6657        // Convert from YUV to non-linear RGB in the source color space.
6658        writeln!(
6659            self.out,
6660            "{l1}float3 srcGammaRgb = ({params}.yuv_conversion_matrix * float4({y}, {uv}, 1.0)).rgb;"
6661        )?;
6662
6663        // Apply the inverse of the source transfer function to convert to
6664        // linear RGB in the source color space.
6665        writeln!(self.out, "{l1}float3 srcLinearRgb = {NAMESPACE}::select(")?;
6666        writeln!(self.out, "{l2}{NAMESPACE}::pow((srcGammaRgb + {params}.src_tf.a - 1.0) / {params}.src_tf.a, {params}.src_tf.g),")?;
6667        writeln!(self.out, "{l2}srcGammaRgb / {params}.src_tf.k,")?;
6668        writeln!(
6669            self.out,
6670            "{l2}srcGammaRgb < {params}.src_tf.k * {params}.src_tf.b);"
6671        )?;
6672
6673        // Multiply by the gamut conversion matrix to convert to linear RGB in
6674        // the destination color space.
6675        writeln!(
6676            self.out,
6677            "{l1}float3 dstLinearRgb = {params}.gamut_conversion_matrix * srcLinearRgb;"
6678        )?;
6679
6680        // Finally, apply the dest transfer function to convert to non-linear
6681        // RGB in the destination color space, and return the result.
6682        writeln!(self.out, "{l1}float3 dstGammaRgb = {NAMESPACE}::select(")?;
6683        writeln!(self.out, "{l2}{params}.dst_tf.a * {NAMESPACE}::pow(dstLinearRgb, 1.0 / {params}.dst_tf.g) - ({params}.dst_tf.a - 1),")?;
6684        writeln!(self.out, "{l2}{params}.dst_tf.k * dstLinearRgb,")?;
6685        writeln!(self.out, "{l2}dstLinearRgb < {params}.dst_tf.b);")?;
6686
6687        writeln!(self.out, "{l1}return float4(dstGammaRgb, 1.0);")?;
6688        Ok(())
6689    }
6690
6691    #[allow(clippy::too_many_arguments)]
6692    fn write_wrapped_image_load(
6693        &mut self,
6694        module: &crate::Module,
6695        func_ctx: &back::FunctionCtx,
6696        image: Handle<crate::Expression>,
6697        _coordinate: Handle<crate::Expression>,
6698        _array_index: Option<Handle<crate::Expression>>,
6699        _sample: Option<Handle<crate::Expression>>,
6700        _level: Option<Handle<crate::Expression>>,
6701    ) -> BackendResult {
6702        // We currently only need to wrap image loads for external textures
6703        let class = match *func_ctx.resolve_type(image, &module.types) {
6704            crate::TypeInner::Image { class, .. } => class,
6705            _ => unreachable!(),
6706        };
6707        if class != crate::ImageClass::External {
6708            return Ok(());
6709        }
6710        let wrapped = WrappedFunction::ImageLoad { class };
6711        if !self.wrapped_functions.insert(wrapped) {
6712            return Ok(());
6713        }
6714
6715        writeln!(self.out, "float4 {IMAGE_LOAD_EXTERNAL_FUNCTION}({EXTERNAL_TEXTURE_WRAPPER_STRUCT} tex, uint2 coords) {{")?;
6716        let l1 = back::Level(1);
6717        let l2 = l1.next();
6718        let l3 = l2.next();
6719        writeln!(
6720            self.out,
6721            "{l1}uint2 plane0_size = uint2(tex.plane0.get_width(), tex.plane0.get_height());"
6722        )?;
6723        // Clamp coords to provided size of external texture to prevent OOB
6724        // read. If params.size is zero then clamp to the actual size of the
6725        // texture.
6726        writeln!(
6727            self.out,
6728            "{l1}uint2 cropped_size = {NAMESPACE}::any(tex.params.size != 0) ? tex.params.size : plane0_size;"
6729        )?;
6730        writeln!(
6731            self.out,
6732            "{l1}coords = {NAMESPACE}::min(coords, cropped_size - 1);"
6733        )?;
6734
6735        // Apply load transformation
6736        writeln!(self.out, "{l1}uint2 plane0_coords = uint2({NAMESPACE}::round(tex.params.load_transform * float3(float2(coords), 1.0)));")?;
6737        writeln!(self.out, "{l1}if (tex.params.num_planes == 1u) {{")?;
6738        // For single plane, simply read from plane0
6739        writeln!(self.out, "{l2}return tex.plane0.read(plane0_coords);")?;
6740        writeln!(self.out, "{l1}}} else {{")?;
6741
6742        // Chroma planes may be subsampled so we must scale the coords accordingly.
6743        writeln!(
6744            self.out,
6745            "{l2}uint2 plane1_size = uint2(tex.plane1.get_width(), tex.plane1.get_height());"
6746        )?;
6747        writeln!(self.out, "{l2}uint2 plane1_coords = uint2({NAMESPACE}::floor(float2(plane0_coords) * float2(plane1_size) / float2(plane0_size)));")?;
6748
6749        // For multi-plane, read the Y value from plane 0
6750        writeln!(self.out, "{l2}float y = tex.plane0.read(plane0_coords).x;")?;
6751
6752        writeln!(self.out, "{l2}float2 uv;")?;
6753        writeln!(self.out, "{l2}if (tex.params.num_planes == 2u) {{")?;
6754        // For 2 planes, read UV from interleaved plane 1
6755        writeln!(self.out, "{l3}uv = tex.plane1.read(plane1_coords).xy;")?;
6756        writeln!(self.out, "{l2}}} else {{")?;
6757        // For 3 planes, read U and V from planes 1 and 2 respectively
6758        writeln!(
6759            self.out,
6760            "{l2}uint2 plane2_size = uint2(tex.plane2.get_width(), tex.plane2.get_height());"
6761        )?;
6762        writeln!(self.out, "{l2}uint2 plane2_coords = uint2({NAMESPACE}::floor(float2(plane0_coords) * float2(plane2_size) / float2(plane0_size)));")?;
6763        writeln!(
6764            self.out,
6765            "{l3}uv = float2(tex.plane1.read(plane1_coords).x, tex.plane2.read(plane2_coords).x);"
6766        )?;
6767        writeln!(self.out, "{l2}}}")?;
6768
6769        self.write_convert_yuv_to_rgb_and_return(l2, "y", "uv", "tex.params")?;
6770
6771        writeln!(self.out, "{l1}}}")?;
6772        writeln!(self.out, "}}")?;
6773        writeln!(self.out)?;
6774        Ok(())
6775    }
6776
6777    #[allow(clippy::too_many_arguments)]
6778    fn write_wrapped_image_sample(
6779        &mut self,
6780        module: &crate::Module,
6781        func_ctx: &back::FunctionCtx,
6782        image: Handle<crate::Expression>,
6783        _sampler: Handle<crate::Expression>,
6784        _gather: Option<crate::SwizzleComponent>,
6785        _coordinate: Handle<crate::Expression>,
6786        _array_index: Option<Handle<crate::Expression>>,
6787        _offset: Option<Handle<crate::Expression>>,
6788        _level: crate::SampleLevel,
6789        _depth_ref: Option<Handle<crate::Expression>>,
6790        clamp_to_edge: bool,
6791    ) -> BackendResult {
6792        // We currently only need to wrap textureSampleBaseClampToEdge, for
6793        // both sampled and external textures.
6794        if !clamp_to_edge {
6795            return Ok(());
6796        }
6797        let class = match *func_ctx.resolve_type(image, &module.types) {
6798            crate::TypeInner::Image { class, .. } => class,
6799            _ => unreachable!(),
6800        };
6801        let wrapped = WrappedFunction::ImageSample {
6802            class,
6803            clamp_to_edge: true,
6804        };
6805        if !self.wrapped_functions.insert(wrapped) {
6806            return Ok(());
6807        }
6808        match class {
6809            crate::ImageClass::External => {
6810                writeln!(self.out, "float4 {IMAGE_SAMPLE_BASE_CLAMP_TO_EDGE_FUNCTION}({EXTERNAL_TEXTURE_WRAPPER_STRUCT} tex, {NAMESPACE}::sampler samp, float2 coords) {{")?;
6811                let l1 = back::Level(1);
6812                let l2 = l1.next();
6813                let l3 = l2.next();
6814                writeln!(self.out, "{l1}uint2 plane0_size = uint2(tex.plane0.get_width(), tex.plane0.get_height());")?;
6815                writeln!(
6816                    self.out,
6817                    "{l1}coords = tex.params.sample_transform * float3(coords, 1.0);"
6818                )?;
6819
6820                // Calculate the sample bounds. The purported size of the texture
6821                // (params.size) is irrelevant here as we are dealing with normalized
6822                // coordinates. Usually we would clamp to (0,0)..(1,1). However, we must
6823                // apply the sample transformation to that, also bearing in mind that it
6824                // may contain a flip on either axis. We calculate and adjust for the
6825                // half-texel separately for each plane as it depends on the actual
6826                // texture size which may vary between planes.
6827                writeln!(
6828                    self.out,
6829                    "{l1}float2 bounds_min = tex.params.sample_transform * float3(0.0, 0.0, 1.0);"
6830                )?;
6831                writeln!(
6832                    self.out,
6833                    "{l1}float2 bounds_max = tex.params.sample_transform * float3(1.0, 1.0, 1.0);"
6834                )?;
6835                writeln!(self.out, "{l1}float4 bounds = float4({NAMESPACE}::min(bounds_min, bounds_max), {NAMESPACE}::max(bounds_min, bounds_max));")?;
6836                writeln!(
6837                    self.out,
6838                    "{l1}float2 plane0_half_texel = float2(0.5, 0.5) / float2(plane0_size);"
6839                )?;
6840                writeln!(
6841                    self.out,
6842                    "{l1}float2 plane0_coords = {NAMESPACE}::clamp(coords, bounds.xy + plane0_half_texel, bounds.zw - plane0_half_texel);"
6843                )?;
6844                writeln!(self.out, "{l1}if (tex.params.num_planes == 1u) {{")?;
6845                // For single plane, simply sample from plane0
6846                writeln!(
6847                    self.out,
6848                    "{l2}return tex.plane0.sample(samp, plane0_coords, {NAMESPACE}::level(0.0f));"
6849                )?;
6850                writeln!(self.out, "{l1}}} else {{")?;
6851                writeln!(self.out, "{l2}uint2 plane1_size = uint2(tex.plane1.get_width(), tex.plane1.get_height());")?;
6852                writeln!(
6853                    self.out,
6854                    "{l2}float2 plane1_half_texel = float2(0.5, 0.5) / float2(plane1_size);"
6855                )?;
6856                writeln!(
6857                    self.out,
6858                    "{l2}float2 plane1_coords = {NAMESPACE}::clamp(coords, bounds.xy + plane1_half_texel, bounds.zw - plane1_half_texel);"
6859                )?;
6860
6861                // For multi-plane, sample the Y value from plane 0
6862                writeln!(
6863                    self.out,
6864                    "{l2}float y = tex.plane0.sample(samp, plane0_coords, {NAMESPACE}::level(0.0f)).r;"
6865                )?;
6866                writeln!(self.out, "{l2}float2 uv = float2(0.0, 0.0);")?;
6867                writeln!(self.out, "{l2}if (tex.params.num_planes == 2u) {{")?;
6868                // For 2 planes, sample UV from interleaved plane 1
6869                writeln!(
6870                    self.out,
6871                    "{l3}uv = tex.plane1.sample(samp, plane1_coords, {NAMESPACE}::level(0.0f)).xy;"
6872                )?;
6873                writeln!(self.out, "{l2}}} else {{")?;
6874                // For 3 planes, sample U and V from planes 1 and 2 respectively
6875                writeln!(self.out, "{l3}uint2 plane2_size = uint2(tex.plane2.get_width(), tex.plane2.get_height());")?;
6876                writeln!(
6877                    self.out,
6878                    "{l3}float2 plane2_half_texel = float2(0.5, 0.5) / float2(plane2_size);"
6879                )?;
6880                writeln!(
6881                    self.out,
6882                    "{l3}float2 plane2_coords = {NAMESPACE}::clamp(coords, bounds.xy + plane2_half_texel, bounds.zw - plane1_half_texel);"
6883                )?;
6884                writeln!(self.out, "{l3}uv.x = tex.plane1.sample(samp, plane1_coords, {NAMESPACE}::level(0.0f)).x;")?;
6885                writeln!(self.out, "{l3}uv.y = tex.plane2.sample(samp, plane2_coords, {NAMESPACE}::level(0.0f)).x;")?;
6886                writeln!(self.out, "{l2}}}")?;
6887
6888                self.write_convert_yuv_to_rgb_and_return(l2, "y", "uv", "tex.params")?;
6889
6890                writeln!(self.out, "{l1}}}")?;
6891                writeln!(self.out, "}}")?;
6892                writeln!(self.out)?;
6893            }
6894            _ => {
6895                writeln!(self.out, "{NAMESPACE}::float4 {IMAGE_SAMPLE_BASE_CLAMP_TO_EDGE_FUNCTION}({NAMESPACE}::texture2d<float, {NAMESPACE}::access::sample> tex, {NAMESPACE}::sampler samp, {NAMESPACE}::float2 coords) {{")?;
6896                let l1 = back::Level(1);
6897                writeln!(self.out, "{l1}{NAMESPACE}::float2 half_texel = 0.5 / {NAMESPACE}::float2(tex.get_width(0u), tex.get_height(0u));")?;
6898                writeln!(
6899                    self.out,
6900                    "{l1}return tex.sample(samp, {NAMESPACE}::clamp(coords, half_texel, 1.0 - half_texel), {NAMESPACE}::level(0.0));"
6901                )?;
6902                writeln!(self.out, "}}")?;
6903                writeln!(self.out)?;
6904            }
6905        }
6906        Ok(())
6907    }
6908
6909    fn write_wrapped_image_query(
6910        &mut self,
6911        module: &crate::Module,
6912        func_ctx: &back::FunctionCtx,
6913        image: Handle<crate::Expression>,
6914        query: crate::ImageQuery,
6915    ) -> BackendResult {
6916        // We currently only need to wrap size image queries for external textures
6917        if !matches!(query, crate::ImageQuery::Size { .. }) {
6918            return Ok(());
6919        }
6920        let class = match *func_ctx.resolve_type(image, &module.types) {
6921            crate::TypeInner::Image { class, .. } => class,
6922            _ => unreachable!(),
6923        };
6924        if class != crate::ImageClass::External {
6925            return Ok(());
6926        }
6927        let wrapped = WrappedFunction::ImageQuerySize { class };
6928        if !self.wrapped_functions.insert(wrapped) {
6929            return Ok(());
6930        }
6931        writeln!(
6932            self.out,
6933            "uint2 {IMAGE_SIZE_EXTERNAL_FUNCTION}({EXTERNAL_TEXTURE_WRAPPER_STRUCT} tex) {{"
6934        )?;
6935        let l1 = back::Level(1);
6936        let l2 = l1.next();
6937        writeln!(
6938            self.out,
6939            "{l1}if ({NAMESPACE}::any(tex.params.size != uint2(0u))) {{"
6940        )?;
6941        writeln!(self.out, "{l2}return tex.params.size;")?;
6942        writeln!(self.out, "{l1}}} else {{")?;
6943        // params.size == (0, 0) indicates to query and return plane 0's actual size
6944        writeln!(
6945            self.out,
6946            "{l2}return uint2(tex.plane0.get_width(), tex.plane0.get_height());"
6947        )?;
6948        writeln!(self.out, "{l1}}}")?;
6949        writeln!(self.out, "}}")?;
6950        writeln!(self.out)?;
6951        Ok(())
6952    }
6953
6954    fn write_wrapped_cooperative_load(
6955        &mut self,
6956        module: &crate::Module,
6957        func_ctx: &back::FunctionCtx,
6958        columns: crate::CooperativeSize,
6959        rows: crate::CooperativeSize,
6960        pointer: Handle<crate::Expression>,
6961    ) -> BackendResult {
6962        let ptr_ty = func_ctx.resolve_type(pointer, &module.types);
6963        let space = ptr_ty.pointer_space().unwrap();
6964        let space_name = space.to_msl_name().unwrap_or_default();
6965        let scalar = ptr_ty
6966            .pointer_base_type()
6967            .unwrap()
6968            .inner_with(&module.types)
6969            .scalar()
6970            .unwrap();
6971        let wrapped = WrappedFunction::CooperativeLoad {
6972            space_name,
6973            columns,
6974            rows,
6975            scalar,
6976        };
6977        if !self.wrapped_functions.insert(wrapped) {
6978            return Ok(());
6979        }
6980        let scalar_name = scalar.to_msl_name();
6981        writeln!(
6982            self.out,
6983            "{NAMESPACE}::simdgroup_{scalar_name}{}x{} {COOPERATIVE_LOAD_FUNCTION}(const {space_name} {scalar_name}* ptr, int stride, bool is_row_major) {{",
6984            columns as u32, rows as u32,
6985        )?;
6986        let l1 = back::Level(1);
6987        writeln!(
6988            self.out,
6989            "{l1}{NAMESPACE}::simdgroup_{scalar_name}{}x{} m;",
6990            columns as u32, rows as u32
6991        )?;
6992        let matrix_origin = "0";
6993        writeln!(
6994            self.out,
6995            "{l1}simdgroup_load(m, ptr, stride, {matrix_origin}, is_row_major);"
6996        )?;
6997        writeln!(self.out, "{l1}return m;")?;
6998        writeln!(self.out, "}}")?;
6999        writeln!(self.out)?;
7000        Ok(())
7001    }
7002
7003    fn write_wrapped_cooperative_multiply_add(
7004        &mut self,
7005        module: &crate::Module,
7006        func_ctx: &back::FunctionCtx,
7007        space: crate::AddressSpace,
7008        a: Handle<crate::Expression>,
7009        b: Handle<crate::Expression>,
7010        c: Handle<crate::Expression>,
7011    ) -> BackendResult {
7012        let space_name = space.to_msl_name().unwrap_or_default();
7013        let (a_c, a_r, ab_scalar) = match *func_ctx.resolve_type(a, &module.types) {
7014            crate::TypeInner::CooperativeMatrix {
7015                columns,
7016                rows,
7017                scalar,
7018                ..
7019            } => (columns, rows, scalar),
7020            _ => unreachable!(),
7021        };
7022        let (b_c, b_r) = match *func_ctx.resolve_type(b, &module.types) {
7023            crate::TypeInner::CooperativeMatrix { columns, rows, .. } => (columns, rows),
7024            _ => unreachable!(),
7025        };
7026        let c_scalar = match *func_ctx.resolve_type(c, &module.types) {
7027            crate::TypeInner::CooperativeMatrix { scalar, .. } => scalar,
7028            _ => unreachable!(),
7029        };
7030        let wrapped = WrappedFunction::CooperativeMultiplyAdd {
7031            space_name,
7032            columns: b_c,
7033            rows: a_r,
7034            intermediate: a_c,
7035            ab_scalar,
7036            c_scalar,
7037        };
7038        if !self.wrapped_functions.insert(wrapped) {
7039            return Ok(());
7040        }
7041        let ab_scalar_name = ab_scalar.to_msl_name();
7042        let c_scalar_name = c_scalar.to_msl_name();
7043        writeln!(
7044            self.out,
7045            "{NAMESPACE}::simdgroup_{c_scalar_name}{}x{} {COOPERATIVE_MULTIPLY_ADD_FUNCTION}(const {space_name} {NAMESPACE}::simdgroup_{ab_scalar_name}{}x{}& a, const {space_name} {NAMESPACE}::simdgroup_{ab_scalar_name}{}x{}& b, const {space_name} {NAMESPACE}::simdgroup_{c_scalar_name}{}x{}& c) {{",
7046            b_c as u32, a_r as u32, a_c as u32, a_r as u32, b_c as u32, b_r as u32, b_c as u32, a_r as u32,
7047        )?;
7048        let l1 = back::Level(1);
7049        writeln!(
7050            self.out,
7051            "{l1}{NAMESPACE}::simdgroup_{c_scalar_name}{}x{} d;",
7052            b_c as u32, a_r as u32
7053        )?;
7054        writeln!(self.out, "{l1}simdgroup_multiply_accumulate(d,a,b,c);")?;
7055        writeln!(self.out, "{l1}return d;")?;
7056        writeln!(self.out, "}}")?;
7057        writeln!(self.out)?;
7058        Ok(())
7059    }
7060
7061    pub(super) fn write_wrapped_functions(
7062        &mut self,
7063        module: &crate::Module,
7064        func_ctx: &back::FunctionCtx,
7065        options: &Options,
7066    ) -> BackendResult {
7067        for (expr_handle, expr) in func_ctx.expressions.iter() {
7068            match *expr {
7069                crate::Expression::Unary { op, expr: operand } => {
7070                    self.write_wrapped_unary_op(module, func_ctx, op, operand)?;
7071                }
7072                crate::Expression::Binary { op, left, right } => {
7073                    self.write_wrapped_binary_op(module, func_ctx, expr_handle, op, left, right)?;
7074                }
7075                crate::Expression::Math {
7076                    fun,
7077                    arg,
7078                    arg1,
7079                    arg2,
7080                    arg3,
7081                } => {
7082                    self.write_wrapped_math_function(module, func_ctx, fun, arg, arg1, arg2, arg3)?;
7083                }
7084                crate::Expression::As { expr, target } => {
7085                    self.write_wrapped_cast(module, func_ctx, expr, target)?;
7086                }
7087                crate::Expression::ImageLoad {
7088                    image,
7089                    coordinate,
7090                    array_index,
7091                    sample,
7092                    level,
7093                } => {
7094                    self.write_wrapped_image_load(
7095                        module,
7096                        func_ctx,
7097                        image,
7098                        coordinate,
7099                        array_index,
7100                        sample,
7101                        level,
7102                    )?;
7103                }
7104                crate::Expression::ImageSample {
7105                    image,
7106                    sampler,
7107                    gather,
7108                    coordinate,
7109                    array_index,
7110                    offset,
7111                    level,
7112                    depth_ref,
7113                    clamp_to_edge,
7114                } => {
7115                    self.write_wrapped_image_sample(
7116                        module,
7117                        func_ctx,
7118                        image,
7119                        sampler,
7120                        gather,
7121                        coordinate,
7122                        array_index,
7123                        offset,
7124                        level,
7125                        depth_ref,
7126                        clamp_to_edge,
7127                    )?;
7128                }
7129                crate::Expression::ImageQuery { image, query } => {
7130                    self.write_wrapped_image_query(module, func_ctx, image, query)?;
7131                }
7132                crate::Expression::CooperativeLoad {
7133                    columns,
7134                    rows,
7135                    role: _,
7136                    ref data,
7137                } => {
7138                    self.write_wrapped_cooperative_load(
7139                        module,
7140                        func_ctx,
7141                        columns,
7142                        rows,
7143                        data.pointer,
7144                    )?;
7145                }
7146                crate::Expression::CooperativeMultiplyAdd { a, b, c } => {
7147                    let space = crate::AddressSpace::Private;
7148                    self.write_wrapped_cooperative_multiply_add(module, func_ctx, space, a, b, c)?;
7149                }
7150                crate::Expression::RayQueryGetIntersection { committed, .. } => {
7151                    self.write_rq_get_intersection_function(module, committed, options)?;
7152                }
7153                _ => {}
7154            }
7155        }
7156
7157        Ok(())
7158    }
7159
7160    // Returns the array of mapped entry point names.
7161    fn write_functions(
7162        &mut self,
7163        module: &crate::Module,
7164        mod_info: &valid::ModuleInfo,
7165        options: &Options,
7166        pipeline_options: &PipelineOptions,
7167    ) -> Result<TranslationInfo, Error> {
7168        use nt::VertexFormat;
7169
7170        // Define structs to hold resolved/generated data for vertex buffers and
7171        // their attributes.
7172        struct AttributeMappingResolved {
7173            ty_name: String,
7174            dimension: Option<crate::VectorSize>,
7175            scalar: crate::Scalar,
7176            name: String,
7177        }
7178        let mut am_resolved = FastHashMap::<u32, AttributeMappingResolved>::default();
7179
7180        struct VertexBufferMappingResolved<'a> {
7181            id: u32,
7182            stride: u32,
7183            step_mode: back::msl::VertexBufferStepMode,
7184            ty_name: String,
7185            param_name: String,
7186            elem_name: String,
7187            attributes: &'a Vec<back::msl::AttributeMapping>,
7188        }
7189        let mut vbm_resolved = Vec::<VertexBufferMappingResolved>::new();
7190
7191        // Define a struct to hold a named reference to a byte-unpacking function.
7192        struct UnpackingFunction {
7193            name: String,
7194            byte_count: u32,
7195            dimension: Option<crate::VectorSize>,
7196            scalar: crate::Scalar,
7197        }
7198        let mut unpacking_functions = FastHashMap::<VertexFormat, UnpackingFunction>::default();
7199
7200        // Check if we are attempting vertex pulling. If we are, generate some
7201        // names we'll need, and iterate the vertex buffer mappings to output
7202        // all the conversion functions we'll need to unpack the attribute data.
7203        // We can re-use these names for all entry points that need them, since
7204        // those entry points also use self.namer.
7205        let mut needs_vertex_id = false;
7206        let v_id = self.namer.call("v_id");
7207
7208        let mut needs_instance_id = false;
7209        let i_id = self.namer.call("i_id");
7210        if pipeline_options.vertex_pulling_transform {
7211            for vbm in &pipeline_options.vertex_buffer_mappings {
7212                let buffer_id = vbm.id;
7213                let buffer_stride = vbm.stride;
7214
7215                assert!(
7216                    buffer_stride > 0,
7217                    "Vertex pulling requires a non-zero buffer stride."
7218                );
7219
7220                match vbm.step_mode {
7221                    back::msl::VertexBufferStepMode::Constant => {}
7222                    back::msl::VertexBufferStepMode::ByVertex => {
7223                        needs_vertex_id = true;
7224                    }
7225                    back::msl::VertexBufferStepMode::ByInstance => {
7226                        needs_instance_id = true;
7227                    }
7228                }
7229
7230                let buffer_ty = self.namer.call(format!("vb_{buffer_id}_type").as_str());
7231                let buffer_param = self.namer.call(format!("vb_{buffer_id}_in").as_str());
7232                let buffer_elem = self.namer.call(format!("vb_{buffer_id}_elem").as_str());
7233
7234                vbm_resolved.push(VertexBufferMappingResolved {
7235                    id: buffer_id,
7236                    stride: buffer_stride,
7237                    step_mode: vbm.step_mode,
7238                    ty_name: buffer_ty,
7239                    param_name: buffer_param,
7240                    elem_name: buffer_elem,
7241                    attributes: &vbm.attributes,
7242                });
7243
7244                // Iterate the attributes and generate needed unpacking functions.
7245                for attribute in &vbm.attributes {
7246                    if unpacking_functions.contains_key(&attribute.format) {
7247                        continue;
7248                    }
7249                    let (name, byte_count, dimension, scalar) =
7250                        match self.write_unpacking_function(attribute.format) {
7251                            Ok((name, byte_count, dimension, scalar)) => {
7252                                (name, byte_count, dimension, scalar)
7253                            }
7254                            _ => {
7255                                continue;
7256                            }
7257                        };
7258                    unpacking_functions.insert(
7259                        attribute.format,
7260                        UnpackingFunction {
7261                            name,
7262                            byte_count,
7263                            dimension,
7264                            scalar,
7265                        },
7266                    );
7267                }
7268            }
7269        }
7270
7271        let mut pass_through_globals = Vec::new();
7272        for (fun_handle, fun) in module.functions.iter() {
7273            log::trace!(
7274                "function {:?}, handle {:?}",
7275                fun.name.as_deref().unwrap_or("(anonymous)"),
7276                fun_handle
7277            );
7278
7279            let ctx = back::FunctionCtx {
7280                ty: back::FunctionType::Function(fun_handle),
7281                info: &mod_info[fun_handle],
7282                expressions: &fun.expressions,
7283                named_expressions: &fun.named_expressions,
7284            };
7285
7286            writeln!(self.out)?;
7287            self.write_wrapped_functions(module, &ctx, options)?;
7288
7289            let fun_info = &mod_info[fun_handle];
7290            pass_through_globals.clear();
7291            let mut needs_buffer_sizes = false;
7292            for (handle, var) in module.global_variables.iter() {
7293                if !fun_info[handle].is_empty() {
7294                    if var.space.needs_pass_through() {
7295                        pass_through_globals.push(handle);
7296                    }
7297                    needs_buffer_sizes |= module.types[var.ty]
7298                        .inner
7299                        .needs_host_buffer_byte_size(&module.types);
7300                }
7301            }
7302
7303            let fun_name = &self.names[&NameKey::Function(fun_handle)];
7304            match fun.result {
7305                Some(ref result) => {
7306                    let ty_name = TypeContext {
7307                        handle: result.ty,
7308                        gctx: module.to_ctx(),
7309                        names: &self.names,
7310                        access: crate::StorageAccess::empty(),
7311                        first_time: false,
7312                    };
7313                    write!(self.out, "{ty_name}")?;
7314                }
7315                None => {
7316                    write!(self.out, "void")?;
7317                }
7318            }
7319            writeln!(self.out, " {fun_name}(")?;
7320
7321            for (index, arg) in fun.arguments.iter().enumerate() {
7322                let name = &self.names[&NameKey::FunctionArgument(fun_handle, index as u32)];
7323                let param_type_name = TypeContext {
7324                    handle: arg.ty,
7325                    gctx: module.to_ctx(),
7326                    names: &self.names,
7327                    access: match module.types[arg.ty].inner {
7328                        crate::TypeInner::Image {
7329                            class: crate::ImageClass::Storage { access, .. },
7330                            ..
7331                        } => access,
7332                        _ => crate::StorageAccess::empty(),
7333                    },
7334                    first_time: false,
7335                };
7336                let separator = separate(
7337                    !pass_through_globals.is_empty()
7338                        || index + 1 != fun.arguments.len()
7339                        || needs_buffer_sizes,
7340                );
7341                writeln!(
7342                    self.out,
7343                    "{}{} {}{}",
7344                    back::INDENT,
7345                    param_type_name,
7346                    name,
7347                    separator
7348                )?;
7349            }
7350            for (index, &handle) in pass_through_globals.iter().enumerate() {
7351                let tyvar = TypedGlobalVariable {
7352                    module,
7353                    names: &self.names,
7354                    handle,
7355                    usage: fun_info[handle],
7356                    reference: true,
7357                };
7358                let separator =
7359                    separate(index + 1 != pass_through_globals.len() || needs_buffer_sizes);
7360                write!(self.out, "{}", back::INDENT)?;
7361                tyvar.try_fmt(&mut self.out)?;
7362                writeln!(self.out, "{separator}")?;
7363            }
7364
7365            if needs_buffer_sizes {
7366                writeln!(
7367                    self.out,
7368                    "{}constant _mslBufferSizes& _buffer_sizes",
7369                    back::INDENT
7370                )?;
7371            }
7372
7373            writeln!(self.out, ") {{")?;
7374
7375            let guarded_indices =
7376                index::find_checked_indexes(module, fun, fun_info, options.bounds_check_policies);
7377
7378            let context = StatementContext {
7379                expression: ExpressionContext {
7380                    function: fun,
7381                    origin: FunctionOrigin::Handle(fun_handle),
7382                    info: fun_info,
7383                    lang_version: options.lang_version,
7384                    policies: options.bounds_check_policies,
7385                    guarded_indices,
7386                    module,
7387                    mod_info,
7388                    pipeline_options,
7389                    force_loop_bounding: options.force_loop_bounding,
7390                    emit_int_div_checks: options.emit_int_div_checks,
7391                    ray_query_initialization_tracking: options.ray_query_initialization_tracking,
7392                },
7393                result_struct: None,
7394            };
7395
7396            self.put_locals(&context.expression)?;
7397            self.update_expressions_to_bake(fun, fun_info, &context.expression);
7398            self.put_block(back::Level(1), &fun.body, &context)?;
7399            writeln!(self.out, "}}")?;
7400            self.named_expressions.clear();
7401            self.hoisted_loop_expressions.clear();
7402        }
7403
7404        let ep_range = get_entry_points(module, pipeline_options.entry_point.as_ref())
7405            .map_err(|(stage, name)| Error::EntryPointNotFound(stage, name))?;
7406
7407        let mut info = TranslationInfo {
7408            entry_point_names: Vec::with_capacity(ep_range.len()),
7409        };
7410
7411        for ep_index in ep_range {
7412            let ep = &module.entry_points[ep_index];
7413            let fun = &ep.function;
7414            let fun_info = mod_info.get_entry_point(ep_index);
7415            let mut ep_error = None;
7416
7417            // For vertex_id and instance_id arguments, presume that we'll
7418            // use our generated names, but switch to the name of an
7419            // existing @builtin param, if we find one.
7420            let mut v_existing_id = None;
7421            let mut i_existing_id = None;
7422
7423            log::trace!(
7424                "entry point {:?}, index {:?}",
7425                fun.name.as_deref().unwrap_or("(anonymous)"),
7426                ep_index
7427            );
7428
7429            let ctx = back::FunctionCtx {
7430                ty: back::FunctionType::EntryPoint(ep_index as u16),
7431                info: fun_info,
7432                expressions: &fun.expressions,
7433                named_expressions: &fun.named_expressions,
7434            };
7435
7436            self.write_wrapped_functions(module, &ctx, options)?;
7437
7438            let (em_str, in_mode, out_mode, can_vertex_pull) = match ep.stage {
7439                crate::ShaderStage::Vertex => (
7440                    Some("vertex"),
7441                    LocationMode::VertexInput,
7442                    LocationMode::VertexOutput,
7443                    true,
7444                ),
7445                crate::ShaderStage::Fragment => (
7446                    Some("fragment"),
7447                    LocationMode::FragmentInput,
7448                    LocationMode::FragmentOutput,
7449                    false,
7450                ),
7451                crate::ShaderStage::Compute => (
7452                    Some("kernel"),
7453                    LocationMode::Uniform,
7454                    LocationMode::Uniform,
7455                    false,
7456                ),
7457                crate::ShaderStage::Task => {
7458                    (None, LocationMode::Uniform, LocationMode::Uniform, false)
7459                }
7460                crate::ShaderStage::Mesh => {
7461                    (None, LocationMode::Uniform, LocationMode::MeshOutput, false)
7462                }
7463                crate::ShaderStage::RayGeneration
7464                | crate::ShaderStage::AnyHit
7465                | crate::ShaderStage::ClosestHit
7466                | crate::ShaderStage::Miss => unimplemented!(),
7467            };
7468
7469            // Should this entry point be modified to do vertex pulling?
7470            let do_vertex_pulling = can_vertex_pull
7471                && pipeline_options.vertex_pulling_transform
7472                && !pipeline_options.vertex_buffer_mappings.is_empty();
7473
7474            // Is any global variable used by this entry point dynamically sized?
7475            let needs_buffer_sizes = do_vertex_pulling
7476                || module
7477                    .global_variables
7478                    .iter()
7479                    .filter(|&(handle, _)| !fun_info[handle].is_empty())
7480                    .any(|(_, var)| {
7481                        module.types[var.ty]
7482                            .inner
7483                            .needs_host_buffer_byte_size(&module.types)
7484                    });
7485
7486            // skip this entry point if any global bindings are missing,
7487            // or their types are incompatible.
7488            if !options.fake_missing_bindings {
7489                for (var_handle, var) in module.global_variables.iter() {
7490                    if fun_info[var_handle].is_empty() {
7491                        continue;
7492                    }
7493                    match var.space {
7494                        crate::AddressSpace::Uniform
7495                        | crate::AddressSpace::Storage { .. }
7496                        | crate::AddressSpace::Handle => {
7497                            let br = match var.binding {
7498                                Some(ref br) => br,
7499                                None => {
7500                                    let var_name = var.name.clone().unwrap_or_default();
7501                                    ep_error =
7502                                        Some(super::EntryPointError::MissingBinding(var_name));
7503                                    break;
7504                                }
7505                            };
7506                            let target = options.get_resource_binding_target(ep, br);
7507                            let good = match target {
7508                                Some(target) => {
7509                                    // We intentionally don't dereference binding_arrays here,
7510                                    // so that binding arrays fall to the buffer location.
7511
7512                                    match module.types[var.ty].inner {
7513                                        crate::TypeInner::Image {
7514                                            class: crate::ImageClass::External,
7515                                            ..
7516                                        } => target.external_texture.is_some(),
7517                                        crate::TypeInner::Image { .. } => target.texture.is_some(),
7518                                        crate::TypeInner::Sampler { .. } => {
7519                                            target.sampler.is_some()
7520                                        }
7521                                        _ => target.buffer.is_some(),
7522                                    }
7523                                }
7524                                None => false,
7525                            };
7526                            if !good {
7527                                ep_error = Some(super::EntryPointError::MissingBindTarget(*br));
7528                                break;
7529                            }
7530                        }
7531                        crate::AddressSpace::Immediate => {
7532                            if let Err(e) = options.resolve_immediates(ep) {
7533                                ep_error = Some(e);
7534                                break;
7535                            }
7536                        }
7537                        crate::AddressSpace::Function
7538                        | crate::AddressSpace::Private
7539                        | crate::AddressSpace::WorkGroup
7540                        | crate::AddressSpace::TaskPayload => {}
7541                        crate::AddressSpace::RayPayload
7542                        | crate::AddressSpace::IncomingRayPayload => unimplemented!(),
7543                    }
7544                }
7545                if needs_buffer_sizes {
7546                    if let Err(err) = options.resolve_sizes_buffer(ep) {
7547                        ep_error = Some(err);
7548                    }
7549                }
7550            }
7551
7552            if let Some(err) = ep_error {
7553                info.entry_point_names.push(Err(err));
7554                continue;
7555            }
7556            let fun_name = self.names[&NameKey::EntryPoint(ep_index as _)].clone();
7557            info.entry_point_names.push(Ok(fun_name.clone()));
7558
7559            writeln!(self.out)?;
7560
7561            // Since `Namer.reset` wasn't expecting struct members to be
7562            // suddenly injected into another namespace like this,
7563            // `self.names` doesn't keep them distinct from other variables.
7564            // Generate fresh names for these arguments, and remember the
7565            // mapping.
7566            let mut flattened_member_names = FastHashMap::default();
7567            // Varyings' members get their own namespace
7568            let mut varyings_namer = proc::Namer::default();
7569
7570            let mut empty_names = FastHashMap::default(); // Create a throwaway map
7571            varyings_namer.reset(
7572                module,
7573                &super::keywords::RESERVED_SET,
7574                proc::KeywordSet::empty(),
7575                proc::CaseInsensitiveKeywordSet::empty(),
7576                &[CLAMPED_LOD_LOAD_PREFIX],
7577                &mut empty_names,
7578            );
7579
7580            // List all the Naga `EntryPoint`'s `Function`'s arguments,
7581            // flattening structs into their members. In Metal, we will pass
7582            // each of these values to the entry point as a separate argument—
7583            // except for the varyings, handled next.
7584            let mut flattened_arguments = Vec::new();
7585            for (arg_index, arg) in fun.arguments.iter().enumerate() {
7586                match module.types[arg.ty].inner {
7587                    crate::TypeInner::Struct { ref members, .. } => {
7588                        for (member_index, member) in members.iter().enumerate() {
7589                            let member_index = member_index as u32;
7590                            flattened_arguments.push((
7591                                NameKey::StructMember(arg.ty, member_index),
7592                                member.ty,
7593                                member.binding.as_ref(),
7594                            ));
7595                            let name_key = NameKey::StructMember(arg.ty, member_index);
7596                            let name = match member.binding {
7597                                Some(crate::Binding::Location { .. }) => {
7598                                    if do_vertex_pulling {
7599                                        self.namer.call(&self.names[&name_key])
7600                                    } else {
7601                                        varyings_namer.call(&self.names[&name_key])
7602                                    }
7603                                }
7604                                _ => self.namer.call(&self.names[&name_key]),
7605                            };
7606                            flattened_member_names.insert(name_key, name);
7607                        }
7608                    }
7609                    _ => flattened_arguments.push((
7610                        NameKey::EntryPointArgument(ep_index as _, arg_index as u32),
7611                        arg.ty,
7612                        arg.binding.as_ref(),
7613                    )),
7614                }
7615            }
7616
7617            // Identify the varyings among the argument values, and maybe emit
7618            // a struct type named `<fun>Input` to hold them. If we are doing
7619            // vertex pulling, we instead update our attribute mapping to
7620            // note the types, names, and zero values of the attributes.
7621            let stage_in_name = self.namer.call(&format!("{fun_name}Input"));
7622            let varyings_member_name = self.namer.call("varyings");
7623            let mut has_varyings = false;
7624
7625            if !flattened_arguments.is_empty() {
7626                if !do_vertex_pulling {
7627                    writeln!(self.out, "struct {stage_in_name} {{")?;
7628                }
7629                for &(ref name_key, ty, binding) in flattened_arguments.iter() {
7630                    let Some(binding) = binding else {
7631                        continue;
7632                    };
7633                    let name = match *name_key {
7634                        NameKey::StructMember(..) => &flattened_member_names[name_key],
7635                        _ => &self.names[name_key],
7636                    };
7637                    let ty_name = TypeContext {
7638                        handle: ty,
7639                        gctx: module.to_ctx(),
7640                        names: &self.names,
7641                        access: crate::StorageAccess::empty(),
7642                        first_time: false,
7643                    };
7644                    let resolved = options.resolve_local_binding(binding, in_mode)?;
7645                    let location = match *binding {
7646                        crate::Binding::Location { location, .. } => Some(location),
7647                        crate::Binding::BuiltIn(crate::BuiltIn::Barycentric { .. }) => None,
7648                        crate::Binding::BuiltIn(_) => continue,
7649                    };
7650                    if do_vertex_pulling {
7651                        let Some(location) = location else {
7652                            continue;
7653                        };
7654                        // Update our attribute mapping.
7655                        am_resolved.insert(
7656                            location,
7657                            AttributeMappingResolved {
7658                                ty_name: ty_name.to_string(),
7659                                dimension: ty_name.vector_size(),
7660                                scalar: ty_name.scalar().unwrap(),
7661                                name: name.to_string(),
7662                            },
7663                        );
7664                    } else {
7665                        has_varyings = true;
7666                        if let super::ResolvedBinding::User {
7667                            prefix,
7668                            index,
7669                            interpolation: Some(super::ResolvedInterpolation::PerVertex),
7670                        } = resolved
7671                        {
7672                            if options.lang_version < (4, 0) {
7673                                return Err(Error::PerVertexNotSupported);
7674                            }
7675                            write!(
7676                                self.out,
7677                                "{}{NAMESPACE}::vertex_value<{}> {name} [[user({prefix}{index})]]",
7678                                back::INDENT,
7679                                ty_name.unwrap_array()
7680                            )?;
7681                        } else {
7682                            write!(self.out, "{}{} {}", back::INDENT, ty_name, name)?;
7683                            resolved.try_fmt(&mut self.out)?;
7684                        }
7685                        writeln!(self.out, ";")?;
7686                    }
7687                }
7688                if !do_vertex_pulling {
7689                    writeln!(self.out, "}};")?;
7690                }
7691            }
7692
7693            // Define a struct type named for the return value, if any, named
7694            // `<fun>Output`.
7695            let stage_out_name = self.namer.call(&format!("{fun_name}Output"));
7696            let result_member_name = self.namer.call("member");
7697            let result_type_name = match fun.result {
7698                Some(ref result)
7699                    if ep.stage != crate::ShaderStage::Task
7700                        && !only_writes_discarded_depth(module, ep) =>
7701                {
7702                    let mut result_members = Vec::new();
7703                    if let crate::TypeInner::Struct { ref members, .. } =
7704                        module.types[result.ty].inner
7705                    {
7706                        for (member_index, member) in members.iter().enumerate() {
7707                            result_members.push((
7708                                &self.names[&NameKey::StructMember(result.ty, member_index as u32)],
7709                                member.ty,
7710                                member.binding.as_ref(),
7711                            ));
7712                        }
7713                    } else {
7714                        result_members.push((
7715                            &result_member_name,
7716                            result.ty,
7717                            result.binding.as_ref(),
7718                        ));
7719                    }
7720
7721                    writeln!(self.out, "struct {stage_out_name} {{")?;
7722                    let mut has_point_size = false;
7723                    for (name, ty, binding) in result_members {
7724                        let ty_name = TypeContext {
7725                            handle: ty,
7726                            gctx: module.to_ctx(),
7727                            names: &self.names,
7728                            access: crate::StorageAccess::empty(),
7729                            first_time: true,
7730                        };
7731                        let binding = binding.ok_or_else(|| {
7732                            Error::GenericValidation("Expected binding, got None".into())
7733                        })?;
7734
7735                        if let crate::Binding::BuiltIn(crate::BuiltIn::PointSize) = *binding {
7736                            has_point_size = true;
7737                            if !pipeline_options.allow_and_force_point_size {
7738                                continue;
7739                            }
7740                        }
7741
7742                        if discards_frag_depth(ep) && is_frag_depth(Some(binding)) {
7743                            continue;
7744                        }
7745
7746                        let array_len = match module.types[ty].inner {
7747                            crate::TypeInner::Array {
7748                                size: crate::ArraySize::Constant(size),
7749                                ..
7750                            } => Some(size),
7751                            _ => None,
7752                        };
7753                        let resolved = match (
7754                            options.resolve_local_binding(binding, out_mode)?,
7755                            ep.early_depth_test,
7756                        ) {
7757                            (
7758                                super::ResolvedBinding::BuiltIn(crate::BuiltIn::FragDepth),
7759                                Some(crate::EarlyDepthTest::Allow { conservative }),
7760                            ) => super::ResolvedBinding::ConservativeDepth(conservative),
7761                            (resolved, _) => resolved,
7762                        };
7763                        write!(self.out, "{}{} {}", back::INDENT, ty_name, name)?;
7764                        resolved.try_fmt(&mut self.out)?;
7765                        if let Some(array_len) = array_len {
7766                            write!(self.out, " [{array_len}]")?;
7767                        }
7768                        writeln!(self.out, ";")?;
7769                    }
7770
7771                    if pipeline_options.allow_and_force_point_size
7772                        && ep.stage == crate::ShaderStage::Vertex
7773                        && !has_point_size
7774                    {
7775                        // inject the point size output last
7776                        writeln!(
7777                            self.out,
7778                            "{}float _point_size [[point_size]];",
7779                            back::INDENT
7780                        )?;
7781                    }
7782                    writeln!(self.out, "}};")?;
7783                    &stage_out_name
7784                }
7785                Some(ref result) if ep.stage == crate::ShaderStage::Task => {
7786                    assert_eq!(
7787                        module.types[result.ty].inner,
7788                        crate::TypeInner::Vector {
7789                            size: crate::VectorSize::Tri,
7790                            scalar: crate::Scalar::U32
7791                        }
7792                    );
7793
7794                    "metal::uint3"
7795                }
7796                _ => "void",
7797            };
7798
7799            let out_mesh_info = if let Some(ref mesh_info) = ep.mesh_info {
7800                Some(self.write_mesh_output_types(
7801                    mesh_info,
7802                    &fun_name,
7803                    module,
7804                    pipeline_options.allow_and_force_point_size,
7805                    options,
7806                )?)
7807            } else {
7808                None
7809            };
7810
7811            // If we're doing a vertex pulling transform, define the buffer
7812            // structure types.
7813            if do_vertex_pulling {
7814                for vbm in &vbm_resolved {
7815                    let buffer_stride = vbm.stride;
7816                    let buffer_ty = &vbm.ty_name;
7817
7818                    // Define a structure of bytes of the appropriate size.
7819                    // When we access the attributes, we'll be unpacking these
7820                    // bytes at some offset.
7821                    writeln!(
7822                        self.out,
7823                        "struct {buffer_ty} {{ metal::uchar data[{buffer_stride}]; }};"
7824                    )?;
7825                }
7826            }
7827
7828            let is_wrapped = matches!(
7829                ep.stage,
7830                crate::ShaderStage::Task | crate::ShaderStage::Mesh
7831            );
7832            let fun_name = fun_name.clone();
7833            let nested_fun_name = if is_wrapped {
7834                self.namer.call(&format!("_{fun_name}"))
7835            } else {
7836                fun_name.clone()
7837            };
7838
7839            // https://web.archive.org/web/20181029003926/https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf
7840            if ep.stage == crate::ShaderStage::Compute && options.lang_version >= (2, 1) {
7841                let total_threads =
7842                    ep.workgroup_size[0] * ep.workgroup_size[1] * ep.workgroup_size[2];
7843                write!(
7844                    self.out,
7845                    "[[max_total_threads_per_threadgroup({total_threads})]] "
7846                )?;
7847            }
7848
7849            if ep.stage == crate::ShaderStage::Fragment
7850                && ep.early_depth_test == Some(crate::EarlyDepthTest::Force)
7851            {
7852                if options.lang_version < (1, 2) {
7853                    return Err(Error::UnsupportedAttribute(
7854                        "early_fragment_tests".to_string(),
7855                    ));
7856                }
7857                write!(self.out, "[[early_fragment_tests]] ")?;
7858            }
7859
7860            // Write the entry point function's name, and begin its argument list.
7861            if let Some(em_str) = em_str {
7862                write!(self.out, "{em_str} ")?;
7863            }
7864            writeln!(self.out, "{result_type_name} {nested_fun_name}(")?;
7865
7866            let mut args = Vec::new();
7867
7868            // If we have produced a struct holding the `EntryPoint`'s
7869            // `Function`'s arguments' varyings, pass that struct first.
7870            if has_varyings {
7871                args.push(EntryPointArgument {
7872                    ty_name: stage_in_name,
7873                    name: varyings_member_name.clone(),
7874                    binding: " [[stage_in]]".to_string(),
7875                    init: None,
7876                });
7877            }
7878
7879            let mut local_invocation_index = None;
7880
7881            // Then pass the remaining arguments not included in the varyings
7882            // struct.
7883            for &(ref name_key, ty, binding) in flattened_arguments.iter() {
7884                let binding = match binding {
7885                    Some(&crate::Binding::BuiltIn(crate::BuiltIn::Barycentric { .. })) => continue,
7886                    Some(binding @ &crate::Binding::BuiltIn { .. }) => binding,
7887                    _ => continue,
7888                };
7889                let name = match *name_key {
7890                    NameKey::StructMember(..) => &flattened_member_names[name_key],
7891                    _ => &self.names[name_key],
7892                };
7893
7894                if binding == &crate::Binding::BuiltIn(crate::BuiltIn::LocalInvocationIndex) {
7895                    local_invocation_index = Some(name_key);
7896                }
7897
7898                let ty_name = TypeContext {
7899                    handle: ty,
7900                    gctx: module.to_ctx(),
7901                    names: &self.names,
7902                    access: crate::StorageAccess::empty(),
7903                    first_time: false,
7904                };
7905
7906                match *binding {
7907                    crate::Binding::BuiltIn(crate::BuiltIn::VertexIndex) => {
7908                        v_existing_id = Some(name.clone());
7909                    }
7910                    crate::Binding::BuiltIn(crate::BuiltIn::InstanceIndex) => {
7911                        i_existing_id = Some(name.clone());
7912                    }
7913                    _ => {}
7914                };
7915
7916                let resolved = options.resolve_local_binding(binding, in_mode)?;
7917                let mut binding = String::new();
7918                resolved.try_fmt(&mut binding)?;
7919
7920                args.push(EntryPointArgument {
7921                    ty_name: format!("{ty_name}"),
7922                    name: name.clone(),
7923                    binding,
7924                    init: None,
7925                });
7926            }
7927
7928            let need_workgroup_variables_initialization =
7929                self.need_workgroup_variables_initialization(options, ep, module, fun_info);
7930
7931            if local_invocation_index.is_none()
7932                && (need_workgroup_variables_initialization
7933                    || ep.stage == crate::ShaderStage::Task
7934                    || ep.stage == crate::ShaderStage::Mesh)
7935            {
7936                args.push(EntryPointArgument {
7937                    ty_name: "uint".to_string(),
7938                    name: "__local_invocation_index".to_string(),
7939                    binding: " [[thread_index_in_threadgroup]]".to_string(),
7940                    init: None,
7941                });
7942            }
7943
7944            // Those global variables used by this entry point and its callees
7945            // get passed as arguments. `Private` globals are an exception, they
7946            // don't outlive this invocation, so we declare them below as locals
7947            // within the entry point.
7948            for (handle, var) in module.global_variables.iter() {
7949                let usage = fun_info[handle];
7950                if usage.is_empty() || var.space == crate::AddressSpace::Private {
7951                    continue;
7952                }
7953
7954                if options.lang_version < (1, 2) {
7955                    match var.space {
7956                        // This restriction is not documented in the MSL spec
7957                        // but validation will fail if it is not upheld.
7958                        //
7959                        // We infer the required version from the "Function
7960                        // Buffer Read-Writes" section of [what's new], where
7961                        // the feature sets listed correspond with the ones
7962                        // supporting MSL 1.2.
7963                        //
7964                        // [what's new]: https://developer.apple.com/library/archive/documentation/Miscellaneous/Conceptual/MetalProgrammingGuide/WhatsNewiniOS10tvOS10andOSX1012/WhatsNewiniOS10tvOS10andOSX1012.html
7965                        crate::AddressSpace::Storage { access }
7966                            if access.contains(crate::StorageAccess::STORE)
7967                                && ep.stage == crate::ShaderStage::Fragment =>
7968                        {
7969                            return Err(Error::UnsupportedWritableStorageBuffer)
7970                        }
7971                        crate::AddressSpace::Handle => {
7972                            match module.types[var.ty].inner {
7973                                crate::TypeInner::Image {
7974                                    class: crate::ImageClass::Storage { access, .. },
7975                                    ..
7976                                } => {
7977                                    // This restriction is not documented in the MSL spec
7978                                    // but validation will fail if it is not upheld.
7979                                    //
7980                                    // We infer the required version from the "Function
7981                                    // Texture Read-Writes" section of [what's new], where
7982                                    // the feature sets listed correspond with the ones
7983                                    // supporting MSL 1.2.
7984                                    //
7985                                    // [what's new]: https://developer.apple.com/library/archive/documentation/Miscellaneous/Conceptual/MetalProgrammingGuide/WhatsNewiniOS10tvOS10andOSX1012/WhatsNewiniOS10tvOS10andOSX1012.html
7986                                    if access.contains(crate::StorageAccess::STORE)
7987                                        && (ep.stage == crate::ShaderStage::Vertex
7988                                            || ep.stage == crate::ShaderStage::Fragment)
7989                                    {
7990                                        return Err(Error::UnsupportedWritableStorageTexture(
7991                                            ep.stage,
7992                                        ));
7993                                    }
7994
7995                                    if access.contains(
7996                                        crate::StorageAccess::LOAD | crate::StorageAccess::STORE,
7997                                    ) {
7998                                        return Err(Error::UnsupportedRWStorageTexture);
7999                                    }
8000                                }
8001                                _ => {}
8002                            }
8003                        }
8004                        _ => {}
8005                    }
8006                }
8007
8008                // Check min MSL version for binding arrays
8009                match var.space {
8010                    crate::AddressSpace::Handle => match module.types[var.ty].inner {
8011                        crate::TypeInner::BindingArray { base, .. } => {
8012                            match module.types[base].inner {
8013                                crate::TypeInner::Sampler { .. } => {
8014                                    if options.lang_version < (2, 0) {
8015                                        return Err(Error::UnsupportedArrayOf(
8016                                            "samplers".to_string(),
8017                                        ));
8018                                    }
8019                                }
8020                                crate::TypeInner::Image { class, .. } => match class {
8021                                    crate::ImageClass::Sampled { .. }
8022                                    | crate::ImageClass::Depth { .. }
8023                                    | crate::ImageClass::Storage {
8024                                        access: crate::StorageAccess::LOAD,
8025                                        ..
8026                                    } => {
8027                                        // Array of textures since:
8028                                        // - iOS: Metal 1.2 (check depends on https://github.com/gfx-rs/naga/issues/2164)
8029                                        // - macOS: Metal 2
8030
8031                                        if options.lang_version < (2, 0) {
8032                                            return Err(Error::UnsupportedArrayOf(
8033                                                "textures".to_string(),
8034                                            ));
8035                                        }
8036                                    }
8037                                    crate::ImageClass::Storage {
8038                                        access: crate::StorageAccess::STORE,
8039                                        ..
8040                                    } => {
8041                                        // Array of write-only textures since:
8042                                        // - iOS: Metal 2.2 (check depends on https://github.com/gfx-rs/naga/issues/2164)
8043                                        // - macOS: Metal 2
8044
8045                                        if options.lang_version < (2, 0) {
8046                                            return Err(Error::UnsupportedArrayOf(
8047                                                "write-only textures".to_string(),
8048                                            ));
8049                                        }
8050                                    }
8051                                    crate::ImageClass::Storage { .. } => {
8052                                        if options.lang_version < (3, 0) {
8053                                            return Err(Error::UnsupportedArrayOf(
8054                                                "read-write textures".to_string(),
8055                                            ));
8056                                        }
8057                                    }
8058                                    crate::ImageClass::External => {
8059                                        return Err(Error::UnsupportedArrayOf(
8060                                            "external textures".to_string(),
8061                                        ));
8062                                    }
8063                                },
8064                                _ => {
8065                                    return Err(Error::UnsupportedArrayOfType(base));
8066                                }
8067                            }
8068                        }
8069                        _ => {}
8070                    },
8071                    _ => {}
8072                }
8073
8074                // the resolves have already been checked for `!fake_missing_bindings` case
8075                let resolved = match var.space {
8076                    crate::AddressSpace::Immediate => options.resolve_immediates(ep).ok(),
8077                    crate::AddressSpace::WorkGroup => None,
8078                    crate::AddressSpace::TaskPayload => Some(back::msl::ResolvedBinding::Payload),
8079                    _ => options
8080                        .resolve_resource_binding(ep, var.binding.as_ref().unwrap())
8081                        .ok(),
8082                };
8083                if let Some(ref resolved) = resolved {
8084                    // Inline samplers are be defined in the EP body
8085                    if resolved.as_inline_sampler(options).is_some() {
8086                        continue;
8087                    }
8088                }
8089
8090                match module.types[var.ty].inner {
8091                    crate::TypeInner::Image {
8092                        class: crate::ImageClass::External,
8093                        ..
8094                    } => {
8095                        // External texture global variables get lowered to 3 textures
8096                        // and a constant buffer. We must emit a separate argument for
8097                        // each of these.
8098                        let target = match resolved {
8099                            Some(back::msl::ResolvedBinding::Resource(target)) => {
8100                                target.external_texture
8101                            }
8102                            _ => None,
8103                        };
8104
8105                        for i in 0..3 {
8106                            let plane_name = &self.names[&NameKey::ExternalTextureGlobalVariable(
8107                                handle,
8108                                ExternalTextureNameKey::Plane(i),
8109                            )];
8110                            let ty_name = format!(
8111                                "{NAMESPACE}::texture2d<float, {NAMESPACE}::access::sample>"
8112                            );
8113                            let name = plane_name.clone();
8114                            let binding = if let Some(ref target) = target {
8115                                format!(" [[texture({})]]", target.planes[i])
8116                            } else {
8117                                String::new()
8118                            };
8119                            args.push(EntryPointArgument {
8120                                ty_name,
8121                                name,
8122                                binding,
8123                                init: None,
8124                            });
8125                        }
8126                        let params_ty_name = &self.names
8127                            [&NameKey::Type(module.special_types.external_texture_params.unwrap())];
8128                        let params_name = &self.names[&NameKey::ExternalTextureGlobalVariable(
8129                            handle,
8130                            ExternalTextureNameKey::Params,
8131                        )];
8132                        let binding = if let Some(ref target) = target {
8133                            format!(" [[buffer({})]]", target.params)
8134                        } else {
8135                            String::new()
8136                        };
8137
8138                        args.push(EntryPointArgument {
8139                            ty_name: format!("constant {params_ty_name}&"),
8140                            name: params_name.clone(),
8141                            binding,
8142                            init: None,
8143                        });
8144                    }
8145                    _ => {
8146                        if var.space == crate::AddressSpace::WorkGroup
8147                            && ep.stage == crate::ShaderStage::Mesh
8148                        {
8149                            continue;
8150                        }
8151                        let tyvar = TypedGlobalVariable {
8152                            module,
8153                            names: &self.names,
8154                            handle,
8155                            usage,
8156                            reference: true,
8157                        };
8158                        let parts = tyvar.to_parts()?;
8159                        let mut binding = String::new();
8160                        if let Some(resolved) = resolved {
8161                            resolved.try_fmt(&mut binding)?;
8162                        }
8163                        args.push(EntryPointArgument {
8164                            ty_name: parts.ty_name,
8165                            name: parts.var_name,
8166                            binding,
8167                            init: var.init,
8168                        });
8169                    }
8170                }
8171            }
8172
8173            if do_vertex_pulling {
8174                if needs_vertex_id && v_existing_id.is_none() {
8175                    // Write the [[vertex_id]] argument.
8176                    args.push(EntryPointArgument {
8177                        ty_name: "uint".to_string(),
8178                        name: v_id.clone(),
8179                        binding: " [[vertex_id]]".to_string(),
8180                        init: None,
8181                    });
8182                }
8183
8184                if needs_instance_id && i_existing_id.is_none() {
8185                    args.push(EntryPointArgument {
8186                        ty_name: "uint".to_string(),
8187                        name: i_id.clone(),
8188                        binding: " [[instance_id]]".to_string(),
8189                        init: None,
8190                    });
8191                }
8192
8193                // Iterate vbm_resolved, output one argument for every vertex buffer,
8194                // using the names we generated earlier.
8195                for vbm in &vbm_resolved {
8196                    let id = &vbm.id;
8197                    let ty_name = &vbm.ty_name;
8198                    let param_name = &vbm.param_name;
8199                    args.push(EntryPointArgument {
8200                        ty_name: format!("const device {ty_name}*"),
8201                        name: param_name.clone(),
8202                        binding: format!(" [[buffer({id})]]"),
8203                        init: None,
8204                    });
8205                }
8206            }
8207
8208            // If this entry uses any variable-length arrays, their sizes are
8209            // passed as a final struct-typed argument.
8210            if needs_buffer_sizes {
8211                // this is checked earlier
8212                let resolved = options.resolve_sizes_buffer(ep).unwrap();
8213                let mut binding = String::new();
8214                resolved.try_fmt(&mut binding)?;
8215                args.push(EntryPointArgument {
8216                    ty_name: "constant _mslBufferSizes&".to_string(),
8217                    name: "_buffer_sizes".to_string(),
8218                    binding,
8219                    init: None,
8220                });
8221            }
8222
8223            let mut is_first_arg = true;
8224            for arg in &args {
8225                if is_first_arg {
8226                    write!(self.out, "  ")?;
8227                } else {
8228                    write!(self.out, ", ")?;
8229                }
8230                is_first_arg = false;
8231                write!(self.out, "{} {}", arg.ty_name, arg.name)?;
8232                if !is_wrapped {
8233                    write!(self.out, "{}", arg.binding)?;
8234                    if let Some(init) = arg.init {
8235                        write!(self.out, " = ")?;
8236                        self.put_const_expression(
8237                            init,
8238                            module,
8239                            mod_info,
8240                            &module.global_expressions,
8241                        )?;
8242                    }
8243                }
8244                writeln!(self.out)?;
8245            }
8246            if ep.stage == crate::ShaderStage::Mesh {
8247                for (handle, var) in module.global_variables.iter() {
8248                    if var.space != crate::AddressSpace::WorkGroup || fun_info[handle].is_empty() {
8249                        continue;
8250                    }
8251                    if is_first_arg {
8252                        write!(self.out, "  ")?;
8253                    } else {
8254                        write!(self.out, ", ")?;
8255                    }
8256                    let ty_context = TypeContext {
8257                        handle: module.global_variables[handle].ty,
8258                        gctx: module.to_ctx(),
8259                        names: &self.names,
8260                        access: crate::StorageAccess::empty(),
8261                        first_time: false,
8262                    };
8263                    writeln!(
8264                        self.out,
8265                        "threadgroup {ty_context}& {}",
8266                        self.names[&NameKey::GlobalVariable(handle)]
8267                    )?;
8268                }
8269            }
8270
8271            // end of the entry point argument list
8272            writeln!(self.out, ") {{")?;
8273
8274            // Starting the function body.
8275            if do_vertex_pulling {
8276                // Provide zero values for all the attributes, which we will overwrite with
8277                // real data from the vertex attribute buffers, if the indices are in-bounds.
8278                for vbm in &vbm_resolved {
8279                    for attribute in vbm.attributes {
8280                        let location = attribute.shader_location;
8281                        let am_option = am_resolved.get(&location);
8282                        if am_option.is_none() {
8283                            // This bound attribute isn't used in this entry point, so
8284                            // don't bother zero-initializing it.
8285                            continue;
8286                        }
8287                        let am = am_option.unwrap();
8288                        let attribute_ty_name = &am.ty_name;
8289                        let attribute_name = &am.name;
8290
8291                        writeln!(
8292                            self.out,
8293                            "{}{attribute_ty_name} {attribute_name} = {{}};",
8294                            back::Level(1)
8295                        )?;
8296                    }
8297
8298                    // Output a bounds check block that will set real values for the
8299                    // attributes, if the bounds are satisfied.
8300                    write!(self.out, "{}if (", back::Level(1))?;
8301
8302                    let idx = &vbm.id;
8303                    let stride = &vbm.stride;
8304                    let index_name = match vbm.step_mode {
8305                        back::msl::VertexBufferStepMode::Constant => "0",
8306                        back::msl::VertexBufferStepMode::ByVertex => {
8307                            if let Some(ref name) = v_existing_id {
8308                                name
8309                            } else {
8310                                &v_id
8311                            }
8312                        }
8313                        back::msl::VertexBufferStepMode::ByInstance => {
8314                            if let Some(ref name) = i_existing_id {
8315                                name
8316                            } else {
8317                                &i_id
8318                            }
8319                        }
8320                    };
8321                    write!(
8322                        self.out,
8323                        "{index_name} < (_buffer_sizes.buffer_size{idx} / {stride})"
8324                    )?;
8325
8326                    writeln!(self.out, ") {{")?;
8327
8328                    // Pull the bytes out of the vertex buffer.
8329                    let ty_name = &vbm.ty_name;
8330                    let elem_name = &vbm.elem_name;
8331                    let param_name = &vbm.param_name;
8332
8333                    writeln!(
8334                        self.out,
8335                        "{}const {ty_name} {elem_name} = {param_name}[{index_name}];",
8336                        back::Level(2),
8337                    )?;
8338
8339                    // Now set real values for each of the attributes, by unpacking the data
8340                    // from the buffer elements.
8341                    for attribute in vbm.attributes {
8342                        let location = attribute.shader_location;
8343                        let Some(am) = am_resolved.get(&location) else {
8344                            // This bound attribute isn't used in this entry point, so
8345                            // don't bother extracting the data. Too bad we emitted the
8346                            // unpacking function earlier -- it might not get used.
8347                            continue;
8348                        };
8349                        let attribute_name = &am.name;
8350                        let attribute_ty_name = &am.ty_name;
8351
8352                        let offset = attribute.offset;
8353                        let func = unpacking_functions
8354                            .get(&attribute.format)
8355                            .expect("Should have generated this unpacking function earlier.");
8356                        let func_name = &func.name;
8357
8358                        // Check dimensionality of the attribute compared to the unpacking
8359                        // function. If attribute dimension > unpack dimension, we have to
8360                        // pad out the unpack value from a vec4(0, 0, 0, 1) of matching
8361                        // scalar type. Otherwise, if attribute dimension is < unpack
8362                        // dimension, then we need to explicitly truncate the result.
8363                        let needs_padding_or_truncation = am.dimension.cmp(&func.dimension);
8364
8365                        // We need an extra type conversion if the shader type does not
8366                        // match the type returned from the unpacking function.
8367                        let needs_conversion = am.scalar != func.scalar;
8368
8369                        if needs_padding_or_truncation != Ordering::Equal {
8370                            // Emit a comment flagging that a conversion is happening,
8371                            // since the actual logic can be at the end of a long line.
8372                            writeln!(
8373                                self.out,
8374                                "{}// {attribute_ty_name} <- {:?}",
8375                                back::Level(2),
8376                                attribute.format
8377                            )?;
8378                        }
8379
8380                        write!(self.out, "{}{attribute_name} = ", back::Level(2),)?;
8381
8382                        if needs_padding_or_truncation == Ordering::Greater {
8383                            // Needs padding: emit constructor call for wider type
8384                            write!(self.out, "{attribute_ty_name}(")?;
8385                        }
8386
8387                        // Emit call to unpacking function
8388                        if needs_conversion {
8389                            put_numeric_type(&mut self.out, am.scalar, func.dimension.as_slice())?;
8390                            write!(self.out, "(")?;
8391                        }
8392                        write!(self.out, "{func_name}({elem_name}.data[{offset}]")?;
8393                        for i in (offset + 1)..(offset + func.byte_count) {
8394                            write!(self.out, ", {elem_name}.data[{i}]")?;
8395                        }
8396                        write!(self.out, ")")?;
8397                        if needs_conversion {
8398                            write!(self.out, ")")?;
8399                        }
8400
8401                        match needs_padding_or_truncation {
8402                            Ordering::Greater => {
8403                                // Padding
8404                                let ty_is_int = scalar_is_int(am.scalar);
8405                                let zero_value = if ty_is_int { "0" } else { "0.0" };
8406                                let one_value = if ty_is_int { "1" } else { "1.0" };
8407                                for i in func.dimension.map_or(1, u8::from)
8408                                    ..am.dimension.map_or(1, u8::from)
8409                                {
8410                                    write!(
8411                                        self.out,
8412                                        ", {}",
8413                                        if i == 3 { one_value } else { zero_value }
8414                                    )?;
8415                                }
8416                            }
8417                            Ordering::Less => {
8418                                // Truncate to the first `am.dimension` components
8419                                write!(
8420                                    self.out,
8421                                    ".{}",
8422                                    &"xyzw"[0..usize::from(am.dimension.map_or(1, u8::from))]
8423                                )?;
8424                            }
8425                            Ordering::Equal => {}
8426                        }
8427
8428                        if needs_padding_or_truncation == Ordering::Greater {
8429                            write!(self.out, ")")?;
8430                        }
8431
8432                        writeln!(self.out, ";")?;
8433                    }
8434
8435                    // End the bounds check / attribute setting block.
8436                    writeln!(self.out, "{}}}", back::Level(1))?;
8437                }
8438            }
8439
8440            // Metal doesn't support private mutable variables outside of functions,
8441            // so we put them here, just like the locals.
8442            for (handle, var) in module.global_variables.iter() {
8443                let usage = fun_info[handle];
8444                if usage.is_empty() {
8445                    continue;
8446                }
8447                if var.space == crate::AddressSpace::Private {
8448                    let tyvar = TypedGlobalVariable {
8449                        module,
8450                        names: &self.names,
8451                        handle,
8452                        usage,
8453
8454                        reference: false,
8455                    };
8456                    write!(self.out, "{}", back::INDENT)?;
8457                    tyvar.try_fmt(&mut self.out)?;
8458                    match var.init {
8459                        Some(value) => {
8460                            write!(self.out, " = ")?;
8461                            self.put_const_expression(
8462                                value,
8463                                module,
8464                                mod_info,
8465                                &module.global_expressions,
8466                            )?;
8467                            writeln!(self.out, ";")?;
8468                        }
8469                        None => {
8470                            writeln!(self.out, " = {{}};")?;
8471                        }
8472                    };
8473                } else if let Some(ref binding) = var.binding {
8474                    let resolved = options.resolve_resource_binding(ep, binding).unwrap();
8475                    if let Some(sampler) = resolved.as_inline_sampler(options) {
8476                        // write an inline sampler
8477                        let name = &self.names[&NameKey::GlobalVariable(handle)];
8478                        writeln!(
8479                            self.out,
8480                            "{}constexpr {}::sampler {}(",
8481                            back::INDENT,
8482                            NAMESPACE,
8483                            name
8484                        )?;
8485                        self.put_inline_sampler_properties(back::Level(2), sampler, options)?;
8486                        writeln!(self.out, "{});", back::INDENT)?;
8487                    } else if module.types[var.ty].inner.is_external_image() {
8488                        // Wrap the individual arguments for each external texture global
8489                        // in a struct which can be easily passed around.
8490                        let wrapper_name = &self.names[&NameKey::GlobalVariable(handle)];
8491                        let l1 = back::Level(1);
8492                        let l2 = l1.next();
8493                        writeln!(
8494                            self.out,
8495                            "{l1}const {EXTERNAL_TEXTURE_WRAPPER_STRUCT} {wrapper_name} {{"
8496                        )?;
8497                        for i in 0..3 {
8498                            let plane_name = &self.names[&NameKey::ExternalTextureGlobalVariable(
8499                                handle,
8500                                ExternalTextureNameKey::Plane(i),
8501                            )];
8502                            writeln!(self.out, "{l2}.plane{i} = {plane_name},")?;
8503                        }
8504                        let params_name = &self.names[&NameKey::ExternalTextureGlobalVariable(
8505                            handle,
8506                            ExternalTextureNameKey::Params,
8507                        )];
8508                        writeln!(self.out, "{l2}.params = {params_name},")?;
8509                        writeln!(self.out, "{l1}}};")?;
8510                    }
8511                }
8512            }
8513
8514            if need_workgroup_variables_initialization {
8515                self.write_workgroup_variables_initialization(
8516                    module,
8517                    mod_info,
8518                    fun_info,
8519                    local_invocation_index,
8520                    ep.stage,
8521                )?;
8522            }
8523
8524            // Now take the arguments that we gathered into structs, and the
8525            // structs that we flattened into arguments, and emit local
8526            // variables with initializers that put everything back the way the
8527            // body code expects.
8528            //
8529            // If we had to generate fresh names for struct members passed as
8530            // arguments, be sure to use those names when rebuilding the struct.
8531            //
8532            // "Each day, I change some zeros to ones, and some ones to zeros.
8533            // The rest, I leave alone."
8534            for (arg_index, arg) in fun.arguments.iter().enumerate() {
8535                let arg_name =
8536                    &self.names[&NameKey::EntryPointArgument(ep_index as _, arg_index as u32)];
8537                match module.types[arg.ty].inner {
8538                    crate::TypeInner::Struct { ref members, .. } => {
8539                        let struct_name = &self.names[&NameKey::Type(arg.ty)];
8540                        write!(
8541                            self.out,
8542                            "{}const {} {} = {{ ",
8543                            back::INDENT,
8544                            struct_name,
8545                            arg_name
8546                        )?;
8547                        for (member_index, member) in members.iter().enumerate() {
8548                            let key = NameKey::StructMember(arg.ty, member_index as u32);
8549                            let name = &flattened_member_names[&key];
8550                            if member_index != 0 {
8551                                write!(self.out, ", ")?;
8552                            }
8553                            // insert padding initialization, if needed
8554                            if self
8555                                .struct_member_pads
8556                                .contains(&(arg.ty, member_index as u32))
8557                            {
8558                                write!(self.out, "{{}}, ")?;
8559                            }
8560                            match member.binding {
8561                                Some(crate::Binding::Location {
8562                                    interpolation: Some(crate::Interpolation::PerVertex),
8563                                    ..
8564                                }) => {
8565                                    writeln!(
8566                                        self.out,
8567                                        "{0}{{ {1}.{2}.get({NAMESPACE}::vertex_index::first), {1}.{2}.get({NAMESPACE}::vertex_index::second), {1}.{2}.get({NAMESPACE}::vertex_index::third) }}",
8568                                        back::INDENT,
8569                                        varyings_member_name,
8570                                        arg_name,
8571                                    )?;
8572                                    continue;
8573                                }
8574                                Some(crate::Binding::Location { .. }) if has_varyings => {
8575                                    write!(self.out, "{varyings_member_name}.")?;
8576                                }
8577                                _ => (),
8578                            }
8579                            write!(self.out, "{name}")?;
8580                        }
8581                        writeln!(self.out, " }};")?;
8582                    }
8583                    _ => match arg.binding {
8584                        Some(crate::Binding::Location {
8585                            interpolation: Some(crate::Interpolation::PerVertex),
8586                            ..
8587                        }) => {
8588                            let ty_name = TypeContext {
8589                                handle: arg.ty,
8590                                gctx: module.to_ctx(),
8591                                names: &self.names,
8592                                access: crate::StorageAccess::empty(),
8593                                first_time: false,
8594                            };
8595                            writeln!(
8596                                self.out,
8597                                "{0}const {ty_name} {arg_name} = {{ {1}.{2}.get({NAMESPACE}::vertex_index::first), {1}.{2}.get({NAMESPACE}::vertex_index::second), {1}.{2}.get({NAMESPACE}::vertex_index::third) }};",
8598                                back::INDENT,
8599                                varyings_member_name,
8600                                arg_name,
8601                            )?;
8602                        }
8603                        Some(crate::Binding::Location { .. })
8604                        | Some(crate::Binding::BuiltIn(crate::BuiltIn::Barycentric { .. }))
8605                            if has_varyings =>
8606                        {
8607                            writeln!(
8608                                self.out,
8609                                "{}const auto {} = {}.{};",
8610                                back::INDENT,
8611                                arg_name,
8612                                varyings_member_name,
8613                                arg_name
8614                            )?;
8615                        }
8616                        _ => {}
8617                    },
8618                }
8619            }
8620
8621            let guarded_indices =
8622                index::find_checked_indexes(module, fun, fun_info, options.bounds_check_policies);
8623
8624            let context = StatementContext {
8625                expression: ExpressionContext {
8626                    function: fun,
8627                    origin: FunctionOrigin::EntryPoint(ep_index as _),
8628                    info: fun_info,
8629                    lang_version: options.lang_version,
8630                    policies: options.bounds_check_policies,
8631                    guarded_indices,
8632                    module,
8633                    mod_info,
8634                    pipeline_options,
8635                    force_loop_bounding: options.force_loop_bounding,
8636                    emit_int_div_checks: options.emit_int_div_checks,
8637                    ray_query_initialization_tracking: options.ray_query_initialization_tracking,
8638                },
8639                result_struct: if ep.stage == crate::ShaderStage::Task {
8640                    None
8641                } else {
8642                    Some(&stage_out_name)
8643                },
8644            };
8645
8646            // Finally, declare all the local variables that we need
8647            //TODO: we can postpone this till the relevant expressions are emitted
8648            self.put_locals(&context.expression)?;
8649            self.update_expressions_to_bake(fun, fun_info, &context.expression);
8650            self.put_block(back::Level(1), &fun.body, &context)?;
8651            writeln!(self.out, "}}")?;
8652            if ep_index + 1 != module.entry_points.len() {
8653                writeln!(self.out)?;
8654            }
8655            self.named_expressions.clear();
8656            self.hoisted_loop_expressions.clear();
8657
8658            if is_wrapped {
8659                self.write_wrapper_function(NestedFunctionInfo {
8660                    options,
8661                    ep,
8662                    module,
8663                    mod_info,
8664                    fun_info,
8665                    args,
8666                    local_invocation_index,
8667                    nested_name: &nested_fun_name,
8668                    outer_name: &fun_name,
8669                    out_mesh_info,
8670                })?;
8671            }
8672        }
8673
8674        Ok(info)
8675    }
8676
8677    pub(super) fn write_barrier(
8678        &mut self,
8679        flags: crate::Barrier,
8680        level: back::Level,
8681    ) -> BackendResult {
8682        // Note: OR-ring bitflags requires `__HAVE_MEMFLAG_OPERATORS__`,
8683        // so we try to avoid it here.
8684        if flags.is_empty() {
8685            writeln!(
8686                self.out,
8687                "{level}{NAMESPACE}::threadgroup_barrier({NAMESPACE}::mem_flags::mem_none);",
8688            )?;
8689        }
8690        if flags.contains(crate::Barrier::STORAGE) {
8691            writeln!(
8692                self.out,
8693                "{level}{NAMESPACE}::threadgroup_barrier({NAMESPACE}::mem_flags::mem_device);",
8694            )?;
8695        }
8696        if flags.contains(crate::Barrier::WORK_GROUP) {
8697            writeln!(
8698                self.out,
8699                "{level}{NAMESPACE}::threadgroup_barrier({NAMESPACE}::mem_flags::mem_threadgroup);",
8700            )?;
8701            if self.needs_object_memory_barriers {
8702                writeln!(
8703                    self.out,
8704                    "{level}{NAMESPACE}::threadgroup_barrier({NAMESPACE}::mem_flags::mem_object_data);",
8705                )?;
8706            }
8707        }
8708        if flags.contains(crate::Barrier::SUB_GROUP) {
8709            writeln!(
8710                self.out,
8711                "{level}{NAMESPACE}::simdgroup_barrier({NAMESPACE}::mem_flags::mem_threadgroup);",
8712            )?;
8713        }
8714        if flags.contains(crate::Barrier::TEXTURE) {
8715            writeln!(
8716                self.out,
8717                "{level}{NAMESPACE}::threadgroup_barrier({NAMESPACE}::mem_flags::mem_texture);",
8718            )?;
8719        }
8720        Ok(())
8721    }
8722}
8723
8724/// Initializing workgroup variables is more tricky for Metal because we have to deal
8725/// with atomics at the type-level (which don't have a copy constructor).
8726mod workgroup_mem_init {
8727    use crate::EntryPoint;
8728
8729    use super::*;
8730
8731    enum Access {
8732        GlobalVariable(Handle<crate::GlobalVariable>),
8733        StructMember(Handle<crate::Type>, u32),
8734        Array(usize),
8735    }
8736
8737    impl Access {
8738        fn write<W: Write>(
8739            &self,
8740            writer: &mut W,
8741            names: &FastHashMap<NameKey, String>,
8742        ) -> Result<(), core::fmt::Error> {
8743            match *self {
8744                Access::GlobalVariable(handle) => {
8745                    write!(writer, "{}", names[&NameKey::GlobalVariable(handle)])
8746                }
8747                Access::StructMember(handle, index) => {
8748                    write!(writer, ".{}", names[&NameKey::StructMember(handle, index)])
8749                }
8750                Access::Array(depth) => write!(writer, ".{WRAPPED_ARRAY_FIELD}[__i{depth}]"),
8751            }
8752        }
8753    }
8754
8755    struct AccessStack {
8756        stack: Vec<Access>,
8757        array_depth: usize,
8758    }
8759
8760    impl AccessStack {
8761        const fn new() -> Self {
8762            Self {
8763                stack: Vec::new(),
8764                array_depth: 0,
8765            }
8766        }
8767
8768        fn enter_array<R>(&mut self, cb: impl FnOnce(&mut Self, usize) -> R) -> R {
8769            let array_depth = self.array_depth;
8770            self.stack.push(Access::Array(array_depth));
8771            self.array_depth += 1;
8772            let res = cb(self, array_depth);
8773            self.stack.pop();
8774            self.array_depth -= 1;
8775            res
8776        }
8777
8778        fn enter<R>(&mut self, new: Access, cb: impl FnOnce(&mut Self) -> R) -> R {
8779            self.stack.push(new);
8780            let res = cb(self);
8781            self.stack.pop();
8782            res
8783        }
8784
8785        fn write<W: Write>(
8786            &self,
8787            writer: &mut W,
8788            names: &FastHashMap<NameKey, String>,
8789        ) -> Result<(), core::fmt::Error> {
8790            for next in self.stack.iter() {
8791                next.write(writer, names)?;
8792            }
8793            Ok(())
8794        }
8795    }
8796
8797    impl<W: Write> Writer<W> {
8798        pub(super) fn need_workgroup_variables_initialization(
8799            &mut self,
8800            options: &Options,
8801            ep: &EntryPoint,
8802            module: &crate::Module,
8803            fun_info: &valid::FunctionInfo,
8804        ) -> bool {
8805            let is_task = ep.stage == crate::ShaderStage::Task;
8806            options.zero_initialize_workgroup_memory
8807                && ep.stage.compute_like()
8808                && module.global_variables.iter().any(|(handle, var)| {
8809                    let is_right_address_space = var.space == crate::AddressSpace::WorkGroup
8810                        || (var.space == crate::AddressSpace::TaskPayload && is_task);
8811                    !fun_info[handle].is_empty() && is_right_address_space
8812                })
8813        }
8814
8815        pub fn write_workgroup_variables_initialization(
8816            &mut self,
8817            module: &crate::Module,
8818            module_info: &valid::ModuleInfo,
8819            fun_info: &valid::FunctionInfo,
8820            local_invocation_index: Option<&NameKey>,
8821            stage: crate::ShaderStage,
8822        ) -> BackendResult {
8823            let level = back::Level(1);
8824
8825            writeln!(
8826                self.out,
8827                "{}if ({} == 0u) {{",
8828                level,
8829                local_invocation_index
8830                    .map(|name_key| self.names[name_key].as_str())
8831                    .unwrap_or("__local_invocation_index"),
8832            )?;
8833
8834            let mut access_stack = AccessStack::new();
8835
8836            let is_task = stage == crate::ShaderStage::Task;
8837            let vars = module.global_variables.iter().filter(|&(handle, var)| {
8838                let is_right_address_space = var.space == crate::AddressSpace::WorkGroup
8839                    || (var.space == crate::AddressSpace::TaskPayload && is_task);
8840                !fun_info[handle].is_empty() && is_right_address_space
8841            });
8842
8843            for (handle, var) in vars {
8844                access_stack.enter(Access::GlobalVariable(handle), |access_stack| {
8845                    self.write_workgroup_variable_initialization(
8846                        module,
8847                        module_info,
8848                        var.ty,
8849                        access_stack,
8850                        level.next(),
8851                    )
8852                })?;
8853            }
8854
8855            writeln!(self.out, "{level}}}")?;
8856            self.write_barrier(crate::Barrier::WORK_GROUP, level)
8857        }
8858
8859        fn write_workgroup_variable_initialization(
8860            &mut self,
8861            module: &crate::Module,
8862            module_info: &valid::ModuleInfo,
8863            ty: Handle<crate::Type>,
8864            access_stack: &mut AccessStack,
8865            level: back::Level,
8866        ) -> BackendResult {
8867            if module_info[ty].contains(valid::TypeFlags::CONSTRUCTIBLE) {
8868                write!(self.out, "{level}")?;
8869                access_stack.write(&mut self.out, &self.names)?;
8870                writeln!(self.out, " = {{}};")?;
8871            } else {
8872                match module.types[ty].inner {
8873                    crate::TypeInner::Atomic { .. } => {
8874                        write!(
8875                            self.out,
8876                            "{level}{NAMESPACE}::atomic_store_explicit({ATOMIC_REFERENCE}"
8877                        )?;
8878                        access_stack.write(&mut self.out, &self.names)?;
8879                        writeln!(self.out, ", 0, {NAMESPACE}::memory_order_relaxed);")?;
8880                    }
8881                    crate::TypeInner::Array { base, size, .. } => {
8882                        let count = match size.resolve(module.to_ctx())? {
8883                            proc::IndexableLength::Known(count) => count,
8884                            proc::IndexableLength::Dynamic => unreachable!(),
8885                        };
8886
8887                        access_stack.enter_array(|access_stack, array_depth| {
8888                            writeln!(
8889                                self.out,
8890                                "{level}for (int __i{array_depth} = 0; __i{array_depth} < {count}; __i{array_depth}++) {{"
8891                            )?;
8892                            self.write_workgroup_variable_initialization(
8893                                module,
8894                                module_info,
8895                                base,
8896                                access_stack,
8897                                level.next(),
8898                            )?;
8899                            writeln!(self.out, "{level}}}")?;
8900                            BackendResult::Ok(())
8901                        })?;
8902                    }
8903                    crate::TypeInner::Struct { ref members, .. } => {
8904                        for (index, member) in members.iter().enumerate() {
8905                            access_stack.enter(
8906                                Access::StructMember(ty, index as u32),
8907                                |access_stack| {
8908                                    self.write_workgroup_variable_initialization(
8909                                        module,
8910                                        module_info,
8911                                        member.ty,
8912                                        access_stack,
8913                                        level,
8914                                    )
8915                                },
8916                            )?;
8917                        }
8918                    }
8919                    _ => unreachable!(),
8920                }
8921            }
8922
8923            Ok(())
8924        }
8925    }
8926}
8927
8928impl crate::AtomicFunction {
8929    const fn to_msl(self) -> &'static str {
8930        match self {
8931            Self::Add => "fetch_add",
8932            Self::Subtract => "fetch_sub",
8933            Self::And => "fetch_and",
8934            Self::InclusiveOr => "fetch_or",
8935            Self::ExclusiveOr => "fetch_xor",
8936            Self::Min => "fetch_min",
8937            Self::Max => "fetch_max",
8938            Self::Exchange { compare: None } => "exchange",
8939            Self::Exchange { compare: Some(_) } => ATOMIC_COMP_EXCH_FUNCTION,
8940        }
8941    }
8942
8943    fn to_msl_64_bit(self) -> Result<&'static str, Error> {
8944        Ok(match self {
8945            Self::Min => "min",
8946            Self::Max => "max",
8947            _ => Err(Error::FeatureNotImplemented(
8948                "64-bit atomic operation other than min/max".to_string(),
8949            ))?,
8950        })
8951    }
8952}