[clang] 4929b7d - [CIR][OpenCL] Fix vector ternary with unsigned condition or operands (#237750)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 3 04:01:50 PST 2026
Author: Arseniy Obolenskiy
Date: 2026-11-03T13:01:44+01:00
New Revision: 4929b7dad5d706e06ceb8dff1579a262be2c53ce
URL: https://github.com/llvm/llvm-project/commit/4929b7dad5d706e06ceb8dff1579a262be2c53ce
DIFF: https://github.com/llvm/llvm-project/commit/4929b7dad5d706e06ceb8dff1579a262be2c53ce.diff
LOG: [CIR][OpenCL] Fix vector ternary with unsigned condition or operands (#237750)
Use a signed MSB test and bitcast operands to match its type
Added:
Modified:
clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
clang/test/CIR/CodeGenOpenCL/vector.cl
Removed:
################################################################################
diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
index 90796feb5e6f6a..ed15c81b878458 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
@@ -3344,19 +3344,21 @@ mlir::Value ScalarExprEmitter::VisitAbstractConditionalOperator(
mlir::Value lhsValue = Visit(lhsExpr);
mlir::Value rhsValue = Visit(rhsExpr);
- mlir::Type vecTy = convertType(condType);
+ // Test the MSB with a signed compare, as classic codegen does.
+ mlir::Type vecTy =
+ convertType(cgf.getContext().getCorrespondingSignedType(condType));
+ condValue = builder.createBitcast(condValue, vecTy);
mlir::Value zeroVec = builder.getNullValue(vecTy, loc);
auto testMSB = cir::VecCmpOp::create(
builder, loc, vecTy, cir::CmpOpKind::lt, condValue, zeroVec);
mlir::Value tmp = builder.createIntCast(testMSB, vecTy);
mlir::Value tmp2 = builder.createNot(tmp);
- // Cast float to int to perform ANDs if necessary.
+ // Bitcast float or
diff erently signed operands to the condition type.
mlir::Value rhsTmp = rhsValue;
mlir::Value lhsTmp = lhsValue;
bool wasCast = false;
- auto rhsVecTy = cast<cir::VectorType>(rhsValue.getType());
- if (cir::isAnyFloatingPointType(rhsVecTy.getElementType())) {
+ if (rhsValue.getType() != vecTy) {
rhsTmp = builder.createBitcast(rhsValue, tmp2.getType());
lhsTmp = builder.createBitcast(lhsValue, tmp.getType());
wasCast = true;
diff --git a/clang/test/CIR/CodeGenOpenCL/vector.cl b/clang/test/CIR/CodeGenOpenCL/vector.cl
index 9e71e34da4724f..dfb493906d471f 100644
--- a/clang/test/CIR/CodeGenOpenCL/vector.cl
+++ b/clang/test/CIR/CodeGenOpenCL/vector.cl
@@ -9,6 +9,7 @@
typedef __attribute__(( ext_vector_type(2) )) int int2;
typedef __attribute__(( ext_vector_type(4) )) int int4;
+typedef __attribute__(( ext_vector_type(4) )) unsigned int uint4;
typedef __attribute__(( ext_vector_type(4) )) float float4;
int4 vec_ternary(int4 c, int4 a, int4 b) {
@@ -72,6 +73,36 @@ float4 vec_ternary_f4(int4 c, float4 a, float4 b) {
// LLVM: %[[RESULT:.*]] = select <4 x i1> %[[IS_NEG]], <4 x float> %[[A:.*]], <4 x float> %[[B:.*]]
// LLVM: ret <4 x float> %[[RESULT]]
+uint4 vec_ternary_u4(int4 c, uint4 a, uint4 b) {
+ return c ? a : b;
+}
+
+// CIR: cir.func {{.*}} @vec_ternary_u4
+// CIR: %[[C_CMP:.*]] = cir.vec.cmp(lt, %{{.*}}, %{{.*}}) : !cir.vector<4 x !s32i>, !cir.vector<4 x !s32i>
+// CIR: %[[C_CMP_NOT:.*]] = cir.not %[[C_CMP]] : !cir.vector<4 x !s32i>
+// CIR: %[[TMP_B_VI4:.*]] = cir.cast bitcast %{{.*}} : !cir.vector<4 x !u32i> -> !cir.vector<4 x !s32i>
+// CIR: %[[TMP_A_VI4:.*]] = cir.cast bitcast %{{.*}} : !cir.vector<4 x !u32i> -> !cir.vector<4 x !s32i>
+// CIR: %[[TMP_1:.*]] = cir.and %[[TMP_B_VI4]], %[[C_CMP_NOT]] : !cir.vector<4 x !s32i>
+// CIR: %[[TMP_2:.*]] = cir.and %[[TMP_A_VI4]], %[[C_CMP]] : !cir.vector<4 x !s32i>
+// CIR: %[[TMP_1_1_OR:.*]] = cir.or %[[TMP_1]], %[[TMP_2]] : !cir.vector<4 x !s32i>
+// CIR: %{{.*}} = cir.cast bitcast %[[TMP_1_1_OR]] : !cir.vector<4 x !s32i> -> !cir.vector<4 x !u32i>
+
+// LLVM: %[[IS_NEG:.*]] = icmp slt <4 x i32> %[[COND:.*]], zeroinitializer
+// LLVM: %[[RESULT:.*]] = select <4 x i1> %[[IS_NEG]], <4 x i32> %[[A:.*]], <4 x i32> %[[B:.*]]
+// LLVM: ret <4 x i32> %[[RESULT]]
+
+uint4 vec_ternary_ucond(uint4 c, uint4 a, uint4 b) {
+ return c ? a : b;
+}
+
+// CIR: cir.func {{.*}} @vec_ternary_ucond
+// CIR: %[[C_S:.*]] = cir.cast bitcast %{{.*}} : !cir.vector<4 x !u32i> -> !cir.vector<4 x !s32i>
+// CIR: %{{.*}} = cir.vec.cmp(lt, %[[C_S]], %{{.*}}) : !cir.vector<4 x !s32i>, !cir.vector<4 x !s32i>
+
+// LLVM: %[[IS_NEG:.*]] = icmp slt <4 x i32> %[[COND:.*]], zeroinitializer
+// LLVM: %[[RESULT:.*]] = select <4 x i1> %[[IS_NEG]], <4 x i32> %[[A:.*]], <4 x i32> %[[B:.*]]
+// LLVM: ret <4 x i32> %[[RESULT]]
+
int4 vec_unary_inc(int4 a) {
++a;
return a;
More information about the cfe-commits
mailing list