https://github.com/xlauko created https://github.com/llvm/llvm-project/pull/243393
Lower `cir.complex.real` and `cir.complex.imag` with `createOrFold`, so the `llvm.extractvalue` folds through the `llvm.insertvalue` chain that `cir.complex.create` lowers to. Until now the fold relied on the CIR folders seeing the original `cir.complex.create`, which only works while dialect conversion keeps replaced ops around for rollback. The LLVM folder works on converted values in either mode, and it also folds extracts of complex results that never came from a `cir.complex.create`. One example is the `cir.complex.fadd` result in `complex-compound-assignment.cpp`, which now matches classic codegen. That is the only test change. Assisted-by: Claude Code (claude-opus-5-5). >From 3808618c70c9638b7c2b458aa11c60b08320ac8e Mon Sep 17 00:00:00 2001 From: Henrich Lauko <[email protected]> Date: Tue, 17 Nov 2026 08:58:58 +0000 Subject: [PATCH] [CIR] Fold extractvalue when lowering complex real and imag Lower cir.complex.real and cir.complex.imag with createOrFold, so the llvm.extractvalue folds through the llvm.insertvalue chain that cir.complex.create lowers to. Until now the fold depended on the CIR folders seeing the original cir.complex.create, which only happens while dialect conversion keeps replaced ops around for rollback. The LLVM folder works on the converted values in either mode and also catches extracts of complex results that never came from a cir.complex.create, such as the cir.complex.fadd result in complex-compound-assignment.cpp, which now matches classic codegen. Assisted-by: Claude Code (claude-opus-5-5). --- clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 10 ++++------ clang/test/CIR/CodeGen/complex-compound-assignment.cpp | 8 +++----- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 5d9340bf652044..4223467ff307d8 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -6177,12 +6177,10 @@ mlir::LogicalResult CIRToLLVMComplexCreateOpLowering::matchAndRewrite( mlir::LogicalResult CIRToLLVMComplexRealOpLowering::matchAndRewrite( cir::ComplexRealOp op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) const { - mlir::Type resultLLVMTy = getTypeConverter()->convertType(op.getType()); mlir::Value operand = adaptor.getOperand(); if (mlir::isa<cir::ComplexType>(op.getOperand().getType())) { - operand = mlir::LLVM::ExtractValueOp::create( - rewriter, op.getLoc(), resultLLVMTy, operand, - llvm::ArrayRef<std::int64_t>{0}); + operand = rewriter.createOrFold<mlir::LLVM::ExtractValueOp>( + op.getLoc(), operand, llvm::ArrayRef<std::int64_t>{0}); } rewriter.replaceOp(op, operand); return mlir::success(); @@ -6196,8 +6194,8 @@ mlir::LogicalResult CIRToLLVMComplexImagOpLowering::matchAndRewrite( mlir::Location loc = op.getLoc(); if (mlir::isa<cir::ComplexType>(op.getOperand().getType())) { - operand = mlir::LLVM::ExtractValueOp::create( - rewriter, loc, resultLLVMTy, operand, llvm::ArrayRef<std::int64_t>{1}); + operand = rewriter.createOrFold<mlir::LLVM::ExtractValueOp>( + loc, operand, llvm::ArrayRef<std::int64_t>{1}); } else { mlir::TypedAttr zeroAttr = rewriter.getZeroAttr(resultLLVMTy); operand = diff --git a/clang/test/CIR/CodeGen/complex-compound-assignment.cpp b/clang/test/CIR/CodeGen/complex-compound-assignment.cpp index ba79157f382546..ed4afa9b54cff1 100644 --- a/clang/test/CIR/CodeGen/complex-compound-assignment.cpp +++ b/clang/test/CIR/CodeGen/complex-compound-assignment.cpp @@ -195,12 +195,10 @@ void foo3() { // LLVM: %[[ADD_IMAG:.*]] = fadd float %[[B_IMAG]], %[[A_IMAG]] // LLVM: %[[TMP_RESULT:.*]] = insertvalue { float, float } poison, float %[[ADD_REAL]], 0 // LLVM: %[[RESULT:.*]] = insertvalue { float, float } %[[TMP_RESULT]], float %[[ADD_IMAG]], 1 -// LLVM: %[[RESULT_REAL:.*]] = extractvalue { float, float } %[[RESULT]], 0 -// LLVM: %[[RESULT_IMAG:.*]] = extractvalue { float, float } %[[RESULT]], 1 -// LLVM: %[[RESULT_REAL_F16:.*]] = fptrunc float %[[RESULT_REAL]] to half -// LLVM: %[[RESULT_IMAG_F16:.*]] = fptrunc float %[[RESULT_IMAG]] to half +// LLVM: %[[RESULT_REAL_F16:.*]] = fptrunc float %[[ADD_REAL]] to half +// LLVM: %[[RESULT_IMAG_F16:.*]] = fptrunc float %[[ADD_IMAG]] to half // LLVM: %[[TMP_RESULT_F16:.*]] = insertvalue { half, half } undef, half %[[RESULT_REAL_F16]], 0 -// LLVM: %[[RESULT_F16:.*]] = insertvalue { half, half } %29, half %[[RESULT_IMAG_F16]], 1 +// LLVM: %[[RESULT_F16:.*]] = insertvalue { half, half } %[[TMP_RESULT_F16]], half %[[RESULT_IMAG_F16]], 1 // LLVM: store { half, half } %[[RESULT_F16]], ptr %[[B_ADDR]], align 2 // OGCG: %[[A_ADDR:.*]] = alloca { half, half }, align 2 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
