Translator: an unknown runtime-library routine is refused as its family, and AGENTS.md allows the families
AGENTS.md's name rule now also allows the body-less runtime-library routines Metal's compiler links by symbol: the ___metal_* maths routines and the __tensorops_impl_* family. Their declarations have no body, so the symbol is the only thing that says what the routine is. An unknown member of such a family is a typed refusal. Both families were already gated on that structure. The bitcode reader classifies a routine name only on a declaration, and the matmul2d lowering takes a __tensorops_impl_matmul2d_op_* function only when it has no body and is in air.externally_defined. Two tests now show that a defined function of either name is the module's own: - a_defined_function_named_as_a_runtime_routine_is_the_modules_own (bitcode, through llvm-as); - a_defined_function_named_as_a_tensor_op_is_the_modules_own (canon). Both tests pass at the base as well. An unknown member used to be refused as Linking, the refusal for an undefined function that linking could supply, which these never are. Now the post-check refuses a surviving call to a body-less declaration as its family: - an unmodeled ___metal_* routine as an unknown intrinsic, the same refusal an unknown air.* name gets; - a __tensorops_impl_* function in air.externally_defined, outside the matmul2d ABI, as Tensors, which is how the matmul2d lowering already refuses its own unmodeled functions. The same name outside that section is not a family member, and still needs linking. intrinsics::is_routine_family and tensor_ops::in_family state the two families. docs/REFUSALS.md lists the new Tensors site. Verified on the CPU with no device, in job m3-max-82525 at a commit of the same tree (4974c671a8): - an_unknown_runtime_library_routine_is_refused_as_its_family is red with this change's product files ata0d2d2efdd(___metal_ldexp_float: Linking, expected UnknownIntrinsic) and green with them; - the two look-alike tests and surviving_calls_are_refused_by_family_naming_the_callee pass in both runs; - the refusal surface regenerates to these files exactly, and its --check passes; - the metal2vulkan-air and metal2vulkan suites: 3455 passed, 22 skipped.
This commit is contained in:
1 parent
a0d2d2efdd
commit
4a96499072
7 files changed
+186
-8
No files matched your search
@@ -170,9 +170,13 @@ as a known gap.
|
||||
|
||||
- **Structure over names.** Decide lowering from IR structure (types, address spaces, access
|
||||
chains, AIR's metadata ABI), never from a function, type, variable or shader name. Dispatching on
|
||||
the stable `air.*` and `llvm.*` intrinsic families is allowed; prefer a structural test when one
|
||||
exists. A name-keyed branch that passes one shader while failing identically shaped others is a
|
||||
defect. No behavior keyed to an app, a shader hash or a corpus census.
|
||||
the stable `air.*` and `llvm.*` intrinsic families is allowed; the same holds for the bodiless
|
||||
runtime-library routines Metal's compiler links by symbol (the `___metal_*` maths routines and
|
||||
the `__tensorops_impl_*` family), where the declaration has no body and the symbol is the only
|
||||
thing that says what the routine is; an unknown member of such a family is a typed refusal.
|
||||
Prefer a structural test when one exists. A name-keyed branch that passes one shader while
|
||||
failing identically shaped others is a defect. No behavior keyed to an app, a shader hash or a
|
||||
corpus census.
|
||||
- **Honest FALLBACK.** Unsupported input returns a typed `Err` (the command line prints
|
||||
`FALLBACK`). Never emit wrong-but-valid SPIR-V to silence a gate.
|
||||
- **Scope.** The runtime has a floor: one Vulkan version plus a fixed extension list
|
||||
|
||||
@@ -1711,6 +1711,72 @@ fn a_public_fixture_survives_mutation() {
|
||||
mutate(stream_of(&bytes));
|
||||
}
|
||||
|
||||
/// Metal's runtime routines are taken by their symbol only on a declaration, which has no body to
|
||||
/// say what it is: a module that declares `___metal_frexp_float_pthreadint32` calls the math
|
||||
/// family's `frexp`, and one that defines a function of that name calls its own definition.
|
||||
#[test]
|
||||
fn a_defined_function_named_as_a_runtime_routine_is_the_modules_own() {
|
||||
let Some(tool) = llvm_as() else {
|
||||
eprintln!("SKIP a_defined_function_named_as_a_runtime_routine_is_the_modules_own: llvm-as not found");
|
||||
return;
|
||||
};
|
||||
let scratch = Scratch::new();
|
||||
let kernel = "define void @k(ptr addrspace(1) %out) {\n %e = alloca i32\n %r = call float @___metal_frexp_float_pthreadint32(float 1.0, ptr %e)\n store float %r, ptr addrspace(1) %out\n ret void\n}\n";
|
||||
let decoded = |routine: &str, stem: &str| -> ModuleTables {
|
||||
let ll = scratch.0.join(format!("{stem}.ll"));
|
||||
std::fs::write(&ll, format!("{routine}\n{kernel}")).unwrap();
|
||||
let out = scratch.0.join(format!("{stem}.bc"));
|
||||
let bytes = assemble(&tool, &[ll.as_os_str()], &out)
|
||||
.unwrap_or_else(|| panic!("llvm-as rejected the {stem} module"));
|
||||
decode(stream_of(&bytes)).unwrap_or_else(|e| panic!("{stem}: {e}"))
|
||||
};
|
||||
let callee = |t: &ModuleTables| -> crate::ir::Callee {
|
||||
let p = &t.parts;
|
||||
let k = p
|
||||
.funcs
|
||||
.values()
|
||||
.find(|f| p.symbols.get(f.name) == Some(&b"k"[..]))
|
||||
.expect("the kernel");
|
||||
let body = k.body.as_ref().expect("the kernel's body");
|
||||
body.insts
|
||||
.values()
|
||||
.find_map(|i| match &i.kind {
|
||||
crate::ir::InstKind::Call(c) => Some(c.callee.clone()),
|
||||
_ => None,
|
||||
})
|
||||
.expect("the kernel's call")
|
||||
};
|
||||
let declared = decoded(
|
||||
"declare float @___metal_frexp_float_pthreadint32(float, ptr)",
|
||||
"declared",
|
||||
);
|
||||
let c = callee(&declared);
|
||||
assert!(
|
||||
matches!(
|
||||
&c,
|
||||
crate::ir::Callee::Air {
|
||||
op: crate::intrinsics::AirIntrinsic::Math(m),
|
||||
..
|
||||
} if m.f == crate::intrinsics::MathFn::Frexp
|
||||
),
|
||||
"{c:?}"
|
||||
);
|
||||
let defined = decoded(
|
||||
"define float @___metal_frexp_float_pthreadint32(float %x, ptr %e) {\n store i32 9, ptr %e\n ret float 2.5\n}",
|
||||
"defined",
|
||||
);
|
||||
let c = callee(&defined);
|
||||
let crate::ir::Callee::Direct(f) = c else {
|
||||
panic!("{c:?}");
|
||||
};
|
||||
let f = defined.parts.funcs.get(f).expect("the callee");
|
||||
assert!(f.body.is_some(), "the callee is the module's definition");
|
||||
assert_eq!(
|
||||
defined.parts.symbols.get(f.name),
|
||||
Some(&b"___metal_frexp_float_pthreadint32"[..])
|
||||
);
|
||||
}
|
||||
|
||||
/// Whether `xcrun metal-as` exists. Tests that need it skip visibly when it does not, and fail
|
||||
/// when it exists but rejects a committed fixture.
|
||||
fn have_metal_as() -> bool {
|
||||
|
||||
@@ -1489,6 +1489,10 @@ fn post_check(
|
||||
.and_then(|f| parts.symbols.get(f.name))
|
||||
.unwrap_or(b"?")
|
||||
};
|
||||
let section = |f: FuncId| match parts.funcs.get(f).and_then(|f| f.section) {
|
||||
Some(crate::ir::Section::Other(s)) => parts.symbols.get(s),
|
||||
_ => None,
|
||||
};
|
||||
let diag = |bytes: &[u8]| DiagName::new(String::from_utf8_lossy(bytes));
|
||||
// A call to the declaration `f` this module does not define.
|
||||
let undefined =
|
||||
@@ -1504,9 +1508,22 @@ fn post_check(
|
||||
Some(Stub::Resolved { .. }) => {
|
||||
Err(invariant("a call to a resolved stub survived linking"))
|
||||
}
|
||||
// A dynamic-library import, or a declaration no library can define.
|
||||
// A dynamic-library import, or a declaration no library can define: an
|
||||
// unknown member of a family of Metal's runtime-library routines, which
|
||||
// Metal supplies, is refused as its family, and any other needs linking.
|
||||
None => match stubs.import_refusal(Item::Func(f), symbol(f)) {
|
||||
Some(e) => Err(e),
|
||||
None if intrinsics::is_routine_family(symbol(f)) => {
|
||||
let kind =
|
||||
InputErrorKind::Intrinsic(IntrinsicError::UnknownIntrinsic {
|
||||
name: diag(symbol(f)),
|
||||
});
|
||||
Err(InputError::new(kind, at.clone()).into())
|
||||
}
|
||||
None if crate::tensor_ops::in_family(symbol(f), section(f)) => {
|
||||
Ok(Unsupported::not_implemented(Feature::Tensors)
|
||||
.naming(diag(symbol(f))))
|
||||
}
|
||||
None => Ok(Unsupported::not_implemented(Feature::Linking)
|
||||
.naming(diag(symbol(f)))),
|
||||
},
|
||||
|
||||
@@ -3476,7 +3476,8 @@ fn surviving_calls_are_refused_by_family_naming_the_callee() {
|
||||
assert_eq!(u.location, Some(at.clone()));
|
||||
assert_eq!(u.symbol, Some(DiagName::new("air.some_future_op")));
|
||||
|
||||
// An undefined external function (the shape of the tensor-ops library calls) needs linking.
|
||||
// An undefined external function needs linking, one named as the tensor-ops library's included
|
||||
// where it is not in that library's section, `air.externally_defined`.
|
||||
let u = refusal(specialize_call(|m, fty| {
|
||||
Callee::Direct(m.declare("__tensorops_impl_matmul2d_op_cooperative_tensor_init", fty))
|
||||
}));
|
||||
@@ -3502,6 +3503,73 @@ fn surviving_calls_are_refused_by_family_naming_the_callee() {
|
||||
assert_eq!(u.symbol, None);
|
||||
}
|
||||
|
||||
/// Marks `d` as declared in `air.externally_defined`, the tensor-ops library's section.
|
||||
fn externally_defined(m: &mut ModuleBuilder, d: FuncId) {
|
||||
let s = m.sym("air.externally_defined");
|
||||
m.func_mut(d).unwrap().section = Some(Section::Other(s));
|
||||
}
|
||||
|
||||
/// A body-less declaration of a family of Metal's runtime-library routines that nothing models is
|
||||
/// refused at its call as its family, naming it: a `___metal_*` routine the math family does not
|
||||
/// model is an unknown intrinsic, and a `__tensorops_impl_*` function in `air.externally_defined`
|
||||
/// outside the `matmul2d` ABI is tensors, as an unmodeled function of that ABI is (the `matmul2d`
|
||||
/// lowering's refusal). Outside the section a `__tensorops_impl_*` name is no member, and needs
|
||||
/// linking like any other undefined function.
|
||||
#[test]
|
||||
fn an_unknown_runtime_library_routine_is_refused_as_its_family() {
|
||||
let at = Location::function(DiagName::new("k"), Some(0), Some(0));
|
||||
let declared = |name: &'static str, external: bool| {
|
||||
refusal(specialize_call(move |m, fty| {
|
||||
let d = m.declare(name, fty);
|
||||
if external {
|
||||
externally_defined(m, d);
|
||||
}
|
||||
Callee::Direct(d)
|
||||
}))
|
||||
};
|
||||
for (name, external, feature) in [
|
||||
("___metal_ldexp_float", false, Feature::UnknownIntrinsic),
|
||||
(
|
||||
"___metal_frexp_float_pthreadv2int32",
|
||||
false,
|
||||
Feature::UnknownIntrinsic,
|
||||
),
|
||||
("__tensorops_impl_conv2d_op_run", true, Feature::Tensors),
|
||||
("__tensorops_impl_conv2d_op_run", false, Feature::Linking),
|
||||
] {
|
||||
let u = declared(name, external);
|
||||
assert_eq!(u.feature, feature, "{name} external {external}");
|
||||
assert_eq!(u.location, Some(at.clone()), "{name}");
|
||||
assert_eq!(u.symbol, Some(DiagName::new(name)), "{name}");
|
||||
}
|
||||
let u = declared("__tensorops_impl_matmul2d_op_cooperative_tensor_copy", true);
|
||||
assert_eq!(u.feature, Feature::Tensors);
|
||||
assert_eq!(u.location, Some(at));
|
||||
}
|
||||
|
||||
/// A function of the tensor-ops library is taken by its symbol only where the module declares it
|
||||
/// without a body: one the module defines under the same name, in the same section, is the
|
||||
/// module's own and is inlined like any other, where the body-less declaration is the `matmul2d`
|
||||
/// ABI's (here refused as tensors, called without its operands).
|
||||
#[test]
|
||||
fn a_defined_function_named_as_a_tensor_op_is_the_modules_own() {
|
||||
let name = "__tensorops_impl_matmul2d_op_cooperative_tensor_init";
|
||||
let u = refusal(specialize_call(|m, fty| {
|
||||
let d = m.declare(name, fty);
|
||||
externally_defined(m, d);
|
||||
Callee::Direct(d)
|
||||
}));
|
||||
assert_eq!(u.feature, Feature::Tensors);
|
||||
let defined = specialize_call(|m, fty| {
|
||||
let mut f = m.define(name, fty);
|
||||
f.ret(None);
|
||||
let d = f.finish();
|
||||
externally_defined(m, d);
|
||||
Callee::Direct(d)
|
||||
});
|
||||
assert!(defined.is_ok(), "{:?}", defined.err());
|
||||
}
|
||||
|
||||
/// The decoder keeps a call through a function's own constant address indirect when the call's
|
||||
/// function type is not the function's. Such a call is classified by that function, not as a
|
||||
/// dynamic call: a defined function whose parameters cannot receive the call's arguments is invalid
|
||||
|
||||
@@ -418,13 +418,24 @@ pub enum DeclClass {
|
||||
Mismatch,
|
||||
}
|
||||
|
||||
/// The symbol prefix of Metal's runtime routines: functions Metal's compiler declares without a
|
||||
/// body and Metal supplies, so the symbol is all that says which routine one is.
|
||||
const ROUTINE_PREFIX: &[u8] = b"___metal_";
|
||||
|
||||
/// The Metal runtime routine a declaration named `name` is, when the math family models it:
|
||||
/// `___metal_frexp_*` or `___metal_ilogb_*`.
|
||||
fn routine(name: &[u8]) -> Option<Math> {
|
||||
let rest = name.strip_prefix(b"___metal_")?;
|
||||
let rest = name.strip_prefix(ROUTINE_PREFIX)?;
|
||||
Math::parse_routine(std::str::from_utf8(rest).ok()?)
|
||||
}
|
||||
|
||||
/// Whether a declaration named `name` is of the family of Metal's runtime routines, modeled or
|
||||
/// not. A call that survives to a body-less one the math family does not model is an unknown
|
||||
/// intrinsic: Metal defines it, and nothing here says what it does.
|
||||
pub(crate) fn is_routine_family(name: &[u8]) -> bool {
|
||||
name.starts_with(ROUTINE_PREFIX)
|
||||
}
|
||||
|
||||
/// Whether a declaration named `name` is an intrinsic: its name has the `air.` or the `llvm.`
|
||||
/// prefix of the stable ABI, or it is a Metal runtime routine the math family models (`frexp`,
|
||||
/// `ilogb`). Any other declaration is an ordinary function.
|
||||
|
||||
@@ -41,6 +41,18 @@ pub const SECTION: &[u8] = b"air.externally_defined";
|
||||
/// The symbol prefix of the `matmul2d` ABI.
|
||||
pub const PREFIX: &[u8] = b"__tensorops_impl_matmul2d_op_";
|
||||
|
||||
/// The symbol prefix of every function of the tensor-ops implementation headers, the `matmul2d`
|
||||
/// ABI's among them.
|
||||
const FAMILY: &[u8] = b"__tensorops_impl_";
|
||||
|
||||
/// Whether a declaration named `symbol` in `section` is of the tensor-ops implementation's family:
|
||||
/// named under `__tensorops_impl_` in [`SECTION`]. Metal supplies each body-less one when it creates
|
||||
/// the pipeline, so a call that survives to one [`MatMul2dFn::of_declaration`] does not model is
|
||||
/// refused as tensors.
|
||||
pub(crate) fn in_family(symbol: &[u8], section: Option<&[u8]>) -> bool {
|
||||
section == Some(SECTION) && symbol.starts_with(FAMILY)
|
||||
}
|
||||
|
||||
/// `__tensor_ops_datatype_invalid`: the scale type a `_v2` run passes for a tensor operand without
|
||||
/// scale factors (`tensor_plane_scales`).
|
||||
pub(crate) const NO_SCALE: u64 = 0;
|
||||
|
||||
+2
-2
@@ -131,7 +131,7 @@ Columns:
|
||||
| `TileShading` | | every device (not implemented) | `ours` | 3: `crates/metal2vulkan/src/lower/access/tile.rs (not_implemented)`, `crates/metal2vulkan/src/lower/sync/barrier.rs (tile_shading)` (+1) | `rare`: tile shaders (Apple GPU families) |
|
||||
| `Imageblocks` | An imageblock outside the forms implemented | every device (not implemented) | `ours` | 6: `crates/metal2vulkan/src/lower/access/imageblock.rs (imageblocks)`, `crates/metal2vulkan/src/lower/access/mod.rs (select_param)` (+4) | `rare`: imageblocks (Apple GPU families) |
|
||||
| `RayTracing` | | devices without a capability the site computes (`missing`); every device (no Vulkan mechanism); every device (not implemented) | `ours` | 10: `crates/metal2vulkan/src/lower/raytracing/mod.rs (instance_count)`, `crates/metal2vulkan/src/lower/raytracing/mod.rs (intersect, 5 sites)` (+4) | `rare`: ray tracing (out of scope by ruling) |
|
||||
| `Tensors` | | every device (not implemented) | `ours` | 2: `crates/metal2vulkan-air/src/canon/matmul2d/synth.rs (refuse)`, `crates/metal2vulkan/src/lower/select.rs (route_air)` | `rare`: shader tensors |
|
||||
| `Tensors` | | every device (not implemented) | `ours` | 3: `crates/metal2vulkan-air/src/canon/matmul2d/synth.rs (refuse)`, `crates/metal2vulkan-air/src/canon/mod.rs (post_check)` (+1) | `rare`: shader tensors |
|
||||
| `RasterizationRateMap` | | devices without VkFeature::MultiViewport; devices without VkFeature::ShaderDrawParameters; devices without VkFeature::ShaderOutputViewportIndex; every device (not implemented) | `ours` | 2: `crates/metal2vulkan/src/lower/stage_io/plan.rs (region_viewport, 2 sites)` | `rare`: rasterization rate maps |
|
||||
| `GpuEncodedIndirectCommands` | | devices short of a Vulkan feature the site names; devices without buffer device addresses (`addressed_needs`); every device (no Vulkan mechanism); every device (not implemented) | `ours` | 14: `crates/metal2vulkan/src/lower/access/icb.rs (needs, 2 sites)`, `crates/metal2vulkan/src/lower/access/icb.rs (select, 2 sites)` (+7) | `rare`: indirect command buffers encoded by a kernel |
|
||||
| `VulkanVersion` | The device's Vulkan version is below the 1.2 floor | devices without ApiVersion::V1_2 | `device` | 2: `crates/metal2vulkan/src/ash_interop/query.rs (query)`, `crates/metal2vulkan/src/device/derive.rs (from_queries)` | `none`: a device below Vulkan 1.2 |
|
||||
@@ -465,7 +465,7 @@ Each refusal's sites, with the function that builds it.
|
||||
- `feature::TileShading`: `crates/metal2vulkan/src/lower/access/tile.rs (not_implemented)`; `crates/metal2vulkan/src/lower/sync/barrier.rs (tile_shading)`; `crates/metal2vulkan/src/pipeline/compile/api.rs (tile_pipeline)`
|
||||
- `feature::Imageblocks`: `crates/metal2vulkan/src/lower/access/imageblock.rs (imageblocks)`; `crates/metal2vulkan/src/lower/access/mod.rs (select_param)`; `crates/metal2vulkan/src/lower/fragment/mod.rs (select_param)`; `crates/metal2vulkan/src/lower/stage_io/plan.rs (entry)`; `crates/metal2vulkan/src/lower/texture/mod.rs (select)`; `crates/metal2vulkan/src/lower/texture/slice.rs (imageblocks)`
|
||||
- `feature::RayTracing`: `crates/metal2vulkan/src/lower/raytracing/mod.rs (instance_count)`; `crates/metal2vulkan/src/lower/raytracing/mod.rs (intersect, 5 sites)`; `crates/metal2vulkan/src/lower/raytracing/mod.rs (primitive_data_read)`; `crates/metal2vulkan/src/lower/raytracing/mod.rs (select_param)`; `crates/metal2vulkan/src/lower/raytracing/mod.rs (slot_of)`; `crates/metal2vulkan/src/lower/select.rs (route_air)`
|
||||
- `feature::Tensors`: `crates/metal2vulkan-air/src/canon/matmul2d/synth.rs (refuse)`; `crates/metal2vulkan/src/lower/select.rs (route_air)`
|
||||
- `feature::Tensors`: `crates/metal2vulkan-air/src/canon/matmul2d/synth.rs (refuse)`; `crates/metal2vulkan-air/src/canon/mod.rs (post_check)`; `crates/metal2vulkan/src/lower/select.rs (route_air)`
|
||||
- `feature::RasterizationRateMap`: `crates/metal2vulkan/src/lower/stage_io/plan.rs (region_viewport, 2 sites)`
|
||||
- `feature::GpuEncodedIndirectCommands`: `crates/metal2vulkan/src/lower/access/icb.rs (needs, 2 sites)`; `crates/metal2vulkan/src/lower/access/icb.rs (select, 2 sites)`; `crates/metal2vulkan/src/lower/access/mod.rs (plan_replay, 3 sites)`; `crates/metal2vulkan/src/lower/access/object.rs (select)`; `crates/metal2vulkan/src/lower/select.rs (finish)`; `crates/metal2vulkan/src/lower/stage_io/plan.rs (indirect_range)`; `crates/metal2vulkan/src/lower/stage_io/plan.rs (merged)`; `crates/metal2vulkan/src/lower/stage_io/plan.rs (param)`; `crates/metal2vulkan/src/pipeline/compile.rs (analyze_one, 2 sites)`
|
||||
- `feature::VulkanVersion`: `crates/metal2vulkan/src/ash_interop/query.rs (query)`; `crates/metal2vulkan/src/device/derive.rs (from_queries)`
|
||||
|
||||
Reference in new issue
Block a user