Author: Zahira Ammarguellat Date: 2025-11-24T07:51:55-05:00 New Revision: 999deef63df5a057350a1e3bf211e536d5cfbc82
URL: https://github.com/llvm/llvm-project/commit/999deef63df5a057350a1e3bf211e536d5cfbc82 DIFF: https://github.com/llvm/llvm-project/commit/999deef63df5a057350a1e3bf211e536d5cfbc82.diff LOG: Desugar complex element types for promoted complex division (#168943) This patch fixes a crash in Clang that occurs when the compiler retrieves the element type of a complex type but receives a sugared type. See example here: https://godbolt.org/z/cdbdeMcaT This patch fixes the crash. Added: Modified: clang/lib/CodeGen/CGExprComplex.cpp clang/lib/Sema/SemaExpr.cpp clang/test/CodeGen/promoted-complex-div.c Removed: ################################################################################ diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp index d281c4c20616a..bca7c30557f03 100644 --- a/clang/lib/CodeGen/CGExprComplex.cpp +++ b/clang/lib/CodeGen/CGExprComplex.cpp @@ -320,7 +320,7 @@ class ComplexExprEmitter QualType getPromotionType(FPOptionsOverride Features, QualType Ty, bool IsComplexDivisor) { if (auto *CT = Ty->getAs<ComplexType>()) { - QualType ElementType = CT->getElementType(); + QualType ElementType = CT->getElementType().getCanonicalType(); bool IsFloatingType = ElementType->isFloatingType(); bool IsComplexRangePromoted = CGF.getLangOpts().getComplexRange() == LangOptions::ComplexRangeKind::CX_Promoted; diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 10f0ec3010c6c..d3c2cc559ea20 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -10726,7 +10726,7 @@ static void DetectPrecisionLossInComplexDivision(Sema &S, QualType DivisorTy, if (!CT) return; - QualType ElementType = CT->getElementType(); + QualType ElementType = CT->getElementType().getCanonicalType(); bool IsComplexRangePromoted = S.getLangOpts().getComplexRange() == LangOptions::ComplexRangeKind::CX_Promoted; if (!ElementType->isFloatingType() || !IsComplexRangePromoted) diff --git a/clang/test/CodeGen/promoted-complex-div.c b/clang/test/CodeGen/promoted-complex-div.c index 7ed7b07db83ae..006b5e334e6ea 100644 --- a/clang/test/CodeGen/promoted-complex-div.c +++ b/clang/test/CodeGen/promoted-complex-div.c @@ -81,3 +81,55 @@ _Complex double divf(_Complex double a, _Complex double b) { return a / b; // nopromotion-warning{{excess precision is requested but the target does not support excess precision which may result in observable diff erences in complex division behavior}} } + +// This test ensures that Clang does not crash when complex element types +// require desugaring under -complex-range=promoted. Previously, a sugared +// typedef element type (e.g., 'typedef double a') caused a crash during +// complex range evaluation in both Sema and CodeGen. +typedef double a; +_Complex double *b; +// CHECK-LABEL: define dso_local void @DivideByComplexZero +void DivideByComplexZero() { + // CHECK: fpext double {{.*}} to x86_fp80 + // CHECK: fpext double {{.*}} to x86_fp80 + // CHECK: fmul x86_fp80 + // CHECK: fmul x86_fp80 + // CHECK: fadd x86_fp80 + // CHECK: fmul x86_fp80 + // CHECK: fmul x86_fp80 + // CHECK: fsub x86_fp80 + // CHECK: fdiv x86_fp80 + // CHECK: fdiv x86_fp80 + // CHECK: fptrunc x86_fp80 + // CHECK: fptrunc x86_fp80 + + // NOX87: call double @llvm.fabs.f64(double {{.*}}) + // NOX87-NEXT: call double @llvm.fabs.f64(double {{.*}} + // NOX87-NEXT: fcmp ugt double {{.*}}, {{.*}} + // NOX87-NEXT: br i1 {{.*}}, label + // NOX87: abs_rhsr_greater_or_equal_abs_rhsi: + // NOX87-NEXT: fmul double + // NOX87-NEXT: fadd double + // NOX87-NEXT: fdiv double + // NOX87-NEXT: fmul double + // NOX87-NEXT: fsub double + // NOX87-NEXT: fdiv double + // NOX87-NEXT: br label {{.*}} + // NOX87: abs_rhsr_less_than_abs_rhsi: + // NOX87-NEXT: fmul double + // NOX87-NEXT: fadd double + // NOX87-NEXT: fdiv double + // NOX87-NEXT: fmul double + // NOX87-NEXT: fsub double + // NOX87-NEXT: fdiv double + // NOX87-NEXT: br label {{.*}} + // NOX87: complex_div: + // NOX87-NEXT: phi double + // NOX87-NEXT: phi double + // NOX87-NEXT: getelementptr inbounds nuw { double, double }, ptr {{.*}}, i32 0, i32 0 + // NOX87-NEXT: getelementptr inbounds nuw { double, double }, ptr {{.*}}, i32 0, i32 1 + // NOX87-NEXT: store double + // NOX87-NEXT: store double + + *b /= 1.0iF * (a)0; +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
