[clang] [CIR] Implement AddOp for ComplexType (PR #147578)
Henrich Lauko via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 9 23:29:16 PDT 2025
================
@@ -2357,6 +2358,56 @@ mlir::LogicalResult CIRToLLVMVecTernaryOpLowering::matchAndRewrite(
return mlir::success();
}
+mlir::LogicalResult CIRToLLVMComplexAddOpLowering::matchAndRewrite(
+ cir::ComplexAddOp op, OpAdaptor adaptor,
+ mlir::ConversionPatternRewriter &rewriter) const {
+ mlir::Value lhs = adaptor.getLhs();
+ mlir::Value rhs = adaptor.getRhs();
+ mlir::Location loc = op.getLoc();
+
+ auto complexType = mlir::cast<cir::ComplexType>(op.getLhs().getType());
+ mlir::Type complexElemTy =
+ getTypeConverter()->convertType(complexType.getElementType());
+ auto lhsReal =
+ rewriter.create<mlir::LLVM::ExtractValueOp>(loc, complexElemTy, lhs, 0);
+ auto lhsImag =
+ rewriter.create<mlir::LLVM::ExtractValueOp>(loc, complexElemTy, lhs, 1);
+ auto rhsReal =
+ rewriter.create<mlir::LLVM::ExtractValueOp>(loc, complexElemTy, rhs, 0);
+ auto rhsImag =
+ rewriter.create<mlir::LLVM::ExtractValueOp>(loc, complexElemTy, rhs, 1);
+
+ mlir::Value newReal;
+ mlir::Value newImag;
+ if (complexElemTy.isInteger()) {
+ newReal = rewriter.create<mlir::LLVM::AddOp>(loc, complexElemTy, lhsReal,
+ rhsReal);
+ newImag = rewriter.create<mlir::LLVM::AddOp>(loc, complexElemTy, lhsImag,
+ rhsImag);
+ } else {
+ assert(!cir::MissingFeatures::fastMathFlags());
+ assert(!cir::MissingFeatures::fpConstraints());
+ newReal = rewriter.create<mlir::LLVM::FAddOp>(loc, complexElemTy, lhsReal,
+ rhsReal);
+ newImag = rewriter.create<mlir::LLVM::FAddOp>(loc, complexElemTy, lhsImag,
+ rhsImag);
+ }
+
+ mlir::Type complexLLVMTy =
+ getTypeConverter()->convertType(op.getResult().getType());
+ auto initialComplex =
+ rewriter.create<mlir::LLVM::PoisonOp>(op->getLoc(), complexLLVMTy);
+
+ auto realComplex = rewriter.create<mlir::LLVM::InsertValueOp>(
+ op->getLoc(), initialComplex, newReal, 0);
+
+ auto complex = rewriter.create<mlir::LLVM::InsertValueOp>(
+ op->getLoc(), realComplex, newImag, 1);
+
+ rewriter.replaceOp(op, complex);
----------------
xlauko wrote:
Use `replaceOpWithNewOp` here.
https://github.com/llvm/llvm-project/pull/147578
More information about the cfe-commits
mailing list