diff --git a/crates/metal2vulkan-validation/src/air_writer/tests.rs b/crates/metal2vulkan-validation/src/air_writer/tests.rs index dd28ab703..55218700e 100644 --- a/crates/metal2vulkan-validation/src/air_writer/tests.rs +++ b/crates/metal2vulkan-validation/src/air_writer/tests.rs @@ -570,9 +570,20 @@ struct Tally { } impl Tally { - /// A step on the input at `path` failed with `error`: a failure. + /// A step on the input at `path` failed with `error`: skipped where it is the toolchain's own + /// refusal of the version the input states, above the newest version the toolchain says it + /// takes ([`air_ceiling`], [`std_ceiling`]); else a failure. fn refused(&mut self, path: &Path, error: &str) { - self.failures.push(format!("{}: {error}", path.display())); + let text = std::fs::read_to_string(path).unwrap_or_default(); + let above = (air_version_of(&text).and_then(|input| air_ceiling(error, input))) + .or_else(|| metal_std_of(&text).and_then(|std| std_ceiling(error, std))); + match above { + Some((input, newest)) => self.above.push(format!( + "{}: the input's {input}, the toolchain's newest {newest}", + path.display() + )), + None => self.failures.push(format!("{}: {error}", path.display())), + } } /// Lists each skipped input, and the total of those above the toolchain's newest version; then @@ -596,6 +607,69 @@ impl Tally { } } +/// The dotted numbers of `version` (`2.9.0`, `2.8`), as major, minor and patch, a missing one 0. +fn version_numbers(version: &str) -> Option<[u32; 3]> { + let mut numbers = [0; 3]; + for (at, part) in version.split('.').enumerate() { + *numbers.get_mut(at)? = part.parse().ok()?; + } + Some(numbers) +} + +/// The AIR version typed AIR text states: the operands of the node its `!air.version` names. +fn air_version_of(text: &str) -> Option<[u32; 3]> { + let node = (text.lines()) + .find_map(|l| l.strip_prefix("!air.version = !{!"))? + .strip_suffix('}')?; + let prefix = format!("!{node} = !{{"); + let operands = (text.lines()) + .find_map(|l| l.strip_prefix(prefix.as_str()))? + .strip_suffix('}')?; + let numbers: Vec = (operands.split(',')) + .map(|o| o.trim().strip_prefix("i32 ")?.parse().ok()) + .collect::>()?; + numbers.try_into().ok() +} + +/// The Metal standard an MSL source names: its `// metal-std:` line. +fn metal_std_of(text: &str) -> Option<&str> { + (text.lines()) + .find_map(|l| l.trim().strip_prefix("// metal-std:")) + .map(str::trim) +} + +/// `xcrun metallib`'s refusal of an input of AIR version `input` in `error`: "air version set to +/// X (...), but expecting Y", where X is the input's own version and Y, the version the toolchain +/// says it expects, is below it. The two versions as text; `None` for any other error. +fn air_ceiling(error: &str, input: [u32; 3]) -> Option<(String, String)> { + let (set, rest) = error.split_once("air version set to ")?.1.split_once(' ')?; + let expecting = rest + .split_once("but expecting ")? + .1 + .split_whitespace() + .next()?; + let ok = version_numbers(set)? == input && version_numbers(expecting)? < input; + ok.then(|| (format!("AIR {set}"), format!("AIR {expecting}"))) +} + +/// `xcrun metal`'s refusal of the Metal standard `std` a source names, in `error`: "invalid value +/// 'S' in '-std=S'" with S the source's own, where every `metalX.Y` standard the compiler's notes +/// offer is below it. The source's standard and the newest offered; `None` for any other error. +fn std_ceiling(error: &str, std: &str) -> Option<(String, String)> { + if !error.contains(&format!("invalid value '{std}' in '-std={std}'")) { + return None; + } + let input = version_numbers(std.strip_prefix("metal")?)?; + let newest = (error.lines()) + .filter_map(|l| l.split_once("note: use 'metal")?.1.split_once('\'')) + .filter_map(|(offered, _)| version_numbers(offered)) + .max()?; + (newest < input).then(|| { + let newest = format!("metal{}.{}", newest[0], newest[1]); + (std.to_string(), newest) + }) +} + /// Round trip every input, each outcome in a [`Tally`]: an input the decoder itself refuses is /// listed visibly, not failed. fn round_trip_all(