Skip to content

Commit 971cc61

Browse files
Disable FTZ/DAZ when compiling shared libraries by default.
This fixes #57589, and aligns Clang with the behavior of current versions of gcc. There is a new option, -mdaz-ftz, to control the linking of the file that sets FTZ/DAZ on startup, and this flag is on by default if -ffast-math is present and -shared isn't.
1 parent 4e9decf commit 971cc61

File tree

5 files changed

+68
-7
lines changed

5 files changed

+68
-7
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ Non-comprehensive list of changes in this release
207207
- ``__typeof_unqual__`` is available in all C modes as an extension, which behaves
208208
like ``typeof_unqual`` from C23, similar to ``__typeof__`` and ``typeof``.
209209

210+
211+
* Code compiled with ``-shared`` and ``-ffast-math`` will no longer enable
212+
flush-to-zero floating-point mode by default. This decision can be overridden
213+
with use of ``-mdaz-ftz``. This behavior now matches GCC's behavior.
214+
(`#57589 <https://github.com/llvm/llvm-project/issues/57589>`_)
215+
210216
New Compiler Flags
211217
------------------
212218
- ``-fsanitize=implicit-bitfield-conversion`` checks implicit truncation and

clang/docs/UsersManual.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,8 @@ floating point semantic models: precise (the default), strict, and fast.
15061506

15071507
* ``-ffp-contract=fast``
15081508

1509-
Note: ``-ffast-math`` causes ``crtfastmath.o`` to be linked with code. See
1509+
Note: ``-ffast-math`` causes ``crtfastmath.o`` to be linked with code unless
1510+
``-shared`` or ``-mno-daz-ftz`` is present. See
15101511
:ref:`crtfastmath.o` for more details.
15111512

15121513
.. option:: -fno-fast-math
@@ -1560,7 +1561,8 @@ floating point semantic models: precise (the default), strict, and fast.
15601561
``-ffp-contract``.
15611562

15621563
Note: ``-fno-fast-math`` implies ``-fdenormal-fp-math=ieee``.
1563-
``-fno-fast-math`` causes ``crtfastmath.o`` to not be linked with code.
1564+
``-fno-fast-math`` causes ``crtfastmath.o`` to not be linked with code
1565+
unless ``-mdaz-ftz`` is present.
15641566

15651567
.. option:: -fdenormal-fp-math=<value>
15661568

@@ -1938,10 +1940,13 @@ by using ``#pragma STDC FENV_ROUND`` with a value other than ``FE_DYNAMIC``.
19381940

19391941
A note about ``crtfastmath.o``
19401942
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1941-
``-ffast-math`` and ``-funsafe-math-optimizations`` cause ``crtfastmath.o`` to be
1942-
automatically linked, which adds a static constructor that sets the FTZ/DAZ
1943+
``-ffast-math`` and ``-funsafe-math-optimizations`` without the ``-shared``
1944+
option cause ``crtfastmath.o`` to be
1945+
automatically linked, which adds a static constructor that sets the FTZ/DAZ
19431946
bits in MXCSR, affecting not only the current compilation unit but all static
1944-
and shared libraries included in the program.
1947+
and shared libraries included in the program. This decision can be overridden
1948+
by using either the flag ``-mdaz-ftz`` or ``-mno-daz-ftz`` to respectively
1949+
link or not link ``crtfastmath.o``.
19451950

19461951
.. _FLT_EVAL_METHOD:
19471952

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2615,6 +2615,11 @@ defm protect_parens : BoolFOption<"protect-parens",
26152615
"floating-point expressions are evaluated">,
26162616
NegFlag<SetFalse>>;
26172617

2618+
defm daz_ftz : SimpleMFlag<"daz-ftz",
2619+
"Globally set", "Do not globally set",
2620+
" the denormals-are-zero (DAZ) and flush-to-zero (FTZ) bits in the "
2621+
"floating-point control register on program startup.">;
2622+
26182623
def ffor_scope : Flag<["-"], "ffor-scope">, Group<f_Group>;
26192624
def fno_for_scope : Flag<["-"], "fno-for-scope">, Group<f_Group>;
26202625

clang/lib/Driver/ToolChain.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,9 +1307,14 @@ void ToolChain::AddCCKextLibArgs(const ArgList &Args,
13071307

13081308
bool ToolChain::isFastMathRuntimeAvailable(const ArgList &Args,
13091309
std::string &Path) const {
1310+
// Don't implicitly link in mode-changing libraries in a shared library, since
1311+
// this can have very deleterious effects. See the various links from
1312+
// https://github.com/llvm/llvm-project/issues/57589 for more information.
1313+
bool Default = !Args.hasArg(options::OPT_shared);
1314+
13101315
// Do not check for -fno-fast-math or -fno-unsafe-math when -Ofast passed
13111316
// (to keep the linker options consistent with gcc and clang itself).
1312-
if (!isOptimizationLevelFast(Args)) {
1317+
if (Default && !isOptimizationLevelFast(Args)) {
13131318
// Check if -ffast-math or -funsafe-math.
13141319
Arg *A =
13151320
Args.getLastArg(options::OPT_ffast_math, options::OPT_fno_fast_math,
@@ -1318,8 +1323,14 @@ bool ToolChain::isFastMathRuntimeAvailable(const ArgList &Args,
13181323

13191324
if (!A || A->getOption().getID() == options::OPT_fno_fast_math ||
13201325
A->getOption().getID() == options::OPT_fno_unsafe_math_optimizations)
1321-
return false;
1326+
Default = false;
13221327
}
1328+
1329+
// Whatever decision came as a result of the above implicit settings, either
1330+
// -mdaz-ftz or -mno-daz-ftz is capable of overriding it.
1331+
if (!Args.hasFlag(options::OPT_mdaz_ftz, options::OPT_mno_daz_ftz, Default))
1332+
return false;
1333+
13231334
// If crtfastmath.o exists add it to the arguments.
13241335
Path = GetFilePath("crtfastmath.o");
13251336
return (Path != "crtfastmath.o"); // Not found.

clang/test/Driver/linux-ld.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,40 @@
14461446
// RUN: %clang --target=i386-unknown-linux -no-pie -### %s -ffast-math \
14471447
// RUN: --sysroot=%S/Inputs/basic_linux_tree 2>&1 \
14481448
// RUN: | FileCheck --check-prefix=CHECK-NOCRTFASTMATH %s
1449+
// Don't link crtfastmath.o with -shared
1450+
// RUN: %clang --target=x86_64-unknown-linux -no-pie -### %s -ffast-math -shared \
1451+
// RUN: --gcc-toolchain="" \
1452+
// RUN: --sysroot=%S/Inputs/basic_linux_tree 2>&1 \
1453+
// RUN: | FileCheck --check-prefix=CHECK-NOCRTFASTMATH %s
1454+
// RUN: %clang --target=x86_64-unknown-linux -no-pie -### %s -Ofast -shared \
1455+
// RUN: --gcc-toolchain="" \
1456+
// RUN: --sysroot=%S/Inputs/basic_linux_tree 2>&1 \
1457+
// RUN: | FileCheck --check-prefix=CHECK-NOCRTFASTMATH %s
1458+
// Check for effects of -mdaz-ftz
1459+
// RUN: %clang --target=x86_64-unknown-linux -no-pie -### %s -ffast-math -shared -mdaz-ftz \
1460+
// RUN: --gcc-toolchain="" \
1461+
// RUN: --sysroot=%S/Inputs/basic_linux_tree 2>&1 \
1462+
// RUN: | FileCheck --check-prefix=CHECK-CRTFASTMATH %s
1463+
// RUN: %clang --target=x86_64-unknown-linux -no-pie -### %s -ffast-math -mdaz-ftz \
1464+
// RUN: --gcc-toolchain="" \
1465+
// RUN: --sysroot=%S/Inputs/basic_linux_tree 2>&1 \
1466+
// RUN: | FileCheck --check-prefix=CHECK-CRTFASTMATH %s
1467+
// RUN: %clang --target=x86_64-unknown-linux -no-pie -### %s -mdaz-ftz \
1468+
// RUN: --gcc-toolchain="" \
1469+
// RUN: --sysroot=%S/Inputs/basic_linux_tree 2>&1 \
1470+
// RUN: | FileCheck --check-prefix=CHECK-CRTFASTMATH %s
1471+
// RUN: %clang --target=x86_64-unknown-linux -no-pie -### %s -ffast-math -shared -mno-daz-ftz \
1472+
// RUN: --gcc-toolchain="" \
1473+
// RUN: --sysroot=%S/Inputs/basic_linux_tree 2>&1 \
1474+
// RUN: | FileCheck --check-prefix=CHECK-NOCRTFASTMATH %s
1475+
// RUN: %clang --target=x86_64-unknown-linux -no-pie -### %s -ffast-math -mno-daz-ftz \
1476+
// RUN: --gcc-toolchain="" \
1477+
// RUN: --sysroot=%S/Inputs/basic_linux_tree 2>&1 \
1478+
// RUN: | FileCheck --check-prefix=CHECK-NOCRTFASTMATH %s
1479+
// RUN: %clang --target=x86_64-unknown-linux -no-pie -### %s -mno-daz-ftz \
1480+
// RUN: --gcc-toolchain="" \
1481+
// RUN: --sysroot=%S/Inputs/basic_linux_tree 2>&1 \
1482+
// RUN: | FileCheck --check-prefix=CHECK-NOCRTFASTMATH %s
14491483
// CHECK-CRTFASTMATH: usr/lib/gcc/x86_64-unknown-linux/10.2.0{{/|\\\\}}crtfastmath.o
14501484
// CHECK-NOCRTFASTMATH-NOT: crtfastmath.o
14511485

0 commit comments

Comments
 (0)