Skip to content

[Clang] [Sema] Fix bug in _Complex float+int arithmetic #83063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 13, 2024
Merged
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
7 changes: 7 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ Bug Fixes in This Version
for logical operators in C23.
Fixes (`#64356 <https://github.com/llvm/llvm-project/issues/64356>`_).

- When performing mixed arithmetic between ``_Complex`` floating-point types and integers,
Clang now correctly promotes the integer to its corresponding real floating-point
type only rather than to the complex type (e.g. ``_Complex float / int`` is now evaluated
as ``_Complex float / float`` rather than ``_Complex float / _Complex float``), as mandated
by the C standard. This significantly improves codegen of `*` and `/` especially.
Fixes (`#31205 <https://github.com/llvm/llvm-project/issues/31205>`_).

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
15 changes: 7 additions & 8 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1099,12 +1099,13 @@ ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT,
return E;
}

/// Converts an integer to complex float type. Helper function of
/// Convert complex integers to complex floats and real integers to
/// real floats as required for complex arithmetic. Helper function of
/// UsualArithmeticConversions()
///
/// \return false if the integer expression is an integer type and is
/// successfully converted to the complex type.
static bool handleIntegerToComplexFloatConversion(Sema &S, ExprResult &IntExpr,
/// successfully converted to the (complex) float type.
static bool handleComplexIntegerToFloatConversion(Sema &S, ExprResult &IntExpr,
ExprResult &ComplexExpr,
QualType IntTy,
QualType ComplexTy,
Expand All @@ -1114,8 +1115,6 @@ static bool handleIntegerToComplexFloatConversion(Sema &S, ExprResult &IntExpr,
if (IntTy->isIntegerType()) {
QualType fpTy = ComplexTy->castAs<ComplexType>()->getElementType();
IntExpr = S.ImpCastExprToType(IntExpr.get(), fpTy, CK_IntegralToFloating);
IntExpr = S.ImpCastExprToType(IntExpr.get(), ComplexTy,
CK_FloatingRealToComplex);
} else {
assert(IntTy->isComplexIntegerType());
IntExpr = S.ImpCastExprToType(IntExpr.get(), ComplexTy,
Expand Down Expand Up @@ -1160,11 +1159,11 @@ static QualType handleComplexFloatConversion(Sema &S, ExprResult &Shorter,
static QualType handleComplexConversion(Sema &S, ExprResult &LHS,
ExprResult &RHS, QualType LHSType,
QualType RHSType, bool IsCompAssign) {
// if we have an integer operand, the result is the complex type.
if (!handleIntegerToComplexFloatConversion(S, RHS, LHS, RHSType, LHSType,
// Handle (complex) integer types.
if (!handleComplexIntegerToFloatConversion(S, RHS, LHS, RHSType, LHSType,
/*SkipCast=*/false))
return LHSType;
if (!handleIntegerToComplexFloatConversion(S, LHS, RHS, LHSType, RHSType,
if (!handleComplexIntegerToFloatConversion(S, LHS, RHS, LHSType, RHSType,
/*SkipCast=*/IsCompAssign))
return RHSType;

Expand Down
146 changes: 146 additions & 0 deletions clang/test/CodeGen/complex-math-mixed.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// RUN: %clang_cc1 %s -O0 -emit-llvm -triple x86_64-unknown-unknown -o - | FileCheck %s --check-prefix=X86
// RUN: %clang_cc1 %s -O0 -triple x86_64-unknown-unknown -fsyntax-only -ast-dump | FileCheck %s --check-prefix=AST

// Check that for 'F _Complex + int' (F = real floating-point type), we emit an
// implicit cast from 'int' to 'F', but NOT to 'F _Complex' (i.e. that we do
// 'F _Complex + F', NOT 'F _Complex + F _Complex'), and likewise for -/*.

// AST-NOT: FloatingRealToComplex

float _Complex add_float_ci(float _Complex a, int b) {
// X86-LABEL: @add_float_ci
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to float
// X86: fadd float {{.*}}, [[I]]
// X86-NOT: fadd
return a + b;
}

float _Complex add_float_ic(int a, float _Complex b) {
// X86-LABEL: @add_float_ic
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to float
// X86: fadd float [[I]]
// X86-NOT: fadd
return a + b;
}

float _Complex sub_float_ci(float _Complex a, int b) {
// X86-LABEL: @sub_float_ci
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to float
// X86: fsub float {{.*}}, [[I]]
// X86-NOT: fsub
return a - b;
}

float _Complex sub_float_ic(int a, float _Complex b) {
// X86-LABEL: @sub_float_ic
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to float
// X86: fsub float [[I]]
// X86: fneg
// X86-NOT: fsub
return a - b;
}

float _Complex mul_float_ci(float _Complex a, int b) {
// X86-LABEL: @mul_float_ci
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to float
// X86: fmul float {{.*}}, [[I]]
// X86: fmul float {{.*}}, [[I]]
// X86-NOT: fmul
return a * b;
}

float _Complex mul_float_ic(int a, float _Complex b) {
// X86-LABEL: @mul_float_ic
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to float
// X86: fmul float [[I]]
// X86: fmul float [[I]]
// X86-NOT: fmul
return a * b;
}

float _Complex div_float_ci(float _Complex a, int b) {
// X86-LABEL: @div_float_ci
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to float
// X86: fdiv float {{.*}}, [[I]]
// X86: fdiv float {{.*}}, [[I]]
// X86-NOT: @__divsc3
return a / b;
}

// There is no good way of doing this w/o converting the 'int' to a complex
// number, so we expect complex division here.
float _Complex div_float_ic(int a, float _Complex b) {
// X86-LABEL: @div_float_ic
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to float
// X86: call {{.*}} @__divsc3(float {{.*}} [[I]], float noundef 0.{{0+}}e+00, float {{.*}}, float {{.*}})
return a / b;
}

double _Complex add_double_ci(double _Complex a, int b) {
// X86-LABEL: @add_double_ci
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to double
// X86: fadd double {{.*}}, [[I]]
// X86-NOT: fadd
return a + b;
}

double _Complex add_double_ic(int a, double _Complex b) {
// X86-LABEL: @add_double_ic
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to double
// X86: fadd double [[I]]
// X86-NOT: fadd
return a + b;
}

double _Complex sub_double_ci(double _Complex a, int b) {
// X86-LABEL: @sub_double_ci
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to double
// X86: fsub double {{.*}}, [[I]]
// X86-NOT: fsub
return a - b;
}

double _Complex sub_double_ic(int a, double _Complex b) {
// X86-LABEL: @sub_double_ic
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to double
// X86: fsub double [[I]]
// X86: fneg
// X86-NOT: fsub
return a - b;
}

double _Complex mul_double_ci(double _Complex a, int b) {
// X86-LABEL: @mul_double_ci
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to double
// X86: fmul double {{.*}}, [[I]]
// X86: fmul double {{.*}}, [[I]]
// X86-NOT: fmul
return a * b;
}

double _Complex mul_double_ic(int a, double _Complex b) {
// X86-LABEL: @mul_double_ic
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to double
// X86: fmul double [[I]]
// X86: fmul double [[I]]
// X86-NOT: fmul
return a * b;
}

double _Complex div_double_ci(double _Complex a, int b) {
// X86-LABEL: @div_double_ci
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to double
// X86: fdiv double {{.*}}, [[I]]
// X86: fdiv double {{.*}}, [[I]]
// X86-NOT: @__divdc3
return a / b;
}

// There is no good way of doing this w/o converting the 'int' to a complex
// number, so we expect complex division here.
double _Complex div_double_ic(int a, double _Complex b) {
// X86-LABEL: @div_double_ic
// X86: [[I:%.*]] = sitofp i32 {{%.*}} to double
// X86: call {{.*}} @__divdc3(double {{.*}} [[I]], double noundef 0.{{0+}}e+00, double {{.*}}, double {{.*}})
return a / b;
}
48 changes: 23 additions & 25 deletions clang/test/CodeGen/volatile.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %clang_cc1 -O2 -triple=x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck %s -check-prefix CHECK
// RUN: %clang_cc1 -O2 -triple=x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck %s
struct agg
{
int a ;
Expand All @@ -10,34 +10,32 @@ _Complex float cf;
int volatile vol =10;
void f0() {
const_cast<volatile _Complex float &>(cf) = const_cast<volatile _Complex float&>(cf) + 1;
// CHECK: %cf.real = load volatile float, ptr @cf
// CHECK: %cf.imag = load volatile float, ptr getelementptr
// CHECK: %add.r = fadd float %cf.real, 1.000000e+00
// CHECK: %add.i = fadd float %cf.imag, 0.000000e+00
// CHECK: store volatile float %add.r
// CHECK: store volatile float %add.i, ptr getelementptr
// CHECK: [[Re1:%.*]] = load volatile float, ptr @cf
// CHECK: [[Im1:%.*]] = load volatile float, ptr getelementptr
// CHECK: [[Add1:%.*]] = fadd float [[Re1]], 1.000000e+00
// CHECK: store volatile float [[Add1]], ptr @cf
// CHECK: store volatile float [[Im1]], ptr getelementptr
static_cast<volatile _Complex float &>(cf) = static_cast<volatile _Complex float&>(cf) + 1;
// CHECK: %cf.real1 = load volatile float, ptr @cf
// CHECK: %cf.imag2 = load volatile float, ptr getelementptr
// CHECK: %add.r3 = fadd float %cf.real1, 1.000000e+00
// CHECK: %add.i4 = fadd float %cf.imag2, 0.000000e+00
// CHECK: store volatile float %add.r3, ptr @cf
// CHECK: store volatile float %add.i4, ptr getelementptr
// CHECK: [[Re2:%.*]] = load volatile float, ptr @cf
// CHECK: [[Im2:%.*]] = load volatile float, ptr getelementptr
// CHECK: [[Add2:%.*]] = fadd float [[Re2]], 1.000000e+00
// CHECK: store volatile float [[Add2]], ptr @cf
// CHECK: store volatile float [[Im2]], ptr getelementptr
const_cast<volatile int &>(a.a) = const_cast<volatile int &>(t.a) ;
// CHECK: %0 = load volatile i32, ptr @t
// CHECK: store volatile i32 %0, ptr @a
// CHECK: [[I1:%.*]] = load volatile i32, ptr @t
// CHECK: store volatile i32 [[I1]], ptr @a
static_cast<volatile int &>(a.b) = static_cast<volatile int &>(t.a) ;
// CHECK: %1 = load volatile i32, ptr @t
// CHECK: store volatile i32 %1, ptr getelementptr
// CHECK: [[I2:%.*]] = load volatile i32, ptr @t
// CHECK: store volatile i32 [[I2]], ptr getelementptr
const_cast<volatile int&>(vt) = const_cast<volatile int&>(vt) + 1;
// CHECK: %2 = load volatile i32, ptr @vt
// CHECK: %add = add nsw i32 %2, 1
// CHECK: store volatile i32 %add, ptr @vt
// CHECK: [[I3:%.*]] = load volatile i32, ptr @vt
// CHECK: [[Add3:%.*]] = add nsw i32 [[I3]], 1
// CHECK: store volatile i32 [[Add3]], ptr @vt
static_cast<volatile int&>(vt) = static_cast<volatile int&>(vt) + 1;
// CHECK: %3 = load volatile i32, ptr @vt
// CHECK: %add5 = add nsw i32 %3, 1
// CHECK: store volatile i32 %add5, ptr @vt
// CHECK: [[I4:%.*]] = load volatile i32, ptr @vt
// CHECK: [[Add4:%.*]] = add nsw i32 [[I4]], 1
// CHECK: store volatile i32 [[Add4]], ptr @vt
vt = const_cast<int&>(vol);
// %4 = load i32, ptr @vol
// store i32 %4, ptr @vt
// [[I5:%.*]] = load i32, ptr @vol
// store i32 [[I5]], ptr @vt
}
115 changes: 115 additions & 0 deletions clang/test/Sema/complex-arithmetic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// RUN: %clang_cc1 -verify %s
// expected-no-diagnostics

// This tests evaluation of _Complex arithmetic at compile time.

#define APPROX_EQ(a, b) ( \
__builtin_fabs(__real (a) - __real (b)) < 0.0001 && \
__builtin_fabs(__imag (a) - __imag (b)) < 0.0001 \
)

#define EVAL(a, b) _Static_assert(a == b, "")
#define EVALF(a, b) _Static_assert(APPROX_EQ(a, b), "")

// _Complex float + _Complex float
void a() {
EVALF((2.f + 3i) + (4.f + 5i), 6.f + 8i);
EVALF((2.f + 3i) - (4.f + 5i), -2.f - 2i);
EVALF((2.f + 3i) * (4.f + 5i), -7.f + 22i);
EVALF((2.f + 3i) / (4.f + 5i), 0.5609f + 0.0487i);

EVALF((2. + 3i) + (4. + 5i), 6. + 8i);
EVALF((2. + 3i) - (4. + 5i), -2. - 2i);
EVALF((2. + 3i) * (4. + 5i), -7. + 22i);
EVALF((2. + 3i) / (4. + 5i), .5609 + .0487i);
}

// _Complex int + _Complex int
void b() {
EVAL((2 + 3i) + (4 + 5i), 6 + 8i);
EVAL((2 + 3i) - (4 + 5i), -2 - 2i);
EVAL((2 + 3i) * (4 + 5i), -7 + 22i);
EVAL((8 + 30i) / (4 + 5i), 4 + 1i);
}

// _Complex float + float
void c() {
EVALF((2.f + 4i) + 3.f, 5.f + 4i);
EVALF((2.f + 4i) - 3.f, -1.f + 4i);
EVALF((2.f + 4i) * 3.f, 6.f + 12i);
EVALF((2.f + 4i) / 2.f, 1.f + 2i);

EVALF(3.f + (2.f + 4i), 5.f + 4i);
EVALF(3.f - (2.f + 4i), 1.f - 4i);
EVALF(3.f * (2.f + 4i), 6.f + 12i);
EVALF(3.f / (2.f + 4i), .3f - 0.6i);

EVALF((2. + 4i) + 3., 5. + 4i);
EVALF((2. + 4i) - 3., -1. + 4i);
EVALF((2. + 4i) * 3., 6. + 12i);
EVALF((2. + 4i) / 2., 1. + 2i);

EVALF(3. + (2. + 4i), 5. + 4i);
EVALF(3. - (2. + 4i), 1. - 4i);
EVALF(3. * (2. + 4i), 6. + 12i);
EVALF(3. / (2. + 4i), .3 - 0.6i);
}

// _Complex int + int
void d() {
EVAL((2 + 4i) + 3, 5 + 4i);
EVAL((2 + 4i) - 3, -1 + 4i);
EVAL((2 + 4i) * 3, 6 + 12i);
EVAL((2 + 4i) / 2, 1 + 2i);

EVAL(3 + (2 + 4i), 5 + 4i);
EVAL(3 - (2 + 4i), 1 - 4i);
EVAL(3 * (2 + 4i), 6 + 12i);
EVAL(20 / (2 + 4i), 2 - 4i);
}

// _Complex float + int
void e() {
EVALF((2.f + 4i) + 3, 5.f + 4i);
EVALF((2.f + 4i) - 3, -1.f + 4i);
EVALF((2.f + 4i) * 3, 6.f + 12i);
EVALF((2.f + 4i) / 2, 1.f + 2i);

EVALF(3 + (2.f + 4i), 5.f + 4i);
EVALF(3 - (2.f + 4i), 1.f - 4i);
EVALF(3 * (2.f + 4i), 6.f + 12i);
EVALF(3 / (2.f + 4i), .3f - 0.6i);

EVALF((2. + 4i) + 3, 5. + 4i);
EVALF((2. + 4i) - 3, -1. + 4i);
EVALF((2. + 4i) * 3, 6. + 12i);
EVALF((2. + 4i) / 2, 1. + 2i);

EVALF(3 + (2. + 4i), 5. + 4i);
EVALF(3 - (2. + 4i), 1. - 4i);
EVALF(3 * (2. + 4i), 6. + 12i);
EVALF(3 / (2. + 4i), .3 - 0.6i);
}

// _Complex int + float
void f() {
EVALF((2 + 4i) + 3.f, 5.f + 4i);
EVALF((2 + 4i) - 3.f, -1.f + 4i);
EVALF((2 + 4i) * 3.f, 6.f + 12i);
EVALF((2 + 4i) / 2.f, 1.f + 2i);

EVALF(3.f + (2 + 4i), 5.f + 4i);
EVALF(3.f - (2 + 4i), 1.f - 4i);
EVALF(3.f * (2 + 4i), 6.f + 12i);
EVALF(3.f / (2 + 4i), .3f - 0.6i);

EVALF((2 + 4i) + 3., 5. + 4i);
EVALF((2 + 4i) - 3., -1. + 4i);
EVALF((2 + 4i) * 3., 6. + 12i);
EVALF((2 + 4i) / 2., 1. + 2i);

EVALF(3. + (2 + 4i), 5. + 4i);
EVALF(3. - (2 + 4i), 1. - 4i);
EVALF(3. * (2 + 4i), 6. + 12i);
EVALF(3. / (2 + 4i), .3 - 0.6i);
}