[clang] [CIR] Fold extractvalue when lowering complex real and imag (PR #243393)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 17 01:02:32 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clangir
Author: Henrich Lauko (xlauko)
<details>
<summary>Changes</summary>
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).
---
Full diff: https://github.com/llvm/llvm-project/pull/243393.diff
2 Files Affected:
- (modified) clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp (+4-6)
- (modified) clang/test/CIR/CodeGen/complex-compound-assignment.cpp (+3-5)
``````````diff
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 5d9340bf65204..4223467ff307d 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 ba79157f38254..ed4afa9b54cff 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
``````````
</details>
https://github.com/llvm/llvm-project/pull/243393
More information about the cfe-commits
mailing list