diff --git a/crates/metal2vulkan-air/src/abi/module.rs b/crates/metal2vulkan-air/src/abi/module.rs index 9a20df512..7fb948d64 100644 --- a/crates/metal2vulkan-air/src/abi/module.rs +++ b/crates/metal2vulkan-air/src/abi/module.rs @@ -388,9 +388,12 @@ fn plain_signature(n: &mut Ops<'_>, site: &Site) -> Result, !"", }`. The `air.max_*` limits are /// decoded; `wchar_size`, `frame-pointer`, `SDK Version`, and debug info's `Dwarf Version` and -/// `Debug Info Version` are LLVM's and carry nothing the translation reads; `air.workaround.sampler_state_variable_length_encoding` (an `i1`) names the -/// form of the module's constexpr sampler words, which each `air.sampler_state` global's own type -/// states; any other flag is unknown. +/// `Debug Info Version` are LLVM's and carry nothing the translation reads, and neither do +/// `agx.allow_mismatched_component_counts` (an integer, 0 or 1) and `irc.version_major`, +/// `irc.version_minor` and `irc.version_patch` (integers), each stated once: their shapes are +/// checked and nothing is kept. `air.workaround.sampler_state_variable_length_encoding` (an `i1`) +/// names the form of the module's constexpr sampler words, which each `air.sampler_state` global's +/// own type states. Any other flag is unknown. fn module_flags( m: &Module, ops: &[MdId], @@ -399,11 +402,28 @@ fn module_flags( ) -> Result { let key = Some("llvm.module.flags"); let mut l = ModuleLimits::default(); + let (mut mismatch, mut major, mut minor, mut patch) = (false, false, false, false); for &id in ops { meter.tick(1)?; let mut n = Ops::node(m, id, site.clone())?; n.u32(key)?; let flag = n.string(key)?; + // The converter's flags: an integer each, stated once; the agx flag 0 or 1. + let shape = match flag { + b"agx.allow_mismatched_component_counts" => Some((&mut mismatch, 1)), + b"irc.version_major" => Some((&mut major, u32::MAX)), + b"irc.version_minor" => Some((&mut minor, u32::MAX)), + b"irc.version_patch" => Some((&mut patch, u32::MAX)), + _ => None, + }; + if let Some((seen, limit)) = shape { + let repeated = std::mem::replace(seen, true); + if n.u32(key)? > limit || repeated { + return Err(site.err(AbiError::BadValue, key)); + } + n.finish()?; + continue; + } let slot = match flag { b"air.max_device_buffers" => &mut l.max_device_buffers, b"air.max_constant_buffers" => &mut l.max_constant_buffers, diff --git a/crates/metal2vulkan-air/src/abi/tests.rs b/crates/metal2vulkan-air/src/abi/tests.rs index 9ccae4a55..456678f4a 100644 --- a/crates/metal2vulkan-air/src/abi/tests.rs +++ b/crates/metal2vulkan-air/src/abi/tests.rs @@ -3436,6 +3436,46 @@ fn static_samplers_decode_from_their_globals() { ); } +/// The converter's module flags (`agx.allow_mismatched_component_counts`, `irc.version_*`) are +/// checked for their shapes and kept nowhere: the module decodes as without them. +#[test] +fn converter_module_flags_are_checked_and_dropped() { + let flags = |nodes: &[(u64, &'static str, u64)]| { + let nodes = nodes.to_vec(); + module_with(move |b| { + let ids: Vec = nodes + .iter() + .map(|&(behavior, key, value)| node(b, &[I(behavior), S(key), I(value)])) + .collect(); + b.named_md("llvm.module.flags", &ids); + }) + }; + let plain = flags(&[(7, "air.max_textures", 128)]).unwrap(); + let with = flags(&[ + (7, "air.max_textures", 128), + (7, "agx.allow_mismatched_component_counts", 1), + (1, "irc.version_major", 3), + (1, "irc.version_minor", 1), + (1, "irc.version_patch", 4), + ]) + .unwrap(); + assert_eq!(with.limits, plain.limits); + assert_eq!(with.compile, plain.compile); + flags(&[(7, "agx.allow_mismatched_component_counts", 0)]).unwrap(); + flags(&[(1, "irc.version_major", 3)]).unwrap(); + for refused in [ + &[(1, "irc.version_major", 3), (1, "irc.version_major", 3)][..], + &[ + (7, "agx.allow_mismatched_component_counts", 1), + (7, "agx.allow_mismatched_component_counts", 1), + ][..], + &[(7, "agx.allow_mismatched_component_counts", 2)][..], + ] { + expect_abi(flags(refused), AbiError::BadValue); + } + expect_abi(flags(&[(7, "agx.something_else", 1)]), AbiError::UnknownKey); +} + #[test] fn visible_function_references_and_signatures() { let abi = module_with(|b| { diff --git a/crates/metal2vulkan-validation/fixtures/public/kernel_converter_module_flags.ll b/crates/metal2vulkan-validation/fixtures/public/kernel_converter_module_flags.ll new file mode 100644 index 000000000..bc12f69ee --- /dev/null +++ b/crates/metal2vulkan-validation/fixtures/public/kernel_converter_module_flags.ll @@ -0,0 +1,23 @@ +; Owned synthetic fixture: a compute function whose module carries the module flags +; agx.allow_mismatched_component_counts and irc.version_*, in the form seen in modules Apple's +; Metal shader converter wrote (irc.version 3.0.0). Written by hand; not derived from a +; third-party metallib. +source_filename = "kernel_converter_module_flags.ll" +target triple = "air64-apple-macosx14.0.0" + +define void @converter_flags_store(ptr addrspace(1) %out) { + store i32 42, ptr addrspace(1) %out, align 4 + ret void +} + +!llvm.module.flags = !{!10, !11, !12, !13} +!10 = !{i32 7, !"agx.allow_mismatched_component_counts", i32 1} +!11 = !{i32 1, !"irc.version_major", i32 3} +!12 = !{i32 1, !"irc.version_minor", i32 0} +!13 = !{i32 1, !"irc.version_patch", i32 0} + +!air.kernel = !{!0} +!0 = !{ptr @converter_flags_store, !1, !2} +!1 = !{} +!2 = !{!3} +!3 = !{i32 0, !"air.buffer", !"air.buffer_size", i32 4, !"air.location_index", i32 0, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.arg_type_name", !"int", !"air.arg_name", !"out"} diff --git a/crates/metal2vulkan/src/cache/mod.rs b/crates/metal2vulkan/src/cache/mod.rs index 98650fc7c..9c6cf387a 100644 --- a/crates/metal2vulkan/src/cache/mod.rs +++ b/crates/metal2vulkan/src/cache/mod.rs @@ -60,7 +60,7 @@ mod tests; /// unchanged version, and its bless records the version. Independent of the crate's version and of /// [`ARTIFACT_FORMAT_VERSION`]. The commit that bumps it says what changed; this comment keeps no /// history. -pub const SEMANTICS_VERSION: u32 = 2386; +pub const SEMANTICS_VERSION: u32 = 2387; /// The byte-layout version of encoded artifacts (`CompiledFunction::to_bytes` and the /// [`PipelineArtifact`] encodings). Every encoded artifact carries it, and a decode under another diff --git a/crates/metal2vulkan/src/tests/engine_converter_module_flags.rs b/crates/metal2vulkan/src/tests/engine_converter_module_flags.rs new file mode 100644 index 000000000..353f47b48 --- /dev/null +++ b/crates/metal2vulkan/src/tests/engine_converter_module_flags.rs @@ -0,0 +1,27 @@ +//! A module with the `agx.*` and `irc.*` module flags decodes and compiles. +//! +//! The owned fixture `kernel_converter_module_flags` carries `agx.allow_mismatched_component_counts` +//! and `irc.version_major`, `_minor` and `_patch` in `llvm.module.flags`, the flags seen in modules +//! Apple's Metal shader converter wrote. `llvm-as` assembles the fixture; a missing tool skips +//! visibly. + +use crate::pipeline::testing::{ + compile, fixtures_dir, link_and_validate, llvm_assemble, profile, AssembleError, +}; + +/// The function compiles, links and validates under the converter's module flags. +#[test] +fn a_module_with_the_converter_s_flags_compiles() { + let path = fixtures_dir().join("kernel_converter_module_flags.ll"); + let bc = match llvm_assemble(&path) { + Ok(b) => b, + Err(AssembleError::Missing(why)) => { + eprintln!("SKIPPED: {why}"); + return; + } + Err(AssembleError::Rejected(why)) => panic!("llvm-as rejected the fixture: {why}"), + }; + let p = profile("nvidia-rtx50-linux").expect("profile"); + let a = compile(&bc, "converter_flags_store", &p).unwrap_or_else(|e| panic!("{e}")); + link_and_validate(&a, &p).unwrap_or_else(|e| panic!("L3: {e}")); +} diff --git a/crates/metal2vulkan/src/tests/mod.rs b/crates/metal2vulkan/src/tests/mod.rs index df958020b..4ab04c95e 100644 --- a/crates/metal2vulkan/src/tests/mod.rs +++ b/crates/metal2vulkan/src/tests/mod.rs @@ -35,6 +35,7 @@ mod engine_buffer_table; mod engine_chosen_pointers; mod engine_compute_limits; mod engine_convert_rounding; +mod engine_converter_module_flags; mod engine_copy; mod engine_coverage_loop; mod engine_dead_fetch; diff --git a/crates/metal2vulkan/tests/public_fixture_expectations_engine.tsv b/crates/metal2vulkan/tests/public_fixture_expectations_engine.tsv index 1f36583b2..388c30892 100644 --- a/crates/metal2vulkan/tests/public_fixture_expectations_engine.tsv +++ b/crates/metal2vulkan/tests/public_fixture_expectations_engine.tsv @@ -30,6 +30,7 @@ kernel_cloned_continuation_uniform_read.ll cloned_continuation_uniform_read ok o kernel_compare_two_element_pointers.ll compare_two_element_pointers ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok kernel_constant_cursor_across_a_call.ll kernel_constant_cursor_across_a_call ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok kernel_convert_fma_fract.ll kernel_convert_fma_fract ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok +kernel_converter_module_flags.ll converter_flags_store ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok kernel_copy_a_float_run.ll copy_a_float_run ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok kernel_copy_word.ll copy_word ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok kernel_copy_word_declared_read.ll copy_word_declared_read ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok