diff --git a/CHANGELOG.md b/CHANGELOG.md index ae8cdb2e6..c5ec7abd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Fixed + +- A descriptor the translator synthesizes only to type an AIR value is retracted whenever nothing + consumes that value. This already covered `air.get_read_sampler()`; it now covers + `air.get_null_texture_*()` too, so a shader that merely asks whether an optional attachment is + bound no longer demands a texture descriptor it never reads. The rule is stated once, over the + set of synthesized placeholders, rather than per variable. + ### Added - Reflection schema v32: `ShaderReflection::bindings` reports each resource exactly once. A fragment diff --git a/src/native/tests/textures.rs b/src/native/tests/textures.rs index 6f48339be..a1c455575 100644 --- a/src/native/tests/textures.rs +++ b/src/native/tests/textures.rs @@ -835,20 +835,22 @@ attributes #3 = { convergent nounwind memory(argmem: write) } #[test] fn native_get_null_texture_models_unmodeled_placeholder() { - // `air.get_null_texture_2d()` yields a synthesized image in the texture descriptor band. Even a - // kernel that only stores the opaque handle must remain valid, and the translator-owned resource - // must not escape into sampler/color-input bindings. + // `air.get_null_texture_2d()` yields a synthesized image in the texture descriptor band. A + // kernel that reads through the placeholder keeps that descriptor, and the translator-owned + // resource must not escape into sampler/color-input bindings. The opposite case -- a + // placeholder nothing consumes, whose descriptor is retracted -- is + // `tests/reflection_covers_declared_bindings.rs`. let ll = r#" define void @k(ptr addrspace(1) %out) { entry: - %slot = alloca ptr addrspace(1), align 8 %tex = call ptr addrspace(1) @air.get_null_texture_2d() - store ptr addrspace(1) %tex, ptr %slot, align 8 - store i32 0, ptr addrspace(1) %out, align 4 + %width = call i32 @air.get_width_texture_2d(ptr addrspace(1) %tex, i32 0) + store i32 %width, ptr addrspace(1) %out, align 4 ret void } declare ptr addrspace(1) @air.get_null_texture_2d() +declare i32 @air.get_width_texture_2d(ptr addrspace(1), i32) !air.kernel = !{!0} !0 = !{ptr @k, !1, !2} @@ -866,6 +868,25 @@ declare ptr addrspace(1) @air.get_null_texture_2d() let asm = disassemble(&spv).expect("disassemble"); assert!(asm.contains("OpFunction"), "{asm}"); assert!(asm.contains("Binding 32"), "{asm}"); + for line in asm.lines() { + let Some(binding) = line + .split_whitespace() + .collect::>() + .split_first() + .and_then(|(head, rest)| (*head == "OpDecorate").then_some(rest)) + .and_then(|rest| match rest { + [_, "Binding", value] => value.parse::().ok(), + _ => None, + }) + else { + continue; + }; + assert!( + !crate::reflect::SAMPLER_BINDING_RANGE.contains(binding) + && !crate::reflect::COLOR_INPUT_BINDING_RANGE.contains(binding), + "the translator-owned placeholder must stay in the texture band; got:\n{asm}" + ); + } let _ = std::fs::remove_dir_all(tmp); } diff --git a/src/passes/mod.rs b/src/passes/mod.rs index d8699043f..bfd22b1c2 100644 --- a/src/passes/mod.rs +++ b/src/passes/mod.rs @@ -369,6 +369,10 @@ struct Ctx { /// lazily-created default sampler variable id, for `air.get_read_sampler()` (a sampler-less /// `texture.read` still passes a sampler operand AIR-side; we synthesize one valid sampler). default_sampler_var: Option, + /// Descriptor variables the translator invented to give an AIR value a legal SPIR-V type, with + /// no Metal argument behind them. Retracted when unconsumed; see + /// [`stage_input::drop_unconsumed_placeholder_descriptor_loads`]. + placeholder_descriptor_vars: HashSet, /// Loaded sampler value ids with an exact static or pipeline-provided state. sampler_states: HashMap, /// Direct loads of runtime sampler bindings that were given pipeline state. @@ -459,6 +463,7 @@ impl Ctx { specialized_runtime_sampler_values: HashSet::new(), ambiguous_sampler_states: HashSet::new(), default_null_image_vars: HashMap::new(), + placeholder_descriptor_vars: HashSet::new(), implicit_imageblock_vars: HashMap::new(), fragment_imageblock_vars: HashMap::new(), uses_fragment_imageblock: false, @@ -2662,9 +2667,10 @@ pub(crate) fn transform_with_options_and_sidecar( // sealing synthesized before global liveness runs. decorate_ptr_access_chain_base_strides(&mut ctx); ctx.module.types_global_values.append(&mut ctx.new_globals); - // `air.get_read_sampler()` needed a real sampler descriptor to type its result; if nothing ever - // consumed that value, retract the synthesis before liveness runs so the descriptor goes too. - stage_input::drop_unconsumed_default_sampler_loads(&mut ctx); + // `air.get_read_sampler()` and `air.get_null_texture_*()` each needed a real descriptor to type + // their result; where nothing ever consumed that value, retract the synthesis before liveness + // runs so the descriptor goes too. + stage_input::drop_unconsumed_placeholder_descriptor_loads(&mut ctx); // The closures above are the last of this boundary's instruction changes, and any of them can // delete a variable's last use. Re-establish global liveness against the finished bodies before // the collection that would otherwise root a stranded variable at its own interface entry. diff --git a/src/passes/stage_input/layout.rs b/src/passes/stage_input/layout.rs index 6fbf34adf..945f4bec1 100644 --- a/src/passes/stage_input/layout.rs +++ b/src/passes/stage_input/layout.rs @@ -30,6 +30,40 @@ impl Ctx { } } + /// Create one module-scope `UniformConstant` descriptor variable pointing at `pointee_ty`, + /// decorate it at `binding` in the descriptor layout's set, and put it on the entry interface. + /// + /// Every descriptor the passes synthesize goes through here, so the four of them cannot drift + /// apart on how a resource variable is created, bound, and reached from the entry point. + /// Choosing the binding stays with each caller, because the ABI band a synthesized descriptor + /// belongs to is what distinguishes them. + pub(in crate::passes) fn descriptor_variable( + &mut self, + pointee_ty: Word, + binding: u32, + ) -> Word { + let set = self.descriptor_layout.set; + let pointer_ty = self.ty_ptr(StorageClass::UniformConstant, pointee_ty); + let var = self.module.fresh_id(); + self.new_globals.push(Instruction::new( + Op::Variable, + Some(pointer_ty), + Some(var), + vec![Operand::StorageClass(StorageClass::UniformConstant)], + )); + decorate_binding(&mut self.module, var, set, binding); + self.interface_buffer_var(var); + var + } + + /// Record a descriptor the translator invented purely to give an AIR value a legal SPIR-V type, + /// with no Metal argument behind it. See [`drop_unconsumed_placeholder_descriptor_loads`]. + fn placeholder_descriptor(&mut self, pointee_ty: Word, binding: u32) -> Word { + let var = self.descriptor_variable(pointee_ty, binding); + self.placeholder_descriptor_vars.insert(var); + var + } + /// Lazily create a default sampler resource variable for `air.get_read_sampler()`. Metal's /// `texture.read(coord)` is sampler-less, but AIR still threads a sampler pointer (from /// `air.get_read_sampler`) into the read intrinsic, where our `lower_read` ignores it. To keep the @@ -44,16 +78,7 @@ impl Ctx { let binding = allocate_static_sampler_binding(&self.module, layout) .ok_or_else(|| "no free sampler binding for synthesized read sampler".to_string())?; let sty = self.ty_sampler(); - let pptr = self.ty_ptr(StorageClass::UniformConstant, sty); - let var = self.module.fresh_id(); - self.new_globals.push(Instruction::new( - Op::Variable, - Some(pptr), - Some(var), - vec![Operand::StorageClass(StorageClass::UniformConstant)], - )); - decorate_binding(&mut self.module, var, layout.set, binding); - self.interface_buffer_var(var); + let var = self.placeholder_descriptor(sty, binding); self.default_sampler_var = Some(var); Ok(var) } @@ -76,16 +101,7 @@ impl Ctx { let binding = allocate_default_texture_binding(&self.module, layout) .ok_or_else(|| "no free texture binding for synthesized null image".to_string())?; let img_ty = self.ty_image(dim, arrayed, ImageComp::Float); - let pptr = self.ty_ptr(StorageClass::UniformConstant, img_ty); - let var = self.module.fresh_id(); - self.new_globals.push(Instruction::new( - Op::Variable, - Some(pptr), - Some(var), - vec![Operand::StorageClass(StorageClass::UniformConstant)], - )); - decorate_binding(&mut self.module, var, layout.set, binding); - self.interface_buffer_var(var); + let var = self.placeholder_descriptor(img_ty, binding); self.default_null_image_vars.insert((dim, arrayed), var); Ok(var) } @@ -123,16 +139,7 @@ impl Ctx { ) })?; let image_ty = self.ty_storage_image(Dim::Dim2D, true, format, comp); - let pointer_ty = self.ty_ptr(StorageClass::UniformConstant, image_ty); - let var = self.module.fresh_id(); - self.new_globals.push(Instruction::new( - Op::Variable, - Some(pointer_ty), - Some(var), - vec![Operand::StorageClass(StorageClass::UniformConstant)], - )); - decorate_binding(&mut self.module, var, layout.set, binding); - self.interface_buffer_var(var); + let var = self.descriptor_variable(image_ty, binding); self.implicit_imageblock_vars .insert((attachment, data_rate), (var, image_ty, format)); Ok((var, image_ty)) @@ -168,25 +175,15 @@ impl Ctx { vec![Operand::Capability(capability)], )); } - let image_ty = - self.ty_storage_image(Dim::Dim2D, false, format.image_format, format.component); - let pointer_ty = self.ty_ptr(StorageClass::UniformConstant, image_ty); - let var = self.module.fresh_id(); - self.new_globals.push(Instruction::new( - Op::Variable, - Some(pointer_ty), - Some(var), - vec![Operand::StorageClass(StorageClass::UniformConstant)], - )); - let layout = self.descriptor_layout; - let binding = layout.fragment_imageblock_binding(master_member) + let binding = self.descriptor_layout.fragment_imageblock_binding(master_member) .ok_or_else(|| { format!( "fragment imageblock master member {master_member} exceeds the descriptor ABI band" ) })?; - decorate_binding(&mut self.module, var, layout.set, binding); - self.interface_buffer_var(var); + let image_ty = + self.ty_storage_image(Dim::Dim2D, false, format.image_format, format.component); + let var = self.descriptor_variable(image_ty, binding); self.fragment_imageblock_vars .insert(master_member, (var, image_ty)); Ok((var, image_ty)) @@ -368,30 +365,39 @@ fn decorate_layout_recursive(ctx: &mut Ctx, ty: Word, defs: &HashMap>(); + assert!( + textures.is_empty(), + "the kernel only asks whether the placeholder is bound and never reads it, so it must not \ + demand a texture descriptor; it declares {textures:?}" + ); + let text = metal2vulkan::disassemble(&spirv).expect("disassemble"); + assert!( + !text.contains("OpTypeImage"), + "no image is bound, so no image type should survive:\n{text}" + ); + assert_eq!(declared.len(), 1, "the one output buffer"); +} + /// A fragment shader that reads its render target back through the implicit imageblock. /// /// `air.load.implicit_imageblock.*` is detected from the module body, not from the stage, and the