Skip to content

Commit 4daab3e

Browse files
committed
* SPIRVWriter: deal with integer signedness mismatch between the lhs operand and the binary op itself
1 parent 78f0ffd commit 4daab3e

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

lib/SPIRV/SPIRVWriter.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,33 @@ SPIRVInstruction *LLVMToSPIRVBase::transBinaryInst(BinaryOperator *B,
11511151

11521152
// take care of signed/unsigned type conversion mismatches,
11531153
// TODO: as with unary instructions, we need to do this properly at some point
1154-
const auto type = transType(B->getType());
1154+
const auto binary_op_type = transType(B->getType());
1155+
const auto lhs_type = Op0->getType();
1156+
SPIRVType *type = binary_op_type;
1157+
// assume the lhs type if there is an integer type mismatch
1158+
if (binary_op_type != lhs_type) {
1159+
if (lhs_type->isTypeInt() && binary_op_type->isTypeInt()) {
1160+
assert(((SPIRVTypeInt *)lhs_type)->isSigned() !=
1161+
((SPIRVTypeInt *)binary_op_type)->isSigned());
1162+
assert(((SPIRVTypeInt *)lhs_type)->getBitWidth() ==
1163+
((SPIRVTypeInt *)binary_op_type)->getBitWidth());
1164+
type = lhs_type;
1165+
} else if (lhs_type->isTypeVectorInt() &&
1166+
binary_op_type->isTypeVectorInt()) {
1167+
[[maybe_unused]] const auto lhs_vec_type = (SPIRVTypeVector *)lhs_type;
1168+
[[maybe_unused]] const auto bin_vec_type =
1169+
(SPIRVTypeVector *)binary_op_type;
1170+
assert(lhs_vec_type->getComponentCount() ==
1171+
bin_vec_type->getComponentCount());
1172+
[[maybe_unused]] const auto lhs_int_type =
1173+
(SPIRVTypeInt *)lhs_vec_type->getVectorComponentType();
1174+
[[maybe_unused]] const auto bin_int_type =
1175+
(SPIRVTypeInt *)binary_op_type->getVectorComponentType();
1176+
assert(lhs_int_type->isSigned() != bin_int_type->isSigned());
1177+
assert(lhs_int_type->getBitWidth() == bin_int_type->getBitWidth());
1178+
type = lhs_type;
1179+
}
1180+
}
11551181
const auto is_int = type->isTypeInt();
11561182
const auto is_sint = (is_int ? ((SPIRVTypeInt *)type)->isSigned() : false);
11571183
const auto is_uint = (is_int ? !((SPIRVTypeInt *)type)->isSigned() : false);

0 commit comments

Comments
 (0)