Skip to content

[Clang][Driver] Override complex number calculation method by -fno-fast-math #132680

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 4 commits into from
Jun 12, 2025
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
22 changes: 20 additions & 2 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2995,8 +2995,9 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
StringRef Float16ExcessPrecision = "";
StringRef BFloat16ExcessPrecision = "";
LangOptions::ComplexRangeKind Range = LangOptions::ComplexRangeKind::CX_None;
std::string ComplexRangeStr = "";
std::string GccRangeComplexOption = "";
std::string ComplexRangeStr;
std::string GccRangeComplexOption;
std::string LastComplexRangeOption;

auto setComplexRange = [&](LangOptions::ComplexRangeKind NewRange) {
// Warn if user expects to perform full implementation of complex
Expand Down Expand Up @@ -3080,6 +3081,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
EmitComplexRangeDiag(D, GccRangeComplexOption, "-fcx-limited-range");
}
GccRangeComplexOption = "-fcx-limited-range";
LastComplexRangeOption = A->getSpelling();
Range = LangOptions::ComplexRangeKind::CX_Basic;
break;
case options::OPT_fno_cx_limited_range:
Expand All @@ -3093,6 +3095,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
"-fno-cx-limited-range");
}
GccRangeComplexOption = "-fno-cx-limited-range";
LastComplexRangeOption = A->getSpelling();
Range = LangOptions::ComplexRangeKind::CX_Full;
break;
case options::OPT_fcx_fortran_rules:
Expand All @@ -3102,6 +3105,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
else
EmitComplexRangeDiag(D, GccRangeComplexOption, "-fcx-fortran-rules");
GccRangeComplexOption = "-fcx-fortran-rules";
LastComplexRangeOption = A->getSpelling();
Range = LangOptions::ComplexRangeKind::CX_Improved;
break;
case options::OPT_fno_cx_fortran_rules:
Expand All @@ -3114,6 +3118,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
"-fno-cx-fortran-rules");
}
GccRangeComplexOption = "-fno-cx-fortran-rules";
LastComplexRangeOption = A->getSpelling();
Range = LangOptions::ComplexRangeKind::CX_Full;
break;
case options::OPT_fcomplex_arithmetic_EQ: {
Expand Down Expand Up @@ -3148,6 +3153,8 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
ComplexArithmeticStr(RangeVal));
}
}
LastComplexRangeOption =
Args.MakeArgString(A->getSpelling() + A->getValue());
Range = RangeVal;
break;
}
Expand Down Expand Up @@ -3201,6 +3208,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
} else
D.Diag(diag::err_drv_unsupported_option_argument)
<< A->getSpelling() << Val;
LastComplexRangeOption = A->getSpelling();
break;
}

Expand Down Expand Up @@ -3386,6 +3394,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
[[fallthrough]];
case options::OPT_ffast_math:
applyFastMath(true);
LastComplexRangeOption = A->getSpelling();
if (A->getOption().getID() == options::OPT_Ofast)
LastFpContractOverrideOption = "-Ofast";
else
Expand All @@ -3403,6 +3412,15 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
ApproxFunc = false;
SignedZeros = true;
restoreFPContractState();
// If the last specified option related to complex range is not
// -ffast-math or -ffp-model=, emit warning.
if (LastComplexRangeOption != "-ffast-math" &&
LastComplexRangeOption != "-ffp-model=" &&
Range != LangOptions::ComplexRangeKind::CX_Full)
EmitComplexRangeDiag(D, LastComplexRangeOption, "-fno-fast-math");
Range = LangOptions::ComplexRangeKind::CX_None;
LastComplexRangeOption = "";
GccRangeComplexOption = "";
LastFpContractOverrideOption = "";
break;
} // End switch (A->getOption().getID())
Expand Down
100 changes: 92 additions & 8 deletions clang/test/Driver/range.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,83 @@
// RUN: %clang -### -target x86_64 -ffast-math -fcomplex-arithmetic=basic -c %s 2>&1 \
// RUN: | FileCheck --check-prefix=BASIC %s

// BASIC: -complex-range=basic
// FULL: -complex-range=full
// PRMTD: -complex-range=promoted
// BASIC-NOT: -complex-range=improved
// CHECK-NOT: -complex-range=basic
// IMPRVD: -complex-range=improved
// IMPRVD-NOT: -complex-range=basic
// CHECK-NOT: -complex-range=improved
// RUN: %clang -### --target=x86_64 -fcx-limited-range -fno-fast-math \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE,WARN21 %s

// RUN: %clang -### -Werror --target=x86_64 -fno-cx-limited-range -fno-fast-math \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE %s

// RUN: %clang -### --target=x86_64 -fcx-fortran-rules -fno-fast-math \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE,WARN22 %s

// RUN: %clang -### -Werror --target=x86_64 -fno-cx-fortran-rules -fno-fast-math \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE %s

// RUN: %clang -### -Werror --target=x86_64 -ffast-math -fno-fast-math \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE %s

// RUN: %clang -### --target=x86_64 -fcomplex-arithmetic=basic -fno-fast-math \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE,WARN23 %s

// RUN: %clang -### --target=x86_64 -fcomplex-arithmetic=promoted -fno-fast-math \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE,WARN24 %s

// RUN: %clang -### --target=x86_64 -fcomplex-arithmetic=improved -fno-fast-math \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE,WARN25 %s

// RUN: %clang -### -Werror --target=x86_64 -fcomplex-arithmetic=full -fno-fast-math \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE %s

// RUN: %clang -### -Werror --target=x86_64 -ffp-model=aggressive -fno-fast-math \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE %s

// RUN: %clang -### -Werror --target=x86_64 -ffp-model=fast -fno-fast-math \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE %s

// RUN: %clang -### -Werror --target=x86_64 -ffp-model=precise -fno-fast-math \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE %s

// RUN: %clang -### -Werror --target=x86_64 -ffp-model=strict -fno-fast-math \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE %s

// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -fcx-limited-range \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=BASIC %s

// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -fno-cx-limited-range \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s

// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -fcx-fortran-rules \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=IMPRVD %s

// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -fno-cx-fortran-rules \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s

// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -ffast-math \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=BASIC %s

// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -fcomplex-arithmetic=basic \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=BASIC %s

// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -fcomplex-arithmetic=promoted \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=PRMTD %s

// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -fcomplex-arithmetic=improved \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=IMPRVD %s

// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -fcomplex-arithmetic=full \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s

// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -ffp-model=aggressive \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=BASIC %s

// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -ffp-model=fast \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=PRMTD %s

// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -ffp-model=precise \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s

// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -ffp-model=strict \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s

// WARN1: warning: overriding '-fcx-limited-range' option with '-fcx-fortran-rules' [-Woverriding-option]
// WARN2: warning: overriding '-fno-cx-limited-range' option with '-fcx-fortran-rules' [-Woverriding-option]
Expand All @@ -196,5 +265,20 @@
// WARN14: overriding '-complex-range=promoted' option with '-fcx-limited-range' [-Woverriding-option]
// WARN17: warning: overriding '-fcomplex-arithmetic=full' option with '-fcomplex-arithmetic=basic' [-Woverriding-option]
// WARN20: warning: overriding '-fcx-fortran-rules' option with '-fcx-limited-range' [-Woverriding-option]
// WARN21: warning: overriding '-fcx-limited-range' option with '-fno-fast-math' [-Woverriding-option]
// WARN22: warning: overriding '-fcx-fortran-rules' option with '-fno-fast-math' [-Woverriding-option]
// WARN23: warning: overriding '-fcomplex-arithmetic=basic' option with '-fno-fast-math' [-Woverriding-option]
// WARN24: warning: overriding '-fcomplex-arithmetic=promoted' option with '-fno-fast-math' [-Woverriding-option]
// WARN25: warning: overriding '-fcomplex-arithmetic=improved' option with '-fno-fast-math' [-Woverriding-option]

// BASIC: -complex-range=basic
// FULL: -complex-range=full
// PRMTD: -complex-range=promoted
// BASIC-NOT: -complex-range=improved
// CHECK-NOT: -complex-range=basic
// IMPRVD: -complex-range=improved
// IMPRVD-NOT: -complex-range=basic
// CHECK-NOT: -complex-range=improved
// RANGE-NOT: -complex-range=

// ERR: error: unsupported argument 'foo' to option '-fcomplex-arithmetic='
Loading