Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGExprComplex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10726,7 +10726,7 @@ static void DetectPrecisionLossInComplexDivision(Sema &S, QualType DivisorTy,
if (!CT)
return;

QualType ElementType = CT->getElementType();
QualType ElementType = CT->getElementType().getCanonicalType();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just do this in GetHigherPrecisionFPType()?

I guess it doesn't matter that much either way, since it only has two callers.

bool IsComplexRangePromoted = S.getLangOpts().getComplexRange() ==
LangOptions::ComplexRangeKind::CX_Promoted;
if (!ElementType->isFloatingType() || !IsComplexRangePromoted)
Expand Down
52 changes: 52 additions & 0 deletions clang/test/CodeGen/promoted-complex-div.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}