Translator: accept the module flags Apple's Metal shader converter writes

Shaders made by Apple's Metal shader converter could not be translated. The converter writes four
module flags the translator did not know, and an unknown flag is refused with Abi(UnknownKey).

The four flags:
- agx.allow_mismatched_component_counts (an i32, 0 or 1)
- irc.version_major, irc.version_minor, irc.version_patch (integers; 3.0.0 in the shaders seen)

What changes: the decoder accepts these flags and ignores them, as it treats wchar_size,
frame-pointer and the debug-info flags. It still checks them:
- each flag appears at most once;
- the agx flag is 0 or 1;
- the version parts are integers.
Anything else is refused as before. No public API changes.

Evidence:
- A new fixture with these flags compiles to the same SPIR-V as the same shader without them, on
  all 15 profiles: the flags change nothing in the translation.
- converter_module_flags_are_checked_and_dropped and the kernel_converter_module_flags fixture fail
  without this change.
- Of 12 converter-made shaders, 8 carry these flags. They now get past the flags, but each stops at
  a different, unrelated refusal; none compiles with this change alone.

Version: SEMANTICS_VERSION 2386 -> 2387, because inputs refused before can now compile.

Golden table: left for the bless on development; golden_hashes_match_the_table lists the fixture's
15 new cells. The fixture's expectations row is included.

Not run: device tests, the Windows clippy legs, the CI matrix.
This commit is contained in:
Jacob McSwain committed 2026-12-04 19:36:27 -06:00
1 parent a71ba816e3
commit 74e4ad9d3a
7 files changed
+116 -4

No files matched your search

+23 -3
View File
@@ -388,9 +388,12 @@ fn plain_signature(n: &mut Ops<'_>, site: &Site) -> Result<LinkageSignature, Err
/// `!llvm.module.flags`: `!{i32 <behavior>, !"<key>", <value>}`. The `air.max_*` limits are
/// decoded; `wchar_size`, `frame-pointer`, `SDK Version`, and debug info's `Dwarf Version` and
/// `Debug Info Version` are LLVM's and carry nothing the translation reads; `air.workaround.sampler_state_variable_length_encoding` (an `i1`) names the
/// form of the module's constexpr sampler words, which each `air.sampler_state` global's own type
/// states; any other flag is unknown.
/// `Debug Info Version` are LLVM's and carry nothing the translation reads, and neither do
/// `agx.allow_mismatched_component_counts` (an integer, 0 or 1) and `irc.version_major`,
/// `irc.version_minor` and `irc.version_patch` (integers), each stated once: their shapes are
/// checked and nothing is kept. `air.workaround.sampler_state_variable_length_encoding` (an `i1`)
/// names the form of the module's constexpr sampler words, which each `air.sampler_state` global's
/// own type states. Any other flag is unknown.
fn module_flags(
m: &Module,
ops: &[MdId],
@@ -399,11 +402,28 @@ fn module_flags(
) -> Result<ModuleLimits, Error> {
let key = Some("llvm.module.flags");
let mut l = ModuleLimits::default();
let (mut mismatch, mut major, mut minor, mut patch) = (false, false, false, false);
for &id in ops {
meter.tick(1)?;
let mut n = Ops::node(m, id, site.clone())?;
n.u32(key)?;
let flag = n.string(key)?;
// The converter's flags: an integer each, stated once; the agx flag 0 or 1.
let shape = match flag {
b"agx.allow_mismatched_component_counts" => Some((&mut mismatch, 1)),
b"irc.version_major" => Some((&mut major, u32::MAX)),
b"irc.version_minor" => Some((&mut minor, u32::MAX)),
b"irc.version_patch" => Some((&mut patch, u32::MAX)),
_ => None,
};
if let Some((seen, limit)) = shape {
let repeated = std::mem::replace(seen, true);
if n.u32(key)? > limit || repeated {
return Err(site.err(AbiError::BadValue, key));
}
n.finish()?;
continue;
}
let slot = match flag {
b"air.max_device_buffers" => &mut l.max_device_buffers,
b"air.max_constant_buffers" => &mut l.max_constant_buffers,
+40
View File
@@ -3436,6 +3436,46 @@ fn static_samplers_decode_from_their_globals() {
);
}
/// The converter's module flags (`agx.allow_mismatched_component_counts`, `irc.version_*`) are
/// checked for their shapes and kept nowhere: the module decodes as without them.
#[test]
fn converter_module_flags_are_checked_and_dropped() {
let flags = |nodes: &[(u64, &'static str, u64)]| {
let nodes = nodes.to_vec();
module_with(move |b| {
let ids: Vec<MdId> = nodes
.iter()
.map(|&(behavior, key, value)| node(b, &[I(behavior), S(key), I(value)]))
.collect();
b.named_md("llvm.module.flags", &ids);
})
};
let plain = flags(&[(7, "air.max_textures", 128)]).unwrap();
let with = flags(&[
(7, "air.max_textures", 128),
(7, "agx.allow_mismatched_component_counts", 1),
(1, "irc.version_major", 3),
(1, "irc.version_minor", 1),
(1, "irc.version_patch", 4),
])
.unwrap();
assert_eq!(with.limits, plain.limits);
assert_eq!(with.compile, plain.compile);
flags(&[(7, "agx.allow_mismatched_component_counts", 0)]).unwrap();
flags(&[(1, "irc.version_major", 3)]).unwrap();
for refused in [
&[(1, "irc.version_major", 3), (1, "irc.version_major", 3)][..],
&[
(7, "agx.allow_mismatched_component_counts", 1),
(7, "agx.allow_mismatched_component_counts", 1),
][..],
&[(7, "agx.allow_mismatched_component_counts", 2)][..],
] {
expect_abi(flags(refused), AbiError::BadValue);
}
expect_abi(flags(&[(7, "agx.something_else", 1)]), AbiError::UnknownKey);
}
#[test]
fn visible_function_references_and_signatures() {
let abi = module_with(|b| {
@@ -0,0 +1,23 @@
; Owned synthetic fixture: a compute function whose module carries the module flags
; agx.allow_mismatched_component_counts and irc.version_*, in the form seen in modules Apple's
; Metal shader converter wrote (irc.version 3.0.0). Written by hand; not derived from a
; third-party metallib.
source_filename = "kernel_converter_module_flags.ll"
target triple = "air64-apple-macosx14.0.0"
define void @converter_flags_store(ptr addrspace(1) %out) {
store i32 42, ptr addrspace(1) %out, align 4
ret void
}
!llvm.module.flags = !{!10, !11, !12, !13}
!10 = !{i32 7, !"agx.allow_mismatched_component_counts", i32 1}
!11 = !{i32 1, !"irc.version_major", i32 3}
!12 = !{i32 1, !"irc.version_minor", i32 0}
!13 = !{i32 1, !"irc.version_patch", i32 0}
!air.kernel = !{!0}
!0 = !{ptr @converter_flags_store, !1, !2}
!1 = !{}
!2 = !{!3}
!3 = !{i32 0, !"air.buffer", !"air.buffer_size", i32 4, !"air.location_index", i32 0, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.arg_type_name", !"int", !"air.arg_name", !"out"}
+1 -1
View File
@@ -60,7 +60,7 @@ mod tests;
/// unchanged version, and its bless records the version. Independent of the crate's version and of
/// [`ARTIFACT_FORMAT_VERSION`]. The commit that bumps it says what changed; this comment keeps no
/// history.
pub const SEMANTICS_VERSION: u32 = 2386;
pub const SEMANTICS_VERSION: u32 = 2387;
/// The byte-layout version of encoded artifacts (`CompiledFunction::to_bytes` and the
/// [`PipelineArtifact`] encodings). Every encoded artifact carries it, and a decode under another
@@ -0,0 +1,27 @@
//! A module with the `agx.*` and `irc.*` module flags decodes and compiles.
//!
//! The owned fixture `kernel_converter_module_flags` carries `agx.allow_mismatched_component_counts`
//! and `irc.version_major`, `_minor` and `_patch` in `llvm.module.flags`, the flags seen in modules
//! Apple's Metal shader converter wrote. `llvm-as` assembles the fixture; a missing tool skips
//! visibly.
use crate::pipeline::testing::{
compile, fixtures_dir, link_and_validate, llvm_assemble, profile, AssembleError,
};
/// The function compiles, links and validates under the converter's module flags.
#[test]
fn a_module_with_the_converter_s_flags_compiles() {
let path = fixtures_dir().join("kernel_converter_module_flags.ll");
let bc = match llvm_assemble(&path) {
Ok(b) => b,
Err(AssembleError::Missing(why)) => {
eprintln!("SKIPPED: {why}");
return;
}
Err(AssembleError::Rejected(why)) => panic!("llvm-as rejected the fixture: {why}"),
};
let p = profile("nvidia-rtx50-linux").expect("profile");
let a = compile(&bc, "converter_flags_store", &p).unwrap_or_else(|e| panic!("{e}"));
link_and_validate(&a, &p).unwrap_or_else(|e| panic!("L3: {e}"));
}
+1
View File
@@ -35,6 +35,7 @@ mod engine_buffer_table;
mod engine_chosen_pointers;
mod engine_compute_limits;
mod engine_convert_rounding;
mod engine_converter_module_flags;
mod engine_copy;
mod engine_coverage_loop;
mod engine_dead_fetch;
@@ -30,6 +30,7 @@ kernel_cloned_continuation_uniform_read.ll cloned_continuation_uniform_read ok o
kernel_compare_two_element_pointers.ll compare_two_element_pointers ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok
kernel_constant_cursor_across_a_call.ll kernel_constant_cursor_across_a_call ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok
kernel_convert_fma_fract.ll kernel_convert_fma_fract ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok
kernel_converter_module_flags.ll converter_flags_store ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok
kernel_copy_a_float_run.ll copy_a_float_run ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok
kernel_copy_word.ll copy_word ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok
kernel_copy_word_declared_read.ll copy_word_declared_read ok ok ok ok ok ok ok ok ok ok ok ok ok ok ok
Can't render this file because it is too large.