diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c16a0e0e..ed8734e8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- A kernel imageblock aliased onto the implicit one is refused instead of given tile-local scratch. + An explicit imageblock is ordinarily per-tile scratch, undefined on entry, and the interface gives + it a `Private` array -- an honest refinement. `air.alias_implicit_imageblock` on the same node + states the opposite: the storage *is* the implicit imageblock, the render targets the rasterizer + has already written, so the kernel's first read is of framebuffer content and its writes have to + land back there. The marker was unread, so a tile-resolve kernel loaded an uninitialized `Private` + array where the rasterized colors should have been, resolved it, and reported success -- with no + descriptor for the framebuffer anywhere in the module or its reflection for a consumer to notice + was missing. 30 of 14579 local corpus sources carry the marker; 124 non-aliased explicit + imageblocks keep the scratch lowering unchanged. + - A shader that encodes into an indirect command buffer is refused instead of translated into one that does not. The `air.*_command` encoder families -- `set_pipeline_state_compute_command`, `set_kernel_buffer_compute_command`, `draw_primitives_render_command` and their siblings -- were diff --git a/src/meta/mod.rs b/src/meta/mod.rs index c5af0eb23..edfe6ee30 100644 --- a/src/meta/mod.rs +++ b/src/meta/mod.rs @@ -647,6 +647,15 @@ pub struct KernMeta { /// `(parameter index, AIR role)` for every enabled entry parameter whose role has no /// lowering. See [`FragMeta::unmodelled_input_params`]. pub unmodelled_input_params: Vec<(u32, String)>, + /// Parameter indices of explicit imageblocks whose AIR node carries + /// `air.alias_implicit_imageblock`. + /// + /// An explicit imageblock is ordinarily tile-local scratch, and the emitter gives it Private + /// storage. This marker says the opposite: the storage *is* the implicit imageblock -- the + /// render targets the rasterizer already wrote -- so the kernel's first read is of framebuffer + /// content, and its writes have to land back there. Private scratch is neither, so the marker + /// cannot be dropped. + pub aliased_implicit_imageblock_params: Vec, /// Every attribute on the `!air.kernel` root this stage has no model for, as it reads in AIR. /// Mirrors [`FragMeta::unmodelled_stage_attributes`]. pub unmodelled_stage_attributes: Vec, @@ -818,6 +827,7 @@ fn parse_air_kernel_meta_with_nodes( let mut roles = vec![]; let mut unmodelled_input_params: Vec<(u32, String)> = vec![]; + let mut aliased_implicit_imageblock_params: Vec = vec![]; let mut function_constant_buffer_locations = HashMap::new(); let mut buffer_layouts = HashMap::new(); let mut imageblock_layouts = HashMap::new(); @@ -921,6 +931,9 @@ fn parse_air_kernel_meta_with_nodes( if let Some(t) = layout { imageblock_layouts.insert(idx, t); } + if strs.iter().any(|s| s == "alias_implicit_imageblock") { + aliased_implicit_imageblock_params.push(idx); + } KernRole::Other } "sampler" => KernRole::Sampler(resource_location(node, idx)), @@ -967,6 +980,7 @@ fn parse_air_kernel_meta_with_nodes( Some(KernMeta { roles, unmodelled_input_params, + aliased_implicit_imageblock_params, unmodelled_stage_attributes, max_work_group_size, function_constant_buffer_locations, diff --git a/src/native/tests/textures.rs b/src/native/tests/textures.rs index d8139df4b..5df56d34c 100644 --- a/src/native/tests/textures.rs +++ b/src/native/tests/textures.rs @@ -4528,6 +4528,67 @@ declare i32 @air.get_width_texture_2d(ptr addrspace(1), i32) let _ = std::fs::remove_dir_all(tmp); } +/// An explicit imageblock is tile-local scratch, undefined on entry, and Private storage is an +/// honest refinement of that. `air.alias_implicit_imageblock` states the opposite: the storage is +/// the implicit imageblock -- the render targets the rasterizer already wrote -- so the kernel's +/// first read is of framebuffer content and its writes have to land back there. Neither is true of +/// Private scratch. +/// +/// Nothing else in the module distinguishes the two. The marker sits beside the ordinary +/// `explicit` qualifier on the same node, the body is the same `air.imageblock_data` pointer, and +/// the emitted module validates and reports the same descriptors either way -- it just resolves an +/// uninitialized array. So the pair is checked from both ends: the marker must refuse, and the same +/// shader without it must still translate. +#[test] +fn native_kernel_imageblock_aliased_onto_the_implicit_one_is_refused() { + let scratch = r#" +target triple = "spirv-unknown-vulkan1.2" +%"struct.metal::_imageblock_base" = type { ptr addrspace(4) } + +define void @k(%"struct.metal::_imageblock_base" %img_blk, ptr addrspace(1) %dst, <2 x i16> %tid) { +entry: + %ptr = tail call ptr addrspace(4) @air.imageblock_data(<2 x i16> %tid, i32 0, i16 0) + %v = load <4 x half>, ptr addrspace(4) %ptr, align 8 + store <4 x half> %v, ptr addrspace(1) %dst, align 8 + ret void +} + +declare ptr addrspace(4) @air.imageblock_data(<2 x i16>, i32, i16) + +!air.kernel = !{!0} +!0 = !{ptr @k, !1, !2} +!1 = !{} +!2 = !{!3, !5, !6} +!3 = !{i32 0, !"air.imageblock", !"explicit", !"air.imageblock_data_size", i32 8, !"air.struct_type_info", !4, ALIAS!"air.arg_type_align_size", i32 8, !"air.arg_type_name", !"imageblock", !"air.arg_name", !"imgBlk"} +!4 = !{i32 0, i32 8, i32 0, !"half4", !"v"} +!5 = !{i32 1, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.write", !"air.address_space", i32 1, !"air.arg_type_name", !"half4*", !"air.arg_name", !"dst"} +!6 = !{i32 2, !"air.thread_position_in_threadgroup", !"air.arg_type_name", !"ushort2", !"air.arg_name", !"tid"} +"#; + let tmp = std::env::temp_dir().join(format!( + "metal2vulkan_kernel_imageblock_alias_{}", + std::process::id() + )); + let _ = std::fs::create_dir_all(&tmp); + + let plain = scratch.replace("ALIAS", ""); + assert_ne!(plain, scratch, "the template has no ALIAS splice point"); + crate::translate_sanitized_native(&plain, Stage::Kernel, &tmp) + .expect("tile-local scratch still translates"); + + let aliased = scratch.replace("ALIAS", "!\"air.alias_implicit_imageblock\", "); + let error = crate::translate_sanitized_native(&aliased, Stage::Kernel, &tmp) + .expect_err("an imageblock aliased onto the render targets has no Private equivalent"); + assert!( + error.contains("implicit imageblock"), + "the refusal must name what the storage aliases: {error}" + ); + assert!( + error.contains("parameter 0"), + "the refusal must name the parameter carrying the marker: {error}" + ); + let _ = std::fs::remove_dir_all(tmp); +} + #[test] fn native_kernel_imageblock_slice_write_uses_private_scratch_and_storage_image() { let ll = r#" diff --git a/src/passes/stage_input/mod.rs b/src/passes/stage_input/mod.rs index a177c0b0f..380a3a476 100644 --- a/src/passes/stage_input/mod.rs +++ b/src/passes/stage_input/mod.rs @@ -388,6 +388,20 @@ pub(super) fn build_stage_input( )); } + // An explicit imageblock is tile-local scratch, and the interface gives it Private storage, + // undefined on entry. `air.alias_implicit_imageblock` says its storage is the implicit + // imageblock instead -- the render targets the rasterizer has already written -- so the + // kernel's reads are of framebuffer content and its writes have to reach it. Private scratch + // is neither: the module validates and resolves an uninitialized array, and no descriptor even + // appears for a consumer to notice was missing. + if let Some(param) = kern.and_then(|meta| meta.aliased_implicit_imageblock_params.first()) { + return Err(format!( + "entry parameter {param} is an imageblock aliased onto the implicit imageblock, \ + which this translator has no storage for; emitting the module would read tile-local \ + scratch where the render targets should be" + )); + } + // `[[early_fragment_tests]]` runs the depth and stencil tests before the body, so the body // cannot be the source of a value either test compares -- it does not exist yet. Metal rejects // the pair outright. Emitting both would give a module whose `FragDepth` / `FragStencilRefEXT`