Skip to content

Commit cd8df19

Browse files
author
Andy Kaylor
committed
Align -ffp-model=fast denormal handling with -ffast-math
This is an attempt to better align the denormal handling of -ffp-model=fast with the behavior of -ffast-math. The clang user's manual claims that -ffp-model=fast "behaves identically to specifying both -ffast-math and -ffp-contract=fast." That isn't entirely correct. One difference is that -ffast-math causes crtfastmath.o to be linked if it is available, and passes -fdenormal-fp-math=PreserveSign as a -cc1 option when crtfastmath.o is available. I'm not sure the current behavior is reasonable and consistent for all tool chains, but I'm not trying to fix that here. This is just trying to make an incremental improvement. I am going to be proposing further changes to -ffp-model=fast, decoupling it from -ffast-math and introducing a new -ffp-model=aggressive that matches the current behavior, but I wanted to solidfy the current behavior before I do that.
1 parent 5b4ed0d commit cd8df19

File tree

6 files changed

+22
-3
lines changed

6 files changed

+22
-3
lines changed

clang/docs/UsersManual.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,8 +1452,8 @@ floating point semantic models: precise (the default), strict, and fast.
14521452
"fenv_access", "off", "on", "off"
14531453
"rounding_mode", "tonearest", "dynamic", "tonearest"
14541454
"contract", "on", "off", "fast"
1455-
"denormal_fp_math", "IEEE", "IEEE", "IEEE"
1456-
"denormal_fp32_math", "IEEE","IEEE", "IEEE"
1455+
"denormal_fp_math", "IEEE", "IEEE", "target-dependent"
1456+
"denormal_fp32_math", "IEEE","IEEE", "target-dependent"
14571457
"support_math_errno", "on", "on", "off"
14581458
"no_honor_nans", "off", "off", "on"
14591459
"no_honor_infinities", "off", "off", "on"

clang/lib/Driver/ToolChain.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1314,11 +1314,17 @@ bool ToolChain::isFastMathRuntimeAvailable(const ArgList &Args,
13141314
Arg *A =
13151315
Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
13161316
options::OPT_funsafe_math_optimizations,
1317-
options::OPT_fno_unsafe_math_optimizations);
1317+
options::OPT_fno_unsafe_math_optimizations,
1318+
options::OPT_ffp_model_EQ);
13181319

13191320
if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
13201321
A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
13211322
return false;
1323+
if (A && A->getOption().getID() == options::OPT_ffp_model_EQ) {
1324+
StringRef Model = A->getValue();
1325+
if (!Model.equals("fast"))
1326+
return false;
1327+
}
13221328
}
13231329
// If crtfastmath.o exists add it to the arguments.
13241330
Path = GetFilePath("crtfastmath.o");

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2942,6 +2942,10 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
29422942
if (Val.equals("fast")) {
29432943
FPModel = Val;
29442944
applyFastMath();
2945+
// The target-specific getDefaultDenormalModeForType handler should
2946+
// account for -ffp-model=fast and choose its behavior
2947+
DenormalFPMath = DefaultDenormalFPMath;
2948+
DenormalFP32Math = DefaultDenormalFP32Math;
29452949
} else if (Val.equals("precise")) {
29462950
optID = options::OPT_ffp_contract;
29472951
FPModel = Val;

clang/test/Driver/default-denormal-fp-math.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55

66
// crtfastmath enables ftz and daz
77
// RUN: %clang -### -target x86_64-unknown-linux-gnu -ffast-math --sysroot=%S/Inputs/basic_linux_tree -c %s -v 2>&1 | FileCheck -check-prefix=CHECK-PRESERVESIGN %s
8+
// RUN: %clang -### -target x86_64-unknown-linux-gnu -ffp-model=fast --sysroot=%S/Inputs/basic_linux_tree -c %s -v 2>&1 | FileCheck -check-prefix=CHECK-PRESERVESIGN %s
89

910
// crt not linked in with nostartfiles
1011
// RUN: %clang -### -target x86_64-unknown-linux-gnu -ffast-math -nostartfiles --sysroot=%S/Inputs/basic_linux_tree -c %s -v 2>&1 | FileCheck -check-prefix=CHECK-IEEE %s
12+
// RUN: %clang -### -target x86_64-unknown-linux-gnu -ffp-model=fast -nostartfiles --sysroot=%S/Inputs/basic_linux_tree -c %s -v 2>&1 | FileCheck -check-prefix=CHECK-IEEE %s
1113

1214
// If there's no crtfastmath, don't assume ftz/daz
1315
// RUN: %clang -### -target x86_64-unknown-linux-gnu -ffast-math --sysroot=/dev/null -c %s -v 2>&1 | FileCheck -check-prefix=CHECK-IEEE %s
16+
// RUN: %clang -### -target x86_64-unknown-linux-gnu -ffp-model=fast --sysroot=/dev/null -c %s -v 2>&1 | FileCheck -check-prefix=CHECK-IEEE %s
1417

1518
// RUN: %clang -### -target x86_64-scei-ps4 -c %s -v 2>&1 | FileCheck -check-prefix=CHECK-PRESERVESIGN %s
1619

clang/test/Driver/linux-ld.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,9 @@
14171417
// RUN: %clang --target=x86_64-unknown-linux -no-pie -### %s -funsafe-math-optimizations\
14181418
// RUN: --sysroot=%S/Inputs/basic_linux_tree 2>&1 \
14191419
// RUN: | FileCheck --check-prefix=CHECK-CRTFASTMATH %s
1420+
// RUN: %clang --target=x86_64-unknown-linux -no-pie -### %s -ffp-model=fast \
1421+
// RUN: --sysroot=%S/Inputs/basic_linux_tree 2>&1 \
1422+
// RUN: | FileCheck --check-prefix=CHECK-CRTFASTMATH %s
14201423
// RUN: %clang --target=x86_64-unknown-linux -no-pie -### %s -Ofast\
14211424
// RUN: --sysroot=%S/Inputs/basic_linux_tree 2>&1 \
14221425
// RUN: | FileCheck --check-prefix=CHECK-CRTFASTMATH %s

clang/test/Driver/solaris-ld.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@
193193
// RUN: %clang --target=sparc-sun-solaris2.11 -### %s -ffast-math \
194194
// RUN: --sysroot=%S/Inputs/solaris_sparc_tree 2>&1 \
195195
// RUN: | FileCheck --check-prefix=CHECK-CRTFASTMATH-SPARC32 %s
196+
// RUN: %clang --target=sparc-sun-solaris2.11 -### %s -ffp-model=fast \
197+
// RUN: --sysroot=%S/Inputs/solaris_sparc_tree 2>&1 \
198+
// RUN: | FileCheck --check-prefix=CHECK-CRTFASTMATH-SPARC32 %s
196199
// CHECK-CRTFASTMATH-SPARC32: "-isysroot" "[[SYSROOT:[^"]+]]"
197200
// CHECK-CRTFASTMATH-SPARC32: "[[SYSROOT]]/usr/gcc/4.8/lib/gcc/sparc-sun-solaris2.11/4.8.2{{/|\\\\}}crtfastmath.o"
198201
// CHECK-NOCRTFASTMATH-SPARC32-NOT: crtfastmath.o

0 commit comments

Comments
 (0)