Skip to content

Commit 9b8f227

Browse files
committed
Add clang warning
1 parent a8495da commit 9b8f227

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,9 @@ 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_reenabled_after_veclib: Warning<
506+
"math errno re-enabled by '%0' after it was implicitly disabled by '%1',"
507+
" this may prevent vectorization with the specified vector library">;
505508

506509
def note_drv_command_failed_diag_msg : Note<
507510
"diagnostic msg: %0">;

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 21 additions & 2 deletions
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"
@@ -2857,6 +2858,10 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
28572858
// List of veclibs which when used with -fveclib imply -fno-math-errno.
28582859
constexpr std::array VecLibImpliesNoMathErrno{llvm::StringLiteral("ArmPL"),
28592860
llvm::StringLiteral("SLEEF")};
2861+
bool NoMathErrnoWasImpliedByVecLib = false;
2862+
const Arg *VecLibArg = nullptr;
2863+
// Track the arg (if any) that reenabled errno after -fveclib for diagnostics.
2864+
const Arg *ArgThatReenabledMathErrnoAfterVecLib = nullptr;
28602865

28612866
// Handle various floating point optimization flags, mapping them to the
28622867
// appropriate LLVM code generation flags. This is complicated by several
@@ -2964,6 +2969,12 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
29642969
}
29652970

29662971
for (const Arg *A : Args) {
2972+
auto CheckMathErrnoForVecLib =
2973+
llvm::make_scope_exit([&, MathErrnoBeforeArg = MathErrno] {
2974+
if (NoMathErrnoWasImpliedByVecLib && !MathErrnoBeforeArg && MathErrno)
2975+
ArgThatReenabledMathErrnoAfterVecLib = A;
2976+
});
2977+
29672978
switch (A->getOption().getID()) {
29682979
// If this isn't an FP option skip the claim below
29692980
default: continue;
@@ -3130,8 +3141,11 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
31303141
FPExceptionBehavior = "strict";
31313142
break;
31323143
case options::OPT_fveclib:
3133-
if (llvm::is_contained(VecLibImpliesNoMathErrno, A->getValue()))
3144+
VecLibArg = A;
3145+
if (llvm::is_contained(VecLibImpliesNoMathErrno, A->getValue())) {
31343146
MathErrno = false;
3147+
NoMathErrnoWasImpliedByVecLib = true;
3148+
}
31353149
break;
31363150
case options::OPT_fno_trapping_math:
31373151
if (!TrappingMathPresent && !FPExceptionBehavior.empty() &&
@@ -3346,8 +3360,13 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
33463360
if (ApproxFunc)
33473361
CmdArgs.push_back("-fapprox-func");
33483362

3349-
if (MathErrno)
3363+
if (MathErrno) {
33503364
CmdArgs.push_back("-fmath-errno");
3365+
if (NoMathErrnoWasImpliedByVecLib)
3366+
D.Diag(clang::diag::warn_drv_math_errno_reenabled_after_veclib)
3367+
<< ArgThatReenabledMathErrnoAfterVecLib->getAsString(Args)
3368+
<< VecLibArg->getAsString(Args);
3369+
}
33513370

33523371
if (AssociativeMath && ReciprocalMath && !SignedZeros && ApproxFunc &&
33533372
!TrappingMath)

clang/test/Driver/fveclib.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,22 @@
7575
// CHECK-ERRNO-ARMPL: "-fveclib=ArmPL"
7676
// CHECK-ERRNO-ARMPL-NOT: "-fmath-errno"
7777

78-
// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=ArmPL -fmath-errno %s 2>&1 | FileCheck --check-prefix=CHECK-FORCE-ERRNO-ARMPL %s
79-
// CHECK-FORCE-ERRNO-ARMPL: "-fveclib=ArmPL"
80-
// CHECK-FORCE-ERRNO-ARMPL-SAME: "-fmath-errno"
81-
82-
// RUN: %clang -### --target=aarch64-linux-gnu -fveclib=SLEEF -fmath-errno %s 2>&1 | FileCheck --check-prefix=CHECK-FORCE-ERRNO-SLEEF %s
83-
// CHECK-FORCE-ERRNO-SLEEF: "-fveclib=SLEEF"
84-
// CHECK-FORCE-ERRNO-SLEEF-SAME: "-fmath-errno"
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 re-enabled by '-fmath-errno' after it was implicitly disabled by '-fveclib=ArmPL', this may prevent vectorization with the specified vector library
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 re-enabled by '-fmath-errno' after it was implicitly disabled by '-fveclib=SLEEF', this may prevent vectorization with the specified vector library
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 re-enabled by '-fno-fast-math' after it was implicitly disabled by '-fveclib=ArmPL', this may prevent vectorization with the specified vector library
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 re-enabled by '-ffp-model=strict' after it was implicitly disabled by '-fveclib=ArmPL', this may prevent vectorization with the specified vector library
95+
// CHECK-REENABLE-ERRNO-FP-MODEL: "-fveclib=ArmPL"
96+
// CHECK-REENABLE-ERRNO-FP-MODEL-SAME: "-fmath-errno"

0 commit comments

Comments
 (0)