https://github.com/AmrDeveloper updated 
https://github.com/llvm/llvm-project/pull/172163

>From fc2ae48c8519adb017b8e38509ff491679baa805 Mon Sep 17 00:00:00 2001
From: Amr Hesham <[email protected]>
Date: Sat, 13 Dec 2025 16:46:33 +0100
Subject: [PATCH 1/2] [clang] Fixed a crash when explicitly casting to atomic
 complex

---
 clang/docs/ReleaseNotes.rst         |  1 +
 clang/lib/CodeGen/CGExprComplex.cpp |  2 ++
 clang/test/CodeGen/complex.c        | 15 +++++++++++++++
 3 files changed, 18 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index feaf92ad4415f..16f983fe82ddd 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -800,6 +800,7 @@ Crash and bug fixes
   containing a single colon. (#GH167905)
 - Fixed a crash when parsing malformed #pragma clang loop 
vectorize_width(4,8,16)
   by diagnosing invalid comma-separated argument lists. (#GH166325)
+- Fixed a crash when explicitly casting a scalar to an atomic complex. 
(#GH114885)
 
 Improvements
 ^^^^^^^^^^^^
diff --git a/clang/lib/CodeGen/CGExprComplex.cpp 
b/clang/lib/CodeGen/CGExprComplex.cpp
index e5815ef1130dc..c8f8bda5d567e 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -533,6 +533,8 @@ ComplexPairTy 
ComplexExprEmitter::EmitScalarToComplexCast(llvm::Value *Val,
                                                           QualType DestType,
                                                           SourceLocation Loc) {
   // Convert the input element to the element type of the complex.
+  if (DestType->isAtomicType())
+    DestType = DestType->castAs<AtomicType>()->getValueType();
   DestType = DestType->castAs<ComplexType>()->getElementType();
   Val = CGF.EmitScalarConversion(Val, SrcType, DestType, Loc);
 
diff --git a/clang/test/CodeGen/complex.c b/clang/test/CodeGen/complex.c
index 91fc9dda72f72..c2f95b93e3016 100644
--- a/clang/test/CodeGen/complex.c
+++ b/clang/test/CodeGen/complex.c
@@ -593,3 +593,18 @@ void imag_on_scalar_with_type_promotion() {
   _Float16 _Complex a;
   _Float16 b = __real__(__imag__ a);
 }
+
+// CHECK-LABEL: define dso_local void @explicit_cast_scalar_to_atomic_complex(
+// CHECK-SAME: ) #[[ATTR0]] {
+// CHECK-NEXT:  [[ENTRY:.*:]]
+// CHECK-NEXT:    [[A:%.*]] = 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:    ret void
+//
+void explicit_cast_scalar_to_atomic_complex() {
+  _Atomic _Complex float a = (_Atomic _Complex float)2.0f;
+}
+

>From 18142ae7f0851fcdb5ff5a44432296fd4deea52c Mon Sep 17 00:00:00 2001
From: Amr Hesham <[email protected]>
Date: Tue, 16 Dec 2025 14:29:35 +0100
Subject: [PATCH 2/2] Address code review comments

---
 clang/lib/CodeGen/CGExprComplex.cpp | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/clang/lib/CodeGen/CGExprComplex.cpp 
b/clang/lib/CodeGen/CGExprComplex.cpp
index c8f8bda5d567e..435540f6b3229 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -183,14 +183,16 @@ class ComplexExprEmitter
     // here.
     if (E->changesVolatileQualification())
       return EmitLoadOfLValue(E);
-    return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
+    return EmitCast(E->getCastKind(), E->getSubExpr(),
+                    E->getType().getAtomicUnqualifiedType());
   }
   ComplexPairTy VisitCastExpr(CastExpr *E) {
     if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E))
       CGF.CGM.EmitExplicitCastExprType(ECE, &CGF);
     if (E->changesVolatileQualification())
        return EmitLoadOfLValue(E);
-    return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
+    return EmitCast(E->getCastKind(), E->getSubExpr(),
+                    E->getType().getAtomicUnqualifiedType());
   }
   ComplexPairTy VisitCallExpr(const CallExpr *E);
   ComplexPairTy VisitStmtExpr(const StmtExpr *E);
@@ -533,8 +535,6 @@ ComplexPairTy 
ComplexExprEmitter::EmitScalarToComplexCast(llvm::Value *Val,
                                                           QualType DestType,
                                                           SourceLocation Loc) {
   // Convert the input element to the element type of the complex.
-  if (DestType->isAtomicType())
-    DestType = DestType->castAs<AtomicType>()->getValueType();
   DestType = DestType->castAs<ComplexType>()->getElementType();
   Val = CGF.EmitScalarConversion(Val, SrcType, DestType, Loc);
 
@@ -632,7 +632,8 @@ ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, 
Expr *Op,
   case CK_FloatingRealToComplex:
   case CK_IntegralRealToComplex: {
     CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op);
-    return EmitScalarToComplexCast(CGF.EmitScalarExpr(Op), Op->getType(),
+    return EmitScalarToComplexCast(CGF.EmitScalarExpr(Op),
+                                   Op->getType().getAtomicUnqualifiedType(),
                                    DestTy, Op->getExprLoc());
   }
 
@@ -1222,9 +1223,7 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E,
                          RValue &Val) {
   TestAndClearIgnoreReal();
   TestAndClearIgnoreImag();
-  QualType LHSTy = E->getLHS()->getType();
-  if (const AtomicType *AT = LHSTy->getAs<AtomicType>())
-    LHSTy = AT->getValueType();
+  QualType LHSTy = E->getLHS()->getType().getAtomicUnqualifiedType();
 
   BinOpInfo OpInfo;
   OpInfo.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts());
@@ -1242,7 +1241,7 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E,
                        E->getComputationResultType(), IsComplexDivisor);
   if (PromotionTypeCR.isNull())
     PromotionTypeCR = E->getComputationResultType();
-  OpInfo.Ty = PromotionTypeCR;
+  OpInfo.Ty = PromotionTypeCR.getAtomicUnqualifiedType();
   QualType ComplexElementTy =
       OpInfo.Ty->castAs<ComplexType>()->getElementType();
   QualType PromotionTypeRHS =

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

Reply via email to