[clang] [CIR] Fix crashes in the complex constant folders (PR #246012)

via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 4 05:47:31 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Henrich Lauko (xlauko)

<details>
<summary>Changes</summary>

The folders of `cir.complex.real`, `cir.complex.imag` and `cir.complex.create` asserted on constants that the `cir.const` verifier accepts.

- `cir.complex.real` and `cir.complex.imag` cast a constant operand to `#cir.const_complex`, but a complex constant can also be `#cir.zero`, `#cir.undef` or `#cir.poison`. CIRGen emits `#cir.zero` for a value-initialized complex, so clang crashed on `__real__ CI()`. A part of `#cir.zero` now folds to the zero of the element type, and a part of `#cir.undef` or `#cir.poison` folds to undef or poison, as the cast folder does for its operand.
- `cir.complex.create` built a `#cir.const_complex` from any two constant parts, whose verifier accepts only int or float literals. A create with an undef, poison or global-view part is now left alone.


Assisted-by: Claude Code (Fable 5.2).



---
Full diff: https://github.com/llvm/llvm-project/pull/246012.diff


5 Files Affected:

- (modified) clang/lib/CIR/Dialect/IR/CIRDialect.cpp (+55-23) 
- (added) clang/test/CIR/CodeGen/complex-part-of-zero.cpp (+32) 
- (modified) clang/test/CIR/Transforms/complex-create-fold.cir (+60) 
- (modified) clang/test/CIR/Transforms/complex-imag-fold.cir (+72) 
- (modified) clang/test/CIR/Transforms/complex-real-fold.cir (+72) 


``````````diff
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 28bcda81bc3db6..c23a548fe130b3 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -1373,15 +1373,21 @@ static Value tryFoldCastChain(cir::CastOp op) {
   return {};
 }
 
-OpFoldResult cir::CastOp::fold(FoldAdaptor adaptor) {
-  if (mlir::isa_and_present<cir::PoisonAttr>(adaptor.getSrc())) {
-    // Propagate poison value
-    return cir::PoisonAttr::get(getContext(), getType());
-  }
+/// Returns `#cir.undef` or `#cir.poison` of type \p type when \p attr is that
+/// constant, and null otherwise.
+static mlir::Attribute propagateUndefOrPoison(mlir::Attribute attr,
+                                              mlir::Type type) {
+  if (mlir::isa_and_present<cir::UndefAttr>(attr))
+    return cir::UndefAttr::get(type);
+  if (mlir::isa_and_present<cir::PoisonAttr>(attr))
+    return cir::PoisonAttr::get(type);
+  return {};
+}
 
-  // Propagate Undef value
-  if (mlir::isa_and_present<cir::UndefAttr>(adaptor.getSrc()))
-    return cir::UndefAttr::get(getType());
+OpFoldResult cir::CastOp::fold(FoldAdaptor adaptor) {
+  if (mlir::Attribute result =
+          propagateUndefOrPoison(adaptor.getSrc(), getType()))
+    return result;
 
   if (getSrc().getType() == getType()) {
     switch (getKind()) {
@@ -4926,11 +4932,13 @@ LogicalResult cir::ComplexCreateOp::verify() {
 OpFoldResult cir::ComplexCreateOp::fold(FoldAdaptor adaptor) {
   mlir::Attribute real = adaptor.getReal();
   mlir::Attribute imag = adaptor.getImag();
-  if (!real || !imag)
+  // The operation folds into a `#cir.const_complex`, which holds int or float
+  // literals only. Any other constant part, such as undef or poison, leaves
+  // the operation in place.
+  constexpr auto isLiteral = llvm::IsaAndPresentPred<cir::IntAttr, cir::FPAttr>;
+  if (!isLiteral(real) || !isLiteral(imag))
     return {};
 
-  // When both of real and imag are constants, we can fold the operation into an
-  // `#cir.const_complex` operation.
   auto realAttr = mlir::cast<mlir::TypedAttr>(real);
   auto imagAttr = mlir::cast<mlir::TypedAttr>(imag);
   return cir::ConstComplexAttr::get(realAttr, imagAttr);
@@ -4940,6 +4948,40 @@ OpFoldResult cir::ComplexCreateOp::fold(FoldAdaptor adaptor) {
 // ComplexRealOp
 //===----------------------------------------------------------------------===//
 
+namespace {
+/// The real and imaginary parts of a complex value, as far as a folder knows
+/// them. An unknown part is null.
+struct ComplexParts {
+  mlir::OpFoldResult real;
+  mlir::OpFoldResult imag;
+};
+} // namespace
+
+/// Returns the parts of \p complex, whose constant value is \p complexAttr (if
+/// any), as values or attributes of type \p partTy.
+static ComplexParts getComplexParts(mlir::Value complex,
+                                    mlir::Attribute complexAttr,
+                                    mlir::Type partTy) {
+  if (auto create = complex.getDefiningOp<cir::ComplexCreateOp>())
+    return {create.getReal(), create.getImag()};
+  if (!complexAttr)
+    return {};
+  if (auto constant = mlir::dyn_cast<cir::ConstComplexAttr>(complexAttr))
+    return {constant.getReal(), constant.getImag()};
+
+  // `#cir.zero`, `#cir.undef` and `#cir.poison` give both parts the same value.
+  mlir::Attribute part;
+  if (mlir::isa<cir::ZeroAttr>(complexAttr)) {
+    if (mlir::isa<cir::IntType>(partTy))
+      part = cir::IntAttr::get(partTy, 0);
+    else
+      part = cir::FPAttr::getZero(partTy);
+  } else {
+    part = propagateUndefOrPoison(complexAttr, partTy);
+  }
+  return {part, part};
+}
+
 LogicalResult cir::ComplexRealOp::verify() {
   mlir::Type operandTy = getOperand().getType();
   if (auto complexOperandTy = mlir::dyn_cast<cir::ComplexType>(operandTy))
@@ -4957,12 +4999,7 @@ OpFoldResult cir::ComplexRealOp::fold(FoldAdaptor adaptor) {
   if (!mlir::isa<cir::ComplexType>(getOperand().getType()))
     return nullptr;
 
-  if (auto complexCreateOp = getOperand().getDefiningOp<cir::ComplexCreateOp>())
-    return complexCreateOp.getOperand(0);
-
-  auto complex =
-      mlir::cast_if_present<cir::ConstComplexAttr>(adaptor.getOperand());
-  return complex ? complex.getReal() : nullptr;
+  return getComplexParts(getOperand(), adaptor.getOperand(), getType()).real;
 }
 
 //===----------------------------------------------------------------------===//
@@ -4986,12 +5023,7 @@ OpFoldResult cir::ComplexImagOp::fold(FoldAdaptor adaptor) {
   if (!mlir::isa<cir::ComplexType>(getOperand().getType()))
     return nullptr;
 
-  if (auto complexCreateOp = getOperand().getDefiningOp<cir::ComplexCreateOp>())
-    return complexCreateOp.getOperand(1);
-
-  auto complex =
-      mlir::cast_if_present<cir::ConstComplexAttr>(adaptor.getOperand());
-  return complex ? complex.getImag() : nullptr;
+  return getComplexParts(getOperand(), adaptor.getOperand(), getType()).imag;
 }
 
 //===----------------------------------------------------------------------===//
diff --git a/clang/test/CIR/CodeGen/complex-part-of-zero.cpp b/clang/test/CIR/CodeGen/complex-part-of-zero.cpp
new file mode 100644
index 00000000000000..6c3af272fc5027
--- /dev/null
+++ b/clang/test/CIR/CodeGen/complex-part-of-zero.cpp
@@ -0,0 +1,32 @@
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
+// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM
+
+// A value-initialized complex is `#cir.zero`, and its parts fold to zero.
+
+int real_of_zero_int_complex() {
+  using IntComplex = int _Complex;
+  return __real__ IntComplex();
+}
+
+// CIR-LABEL: cir.func {{.*}}@_Z24real_of_zero_int_complexv
+// CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i
+// CIR: cir.store %[[ZERO]], {{.*}} : !s32i, !cir.ptr<!s32i>
+
+// LLVM-LABEL: define {{.*}}i32 @_Z24real_of_zero_int_complexv
+// LLVM: ret i32 0
+
+float imag_of_zero_float_complex() {
+  using FloatComplex = float _Complex;
+  return __imag__ FloatComplex();
+}
+
+// CIR-LABEL: cir.func {{.*}}@_Z26imag_of_zero_float_complexv
+// CIR: %[[ZERO:.*]] = cir.const #cir.fp<0.000000e+00> : !cir.float
+// CIR: cir.store %[[ZERO]], {{.*}} : !cir.float, !cir.ptr<!cir.float>
+
+// LLVM-LABEL: define {{.*}}float @_Z26imag_of_zero_float_complexv
+// LLVM: ret float 0.000000e+00
diff --git a/clang/test/CIR/Transforms/complex-create-fold.cir b/clang/test/CIR/Transforms/complex-create-fold.cir
index 963ef464a76954..75d67b1d887e8e 100644
--- a/clang/test/CIR/Transforms/complex-create-fold.cir
+++ b/clang/test/CIR/Transforms/complex-create-fold.cir
@@ -1,6 +1,7 @@
 // RUN: cir-opt %s -cir-canonicalize -o - | FileCheck %s
 
 !s32i = !cir.int<s, 32>
+!u64i = !cir.int<u, 64>
 
 module {
   cir.func @fold_complex_create_test() -> !cir.complex<!s32i>  {
@@ -25,6 +26,65 @@ module {
 // CHECK:   cir.store{{.*}} %[[TMP:.*]], %[[RET]] : !cir.complex<!s32i>, !cir.ptr<!cir.complex<!s32i>>
 // CHECK:   %[[TMP_2:.*]] = cir.load %[[RET]] : !cir.ptr<!cir.complex<!s32i>>, !cir.complex<!s32i>
 // CHECK:   cir.return %[[TMP_2]] : !cir.complex<!s32i>
+// CHECK: }
+
+  cir.func @fold_complex_create_float_test() -> !cir.complex<!cir.float> {
+    %0 = cir.const #cir.fp<1.000000e+00> : !cir.float
+    %1 = cir.const #cir.fp<2.000000e+00> : !cir.float
+    %2 = cir.complex.create %0, %1 : !cir.float -> !cir.complex<!cir.float>
+    cir.return %2 : !cir.complex<!cir.float>
+  }
+
+// CHECK: cir.func{{.*}} @fold_complex_create_float_test() -> !cir.complex<!cir.float> {
+// CHECK-NEXT:   %[[COMPLEX:.*]] = cir.const #cir.const_complex<#cir.fp<1.000000e+00> : !cir.float, #cir.fp<2.000000e+00> : !cir.float> : !cir.complex<!cir.float>
+// CHECK-NEXT:   cir.return %[[COMPLEX]] : !cir.complex<!cir.float>
+// CHECK: }
+
+  // A `#cir.const_complex` holds int or float literals only, so a constant
+  // part of any other kind is not folded.
+
+  cir.func @no_fold_complex_create_undef_part_test() -> !cir.complex<!s32i> {
+    %0 = cir.const #cir.int<1> : !s32i
+    %1 = cir.const #cir.undef : !s32i
+    %2 = cir.complex.create %0, %1 : !s32i -> !cir.complex<!s32i>
+    cir.return %2 : !cir.complex<!s32i>
+  }
+
+// CHECK: cir.func{{.*}} @no_fold_complex_create_undef_part_test() -> !cir.complex<!s32i> {
+// CHECK-NEXT:   %[[REAL:.*]] = cir.const #cir.int<1> : !s32i
+// CHECK-NEXT:   %[[IMAG:.*]] = cir.const #cir.undef : !s32i
+// CHECK-NEXT:   %[[COMPLEX:.*]] = cir.complex.create %[[REAL]], %[[IMAG]] : !s32i -> !cir.complex<!s32i>
+// CHECK-NEXT:   cir.return %[[COMPLEX]] : !cir.complex<!s32i>
+// CHECK: }
+
+  cir.func @no_fold_complex_create_poison_part_test() -> !cir.complex<!cir.float> {
+    %0 = cir.const #cir.poison : !cir.float
+    %1 = cir.const #cir.fp<1.000000e+00> : !cir.float
+    %2 = cir.complex.create %0, %1 : !cir.float -> !cir.complex<!cir.float>
+    cir.return %2 : !cir.complex<!cir.float>
+  }
+
+// CHECK: cir.func{{.*}} @no_fold_complex_create_poison_part_test() -> !cir.complex<!cir.float> {
+// CHECK-NEXT:   %[[REAL:.*]] = cir.const #cir.poison : !cir.float
+// CHECK-NEXT:   %[[IMAG:.*]] = cir.const #cir.fp<1.000000e+00> : !cir.float
+// CHECK-NEXT:   %[[COMPLEX:.*]] = cir.complex.create %[[REAL]], %[[IMAG]] : !cir.float -> !cir.complex<!cir.float>
+// CHECK-NEXT:   cir.return %[[COMPLEX]] : !cir.complex<!cir.float>
+// CHECK: }
+
+  cir.global external @g = #cir.int<0> : !s32i
+
+  cir.func @no_fold_complex_create_global_view_part_test() -> !cir.complex<!u64i> {
+    %0 = cir.const #cir.global_view<@g> : !u64i
+    %1 = cir.const #cir.int<1> : !u64i
+    %2 = cir.complex.create %0, %1 : !u64i -> !cir.complex<!u64i>
+    cir.return %2 : !cir.complex<!u64i>
+  }
+
+// CHECK: cir.func{{.*}} @no_fold_complex_create_global_view_part_test() -> !cir.complex<!u64i> {
+// CHECK-NEXT:   %[[REAL:.*]] = cir.const #cir.global_view<@g> : !u64i
+// CHECK-NEXT:   %[[IMAG:.*]] = cir.const #cir.int<1> : !u64i
+// CHECK-NEXT:   %[[COMPLEX:.*]] = cir.complex.create %[[REAL]], %[[IMAG]] : !u64i -> !cir.complex<!u64i>
+// CHECK-NEXT:   cir.return %[[COMPLEX]] : !cir.complex<!u64i>
 // CHECK: }
 
 }
diff --git a/clang/test/CIR/Transforms/complex-imag-fold.cir b/clang/test/CIR/Transforms/complex-imag-fold.cir
index e0e8d900544921..5143cc33f8bd9b 100644
--- a/clang/test/CIR/Transforms/complex-imag-fold.cir
+++ b/clang/test/CIR/Transforms/complex-imag-fold.cir
@@ -37,3 +37,75 @@ module {
   // CHECK:   cir.return %[[ARG_1]] : !s32i
   // CHECK: }
 }
+
+// -----
+
+// Both parts of a `#cir.zero` complex constant are zero.
+
+!s32i = !cir.int<s, 32>
+
+module {
+  cir.func @fold_complex_imag_of_zero_int_test() -> !s32i {
+    %0 = cir.const #cir.zero : !cir.complex<!s32i>
+    %1 = cir.complex.imag %0 : !cir.complex<!s32i> -> !s32i
+    cir.return %1 : !s32i
+  }
+
+  // CHECK: cir.func @fold_complex_imag_of_zero_int_test() -> !s32i {
+  // CHECK-NEXT:   %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i
+  // CHECK-NEXT:   cir.return %[[ZERO]] : !s32i
+  // CHECK: }
+
+  cir.func @fold_complex_imag_of_zero_float_test() -> !cir.float {
+    %0 = cir.const #cir.zero : !cir.complex<!cir.float>
+    %1 = cir.complex.imag %0 : !cir.complex<!cir.float> -> !cir.float
+    cir.return %1 : !cir.float
+  }
+
+  // CHECK: cir.func @fold_complex_imag_of_zero_float_test() -> !cir.float {
+  // CHECK-NEXT:   %[[ZERO:.*]] = cir.const #cir.fp<0.000000e+00> : !cir.float
+  // CHECK-NEXT:   cir.return %[[ZERO]] : !cir.float
+  // CHECK: }
+
+  cir.func @fold_complex_imag_of_zero_long_double_test() -> !cir.long_double<!cir.f80> {
+    %0 = cir.const #cir.zero : !cir.complex<!cir.long_double<!cir.f80>>
+    %1 = cir.complex.imag %0 : !cir.complex<!cir.long_double<!cir.f80>> -> !cir.long_double<!cir.f80>
+    cir.return %1 : !cir.long_double<!cir.f80>
+  }
+
+  // CHECK: cir.func @fold_complex_imag_of_zero_long_double_test() -> !cir.long_double<!cir.f80> {
+  // CHECK-NEXT:   %[[ZERO:.*]] = cir.const #cir.fp<0.000000e+00> : !cir.long_double<!cir.f80>
+  // CHECK-NEXT:   cir.return %[[ZERO]] : !cir.long_double<!cir.f80>
+  // CHECK: }
+}
+
+// -----
+
+// A part of a `#cir.undef` or `#cir.poison` complex constant is undef or
+// poison.
+
+!s32i = !cir.int<s, 32>
+
+module {
+  cir.func @fold_complex_imag_of_undef_test() -> !s32i {
+    %0 = cir.const #cir.undef : !cir.complex<!s32i>
+    %1 = cir.complex.imag %0 : !cir.complex<!s32i> -> !s32i
+    cir.return %1 : !s32i
+  }
+
+  // CHECK: cir.func @fold_complex_imag_of_undef_test() -> !s32i {
+  // CHECK-NEXT:   %[[PART:.*]] = cir.const #cir.undef : !s32i
+  // CHECK-NEXT:   cir.return %[[PART]] : !s32i
+  // CHECK: }
+
+  cir.func @fold_complex_imag_of_poison_test() -> !s32i {
+    %0 = cir.const #cir.poison : !cir.complex<!s32i>
+    %1 = cir.complex.imag %0 : !cir.complex<!s32i> -> !s32i
+    cir.return %1 : !s32i
+  }
+
+  // CHECK: cir.func @fold_complex_imag_of_poison_test() -> !s32i {
+  // CHECK-NEXT:   %[[PART:.*]] = cir.const #cir.poison : !s32i
+  // CHECK-NEXT:   cir.return %[[PART]] : !s32i
+  // CHECK: }
+}
diff --git a/clang/test/CIR/Transforms/complex-real-fold.cir b/clang/test/CIR/Transforms/complex-real-fold.cir
index dad9c00cfdb086..64b61fc75254f9 100644
--- a/clang/test/CIR/Transforms/complex-real-fold.cir
+++ b/clang/test/CIR/Transforms/complex-real-fold.cir
@@ -37,3 +37,75 @@ module {
   // CHECK:   cir.return %[[ARG_0]] : !s32i
   // CHECK: }
 }
+
+// -----
+
+// Both parts of a `#cir.zero` complex constant are zero.
+
+!s32i = !cir.int<s, 32>
+
+module {
+  cir.func @fold_complex_real_of_zero_int_test() -> !s32i {
+    %0 = cir.const #cir.zero : !cir.complex<!s32i>
+    %1 = cir.complex.real %0 : !cir.complex<!s32i> -> !s32i
+    cir.return %1 : !s32i
+  }
+
+  // CHECK: cir.func @fold_complex_real_of_zero_int_test() -> !s32i {
+  // CHECK-NEXT:   %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i
+  // CHECK-NEXT:   cir.return %[[ZERO]] : !s32i
+  // CHECK: }
+
+  cir.func @fold_complex_real_of_zero_float_test() -> !cir.float {
+    %0 = cir.const #cir.zero : !cir.complex<!cir.float>
+    %1 = cir.complex.real %0 : !cir.complex<!cir.float> -> !cir.float
+    cir.return %1 : !cir.float
+  }
+
+  // CHECK: cir.func @fold_complex_real_of_zero_float_test() -> !cir.float {
+  // CHECK-NEXT:   %[[ZERO:.*]] = cir.const #cir.fp<0.000000e+00> : !cir.float
+  // CHECK-NEXT:   cir.return %[[ZERO]] : !cir.float
+  // CHECK: }
+
+  cir.func @fold_complex_real_of_zero_long_double_test() -> !cir.long_double<!cir.f80> {
+    %0 = cir.const #cir.zero : !cir.complex<!cir.long_double<!cir.f80>>
+    %1 = cir.complex.real %0 : !cir.complex<!cir.long_double<!cir.f80>> -> !cir.long_double<!cir.f80>
+    cir.return %1 : !cir.long_double<!cir.f80>
+  }
+
+  // CHECK: cir.func @fold_complex_real_of_zero_long_double_test() -> !cir.long_double<!cir.f80> {
+  // CHECK-NEXT:   %[[ZERO:.*]] = cir.const #cir.fp<0.000000e+00> : !cir.long_double<!cir.f80>
+  // CHECK-NEXT:   cir.return %[[ZERO]] : !cir.long_double<!cir.f80>
+  // CHECK: }
+}
+
+// -----
+
+// A part of a `#cir.undef` or `#cir.poison` complex constant is undef or
+// poison.
+
+!s32i = !cir.int<s, 32>
+
+module {
+  cir.func @fold_complex_real_of_undef_test() -> !s32i {
+    %0 = cir.const #cir.undef : !cir.complex<!s32i>
+    %1 = cir.complex.real %0 : !cir.complex<!s32i> -> !s32i
+    cir.return %1 : !s32i
+  }
+
+  // CHECK: cir.func @fold_complex_real_of_undef_test() -> !s32i {
+  // CHECK-NEXT:   %[[PART:.*]] = cir.const #cir.undef : !s32i
+  // CHECK-NEXT:   cir.return %[[PART]] : !s32i
+  // CHECK: }
+
+  cir.func @fold_complex_real_of_poison_test() -> !s32i {
+    %0 = cir.const #cir.poison : !cir.complex<!s32i>
+    %1 = cir.complex.real %0 : !cir.complex<!s32i> -> !s32i
+    cir.return %1 : !s32i
+  }
+
+  // CHECK: cir.func @fold_complex_real_of_poison_test() -> !s32i {
+  // CHECK-NEXT:   %[[PART:.*]] = cir.const #cir.poison : !s32i
+  // CHECK-NEXT:   cir.return %[[PART]] : !s32i
+  // CHECK: }
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/246012


More information about the cfe-commits mailing list