[clang] [CIR] Fix crashes in the complex constant folders (PR #246012)
Henrich Lauko via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 25 08:01:48 PST 2026
https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/246012
>From bb51be7ec232fad73fcce3a729b912ea45167b0f Mon Sep 17 00:00:00 2001
From: Henrich Lauko <hlauko at nvidia.com>
Date: Wed, 25 Nov 2026 14:09:20 +0000
Subject: [PATCH] [CIR] Fix crashes in the complex constant folders
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`. 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. 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. The attribute holds int or float literals only, so its verifier
failed on a part that is undef, poison or a global view. The folder now
leaves such a create alone.
Assisted-by: Claude Code (Fable 5.2).
---
clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 40 ++++++++---
clang/test/CIR/CodeGen/complex.cpp | 46 ++++++++++++
.../CIR/Transforms/complex-create-fold.cir | 48 +++++++++++++
.../test/CIR/Transforms/complex-imag-fold.cir | 72 +++++++++++++++++++
.../test/CIR/Transforms/complex-real-fold.cir | 72 +++++++++++++++++++
5 files changed, 269 insertions(+), 9 deletions(-)
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index e81db4631eeef4..760f4bc26fe933 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -4926,11 +4926,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.
+ if (!mlir::isa_and_present<cir::IntAttr, cir::FPAttr>(real) ||
+ !mlir::isa_and_present<cir::IntAttr, cir::FPAttr>(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 +4942,24 @@ OpFoldResult cir::ComplexCreateOp::fold(FoldAdaptor adaptor) {
// ComplexRealOp
//===----------------------------------------------------------------------===//
+/// Returns the value of both parts of the complex constant \p complex when it
+/// is `#cir.zero`, `#cir.undef` or `#cir.poison`, and null otherwise.
+static mlir::Attribute getUniformComplexPart(mlir::Attribute complex,
+ mlir::Type partTy) {
+ if (!complex)
+ return {};
+ if (mlir::isa<cir::ZeroAttr>(complex)) {
+ if (mlir::isa<cir::IntType>(partTy))
+ return cir::IntAttr::get(partTy, 0);
+ return cir::FPAttr::getZero(partTy);
+ }
+ if (mlir::isa<cir::UndefAttr>(complex))
+ return cir::UndefAttr::get(partTy);
+ if (mlir::isa<cir::PoisonAttr>(complex))
+ return cir::PoisonAttr::get(partTy);
+ return {};
+}
+
LogicalResult cir::ComplexRealOp::verify() {
mlir::Type operandTy = getOperand().getType();
if (auto complexOperandTy = mlir::dyn_cast<cir::ComplexType>(operandTy))
@@ -4960,9 +4980,10 @@ OpFoldResult cir::ComplexRealOp::fold(FoldAdaptor adaptor) {
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;
+ mlir::Attribute operand = adaptor.getOperand();
+ if (auto complex = mlir::dyn_cast_if_present<cir::ConstComplexAttr>(operand))
+ return complex.getReal();
+ return getUniformComplexPart(operand, getType());
}
//===----------------------------------------------------------------------===//
@@ -4989,9 +5010,10 @@ OpFoldResult cir::ComplexImagOp::fold(FoldAdaptor adaptor) {
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;
+ mlir::Attribute operand = adaptor.getOperand();
+ if (auto complex = mlir::dyn_cast_if_present<cir::ConstComplexAttr>(operand))
+ return complex.getImag();
+ return getUniformComplexPart(operand, getType());
}
//===----------------------------------------------------------------------===//
diff --git a/clang/test/CIR/CodeGen/complex.cpp b/clang/test/CIR/CodeGen/complex.cpp
index 377fc3f4f8ed0a..d5dd6599ddaefc 100644
--- a/clang/test/CIR/CodeGen/complex.cpp
+++ b/clang/test/CIR/CodeGen/complex.cpp
@@ -2041,6 +2041,52 @@ void compare_two_complex_bin_ops() {
// OGCG: %[[RESULT_I8:.*]] = zext i1 %[[RESULT]] to i8
// OGCG: store i8 %[[RESULT_I8]], ptr %[[C_ADDR]], align 1
+// Taking a part of a value-initialized complex folds the `#cir.zero` constant
+// to the zero of the element type.
+int real_of_zero_int_complex() {
+ using IntComplex = int _Complex;
+ return __real__ IntComplex();
+}
+
+// CIR-LABEL: cir.func {{.*}}@_Z24real_of_zero_int_complexv
+// CIR: %[[RETVAL:.*]] = cir.alloca "" {{.*}} return_slot : !cir.ptr<!s32i>
+// CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i
+// CIR: cir.store %[[ZERO]], %[[RETVAL]] : !s32i, !cir.ptr<!s32i>
+// CIR: %[[RET:.*]] = cir.load %[[RETVAL]] : !cir.ptr<!s32i>, !s32i
+// CIR: cir.return %[[RET]] : !s32i
+
+// FIXME: CIR keeps the return slot at -O0 where OGCG returns the value directly.
+// LLVM-LABEL: define {{.*}}i32 @_Z24real_of_zero_int_complexv
+// LLVM: %[[RETVAL:.*]] = alloca i32, align 4
+// LLVM: store i32 0, ptr %[[RETVAL]], align 4
+// LLVM: %[[RET:.*]] = load i32, ptr %[[RETVAL]], align 4
+// LLVM: ret i32 %[[RET]]
+
+// OGCG-LABEL: define {{.*}}i32 @_Z24real_of_zero_int_complexv
+// OGCG: 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: %[[RETVAL:.*]] = cir.alloca "" {{.*}} return_slot : !cir.ptr<!cir.float>
+// CIR: %[[ZERO:.*]] = cir.const #cir.fp<0.000000e+00> : !cir.float
+// CIR: cir.store %[[ZERO]], %[[RETVAL]] : !cir.float, !cir.ptr<!cir.float>
+// CIR: %[[RET:.*]] = cir.load %[[RETVAL]] : !cir.ptr<!cir.float>, !cir.float
+// CIR: cir.return %[[RET]] : !cir.float
+
+// FIXME: CIR keeps the return slot at -O0 where OGCG returns the value directly.
+// LLVM-LABEL: define {{.*}}float @_Z26imag_of_zero_float_complexv
+// LLVM: %[[RETVAL:.*]] = alloca float, align 4
+// LLVM: store float 0.000000e+00, ptr %[[RETVAL]], align 4
+// LLVM: %[[RET:.*]] = load float, ptr %[[RETVAL]], align 4
+// LLVM: ret float %[[RET]]
+
+// OGCG-LABEL: define {{.*}}float @_Z26imag_of_zero_float_complexv
+// OGCG: ret float 0.000000e+00
+
// A captureless lambda with a _Complex return type, converted to a function
// pointer, generates a static invoker that forwards the call and returns the
// complex result through emitReturnOfRValue.
diff --git a/clang/test/CIR/Transforms/complex-create-fold.cir b/clang/test/CIR/Transforms/complex-create-fold.cir
index 963ef464a76954..e2a8ded4477d6a 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,53 @@ 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: }
+
+ // 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: }
+}
More information about the cfe-commits
mailing list