Close remaining AIR layout seams
Carry parsed vector ABI alignment into pre-emitter memcpy and metadata sizing, make the runtime-array raw-word fast path sidecar-aware, and propagate nested or missing AIR mapping failures into typed evidence.\n\nVerified with full serial workspace tests, clippy, Rustdoc, the release reproducer, and a 500-row current-fingerprint translation audit with zero failures.
This commit is contained in:
1 parent
adb19e2c2c
commit
d7d64f51dd
13 files changed
+215
-55
No files matched your search
@@ -54,6 +54,7 @@ pub(crate) struct AirStructLayoutMapping {
|
||||
pub(crate) enum AirStructLayoutMappingStatus {
|
||||
MappedNatural,
|
||||
MappedExplicit,
|
||||
ParameterMissing,
|
||||
ParameterIsNotPointer,
|
||||
MetadataIsNotStruct,
|
||||
EmittedShapeMismatch,
|
||||
|
||||
+18
-10
@@ -138,11 +138,13 @@ pub(super) fn native_size_align(
|
||||
}
|
||||
}
|
||||
|
||||
/// The "Memcpy" rule: storage layout for a `memcpy` (a vec3 is padded to 4 lanes = 16/16, its
|
||||
/// size==align; arrays align to at least 4). Was `LlModule::native_memcpy_type_size_align`.
|
||||
/// The "Memcpy" rule: LLVM allocation layout for a `memcpy`, using the source vector ABI alignment
|
||||
/// (or LLVM's power-of-two default) and aligning arrays to at least four bytes. Was
|
||||
/// `LlModule::native_memcpy_type_size_align`.
|
||||
pub(super) fn memcpy_size_align(
|
||||
ty: &LlType,
|
||||
resolve: &impl Fn(&LlType) -> LlType,
|
||||
data_layout: Option<&AirDataLayout>,
|
||||
) -> Option<(u64, u64)> {
|
||||
match resolve(ty) {
|
||||
LlType::Bool => Some((1, 1)),
|
||||
@@ -154,13 +156,18 @@ pub(super) fn memcpy_size_align(
|
||||
LlType::Float => Some((4, 4)),
|
||||
LlType::Ptr(_) => Some((8, 8)),
|
||||
LlType::Vector(elem, lanes) => {
|
||||
let (elem_size, _elem_align) = memcpy_size_align(&elem, resolve)?;
|
||||
let storage_lanes = if lanes == 3 { 4 } else { lanes };
|
||||
let size = elem_size * storage_lanes as u64;
|
||||
Some((size, size))
|
||||
let (elem_size, _elem_align) = memcpy_size_align(&elem, resolve, data_layout)?;
|
||||
let store_size = elem_size.checked_mul(u64::from(lanes))?;
|
||||
let store_bits = u32::try_from(store_size.checked_mul(8)?).ok()?;
|
||||
let align = u64::from(
|
||||
data_layout
|
||||
.map(|layout| layout.vector_align_bytes(store_bits))
|
||||
.unwrap_or_else(|| store_bits.next_power_of_two() / 8),
|
||||
);
|
||||
Some((round_up_u64(store_size, align), align))
|
||||
}
|
||||
LlType::Array(elem, len) => {
|
||||
let (elem_size, elem_align) = memcpy_size_align(&elem, resolve)?;
|
||||
let (elem_size, elem_align) = memcpy_size_align(&elem, resolve, data_layout)?;
|
||||
Some((
|
||||
round_up_u64(elem_size, elem_align) * len as u64,
|
||||
elem_align.max(4),
|
||||
@@ -170,7 +177,7 @@ pub(super) fn memcpy_size_align(
|
||||
let mut offset = 0u64;
|
||||
let mut max_align = 1u64;
|
||||
for field in fields {
|
||||
let (size, align) = memcpy_size_align(&field, resolve)?;
|
||||
let (size, align) = memcpy_size_align(&field, resolve, data_layout)?;
|
||||
max_align = max_align.max(align);
|
||||
offset = round_up_u64(offset, align);
|
||||
offset += size;
|
||||
@@ -241,19 +248,20 @@ pub(super) fn raw_size_align(
|
||||
pub(super) fn air_metadata_size_align(
|
||||
ty: &AirType,
|
||||
resolve: &impl Fn(&LlType) -> LlType,
|
||||
data_layout: Option<&AirDataLayout>,
|
||||
) -> Option<(u64, u64)> {
|
||||
match ty {
|
||||
AirType::Struct(members) => {
|
||||
let mut end = 0u64;
|
||||
let mut max_align = 1u64;
|
||||
for member in members {
|
||||
let (size, align) = air_metadata_size_align(&member.ty, resolve)?;
|
||||
let (size, align) = air_metadata_size_align(&member.ty, resolve, data_layout)?;
|
||||
max_align = max_align.max(align);
|
||||
end = end.max(u64::from(member.offset) + size);
|
||||
}
|
||||
Some((round_up_u64(end, max_align), max_align))
|
||||
}
|
||||
_ => memcpy_size_align(&ll_type_from_air_type(ty), resolve),
|
||||
_ => memcpy_size_align(&ll_type_from_air_type(ty), resolve, data_layout),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -54,6 +54,7 @@ impl Emitter {
|
||||
.filter_map(|instruction| instruction.result_id.map(|id| (id, instruction.clone())))
|
||||
.collect::<HashMap<_, _>>();
|
||||
|
||||
let param_count = param_types.len();
|
||||
for (index, param_type) in param_types.into_iter().enumerate() {
|
||||
let Some(layout) = buffer_layouts.get(&(index as u32)) else {
|
||||
continue;
|
||||
@@ -83,6 +84,23 @@ impl Emitter {
|
||||
status,
|
||||
});
|
||||
}
|
||||
let mut missing_params = buffer_layouts
|
||||
.keys()
|
||||
.copied()
|
||||
.filter(|index| *index as usize >= param_count)
|
||||
.collect::<Vec<_>>();
|
||||
missing_params.sort_unstable();
|
||||
self.emit_sidecar
|
||||
.air_struct_layout_mappings
|
||||
.extend(
|
||||
missing_params
|
||||
.into_iter()
|
||||
.map(|param_index| AirStructLayoutMapping {
|
||||
param_index,
|
||||
struct_ty: None,
|
||||
status: AirStructLayoutMappingStatus::ParameterMissing,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -288,7 +306,8 @@ fn air_type_matches_existing(
|
||||
return false;
|
||||
};
|
||||
def.class.opcode == Op::TypeStruct
|
||||
&& map_existing_air_struct_offsets(defs, def, members, air_data_layout).is_some()
|
||||
&& map_existing_air_struct_offsets(defs, def, members, air_data_layout)
|
||||
.is_some_and(|offsets| offsets.windows(2).all(|window| window[1] > window[0]))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -410,3 +429,59 @@ fn is_backend_padding_array(defs: &HashMap<Word, Instruction>, ty: Word) -> bool
|
||||
};
|
||||
type_int_width(defs, elem) == Some(8)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn nested_nonincreasing_air_offsets_fail_the_top_level_mapping() {
|
||||
let byte_ty = 1;
|
||||
let inner_ty = 2;
|
||||
let outer_ty = 3;
|
||||
let defs = HashMap::from([
|
||||
(
|
||||
byte_ty,
|
||||
Instruction::new(
|
||||
Op::TypeInt,
|
||||
None,
|
||||
Some(byte_ty),
|
||||
vec![Operand::LiteralBit32(8), Operand::LiteralBit32(0)],
|
||||
),
|
||||
),
|
||||
(
|
||||
inner_ty,
|
||||
Instruction::new(
|
||||
Op::TypeStruct,
|
||||
None,
|
||||
Some(inner_ty),
|
||||
vec![Operand::IdRef(byte_ty), Operand::IdRef(byte_ty)],
|
||||
),
|
||||
),
|
||||
(
|
||||
outer_ty,
|
||||
Instruction::new(
|
||||
Op::TypeStruct,
|
||||
None,
|
||||
Some(outer_ty),
|
||||
vec![Operand::IdRef(inner_ty)],
|
||||
),
|
||||
),
|
||||
]);
|
||||
let air_ty = AirType::Struct(vec![AirMember {
|
||||
offset: 0,
|
||||
ty: AirType::Struct(vec![
|
||||
AirMember {
|
||||
offset: 0,
|
||||
ty: AirType::Scalar(AirScalar::UChar),
|
||||
},
|
||||
AirMember {
|
||||
offset: 0,
|
||||
ty: AirType::Scalar(AirScalar::UChar),
|
||||
},
|
||||
]),
|
||||
}]);
|
||||
|
||||
assert!(!air_type_matches_existing(&defs, outer_ty, &air_ty, None));
|
||||
}
|
||||
}
|
||||
@@ -72,10 +72,12 @@ fn raw_rule_errors_on_uncovered_types() {
|
||||
|
||||
#[test]
|
||||
fn raw_rule_uses_source_vector_abi_alignment() {
|
||||
let mut e = emitter();
|
||||
e.air_data_layout = Some(
|
||||
crate::layout::AirDataLayout::parse("e-v24:64:64").expect("parse custom vector alignment"),
|
||||
);
|
||||
let ir = LlModule::parse(concat!(
|
||||
"target datalayout = \"e-v24:64:64\"\n",
|
||||
"define void @k() {\nentry:\n ret void\n}\n",
|
||||
))
|
||||
.expect("module with custom datalayout parses");
|
||||
let e = Emitter::new(ir);
|
||||
|
||||
assert_eq!(
|
||||
e.raw_type_size_align(&vec(LlType::Int(8), 3))
|
||||
|
||||
@@ -413,6 +413,7 @@ impl Emitter {
|
||||
ir.inline_simple_static_initializers();
|
||||
ir.fold_static_initializer_constants();
|
||||
ir.inline_ordinary_leaf_helpers();
|
||||
let air_data_layout = ir.air_data_layout.clone();
|
||||
let mut module = Module::new();
|
||||
module.capabilities.push(Instruction::new(
|
||||
Op::Capability,
|
||||
@@ -442,7 +443,7 @@ impl Emitter {
|
||||
module,
|
||||
ir,
|
||||
emit_sidecar: crate::emit_sidecar::EmitSidecar::default(),
|
||||
air_data_layout: None,
|
||||
air_data_layout,
|
||||
interner: TypeInterner::new(),
|
||||
glsl_ext: None,
|
||||
values: HashMap::new(),
|
||||
@@ -955,10 +956,13 @@ impl Emitter {
|
||||
buffer_layouts: Option<&HashMap<u32, crate::meta::AirType>>,
|
||||
air_data_layout: Option<&crate::layout::AirDataLayout>,
|
||||
) -> Result<(Module, crate::emit_sidecar::EmitSidecar), String> {
|
||||
self.air_data_layout = air_data_layout.cloned();
|
||||
if let Some(air_data_layout) = air_data_layout {
|
||||
self.air_data_layout = Some(air_data_layout.clone());
|
||||
}
|
||||
self.emit_inner()?;
|
||||
self.record_air_struct_offsets(buffer_layouts, air_data_layout);
|
||||
self.emit_sidecar.air_data_layout = air_data_layout.cloned();
|
||||
let air_data_layout = self.air_data_layout.clone();
|
||||
self.record_air_struct_offsets(buffer_layouts, air_data_layout.as_ref());
|
||||
self.emit_sidecar.air_data_layout = air_data_layout;
|
||||
if self.used_device_address {
|
||||
self.lower_bda_null_aggregate_pointers()?;
|
||||
self.switch_to_physical_storage_buffer64();
|
||||
|
||||
+18
-3
@@ -130,6 +130,7 @@ pub(super) struct LlGlobal {
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(super) struct LlModule {
|
||||
pub(super) air_data_layout: Option<crate::layout::AirDataLayout>,
|
||||
pub(super) types: HashMap<String, LlType>,
|
||||
pub(super) functions: Vec<LlFunction>,
|
||||
pub(super) declarations: Vec<LlDeclaration>,
|
||||
@@ -702,11 +703,11 @@ mod layout_abi_tests {
|
||||
//! are the contract that fold must preserve. Each calculator here is one `LayoutRule` variant:
|
||||
//!
|
||||
//! - `type_storage_size_align` → "Native" (packed-style: vec3 = 12/4)
|
||||
//! - `native_memcpy_type_size_align` → "Memcpy" (vec3 padded to 4 lanes: 16/16)
|
||||
//! - `native_memcpy_type_size_align` → "Memcpy" (source vector ABI allocation layout)
|
||||
//! - `air_metadata_type_size_align` → "AirMetadata" (packed/unpacked distinguished by type)
|
||||
//!
|
||||
//! The passes-layer std140 calculator (`stage_input::layout::ty_size_align`, a THIRD rule where
|
||||
//! vec3 = 12/16) is pinned in that module's own tests.
|
||||
//! The passes layer applies the same source ABI alignment to emitted SPIR-V types through its
|
||||
//! sidecar-aware layout calculator.
|
||||
use super::*;
|
||||
use crate::meta::{AirMember, AirScalar, AirType};
|
||||
|
||||
@@ -808,6 +809,20 @@ mod layout_abi_tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn memcpy_rule_uses_parsed_source_vector_alignment() {
|
||||
let m = LlModule::parse(concat!(
|
||||
"target datalayout = \"e-v24:64:64\"\n",
|
||||
"define void @k() {\nentry:\n ret void\n}\n",
|
||||
))
|
||||
.expect("module with custom datalayout parses");
|
||||
|
||||
assert_eq!(
|
||||
m.native_memcpy_type_size_align(&vec(LlType::Int(8), 3)),
|
||||
Some((8, 8))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn air_metadata_rule_distinguishes_packed_from_unpacked() {
|
||||
// "AirMetadata" (`air_metadata_type_size_align`): the metadata type system carries the
|
||||
|
||||
@@ -154,6 +154,7 @@ impl LlModule {
|
||||
|| imageblock_nonzero_byte_field,
|
||||
);
|
||||
let mut module = Self {
|
||||
air_data_layout: crate::layout::AirDataLayout::from_ir(ll)?,
|
||||
types,
|
||||
functions,
|
||||
declarations,
|
||||
|
||||
@@ -393,14 +393,22 @@ impl LlModule {
|
||||
&self,
|
||||
ty: &LlType,
|
||||
) -> Option<(u64, u64)> {
|
||||
crate::layout::memcpy_size_align(ty, &|t| self.resolve_known_type(t))
|
||||
crate::layout::memcpy_size_align(
|
||||
ty,
|
||||
&|t| self.resolve_known_type(t),
|
||||
self.air_data_layout.as_ref(),
|
||||
)
|
||||
}
|
||||
|
||||
pub(in crate::native) fn air_metadata_type_size_align(
|
||||
&self,
|
||||
ty: &AirType,
|
||||
) -> Option<(u64, u64)> {
|
||||
crate::layout::air_metadata_size_align(ty, &|t| self.resolve_known_type(t))
|
||||
crate::layout::air_metadata_size_align(
|
||||
ty,
|
||||
&|t| self.resolve_known_type(t),
|
||||
self.air_data_layout.as_ref(),
|
||||
)
|
||||
}
|
||||
|
||||
pub(in crate::native) fn propagate_raw_buffer_params(&mut self) {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use super::*;
|
||||
use crate::passes::stage_input::{
|
||||
decorate_block_struct, is_backend_padding_array, round_up, ty_size_align,
|
||||
decorate_block_struct, is_backend_padding_array, layout_ty_size_align, round_up,
|
||||
};
|
||||
|
||||
mod raw_word_rewrite;
|
||||
@@ -55,6 +55,59 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn runtime_array_word_index_respects_source_vector_alignment() {
|
||||
let mut module = Module::new();
|
||||
module.header = Some(ModuleHeader::new(20));
|
||||
module.types_global_values = vec![
|
||||
Instruction::new(
|
||||
Op::TypeInt,
|
||||
None,
|
||||
Some(1),
|
||||
vec![Operand::LiteralBit32(8), Operand::LiteralBit32(0)],
|
||||
),
|
||||
Instruction::new(
|
||||
Op::TypeInt,
|
||||
None,
|
||||
Some(2),
|
||||
vec![Operand::LiteralBit32(32), Operand::LiteralBit32(0)],
|
||||
),
|
||||
Instruction::new(
|
||||
Op::TypeVector,
|
||||
None,
|
||||
Some(3),
|
||||
vec![Operand::IdRef(1), Operand::LiteralBit32(3)],
|
||||
),
|
||||
Instruction::new(Op::TypeRuntimeArray, None, Some(4), vec![Operand::IdRef(3)]),
|
||||
Instruction::new(Op::TypeStruct, None, Some(5), vec![Operand::IdRef(4)]),
|
||||
Instruction::new(
|
||||
Op::Constant,
|
||||
Some(2),
|
||||
Some(6),
|
||||
vec![Operand::LiteralBit32(0)],
|
||||
),
|
||||
];
|
||||
let defs = defs_from_module(&module);
|
||||
let value_types = HashMap::from([(7, 2)]);
|
||||
|
||||
let default_ctx = Ctx::new(module.clone());
|
||||
assert_eq!(
|
||||
runtime_array_word_index(&default_ctx, &defs, &value_types, 5, &[6, 7]),
|
||||
Some(7)
|
||||
);
|
||||
|
||||
let mut custom_ctx = Ctx::new(module);
|
||||
custom_ctx.air_data_layout = Some(
|
||||
crate::layout::AirDataLayout::parse("e-v24:64:64")
|
||||
.expect("parse vector alignment override"),
|
||||
);
|
||||
assert_eq!(
|
||||
runtime_array_word_index(&custom_ctx, &defs, &value_types, 5, &[6, 7]),
|
||||
None,
|
||||
"an eight-byte vector allocation cannot be addressed as one raw word"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn workgroup_root_select_arm_rewrites_to_element_zero() {
|
||||
let mut module = Module::new();
|
||||
|
||||
@@ -332,23 +332,23 @@ pub(in crate::passes) fn plan_structured_raw_word_pointer_rewrite(
|
||||
let path = paths.get(&ptr)?;
|
||||
let block_ty = buffer_types.get(&path.root).copied()?;
|
||||
let mut prefix = Vec::new();
|
||||
let word_index = if let Some(byte_offset) =
|
||||
access_path_byte_offset(ctx, types, block_ty, &path.indices)
|
||||
{
|
||||
if byte_offset % 4 != 0 {
|
||||
return None;
|
||||
}
|
||||
ctx.const_uint(byte_offset / 4)
|
||||
} else {
|
||||
let dynamic_index = runtime_array_word_index(types, value_types, block_ty, &path.indices)?;
|
||||
coerce_storage_buffer_word_index_to_uint(
|
||||
ctx,
|
||||
types,
|
||||
value_types,
|
||||
dynamic_index,
|
||||
&mut prefix,
|
||||
)?
|
||||
};
|
||||
let word_index =
|
||||
if let Some(byte_offset) = access_path_byte_offset(ctx, types, block_ty, &path.indices) {
|
||||
if byte_offset % 4 != 0 {
|
||||
return None;
|
||||
}
|
||||
ctx.const_uint(byte_offset / 4)
|
||||
} else {
|
||||
let dynamic_index =
|
||||
runtime_array_word_index(ctx, types, value_types, block_ty, &path.indices)?;
|
||||
coerce_storage_buffer_word_index_to_uint(
|
||||
ctx,
|
||||
types,
|
||||
value_types,
|
||||
dynamic_index,
|
||||
&mut prefix,
|
||||
)?
|
||||
};
|
||||
let binding = descriptor_binding(&ctx.module, path.root)?;
|
||||
let raw_var = match raw_alias_vars.get(&path.root).copied() {
|
||||
Some(var) => var,
|
||||
@@ -381,6 +381,7 @@ pub(in crate::passes) fn plan_structured_raw_word_pointer_rewrite(
|
||||
}
|
||||
|
||||
pub(in crate::passes) fn runtime_array_word_index(
|
||||
ctx: &Ctx,
|
||||
types: &HashMap<Word, Instruction>,
|
||||
value_types: &HashMap<Word, Word>,
|
||||
block_ty: Word,
|
||||
@@ -408,7 +409,7 @@ pub(in crate::passes) fn runtime_array_word_index(
|
||||
Some(Operand::IdRef(elem)) => *elem,
|
||||
_ => return None,
|
||||
};
|
||||
let (size, align) = ty_size_align(elem, types);
|
||||
let (size, align) = layout_ty_size_align(ctx, elem, types);
|
||||
if round_up(size, align) != 4 || !value_types.contains_key(index) {
|
||||
return None;
|
||||
}
|
||||
|
||||
@@ -383,9 +383,3 @@ pub(in crate::passes) fn layout_ty_size_align(
|
||||
}
|
||||
|
||||
pub(in crate::passes) use crate::layout::round_up_u32 as round_up;
|
||||
|
||||
/// (size, align) in bytes for a SPIR-V type, MSL/std140 vector rules. Covers float/int scalars,
|
||||
/// vectors (align = 2x for vec2, 4x for vec3/4), arrays, and nested structs.
|
||||
pub(in crate::passes) fn ty_size_align(ty: Word, defs: &HashMap<Word, Instruction>) -> (u32, u32) {
|
||||
crate::layout::spirv_size_align(ty, defs, crate::layout::SpirvLayout::natural(None))
|
||||
}
|
||||
@@ -18,9 +18,7 @@ use layout::*;
|
||||
|
||||
// Layout size/align helpers are reused by the lower pass to decorate OpPtrAccessChain base
|
||||
// pointer types with ArrayStride (a sibling-module of interface cannot reach `layout` directly).
|
||||
pub(in crate::passes) use layout::{
|
||||
decorate_block_struct, layout_ty_size_align, round_up, ty_size_align,
|
||||
};
|
||||
pub(in crate::passes) use layout::{decorate_block_struct, layout_ty_size_align, round_up};
|
||||
|
||||
mod air_layout;
|
||||
pub(in crate::passes) use air_layout::*;
|
||||
|
||||
+3
-3
@@ -173,7 +173,7 @@ pub fn air_to_sanitized_ll(src: &str, tmp: &Path) -> Result<String, String> {
|
||||
Ok(air_to_sanitized_ll_with_datalayout(src, tmp)?.0)
|
||||
}
|
||||
|
||||
/// Like [`air_to_sanitized_ll`] but also returns the AIR `target datalayout` string it strips. The
|
||||
/// Like [`air_to_sanitized_ll`] but also returns the AIR `target datalayout` string it preserves. The
|
||||
/// sanitizer preserves that line as the executable source-layout contract and also returns its value
|
||||
/// for reflection, avoiding a second source read. The sanitized text is byte-identical to
|
||||
/// [`air_to_sanitized_ll`].
|
||||
@@ -533,8 +533,8 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn sanitizer_captures_and_preserves_target_datalayout() {
|
||||
// R4: the datalayout survives as a captured string even though it is stripped from the
|
||||
// sanitized text handed to the emitter.
|
||||
// The same datalayout survives both as a captured reflection string and in the sanitized
|
||||
// text handed to executable layout lowering.
|
||||
let tmp = std::env::temp_dir();
|
||||
let src = tmp.join("m2v_r4_datalayout_test.ll");
|
||||
std::fs::write(
|
||||
|
||||
Reference in new issue
Block a user