Skip to content

Commit

Permalink
[clang] Make -fveclib={ArmPL,SLEEF} imply -fno-math-errno (llvm#112580)
Browse files Browse the repository at this point in the history
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 llvm#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."
  • Loading branch information
MacDue authored and NoumanAmir657 committed Nov 4, 2024
1 parent bd6b4e5 commit b669728
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 1 deletion.
4 changes: 4 additions & 0 deletions clang/include/clang/Basic/DiagnosticDriverKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,10 @@ def err_sls_hardening_arm_not_supported : Error<
def warn_drv_large_data_threshold_invalid_code_model: Warning<
"'%0' only applies to medium and large code models">,
InGroup<UnusedCommandLineArgument>;
def warn_drv_math_errno_enabled_after_veclib: Warning<
"math errno enabled by '%0' after it was implicitly disabled by '%1',"
" this may limit the utilization of the vector library">,
InGroup<MathErrnoEnabledWithVecLib>;

def note_drv_command_failed_diag_msg : Note<
"diagnostic msg: %0">;
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Basic/DiagnosticGroups.td
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def FloatZeroConversion : DiagGroup<"float-zero-conversion">;
def FloatConversion :
DiagGroup<"float-conversion", [FloatOverflowConversion,
FloatZeroConversion]>;
def MathErrnoEnabledWithVecLib : DiagGroup<"math-errno-enabled-with-veclib">;

def FrameAddress : DiagGroup<"frame-address">;
def FreeNonHeapObject : DiagGroup<"free-nonheap-object">;
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -3411,6 +3411,9 @@ def fno_experimental_isel : Flag<["-"], "fno-experimental-isel">, Group<f_clang_
def fveclib : Joined<["-"], "fveclib=">, Group<f_Group>,
Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
HelpText<"Use the given vector functions library">,
HelpTextForVariants<[ClangOption, CC1Option],
"Use the given vector functions library. "
"Note: -fveclib={ArmPL,SLEEF} implies -fno-math-errno">,
Values<"Accelerate,libmvec,MASSV,SVML,SLEEF,Darwin_libsystem_m,ArmPL,AMDLIBM,none">,
NormalizedValuesScope<"llvm::driver::VectorLibrary">,
NormalizedValues<["Accelerate", "LIBMVEC", "MASSV", "SVML", "SLEEF",
Expand Down
29 changes: 28 additions & 1 deletion clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "clang/Driver/SanitizerArgs.h"
#include "clang/Driver/Types.h"
#include "clang/Driver/XRayArgs.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/BinaryFormat/Magic.h"
Expand Down Expand Up @@ -2854,6 +2855,14 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
bool OFastEnabled, const ArgList &Args,
ArgStringList &CmdArgs,
const JobAction &JA) {
// List of veclibs which when used with -fveclib imply -fno-math-errno.
constexpr std::array VecLibImpliesNoMathErrno{llvm::StringLiteral("ArmPL"),
llvm::StringLiteral("SLEEF")};
bool NoMathErrnoWasImpliedByVecLib = false;
const Arg *VecLibArg = nullptr;
// Track the arg (if any) that enabled errno after -fveclib for diagnostics.
const Arg *ArgThatEnabledMathErrnoAfterVecLib = nullptr;

// Handle various floating point optimization flags, mapping them to the
// appropriate LLVM code generation flags. This is complicated by several
// "umbrella" flags, so we do this by stepping through the flags incrementally
Expand Down Expand Up @@ -2960,6 +2969,12 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
}

for (const Arg *A : Args) {
auto CheckMathErrnoForVecLib =
llvm::make_scope_exit([&, MathErrnoBeforeArg = MathErrno] {
if (NoMathErrnoWasImpliedByVecLib && !MathErrnoBeforeArg && MathErrno)
ArgThatEnabledMathErrnoAfterVecLib = A;
});

switch (A->getOption().getID()) {
// If this isn't an FP option skip the claim below
default: continue;
Expand Down Expand Up @@ -3125,6 +3140,13 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
TrappingMathPresent = true;
FPExceptionBehavior = "strict";
break;
case options::OPT_fveclib:
VecLibArg = A;
NoMathErrnoWasImpliedByVecLib =
llvm::is_contained(VecLibImpliesNoMathErrno, A->getValue());
if (NoMathErrnoWasImpliedByVecLib)
MathErrno = false;
break;
case options::OPT_fno_trapping_math:
if (!TrappingMathPresent && !FPExceptionBehavior.empty() &&
FPExceptionBehavior != "ignore")
Expand Down Expand Up @@ -3338,8 +3360,13 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
if (ApproxFunc)
CmdArgs.push_back("-fapprox-func");

if (MathErrno)
if (MathErrno) {
CmdArgs.push_back("-fmath-errno");
if (NoMathErrnoWasImpliedByVecLib)
D.Diag(clang::diag::warn_drv_math_errno_enabled_after_veclib)
<< ArgThatEnabledMathErrnoAfterVecLib->getAsString(Args)
<< VecLibArg->getAsString(Args);
}

if (AssociativeMath && ReciprocalMath && !SignedZeros && ApproxFunc &&
!TrappingMath)
Expand Down
1 change: 1 addition & 0 deletions clang/test/Driver/autocomplete.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
// WARNING-NEXT: -Wmain-return-type
// WARNING-NEXT: -Wmalformed-warning-check
// WARNING-NEXT: -Wmany-braces-around-scalar-init
// WARNING-NEXT: -Wmath-errno-enabled-with-veclib
// WARNING-NEXT: -Wmathematical-notation-identifier-extension
// WARNING-NEXT: -Wmax-tokens
// WARNING-NEXT: -Wmax-unsigned-zero
Expand Down
53 changes: 53 additions & 0 deletions clang/test/Driver/fveclib.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,56 @@

// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=ArmPL -flto %s 2>&1 | FileCheck --check-prefix=CHECK-LTO-ARMPL %s
// CHECK-LTO-ARMPL: "-plugin-opt=-vector-library=ArmPL"


/* Verify that -fmath-errno is set correctly for the vector library. */

// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fveclib=LIBMVEC %s 2>&1 | FileCheck --check-prefix=CHECK-ERRNO-LIBMVEC %s
// CHECK-ERRNO-LIBMVEC: "-fveclib=LIBMVEC"
// CHECK-ERRNO-LIBMVEC-SAME: "-fmath-errno"

// RUN: %clang -### --target=powerpc64-unknown-linux-gnu -fveclib=MASSV %s 2>&1 | FileCheck --check-prefix=CHECK-ERRNO-MASSV %s
// CHECK-ERRNO-MASSV: "-fveclib=MASSV"
// CHECK-ERRNO-MASSV-SAME: "-fmath-errno"

// RUN: %clang -### --target=x86_64-unknown-linux-gnu -fveclib=SVML %s 2>&1 | FileCheck --check-prefix=CHECK-ERRNO-SVML %s
// CHECK-ERRNO-SVML: "-fveclib=SVML"
// CHECK-ERRNO-SVML-SAME: "-fmath-errno"

// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=SLEEF %s 2>&1 | FileCheck --check-prefix=CHECK-ERRNO-SLEEF %s
// CHECK-ERRNO-SLEEF-NOT: "-fmath-errno"
// CHECK-ERRNO-SLEEF: "-fveclib=SLEEF"
// CHECK-ERRNO-SLEEF-NOT: "-fmath-errno"

// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=ArmPL %s 2>&1 | FileCheck --check-prefix=CHECK-ERRNO-ARMPL %s
// CHECK-ERRNO-ARMPL-NOT: "-fmath-errno"
// CHECK-ERRNO-ARMPL: "-fveclib=ArmPL"
// CHECK-ERRNO-ARMPL-NOT: "-fmath-errno"

// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=ArmPL -fmath-errno %s 2>&1 | FileCheck --check-prefix=CHECK-REENABLE-ERRNO-ARMPL %s
// 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]
// CHECK-REENABLE-ERRNO-ARMPL: "-fveclib=ArmPL"
// CHECK-REENABLE-ERRNO-ARMPL-SAME: "-fmath-errno"

// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=SLEEF -fmath-errno %s 2>&1 | FileCheck --check-prefix=CHECK-REENABLE-ERRNO-SLEEF %s
// 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]
// CHECK-REENABLE-ERRNO-SLEEF: "-fveclib=SLEEF"
// CHECK-REENABLE-ERRNO-SLEEF-SAME: "-fmath-errno"

// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=ArmPL -fno-fast-math %s 2>&1 | FileCheck --check-prefix=CHECK-REENABLE-ERRNO-NFM %s
// 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]
// CHECK-REENABLE-ERRNO-NFM: "-fveclib=ArmPL"
// CHECK-REENABLE-ERRNO-NFM-SAME: "-fmath-errno"

// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=ArmPL -ffp-model=strict %s 2>&1 | FileCheck --check-prefix=CHECK-REENABLE-ERRNO-FP-MODEL %s
// 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]
// CHECK-REENABLE-ERRNO-FP-MODEL: "-fveclib=ArmPL"
// CHECK-REENABLE-ERRNO-FP-MODEL-SAME: "-fmath-errno"

/* Verify the warning points at the last arg to enable -fmath-errno. */
// 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
// 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]

/* Verify no warning when math-errno is re-enabled for a different veclib (that does not imply -fno-math-errno). */
// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=ArmPL -fmath-errno -fveclib=LIBMVEC %s 2>&1 | FileCheck --check-prefix=CHECK-REPEAT-VECLIB %s
// CHECK-REPEAT-VECLIB-NOT: math errno enabled

0 comments on commit b669728

Please sign in to comment.