[clang] [CIR] Lower complex constants through CIRAttrToValue (PR #246485)

Henrich Lauko via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 26 12:31:14 PST 2026


https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/246485

>From be13150170fb02c827dd42f57e0dd6bfd46fbef0 Mon Sep 17 00:00:00 2001
From: Henrich Lauko <hlauko at nvidia.com>
Date: Thu, 26 Nov 2026 15:58:16 +0000
Subject: [PATCH 1/2] [CIR] Lower complex constants through CIRAttrToValue

The complex branch of the cir.const lowering rebuilt the same
llvm.mlir.constant as CIRAttrToValue::visitCirAttr(ConstComplexAttr).
Delegate to it, and drop UndefAttr checks that the earlier undef
case already makes unreachable. No output change.

Assisted-by: Claude Code (Opus 5.5).
---
 .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 50 +++++--------------
 1 file changed, 13 insertions(+), 37 deletions(-)

diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 245ce5096d461..00799c5d1e2a7 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();
   }

>From db3f3be1f12c3f4dd2b28612fdf413d4ae89bcd0 Mon Sep 17 00:00:00 2001
From: Henrich Lauko <hlauko at nvidia.com>
Date: Thu, 26 Nov 2026 20:31:01 +0000
Subject: [PATCH 2/2] [CIR] Test lowering of undef and poison aggregate
 constants

An undef or poison array, record or complex constant lowers to
llvm.mlir.undef or llvm.mlir.poison of the whole type before the
per-type lowering, so the removed isa checks never matched.

Assisted-by: Claude Code (Opus 5.5).
---
 .../CIR/Lowering/const-undef-aggregate.cir    | 31 +++++++++++++++++++
 1 file changed, 31 insertions(+)
 create mode 100644 clang/test/CIR/Lowering/const-undef-aggregate.cir

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 0000000000000..a1eef096d4076
--- /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
+  }
+}



More information about the cfe-commits mailing list