Files
Anees Iqbal a7dd2e846e Translator: the binder's plan goes, with everything only it reaches, and the TexelRows and YCbCrInPlace forms
The runtime never made a plan (it binds each draw and dispatch by the pipeline's interface), and
since the census digests each pipeline's content alone (two commits back) nothing in the tree calls
one. What only the plan reached goes with it:

- bind: BindState, BindShape and their shapes, BindingPlan and its instantiation
  (DispatchCommands, DrawCommands, push, descriptor and root-table templates and writes),
  plan.rs and plan/ (compute, resources, slots), instantiate.rs, resident.rs, the neutral
  resources' plan side, the heap's member planning and table bytes, the mesh arguments pass
  (MeshArguments, MeshArgumentsForm, fold_object_grid, metal_object_grid), ExecutionStatus and
  StatusBits as public items, TextureShape::Buffer, IndirectRef with the four indirect call
  variants (Dispatch::ThreadgroupsIndirect, DrawCall::Indirect, PatchesIndirect,
  MeshThreadgroupsIndirect) and IndexBufferRef::fetch_end. Dispatch, DrawCall and IndexBufferRef
  stay: validation states its cases' calls with them.
- error::BindError leaves the public surface; a crate-private BindError of three variants stays
  as the error of the heap's view derivation, which lower::texture::slot's storage-read check
  reads (bind::heap::tier_read_gaps). That check keeps only the storage view's format: the
  sampled-view branch and ImageViewDesc were reached by the plan alone.
- vk::DrawCommand, the plan's draw output.
- pipeline: ComputePipeline::plan, RenderPipeline::plan; cache: VariantKey::covers and
  large_buffer_form.
- The TexelRows form (bind::texel_rows, LoweringInputs::texel_rows, RuntimeFact and PushField
  TexelRows) and the YCbCrInPlace form (bind::ycbcr_in_place, lower/sampler/in_place.rs,
  LoweringInputs::ycbcr_in_place and ycbcr_planes_sampled, RuntimeFact and PushField
  YCbCrInPlace, PrecisionNote FloatSampleFilter and HalfSampleFilter, Routine::Quad). Only the
  binder set those inputs.
- The tests of the deleted items, and the plan halves of mixed tests. Of 6810 tests (`#[test]`
  functions in the workspace) 309 leave and 11 come in their place: the lowering halves of mixed
  tests, kept without the plan (the heap layout, the stage-in stride and region facts, the
  emulated-simdgroup and bicubic facts, the iris-xe storage classes), and the kept tests of the two
  renamed files. 50 others are edited to drop a plan half or a deleted item (the api_surface round
  trips; the simd_emulated, extended-range, recursion, dynamic-root, handles, heap-storage-read
  and mesh lowering cases). 6512 remain. engine_graphics_binder.rs and engine_heap_binder.rs keep
  one and two tests and are renamed engine_unattached_colors.rs and engine_heap_groups.rs.
- The runtime guard test's list of files the runtime inputs reach without being inputs drops the
  seven no input reaches since the cut: cache/digest.rs, lower/simd/divergent.rs,
  lower/simd/workgroup.rs, pipeline/artifact.rs, pipeline/layout.rs, pipeline/stage_io.rs and
  pipeline/vertex.rs; three of its reasons that named the plan or the binder say what reaches the file now.

559 public items leave (api_signatures.tsv: 55 structs, 26 enums, 140 variants, 245 fields,
52 methods, 24 associated constants, 7 constants, 5 functions, 5 impls). Consumer search, the
consumer's development at 92ffbed817, `git grep -w NAME` of each of the 112 removed names: 0 hits
for 105; the others are the consumer's own items: TessellationFactors 4 (its
Bound::TessellationFactors), YCbCrMatrix 3 (its own string and doc), texel_rows 2 (its own fn),
ThreadgroupsIndirect 6 (its own Dispatch enums and rt::Dispatch::Indirect), DrawIndexed 1 (a doc
naming MTLIndirectCommandType values), covers 109 and plan 105 (generic words; the `.covers(`
calls are its Extent's, the `.plan(` calls its video encoder's rate.plan). Its one import of
metal2vulkan::bind is the argument-buffer layout types, which stay.

The forms' audit. A texture over a buffer: the runtime's
tests::over::every_device_holds_a_texture_over_a_buffer_in_place. A two-plane YCbCr sample: no
runtime test; the runtime makes no texture of a two-plane YCbCr format (Gap::TextureFormat, the
runtime README's "A format is made where the translator's format table maps it" row), the
conformance cases ycbcr_gathers and ycbcr_textures record Metal's answers, and
known-gaps-rt.tsv lists the runtime's refusals (ycbcr-gathers-*, ycbcr-texel-reads-420v).

Versions:
- RUNTIME_VERSION 2550: it covers plan instantiation and the binder's run-time data. Nothing calls
  those since step 2: the runtime never used the binder, and the own-context executor is gone. A
  run of an unchanged artifact does nothing different, so the guard hash (runtime-sources.sha256)
  moves alone, and the RUNTIME_VERSION doc is rewritten to name what it still covers.
- SEMANTICS_VERSION 2273: no output changes for any input that still exists. The TexelRows and
  YCbCrInPlace forms, their facts, push fields and the two notes were reachable only through
  LoweringInputs::ycbcr_in_place, ycbcr_planes_sampled and texel_rows. Only the binder set those;
  the runtime sets none. The function key appended each of them only when set, so every surviving
  key hashes the same bytes. Two type declarations stay where they were, so module words do not
  move: bool in the bicubic types, uvec4 in the bicubic routines. Byte-identical modules are
  expected, not verified here; the 252-row hash probe is the check.
- ARTIFACT_FORMAT_VERSION 591: the codec tags enum variants by name. The removed RuntimeFact,
  PushField and PrecisionNote variants leave their tags unused, which cache/mod.rs's rule says
  needs no bump: an old entry holding one fails to decode, which is a miss. No codec_struct field
  is removed. That is why SamplerDesc.ycbcr stays.

The guard hash (crates/metal2vulkan-validation/runtime-sources.sha256) moves for the bind/
sources this deletes and edits and for vk/draw.rs, which leaves its list. No run of an unchanged
artifact changes: what stays in bind/ computes what it did (the heap's tier check takes the same
storage format; its removed sampled branch had no caller).

Docs: docs/API.md (section 5 is the interface; the binding and plan sections go), README.md,
docs/ARCHITECTURE.md, docs/HOWTO.md, docs/VALIDATION.md, the runtime README's DrawInput and
identity-domain rows, docs/REFUSALS.md (regenerated; the binder's section goes),
api_reference.md, api_signatures.tsv (blessed: removals only), tests/api_surface.rs, the
signatures floor (5000), the bind modules' own docs, and the public field docs of PushField,
RuntimeFact and SpecKey that named BindState, BindShape or BindingPlan. AGENTS.md: the project
table names pipelines and their interfaces, not binding plans; its "core lives in" line stays
true (bind/ still holds what the lowering reads).

Verified on 31692939d4, this commit's tree (steelbrain/cutbinder-probe-6): job m3-max-68107,
fmt, signatures, refusal surface, clippy -D warnings (workspace plain, --cfg m2v_gpu,
--cfg m2v_cpu_icd; the translator with no feature and with each alone; the AIR crate alone),
workspace Rustdoc -D warnings, each crate's private intra-doc links at 0, api-docs; job
m3-max-68109, the product, validation and conformance suites, doctests, and on lavapipe the
validation and runtime suites under --cfg m2v_cpu_icd, the conformance replay and the public
cases' qualification.
2026-11-22 10:15:34 -08:00

305 lines
14 KiB
Rust

//! A tour of the API: the flow `docs/API.md` walks, on owned public fixtures and committed device
//! profiles, with no GPU. It needs no feature.
//!
//! ```sh
//! PATH=/opt/homebrew/opt/llvm/bin:$PATH \
//! cargo run --example api_preview
//! ```
//!
//! The library reads AIR bitcode (a metallib, a bitcode wrapper, or a raw module). The owned
//! fixtures are committed as LLVM text, so the example assembles them with `llvm-as` (LLVM 21 or
//! newer) from `PATH` first, as a consumer holding `.ll` would.
//!
//! It compiles a kernel for the committed `moltenvk-m3` profile under the runtime's policy (and the
//! same kernel as a bare module's entry, linked alone), prints its pipeline interface and layout,
//! round-trips the pipeline through its cache encoding, answers a pipeline-input request, shows a
//! typed refusal on `runtime-floor`, compiles a kernel with `[[stage_in]]`, and compiles a render
//! pipeline and a tessellation pipeline. `tests/api_preview_example.rs` runs it in the gate and
//! checks that every code block of `docs/API.md` is quoted from it.
use std::path::{Path, PathBuf};
use std::process::Command;
use metal2vulkan::cache::PipelineArtifact;
use metal2vulkan::device::{Device, DeviceDescription, DevicePolicy};
use metal2vulkan::error::{Error, Needs, RuntimeInput};
use metal2vulkan::library::{
CompiledFunction, FunctionConstantValue, FunctionConstantValues, Library,
};
use metal2vulkan::metal::{AttributeFormat, DataType, PixelFormat, StepFunction, VertexFormat};
use metal2vulkan::pipeline::{
ColorAttachmentDescriptor, ComputePipeline, ComputePipelineDescriptor, EntryDescriptor,
EntryRef, FunctionDescriptor, LoweringInputs, RenderPipelineDescriptor,
StageInputAttributeDescriptor, StageInputDescriptor, StageInputLayoutDescriptor, StageSource,
TessellationDescriptor, VertexAttributeDescriptor, VertexBufferLayoutDescriptor,
VertexDescriptor,
};
use metal2vulkan::{Budget, CancelToken};
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
/// The repository root: the committed profiles and fixtures live under it.
fn repo() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("../..")
}
/// Owned fixture `stem`
/// (`crates/metal2vulkan-validation/fixtures/public/engine/<stem>.ll`) as AIR bitcode.
fn fixture_bitcode(stem: &str) -> Result<Vec<u8>> {
let ll: PathBuf = repo()
.join("crates/metal2vulkan-validation/fixtures/public/engine")
.join(format!("{stem}.ll"));
let out = Command::new("llvm-as")
.arg("-o")
.arg("-")
.arg(&ll)
.output()
.map_err(|e| format!("cannot run llvm-as (put LLVM's bin directory on PATH): {e}"))?;
if !out.status.success() {
let stderr = String::from_utf8_lossy(&out.stderr);
return Err(format!("llvm-as {}: {stderr}", ll.display()).into());
}
Ok(out.stdout)
}
/// Committed profile `stem` (`crates/metal2vulkan-validation/profiles/<stem>.json`).
fn profile_json(stem: &str) -> Result<String> {
Ok(std::fs::read_to_string(repo().join(format!(
"crates/metal2vulkan-validation/profiles/{stem}.json"
)))?)
}
/// The device a runtime compiles for on committed profile `stem`: the default policy, the
/// identity domains.
fn runtime_device(stem: &str) -> Result<Device> {
let json = profile_json(stem)?;
let description = DeviceDescription::from_khronos_json(&json, None)?;
let device = Device::from_queries(description, DevicePolicy::default())?;
Ok(device)
}
/// Lowercase hex of a digest.
fn hex(digest: &[u8]) -> String {
digest.iter().map(|b| format!("{b:02x}")).collect()
}
pub fn main() -> Result<()> {
// 1. A device: what the consumer enabled, and its policy, the runtime's default.
let device = runtime_device("moltenvk-m3")?;
let enables = device.required_enables();
println!(
"device: profile {}, {} features and {} extensions to enable",
hex(&device.profile().fingerprint()),
enables.features.len(),
enables.extensions.len(),
);
println!(" Metal properties: {:?}", device.metal_properties());
// 2. A library from AIR bitcode, and its function table.
let sampling = Library::from_air(&fixture_bitcode("sampling")?)?;
for info in sampling.functions() {
println!("library: {} ({:?})", info.name, info.stage);
}
// 3. A compute pipeline, under a 20-second deadline and a cancellation token.
let cancel = CancelToken::new();
let mut budget = Budget::standard();
budget.cancel = Some(cancel.clone());
let kernel = FunctionDescriptor::new(sampling.function("sample_level")?);
let descriptor = ComputePipelineDescriptor::new(StageSource::Function(kernel));
let pipeline = device.compile_compute_pipeline(&descriptor, &budget)?;
println!(
"compute pipeline {:?}: {} SPIR-V words, entry point {:?}",
pipeline.key,
pipeline.stage.spirv.len(),
pipeline.stage.entry_point,
);
// What the module the driver will compile contains: the verifier's counts of it.
let stats = pipeline.stage.stats;
println!(
"{} blocks, {} instructions",
stats.blocks, stats.instructions
);
assert!(stats.reachable_blocks <= stats.blocks);
// The same kernel as an entry of a bare module, through the device's profile: no library,
// the same compile, and its module linked alone.
let bitcode = fixture_bitcode("sampling")?;
let entry = EntryDescriptor::new(&bitcode, EntryRef::Name("sample_level"));
let profile = device.profile();
let (compiled, report) = profile.compile_entry(&entry, &LoweringInputs::new(), &budget);
let alone = profile.link_standalone(&compiled?)?;
println!(
"entry sample_level: {} SPIR-V words linked alone, {} work units",
alone.words.len(),
report.work_units
);
// 4. What the pipeline binds, how, and what it depends on.
for (slot, interface) in &pipeline.interface.slots {
println!(" slot {slot:?}: {interface:?}");
}
for set in &pipeline.layout.sets {
for binding in &set.bindings {
println!(
" set {} binding {}: {:?} x{} from {:?}",
set.set, binding.binding, binding.ty, binding.count, binding.source
);
}
}
for range in &pipeline.layout.push_constants {
println!(" push constants: {range:?}");
}
for dependency in &pipeline.dependencies {
let (fact, mechanism) = (dependency.fact, dependency.mechanism);
println!(" depends on {fact:?} via {mechanism:?}");
}
println!(" precision notes: {:?}", pipeline.stage.precision_notes);
println!(" Metal info: {:?}", pipeline.metal_info);
// 5. The cache: a pipeline encodes under its key and decodes for the same device.
let bytes = pipeline.to_bytes();
let cached = ComputePipeline::from_bytes_for_key(&bytes, &pipeline.key, &device)?;
assert_eq!(cached, pipeline);
println!(
"cache: {} bytes round-trip under {:?}",
bytes.len(),
pipeline.key
);
// A function compiles on its own under a key known before compiling, and a pipeline links
// the cached function without decoding or lowering it again.
let kernel = FunctionDescriptor::new(sampling.function("sample_level")?);
let inputs = LoweringInputs::new();
let key = device.function_compile_key(&kernel, &inputs);
let compiled = device.compile_function(&kernel, &inputs, &budget)?;
let bytes = compiled.to_bytes();
let function = CompiledFunction::from_bytes_for_key(&bytes, &key, &device)?;
let descriptor = ComputePipelineDescriptor::new(StageSource::Compiled(function));
let linked = device.compile_compute_pipeline(&descriptor, &budget)?;
assert_eq!(linked, pipeline);
println!(
"cache: function {key:?} ({} bytes) links to the same pipeline",
bytes.len()
);
// 6. A function-constant value, supplied by index. A constant left undefined compiles too, as
// Metal's does: `is_function_constant_defined` names it false, and a value of it alone is zero.
let library = Library::from_air(&fixture_bitcode("kernel_vector_function_constant")?)?;
let function = library.function("kernel_vector_function_constant")?;
let mut kernel = FunctionDescriptor::new(function);
let descriptor = ComputePipelineDescriptor::new(StageSource::Function(kernel.clone()));
let undefined = device.compile_compute_pipeline(&descriptor, &budget)?;
let words = [1u32, 2, 3, 4].map(u32::to_le_bytes).concat();
let value = FunctionConstantValue::new(DataType::UInt4, words);
kernel.constants = FunctionConstantValues::defaults().with_index(0, value);
let descriptor = ComputePipelineDescriptor::new(StageSource::Function(kernel));
let specialized = device.compile_compute_pipeline(&descriptor, &budget)?;
assert_ne!(undefined.key, specialized.key);
println!("function constant supplied: {:?}", specialized.key);
// 7. A typed refusal: fragment function `sample_min_lod` clamps its level of detail, which
// needs `shaderResourceMinLod`; the `runtime-floor` profile lacks it.
let floor = runtime_device("runtime-floor")?;
let min_lod = FunctionDescriptor::new(sampling.function("sample_min_lod")?);
match floor.compile_function(&min_lod, &LoweringInputs::new(), &Budget::standard()) {
Err(Error::Unsupported(refusal)) => match (&refusal.feature, &refusal.needs) {
(feature, Needs::AnyOf(alternatives)) => {
println!("refused {feature:?}: needs any of {alternatives:?}");
}
(feature, needs) => println!("refused {feature:?}: {needs:?}"),
},
Err(other) => return Err(other.into()),
Ok(_) => return Err("runtime-floor compiled sample_min_lod".into()),
}
stage_in(&device, &budget)?;
render(&device)?;
tessellation(&device)
}
/// A kernel with `[[stage_in]]` on `device`: the stage-input descriptor it asks for, and the kernel
/// compiled on its own under it.
fn stage_in(device: &Device, budget: &Budget) -> Result<()> {
let library = Library::from_air(&fixture_bitcode("kernel_stage_in")?)?;
let kernel = FunctionDescriptor::new(library.function("stage_in_region")?);
let mut descriptor = ComputePipelineDescriptor::new(StageSource::Function(kernel.clone()));
match device.compile_compute_pipeline(&descriptor, budget) {
Err(Error::NeedsInput(RuntimeInput::StageInputDescriptor { attribute })) => {
let mut stage_input = StageInputDescriptor::new();
let fetch = StageInputAttributeDescriptor::new(AttributeFormat::Float, 0, 1);
stage_input.attributes.insert(attribute, fetch);
let mut layout = StageInputLayoutDescriptor::new(Some(4));
layout.step_function = StepFunction::ThreadPositionInGridX;
stage_input.layouts.insert(1, layout);
descriptor.stage_input = Some(stage_input);
}
other => return Err(format!("expected a stage-input request: {other:?}").into()),
}
let pipeline = device.compile_compute_pipeline(&descriptor, budget)?;
println!("stage-in pipeline {:?}", pipeline.key);
// Compiled on its own, the kernel compiles under the pipeline's stage-input descriptor.
let mut inputs = LoweringInputs::new();
inputs.stage_input = descriptor.stage_input.clone();
let compiled = device.compile_function(&kernel, &inputs, budget)?;
let mut linked = descriptor.clone();
linked.kernel = StageSource::Compiled(compiled);
assert_eq!(device.compile_compute_pipeline(&linked, budget)?, pipeline);
Ok(())
}
/// A render pipeline on `device`: its stages and its vertex input.
fn render(device: &Device) -> Result<()> {
let library = Library::from_air(&fixture_bitcode("stage_io")?)?;
let vertex = FunctionDescriptor::new(library.function("stage_io_vertex")?);
let fragment = FunctionDescriptor::new(library.function("stage_io_fragment")?);
let mut vertex_descriptor = VertexDescriptor::new();
let layout = VertexBufferLayoutDescriptor::new(Some(24));
vertex_descriptor.layouts.insert(0, layout);
for (index, format, offset) in [
(0, VertexFormat::Float3, 0),
(1, VertexFormat::Half2, 12),
(4, VertexFormat::UShort2, 16),
(7, VertexFormat::UChar4, 20),
] {
let attribute = VertexAttributeDescriptor::new(format, offset, 0);
vertex_descriptor.attributes.insert(index, attribute);
}
let mut descriptor = RenderPipelineDescriptor::new(StageSource::Function(vertex));
descriptor.fragment = Some(StageSource::Function(fragment));
descriptor.vertex_descriptor = Some(vertex_descriptor);
let color = ColorAttachmentDescriptor::new(PixelFormat::RGBA8Unorm);
descriptor.color_attachments[0] = Some(color);
let color = ColorAttachmentDescriptor::new(PixelFormat::RGBA32Uint);
descriptor.color_attachments[2] = Some(color);
descriptor.depth_format = Some(PixelFormat::Depth32Float);
let pipeline = device.compile_render_pipeline(&descriptor, &Budget::standard())?;
println!("render pipeline {:?}", pipeline.key);
for stage in &pipeline.stages {
println!(
" stage {:?}: {} SPIR-V words",
stage.stage,
stage.spirv.len()
);
}
println!(" vertex input: {:?}", pipeline.vertex_input);
Ok(())
}
/// A tessellation pipeline on `device`: a post-tessellation vertex function.
fn tessellation(device: &Device) -> Result<()> {
let library = Library::from_air(&fixture_bitcode("tessellation")?)?;
let vertex = FunctionDescriptor::new(library.function("tess_quad_buffer")?);
let mut descriptor = RenderPipelineDescriptor::new(StageSource::Function(vertex));
descriptor.tessellation = Some(TessellationDescriptor::default());
let pipeline = device.compile_render_pipeline(&descriptor, &Budget::standard())?;
let stages: Vec<_> = pipeline.stages.iter().map(|stage| stage.stage).collect();
println!("tessellation pipeline {:?}: {stages:?}", pipeline.key);
println!(" {:?}", pipeline.graphics.tessellation);
Ok(())
}