Author: Amr Hesham
Date: 2026-04-02T22:55:43+02:00
New Revision: f2dff15995b38dc4649a258ead3865c0b79a1abe

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

LOG: [clang] Fixed a crash when explicitly casting between atomic complex types 
(#172210)

Fixed a crash when explicitly casting between atomic complex types

resolve: #172208

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/CodeGen/CGExprComplex.cpp
    clang/test/CodeGen/complex.c

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 688f0a2c2bb75..442ec58adc25a 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -462,6 +462,7 @@ Miscellaneous Clang Crashes Fixed
 - Fixed an assertion failure when casting a function pointer with a target 
with a non-default program address space. (#GH186210)
 - Fixed a crash when ``decltype(__builtin_FUNCTION())`` is used as a template 
type argument. (#GH167433)
 - Fixed an assertion failure when parsing an invalid ``decltype`` specifier 
with missing parentheses or extra semicolons. (#GH188014)
+- Fixed a crash when explicitly casting a complex type to or from an atomic 
complex type. (#GH172208)
 
 OpenACC Specific Changes
 ------------------------

diff  --git a/clang/lib/CodeGen/CGExprComplex.cpp 
b/clang/lib/CodeGen/CGExprComplex.cpp
index 92129a041fd8e..a97441d3b63c4 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -507,8 +507,12 @@ ComplexPairTy 
ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val,
                                                            QualType DestType,
                                                            SourceLocation Loc) 
{
   // Get the src/dest element type.
-  SrcType = SrcType->castAs<ComplexType>()->getElementType();
-  DestType = DestType->castAs<ComplexType>()->getElementType();
+  SrcType = SrcType.getAtomicUnqualifiedType()
+                ->castAs<ComplexType>()
+                ->getElementType();
+  DestType = DestType.getAtomicUnqualifiedType()
+                 ->castAs<ComplexType>()
+                 ->getElementType();
 
   // C99 6.3.1.6: When a value of complex type is converted to another
   // complex type, both the real and imaginary parts follow the conversion

diff  --git a/clang/test/CodeGen/complex.c b/clang/test/CodeGen/complex.c
index a7654dcb76e32..c6fe7c23072a6 100644
--- a/clang/test/CodeGen/complex.c
+++ b/clang/test/CodeGen/complex.c
@@ -593,6 +593,91 @@ void imag_on_scalar_with_type_promotion() {
   _Float16 _Complex a;
   _Float16 b = __real__(__imag__ a);
 }
+
+// CHECK-LABEL: define dso_local void 
@explicit_cast_atomic_complex_to_atomic_complex(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[A:%.*]] = alloca { float, float }, align 8
+// CHECK-NEXT:    [[B:%.*]] = alloca { i32, i32 }, align 8
+// CHECK-NEXT:    [[ATOMIC_TEMP:%.*]] = alloca { float, float }, align 8
+// CHECK-NEXT:    [[A_REALP:%.*]] = getelementptr inbounds nuw { float, float 
}, ptr [[A]], i32 0, i32 0
+// CHECK-NEXT:    [[A_IMAGP:%.*]] = getelementptr inbounds nuw { float, float 
}, ptr [[A]], i32 0, i32 1
+// CHECK-NEXT:    store float 2.000000e+00, ptr [[A_REALP]], align 8
+// CHECK-NEXT:    store float 0.000000e+00, ptr [[A_IMAGP]], align 4
+// CHECK-NEXT:    [[ATOMIC_LOAD:%.*]] = load atomic i64, ptr [[A]] seq_cst, 
align 8
+// CHECK-NEXT:    store i64 [[ATOMIC_LOAD]], ptr [[ATOMIC_TEMP]], align 8
+// CHECK-NEXT:    [[ATOMIC_TEMP_REALP:%.*]] = getelementptr inbounds nuw { 
float, float }, ptr [[ATOMIC_TEMP]], i32 0, i32 0
+// CHECK-NEXT:    [[ATOMIC_TEMP_REAL:%.*]] = load float, ptr 
[[ATOMIC_TEMP_REALP]], align 8
+// CHECK-NEXT:    [[ATOMIC_TEMP_IMAGP:%.*]] = getelementptr inbounds nuw { 
float, float }, ptr [[ATOMIC_TEMP]], i32 0, i32 1
+// CHECK-NEXT:    [[ATOMIC_TEMP_IMAG:%.*]] = load float, ptr 
[[ATOMIC_TEMP_IMAGP]], align 4
+// CHECK-NEXT:    [[CONV:%.*]] = fptosi float [[ATOMIC_TEMP_REAL]] to i32
+// CHECK-NEXT:    [[CONV1:%.*]] = fptosi float [[ATOMIC_TEMP_IMAG]] to i32
+// CHECK-NEXT:    [[B_REALP:%.*]] = getelementptr inbounds nuw { i32, i32 }, 
ptr [[B]], i32 0, i32 0
+// CHECK-NEXT:    [[B_IMAGP:%.*]] = getelementptr inbounds nuw { i32, i32 }, 
ptr [[B]], i32 0, i32 1
+// CHECK-NEXT:    store i32 [[CONV]], ptr [[B_REALP]], align 8
+// CHECK-NEXT:    store i32 [[CONV1]], ptr [[B_IMAGP]], align 4
+// CHECK-NEXT:    ret void
+//
+void explicit_cast_atomic_complex_to_atomic_complex() {
+  _Atomic _Complex float a = 2.0f;
+  _Atomic _Complex int b = (_Atomic _Complex int)a;
+}
+
+// CHECK-LABEL: define dso_local void @explicit_cast_atomic_complex_to_complex(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[A:%.*]] = alloca { float, float }, align 8
+// CHECK-NEXT:    [[B:%.*]] = alloca { i32, i32 }, align 4
+// CHECK-NEXT:    [[ATOMIC_TEMP:%.*]] = alloca { float, float }, align 8
+// CHECK-NEXT:    [[A_REALP:%.*]] = getelementptr inbounds nuw { float, float 
}, ptr [[A]], i32 0, i32 0
+// CHECK-NEXT:    [[A_IMAGP:%.*]] = getelementptr inbounds nuw { float, float 
}, ptr [[A]], i32 0, i32 1
+// CHECK-NEXT:    store float 2.000000e+00, ptr [[A_REALP]], align 8
+// CHECK-NEXT:    store float 0.000000e+00, ptr [[A_IMAGP]], align 4
+// CHECK-NEXT:    [[ATOMIC_LOAD:%.*]] = load atomic i64, ptr [[A]] seq_cst, 
align 8
+// CHECK-NEXT:    store i64 [[ATOMIC_LOAD]], ptr [[ATOMIC_TEMP]], align 8
+// CHECK-NEXT:    [[ATOMIC_TEMP_REALP:%.*]] = getelementptr inbounds nuw { 
float, float }, ptr [[ATOMIC_TEMP]], i32 0, i32 0
+// CHECK-NEXT:    [[ATOMIC_TEMP_REAL:%.*]] = load float, ptr 
[[ATOMIC_TEMP_REALP]], align 8
+// CHECK-NEXT:    [[ATOMIC_TEMP_IMAGP:%.*]] = getelementptr inbounds nuw { 
float, float }, ptr [[ATOMIC_TEMP]], i32 0, i32 1
+// CHECK-NEXT:    [[ATOMIC_TEMP_IMAG:%.*]] = load float, ptr 
[[ATOMIC_TEMP_IMAGP]], align 4
+// CHECK-NEXT:    [[CONV:%.*]] = fptosi float [[ATOMIC_TEMP_REAL]] to i32
+// CHECK-NEXT:    [[CONV1:%.*]] = fptosi float [[ATOMIC_TEMP_IMAG]] to i32
+// CHECK-NEXT:    [[B_REALP:%.*]] = getelementptr inbounds nuw { i32, i32 }, 
ptr [[B]], i32 0, i32 0
+// CHECK-NEXT:    [[B_IMAGP:%.*]] = getelementptr inbounds nuw { i32, i32 }, 
ptr [[B]], i32 0, i32 1
+// CHECK-NEXT:    store i32 [[CONV]], ptr [[B_REALP]], align 4
+// CHECK-NEXT:    store i32 [[CONV1]], ptr [[B_IMAGP]], align 4
+// CHECK-NEXT:    ret void
+//
+void explicit_cast_atomic_complex_to_complex() {
+  _Atomic _Complex float a = 2.0f;
+  _Complex int b = (_Complex int)a;
+}
+
+// CHECK-LABEL: define dso_local void @explicit_cast_complex_to_atomic_complex(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[A:%.*]] = alloca { float, float }, align 4
+// CHECK-NEXT:    [[B:%.*]] = alloca { i32, i32 }, align 8
+// CHECK-NEXT:    [[A_REALP:%.*]] = getelementptr inbounds nuw { float, float 
}, ptr [[A]], i32 0, i32 0
+// CHECK-NEXT:    [[A_IMAGP:%.*]] = getelementptr inbounds nuw { float, float 
}, ptr [[A]], i32 0, i32 1
+// CHECK-NEXT:    store float 2.000000e+00, ptr [[A_REALP]], align 4
+// CHECK-NEXT:    store float 0.000000e+00, ptr [[A_IMAGP]], align 4
+// CHECK-NEXT:    [[A_REALP1:%.*]] = getelementptr inbounds nuw { float, float 
}, ptr [[A]], i32 0, i32 0
+// CHECK-NEXT:    [[A_REAL:%.*]] = load float, ptr [[A_REALP1]], align 4
+// CHECK-NEXT:    [[A_IMAGP2:%.*]] = getelementptr inbounds nuw { float, float 
}, ptr [[A]], i32 0, i32 1
+// CHECK-NEXT:    [[A_IMAG:%.*]] = load float, ptr [[A_IMAGP2]], align 4
+// CHECK-NEXT:    [[CONV:%.*]] = fptosi float [[A_REAL]] to i32
+// CHECK-NEXT:    [[CONV3:%.*]] = fptosi float [[A_IMAG]] to i32
+// CHECK-NEXT:    [[B_REALP:%.*]] = getelementptr inbounds nuw { i32, i32 }, 
ptr [[B]], i32 0, i32 0
+// CHECK-NEXT:    [[B_IMAGP:%.*]] = getelementptr inbounds nuw { i32, i32 }, 
ptr [[B]], i32 0, i32 1
+// CHECK-NEXT:    store i32 [[CONV]], ptr [[B_REALP]], align 8
+// CHECK-NEXT:    store i32 [[CONV3]], ptr [[B_IMAGP]], align 4
+// CHECK-NEXT:    ret void
+//
+void explicit_cast_complex_to_atomic_complex() {
+  _Complex float a = 2.0f;
+  _Atomic _Complex int b = (_Atomic _Complex int)a;
+}
+
 //.
 // CHECK: [[PROF2]] = !{!"branch_weights", i32 1, i32 1048575}
 //.


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

Reply via email to