Skip to content

Commit 0dbb3b4

Browse files
committed
Use concrete_int_scalars and vector_sizes helpers. Avoid Box::leak.
1 parent 4c6e527 commit 0dbb3b4

File tree

2 files changed

+30
-60
lines changed

2 files changed

+30
-60
lines changed

naga/src/back/msl/keywords.rs

Lines changed: 22 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
use crate::common;
2-
use crate::proc::KeywordSet;
1+
use crate::proc::{concrete_int_scalars, vector_size_str, vector_sizes, KeywordSet};
32
use crate::racy_lock::RacyLock;
4-
use alloc::vec::Vec;
5-
use alloc::{boxed::Box, format};
3+
use alloc::{format, string::String, vec::Vec};
64

75
// MSLS - Metal Shading Language Specification:
86
// https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf
97
//
108
// C++ - Standard for Programming Language C++ (N4431)
119
// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4431.pdf
12-
pub const RESERVED: &[&str] = &[
10+
const RESERVED: &[&str] = &[
1311
// Undocumented
1412
"assert", // found in https://github.com/gfx-rs/wgpu/issues/5347
1513
// Standard for Programming Language C++ (N4431): 2.5 Alternative tokens
@@ -363,65 +361,31 @@ pub const RESERVED: &[&str] = &[
363361
super::writer::EXTERNAL_TEXTURE_WRAPPER_STRUCT,
364362
];
365363

366-
const CONCRETE_INTEGER_SCALARS: [crate::Scalar; 4] = [
367-
crate::Scalar::I32,
368-
crate::Scalar::U32,
369-
crate::Scalar::I64,
370-
crate::Scalar::U64,
371-
];
372-
373-
const CONCRETE_VECTOR_SIZES: [crate::VectorSize; 3] = [
374-
crate::VectorSize::Bi,
375-
crate::VectorSize::Tri,
376-
crate::VectorSize::Quad,
377-
];
378-
379-
/// The above set of reserved keywords, turned into a cached HashSet. This saves
380-
/// significant time during [`Namer::reset`](crate::proc::Namer::reset).
381-
///
382-
/// See <https://github.com/gfx-rs/wgpu/pull/7338> for benchmarks.
383-
pub static RESERVED_SET: RacyLock<KeywordSet> = RacyLock::new(|| {
384-
let mut set = KeywordSet::from_iter(RESERVED);
385-
// Add all concrete integer dot product function variants.
386-
// These are generated to match the names produced by
387-
// `Writer::get_dot_wrapper_function_helper_name`, ensuring they stay in sync.
388-
let mut dot_function_names: Vec<&'static str> = Vec::new();
389-
for scalar in CONCRETE_INTEGER_SCALARS {
390-
// Map scalar to MSL type name (matching the logic in Scalar::to_msl_name)
391-
let type_name = match scalar {
392-
crate::Scalar {
393-
kind: crate::ScalarKind::Sint,
394-
width: 4,
395-
} => "int",
396-
crate::Scalar {
397-
kind: crate::ScalarKind::Uint,
398-
width: 4,
399-
} => "uint",
400-
crate::Scalar {
401-
kind: crate::ScalarKind::Sint,
402-
width: 8,
403-
} => "long",
404-
crate::Scalar {
405-
kind: crate::ScalarKind::Uint,
406-
width: 8,
407-
} => "ulong",
408-
_ => continue, // Skip non-integer or unsupported types
409-
};
410-
411-
for size in CONCRETE_VECTOR_SIZES {
412-
let size_suffix = common::vector_size_str(size);
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`.
367+
static 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) {
413371
let fun_name = format!(
414372
"{}_{}{}",
415373
super::writer::DOT_FUNCTION_PREFIX,
416-
type_name,
374+
scalar,
417375
size_suffix
418376
);
419-
// Convert to &'static str by leaking the String
420-
// (In theory) This is safe because these are generated once and cached
421-
let leaked = Box::leak(fun_name.into_boxed_str());
422-
dot_function_names.push(leaked);
377+
names.push(fun_name);
423378
}
424379
}
425-
set.extend(dot_function_names);
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.
387+
pub 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));
426390
set
427391
});

naga/src/back/msl/writer.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
back::{self, get_entry_points, Baked},
2020
common,
2121
proc::{
22-
self,
22+
self, concrete_int_scalars,
2323
index::{self, BoundsCheck},
2424
ExternalTextureNameKey, NameKey, TypeResolution,
2525
},
@@ -489,7 +489,7 @@ pub struct Writer<W> {
489489
}
490490

491491
impl crate::Scalar {
492-
fn to_msl_name(self) -> &'static str {
492+
pub(super) fn to_msl_name(self) -> &'static str {
493493
use crate::ScalarKind as Sk;
494494
match self {
495495
Self {
@@ -5796,12 +5796,18 @@ template <typename A>
57965796
}
57975797

57985798
/// Build the mangled helper name for integer vector dot products.
5799+
///
5800+
/// `scalar` must be a concrete integer scalar type.
5801+
///
57995802
/// Result format: `{DOT_FUNCTION_PREFIX}_{type}{N}` (e.g., `naga_dot_int3`).
58005803
fn get_dot_wrapper_function_helper_name(
58015804
&self,
58025805
scalar: crate::Scalar,
58035806
size: crate::VectorSize,
58045807
) -> String {
5808+
// Check for consistency with [`super::keywords::RESERVED_SET`]
5809+
debug_assert!(concrete_int_scalars().any(|s| s == scalar));
5810+
58055811
let type_name = scalar.to_msl_name();
58065812
let size_suffix = common::vector_size_str(size);
58075813
format!("{DOT_FUNCTION_PREFIX}_{type_name}{size_suffix}")

0 commit comments

Comments
 (0)