Document linked-function names and constants; lane test on the device

docs/API.md section 3 states the rule: a reference resolves by
LinkedFunction::name (the specialized name, else the function's own), a
linked module with constants is specialized with the function's values, a
specialized function reads an unvalued constant as undefined, and an
unspecialized one (newFunctionWithName:) that reaches a constant is refused
with LinkProblem::UnresolvedConstant. Section 10 adds the zero read of a
linked function's unvalued constant to the accepted differences. The runtime
README says how a stage links visible functions and by which name.

The probe's header records its 19 cases as measured on the M3 Max under macOS
27.0.1 (26A434), MTL_DEBUG_LAYER unset, one case a process through maclock:
the four of the brief (specialized 16, plain and other unresolved custom_fn,
both 16) and the constant shapes (newFunctionWithName: reaching the constant,
guarded or not, fails "cannot lower module with unresolved function
constants"; plain_fn links; every specialized form links, unvalued reads
storing nothing).

runtime_lane::linked_functions runs both through the runtime: names answered,
16 at word 0 with every word 3x + 1, plain and another_fn needing custom_fn,
both 16; derived 6x, guarded 3x and x - 1, plain_fn x + 2, base_fn unvalued 1;
by name: base_fn and guarded_fn refused UnresolvedConstant, plain_fn 7,
constantValues 3 16, empty values and a descriptor without values link.
tests::pipeline gains the duplicate-name check by link name: one function
under its own and a specialized name is no repeat, two under one specialized
name are.

Verified: on this M3 Max, MoltenVK, through maclock and memcap (6 GiB),
RUSTFLAGS='--cfg m2v_gpu' cargo nextest run -p metal2vulkan-validation
--all-features -E 'test(/runtime_lane::linked_functions::/)': 2/2 passed.
cargo nextest run -p metal2vulkan-runtime --all-features -E
'test(/tests::pipeline::/)': 5/5 passed.

(Carried to development from steelbrain/reims-graphics-kext 228cf1424c.)
This commit is contained in:
Anees Iqbal committed 2026-11-01 21:26:17 -08:00
1 parent 5a0f529a26
commit 6a41f826c8
6 files changed
+342 -12

No files matched your search

+18
View File
@@ -496,6 +496,20 @@ layout stride of `None` is `MTLBufferLayoutStrideDynamic`: the stride comes from
- **Visible functions and recursion.** Linked visible functions, function tables and
intersection function tables compile; recursion through visible functions runs on a stack of
frames sized by `max_call_stack_depth`. A call that finds the stack full returns zero.
- **Linked functions: names and constants.** A visible-function reference resolves to the linked
function whose `LinkedFunction::name` it names: its specialized name where it was made with one
(`with_specialized_name`, Metal's `MTLFunctionDescriptor.specializedName`), else its library
function's own; its body is still that library function's. A linked function whose module
declares function constants is specialized with its own values before it is linked, its
module's static initializer run over them. A function made specialized
(`LinkedFunction::specialized`: with constant values, or by `newFunctionWithDescriptor:` with or
without them) reads a constant it was given no value for as an entry's undefined constants read
(section 10, "Function constants"). One made unspecialized (`newFunctionWithName:`) that reaches
one of its module's constants refuses the pipeline at a call that survives specialization,
`DescriptorError::LinkedFunction` with `LinkProblem::UnresolvedConstant { index }`, guarded by
`is_function_constant_defined` or not, as Metal fails the creation ("cannot lower module with
unresolved function constants"); one that reaches none links. The specialized name and the
specialization are part of the compile key.
- **Binary functions, required threadgroup sizes, buffer mutability.** These descriptor fields
change no translation and are in no compile key; the runtime checks them as Metal does.
- **Dynamic libraries.** `DynamicLibrary::new(library, &budget)` checks a dynamic metallib as
@@ -3685,6 +3699,10 @@ each item named states the rule in full.
it and an argument gated on it, negated or not, is absent. Metal makes the same pipeline, and its
reads of the constant vary from read to read: on an M3 Max under macOS 27.0.1 it read `u + 3` as
0, `u * 7 + 1` as 1, `u == 0 ? 111 : 222` as 222 and `b ? 5 : 6` as 5.
- **A linked function's constant without a value reads as zero too**, where the function was made
specialized (section 3, "Linked functions: names and constants"). Metal makes that pipeline,
and its read is undefined: a kernel storing `x * scale + 1` of such a function stored nothing on
an M3 Max under macOS 27.0.1.
**Kernels**