Skip to content

Commit 1f9953c

Browse files
authored
[clang] Make -fveclib={ArmPL,SLEEF} imply -fno-math-errno (#112580)
These two veclibs are only available for AArch64 targets, and as mentioned in https://discourse.llvm.org/t/rfc-should-fveclib-imply-fno-math-errno-for-all-targets/81384, we (Arm) think that `-fveclib` should imply `-fno-math-errno`. By setting `-fveclib` the user shows they intend to use the vector math functions, which implies they don't care about errno. However, currently, the vector mappings won't be used in many cases without setting `-fno-math-errno` separately. Making this change would also help resolve some inconsistencies in how vector mappings are applied (see #108980 (comment)). Note: Both SLEEF and ArmPL state that they do not set `errno`: - https://developer.arm.com/documentation/101004/2410/General-information/Arm-Performance-Libraries-math-functions * "The vector functions in libamath which are available on Linux may not set errno nor raise exceptions" - https://sleef.org/2-references/libm/ * "These functions do not set errno nor raise an exception."
1 parent eb9af19 commit 1f9953c

File tree

6 files changed

+90
-1
lines changed

6 files changed

+90
-1
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,10 @@ def err_sls_hardening_arm_not_supported : Error<
502502
def warn_drv_large_data_threshold_invalid_code_model: Warning<
503503
"'%0' only applies to medium and large code models">,
504504
InGroup<UnusedCommandLineArgument>;
505+
def warn_drv_math_errno_enabled_after_veclib: Warning<
506+
"math errno enabled by '%0' after it was implicitly disabled by '%1',"
507+
" this may limit the utilization of the vector library">,
508+
InGroup<MathErrnoEnabledWithVecLib>;
505509

506510
def note_drv_command_failed_diag_msg : Note<
507511
"diagnostic msg: %0">;

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def FloatZeroConversion : DiagGroup<"float-zero-conversion">;
125125
def FloatConversion :
126126
DiagGroup<"float-conversion", [FloatOverflowConversion,
127127
FloatZeroConversion]>;
128+
def MathErrnoEnabledWithVecLib : DiagGroup<"math-errno-enabled-with-veclib">;
128129

129130
def FrameAddress : DiagGroup<"frame-address">;
130131
def FreeNonHeapObject : DiagGroup<"free-nonheap-object">;

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3411,6 +3411,9 @@ def fno_experimental_isel : Flag<["-"], "fno-experimental-isel">, Group<f_clang_
34113411
def fveclib : Joined<["-"], "fveclib=">, Group<f_Group>,
34123412
Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
34133413
HelpText<"Use the given vector functions library">,
3414+
HelpTextForVariants<[ClangOption, CC1Option],
3415+
"Use the given vector functions library. "
3416+
"Note: -fveclib={ArmPL,SLEEF} implies -fno-math-errno">,
34143417
Values<"Accelerate,libmvec,MASSV,SVML,SLEEF,Darwin_libsystem_m,ArmPL,AMDLIBM,none">,
34153418
NormalizedValuesScope<"llvm::driver::VectorLibrary">,
34163419
NormalizedValues<["Accelerate", "LIBMVEC", "MASSV", "SVML", "SLEEF",

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "clang/Driver/SanitizerArgs.h"
4242
#include "clang/Driver/Types.h"
4343
#include "clang/Driver/XRayArgs.h"
44+
#include "llvm/ADT/ScopeExit.h"
4445
#include "llvm/ADT/SmallSet.h"
4546
#include "llvm/ADT/StringExtras.h"
4647
#include "llvm/BinaryFormat/Magic.h"
@@ -2854,6 +2855,14 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
28542855
bool OFastEnabled, const ArgList &Args,
28552856
ArgStringList &CmdArgs,
28562857
const JobAction &JA) {
2858+
// List of veclibs which when used with -fveclib imply -fno-math-errno.
2859+
constexpr std::array VecLibImpliesNoMathErrno{llvm::StringLiteral("ArmPL"),
2860+
llvm::StringLiteral("SLEEF")};
2861+
bool NoMathErrnoWasImpliedByVecLib = false;
2862+
const Arg *VecLibArg = nullptr;
2863+
// Track the arg (if any) that enabled errno after -fveclib for diagnostics.
2864+
const Arg *ArgThatEnabledMathErrnoAfterVecLib = nullptr;
2865+
28572866
// Handle various floating point optimization flags, mapping them to the
28582867
// appropriate LLVM code generation flags. This is complicated by several
28592868
// "umbrella" flags, so we do this by stepping through the flags incrementally
@@ -2960,6 +2969,12 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
29602969
}
29612970

29622971
for (const Arg *A : Args) {
2972+
auto CheckMathErrnoForVecLib =
2973+
llvm::make_scope_exit([&, MathErrnoBeforeArg = MathErrno] {
2974+
if (NoMathErrnoWasImpliedByVecLib && !MathErrnoBeforeArg && MathErrno)
2975+
ArgThatEnabledMathErrnoAfterVecLib = A;
2976+
});
2977+
29632978
switch (A->getOption().getID()) {
29642979
// If this isn't an FP option skip the claim below
29652980
default: continue;
@@ -3125,6 +3140,13 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
31253140
TrappingMathPresent = true;
31263141
FPExceptionBehavior = "strict";
31273142
break;
3143+
case options::OPT_fveclib:
3144+
VecLibArg = A;
3145+
NoMathErrnoWasImpliedByVecLib =
3146+
llvm::is_contained(VecLibImpliesNoMathErrno, A->getValue());
3147+
if (NoMathErrnoWasImpliedByVecLib)
3148+
MathErrno = false;
3149+
break;
31283150
case options::OPT_fno_trapping_math:
31293151
if (!TrappingMathPresent && !FPExceptionBehavior.empty() &&
31303152
FPExceptionBehavior != "ignore")
@@ -3338,8 +3360,13 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
33383360
if (ApproxFunc)
33393361
CmdArgs.push_back("-fapprox-func");
33403362

3341-
if (MathErrno)
3363+
if (MathErrno) {
33423364
CmdArgs.push_back("-fmath-errno");
3365+
if (NoMathErrnoWasImpliedByVecLib)
3366+
D.Diag(clang::diag::warn_drv_math_errno_enabled_after_veclib)
3367+
<< ArgThatEnabledMathErrnoAfterVecLib->getAsString(Args)
3368+
<< VecLibArg->getAsString(Args);
3369+
}
33433370

33443371
if (AssociativeMath && ReciprocalMath && !SignedZeros && ApproxFunc &&
33453372
!TrappingMath)

clang/test/Driver/autocomplete.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
// WARNING-NEXT: -Wmain-return-type
115115
// WARNING-NEXT: -Wmalformed-warning-check
116116
// WARNING-NEXT: -Wmany-braces-around-scalar-init
117+
// WARNING-NEXT: -Wmath-errno-enabled-with-veclib
117118
// WARNING-NEXT: -Wmathematical-notation-identifier-extension
118119
// WARNING-NEXT: -Wmax-tokens
119120
// WARNING-NEXT: -Wmax-unsigned-zero

clang/test/Driver/fveclib.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,56 @@
4949

5050
// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=ArmPL -flto %s 2>&1 | FileCheck --check-prefix=CHECK-LTO-ARMPL %s
5151
// CHECK-LTO-ARMPL: "-plugin-opt=-vector-library=ArmPL"
52+
53+
54+
/* Verify that -fmath-errno is set correctly for the vector library. */
55+
56+
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fveclib=LIBMVEC %s 2>&1 | FileCheck --check-prefix=CHECK-ERRNO-LIBMVEC %s
57+
// CHECK-ERRNO-LIBMVEC: "-fveclib=LIBMVEC"
58+
// CHECK-ERRNO-LIBMVEC-SAME: "-fmath-errno"
59+
60+
// RUN: %clang -### --target=powerpc64-unknown-linux-gnu -fveclib=MASSV %s 2>&1 | FileCheck --check-prefix=CHECK-ERRNO-MASSV %s
61+
// CHECK-ERRNO-MASSV: "-fveclib=MASSV"
62+
// CHECK-ERRNO-MASSV-SAME: "-fmath-errno"
63+
64+
// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fveclib=SVML %s 2>&1 | FileCheck --check-prefix=CHECK-ERRNO-SVML %s
65+
// CHECK-ERRNO-SVML: "-fveclib=SVML"
66+
// CHECK-ERRNO-SVML-SAME: "-fmath-errno"
67+
68+
// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=SLEEF %s 2>&1 | FileCheck --check-prefix=CHECK-ERRNO-SLEEF %s
69+
// CHECK-ERRNO-SLEEF-NOT: "-fmath-errno"
70+
// CHECK-ERRNO-SLEEF: "-fveclib=SLEEF"
71+
// CHECK-ERRNO-SLEEF-NOT: "-fmath-errno"
72+
73+
// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=ArmPL %s 2>&1 | FileCheck --check-prefix=CHECK-ERRNO-ARMPL %s
74+
// CHECK-ERRNO-ARMPL-NOT: "-fmath-errno"
75+
// CHECK-ERRNO-ARMPL: "-fveclib=ArmPL"
76+
// CHECK-ERRNO-ARMPL-NOT: "-fmath-errno"
77+
78+
// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=ArmPL -fmath-errno %s 2>&1 | FileCheck --check-prefix=CHECK-REENABLE-ERRNO-ARMPL %s
79+
// CHECK-REENABLE-ERRNO-ARMPL: math errno enabled by '-fmath-errno' after it was implicitly disabled by '-fveclib=ArmPL', this may limit the utilization of the vector library [-Wmath-errno-enabled-with-veclib]
80+
// CHECK-REENABLE-ERRNO-ARMPL: "-fveclib=ArmPL"
81+
// CHECK-REENABLE-ERRNO-ARMPL-SAME: "-fmath-errno"
82+
83+
// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=SLEEF -fmath-errno %s 2>&1 | FileCheck --check-prefix=CHECK-REENABLE-ERRNO-SLEEF %s
84+
// CHECK-REENABLE-ERRNO-SLEEF: math errno enabled by '-fmath-errno' after it was implicitly disabled by '-fveclib=SLEEF', this may limit the utilization of the vector library [-Wmath-errno-enabled-with-veclib]
85+
// CHECK-REENABLE-ERRNO-SLEEF: "-fveclib=SLEEF"
86+
// CHECK-REENABLE-ERRNO-SLEEF-SAME: "-fmath-errno"
87+
88+
// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=ArmPL -fno-fast-math %s 2>&1 | FileCheck --check-prefix=CHECK-REENABLE-ERRNO-NFM %s
89+
// CHECK-REENABLE-ERRNO-NFM: math errno enabled by '-fno-fast-math' after it was implicitly disabled by '-fveclib=ArmPL', this may limit the utilization of the vector library [-Wmath-errno-enabled-with-veclib]
90+
// CHECK-REENABLE-ERRNO-NFM: "-fveclib=ArmPL"
91+
// CHECK-REENABLE-ERRNO-NFM-SAME: "-fmath-errno"
92+
93+
// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=ArmPL -ffp-model=strict %s 2>&1 | FileCheck --check-prefix=CHECK-REENABLE-ERRNO-FP-MODEL %s
94+
// CHECK-REENABLE-ERRNO-FP-MODEL: math errno enabled by '-ffp-model=strict' after it was implicitly disabled by '-fveclib=ArmPL', this may limit the utilization of the vector library [-Wmath-errno-enabled-with-veclib]
95+
// CHECK-REENABLE-ERRNO-FP-MODEL: "-fveclib=ArmPL"
96+
// CHECK-REENABLE-ERRNO-FP-MODEL-SAME: "-fmath-errno"
97+
98+
/* Verify the warning points at the last arg to enable -fmath-errno. */
99+
// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=ArmPL -fno-fast-math -fno-math-errno -ffp-model=strict %s 2>&1 | FileCheck --check-prefix=CHECK-ENABLED-LAST %s
100+
// CHECK-ENABLED-LAST: math errno enabled by '-ffp-model=strict' after it was implicitly disabled by '-fveclib=ArmPL', this may limit the utilization of the vector library [-Wmath-errno-enabled-with-veclib]
101+
102+
/* Verify no warning when math-errno is re-enabled for a different veclib (that does not imply -fno-math-errno). */
103+
// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=ArmPL -fmath-errno -fveclib=LIBMVEC %s 2>&1 | FileCheck --check-prefix=CHECK-REPEAT-VECLIB %s
104+
// CHECK-REPEAT-VECLIB-NOT: math errno enabled

0 commit comments

Comments
 (0)