Author: Henrich Lauko
Date: 2026-11-27T13:22:31+01:00
New Revision: f2cf1ce7c63b8c2b28e704fb277dc68949a7271b

URL: 
https://github.com/llvm/llvm-project/commit/f2cf1ce7c63b8c2b28e704fb277dc68949a7271b
DIFF: 
https://github.com/llvm/llvm-project/commit/f2cf1ce7c63b8c2b28e704fb277dc68949a7271b.diff

LOG: [CIR] Lower complex constants through CIRAttrToValue (#246485)

The complex branch of the `cir.const` lowering rebuilt the same
`llvm.mlir.constant` that
`CIRAttrToValue::visitCirAttr(ConstComplexAttr)` already produces. It
now delegates to the visitor, keeping only the zero case, which lowers
to a `[0, 0]` constant rather than `llvm.mlir.zero`. It also drops
`UndefAttr` checks in the array, record and complex branches that the
early undef return makes unreachable.

Assisted-by: Claude Code (Opus 5.5).

Added: 
    clang/test/CIR/Lowering/const-undef-aggregate.cir

Modified: 
    clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp 
b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 245ce5096d461a..00799c5d1e2a7a 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -2966,7 +2966,7 @@ mlir::LogicalResult 
CIRToLLVMConstantOpLowering::matchAndRewrite(
     // Unlike a global initializer, the constant must keep its type.
     mlir::Attribute value = spellOutSplitTrailingZeros(op.getValue());
     const auto constArr = mlir::dyn_cast<cir::ConstArrayAttr>(value);
-    if (!constArr && !isa<cir::ZeroAttr, cir::UndefAttr>(value))
+    if (!constArr && !isa<cir::ZeroAttr>(value))
       return op.emitError() << "array does not have a constant initializer";
 
     std::optional<mlir::Attribute> denseAttr;
@@ -3001,7 +3001,7 @@ mlir::LogicalResult 
CIRToLLVMConstantOpLowering::matchAndRewrite(
     rewriter.replaceOp(op, initVal);
     return mlir::success();
   } else if (mlir::isa<cir::RecordType>(op.getType())) {
-    if (mlir::isa<cir::ZeroAttr, cir::UndefAttr>(attr)) {
+    if (mlir::isa<cir::ZeroAttr>(attr)) {
       mlir::Value initVal =
           lowerCirAttrAsValue(op->getLoc(), moduleOp, attr, rewriter,
                               symbolTables, typeConverter, preConversionTypes);
@@ -3013,43 +3013,19 @@ mlir::LogicalResult 
CIRToLLVMConstantOpLowering::matchAndRewrite(
     return op.emitError() << "unsupported lowering for record constant type "
                           << op.getType();
   } else if (auto complexTy = mlir::dyn_cast<cir::ComplexType>(op.getType())) {
-    mlir::Type complexElemTy = complexTy.getElementType();
-    mlir::Type complexElemLLVMTy = typeConverter->convertType(complexElemTy);
-
-    if (auto zeroInitAttr = mlir::dyn_cast<cir::ZeroAttr>(op.getValue())) {
-      mlir::TypedAttr zeroAttr = rewriter.getZeroAttr(complexElemLLVMTy);
-      mlir::ArrayAttr array = rewriter.getArrayAttr({zeroAttr, zeroAttr});
-      rewriter.replaceOpWithNewOp<mlir::LLVM::ConstantOp>(
-          op, getTypeConverter()->convertType(op.getType()), array);
-      return mlir::success();
-    }
-
-    if (mlir::isa<cir::UndefAttr>(op.getValue())) {
-      rewriter.replaceOpWithNewOp<mlir::LLVM::UndefOp>(
-          op, getTypeConverter()->convertType(op.getType()));
-      return mlir::success();
-    }
-
-    auto complexAttr = mlir::cast<cir::ConstComplexAttr>(op.getValue());
-
-    mlir::Attribute components[2];
-    if (mlir::isa<cir::IntType>(complexElemTy)) {
-      components[0] = rewriter.getIntegerAttr(
-          complexElemLLVMTy,
-          mlir::cast<cir::IntAttr>(complexAttr.getReal()).getValue());
-      components[1] = rewriter.getIntegerAttr(
-          complexElemLLVMTy,
-          mlir::cast<cir::IntAttr>(complexAttr.getImag()).getValue());
+    if (mlir::isa<cir::ZeroAttr>(op.getValue())) {
+      mlir::TypedAttr zeroAttr = rewriter.getZeroAttr(
+          typeConverter->convertType(complexTy.getElementType()));
+      attr = rewriter.getArrayAttr({zeroAttr, zeroAttr});
     } else {
-      components[0] = rewriter.getFloatAttr(
-          complexElemLLVMTy,
-          mlir::cast<cir::FPAttr>(complexAttr.getReal()).getValue());
-      components[1] = rewriter.getFloatAttr(
-          complexElemLLVMTy,
-          mlir::cast<cir::FPAttr>(complexAttr.getImag()).getValue());
+      mlir::Value initVal =
+          lowerCirAttrAsValue(op->getLoc(), moduleOp, op.getValue(), rewriter,
+                              symbolTables, typeConverter, preConversionTypes);
+      if (!initVal)
+        return mlir::failure();
+      rewriter.replaceOp(op, initVal);
+      return mlir::success();
     }
-
-    attr = rewriter.getArrayAttr(components);
   } else {
     return op.emitError() << "unsupported constant type " << op.getType();
   }

diff  --git a/clang/test/CIR/Lowering/const-undef-aggregate.cir 
b/clang/test/CIR/Lowering/const-undef-aggregate.cir
new file mode 100644
index 00000000000000..a1eef096d4076d
--- /dev/null
+++ b/clang/test/CIR/Lowering/const-undef-aggregate.cir
@@ -0,0 +1,31 @@
+// RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s
+
+// An undef or poison aggregate constant lowers to llvm.mlir.undef or
+// llvm.mlir.poison of the whole type, before the per-type lowering.
+
+!s32i = !cir.int<s, 32>
+!rec_S = !cir.struct<"S" {data !s32i, data !cir.float}>
+
+module {
+  // CHECK-LABEL: llvm.func @undef_aggregates
+  cir.func @undef_aggregates() {
+    // CHECK: llvm.mlir.undef : !llvm.array<4 x i32>
+    %0 = cir.const #cir.undef : !cir.array<!s32i x 4>
+    // CHECK: llvm.mlir.undef : !llvm.struct<"struct.S", (i32, f32)>
+    %1 = cir.const #cir.undef : !rec_S
+    // CHECK: llvm.mlir.undef : !llvm.struct<(f32, f32)>
+    %2 = cir.const #cir.undef : !cir.complex<!cir.float>
+    cir.return
+  }
+
+  // CHECK-LABEL: llvm.func @poison_aggregates
+  cir.func @poison_aggregates() {
+    // CHECK: llvm.mlir.poison : !llvm.array<4 x i32>
+    %0 = cir.const #cir.poison : !cir.array<!s32i x 4>
+    // CHECK: llvm.mlir.poison : !llvm.struct<"struct.S", (i32, f32)>
+    %1 = cir.const #cir.poison : !rec_S
+    // CHECK: llvm.mlir.poison : !llvm.struct<(f32, f32)>
+    %2 = cir.const #cir.poison : !cir.complex<!cir.float>
+    cir.return
+  }
+}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to