Skip to content

Commit 7054230

Browse files
committed
Prefer signed OpTypeInt for fallback int32_type/int64_type
When resolve_operand_type falls back to int32_type for cross-class operations (e.g. ConvertSToF operand type), an unsigned int type causes SPIRV-Cross to generate unsigned conversion code. This turns negative float-to-int-to-float round-trips into zero.
1 parent 7f7961e commit 7054230

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

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

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,18 +2261,29 @@ impl TypeClass {
22612261

22622262
/// Find a SPIR-V type declaration's result ID by opcode and optional bit-width.
22632263
fn find_spirv_type(module: &Module, opcode: Op, width: Option<u32>) -> Option<Word> {
2264+
let matches_width = |inst: &Instruction| {
2265+
inst.class.opcode == opcode
2266+
&& match width {
2267+
Some(w) => inst.operands.first() == Some(&rspirv::dr::Operand::LiteralBit32(w)),
2268+
None => true,
2269+
}
2270+
};
2271+
// For OpTypeInt, prefer signed (signedness=1). The int32_type/int64_type
2272+
// fallback is used in resolve_operand_type for cross-class operations like
2273+
// ConvertSToF. If the fallback is unsigned, cross-compilers (SPIRV-Cross)
2274+
// may generate unsigned conversion code, turning negative values to zero.
2275+
if opcode == Op::TypeInt {
2276+
if let Some(inst) = module.types_global_values.iter().find(|inst| {
2277+
matches_width(inst)
2278+
&& inst.operands.get(1) == Some(&rspirv::dr::Operand::LiteralBit32(1))
2279+
}) {
2280+
return inst.result_id;
2281+
}
2282+
}
22642283
module
22652284
.types_global_values
22662285
.iter()
2267-
.find(|inst| {
2268-
inst.class.opcode == opcode
2269-
&& match width {
2270-
Some(w) => {
2271-
inst.operands.first() == Some(&rspirv::dr::Operand::LiteralBit32(w))
2272-
}
2273-
None => true,
2274-
}
2275-
})
2286+
.find(|inst| matches_width(inst))
22762287
.and_then(|inst| inst.result_id)
22772288
}
22782289

0 commit comments

Comments
 (0)