A declined emit step must emit nothing

The emitter dispatches most lowerings as a chain of responsibility: a handler takes the caller's
instruction stream and answers `Ok(true)` for "I emitted it", `Ok(false)` for "not mine, try the
next". So `Ok(false)` is a claim about the stream as much as about the handler, and the next handler
— or `drop_unmodeled_memcpy`, which discards the call outright — acts on it.

A handler that walks an aggregate cannot know it will decline until it reaches the member that
defeats it, and by then it has emitted the members before it. Seven in the memcpy family do this.
The worst is `emit_prefix_struct_memcpy`: it copies field 0 with a real `OpCopyMemory`, finds field
1 running past the requested byte count, and reports that it did nothing. A 6-byte `llvm.memcpy`
between two `{ i32, i32 }` allocas reaches it — the destination's first field is written and the
rest is not, in a module that would validate cleanly.

`emit_raw_to_typed_struct_memcpy` already avoided this by building into a scratch vector and
splicing it in only on success, with the reason written next to it: "nothing partial may be
committed to the real stream". That pattern is now `staged_emit`, named and applied, so it is used
rather than remembered. Seven handlers run through it; ids a declined attempt minted stay minted,
which costs nothing and keeps every id unique across attempts.

Measured over 2880 corpus sources: **3 change, and every difference is dead code the leak left
behind** — 12 `OpBitcast`, 17 `OpCompositeExtract` and 1 `OpLoad` removed from each, with the
`OpStore`/`OpCopyMemory` counts and every other opcode identical, and all three still spirv-val
clean. So on this corpus the leak was dead weight rather than a partial write; the partial write is
reachable, not observed.

I could not author a shippable fixture that shows the leak inside a *translating* module: the
shapes I built either translate without reaching the declining handler, or fall back because the
memcpy survives as an `OpFunctionCall` (`bugs/memcpy/` holds them). The regression cover is
therefore `staged_emit`'s own tests plus the corpus measurement, not a fixture.

`cargo fmt --all --check`, clippy `-D warnings`, `RUSTDOCFLAGS='-D warnings' cargo doc`,
`cargo test -p metal2vulkan` debug and release, `cargo test -p metal2vulkan-validation`.
This commit is contained in:
Anees Iqbal committed 2026-08-31 21:09:28 +03:00
1 parent 8506f33a6d
commit a327e4e5ae
3 files changed
+648 -542

No files matched your search

File diff suppressed because it is too large. Load diff
+2
View File
@@ -35,12 +35,14 @@ mod memory;
mod ops;
mod pointer_network;
mod pointers;
mod staged;
mod types;
#[cfg(test)]
mod layout_tests;
use helpers::*;
use staged::staged_emit;
pub(super) struct Emitter {
module: Module,
+83
View File
@@ -0,0 +1,83 @@
//! The emitter's decline discipline.
use crate::spirv_module::Instruction;
/// Run an emit step that is allowed to decline, guaranteeing that a decline emits nothing.
///
/// The emitter dispatches most lowerings as a chain of responsibility: a handler takes the caller's
/// instruction stream, returns `Ok(true)` for "I emitted it" and `Ok(false)` for "not mine, try the
/// next one". `Ok(false)` is therefore a claim about the stream as much as about the handler, and
/// the next handler in the chain — or [`drop_unmodeled_memcpy`](super::Emitter::drop_unmodeled_memcpy),
/// which discards the call entirely — acts on it.
///
/// A handler that walks an aggregate cannot know it will decline until it reaches the member that
/// defeats it, by which point it has emitted the members before it. `emit_prefix_struct_memcpy` did
/// exactly that: it copies field 0, finds field 1 running past the requested byte count, and reports
/// that it did nothing — leaving the destination's first field written and the rest not, in a module
/// that then validates cleanly. `emit_raw_to_typed_struct_memcpy` already avoided this by building
/// into a scratch vector and splicing it in only on success ("nothing partial may be committed to the
/// real stream"); this is that pattern, named, so it is applied rather than remembered.
///
/// Ids minted by a declined attempt stay minted. They are never referenced, and skipping a few keeps
/// every id unique across attempts.
pub(in crate::native::emitter) fn staged_emit(
instructions: &mut Vec<Instruction>,
emit: impl FnOnce(&mut Vec<Instruction>) -> Result<bool, String>,
) -> Result<bool, String> {
let mut staged = Vec::new();
if emit(&mut staged)? {
instructions.append(&mut staged);
Ok(true)
} else {
Ok(false)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::spirv_module::Operand;
use spirv::Op;
fn nop() -> Instruction {
Instruction::new(Op::Nop, None, None, vec![Operand::LiteralBit32(0)])
}
/// The whole point: a handler that emitted part of an aggregate before finding the member that
/// defeats it must not leave that part in the caller's stream when it reports `Ok(false)`.
#[test]
fn a_declined_step_leaves_the_stream_it_was_given() {
let mut instructions = vec![nop()];
let declined = staged_emit(&mut instructions, |staged| {
staged.push(nop());
staged.push(nop());
Ok(false)
});
assert_eq!(declined, Ok(false));
assert_eq!(instructions.len(), 1, "a decline must emit nothing");
}
#[test]
fn a_handled_step_appends_in_order() {
let mut instructions = vec![nop()];
let handled = staged_emit(&mut instructions, |staged| {
staged.push(nop());
staged.push(nop());
Ok(true)
});
assert_eq!(handled, Ok(true));
assert_eq!(instructions.len(), 3);
}
/// An `Err` aborts the whole translation attempt, so the stream is never read again; staging it
/// either way would only cost a copy on a path that is already dead.
#[test]
fn an_erroring_step_reports_the_error() {
let mut instructions = Vec::new();
let failed = staged_emit(&mut instructions, |staged| {
staged.push(nop());
Err("no".to_string())
});
assert_eq!(failed, Err("no".to_string()));
}
}