Skip to content

Commit c9c91f5

Browse files
authored
Remove FiniteMathOnly and use only NoHonorINFs and NoHonorNANs. (#97342)
Currently `__FINITE_MATH_ONLY__` is set when `FiniteMathOnly`. And `FiniteMathOnly` is set when `NoHonorInfs` && `NoHonorNans` is true. But what happens one of the latter flags is false? To avoid potential inconsistencies, the internal option `FiniteMathOnly` is removed option and the macro `__FINITE_MATH_ONLY__` is set when `NoHonorInfs` && `NoHonorNans`.
1 parent 844c188 commit c9c91f5

18 files changed

+62
-38
lines changed

clang/include/clang/Basic/LangOptions.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ COMPATIBLE_LANGOPT(GNUInline , 1, 0, "GNU inline semantics")
224224
COMPATIBLE_LANGOPT(NoInlineDefine , 1, 0, "__NO_INLINE__ predefined macro")
225225
COMPATIBLE_LANGOPT(Deprecated , 1, 0, "__DEPRECATED predefined macro")
226226
COMPATIBLE_LANGOPT(FastMath , 1, 0, "fast FP math optimizations, and __FAST_MATH__ predefined macro")
227-
COMPATIBLE_LANGOPT(FiniteMathOnly , 1, 0, "__FINITE_MATH_ONLY__ predefined macro")
228227
COMPATIBLE_LANGOPT(UnsafeFPMath , 1, 0, "Unsafe Floating Point Math")
229228
COMPATIBLE_LANGOPT(ProtectParens , 1, 0, "optimizer honors parentheses "
230229
"when floating-point expressions are evaluated")
@@ -340,7 +339,6 @@ LANGOPT(SinglePrecisionConstants , 1, 0, "treating double-precision floating poi
340339
LANGOPT(FastRelaxedMath , 1, 0, "OpenCL fast relaxed math")
341340
BENIGN_LANGOPT(CLNoSignedZero , 1, 0, "Permit Floating Point optimization without regard to signed zeros")
342341
COMPATIBLE_LANGOPT(CLUnsafeMath , 1, 0, "Unsafe Floating Point Math")
343-
COMPATIBLE_LANGOPT(CLFiniteMathOnly , 1, 0, "__FINITE_MATH_ONLY__ predefined macro")
344342
/// FP_CONTRACT mode (on/off/fast).
345343
BENIGN_ENUM_LANGOPT(DefaultFPContractMode, FPModeKind, 2, FPM_Off, "FP contraction type")
346344
COMPATIBLE_LANGOPT(ExpStrictFP, 1, false, "Enable experimental strict floating point")

clang/include/clang/Driver/Options.td

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,8 +1117,7 @@ def cl_single_precision_constant : Flag<["-"], "cl-single-precision-constant">,
11171117
MarshallingInfoFlag<LangOpts<"SinglePrecisionConstants">>;
11181118
def cl_finite_math_only : Flag<["-"], "cl-finite-math-only">, Group<opencl_Group>,
11191119
Visibility<[ClangOption, CC1Option]>,
1120-
HelpText<"OpenCL only. Allow floating-point optimizations that assume arguments and results are not NaNs or +-Inf.">,
1121-
MarshallingInfoFlag<LangOpts<"CLFiniteMathOnly">>;
1120+
HelpText<"OpenCL only. Allow floating-point optimizations that assume arguments and results are not NaNs or +-Inf.">;
11221121
def cl_kernel_arg_info : Flag<["-"], "cl-kernel-arg-info">, Group<opencl_Group>,
11231122
Visibility<[ClangOption, CC1Option]>,
11241123
HelpText<"OpenCL only. Generate kernel argument metadata.">,
@@ -2609,13 +2608,12 @@ defm approx_func : BoolFOption<"approx-func", LangOpts<"ApproxFunc">, DefaultFal
26092608
"with an approximately equivalent calculation",
26102609
[funsafe_math_optimizations.KeyPath]>,
26112610
NegFlag<SetFalse, [], [ClangOption, CC1Option, FC1Option, FlangOption]>>;
2612-
defm finite_math_only : BoolFOption<"finite-math-only",
2613-
LangOpts<"FiniteMathOnly">, DefaultFalse,
2611+
defm finite_math_only : BoolOptionWithoutMarshalling<"f", "finite-math-only",
26142612
PosFlag<SetTrue, [], [ClangOption, CC1Option],
26152613
"Allow floating-point optimizations that "
26162614
"assume arguments and results are not NaNs or +-inf. This defines "
26172615
"the \\_\\_FINITE\\_MATH\\_ONLY\\_\\_ preprocessor macro.",
2618-
[cl_finite_math_only.KeyPath, ffast_math.KeyPath]>,
2616+
[ffast_math.KeyPath]>,
26192617
NegFlag<SetFalse>>;
26202618
defm signed_zeros : BoolFOption<"signed-zeros",
26212619
LangOpts<"NoSignedZero">, DefaultFalse,
@@ -7815,10 +7813,10 @@ def mreassociate : Flag<["-"], "mreassociate">,
78157813
MarshallingInfoFlag<LangOpts<"AllowFPReassoc">>, ImpliedByAnyOf<[funsafe_math_optimizations.KeyPath]>;
78167814
def menable_no_nans : Flag<["-"], "menable-no-nans">,
78177815
HelpText<"Allow optimization to assume there are no NaNs.">,
7818-
MarshallingInfoFlag<LangOpts<"NoHonorNaNs">>, ImpliedByAnyOf<[ffinite_math_only.KeyPath]>;
7819-
def menable_no_infinities : Flag<["-"], "menable-no-infs">,
7816+
MarshallingInfoFlag<LangOpts<"NoHonorNaNs">>, ImpliedByAnyOf<[ffast_math.KeyPath]>;
7817+
def menable_no_infs : Flag<["-"], "menable-no-infs">,
78207818
HelpText<"Allow optimization to assume there are no infinities.">,
7821-
MarshallingInfoFlag<LangOpts<"NoHonorInfs">>, ImpliedByAnyOf<[ffinite_math_only.KeyPath]>;
7819+
MarshallingInfoFlag<LangOpts<"NoHonorInfs">>, ImpliedByAnyOf<[ffast_math.KeyPath]>;
78227820

78237821
def pic_level : Separate<["-"], "pic-level">,
78247822
HelpText<"Value for __PIC__">,

clang/lib/Basic/LangOptions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ void LangOptions::resetNonModularOptions() {
3434
// invocations that cannot be round-tripped to arguments.
3535
// FIXME: we should derive this automatically from ImpliedBy in tablegen.
3636
AllowFPReassoc = UnsafeFPMath;
37-
NoHonorNaNs = FiniteMathOnly;
38-
NoHonorInfs = FiniteMathOnly;
37+
NoHonorInfs = FastMath;
38+
NoHonorNaNs = FastMath;
3939

4040
// These options do not affect AST generation.
4141
NoSanitizeFiles.clear();

clang/lib/Basic/Targets/OSTargets.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ static void addVisualCDefines(const LangOptions &Opts, MacroBuilder &Builder) {
173173
// "Under /fp:precise and /fp:strict, the compiler doesn't do any mathematical
174174
// transformation unless the transformation is guaranteed to produce a bitwise
175175
// identical result."
176-
const bool any_imprecise_flags =
177-
Opts.FastMath || Opts.FiniteMathOnly || Opts.UnsafeFPMath ||
178-
Opts.AllowFPReassoc || Opts.NoHonorNaNs || Opts.NoHonorInfs ||
179-
Opts.NoSignedZero || Opts.AllowRecip || Opts.ApproxFunc;
176+
const bool any_imprecise_flags = Opts.FastMath || Opts.UnsafeFPMath ||
177+
Opts.AllowFPReassoc || Opts.NoHonorNaNs ||
178+
Opts.NoHonorInfs || Opts.NoSignedZero ||
179+
Opts.AllowRecip || Opts.ApproxFunc;
180180

181181
// "Under both /fp:precise and /fp:fast, the compiler generates code intended
182182
// to run in the default floating-point environment."

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3384,9 +3384,28 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
33843384
CmdArgs.push_back("-ffast-math");
33853385

33863386
// Handle __FINITE_MATH_ONLY__ similarly.
3387-
if (!HonorINFs && !HonorNaNs)
3387+
// The -ffinite-math-only is added to CmdArgs when !HonorINFs && !HonorNaNs.
3388+
// Otherwise process the Xclang arguments to determine if -menable-no-infs and
3389+
// -menable-no-nans are set by the user.
3390+
bool shouldAddFiniteMathOnly = false;
3391+
if (!HonorINFs && !HonorNaNs) {
3392+
shouldAddFiniteMathOnly = true;
3393+
} else {
3394+
bool InfValues = true;
3395+
bool NanValues = true;
3396+
for (const auto *Arg : Args.filtered(options::OPT_Xclang)) {
3397+
StringRef ArgValue = Arg->getValue();
3398+
if (ArgValue == "-menable-no-nans")
3399+
NanValues = false;
3400+
else if (ArgValue == "-menable-no-infs")
3401+
InfValues = false;
3402+
}
3403+
if (!NanValues && !InfValues)
3404+
shouldAddFiniteMathOnly = true;
3405+
}
3406+
if (shouldAddFiniteMathOnly) {
33883407
CmdArgs.push_back("-ffinite-math-only");
3389-
3408+
}
33903409
if (const Arg *A = Args.getLastArg(options::OPT_mfpmath_EQ)) {
33913410
CmdArgs.push_back("-mfpmath");
33923411
CmdArgs.push_back(A->getValue());
@@ -3755,6 +3774,11 @@ static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs,
37553774
CmdArgs.push_back(Args.MakeArgString(CLExtStr));
37563775
}
37573776

3777+
if (Arg *A = Args.getLastArg(options::OPT_cl_finite_math_only)) {
3778+
CmdArgs.push_back("-menable-no-infs");
3779+
CmdArgs.push_back("-menable-no-nans");
3780+
}
3781+
37583782
for (const auto &Arg : ForwardedArguments)
37593783
if (const auto *A = Args.getLastArg(Arg))
37603784
CmdArgs.push_back(Args.MakeArgString(A->getOption().getPrefixedName()));

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,7 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
13181318
if (!LangOpts.MathErrno)
13191319
Builder.defineMacro("__NO_MATH_ERRNO__");
13201320

1321-
if (LangOpts.FastMath || LangOpts.FiniteMathOnly)
1321+
if (LangOpts.FastMath || (LangOpts.NoHonorInfs && LangOpts.NoHonorNaNs))
13221322
Builder.defineMacro("__FINITE_MATH_ONLY__", "1");
13231323
else
13241324
Builder.defineMacro("__FINITE_MATH_ONLY__", "0");

clang/test/CodeGen/finite-math.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -ffinite-math-only -emit-llvm -o - %s | FileCheck %s -check-prefix=CHECK -check-prefix=FINITE
1+
// RUN: %clang_cc1 -menable-no-infs -menable-no-nans -emit-llvm -o - %s | FileCheck %s -check-prefix=CHECK -check-prefix=FINITE
22
// RUN: %clang_cc1 -fno-signed-zeros -emit-llvm -o - %s | FileCheck %s -check-prefix=CHECK -check-prefix=NSZ
33
// RUN: %clang_cc1 -freciprocal-math -emit-llvm -o - %s | FileCheck %s -check-prefix=CHECK -check-prefix=RECIP
44
// RUN: %clang_cc1 -mreassociate -emit-llvm -o - %s | FileCheck %s -check-prefix=CHECK -check-prefix=REASSOC

clang/test/CodeGen/fp-floatcontrol-stack.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %clang_cc1 -triple x86_64-linux-gnu -ffp-contract=on -DDEFAULT=1 -emit-llvm -o - %s | FileCheck --check-prefix=CHECK-DDEFAULT %s
22
// RUN: %clang_cc1 -triple x86_64-linux-gnu -ffp-contract=on -DEBSTRICT=1 -ffp-exception-behavior=strict -emit-llvm -o - %s | FileCheck --check-prefix=CHECK-DEBSTRICT %s
33
// RUN: %clang_cc1 -triple x86_64-linux-gnu -DFAST=1 -ffast-math -ffp-contract=fast -emit-llvm -o - %s | FileCheck --check-prefix=CHECK-FAST %s
4-
// RUN: %clang_cc1 -triple x86_64-linux-gnu -ffp-contract=on -DNOHONOR=1 -menable-no-infs -menable-no-nans -emit-llvm -o - %s | FileCheck --check-prefix=CHECK-NOHONOR %s
4+
// RUN: %clang_cc1 -triple x86_64-linux-gnu -ffp-contract=on -DNOHONOR=1 -ffinite-math-only -menable-no-infs -menable-no-nans -emit-llvm -o - %s | FileCheck --check-prefix=CHECK-NOHONOR %s
55

66
#define FUN(n) \
77
(float z) { return n * z + n; }

clang/test/CodeGen/fp-options-to-fast-math-flags.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -o - %s | FileCheck -check-prefix CHECK-PRECISE %s
22
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -menable-no-nans -emit-llvm -o - %s | FileCheck -check-prefix CHECK-NO-NANS %s
33
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -menable-no-infs -emit-llvm -o - %s | FileCheck -check-prefix CHECK-NO-INFS %s
4-
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ffinite-math-only -emit-llvm -o - %s | FileCheck -check-prefix CHECK-FINITE %s
4+
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -menable-no-infs -menable-no-nans -emit-llvm -o - %s | FileCheck -check-prefix CHECK-FINITE %s
55
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fno-signed-zeros -emit-llvm -o - %s | FileCheck -check-prefix CHECK-NO-SIGNED-ZEROS %s
66
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -mreassociate -emit-llvm -o - %s | FileCheck -check-prefix CHECK-REASSOC %s
77
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -freciprocal-math -emit-llvm -o - %s | FileCheck -check-prefix CHECK-RECIP %s

clang/test/CodeGen/nofpclass.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --check-attributes --version 2
22
// REQUIRES: x86-registered-target
3-
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -target-feature +avx -fenable-matrix -ffinite-math-only -emit-llvm -o - %s | FileCheck -check-prefixes=CFINITEONLY %s
4-
// RUN: %clang_cc1 -x cl -triple x86_64-unknown-unknown -target-feature +avx -fenable-matrix -cl-finite-math-only -emit-llvm -o - %s | FileCheck -check-prefixes=CLFINITEONLY %s
3+
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -target-feature +avx -fenable-matrix -menable-no-infs -menable-no-nans -emit-llvm -o - %s | FileCheck -check-prefixes=CFINITEONLY %s
4+
// RUN: %clang_cc1 -x cl -triple x86_64-unknown-unknown -target-feature +avx -fenable-matrix -menable-no-nans -menable-no-infs -emit-llvm -o - %s | FileCheck -check-prefixes=CLFINITEONLY %s
55

66
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -target-feature +avx -fenable-matrix -menable-no-nans -emit-llvm -o - %s | FileCheck -check-prefixes=NONANS %s
77
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -target-feature +avx -fenable-matrix -menable-no-infs -emit-llvm -o - %s | FileCheck -check-prefixes=NOINFS %s

0 commit comments

Comments
 (0)