diff --git a/crates/metal2vulkan-validation/src/check/output_words.rs b/crates/metal2vulkan-validation/src/check/output_words.rs index 839bbf5c1..a20b3ecbc 100644 --- a/crates/metal2vulkan-validation/src/check/output_words.rs +++ b/crates/metal2vulkan-validation/src/check/output_words.rs @@ -49,6 +49,24 @@ const LARGEST_VALUE: u64 = 4096; /// The most passes the scan's fixed point takes; an entry that needs more is not scanned. const PASSES: usize = 64; +#[cfg(test)] +thread_local! { + /// The values this thread's scans have answered in [`Scan::base`], [`Scan::address`] and + /// [`Scan::affine`], and the most a test lets them answer. + static VISITS: std::cell::Cell<(u64, u64)> = const { std::cell::Cell::new((0, u64::MAX)) }; +} + +/// Count one value [`Scan::base`], [`Scan::address`] or [`Scan::affine`] answers. A test bounds +/// how many a scan answers, and past its bound the scan panics rather than walk on. +fn visit() { + #[cfg(test)] + VISITS.with(|visits| { + let (count, bound) = visits.get(); + assert!(count < bound, "the scan answered more than {bound} values"); + visits.set((count + 1, bound)); + }); +} + /// What one byte of a value is. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ByteKind { @@ -544,6 +562,7 @@ impl Scan<'_> { /// The object `ptr` points into. fn base(&self, ptr: Value, depth: usize) -> Base { + visit(); if depth > 64 { return Base::Other; } @@ -1177,6 +1196,7 @@ impl Scan<'_> { /// `ptr`'s byte offset from its buffer argument, affine in a grid component. fn address(&self, ptr: Value, depth: usize) -> Option { + visit(); if depth > 64 { return None; } @@ -1224,6 +1244,7 @@ impl Scan<'_> { /// The integer `value`, affine in a grid component. fn affine(&self, value: Value, depth: usize) -> Option { + visit(); if depth > 64 { return None; } diff --git a/crates/metal2vulkan-validation/src/check/output_words/tests.rs b/crates/metal2vulkan-validation/src/check/output_words/tests.rs index 3477a6589..00f90be5a 100644 --- a/crates/metal2vulkan-validation/src/check/output_words/tests.rs +++ b/crates/metal2vulkan-validation/src/check/output_words/tests.rs @@ -198,3 +198,196 @@ fn a_helper_query_and_a_discard_feed_a_render_targets_words() { }; assert_eq!(discarding.words, vec![decided; 4]); } + +/// More values than the scan's analyses answer in any module below, where each answers each of +/// the entry's instructions once (each module has fewer than 100), and far fewer than a walk that +/// answers a shared operand again for each of its uses answers (2^40 and more). +const BOUND: u64 = 1_000; + +/// The stores the scan finds in entry `k` of `ll`, and how many values its analyses answered +/// (past [`BOUND`] the scan panics); `None` (a visible skip) without `llvm-as`. +fn bounded_scan(test: &str, ll: &str) -> Option<(Stores, u64)> { + use crate::check::interface::tests::of_text; + VISITS.with(|visits| visits.set((0, BOUND))); + let interface = match of_text(ll, "k", &BTreeMap::new()) { + Ok(interface) => interface, + Err(why) if why.contains("unavailable") => { + eprintln!("SKIP {test}: {why}"); + return None; + } + Err(why) => panic!("{test}: {why}"), + }; + let visits = VISITS.with(|visits| visits.get().0); + Some((interface.stores.expect("a scanned entry"), visits)) +} + +/// A kernel `k` over `out` (buffer 0, `float`) and the grid position `tid` (`uint`), with `body` +/// after an `entry:` label. +fn kernel(body: &str) -> String { + format!( + r#" +target datalayout = "e-p:64:64:64-i64:64:64-f32:32:32-v64:64:64-v128:128:128" +target triple = "air64-apple-macosx14.0.0" +define void @k(ptr addrspace(1) %out, i32 %tid) {{ +entry: +{body} +}} +!air.kernel = !{{!0}} +!0 = !{{ptr @k, !1, !2}} +!1 = !{{}} +!2 = !{{!3, !4}} +!3 = !{{i32 0, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"float", !"air.arg_name", !"out"}} +!4 = !{{i32 1, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint", !"air.arg_name", !"tid"}} +"# + ) +} + +/// A store the scan cannot place of one word-aligned `float` that `float` arithmetic feeds. +fn float_store() -> StoreFacts { + StoreFacts { + place: None, + bytes: (0..4) + .map(|at| ByteFact { + kind: ByteKind::Float(at), + fed: fed(&[Family::FloatArithmetic]), + }) + .collect(), + word_aligned: true, + } +} + +/// The index of a store is a chain of 40 `add`s, each adding the last to itself: `2^40 * tid` +/// floats from `out`. Each link is answered once, not once for each path to it, and the store is +/// placed at `2^42 * tid` bytes, its word a `float` product. +#[test] +fn an_index_whose_links_share_an_operand_is_answered_once_a_link() { + let test = "an_index_whose_links_share_an_operand_is_answered_once_a_link"; + let mut body = String::from("%x0 = zext i32 %tid to i64\n"); + for link in 1..=40 { + body += &format!("%x{link} = add i64 %x{0}, %x{0}\n", link - 1); + } + body += "%p = getelementptr inbounds float, ptr addrspace(1) %out, i64 %x40 +%f = uitofp i32 %tid to float +%g = fmul float %f, %f +store float %g, ptr addrspace(1) %p, align 4 +ret void"; + let Some((stores, visits)) = bounded_scan(test, &kernel(&body)) else { + return; + }; + assert!(visits <= BOUND, "{visits} visits"); + let mut store = float_store(); + store.place = Some(Place { + axis: 0, + period: 1 << 42, + offset: 0, + }); + assert_eq!( + stores, + Stores { + buffers: BTreeMap::from([( + 0, + BufferWrites { + stores: vec![store], + opaque: false, + } + )]), + ..Stores::default() + } + ); +} + +/// A pointer through `links` `select`s, each choosing the last one on both arms, from `out`. +fn select_chain(links: usize) -> String { + let mut body = String::from( + "%f = uitofp i32 %tid to float +%c = icmp eq i32 %tid, 0 +%p0 = getelementptr inbounds float, ptr addrspace(1) %out, i64 0 +", + ); + for link in 1..=links { + body += &format!( + "%p{link} = select i1 %c, ptr addrspace(1) %p{0}, ptr addrspace(1) %p{0}\n", + link - 1 + ); + } + body += &format!("store float %f, ptr addrspace(1) %p{links}, align 4\nret void"); + kernel(&body) +} + +/// A store through a chain of 40 `select`s of one pointer into `out` writes `out` (the scan +/// cannot place it); through a chain of 70, longer than the 64 links the scan follows, it writes +/// a pointer no buffer argument roots. Each link is answered once, not once for each path to it. +#[test] +fn a_pointer_whose_links_share_an_operand_is_answered_once_a_link() { + let test = "a_pointer_whose_links_share_an_operand_is_answered_once_a_link"; + let Some((stores, visits)) = bounded_scan(test, &select_chain(40)) else { + return; + }; + assert!(visits <= BOUND, "{visits} visits"); + assert_eq!( + stores, + Stores { + buffers: BTreeMap::from([( + 0, + BufferWrites { + stores: vec![float_store()], + opaque: false, + } + )]), + ..Stores::default() + } + ); + let Some((stores, visits)) = bounded_scan(test, &select_chain(70)) else { + return; + }; + assert!(visits <= BOUND, "{visits} visits"); + assert_eq!( + stores, + Stores { + unrooted_device_write: true, + ..Stores::default() + } + ); +} + +/// A loop steps a pointer into `out` by one float or by two, as `tid` decides, loading and +/// storing through it: the `phi` that carries it reaches itself through both steps. A pointer on +/// such a cycle roots in no buffer argument, so the store writes an unrooted pointer, and each +/// value of the cycle is answered once. +#[test] +fn a_pointer_a_loop_carries_on_two_paths_is_answered_once() { + let test = "a_pointer_a_loop_carries_on_two_paths_is_answered_once"; + let body = "%f = uitofp i32 %tid to float +br label %loop +loop: +%p = phi ptr addrspace(1) [ %out, %entry ], [ %q1, %one ], [ %q2, %two ] +%i = phi i32 [ 0, %entry ], [ %next, %one ], [ %next, %two ] +%v = load float, ptr addrspace(1) %p, align 4 +%g = fmul float %v, %f +store float %g, ptr addrspace(1) %p, align 4 +%next = add i32 %i, 1 +%done = icmp eq i32 %next, 4 +br i1 %done, label %exit, label %step +step: +%c = icmp ult i32 %next, %tid +br i1 %c, label %one, label %two +one: +%q1 = getelementptr inbounds float, ptr addrspace(1) %p, i64 1 +br label %loop +two: +%q2 = getelementptr inbounds float, ptr addrspace(1) %p, i64 2 +br label %loop +exit: +ret void"; + let Some((stores, visits)) = bounded_scan(test, &kernel(body)) else { + return; + }; + assert!(visits <= BOUND, "{visits} visits"); + assert_eq!( + stores, + Stores { + unrooted_device_write: true, + ..Stores::default() + } + ); +}