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.
This commit is contained in:
Zahira Ammarguellat 2025-11-24 07:51:55 -05:00 committed by GitHub
parent 65fd9f1f89
commit 999deef63d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 54 additions and 2 deletions

View File

@ -320,7 +320,7 @@ public:
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;

View File

@ -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)

View File

@ -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 differences 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;
}