diff --git a/crates/metal2vulkan-air/src/canon/reach.rs b/crates/metal2vulkan-air/src/canon/reach.rs index 5ddbb8091..f5c17b66f 100644 --- a/crates/metal2vulkan-air/src/canon/reach.rs +++ b/crates/metal2vulkan-air/src/canon/reach.rs @@ -513,8 +513,19 @@ impl Bounds<'_, '_> { _ => None, }, InstKind::BinOp { op, a, b, .. } => { - let (Some((a0, a1)), Some((b0, b1))) = (self.of(*a, d, m)?, self.of(*b, d, m)?) - else { + let (ra, rb) = (self.of(*a, d, m)?, self.of(*b, d, m)?); + // Bounded by one operand alone: `and` by its bounded operand, and an unsigned + // remainder by its divisor, whatever the other holds. + match (op, ra, rb) { + (BinOp::And, Some((_, x)), None) | (BinOp::And, None, Some((_, x))) => { + return Ok(Some((0, x))); + } + (BinOp::URem, None, Some((b0, b1))) if b0 >= 1 => { + return Ok(Some((0, b1 - 1))); + } + _ => {} + } + let (Some((a0, a1)), Some((b0, b1))) = (ra, rb) else { return Ok(None); }; // The greatest value of the bits of `x`. diff --git a/crates/metal2vulkan/src/tests/engine_private_memory.rs b/crates/metal2vulkan/src/tests/engine_private_memory.rs index 6c56ec8a9..a5dddef8a 100644 --- a/crates/metal2vulkan/src/tests/engine_private_memory.rs +++ b/crates/metal2vulkan/src/tests/engine_private_memory.rs @@ -18,6 +18,9 @@ //! bytes more is refused needing that. //! - An array reached only at constant indices counts nothing: three times the RTX 5080's 32 KiB //! compiles there, and records no note on a device not measured. +//! - An array whose every index the constants bound counts only what they reach: the kernel +//! `reached` declares 256 KiB and reaches 128 bytes of it, which both devices hold with no note; +//! with one index unbounded it keeps the 256 KiB and is refused on both. //! //! `llvm-as` assembles the functions; a missing tool skips visibly. @@ -328,3 +331,85 @@ fn an_array_reached_only_at_constant_indices_counts_nothing() { let indexed = notes("nvidia-rtx5080-laptop", n, "words").expect("assembled"); assert_eq!(indexed.map(|_| ()), needs(u64::from(n) * 4, 32 * 1024)); } + +/// The kernel `reached` keeps `float a[64][64][16]`, 256 KiB a thread, as a kernel written for its +/// largest tile does: for `i` below 2 and `j` below 4 it writes `a[i][j][3] = gid + j`, then +/// stores `a[read][3][3]` to its output word, `read` being the LLVM value `read` names (of `%gid`). +fn reached(read: &str) -> Option> { + let text = format!( + r#"; Owned synthetic kernel: a private array its counters reach in part. +source_filename = "private_reach.metal" + +define void @reached(ptr addrspace(1) %out, i32 %gid) {{ +entry: + %a = alloca [64 x [64 x [16 x float]]], align 4 + br label %outer +outer: + %i = phi i32 [ 0, %entry ], [ %i1, %latch ] + br label %inner +inner: + %j = phi i32 [ 0, %outer ], [ %j1, %inner ] + %s = add i32 %gid, %j + %v = uitofp i32 %s to float + %p = getelementptr inbounds [64 x [64 x [16 x float]]], ptr %a, i32 0, i32 %i, i32 %j, i32 3 + store float %v, ptr %p, align 4 + %j1 = add nuw i32 %j, 1 + %jd = icmp eq i32 %j1, 4 + br i1 %jd, label %latch, label %inner +latch: + %i1 = add nuw i32 %i, 1 + %id = icmp eq i32 %i1, 2 + br i1 %id, label %done, label %outer +done: + %k = {read} + %q = getelementptr inbounds [64 x [64 x [16 x float]]], ptr %a, i32 0, i32 %k, i32 3, i32 3 + %x = load float, ptr %q, align 4 + %g = zext i32 %gid to i64 + %o = getelementptr inbounds float, ptr addrspace(1) %out, i64 %g + store float %x, ptr addrspace(1) %o, align 4 + ret void +}} + +!air.kernel = !{{!0}} +!0 = !{{ptr @reached, !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", !"gid"}} +"# + ); + match metal2vulkan_devtools::llvm_assemble_text(&text) { + Ok(b) => Some(b), + Err(AssembleError::Missing(why)) => { + eprintln!("SKIPPED: {why}"); + None + } + Err(AssembleError::Rejected(why)) => panic!("llvm-as rejected the kernel: {why}"), + } +} + +/// An array whose every index the constants bound keeps only what its indices reach: `float +/// a[64][64][16]` (256 KiB) reached at `a[0..2][0..4][3]` and `a[gid & 1][3][3]` is allocated as +/// `float a[2][4][4]`, 128 bytes a thread, which the RTX 5080's 32 KiB and the floor of a device +/// not measured hold with no note. Read at `a[gid][3][3]`, an index with no bound, it keeps its +/// 256 KiB and is refused on both, needing them. +#[test] +fn an_array_its_bounded_indices_reach_in_part_keeps_only_that_part() { + let assumed = PrecisionNote::FunctionStorageAssumed; + let Some(bounded) = reached("and i32 %gid, 1") else { + return; + }; + let unbounded = reached("add i32 %gid, 0").expect("assembled"); + for (stem, available) in [ + ("nvidia-rtx5080-laptop", 32 * 1024), + ("amd-rdna3-radv", 16 * 1024), + ] { + let p = profile(stem, Default::default()).expect("profile"); + let a = compile(&bounded, "reached", &p).unwrap_or_else(|e| panic!("{stem}: {e}")); + link_and_validate(&a, &p).unwrap_or_else(|e| panic!("{stem}: L3: {e}")); + assert_eq!(a.interface.private_bytes, 2 * 4 * 4 * 4, "{stem}"); + assert!(!a.precision.contains(&assumed), "{stem}: {:?}", a.precision); + let declared = compile(&unbounded, "reached", &p).map(|_| ()); + assert_eq!(declared, needs(64 * 64 * 16 * 4, available), "{stem}"); + } +}