Skip to content

Commit 2dae722

Browse files
committed
Fix type inference for conversion and vector result types
Add conversion operations (ConvertFToS, ConvertFToU, ConvertSToF, ConvertUToF) to required_result_type_class so the reconstruction pass can infer correct result types for conversion instructions. Preserve vector result types (TypeClass::Other) in infer_result_type instead of incorrectly replacing them with scalar types. SPIR-V arithmetic ops work component-wise on vectors, so a vector result type is valid even when the opcode requires a specific scalar sort.
1 parent fcdd2fa commit 2dae722

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

  • rust/spirv-tools-opt/src/direct

rust/spirv-tools-opt/src/direct/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2326,7 +2326,11 @@ fn required_result_type_class(op: Op) -> Option<TypeClass> {
23262326
| Op::LogicalNotEqual
23272327
| Op::LogicalNot => Some(TypeClass::Bool),
23282328

2329-
// Select/CopyObject/conversions: result type depends on context
2329+
// Conversion operations: result type is determined by the target type
2330+
Op::ConvertFToS | Op::ConvertFToU => Some(TypeClass::Int),
2331+
Op::ConvertSToF | Op::ConvertUToF => Some(TypeClass::Float),
2332+
2333+
// Select/CopyObject/other: result type depends on context
23302334
_ => None,
23312335
}
23322336
}
@@ -2398,6 +2402,14 @@ fn infer_result_type(
23982402
return original_result_type;
23992403
}
24002404

2405+
// If current type is Other (vector/matrix/struct), keep it unchanged.
2406+
// SPIR-V arithmetic ops work component-wise on vectors, so a vector
2407+
// result type is valid even when the opcode "requires" a scalar type.
2408+
// Changing a vector type to a scalar type would produce invalid SPIR-V.
2409+
if current_class == TypeClass::Other {
2410+
return original_result_type;
2411+
}
2412+
24012413
// Type mismatch - need to infer the correct type
24022414
match required {
24032415
TypeClass::Bool => {

0 commit comments

Comments
 (0)