Skip to content

Commit

Permalink
Add clang warning
Browse files Browse the repository at this point in the history
  • Loading branch information
MacDue committed Oct 17, 2024
1 parent 75bcb54 commit cab134c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/DiagnosticDriverKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,9 @@ 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_reenabled_after_veclib: Warning<
"math errno re-enabled by '%0' after it was implicitly disabled by '%1',"
" this may prevent vectorization with the specified vector library">;

def note_drv_command_failed_diag_msg : Note<
"diagnostic msg: %0">;
Expand Down
23 changes: 21 additions & 2 deletions 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 @@ -2857,6 +2858,10 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
// 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 reenabled errno after -fveclib for diagnostics.
const Arg *ArgThatReenabledMathErrnoAfterVecLib = nullptr;

// Handle various floating point optimization flags, mapping them to the
// appropriate LLVM code generation flags. This is complicated by several
Expand Down Expand Up @@ -2964,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)
ArgThatReenabledMathErrnoAfterVecLib = A;
});

switch (A->getOption().getID()) {
// If this isn't an FP option skip the claim below
default: continue;
Expand Down Expand Up @@ -3130,8 +3141,11 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
FPExceptionBehavior = "strict";
break;
case options::OPT_fveclib:
if (llvm::is_contained(VecLibImpliesNoMathErrno, A->getValue()))
VecLibArg = A;
if (llvm::is_contained(VecLibImpliesNoMathErrno, A->getValue())) {
MathErrno = false;
NoMathErrnoWasImpliedByVecLib = true;
}
break;
case options::OPT_fno_trapping_math:
if (!TrappingMathPresent && !FPExceptionBehavior.empty() &&
Expand Down Expand Up @@ -3346,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_reenabled_after_veclib)
<< ArgThatReenabledMathErrnoAfterVecLib->getAsString(Args)
<< VecLibArg->getAsString(Args);
}

if (AssociativeMath && ReciprocalMath && !SignedZeros && ApproxFunc &&
!TrappingMath)
Expand Down
26 changes: 19 additions & 7 deletions clang/test/Driver/fveclib.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,22 @@
// 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-FORCE-ERRNO-ARMPL %s
// CHECK-FORCE-ERRNO-ARMPL: "-fveclib=ArmPL"
// CHECK-FORCE-ERRNO-ARMPL-SAME: "-fmath-errno"

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

0 comments on commit cab134c

Please sign in to comment.