Refuse an imageblock that is really the framebuffer
An explicit `[[imageblock]]` is tile-local scratch, undefined on entry, and the interface gives it a `Private` array. That is an honest refinement of undefined storage. `air.alias_implicit_imageblock`, on the same node beside the ordinary `explicit` qualifier, states the opposite: the storage *is* the implicit imageblock -- the render targets the rasterizer has already written. The kernel's first read is of framebuffer content and its writes have to land back there. The marker was unread. `resolveTileMemoryKernel_ColorBlockSimple`, whose body begins by loading a `<4 x half>` out of the imageblock and ends by writing the resolve to a texture, was emitted reading `%_ptr_Private__arr_v4half_uint_2` -- an uninitialized per-invocation array. It validated, and neither the module nor its reflection carried any descriptor for the framebuffer that a consumer could have noticed was missing. 30 of 14579 local corpus sources carry the marker. Vulkan has no compute stage inside a render pass to alias against, so the answer is a refusal naming the parameter. The 124 non-aliased explicit imageblocks keep the Private-scratch lowering exactly as it was; the authored test drives both arms off one template, since the aliased and unaliased nodes differ only in that one string. Verified: `cargo fmt --all --check`, `cargo clippy --workspace --all-targets -D warnings`, warnings-denied all-features Rustdoc, `cargo test -p metal2vulkan` in debug and release, `cargo test -p metal2vulkan-validation`. Suppressing the decode fails the new test and no other. A/B against the previous commit over 2880 corpus sources: 0 SPIR-V byte changes, and exactly the nine sources carrying the marker move from OK to FALLBACK.
This commit is contained in:
1 parent
66c7e7d42b
commit
9db8b14645
4 files changed
+100
No files matched your search
@@ -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
|
||||
|
||||
@@ -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<u32>,
|
||||
/// 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<String>,
|
||||
@@ -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<u32> = 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,
|
||||
|
||||
@@ -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<ImageBlockData, layout_explicit>", !"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#"
|
||||
|
||||
@@ -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`
|
||||
|
||||
Reference in new issue
Block a user