Skip to content

Commit

Permalink
[NFC] Refactor fast-math handling for clang driver (#81173)
Browse files Browse the repository at this point in the history
This refactors the fast-math handling in the clang driver, moving the
settings into a lambda that is shared by the -ffp-model=fast and
-ffast-math code. Previously the -ffp-model=fast handler changed the
local option variable and fell through to the -ffast-math handler.

This refactoring is intended to prepare the way for decoupling the
-ffp-model=fast settings from the -ffast-math settings and possibly
introduce a less aggressive fp-model.
  • Loading branch information
andykaylor committed Feb 12, 2024
1 parent 2fcfc97 commit 73159a9
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2778,6 +2778,26 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
LangOptions::ComplexRangeKind Range = LangOptions::ComplexRangeKind::CX_None;
std::string ComplexRangeStr = "";

// Lambda to set fast-math options. This is also used by -ffp-model=fast
auto applyFastMath = [&]() {
HonorINFs = false;
HonorNaNs = false;
MathErrno = false;
AssociativeMath = true;
ReciprocalMath = true;
ApproxFunc = true;
SignedZeros = false;
TrappingMath = false;
RoundingFPMath = false;
FPExceptionBehavior = "";
// If fast-math is set then set the fp-contract mode to fast.
FPContract = "fast";
// ffast-math enables limited range rules for complex multiplication and
// division.
Range = LangOptions::ComplexRangeKind::CX_Limited;
SeenUnsafeMathModeOption = true;
};

if (const Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
CmdArgs.push_back("-mlimit-float-precision");
CmdArgs.push_back(A->getValue());
Expand Down Expand Up @@ -2842,9 +2862,8 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
<< Args.MakeArgString("-ffp-model=" + FPModel)
<< Args.MakeArgString("-ffp-model=" + Val);
if (Val.equals("fast")) {
optID = options::OPT_ffast_math;
FPModel = Val;
FPContract = "fast";
applyFastMath();
} else if (Val.equals("precise")) {
optID = options::OPT_ffp_contract;
FPModel = Val;
Expand Down Expand Up @@ -3061,22 +3080,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
continue;
[[fallthrough]];
case options::OPT_ffast_math: {
HonorINFs = false;
HonorNaNs = false;
MathErrno = false;
AssociativeMath = true;
ReciprocalMath = true;
ApproxFunc = true;
SignedZeros = false;
TrappingMath = false;
RoundingFPMath = false;
FPExceptionBehavior = "";
// If fast-math is set then set the fp-contract mode to fast.
FPContract = "fast";
SeenUnsafeMathModeOption = true;
// ffast-math enables fortran rules for complex multiplication and
// division.
Range = LangOptions::ComplexRangeKind::CX_Limited;
applyFastMath();
break;
}
case options::OPT_fno_fast_math:
Expand Down

0 comments on commit 73159a9

Please sign in to comment.