From 86bb6819ff884be0a3ffa746a29544b6c5e740c0 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 9 Oct 2026 09:14:33 +0200 Subject: [PATCH 1/2] Fix vector float-to-int conversions OpConvertFToS produced a signed convert_intN value for an unsigned uintN result. Scalars convert implicitly, but OpenCL C has no implicit conversion between vector types, so reinterpret the result with as_type as other signed operations do. The NaN guard of saturating conversions spliced isnan(x) in as the condition of a select, which for a vector is a mask as wide as x's elements rather than the result's, and selected an untyped 0 that outranks narrow element types. Bring the mask to the result's width through relational_mask and a new expression-level select_mask, factored out of select_condition, and select the result type's null constant. Fixes #10. --- lib/instns.cpp | 6 ++- lib/translator.h | 4 ++ lib/utils.cpp | 14 +++++-- tests/cases/convert_ftoi_vector.spt | 65 +++++++++++++++++++++++++++++ tests/cases/cov_intops_convert.spt | 2 +- 5 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 tests/cases/convert_ftoi_vector.spt diff --git a/lib/instns.cpp b/lib/instns.cpp index 2d66352..f817e97 100644 --- a/lib/instns.cpp +++ b/lib/instns.cpp @@ -958,11 +958,15 @@ bool translator_impl::translate_instruction(const Instruction &inst, } val = c::call(fn, {value(op)}); + if (opcode == spv::Op::OpConvertFToS) { + val = as_type(rtype, val); + } // SPIR-V requires that NaNs be converted to 0 for saturating conversions // but OpenCL C just recommends it (ยง6.2.3) if (sat) { - val = c::ternary(call_values("isnan", {op}), c::literal("0"), val); + auto is_nan = relational_mask(op, call_values("isnan", {op})); + val = c::ternary(select_mask(is_nan, rtype), null_constant(rtype), val); } break; diff --git a/lib/translator.h b/lib/translator.h index 1588901..dbdbf52 100644 --- a/lib/translator.h +++ b/lib/translator.h @@ -250,6 +250,10 @@ struct translator_impl { // mask as wide as the selected elements. c::expr_ref select_condition(uint32_t cond, uint32_t result_tyid) const; + // `mask`, a canonical intN mask (or a scalar condition), as the condition of + // a select of `result_tyid` values. + c::expr_ref select_mask(c::expr_ref mask, uint32_t result_tyid) const; + // The C type of the components of vector type `tyid`. std::string src_vector_element_type(uint32_t tyid) const; diff --git a/lib/utils.cpp b/lib/utils.cpp index 8ab506b..de5017d 100644 --- a/lib/utils.cpp +++ b/lib/utils.cpp @@ -163,15 +163,23 @@ c::expr_ref translator_impl::relational_mask(uint32_t operand, c::expr_ref translator_impl::select_condition(uint32_t cond, uint32_t result_tyid) const { + if (type_for_val(cond)->kind() != Type::Kind::kVector) { + return value(cond); + } + return select_mask(value(cond), result_tyid); +} + +c::expr_ref translator_impl::select_mask(c::expr_ref mask, + uint32_t result_tyid) const { auto result_type = type_for(result_tyid); - if (type_for_val(cond)->kind() != Type::Kind::kVector || + if (result_type->kind() != Type::Kind::kVector || element_width(result_type) == 32) { - return value(cond); + return mask; } auto count = std::to_string(result_type->AsVector()->element_count()); return c::call("convert_" + signed_int_type(element_width(result_type)) + count, - {value(cond)}); + {std::move(mask)}); } std::string translator_impl::src_vector_element_type(uint32_t tyid) const { diff --git a/tests/cases/convert_ftoi_vector.spt b/tests/cases/convert_ftoi_vector.spt new file mode 100644 index 0000000..f3c1aca --- /dev/null +++ b/tests/cases/convert_ftoi_vector.spt @@ -0,0 +1,65 @@ +; Float-to-int conversions on vectors. Integer values are emitted as unsigned C +; types and OpenCL C has no implicit conversion between vector types, so the +; signed result of OpConvertFToS is reinterpreted as the result type. A +; saturating conversion maps NaN to 0 with a select, whose condition must be as +; wide as the result's elements and whose zero must have the result type, +; whatever the width of the source. +; RUN: %spirv2clc --asm %s | %clang -c -cl-std=CL1.2 -x cl - -o /dev/null +; RUN: %spirv2clc --asm %s | %FileCheck %s + OpCapability Kernel + OpCapability Addresses + OpCapability Linkage + OpCapability Int16 + OpCapability Int64 + OpCapability Float64 + OpMemoryModel Physical64 OpenCL + OpDecorate %sat_s SaturatedConversion + OpDecorate %sat_u SaturatedConversion + OpDecorate %sat_narrow SaturatedConversion + OpDecorate %sat_scalar SaturatedConversion + OpDecorate %sat_wide SaturatedConversion + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %ushort = OpTypeInt 16 0 + %ulong = OpTypeInt 64 0 + %f32 = OpTypeFloat 32 + %f64 = OpTypeFloat 64 + %v4uint = OpTypeVector %uint 4 + %v4ushort = OpTypeVector %ushort 4 + %v4ulong = OpTypeVector %ulong 4 + %v4f32 = OpTypeVector %f32 4 + %v4f64 = OpTypeVector %f64 4 + %p_v4u = OpTypePointer CrossWorkgroup %v4uint + %p_v4s = OpTypePointer CrossWorkgroup %v4ushort + %p_u = OpTypePointer CrossWorkgroup %uint + %p_v4l = OpTypePointer CrossWorkgroup %v4ulong + %fnty = OpTypeFunction %void %v4f64 %v4f32 %f64 %p_v4u %p_v4s %p_u %p_v4l + %fn = OpFunction %void None %fnty + %d = OpFunctionParameter %v4f64 + %f = OpFunctionParameter %v4f32 + %s = OpFunctionParameter %f64 + %ou = OpFunctionParameter %p_v4u + %os = OpFunctionParameter %p_v4s + %osc = OpFunctionParameter %p_u + %ol = OpFunctionParameter %p_v4l + %lbl = OpLabel + %ftos = OpConvertFToS %v4uint %d + %sat_s = OpConvertFToS %v4uint %d + %sat_u = OpConvertFToU %v4uint %d + %sat_narrow = OpConvertFToS %v4ushort %f + %sat_scalar = OpConvertFToS %uint %s + %sat_wide = OpConvertFToS %v4ulong %f + OpStore %ou %ftos + OpStore %ou %sat_s + OpStore %ou %sat_u + OpStore %os %sat_narrow + OpStore %osc %sat_scalar + OpStore %ol %sat_wide + OpReturn + OpFunctionEnd +; CHECK: uint4 {{v[0-9]+}} = as_uint4(convert_int4_rtz([[D:v[0-9]+]])); +; CHECK: uint4 {{v[0-9]+}} = convert_int4(isnan([[D]])) ? (uint4)(0) : as_uint4(convert_int4_sat_rtz([[D]])); +; CHECK: uint4 {{v[0-9]+}} = convert_int4(isnan([[D]])) ? (uint4)(0) : convert_uint4_sat_rtz([[D]]); +; CHECK: ushort4 {{v[0-9]+}} = convert_short4(isnan([[F:v[0-9]+]])) ? (ushort4)(0) : as_ushort4(convert_short4_sat_rtz([[F]])); +; CHECK: uint {{v[0-9]+}} = isnan([[S:v[0-9]+]]) ? (uint)0 : as_uint(convert_int_sat_rtz([[S]])); +; CHECK: ulong4 {{v[0-9]+}} = convert_long4(isnan([[F]])) ? (ulong4)(0) : as_ulong4(convert_long4_sat_rtz([[F]])); diff --git a/tests/cases/cov_intops_convert.spt b/tests/cases/cov_intops_convert.spt index 80eb6fa..1d61d36 100644 --- a/tests/cases/cov_intops_convert.spt +++ b/tests/cases/cov_intops_convert.spt @@ -46,7 +46,7 @@ OpReturn OpFunctionEnd ; CHECK: = convert_uint_rtz( -; CHECK: = convert_int_rtz( +; CHECK: = as_uint(convert_int_rtz( ; OpSatConvertSToU: signed source (as_int) -> unsigned (ushort) dest ; CHECK: = convert_ushort_sat(as_int( ; OpSatConvertUToS: unsigned source -> signed (short) dest From 76a4ea3907a3fc90b141940740688bb61af7c6f1 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 9 Oct 2026 09:15:21 +0200 Subject: [PATCH 2/2] Reinterpret signed vector integer conversions as the result type OpSConvert and OpSatConvertUToS convert to the signed spelling of their result type, but the result is declared with the unsigned spelling. That converts implicitly for scalars but is an error for vectors, so reinterpret the result with as_type like OpConvertFToS. --- lib/instns.cpp | 14 +++++------ tests/cases/convert_signed_vector.spt | 35 +++++++++++++++++++++++++++ tests/cases/cov_intops_convert.spt | 4 +-- tests/cases/cov_satconvert_signed.spt | 5 ++-- 4 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 tests/cases/convert_signed_vector.spt diff --git a/lib/instns.cpp b/lib/instns.cpp index f817e97..5834740 100644 --- a/lib/instns.cpp +++ b/lib/instns.cpp @@ -1007,11 +1007,11 @@ bool translator_impl::translate_instruction(const Instruction &inst, break; case spv::Op::OpSatConvertUToS: // Unsigned source -> signed dest, saturating (clamps to the signed range). - // The operand is taken as-is (unsigned); the destination is the signed - // result type (cf. OpConvertFToS, which likewise converts to the signed - // type name without reinterpreting the operand). - val = call_values("convert_" + src_type_signed(rtype) + "_sat", - {inst.GetSingleWordOperand(2)}); + // The operand is taken as-is (unsigned); the signed result is + // reinterpreted as the (unsigned) result type. + val = + as_type(rtype, call_values("convert_" + src_type_signed(rtype) + "_sat", + {inst.GetSingleWordOperand(2)})); break; case spv::Op::OpBitcast: { auto operand = inst.GetSingleWordOperand(2); @@ -1026,8 +1026,8 @@ bool translator_impl::translate_instruction(const Instruction &inst, break; } case spv::Op::OpSConvert: - val = c::call("convert_" + src_type_signed(rtype), - {as_signed(inst.GetSingleWordOperand(2))}); + val = as_type(rtype, c::call("convert_" + src_type_signed(rtype), + {as_signed(inst.GetSingleWordOperand(2))})); break; case spv::Op::OpFConvert: case spv::Op::OpUConvert: diff --git a/tests/cases/convert_signed_vector.spt b/tests/cases/convert_signed_vector.spt new file mode 100644 index 0000000..b954f83 --- /dev/null +++ b/tests/cases/convert_signed_vector.spt @@ -0,0 +1,35 @@ +; Integer conversions with a signed result on vectors. Integer values are +; emitted as unsigned C types and OpenCL C has no implicit conversion between +; vector types, so the signed result of convert_ is reinterpreted +; as the result type. +; RUN: %spirv2clc --asm %s | %clang -c -cl-std=CL1.2 -x cl - -o /dev/null +; RUN: %spirv2clc --asm %s | %FileCheck %s + OpCapability Kernel + OpCapability Addresses + OpCapability Linkage + OpCapability Int64 + OpCapability Int16 + OpMemoryModel Physical64 OpenCL + %void = OpTypeVoid + %uint = OpTypeInt 32 0 + %ushort = OpTypeInt 16 0 + %ulong = OpTypeInt 64 0 + %v4uint = OpTypeVector %uint 4 + %v4ushort = OpTypeVector %ushort 4 + %v4ulong = OpTypeVector %ulong 4 + %p_v4l = OpTypePointer CrossWorkgroup %v4ulong + %p_v4s = OpTypePointer CrossWorkgroup %v4ushort + %fnty = OpTypeFunction %void %v4uint %p_v4l %p_v4s + %fn = OpFunction %void None %fnty + %x = OpFunctionParameter %v4uint + %ol = OpFunctionParameter %p_v4l + %os = OpFunctionParameter %p_v4s + %lbl = OpLabel + %sext = OpSConvert %v4ulong %x + %satus = OpSatConvertUToS %v4ushort %x + OpStore %ol %sext + OpStore %os %satus + OpReturn + OpFunctionEnd +; CHECK: ulong4 {{v[0-9]+}} = as_ulong4(convert_long4(as_int4([[X:v[0-9]+]]))); +; CHECK: ushort4 {{v[0-9]+}} = as_ushort4(convert_short4_sat([[X]])); diff --git a/tests/cases/cov_intops_convert.spt b/tests/cases/cov_intops_convert.spt index 1d61d36..cbb9d2d 100644 --- a/tests/cases/cov_intops_convert.spt +++ b/tests/cases/cov_intops_convert.spt @@ -50,7 +50,7 @@ ; OpSatConvertSToU: signed source (as_int) -> unsigned (ushort) dest ; CHECK: = convert_ushort_sat(as_int( ; OpSatConvertUToS: unsigned source -> signed (short) dest -; CHECK: = convert_short_sat( -; CHECK: = convert_long(as_int( +; CHECK: = as_ushort(convert_short_sat( +; CHECK: = as_ulong(convert_long(as_int( ; CHECK: = convert_ulong( ; CHECK: = convert_double( diff --git a/tests/cases/cov_satconvert_signed.spt b/tests/cases/cov_satconvert_signed.spt index a01a546..b272b90 100644 --- a/tests/cases/cov_satconvert_signed.spt +++ b/tests/cases/cov_satconvert_signed.spt @@ -4,7 +4,8 @@ ; reinterpreted as signed (as_int) so negatives saturate to 0, not to a huge ; positive; the destination is the unsigned result type (convert_uchar_sat). ; - OpSatConvertUToS: unsigned source -> signed dest. The operand is taken -; as-is and the destination is the signed result type (convert_char_sat). +; as-is, converted to the signed type (convert_char_sat) and reinterpreted +; as the unsigned result type (as_uchar). ; (Previously both were emitted with the source/dest signedness swapped.) ; RUN: %spirv2clc --asm %s | %clang -c -cl-std=CL1.2 -x cl - -o /dev/null ; RUN: %spirv2clc --asm %s | %FileCheck %s @@ -30,4 +31,4 @@ OpReturn OpFunctionEnd ; CHECK: = convert_uchar_sat(as_int({{v[0-9]+}})); -; CHECK: = convert_char_sat({{v[0-9]+}}); +; CHECK: = as_uchar(convert_char_sat({{v[0-9]+}}));