Skip to content

Commit 84a9ec2

Browse files
committed
Remove redundant option -menable-unsafe-fp-math.
There are currently two options that are used to tell the compiler to perform unsafe floating-point optimizations: '-ffast-math' and '-funsafe-math-optimizations'. '-ffast-math' is enabled by default. It automatically enables the driver option '-menable-unsafe-fp-math'. Below is a table illustrating the special operations enabled automatically by '-ffast-math', '-funsafe-math-optimizations' and '-menable-unsafe-fp-math' respectively. Special Operations -ffast-math -funsafe-math-optimizations -menable-unsafe-fp-math MathErrno 0 1 1 FiniteMathOnly 1 0 0 AllowFPReassoc 1 1 1 NoSignedZero 1 1 1 AllowRecip 1 1 1 ApproxFunc 1 1 1 RoundingMath 0 0 0 UnsafeFPMath 1 0 1 FPContract fast on on '-ffast-math' enables '-fno-math-errno', '-ffinite-math-only', '-funsafe-math-optimzations' and sets 'FpContract' to 'fast'. The driver option '-menable-unsafe-fp-math' enables the same special options than '-funsafe-math-optimizations'. This is redundant. We propose to remove the driver option '-menable-unsafe-fp-math' and use instead, the setting of the special operations to set the function attribute 'unsafe-fp-math'. This attribute will be enabled only if those special operations are enabled and if 'FPContract' is either 'fast' or set to the default value. Differential Revision: https://reviews.llvm.org/D135097
1 parent 6ce8727 commit 84a9ec2

File tree

13 files changed

+59
-38
lines changed

13 files changed

+59
-38
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,9 @@ Arm and AArch64 Support in Clang
598598

599599
Floating Point Support in Clang
600600
-------------------------------
601+
- The driver option ``-menable-unsafe-fp-math`` has been removed. To enable
602+
unsafe floating-point optimizations use ``-funsafe-math-optimizations`` or
603+
``-ffast-math`` instead.
601604

602605
Internal API Changes
603606
--------------------

clang/include/clang/Driver/Options.td

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,10 +1596,6 @@ defm fast_math : BoolFOption<"fast-math",
15961596
PosFlag<SetTrue, [CC1Option], "Allow aggressive, lossy floating-point optimizations",
15971597
[cl_fast_relaxed_math.KeyPath]>,
15981598
NegFlag<SetFalse>>;
1599-
def menable_unsafe_fp_math : Flag<["-"], "menable-unsafe-fp-math">, Flags<[CC1Option]>,
1600-
HelpText<"Allow unsafe floating-point math optimizations which may decrease precision">,
1601-
MarshallingInfoFlag<LangOpts<"UnsafeFPMath">>,
1602-
ImpliedByAnyOf<[cl_unsafe_math_optimizations.KeyPath, ffast_math.KeyPath]>;
16031599
defm math_errno : BoolFOption<"math-errno",
16041600
LangOpts<"MathErrno">, DefaultFalse,
16051601
PosFlag<SetTrue, [CC1Option], "Require math functions to indicate errors by setting errno">,
@@ -1879,20 +1875,23 @@ def fsanitize_undefined_strip_path_components_EQ : Joined<["-"], "fsanitize-unde
18791875
} // end -f[no-]sanitize* flags
18801876

18811877
def funsafe_math_optimizations : Flag<["-"], "funsafe-math-optimizations">,
1882-
Group<f_Group>;
1878+
Group<f_Group>, Flags<[CC1Option]>,
1879+
HelpText<"Allow unsafe floating-point math optimizations which may decrease precision">,
1880+
MarshallingInfoFlag<LangOpts<"UnsafeFPMath">>,
1881+
ImpliedByAnyOf<[cl_unsafe_math_optimizations.KeyPath, ffast_math.KeyPath]>;
18831882
def fno_unsafe_math_optimizations : Flag<["-"], "fno-unsafe-math-optimizations">,
18841883
Group<f_Group>;
18851884
def fassociative_math : Flag<["-"], "fassociative-math">, Group<f_Group>;
18861885
def fno_associative_math : Flag<["-"], "fno-associative-math">, Group<f_Group>;
18871886
defm reciprocal_math : BoolFOption<"reciprocal-math",
18881887
LangOpts<"AllowRecip">, DefaultFalse,
18891888
PosFlag<SetTrue, [CC1Option], "Allow division operations to be reassociated",
1890-
[menable_unsafe_fp_math.KeyPath]>,
1889+
[funsafe_math_optimizations.KeyPath]>,
18911890
NegFlag<SetFalse>>;
18921891
defm approx_func : BoolFOption<"approx-func", LangOpts<"ApproxFunc">, DefaultFalse,
18931892
PosFlag<SetTrue, [CC1Option], "Allow certain math function calls to be replaced "
18941893
"with an approximately equivalent calculation",
1895-
[menable_unsafe_fp_math.KeyPath]>,
1894+
[funsafe_math_optimizations.KeyPath]>,
18961895
NegFlag<SetFalse>>;
18971896
defm finite_math_only : BoolFOption<"finite-math-only",
18981897
LangOpts<"FiniteMathOnly">, DefaultFalse,
@@ -1901,7 +1900,7 @@ defm finite_math_only : BoolFOption<"finite-math-only",
19011900
defm signed_zeros : BoolFOption<"signed-zeros",
19021901
LangOpts<"NoSignedZero">, DefaultFalse,
19031902
NegFlag<SetTrue, [CC1Option], "Allow optimizations that ignore the sign of floating point zeros",
1904-
[cl_no_signed_zeros.KeyPath, menable_unsafe_fp_math.KeyPath]>,
1903+
[cl_no_signed_zeros.KeyPath, funsafe_math_optimizations.KeyPath]>,
19051904
PosFlag<SetFalse>>;
19061905
def fhonor_nans : Flag<["-"], "fhonor-nans">, Group<f_Group>;
19071906
def fno_honor_nans : Flag<["-"], "fno-honor-nans">, Group<f_Group>;
@@ -5426,7 +5425,7 @@ def menable_no_nans : Flag<["-"], "menable-no-nans">,
54265425
MarshallingInfoFlag<LangOpts<"NoHonorNaNs">>, ImpliedByAnyOf<[ffinite_math_only.KeyPath]>;
54275426
def mreassociate : Flag<["-"], "mreassociate">,
54285427
HelpText<"Allow reassociation transformations for floating-point instructions">,
5429-
MarshallingInfoFlag<LangOpts<"AllowFPReassoc">>, ImpliedByAnyOf<[menable_unsafe_fp_math.KeyPath]>;
5428+
MarshallingInfoFlag<LangOpts<"AllowFPReassoc">>, ImpliedByAnyOf<[funsafe_math_optimizations.KeyPath]>;
54305429
def mabi_EQ_ieeelongdouble : Flag<["-"], "mabi=ieeelongdouble">,
54315430
HelpText<"Use IEEE 754 quadruple-precision for long double">,
54325431
MarshallingInfoFlag<LangOpts<"PPCIEEELongDouble">>;

clang/lib/CodeGen/CGCall.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1862,7 +1862,11 @@ void CodeGenModule::getDefaultFunctionAttributes(StringRef Name,
18621862
FuncAttrs.addAttribute("no-nans-fp-math", "true");
18631863
if (LangOpts.ApproxFunc)
18641864
FuncAttrs.addAttribute("approx-func-fp-math", "true");
1865-
if (LangOpts.UnsafeFPMath)
1865+
if ((LangOpts.FastMath ||
1866+
!LangOpts.FastMath && LangOpts.AllowFPReassoc && LangOpts.AllowRecip &&
1867+
!LangOpts.FiniteMathOnly && LangOpts.NoSignedZero &&
1868+
LangOpts.ApproxFunc) &&
1869+
LangOpts.getDefaultFPContractMode() != LangOptions::FPModeKind::FPM_Off)
18661870
FuncAttrs.addAttribute("unsafe-fp-math", "true");
18671871
if (CodeGenOpts.SoftFloat)
18681872
FuncAttrs.addAttribute("use-soft-float", "true");

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3103,7 +3103,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
31033103

31043104
if (!MathErrno && AssociativeMath && ReciprocalMath && !SignedZeros &&
31053105
ApproxFunc && !TrappingMath)
3106-
CmdArgs.push_back("-menable-unsafe-fp-math");
3106+
CmdArgs.push_back("-funsafe-math-optimizations");
31073107

31083108
if (!SignedZeros)
31093109
CmdArgs.push_back("-fno-signed-zeros");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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
8-
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -menable-unsafe-fp-math -emit-llvm -o - %s | FileCheck -check-prefix CHECK-UNSAFE %s
8+
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -funsafe-math-optimizations -emit-llvm -o - %s | FileCheck -check-prefix CHECK-UNSAFE %s
99
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ffast-math -emit-llvm -o - %s | FileCheck -check-prefix CHECK-FAST %s
1010

1111
float fn(float);

clang/test/CodeGen/func-attr.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %clang -c -ffast-math -emit-llvm -S -o - %s \
2+
// RUN: | FileCheck %s
3+
4+
// RUN: %clang -c -funsafe-math-optimizations -emit-llvm -S -o - %s \
5+
// RUN: | FileCheck %s
6+
7+
float foo(float a, float b) {
8+
return a+b;
9+
}
10+
11+
// CHECK: define{{.*}} float @foo(float noundef %a, float noundef %b) [[FAST_ATTRS:#[0-9]+]]
12+
// CHECK: attributes [[FAST_ATTRS]] = { {{.*}} "approx-func-fp-math"="true" {{.*}} "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" {{.*}} "unsafe-fp-math"="true"

clang/test/CodeGen/libcalls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %clang_cc1 -Wno-implicit-function-declaration -fmath-errno -emit-llvm -o - %s -triple i386-unknown-unknown | FileCheck -check-prefix CHECK-YES %s
22
// RUN: %clang_cc1 -Wno-implicit-function-declaration -emit-llvm -o - %s -triple i386-unknown-unknown | FileCheck -check-prefix CHECK-NO %s
3-
// RUN: %clang_cc1 -Wno-implicit-function-declaration -menable-unsafe-fp-math -emit-llvm -o - %s -triple i386-unknown-unknown | FileCheck -check-prefix CHECK-FAST %s
3+
// RUN: %clang_cc1 -Wno-implicit-function-declaration -funsafe-math-optimizations -emit-llvm -o - %s -triple i386-unknown-unknown | FileCheck -check-prefix CHECK-FAST %s
44

55
// CHECK-YES-LABEL: define{{.*}} void @test_sqrt
66
// CHECK-NO-LABEL: define{{.*}} void @test_sqrt

clang/test/CodeGenCUDA/propagate-metadata.cu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
// RUN: %clang_cc1 -x cuda %s -emit-llvm -mlink-builtin-bitcode %t.bc \
2424
// RUN: -fdenormal-fp-math-f32=preserve-sign -o - \
25-
// RUN: -fcuda-is-device -menable-unsafe-fp-math -triple nvptx-unknown-unknown \
25+
// RUN: -fcuda-is-device -funsafe-math-optimizations -triple nvptx-unknown-unknown \
2626
// RUN: | FileCheck %s --check-prefix=CHECK --check-prefix=FAST
2727

2828
// Wrap everything in extern "C" so we don't have to worry about name mangling

clang/test/Driver/cl-options.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,23 @@
4343
// EXTERNAL_I: "-isystem" "path"
4444

4545
// RUN: %clang_cl /fp:fast /fp:except -### -- %s 2>&1 | FileCheck -check-prefix=fpexcept %s
46-
// fpexcept-NOT: -menable-unsafe-fp-math
46+
// fpexcept-NOT: -funsafe-math-optimizations
4747
// fpexcept: -ffp-exception-behavior=strict
4848

4949
// RUN: %clang_cl /fp:fast /fp:except /fp:except- -### -- %s 2>&1 | FileCheck -check-prefix=fpexcept_ %s
50-
// fpexcept_: -menable-unsafe-fp-math
50+
// fpexcept_: -funsafe-math-optimizations
5151
// fpexcept_: -ffp-exception-behavior=ignore
5252

5353
// RUN: %clang_cl /fp:precise /fp:fast -### -- %s 2>&1 | FileCheck -check-prefix=fpfast %s
54-
// fpfast: -menable-unsafe-fp-math
54+
// fpfast: -funsafe-math-optimizations
5555
// fpfast: -ffast-math
5656

5757
// RUN: %clang_cl /fp:fast /fp:precise -### -- %s 2>&1 | FileCheck -check-prefix=fpprecise %s
58-
// fpprecise-NOT: -menable-unsafe-fp-math
58+
// fpprecise-NOT: -funsafe-math-optimizations
5959
// fpprecise-NOT: -ffast-math
6060

6161
// RUN: %clang_cl /fp:fast /fp:strict -### -- %s 2>&1 | FileCheck -check-prefix=fpstrict %s
62-
// fpstrict-NOT: -menable-unsafe-fp-math
62+
// fpstrict-NOT: -funsafe-math-optimizations
6363
// fpstrict-NOT: -ffast-math
6464
// fpstrict: -ffp-contract=off
6565

clang/test/Driver/fast-math.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,14 @@
151151
// RUN: -fno-signed-zeros -fno-trapping-math -fapprox-func -c %s 2>&1 \
152152
// RUN: | FileCheck --check-prefix=CHECK-UNSAFE-MATH %s
153153
// CHECK-UNSAFE-MATH: "-cc1"
154-
// CHECK-UNSAFE-MATH: "-menable-unsafe-fp-math"
154+
// CHECK-UNSAFE-MATH: "-funsafe-math-optimizations"
155155
// CHECK-UNSAFE-MATH: "-mreassociate"
156156
//
157157
// RUN: %clang -### -fno-fast-math -fno-math-errno -fassociative-math -freciprocal-math \
158158
// RUN: -fno-signed-zeros -fno-trapping-math -fapprox-func -c %s 2>&1 \
159159
// RUN: | FileCheck --check-prefix=CHECK-NO-FAST-MATH-UNSAFE-MATH %s
160160
// CHECK-NO-FAST-MATH-UNSAFE-MATH: "-cc1"
161-
// CHECK-NO-FAST-MATH-UNSAFE-MATH: "-menable-unsafe-fp-math"
161+
// CHECK-NO-FAST-MATH-UNSAFE-MATH: "-funsafe-math-optimizations"
162162
// CHECK-NO-FAST-MATH-UNSAFE-MATH: "-mreassociate"
163163

164164
// The 2nd -fno-fast-math overrides -fassociative-math.
@@ -167,7 +167,7 @@
167167
// RUN: -fno-fast-math -fno-signed-zeros -fno-trapping-math -c %s 2>&1 \
168168
// RUN: | FileCheck --check-prefix=CHECK-UNSAFE-MATH-NO-FAST-MATH %s
169169
// CHECK-UNSAFE-MATH-NO-FAST-MATH: "-cc1"
170-
// CHECK-UNSAFE-MATH-NO-FAST-MATH-NOT: "-menable-unsafe-fp-math"
170+
// CHECK-UNSAFE-MATH-NO-FAST-MATH-NOT: "-funsafe-math-optimizations"
171171
// CHECK-UNSAFE-MATH-NO-FAST-MATH-NOT: "-mreassociate"
172172
//
173173
// Check that various umbrella flags also enable these frontend options.
@@ -280,7 +280,7 @@
280280
// RUN: | FileCheck --check-prefix=CHECK-NO-UNSAFE-MATH %s
281281

282282
// CHECK-NO-UNSAFE-MATH: "-cc1"
283-
// CHECK-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math"
283+
// CHECK-NO-UNSAFE-MATH-NOT: "-funsafe-math-optimizations"
284284
// CHECK-NO_UNSAFE-MATH-NOT: "-mreassociate"
285285
// CHECK-NO-UNSAFE-MATH: "-o"
286286

@@ -292,9 +292,9 @@
292292
// RUN: | FileCheck --check-prefix=CHECK-REASSOC-NO-UNSAFE-MATH %s
293293

294294
// CHECK-REASSOC-NO-UNSAFE-MATH: "-cc1"
295-
// CHECK-REASSOC-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math"
295+
// CHECK-REASSOC-NO_UNSAFE-MATH-NOT: "-funsafe-math-optimizations"
296296
// CHECK-REASSOC-NO_UNSAFE-MATH: "-mreassociate"
297-
// CHECK-REASSOC-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math"
297+
// CHECK-REASSOC-NO-UNSAFE-MATH-NOT: "-funsafe-math-optimizations"
298298
// CHECK-REASSOC-NO-UNSAFE-MATH: "-o"
299299

300300

@@ -309,9 +309,9 @@
309309
// RUN: | FileCheck --check-prefix=CHECK-NO-REASSOC-NO-UNSAFE-MATH %s
310310

311311
// CHECK-NO-REASSOC-NO-UNSAFE-MATH: "-cc1"
312-
// CHECK-NO-REASSOC-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math"
312+
// CHECK-NO-REASSOC-NO_UNSAFE-MATH-NOT: "-funsafe-math-optimizations"
313313
// CHECK-NO-REASSOC-NO_UNSAFE-MATH-NOT: "-mreassociate"
314-
// CHECK-NO-REASSOC-NO-UNSAFE-MATH-NOT: "-menable-unsafe-fp-math"
314+
// CHECK-NO-REASSOC-NO_UNSAFE-MATH-NOT: "-funsafe-math-optimizations"
315315
// CHECK-NO-REASSOC-NO-UNSAFE-MATH: "-o"
316316

317317

0 commit comments

Comments
 (0)