naga/back/msl/
keywords.rs

1use crate::proc::{concrete_int_scalars, vector_size_str, vector_sizes, KeywordSet};
2use crate::racy_lock::RacyLock;
3use alloc::{format, string::String, vec::Vec};
4
5// MSLS - Metal Shading Language Specification:
6// https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf
7//
8// C++ - Standard for Programming Language C++ (N4431)
9// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4431.pdf
10const RESERVED: &[&str] = &[
11    // Undocumented
12    "assert", // found in https://github.com/gfx-rs/wgpu/issues/5347
13    // Standard for Programming Language C++ (N4431): 2.5 Alternative tokens
14    "and",
15    "bitor",
16    "or",
17    "xor",
18    "compl",
19    "bitand",
20    "and_eq",
21    "or_eq",
22    "xor_eq",
23    "not",
24    "not_eq",
25    // Standard for Programming Language C++ (N4431): 2.11 Keywords
26    "alignas",
27    "alignof",
28    "asm",
29    "auto",
30    "bool",
31    "break",
32    "case",
33    "catch",
34    "char",
35    "char16_t",
36    "char32_t",
37    "class",
38    "const",
39    "constexpr",
40    "const_cast",
41    "continue",
42    "decltype",
43    "default",
44    "delete",
45    "do",
46    "double",
47    "dynamic_cast",
48    "else",
49    "enum",
50    "explicit",
51    "export",
52    "extern",
53    "false",
54    "float",
55    "for",
56    "friend",
57    "goto",
58    "if",
59    "inline",
60    "int",
61    "long",
62    "mutable",
63    "namespace",
64    "new",
65    "noexcept",
66    "nullptr",
67    "operator",
68    "private",
69    "protected",
70    "public",
71    "register",
72    "reinterpret_cast",
73    "return",
74    "short",
75    "signed",
76    "sizeof",
77    "static",
78    "static_assert",
79    "static_cast",
80    "struct",
81    "switch",
82    "template",
83    "this",
84    "thread_local",
85    "throw",
86    "true",
87    "try",
88    "typedef",
89    "typeid",
90    "typename",
91    "union",
92    "unsigned",
93    "using",
94    "virtual",
95    "void",
96    "volatile",
97    "wchar_t",
98    "while",
99    // Metal Shading Language Specification: 1.4.4 Restrictions
100    "main",
101    // Metal Shading Language Specification: 2.1 Scalar Data Types
102    "int8_t",
103    "uchar",
104    "uint8_t",
105    "int16_t",
106    "ushort",
107    "uint16_t",
108    "int32_t",
109    "uint",
110    "uint32_t",
111    "int64_t",
112    "uint64_t",
113    "half",
114    "bfloat",
115    "size_t",
116    "ptrdiff_t",
117    // Metal Shading Language Specification: 2.2 Vector Data Types
118    "bool2",
119    "bool3",
120    "bool4",
121    "char2",
122    "char3",
123    "char4",
124    "short2",
125    "short3",
126    "short4",
127    "int2",
128    "int3",
129    "int4",
130    "long2",
131    "long3",
132    "long4",
133    "uchar2",
134    "uchar3",
135    "uchar4",
136    "ushort2",
137    "ushort3",
138    "ushort4",
139    "uint2",
140    "uint3",
141    "uint4",
142    "ulong2",
143    "ulong3",
144    "ulong4",
145    "half2",
146    "half3",
147    "half4",
148    "bfloat2",
149    "bfloat3",
150    "bfloat4",
151    "float2",
152    "float3",
153    "float4",
154    "vec",
155    // Metal Shading Language Specification: 2.2.3 Packed Vector Types
156    "packed_bool2",
157    "packed_bool3",
158    "packed_bool4",
159    "packed_char2",
160    "packed_char3",
161    "packed_char4",
162    "packed_short2",
163    "packed_short3",
164    "packed_short4",
165    "packed_int2",
166    "packed_int3",
167    "packed_int4",
168    "packed_uchar2",
169    "packed_uchar3",
170    "packed_uchar4",
171    "packed_ushort2",
172    "packed_ushort3",
173    "packed_ushort4",
174    "packed_uint2",
175    "packed_uint3",
176    "packed_uint4",
177    "packed_half2",
178    "packed_half3",
179    "packed_half4",
180    "packed_bfloat2",
181    "packed_bfloat3",
182    "packed_bfloat4",
183    "packed_float2",
184    "packed_float3",
185    "packed_float4",
186    "packed_long2",
187    "packed_long3",
188    "packed_long4",
189    "packed_vec",
190    // Metal Shading Language Specification: 2.3 Matrix Data Types
191    "half2x2",
192    "half2x3",
193    "half2x4",
194    "half3x2",
195    "half3x3",
196    "half3x4",
197    "half4x2",
198    "half4x3",
199    "half4x4",
200    "float2x2",
201    "float2x3",
202    "float2x4",
203    "float3x2",
204    "float3x3",
205    "float3x4",
206    "float4x2",
207    "float4x3",
208    "float4x4",
209    "matrix",
210    // Metal Shading Language Specification: 2.6 Atomic Data Types
211    "atomic",
212    "atomic_int",
213    "atomic_uint",
214    "atomic_bool",
215    "atomic_ulong",
216    "atomic_float",
217    // Metal Shading Language Specification: 2.20 Type Conversions and Re-interpreting Data
218    "as_type",
219    // Metal Shading Language Specification: 4 Address Spaces
220    "device",
221    "constant",
222    "thread",
223    "threadgroup",
224    "threadgroup_imageblock",
225    "ray_data",
226    "object_data",
227    // Metal Shading Language Specification: 5.1 Functions
228    "vertex",
229    "fragment",
230    "kernel",
231    // Metal Shading Language Specification: 6.1 Namespace and Header Files
232    "metal",
233    // C99 / C++ extension:
234    "restrict",
235    // Metal reserved types in <metal_types>:
236    "llong",
237    "ullong",
238    "quad",
239    "complex",
240    "imaginary",
241    // Constants in <metal_types>:
242    "CHAR_BIT",
243    "SCHAR_MAX",
244    "SCHAR_MIN",
245    "UCHAR_MAX",
246    "CHAR_MAX",
247    "CHAR_MIN",
248    "USHRT_MAX",
249    "SHRT_MAX",
250    "SHRT_MIN",
251    "UINT_MAX",
252    "INT_MAX",
253    "INT_MIN",
254    "ULONG_MAX",
255    "LONG_MAX",
256    "LONG_MIN",
257    "ULLONG_MAX",
258    "LLONG_MAX",
259    "LLONG_MIN",
260    "FLT_DIG",
261    "FLT_MANT_DIG",
262    "FLT_MAX_10_EXP",
263    "FLT_MAX_EXP",
264    "FLT_MIN_10_EXP",
265    "FLT_MIN_EXP",
266    "FLT_RADIX",
267    "FLT_MAX",
268    "FLT_MIN",
269    "FLT_EPSILON",
270    "FLT_DECIMAL_DIG",
271    "FP_ILOGB0",
272    "FP_ILOGB0",
273    "FP_ILOGBNAN",
274    "FP_ILOGBNAN",
275    "MAXFLOAT",
276    "HUGE_VALF",
277    "INFINITY",
278    "NAN",
279    "M_E_F",
280    "M_LOG2E_F",
281    "M_LOG10E_F",
282    "M_LN2_F",
283    "M_LN10_F",
284    "M_PI_F",
285    "M_PI_2_F",
286    "M_PI_4_F",
287    "M_1_PI_F",
288    "M_2_PI_F",
289    "M_2_SQRTPI_F",
290    "M_SQRT2_F",
291    "M_SQRT1_2_F",
292    "HALF_DIG",
293    "HALF_MANT_DIG",
294    "HALF_MAX_10_EXP",
295    "HALF_MAX_EXP",
296    "HALF_MIN_10_EXP",
297    "HALF_MIN_EXP",
298    "HALF_RADIX",
299    "HALF_MAX",
300    "HALF_MIN",
301    "HALF_EPSILON",
302    "HALF_DECIMAL_DIG",
303    "MAXHALF",
304    "HUGE_VALH",
305    "M_E_H",
306    "M_LOG2E_H",
307    "M_LOG10E_H",
308    "M_LN2_H",
309    "M_LN10_H",
310    "M_PI_H",
311    "M_PI_2_H",
312    "M_PI_4_H",
313    "M_1_PI_H",
314    "M_2_PI_H",
315    "M_2_SQRTPI_H",
316    "M_SQRT2_H",
317    "M_SQRT1_2_H",
318    "DBL_DIG",
319    "DBL_MANT_DIG",
320    "DBL_MAX_10_EXP",
321    "DBL_MAX_EXP",
322    "DBL_MIN_10_EXP",
323    "DBL_MIN_EXP",
324    "DBL_RADIX",
325    "DBL_MAX",
326    "DBL_MIN",
327    "DBL_EPSILON",
328    "DBL_DECIMAL_DIG",
329    "MAXDOUBLE",
330    "HUGE_VAL",
331    "M_E",
332    "M_LOG2E",
333    "M_LOG10E",
334    "M_LN2",
335    "M_LN10",
336    "M_PI",
337    "M_PI_2",
338    "M_PI_4",
339    "M_1_PI",
340    "M_2_PI",
341    "M_2_SQRTPI",
342    "M_SQRT2",
343    "M_SQRT1_2",
344    // Naga utilities
345    "DefaultConstructible",
346    super::writer::FREXP_FUNCTION,
347    super::writer::MODF_FUNCTION,
348    super::writer::ABS_FUNCTION,
349    super::writer::DIV_FUNCTION,
350    // DOT_FUNCTION_PREFIX variants are added dynamically below
351    super::writer::MOD_FUNCTION,
352    super::writer::NEG_FUNCTION,
353    super::writer::F2I32_FUNCTION,
354    super::writer::F2U32_FUNCTION,
355    super::writer::F2I64_FUNCTION,
356    super::writer::F2U64_FUNCTION,
357    super::writer::IMAGE_LOAD_EXTERNAL_FUNCTION,
358    super::writer::IMAGE_SAMPLE_BASE_CLAMP_TO_EDGE_FUNCTION,
359    super::writer::IMAGE_SIZE_EXTERNAL_FUNCTION,
360    super::writer::ARGUMENT_BUFFER_WRAPPER_STRUCT,
361    super::writer::EXTERNAL_TEXTURE_WRAPPER_STRUCT,
362];
363
364// The set of concrete integer dot product function variants.
365// This must match the set of names that could be produced by
366// `Writer::get_dot_wrapper_function_helper_name`.
367static DOT_FUNCTION_NAMES: RacyLock<Vec<String>> = RacyLock::new(|| {
368    let mut names = Vec::new();
369    for scalar in concrete_int_scalars().map(crate::Scalar::to_msl_name) {
370        for size_suffix in vector_sizes().map(vector_size_str) {
371            let fun_name = format!(
372                "{}_{}{}",
373                super::writer::DOT_FUNCTION_PREFIX,
374                scalar,
375                size_suffix
376            );
377            names.push(fun_name);
378        }
379    }
380    names
381});
382
383/// The above set of reserved keywords, turned into a cached HashSet. This saves
384/// significant time during [`Namer::reset`](crate::proc::Namer::reset).
385///
386/// See <https://github.com/gfx-rs/wgpu/pull/7338> for benchmarks.
387pub static RESERVED_SET: RacyLock<KeywordSet> = RacyLock::new(|| {
388    let mut set = KeywordSet::from_iter(RESERVED);
389    set.extend(DOT_FUNCTION_NAMES.iter().map(String::as_str));
390    set
391});