[clang] [CIR] Add binary operators (PR #132420)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 25 12:06:08 PDT 2025
================
@@ -994,6 +992,127 @@ mlir::LogicalResult CIRToLLVMUnaryOpLowering::matchAndRewrite(
<< elementType;
}
+mlir::LLVM::IntegerOverflowFlags
+CIRToLLVMBinOpLowering::getIntOverflowFlag(cir::BinOp op) const {
+ if (op.getNoUnsignedWrap())
+ return mlir::LLVM::IntegerOverflowFlags::nuw;
+
+ if (op.getNoSignedWrap())
+ return mlir::LLVM::IntegerOverflowFlags::nsw;
+
+ return mlir::LLVM::IntegerOverflowFlags::none;
+}
+
+static bool isIntTypeUnsigned(mlir::Type type) {
+ // TODO: Ideally, we should only need to check cir::IntType here.
+ return mlir::isa<cir::IntType>(type)
+ ? mlir::cast<cir::IntType>(type).isUnsigned()
+ : mlir::cast<mlir::IntegerType>(type).isUnsigned();
+}
+
+mlir::LogicalResult CIRToLLVMBinOpLowering::matchAndRewrite(
+ cir::BinOp op, OpAdaptor adaptor,
+ mlir::ConversionPatternRewriter &rewriter) const {
+ if (adaptor.getLhs().getType() != adaptor.getRhs().getType())
+ return op.emitError() << "inconsistent operands' types not supported yet";
+
+ mlir::Type type = op.getRhs().getType();
+ assert(!cir::MissingFeatures::vectorType());
+ if (!mlir::isa<cir::IntType, cir::BoolType, cir::CIRFPTypeInterface,
+ mlir::IntegerType>(type))
+ return op.emitError() << "operand type not supported yet";
+
+ auto llvmTy = getTypeConverter()->convertType(op.getType());
+ mlir::Type llvmEltTy =
+ mlir::isa<mlir::VectorType>(llvmTy)
+ ? mlir::cast<mlir::VectorType>(llvmTy).getElementType()
+ : llvmTy;
+ auto rhs = adaptor.getRhs();
+ auto lhs = adaptor.getLhs();
+
+ type = elementTypeIfVector(type);
+
+ switch (op.getKind()) {
+ case cir::BinOpKind::Add:
+ if (mlir::isa<mlir::IntegerType>(llvmEltTy)) {
+ if (op.getSaturated()) {
+ if (isIntTypeUnsigned(type)) {
+ rewriter.replaceOpWithNewOp<mlir::LLVM::UAddSat>(op, lhs, rhs);
+ break;
+ }
+ rewriter.replaceOpWithNewOp<mlir::LLVM::SAddSat>(op, lhs, rhs);
+ break;
+ }
+ rewriter.replaceOpWithNewOp<mlir::LLVM::AddOp>(op, llvmTy, lhs, rhs,
+ getIntOverflowFlag(op));
+ } else
----------------
andykaylor wrote:
The LLVM Coding Standard requires braces on the `else` when they are needed for the `if`. Likewise below.
https://github.com/llvm/llvm-project/pull/132420
More information about the cfe-commits
mailing list