Translator test: a type only dead values had leaves with its capability (red)

After fd8b132834 made a slot's direct base and address in the entry block,
dead removal leaves the ones no access reads, but not their types: an
unused OpTypeInt 64 and a { uint, uint } struct stay declared, and with
the 64-bit type the module's OpCapability Int64 (3 of 8 sampled modules in
the byte-identity probe). Dead removal drops only vector and function
types.

The test builds that shape: a kernel that widens a loaded word to a 64-bit
integer and pairs the word with itself in a { uint, uint } struct, and
reads neither. After dead removal both types must leave, and the module
must no longer require Int64 or shaderInt64. Red here: no type leaves, so
the report counts 0 types and Int64 stays.
This commit is contained in:
Anees Iqbal committed 2026-12-02 14:24:14 -08:00
1 parent 6ae0f8244b
commit 95199bb968
1 file changed
+89
+89
View File
@@ -1044,6 +1044,7 @@ impl Sim<'_> {
#[cfg(test)]
mod tests {
use super::*;
use crate::device::requirements::{SpirvCapability, VkFeature};
use crate::pipeline::sym::{ResourceKind, ResourceSlot, Stage};
use crate::spirv::builder::{FunctionBuilder, ModuleBuilder};
use crate::spirv::error::BResult;
@@ -1575,6 +1576,94 @@ mod tests {
}
}
/// A kernel that widens word 0 to a 64-bit integer and pairs word 0 with itself in a
/// `{ uint, uint }` struct, and reads neither (what a slot's direct base and address leave
/// when no access reads them), then stores word 0 to word 1. The 64-bit integer type and the
/// struct type are the dead values' alone.
fn wide_base_kernel() -> BResult<TypedModule> {
let mut b = ModuleBuilder::new();
let buf = buffer(&mut b)?;
let void = b.ty(Type::Void)?;
let u32_ = b.ty(Type::Int {
width: 32,
signed: false,
})?;
let u64_ = b.ty(Type::Int {
width: 64,
signed: false,
})?;
let pair = b.ty(Type::Struct {
members: [u32_, u32_].into(),
layout: None,
})?;
let mut fb = b.function(void)?;
let p0 = word(&mut fb, buf, 0)?;
let w0 = fb.load(p0, MemAccess::default())?;
fb.convert(crate::spirv::ir::ConvOp::UConvert, w0, u64_)?;
fb.composite_construct(pair, &[w0, w0])?;
let p1 = word(&mut fb, buf, 1)?;
fb.store(p1, w0, MemAccess::default())?;
fb.ret(None)?;
let id = fb.finish()?;
b.entry(
ExecModel::GLCompute,
id,
vec![ExecMode::LocalSize { x: 1, y: 1, z: 1 }],
)?;
b.finalize()
}
/// The 64-bit integer type and the `{ uint, uint }` struct type only dead values had leave
/// with them, and with the 64-bit type the module's `Int64` capability and `shaderInt64`
/// feature: what a module requires is derived from the types it still declares. The module
/// stays valid, computes what it computed, serializes with no `Int64`, and a second pass
/// finds nothing.
#[test]
fn a_type_only_dead_values_had_leaves_with_its_capability() {
let int64 = SpirvCapability::of(crate::spv::Capability::Int64);
let mut m = wide_base_kernel().expect("build");
assert!(m.requirements.spirv_capabilities.contains(&int64));
let report =
remove_dead(&mut m, &mut Meter::new(&crate::budget::Budget::default())).expect("dce");
assert_eq!((report.insts, report.types), (2, 2), "{report:?}");
let wide = (m.decls().iter()).any(|d| {
matches!(d, Decl::Type(t) if matches!(
m.types().get(*t),
Ok(Type::Int { width: 64, .. } | Type::Struct { layout: None, .. })
))
});
assert!(
!wide,
"the 64-bit integer or the struct type stays declared"
);
assert!(!m.requirements.spirv_capabilities.contains(&int64));
assert!(!m.requirements.features.contains(&VkFeature::ShaderInt64));
let profile = committed_profiles()
.expect("profiles")
.into_iter()
.find(|(s, _)| s == "moltenvk-m3")
.expect("moltenvk-m3")
.1;
assert_valid(m.clone(), &profile);
let res = Resources {
buffers: vec![Buffer {
slot: Some(slot(0)),
address: None,
bytes: [9u32, 0].iter().flat_map(|w| w.to_le_bytes()).collect(),
}],
push: Vec::new(),
spec: Default::default(),
};
let out = run(&m, &Config::default(), res).expect("run");
let bytes = out.buffers.first().expect("buffer");
assert_eq!(bytes.get(4..8), Some(&9u32.to_le_bytes()[..]));
let serialized = crate::spirv::serialize::serialize(m.clone()).expect("serialize");
assert!(!serialized.requirements.spirv_capabilities.contains(&int64));
let again =
remove_dead(&mut m, &mut Meter::new(&crate::budget::Budget::default())).expect("dce");
assert_eq!(again, DeadReport::default());
}
/// A call of a function that stores stays though nothing reads its result, and so does a call
/// of a function that calls it; neither function is pure, so both stay and the store still
/// runs.