Skip to content

[CIR] Upstream TernaryOp for VectorType #142393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -2194,4 +2194,40 @@ def VecShuffleDynamicOp : CIR_Op<"vec.shuffle.dynamic",
let hasVerifier = 1;
}

//===----------------------------------------------------------------------===//
// VecTernaryOp
//===----------------------------------------------------------------------===//

def VecTernaryOp : CIR_Op<"vec.ternary",
[Pure, AllTypesMatch<["result", "lhs", "rhs"]>]> {
let summary = "The `cond ? a : b` ternary operator for vector types";
let description = [{
The `cir.vec.ternary` operation represents the C/C++ ternary operator,
`?:`, for vector types, which does a `select` on individual elements of the
vectors. Unlike a regular `?:` operator, there is no short circuiting. All
three arguments are always evaluated. Because there is no short
circuiting, there are no regions in this operation, unlike cir.ternary.

The first argument is a vector of integral type. The second and third
arguments are vectors of the same type and have the same number of elements
as the first argument.

The result is a vector of the same type as the second and third arguments.
Each element of the result is `(bool)a[n] ? b[n] : c[n]`.
}];

let arguments = (ins
CIR_VectorOfIntType:$cond,
CIR_VectorType:$lhs,
CIR_VectorType:$rhs
);

let results = (outs CIR_VectorType:$result);
let assemblyFormat = [{
`(` $cond `,` $lhs`,` $rhs `)` `:` qualified(type($cond)) `,`
qualified(type($lhs)) attr-dict
}];
let hasVerifier = 1;
}

#endif // CLANG_CIR_DIALECT_IR_CIROPS_TD
23 changes: 16 additions & 7 deletions clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1956,19 +1956,28 @@ mlir::Value ScalarExprEmitter::VisitAbstractConditionalOperator(
}
}

QualType condType = condExpr->getType();

// OpenCL: If the condition is a vector, we can treat this condition like
// the select function.
if ((cgf.getLangOpts().OpenCL && condExpr->getType()->isVectorType()) ||
condExpr->getType()->isExtVectorType()) {
if ((cgf.getLangOpts().OpenCL && condType->isVectorType()) ||
condType->isExtVectorType()) {
assert(!cir::MissingFeatures::vectorType());
cgf.cgm.errorNYI(e->getSourceRange(), "vector ternary op");
}

if (condExpr->getType()->isVectorType() ||
condExpr->getType()->isSveVLSBuiltinType()) {
assert(!cir::MissingFeatures::vecTernaryOp());
cgf.cgm.errorNYI(e->getSourceRange(), "vector ternary op");
return {};
if (condType->isVectorType() || condType->isSveVLSBuiltinType()) {
if (!condType->isVectorType()) {
assert(!cir::MissingFeatures::vecTernaryOp());
cgf.cgm.errorNYI(loc, "TernaryOp for SVE vector");
return {};
}

mlir::Value condValue = Visit(condExpr);
mlir::Value lhsValue = Visit(lhsExpr);
mlir::Value rhsValue = Visit(rhsExpr);
return builder.create<cir::VecTernaryOp>(loc, condValue, lhsValue,
rhsValue);
}

// If this is a really simple expression (like x ? 4 : 5), emit this as a
Expand Down
17 changes: 17 additions & 0 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,23 @@ LogicalResult cir::VecShuffleDynamicOp::verify() {
return success();
}

//===----------------------------------------------------------------------===//
// VecTernaryOp
//===----------------------------------------------------------------------===//

LogicalResult cir::VecTernaryOp::verify() {
// Verify that the condition operand has the same number of elements as the
// other operands. (The automatic verification already checked that all
// operands are vector types and that the second and third operands are the
// same type.)
if (getCond().getType().getSize() != getLhs().getType().getSize()) {
return emitOpError() << ": the number of elements in "
<< getCond().getType() << " and " << getLhs().getType()
<< " don't match";
}
return success();
}

//===----------------------------------------------------------------------===//
// TableGen'd op method definitions
//===----------------------------------------------------------------------===//
Expand Down
17 changes: 16 additions & 1 deletion clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1730,7 +1730,8 @@ void ConvertCIRToLLVMPass::runOnOperation() {
CIRToLLVMVecExtractOpLowering,
CIRToLLVMVecInsertOpLowering,
CIRToLLVMVecCmpOpLowering,
CIRToLLVMVecShuffleDynamicOpLowering
CIRToLLVMVecShuffleDynamicOpLowering,
CIRToLLVMVecTernaryOpLowering
// clang-format on
>(converter, patterns.getContext());

Expand Down Expand Up @@ -1934,6 +1935,20 @@ mlir::LogicalResult CIRToLLVMVecShuffleDynamicOpLowering::matchAndRewrite(
return mlir::success();
}

mlir::LogicalResult CIRToLLVMVecTernaryOpLowering::matchAndRewrite(
cir::VecTernaryOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
// Convert `cond` into a vector of i1, then use that in a `select` op.
mlir::Value bitVec = rewriter.create<mlir::LLVM::ICmpOp>(
op.getLoc(), mlir::LLVM::ICmpPredicate::ne, adaptor.getCond(),
rewriter.create<mlir::LLVM::ZeroOp>(
op.getCond().getLoc(),
typeConverter->convertType(op.getCond().getType())));
rewriter.replaceOpWithNewOp<mlir::LLVM::SelectOp>(
op, bitVec, adaptor.getLhs(), adaptor.getRhs());
return mlir::success();
}

std::unique_ptr<mlir::Pass> createConvertCIRToLLVMPass() {
return std::make_unique<ConvertCIRToLLVMPass>();
}
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,16 @@ class CIRToLLVMVecShuffleDynamicOpLowering
mlir::ConversionPatternRewriter &) const override;
};

class CIRToLLVMVecTernaryOpLowering
: public mlir::OpConversionPattern<cir::VecTernaryOp> {
public:
using mlir::OpConversionPattern<cir::VecTernaryOp>::OpConversionPattern;

mlir::LogicalResult
matchAndRewrite(cir::VecTernaryOp op, OpAdaptor,
mlir::ConversionPatternRewriter &) const override;
};

} // namespace direct
} // namespace cir

Expand Down
57 changes: 56 additions & 1 deletion clang/test/CIR/CodeGen/vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1069,4 +1069,59 @@ void foo17() {

// OGCG: %[[VEC_A:.*]] = alloca <2 x double>, align 16
// OGCG: %[[TMP:.*]] = load <2 x double>, ptr %[[VEC_A]], align 16
// OGCG: %[[RES:.*]]= fptoui <2 x double> %[[TMP]] to <2 x i16>
// OGCG: %[[RES:.*]]= fptoui <2 x double> %[[TMP]] to <2 x i16>

void foo20() {
vi4 a;
vi4 b;
vi4 c;
vi4 r = c ? a : b;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see a test case where the operands are expressions. Something like

vi4 r = (a > b) ? (a - b) : (b - a);

}

// CIR: %[[RES:.*]] = cir.vec.ternary({{.*}}, {{.*}}, {{.*}}) : !cir.vector<4 x !s32i>, !cir.vector<4 x !s32i>

// LLVM: %[[VEC_COND:.*]] = icmp ne <4 x i32> {{.*}}, zeroinitializer
// LLVM: %[[RES:.*]] = select <4 x i1> %[[VEC_COND]], <4 x i32> {{.*}}, <4 x i32> {{.*}}

// OGCG: %[[VEC_COND:.*]] = icmp ne <4 x i32> {{.*}}, zeroinitializer
// OGCG: %[[RES:.*]] = select <4 x i1> %[[VEC_COND]], <4 x i32> {{.*}}, <4 x i32> {{.*}}

void foo21() {
vi4 a;
vi4 b;
vi4 r = (a > b) ? (a - b) : (b - a);
}

// CIR: %[[VEC_COND:.*]] = cir.vec.cmp(gt, {{.*}}, {{.*}}) : !cir.vector<4 x !s32i>, !cir.vector<4 x !s32i>
// CIR: %[[LHS:.*]] = cir.binop(sub, {{.*}}, {{.*}}) : !cir.vector<4 x !s32i>
// CIR: %[[RHS:.*]] = cir.binop(sub, {{.*}}, {{.*}}) : !cir.vector<4 x !s32i>
// CIR: %[[RES:.*]] = cir.vec.ternary(%[[VEC_COND]], %[[LHS]], %[[RHS]]) : !cir.vector<4 x !s32i>, !cir.vector<4 x !s32i>

// LLVM: %[[CMP:.*]] = icmp sgt <4 x i32> {{.*}}, {{.*}}
// LLVM: %[[SEXT:.*]] = sext <4 x i1> %[[CMP]] to <4 x i32>
// LLVM: %[[LHS:.*]] = sub <4 x i32> {{.*}}, {{.*}}
// LLVM: %[[RHS:.*]] = sub <4 x i32> {{.*}}, {{.*}}
// LLVM: %[[VEC_COND:.*]] = icmp ne <4 x i32> %[[SEXT]], zeroinitializer
// LLVM: %[[RES:.*]] = select <4 x i1> %[[VEC_COND]], <4 x i32> %[[LHS]], <4 x i32> %[[RHS]]

// OGCG: %[[CMP:.*]] = icmp sgt <4 x i32> {{.*}}, {{.*}}
// OGCG: %[[SEXT:.*]] = sext <4 x i1> %[[CMP]] to <4 x i32>
// OGCG: %[[LHS:.*]] = sub <4 x i32> {{.*}}, {{.*}}
// OGCG: %[[RHS:.*]] = sub <4 x i32> {{.*}}, {{.*}}
// OGCG: %[[VEC_COND:.*]] = icmp ne <4 x i32> %[[SEXT]], zeroinitializer
// OGCG: %[[RES:.*]] = select <4 x i1> %[[VEC_COND]], <4 x i32> %[[LHS]], <4 x i32> %[[RHS]]

void foo22() {
vf4 a;
vf4 b;
vi4 c;
vf4 r = c ? a : b;
}

// CIR: %[[RES:.*]] = cir.vec.ternary({{.*}}, {{.*}}, {{.*}}) : !cir.vector<4 x !s32i>, !cir.vector<4 x !cir.float>

// LLVM: %[[VEC_COND:.*]] = icmp ne <4 x i32> {{.*}}, zeroinitializer
// LLVM: %[[RES:.*]] = select <4 x i1> %[[VEC_COND]], <4 x float> {{.*}}, <4 x float> {{.*}}

// OGCG: %[[VEC_COND:.*]] = icmp ne <4 x i32> {{.*}}, zeroinitializer
// OGCG: %[[RES:.*]] = select <4 x i1> %[[VEC_COND]], <4 x float> {{.*}}, <4 x float> {{.*}}