-
Notifications
You must be signed in to change notification settings - Fork 13.7k
[CodeGen] Provide MachineFunction::hasUnsafeFPMath
#127488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
paperchalice
wants to merge
1
commit into
llvm:main
Choose a base branch
from
paperchalice:rm-unsafe-fp-math
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16280,15 +16280,6 @@ ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) { | |
return DAG.getBuildVector(VT, DL, Ops); | ||
} | ||
|
||
// Returns true if floating point contraction is allowed on the FMUL-SDValue | ||
// `N` | ||
static bool isContractableFMUL(const TargetOptions &Options, SDValue N) { | ||
assert(N.getOpcode() == ISD::FMUL); | ||
|
||
return Options.AllowFPOpFusion == FPOpFusion::Fast || Options.UnsafeFPMath || | ||
N->getFlags().hasAllowContract(); | ||
} | ||
|
||
// Returns true if `N` can assume no infinities involved in its computation. | ||
static bool hasNoInfs(const TargetOptions &Options, SDValue N) { | ||
return Options.NoInfsFPMath || N->getFlags().hasNoInfs(); | ||
|
@@ -16320,8 +16311,9 @@ SDValue DAGCombiner::visitFADDForFMACombine(SDNode *N) { | |
if (!HasFMAD && !HasFMA) | ||
return SDValue(); | ||
|
||
bool AllowFusionGlobally = (Options.AllowFPOpFusion == FPOpFusion::Fast || | ||
Options.UnsafeFPMath || HasFMAD); | ||
bool UnsafeFPMath = DAG.getMachineFunction().hasUnsafeFPMath(); | ||
bool AllowFusionGlobally = | ||
(Options.AllowFPOpFusion == FPOpFusion::Fast || UnsafeFPMath || HasFMAD); | ||
// If the addition is not contractable, do not combine. | ||
if (!AllowFusionGlobally && !N->getFlags().hasAllowContract()) | ||
return SDValue(); | ||
|
@@ -16379,8 +16371,7 @@ SDValue DAGCombiner::visitFADDForFMACombine(SDNode *N) { | |
// fadd (G, (fma A, B, (fma (C, D, (fmul (E, F)))))) --> | ||
// fma A, B, (fma C, D, fma (E, F, G)). | ||
// This requires reassociation because it changes the order of operations. | ||
bool CanReassociate = | ||
Options.UnsafeFPMath || N->getFlags().hasAllowReassociation(); | ||
bool CanReassociate = UnsafeFPMath || N->getFlags().hasAllowReassociation(); | ||
if (CanReassociate) { | ||
SDValue FMA, E; | ||
if (isFusedOp(N0) && N0.hasOneUse()) { | ||
|
@@ -16558,8 +16549,9 @@ SDValue DAGCombiner::visitFSUBForFMACombine(SDNode *N) { | |
return SDValue(); | ||
|
||
const SDNodeFlags Flags = N->getFlags(); | ||
bool AllowFusionGlobally = (Options.AllowFPOpFusion == FPOpFusion::Fast || | ||
Options.UnsafeFPMath || HasFMAD); | ||
bool UnsafeFPMath = DAG.getMachineFunction().hasUnsafeFPMath(); | ||
bool AllowFusionGlobally = | ||
(Options.AllowFPOpFusion == FPOpFusion::Fast || UnsafeFPMath || HasFMAD); | ||
|
||
// If the subtraction is not contractable, do not combine. | ||
if (!AllowFusionGlobally && !N->getFlags().hasAllowContract()) | ||
|
@@ -16714,8 +16706,8 @@ SDValue DAGCombiner::visitFSUBForFMACombine(SDNode *N) { | |
} | ||
} | ||
|
||
auto isReassociable = [&Options](SDNode *N) { | ||
return Options.UnsafeFPMath || N->getFlags().hasAllowReassociation(); | ||
auto isReassociable = [UnsafeFPMath](SDNode *N) { | ||
return UnsafeFPMath || N->getFlags().hasAllowReassociation(); | ||
}; | ||
|
||
auto isContractableAndReassociableFMUL = [&isContractableFMUL, | ||
|
@@ -16729,7 +16721,7 @@ SDValue DAGCombiner::visitFSUBForFMACombine(SDNode *N) { | |
|
||
// More folding opportunities when target permits. | ||
if (Aggressive && isReassociable(N)) { | ||
bool CanFuse = Options.UnsafeFPMath || N->getFlags().hasAllowContract(); | ||
bool CanFuse = UnsafeFPMath || N->getFlags().hasAllowContract(); | ||
// fold (fsub (fma x, y, (fmul u, v)), z) | ||
// -> (fma x, y (fma u, v, (fneg z))) | ||
if (CanFuse && isFusedOp(N0) && | ||
|
@@ -16878,6 +16870,16 @@ SDValue DAGCombiner::visitFMULForFMADistributiveCombine(SDNode *N) { | |
if (!hasNoInfs(Options, FAdd)) | ||
return SDValue(); | ||
|
||
// Returns true if floating point contraction is allowed on the FMUL-SDValue | ||
// `N` | ||
auto isContractableFMUL = [this](const TargetOptions &Options, SDValue N) { | ||
assert(N.getOpcode() == ISD::FMUL); | ||
|
||
return Options.AllowFPOpFusion == FPOpFusion::Fast || | ||
DAG.getMachineFunction().hasUnsafeFPMath() || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is one of the most egregious uses of the unsafe-fp-math. It is interpreted inconsistently and confusingly. In this case, we already have the global fusion option and the contract flag. In a separate PR, can you drop the usage of unsafe fp math here? |
||
N->getFlags().hasAllowContract(); | ||
}; | ||
|
||
// Floating-point multiply-add without intermediate rounding. | ||
bool HasFMA = | ||
isContractableFMUL(Options, SDValue(N, 0)) && | ||
|
@@ -16886,7 +16888,7 @@ SDValue DAGCombiner::visitFMULForFMADistributiveCombine(SDNode *N) { | |
|
||
// Floating-point multiply-add with intermediate rounding. This can result | ||
// in a less precise result due to the changed rounding order. | ||
bool HasFMAD = Options.UnsafeFPMath && | ||
bool HasFMAD = DAG.getMachineFunction().hasUnsafeFPMath() && | ||
(LegalOperations && TLI.isFMADLegal(DAG, N)); | ||
|
||
// No valid opcode, do not combine. | ||
|
@@ -16971,6 +16973,7 @@ SDValue DAGCombiner::visitFADD(SDNode *N) { | |
SDValue N1 = N->getOperand(1); | ||
bool N0CFP = DAG.isConstantFPBuildVectorOrConstantFP(N0); | ||
bool N1CFP = DAG.isConstantFPBuildVectorOrConstantFP(N1); | ||
bool UnsafeFPMath = DAG.getMachineFunction().hasUnsafeFPMath(); | ||
EVT VT = N->getValueType(0); | ||
SDLoc DL(N); | ||
const TargetOptions &Options = DAG.getTarget().Options; | ||
|
@@ -17052,7 +17055,7 @@ SDValue DAGCombiner::visitFADD(SDNode *N) { | |
// If 'unsafe math' or reassoc and nsz, fold lots of things. | ||
// TODO: break out portions of the transformations below for which Unsafe is | ||
// considered and which do not require both nsz and reassoc | ||
if (((Options.UnsafeFPMath && Options.NoSignedZerosFPMath) || | ||
if (((UnsafeFPMath && Options.NoSignedZerosFPMath) || | ||
(Flags.hasAllowReassociation() && Flags.hasNoSignedZeros())) && | ||
AllowNewConst) { | ||
// fadd (fadd x, c1), c2 -> fadd x, c1 + c2 | ||
|
@@ -17190,6 +17193,7 @@ SDValue DAGCombiner::visitFSUB(SDNode *N) { | |
const TargetOptions &Options = DAG.getTarget().Options; | ||
const SDNodeFlags Flags = N->getFlags(); | ||
SelectionDAG::FlagInserter FlagsInserter(DAG, N); | ||
bool UnsafeFPMath = DAG.getMachineFunction().hasUnsafeFPMath(); | ||
|
||
if (SDValue R = DAG.simplifyFPBinop(N->getOpcode(), N0, N1, Flags)) | ||
return R; | ||
|
@@ -17239,7 +17243,7 @@ SDValue DAGCombiner::visitFSUB(SDNode *N) { | |
} | ||
} | ||
|
||
if (((Options.UnsafeFPMath && Options.NoSignedZerosFPMath) || | ||
if (((UnsafeFPMath && Options.NoSignedZerosFPMath) || | ||
(Flags.hasAllowReassociation() && Flags.hasNoSignedZeros())) && | ||
N1.getOpcode() == ISD::FADD) { | ||
// X - (X + Y) -> -Y | ||
|
@@ -17376,7 +17380,6 @@ SDValue DAGCombiner::visitFMUL(SDNode *N) { | |
ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, true); | ||
EVT VT = N->getValueType(0); | ||
SDLoc DL(N); | ||
const TargetOptions &Options = DAG.getTarget().Options; | ||
const SDNodeFlags Flags = N->getFlags(); | ||
SelectionDAG::FlagInserter FlagsInserter(DAG, N); | ||
|
||
|
@@ -17400,7 +17403,8 @@ SDValue DAGCombiner::visitFMUL(SDNode *N) { | |
if (SDValue NewSel = foldBinOpIntoSelect(N)) | ||
return NewSel; | ||
|
||
if (Options.UnsafeFPMath || Flags.hasAllowReassociation()) { | ||
if (DAG.getMachineFunction().hasUnsafeFPMath() || | ||
Flags.hasAllowReassociation()) { | ||
// fmul (fmul X, C1), C2 -> fmul X, C1 * C2 | ||
if (DAG.isConstantFPBuildVectorOrConstantFP(N1) && | ||
N0.getOpcode() == ISD::FMUL) { | ||
|
@@ -17526,10 +17530,10 @@ template <class MatchContextClass> SDValue DAGCombiner::visitFMA(SDNode *N) { | |
ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1); | ||
EVT VT = N->getValueType(0); | ||
SDLoc DL(N); | ||
const TargetOptions &Options = DAG.getTarget().Options; | ||
// FMA nodes have flags that propagate to the created nodes. | ||
SelectionDAG::FlagInserter FlagsInserter(DAG, N); | ||
MatchContextClass matcher(DAG, TLI, N); | ||
bool UnsafeFPMath = DAG.getMachineFunction().hasUnsafeFPMath(); | ||
|
||
// Constant fold FMA. | ||
if (SDValue C = | ||
|
@@ -17552,8 +17556,8 @@ template <class MatchContextClass> SDValue DAGCombiner::visitFMA(SDNode *N) { | |
return matcher.getNode(ISD::FMA, DL, VT, NegN0, NegN1, N2); | ||
} | ||
|
||
// FIXME: use fast math flags instead of Options.UnsafeFPMath | ||
if (Options.UnsafeFPMath) { | ||
// FIXME: use fast math flags instead of MachineFunction::hasUnsafeFPMath() | ||
if (UnsafeFPMath) { | ||
if (N0CFP && N0CFP->isZero()) | ||
return N2; | ||
if (N1CFP && N1CFP->isZero()) | ||
|
@@ -17571,8 +17575,7 @@ template <class MatchContextClass> SDValue DAGCombiner::visitFMA(SDNode *N) { | |
!DAG.isConstantFPBuildVectorOrConstantFP(N1)) | ||
return matcher.getNode(ISD::FMA, DL, VT, N1, N0, N2); | ||
|
||
bool CanReassociate = | ||
Options.UnsafeFPMath || N->getFlags().hasAllowReassociation(); | ||
bool CanReassociate = UnsafeFPMath || N->getFlags().hasAllowReassociation(); | ||
if (CanReassociate) { | ||
// (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2) | ||
if (matcher.match(N2, ISD::FMUL) && N0 == N2.getOperand(0) && | ||
|
@@ -17667,7 +17670,7 @@ SDValue DAGCombiner::combineRepeatedFPDivisors(SDNode *N) { | |
// TODO: Limit this transform based on optsize/minsize - it always creates at | ||
// least 1 extra instruction. But the perf win may be substantial enough | ||
// that only minsize should restrict this. | ||
bool UnsafeMath = DAG.getTarget().Options.UnsafeFPMath; | ||
bool UnsafeMath = DAG.getMachineFunction().hasUnsafeFPMath(); | ||
const SDNodeFlags Flags = N->getFlags(); | ||
if (LegalDAG || (!UnsafeMath && !Flags.hasAllowReciprocal())) | ||
return SDValue(); | ||
|
@@ -17744,6 +17747,7 @@ SDValue DAGCombiner::visitFDIV(SDNode *N) { | |
const TargetOptions &Options = DAG.getTarget().Options; | ||
SDNodeFlags Flags = N->getFlags(); | ||
SelectionDAG::FlagInserter FlagsInserter(DAG, N); | ||
bool UnsafeFPMath = DAG.getMachineFunction().hasUnsafeFPMath(); | ||
|
||
if (SDValue R = DAG.simplifyFPBinop(N->getOpcode(), N0, N1, Flags)) | ||
return R; | ||
|
@@ -17774,7 +17778,7 @@ SDValue DAGCombiner::visitFDIV(SDNode *N) { | |
// isn't too nasty (eg NaN, denormal, ...). | ||
if (((st == APFloat::opOK && !Recip.isDenormal()) || | ||
(st == APFloat::opInexact && | ||
(Options.UnsafeFPMath || Flags.hasAllowReciprocal()))) && | ||
(UnsafeFPMath || Flags.hasAllowReciprocal()))) && | ||
(!LegalOperations || | ||
// FIXME: custom lowering of ConstantFP might fail (see e.g. ARM | ||
// backend)... we should handle this gracefully after Legalize. | ||
|
@@ -17785,7 +17789,7 @@ SDValue DAGCombiner::visitFDIV(SDNode *N) { | |
DAG.getConstantFP(Recip, DL, VT)); | ||
} | ||
|
||
if (Options.UnsafeFPMath || Flags.hasAllowReciprocal()) { | ||
if (UnsafeFPMath || Flags.hasAllowReciprocal()) { | ||
// If this FDIV is part of a reciprocal square root, it may be folded | ||
// into a target-specific square root estimate instruction. | ||
if (N1.getOpcode() == ISD::FSQRT) { | ||
|
@@ -17860,7 +17864,7 @@ SDValue DAGCombiner::visitFDIV(SDNode *N) { | |
|
||
// Fold X/Sqrt(X) -> Sqrt(X) | ||
if ((Options.NoSignedZerosFPMath || Flags.hasNoSignedZeros()) && | ||
(Options.UnsafeFPMath || Flags.hasAllowReassociation())) | ||
(UnsafeFPMath || Flags.hasAllowReassociation())) | ||
if (N1.getOpcode() == ISD::FSQRT && N0 == N1.getOperand(0)) | ||
return N1; | ||
|
||
|
@@ -18356,7 +18360,7 @@ SDValue DAGCombiner::visitFP_ROUND(SDNode *N) { | |
// single-step fp_round we want to fold to. | ||
// In other words, double rounding isn't the same as rounding. | ||
// Also, this is a value preserving truncation iff both fp_round's are. | ||
if (DAG.getTarget().Options.UnsafeFPMath || N0IsTrunc) | ||
if (DAG.getMachineFunction().hasUnsafeFPMath() || N0IsTrunc) | ||
return DAG.getNode( | ||
ISD::FP_ROUND, DL, VT, N0.getOperand(0), | ||
DAG.getIntPtrConstant(NIsTrunc && N0IsTrunc, DL, /*isTarget=*/true)); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably should cache this in a bit in the function instead of requerying the attribute on every use, there are a lot of these. (plus we should stop checking the attributes and move to flags, but that's a bigger ask)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change it to a member variable is ok. Currently
resetTargetOptions
has higher priority than--enable-unsafe-fp-math
, this results in some unreliable codes in backends and tests, change it toOptions.UnsafeFPMath || F.getFnAttribute("unsafe-fp-math").getValueAsBool()
break some tests.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does
--enable-unsafe-fp-math
currently setunsafe-fp-math
on all functions?If it does not, then the change will likely affect performance, as unsafe math will only enable unsafe optimizations in explicitly marked functions only. Ignoring global
Options.UnsafeFPMath
may not be the best idea.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--enable-unsafe-fp-math
is always ignored afterresetTargetOptions
, which is called before instruction selection and subtarget info initialization.Only
CodeGen/NVPTX/fast-math.ll
andCodeGen/X86/change-unsafe-fp-math.ll
failed.