Skip to content

Commit 999deef

Browse files
authored
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.
1 parent 65fd9f1 commit 999deef

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

clang/lib/CodeGen/CGExprComplex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ class ComplexExprEmitter
320320
QualType getPromotionType(FPOptionsOverride Features, QualType Ty,
321321
bool IsComplexDivisor) {
322322
if (auto *CT = Ty->getAs<ComplexType>()) {
323-
QualType ElementType = CT->getElementType();
323+
QualType ElementType = CT->getElementType().getCanonicalType();
324324
bool IsFloatingType = ElementType->isFloatingType();
325325
bool IsComplexRangePromoted = CGF.getLangOpts().getComplexRange() ==
326326
LangOptions::ComplexRangeKind::CX_Promoted;

clang/lib/Sema/SemaExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10726,7 +10726,7 @@ static void DetectPrecisionLossInComplexDivision(Sema &S, QualType DivisorTy,
1072610726
if (!CT)
1072710727
return;
1072810728

10729-
QualType ElementType = CT->getElementType();
10729+
QualType ElementType = CT->getElementType().getCanonicalType();
1073010730
bool IsComplexRangePromoted = S.getLangOpts().getComplexRange() ==
1073110731
LangOptions::ComplexRangeKind::CX_Promoted;
1073210732
if (!ElementType->isFloatingType() || !IsComplexRangePromoted)

clang/test/CodeGen/promoted-complex-div.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,55 @@ _Complex double divf(_Complex double a, _Complex double b) {
8181

8282
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}}
8383
}
84+
85+
// This test ensures that Clang does not crash when complex element types
86+
// require desugaring under -complex-range=promoted. Previously, a sugared
87+
// typedef element type (e.g., 'typedef double a') caused a crash during
88+
// complex range evaluation in both Sema and CodeGen.
89+
typedef double a;
90+
_Complex double *b;
91+
// CHECK-LABEL: define dso_local void @DivideByComplexZero
92+
void DivideByComplexZero() {
93+
// CHECK: fpext double {{.*}} to x86_fp80
94+
// CHECK: fpext double {{.*}} to x86_fp80
95+
// CHECK: fmul x86_fp80
96+
// CHECK: fmul x86_fp80
97+
// CHECK: fadd x86_fp80
98+
// CHECK: fmul x86_fp80
99+
// CHECK: fmul x86_fp80
100+
// CHECK: fsub x86_fp80
101+
// CHECK: fdiv x86_fp80
102+
// CHECK: fdiv x86_fp80
103+
// CHECK: fptrunc x86_fp80
104+
// CHECK: fptrunc x86_fp80
105+
106+
// NOX87: call double @llvm.fabs.f64(double {{.*}})
107+
// NOX87-NEXT: call double @llvm.fabs.f64(double {{.*}}
108+
// NOX87-NEXT: fcmp ugt double {{.*}}, {{.*}}
109+
// NOX87-NEXT: br i1 {{.*}}, label
110+
// NOX87: abs_rhsr_greater_or_equal_abs_rhsi:
111+
// NOX87-NEXT: fmul double
112+
// NOX87-NEXT: fadd double
113+
// NOX87-NEXT: fdiv double
114+
// NOX87-NEXT: fmul double
115+
// NOX87-NEXT: fsub double
116+
// NOX87-NEXT: fdiv double
117+
// NOX87-NEXT: br label {{.*}}
118+
// NOX87: abs_rhsr_less_than_abs_rhsi:
119+
// NOX87-NEXT: fmul double
120+
// NOX87-NEXT: fadd double
121+
// NOX87-NEXT: fdiv double
122+
// NOX87-NEXT: fmul double
123+
// NOX87-NEXT: fsub double
124+
// NOX87-NEXT: fdiv double
125+
// NOX87-NEXT: br label {{.*}}
126+
// NOX87: complex_div:
127+
// NOX87-NEXT: phi double
128+
// NOX87-NEXT: phi double
129+
// NOX87-NEXT: getelementptr inbounds nuw { double, double }, ptr {{.*}}, i32 0, i32 0
130+
// NOX87-NEXT: getelementptr inbounds nuw { double, double }, ptr {{.*}}, i32 0, i32 1
131+
// NOX87-NEXT: store double
132+
// NOX87-NEXT: store double
133+
134+
*b /= 1.0iF * (a)0;
135+
}

0 commit comments

Comments
 (0)