diff --git a/crates/metal2vulkan-validation/src/air_writer/tests.rs b/crates/metal2vulkan-validation/src/air_writer/tests.rs index 854f07f8f..2870a1379 100644 --- a/crates/metal2vulkan-validation/src/air_writer/tests.rs +++ b/crates/metal2vulkan-validation/src/air_writer/tests.rs @@ -570,18 +570,12 @@ struct Tally { } impl Tally { - /// 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. + /// A step on the input at `path` failed with `error`: skipped where the toolchain refused a + /// version the input states that is newer than it takes ([`above_the_toolchain`]); else a + /// failure. fn refused(&mut self, path: &Path, error: &str) { - 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() - )), + match above_the_toolchain(path, error) { + Some(why) => self.above.push(format!("{}: {why}", path.display())), None => self.failures.push(format!("{}: {error}", path.display())), } } @@ -607,6 +601,37 @@ impl Tally { } } +/// Why the toolchain refused the input at `path` with `error`, where `error` is its own refusal of +/// a version the input states that is newer than the toolchain takes: the input's AIR version +/// ([`air_ceiling`]), its Metal standard ([`std_ceiling`]) or its target architecture +/// ([`arch_ceiling`]). `None` for any other error, which is a failure. +pub(crate) fn above_the_toolchain(path: &Path, error: &str) -> Option { + let text = std::fs::read_to_string(path).unwrap_or_default(); + let ceiling = (air_version_of(&text).and_then(|input| air_ceiling(error, input))) + .or_else(|| metal_std_of(&text).and_then(|std| std_ceiling(error, std))) + .map(|(input, newest)| format!("the input's {input}, the toolchain's newest {newest}")); + ceiling.or_else(|| target_arch_of(&text).and_then(|arch| arch_ceiling(error, arch))) +} + +/// The architecture of the `target triple` typed AIR text states: the triple's first part. +fn target_arch_of(text: &str) -> Option<&str> { + let triple = (text.lines()) + .find_map(|l| l.strip_prefix("target triple = \""))? + .strip_suffix('"')?; + triple.split('-').next() +} + +/// `metal-opt`'s refusal of the input's own AIR architecture `arch` in `error`: "unrecognized +/// architecture 'A' provided", A an AIR architecture of a version (`air64_v29`), which an older +/// toolchain does not know. The reason as text; `None` for any other error. +fn arch_ceiling(error: &str, arch: &str) -> Option { + let versioned = (arch.strip_prefix("air64_v")) + .is_some_and(|v| !v.is_empty() && v.bytes().all(|b| b.is_ascii_digit())); + let refused = error.contains(&format!("unrecognized architecture '{arch}' provided")); + (versioned && refused) + .then(|| format!("the input's architecture {arch}, which the toolchain does not know")) +} + /// 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]; diff --git a/crates/metal2vulkan-validation/src/oracle_fuel/tests.rs b/crates/metal2vulkan-validation/src/oracle_fuel/tests.rs index 2e7c0f269..8d61b9fad 100644 --- a/crates/metal2vulkan-validation/src/oracle_fuel/tests.rs +++ b/crates/metal2vulkan-validation/src/oracle_fuel/tests.rs @@ -10,6 +10,7 @@ //! `metal-opt -passes=verify` and `metallib`, the oracle's next steps: synthetic typed MSL and //! every public fixture whose entry reaches a loop. +use std::collections::BTreeMap; use std::ffi::OsString; use std::path::PathBuf; @@ -26,8 +27,11 @@ use metal2vulkan_air::ir::{ use super::plan::barrier_budget; use super::status::{check_status, initial_status, STATUS_BYTES}; use super::*; -use crate::air_writer::tests::compile_msl; +use crate::air_writer::tests::{above_the_toolchain, compile_msl}; use crate::air_writer::{decode_as_written, metal_as, write}; +use crate::check::interface::AirInterface; +use crate::check::loop_free::DecodedModule; +use crate::executor_contract::buffer_slots::NativeBufferSlots; use crate::gpu::compile::xcrun; use crate::ScratchDir; @@ -1528,12 +1532,25 @@ fn has_loop(m: &Module) -> bool { }) } +/// The status slot the oracle gives `entry` of `bitcode`: the highest buffer slot the entry's +/// interface leaves free, as the oracle allocates it (`NativeBufferSlots::allocate`). +fn status_slot(bitcode: &[u8], entry: &str) -> Result { + let decoded = DecodedModule::from_bitcode(entry, bitcode).map_err(|e| format!("{e:?}"))?; + let abi = decoded.abi().map_err(str::to_string)?; + let interface = AirInterface::new(decoded.module(), abi, entry, &BTreeMap::new())?; + NativeBufferSlots::from_interface(&interface)?.allocate() +} + /// Every public fixture with a loop, as typed AIR (`.air.ll` through `metal-as`, `.metal` -/// through `metal -c`): each entry that reaches a loop is instrumented and round-trips. Typed -/// refusals are listed visibly; any other error fails. +/// through `metal -c`): each entry that reaches a loop is instrumented with its status buffer at +/// the slot the oracle gives it ([`status_slot`]) and round-trips. Typed refusals are listed +/// visibly; a fixture whose own version the toolchain refuses as newer than it takes is skipped, +/// listed and counted, as the air writer's round trips skip it (`above_the_toolchain`); any other +/// error fails. #[test] fn public_fixtures_with_loops_round_trip_instrumented() { - if !have_xcrun("public_fixtures_with_loops_round_trip_instrumented") { + const TEST: &str = "public_fixtures_with_loops_round_trip_instrumented"; + if !have_xcrun(TEST) { return; } let scratch = ScratchDir::new("oracle-fuel-fixtures").unwrap(); @@ -1549,6 +1566,17 @@ fn public_fixtures_with_loops_round_trip_instrumented() { } inputs.sort(); let (mut instrumented, mut refused, mut failures) = (0, Vec::new(), Vec::new()); + let mut above = Vec::new(); + // A step on the fixture's own AIR failed: skipped where the toolchain refused a version the + // fixture states as newer than it takes, else a failure. + let fixture_failed = + |[above, failures]: [&mut Vec; 2], path: &PathBuf, error: String| { + let name = path.display(); + match above_the_toolchain(path, &error) { + Some(why) => above.push(format!("{name}: {why}")), + None => failures.push(format!("{name}: {error}")), + } + }; for path in &inputs { let name = path.to_string_lossy().into_owned(); let bytes = if name.ends_with(".air.ll") { @@ -1558,9 +1586,12 @@ fn public_fixtures_with_loops_round_trip_instrumented() { } else { compile_msl(path, &scratch) }; - let Ok(bytes) = bytes else { - failures.push(format!("{name}: {}", bytes.unwrap_err())); - continue; + let bytes = match bytes { + Ok(bytes) => bytes, + Err(e) => { + fixture_failed([&mut above, &mut failures], path, e); + continue; + } }; let Ok(d) = decode_as_written(&bytes, &mut Meter::new(&Budget::default())) else { continue; // outside the writer's input @@ -1574,14 +1605,22 @@ fn public_fixtures_with_loops_round_trip_instrumented() { let level = match strictest_level(&bytes, &scratch) { Ok(level) => level, Err(e) => { - failures.push(format!("{name}: {e}")); + fixture_failed([&mut above, &mut failures], path, e); continue; } }; for entry in stage_entries(&d.module) { + let slot = match status_slot(&bytes, &entry) { + Ok(slot) => slot, + Err(e) => { + failures.push(format!("{name} @{entry}: {e}")); + continue; + } + }; let d = decode_bytes(&bytes); let pointees = Pointees::from_table(&d.pointees, &d.module); - match instrument(d.module, pointees, d.enum_attrs, &entry, &config()) { + let config = FuelConfig::oracle(slot); + match instrument(d.module, pointees, d.enum_attrs, &entry, &config) { Ok(out) if out.report.loops + out.report.barrier_loops == 0 => {} Ok(out) => match round_trip_at(&out, &scratch, level) { Ok(()) => instrumented += 1, @@ -1593,12 +1632,22 @@ fn public_fixtures_with_loops_round_trip_instrumented() { } } for r in &refused { - eprintln!("public_fixtures_with_loops_round_trip_instrumented: REFUSED {r}"); + eprintln!("{TEST}: REFUSED {r}"); + } + for a in &above { + eprintln!("{TEST}: SKIP (above the toolchain's newest version): {a}"); + } + if !above.is_empty() { + eprintln!( + "{TEST}: SKIP {} of {} inputs above the toolchain's newest version", + above.len(), + inputs.len() + ); } assert!(failures.is_empty(), "{}", failures.join("\n\n")); assert!(instrumented > 0, "no fixture entry reached a loop"); eprintln!( - "public_fixtures_with_loops_round_trip_instrumented: {instrumented} entries instrumented, {} refused", + "{TEST}: {instrumented} entries instrumented, {} refused", refused.len() ); }